chiark / gitweb /
doc/*.tex: Add, remove, and/or fix stubs to match reality.
[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}{#1}}
512   \x{c-type-bool} \x{c-type-char} \x{c-type-wchar-t} \x{c-type-signed-char}
513   \x{c-type-unsigned-char} \x{c-type-short} \x{c-type-unsigned-short}
514   \x{c-type-int} \x{c-type-unsigned} \x{c-type-long} \x{c-type-unsigned-long}
515   \x{c-type-long-long} \x{c-type-unsigned-long-long} \x{c-type-size-t}
516   \x{c-type-ptrdiff-t} \x{c-type-float} \x{c-type-double}
517   \x{c-type-long-double} \x{c-type-float-imaginary}
518   \x{c-type-double-imaginary} \x{c-type-long-double-imaginary}
519   \x{c-type-float-complex} \x{c-type-double-complex}
520   \x{c-type-long-double-complex} \x{c-type-va-list} \x{c-type-void}
521   A number of symbolic type specifiers for builtin types are predefined as
522   shown in \xref{tab:codegen.c-types.simple}.  These are all defined as if by
523   @|define-simple-c-type|, so can be used to construct qualified types.
524 \end{describe}
525
526 \begin{table}
527   \begin{tabular}[C]{ll}                                           \hlx*{hv}
528     \thd{C type}        & \thd{Specifiers}                      \\ \hlx{vhv}
529     @|void|             & @|void|                               \\ \hlx{v}
530     @|_Bool|            & @|bool|                               \\ \hlx{v}
531     @|char|             & @|char|                               \\ \hlx{}
532     @|wchar_t|          & @|wchar-t|                            \\ \hlx{v}
533     @|signed char|      & @|signed-char|, @|schar|              \\ \hlx{}
534     @|unsigned char|    & @|unsigned-char|, @|uchar|            \\ \hlx{v}
535     @|short|            & @|short|, @|signed-short|, @|short-int|,
536                           @|signed-short-int| @|sshort|         \\ \hlx{}
537     @|unsigned short|   & @|unsigned-short|, @|unsigned-short-int|,
538                           @|ushort|                             \\ \hlx{v}
539     @|int|              & @|int|, @|signed|, @|signed-int|,
540                           @|sint|                               \\ \hlx{}
541     @|unsigned int|     & @|unsigned|, @|unsigned-int|, @|uint| \\ \hlx{v}
542     @|long|             & @|long|, @|signed-long|, @|long-int|,
543                           @|signed-long-int|, @|slong|          \\ \hlx{}
544     @|unsigned long|    & @|unsigned-long|, @|unsigned-long-int|,
545                           @|ulong|                              \\ \hlx{v}
546     @|long long|        & @|long-long|, @|signed-long-long|,
547                           @|long-long-int|,                     \\ \hlx{}
548                         & \qquad @|signed-long-long-int|,
549                           @|llong|, @|sllong|                   \\ \hlx{v}
550     @|unsigned long long|
551                         & @|unsigned-long-long|, @|unsigned-long-long-int|,
552                           @|ullong|                             \\ \hlx{v}
553     @|size_t|           & @|size-t|                             \\ \hlx{}
554     @|ptrdiff_t|        & @|ptrdiff-t|                          \\ \hlx{v}
555     @|float|            & @|float|                              \\ \hlx{}
556     @|double|           & @|double|                             \\ \hlx{}
557     @|long double|      & @|long-double|                        \\ \hlx{v}
558     @|float _Imaginary| & @|float-imaginary|                    \\ \hlx{}
559     @|double _Imaginary|& @|double-imaginary|                   \\ \hlx{}
560     @|long double _Imaginary|
561                         & @|long-double-imaginary|              \\ \hlx{v}
562     @|float _Complex|   & @|float-complex|                      \\ \hlx{}
563     @|double _Complex|  & @|double-complex|                     \\ \hlx{}
564     @|long double _Complex|
565                         & @|long-double-complex|                \\ \hlx{v}
566     @|va_list|          & @|va-list|                            \\ \hlx*{vh}
567   \end{tabular}
568   \caption{Builtin symbolic type specifiers for simple C types}
569   \label{tab:codegen.c-types.simple}
570 \end{table}
571
572 \begin{describe}{fun}
573     {make-simple-type @<name> \&optional @<qualifiers> @> @<c-type>}
574   Return the (unique interned) simple C type object for the C type whose name
575   is @<name> (a string) and which has the given @<qualifiers> (a list of
576   keywords).
577 \end{describe}
578
579 \begin{describe}{gf}{c-type-name @<c-type> @> @<string>}
580   Returns the name of a @|simple-c-type| instance @<c-type> as an immutable
581   string.
582 \end{describe}
583
584 \begin{describe}{mac}
585     {define-simple-c-type
586        \=@{ @<name> @! (@<name>^+) @}
587          @{ @<string> @! (@<string>^*) @}                     \+\\
588          @[[ @|:export| @<export-flag> @]]
589       \-\nlret @<name>}
590   Define type specifiers for a new simple C type.  Each symbol @<name> is
591   defined as a symbolic type specifier for the (unique interned) simple C
592   type whose name is the value of (the first) @<string>.  Further, each
593   @<name> is defined to be a type operator: the type specifier @|(@<name>
594   @<qualifier>^*)| evaluates to the (unique interned) simple C type whose
595   name is (the first) @<string> and which has the @<qualifiers> (which are
596   evaluated).
597
598   Each of the @<string>s is associated with the resulting type for retrieval
599   by \descref{fun}{find-simple-c-type}.  Furthermore, a variable
600   @|c-type-@<name>| is defined, for the first @<name> only, and initialized
601   with the newly constructed C type object.
602
603   If @<export-flag> is true, then the @|c-type-@<name>| variable name, and
604   all of the @<name>s, are exported from the current package.
605 \end{describe}
606
607 \begin{describe}{fun}
608     {find-simple-c-type @<string> @> @{ @<simple-c-type> @! @|nil| @}}
609   If @<string> is the name of a simple C type, as established by the
610   \descref{mac}{define-simple-c-type}[macro], then return the corresponding
611   @|simple-c-type| object; otherwise, return @|nil|.
612 \end{describe}
613
614 \begin{describe}{cls}{tagged-c-type (qualifiable-c-type)
615     \&key :qualifiers :tag}
616   Provides common behaviour for C tagged types.  A @<tag> is a string
617   containing a C identifier.
618
619   Two tagged types are equal if and only if they have the same class, their
620   @<tag>s are @|string=|, and they have matching qualifiers.  (User-defined
621   subclasses may have additional methods on @|c-type-equal-p| which impose
622   further restrictions.)
623 \end{describe}
624 \begin{boxy}[Bug]
625   Sod maintains distinct namespaces for the three kinds of tagged types.  In
626   C, there is only one namespace for tags which is shared between enums,
627   structs and unions.
628 \end{boxy}
629
630 \begin{describe}{gf}{c-type-tag @<c-type> @> @<keyword>}
631 \end{describe}
632
633 \begin{describe}{fun}
634     {make-c-tagged-type @<kind> @<tag> \&optional @<qualifiers>
635       @> @<tagged-type>}
636 \end{describe}
637
638 \begin{describe}{gf}{c-tagged-type-kind @<c-type> @> @<keyword>}
639   Returns a keyword classifying the tagged @<c-type>: one of @|:enum|,
640   @|:struct| or @|:union|.  User-defined subclasses of @|tagged-c-type|
641   should return their own classification symbols.  It is intended that
642   @|(string-downcase (c-tagged-type-kind @<c-type>))| be valid C
643   syntax.\footnote{%
644     Alas, C doesn't provide a syntactic category for these keywords;
645     \Cplusplus\ calls them a @<class-key>.} %
646   There is a method defined for each of the built-in tagged type classes
647   @|c-struct-type|, @|c-union-type| and @|c-enum-type|.
648 \end{describe}
649
650 \begin{describe}{gf}{kind-c-tagged-type @<keyword> @> @<symbol>}
651   This is not quite the inverse of @|c-tagged-type-kind|.  Given a keyword
652   naming a kind of tagged type, return the name of the corresponding C
653   type class as a symbol.
654 \end{describe}
655
656 \begin{describe}{cls}{c-enum-type (tagged-c-type) \&key :qualifiers :tag}
657   Represents a C enumerated type.  An instance denotes the C type @|enum|
658   @<tag>.  See the direct superclass @|tagged-c-type| for details.
659
660   The type specifier @|(enum @<tag> @<qualifier>^*)| returns the (unique
661   interned) enumerated type with the given @<tag> and @<qualifier>s (all
662   evaluated).
663 \end{describe}
664
665 \begin{describe}{fun}
666     {make-enum-type @<tag> \&optional @<qualifiers> @> @<c-enum-type>}
667   Return the (unique interned) C type object for the enumerated C type whose
668   tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
669   keywords).
670 \end{describe}
671
672 \begin{describe}{cls}{c-struct-type (tagged-c-type) \&key :qualifiers :tag}
673   Represents a C structured type.  An instance denotes the C type @|struct|
674   @<tag>.  See the direct superclass @|tagged-c-type| for details.
675
676   The type specifier @|(struct @<tag> @<qualifier>^*)| returns the (unique
677   interned) structured type with the given @<tag> and @<qualifier>s (all
678   evaluated).
679 \end{describe}
680
681 \begin{describe}{fun}
682     {make-struct-type @<tag> \&optional @<qualifiers> @> @<c-struct-type>}
683   Return the (unique interned) C type object for the structured C type whose
684   tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
685   keywords).
686 \end{describe}
687
688 \begin{describe}{cls}{c-union-type (tagged-c-type) \&key :qualifiers :tag}
689   Represents a C union type.  An instance denotes the C type @|union|
690   @<tag>.  See the direct superclass @|tagged-c-type|
691   for details.
692
693   The type specifier @|(union @<tag> @<qualifier>^*)| returns the (unique
694   interned) union type with the given @<tag> and @<qualifier>s (all
695   evaluated).
696 \end{describe}
697 \begin{describe}{fun}
698     {make-union-type @<tag> \&optional @<qualifiers> @> @<c-union-type>}
699   Return the (unique interned) C type object for the union C type whose tag
700   is @<tag> (a string) and which has the given @<qualifiers> (a list of
701   keywords).
702 \end{describe}
703
704
705 \subsection{Compound C types} \label{sec:code.c-types.compound}
706
707 Some C types are \emph{compound types}: they're defined in terms of existing
708 types.  The classes which represent compound types implement a common
709 protocol.
710
711 \begin{describe}{gf}{c-type-subtype @<c-type> @> @<subtype>}
712   Returns the underlying type of a compound type @<c-type>.  Precisely what
713   this means depends on the class of @<c-type>.
714 \end{describe}
715
716
717 \subsection{Atomic types} \label{sec:clang.c-types.atomic}
718
719 Atomic types are compound types.  The subtype of an atomic type is simply the
720 underlying type of the object.  Note that, as far as Sod is concerned, atomic
721 types are not the same as atomic-qualified types: you must be consistent
722 about which you use.
723
724 \begin{describe}{cls}
725     {c-atomic-type (qualifiable-c-type) \&key :qualifiers :subtype}
726   Represents an atomic type.  An instance denotes the C type
727   @|_Atomic(@<subtype>)|.
728
729   The @<subtype> may be any C type.\footnote{%
730     C does not permit atomic function or array types.} %
731   Two atomic types are equal if and only if their subtypes are equal and they
732   have matching qualifiers.  It is possible, though probably not useful, to
733   have an atomic-qualified atomic type.
734
735   The type specifier @|(atomic @<type-spec> @<qualifier>^*)| returns a type
736   qualified atomic @<subtype>, where @<subtype> is the type specified by
737   @<type-spec> and the @<qualifier>s are qualifier keywords (which are
738   evaluated).
739 \end{describe}
740
741 \begin{describe}{fun}
742     {make-atomic-type @<c-type> \&optional @<qualifiers> @> @<c-atomic-type>}
743   Return an object describing the type qualified atomic @<subtype>.  If
744   @<subtype> is interned, then the returned atomic type object is interned
745   also.
746 \end{describe}
747
748
749 \subsection{Pointer types} \label{sec:clang.c-types.pointer}
750
751 Pointers are compound types.  The subtype of a pointer type is the type it
752 points to.
753
754 \begin{describe}{cls}
755     {c-pointer-type (qualifiable-c-type) \&key :qualifiers :subtype}
756   Represents a C pointer type.  An instance denotes the C type @<subtype>
757   @|*|@<qualifiers>.
758
759   The @<subtype> may be any C type.  Two pointer types are equal if and only
760   if their subtypes are equal and they have matching qualifiers.
761
762   The type specifier @|(* @<type-spec> @<qualifier>^*)| returns a type
763   qualified pointer-to-@<subtype>, where @<subtype> is the type specified by
764   @<type-spec> and the @<qualifier>s are qualifier keywords (which are
765   evaluated).  The synonyms @|ptr| and @|pointer| may be used in place of the
766   star @`*'.
767
768   The symbol @|string| is a type specifier for the type pointer to
769   characters; the symbol @|const-string| is a type specifier for the type
770   pointer to constant characters.
771 \end{describe}
772
773 \begin{describe}{fun}
774     {make-pointer-type @<c-type> \&optional @<qualifiers>
775       @> @<c-pointer-type>}
776   Return an object describing the type qualified pointer to @<subtype>.
777   If @<subtype> is interned, then the returned pointer type object is
778   interned also.
779 \end{describe}
780
781
782 \subsection{Array types} \label{sec:clang.c-types.array}
783
784 Arrays implement the compound-type protocol.  The subtype of an array type is
785 the array element type.
786
787 \begin{describe}{cls}{c-array-type (c-type) \&key :subtype :dimensions}
788   Represents a multidimensional C array type.  The @<dimensions> are a list
789   of dimension specifiers $d_0$, $d_1$, \ldots, $d_{n-1}$; an instance then
790   denotes the C type @<subtype> @|[$d_0$][$d_1$]$\ldots$[$d_{n-1}$]|.  An
791   individual dimension specifier is either a string containing a C integral
792   constant expression, or nil which is equivalent to an empty string.  Only
793   the first (outermost) dimension $d_0$ should be empty.
794
795   C doesn't actually have multidimensional arrays as a primitive notion;
796   rather, it permits an array (with known extent) to be the element type of
797   an array, which achieves an equivalent effect.  C arrays are stored in
798   row-major order: i.e., if we write down the indices of the elements of an
799   array in order of ascending address, the rightmost index varies fastest;
800   hence, the type constructed is more accurately an array of $d_0$ arrays of
801   $d_1$ of \ldots\ arrays of $d_{n-1}$ elements of type @<subtype>.  We shall
802   continue to abuse terminology and refer to multidimensional arrays.
803
804   The type specifier @|([] @<type-spec> @<dimension>^*)| constructs a
805   multidimensional array with the given @<dimension>s whose elements have the
806   type specified by @<type-spec>.  If no dimensions are given then a
807   single-dimensional array with unspecified extent.  The synonyms @|array|
808   and @|vector| may be used in place of the brackets @`[]'.
809 \end{describe}
810
811 \begin{describe}{fun}
812     {make-array-type @<subtype> @<dimensions> @> @<c-array-type>}
813   Return an object describing the type of arrays with given @<dimensions> and
814   with element type @<subtype> (an instance of @|c-type|).  The @<dimensions>
815   argument is a list whose elements are strings or nil; see the description
816   of the class @|c-array-type| above for details.
817 \end{describe}
818
819 \begin{describe}{gf}{c-array-dimensions @<c-type> @> @<list>}
820   Returns the dimensions of @<c-type>, an array type, as an immutable list.
821 \end{describe}
822
823
824 \subsection{Function types} \label{sec:clang.c-types.fun}
825
826 Function types implement the compound-type protocol.  The subtype of a
827 function type is the type of the function's return value.
828
829 \begin{describe}{cls}{argument}
830   Represents an ordinary function argument.
831 \end{describe}
832
833 \begin{describe}{fun}{argumentp @<value> @> @<generalized-boolean>}
834   Decide whether @<value> is an @<argument> object: if so, return non-nil; if
835   not return nil.
836 \end{describe}
837
838 \begin{describe}{fun}
839     {make-argument @<name> @<c-type> \&optional @<default> @> @<argument>}
840   Construct and a return a new @<argument> object.  The argument has type
841   @<c-type>, which must be a @|c-type| object, and is named @<name>.
842
843   The @<name> may be nil to indicate that the argument has no name: in this
844   case the argument will be formatted as an abstract declarator, which is not
845   suitable for function definitions.  If @<name> is not nil, then the
846   @<name>'s print representation, with @|*print-escape*| nil, is used as the
847   argument name.
848
849   A @<default> may be supplied.  If the argument is used in a
850   keyword-argument list (e.g., in a \descref{cls}{c-keyword-function-type}
851   [object]), and the @<default> value is provided and non-nil, then its
852   (unescaped) printed representation is used to provide a default value if
853   the keyword argument is not supplied by the caller.
854 \end{describe}
855
856 \begin{describe*}
857     {\dhead{fun}{argument-name @<argument> @> @<name>}
858      \dhead{fun}{argument-type @<argument> @> @<c-type>}
859      \dhead{fun}{argument-default @<argument> @> @<default>}}
860   Accessor functions for @|argument| objects.  They return the appropriate
861   component of the object, as set by to @|make-argument|.  The @<default> is
862   nil if no default was provided to @|make-argument|.
863 \end{describe*}
864
865 \begin{describe}{gf}
866     {commentify-argument-name @<name> @> @<commentified-name>}
867   Convert the argument name @<name> so that it's suitable to declare the
868   function in a header file.
869
870   Robust header files shouldn't include literal argument names in
871   declarations of functions or function types, since this restricts the
872   including file from defining such names as macros.  This generic function
873   is used to convert names into a safe form.
874
875   \begin{describe}{meth}{null}
876       {commentify-argument-name (@<name> null) @> nil}
877     Returns nil: if the argument name is already omitted, it's safe for use
878     in a header file.
879   \end{describe}
880   \begin{describe}{meth}{t}
881       {commentify-argument-name (@<name> t) @> @<string>}
882     Returns the print form of @<name> wrapped in a C comment, as
883     @`/*@<name>*/'.
884   \end{describe}
885 \end{describe}
886
887 \begin{describe}{fun}
888     {commentify-argument-names @<arguments> @> @<commentified-arguments>}
889   Convert the @<arguments> list so that it's suitable for use in a header
890   file.
891
892   The @<arguments> list should be a list whose items are @|argument| objects
893   or the keyword @|:ellipsis|.  The return value is a list constructed as
894   follows.  For each @|argument| object in the input list, there is a
895   corresponding @|argument| object in the returned list, with the same type,
896   and whose name is the result of @|commentify-argument-name| applied to the
897   input argument name; an @|:ellipsis| in the input list is passed through
898   unchanged.
899 \end{describe}
900
901 \begin{describe}{cls}{c-function-type (c-type) \&key :subtype :arguments}
902   Represents C function types.  An instance denotes the type of a C
903   function which accepts the @<arguments> and returns @<subtype>.
904
905   The @<arguments> are a possibly empty list.  All but the last element of
906   the list must be @|argument| objects; the final element may instead be the
907   keyword @|:ellipsis|, which denotes a variable argument list.
908
909   An @<arguments> list consisting of a single argument with type @|void| is
910   converted into an empty list.  On output as C code, an empty argument list
911   is written as @|void|.  It is not possible to represent a pre-ANSI C
912   function without prototypes.
913
914   Two function types are considered to be the same if their return types are
915   the same, and their argument lists consist of arguments with the same type,
916   in the same order, and either both or neither argument list ends with
917   @|:ellipsis|; argument names are not compared.
918
919   The type specifier
920   \begin{prog}
921     (fun @<return-type>
922          @{ (@<arg-name> @<arg-type>) @}^*
923          @[:ellipsis @! . @<form>@])
924   \end{prog}
925   constructs a function type.  The function has the subtype @<return-type>.
926   The remaining items in the type-specifier list are used to construct the
927   argument list.  The argument items are a possibly improper list, beginning
928   with zero or more \emph{explicit arguments}: two-item
929   @<arg-name>/@<arg-type> lists.  For each such list, an @|argument| object
930   is constructed with the given name (evaluated) and type.  Following the
931   explicit arguments, there may be
932   \begin{itemize}
933   \item nothing, in which case the function's argument list consists only of
934     the explicit arguments;
935   \item the keyword @|:ellipsis|, as the final item in the type-specifier
936     list, indicating a variable argument list may follow the explicit
937     arguments; or
938   \item a possibly-improper list tail, beginning with an atom either as a
939     list item or as the final list cdr, indicating that the entire list tail
940     is a Lisp expression which is to be evaluated to compute the remaining
941     arguments.
942   \end{itemize}
943   A tail expression may return a list of @|argument| objects, optionally
944   followed by an @|:ellipsis|.
945
946   For example,
947   \begin{prog}
948     (c-type (fun \=(lisp (c-type-subtype other-func))         \+\\
949                    ("first" int) . (c-function-arguments other-func))
950   \end{prog}
951   evaluates to a function type like @|other-func|, only with an additional
952   argument of type @|int| added to the front of its argument list.  This
953   could also have been written
954   \begin{prog}
955     (let (\=(args (c-function-arguments other-func))          \+\\
956             (ret (c-type-subtype other-func)))                \-\\ \ind
957       (c-type (fun \=(lisp ret) ("first" int) . args)
958   \end{prog}
959 \end{describe}
960
961 \begin{describe}{cls}
962     {c-keyword-function-type (c-function-type)
963       \&key :subtype :arguments :keywords}
964   Represents `functions' which accept keyword arguments.  Of course, actual C
965   functions can't accept keyword arguments directly, but this type is useful
966   for describing messages and methods which deal with keyword arguments.
967
968   An instance denotes the type of C function which accepts the position
969   argument list @<arguments>, and keyword arguments from the @<keywords>
970   list, and returns @<subtype>.  Either or both of the @<arguments> and
971   @<keywords> lists may be empty.  (It is important to note the distinction
972   between a function which doesn't accept keyword arguments, and one which
973   does but for which no keyword arguments are defined.  In particular, the
974   latter function can be changed later to accept a keyword argument without
975   breaking compatibility with old code.)  The @<arguments> and @<keywords>
976   lists must \emph{not} contain @|:ellipsis| markers: a function can accept
977   keywords, or a variable-length argument tail, but not both.
978
979   Keyword arguments may (but need not) have a \emph{default value} which is
980   supplied to the function body if the keyword is omitted.
981
982   Keyword functions are never considered to be the same as ordinary
983   functions.  Two keyword function types are considered to be the same if
984   their return types are the same, and their positional argument lists
985   consist of arguments with the same type, in the same order: the keyword
986   arguments accepted by the functions is not significant.
987
988   Keyword functions are constructed using an extended version of the @|fun|
989   specifier used for ordinary C function types.  The extended syntax is as
990   follows.
991   \begin{prog}
992     (fun \=@<return-type>
993            @{ (@<arg-name> @<arg-type>) @}^*                  \+\\
994            @{ \=:keys @{ (@<kw-name> @<kw-type> @[@<kw-default>@]) @}^*
995                    @[. @<form>@] @!                           \+\\
996                  . @<form> @}
997   \end{prog}
998   where either the symbol @|:keys| appears literally in the specifier, or the
999   @<form> evaluates to a list containing the symbol @|:keys|.  (If neither of
1000   these circumstances obtains, then the specifier constructs an ordinary
1001   function type.)
1002
1003   See the description of \descref{cls}{c-function-type} for how a trailing
1004   @<form> is handled.
1005
1006   The list of @<arg-name>s and @<arg-type>s describes the positional
1007   arguments.  The list of @<kw-name>s, @<kw-type>s and @<kw-defaults>s
1008   describes the keyword arguments.
1009 \end{describe}
1010
1011 \begin{describe}{fun}
1012     {make-function-type @<subtype> @<arguments> @> @<c-function-type>}
1013   Construct and return a new function type, returning @<subtype> and
1014   accepting the @<arguments>.
1015
1016   If the @<arguments> list contains a @|:keys| marker, then a
1017   \descref{cls}{c-keyword-function-type}[object] is returned: those arguments
1018   preceding the @|:keys| marker form the positional argument list, and those
1019   following the marker form the list of keyword arguments.
1020 \end{describe}
1021
1022 \begin{describe}{fun}
1023     {make-keyword-function-type @<subtype> @<arguments> @<keywords>
1024       \nlret @<c-keyword-function-type>}
1025   Construct and return a new keyword-function type, returning @<subtype> and
1026   accepting the @<arguments> and @<keywords>.
1027 \end{describe}
1028
1029 \begin{describe}{gf}
1030     {c-function-arguments @<c-function-type> @> @<arguments>}
1031   Return the (non-keyword) argument list of the @<c-function-type>.
1032 \end{describe}
1033
1034 \begin{describe}{gf}
1035     {c-function-keywords @<c-function-type> @> @<keywords>}
1036   Return the keyword-argument list of the @<c-function-type>.
1037 \end{describe}
1038
1039 \begin{describe}{fun}
1040     {commentify-function-type @<c-function-type> @> @<commentified-c-type>}
1041   Return a commentified version of the @<c-function-type>.
1042
1043   The returned type has the same subtype as the given type, and the argument
1044   list of the returned type is the result of applying
1045   @|commentify-argument-names| to the argument list of the given type.
1046 \end{describe}
1047
1048 \begin{describe}{fun}{reify-variable-argument-tail @<arguments> @> @<list>}
1049   If the @<argument> list contains an @|:ellipsis| marker, then replace it
1050   with a @|va_list|.  The name for the new argument, if any, is taken from
1051   the \descref{var}{*sod-ap*}[variable].  The new list is returned; the
1052   original list is not modified, but may share structure with the new list.
1053 \end{describe}
1054
1055 \begin{describe}{fun}
1056     {merge-keyword-lists @<what-function> @<lists> @> @<list>}
1057   Merge a number of keyword-argument lists together and return the result.
1058
1059   The @<what-function> is either nil or a function designator; see below.
1060
1061   The @<lists> parameter is a list consisting of a number of
1062   @|(@<report-function> . @<args>)| pairs: in each pair, @<report-function>
1063   is either nil or a function designator, and @<args> is a list of
1064   \descref{cls}{argument} objects.
1065
1066   The resulting list contains exactly one argument for each distinct argument
1067   name appearing in the input @<lists>; this argument will contain the
1068   default value from the earliest occurrence in the input @<lists> of an
1069   argument with that name.
1070
1071   If the same name appears multiple times with different types, a continuable
1072   error will be signalled, and one of the conflicting argument types will be
1073   chosen arbitrarily.  The @<what-function> will be called to establish
1074   information which will be reported to the user.  It will be called with no
1075   arguments and is expected to return two values:
1076   \begin{itemize}
1077   \item a file location @<floc> or other object acceptable to
1078     \descref{gf}{file-location}, to be used as the location of the main
1079     error; and
1080   \item an object @<what>, whose printed representation should be a noun
1081     phrase describing the object for which the argument lists are being
1082     combined.
1083   \end{itemize}
1084   The phrasing of the error message is `type mismatch in @<what>'.  Either,
1085   or both, of @<floc> and @<what> may be nil, though this is considered poor
1086   practice; if @<what-function> is nil, this is equivalent to a function
1087   which returns two nil values.  Following the error, the @<report-function>s
1088   for the @<args> lists containing the conflicting argument objects are
1089   called, in an arbitrary order, with a single argument which is the
1090   offending @|argument| object; the function is expected to issue information
1091   messages (see \descref{fun}{info}) to give more detail for diagnosing the
1092   conflict.  If a @<report-function> is nil, then nothing happens; this is
1093   considered poor practice.
1094 \end{describe}
1095
1096 \begin{describe}{fun}
1097     {pprint-c-function-type @<return-type> @<stream>
1098                             @<print-args> @<print-kernel>}
1099   Provides the top-level structure for printing C function types.
1100
1101   Output is written to @<stream> to describe a function type returning
1102   @<return-type>, whose declarator kernel (containing the name, and any
1103   further type operands) will be printed by @<print-kernel>, and whose
1104   arguments, if any, will be printed by @<print-args>.
1105
1106   The @<print-kernel> function is a standard kernel-printing function
1107   following the \descref{gf}{pprint-c-type}[protocol].
1108
1109   The @<print-args> function is given a single argument, which is the
1110   @<stream> to print on.  It should not print the surrounding parentheses.
1111
1112   The output written to @<stream> looks approximately like
1113   \begin{prog}
1114     @<return-type> @<kernel>(@<args>)
1115   \end{prog}
1116 \end{describe}
1117
1118 \begin{describe}{fun}{pprint-argument-list @<args> @<stream> @> @<flag>}
1119   Print an argument list to @<stream>.
1120
1121   The @<args> is a list of \descref{cls}{argument}[objects], optionally
1122   containing an @|:ellipsis| marker.  The function returns true if any
1123   arguments were actually printed.
1124 \end{describe}
1125
1126
1127 \subsection{Parsing C types} \label{sec:clang.c-types.parsing}
1128
1129 \begin{describe}{fun}
1130     {parse-c-type @<scanner>
1131       @> @<result> @<success-flag> @<consumed-flag>}
1132 \end{describe}
1133
1134 \begin{describe}{fun}
1135     {parse-declarator @<scanner> @<base-type> \&key :kernel :abstractp
1136       \nlret @<result> @<success-flag> @<consumed-flag>}
1137 \end{describe}
1138
1139
1140 \subsection{Class types} \label{sec:clang.c-types.class}
1141
1142 \begin{describe}{cls}
1143     {c-class-type (simple-c-type) \&key :class :tag :qualifiers :name}
1144 \end{describe}
1145
1146 \begin{describe*}
1147     {\dhead{gf}{c-type-class @<class-type> @> @<class>}
1148      \dhead{gf}{setf (c-type-class @<class-type>) @<class>}}
1149 \end{describe*}
1150
1151 \begin{describe}{fun}{find-class-type @<name> @> @<class-type-or-nil>}
1152 \end{describe}
1153
1154 \begin{describe}{fun}
1155     {make-class-type @<name> \&optional @<qualifiers> @> @<class-type>}
1156 \end{describe}
1157
1158 \begin{describe}{fun}{find-sod-class @<name> @> @<class>}
1159 \end{describe}
1160
1161 \begin{describe}{fun}{record-sod-class @<class>}
1162 \end{describe}
1163
1164 %%%--------------------------------------------------------------------------
1165 \section{Generating C code} \label{sec:clang.codegen}
1166
1167 This section deals with Sod's facilities for constructing and manipulating C
1168 expressions, declarations, instructions and definitions.
1169
1170
1171 \subsection{Temporary names} \label{sec:clang.codegen.temporaries}
1172
1173 Many C-level objects, especially ones with external linkage or inclusion in a
1174 header file, are assigned names which are simple strings, perhaps fixed ones,
1175 perhaps constructed.  Other objects don't need meaningful names, and
1176 suitably unique constructed names would be tedious and most likely rather
1177 opaque.  Therefore Sod has an ability to construct \emph{temporary names}.
1178
1179 These aren't temporary in the sense that they name C objects which have
1180 limited lifetimes at runtime.  Rather, the idea is that the names be
1181 significant only to small pieces of Lisp code, which will soon forget about
1182 them.
1183
1184 \subsubsection{The temporary name protocol}
1185 Temporary names are represented by objects which implement a simple protocol.
1186
1187 \begin{describe}{gf}{format-temporary-name @<var> @<stream>}
1188 \end{describe}
1189
1190 \begin{describe*}
1191     {\dhead{gf}{var-in-use-p @<var> @> @<generalized-boolean>}
1192      \dhead{gf}{setf (var-in-use-p @<var>) @<generalized-boolean>}}
1193 \end{describe*}
1194
1195 \subsubsection{Temporary name objects}
1196
1197 \begin{describe}{cls}{temporary-name () \&key :tag}
1198   A temporary name object.  This is the root of a small collection of
1199   subclasses, but is also usable on its own.
1200 \end{describe}
1201
1202 \begin{describe}{gf}{temp-tag @<name> @> @<tag>}
1203 \end{describe}
1204
1205 \begin{describe}{meth}{temporary-name}
1206     {commentify-argument-name (@<name> temporary-name) @> nil}
1207 \end{describe}
1208
1209 \begin{table}
1210   \begin{tabular}[C]{*2{>{\codeface}l}}                            \hlx*{hv}
1211     \thd{\textbf{Class}} & \thd{\textbf{Name format}}           \\ \hlx{vhv}
1212     temporary-name              & @<tag>                        \\
1213     temporary-argument          & sod__a@<tag>                  \\
1214     temporary-function          & sod__f@<tag>                  \\
1215     temporary-variable          & sod__v@<tag>                  \\ \hlx*{vh}
1216   \end{tabular}
1217   \caption{Temporary name formats}
1218   \label{tab:codegen.codegen.temps-format}
1219 \end{table}
1220
1221 \begin{describe}{cls}{temporary-argument (temporary-name) \&key :tag}
1222 \end{describe}
1223
1224 \begin{describe}{cls}{temporary-function (temporary-name) \&key :tag}
1225 \end{describe}
1226
1227 \begin{describe}{fun}{temporary-function @> @<name>}
1228 \end{describe}
1229
1230 \begin{describe}{cls}
1231     {temporary-variable (temporary-name) \&key :tag :in-use-p}
1232 \end{describe}
1233
1234 \subsubsection{Well-known `temporary' names}
1235
1236 \begin{table}
1237   \def\x#1{\desclabel{var}{#1}}
1238   \x{*sod-ap*} \x{*sod-master-ap*} \x{*null-pointer*}
1239   \begin{tabular}[C]{*2{>{\codeface}l}}                            \hlx*{hv}
1240     \thd{\textbf{Variable}} & \thd{\textbf{Name format}}        \\ \hlx{vhv}
1241     {}*sod-ap*                  & sod__ap                       \\
1242     {}*sod-master-ap*           & sod__master_ap                \\
1243     {}*null-pointer*            & NULL                          \\ \hlx*{vh}
1244   \end{tabular}
1245   \caption{Well-known temporary names}
1246   \label{tab:codegen.codegen.well-known-temps}
1247 \end{table}
1248
1249
1250 \subsection{Instructions} \label{sec:clang.codegen.insts}
1251
1252 \begin{describe}{cls}{inst () \&key}
1253 \end{describe}
1254
1255 \begin{describe}{gf}{inst-metric @<inst>}
1256 \end{describe}
1257
1258 \begin{describe}{mac}
1259     {definst @<code> (@<streamvar> \&key @<export>) (@<arg>^*)  \\ \ind
1260       @[[ @<declaration>^* @! @<doc-string> @]]                 \\
1261       @<form>^*
1262      \-\nlret @<code>}
1263 \end{describe}
1264
1265 \begin{describe}{mac}
1266     {format-compound-statement
1267         (@<stream> @<child> \&optional @<morep>)                \\ \ind
1268       @<declaration>^*                                          \\
1269       @<form>^*}
1270 \end{describe}
1271
1272 \begin{describe}{fun}
1273     {format-banner-comment @<stream> @<control> \&rest @<args>}
1274 \end{describe}
1275
1276 \begin{table}
1277   \begin{tabular}[C]{ll>{\codeface}l}                              \hlx*{hv}
1278     \thd{Class name} &
1279     \thd{Arguments} &
1280     \thd{Output format}                                         \\ \hlx{vhv}
1281     @|var|      & @<name> @<type> @|\&optional| @<init>
1282                                            & @<type> @<name> @[= @<init>@];
1283                                                                 \\ \hlx{v}
1284     @|set|      & @<var> @<expr>           & @<var> = @<expr>;  \\ \hlx{v}
1285     @|update|   & @<var> @<op> @<expr>     & @<var> @<op>= @<expr>;
1286                                                                 \\ \hlx{v}
1287     @|cond|     & @<cond> @<conseq> @<alt> & @<cond> ? @<conseq> : @<alt>
1288                                                                 \\ \hlx{v}
1289     @|return|   & @<expr>                  & return @[@<expr>@];
1290                                                                 \\ \hlx{v}
1291     @|break|    & ---                      & break;             \\ \hlx{v}
1292     @|continue| & ---                      & continue;          \\ \hlx{v}
1293     @|expr|     & @<expr>                  & @<expr>;           \\ \hlx{v}
1294     @|call|     & @<func> @|\&rest| @<args>
1295                                            & @<func>(@<arg>_1,
1296                                                      $\ldots$,
1297                                                      @<arg>_n)  \\ \hlx{v}
1298     @|banner|   & @<control> @|\&rest| @<args>
1299                                            & /* @<banner> */    \\ \hlx{vhv}
1300     @|block|    & @<decls> @<body>         & \{ @[@<decls>@] @<body> \}
1301                                                                 \\ \hlx{v}
1302     @|if|       & @<cond> @<conseq> @|\&optional| @<alt>
1303                                            & if (@<cond>) @<conseq>
1304                                              @[else @<alt>@]    \\ \hlx{v}
1305     @|for|      & @<init> @<cond> @<update> @<body> &
1306       for (@<init>; @<cond>; @<update>) @<body>                 \\ \hlx{v}
1307     @|while|    & @<cond> @<body>          & while (@<cond>) @<body>
1308                                                                 \\ \hlx{v}
1309     @|do-while| & @<body> @<cond>          & do @<body> while (@<cond>);
1310                                                                 \\ \hlx{v}
1311     @|function| &
1312       \vtop{\hbox{\strut @<name> @<type> @<body>}
1313             \hbox{\strut \quad @|\&optional @<banner>|}
1314             \hbox{\strut \quad @|\&rest| @<banner-args>}} &
1315       \vtop{\hbox{\strut @[/* @<banner> */@]}
1316             \hbox{\strut @<type>_0 @<name>(@<type>_1 @<arg>_1, $\ldots$,
1317                                            @<type>_n @<arg>_n @[, \dots@])}
1318             \hbox{\strut \quad @<body>}}                        \\ \hlx*{vh}
1319   \end{tabular}
1320   \caption{Instruction classes}
1321   \label{tab:codegen.codegen.insts}
1322 \end{table}
1323
1324 \begin{describe*}
1325     {\dhead*{cls}{@<code>-inst (inst) \&key \dots}
1326      \dhead*{fn}{make-@<code>-inst \dots}
1327      \dhead*{gf}{inst-@<slot> @<inst> @> @<value>}}
1328   \def\instclass#1#2#3{%
1329     #1{cls}{#3-inst}[#2]%
1330     #1{fun}{make-#3-inst}[#2]%
1331   }
1332   \def\instslot#1#2#3{#1{gf}{inst-#3}[#2]}
1333   \def\makelabels#1#2{%
1334     \def\x{\instclass{#1}{#2}}
1335       \x{var} \x{set} \x{update} \x{cond} \x{return} \x{break} \x{continue}
1336       \x{expr} \x{call} \x{banner} \x{block} \x{if} \x{for} \x{while}
1337       \x{do-while} \x{function}
1338     \def\x{\instslot{#1}{#2}}
1339       \x{name} \x{type} \x{init} \x{var} \x{expr} \x{op} \x{cond} \x{conseq}
1340       \x{alt} \x{func} \x{args} \x{control} \x{decls} \x{body} \x{update}
1341       \x{banner} \x{banner-args}
1342   }
1343   \makelabels{\desclabel}{|(}
1344
1345   Sod provides a number of built-in instruction types generated by
1346   \descref{mac}{definst}: see \xref{tab:codegen.codegen.insts}.
1347
1348   \makelabels{\descindex}{|)}
1349 \end{describe*}
1350
1351
1352 \subsection{Code generation} \label{sec:clang.codegen.codegen}
1353
1354 \begin{describe}{gf}{codegen-functions @<codegen> @> @<list>}
1355 \end{describe}
1356
1357 \begin{describe}{gf}
1358     {ensure-var @<codegen> @<name> @<type> \&optional @<init>}
1359 \end{describe}
1360
1361 \begin{describe}{gf}{emit-inst @<codegen> @<inst>}
1362 \end{describe}
1363
1364 \begin{describe}{gf}{emit-insts @<codegen> @<insts>}
1365 \end{describe}
1366
1367 \begin{describe}{gf}{emit-decl @<codegen> @<decl>}
1368 \end{describe}
1369
1370 \begin{describe}{gf}{emit-decls @<codegen> @<decls>}
1371 \end{describe}
1372
1373 \begin{describe}{fun}{emit-banner @<codegen> @<control> \&rest @<args>}
1374 \end{describe}
1375
1376 \begin{describe}{gf}{codegen-push @<codegen>}
1377 \end{describe}
1378
1379 \begin{describe}{gf}{codegen-pop @<codegen> @> @<decls> @<insts>}
1380 \end{describe}
1381
1382 \begin{describe}{gf}{codegen-pop-block @<codegen> @> @<block-inst>}
1383 \end{describe}
1384
1385 \begin{describe}{gf}
1386     {codegen-pop-function @<codegen> @<name> @<type> @> @<name>}
1387 \end{describe}
1388
1389 \begin{describe}{gf}{codegen-add-function @<codegen> @<function>}
1390 \end{describe}
1391
1392 \begin{describe}{fun}
1393     {codegen-build-function @<codegen> @<name> @<type> @<vars> @<insts>
1394       @> @<name>}
1395 \end{describe}
1396
1397 \begin{describe}{gf}{temporary-var @<codegen> @<type> @> @<name>}
1398 \end{describe}
1399
1400 \begin{describe}{mac}
1401     {with-temporary-var (@<codegen> @<var> @<type>)             \\ \ind
1402       @<declaration>^*                                          \\
1403       @<form>^*
1404      \-\nlret @<value>^*}
1405 \end{describe}
1406
1407 \begin{describe}{fun}{deliver-expr @<codegen> @<target> @<expr>}
1408 \end{describe}
1409
1410 \begin{describe}{fun}
1411     {deliver-call @<codegen> @<target> @<func> \&rest @<args>}
1412 \end{describe}
1413
1414 \begin{describe}{fun}{convert-stmts @<codegen> @<target> @<type> @<func>}
1415 \end{describe}
1416
1417 \begin{describe}{cls}{codegen () \&key :vars :insts (:temp-index 0)}
1418 \end{describe}
1419
1420 %%%--------------------------------------------------------------------------
1421 \section{Literal C code fragments} \label{sec:clang.fragment}
1422
1423 \begin{describe}{cls}{c-fragment () \&key :location :text}
1424 \end{describe}
1425
1426 \begin{describe}{gf}{c-fragment-text @<fragment> @> @<string>}
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: