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