6ef829b3defc9912a5ae8ee2b9991d74052300ca
[sod] / doc / sod-protocol.tex
1 %%% -*-latex-*-
2 %%%
3 %%% Description of the internal class structure and protocol
4 %%%
5 %%% (c) 2009 Straylight/Edgeware
6 %%%
7
8 %%%----- Licensing notice ---------------------------------------------------
9 %%%
10 %%% This file is part of the Simple Object Definition system.
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{Protocol overview} \label{ch:proto}
27
28 This chapter provides an overview of the Sod translator's internal object
29 model. It describes most of the important classes and generic functions, how
30 they are used to build a model of a Sod module and produce output code, and
31 how an extension might modify the translator's behaviour.
32
33 I assume familiarity with the Common Lisp Object System (CLOS). Familiarity
34 with the CLOS Metaobject Protocol isn't necessary but may be instructive.
35
36 %%%--------------------------------------------------------------------------
37 \section{A tour through the translator}
38
39 At the very highest level, the Sod translator works in two phases: it
40 \emph{parses} source files into an internal representation, and then it
41 \emph{generates} output files from the internal representation.
42
43 The function @|read-module| is given a pathname for a file: it opens the
44 file, parses the program text, and returns a @|module| instance describing
45 the classes and other items found.
46
47 At the other end, the main output function is @|output-module|, which is
48 given a module, an output stream and a
49
50
51 %%%--------------------------------------------------------------------------
52 \section{Specification conventions} \label{sec:proto.conventions}
53
54 Throughout this specification, the phrase `it is an error' indicates that a
55 particular circumstance is erroneous and results in unspecified and possibly
56 incorrect behaviour. In particular, the situation need not be immediately
57 diagnosed, and the consequences may be far-reaching.
58
59 The following conventions apply throughout this specification.
60
61 \begin{itemize}
62
63 \item If a specification describes an argument as having a particular type or
64 syntax, then it is an error to provide an argument not having that
65 particular type or syntax.
66
67 \item If a specification describes a function then that function might be
68 implemented as a generic function; it is an error to attempt to (re)define
69 it as a generic function, or to attempt to add methods to it. A function
70 specified as being a generic function will certainly be so; if user methods
71 are permitted on the generic function then this will be specified.
72
73 \item Where a class precedence list is specified, either explicitly or
74 implicitly by a class hierarchy, the implementation may include additional
75 superclasses not specified here. Such additional superclasses will not
76 affect the order of specified classes in the class precedence lists either
77 of specified classes themselves or of user-defined subclasses of specified
78 classes.
79
80 \item Unless otherwise specified, generic functions use the standard method
81 combination.
82
83 \item The specifications for methods are frequently brief; they should be
84 read in conjunction with and in the context of the specification for the
85 generic function and specializing classes, if any.
86
87 \item An object $o$ is a \emph{direct instance} of a class $c$ if @|(eq
88 (class-of $o$) $c$)|; $o$ is an \emph{instance} of $c$ if it is a direct
89 instance of any subclass of $c$.
90
91 \item If a class is specified as being \emph{abstract} then it is an error to
92 construct direct instances of it, e.g., using @|make-instance|.
93
94 \item If an object is specified as being \emph{immutable} then it is an error
95 to mutate it, e.g., using @|(setf (slot-value \ldots) \ldots)|. Programs
96 may rely on immutable objects retaining their state.
97
98 \item A value is \emph{fresh} if it is guaranteed to be not @|eql| to any
99 previously existing value.
100
101 \item Unless otherwise specified, it is an error to change the class of an
102 instance of any class described here; and it is an error to change the
103 class of an object to a class described here.
104
105 \end{itemize}
106
107 \subsection{Format of the entries} \label{sec:proto.conventions.format}
108
109 Most symbols defined by the protocol have their own entries. An entry begins
110 with a header line, showing a synopsis of the symbol on the left, and the
111 category (function, class, macro, etc.) on the right.
112
113 \begin{describe}{fun}{example-function @<required>
114 \&optional @<optional>
115 \&rest @<rest>
116 \&key :keyword}
117 The synopsis for a function, generic function or method describes the
118 function's lambda-list using the usual syntax. Note that keyword arguments
119 are shown by naming their keywords; in the description, the value passed
120 for the keyword argument @|:keyword| is shown as @<keyword>.
121
122 For a method, specializers are shown using the usual @|defmethod| syntax,
123 e.g.,
124 \begin{quote} \sffamily
125 some-generic-function ((@<specialized> list) @<unspecialized>)
126 \end{quote}
127 \end{describe}
128
129 \begin{describe}{mac}{example-macro
130 ( @{ @<symbol> @! (@<symbol> @<form>) @}^* ) \\ \push
131 @[[ @<declaration>^* @! @<documentation-string> @]] \\
132 @<body-form>^*}
133 The synopsis for a macro describes the acceptable syntax using the
134 following notation.
135 \begin{itemize}
136 \item Literal symbols, e.g., keywords and parenthesis, are shown in
137 @|monospace|.
138 \item Metasyntactic variables are shown in @<italics>.
139 \item Items are grouped together by braces `@{ $\dots$ @}'. The notation
140 `@{ $\dots$ @}^*' indicates that the enclosed items may be repeated zero
141 or more times; `@{ $\dots$ @}^+' indicates that the enclosed items may be
142 repeated one or more times. This notation may be applied to a single
143 item without the braces.
144 \item Optional items are shown enclosed in brackets `@[ $\dots$ @]'.
145 \item Alternatives are separated by vertical bars `@!'; the vertical bar
146 has low precedence, so alternatives extend as far as possible between
147 bars and up to the enclosing brackets if any.
148 \item A sequence of alternatives enclosed in double-brackets `@[[ $\ldots$
149 @]]' indicates that the alternatives may occur in any order, but each may
150 appear at most once unless marked by a star.
151 \end{itemize}
152 For example, the notation at the head of this example describes syntax
153 for @|let|.
154 \end{describe}
155
156 \begin{describe}{cls}{example-class (direct-super other-direct-super) \&key
157 :initarg}
158 The synopsis for a class lists the class's direct superclasses, and the
159 acceptable initargs in the form of a lambda-list. The initargs may be
160 passed to @|make-instance| when constructing an instance of the class or a
161 subclass of it. If instances of the class may be reinitialized, or if
162 objects can be changed to be instances of the class, then these initargs
163 may also be passed to @|reinitialize-instance| and/or @|change-class| as
164 applicable; the class description will state explicitly when these
165 operations are allowed.
166 \end{describe}
167
168 %%%--------------------------------------------------------------------------
169 \section{C type representation} \label{sec:proto.c-types}
170
171 \subsection{Overview} \label{sec:proto.c-types.over}
172
173 The Sod translator represents C types in a fairly simple and direct way.
174 However, because it spends a fair amount of its time dealing with C types, it
175 provides a number of useful operations and macros.
176
177 The class hierarchy is shown in~\xref{fig:proto.c-types}.
178
179 \begin{figure} \centering
180 \parbox{10pt}{\begin{tabbing}
181 @|c-type| \\ \push
182 @|qualifiable-c-type| \\ \push
183 @|simple-c-type| \\ \push
184 @|c-class-type| \- \\
185 @|tagged-c-type| \\ \push
186 @|c-struct-type| \\
187 @|c-union-type| \\
188 @|c-enum-type| \- \\
189 @|c-pointer-type| \- \\
190 @|c-array-type| \\
191 @|c-function-type|
192 \end{tabbing}}
193 \caption{Classes representing C types}
194 \label{fig:proto.c-types}
195 \end{figure}
196
197 C type objects are immutable unless otherwise specified.
198
199 \subsubsection{Constructing C type objects}
200 There is a constructor function for each non-abstract class of C type object.
201 Note, however, that constructor functions need not generate a fresh type
202 object if a previously existing type object is suitable. In this case, we
203 say that the objects are \emph{interned}. Some constructor functions are
204 specified to return interned objects: programs may rely on receiving the same
205 (@|eq|) type object for similar (possibly merely @|equal|) arguments. Where
206 not specified, clients may still not rely on receiving fresh objects.
207
208 A convenient S-expression notation is provided by the @|c-type| macro. Use
209 of this macro is merely an abbreviation for corresponding use of the various
210 constructor functions, and therefore interns type objects in the same manner.
211 The syntax accepted by the macro can be extended in order to support new
212 classes: see @|defctype|, @|c-type-alias| and @|define-c-type-syntax|.
213
214 The descriptions of each of the various classes include descriptions of the
215 initargs which may be passed to @|make-instance| when constructing a new
216 instance of the class. However, the constructor functions and S-expression
217 syntax are strongly recommended over direct use of @|make-instance|.
218
219 \subsubsection{Printing}
220 There are two protocols for printing C types. Unfortunately they have
221 similar names.
222 \begin{itemize}
223 \item The @|print-c-type| function prints a C type value using the
224 S-expression notation. It is mainly useful for diagnostic purposes.
225 \item The @|pprint-c-type| function prints a C type as a C-syntax
226 declaration.
227 \end{itemize}
228 Neither generic function defines a default primary method; subclasses of
229 @|c-type| must define their own methods in order to print correctly.
230
231 \subsection{The C type root class} \label{sec:proto.c-types.root}
232
233 \begin{describe}{cls}{c-type ()}
234 The class @|c-type| marks the root of the built-in C type hierarchy.
235
236 Users may define subclasses of @|c-type|. All non-abstract subclasses must
237 have a primary method defined on @|pprint-c-type|; unless instances of the
238 subclass are interned, a method on @|c-type-equal-p| is also required.
239
240 The class @|c-type| is abstract.
241 \end{describe}
242
243 \subsection{C type S-expression notation} \label{sec:proto.c-types.sexp}
244
245 The S-expression representation of a type is described syntactically as a
246 type specifier. Type specifiers fit into two syntactic categories.
247 \begin{itemize}
248 \item A \emph{symbolic type specifier} consists of a symbol. It has a
249 single, fixed meaning: if @<name> is a symbolic type specifier, then each
250 use of @<name> in a type specifier evaluates to the same (@|eq|) type
251 object, until the @<name> is redefined.
252 \item A \emph{type operator} is a symbol; the corresponding specifier is a
253 list whose @|car| is the operator. The remaining items in the list are
254 arguments to the type operator.
255 \end{itemize}
256
257 \begin{describe}{mac}{c-type @<type-spec> @to @<type>}
258 Evaluates to a C type object, as described by the type specifier
259 @<type-spec>.
260 \end{describe}
261
262 \begin{describe}{mac}{
263 defctype @{ @<name> @! (@<name>^*) @} @<type-spec> @to @<names>}
264 Defines a new symbolic type specifier @<name>; if a list of @<name>s is
265 given, then all are defined in the same way. The type constructed by using
266 any of the @<name>s is as described by the type specifier @<type-spec>.
267
268 The resulting type object is constructed once, at the time that the macro
269 expansion is evaluated; the same (@|eq|) value is used each time any
270 @<name> is used in a type specifier.
271 \end{describe}
272
273 \begin{describe}{mac}{c-type-alias @<original> @<alias>^* @to @<aliases>}
274 Defines each @<alias> as being a type operator identical in behaviour to
275 @<original>. If @<original> is later redefined then the behaviour of the
276 @<alias>es changes too.
277 \end{describe}
278
279 \begin{describe}{mac}{%
280 define-c-type-syntax @<name> @<lambda-list> \\ \push
281 @<form>^* \-\\
282 @to @<name>}
283 Defines the symbol @<name> as a new type operator. When a list of the form
284 @|(@<name> @<argument>^*)| is used as a type specifier, the @<argument>s
285 are bound to fresh variables according to @<lambda-list> (a destructuring
286 lambda-list) and the @<form>s evaluated in order in the resulting lexical
287 environment as an implicit @|progn|. The value should be a Lisp form which
288 will evaluate to the type specified by the arguments.
289
290 The @<form>s may call @|expand-c-type-spec| in order to recursively expand
291 type specifiers among its arguments.
292 \end{describe}
293
294 \begin{describe}{fun}{expand-c-type-spec @<type-spec> @to @<form>}
295 Returns the Lisp form that @|(c-type @<type-spec>)| would expand into.
296 \end{describe}
297
298 \begin{describe}{gf}{%
299 print-c-type @<stream> @<type> \&optional @<colon> @<atsign>}
300 Print the C type object @<type> to @<stream> in S-expression form. The
301 @<colon> and @<atsign> arguments may be interpreted in any way which seems
302 appropriate: they are provided so that @|print-c-type| may be called via
303 @|format|'s @|\char`\~/\dots/| command; they are not set when
304 @|print-c-type| is called by Sod functions.
305
306 There should be a method defined for every C type class; there is no
307 default method.
308 \end{describe}
309
310 \subsection{Comparing C types} \label{sec:proto.c-types.cmp}
311
312 It is necessary to compare C types for equality, for example when checking
313 argument lists for methods. This is done by @|c-type-equal-p|.
314
315 \begin{describe}{gf}{c-type-equal-p @<type>_1 @<type>_2 @to @<boolean>}
316 The generic function @|c-type-equal-p| compares two C types @<type>_1 and
317 @<type>_2 for equality; it returns true if the two types are equal and
318 false if they are not.
319
320 Two types are equal if they are structurally similar, where this property
321 is defined by methods for each individual class; see the descriptions of
322 the classes for the details.
323
324 The generic function @|c-type-equal-p| uses the @|and| method combination.
325
326 \begin{describe}{meth}{c-type-equal-p @<type>_1 @<type>_2}
327 A default primary method for @|c-type-equal-p| is defined. It simply
328 returns @|nil|. This way, methods can specialize on both arguments
329 without fear that a call will fail because no methods are applicable.
330 \end{describe}
331 \begin{describe}{ar-meth}{c-type-equal-p @<type>_1 @<type>_2}
332 A default around-method for @|c-type-equal-p| is defined. It returns
333 true if @<type>_1 and @<type>_2 are @|eql|; otherwise it delegates to the
334 primary methods. Since several common kinds of C types are interned,
335 this is a common case worth optimizing.
336 \end{describe}
337 \end{describe}
338
339 \subsection{Outputting C types} \label{sec:proto.c-types.output}
340
341 \begin{describe}{gf}{pprint-c-type @<type> @<stream> @<kernel>}
342 The generic function @|pprint-c-type| pretty-prints to @<stream> a C-syntax
343 declaration of an object or function of type @<type>. The result is
344 written to @<stream>.
345
346 A C declaration has two parts: a sequence of \emph{declaration specifiers}
347 and a \emph{declarator}. The declarator syntax involves parentheses and
348 operators, in order to reflect the operators applicable to the declared
349 variable. For example, the name of a pointer variable is preceded by @`*';
350 the name of an array is followed by dimensions enclosed in @`['\dots @`]'.
351
352 The @<kernel> argument must be a function designator (though see the
353 standard around-method); it is invoked as
354 \begin{quote} \codeface
355 (funcall @<kernel> @<stream> @<priority> @<spacep>)
356 \end{quote}
357 It should write to @<stream> -- which may not be the same stream originally
358 passed into the generic function -- the `kernel' of the declarator, i.e.,
359 the part to which prefix and/or postfix operators are attached to form the
360 full declarator.
361
362 The methods on @|pprint-c-type| specialized for compound types work by
363 recursively calling @|pprint-c-type| on the subtype, passing down a closure
364 which prints the necessary additional declarator operators before calling
365 the original @<kernel> function. The additional arguments @<priority> and
366 @<spacep> support this implementation technique.
367
368 The @<priority> argument describes the surrounding operator context. It is
369 zero if no type operators are directly attached to the kernel (i.e., there
370 are no operators at all, or the kernel is enclosed in parentheses), one if
371 a prefix operator is directly attached, or two if a postfix operator is
372 directly attached. If the @<kernel> function intends to provide its own
373 additional declarator operators, it should check the @<priority> in order
374 to determine whether parentheses are necessary. See also the
375 @|maybe-in-parens| macro (page~\pageref{mac:maybe-in-parens}).
376
377 The @<spacep> argument indicates whether a space needs to be printed in
378 order to separate the declarator from the declaration specifiers. A kernel
379 which contains an identifier should insert a space before the identifier
380 when @<spacep> is non-nil. An `empty' kernel, as found in an abstract
381 declarator (one that specifies no name), looks more pleasing without a
382 trailing space. See also the @|c-type-space| function
383 (page~\pageref{fun:c-type-space}).
384
385 Every concrete subclass of @|c-type| is expected to provide a primary
386 method on this function. There is no default primary method.
387
388 \begin{describe}{ar-meth}{pprint-c-type @<type> @<stream> @<kernel>}
389 A default around method is defined on @|pprint-c-type| which `canonifies'
390 non-function @<kernel> arguments. In particular:
391 \begin{itemize}
392 \item if @<kernel> is nil, then @|pprint-c-type| is called recursively
393 with a @<kernel> function that does nothing; and
394 \item if @<kernel> is any other kind of object, then @|pprint-c-type| is
395 called recursively with a @<kernel> function that prints the object as
396 if by @|princ|, preceded if necessary by space using @|c-type-space|.
397 \end{itemize}
398 \end{describe}
399 \end{describe}
400
401 \begin{describe}{fun}{c-type-space @<stream>}
402 Writes a space and other pretty-printing instructions to @<stream> in order
403 visually to separate a declarator from the preceding declaration
404 specifiers. The precise details are subject to change.
405 \end{describe}
406
407 \begin{describe}{mac}{%
408 maybe-in-parens (@<stream-var> @<guard-form>) \\ \push
409 @<form>^*}
410 The @<guard-form> is evaluated, and then the @<form>s are evaluated in
411 sequence within a pretty-printer logical block writing to the stream named
412 by the symbol @<stream-var>. If the @<guard-form> evaluates to nil, then
413 the logical block has empty prefix and suffix strings; if it evaluates to a
414 non-nil value, then the logical block has prefix and suffix @`(' and @`)'
415 respectively.
416
417 Note that this may cause @<stream> to be bound to a different stream object
418 within the @<form>s.
419 \end{describe}
420
421 \subsection{Type qualifiers and qualifiable types}
422 \label{sec:proto.ctypes.qual}
423
424 \begin{describe}{cls}{qualifiable-c-type (c-type) \&key :qualifiers}
425 The class @|qualifiable-c-type| describes C types which can bear
426 `qualifiers' (\Cplusplus\ calls them `cv-qualifiers'): @|const|,
427 @|restrict| and @|volatile|.
428
429 The @<qualifiers> are a list of keyword symbols @|:const|, @|:restrict| and
430 @|:volatile|. There is no built-in limitation to these particular
431 qualifiers; others keywords may be used, though this isn't recommended.
432
433 Two qualifiable types are equal only if they have \emph{matching
434 qualifiers}: i.e., every qualifier attached to one is also attached to
435 the other: order is not significant, and neither is multiplicity.
436
437 The class @|qualifiable-c-type| is abstract.
438 \end{describe}
439
440 \begin{describe}{gf}{c-type-qualifiers @<type> @to @<list>}
441 Returns the qualifiers of the @|qualifiable-c-type| instance @<type> as an
442 immutable list.
443 \end{describe}
444
445 \begin{describe}{fun}{qualify-type @<type> @<qualifiers>}
446 The argument @<type> must be an instance of @|qualifiable-c-type|,
447 currently bearing no qualifiers, and @<qualifiers> a list of qualifier
448 keywords. The result is a C type object like @<c-type> except that it
449 bears the given @<qualifiers>.
450
451 The @<type> is not modified. If @<type> is interned, then the returned
452 type will be interned.
453 \end{describe}
454
455 \begin{describe}{fun}{format-qualifiers @<qualifiers>}
456 Returns a string containing the qualifiers listed in @<qualifiers> in C
457 syntax, with a space after each. In particular, if @<qualifiers> is
458 non-null then the final character of the returned string will be a space.
459 \end{describe}
460
461 \subsection{Leaf types} \label{sec:proto.c-types.leaf}
462
463 A \emph{leaf type} is a type which is not defined in terms of another type.
464 In Sod, the leaf types are
465 \begin{itemize}
466 \item \emph{simple types}, including builtin types like @|int| and @|char|,
467 as well as type names introduced by @|typename|, because Sod isn't
468 interested in what the type name means, merely that it names a type; and
469 \item \emph{tagged types}, i.e., enum, struct and union types which are named
470 by a keyword identifying the kind of type, and a \emph{tag}.
471 \end{itemize}
472
473 \begin{describe}{cls}{simple-c-type (qualifiable-c-type)
474 \&key :qualifiers :name}
475 The class of `simple types'; an instance denotes the type @<qualifiers>
476 @<name>.
477
478 A simple type object maintains a \emph{name}, which is a string whose
479 contents are the C name for the type. The initarg @|:name| may be used to
480 provide this name when calling @|make-instance|.
481
482 Two simple type objects are equal if and only if they have @|string=| names
483 and matching qualifiers.
484
485 A number of symbolic type specifiers for builtin types are predefined as
486 shown in \xref{tab:proto.c-types.simple}. These are all defined as if by
487 @|define-simple-c-type|, so can be used to construct qualified types.
488 \end{describe}
489
490 \begin{table}
491 \begin{tabular}[C]{|l|l|} \hlx{hv}
492 \textbf{C type} & \textbf{Specifiers} \\ \hlx{vhv}
493 @|void| & @|void| \\ \hlx{vhv}
494 @|char| & @|char| \\ \hlx{v}
495 @|unsigned char| & @|unsigned-char|, @|uchar| \\ \hlx{v}
496 @|signed char| & @|signed-char|, @|schar| \\ \hlx{vhv}
497 @|short| & @|short|, @|signed-short|, @|short-int|,
498 @|signed-short-int| @|sshort| \\ \hlx{v}
499 @|unsigned short| & @|unsigned-short|, @|unsigned-short-int|,
500 @|ushort| \\ \hlx{vhv}
501 @|int| & @|int|, @|signed|, @|signed-int|,
502 @|sint| \\ \hlx{v}
503 @|unsigned int| & @|unsigned|, @|unsigned-int|, @|uint| \\ \hlx{vhv}
504 @|long| & @|long|, @|signed-long|, @|long-int|,
505 @|signed-long-int|, @|slong| \\ \hlx{v}
506 @|unsigned long| & @|unsigned-long|, @|unsigned-long-int|,
507 @|ulong| \\ \hlx{vhv}
508 @|long long| & @|long-long|, @|signed-long-long|,
509 @|long-long-int|, \\
510 & \qquad @|signed-long-long-int|,
511 @|llong|, @|sllong| \\ \hlx{v}
512 @|unsigned long long|
513 & @|unsigned-long-long|, @|unsigned-long-long-int|,
514 @|ullong| \\ \hlx{vhv}
515 @|float| & @|float| \\ \hlx{v}
516 @|double| & @|double| \\ \hlx{vhv}
517 @|va_list| & @|va-list| \\ \hlx{v}
518 @|size_t| & @|size-t| \\ \hlx{v}
519 @|ptrdiff_t| & @|ptrdiff-t| \\ \hlx{vh}
520 \end{tabular}
521 \caption{Builtin symbolic type specifiers for simple C types}
522 \label{tab:proto.c-types.simple}
523 \end{table}
524
525 \begin{describe}{fun}{make-simple-type @<name> \&optional @<qualifiers>}
526 Return the (unique interned) simple C type object for the C type whose name
527 is @<name> (a string) and which has the given @<qualifiers> (a list of
528 keywords).
529 \end{describe}
530
531 \begin{describe}{gf}{c-type-name @<type>}
532 Returns the name of a @|simple-c-type| instance @<type> as an immutable
533 string.
534 \end{describe}
535
536 \begin{describe}{mac}{%
537 define-simple-c-type @{ @<name> @! (@<name>^*) @} @<string>}
538 Define type specifiers for a new simple C type. Each symbol @<name> is
539 defined as a symbolic type specifier for the (unique interned) simple C
540 type whose name is the value of @<string>. Further, each @<name> is
541 defined to be a type operator: the type specifier @|(@<name>
542 @<qualifier>^*)| evaluates to the (unique interned) simple C type whose
543 name is @<string> and which has the @<qualifiers> (which are evaluated).
544 \end{describe}
545
546 \begin{describe}{cls}{tagged-c-type (qualifiable-c-type)
547 \&key :qualifiers :tag}
548 Provides common behaviour for C tagged types. A @<tag> is a string
549 containing a C identifier.
550
551 Two tagged types are equal if and only if they have the same class, their
552 @<tag>s are @|string=|, and they have matching qualifiers. (User-defined
553 subclasses may have additional methods on @|c-type-equal-p| which impose
554 further restrictions.)
555 \end{describe}
556 \begin{boxy}[Bug]
557 Sod maintains distinct namespaces for the three kinds of tagged types. In
558 C, there is only one namespace for tags which is shared between enums,
559 structs and unions.
560 \end{boxy}
561
562 \begin{describe}{gf}{c-tagged-type-kind @<type>}
563 Returns a symbol classifying the tagged @<type>: one of @|enum|, @|struct|
564 or @|union|. User-defined subclasses of @|tagged-c-type| should return
565 their own classification symbols. It is intended that @|(string-downcase
566 (c-tagged-type-kind @<type>))| be valid C syntax.\footnote{%
567 Alas, C doesn't provide a syntactic category for these keywords;
568 \Cplusplus\ calls them a @<class-key>.} %
569 \end{describe}
570
571 \begin{describe}{cls}{c-enum-type (tagged-c-type) \&key :qualifiers :tag}
572 Represents a C enumerated type. An instance denotes the C type @|enum|
573 @<tag>. See the direct superclass @|tagged-c-type| for details.
574
575 The type specifier @|(enum @<tag> @<qualifier>^*)| returns the (unique
576 interned) enumerated type with the given @<tag> and @<qualifier>s (all
577 evaluated).
578 \end{describe}
579 \begin{describe}{fun}{make-enum-type @<tag> \&optional @<qualifiers>}
580 Return the (unique interned) C type object for the enumerated C type whose
581 tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
582 keywords).
583 \end{describe}
584
585 \begin{describe}{cls}{c-struct-type (tagged-c-type) \&key :qualifiers :tag}
586 Represents a C structured type. An instance denotes the C type @|struct|
587 @<tag>. See the direct superclass @|tagged-c-type| for details.
588
589 The type specifier @|(struct @<tag> @<qualifier>^*)| returns the (unique
590 interned) structured type with the given @<tag> and @<qualifier>s (all
591 evaluated).
592 \end{describe}
593 \begin{describe}{fun}{make-struct-type @<tag> \&optional @<qualifiers>}
594 Return the (unique interned) C type object for the structured C type whose
595 tag is @<tag> (a string) and which has the given @<qualifiers> (a list of
596 keywords).
597 \end{describe}
598
599 \begin{describe}{cls}{c-union-type (tagged-c-type) \&key :qualifiers :tag}
600 Represents a C union type. An instance denotes the C type @|union|
601 @<tag>. See the direct superclass @|tagged-c-type|
602 for details.
603
604 The type specifier @|(union @<tag> @<qualifier>^*)| returns the (unique
605 interned) union type with the given @<tag> and @<qualifier>s (all
606 evaluated).
607 \end{describe}
608 \begin{describe}{fun}{make-union-type @<tag> \&optional @<qualifiers>}
609 Return the (unique interned) C type object for the union C type whose tag
610 is @<tag> (a string) and which has the given @<qualifiers> (a list of
611 keywords).
612 \end{describe}
613
614 \subsection{Pointers and arrays} \label{sec:proto.c-types.ptr-array}
615
616 Pointers and arrays are \emph{compound types}: they're defined in terms of
617 existing types. A pointer describes the type of objects it points to; an
618 array describes the type of array element.
619 \begin{describe}{gf}{c-type-subtype @<type>}
620 Returns the underlying type of a compound type @<type>. Precisely what
621 this means depends on the class of @<type>.
622 \end{describe}
623
624 \begin{describe}{cls}{c-pointer-type (qualifiable-c-type)
625 \&key :qualifiers :subtype}
626 Represents a C pointer type. An instance denotes the C type @<subtype>
627 @|*|@<qualifiers>.
628
629 The @<subtype> may be any C type. Two pointer types are equal if and only
630 if their subtypes are equal and they have matching qualifiers.
631
632 The type specifier @|(* @<type-spec> @<qualifier>^*)| returns a type
633 qualified pointer-to-@<subtype>, where @<subtype> is the type specified by
634 @<type-spec> and the @<qualifier>s are qualifier keywords (which are
635 evaluated). The synonyms @|ptr| and @|pointer| may be used in place of the
636 star @`*'.
637
638 The symbol @|string| is a type specifier for the type of pointer to
639 characters; the symbol @|const-string| is a type specifier for the type
640 pointer to constant characters.
641 \end{describe}
642 \begin{describe}{fun}{make-pointer-type @<subtype> \&optional @<qualifiers>}
643 Return an object describing the type of qualified pointers to @<subtype>.
644 If @<subtype> is interned, then the returned pointer type object is
645 interned also.
646 \end{describe}
647
648 \begin{describe}{cls}{c-array-type (c-type) \&key :subtype :dimensions}
649 Represents a multidimensional C array type. The @<dimensions> are a list
650 of dimension specifiers $d_0$, $d_1$, \ldots, $d_{n-1}$; an instance then
651 denotes the C type @<subtype> @|[$d_0$][$d_1$]$\ldots$[$d_{n-1}$]|. An
652 individual dimension specifier is either a string containing a C integral
653 constant expression, or nil which is equivalent to an empty string. Only
654 the first (outermost) dimension $d_0$ should be empty.
655
656 C doesn't actually have multidimensional arrays as a primitive notion;
657 rather, it permits an array (with known extent) to be the element type of
658 an array, which achieves an equivalent effect. C arrays are stored in
659 row-major order: i.e., if we write down the indices of the elements of an
660 array in order of ascending address, the rightmost index varies fastest;
661 hence, the type constructed is more accurately an array of $d_0$ arrays of
662 $d_1$ of \ldots\ arrays of $d_{n-1}$ elements of type @<subtype>. We shall
663 continue to abuse terminology and refer to multidimensional arrays.
664
665 The type specifier @|([] @<type-spec> @<dimension>^*)| constructs a
666 multidimensional array with the given @<dimension>s whose elements have the
667 type specified by @<type-spec>. If no dimensions are given then a
668 single-dimensional array with unspecified extent. The synonyms @|array|
669 and @|vector| may be used in place of the brackets @`[]'.
670 \end{describe}
671 \begin{describe}{fun}{make-array-type @<subtype> @<dimensions>}
672 Return an object describing the type of arrays with given @<dimensions> and
673 with element type @<subtype> (an instance of @|c-type|). The @<dimensions>
674 argument is a list whose elements are strings or nil; see the description
675 of the class @|c-array-type| above for details.
676 \end{describe}
677 \begin{describe}{gf}{c-array-dimensions @<type>}
678 Returns the dimensions of @<type>, an array type, as an immutable list.
679 \end{describe}
680
681 \subsection{Function types} \label{sec:proto.c-types.fun}
682
683 \begin{describe}{cls}{c-function-type (c-type) \&key :subtype :arguments}
684 Represents C function types. An instance denotes the C type of a C
685 function which
686 \end{describe}
687
688 %%%----- That's all, folks --------------------------------------------------
689
690 %%% Local variables:
691 %%% mode: LaTeX
692 %%% TeX-master: "sod.tex"
693 %%% TeX-PDF-mode: t
694 %%% End: