2 * Parse and validate C declarations.
3 * Copyright © 2011 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 <http://www.gnu.org/licenses/>.
28 #include "normalize.h"
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).
35 static bool valid_declspecs(struct cdecl *decl, bool top)
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;
43 typemap = cdecl__build_typemap(specs);
47 for (struct cdecl_declspec *c = specs; c; c = c->next) {
48 switch (cdecl_spec_kind(c)) {
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");
58 if (top && abstract) {
59 fprintf(stderr, "type names cannot have storage-class specifiers\n");
63 if (!top && c->type != CDECL_STOR_REGISTER) {
64 fprintf(stderr, "function parameters may only have register storage\n");
68 if (++num_storage > 1) {
69 fprintf(stderr, "too many storage-class specifiers\n");
75 * Restrict qualifiers are only valid in the
76 * pointer qualifier list, which isn't checked here.
78 if (c->type == CDECL_QUAL_RESTRICT) {
79 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
85 fprintf(stderr, "type names cannot have function specifiers\n");
89 if (!top || d->type != CDECL_DECL_FUNCTION) {
90 fprintf(stderr, "only function declarations may have function specifiers.\n");
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.
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.
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,
119 * - The function declarator has a null child declarator.
120 * - The function declarator has exactly one parameter, and is not variadic.
121 * - The function parameter has a type specifier, and it is a typedef name.
122 * - The function parameter has no other declaration specifiers.
123 * - The function parameter does not declare an identifier.
125 * Since cdecl99 supports things like [*] in any context (in C, such constructs
126 * are only valid in function parameter lists), we don't treat them specially
130 static struct cdecl_declarator *reduce_function(struct cdecl *param)
132 struct cdecl_declspec *spec = param->specifiers;
133 struct cdecl_declarator *decl = param->declarators;
134 struct cdecl_declarator *last;
136 for (last = decl; last && last->type != CDECL_DECL_NULL;)
142 last->type = CDECL_DECL_IDENT;
143 last->u.ident = spec->ident;
150 static bool function_is_reducible(struct cdecl_declarator *d)
152 if (d->type != CDECL_DECL_FUNCTION)
154 if (d->child->type != CDECL_DECL_NULL)
155 return false; /* e.g., int (*)(x) */
157 if (!d->u.function.parameters)
158 return false; /* e.g., int f() */
159 if (d->u.function.parameters->next)
160 return false; /* e.g., int (x, y) */
161 if (d->u.function.variadic)
162 return false; /* e.g., int (x, ...) */
164 if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
165 return false; /* e.g. int (int) */
166 if (d->u.function.parameters->specifiers->next)
167 return false; /* e.g. int (size_t const) */
173 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
175 struct cdecl_declarator *new;
177 if (!function_is_reducible(d))
180 new = reduce_function(d->u.function.parameters);
182 return 0; /* e.g. int (foo bar) */
191 * The parser's bias towards considering things as functions whenever possible
192 * makes nested parentheses tricky. (x) is considered to be part of a function
193 * declarator until simplify_functions converts it. The problem is that
194 * (((x))) is not valid as part of a function declarator, but it *is* valid
195 * as an identifier enclosed 3 times in parentheses. This is complicated by
196 * the fact that things like (((int))) are not valid anywhere.
198 * To avoid ambiguities, the parser actually emits a "function" declarator for
199 * every pair of parentheses. The ones that can't reasonably be functions
200 * consist of a single "parameter" with no declaration specifiers (note that
201 * every valid function parameter will have at least one type specifier).
203 * This pass is to remove these fake functions from the parse tree. We take
204 * care to avoid turning invalid things like ((int)) into valid things like
205 * (int) by observing that the only valid function declarators that appear
206 * in these "fake" parentheses are those that have a non-null child declarator
207 * (for instance, int ((*)(int)) *or* those that will be eliminated by the
208 * simplify_functions pass.
212 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
216 if (d->type != CDECL_DECL_FUNCTION)
219 param = d->u.function.parameters;
220 if (param && param->specifiers == NULL) {
221 struct cdecl_declarator *decl;
223 assert(!param->next);
225 decl = param->declarators;
226 if (decl->type == CDECL_DECL_NULL) {
229 d->u.function.parameters = NULL;
233 if (d->child->type != CDECL_DECL_NULL) {
234 fprintf(stderr, "invalid function parameter\n");
244 * We may have replaced d with another fake function which
245 * also needs to be eliminated.
247 if (reduce_parentheses(p, decl) < 0)
251 * If the remaining declarator is a function, make sure it's
252 * valid by checking its reducibility.
255 if (decl->type == CDECL_DECL_FUNCTION
256 && decl->child->type == CDECL_DECL_NULL
257 && !function_is_reducible(decl)) {
258 fprintf(stderr, "too many parentheses in function\n");
269 * Function parameters and return types have a few restrictions that are
270 * really easy to check in comparison to the above absurdity.
273 check_parameters(struct cdecl_declarator **p, struct cdecl_declarator *d)
275 struct cdecl_declspec *spec;
277 bool has_void = false;
279 if (d->type != CDECL_DECL_FUNCTION)
282 for (param = d->u.function.parameters; param; param = param->next) {
283 if (!valid_declspecs(param, false))
286 for (spec = param->specifiers; spec; spec = spec->next) {
287 if (spec->type == CDECL_TYPE_VOID
288 && param->declarators->type == CDECL_DECL_NULL)
293 if (has_void && d->u.function.parameters->next) {
294 fprintf(stderr, "a void parameter must stand alone\n");
296 } else if (has_void && d->u.function.variadic) {
297 fprintf(stderr, "variadic functions cannot have a void parameter\n");
305 * Functions cannot return arrays or functions. Since the parse tree is
306 * "inside-out", we need to look for functions as the child declarator.
309 check_rettypes(struct cdecl_declarator **p, struct cdecl_declarator *d)
311 if (!d->child || d->child->type != CDECL_DECL_FUNCTION)
315 case CDECL_DECL_FUNCTION:
316 fprintf(stderr, "functions cannot return functions\n");
318 case CDECL_DECL_ARRAY:
319 fprintf(stderr, "functions cannot return arrays\n");
327 check_arrays(struct cdecl_declarator **p, struct cdecl_declarator *d)
329 if (!d->child || d->child->type != CDECL_DECL_ARRAY)
333 case CDECL_DECL_FUNCTION:
334 fprintf(stderr, "array members cannot be functions\n");
342 normalize_specs(struct cdecl_declarator **p, struct cdecl_declarator *d)
344 struct cdecl_function *func;
345 struct cdecl_pointer *ptr;
348 case CDECL_DECL_POINTER:
350 ptr->qualifiers = cdecl__normalize_specs(ptr->qualifiers);
352 case CDECL_DECL_FUNCTION:
353 func = &d->u.function;
354 for (struct cdecl *i = func->parameters; i; i = i->next)
355 i->specifiers = cdecl__normalize_specs(i->specifiers);
363 * Traverse the parse tree, calling a function on every declarator in a
364 * depth-first preorder traversal. The function is given a pointer to the
365 * declarator as well as to the pointer which was used to reach that
366 * declarator: this can be used to rewrite entire subtrees.
368 static bool forall_declarators(struct cdecl *decl,
369 int f(struct cdecl_declarator **, struct cdecl_declarator *))
371 struct cdecl_declarator *d, **p;
373 for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
386 if (d->type == CDECL_DECL_FUNCTION) {
389 for (i = d->u.function.parameters; i; i = i->next) {
390 if (!forall_declarators(i, f))
399 struct cdecl *cdecl_parse_decl(const char *declstr)
401 struct cdecl_declspec *norm_specs;
402 YY_BUFFER_STATE state;
409 rc = cdecl__yylex_init(&scanner);
413 state = cdecl__yy_scan_string(declstr, scanner);
414 rc = cdecl__yyparse(scanner, &decl);
415 cdecl__yy_delete_buffer(state, scanner);
416 cdecl__yylex_destroy(scanner);
422 * Since the top-level specifiers are shared between each top-level
423 * declarator, we need to normalize them once and then propagate the
424 * new specifier list.
426 norm_specs = cdecl__normalize_specs(decl->specifiers);
428 for (struct cdecl *i = decl; i; i = i->next) {
429 i->specifiers = norm_specs;
431 if (!forall_declarators(i, reduce_parentheses))
433 if (!forall_declarators(i, simplify_functions))
435 if (!forall_declarators(i, check_parameters))
437 if (!forall_declarators(i, check_rettypes))
439 if (!forall_declarators(i, check_arrays))
441 if (!forall_declarators(i, normalize_specs))
444 if (!valid_declspecs(i, true))
447 if (cdecl_is_abstract(i->declarators)
448 && (i != decl || i->next)) {
449 fprintf(stderr, "mixing type names and declarations is not allowed\n");
460 struct cdecl *cdecl_parse_english(const char *english)
462 YY_BUFFER_STATE state;
469 rc = cdecl__yylex_init_extra(true, &scanner);
473 state = cdecl__yy_scan_string(english, scanner);
474 rc = cdecl__yyparse(scanner, &decl);
475 cdecl__yy_delete_buffer(state, scanner);
476 cdecl__yylex_destroy(scanner);
481 for (struct cdecl *i = decl; i; i = i->next) {
482 i->specifiers = cdecl__normalize_specs(i->specifiers);
484 if (!forall_declarators(i, check_parameters))
486 if (!forall_declarators(i, check_rettypes))
488 if (!forall_declarators(i, check_arrays))
490 if (!forall_declarators(i, normalize_specs))
493 if (!valid_declspecs(i, true))
503 void cdecl_free(struct cdecl *decl)