]> git.draconx.ca Git - cdecl99.git/blob - src/parse-decl.c
Fix parsing of int (x*).
[cdecl99.git] / src / parse-decl.c
1 /*
2  *  Parse and validate C declarations.
3  *  Copyright © 2011-2012, 2020 Nick Bowler
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #include <config.h>
19 #include <stdio.h>
20 #include <assert.h>
21 #include <stdbool.h>
22
23 #include "cdecl.h"
24 #include "typemap.h"
25 #include "parse.h"
26 #include "scan.h"
27 #include "i18n.h"
28 #include "normalize.h"
29
30 /*
31  * Verify the declaration specifiers of a declaration.  If top is true, treat
32  * this as a top-level declaration.  Otherwise, treat this as a function
33  * parameter (which carries additional constraints).
34  */
35 static bool valid_declspecs(struct cdecl *decl, bool top)
36 {
37         struct cdecl_declspec *specs = decl->specifiers;
38         struct cdecl_declarator *d   = decl->declarators;
39         bool abstract = cdecl_is_abstract(d);
40         unsigned num_storage = 0;
41         unsigned long typemap;
42
43         typemap = cdecl__build_typemap(specs);
44         if (typemap == -1)
45                 return false;
46
47         for (struct cdecl_declspec *c = specs; c; c = c->next) {
48                 switch (cdecl_spec_kind(c)) {
49                 case CDECL_SPEC_TYPE:
50                         if (c->type == CDECL_TYPE_VOID &&
51                             (d->type == CDECL_DECL_IDENT
52                              || d->type == CDECL_DECL_ARRAY)) {
53                                 fprintf(stderr, "invalid declaration of type void\n");
54                                 return false;
55                         }
56                         continue;
57                 case CDECL_SPEC_STOR:
58                         if (top && abstract) {
59                                 fprintf(stderr, "type names cannot have storage-class specifiers\n");
60                                 return false;
61                         }
62
63                         if (!top && c->type != CDECL_STOR_REGISTER) {
64                                 fprintf(stderr, "function parameters may only have register storage\n");
65                                 return false;
66                         }
67
68                         if (++num_storage > 1) {
69                                 fprintf(stderr, "too many storage-class specifiers\n");
70                                 return false;
71                         }
72                         break;
73                 case CDECL_SPEC_QUAL:
74                         /*
75                          * Restrict qualifiers are only valid in the
76                          * pointer qualifier list, which isn't checked here.
77                          */
78                         if (c->type == CDECL_QUAL_RESTRICT) {
79                                 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
80                                 return false;
81                         }
82                         break;
83                 case CDECL_SPEC_FUNC:
84                         if (abstract) {
85                                 fprintf(stderr, "type names cannot have function specifiers\n");
86                                 return false;
87                         }
88
89                         if (!top || d->type != CDECL_DECL_FUNCTION) {
90                                 fprintf(stderr, "only function declarations may have function specifiers.\n");
91                                 return false;
92                         }
93                         break;
94                 default:
95                         assert(0);
96                 }
97         }
98
99         return true;
100 }
101
102 /*
103  * The C grammar leaves ambiguous some cases where parentheses represent a
104  * function declarator or just parentheses.  The language uses additional
105  * context (whether or not a typedef is in scope, etc.) to resolve these
106  * ambiguities, but we don't have access to that kind of information.
107  *
108  * The cdecl99 parser uses an unambiguous grammar which treats almost
109  * everything as a function, and thus considers things like 'int (x)' to
110  * be a function type with a single parameter of type 'x' (a typedef name),
111  * returning int.  This can result in very complicated types for simple
112  * declarations.  Ideally, cdecl99 should try and find the "simplest"
113  * explanation for a given declaration.
114  *
115  * Whether or not it achieves the simplest explanation, we apply a simple rule:
116  * if a declarator could be interpreted as something other than a function,
117  * do that.
118  *
119  * Since cdecl99 supports things like [*] in any context (in C, such constructs
120  * are only valid in function parameter lists), we don't treat them specially
121  * here.
122  */
123
124 static struct cdecl_declarator *reduce_function(struct cdecl *param)
125 {
126         struct cdecl_declspec   *spec = param->specifiers;
127         struct cdecl_declarator *decl = param->declarators;
128         struct cdecl_declarator *last;
129
130         for (last = decl; last && last->type != CDECL_DECL_NULL;)
131                 last = last->child;
132
133         if (!last)
134                 return NULL;
135
136         last->type = CDECL_DECL_IDENT;
137         last->u.ident = spec->ident;
138         free(param);
139         free(spec);
140
141         return decl;
142 }
143
144 static bool function_is_reducible(struct cdecl_declarator *d)
145 {
146         if (d->type != CDECL_DECL_FUNCTION)
147                 return false;
148         if (d->child->type != CDECL_DECL_NULL)
149                 return false; /* e.g., int (*)(x) */
150
151         if (!d->u.function.parameters)
152                 return false; /* e.g., int f() */
153         if (d->u.function.parameters->next)
154                 return false; /* e.g., int (x, y) */
155         if (d->u.function.variadic)
156                 return false; /* e.g., int (x, ...) */
157
158         if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
159                 return false; /* e.g. int (int) */
160         if (d->u.function.parameters->specifiers->next)
161                 return false; /* e.g. int (size_t const) */
162         if (d->u.function.parameters->declarators->type == CDECL_DECL_POINTER)
163                 return false; /* e.g. int (x *) */
164
165         return true;
166 }
167
168 static int
169 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
170 {
171         struct cdecl_declarator *new;
172
173         if (!function_is_reducible(d))
174                 return 0;
175
176         new = reduce_function(d->u.function.parameters);
177         if (!new)
178                 return 0; /* e.g. int (foo bar) */
179         *p = new;
180         free(d->child);
181         free(d);
182
183         return 1;
184 }
185
186 /*
187  * The parser's bias towards considering things as functions whenever possible
188  * makes nested parentheses tricky.  (x) is considered to be part of a function
189  * declarator until simplify_functions converts it.  The problem is that
190  * (((x))) is not valid as part of a function declarator, but it *is* valid
191  * as an identifier enclosed 3 times in parentheses.  This is complicated by
192  * the fact that things like (((int))) are not valid anywhere.
193  *
194  * To avoid ambiguities, the parser actually emits a "function" declarator for
195  * every pair of parentheses.  The ones that can't reasonably be functions
196  * consist of a single "parameter" with no declaration specifiers (note that
197  * every valid function parameter will have at least one type specifier).
198  *
199  * This pass is to remove these fake functions from the parse tree.  We take
200  * care to avoid turning invalid things like ((int)) into valid things like
201  * (int) by observing that the only valid function declarators that appear
202  * in these "fake" parentheses are those that have a non-null child declarator
203  * (for instance, int ((*)(int)) *or* those that will be eliminated by the
204  * simplify_functions pass.
205  */
206
207 static int
208 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
209 {
210         struct cdecl *param;
211
212         if (d->type != CDECL_DECL_FUNCTION)
213                 return 0;
214
215         param = d->u.function.parameters;
216         if (param && param->specifiers == NULL) {
217                 struct cdecl_declarator *decl;
218
219                 assert(!param->next);
220
221                 decl = param->declarators;
222                 if (decl->type == CDECL_DECL_NULL) {
223                         free(decl);
224                         free(param);
225                         d->u.function.parameters = NULL;
226                         return 0;
227                 }
228
229                 if (d->child->type != CDECL_DECL_NULL) {
230                         fprintf(stderr, "invalid function parameter\n");
231                         return -1;
232                 }
233
234                 free(d->child);
235                 free(param);
236                 free(d);
237                 *p = decl;
238
239                 /*
240                  * We may have replaced d with another fake function which
241                  * also needs to be eliminated.
242                  */
243                 if (reduce_parentheses(p, decl) < 0)
244                         return -1;
245
246                 /*
247                  * If the remaining declarator is a function, make sure it's
248                  * valid by checking its reducibility.
249                  */
250                 decl = *p;
251                 if (decl->type == CDECL_DECL_FUNCTION
252                     && decl->child->type == CDECL_DECL_NULL
253                     && !function_is_reducible(decl)) {
254                         fprintf(stderr, "too many parentheses in function\n");
255                         return -1;
256                 }
257
258                 return 1;
259         }
260
261         return 0;
262 }
263
264 /*
265  * Function parameters and return types have a few restrictions that are
266  * really easy to check in comparison to the above absurdity.
267  */
268 static int
269 check_parameters(struct cdecl_declarator **p, struct cdecl_declarator *d)
270 {
271         struct cdecl_declspec *spec;
272         struct cdecl *param;
273         bool has_void = false;
274
275         if (d->type != CDECL_DECL_FUNCTION)
276                 return 0;
277
278         for (param = d->u.function.parameters; param; param = param->next) {
279                 if (!valid_declspecs(param, false))
280                         return -1;
281
282                 /* Check for "void" function parameters as a special case. */
283                 for (spec = param->specifiers; spec; spec = spec->next) {
284                         if (param->declarators->type != CDECL_DECL_NULL)
285                                 continue;
286                         if (spec->type != CDECL_TYPE_VOID)
287                                 continue;
288
289                         if (spec != param->specifiers || spec->next != NULL) {
290                                 fprintf(stderr, "void parameter must not have extra specifiers\n");
291                                 return -1;
292                         } else if (d->u.function.parameters->next) {
293                                 fprintf(stderr, "a void parameter must stand alone\n");
294                                 return -1;
295                         } else if (d->u.function.variadic) {
296                                 fprintf(stderr, "variadic functions cannot have a void parameter\n");
297                                 return -1;
298                         }
299                 }
300         }
301
302         return 0;
303 }
304
305 /*
306  * Functions cannot return arrays or functions.  Since the parse tree is
307  * "inside-out", we need to look for functions as the child declarator.
308  */
309 static int
310 check_rettypes(struct cdecl_declarator **p, struct cdecl_declarator *d)
311 {
312         if (!d->child || d->child->type != CDECL_DECL_FUNCTION)
313                 return 0;
314
315         switch (d->type) {
316         case CDECL_DECL_FUNCTION:
317                 fprintf(stderr, "functions cannot return functions\n");
318                 return -1;
319         case CDECL_DECL_ARRAY:
320                 fprintf(stderr, "functions cannot return arrays\n");
321                 return -1;
322         }
323
324         return 0;
325 }
326
327 static int
328 check_arrays(struct cdecl_declarator **p, struct cdecl_declarator *d)
329 {
330         if (!d->child || d->child->type != CDECL_DECL_ARRAY)
331                 return 0;
332
333         switch (d->type) {
334         case CDECL_DECL_FUNCTION:
335                 fprintf(stderr, "array members cannot be functions\n");
336                 return -1;
337         }
338
339         return 0;
340 }
341
342 static int
343 normalize_specs(struct cdecl_declarator **p, struct cdecl_declarator *d)
344 {
345         struct cdecl_function *func;
346         struct cdecl_pointer *ptr;
347
348         switch (d->type) {
349         case CDECL_DECL_POINTER:
350                 ptr = &d->u.pointer;
351                 ptr->qualifiers = cdecl__normalize_specs(ptr->qualifiers);
352                 break;
353         case CDECL_DECL_FUNCTION:
354                 func = &d->u.function;
355                 for (struct cdecl *i = func->parameters; i; i = i->next)
356                         i->specifiers = cdecl__normalize_specs(i->specifiers);
357                 break;
358         }
359
360         return 0;
361 }
362
363 static int
364 check_qualifiers(struct cdecl_declarator **p, struct cdecl_declarator *d)
365 {
366         struct cdecl_declspec *spec;
367         struct cdecl_pointer *ptr;
368
369         if (!d->child || d->child->type != CDECL_DECL_POINTER)
370                 return 0;
371
372         ptr = &d->child->u.pointer;
373         for (spec = ptr->qualifiers; spec; spec = spec->next) {
374                 if (spec->type == CDECL_QUAL_RESTRICT
375                     && d->type == CDECL_DECL_FUNCTION) {
376                         fprintf(stderr, "function pointers cannot be restrict-qualified\n");
377                         return -1;
378                 }
379         }
380
381         return 0;
382 }
383
384 /*
385  * Traverse the parse tree, calling a function on every declarator in a
386  * depth-first preorder traversal.  The function is given a pointer to the
387  * declarator as well as to the pointer which was used to reach that
388  * declarator: this can be used to rewrite entire subtrees.
389  */
390 static bool forall_declarators(struct cdecl *decl,
391         int f(struct cdecl_declarator **, struct cdecl_declarator *))
392 {
393         struct cdecl_declarator *d, **p;
394
395         for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
396                 switch (f(p, d)) {
397                 case 0:
398                         break;
399                 case 1:
400                         d = *p;
401                         break;
402                 case -1:
403                         return false;
404                 default:
405                         assert(0);
406                 }
407
408                 if (d->type == CDECL_DECL_FUNCTION) {
409                         struct cdecl *i;
410
411                         for (i = d->u.function.parameters; i; i = i->next) {
412                                 if (!forall_declarators(i, f))
413                                         return false;
414                         }
415                 }
416         }
417
418         return true;
419 }
420
421 struct cdecl *cdecl_parse_decl(const char *declstr)
422 {
423         struct cdecl_declspec *norm_specs;
424         YY_BUFFER_STATE state;
425         yyscan_t scanner;
426         struct cdecl *decl;
427         int rc;
428
429         cdecl__init_i18n();
430
431         rc = cdecl__yylex_init(&scanner);
432         if (rc != 0)
433                 return NULL;
434
435         state = cdecl__yy_scan_string(declstr, scanner);
436         rc = cdecl__yyparse(scanner, &decl);
437         cdecl__yy_delete_buffer(state, scanner);
438         cdecl__yylex_destroy(scanner);
439
440         if (rc != 0)
441                 return NULL;
442
443         /*
444          * Since the top-level specifiers are shared between each top-level
445          * declarator, we need to normalize them once and then propagate the
446          * new specifier list.
447          */
448         norm_specs = cdecl__normalize_specs(decl->specifiers);
449
450         for (struct cdecl *i = decl; i; i = i->next) {
451                 i->specifiers = norm_specs;
452
453                 if (!forall_declarators(i, reduce_parentheses))
454                         goto err;
455                 if (!forall_declarators(i, simplify_functions))
456                         goto err;
457                 if (!forall_declarators(i, check_parameters))
458                         goto err;
459                 if (!forall_declarators(i, check_rettypes))
460                         goto err;
461                 if (!forall_declarators(i, check_arrays))
462                         goto err;
463                 if (!forall_declarators(i, normalize_specs))
464                         goto err;
465                 if (!forall_declarators(i, check_qualifiers))
466                         goto err;
467
468                 if (!valid_declspecs(i, true))
469                         goto err;
470
471                 if (cdecl_is_abstract(i->declarators)
472                     && (i != decl || i->next)) {
473                         fprintf(stderr, "mixing type names and declarations is not allowed\n");
474                         goto err;
475                 }
476         }
477
478         return decl;
479 err:
480         cdecl__free(decl);
481         return NULL;
482 }
483
484 struct cdecl *cdecl_parse_english(const char *english)
485 {
486         YY_BUFFER_STATE state;
487         yyscan_t scanner;
488         struct cdecl *decl;
489         int rc;
490
491         cdecl__init_i18n();
492
493         rc = cdecl__yylex_init_extra(true, &scanner);
494         if (rc != 0)
495                 return NULL;
496
497         state = cdecl__yy_scan_string(english, scanner);
498         rc = cdecl__yyparse(scanner, &decl);
499         cdecl__yy_delete_buffer(state, scanner);
500         cdecl__yylex_destroy(scanner);
501
502         if (rc != 0)
503                 return NULL;
504
505         for (struct cdecl *i = decl; i; i = i->next) {
506                 i->specifiers = cdecl__normalize_specs(i->specifiers);
507
508                 if (!forall_declarators(i, check_parameters))
509                         goto err;
510                 if (!forall_declarators(i, check_rettypes))
511                         goto err;
512                 if (!forall_declarators(i, check_arrays))
513                         goto err;
514                 if (!forall_declarators(i, normalize_specs))
515                         goto err;
516                 if (!forall_declarators(i, check_qualifiers))
517                         goto err;
518
519                 if (!valid_declspecs(i, true))
520                         goto err;
521         }
522
523         return decl;
524 err:
525         cdecl__free(decl);
526         return NULL;
527 }
528
529 void cdecl_free(struct cdecl *decl)
530 {
531         cdecl__free(decl);
532 }