]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
libcdecl: Perform all parser allocations via cdecl__alloc_item.
[cdecl99.git] / src / parse.y
1 %code top {
2 /*
3  *  Parser for C declarations.
4  *  Copyright © 2011-2012, 2021, 2023 Nick Bowler
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 }
20
21 %name-prefix "cdecl__yy"
22 %parse-param {void *scanner}
23 %parse-param {struct cdecl **out}
24 %lex-param {yyscan_t scanner}
25 %define api.pure
26 %error-verbose
27 %locations
28
29 %{
30 #include <config.h>
31 #include <assert.h>
32 #include <stdbool.h>
33
34 #include "scan.h"
35 #include "cdecl.h"
36 #include "cdecl-internal.h"
37 #include "errmsg.h"
38
39 #define FAIL(msg) do { \
40         yyerror(&yylloc, NULL, NULL, msg); \
41         YYERROR; \
42 } while (0)
43
44 /*
45  * Allocate a parse tree node via cdecl__alloc_item.
46  *
47  * - m1 specifies the item's union member to assign to ptr, which selects the
48  *   type of node to allocate.
49  *
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.
55  *
56  * The resulting pointer can be passed directly to free, as the union is the
57  * first member of the parse_item structure.
58  *
59  * Use the wrapper macros below instead of this one.
60  */
61 #define ALLOC_ITEM(ptr, m1, m2) do { \
62         struct parse_item *item; \
63         if (!(item = cdecl__alloc_item(0))) \
64                 YYERROR; \
65         item->u.m1.m2 = (void *)item->u.declarator.child; \
66         (ptr) = &item->u.m1; \
67 } while (0)
68
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)
73
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_; \
79 } while (0)
80
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_; \
86 } while (0)
87
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_; \
93 } while (0)
94
95 #define ALLOC_DECLSPEC(ptr, type_) do { \
96         ALLOC_ITEM_DECLSPEC(ptr); \
97         (ptr)->type = type_; \
98         (ptr)->ident = NULL; \
99 } while (0)
100
101 #define ALLOC_DECL(ptr, specifiers_, declarators_) do { \
102         ALLOC_ITEM_DECL(ptr); \
103         (ptr)->specifiers = specifiers_; \
104         (ptr)->declarators = declarators_; \
105 } while (0)
106
107 /*
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.
111  */
112 #define yytnamerr(a, b) cdecl__strlcpy(a, b, (a) ? INT_MAX : 0)
113 %}
114
115 %code requires {
116 #include <inttypes.h>
117 }
118
119 %code provides {
120 void cdecl__free(struct cdecl *);
121 int cdecl__yyparse(void *scanner, struct cdecl **out);
122 const char *cdecl__token_name(unsigned token);
123 }
124
125 %union {
126         uintmax_t uintval;
127         unsigned spectype;
128         _Bool boolval;
129         struct cdecl_declspec *declspec;
130         struct cdecl_declarator *declarator;
131         struct cdecl *decl;
132         struct parse_item *item;
133 }
134
135 %{
136 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
137 static void free_decl(struct cdecl *);
138
139 static void free_declspec(struct cdecl_declspec *x)
140 {
141         struct cdecl_declspec *p;
142         while (x) {
143                 p = x->next;
144                 free(x);
145                 x = p;
146         }
147 }
148
149 static void free_declarator(struct cdecl_declarator *x)
150 {
151         struct cdecl_declarator *p;
152
153         while (x) {
154                 p = x->child;
155
156                 switch (x->type) {
157                 case CDECL_DECL_NULL:
158                         x = NULL;
159                 case CDECL_DECL_IDENT:
160                 case CDECL_DECL_ARRAY:
161                         break;
162                 case CDECL_DECL_POINTER:
163                         free_declspec(x->u.pointer.qualifiers);
164                         break;
165                 case CDECL_DECL_FUNCTION:
166                         free_decl(x->u.function.parameters);
167                         break;
168                 default:
169                         assert(0);
170                 }
171
172                 free(x);
173                 x = p;
174         }
175 }
176
177 static void free_decl(struct cdecl *x)
178 {
179         struct cdecl *p;
180
181         while (x) {
182                 p = x->next;
183
184                 /* The specifiers may be shared by an entire chain. */
185                 if (!p || p->specifiers != x->specifiers)
186                         free_declspec(x->specifiers);
187
188                 free_declarator(x->declarators);
189                 free(x);
190                 x = p;
191         }
192 }
193
194 void cdecl__free(struct cdecl *decl)
195 {
196         free_decl(decl);
197 }
198
199 /*
200  * Join two declaration specifier lists into a single list, with "a" being the
201  * head of the new list.
202  *
203  * The list "a" is assumed to be nonempty.
204  */
205 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
206 {
207         while (a->next)
208                 a = a->next;
209         a->next = b;
210 }
211
212 /*
213  * Alter an abstract declarator (type name) to declare an identifier instead,
214  * used by the English parser rules to reduce "identifier as type" sequences.
215  */
216 static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
217 {
218         struct cdecl_declarator *d, **p = &decl->declarators;
219
220         while ((d = *p)->child)
221                 p = &d->child;
222
223         *p = &ident->u.declarator;
224         return decl;
225 }
226
227 static struct cdecl_declarator *nulldecl(void)
228 {
229         static const struct cdecl_declarator nulldecl = {0};
230         return (void *)&nulldecl;
231 }
232 #define NULLDECL (nulldecl())
233
234 %}
235
236 %destructor { free($$); }            <item>
237 %destructor { free_declspec($$); }   <declspec>
238 %destructor { free_declarator($$); } <declarator>
239 %destructor { free_decl($$); }       <decl>
240
241 /* Magic tokens */
242 %token T_LEX_ERROR
243
244 %token <item>    T_IDENT "identifier"
245 %token <uintval> T_UINT  "integer constant"
246
247 %token T_SEMICOLON ";"
248 %token T_ASTERISK  "*"
249 %token T_LPAREN    "("
250 %token T_RPAREN    ")"
251 %token T_LBRACKET  "["
252 %token T_RBRACKET  "]"
253 %token T_COMMA     ","
254 %token T_ELLIPSIS  "..."
255
256 %token <spectype> T_TYPEDEF   "typedef"
257 %token <spectype> T_EXTERN    "extern"
258 %token <spectype> T_STATIC    "static"
259 %token <spectype> T_AUTO      "auto"
260 %token <spectype> T_REGISTER  "register"
261
262 %token <spectype> T_INLINE    "inline"
263
264 %token <spectype> T_RESTRICT  "restrict"
265 %token <spectype> T_VOLATILE  "volatile"
266 %token <spectype> T_CONST     "const"
267
268 %token <spectype> T_VOID      "void"
269 %token <spectype> T_CHAR      "char"
270 %token <spectype> T_SHORT     "short"
271 %token <spectype> T_INT       "int"
272 %token <spectype> T_LONG      "long"
273 %token <spectype> T_FLOAT     "float"
274 %token <spectype> T_DOUBLE    "double"
275 %token <spectype> T_SIGNED    "signed"
276 %token <spectype> T_UNSIGNED  "unsigned"
277 %token <spectype> T_BOOL      "_Bool"
278 %token <spectype> T_COMPLEX   "_Complex"
279 %token <spectype> T_IMAGINARY "_Imaginary"
280
281 %token <spectype> T_STRUCT    "struct"
282 %token <spectype> T_UNION     "union"
283 %token <spectype> T_ENUM      "enum"
284
285 /*
286  * English keywords.
287  */
288 %token T_TYPE      "type"
289 %token T_DECLARE   "declare"
290 %token T_POINTER   "pointer"
291 %token T_FUNCTION  "function"
292 %token T_RETURNING "returning"
293 %token T_ARRAY     "array"
294 %token T_TO        "to"
295 %token T_OF        "of"
296 %token T_AS        "as"
297 %token T_VLA       "variable-length"
298
299 %type <item>       vla_ident
300 %type <uintval>    array_length
301 %type <boolval>    varargs
302 %type <spectype>   declspec_simple qualifier_simple
303 %type <spectype>   typespec_simple typespec_tagged
304 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
305 %type <declspec>   qualifier qualifiers
306 %type <declspec>   declspecs declspecs_noid
307 %type <declarator> direct_declarator declarator pointer array parens postfix
308 %type <declarator> direct_declarator_ish declarator_ish parameter_type_list
309 %type <decl>       cdecl declaration declarators declarator_wrap parameter
310
311 %type <item>       english_vla
312 %type <declspec>   storage_func_specs post_specs
313 %type <declspec>   type_qual_spec type_qual_specs typedef_name_qual
314 %type <declarator> english_declarator english_array english_function
315 %type <declarator> english_parameter_list null_decl
316 %type <decl>       english english_declaration english_parameter
317
318 /* Precedence declaration to avoid conflict in english_parameter; see below. */
319 %right T_TYPE
320 %right T_IDENT
321
322 %%
323
324 input: cdecl { *out = $1; }
325 cdecl: english | declaration
326
327 semi: | T_SEMICOLON
328 declaration: declspecs declarators semi {
329         $$ = $2;
330         $$->specifiers = $1;
331 };
332
333 /*
334  * We support parsing declarations using arbitrary identifiers as type
335  * specifiers (a la C typedef).  To avoid confusion with identifiers that
336  * may also be used as declarators, note the following:
337  *
338  *  (a) Every valid C declaration must have at least one type specifier, and
339  *  (b) Valid declarations with typedef names have exactly one type specifier.
340  *
341  * So the rule applied when parsing specifiers is: an identifier is a type
342  * specifier only if we have not yet seen any type specifiers whatsoever
343  * (within one declaration specifier list).
344  *
345  * Treating identifiers as type specifiers by default can lead to strange and
346  * unexpected parses; libcdecl applies a simplification step to the resulting
347  * parse tree afterwards.
348  */
349 declspecs: declspec_notype declspecs {
350         $$ = $1;
351         $$->next = $2;
352 } | typespec declspecs_noid {
353         $$ = $1;
354         $$->next = $2;
355 }
356
357 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
358         $$ = $1;
359         $$->next = $2;
360 }
361
362 qualifiers: { $$ = NULL; } | qualifiers qualifier {
363         $$ = $2;
364         $$->next = $1;
365 }
366
367 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
368         $$ = $1;
369         $$->next = $3;
370 }
371
372 declarator_wrap: declarator {
373         ALLOC_DECL($$, NULL, $1);
374 }
375
376 declspec_simple: T_AUTO
377         | T_TYPEDEF
378         | T_EXTERN
379         | T_STATIC
380         | T_REGISTER
381         | T_INLINE
382
383 typespec_simple: T_VOID
384         | T_CHAR
385         | T_SHORT
386         | T_INT
387         | T_LONG
388         | T_FLOAT
389         | T_DOUBLE
390         | T_SIGNED
391         | T_UNSIGNED
392         | T_BOOL
393         | T_COMPLEX
394         | T_IMAGINARY
395
396 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
397
398 qualifier_simple: T_CONST
399         | T_RESTRICT
400         | T_VOLATILE
401
402 declspec_notype: qualifier | declspec_simple { ALLOC_DECLSPEC($$, $1); }
403 typespec_noid: typespec_simple { ALLOC_DECLSPEC($$, $1); }
404 qualifier: qualifier_simple { ALLOC_DECLSPEC($$, $1); }
405
406 typespec: typespec_noid | typespec_tagged T_IDENT {
407         /* Compiler should be able to elide this assignment. */
408         $2->u.declspec.ident = $2->u.declarator.u.ident;
409
410         $$ = &$2->u.declspec;
411         $$->type = $1;
412 }
413
414 declspec_noid: declspec_notype | typespec_noid
415
416 vla_ident: T_IDENT | T_ASTERISK {
417         if (!($$ = cdecl__alloc_item(1)))
418                 YYERROR;
419         *$$->s = 0;
420 }
421
422 array: T_LBRACKET array_length T_RBRACKET {
423         ALLOC_ARRAY($$, $2);
424 } | T_LBRACKET vla_ident T_RBRACKET {
425         $$ = &$2->u.declarator;
426         $$->type = CDECL_DECL_ARRAY;
427         $$->u.array.vla = $$->u.ident;
428         $$->u.array.length = 0;
429 }
430
431 parameter: declspecs declarator {
432         ALLOC_DECL($$, $1, $2);
433 }
434
435 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
436
437 parameter_type_list: parameter varargs {
438         ALLOC_FUNCTION($$, $1, $2);
439 } | parameter T_COMMA parameter_type_list {
440         $$ = $3;
441         $1->next = $$->u.function.parameters;
442         $$->u.function.parameters = $1;
443 }
444
445 parens: T_LPAREN parameter_type_list T_RPAREN {
446         $$ = $2;
447 } | T_LPAREN declarator_ish T_RPAREN {
448         struct cdecl *fake_params;
449
450         ALLOC_DECL(fake_params, NULL, $2);
451         ALLOC_FUNCTION($$, fake_params, false);
452 }
453
454 pointer: T_ASTERISK qualifiers direct_declarator {
455         ALLOC_POINTER($$, $2, $3);
456 } | T_ASTERISK qualifiers pointer {
457         ALLOC_POINTER($$, $2, $3);
458 }
459
460 declarator: direct_declarator | pointer
461 declarator_ish: direct_declarator_ish | pointer
462 postfix: array | parens
463
464 direct_declarator_ish: {
465         $$ = NULLDECL;
466 } | direct_declarator_ish postfix {
467         $$ = $2;
468         $$->child = $1;
469 }
470
471 direct_declarator: {
472         $$ = NULLDECL;
473 } | T_IDENT {
474         $$ = &$1->u.declarator;
475 } | direct_declarator postfix {
476         $$ = $2;
477         $$->child = $1;
478 }
479
480 english: T_DECLARE T_IDENT T_AS english_declaration {
481         $$ = insert_identifier($4, $2);
482 } | T_TYPE english_declaration {
483         $$ = $2;
484 }
485
486 /*
487  * We use a precedence declaration to prefer shifting an identifier
488  * over reducing this empty rule; see below.
489  */
490 storage_func_specs: %prec T_TYPE { $$ = NULL; }
491 storage_func_specs: declspec_simple storage_func_specs {
492         ALLOC_DECLSPEC($$, $1);
493         $$->next = $2;
494 }
495
496 type_qual_spec: typespec_noid | qualifier
497
498 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
499         $$ = $1;
500         $$->next = $2;
501 }
502
503 /*
504  * The "qualifiers" nonterminal needs to be used here to avoid shift/reduce
505  * conflicts with pointer declarators.  So we end up needing to stitch
506  * together three different specifiers lists.
507  */
508 post_specs: qualifiers typespec type_qual_specs {
509         $2->next = $3;
510         join_specs($2, $1);
511         $$ = $2;
512 }
513
514 english_declaration: storage_func_specs english_declarator post_specs {
515         join_specs($3, $1);
516         ALLOC_DECL($$, $3, $2);
517 }
518
519 english_declarator: {
520         $$ = NULLDECL;
521 } | english_declarator qualifiers T_POINTER T_TO {
522         ALLOC_POINTER($$, $2, $1);
523 } | english_declarator english_array {
524         $$ = $2;
525         $$->child = $1;
526 } | english_declarator english_function {
527         $$ = $2;
528         $$->child = $1;
529 }
530
531 english_function: T_FUNCTION T_RETURNING {
532         ALLOC_FUNCTION($$, NULL, false);
533 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
534         $$ = $3;
535 }
536
537 english_parameter_list: english_parameter varargs {
538         ALLOC_FUNCTION($$, $1, $2);
539 } | english_parameter T_COMMA english_parameter_list {
540         $$ = $3;
541         $1->next = $$->u.function.parameters;
542         $$->u.function.parameters = $1;
543 }
544
545 typedef_name_qual: T_IDENT qualifiers {
546         /* Compiler should be able to elide this assignment. */
547         $1->u.declspec.ident = $1->u.declarator.u.ident;
548
549         $$ = &$1->u.declspec;
550         $$->type = CDECL_TYPE_IDENT;
551         $$->next = $2;
552 }
553
554 null_decl: {
555         $$ = NULLDECL;
556 }
557
558 /*
559  * There is a shift/reduce conflict here when an identifier appears as the
560  * first token.  The conflict is between shifting T_IDENT, or reducing the
561  * empty production for storage_func_specs (cf. english_declaration).
562  *
563  *   - In either case, if we reduce, we won't match T_IDENT T_AS since the
564  *     stack now has the extra storage_func_specs nonterminal symbol.
565  *   - And if we shift, we won't match english_declaration since it is
566  *     too late to add storage_func_specs to the stack.
567  *
568  * The only valid input affected by the conflict is a simple type names,
569  * possibly followed by qualifiers.  So the conflict is adequately resolved
570  * by shifting, so long as we have a special-case reduction to handle this.
571  */
572 english_parameter: english_declaration | typedef_name_qual null_decl {
573         ALLOC_DECL($$, $1, $2);
574 } | T_IDENT T_AS english_declaration {
575         $$ = insert_identifier($3, $1);
576 }
577
578 english_array: T_VLA T_ARRAY english_vla T_OF {
579         $$ = &$3->u.declarator;
580         $$->type = CDECL_DECL_ARRAY;
581         $$->u.array.vla = $$->u.ident;
582         $$->u.array.length = 0;
583 } | T_ARRAY array_length T_OF {
584         ALLOC_ARRAY($$, $2);
585 }
586
587 array_length: { $$ = 0; }
588 array_length: T_UINT {
589         if (!($$ = $1))
590                 FAIL(_("array length must be positive"));
591 }
592
593 english_vla: T_IDENT | {
594         if (!($$ = cdecl__alloc_item(1)))
595                 YYERROR;
596         *$$->s = 0;
597 }
598
599 %%
600
601 /*
602  * Expose the token string table to the rest of the library, in order to
603  * produce strings that match parser keywords.
604  *
605  * In order for this to work properly, the Bison output must be postprocessed
606  * by fix-yytname.awk to remove pointless quotation marks from the keyword
607  * strings.
608  */
609 const char *cdecl__token_name(unsigned token)
610 {
611         return yytname[YYTRANSLATE(token)];
612 }
613
614 static void
615 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
616 {
617         if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
618                 return;
619
620         cdecl__err(CDECL_ENOPARSE, "%s", err);
621 }