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