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 * Determine if a declarator declares an identifier (other than a function
31 static bool is_abstract(struct cdecl_declarator *d)
36 return d->type != CDECL_DECL_IDENT;
40 * Verify the declaration specifiers of a declaration. If top is true, treat
41 * this as a top-level declaration. Otherwise, treat this as a function
42 * parameter (which carries additional constraints).
44 static bool valid_declspecs(struct cdecl *decl, bool top)
46 struct cdecl_declspec *specs = decl->specifiers;
47 struct cdecl_declarator *d = decl->declarators;
48 bool abstract = is_abstract(d);
49 unsigned num_storage = 0;
50 unsigned long typemap;
52 typemap = cdecl__build_typemap(specs);
56 for (struct cdecl_declspec *c = specs; c; c = c->next) {
57 switch (cdecl_spec_kind(c)) {
59 if (c->type == CDECL_TYPE_VOID &&
60 (d->type == CDECL_DECL_IDENT
61 || d->type == CDECL_DECL_ARRAY)) {
62 fprintf(stderr, "invalid declaration of type void\n");
67 if (top && abstract) {
68 fprintf(stderr, "type names cannot have storage-class specifiers\n");
72 if (!top && c->type != CDECL_STOR_REGISTER) {
73 fprintf(stderr, "function parameters may only have register storage\n");
77 if (++num_storage > 1) {
78 fprintf(stderr, "too many storage-class specifiers\n");
84 * Restrict qualifiers are only valid in the
85 * pointer qualifier list, which isn't checked here.
87 if (c->type == CDECL_QUAL_RESTRICT) {
88 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
94 fprintf(stderr, "type names cannot have function specifiers\n");
98 if (!top || d->type != CDECL_DECL_FUNCTION) {
99 fprintf(stderr, "only function declarations may have function specifiers.\n");
112 * The C grammar leaves ambiguous some cases where parentheses represent a
113 * function declarator or just parentheses. The language uses additional
114 * context (whether or not a typedef is in scope, etc.) to resolve these
115 * ambiguities, but we don't have access to that kind of information.
117 * The cdecl99 parser uses an unambiguous grammar which treats almost
118 * everything as a function, and thus considers things like 'int (x)' to
119 * be a function type with a single parameter of type 'x' (a typedef name),
120 * returning int. This can result in very complicated types for simple
121 * declarations. Ideally, cdecl99 should try and find the "simplest"
122 * explanation for a given declaration.
124 * Whether or not it achieves the simplest explanation, we apply a simple rule:
125 * if a declarator could be interpreted as something other than a function,
128 * - The function declarator has a null child declarator.
129 * - The function declarator has exactly one parameter, and is not variadic.
130 * - The function parameter has a type specifier, and it is a typedef name.
131 * - The function parameter has no other declaration specifiers.
132 * - The function parameter does not declare an identifier.
134 * Since cdecl99 supports things like [*] in any context (in C, such constructs
135 * are only valid in function parameter lists), we don't treat them specially
139 static struct cdecl_declarator *reduce_function(struct cdecl *param)
141 struct cdecl_declspec *spec = param->specifiers;
142 struct cdecl_declarator *decl = param->declarators;
143 struct cdecl_declarator *last;
145 for (last = decl; last && last->type != CDECL_DECL_NULL;)
151 last->type = CDECL_DECL_IDENT;
152 last->u.ident = spec->ident;
159 static bool function_is_reducible(struct cdecl_declarator *d)
161 if (d->type != CDECL_DECL_FUNCTION)
163 if (d->child->type != CDECL_DECL_NULL)
164 return false; /* e.g., int (*)(x) */
166 if (!d->u.function.parameters)
167 return false; /* e.g., int f() */
168 if (d->u.function.parameters->next)
169 return false; /* e.g., int (x, y) */
170 if (d->u.function.variadic)
171 return false; /* e.g., int (x, ...) */
173 if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
174 return false; /* e.g. int (int) */
175 if (d->u.function.parameters->specifiers->next)
176 return false; /* e.g. int (size_t const) */
182 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
184 struct cdecl_declarator *new;
186 if (!function_is_reducible(d))
189 new = reduce_function(d->u.function.parameters);
191 return 0; /* e.g. int (foo bar) */
200 * The parser's bias towards considering things as functions whenever possible
201 * makes nested parentheses tricky. (x) is considered to be part of a function
202 * declarator until simplify_functions converts it. The problem is that
203 * (((x))) is not valid as part of a function declarator, but it *is* valid
204 * as an identifier enclosed 3 times in parentheses. This is complicated by
205 * the fact that things like (((int))) are not valid anywhere.
207 * To avoid ambiguities, the parser actually emits a "function" declarator for
208 * every pair of parentheses. The ones that can't reasonably be functions
209 * consist of a single "parameter" with no declaration specifiers (note that
210 * every valid function parameter will have at least one type specifier).
212 * This pass is to remove these fake functions from the parse tree. We take
213 * care to avoid turning invalid things like ((int)) into valid things like
214 * (int) by observing that the only valid function declarators that appear
215 * in these "fake" parentheses are those that have a non-null child declarator
216 * (for instance, int ((*)(int)) *or* those that will be eliminated by the
217 * simplify_functions pass.
221 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
225 if (d->type != CDECL_DECL_FUNCTION)
228 param = d->u.function.parameters;
229 if (param && param->specifiers == NULL) {
230 struct cdecl_declarator *decl;
232 assert(!param->next);
234 decl = param->declarators;
235 if (decl->type == CDECL_DECL_NULL) {
238 d->u.function.parameters = NULL;
242 if (d->child->type != CDECL_DECL_NULL) {
243 fprintf(stderr, "invalid function parameter\n");
253 * We may have replaced d with another fake function which
254 * also needs to be eliminated.
256 if (reduce_parentheses(p, decl) < 0)
260 * If the remaining declarator is a function, make sure it's
261 * valid by checking its reducibility.
264 if (decl->type == CDECL_DECL_FUNCTION
265 && decl->child->type == CDECL_DECL_NULL
266 && !function_is_reducible(decl)) {
267 fprintf(stderr, "too many parentheses in function\n");
278 * Function parameters and return types have a few restrictions that are
279 * really easy to check in comparison to the above absurdity.
282 check_parameters(struct cdecl_declarator **p, struct cdecl_declarator *d)
284 struct cdecl_declspec *spec;
286 bool has_void = false;
288 if (d->type != CDECL_DECL_FUNCTION)
291 for (param = d->u.function.parameters; param; param = param->next) {
292 if (!valid_declspecs(param, false))
295 for (spec = param->specifiers; spec; spec = spec->next) {
296 if (spec->type == CDECL_TYPE_VOID
297 && param->declarators->type == CDECL_DECL_NULL)
302 if (has_void && d->u.function.parameters->next) {
303 fprintf(stderr, "a void parameter must stand alone\n");
305 } else if (has_void && d->u.function.variadic) {
306 fprintf(stderr, "variadic functions cannot have a void parameter\n");
314 * Functions cannot return arrays or functions. Since the parse tree is
315 * "inside-out", we need to look for functions as the child declarator.
318 check_rettypes(struct cdecl_declarator **p, struct cdecl_declarator *d)
320 if (!d->child || d->child->type != CDECL_DECL_FUNCTION)
324 case CDECL_DECL_FUNCTION:
325 fprintf(stderr, "functions cannot return functions\n");
327 case CDECL_DECL_ARRAY:
328 fprintf(stderr, "functions cannot return arrays\n");
336 check_arrays(struct cdecl_declarator **p, struct cdecl_declarator *d)
338 if (!d->child || d->child->type != CDECL_DECL_ARRAY)
342 case CDECL_DECL_FUNCTION:
343 fprintf(stderr, "array members cannot be functions\n");
351 * Traverse the parse tree, calling a function on every declarator in a
352 * depth-first preorder traversal. The function is given a pointer to the
353 * declarator as well as to the pointer which was used to reach that
354 * declarator: this can be used to rewrite entire subtrees.
356 static bool forall_declarators(struct cdecl *decl,
357 int f(struct cdecl_declarator **, struct cdecl_declarator *))
359 struct cdecl_declarator *d, **p;
361 for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
374 if (d->type == CDECL_DECL_FUNCTION) {
377 for (i = d->u.function.parameters; i; i = i->next) {
378 if (!forall_declarators(i, f))
387 struct cdecl *cdecl_parse_decl(const char *declstr)
389 YY_BUFFER_STATE state;
394 rc = yylex_init(&scanner);
398 state = yy_scan_string(declstr, scanner);
399 rc = yyparse(scanner, &decl);
400 yy_delete_buffer(state, scanner);
401 yylex_destroy(scanner);
406 for (struct cdecl *i = decl; i; i = i->next) {
407 if (!forall_declarators(i, reduce_parentheses))
409 if (!forall_declarators(i, simplify_functions))
411 if (!forall_declarators(i, check_parameters))
413 if (!forall_declarators(i, check_rettypes))
415 if (!forall_declarators(i, check_arrays))
418 if (!valid_declspecs(i, true))
421 if (is_abstract(i->declarators) && (i != decl || i->next)) {
422 fprintf(stderr, "mixing type names and declarations is not allowed\n");
433 struct cdecl *cdecl_parse_english(const char *english)
435 YY_BUFFER_STATE state;
440 rc = yylex_init_extra(true, &scanner);
444 state = yy_scan_string(english, scanner);
445 rc = yyparse(scanner, &decl);
446 yy_delete_buffer(state, scanner);
447 yylex_destroy(scanner);
452 for (struct cdecl *i = decl; i; i = i->next) {
453 if (!forall_declarators(i, check_parameters))
455 if (!forall_declarators(i, check_rettypes))
457 if (!forall_declarators(i, check_arrays))
460 if (!valid_declspecs(i, true))