2 * Parse and validate C declarations.
3 * Copyright © 2011-2012, 2020 Nick Bowler
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
29 #include "normalize.h"
32 * Verify the declaration specifiers of a declaration. If top is true, treat
33 * this as a top-level declaration. Otherwise, treat this as a function
34 * parameter (which carries additional constraints).
36 static bool valid_declspecs(struct cdecl *decl, bool top)
38 struct cdecl_declspec *specs = decl->specifiers;
39 struct cdecl_declarator *d = decl->declarators;
40 bool abstract = cdecl_is_abstract(d);
41 unsigned num_storage = 0;
42 unsigned long typemap;
44 typemap = cdecl__build_typemap(specs);
48 for (struct cdecl_declspec *c = specs; c; c = c->next) {
49 switch (cdecl_spec_kind(c)) {
51 if (c->type == CDECL_TYPE_VOID &&
52 (d->type == CDECL_DECL_IDENT
53 || d->type == CDECL_DECL_ARRAY)) {
54 fprintf(stderr, "invalid declaration of type void\n");
59 if (top && abstract) {
60 fprintf(stderr, "type names cannot have storage-class specifiers\n");
64 if (!top && c->type != CDECL_STOR_REGISTER) {
65 fprintf(stderr, "function parameters may only have register storage\n");
69 if (++num_storage > 1) {
70 fprintf(stderr, "too many storage-class specifiers\n");
76 * Restrict qualifiers are only valid in the
77 * pointer qualifier list, which isn't checked here.
79 if (c->type == CDECL_QUAL_RESTRICT) {
80 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
86 fprintf(stderr, "type names cannot have function specifiers\n");
90 if (!top || d->type != CDECL_DECL_FUNCTION) {
91 fprintf(stderr, "only function declarations may have function specifiers.\n");
104 * The C grammar leaves ambiguous some cases where parentheses represent a
105 * function declarator or just parentheses. The language uses additional
106 * context (whether or not a typedef is in scope, etc.) to resolve these
107 * ambiguities, but we don't have access to that kind of information.
109 * The cdecl99 parser uses an unambiguous grammar which treats almost
110 * everything as a function, and thus considers things like 'int (x)' to
111 * be a function type with a single parameter of type 'x' (a typedef name),
112 * returning int. This can result in very complicated types for simple
113 * declarations. Ideally, cdecl99 should try and find the "simplest"
114 * explanation for a given declaration.
116 * Whether or not it achieves the simplest explanation, we apply a simple rule:
117 * if a declarator could be interpreted as something other than a function,
120 * Since cdecl99 supports things like [*] in any context (in C, such constructs
121 * are only valid in function parameter lists), we don't treat them specially
125 static struct cdecl_declarator *reduce_function(struct cdecl *param)
127 struct cdecl_declspec *spec = param->specifiers;
128 struct cdecl_declarator *decl = param->declarators;
129 struct cdecl_declarator *last;
131 for (last = decl; last && last->type != CDECL_DECL_NULL;)
137 last->type = CDECL_DECL_IDENT;
138 last->u.ident = spec->ident;
145 static bool function_is_reducible(struct cdecl_declarator *d)
147 if (d->type != CDECL_DECL_FUNCTION)
149 if (d->child->type != CDECL_DECL_NULL)
150 return false; /* e.g., int (*)(x) */
152 if (!d->u.function.parameters)
153 return false; /* e.g., int f() */
154 if (d->u.function.parameters->next)
155 return false; /* e.g., int (x, y) */
156 if (d->u.function.variadic)
157 return false; /* e.g., int (x, ...) */
159 if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
160 return false; /* e.g. int (int) */
161 if (d->u.function.parameters->specifiers->next)
162 return false; /* e.g. int (size_t const) */
163 if (d->u.function.parameters->declarators->type == CDECL_DECL_POINTER)
164 return false; /* e.g. int (x *) */
170 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
172 struct cdecl_declarator *new;
174 if (!function_is_reducible(d))
177 new = reduce_function(d->u.function.parameters);
179 return 0; /* e.g. int (foo bar) */
188 * The parser's bias towards considering things as functions whenever possible
189 * makes nested parentheses tricky. (x) is considered to be part of a function
190 * declarator until simplify_functions converts it. The problem is that
191 * (((x))) is not valid as part of a function declarator, but it *is* valid
192 * as an identifier enclosed 3 times in parentheses. This is complicated by
193 * the fact that things like (((int))) are not valid anywhere.
195 * To avoid ambiguities, the parser actually emits a "function" declarator for
196 * every pair of parentheses. The ones that can't reasonably be functions
197 * consist of a single "parameter" with no declaration specifiers (note that
198 * every valid function parameter will have at least one type specifier).
200 * This pass is to remove these fake functions from the parse tree. We take
201 * care to avoid turning invalid things like ((int)) into valid things like
202 * (int) by observing that the only valid function declarators that appear
203 * in these "fake" parentheses are those that have a non-null child declarator
204 * (for instance, int ((*)(int)) *or* those that will be eliminated by the
205 * simplify_functions pass.
209 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
213 if (d->type != CDECL_DECL_FUNCTION)
216 param = d->u.function.parameters;
217 if (param && param->specifiers == NULL) {
218 struct cdecl_declarator *decl;
220 assert(!param->next);
222 decl = param->declarators;
223 if (decl->type == CDECL_DECL_NULL) {
226 d->u.function.parameters = NULL;
230 if (d->child->type != CDECL_DECL_NULL) {
231 fprintf(stderr, "invalid function parameter\n");
241 * We may have replaced d with another fake function which
242 * also needs to be eliminated.
244 if (reduce_parentheses(p, decl) < 0)
248 * If the remaining declarator is a function, make sure it's
249 * valid by checking its reducibility.
252 if (decl->type == CDECL_DECL_FUNCTION
253 && decl->child->type == CDECL_DECL_NULL
254 && !function_is_reducible(decl)) {
255 fprintf(stderr, "too many parentheses in function\n");
266 * Function parameters and return types have a few restrictions that are
267 * really easy to check in comparison to the above absurdity.
270 check_parameters(struct cdecl_declarator **p, struct cdecl_declarator *d)
272 struct cdecl_declspec *spec;
274 bool has_void = false;
276 if (d->type != CDECL_DECL_FUNCTION)
279 for (param = d->u.function.parameters; param; param = param->next) {
280 if (!valid_declspecs(param, false))
283 /* Check for "void" function parameters as a special case. */
284 for (spec = param->specifiers; spec; spec = spec->next) {
285 if (param->declarators->type != CDECL_DECL_NULL)
287 if (spec->type != CDECL_TYPE_VOID)
290 if (spec != param->specifiers || spec->next != NULL) {
291 fprintf(stderr, "void parameter must not have extra specifiers\n");
293 } else if (d->u.function.parameters->next) {
294 fprintf(stderr, "a void parameter must stand alone\n");
296 } else if (d->u.function.variadic) {
297 fprintf(stderr, "variadic functions cannot have a void parameter\n");
307 * Functions cannot return arrays or functions. Since the parse tree is
308 * "inside-out", we need to look for functions as the child declarator.
311 check_rettypes(struct cdecl_declarator **p, struct cdecl_declarator *d)
313 if (!d->child || d->child->type != CDECL_DECL_FUNCTION)
317 case CDECL_DECL_FUNCTION:
318 fprintf(stderr, "functions cannot return functions\n");
320 case CDECL_DECL_ARRAY:
321 fprintf(stderr, "functions cannot return arrays\n");
329 check_arrays(struct cdecl_declarator **p, struct cdecl_declarator *d)
331 if (!d->child || d->child->type != CDECL_DECL_ARRAY)
335 case CDECL_DECL_FUNCTION:
336 fprintf(stderr, "array members cannot be functions\n");
344 normalize_specs(struct cdecl_declarator **p, struct cdecl_declarator *d)
346 struct cdecl_function *func;
347 struct cdecl_pointer *ptr;
350 case CDECL_DECL_POINTER:
352 ptr->qualifiers = cdecl__normalize_specs(ptr->qualifiers);
354 case CDECL_DECL_FUNCTION:
355 func = &d->u.function;
356 for (struct cdecl *i = func->parameters; i; i = i->next)
357 i->specifiers = cdecl__normalize_specs(i->specifiers);
365 check_qualifiers(struct cdecl_declarator **p, struct cdecl_declarator *d)
367 struct cdecl_declspec *spec;
368 struct cdecl_pointer *ptr;
370 if (!d->child || d->child->type != CDECL_DECL_POINTER)
373 ptr = &d->child->u.pointer;
374 for (spec = ptr->qualifiers; spec; spec = spec->next) {
375 if (spec->type == CDECL_QUAL_RESTRICT
376 && d->type == CDECL_DECL_FUNCTION) {
377 fprintf(stderr, "function pointers cannot be restrict-qualified\n");
386 * Traverse the parse tree, calling a function on every declarator in a
387 * depth-first preorder traversal. The function is given a pointer to the
388 * declarator as well as to the pointer which was used to reach that
389 * declarator: this can be used to rewrite entire subtrees.
391 static bool forall_declarators(struct cdecl *decl,
392 int f(struct cdecl_declarator **, struct cdecl_declarator *))
394 struct cdecl_declarator *d, **p;
396 for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
409 if (d->type == CDECL_DECL_FUNCTION) {
412 for (i = d->u.function.parameters; i; i = i->next) {
413 if (!forall_declarators(i, f))
422 struct cdecl *cdecl_parse_decl(const char *declstr)
424 struct cdecl_declspec *norm_specs;
425 YY_BUFFER_STATE state;
432 rc = cdecl__yylex_init(&scanner);
436 state = cdecl__yy_scan_string(declstr, scanner);
437 rc = cdecl__yyparse(scanner, &decl);
438 cdecl__yy_delete_buffer(state, scanner);
439 cdecl__yylex_destroy(scanner);
445 * Since the top-level specifiers are shared between each top-level
446 * declarator, we need to normalize them once and then propagate the
447 * new specifier list.
449 norm_specs = cdecl__normalize_specs(decl->specifiers);
450 for (struct cdecl *i = decl; i; i = i->next) {
451 i->specifiers = norm_specs;
454 /* Now perform checks and simplifications on each declarator. */
455 for (struct cdecl *i = decl; i; i = i->next) {
456 if (!forall_declarators(i, reduce_parentheses))
458 if (!forall_declarators(i, simplify_functions))
460 if (!forall_declarators(i, check_parameters))
462 if (!forall_declarators(i, check_rettypes))
464 if (!forall_declarators(i, check_arrays))
466 if (!forall_declarators(i, normalize_specs))
468 if (!forall_declarators(i, check_qualifiers))
471 if (!valid_declspecs(i, true))
474 if (cdecl_is_abstract(i->declarators)
475 && (i != decl || i->next)) {
476 fprintf(stderr, "mixing type names and declarations is not allowed\n");
487 struct cdecl *cdecl_parse_english(const char *english)
489 YY_BUFFER_STATE state;
496 rc = cdecl__yylex_init_extra(true, &scanner);
500 state = cdecl__yy_scan_string(english, scanner);
501 rc = cdecl__yyparse(scanner, &decl);
502 cdecl__yy_delete_buffer(state, scanner);
503 cdecl__yylex_destroy(scanner);
508 for (struct cdecl *i = decl; i; i = i->next) {
509 i->specifiers = cdecl__normalize_specs(i->specifiers);
511 if (!forall_declarators(i, check_parameters))
513 if (!forall_declarators(i, check_rettypes))
515 if (!forall_declarators(i, check_arrays))
517 if (!forall_declarators(i, normalize_specs))
519 if (!forall_declarators(i, check_qualifiers))
522 if (!valid_declspecs(i, true))
532 void cdecl_free(struct cdecl *decl)