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