]> git.draconx.ca Git - cdecl99.git/blob - src/parse.y
14d593ef1622f57a97d0b5bbb26023a07ce0fafb
[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) ( (a) ? yytnamerr_copy(a, b) \
113                               : strlen(b) )
114
115 static size_t yytnamerr_copy(char *dst, const char *src)
116 {
117         return cdecl__strlcpy(dst, src, strlen(src)+1);
118 }
119 %}
120
121 %code requires {
122 #include <inttypes.h>
123 }
124
125 %code provides {
126 void cdecl__free(struct cdecl *);
127 int cdecl__yyparse(void *scanner, struct cdecl **out);
128 const char *cdecl__token_name(unsigned token);
129 }
130
131 %union {
132         uintmax_t uintval;
133         unsigned spectype;
134         _Bool boolval;
135         struct cdecl_declspec *declspec;
136         struct cdecl_declarator *declarator;
137         struct cdecl *decl;
138         struct parse_item *item;
139 }
140
141 %{
142 static void yyerror(YYLTYPE *, yyscan_t, struct cdecl **, const char *);
143 static void free_decl(struct cdecl *);
144
145 static void free_declspec(struct cdecl_declspec *x)
146 {
147         struct cdecl_declspec *p;
148         while (x) {
149                 p = x->next;
150                 free(x);
151                 x = p;
152         }
153 }
154
155 static void free_declarator(struct cdecl_declarator *x)
156 {
157         struct cdecl_declarator *p;
158
159         while (x) {
160                 p = x->child;
161
162                 switch (x->type) {
163                 case CDECL_DECL_NULL:
164                         x = NULL;
165                 case CDECL_DECL_IDENT:
166                 case CDECL_DECL_ARRAY:
167                         break;
168                 case CDECL_DECL_POINTER:
169                         free_declspec(x->u.pointer.qualifiers);
170                         break;
171                 case CDECL_DECL_FUNCTION:
172                         free_decl(x->u.function.parameters);
173                         break;
174                 default:
175                         assert(0);
176                 }
177
178                 free(x);
179                 x = p;
180         }
181 }
182
183 static void free_decl(struct cdecl *x)
184 {
185         struct cdecl *p;
186
187         while (x) {
188                 p = x->next;
189
190                 /* The specifiers may be shared by an entire chain. */
191                 if (!p || p->specifiers != x->specifiers)
192                         free_declspec(x->specifiers);
193
194                 free_declarator(x->declarators);
195                 free(x);
196                 x = p;
197         }
198 }
199
200 void cdecl__free(struct cdecl *decl)
201 {
202         free_decl(decl);
203 }
204
205 /*
206  * Join two declaration specifier lists into a single list, with "a" being the
207  * head of the new list.
208  *
209  * The list "a" is assumed to be nonempty.
210  */
211 static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
212 {
213         while (a->next)
214                 a = a->next;
215         a->next = b;
216 }
217
218 /*
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.
221  */
222 static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
223 {
224         struct cdecl_declarator *d, **p = &decl->declarators;
225
226         while ((d = *p)->child)
227                 p = &d->child;
228
229         *p = &ident->u.declarator;
230         return decl;
231 }
232
233 static struct cdecl_declarator *nulldecl(void)
234 {
235         static const struct cdecl_declarator nulldecl = {0};
236         return (void *)&nulldecl;
237 }
238 #define NULLDECL (nulldecl())
239
240 %}
241
242 %destructor { free($$); }            <item>
243 %destructor { free_declspec($$); }   <declspec>
244 %destructor { free_declarator($$); } <declarator>
245 %destructor { free_decl($$); }       <decl>
246
247 /* Magic tokens */
248 %token T_LEX_ERROR
249
250 %token <item>    T_IDENT "identifier"
251 %token <uintval> T_UINT  "integer constant"
252
253 %token T_SEMICOLON ";"
254 %token T_ASTERISK  "*"
255 %token T_LPAREN    "("
256 %token T_RPAREN    ")"
257 %token T_LBRACKET  "["
258 %token T_RBRACKET  "]"
259 %token T_COMMA     ","
260 %token T_ELLIPSIS  "..."
261
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"
267
268 %token <spectype> T_INLINE    "inline"
269
270 %token <spectype> T_RESTRICT  "restrict"
271 %token <spectype> T_VOLATILE  "volatile"
272 %token <spectype> T_CONST     "const"
273
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"
286
287 %token <spectype> T_STRUCT    "struct"
288 %token <spectype> T_UNION     "union"
289 %token <spectype> T_ENUM      "enum"
290
291 /*
292  * English keywords.
293  */
294 %token T_TYPE      "type"
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"
300 %token T_TO        "to"
301 %token T_OF        "of"
302 %token T_AS        "as"
303 %token T_VLA       "variable-length"
304
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
316
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
323
324 /* Precedence declaration to avoid conflict in english_parameter; see below. */
325 %right T_TYPE
326 %right T_IDENT
327
328 %%
329
330 input: cdecl { *out = $1; }
331 cdecl: english | declaration
332
333 semi: | T_SEMICOLON
334 declaration: declspecs declarators semi {
335         $$ = $2;
336         $$->specifiers = $1;
337 };
338
339 /*
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:
343  *
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.
346  *
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).
350  *
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.
354  */
355 declspecs: declspec_notype declspecs {
356         $$ = $1;
357         $$->next = $2;
358 } | typespec declspecs_noid {
359         $$ = $1;
360         $$->next = $2;
361 }
362
363 declspecs_noid: { $$ = NULL; } | declspec_noid declspecs_noid {
364         $$ = $1;
365         $$->next = $2;
366 }
367
368 qualifiers: { $$ = NULL; } | qualifiers qualifier {
369         $$ = $2;
370         $$->next = $1;
371 }
372
373 declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
374         $$ = $1;
375         $$->next = $3;
376 }
377
378 declarator_wrap: declarator {
379         ALLOC_DECL($$, NULL, $1);
380 }
381
382 declspec_simple: T_AUTO
383         | T_TYPEDEF
384         | T_EXTERN
385         | T_STATIC
386         | T_REGISTER
387         | T_INLINE
388
389 typespec_simple: T_VOID
390         | T_CHAR
391         | T_SHORT
392         | T_INT
393         | T_LONG
394         | T_FLOAT
395         | T_DOUBLE
396         | T_SIGNED
397         | T_UNSIGNED
398         | T_BOOL
399         | T_COMPLEX
400         | T_IMAGINARY
401
402 typespec_tagged: T_STRUCT | T_UNION | T_ENUM | { $$ = CDECL_TYPE_IDENT; }
403
404 qualifier_simple: T_CONST
405         | T_RESTRICT
406         | T_VOLATILE
407
408 declspec_notype: qualifier | declspec_simple { ALLOC_DECLSPEC($$, $1); }
409 typespec_noid: typespec_simple { ALLOC_DECLSPEC($$, $1); }
410 qualifier: qualifier_simple { ALLOC_DECLSPEC($$, $1); }
411
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;
415
416         $$ = &$2->u.declspec;
417         $$->type = $1;
418 }
419
420 declspec_noid: declspec_notype | typespec_noid
421
422 vla_ident: T_IDENT | T_ASTERISK {
423         if (!($$ = cdecl__alloc_item(1)))
424                 YYERROR;
425         *$$->s = 0;
426 }
427
428 array: T_LBRACKET array_length T_RBRACKET {
429         ALLOC_ARRAY($$, $2);
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;
435 }
436
437 parameter: declspecs declarator {
438         ALLOC_DECL($$, $1, $2);
439 }
440
441 varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
442
443 parameter_type_list: parameter varargs {
444         ALLOC_FUNCTION($$, $1, $2);
445 } | parameter T_COMMA parameter_type_list {
446         $$ = $3;
447         $1->next = $$->u.function.parameters;
448         $$->u.function.parameters = $1;
449 }
450
451 parens: T_LPAREN parameter_type_list T_RPAREN {
452         $$ = $2;
453 } | T_LPAREN declarator_ish T_RPAREN {
454         struct cdecl *fake_params;
455
456         ALLOC_DECL(fake_params, NULL, $2);
457         ALLOC_FUNCTION($$, fake_params, false);
458 }
459
460 pointer: T_ASTERISK qualifiers direct_declarator {
461         ALLOC_POINTER($$, $2, $3);
462 } | T_ASTERISK qualifiers pointer {
463         ALLOC_POINTER($$, $2, $3);
464 }
465
466 declarator: direct_declarator | pointer
467 declarator_ish: direct_declarator_ish | pointer
468 postfix: array | parens
469
470 direct_declarator_ish: {
471         $$ = NULLDECL;
472 } | direct_declarator_ish postfix {
473         $$ = $2;
474         $$->child = $1;
475 }
476
477 direct_declarator: {
478         $$ = NULLDECL;
479 } | T_IDENT {
480         $$ = &$1->u.declarator;
481 } | direct_declarator postfix {
482         $$ = $2;
483         $$->child = $1;
484 }
485
486 english: T_DECLARE T_IDENT T_AS english_declaration {
487         $$ = insert_identifier($4, $2);
488 } | T_TYPE english_declaration {
489         $$ = $2;
490 }
491
492 /*
493  * We use a precedence declaration to prefer shifting an identifier
494  * over reducing this empty rule; see below.
495  */
496 storage_func_specs: %prec T_TYPE { $$ = NULL; }
497 storage_func_specs: declspec_simple storage_func_specs {
498         ALLOC_DECLSPEC($$, $1);
499         $$->next = $2;
500 }
501
502 type_qual_spec: typespec_noid | qualifier
503
504 type_qual_specs: { $$ = NULL; } | type_qual_spec type_qual_specs {
505         $$ = $1;
506         $$->next = $2;
507 }
508
509 /*
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.
513  */
514 post_specs: qualifiers typespec type_qual_specs {
515         $2->next = $3;
516         join_specs($2, $1);
517         $$ = $2;
518 }
519
520 english_declaration: storage_func_specs english_declarator post_specs {
521         join_specs($3, $1);
522         ALLOC_DECL($$, $3, $2);
523 }
524
525 english_declarator: {
526         $$ = NULLDECL;
527 } | english_declarator qualifiers T_POINTER T_TO {
528         ALLOC_POINTER($$, $2, $1);
529 } | english_declarator english_array {
530         $$ = $2;
531         $$->child = $1;
532 } | english_declarator english_function {
533         $$ = $2;
534         $$->child = $1;
535 }
536
537 english_function: T_FUNCTION T_RETURNING {
538         ALLOC_FUNCTION($$, NULL, false);
539 } | T_FUNCTION T_LPAREN english_parameter_list T_RPAREN T_RETURNING {
540         $$ = $3;
541 }
542
543 english_parameter_list: english_parameter varargs {
544         ALLOC_FUNCTION($$, $1, $2);
545 } | english_parameter T_COMMA english_parameter_list {
546         $$ = $3;
547         $1->next = $$->u.function.parameters;
548         $$->u.function.parameters = $1;
549 }
550
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;
554
555         $$ = &$1->u.declspec;
556         $$->type = CDECL_TYPE_IDENT;
557         $$->next = $2;
558 }
559
560 null_decl: {
561         $$ = NULLDECL;
562 }
563
564 /*
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).
568  *
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.
573  *
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.
577  */
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);
582 }
583
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 {
590         ALLOC_ARRAY($$, $2);
591 }
592
593 array_length: { $$ = 0; }
594 array_length: T_UINT {
595         if (!($$ = $1))
596                 FAIL(_("array length must be positive"));
597 }
598
599 english_vla: T_IDENT | {
600         if (!($$ = cdecl__alloc_item(1)))
601                 YYERROR;
602         *$$->s = 0;
603 }
604
605 %%
606
607 /*
608  * Expose the token string table to the rest of the library, in order to
609  * produce strings that match parser keywords.
610  *
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
613  * strings.
614  */
615 const char *cdecl__token_name(unsigned token)
616 {
617         return yytname[YYTRANSLATE(token)];
618 }
619
620 static void
621 yyerror(YYLTYPE *loc, yyscan_t scanner, struct cdecl **out, const char *err)
622 {
623         if (strstr(err, yytname[YYTRANSLATE(T_LEX_ERROR)]))
624                 return;
625
626         cdecl__err(CDECL_ENOPARSE, "%s", err);
627 }