2 * Parse and validate C declarations.
3 * Copyright © 2011-2012, 2020-2021, 2023 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/>.
25 #include "cdecl-internal.h"
31 * We can represent type specifiers as a bitmap, which gives us a finite
32 * list of acceptable bitmap values according to the C standard. However,
33 * the "long" specifier is allowed to occur more than once, but only at most
34 * 2 times. Treat it as a special case, assigning an unused bit to represent
37 #define MAP_LLONG_BIT 31
38 #define MAP_LONG_BIT (CDECL_TYPE_LONG-CDECL_SPEC_TYPE)
39 #define CDECL_TYPE_LLONG (CDECL_SPEC_TYPE+MAP_LLONG_BIT)
44 * Convert the declaration specifiers to a bitmap with each bit
45 * corresponding to one specific type specifier.
47 static int valid_typespec(struct cdecl_declspec *s)
49 unsigned long map = 0;
51 for (struct cdecl_declspec *c = s; c; c = c->next) {
54 if (cdecl_spec_kind(c) != CDECL_SPEC_TYPE)
57 bit = c->type - CDECL_SPEC_TYPE;
58 assert(bit < MAP_LLONG_BIT);
61 /* "long" special case */
62 if ((map & bit) == 1ul << MAP_LONG_BIT)
63 bit = 1ul << MAP_LLONG_BIT;
66 if (bit == 1ul << MAP_LLONG_BIT)
67 cdecl__errmsg(CDECL__ETOOLONG);
69 cdecl__errmsg(CDECL__EDUPTYPE);
75 if (typemap_is_valid(map))
79 cdecl__errmsg(CDECL__ENOTYPE);
81 cdecl__errmsg(CDECL__EBADTYPE);
87 * Verify the declaration specifiers of a declaration. If top is true, treat
88 * this as a top-level declaration. Otherwise, treat this as a function
89 * parameter (which carries additional constraints).
91 static bool valid_declspecs(struct cdecl *decl, bool top)
93 struct cdecl_declspec *specs = decl->specifiers;
94 struct cdecl_declarator *d = decl->declarators;
95 bool abstract = cdecl_is_abstract(d);
96 unsigned num_storage = 0;
98 if (!valid_typespec(specs))
101 for (struct cdecl_declspec *c = specs; c; c = c->next) {
102 switch (cdecl_spec_kind(c)) {
103 case CDECL_SPEC_TYPE:
104 if (c->type == CDECL_TYPE_VOID &&
105 (d->type == CDECL_DECL_IDENT
106 || d->type == CDECL_DECL_ARRAY)) {
107 cdecl__errmsg(CDECL__EBADVOID);
111 case CDECL_SPEC_STOR:
112 if (top && abstract) {
113 cdecl__errmsg(CDECL__ETYPESTOR);
117 if (!top && c->type != CDECL_STOR_REGISTER) {
118 cdecl__errmsg(CDECL__EFUNCSTOR);
122 if (++num_storage > 1) {
123 cdecl__errmsg(CDECL__EMANYSTOR);
127 case CDECL_SPEC_QUAL:
129 * Restrict qualifiers are only valid in the
130 * pointer qualifier list, which isn't checked here.
132 if (c->type == CDECL_QUAL_RESTRICT) {
133 cdecl__errmsg(CDECL__EBADQUAL);
137 case CDECL_SPEC_FUNC:
138 if (abstract || !top || d->type != CDECL_DECL_FUNCTION) {
139 cdecl__errmsg(CDECL__ENOTFUNC);
153 * The C grammar leaves ambiguous some cases where parentheses represent a
154 * function declarator or just parentheses. The language uses additional
155 * context (whether or not a typedef is in scope, etc.) to resolve these
156 * ambiguities, but we don't have access to that kind of information.
158 * The cdecl99 parser uses an unambiguous grammar which treats almost
159 * everything as a function, and thus considers things like 'int (x)' to
160 * be a function type with a single parameter of type 'x' (a typedef name),
161 * returning int. This can result in very complicated types for simple
162 * declarations. Ideally, cdecl99 should try and find the "simplest"
163 * explanation for a given declaration.
165 * Whether or not it achieves the simplest explanation, we apply a simple rule:
166 * if a declarator could be interpreted as something other than a function,
169 * Since cdecl99 supports things like [*] in any context (in C, such constructs
170 * are only valid in function parameter lists), we don't treat them specially
174 static struct cdecl_declarator *reduce_function(struct cdecl *param)
176 struct cdecl_declspec *spec = param->specifiers;
177 struct cdecl_declarator *decl = param->declarators;
178 struct cdecl_declarator *last;
180 for (last = decl; last && last->type != CDECL_DECL_NULL;)
186 last->type = CDECL_DECL_IDENT;
187 last->u.ident = spec->ident;
194 static bool function_is_reducible(struct cdecl_declarator *d)
196 if (d->type != CDECL_DECL_FUNCTION)
198 if (d->child->type != CDECL_DECL_NULL)
199 return false; /* e.g., int (*)(x) */
201 if (!d->u.function.parameters)
202 return false; /* e.g., int f() */
203 if (d->u.function.parameters->next)
204 return false; /* e.g., int (x, y) */
205 if (d->u.function.variadic)
206 return false; /* e.g., int (x, ...) */
208 if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
209 return false; /* e.g. int (int) */
210 if (d->u.function.parameters->specifiers->next)
211 return false; /* e.g. int (size_t const) */
212 if (d->u.function.parameters->declarators->type == CDECL_DECL_POINTER)
213 return false; /* e.g. int (x *) */
219 simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
221 struct cdecl_declarator *new;
223 if (!function_is_reducible(d))
226 new = reduce_function(d->u.function.parameters);
228 return 0; /* e.g. int (foo bar) */
237 * The parser's bias towards considering things as functions whenever possible
238 * makes nested parentheses tricky. (x) is considered to be part of a function
239 * declarator until simplify_functions converts it. The problem is that
240 * (((x))) is not valid as part of a function declarator, but it *is* valid
241 * as an identifier enclosed 3 times in parentheses. This is complicated by
242 * the fact that things like (((int))) are not valid anywhere.
244 * To avoid ambiguities, the parser actually emits a "function" declarator for
245 * every pair of parentheses. The ones that can't reasonably be functions
246 * consist of a single "parameter" with no declaration specifiers (note that
247 * every valid function parameter will have at least one type specifier).
249 * This pass is to remove these fake functions from the parse tree. We take
250 * care to avoid turning invalid things like ((int)) into valid things like
251 * (int) by observing that the only valid function declarators that appear
252 * in these "fake" parentheses are those that have a non-null child declarator
253 * (for instance, int ((*)(int)) *or* those that will be eliminated by the
254 * simplify_functions pass.
257 static struct cdecl *fake_function_param(struct cdecl_declarator *d)
261 if (d->type != CDECL_DECL_FUNCTION)
264 param = d->u.function.parameters;
265 if (!param || param->specifiers)
268 assert(!param->next);
273 reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
278 while ((param = fake_function_param(d))) {
279 struct cdecl_declarator *decl = param->declarators;
280 d->u.function.parameters = NULL;
282 if (decl->type != CDECL_DECL_NULL) {
283 if (d->child->type != CDECL_DECL_NULL) {
284 /* Found fake parameter on real function. */
285 d->u.function.parameters = param;
286 cdecl__errmsg(CDECL__EBADPARAM);
290 param->declarators = d;
298 simplify_functions(p, d);
299 if (fake && (*p)->type == CDECL_DECL_FUNCTION) {
300 /* Started with a fake function but ended with a real one. */
301 cdecl__errmsg(CDECL__EMANYPAREN);
309 * Returns nonzero iff the given specifier list contains a specifier
310 * of the indicated type.
312 static int have_specifier(struct cdecl_declspec *s, unsigned type)
314 for (; s; s = s->next)
321 * Check syntax restrictions on a function declarator's child declarator.
322 * That is, "pointer to function", "array of function" and "function
323 * returning function".
325 * Returns -1 if the declaration is invalid, or 0 otherwise.
327 static int check_function_child(struct cdecl_declarator *d)
329 struct cdecl_pointer *ptr;
332 case CDECL_DECL_POINTER:
334 if (have_specifier(ptr->qualifiers, CDECL_QUAL_RESTRICT)) {
335 /* pointer to function cannot be restrict qualified. */
336 cdecl__errmsg(CDECL__ERESTRICTFUNC);
340 case CDECL_DECL_FUNCTION:
341 /* function returning function is never allowed. */
342 cdecl__errmsg(CDECL__ERETFUNC);
344 case CDECL_DECL_ARRAY:
345 /* array of function is never allowed. */
346 cdecl__errmsg(CDECL__EFUNCARRAY);
354 * Check a function parameter declaration for validity, which means it has a
355 * valid combination of declaration specifiers and, if it is a void parameter,
356 * that it is the one special case where this is allowed.
358 * Returns -1 if the declaration is invalid, or 0 otherwise.
360 static int check_function_param(struct cdecl_function *f, struct cdecl *param)
362 if (!valid_declspecs(param, false))
365 /* Check for "void" function parameters as a special case. */
366 if (param->declarators->type == CDECL_DECL_NULL
367 && have_specifier(param->specifiers, CDECL_TYPE_VOID))
369 struct cdecl *fp = f->parameters;
371 if (f->variadic || fp->next || fp->specifiers->next) {
372 cdecl__errmsg(CDECL__EVOIDPARAM);
381 * Normalize the specifier lists for function parameters, and then check the
382 * function declarator for validity.
384 * Returns -1 if the declaration is invalid, or 0 otherwise.
386 static int postproc_function(struct cdecl_declarator *d)
388 struct cdecl_function *func = &d->u.function;
392 for (param = func->parameters; param; param = param->next) {
393 param->specifiers = cdecl__normalize_specs(param->specifiers);
395 if ((rc = check_function_param(func, param)) < 0)
399 return check_function_child(d->child);
403 postproc_common(struct cdecl_declarator **p, struct cdecl_declarator *d)
405 struct cdecl_pointer *ptr;
408 case CDECL_DECL_POINTER:
410 ptr->qualifiers = cdecl__normalize_specs(ptr->qualifiers);
412 case CDECL_DECL_FUNCTION:
413 return postproc_function(d);
414 case CDECL_DECL_ARRAY:
415 if (d->child && d->child->type == CDECL_DECL_FUNCTION) {
416 /* function returning array is never allowed. */
417 cdecl__errmsg(CDECL__ERETARRAY);
427 * Traverse the parse tree, calling a function on every declarator in a
428 * depth-first preorder traversal. The function is given a pointer to the
429 * declarator as well as to the pointer which was used to reach that
430 * declarator: this can be used to rewrite entire subtrees.
432 * The called function may return a negative value to indicate an error
433 * which terminates traversal.
435 * Returns 0 on success, or a negative value on failure.
437 static int forall_declarators(struct cdecl *decl,
438 int f(struct cdecl_declarator **, struct cdecl_declarator *))
440 struct cdecl_declarator *d, **p;
442 for (p = &decl->declarators; *p; p = &d->child) {
450 if (d->type == CDECL_DECL_FUNCTION) {
453 for (i = d->u.function.parameters; i; i = i->next) {
454 rc = forall_declarators(i, f);
464 static struct cdecl *do_parse(const char *str, int english_mode)
466 YY_BUFFER_STATE state;
471 extern int cdecl__yydebug;
476 if (cdecl__yylex_init_extra(english_mode, &scanner) != 0)
479 state = cdecl__yy_scan_string(str, scanner);
480 if (cdecl__yyparse(scanner, &decl) != 0)
482 cdecl__yy_delete_buffer(state, scanner);
483 cdecl__yylex_destroy(scanner);
488 static int do_postprocess(struct cdecl *decl, int english_mode)
490 struct cdecl_declspec *norm_specs;
494 * For a C declaration with more than one full declarator, the
495 * specifier list is common to all of them. Normalize it once,
496 * then propagate that to all the linked cdecl structures.
498 * In english mode, the cdecl structure list always has exactly
499 * one entry so we don't need to do anything differently.
501 norm_specs = cdecl__normalize_specs(decl->specifiers);
502 for (i = decl; i; i = i->next)
503 i->specifiers = norm_specs;
505 for (i = decl; i; i = i->next) {
507 if (forall_declarators(i, reduce_parentheses) < 0)
511 if (forall_declarators(i, postproc_common) < 0)
514 if (!valid_declspecs(i, true))
517 if (decl->next && cdecl_is_abstract(i->declarators)) {
518 /* Abstract full declarators: there can only be one. */
519 cdecl__errmsg(CDECL__EDECLTYPE);
527 static struct cdecl *parse_common(const char *str, int english_mode)
531 if (!(decl = do_parse(str, english_mode)))
534 if (!do_postprocess(decl, english_mode)) {
542 struct cdecl *cdecl_parse_decl(const char *declstr)
544 return parse_common(declstr, false);
547 struct cdecl *cdecl_parse_english(const char *english)
549 return parse_common(english, true);
552 void cdecl_free(struct cdecl *decl)