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>
127 void cdecl__free(struct cdecl *);
128 int cdecl__yyparse(void *scanner, struct cdecl **out);
129 const char *cdecl__token_name(unsigned token);
136 struct cdecl_declspec *declspec;
137 struct cdecl_declarator *declarator;
139 struct parse_item *item;
143 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
144 static void free_decl(struct cdecl *);
146 static void free_declspec(struct cdecl_declspec *x)
148 struct cdecl_declspec *p;
156 static void free_declarator(struct cdecl_declarator *x)
158 struct cdecl_declarator *p;
164 case CDECL_DECL_NULL:
166 case CDECL_DECL_IDENT:
167 case CDECL_DECL_ARRAY:
169 case CDECL_DECL_POINTER:
170 free_declspec(x->u.pointer.qualifiers);
172 case CDECL_DECL_FUNCTION:
173 free_decl(x->u.function.parameters);
184 static void free_decl(struct cdecl *x)
191 /* The specifiers may be shared by an entire chain. */
192 if (!p || p->specifiers != x->specifiers)
193 free_declspec(x->specifiers);
195 free_declarator(x->declarators);
201 void cdecl__free(struct cdecl *decl)
207 * Join two declaration specifier lists into a single list, with "a" being the
208 * head of the new list.
210 * The list "a" is assumed to be nonempty.
212 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
220 * Alter an abstract declarator (type name) to declare an identifier instead,
221 * used by the English parser rules to reduce "identifier as type" sequences.
223 static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
225 struct cdecl_declarator *d, **p = &decl->declarators;
227 while ((d = *p)->child)
230 *p = &ident->u.declarator;
234 static struct cdecl_declarator *nulldecl(void)
236 static const struct cdecl_declarator nulldecl = {0};
237 return (void *)&nulldecl;
239 #define NULLDECL (nulldecl())
243 %destructor { free($$); } <item>
244 %destructor { free_declspec($$); } <declspec>
245 %destructor { free_declarator($$); } <declarator>
246 %destructor { free_decl($$); } <decl>
251 %token <item> T_IDENT "identifier"
252 %token <uintval> T_UINT "integer constant"
254 %token T_SEMICOLON ";"
255 %token T_ASTERISK "*"
258 %token T_LBRACKET "["
259 %token T_RBRACKET "]"
261 %token T_ELLIPSIS "..."
263 %token <spectype> T_TYPEDEF "typedef"
264 %token <spectype> T_EXTERN "extern"
265 %token <spectype> T_STATIC "static"
266 %token <spectype> T_AUTO "auto"
267 %token <spectype> T_REGISTER "register"
269 %token <spectype> T_INLINE "inline"
271 %token <spectype> T_RESTRICT "restrict"
272 %token <spectype> T_VOLATILE "volatile"
273 %token <spectype> T_CONST "const"
275 %token <spectype> T_VOID "void"
276 %token <spectype> T_CHAR "char"
277 %token <spectype> T_SHORT "short"
278 %token <spectype> T_INT "int"
279 %token <spectype> T_LONG "long"
280 %token <spectype> T_FLOAT "float"
281 %token <spectype> T_DOUBLE "double"
282 %token <spectype> T_SIGNED "signed"
283 %token <spectype> T_UNSIGNED "unsigned"
284 %token <spectype> T_BOOL "_Bool"
285 %token <spectype> T_COMPLEX "_Complex"
286 %token <spectype> T_IMAGINARY "_Imaginary"
288 %token <spectype> T_STRUCT "struct"
289 %token <spectype> T_UNION "union"
290 %token <spectype> T_ENUM "enum"
296 %token T_DECLARE "declare"
297 %token T_POINTER "pointer"
298 %token T_FUNCTION "function"
299 %token T_RETURNING "returning"
300 %token T_ARRAY "array"
304 %token T_VLA "variable-length"
306 %type <item> vla_ident
307 %type <uintval> array_length
308 %type <boolval> varargs
309 %type <spectype> declspec_simple qualifier_simple
310 %type <spectype> typespec_simple typespec_tagged
311 %type <declspec> declspec_notype declspec_noid typespec_noid typespec
312 %type <declspec> qualifier qualifiers
313 %type <declspec> declspecs declspecs_noid
314 %type <declarator> direct_declarator declarator pointer array parens postfix
315 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
316 %type <decl> cdecl declaration declarators declarator_wrap parameter
318 %type <item> english_vla
319 %type <declspec> storage_func_specs post_specs
320 %type <declspec> type_qual_spec type_qual_specs typedef_name_qual
321 %type <declarator> english_declarator english_array english_function
322 %type <declarator> english_parameter_list null_decl
323 %type <decl> english english_declaration english_parameter
325 /* Precedence declaration to avoid conflict in english_parameter; see below. */
331 input: cdecl { *out = $1; }
332 cdecl: english | declaration
335 declaration: declspecs declarators semi {
341 * We support parsing declarations using arbitrary identifiers as type
342 * specifiers (a la C typedef). To avoid confusion with identifiers that
343 * may also be used as declarators, note the following:
345 * (a) Every valid C declaration must have at least one type specifier, and
346 * (b) Valid declarations with typedef names have exactly one type specifier.
348 * So the rule applied when parsing specifiers is: an identifier is a type
349 * specifier only if we have not yet seen any type specifiers whatsoever
350 * (within one declaration specifier list).
352 * Treating identifiers as type specifiers by default can lead to strange and
353 * unexpected parses; libcdecl applies a simplification step to the resulting
354 * parse tree afterwards.
356 declspecs: declspec_notype declspecs {
359 } | typespec declspecs_noid {
364 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
369 qualifiers: { $$ = NULL; } | qualifiers qualifier {
374 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
379 declarator_wrap: declarator {
380 ALLOC_DECL($$, NULL, $1);
383 declspec_simple: T_AUTO
390 typespec_simple: T_VOID
403 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
405 qualifier_simple: T_CONST
409 declspec_notype: qualifier | declspec_simple { ALLOC_DECLSPEC($$, $1); }
410 typespec_noid: typespec_simple { ALLOC_DECLSPEC($$, $1); }
411 qualifier: qualifier_simple { ALLOC_DECLSPEC($$, $1); }
413 typespec: typespec_noid | typespec_tagged T_IDENT {
414 /* Compiler should be able to elide this assignment. */
415 $2->u.declspec.ident = $2->u.declarator.u.ident;
417 $$ = &$2->u.declspec;
421 declspec_noid: declspec_notype | typespec_noid
423 vla_ident: T_IDENT | T_ASTERISK {
424 if (!($$ = cdecl__alloc_item(1)))
429 array: T_LBRACKET array_length T_RBRACKET {
431 } | T_LBRACKET vla_ident T_RBRACKET {
432 $$ = &$2->u.declarator;
433 $$->type = CDECL_DECL_ARRAY;
434 $$->u.array.vla = $$->u.ident;
435 $$->u.array.length = 0;
438 parameter: declspecs declarator {
439 ALLOC_DECL($$, $1, $2);
442 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
444 parameter_type_list: parameter varargs {
445 ALLOC_FUNCTION($$, $1, $2);
446 } | parameter T_COMMA parameter_type_list {
448 $1->next = $$->u.function.parameters;
449 $$->u.function.parameters = $1;
452 parens: T_LPAREN parameter_type_list T_RPAREN {
454 } | T_LPAREN declarator_ish T_RPAREN {
455 struct cdecl *fake_params;
457 ALLOC_DECL(fake_params, NULL, $2);
458 ALLOC_FUNCTION($$, fake_params, false);
461 pointer: T_ASTERISK qualifiers direct_declarator {
462 ALLOC_POINTER($$, $2, $3);
463 } | T_ASTERISK qualifiers pointer {
464 ALLOC_POINTER($$, $2, $3);
467 declarator: direct_declarator | pointer
468 declarator_ish: direct_declarator_ish | pointer
469 postfix: array | parens
471 direct_declarator_ish: {
473 } | direct_declarator_ish postfix {
481 $$ = &$1->u.declarator;
482 } | direct_declarator postfix {
487 english: T_DECLARE T_IDENT T_AS english_declaration {
488 $$ = insert_identifier($4, $2);
489 } | T_TYPE english_declaration {
494 * We use a precedence declaration to prefer shifting an identifier
495 * over reducing this empty rule; see below.
497 storage_func_specs: %prec T_TYPE { $$ = NULL; }
498 storage_func_specs: declspec_simple storage_func_specs {
499 ALLOC_DECLSPEC($$, $1);
503 type_qual_spec: typespec_noid | qualifier
505 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
511 * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
512 * conflicts with pointer declarators. So we end up needing to stitch
513 * together three different specifiers lists.
515 post_specs: qualifiers typespec type_qual_specs {
521 english_declaration: storage_func_specs english_declarator post_specs {
523 ALLOC_DECL($$, $3, $2);
526 english_declarator: {
528 } | english_declarator qualifiers T_POINTER T_TO {
529 ALLOC_POINTER($$, $2, $1);
530 } | english_declarator english_array {
533 } | english_declarator english_function {
538 english_function: T_FUNCTION T_RETURNING {
539 ALLOC_FUNCTION($$, NULL, false);
540 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
544 english_parameter_list: english_parameter varargs {
545 ALLOC_FUNCTION($$, $1, $2);
546 } | english_parameter T_COMMA english_parameter_list {
548 $1->next = $$->u.function.parameters;
549 $$->u.function.parameters = $1;
552 typedef_name_qual: T_IDENT qualifiers {
553 /* Compiler should be able to elide this assignment. */
554 $1->u.declspec.ident = $1->u.declarator.u.ident;
556 $$ = &$1->u.declspec;
557 $$->type = CDECL_TYPE_IDENT;
566 * There is a shift/reduce conflict here when an identifier appears as the
567 * first token. The conflict is between shifting T_IDENT, or reducing the
568 * empty production for storage_func_specs (cf. english_declaration).
570 * - In either case, if we reduce, we won't match T_IDENT T_AS since the
571 * stack now has the extra storage_func_specs nonterminal symbol.
572 * - And if we shift, we won't match english_declaration since it is
573 * too late to add storage_func_specs to the stack.
575 * The only valid input affected by the conflict is a simple type names,
576 * possibly followed by qualifiers. So the conflict is adequately resolved
577 * by shifting, so long as we have a special-case reduction to handle this.
579 english_parameter: english_declaration | typedef_name_qual null_decl {
580 ALLOC_DECL($$, $1, $2);
581 } | T_IDENT T_AS english_declaration {
582 $$ = insert_identifier($3, $1);
585 english_array: T_VLA T_ARRAY english_vla T_OF {
586 $$ = &$3->u.declarator;
587 $$->type = CDECL_DECL_ARRAY;
588 $$->u.array.vla = $$->u.ident;
589 $$->u.array.length = 0;
590 } | T_ARRAY array_length T_OF {
594 array_length: { $$ = 0; }
595 array_length: T_UINT {
597 FAIL(_("array length must be positive"));
600 english_vla: T_IDENT | {
601 if (!($$ = cdecl__alloc_item(1)))
609 * Expose the token string table to the rest of the library, in order to
610 * produce strings that match parser keywords.
612 * In order for this to work properly, the Bison output must be postprocessed
613 * by fix-yytname.awk to remove pointless quotation marks from the keyword
616 const char *cdecl__token_name(unsigned token)
618 return yytname[YYTRANSLATE(token)];
622 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
624 if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
627 cdecl__err(CDECL_ENOPARSE, "%s", err);