chiark / gitweb /
doc/sod.sty, doc/runtime.tex: Improve comment typesetting in code.
[sod] / doc / clang.tex
1 %%% -*-latex-*-
2 %%%
3 %%% C language utilities
4 %%%
5 %%% (c) 2015 Straylight/Edgeware
6 %%%
7
8 %%%----- Licensing notice ---------------------------------------------------
9 %%%
10 %%% This file is part of the Sensible Object Design, an object system for C.
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
26 \chapter{C language utilities} \label{ch:clang}
27
28 %%%--------------------------------------------------------------------------
29 \section{C type representation} \label{sec:clang.c-types}
30
31 \subsection{Overview} \label{sec:clang.c-types.over}
32
33 The Sod translator represents C types in a fairly simple and direct way.
34 However, because it spends a fair amount of its time dealing with C types, it
35 provides a number of useful operations and macros.
36
37 The class hierarchy is shown in~\xref{fig:codegen.c-types.classes}.
38
39 \begin{figure} \centering
40   \parbox{10pt}{\begin{tabbing}
41     @|c-type| \\ \ind
42       @|qualifiable-c-type| \\ \ind
43         @|simple-c-type| \\ \ind
44           @|c-class-type| \- \\
45         @|tagged-c-type| \\ \ind
46           @|c-struct-type| \\
47           @|c-union-type| \\
48           @|c-enum-type| \- \\
49         @|c-atomic-type| \\
50         @|c-pointer-type| \- \\
51       @|c-array-type| \\
52       @|c-function-type| \\ \ind
53         @|c-keyword-function-type| \-
54   \end{tabbing}}
55   \caption{Classes representing C types}
56 \label{fig:codegen.c-types.classes}
57 \end{figure}
58
59 C type objects are immutable unless otherwise specified.
60
61 \subsubsection{Constructing C type objects}
62 There is a constructor function for each non-abstract class of C type object.
63 Note, however, that constructor functions need not generate a fresh type
64 object if a previously existing type object is suitable.  In this case, we
65 say that the objects are \emph{interned}.  Some constructor functions are
66 specified to return interned objects: programs may rely on receiving the same
67 (@|eq|) type object for similar (possibly merely @|equal|) arguments.  Where
68 not specified, clients may still not rely on receiving fresh objects.
69
70 A convenient S-expression notation is provided by the
71 \descref{c-type}[macro]{mac}.  Use of this macro is merely an abbreviation
72 for corresponding use of the various constructor functions, and therefore
73 interns type objects in the same manner.  The syntax accepted by the macro
74 can be extended in order to support new classes: see \descref{defctype}{mac},
75 \descref{c-type-alias}{mac} and \descref{define-c-type-syntax}{mac}.
76
77 The descriptions of each of the various classes include descriptions of the
78 initargs which may be passed to @|make-instance| when constructing a new
79 instance of the class.  However, the constructor functions and S-expression
80 syntax are strongly recommended over direct use of @|make-instance|.
81
82 \subsubsection{Printing}
83 There are two protocols for printing C types.  Unfortunately they have
84 similar names.
85 \begin{itemize}
86 \item The \descref{print-c-type}[function]{gf} prints a C type value using
87   the S-expression notation.  It is mainly useful for diagnostic purposes.
88 \item The \descref{pprint-c-type}[function]{gf} prints a C type as a
89   C-syntax declaration.
90 \end{itemize}
91 Neither generic function defines a default primary method; subclasses of
92 @|c-type| must define their own methods in order to print correctly.
93
94
95 \subsection{The C type root class} \label{sec:clang.c-types.root}
96
97 \begin{describe}{cls}{c-type ()}
98   The class @|c-type| marks the root of the built-in C type hierarchy.
99
100   Users may define subclasses of @|c-type|.  All non-abstract subclasses must
101   have a primary method defined on @|pprint-c-type|; unless instances of the
102   subclass are interned, a method on @|c-type-equal-p| is also required.
103
104   The class @|c-type| is abstract.
105 \end{describe}
106
107
108 \subsection{C type S-expression notation} \label{sec:clang.c-types.sexp}
109
110 The S-expression representation of a type is described syntactically as a
111 type specifier.  Type specifiers fit into two syntactic categories.
112 \begin{itemize}
113 \item A \emph{symbolic type specifier} consists of a symbol.  It has a
114   single, fixed meaning: if @<name> is a symbolic type specifier, then each
115   use of @<name> in a type specifier evaluates to the same (@|eq|) type
116   object, until the @<name> is redefined.
117 \item A \emph{type operator} is a symbol; the corresponding specifier is a
118   list whose @|car| is the operator.  The remaining items in the list are
119   arguments to the type operator.
120 \end{itemize}
121
122 \begin{describe}{mac}{c-type @<type-spec> @> @<c-type>}
123   Evaluates to a C type object, as described by the type specifier
124   @<type-spec>.
125 \end{describe}
126
127 \begin{describe}{mac}
128     {defctype \=@{ @<name> @! (@<name>^+) @} @<type-spec> \+ \\
129                 @[[ @|:export| @<export-flag> @]]^* \-
130        \nlret @<names>}
131   Defines a new symbolic type specifier @<name>; if a list of @<name>s is
132   given, then all are defined in the same way.  The type constructed by using
133   any of the @<name>s is as described by the type specifier @<type-spec>.
134
135   The resulting type object is constructed once, at the time that the macro
136   expansion is evaluated; the same (@|eq|) value is used each time any
137   @<name> is used in a type specifier.
138
139   A variable named @|c-type-@<name>|, for the first @<name> only, is defined
140   and initialized to contain the C type object so constructed.  Altering or
141   binding this name is discouraged.
142
143   If @<export-flag> is true, then the variable name, and all of the @<name>s,
144   are exported from the current package.
145 \end{describe}
146
147 \begin{describe}{mac}{c-type-alias @<original> @<alias>^* @> @<aliases>}
148   Defines each @<alias> as being a type operator identical in behaviour to
149   @<original>.  If @<original> is later redefined then the behaviour of the
150   @<alias>es changes too.
151 \end{describe}
152
153 \begin{describe}{mac}
154     {define-c-type-syntax @<name> @<lambda-list> \\ \ind
155       @[[ @<declaration>^* @! @<doc-string> @]] \\
156       @<form>^* \-
157      \nlret @<name>}
158   Defines the symbol @<name> as a new type operator.  When a list of the form
159   @|(@<name> @<argument>^*)| is used as a type specifier, the @<argument>s
160   are bound to fresh variables according to @<lambda-list> (a destructuring
161   lambda-list) and the @<form>s evaluated in order in the resulting lexical
162   environment as an implicit @|progn|.  The value should be a Lisp form which
163   will evaluate to the type specified by the arguments.
164
165   The @<form>s may call @|expand-c-type-spec| in order to recursively expand
166   type specifiers among its arguments.
167 \end{describe}
168
169 \begin{describe}{gf}{expand-c-type-spec @<type-spec> @> @<form>}
170   Returns the Lisp form that @|(c-type @<type-spec>)| would expand into.
171
172   If @<type-spec> is a list, then \descref{expand-c-type-form}{fun} is
173   invoked.
174 \end{describe}
175
176 \begin{describe}{gf}{expand-c-type-form @<head> @<tail> @> @<form>}
177   Returns the Lisp form that @|(c-type (@<head> . @<tail>)| would expand
178   into.
179 \end{describe}
180
181 \begin{describe}{gf}
182     {print-c-type @<stream> @<type> \&optional @<colon> @<atsign>}
183   Print the C type object @<type> to @<stream> in S-expression form.  The
184   @<colon> and @<atsign> arguments may be interpreted in any way which seems
185   appropriate: they are provided so that @|print-c-type| may be called via
186   @|format|'s @|\char`\~/\dots/| command; they are not set when
187   @|print-c-type| is called by Sod functions.
188
189   There should be a method defined for every C type class; there is no
190   default method.
191 \end{describe}
192
193
194 \subsection{Comparing C types} \label{sec:clang.c-types.cmp}
195
196 It is necessary to compare C types for equality, for example when checking
197 argument lists for methods.  This is done by @|c-type-equal-p|.
198
199 \begin{describe}{gf}
200     {c-type-equal-p @<c-type>_1 @<c-type>_2 @> @<generalized-boolean>}
201   The generic function @|c-type-equal-p| compares two C types @<c-type>_1 and
202   @<c-type>_2 for equality; it returns true if the two types are equal and
203   false if they are not.
204
205   Two types are equal if they are structurally similar, where this property
206   is defined by methods for each individual class; see the descriptions of
207   the classes for the details.
208
209   The generic function @|c-type-equal-p| uses the @|and| method combination.
210
211   \begin{describe}{meth}{c-type-equal-p @<c-type>_1 @<c-type>_2}
212     A default primary method for @|c-type-equal-p| is defined.  It simply
213     returns @|nil|.  This way, methods can specialize on both arguments
214     without fear that a call will fail because no methods are applicable.
215   \end{describe}
216   \begin{describe}{ar-meth}{c-type-equal-p @<c-type>_1 @<c-type>_2}
217     A default around-method for @|c-type-equal-p| is defined.  It returns
218     true if @<c-type>_1 and @<c-type>_2 are @|eql|; otherwise it delegates to
219     the primary methods.  Since several common kinds of C types are interned,
220     this is a common case worth optimizing.
221   \end{describe}
222 \end{describe}
223
224
225 \subsection{Outputting C types} \label{sec:clang.c-types.output}
226
227 \begin{describe}{gf}{pprint-c-type @<c-type> @<stream> @<kernel>}
228   The generic function @|pprint-c-type| pretty-prints to @<stream> a C-syntax
229   declaration of an object or function of type @<c-type>.  The result is
230   written to @<stream>.
231
232   A C declaration has two parts: a sequence of \emph{declaration specifiers}
233   and a \emph{declarator}.  The declarator syntax involves parentheses and
234   operators, in order to reflect the operators applicable to the declared
235   variable.  For example, the name of a pointer variable is preceded by @`*';
236   the name of an array is followed by dimensions enclosed in @`['\dots @`]'.
237
238   The @<kernel> argument must be a function designator (though see the
239   standard around-method); it is invoked as
240   \begin{quote} \codeface
241     (funcall @<kernel> @<stream> @<priority> @<spacep>)
242   \end{quote}
243   It should write to @<stream> -- which may not be the same stream originally
244   passed into the generic function -- the `kernel' of the declarator, i.e.,
245   the part to which prefix and/or postfix operators are attached to form the
246   full declarator.
247
248   The methods on @|pprint-c-type| specialized for compound types work by
249   recursively calling @|pprint-c-type| on the subtype, passing down a closure
250   which prints the necessary additional declarator operators before calling
251   the original @<kernel> function.  The additional arguments @<priority> and
252   @<spacep> support this implementation technique.
253
254   The @<priority> argument describes the surrounding operator context.  It is
255   zero if no type operators are directly attached to the kernel (i.e., there
256   are no operators at all, or the kernel is enclosed in parentheses), one if
257   a prefix operator is directly attached, or two if a postfix operator is
258   directly attached.  If the @<kernel> function intends to provide its own
259   additional declarator operators, it should check the @<priority> in order
260   to determine whether parentheses are necessary.  See also the
261   \descref{maybe-in-parens}[macro]{mac}.
262
263   The @<spacep> argument indicates whether a space needs to be printed in
264   order to separate the declarator from the declaration specifiers.  A kernel
265   which contains an identifier should insert a space before the identifier
266   when @<spacep> is non-nil.  An `empty' kernel, as found in an abstract
267   declarator (one that specifies no name), looks more pleasing without a
268   trailing space.  See also the \descref{c-type-space}[function]{fun}.
269
270   Every concrete subclass of @|c-type| is expected to provide a primary
271   method on this function.  There is no default primary method.
272
273   \begin{describe}{ar-meth}{pprint-c-type @<c-type> @<stream> @<kernel>}
274     A default around method is defined on @|pprint-c-type| which `canonifies'
275     non-function @<kernel> arguments.  In particular:
276     \begin{itemize}
277     \item if @<kernel> is nil, then @|pprint-c-type| is called recursively
278       with a @<kernel> function that does nothing; and
279     \item if @<kernel> is any other kind of object, then @|pprint-c-type| is
280       called recursively with a @<kernel> function that prints the object as
281       if by @|princ|, preceded if necessary by space using @|c-type-space|.
282     \end{itemize}
283   \end{describe}
284 \end{describe}
285
286 \begin{describe}{fun}{c-type-space @<stream>}
287   Writes a space and other pretty-printing instructions to @<stream> in order
288   visually to separate a declarator from the preceding declaration
289   specifiers.  The precise details are subject to change.
290 \end{describe}
291
292 \begin{describe}{mac}
293     {maybe-in-parens (@<stream-var> @<guard-form>)
294       @<declaration>^*
295       @<form>^*}
296   The @<guard-form> is evaluated, and then the @<form>s are evaluated in
297   sequence within a pretty-printer logical block writing to the stream named
298   by the symbol @<stream-var>.  If the @<guard-form> evaluates to nil, then
299   the logical block has empty prefix and suffix strings; if it evaluates to a
300   non-nil value, then the logical block has prefix and suffix @`(' and @`)'
301   respectively.
302
303   Note that this may cause @<stream> to be bound to a different stream object
304   within the @<form>s.
305 \end{describe}
306
307
308 \subsection{Type qualifiers and qualifiable types}
309 \label{sec:clang.ctypes.qual}
310
311 Qualifiers -- @|const|, @|volatile|, and so on -- are represented as lists of
312 keywords attached to types.  Not all C types can carry qualifiers: notably,
313 function and array types cannot be qualified.
314
315 For the most part, the C qualifier keywords correspond to like-named Lisp
316 keywords, only the Lisp keyword names are in uppercase.  The correspondence
317 is shown in \xref{tab:clang.ctypes.qual}.
318
319 \begin{table}
320   \begin{tabular}[C]{*2{>{\codeface}l}l}                           \hlx*{hv}
321     \thd{\textbf{C name}}       & \thd{\textbf{Lisp name}}      \\ \hlx{vhv}
322     _Atomic                     & :atomic                       \\
323     const                       & :const                        \\
324     restrict                    & :restrict                     \\
325     volatile                    & :volatile                     \\ \hlx*{vh}
326   \end{tabular}
327   \caption{C and Lisp qualifier names} \label{tab:clang.ctypes.qual}
328 \end{table}
329
330 The default behaviour, on output, is to convert keywords to lowercase and
331 hope for the best: special cases can be dealt with by adding appropriate
332 methods to \descref{c-qualifier-keyword}{gf}.
333
334 \begin{describe}{cls}{qualifiable-c-type (c-type) \&key :qualifiers}
335   The class @|qualifiable-c-type| describes C types which can bear
336   `qualifiers' (\Cplusplus\ calls them `cv-qualifiers'): @|const|,
337   @|restrict| and @|volatile|.
338
339   The @<qualifiers> are a list of keyword symbols @|:const|, @|:restrict| and
340   @|:volatile|.  There is no built-in limitation to these particular
341   qualifiers; others keywords may be used, though this isn't recommended.
342
343   Two qualifiable types are equal only if they have \emph{matching
344   qualifiers}: i.e., every qualifier attached to one is also attached to the
345   other: order is not significant, and neither is multiplicity.
346
347   The class @|qualifiable-c-type| is abstract.
348 \end{describe}
349
350 \begin{describe}{gf}{c-type-qualifiers @<c-type> @> @<list>}
351   Returns the qualifiers of the @|qualifiable-c-type| instance @<c-type> as
352   an immutable list.
353 \end{describe}
354
355 \begin{describe}{fun}{qualify-type @<c-type> @<qualifiers> @> @<c-type>}
356   The argument @<c-type> must be an instance of @|qualifiable-c-type|,
357   currently bearing no qualifiers, and @<qualifiers> a list of qualifier
358   keywords.  The result is a C type object like @<c-type> except that it
359   bears the given @<qualifiers>.
360
361   The @<c-type> is not modified.  If @<c-type> is interned, then the returned
362   type will be interned.
363 \end{describe}
364
365 \begin{describe}{fun}{format-qualifiers @<qualifiers> @> @<string>}
366   Returns a string containing the qualifiers listed in @<qualifiers> in C
367   syntax, with a space after each.  In particular, if @<qualifiers> is
368   non-null then the final character of the returned string will be a space.
369 \end{describe}
370
371 \begin{describe}{gf}{c-qualifier-keyword @<qualifier> @> @<string>}
372   Return, as a string, the C keyword corresponding to the Lisp @<qualifier>.
373
374   There is a standard method, which deals with many qualifiers.  Additional
375   methods exist for qualifier keywords which need special handling, such as
376   @|:atomic|; they are not listed here explicitly.
377
378   \begin{describe}{meth}{c-qualifier-keyword @<keyword> @> @<string>}
379     Returns the @<keyword>'s print-name, in lower case.  This is sufficient
380     for the standard qualifiers @|:const|, @|:restrict|, and @|:volatile|.
381   \end{describe}
382 \end{describe}
383
384 \begin{describe}{fun}{c-type-qualifier-keywords @<c-type> @> @<list>}
385   Return the @<c-type>'s qualifiers, as a list of C keyword names.
386 \end{describe}
387
388
389 \subsection{Storage specifiers} \label{sec:clang.ctypes.specs}
390
391 Some declaration specifiers, mostly to do with how to store the specific
392 object in question, are determinedly `top level', and, unlike qualifiers,
393 don't stay attached to the base type when acted on by declarator operators.
394 Sod calls these `storage specifiers', though no such category exists in the C
395 standard.  They have their own protocol, which is similar in many ways to
396 that of C types.
397
398 Every Lisp keyword is potentially a storage specifier, which simply maps to
399 its lower-case print name in C; but other storage specifiers may be more
400 complicated objects.
401
402 \begin{describe}{cls}
403     {c-storage-specifiers-type (c-type) \&key :subtype :specifiers}
404   A type which carries storage specifiers.  The @<subtype> is the actual
405   type, and may be any C type; the @<specifiers> are a list of
406   storage-specifier objects.
407
408   The type specifier @|(specs @<subtype> @<specifier>^*)| wraps the
409   @<subtype> in a @|c-storage-specifiers-type|, carrying the @<specifier>s,
410   which are a list of storage specifiers in S-expression notation.
411 \end{describe}
412
413 \begin{describe}{fun}{c-type-specifiers @<type> @> @<list>}
414   Returns the list of type specifiers attached to the @<type> object, which
415   must be a @|c-storage-specifiers-type|.
416 \end{describe}
417
418 \begin{describe}{mac}
419     {define-c-storage-specifier-syntax @<name> @<lambda-list> \\ \ind
420       @[[ @<declaration>^* @! @<doc-string> @]] \\
421       @<form>^* \-
422      \nlret @<name>}
423
424   Defines the symbol @<name> as a new storage-specifier operator.  When a
425   list of the form @|(@<name> @<argument>^*)| is used as a storage specifier,
426   the @<argument>s are bound to fresh variables according to the
427   @<lambda-list> (a destructuring lambda-list) and the @<form>s evaluated in
428   order in the resulting lexical environment as an implicit @<progn>.  The
429   value should be a Lisp form which will evaluate to the storage-specifier
430   object described by the arguments.
431
432   The @<form>s may call @|expand-c-storage-specifier| in order to recursively
433   expand storage specifiers among its arguments.
434 \end{describe}
435
436 \begin{describe}{gf}{expand-c-storage-specifier @<spec> @> @<form>}
437   Returns the Lisp form that @<spec> expands to within @|(c-type (specs
438   @<subtype> @<spec>))|.
439
440   If @<spec> is a list, then \descref{expand-c-storage-specifier-form} is
441   invoked.
442 \end{describe}
443
444 \begin{describe}{gf}{expand-c-storage-specifier-form @<spec> @> @<form>}
445   Returns the Lisp form that @|(@<head> . @<tail>)| expands to within
446   @|(c-type (specs @<subtype> (@<head> . @<tail>)))|.
447 \end{describe}
448
449 \begin{describe}{gf}{pprint-c-storage-specifier @<spec> @<stream>}
450 \end{describe}
451
452 \begin{describe}{gf}
453     {print-c-storage-specifier @<stream> @<spec>
454                                \&optional @<colon> @<atsign>}
455 \end{describe}
456
457 \begin{describe}{fun}{wrap-c-type @<func> @<base-type> @> @<c-type>}
458   Apply @<func> to the underlying C type of @<base-type> to create a new
459   `wrapped' type, and attach the storage specifiers of @<base-type> to the
460   wrapped type.
461
462   If @<base-type> is \emph{not} a @|c-storage-specifiers-type|, then return
463   @|(funcall @<func> @<base-type>)|.  Otherwise, return a new
464   @|c-storage-specifiers-type|, with the same specifiers, but whose subtype
465   is the result of applying @<func> to the subtype of the original
466   @<base-type>.
467 \end{describe}
468
469 \begin{describe}{cls}{alignas-storage-specifier () \&key :alignment}
470   The class of @|_Alignas| storage specifiers; an instance denotes the
471   specifier @|_Alignas(@<alignment>)|.  The @<alignment> parameter may be any
472   printable object, but is usually a string or C fragment.
473
474   The storage specifier form @|(alignas @<alignment>)| returns a storage
475   specifier @|_Alignas(@<alignment>)|, where @<alignment> is evaluated.
476 \end{describe}
477
478
479 \subsection{Leaf types} \label{sec:clang.c-types.leaf}
480
481 A \emph{leaf type} is a type which is not defined in terms of another type.
482 In Sod, the leaf types are
483 \begin{itemize}
484 \item \emph{simple types}, including builtin types like @|int| and @|char|,
485   as well as type names introduced by @|typename|, because Sod isn't
486   interested in what the type name means, merely that it names a type; and
487 \item \emph{tagged types}, i.e., enum, struct and union types which are named
488   by a keyword identifying the kind of type, and a \emph{tag}.
489 \end{itemize}
490
491 \begin{describe}{cls}{simple-c-type (qualifiable-c-type)
492     \&key :qualifiers :name}
493   The class of `simple types'; an instance denotes the type @<qualifiers>
494   @<name>.
495
496   A simple type object maintains a \emph{name}, which is a string whose
497   contents are the C name for the type.  The initarg @|:name| may be used to
498   provide this name when calling @|make-instance|.
499
500   Two simple type objects are equal if and only if they have @|string=| names
501   and matching qualifiers.
502
503   A number of symbolic type specifiers for builtin types are predefined as
504   shown in \xref{tab:codegen.c-types.simple}.  These are all defined as if by
505   @|define-simple-c-type|, so can be used to construct qualified types.
506 \end{describe}
507
508 \begin{table}
509   \begin{tabular}[C]{ll}                                           \hlx*{hv}
510     \thd{C type}        & \thd{Specifiers}                      \\ \hlx{vhv}
511     @|void|             & @|void|                               \\ \hlx{v}
512     @|_Bool|            & @|bool|                               \\ \hlx{v}
513     @|char|             & @|char|                               \\ \hlx{}
514     @|wchar_t|          & @|wchar-t|                            \\ \hlx{v}
515     @|signed char|      & @|signed-char|, @|schar|              \\ \hlx{}
516     @|unsigned char|    & @|unsigned-char|, @|uchar|            \\ \hlx{v}
517     @|short|            & @|short|, @|signed-short|, @|short-int|,
518                           @|signed-short-int| @|sshort|         \\ \hlx{}
519     @|unsigned short|   & @|unsigned-short|, @|unsigned-short-int|,
520                           @|ushort|                             \\ \hlx{v}
521     @|int|              & @|int|, @|signed|, @|signed-int|,
522                           @|sint|                               \\ \hlx{}
523     @|unsigned int|     & @|unsigned|, @|unsigned-int|, @|uint| \\ \hlx{v}
524     @|long|             & @|long|, @|signed-long|, @|long-int|,
525                           @|signed-long-int|, @|slong|          \\ \hlx{}
526     @|unsigned long|    & @|unsigned-long|, @|unsigned-long-int|,
527                           @|ulong|                              \\ \hlx{v}
528     @|long long|        & @|long-long|, @|signed-long-long|,
529                           @|long-long-int|,                     \\ \hlx{}
530                         & \qquad @|signed-long-long-int|,
531                           @|llong|, @|sllong|                   \\ \hlx{v}
532     @|unsigned long long|
533                         & @|unsigned-long-long|, @|unsigned-long-long-int|,
534                           @|ullong|                             \\ \hlx{v}
535     @|size_t|           & @|size-t|                             \\ \hlx{}
536     @|ptrdiff_t|        & @|ptrdiff-t|                          \\ \hlx{v}
537     @|float|            & @|float|                              \\ \hlx{}
538     @|double|           & @|double|                             \\ \hlx{}
539     @|long double|      & @|long-double|                        \\ \hlx{v}
540     @|float _Imaginary| & @|float-imaginary|                    \\ \hlx{}
541     @|double _Imaginary|& @|double-imaginary|                   \\ \hlx{}
542     @|long double _Imaginary|
543                         & @|long-double-imaginary|              \\ \hlx{v}
544     @|float _Complex|   & @|float-complex|                      \\ \hlx{}
545     @|double _Complex|  & @|double-complex|                     \\ \hlx{}
546     @|long double _Complex|
547                         & @|long-double-complex|                \\ \hlx{v}
548     @|va_list|          & @|va-list|                            \\ \hlx*{vh}
549   \end{tabular}
550   \caption{Builtin symbolic type specifiers for simple C types}
551   \label{tab:codegen.c-types.simple}
552 \end{table}
553
554 \begin{describe}{fun}
555     {make-simple-type @<name> \&optional @<qualifiers> @> @<c-type>}
556   Return the (unique interned) simple C type object for the C type whose name
557   is @<name> (a string) and which has the given @<qualifiers> (a list of
558   keywords).
559 \end{describe}
560
561 \begin{describe}{gf}{c-type-name @<c-type> @> @<string>}
562   Returns the name of a @|simple-c-type| instance @<c-type> as an immutable
563   string.
564 \end{describe}
565
566 \begin{describe}{mac}
567     {define-simple-c-type \=@{ @<name> @! (@<name>^+) @} @<string> \+ \\
568                             @[[ @|:export| @<export-flag> @]] \-
569       \nlret @<name>}
570   Define type specifiers for a new simple C type.  Each symbol @<name> is
571   defined as a symbolic type specifier for the (unique interned) simple C
572   type whose name is the value of @<string>.  Further, each @<name> is
573   defined to be a type operator: the type specifier @|(@<name>
574   @<qualifier>^*)| evaluates to the (unique interned) simple C type whose
575   name is @<string> and which has the @<qualifiers> (which are evaluated).
576
577   Furthermore, a variable @|c-type-@<name>| is defined, for the first @<name>
578   only, and initialized with the newly constructed C type object.
579
580   If @<export-flag> is true, then the @|c-type-@<name>| variable name, and
581   all of the @<name>s, are exported from the current package.
582 \end{describe}
583
584 \begin{describe}{cls}{tagged-c-type (qualifiable-c-type)
585     \&key :qualifiers :tag}
586   Provides common behaviour for C tagged types.  A @<tag> is a string
587   containing a C identifier.
588
589   Two tagged types are equal if and only if they have the same class, their
590   @<tag>s are @|string=|, and they have matching qualifiers.  (User-defined
591   subclasses may have additional methods on @|c-type-equal-p| which impose
592   further restrictions.)
593 \end{describe}
594 \begin{boxy}[Bug]
595   Sod maintains distinct namespaces for the three kinds of tagged types.  In
596   C, there is only one namespace for tags which is shared between enums,
597   structs and unions.
598 \end{boxy}
599
600 \begin{describe}{gf}{c-tagged-type-kind @<c-type> @> @<keyword>}
601   Returns a keyword classifying the tagged @<c-type>: one of @|:enum|,
602   @|:struct| or @|:union|.  User-defined subclasses of @|tagged-c-type|
603   should return their own classification symbols.  It is intended that
604   @|(string-downcase (c-tagged-type-kind @<c-type>))| be valid C
605   syntax.\footnote{%
606     Alas, C doesn't provide a syntactic category for these keywords;
607     \Cplusplus\ calls them a @<class-key>.} %
608   There is a method defined for each of the built-in tagged type classes
609   @|c-struct-type|, @|c-union-type| and @|c-enum-type|.
610 \end{describe}
611
612 \begin{describe}{gf}{kind-c-tagged-type @<keyword> @> @<symbol>}
613   This is not quite the inverse of @|c-tagged-type-kind|.  Given a keyword
614   naming a kind of tagged type, return the name of the corresponding C
615   type class as a symbol.
616 \end{describe}
617
618 \begin{describe}{cls}{c-enum-type (tagged-c-type) \&key :qualifiers :tag}
619   Represents a C enumerated type.  An instance denotes the C type @|enum|
620   @<tag>.  See the direct superclass @|tagged-c-type| for details.
621
622   The type specifier @|(enum @<tag> @<qualifier>^*)| returns the (unique
623   interned) enumerated type with the given @<tag> and @<qualifier>s (all
624   evaluated).
625 \end{describe}
626 \begin{describe}{fun}
627     {make-enum-type @<tag> \&optional @<qualifiers> @> @<c-enum-type>}
628   Return the (unique interned) C type object for the enumerated C type whose
629   tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
630   keywords).
631 \end{describe}
632
633 \begin{describe}{cls}{c-struct-type (tagged-c-type) \&key :qualifiers :tag}
634   Represents a C structured type.  An instance denotes the C type @|struct|
635   @<tag>.  See the direct superclass @|tagged-c-type| for details.
636
637   The type specifier @|(struct @<tag> @<qualifier>^*)| returns the (unique
638   interned) structured type with the given @<tag> and @<qualifier>s (all
639   evaluated).
640 \end{describe}
641 \begin{describe}{fun}
642     {make-struct-type @<tag> \&optional @<qualifiers> @> @<c-struct-type>}
643   Return the (unique interned) C type object for the structured C type whose
644   tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
645   keywords).
646 \end{describe}
647
648 \begin{describe}{cls}{c-union-type (tagged-c-type) \&key :qualifiers :tag}
649   Represents a C union type.  An instance denotes the C type @|union|
650   @<tag>.  See the direct superclass @|tagged-c-type|
651   for details.
652
653   The type specifier @|(union @<tag> @<qualifier>^*)| returns the (unique
654   interned) union type with the given @<tag> and @<qualifier>s (all
655   evaluated).
656 \end{describe}
657 \begin{describe}{fun}
658     {make-union-type @<tag> \&optional @<qualifiers> @> @<c-union-type>}
659   Return the (unique interned) C type object for the union C type whose tag
660   is @<tag> (a string) and which has the given @<qualifiers> (a list of
661   keywords).
662 \end{describe}
663
664
665 \subsection{Compound C types} \label{sec:code.c-types.compound}
666
667 Some C types are \emph{compound types}: they're defined in terms of existing
668 types.  The classes which represent compound types implement a common
669 protocol.
670
671 \begin{describe}{gf}{c-type-subtype @<c-type> @> @<subtype>}
672   Returns the underlying type of a compound type @<c-type>.  Precisely what
673   this means depends on the class of @<c-type>.
674 \end{describe}
675
676
677 \subsection{Atomic types} \label{sec:clang.c-types.atomic}
678
679 Atomic types are compound types.  The subtype of an atomic type is simply the
680 underlying type of the object.  Note that, as far as Sod is concerned, atomic
681 types are not the same as atomic-qualified types: you must be consistent
682 about which you use.
683
684 \begin{describe}{cls}
685     {c-atomic-type (qualifiable-c-type) \&key :qualifiers :subtype}
686   Represents an atomic type.  An instance denotes the C type
687   @|_Atomic(@<subtype>)|.
688
689   The @<subtype> may be any C type.\footnote{%
690     C does not permit atomic function or array types.} %
691   Two atomic types are equal if and only if their subtypes are equal and they
692   have matching qualifiers.  It is possible, though probably not useful, to
693   have an atomic-qualified atomic type.
694
695   The type specifier @|(atomic @<type-spec> @<qualifier>^*)| returns a type
696   qualified atomic @<subtype>, where @<subtype> is the type specified by
697   @<type-spec> and the @<qualifier>s are qualifier keywords (which are
698   evaluated).
699 \end{describe}
700
701 \begin{describe}{fun}
702     {make-atomic-type @<c-type> \&optional @<qualifiers> @> @<c-atomic-type>}
703   Return an object describing the type qualified atomic @<subtype>.  If
704   @<subtype> is interned, then the returned atomic type object is interned
705   also.
706 \end{describe}
707
708
709 \subsection{Pointer types} \label{sec:clang.c-types.pointer}
710
711 Pointers are compound types.  The subtype of a pointer type is the type it
712 points to.
713
714 \begin{describe}{cls}
715     {c-pointer-type (qualifiable-c-type) \&key :qualifiers :subtype}
716   Represents a C pointer type.  An instance denotes the C type @<subtype>
717   @|*|@<qualifiers>.
718
719   The @<subtype> may be any C type.  Two pointer types are equal if and only
720   if their subtypes are equal and they have matching qualifiers.
721
722   The type specifier @|(* @<type-spec> @<qualifier>^*)| returns a type
723   qualified pointer-to-@<subtype>, where @<subtype> is the type specified by
724   @<type-spec> and the @<qualifier>s are qualifier keywords (which are
725   evaluated).  The synonyms @|ptr| and @|pointer| may be used in place of the
726   star @`*'.
727
728   The symbol @|string| is a type specifier for the type pointer to
729   characters; the symbol @|const-string| is a type specifier for the type
730   pointer to constant characters.
731 \end{describe}
732
733 \begin{describe}{fun}
734     {make-pointer-type @<c-type> \&optional @<qualifiers>
735       @> @<c-pointer-type>}
736   Return an object describing the type qualified pointer to @<subtype>.
737   If @<subtype> is interned, then the returned pointer type object is
738   interned also.
739 \end{describe}
740
741
742 \subsection{Array types} \label{sec:clang.c-types.array}
743
744 Arrays implement the compound-type protocol.  The subtype of an array type is
745 the array element type.
746
747 \begin{describe}{cls}{c-array-type (c-type) \&key :subtype :dimensions}
748   Represents a multidimensional C array type.  The @<dimensions> are a list
749   of dimension specifiers $d_0$, $d_1$, \ldots, $d_{n-1}$; an instance then
750   denotes the C type @<subtype> @|[$d_0$][$d_1$]$\ldots$[$d_{n-1}$]|.  An
751   individual dimension specifier is either a string containing a C integral
752   constant expression, or nil which is equivalent to an empty string.  Only
753   the first (outermost) dimension $d_0$ should be empty.
754
755   C doesn't actually have multidimensional arrays as a primitive notion;
756   rather, it permits an array (with known extent) to be the element type of
757   an array, which achieves an equivalent effect.  C arrays are stored in
758   row-major order: i.e., if we write down the indices of the elements of an
759   array in order of ascending address, the rightmost index varies fastest;
760   hence, the type constructed is more accurately an array of $d_0$ arrays of
761   $d_1$ of \ldots\ arrays of $d_{n-1}$ elements of type @<subtype>.  We shall
762   continue to abuse terminology and refer to multidimensional arrays.
763
764   The type specifier @|([] @<type-spec> @<dimension>^*)| constructs a
765   multidimensional array with the given @<dimension>s whose elements have the
766   type specified by @<type-spec>.  If no dimensions are given then a
767   single-dimensional array with unspecified extent.  The synonyms @|array|
768   and @|vector| may be used in place of the brackets @`[]'.
769 \end{describe}
770
771 \begin{describe}{fun}
772     {make-array-type @<subtype> @<dimensions> @> @<c-array-type>}
773   Return an object describing the type of arrays with given @<dimensions> and
774   with element type @<subtype> (an instance of @|c-type|).  The @<dimensions>
775   argument is a list whose elements are strings or nil; see the description
776   of the class @|c-array-type| above for details.
777 \end{describe}
778
779 \begin{describe}{gf}{c-array-dimensions @<c-type> @> @<list>}
780   Returns the dimensions of @<c-type>, an array type, as an immutable list.
781 \end{describe}
782
783
784 \subsection{Function types} \label{sec:clang.c-types.fun}
785
786 Function types implement the compound-type protocol.  The subtype of a
787 function type is the type of the function's return value.
788
789 \begin{describe}{cls}{argument}
790   Represents an ordinary function argument.
791 \end{describe}
792
793 \begin{describe}{fun}{argumentp @<value> @> @<generalized-boolean>}
794   Decide whether @<value> is an @<argument> object: if so, return non-nil; if
795   not return nil.
796 \end{describe}
797
798 \begin{describe}{fun}
799     {make-argument @<name> @<c-type> \&optional @<default> @> @<argument>}
800   Construct and a return a new @<argument> object.  The argument has type
801   @<c-type>, which must be a @|c-type| object, and is named @<name>.
802
803   The @<name> may be nil to indicate that the argument has no name: in this
804   case the argument will be formatted as an abstract declarator, which is not
805   suitable for function definitions.  If @<name> is not nil, then the
806   @<name>'s print representation, with @|*print-escape*| nil, is used as the
807   argument name.
808
809   A @<default> may be supplied.  If the argument is used in a
810   keyword-argument list (e.g., in a \descref{c-keyword-function-type}
811   [object]{cls}), and the @<default> value is provided and non-nil, then its
812   (unescaped) printed representation is used to provide a default value if
813   the keyword argument is not supplied by the caller.
814 \end{describe}
815
816 \begin{describe*}
817     {\dhead{fun}{argument-name @<argument> @> @<name>}
818      \dhead{fun}{argument-type @<argument> @> @<c-type>}
819      \dhead{fun}{argument-default @<argument> @> @<default>}}
820   Accessor functions for @|argument| objects.  They return the appropriate
821   component of the object, as set by to @|make-argument|.  The @<default> is
822   nil if no default was provided to @|make-argument|.
823 \end{describe*}
824
825 \begin{describe}{gf}
826     {commentify-argument-name @<name> @> @<commentified-name>}
827   Convert the argument name @<name> so that it's suitable to declare the
828   function in a header file.
829
830   Robust header files shouldn't include literal argument names in
831   declarations of functions or function types, since this restricts the
832   including file from defining such names as macros.  This generic function
833   is used to convert names into a safe form.
834
835   \begin{describe}{meth}{commentify-argument-name (@<name> null) @> nil}
836     Returns nil: if the argument name is already omitted, it's safe for use
837     in a header file.
838   \end{describe}
839   \begin{describe}{meth}{commentify-argument-name (@<name> t) @> @<string>}
840     Returns the print form of @<name> wrapped in a C comment, as
841     @`/*@<name>*/'.
842   \end{describe}
843 \end{describe}
844
845 \begin{describe}{fun}
846     {commentify-argument-names @<arguments> @> @<commentified-arguments>}
847   Convert the @<arguments> list so that it's suitable for use in a header
848   file.
849
850   The @<arguments> list should be a list whose items are @|argument| objects
851   or the keyword @|:ellipsis|.  The return value is a list constructed as
852   follows.  For each @|argument| object in the input list, there is a
853   corresponding @|argument| object in the returned list, with the same type,
854   and whose name is the result of @|commentify-argument-name| applied to the
855   input argument name; an @|:ellipsis| in the input list is passed through
856   unchanged.
857 \end{describe}
858
859 \begin{describe}{cls}{c-function-type (c-type) \&key :subtype :arguments}
860   Represents C function types.  An instance denotes the type of a C
861   function which accepts the @<arguments> and returns @<subtype>.
862
863   The @<arguments> are a possibly empty list.  All but the last element of
864   the list must be @|argument| objects; the final element may instead be the
865   keyword @|:ellipsis|, which denotes a variable argument list.
866
867   An @<arguments> list consisting of a single argument with type @|void| is
868   converted into an empty list.  On output as C code, an empty argument list
869   is written as @|void|.  It is not possible to represent a pre-ANSI C
870   function without prototypes.
871
872   Two function types are considered to be the same if their return types are
873   the same, and their argument lists consist of arguments with the same type,
874   in the same order, and either both or neither argument list ends with
875   @|:ellipsis|; argument names are not compared.
876
877   The type specifier
878   \begin{prog}
879     (fun @<return-type>
880          @{ (@<arg-name> @<arg-type>) @}^*
881          @[:ellipsis @! . @<form>@])
882   \end{prog}
883   constructs a function type.  The function has the subtype @<return-type>.
884   The remaining items in the type-specifier list are used to construct the
885   argument list.  The argument items are a possibly improper list, beginning
886   with zero or more \emph{explicit arguments}: two-item
887   @<arg-name>/@<arg-type> lists.  For each such list, an @|argument| object
888   is constructed with the given name (evaluated) and type.  Following the
889   explicit arguments, there may be
890   \begin{itemize}
891   \item nothing, in which case the function's argument list consists only of
892     the explicit arguments;
893   \item the keyword @|:ellipsis|, as the final item in the type-specifier
894     list, indicating a variable argument list may follow the explicit
895     arguments; or
896   \item a possibly-improper list tail, beginning with an atom either as a
897     list item or as the final list cdr, indicating that the entire list tail
898     is Lisp expression which is to be evaluated to compute the remaining
899     arguments.
900   \end{itemize}
901   A tail expression may return a list of @|argument| objects, optionally
902   followed by an @|:ellipsis|.
903
904   For example,
905   \begin{prog}
906     (c-type (fun \=(lisp (c-type-subtype other-func)) \+ \\
907                    ("first" int) . (c-function-arguments other-func))
908   \end{prog}
909   evaluates to a function type like @|other-func|, only with an additional
910   argument of type @|int| added to the front of its argument list.  This
911   could also have been written
912   \begin{prog}
913     (let (\=(args (c-function-arguments other-func)) \+ \\
914             (ret (c-type-subtype other-func))) \- \\ \ind
915       (c-type (fun \=(lisp ret) ("first" int) . args)
916   \end{prog}
917 \end{describe}
918
919 \begin{describe}{cls}
920     {c-keyword-function-type (c-function-type)
921       \&key :subtype :arguments :keywords}
922   Represents `functions' which accept keyword arguments.  Of course, actual C
923   functions can't accept keyword arguments directly, but this type is useful
924   for describing messages and methods which deal with keyword arguments.
925
926   An instance denotes the type of C function which accepts the position
927   argument list @<arguments>, and keyword arguments from the @<keywords>
928   list, and returns @<subtype>.  Either or both of the @<arguments> and
929   @<keywords> lists may be empty.  (It is important to note the distinction
930   between a function which doesn't accept keyword arguments, and one which
931   does but for which no keyword arguments are defined.  In particular, the
932   latter function can be changed later to accept a keyword argument without
933   breaking compatibility with old code.)  The @<arguments> and @<keywords>
934   lists must \emph{not} contain @|:ellipsis| markers: a function can accept
935   keywords, or a variable-length argument tail, but not both.
936
937   Keyword arguments may (but need not) have a \emph{default value} which is
938   supplied to the function body if the keyword is omitted.
939
940   Keyword functions are never considered to be the same as ordinary
941   functions.  Two keyword function types are considered to be the same if
942   their return types are the same, and their positional argument lists consist of
943   arguments with the same type, in the same order: the keyword arguments
944   accepted by the functions is not significant.
945
946   Keyword functions are constructed using an extended version of the @|fun|
947   specifier used for ordinary C function types.  The extended syntax is as
948   follows.
949   \begin{prog}
950     (fun \=@<return-type>
951            @{ (@<arg-name> @<arg-type>) @}^* \+ \\
952            @{ \=:keys @{ (@<kw-name> @<kw-type> @[@<kw-default>@]) @}^*
953                    @[. @<form>@] @! \+ \\
954                  . @<form> @}
955   \end{prog}
956   where either the symbol @|:keys| appears literally in the specifier, or the
957   @<form> evaluates to a list containing the symbol @|:keys|.  (If neither of
958   these circumstances obtains, then the specifier constructs an ordinary
959   function type.)
960
961   See the description of \descref{c-function-type}{cls} for how a trailing
962   @<form> is handled.
963
964   The list of @<arg-name>s and @<arg-type>s describes the positional
965   arguments.  The list of @<kw-name>s, @<kw-type>s and @<kw-defaults>s
966   describes the keyword arguments.
967 \end{describe}
968
969 \begin{describe}{fun}
970     {make-function-type @<subtype> @<arguments> @> @<c-function-type>}
971   Construct and return a new function type, returning @<subtype> and
972   accepting the @<arguments>.
973
974   If the @<arguments> list contains a @|:keys| marker, then a
975   \descref{c-keyword-function-type}[object]{cls} is returned: those arguments
976   preceding the @|:keys| marker form the positional argument list, and those
977   following the marker form the list of keyword arguments.
978 \end{describe}
979
980 \begin{describe}{fun}
981     {make-keyword-function-type @<subtype> @<arguments> @<keywords>
982       \nlret @<c-keyword-function-type>}
983   Construct and return a new keyword-function type, returning @<subtype> and
984   accepting the @<arguments> and @<keywords>.
985 \end{describe}
986
987 \begin{describe}{gf}
988     {c-function-arguments @<c-function-type> @> @<arguments>}
989   Return the arguments list of the @<c-function-type>.
990 \end{describe}
991
992 \begin{describe}{fun}
993     {commentify-function-type @<c-function-type> @> @<commentified-c-type>}
994   Return a commentified version of the @<c-function-type>.
995
996   The returned type has the same subtype as the given type, and the argument
997   list of the returned type is the result of applying
998   @|commentify-argument-names| to the argument list of the given type.
999 \end{describe}
1000
1001 \begin{describe}{fun}{reify-variable-argument-tail @<arguments> @> @<list>}
1002   If the @<argument> list contains an @|:ellipsis| marker, then replace it
1003   with a @|va_list|.  The name for the new argument, if any, is taken from
1004   the \descref{*sod-ap*}[variable]{var}.  The new list is returned; the
1005   original list is not modified, but may share structure with the new list.
1006 \end{describe}
1007
1008 \begin{describe}{fun}{merge-keyword-lists @<lists> @> @<list>}
1009   Merge a number of keyword-argument lists together and return the result.
1010
1011   The @<lists> parameter is a list consisting of a number of @|(@<args>
1012   . @<origin>)| pairs: in each pair, @<args> is a list of
1013   \descref{argument}{cls} objects, and @<origin> is either nil or an object
1014   whose printed representation describes the origin of the corresponding
1015   @<args> list, suitable for inclusion in an error message.
1016
1017   The resulting list contains exactly one argument for each distinct argument
1018   name appearing in the input @<lists>; this argument will contain the
1019   default value from the earliest occurrence in the input @<lists> of an
1020   argument with that name.
1021
1022   If the same name appears multiple times with different types, an error is
1023   signalled quoting the name, conflicting types, and (if non-nil) the origins
1024   of the offending argument objects.
1025 \end{describe}
1026
1027 \begin{describe}{fun}
1028     {pprint-c-function-type @<return-type> @<stream>
1029                             @<print-args> @<print-kernel>}
1030   Provides the top-level structure for printing C function types.
1031
1032   Output is written to @<stream> to describe a function type returning
1033   @<return-type>, whose declarator kernel (containing the name, and any
1034   further type operands) will be printed by @<print-kernel>, and whose
1035   arguments, if any, will be printed by @<print-args>.
1036
1037   The @<print-kernel> function is a standard kernel-printing function
1038   following the \descref{pprint-c-type}[protocol]{gf}.
1039
1040   The @<print-args> function is given a single argument, which is the
1041   @<stream> to print on.  It should not print the surrounding parentheses.
1042
1043   The output written to @<stream> looks approximately like
1044   \begin{prog}
1045     @<return-type> @<kernel>(@<args>)
1046   \end{prog}
1047 \end{describe}
1048
1049 \begin{describe}{fun}{pprint-argument-list @<args> @<stream> @> @<flag>}
1050   Print an argument list to @<stream>.
1051
1052   The @<args> is a list of \descref{argument}[objects]{cls}, optionally
1053   containing an @|:ellipsis| marker.  The function returns true if any
1054   arguments were actually printed.
1055 \end{describe}
1056
1057
1058 \subsection{Parsing C types} \label{sec:clang.c-types.parsing}
1059
1060 \begin{describe}{fun}
1061     {parse-c-type @<scanner>
1062       @> @<result> @<success-flag> @<consumed-flag>}
1063 \end{describe}
1064
1065 \begin{describe}{fun}
1066     {parse-declarator @<scanner> @<base-type> \&key :kernel :abstractp
1067       \nlret @<result> @<success-flag> @<consumed-flag>}
1068 \end{describe}
1069
1070
1071 \subsection{Class types} \label{sec:clang.c-types.class}
1072
1073 \begin{describe}{cls}
1074     {c-class-type (simple-c-type) \&key :class :tag :qualifiers :name}
1075 \end{describe}
1076
1077 \begin{describe*}
1078     {\dhead{gf}{c-type-class @<class-type> @> @<class>}
1079      \dhead{gf}{setf (c-type-class @<class-type>) @<class>}}
1080 \end{describe*}
1081
1082 \begin{describe}{fun}{find-class-type @<name> @> @<class-type-or-nil>}
1083 \end{describe}
1084
1085 \begin{describe}{fun}
1086     {make-class-type @<name> \&optional @<qualifiers> @> @<class-type>}
1087 \end{describe}
1088
1089 \begin{describe}{fun}
1090     {make-class-type @<name> \&optional @<qualifiers> @> @<class-type>}
1091 \end{describe}
1092
1093 \begin{describe}{fun}{find-sod-class @<name> @> @<class>}
1094 \end{describe}
1095
1096 \begin{describe}{fun}{record-sod-class @<class>}
1097 \end{describe}
1098
1099 %%%--------------------------------------------------------------------------
1100 \section{Generating C code} \label{sec:clang.codegen}
1101
1102 This section deals with Sod's facilities for constructing and manipulating C
1103 expressions, declarations, instructions and definitions.
1104
1105
1106 \subsection{Temporary names} \label{sec:clang.codegen.temporaries}
1107
1108 Many C-level objects, especially ones with external linkage or inclusion in a
1109 header file, are assigned names which are simple strings, perhaps fixed ones,
1110 perhaps constructed.  Other objects don't need meaningful names, and
1111 suitably unique constructed names would be tedious and most likely rather
1112 opaque.  Therefore Sod has an ability to construct \emph{temporary names}.
1113
1114 These aren't temporary in the sense that they name C objects which have
1115 limited lifetimes at runtime.  Rather, the idea is that the names be
1116 significant only to small pieces of Lisp code, which will soon forget about
1117 them.
1118
1119 \subsubsection{The temporary name protocol}
1120 Temporary names are represented by objects which implement a simple protocol.
1121
1122 \begin{describe}{gf}{format-temporary-name @<var> @<stream>}
1123 \end{describe}
1124
1125 \begin{describe*}
1126     {\dhead{gf}{var-in-use-p @<var> @> @<generalized-boolean>}
1127      \dhead[setf var-in-use-p]
1128        {gf}{setf (var-in-use-p @<var>) @<generalized-boolean>}}
1129 \end{describe*}
1130
1131 \subsubsection{Temporary name objects}
1132
1133 \begin{describe}{cls}{temporary-name () \&key :tag}
1134   A temporary name object.  This is the root of a small collection of
1135   subclasses, but is also usable on its own.
1136 \end{describe}
1137
1138 \begin{describe}{meth}
1139     {commentify-argument-name (@<name> temporary-name) @> nil}
1140 \end{describe}
1141
1142 \begin{table}
1143   \begin{tabular}[C]{*2{>{\codeface}l}}                            \hlx*{hv}
1144     \thd{\textbf{Class}} & \thd{\textbf{Name format}}           \\ \hlx{vhv}
1145     temporary-name              & @<tag>                        \\
1146     temporary-argument          & sod__a@<tag>                  \\
1147     temporary-function          & sod__f@<tag>                  \\
1148     temporary-variable          & sod__v@<tag>                  \\ \hlx*{vh}
1149   \end{tabular}
1150   \caption{Temporary name formats}
1151   \label{tab:codegen.codegen.temps-format}
1152 \end{table}
1153
1154 \begin{describe}{cls}{temporary-argument (temporary-name) \&key :tag}
1155 \end{describe}
1156
1157 \begin{describe}{cls}{temporary-function (temporary-name) \&key :tag}
1158 \end{describe}
1159
1160 \begin{describe}{fun}{temporary-function @> @<name>}
1161 \end{describe}
1162
1163 \begin{describe}{cls}
1164     {temporary-variable (temporary-name) \&key :tag :in-use-p}
1165 \end{describe}
1166
1167 \subsubsection{Well-known `temporary' names}
1168
1169 \begin{table}
1170   \begin{tabular}[C]{*2{>{\codeface}l}}                            \hlx*{hv}
1171     \thd{\textbf{Variable}} & \thd{\textbf{Name format}}        \\ \hlx{vhv}
1172     {}*sod-ap*                  & sod__ap                       \\
1173     {}*sod-master-ap*           & sod__master_ap                \\
1174     {}*null-pointer*            & NULL                          \\ \hlx*{vh}
1175   \end{tabular}
1176   \caption{Well-known temporary names}
1177   \label{tab:codegen.codegen.well-known-temps}
1178 \end{table}
1179
1180
1181 \subsection{Instructions} \label{sec:clang.codegen.insts}
1182
1183 \begin{describe}{cls}{inst () \&key}
1184 \end{describe}
1185
1186 \begin{describe}{gf}{inst-metric @<inst>}
1187 \end{describe}
1188
1189 \begin{describe}{mac}
1190     {definst @<code> (@<streamvar> \&key @<export>) (@<arg>^*) \\ \ind
1191       @[[ @<declaration>^* @! @<doc-string> @]] \\
1192       @<form>^* \-
1193      \nlret @<code>}
1194 \end{describe}
1195
1196 \begin{describe}{mac}
1197     {format-compound-statement
1198         (@<stream> @<child> \&optional @<morep>) \\ \ind
1199       @<declaration>^* \\
1200       @<form>^*}
1201 \end{describe}
1202
1203 \begin{describe}{fun}
1204     {format-banner-comment @<stream> @<control> \&rest @<args>}
1205 \end{describe}
1206
1207 \begin{table}
1208   \begin{tabular}[C]{ll>{\codeface}l}                              \hlx*{hv}
1209     \thd{Class name} &
1210     \thd{Arguments} &
1211     \thd{Output format}                                         \\ \hlx{vhv}
1212     @|var|      & @<name> @<type> @|\&optional| @<init>
1213                                            & @<type> @<name> @[= @<init>@];
1214                                                                 \\ \hlx{v}
1215     @|set|      & @<var> @<expr>           & @<var> = @<expr>;  \\ \hlx{v}
1216     @|update|   & @<var> @<op> @<expr>     & @<var> @<op>= @<expr>;
1217                                                                 \\ \hlx{v}
1218     @|cond|     & @<cond> @<conseq> @<alt> & @<cond> ? @<conseq> : @<alt>
1219                                                                 \\ \hlx{v}
1220     @|return|   & @<expr>                  & return @[@<expr>@];
1221                                                                 \\ \hlx{v}
1222     @|break|    & ---                      & break;             \\ \hlx{v}
1223     @|continue| & ---                      & continue;          \\ \hlx{v}
1224     @|expr|     & @<expr>                  & @<expr>;           \\ \hlx{v}
1225     @|call|     & @<func> @|\&rest| @<args>
1226                                            & @<func>(@<arg>_1,
1227                                                      $\ldots$,
1228                                                      @<arg>_n)  \\ \hlx{v}
1229     @|banner|   & @<control> @|\&rest| @<args>
1230                                            & /* @<banner> */    \\ \hlx{vhv}
1231     @|block|    & @<decls> @<body>         & \{ @[@<decls>@] @<body> \}
1232                                                                 \\ \hlx{v}
1233     @|if|       & @<cond> @<conseq> @|\&optional| @<alt>
1234                                            & if (@<cond>) @<conseq>
1235                                              @[else @<alt>@]    \\ \hlx{v}
1236     @|for|      & @<init> @<cond> @<update> @<body> &
1237       for (@<init>; @<cond>; @<update>) @<body>                 \\ \hlx{v}
1238     @|while|    & @<cond> @<body>          & while (@<cond>) @<body>
1239                                                                 \\ \hlx{v}
1240     @|do-while| & @<body> @<cond>          & do @<body> while (@<cond>);
1241                                                                 \\ \hlx{v}
1242     @|function| &
1243       \vtop{\hbox{\strut @<name> @<type> @<body>}
1244             \hbox{\strut \quad @|\&optional @<banner>|}
1245             \hbox{\strut \quad @|\&rest| @<banner-args>}} &
1246       \vtop{\hbox{\strut @[/* @<banner> */@]}
1247             \hbox{\strut @<type>_0 @<name>(@<type>_1 @<arg>_1, $\ldots$,
1248                                            @<type>_n @<arg>_n @[, \dots@])}
1249             \hbox{\strut \quad @<body>}}                        \\ \hlx*{vh}
1250   \end{tabular}
1251   \caption{Instruction classes}
1252   \label{tab:codegen.codegen.insts}
1253 \end{table}
1254
1255
1256 \subsection{Code generation} \label{sec:clang.codegen.codegen}
1257
1258 \begin{describe}{gf}{codegen-functions @<codegen> @> @<list>}
1259 \end{describe}
1260
1261 \begin{describe}{gf}
1262     {ensure-var @<codegen> @<name> @<type> \&optional @<init>}
1263 \end{describe}
1264
1265 \begin{describe}{gf}{emit-inst @<codegen> @<inst>}
1266 \end{describe}
1267
1268 \begin{describe}{gf}{emit-insts @<codegen> @<insts>}
1269 \end{describe}
1270
1271 \begin{describe}{gf}{emit-decl @<codegen> @<decl>}
1272 \end{describe}
1273
1274 \begin{describe}{gf}{emit-decls @<codegen> @<decls>}
1275 \end{describe}
1276
1277 \begin{describe}{fun}{emit-banner @<codegen> @<control> \&rest @<args>}
1278 \end{describe}
1279
1280 \begin{describe}{gf}{codegen-push @<codegen>}
1281 \end{describe}
1282
1283 \begin{describe}{gf}{codegen-pop @<codegen> @> @<decls> @<insts>}
1284 \end{describe}
1285
1286 \begin{describe}{gf}{codegen-pop-block @<codegen> @> @<block-inst>}
1287 \end{describe}
1288
1289 \begin{describe}{gf}
1290     {codegen-pop-function @<codegen> @<name> @<type> @> @<name>}
1291 \end{describe}
1292
1293 \begin{describe}{gf}{codegen-add-function @<codegen> @<function>}
1294 \end{describe}
1295
1296 \begin{describe}{fun}
1297     {codegen-build-function @<codegen> @<name> @<type> @<vars> @<insts>
1298       @> @<name>}
1299 \end{describe}
1300
1301 \begin{describe}{gf}{temporary-var @<codegen> @<type> @> @<name>}
1302 \end{describe}
1303
1304 \begin{describe}{mac}
1305     {with-temporary-var (@<codegen> @<var> @<type>) \\ \ind
1306       @<declaration>^* \\
1307       @<form>^* \-
1308      \nlret @<value>^*}
1309 \end{describe}
1310
1311 \begin{describe}{fun}{deliver-expr @<codegen> @<target> @<expr>}
1312 \end{describe}
1313
1314 \begin{describe}{fun}
1315     {deliver-call @<codegen> @<target> @<func> \&rest @<args>}
1316 \end{describe}
1317
1318 \begin{describe}{fun}{convert-stmts @<codegen> @<target> @<type> @<func>}
1319 \end{describe}
1320
1321 \begin{describe}{cls}{codegen () \&key :vars :insts (:temp-index 0)}
1322 \end{describe}
1323
1324 %%%--------------------------------------------------------------------------
1325 \section{Literal C code fragments} \label{sec:clang.fragment}
1326
1327 \begin{describe}{cls}{c-fragment () \&key :location :text}
1328 \end{describe}
1329
1330 \begin{describe}{gf}{c-fragment-text @<fragment> @> @<string>}
1331 \end{describe}
1332
1333 \begin{describe}{fun}
1334     {scan-c-fragment @<scanner> @<end-chars>
1335       @> @<result> @<success-flag> @<consumed-flag>}
1336 \end{describe}
1337
1338 \begin{describe}{fun}
1339     {parse-delimited-fragment @<scanner> @<begin> @<end> \&key :keep-end
1340       \nlret @<result> @<success-flag> @<consumed-flag>}
1341 \end{describe}
1342
1343 %%%----- That's all, folks --------------------------------------------------
1344
1345 %%% Local variables:
1346 %%% mode: LaTeX
1347 %%% TeX-master: "sod.tex"
1348 %%% TeX-PDF-mode: t
1349 %%% End: