3 .Dt LIBCDECL \&3 "Cdecl99 Developer's Manual"
6 .Nd C library for making sense of C declarations
10 .Fd struct cdecl *cdecl_parse_decl(const char *declstr);
11 .Fd struct cdecl *cdecl_parse_english(const char *english);
12 .Fd void cdecl_free(struct cdecl *decl);
14 .Fd size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
15 .Fd size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl);
17 .Fd const struct cdecl_error *cdecl_get_error(void);
19 .Fd int cdecl_spec_kind(struct cdecl_declspec *spec);
22 provides support for parsing C declarations and translating them to something
23 resembling English and vice-versa. This manual describes the programmers'
24 interface only; for details such as what C language features are supported or
25 the syntax of English declarations, please see the
30 is intended to be portable to any system with a working C implementation that
31 at least makes an effort to support C99. The library is thread-safe when
32 appropriate facilities exist and are enabled at build time.
35 reserves all identifiers beginning with either
39 in both the tag and ordinary identifier namespaces. All external names
42 are reserved, and the library headers may define object-like macros beginning
47 library headers may use other identifiers where they do not pollute the global
48 namespaces, such as struct members or function parameter names. Such internal
49 identifiers shall not contain any upper-case letters. As these internal
50 identifiers can only conflict with object-like macros, this practice is safe as
51 long as the convention of defining object-like macros using upper-case letters
54 External names beginning with
56 followed by two consecutive underscores are not considered part of the ABI and
57 are thus subject to change at any time.
58 .Sh ABSTRACT SYNTAX TREE
61 generally operate on an abstract syntax tree representing a C declaration.
62 A string is parsed into an AST which can be subsequently rendered into another
63 format. Since some information about the original string is discarded when
64 generating the AST, parsing a declaration and then rendering to the same format
65 is not the identity function. The AST is represented by the following
67 .Bd -literal -offset indent
70 struct cdecl_declspec *specifiers;
71 struct cdecl_declarator *declarators;
75 At the top level, every declaration consists of one or more declaration
76 specifiers followed by one or more full declarators; hence, the
80 members are always non-null. A declaration with more than one declarator is
81 represented by using the
83 member to form a singly-linked list of ASTs, one element for each declarator.
84 In the case of the toplevel declaration, the declaration specifiers will be
85 identical for all elements of the list. But when the same kind of list is used
86 to represent function parameters, the specifiers may be different for each
89 There are four kinds of declaration specifiers: storage-class, function and
90 type specifiers, as well as type qualifiers. All are represented by the
92 .Bd -literal -offset indent
93 struct cdecl_declspec {
94 struct cdecl_declspec *next;
100 When multiple declaration specifiers are present, they are represented as
101 a singly-linked list, one element for each specifier. Specifiers can appear
102 in any order. The function
104 .Fd int cdecl_spec_kind(struct cdecl_declspec *spec);
106 can be used to determine what kind of specifier
108 is. The result is one of the following values:
109 .Bl -column ".Dv CDECL_SPEC_TYPE"
110 .It Em Kind Ta Em Description
111 .It Dv CDECL_SPEC_TYPE Ta Type specifier.
112 .It Dv CDECL_SPEC_STOR Ta Storage-class specifier.
113 .It Dv CDECL_SPEC_QUAL Ta Type qualifier.
114 .It Dv CDECL_SPEC_FUNC Ta Function specifier.
117 The following table describes all the possible types of declaration specifiers.
118 .Bl -column ".Dv CDECL_TYPE_IMAGINARY"
119 .It Em Em Type Ta Em Description
120 .It Dv CDECL_TYPE_VOID Ta Fa void No type specifier.
121 .It Dv CDECL_TYPE_CHAR Ta Fa char No type specifier.
122 .It Dv CDECL_TYPE_SHORT Ta Fa short No type specifier.
123 .It Dv CDECL_TYPE_INT Ta Fa int No type specifier.
124 .It Dv CDECL_TYPE_LONG Ta Fa long No type specifier.
125 .It Dv CDECL_TYPE_FLOAT Ta Fa float No type specifier.
126 .It Dv CDECL_TYPE_DOUBLE Ta Fa double No type specifier.
127 .It Dv CDECL_TYPE_SIGNED Ta Fa signed No type specifier.
128 .It Dv CDECL_TYPE_UNSIGNED Ta Fa unsigned No type specifier.
129 .It Dv CDECL_TYPE_BOOL Ta Fa _Bool No type specifier.
130 .It Dv CDECL_TYPE_COMPLEX Ta Fa _Comples No type specifier.
131 .It Dv CDECL_TYPE_IMAGINARY Ta Fa _Imaginary No type specifier.
132 .It Dv CDECL_TYPE_STRUCT Ta Fa struct No type specifier. The
134 member points to a C string containing the struct tag.
135 .It Dv CDECL_TYPE_UNION Ta Fa union No type specifier. The
137 member points to a C string containing the union tag.
138 .It Dv CDECL_TYPE_ENUM Ta Fa enum No type specifier. The
140 member points to a C string containing the enum tag.
141 .It Dv CDECL_TYPE_IDENT Ta Typedef name type specifier. The
143 member points to a C string containing the identifier.
144 .It Dv CDECL_STOR_TYPEDEF Ta Fa typedef No storage-class specifier.
145 .It Dv CDECL_STOR_EXTERN Ta Fa extern No storage-class specifier.
146 .It Dv CDECL_STOR_STATIC Ta Fa static No storage-class specifier.
147 .It Dv CDECL_STOR_AUTO Ta Fa auto No storage-class specifier.
148 .It Dv CDECL_STOR_REGISTER Ta Fa register No storage-class specifier.
149 .It Dv CDECL_QUAL_RESTRICT Ta Fa restrict No type qualifier.
150 .It Dv CDECL_QUAL_VOLATILE Ta Fa volatile No type qualifier.
151 .It Dv CDECL_QUAL_CONST Ta Fa const No type qualifier.
152 .It Dv CDECL_FUNC_INLINE Ta Fa inline No function specifier.
155 Declarators are represented by the structure:
156 .Bd -literal -offset indent
157 struct cdecl_declarator {
158 struct cdecl_declarator *child;
162 struct cdecl_pointer pointer;
163 struct cdecl_array array;
164 struct cdecl_function function;
169 There are five types of declarators, distinguished by the
171 struct member. The union
173 contains a member for each declarator type (except null) containing additional
174 information. The possible values are described by the following table. The
175 description of the child member is a lie.
176 .Bl -column ".Dv CDECL_DECL_FUNCTION" ".Em Union Member"
177 .It Em Declarator Type Ta Em Union Member Ta Em Description
178 .It Dv CDECL_DECL_NULL Ta (none) Ta Declares nothing. This
181 .It Dv CDECL_DECL_IDENT Ta Va ident Ta Declares an identifier. This
184 .It Dv CDECL_DECL_POINTER Ta Va pointer Ta Declares a pointer. The
186 member is interpreted as "pointer to child".
187 .It Dv CDECL_DECL_ARRAY Ta Va array Ta Declares an array. The
189 member is interpreted as "array of child".
190 .It Dv CDECL_DECL_FUNCTION Ta Va function Ta Declares a function. The
192 member is interpreted as "function returning child".
194 .Ss Terminal Declarators
195 Null and identifier declarators have no children and are thus leaf nodes. A
196 null declarator indicates an abstract declarator; that is, one which does not
197 declare any identifier. Such declarators appear in type names and possibly
198 function parameters. An identifier declarator has the obvious meaning; the
200 union member points to the C string containing the identifier.
201 .Ss Pointer Declarators
202 .Bd -literal -offset indent
203 struct cdecl_pointer {
204 struct cdecl_declspec *qualifiers;
210 member is non-null, then it points to the first element of a singly-linked list
212 .Ss Array Declarators
213 .Bd -literal -offset indent
222 member is non-null, then this declarator is a variable-length array declarator.
225 member points to an identifier if it is known, else it points to the empty
229 is positive, then this is an array declarator with the specified length.
230 Otherwise, this is an incomplete array declarator.
231 .Ss Function Declarators
232 .Bd -literal -offset indent
233 struct cdecl_function {
234 struct cdecl *parameters;
241 is null, then this is a non-prototype function declarator. Otherwise,
243 points to the first element of a singly-linked list of declarations
244 representing the function parameters. Note that, unlike toplevel declarations,
245 each function parameter has exactly one full declarator (abstract or
248 is true, then the function is variadic.
252 can fail. Such functions will be documented as indicating an error condition
253 in a particular way. It is sometimes necessary to know more about a particular
254 error in order to print an informative error message or perform some other
255 action. To facilitate this,
257 provides a structure which describes a particular error.
258 .Bd -literal -offset indent
267 member identifies the sort of error which has occurred, while the
269 member points to a string containing a human-readable description of the error.
270 This error information can be retrieved by calling the function
272 .Fd const struct cdecl_error *cdecl_get_error(void);
274 which returns a pointer to the error structure most recently generated in the
275 current thread. It is therefore thread-safe in that errors occurring in
276 another thread will not interfere with the current one. The returned pointer
277 shall remain valid until the next call to any function from
279 by the same thread, except that multiple consecutive calls to
281 shall all return the same value. The same applies to the
283 pointer inside the error structure itself.
285 If this function is called before an error has been indicated by an earlier
286 call in the same thread, the behaviour is undefined.
287 .Sh PARSING DECLARATIONS
288 To parse a declaration, the function
290 .Fd struct cdecl *cdecl_parse_decl(const char *declstr);
292 can be used. The provided string is parsed as a C declaration. If successful,
293 this function returns a pointer to an abstract syntax tree representing the
294 declaration. If the parse fails for any reason, the function returns NULL.
296 Similarly, English declarations can be parsed by using the function
298 .Fd struct cdecl *cdecl_parse_english(const char *english);
300 When the AST is no longer needed, it must be freed by passing it to the
303 .Fd void cdecl_free(struct cdecl *decl);
304 .Sh RENDERING DECLARATIONS
305 On the other hand, the abstract syntax tree can be rendered to a string for
306 output. One can use the function
308 .Fd size_t cdecl_explain(char *buf, size_t n, struct cdecl *decl);
310 to format the AST pointed to by
312 into something resembling English. At most one full declarator is rendered
313 in this way; for declarations with more than one full declarator, this function
314 should be called on each
316 in the singly-linked list.
318 In a manner similar to that of
322 bytes, including the '\\0' terminator, are written to
326 is zero, it is acceptable for
328 to be a null pointer. Regardless, the function returns the number of
329 characters that would be written to
333 were long enough, not including the '\\0' terminator. Thus, the entire string
334 was written if a value less than
338 Similarly, the function
340 .Fd size_t cdecl_declare(char *buf, size_t n, struct cdecl *decl);
342 will render the AST pointed to by
346 Nick Bowler <nbowler@draconx.ca>
348 Copyright \(co 2011 Nick Bowler
350 Permission is granted to copy, distribute and/or modify this manual under the
351 terms of the Do What The Fuck You Want To Public License, version 2.