3 * Parser for C declarations.
4 * Copyright © 2011-2012, 2021, 2023-2024 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}
35 #include "cdecl-internal.h"
39 * Allocate a parse tree node via cdecl__alloc_item.
41 * - m1 specifies the item's union member to assign to ptr, which selects the
42 * type of node to allocate.
44 * - m2 specifies the "next" or "child" member, which is initialized to a null
45 * pointer. The cdecl__alloc_item function sets the declarator.child member
46 * to null; we explicitly copy this null pointer to the returned union member
47 * to avoid type punning. It is hoped that compilers will notice that these
48 * pointers are at the same offset therefore the assignment can be elided.
50 * The resulting pointer can be passed directly to free, as the union is the
51 * first member of the parse_item structure.
53 * Use the wrapper macros below instead of this one.
55 #define ALLOC_ITEM(ptr, m1, m2) do { \
56 struct parse_item *item; \
57 if (!(item = cdecl__alloc_item(0))) \
59 item->u.m1.m2 = (void *)item->u.declarator.child; \
60 (ptr) = &item->u.m1; \
63 /* Wrappers for ALLOC_ITEM to allocate various kinds of parser structures. */
64 #define ALLOC_ITEM_DECLARATOR(ptr) ALLOC_ITEM(ptr, declarator, child)
65 #define ALLOC_ITEM_DECLSPEC(ptr) ALLOC_ITEM(ptr, declspec, next)
66 #define ALLOC_ITEM_DECL(ptr) ALLOC_ITEM(ptr, decl, next)
68 #define ALLOC_FUNCTION_(ptr, parameters_) do { \
69 ALLOC_ITEM_DECLARATOR(ptr); \
70 (ptr)->type = CDECL_DECL_FUNCTION; \
71 (ptr)->u.function.parameters = parameters_; \
74 #define ALLOC_FUNCTION(ptr, parameters_, variadic_) do { \
75 ALLOC_FUNCTION_(ptr, parameters_); \
76 (ptr)->u.function.variadic = variadic_; \
79 #define ALLOC_ARRAY(ptr, length_) do { \
80 ALLOC_ITEM_DECLARATOR(ptr); \
81 (ptr)->type = CDECL_DECL_ARRAY; \
82 (ptr)->u.array.vla = NULL; \
83 (ptr)->u.array.length = length_; \
86 #define ALLOC_POINTER(ptr, qualifiers_, child_) do { \
87 ALLOC_ITEM_DECLARATOR(ptr); \
88 (ptr)->child = child_; \
89 (ptr)->type = CDECL_DECL_POINTER; \
90 (ptr)->u.pointer.qualifiers = qualifiers_; \
93 #define ALLOC_DECLSPEC(ptr, type_) do { \
94 ALLOC_ITEM_DECLSPEC(ptr); \
95 (ptr)->type = type_; \
96 (ptr)->ident = NULL; \
99 #define ALLOC_DECL(ptr, specifiers_, declarators_) do { \
100 ALLOC_ITEM_DECL(ptr); \
101 (ptr)->specifiers = specifiers_; \
102 (ptr)->declarators = declarators_; \
106 * With the postprocessing performed by fix-yytname.awk, all the symbol
107 * name strings can be used directly in error messages and there is no
108 * need for any string processing.
110 #define yytnamerr(a, b) ( (a) ? cdecl__strlcpy(a, b, -1) : strlen(b) )
114 #include <inttypes.h>
120 void cdecl__free(struct cdecl *);
121 int cdecl__yyparse(void *scanner, struct cdecl **out);
122 const char *cdecl__token_name(unsigned token);
126 cdecl_uintmax uintval;
129 struct cdecl_declspec *declspec;
130 struct cdecl_declarator *declarator;
132 struct parse_item *item;
136 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
137 static void free_decl(struct cdecl *);
139 static void free_declspec(struct cdecl_declspec *x)
141 struct cdecl_declspec *p;
149 static void free_declarator(struct cdecl_declarator *x)
151 struct cdecl_declarator *p;
157 case CDECL_DECL_NULL:
159 case CDECL_DECL_IDENT:
160 case CDECL_DECL_ARRAY:
162 case CDECL_DECL_POINTER:
163 free_declspec(x->u.pointer.qualifiers);
165 case CDECL_DECL_FUNCTION:
166 free_decl(x->u.function.parameters);
177 static void free_decl(struct cdecl *x)
184 /* The specifiers may be shared by an entire chain. */
185 if (!p || p->specifiers != x->specifiers)
186 free_declspec(x->specifiers);
188 free_declarator(x->declarators);
194 void cdecl__free(struct cdecl *decl)
200 * Join two declaration specifier lists into a single list, with "a" being the
201 * head of the new list.
203 * The list "a" is assumed to be nonempty.
205 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
213 * Join three specifier lists into a single list, and returns the head of
216 * The list "b" is assumed to be a singleton list.
218 static struct cdecl_declspec *join_specs3(struct cdecl_declspec *a,
219 struct cdecl_declspec *b,
220 struct cdecl_declspec *c)
228 * Reverse the order of a "struct cdecl" list, and return the new first
229 * element of the list (i.e., the last element of the original list).
231 static struct cdecl *reverse_decls(struct cdecl *decl)
233 struct cdecl *prev, *next;
235 for (prev = NULL; decl; decl = next) {
245 * Alter an abstract declarator (type name) to declare an identifier instead,
246 * used by the English parser rules to reduce "identifier as type" sequences.
248 static struct cdecl *
249 insert_identifier(struct cdecl *decl, struct parse_item *ident)
251 struct cdecl_declarator *d, **p = &decl->declarators;
253 while ((d = *p)->child)
256 *p = &ident->u.declarator;
260 static struct cdecl_declarator *nulldecl(void)
262 static const struct cdecl_declarator nulldecl = {0};
263 return (void *)&nulldecl;
265 #define NULLDECL (nulldecl())
269 %destructor { free($$); } <item>
270 %destructor { free_declspec($$); } <declspec>
271 %destructor { free_declarator($$); } <declarator>
272 %destructor { free_decl($$); } <decl>
275 %token T_LEX_ERROR "@@@"
277 %token <item> T_IDENT "identifier"
278 %token <uintval> T_UINT "integer constant"
280 %token T_SEMICOLON ";"
281 %token T_ASTERISK "*"
284 %token T_LBRACKET "["
285 %token T_RBRACKET "]"
287 %token T_ELLIPSIS "..."
289 %token <spectype> T_TYPEDEF "typedef"
290 %token <spectype> T_EXTERN "extern"
291 %token <spectype> T_STATIC "static"
292 %token <spectype> T_AUTO "auto"
293 %token <spectype> T_REGISTER "register"
295 %token <spectype> T_INLINE "inline"
297 %token <spectype> T_RESTRICT "restrict"
298 %token <spectype> T_VOLATILE "volatile"
299 %token <spectype> T_CONST "const"
301 %token <spectype> T_VOID "void"
302 %token <spectype> T_CHAR "char"
303 %token <spectype> T_SHORT "short"
304 %token <spectype> T_INT "int"
305 %token <spectype> T_LONG "long"
306 %token <spectype> T_FLOAT "float"
307 %token <spectype> T_DOUBLE "double"
308 %token <spectype> T_SIGNED "signed"
309 %token <spectype> T_UNSIGNED "unsigned"
310 %token <spectype> T_BOOL "_Bool"
311 %token <spectype> T_COMPLEX "_Complex"
312 %token <spectype> T_IMAGINARY "_Imaginary"
314 %token <spectype> T_STRUCT "struct"
315 %token <spectype> T_UNION "union"
316 %token <spectype> T_ENUM "enum"
322 %token T_DECLARE "declare"
323 %token T_POINTER "pointer"
324 %token T_FUNCTION "function"
325 %token T_RETURNING "returning"
326 %token T_ARRAY "array"
330 %token T_VLA "variable-length"
332 %type <item> vla_ident
333 %type <uintval> array_length
334 %type <boolval> varargs
335 %type <spectype> declspec_simple qualifier_simple
336 %type <spectype> typespec_simple typespec_tagged
337 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
338 %type <declspec> qualifier qualifiers
339 %type <declspec> declspecs declspecs_notype declspecs_noid
340 %type <declarator> direct_declarator declarator pointer array parens postfix
341 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
342 %type <decl> cdecl declaration declarators declarator_wrap parameter
344 %type <item> english_vla
345 %type <declspec> storage_func_specs post_specs
346 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
347 %type <declarator> english_declarator english_array english_function
348 %type <declarator> english_parameter_list null_decl
349 %type <decl> english english_declaration english_parameter
351 /* Precedence declaration to avoid conflict in english_parameter; see below. */
357 input: cdecl { *out = $1; }
358 cdecl: english | declaration
361 declaration: declspecs declarators semi {
362 $$ = reverse_decls($2);
367 * We support parsing declarations using arbitrary identifiers as type
368 * specifiers (a la C typedef). To avoid confusion with identifiers that
369 * may also be used as declarators, note the following:
371 * (a) Every valid C declaration must have at least one type specifier, and
372 * (b) Valid declarations with typedef names have exactly one type specifier.
374 * So the rule applied when parsing specifiers is: an identifier is a type
375 * specifier only if we have not yet seen any type specifiers whatsoever
376 * (within one declaration specifier list).
378 * Treating identifiers as type specifiers by default can lead to strange and
379 * unexpected parses; libcdecl applies a simplification step to the resulting
380 * parse tree afterwards.
382 declspecs: declspecs_notype typespec declspecs_noid {
383 $$ = join_specs3($1, $2, $3);
386 declspecs_notype: { $$ = NULL; } | declspecs_notype declspec_notype {
391 declspecs_noid: { $$ = NULL; } | declspecs_noid declspec_noid {
396 qualifiers: { $$ = NULL; } | qualifiers qualifier {
401 declarators: declarator_wrap | declarators T_COMMA declarator_wrap {
406 declarator_wrap: declarator {
407 ALLOC_DECL($$, NULL, $1);
410 declspec_simple: T_AUTO
417 typespec_simple: T_VOID
430 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
432 qualifier_simple: T_CONST
436 declspec_notype: qualifier | declspec_simple { ALLOC_DECLSPEC($$, $1); }
437 typespec_noid: typespec_simple { ALLOC_DECLSPEC($$, $1); }
438 qualifier: qualifier_simple { ALLOC_DECLSPEC($$, $1); }
440 typespec: typespec_noid | typespec_tagged T_IDENT {
441 /* Compiler should be able to elide this assignment. */
442 $2->u.declspec.ident = $2->u.declarator.u.ident;
444 $$ = &$2->u.declspec;
448 declspec_noid: declspec_notype | typespec_noid
450 vla_ident: T_IDENT | T_ASTERISK {
451 if (!($$ = cdecl__alloc_item(1)))
456 array: T_LBRACKET array_length T_RBRACKET {
458 } | T_LBRACKET vla_ident T_RBRACKET {
459 $$ = &$2->u.declarator;
460 $$->type = CDECL_DECL_ARRAY;
461 $$->u.array.vla = $$->u.ident;
462 $$->u.array.length = 0;
465 parameter: declspecs declarator {
466 ALLOC_DECL($$, $1, $2);
469 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
471 parameter_type_list: parameter {
472 ALLOC_FUNCTION_($$, $1);
473 } | parameter_type_list T_COMMA parameter {
475 $3->next = $$->u.function.parameters;
476 $$->u.function.parameters = $3;
479 parens: T_LPAREN parameter_type_list varargs T_RPAREN {
480 $2->u.function.parameters = reverse_decls($2->u.function.parameters);
481 $2->u.function.variadic = $3;
483 } | T_LPAREN declarator_ish T_RPAREN {
484 struct cdecl *fake_params;
486 ALLOC_DECL(fake_params, NULL, $2);
487 ALLOC_FUNCTION_($$, fake_params);
490 pointer: T_ASTERISK qualifiers direct_declarator {
491 ALLOC_POINTER($$, $2, $3);
492 } | T_ASTERISK qualifiers pointer {
493 ALLOC_POINTER($$, $2, $3);
496 declarator: direct_declarator | pointer
497 declarator_ish: direct_declarator_ish | pointer
498 postfix: array | parens
500 direct_declarator_ish: {
502 } | direct_declarator_ish postfix {
510 $$ = &$1->u.declarator;
511 } | direct_declarator postfix {
516 english: T_DECLARE T_IDENT T_AS english_declaration {
517 $$ = insert_identifier($4, $2);
518 } | T_TYPE english_declaration {
523 * We use a precedence declaration to prefer shifting an identifier
524 * over reducing this empty rule; see below.
526 storage_func_specs: %prec T_TYPE { $$ = NULL; }
527 storage_func_specs: storage_func_specs declspec_simple {
528 ALLOC_DECLSPEC($$, $2);
532 type_qual_spec: typespec_noid | qualifier
534 type_qual_specs: { $$ = NULL; } | type_qual_specs type_qual_spec {
540 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
541 * conflicts with pointer declarators. So we end up needing to stitch
542 * together three different specifiers lists.
544 post_specs: qualifiers typespec type_qual_specs {
545 $$ = join_specs3($1, $2, $3);
548 english_declaration: storage_func_specs english_declarator post_specs {
550 ALLOC_DECL($$, $3, $2);
553 english_declarator: {
555 } | english_declarator qualifiers T_POINTER T_TO {
556 ALLOC_POINTER($$, $2, $1);
557 } | english_declarator english_array {
560 } | english_declarator english_function {
565 english_function: T_FUNCTION T_RETURNING {
566 ALLOC_FUNCTION($$, NULL, false);
567 } | T_FUNCTION T_LPAREN english_parameter_list varargs T_RPAREN T_RETURNING {
568 $3->u.function.parameters = reverse_decls($3->u.function.parameters);
569 $3->u.function.variadic = $4;
573 english_parameter_list: english_parameter {
574 ALLOC_FUNCTION_($$, $1);
575 } | english_parameter_list T_COMMA english_parameter {
577 $3->next = $$->u.function.parameters;
578 $$->u.function.parameters = $3;
581 typedef_name_qual: T_IDENT qualifiers {
582 /* Compiler should be able to elide this assignment. */
583 $1->u.declspec.ident = $1->u.declarator.u.ident;
585 $$ = &$1->u.declspec;
586 $$->type = CDECL_TYPE_IDENT;
595 * There is a shift/reduce conflict here when an identifier appears as the
596 * first token. The conflict is between shifting T_IDENT, or reducing the
597 * empty production for storage_func_specs (cf. english_declaration).
599 * - In either case, if we reduce, we won't match T_IDENT T_AS since the
600 * stack now has the extra storage_func_specs nonterminal symbol.
601 * - And if we shift, we won't match english_declaration since it is
602 * too late to add storage_func_specs to the stack.
604 * The only valid input affected by the conflict is a simple type names,
605 * possibly followed by qualifiers. So the conflict is adequately resolved
606 * by shifting, so long as we have a special-case reduction to handle this.
608 english_parameter: english_declaration | typedef_name_qual null_decl {
609 ALLOC_DECL($$, $1, $2);
610 } | T_IDENT T_AS english_declaration {
611 $$ = insert_identifier($3, $1);
614 english_array: T_VLA T_ARRAY english_vla T_OF {
615 $$ = &$3->u.declarator;
616 $$->type = CDECL_DECL_ARRAY;
617 $$->u.array.vla = $$->u.ident;
618 $$->u.array.length = 0;
619 } | T_ARRAY array_length T_OF {
623 array_length: { $$ = 0; }
624 array_length: T_UINT {
626 cdecl__errmsg(CDECL__EZEROARRAY);
631 english_vla: T_IDENT | {
632 if (!($$ = cdecl__alloc_item(1)))
640 * Expose the token string table to the rest of the library, in order to
641 * produce strings that match parser keywords.
643 * In order for this to work properly, the Bison output must be postprocessed
644 * by fix-yytname.awk to remove pointless quotation marks from the keyword
647 const char *cdecl__token_name(unsigned token)
649 return yytname[YYTRANSLATE(token)];
653 * Current versions of GCC (up to 13) want to inline this function into the
654 * parser even when optimizing for size and the results are not great, so
655 * try to prevent such inlining.
657 CDECL__NOINLINE static void
658 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
660 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
663 cdecl__err("%s", err);