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/>.
27 static int verify_declspecs(struct cdecl_declspec *s)
29 unsigned num_storage = 0;
30 unsigned long typemap;
32 typemap = cdecl__build_typemap(s);
36 for (struct cdecl_declspec *c = s; c; c = c->next) {
37 switch (cdecl_spec_kind(c)) {
41 if (++num_storage > 1) {
42 fprintf(stderr, "too many storage-class specifiers\n");
48 * Restrict qualifiers are only valid in the
49 * pointer qualifier list, which isn't checked here.
51 if (c->type == CDECL_QUAL_RESTRICT) {
52 fprintf(stderr, "only pointer types can be restrict-qualified.\n");
58 * We don't support functions yet.
60 fprintf(stderr, "only function declarations may have function specifiers.\n");
71 * The C grammar leaves ambiguous some cases where parentheses represent a
72 * function declarator or just parentheses. The language uses additional
73 * context (whether or not a typedef is in scope, etc.) to resolve these
74 * ambiguities, but we don't have access to that kind of information.
76 * The cdecl99 parser uses an unambiguous grammar which treats almost
77 * everything as a function, and thus considers things like 'int (x)' to
78 * be a function type with a single parameter of type 'x' (a typedef name),
79 * returning int. This can result in very complicated types for simple
80 * declarations. Ideally, cdecl99 should try and find the "simplest"
81 * explanation for a given declaration.
83 * Whether or not it achieves the simplest explanation, we apply a simple rule:
84 * if a declarator could be interpreted as something other than a function,
87 * - The function declarator has a null child declarator.
88 * - The function declarator has exactly one parameter, and is not variadic.
89 * - The function parameter has a type specifier, and it is a typedef name.
90 * - The function parameter has no other declaration specifiers.
91 * - The function parameter does not declare an identifier.
93 * Since cdecl99 supports things like [*] in any context (in C, such constructs
94 * are only valid in function parameter lists), we don't treat them specially
98 static struct cdecl_declarator *reduce_function(struct cdecl *param)
100 struct cdecl_declspec *spec = param->specifiers;
101 struct cdecl_declarator *decl = param->declarators;
102 struct cdecl_declarator *last;
104 for (last = decl; last && last->type != CDECL_DECL_NULL;)
110 last->type = CDECL_DECL_IDENT;
111 last->u.ident = spec->ident;
118 static bool function_is_reducible(struct cdecl_declarator *d)
120 if (d->type != CDECL_DECL_FUNCTION)
122 if (d->child->type != CDECL_DECL_NULL)
123 return false; /* e.g., int (*)(x) */
125 if (!d->u.function.parameters)
126 return false; /* e.g., int f() */
127 if (d->u.function.parameters->next)
128 return false; /* e.g., int (x, y) */
129 if (d->u.function.variadic)
130 return false; /* e.g., int (x, ...) */
132 if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
133 return false; /* e.g. int (int) */
134 if (d->u.function.parameters->specifiers->next)
135 return false; /* e.g. int (size_t const) */
141 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
143 struct cdecl_declarator *new;
145 if (!function_is_reducible(d))
148 new = reduce_function(d->u.function.parameters);
150 return 0; /* e.g. int (foo bar) */
159 * The parser's bias towards considering things as functions whenever possible
160 * makes nested parentheses tricky. (x) is considered to be part of a function
161 * declarator until simplify_functions converts it. The problem is that
162 * (((x))) is not valid as part of a function declarator, but it *is* valid
163 * as an identifier enclosed 3 times in parentheses. This is complicated by
164 * the fact that things like (((int))) are not valid anywhere.
166 * To avoid ambiguities, the parser actually emits a "function" declarator for
167 * every pair of parentheses. The ones that can't reasonably be functions
168 * consist of a single "parameter" with no declaration specifiers (note that
169 * every valid function parameter will have at least one type specifier).
171 * This pass is to remove these fake functions from the parse tree. We take
172 * care to avoid turning invalid things like ((int)) into valid things like
173 * (int) by observing that the only valid function declarators that appear
174 * in these "fake" parentheses are those that have a non-null child declarator
175 * (for instance, int ((*)(int)) *or* those that will be eliminated by the
176 * simplify_functions pass.
180 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
184 if (d->type != CDECL_DECL_FUNCTION)
187 param = d->u.function.parameters;
188 if (param && param->specifiers == NULL) {
189 struct cdecl_declarator *decl;
191 assert(!param->next);
193 decl = param->declarators;
194 if (decl->type == CDECL_DECL_NULL) {
197 d->u.function.parameters = NULL;
201 if (d->child->type != CDECL_DECL_NULL) {
202 fprintf(stderr, "invalid function parameter\n");
212 * We may have replaced d with another fake function which
213 * also needs to be eliminated.
215 if (reduce_parentheses(p, decl) < 0)
219 * If the remaining declarator is a function, make sure it's
220 * valid by checking its reducibility.
223 if (decl->type == CDECL_DECL_FUNCTION
224 && decl->child->type == CDECL_DECL_NULL
225 && !function_is_reducible(decl)) {
226 fprintf(stderr, "too many parentheses in function\n");
237 * Traverse the parse tree, calling a function on every declarator in a
238 * depth-first preorder traversal. The function is given a pointer to the
239 * declarator as well as to the pointer which was used to reach that
240 * declarator: this can be used to rewrite entire subtrees.
242 static bool forall_declarators(struct cdecl *decl,
243 int f(struct cdecl_declarator **, struct cdecl_declarator *))
245 struct cdecl_declarator *d, **p;
247 for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
260 if (d->type == CDECL_DECL_FUNCTION) {
263 for (i = d->u.function.parameters; i; i = i->next) {
264 if (!forall_declarators(i, f))
273 struct cdecl *cdecl_parse_decl(const char *declstr)
275 YY_BUFFER_STATE state;
279 state = yy_scan_string(declstr);
281 yy_delete_buffer(state);
286 if (verify_declspecs(decl->specifiers) != 0)
289 for (struct cdecl *i = decl; i; i = i->next) {
290 if (!forall_declarators(i, reduce_parentheses))
292 if (!forall_declarators(i, simplify_functions))