src/codegen-{proto,impl}.lisp: Gather `definst' forms together.
[sod] / doc / clang.tex
CommitLineData
dea4d055
MW
1%%% -*-latex-*-
2%%%
1f7d590d 3%%% C language utilities
dea4d055 4%%%
1f7d590d 5%%% (c) 2015 Straylight/Edgeware
dea4d055
MW
6%%%
7
8%%%----- Licensing notice ---------------------------------------------------
9%%%
e0808c47 10%%% This file is part of the Sensible Object Design, an object system for C.
dea4d055
MW
11%%%
12%%% SOD is free software; you can redistribute it and/or modify
13%%% it under the terms of the GNU General Public License as published by
14%%% the Free Software Foundation; either version 2 of the License, or
15%%% (at your option) any later version.
16%%%
17%%% SOD is distributed in the hope that it will be useful,
18%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
19%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20%%% GNU General Public License for more details.
21%%%
22%%% You should have received a copy of the GNU General Public License
23%%% along with SOD; if not, write to the Free Software Foundation,
24%%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
1f7d590d 26\chapter{C language utilities} \label{ch:clang}
dea4d055
MW
27
28%%%--------------------------------------------------------------------------
1f7d590d 29\section{C type representation} \label{sec:clang.c-types}
dea4d055 30
1f7d590d 31\subsection{Overview} \label{sec:clang.c-types.over}
dea4d055
MW
32
33The Sod translator represents C types in a fairly simple and direct way.
34However, because it spends a fair amount of its time dealing with C types, it
35provides a number of useful operations and macros.
36
64d1ecf7 37The class hierarchy is shown in~\xref{fig:codegen.c-types.classes}.
dea4d055
MW
38
39\begin{figure} \centering
40 \parbox{10pt}{\begin{tabbing}
1f7d590d
MW
41 @|c-type| \\ \ind
42 @|qualifiable-c-type| \\ \ind
43 @|simple-c-type| \\ \ind
dea4d055 44 @|c-class-type| \- \\
1f7d590d 45 @|tagged-c-type| \\ \ind
dea4d055
MW
46 @|c-struct-type| \\
47 @|c-union-type| \\
48 @|c-enum-type| \- \\
49 @|c-pointer-type| \- \\
50 @|c-array-type| \\
51 @|c-function-type|
52 \end{tabbing}}
53 \caption{Classes representing C types}
64d1ecf7 54\label{fig:codegen.c-types.classes}
dea4d055
MW
55\end{figure}
56
57C type objects are immutable unless otherwise specified.
58
59\subsubsection{Constructing C type objects}
60There is a constructor function for each non-abstract class of C type object.
61Note, however, that constructor functions need not generate a fresh type
62object if a previously existing type object is suitable. In this case, we
63say that the objects are \emph{interned}. Some constructor functions are
64specified to return interned objects: programs may rely on receiving the same
65(@|eq|) type object for similar (possibly merely @|equal|) arguments. Where
66not specified, clients may still not rely on receiving fresh objects.
67
68A convenient S-expression notation is provided by the @|c-type| macro. Use
69of this macro is merely an abbreviation for corresponding use of the various
70constructor functions, and therefore interns type objects in the same manner.
71The syntax accepted by the macro can be extended in order to support new
72classes: see @|defctype|, @|c-type-alias| and @|define-c-type-syntax|.
73
74The descriptions of each of the various classes include descriptions of the
75initargs which may be passed to @|make-instance| when constructing a new
76instance of the class. However, the constructor functions and S-expression
77syntax are strongly recommended over direct use of @|make-instance|.
78
79\subsubsection{Printing}
80There are two protocols for printing C types. Unfortunately they have
81similar names.
82\begin{itemize}
83\item The @|print-c-type| function prints a C type value using the
84 S-expression notation. It is mainly useful for diagnostic purposes.
85\item The @|pprint-c-type| function prints a C type as a C-syntax
86 declaration.
87\end{itemize}
88Neither generic function defines a default primary method; subclasses of
89@|c-type| must define their own methods in order to print correctly.
90
1f7d590d 91\subsection{The C type root class} \label{sec:clang.c-types.root}
dea4d055
MW
92
93\begin{describe}{cls}{c-type ()}
94 The class @|c-type| marks the root of the built-in C type hierarchy.
95
96 Users may define subclasses of @|c-type|. All non-abstract subclasses must
97 have a primary method defined on @|pprint-c-type|; unless instances of the
98 subclass are interned, a method on @|c-type-equal-p| is also required.
99
100 The class @|c-type| is abstract.
101\end{describe}
102
1f7d590d 103\subsection{C type S-expression notation} \label{sec:clang.c-types.sexp}
dea4d055
MW
104
105The S-expression representation of a type is described syntactically as a
106type specifier. Type specifiers fit into two syntactic categories.
107\begin{itemize}
108\item A \emph{symbolic type specifier} consists of a symbol. It has a
109 single, fixed meaning: if @<name> is a symbolic type specifier, then each
110 use of @<name> in a type specifier evaluates to the same (@|eq|) type
111 object, until the @<name> is redefined.
112\item A \emph{type operator} is a symbol; the corresponding specifier is a
113 list whose @|car| is the operator. The remaining items in the list are
114 arguments to the type operator.
115\end{itemize}
116
1f7d590d 117\begin{describe}{mac}{c-type @<type-spec> @> @<c-type>}
dea4d055
MW
118 Evaluates to a C type object, as described by the type specifier
119 @<type-spec>.
120\end{describe}
121
1f7d590d 122\begin{describe}{mac}
e43d3532
MW
123 {defctype \=@{ @<name> @! (@<name>^+) @} @<type-spec> \+ \\
124 @[[ @|:export| @<export-flag> @]]^* \-
125 \nlret @<names>}
dea4d055
MW
126 Defines a new symbolic type specifier @<name>; if a list of @<name>s is
127 given, then all are defined in the same way. The type constructed by using
128 any of the @<name>s is as described by the type specifier @<type-spec>.
129
130 The resulting type object is constructed once, at the time that the macro
131 expansion is evaluated; the same (@|eq|) value is used each time any
132 @<name> is used in a type specifier.
e43d3532
MW
133
134 A variable named @|c-type-@<name>|, for the first @<name> only, is defined
135 and initialized to contain the C type object so constructed. Altering or
136 binding this name is discouraged.
137
138 If @<export-flag> is true, then the variable name, and all of the @<name>s,
139 are exported from the current package.
dea4d055
MW
140\end{describe}
141
1f7d590d 142\begin{describe}{mac}{c-type-alias @<original> @<alias>^* @> @<aliases>}
dea4d055
MW
143 Defines each @<alias> as being a type operator identical in behaviour to
144 @<original>. If @<original> is later redefined then the behaviour of the
145 @<alias>es changes too.
146\end{describe}
147
1f7d590d 148\begin{describe}{mac}
cac85e0b
MW
149 {define-c-type-syntax @<name> @<lambda-list> \\ \ind
150 @[[ @<declaration>^* @! @<doc-string> @]] \\
151 @<form>^* \-
152 \nlret @<name>}
dea4d055
MW
153 Defines the symbol @<name> as a new type operator. When a list of the form
154 @|(@<name> @<argument>^*)| is used as a type specifier, the @<argument>s
155 are bound to fresh variables according to @<lambda-list> (a destructuring
156 lambda-list) and the @<form>s evaluated in order in the resulting lexical
157 environment as an implicit @|progn|. The value should be a Lisp form which
158 will evaluate to the type specified by the arguments.
159
160 The @<form>s may call @|expand-c-type-spec| in order to recursively expand
161 type specifiers among its arguments.
162\end{describe}
163
1f7d590d 164\begin{describe}{fun}{expand-c-type-spec @<type-spec> @> @<form>}
dea4d055
MW
165 Returns the Lisp form that @|(c-type @<type-spec>)| would expand into.
166\end{describe}
167
1f7d590d
MW
168\begin{describe}{gf}
169 {print-c-type @<stream> @<type> \&optional @<colon> @<atsign>}
dea4d055
MW
170 Print the C type object @<type> to @<stream> in S-expression form. The
171 @<colon> and @<atsign> arguments may be interpreted in any way which seems
172 appropriate: they are provided so that @|print-c-type| may be called via
173 @|format|'s @|\char`\~/\dots/| command; they are not set when
174 @|print-c-type| is called by Sod functions.
175
176 There should be a method defined for every C type class; there is no
177 default method.
178\end{describe}
179
1f7d590d 180\subsection{Comparing C types} \label{sec:clang.c-types.cmp}
dea4d055
MW
181
182It is necessary to compare C types for equality, for example when checking
183argument lists for methods. This is done by @|c-type-equal-p|.
184
1f7d590d
MW
185\begin{describe}{gf}
186 {c-type-equal-p @<c-type>_1 @<c-type>_2 @> @<generalized-boolean>}
187 The generic function @|c-type-equal-p| compares two C types @<c-type>_1 and
188 @<c-type>_2 for equality; it returns true if the two types are equal and
dea4d055
MW
189 false if they are not.
190
191 Two types are equal if they are structurally similar, where this property
192 is defined by methods for each individual class; see the descriptions of
193 the classes for the details.
194
195 The generic function @|c-type-equal-p| uses the @|and| method combination.
196
1f7d590d 197 \begin{describe}{meth}{c-type-equal-p @<c-type>_1 @<c-type>_2}
dea4d055
MW
198 A default primary method for @|c-type-equal-p| is defined. It simply
199 returns @|nil|. This way, methods can specialize on both arguments
200 without fear that a call will fail because no methods are applicable.
201 \end{describe}
1f7d590d 202 \begin{describe}{ar-meth}{c-type-equal-p @<c-type>_1 @<c-type>_2}
dea4d055 203 A default around-method for @|c-type-equal-p| is defined. It returns
1f7d590d
MW
204 true if @<c-type>_1 and @<c-type>_2 are @|eql|; otherwise it delegates to
205 the primary methods. Since several common kinds of C types are interned,
dea4d055
MW
206 this is a common case worth optimizing.
207 \end{describe}
208\end{describe}
209
1f7d590d 210\subsection{Outputting C types} \label{sec:clang.c-types.output}
dea4d055 211
1f7d590d 212\begin{describe}{gf}{pprint-c-type @<c-type> @<stream> @<kernel>}
dea4d055 213 The generic function @|pprint-c-type| pretty-prints to @<stream> a C-syntax
1f7d590d 214 declaration of an object or function of type @<c-type>. The result is
dea4d055
MW
215 written to @<stream>.
216
217 A C declaration has two parts: a sequence of \emph{declaration specifiers}
218 and a \emph{declarator}. The declarator syntax involves parentheses and
219 operators, in order to reflect the operators applicable to the declared
220 variable. For example, the name of a pointer variable is preceded by @`*';
221 the name of an array is followed by dimensions enclosed in @`['\dots @`]'.
222
223 The @<kernel> argument must be a function designator (though see the
224 standard around-method); it is invoked as
225 \begin{quote} \codeface
226 (funcall @<kernel> @<stream> @<priority> @<spacep>)
227 \end{quote}
228 It should write to @<stream> -- which may not be the same stream originally
229 passed into the generic function -- the `kernel' of the declarator, i.e.,
230 the part to which prefix and/or postfix operators are attached to form the
231 full declarator.
232
233 The methods on @|pprint-c-type| specialized for compound types work by
234 recursively calling @|pprint-c-type| on the subtype, passing down a closure
235 which prints the necessary additional declarator operators before calling
236 the original @<kernel> function. The additional arguments @<priority> and
237 @<spacep> support this implementation technique.
238
239 The @<priority> argument describes the surrounding operator context. It is
240 zero if no type operators are directly attached to the kernel (i.e., there
241 are no operators at all, or the kernel is enclosed in parentheses), one if
242 a prefix operator is directly attached, or two if a postfix operator is
243 directly attached. If the @<kernel> function intends to provide its own
244 additional declarator operators, it should check the @<priority> in order
245 to determine whether parentheses are necessary. See also the
246 @|maybe-in-parens| macro (page~\pageref{mac:maybe-in-parens}).
247
248 The @<spacep> argument indicates whether a space needs to be printed in
249 order to separate the declarator from the declaration specifiers. A kernel
250 which contains an identifier should insert a space before the identifier
251 when @<spacep> is non-nil. An `empty' kernel, as found in an abstract
252 declarator (one that specifies no name), looks more pleasing without a
253 trailing space. See also the @|c-type-space| function
254 (page~\pageref{fun:c-type-space}).
255
256 Every concrete subclass of @|c-type| is expected to provide a primary
257 method on this function. There is no default primary method.
258
1f7d590d 259 \begin{describe}{ar-meth}{pprint-c-type @<c-type> @<stream> @<kernel>}
dea4d055
MW
260 A default around method is defined on @|pprint-c-type| which `canonifies'
261 non-function @<kernel> arguments. In particular:
262 \begin{itemize}
263 \item if @<kernel> is nil, then @|pprint-c-type| is called recursively
264 with a @<kernel> function that does nothing; and
265 \item if @<kernel> is any other kind of object, then @|pprint-c-type| is
266 called recursively with a @<kernel> function that prints the object as
267 if by @|princ|, preceded if necessary by space using @|c-type-space|.
268 \end{itemize}
269 \end{describe}
270\end{describe}
271
272\begin{describe}{fun}{c-type-space @<stream>}
273 Writes a space and other pretty-printing instructions to @<stream> in order
274 visually to separate a declarator from the preceding declaration
275 specifiers. The precise details are subject to change.
276\end{describe}
277
1f7d590d 278\begin{describe}{mac}
cac85e0b
MW
279 {maybe-in-parens (@<stream-var> @<guard-form>)
280 @<declaration>^*
281 @<form>^*}
dea4d055
MW
282 The @<guard-form> is evaluated, and then the @<form>s are evaluated in
283 sequence within a pretty-printer logical block writing to the stream named
284 by the symbol @<stream-var>. If the @<guard-form> evaluates to nil, then
285 the logical block has empty prefix and suffix strings; if it evaluates to a
286 non-nil value, then the logical block has prefix and suffix @`(' and @`)'
287 respectively.
288
289 Note that this may cause @<stream> to be bound to a different stream object
290 within the @<form>s.
291\end{describe}
292
293\subsection{Type qualifiers and qualifiable types}
1f7d590d 294\label{sec:clang.ctypes.qual}
dea4d055
MW
295
296\begin{describe}{cls}{qualifiable-c-type (c-type) \&key :qualifiers}
297 The class @|qualifiable-c-type| describes C types which can bear
298 `qualifiers' (\Cplusplus\ calls them `cv-qualifiers'): @|const|,
299 @|restrict| and @|volatile|.
300
301 The @<qualifiers> are a list of keyword symbols @|:const|, @|:restrict| and
302 @|:volatile|. There is no built-in limitation to these particular
303 qualifiers; others keywords may be used, though this isn't recommended.
304
305 Two qualifiable types are equal only if they have \emph{matching
306 qualifiers}: i.e., every qualifier attached to one is also attached to
307 the other: order is not significant, and neither is multiplicity.
308
309 The class @|qualifiable-c-type| is abstract.
310\end{describe}
311
1f7d590d
MW
312\begin{describe}{gf}{c-type-qualifiers @<c-type> @> @<list>}
313 Returns the qualifiers of the @|qualifiable-c-type| instance @<c-type> as
314 an immutable list.
dea4d055
MW
315\end{describe}
316
1f7d590d
MW
317\begin{describe}{fun}{qualify-type @<c-type> @<qualifiers> @> @<c-type>}
318 The argument @<c-type> must be an instance of @|qualifiable-c-type|,
dea4d055
MW
319 currently bearing no qualifiers, and @<qualifiers> a list of qualifier
320 keywords. The result is a C type object like @<c-type> except that it
321 bears the given @<qualifiers>.
322
1f7d590d 323 The @<c-type> is not modified. If @<c-type> is interned, then the returned
dea4d055
MW
324 type will be interned.
325\end{describe}
326
327\begin{describe}{fun}{format-qualifiers @<qualifiers>}
328 Returns a string containing the qualifiers listed in @<qualifiers> in C
329 syntax, with a space after each. In particular, if @<qualifiers> is
330 non-null then the final character of the returned string will be a space.
331\end{describe}
332
1f7d590d 333\subsection{Leaf types} \label{sec:clang.c-types.leaf}
dea4d055
MW
334
335A \emph{leaf type} is a type which is not defined in terms of another type.
336In Sod, the leaf types are
337\begin{itemize}
338\item \emph{simple types}, including builtin types like @|int| and @|char|,
339 as well as type names introduced by @|typename|, because Sod isn't
340 interested in what the type name means, merely that it names a type; and
341\item \emph{tagged types}, i.e., enum, struct and union types which are named
342 by a keyword identifying the kind of type, and a \emph{tag}.
343\end{itemize}
344
345\begin{describe}{cls}{simple-c-type (qualifiable-c-type)
346 \&key :qualifiers :name}
347 The class of `simple types'; an instance denotes the type @<qualifiers>
348 @<name>.
349
350 A simple type object maintains a \emph{name}, which is a string whose
351 contents are the C name for the type. The initarg @|:name| may be used to
352 provide this name when calling @|make-instance|.
353
354 Two simple type objects are equal if and only if they have @|string=| names
355 and matching qualifiers.
356
357 A number of symbolic type specifiers for builtin types are predefined as
64d1ecf7 358 shown in \xref{tab:codegen.c-types.simple}. These are all defined as if by
dea4d055
MW
359 @|define-simple-c-type|, so can be used to construct qualified types.
360\end{describe}
361
362\begin{table}
fcb6c0fb
MW
363 \begin{tabular}[C]{ll} \hlx*{hv}
364 \thd{C type} & \thd{Specifiers} \\ \hlx{vhv}
365 @|void| & @|void| \\ \hlx{v}
a4434457
MW
366 @|_Bool| & @|bool| \\ \hlx{v}
367 @|char| & @|char| \\ \hlx{}
a4434457 368 @|wchar_t| & @|wchar-t| \\ \hlx{v}
d21ac4d9
MW
369 @|signed char| & @|signed-char|, @|schar| \\ \hlx{}
370 @|unsigned char| & @|unsigned-char|, @|uchar| \\ \hlx{v}
dea4d055 371 @|short| & @|short|, @|signed-short|, @|short-int|,
fcb6c0fb 372 @|signed-short-int| @|sshort| \\ \hlx{}
dea4d055 373 @|unsigned short| & @|unsigned-short|, @|unsigned-short-int|,
fcb6c0fb 374 @|ushort| \\ \hlx{v}
dea4d055 375 @|int| & @|int|, @|signed|, @|signed-int|,
fcb6c0fb
MW
376 @|sint| \\ \hlx{}
377 @|unsigned int| & @|unsigned|, @|unsigned-int|, @|uint| \\ \hlx{v}
dea4d055 378 @|long| & @|long|, @|signed-long|, @|long-int|,
fcb6c0fb 379 @|signed-long-int|, @|slong| \\ \hlx{}
dea4d055 380 @|unsigned long| & @|unsigned-long|, @|unsigned-long-int|,
fcb6c0fb 381 @|ulong| \\ \hlx{v}
dea4d055 382 @|long long| & @|long-long|, @|signed-long-long|,
d21ac4d9 383 @|long-long-int|, \\ \hlx{}
dea4d055
MW
384 & \qquad @|signed-long-long-int|,
385 @|llong|, @|sllong| \\ \hlx{v}
386 @|unsigned long long|
387 & @|unsigned-long-long|, @|unsigned-long-long-int|,
fcb6c0fb 388 @|ullong| \\ \hlx{v}
d21ac4d9
MW
389 @|size_t| & @|size-t| \\ \hlx{}
390 @|ptrdiff_t| & @|ptrdiff-t| \\ \hlx{v}
fcb6c0fb 391 @|float| & @|float| \\ \hlx{}
a4434457
MW
392 @|double| & @|double| \\ \hlx{}
393 @|long double| & @|long-double| \\ \hlx{v}
394 @|float _Imaginary| & @|float-imaginary| \\ \hlx{}
a4434457 395 @|double _Imaginary|& @|double-imaginary| \\ \hlx{}
a4434457 396 @|long double _Imaginary|
d21ac4d9
MW
397 & @|long-double-imaginary| \\ \hlx{v}
398 @|float _Complex| & @|float-complex| \\ \hlx{}
399 @|double _Complex| & @|double-complex| \\ \hlx{}
a4434457 400 @|long double _Complex|
d21ac4d9
MW
401 & @|long-double-complex| \\ \hlx{v}
402 @|va_list| & @|va-list| \\ \hlx*{vh}
dea4d055
MW
403 \end{tabular}
404 \caption{Builtin symbolic type specifiers for simple C types}
64d1ecf7 405 \label{tab:codegen.c-types.simple}
dea4d055
MW
406\end{table}
407
1f7d590d
MW
408\begin{describe}{fun}
409 {make-simple-type @<name> \&optional @<qualifiers> @> @<c-type>}
dea4d055
MW
410 Return the (unique interned) simple C type object for the C type whose name
411 is @<name> (a string) and which has the given @<qualifiers> (a list of
412 keywords).
413\end{describe}
414
1f7d590d
MW
415\begin{describe}{gf}{c-type-name @<c-type> @> @<string>}
416 Returns the name of a @|simple-c-type| instance @<c-type> as an immutable
dea4d055
MW
417 string.
418\end{describe}
419
1f7d590d 420\begin{describe}{mac}
e43d3532
MW
421 {define-simple-c-type \=@{ @<name> @! (@<name>^+) @} @<string> \+ \\
422 @[[ @|:export| @<export-flag> @]] \-
423 \nlret @<name>}
dea4d055
MW
424 Define type specifiers for a new simple C type. Each symbol @<name> is
425 defined as a symbolic type specifier for the (unique interned) simple C
426 type whose name is the value of @<string>. Further, each @<name> is
427 defined to be a type operator: the type specifier @|(@<name>
428 @<qualifier>^*)| evaluates to the (unique interned) simple C type whose
429 name is @<string> and which has the @<qualifiers> (which are evaluated).
e43d3532
MW
430
431 Furthermore, a variable @|c-type-@<name>| is defined, for the first @<name>
432 only, and initialized with the newly constructed C type object.
433
434 If @<export-flag> is true, then the @|c-type-@<name>| variable name, and
435 all of the @<name>s, are exported from the current package.
dea4d055
MW
436\end{describe}
437
438\begin{describe}{cls}{tagged-c-type (qualifiable-c-type)
439 \&key :qualifiers :tag}
440 Provides common behaviour for C tagged types. A @<tag> is a string
441 containing a C identifier.
442
443 Two tagged types are equal if and only if they have the same class, their
444 @<tag>s are @|string=|, and they have matching qualifiers. (User-defined
445 subclasses may have additional methods on @|c-type-equal-p| which impose
446 further restrictions.)
447\end{describe}
448\begin{boxy}[Bug]
449 Sod maintains distinct namespaces for the three kinds of tagged types. In
450 C, there is only one namespace for tags which is shared between enums,
451 structs and unions.
452\end{boxy}
453
1f7d590d
MW
454\begin{describe}{gf}{c-tagged-type-kind @<c-type> @> @<keyword>}
455 Returns a keyword classifying the tagged @<c-type>: one of @|:enum|,
456 @|:struct| or @|:union|. User-defined subclasses of @|tagged-c-type|
457 should return their own classification symbols. It is intended that
458 @|(string-downcase (c-tagged-type-kind @<c-type>))| be valid C
459 syntax.\footnote{%
dea4d055
MW
460 Alas, C doesn't provide a syntactic category for these keywords;
461 \Cplusplus\ calls them a @<class-key>.} %
1f7d590d
MW
462 There is a method defined for each of the built-in tagged type classes
463 @|c-struct-type|, @|c-union-type| and @|c-enum-type|.
464\end{describe}
465
466\begin{describe}{gf}{kind-c-tagged-type @<keyword> @> @<symbol>}
467 This is not quite the inverse of @|c-tagged-type-kind|. Given a keyword
468 naming a kind of tagged type, return the name of the corresponding C
469 type class as a symbol.
dea4d055
MW
470\end{describe}
471
472\begin{describe}{cls}{c-enum-type (tagged-c-type) \&key :qualifiers :tag}
473 Represents a C enumerated type. An instance denotes the C type @|enum|
474 @<tag>. See the direct superclass @|tagged-c-type| for details.
475
476 The type specifier @|(enum @<tag> @<qualifier>^*)| returns the (unique
477 interned) enumerated type with the given @<tag> and @<qualifier>s (all
478 evaluated).
479\end{describe}
1f7d590d
MW
480\begin{describe}{fun}
481 {make-enum-type @<tag> \&optional @<qualifiers> @> @<c-enum-type>}
dea4d055
MW
482 Return the (unique interned) C type object for the enumerated C type whose
483 tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
484 keywords).
485\end{describe}
486
487\begin{describe}{cls}{c-struct-type (tagged-c-type) \&key :qualifiers :tag}
488 Represents a C structured type. An instance denotes the C type @|struct|
489 @<tag>. See the direct superclass @|tagged-c-type| for details.
490
491 The type specifier @|(struct @<tag> @<qualifier>^*)| returns the (unique
492 interned) structured type with the given @<tag> and @<qualifier>s (all
493 evaluated).
494\end{describe}
1f7d590d
MW
495\begin{describe}{fun}
496 {make-struct-type @<tag> \&optional @<qualifiers> @> @<c-struct-type>}
dea4d055
MW
497 Return the (unique interned) C type object for the structured C type whose
498 tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
499 keywords).
500\end{describe}
501
502\begin{describe}{cls}{c-union-type (tagged-c-type) \&key :qualifiers :tag}
503 Represents a C union type. An instance denotes the C type @|union|
504 @<tag>. See the direct superclass @|tagged-c-type|
505 for details.
506
507 The type specifier @|(union @<tag> @<qualifier>^*)| returns the (unique
508 interned) union type with the given @<tag> and @<qualifier>s (all
509 evaluated).
510\end{describe}
1f7d590d
MW
511\begin{describe}{fun}
512 {make-union-type @<tag> \&optional @<qualifiers> @> @<c-union-type>}
dea4d055
MW
513 Return the (unique interned) C type object for the union C type whose tag
514 is @<tag> (a string) and which has the given @<qualifiers> (a list of
515 keywords).
516\end{describe}
517
1f7d590d
MW
518\subsection{Compound C types} \label{sec:code.c-types.compound}
519
520Some C types are \emph{compound types}: they're defined in terms of existing
521types. The classes which represent compound types implement a common
522protocol.
dea4d055 523
1f7d590d
MW
524\begin{describe}{gf}{c-type-subtype @<c-type> @> @<subtype>}
525 Returns the underlying type of a compound type @<c-type>. Precisely what
526 this means depends on the class of @<c-type>.
dea4d055
MW
527\end{describe}
528
1f7d590d
MW
529\subsection{Pointer types} \label{sec:clang.c-types.pointer}
530
531Pointers compound types. The subtype of a pointer type is the type it points
532to.
533
534\begin{describe}{cls}
535 {c-pointer-type (qualifiable-c-type) \&key :qualifiers :subtype}
dea4d055
MW
536 Represents a C pointer type. An instance denotes the C type @<subtype>
537 @|*|@<qualifiers>.
538
539 The @<subtype> may be any C type. Two pointer types are equal if and only
540 if their subtypes are equal and they have matching qualifiers.
541
542 The type specifier @|(* @<type-spec> @<qualifier>^*)| returns a type
543 qualified pointer-to-@<subtype>, where @<subtype> is the type specified by
544 @<type-spec> and the @<qualifier>s are qualifier keywords (which are
545 evaluated). The synonyms @|ptr| and @|pointer| may be used in place of the
546 star @`*'.
547
fcb6c0fb 548 The symbol @|string| is a type specifier for the type pointer to
dea4d055
MW
549 characters; the symbol @|const-string| is a type specifier for the type
550 pointer to constant characters.
551\end{describe}
1f7d590d
MW
552
553\begin{describe}{fun}
554 {make-pointer-type @<c-type> \&optional @<qualifiers>
555 @> @<c-pointer-type>}
fcb6c0fb 556 Return an object describing the type qualified pointer to @<subtype>.
dea4d055
MW
557 If @<subtype> is interned, then the returned pointer type object is
558 interned also.
559\end{describe}
560
1f7d590d
MW
561\subsection{Array types} \label{sec:clang.c-types.array}
562
fcb6c0fb
MW
563Arrays implement the compound-type protocol. The subtype of an array type is
564the array element type.
1f7d590d 565
dea4d055
MW
566\begin{describe}{cls}{c-array-type (c-type) \&key :subtype :dimensions}
567 Represents a multidimensional C array type. The @<dimensions> are a list
568 of dimension specifiers $d_0$, $d_1$, \ldots, $d_{n-1}$; an instance then
569 denotes the C type @<subtype> @|[$d_0$][$d_1$]$\ldots$[$d_{n-1}$]|. An
570 individual dimension specifier is either a string containing a C integral
571 constant expression, or nil which is equivalent to an empty string. Only
572 the first (outermost) dimension $d_0$ should be empty.
573
574 C doesn't actually have multidimensional arrays as a primitive notion;
575 rather, it permits an array (with known extent) to be the element type of
576 an array, which achieves an equivalent effect. C arrays are stored in
577 row-major order: i.e., if we write down the indices of the elements of an
578 array in order of ascending address, the rightmost index varies fastest;
579 hence, the type constructed is more accurately an array of $d_0$ arrays of
580 $d_1$ of \ldots\ arrays of $d_{n-1}$ elements of type @<subtype>. We shall
581 continue to abuse terminology and refer to multidimensional arrays.
582
583 The type specifier @|([] @<type-spec> @<dimension>^*)| constructs a
584 multidimensional array with the given @<dimension>s whose elements have the
585 type specified by @<type-spec>. If no dimensions are given then a
586 single-dimensional array with unspecified extent. The synonyms @|array|
587 and @|vector| may be used in place of the brackets @`[]'.
588\end{describe}
1f7d590d
MW
589
590\begin{describe}{fun}
591 {make-array-type @<subtype> @<dimensions> @> @<c-array-type>}
dea4d055
MW
592 Return an object describing the type of arrays with given @<dimensions> and
593 with element type @<subtype> (an instance of @|c-type|). The @<dimensions>
594 argument is a list whose elements are strings or nil; see the description
595 of the class @|c-array-type| above for details.
596\end{describe}
1f7d590d
MW
597
598\begin{describe}{gf}{c-array-dimensions @<c-type> @> @<list>}
599 Returns the dimensions of @<c-type>, an array type, as an immutable list.
600\end{describe}
601
602\subsection{Function types} \label{sec:clang.c-types.fun}
603
fcb6c0fb
MW
604Function types implement the compound-type protocol. The subtype of a
605function type is the type of the function's return value.
606
1f7d590d 607\begin{describe}{cls}{argument}
fcb6c0fb 608 Represents an ordinary function argument.
1f7d590d
MW
609\end{describe}
610
611\begin{describe}{fun}{argumentp @<value> @> @<generalized-boolean>}
fcb6c0fb
MW
612 Decide whether @<value> is an @<argument> object: if so, return non-nil; if
613 not return nil.
1f7d590d
MW
614\end{describe}
615
616\begin{describe}{fun}{make-argument @<name> @<c-type> @> @<argument>}
fcb6c0fb
MW
617 Construct and a return a new @<argument> object. The argument has type
618 @<c-type>, which must be a @|c-type| object, and is named @<name>.
619
620 The @<name> may be nil to indicate that the argument has no name: in this
621 case the argument will be formatted as an abstract declarator, which is not
622 suitable for function definitions. If @<name> is not nil, then the
623 @<name>'s print representation, with @|*print-escape*| nil, is used as the
624 argument name.
1f7d590d
MW
625\end{describe}
626
52e2a70f
MW
627\begin{describe*}
628 {\dhead{fun}{argument-name @<argument> @> @<name>}
629 \dhead{fun}{argument-type @<argument> @> @<c-type>}}
630 Accessor functions for @|argument| objects. They return the name (for
631 @|argument-name|) or type (for @|argument-type|) from the object, as passed
632 to @|make-argument|.
633\end{describe*}
dea4d055 634
fcb6c0fb 635\begin{describe}{gf}
1f7d590d 636 {commentify-argument-name @<name> @> @<commentified-name>}
fcb6c0fb
MW
637 Convert the argument name @<name> so that it's suitable to declare the
638 function in a header file.
dea4d055 639
fcb6c0fb
MW
640 Robust header files shouldn't include literal argument names in
641 declarations of functions or function types, since this restricts the
642 including file from defining such names as macros. This generic function
643 is used to convert names into a safe form.
644
645 \begin{describe}{meth}{commentify-argument-name (@<name> null) @> nil}
646 Returns nil: if the argument name is already omitted, it's safe for use
647 in a header file.
648 \end{describe}
649 \begin{describe}{meth}{commentify-argument-name (@<name> t) @> @<string>}
650 Returns the print form of @<name> wrapped in a C comment, as
651 @`/*@<name>*/'.
652 \end{describe}
1f7d590d
MW
653\end{describe}
654
655\begin{describe}{fun}
fcb6c0fb
MW
656 {commentify-argument-names @<arguments> @> @<commentified-arguments>}
657 Convert the @<arguments> list so that it's suitable for use in a header
658 file.
659
660 The @<arguments> list should be a list whose items are @|argument| objects
661 or the keyword @|:ellipsis|. The return value is a list constructed as
662 follows. For each @|argument| object in the input list, there is a
663 corresponding @|argument| object in the returned list, with the same type,
664 and whose name is the result of @|commentify-argument-name| applied to the
665 input argument name; an @|:ellipsis| in the input list is passed through
666 unchanged.
1f7d590d
MW
667\end{describe}
668
fcb6c0fb
MW
669\begin{describe}{cls}{c-function-type (c-type) \&key :subtype :arguments}
670 Represents C function types. An instance denotes the type of a C
671 function which accepts the @<arguments> and returns @<subtype>.
672
673 The @<arguments> are a possibly empty list. All but the last element of
674 the list must be @|argument| objects; the final element may instead be the
675 keyword @|:ellipsis|, which denotes a variable argument list.
676
677 An @<arguments> list consisting of a single argument with type @|void| is
678 converted into an empty list. On output as C code, an empty argument list
679 is written as @|void|. It is not possible to represent a pre-ANSI C
680 function without prototypes.
681
682 Two function types are considered to be the same if their return types are
683 the same, and their argument lists consist of arguments with the same type,
684 in the same order, and either both or neither argument list ends with
685 @|:ellipsis|; argument names are not compared.
686
ed76585e
MW
687 The type specifier
688 \begin{prog}
689 (fun @<return-type>
690 @{ (@<arg-name> @<arg-type>) @}^*
691 @[:ellipsis @! . @<form>@])
692 \end{prog}
693 constructs a function type. The function has the subtype @<return-type>.
694 The remaining items in the type-specifier list are used to construct the
695 argument list. The argument items are a possibly improper list, beginning
696 with zero or more \emph{explicit arguments}: two-item
697 @<arg-name>/@<arg-type> lists. For each such list, an @|argument| object
698 is constructed with the given name (evaluated) and type. Following the
699 explicit arguments, there may be
fcb6c0fb
MW
700 \begin{itemize}
701 \item nothing, in which case the function's argument list consists only of
702 the explicit arguments;
703 \item the keyword @|:ellipsis|, as the final item in the type-specifier
704 list, indicating a variable argument list may follow the explicit
705 arguments; or
706 \item a possibly-improper list tail, beginning with an atom either as a
707 list item or as the final list cdr, indicating that the entire list tail
708 is Lisp expression which is to be evaluated to compute the remaining
709 arguments.
710 \end{itemize}
711 A tail expression may return a list of @|argument| objects, optionally
712 followed by an @|:ellipsis|.
713
714 For example,
715 \begin{prog}
716 (c-type (fun \=(lisp (c-type-subtype other-func)) \+ \\
717 ("first" int) . (c-function-arguments other-func))
718 \end{prog}
719 evaluates to a function type like @|other-func|, only with an additional
720 argument of type @|int| added to the front of its argument list. This
721 could also have been written
722 \begin{prog}
723 (let (\=(args (c-function-arguments other-func)) \+ \\
724 (ret (c-type-subtype other-func))) \- \\ \ind
725 (c-type (fun \=(lisp ret) ("first" int) . args)
726 \end{prog}
1f7d590d
MW
727\end{describe}
728
729\begin{describe}{fun}
fcb6c0fb
MW
730 {make-function-type @<subtype> @<arguments> @> @<c-function-type>}
731 Construct and return a new function type, returning @<subtype> and
732 accepting the @<arguments>.
733\end{describe}
734
735\begin{describe}{gf}
736 {c-function-arguments @<c-function-type> @> @<arguments>}
737 Return the arguments list of the @<c-function-type>.
1f7d590d
MW
738\end{describe}
739
740\begin{describe}{fun}
fcb6c0fb
MW
741 {commentify-function-type @<c-function-type> @> @<commentified-c-type>}
742 Return a commentified version of the @<c-function-type>.
743
744 The returned type has the same subtype as the given type, and the argument
745 list of the returned type is the result of applying
746 @|commentify-argument-names| to the argument list of the given type.
dea4d055
MW
747\end{describe}
748
1f7d590d
MW
749\subsection{Parsing C types} \label{sec:clang.c-types.parsing}
750
756f4928
MW
751\begin{describe}{fun}
752 {parse-c-type @<scanner>
753 @> @<result> @<success-flag> @<consumed-flag>}
754\end{describe}
755
756\begin{describe}{fun}
757 {parse-declarator @<scanner> @<base-type> \&key :kernel :abstractp
758 \nlret @<result> @<success-flag> @<consumed-flag>}
759\end{describe}
760
761\subsection{Class types} \label{sec:clang.c-types.class}
762
763\begin{describe}{cls}
764 {c-class-type (simple-c-type) \&key :class :tag :qualifiers :name}
765\end{describe}
766
767\begin{describe*}
768 {\dhead{gf}{c-type-class @<class-type> @> @<class>}
769 \dhead{gf}{setf (c-type-class @<class-type>) @<class>}}
770\end{describe*}
771
772\begin{describe}{fun}{find-class-type @<name> @> @<class-type-or-nil>}
773\end{describe}
774
775\begin{describe}{fun}
776 {make-class-type @<name> \&optional @<qualifiers> @> @<class-type>}
777\end{describe}
778
779\begin{describe}{fun}
780 {make-class-type @<name> \&optional @<qualifiers> @> @<class-type>}
781\end{describe}
782
783\begin{describe}{fun}{find-sod-class @<name> @> @<class>}
784\end{describe}
785
786\begin{describe}{fun}{record-sod-class @<class>}
787\end{describe}
788
1f7d590d
MW
789%%%--------------------------------------------------------------------------
790\section{Generating C code} \label{sec:clang.codegen}
791
fcb6c0fb
MW
792This section deals with Sod's facilities for constructing and manipulating C
793expressions, declarations, instructions and definitions.
794
795\subsection{Temporary names} \label{sec:clang.codegen.temporaries}
796
797Many C-level objects, especially ones with external linkage or inclusion in a
798header file, are assigned names which are simple strings, perhaps fixed ones,
799perhaps constructed. Other objects don't need meaningful names, and
800suitably unique constructed names would be tedious and most likely rather
801opaque. Therefore Sod has an ability to construct \emph{temporary names}.
802
803These aren't temporary in the sense that they name C objects which have
804limited lifetimes at runtime. Rather, the idea is that the names be
805significant only to small pieces of Lisp code, which will soon forget about
806them.
807
808\subsubsection{The temporary name protocol}
809Temporary names are represented by objects which implement a simple protocol.
810
811\begin{describe}{gf}{format-temporary-name @<var> @<stream>}
812\end{describe}
813
814\begin{describe*}
815 {\dhead{gf}{var-in-use-p @<var> @> @<generalized-boolean>}
816 \dhead[setf var-in-use-p]
817 {gf}{setf (var-in-use-p @<var>) @<generalized-boolean>}}
818\end{describe*}
819
820\subsubsection{Temporary name objects}
821
822\begin{describe}{cls}{temporary-name () \&key :tag}
823 A temporary name object. This is the root of a small collection of
824 subclasses, but is also usable on its own.
825\end{describe}
826
827\begin{describe}{meth}
828 {commentify-argument-name (@<name> temporary-name) @> nil}
829\end{describe}
830
831\begin{table}
832 \begin{tabular}[C]{*2{>{\codeface}l}} \hlx*{hv}
833 \thd{\textbf{Class}} & \thd{\textbf{Name format}} \\ \hlx{vhv}
834 temporary-name & @<tag> \\
835 temporary-argument & sod__a@<tag> \\
836 temporary-function & sod__f@<tag> \\
837 temporary-variable & sod__v@<tag> \\ \hlx*{vh}
838 \end{tabular}
839 \caption{Temporary name formats}
840 \label{tab:codegen.codegen.temps-format}
841\end{table}
842
843\begin{describe}{cls}{temporary-argument (temporary-name) \&key :tag}
844\end{describe}
845
846\begin{describe}{cls}{temporary-function (temporary-name) \&key :tag}
847\end{describe}
848
849\begin{describe}{fun}{temporary-function @> @<name>}
850\end{describe}
851
852\begin{describe}{cls}
853 {temporary-variable (temporary-name) \&key :tag :in-use-p}
854\end{describe}
855
856\subsubsection{Well-known `temporary' names}
857
858\begin{table}
859 \begin{tabular}[C]{*2{>{\codeface}l}} \hlx*{hv}
860 \thd{\textbf{Variable}} & \thd{\textbf{Name format}} \\ \hlx{vhv}
861 {}*sod-ap* & sod__ap \\
0f2c2a9d 862 {}*sod-master-ap* & sod__master_ap \\ \hlx*{vh}
fcb6c0fb
MW
863 \end{tabular}
864 \caption{Well-known temporary names}
865 \label{tab:codegen.codegen.well-known-temps}
866\end{table}
867
868\subsection{Instructions} \label{sec:clang.codegen.insts}
869
870\begin{describe}{cls}{inst () \&key}
871\end{describe}
872
873\begin{describe}{gf}{inst-metric @<inst>}
874\end{describe}
875
876\begin{describe}{mac}
cac85e0b
MW
877 {definst @<code> (@<streamvar> \&key @<export>) (@<arg>^*) \\ \ind
878 @[[ @<declaration>^* @! @<doc-string> @]] \\
879 @<form>^* \-
880 \nlret @<code>}
fcb6c0fb
MW
881\end{describe}
882
883\begin{describe}{mac}
cac85e0b
MW
884 {format-compound-statement
885 (@<stream> @<child> \&optional @<morep>) \\ \ind
886 @<declaration>^* \\
887 @<form>^*}
fcb6c0fb
MW
888\end{describe}
889
890\begin{table}
891 \begin{tabular}[C]{ll>{\codeface}l} \hlx*{hv}
892 \thd{Class name} &
893 \thd{Arguments} &
894 \thd{Output format} \\ \hlx{vhv}
895 @|var| & @<name> @<type> @<init> & @<type> @<name> @[= @<init>@];
896 \\ \hlx{v}
897 @|set| & @<var> @<expr> & @<var> = @<expr>; \\ \hlx{v}
898 @|update| & @<var> @<op> @<expr> & @<var> @<op>= @<expr>;
899 \\ \hlx{v}
900 @|return| & @<expr> & return @[@<expr>@];
901 \\ \hlx{v}
902 @|break| & --- & break; \\ \hlx{v}
903 @|continue| & --- & continue; \\ \hlx{v}
904 @|expr| & @<expr> & @<expr>; \\ \hlx{v}
905 @|call| & @<func> @<args> & @<func>(@<arg>_1,
906 $\ldots$,
b492babc 907 @<arg>_n) \\ \hlx{vhv}
fcb6c0fb
MW
908 @|block| & @<decls> @<body> & \{ @[@<decls>@] @<body> \}
909 \\ \hlx{v}
910 @|if| & @<cond> @<conseq> @<alt> & if (@<cond>) @<conseq>
911 @[else @<alt>@] \\ \hlx{v}
912 @|while| & @<cond> @<body> & while (@<cond>) @<body>
913 \\ \hlx{v}
914 @|do-while| & @<body> @<cond> & do @<body> while (@<cond>);
915 \\ \hlx{v}
916 @|function| & @<name> @<type> @<body> &
917 @<type>_0 @<name>(@<type>_1 @<arg>_1, $\ldots$,
918 @<type>_n @<arg>_n @[, \dots@])
919 @<body> \\ \hlx*{vh}
920 \end{tabular}
921 \caption{Instruction classes}
922 \label{tab:codegen.codegen.insts}
923\end{table}
924
925\subsection{Code generation} \label{sec:clang.codegen.codegen}
926
927\begin{describe}{gf}{codegen-functions @<codegen> @> @<list>}
928\end{describe}
929
930\begin{describe}{gf}
931 {ensure-var @<codegen> @<name> @<type> \&optional @<init>}
932\end{describe}
933
934\begin{describe}{gf}{emit-inst @<codegen> @<inst>}
935\end{describe}
936
937\begin{describe}{gf}{emit-insts @<codegen> @<insts>}
938\end{describe}
939
940\begin{describe}{gf}{emit-decl @<codegen> @<decl>}
941\end{describe}
942
7c3f8ae6 943\begin{describe}{gf}{emit-decls @<codegen> @<decls>}
fcb6c0fb
MW
944\end{describe}
945
946\begin{describe}{gf}{codegen-push @<codegen>}
947\end{describe}
948
949\begin{describe}{gf}{codegen-pop @<codegen> @> @<decls> @<insts>}
950\end{describe}
951
952\begin{describe}{gf}{codegen-pop-block @<codegen> @> @<block-inst>}
953\end{describe}
954
955\begin{describe}{gf}
956 {codegen-pop-function @<codegen> @<name> @<type> @> @<name>}
957\end{describe}
958
959\begin{describe}{gf}{codegen-add-function @<codegen> @<function>}
960\end{describe}
961
962\begin{describe}{fun}
963 {codegen-build-function @<codegen> @<name> @<type> @<vars> @<insts>
964 @> @<name>}
965\end{describe}
966
967\begin{describe}{gf}{temporary-var @<codegen> @<type> @> @<name>}
968\end{describe}
969
970\begin{describe}{mac}
cac85e0b
MW
971 {with-temporary-var (@<codegen> @<var> @<type>) \\ \ind
972 @<declaration>^* \\
973 @<form>^* \-
974 \nlret @<value>^*}
fcb6c0fb
MW
975\end{describe}
976
977\begin{describe}{fun}{deliver-expr @<codegen> @<target> @<expr>}
978\end{describe}
979
357885be
MW
980\begin{describe}{fun}
981 {deliver-call @<codegen> @<target> @<func> \&rest @<args>}
982\end{describe}
983
fcb6c0fb
MW
984\begin{describe}{fun}{convert-stmts @<codegen> @<target> @<type> @<func>}
985\end{describe}
986
987\begin{describe}{cls}{codegen () \&key :vars :insts (:temp-index 0)}
988\end{describe}
989
2c7465ac
MW
990%%%--------------------------------------------------------------------------
991\section{Literal C code fragments} \label{sec:clang.fragment}
992
993\begin{describe}{cls}{c-fragment () \&key :location :text}
994\end{describe}
995
996\begin{describe}{gf}{c-fragment-text @<fragment> @> @<string>}
997\end{describe}
998
999\begin{describe}{fun}
1000 {scan-c-fragment @<scanner> @<end-chars>
1001 @> @<result> @<success-flag> @<consumed-flag>}
1002\end{describe}
1003
1004\begin{describe}{fun}
1005 {parse-delimited-fragment @<scanner> @<begin> @<end> \&key :keep-end
1006 \nlret @<result> @<success-flag> @<consumed-flag>}
1007\end{describe}
1008
dea4d055
MW
1009%%%----- That's all, folks --------------------------------------------------
1010
1011%%% Local variables:
1012%%% mode: LaTeX
1013%%% TeX-master: "sod.tex"
1014%%% TeX-PDF-mode: t
1015%%% End: