+ return true;
+}
+
+/*
+ * The C grammar leaves ambiguous some cases where parentheses represent a
+ * function declarator or just parentheses. The language uses additional
+ * context (whether or not a typedef is in scope, etc.) to resolve these
+ * ambiguities, but we don't have access to that kind of information.
+ *
+ * The cdecl99 parser uses an unambiguous grammar which treats almost
+ * everything as a function, and thus considers things like 'int (x)' to
+ * be a function type with a single parameter of type 'x' (a typedef name),
+ * returning int. This can result in very complicated types for simple
+ * declarations. Ideally, cdecl99 should try and find the "simplest"
+ * explanation for a given declaration.
+ *
+ * Whether or not it achieves the simplest explanation, we apply a simple rule:
+ * if a declarator could be interpreted as something other than a function,
+ * do that.
+ *
+ * Since cdecl99 supports things like [*] in any context (in C, such constructs
+ * are only valid in function parameter lists), we don't treat them specially
+ * here.
+ */
+
+static struct cdecl_declarator *reduce_function(struct cdecl *param)
+{
+ struct cdecl_declarator *d, **p = ¶m->declarators;
+ struct parse_item *spec = (void *)param->specifiers;
+
+ while ((d = *p)->child)
+ p = &d->child;
+
+ if (d->type != CDECL_DECL_NULL)
+ return NULL;
+
+ /*
+ * The child and u.ident members of cdecl_declarator are expected
+ * to be located at identical offsets as, respectively, the next
+ * and ident members within cdecl_declspec, so the expectation is
+ * that the compiler can elide both assignments.
+ */
+ spec->u.declarator.child = (void *)spec->u.declspec.next;
+ spec->u.declarator.u.ident = spec->u.declspec.ident;
+ spec->u.declarator.type = CDECL_DECL_IDENT;
+ *p = &spec->u.declarator;
+
+ d = param->declarators;
+ free(param);
+ return d;
+}
+
+static bool function_is_reducible(struct cdecl_declarator *d)
+{
+ if (d->type != CDECL_DECL_FUNCTION)
+ return false;
+ if (d->child->type != CDECL_DECL_NULL)
+ return false; /* e.g., int (*)(x) */
+
+ if (!d->u.function.parameters)
+ return false; /* e.g., int f() */
+ if (d->u.function.parameters->next)
+ return false; /* e.g., int (x, y) */
+ if (d->u.function.variadic)
+ return false; /* e.g., int (x, ...) */
+
+ if (d->u.function.parameters->specifiers->type != CDECL_TYPE_IDENT)
+ return false; /* e.g. int (int) */
+ if (d->u.function.parameters->specifiers->next)
+ return false; /* e.g. int (size_t const) */
+ if (d->u.function.parameters->declarators->type == CDECL_DECL_POINTER)
+ return false; /* e.g. int (x *) */
+
+ return true;
+}
+
+static int
+simplify_functions(struct cdecl_declarator **p, struct cdecl_declarator *d)
+{
+ struct cdecl_declarator *new;
+
+ if (!function_is_reducible(d))
+ return 0;
+
+ new = reduce_function(d->u.function.parameters);
+ if (!new)
+ return 0; /* e.g. int (foo bar) */
+ *p = new;
+ free(d);
+
+ return 1;
+}
+
+/*
+ * The main parser's bias towards considering things as functions whenever
+ * possible makes nested parentheses tricky. "(x)" is considered to be part
+ * of a function declarator until simplify_functions converts it. The problem
+ * is that "(((x)))" is not valid as part of a function declarator, but it _is_
+ * valid as either an identifier enclosed thrice in parentheses, or an abstract
+ * function declarator enclosed twice in parentheses.
+ *
+ * To avoid ambiguities, the main parser actually returns a function declarator
+ * for every pair of parentheses. The ones we need to look at consist of a
+ * single parameter with an empty specifier list (noting that every real
+ * function parameter will have at least one type specifier).
+ *
+ * There are two cases:
+ *
+ * - For (), the parser emits a parameter with a lone null declarator.
+ * This fake parameter simply gets deleted, leaving us with a normal
+ * function declarator with an empty identifier list.
+ *
+ * - Otherwise, the parameter's outermost declarator is not null. The
+ * function itself is deleted, replaced in the parse tree with the
+ * fake parameter's declarator.
+ *
+ * Repeating until there no fake parameters, this reduction transforms, for
+ * example, "(((x)))" into "(x)", an abstract function declarator. The result
+ * is then subject to the function simplification step, which will turn "(x)"
+ * into x (declaring an identifier).
+ *
+ * The whole process is repeated until no more changes are made to the parse
+ * tree, or a syntax error is detected.
+ */
+static struct cdecl *fake_function_param(struct cdecl_declarator *d)
+{
+ struct cdecl *param;
+
+ if (d->type != CDECL_DECL_FUNCTION)
+ return NULL;
+
+ param = d->u.function.parameters;
+ if (!param || param->specifiers)
+ return NULL;
+
+ assert(!param->next);
+ return param;
+}
+
+static int
+reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
+{
+ struct cdecl *param;
+
+ do {
+ d = *p;
+ while ((param = fake_function_param(d))) {
+ struct cdecl_declarator *decl = param->declarators;
+ d->u.function.parameters = NULL;
+
+ if (decl->type != CDECL_DECL_NULL) {
+ if (d->child->type != CDECL_DECL_NULL) {
+ /* Fake parameter on real function. */
+ d->u.function.parameters = param;
+ cdecl__errmsg(CDECL__EBADPARAM);
+ return -1;
+ }
+
+ param->declarators = d;
+ *p = d = decl;
+ }
+
+ cdecl__free(param);
+ }
+ } while (simplify_functions(p, d));
+