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