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