3 * Parser for C declarations.
4 * Copyright © 2011-2012, 2021, 2023 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 %name-prefix "cdecl__yy"
22 %parse-param {void *scanner}
23 %parse-param {struct cdecl **out}
24 %lex-param {yyscan_t scanner}
36 #include "cdecl-internal.h"
39 #define FAIL(msg) do { \
40 yyerror(&yylloc, NULL, NULL, msg); \
45 * Allocate a parse tree node via cdecl__alloc_item.
47 * - m1 specifies the item's union member to assign to ptr, which selects the
48 * type of node to allocate.
50 * - m2 specifies the "next" or "child" member, which is initialized to a null
51 * pointer. The cdecl__alloc_item function sets the declarator.child member
52 * to null; we explicitly copy this null pointer to the returned union member
53 * to avoid type punning. It is hoped that compilers will notice that these
54 * pointers are at the same offset therefore the assignment can be elided.
56 * The resulting pointer can be passed directly to free, as the union is the
57 * first member of the parse_item structure.
59 * Use the wrapper macros below instead of this one.
61 #define ALLOC_ITEM(ptr, m1, m2) do { \
62 struct parse_item *item; \
63 if (!(item = cdecl__alloc_item(0))) \
65 item->u.m1.m2 = (void *)item->u.declarator.child; \
66 (ptr) = &item->u.m1; \
69 /* Wrappers for ALLOC_ITEM to allocate various kinds of parser structures. */
70 #define ALLOC_ITEM_DECLARATOR(ptr) ALLOC_ITEM(ptr, declarator, child)
71 #define ALLOC_ITEM_DECLSPEC(ptr) ALLOC_ITEM(ptr, declspec, next)
72 #define ALLOC_ITEM_DECL(ptr) ALLOC_ITEM(ptr, decl, next)
74 #define ALLOC_FUNCTION(ptr, parameters_, variadic_) do { \
75 ALLOC_ITEM_DECLARATOR(ptr); \
76 (ptr)->type = CDECL_DECL_FUNCTION; \
77 (ptr)->u.function.parameters = parameters_; \
78 (ptr)->u.function.variadic = variadic_; \
81 #define ALLOC_ARRAY(ptr, length_) do { \
82 ALLOC_ITEM_DECLARATOR(ptr); \
83 (ptr)->type = CDECL_DECL_ARRAY; \
84 (ptr)->u.array.vla = NULL; \
85 (ptr)->u.array.length = length_; \
88 #define ALLOC_POINTER(ptr, qualifiers_, child_) do { \
89 ALLOC_ITEM_DECLARATOR(ptr); \
90 (ptr)->child = child_; \
91 (ptr)->type = CDECL_DECL_POINTER; \
92 (ptr)->u.pointer.qualifiers = qualifiers_; \
95 #define ALLOC_DECLSPEC(ptr, type_) do { \
96 ALLOC_ITEM_DECLSPEC(ptr); \
97 (ptr)->type = type_; \
98 (ptr)->ident = NULL; \
101 #define ALLOC_DECL(ptr, specifiers_, declarators_) do { \
102 ALLOC_ITEM_DECL(ptr); \
103 (ptr)->specifiers = specifiers_; \
104 (ptr)->declarators = declarators_; \
108 * With the postprocessing performed by fix-yytname.awk, all the symbol
109 * name strings can be used directly in error messages and there is no
110 * need for any string processing.
112 #define yytnamerr(a, b) ( (a) ? yytnamerr_copy(a, b) \
115 static size_t yytnamerr_copy(char *dst, const char *src)
117 return cdecl__strlcpy(dst, src, strlen(src)+1);
122 #include <inttypes.h>
126 void cdecl__free(struct cdecl *);
127 int cdecl__yyparse(void *scanner, struct cdecl **out);
128 const char *cdecl__token_name(unsigned token);
135 struct cdecl_declspec *declspec;
136 struct cdecl_declarator *declarator;
138 struct parse_item *item;
142 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
143 static void free_decl(struct cdecl *);
145 static void free_declspec(struct cdecl_declspec *x)
147 struct cdecl_declspec *p;
155 static void free_declarator(struct cdecl_declarator *x)
157 struct cdecl_declarator *p;
163 case CDECL_DECL_NULL:
165 case CDECL_DECL_IDENT:
166 case CDECL_DECL_ARRAY:
168 case CDECL_DECL_POINTER:
169 free_declspec(x->u.pointer.qualifiers);
171 case CDECL_DECL_FUNCTION:
172 free_decl(x->u.function.parameters);
183 static void free_decl(struct cdecl *x)
190 /* The specifiers may be shared by an entire chain. */
191 if (!p || p->specifiers != x->specifiers)
192 free_declspec(x->specifiers);
194 free_declarator(x->declarators);
200 void cdecl__free(struct cdecl *decl)
206 * Join two declaration specifier lists into a single list, with "a" being the
207 * head of the new list.
209 * The list "a" is assumed to be nonempty.
211 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
219 * Alter an abstract declarator (type name) to declare an identifier instead,
220 * used by the English parser rules to reduce "identifier as type" sequences.
222 static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
224 struct cdecl_declarator *d, **p = &decl->declarators;
226 while ((d = *p)->child)
229 *p = &ident->u.declarator;
233 static struct cdecl_declarator *nulldecl(void)
235 static const struct cdecl_declarator nulldecl = {0};
236 return (void *)&nulldecl;
238 #define NULLDECL (nulldecl())
242 %destructor { free($$); } <item>
243 %destructor { free_declspec($$); } <declspec>
244 %destructor { free_declarator($$); } <declarator>
245 %destructor { free_decl($$); } <decl>
250 %token <item> T_IDENT "identifier"
251 %token <uintval> T_UINT "integer constant"
253 %token T_SEMICOLON ";"
254 %token T_ASTERISK "*"
257 %token T_LBRACKET "["
258 %token T_RBRACKET "]"
260 %token T_ELLIPSIS "..."
262 %token <spectype> T_TYPEDEF "typedef"
263 %token <spectype> T_EXTERN "extern"
264 %token <spectype> T_STATIC "static"
265 %token <spectype> T_AUTO "auto"
266 %token <spectype> T_REGISTER "register"
268 %token <spectype> T_INLINE "inline"
270 %token <spectype> T_RESTRICT "restrict"
271 %token <spectype> T_VOLATILE "volatile"
272 %token <spectype> T_CONST "const"
274 %token <spectype> T_VOID "void"
275 %token <spectype> T_CHAR "char"
276 %token <spectype> T_SHORT "short"
277 %token <spectype> T_INT "int"
278 %token <spectype> T_LONG "long"
279 %token <spectype> T_FLOAT "float"
280 %token <spectype> T_DOUBLE "double"
281 %token <spectype> T_SIGNED "signed"
282 %token <spectype> T_UNSIGNED "unsigned"
283 %token <spectype> T_BOOL "_Bool"
284 %token <spectype> T_COMPLEX "_Complex"
285 %token <spectype> T_IMAGINARY "_Imaginary"
287 %token <spectype> T_STRUCT "struct"
288 %token <spectype> T_UNION "union"
289 %token <spectype> T_ENUM "enum"
295 %token T_DECLARE "declare"
296 %token T_POINTER "pointer"
297 %token T_FUNCTION "function"
298 %token T_RETURNING "returning"
299 %token T_ARRAY "array"
303 %token T_VLA "variable-length"
305 %type <item> vla_ident
306 %type <uintval> array_length
307 %type <boolval> varargs
308 %type <spectype> declspec_simple qualifier_simple
309 %type <spectype> typespec_simple typespec_tagged
310 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
311 %type <declspec> qualifier qualifiers
312 %type <declspec> declspecs declspecs_noid
313 %type <declarator> direct_declarator declarator pointer array parens postfix
314 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
315 %type <decl> cdecl declaration declarators declarator_wrap parameter
317 %type <item> english_vla
318 %type <declspec> storage_func_specs post_specs
319 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
320 %type <declarator> english_declarator english_array english_function
321 %type <declarator> english_parameter_list null_decl
322 %type <decl> english english_declaration english_parameter
324 /* Precedence declaration to avoid conflict in english_parameter; see below. */
330 input: cdecl { *out = $1; }
331 cdecl: english | declaration
334 declaration: declspecs declarators semi {
340 * We support parsing declarations using arbitrary identifiers as type
341 * specifiers (a la C typedef). To avoid confusion with identifiers that
342 * may also be used as declarators, note the following:
344 * (a) Every valid C declaration must have at least one type specifier, and
345 * (b) Valid declarations with typedef names have exactly one type specifier.
347 * So the rule applied when parsing specifiers is: an identifier is a type
348 * specifier only if we have not yet seen any type specifiers whatsoever
349 * (within one declaration specifier list).
351 * Treating identifiers as type specifiers by default can lead to strange and
352 * unexpected parses; libcdecl applies a simplification step to the resulting
353 * parse tree afterwards.
355 declspecs: declspec_notype declspecs {
358 } | typespec declspecs_noid {
363 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
368 qualifiers: { $$ = NULL; } | qualifiers qualifier {
373 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
378 declarator_wrap: declarator {
379 ALLOC_DECL($$, NULL, $1);
382 declspec_simple: T_AUTO
389 typespec_simple: T_VOID
402 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
404 qualifier_simple: T_CONST
408 declspec_notype: qualifier | declspec_simple { ALLOC_DECLSPEC($$, $1); }
409 typespec_noid: typespec_simple { ALLOC_DECLSPEC($$, $1); }
410 qualifier: qualifier_simple { ALLOC_DECLSPEC($$, $1); }
412 typespec: typespec_noid | typespec_tagged T_IDENT {
413 /* Compiler should be able to elide this assignment. */
414 $2->u.declspec.ident = $2->u.declarator.u.ident;
416 $$ = &$2->u.declspec;
420 declspec_noid: declspec_notype | typespec_noid
422 vla_ident: T_IDENT | T_ASTERISK {
423 if (!($$ = cdecl__alloc_item(1)))
428 array: T_LBRACKET array_length T_RBRACKET {
430 } | T_LBRACKET vla_ident T_RBRACKET {
431 $$ = &$2->u.declarator;
432 $$->type = CDECL_DECL_ARRAY;
433 $$->u.array.vla = $$->u.ident;
434 $$->u.array.length = 0;
437 parameter: declspecs declarator {
438 ALLOC_DECL($$, $1, $2);
441 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
443 parameter_type_list: parameter varargs {
444 ALLOC_FUNCTION($$, $1, $2);
445 } | parameter T_COMMA parameter_type_list {
447 $1->next = $$->u.function.parameters;
448 $$->u.function.parameters = $1;
451 parens: T_LPAREN parameter_type_list T_RPAREN {
453 } | T_LPAREN declarator_ish T_RPAREN {
454 struct cdecl *fake_params;
456 ALLOC_DECL(fake_params, NULL, $2);
457 ALLOC_FUNCTION($$, fake_params, false);
460 pointer: T_ASTERISK qualifiers direct_declarator {
461 ALLOC_POINTER($$, $2, $3);
462 } | T_ASTERISK qualifiers pointer {
463 ALLOC_POINTER($$, $2, $3);
466 declarator: direct_declarator | pointer
467 declarator_ish: direct_declarator_ish | pointer
468 postfix: array | parens
470 direct_declarator_ish: {
472 } | direct_declarator_ish postfix {
480 $$ = &$1->u.declarator;
481 } | direct_declarator postfix {
486 english: T_DECLARE T_IDENT T_AS english_declaration {
487 $$ = insert_identifier($4, $2);
488 } | T_TYPE english_declaration {
493 * We use a precedence declaration to prefer shifting an identifier
494 * over reducing this empty rule; see below.
496 storage_func_specs: %prec T_TYPE { $$ = NULL; }
497 storage_func_specs: declspec_simple storage_func_specs {
498 ALLOC_DECLSPEC($$, $1);
502 type_qual_spec: typespec_noid | qualifier
504 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
510 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
511 * conflicts with pointer declarators. So we end up needing to stitch
512 * together three different specifiers lists.
514 post_specs: qualifiers typespec type_qual_specs {
520 english_declaration: storage_func_specs english_declarator post_specs {
522 ALLOC_DECL($$, $3, $2);
525 english_declarator: {
527 } | english_declarator qualifiers T_POINTER T_TO {
528 ALLOC_POINTER($$, $2, $1);
529 } | english_declarator english_array {
532 } | english_declarator english_function {
537 english_function: T_FUNCTION T_RETURNING {
538 ALLOC_FUNCTION($$, NULL, false);
539 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
543 english_parameter_list: english_parameter varargs {
544 ALLOC_FUNCTION($$, $1, $2);
545 } | english_parameter T_COMMA english_parameter_list {
547 $1->next = $$->u.function.parameters;
548 $$->u.function.parameters = $1;
551 typedef_name_qual: T_IDENT qualifiers {
552 /* Compiler should be able to elide this assignment. */
553 $1->u.declspec.ident = $1->u.declarator.u.ident;
555 $$ = &$1->u.declspec;
556 $$->type = CDECL_TYPE_IDENT;
565 * There is a shift/reduce conflict here when an identifier appears as the
566 * first token. The conflict is between shifting T_IDENT, or reducing the
567 * empty production for storage_func_specs (cf. english_declaration).
569 * - In either case, if we reduce, we won't match T_IDENT T_AS since the
570 * stack now has the extra storage_func_specs nonterminal symbol.
571 * - And if we shift, we won't match english_declaration since it is
572 * too late to add storage_func_specs to the stack.
574 * The only valid input affected by the conflict is a simple type names,
575 * possibly followed by qualifiers. So the conflict is adequately resolved
576 * by shifting, so long as we have a special-case reduction to handle this.
578 english_parameter: english_declaration | typedef_name_qual null_decl {
579 ALLOC_DECL($$, $1, $2);
580 } | T_IDENT T_AS english_declaration {
581 $$ = insert_identifier($3, $1);
584 english_array: T_VLA T_ARRAY english_vla T_OF {
585 $$ = &$3->u.declarator;
586 $$->type = CDECL_DECL_ARRAY;
587 $$->u.array.vla = $$->u.ident;
588 $$->u.array.length = 0;
589 } | T_ARRAY array_length T_OF {
593 array_length: { $$ = 0; }
594 array_length: T_UINT {
596 FAIL(_("array length must be positive"));
599 english_vla: T_IDENT | {
600 if (!($$ = cdecl__alloc_item(1)))
608 * Expose the token string table to the rest of the library, in order to
609 * produce strings that match parser keywords.
611 * In order for this to work properly, the Bison output must be postprocessed
612 * by fix-yytname.awk to remove pointless quotation marks from the keyword
615 const char *cdecl__token_name(unsigned token)
617 return yytname[YYTRANSLATE(token)];
621 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
623 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
626 cdecl__err(CDECL_ENOPARSE, "%s", err);