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