+static int
+reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
+{
+ struct cdecl *param;
+ int fake = 0;
+
+ 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) {
+ /* Found fake parameter on real function. */
+ d->u.function.parameters = param;
+ cdecl__errmsg(CDECL__EBADPARAM);
+ return -1;
+ }
+
+ param->declarators = d;
+ *p = d = decl;
+ fake = 1;
+ }
+
+ cdecl__free(param);
+ }
+
+ simplify_functions(p, d);
+ if (fake && (*p)->type == CDECL_DECL_FUNCTION) {
+ /* Started with a fake function but ended with a real one. */
+ cdecl__errmsg(CDECL__EMANYPAREN);
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Returns nonzero iff the given specifier list contains a specifier
+ * of the indicated type.
+ */
+static int have_specifier(struct cdecl_declspec *s, unsigned type)
+{
+ for (; s; s = s->next)
+ if (s->type == type)
+ return 1;
+ return 0;
+}
+
+/*
+ * Check syntax restrictions on a function declarator's child declarator.
+ * That is, "pointer to function", "array of function" and "function
+ * returning function".
+ *
+ * Returns -1 if the declaration is invalid, or 0 otherwise.
+ */
+static int check_function_child(struct cdecl_declarator *d)
+{
+ struct cdecl_pointer *ptr;
+
+ switch (d->type) {
+ case CDECL_DECL_POINTER:
+ ptr = &d->u.pointer;
+ if (have_specifier(ptr->qualifiers, CDECL_QUAL_RESTRICT)) {
+ /* pointer to function cannot be restrict qualified. */
+ cdecl__errmsg(CDECL__ERESTRICTFUNC);
+ return -1;
+ }
+ return 0;
+ case CDECL_DECL_FUNCTION:
+ /* function returning function is never allowed. */
+ cdecl__errmsg(CDECL__ERETFUNC);
+ return -1;
+ case CDECL_DECL_ARRAY:
+ /* array of function is never allowed. */
+ cdecl__errmsg(CDECL__EFUNCARRAY);
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Check a function parameter declaration for validity, which means it has a
+ * valid combination of declaration specifiers and, if it is a void parameter,
+ * that it is the one special case where this is allowed.
+ *
+ * Returns -1 if the declaration is invalid, or 0 otherwise.
+ */
+static int check_function_param(struct cdecl_function *f, struct cdecl *param)
+{
+ if (!valid_declspecs(param, false))
+ return -1;
+
+ /* Check for "void" function parameters as a special case. */
+ if (param->declarators->type == CDECL_DECL_NULL
+ && have_specifier(param->specifiers, CDECL_TYPE_VOID))
+ {
+ struct cdecl *fp = f->parameters;
+
+ if (f->variadic || fp->next || fp->specifiers->next) {
+ cdecl__errmsg(CDECL__EVOIDPARAM);
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/*
+ * Normalize the specifier lists for function parameters, and then check the
+ * function declarator for validity.
+ *
+ * Returns -1 if the declaration is invalid, or 0 otherwise.
+ */
+static int postproc_function(struct cdecl_declarator *d)
+{
+ struct cdecl_function *func = &d->u.function;
+ struct cdecl *param;
+ int rc;
+
+ for (param = func->parameters; param; param = param->next) {
+ param->specifiers = cdecl__normalize_specs(param->specifiers);
+
+ if ((rc = check_function_param(func, param)) < 0)
+ return rc;
+ }
+
+ return check_function_child(d->child);
+}
+
+static int
+postproc_common(struct cdecl_declarator **p, struct cdecl_declarator *d)
+{
+ struct cdecl_pointer *ptr;
+
+ switch (d->type) {
+ case CDECL_DECL_POINTER:
+ ptr = &d->u.pointer;
+ ptr->qualifiers = cdecl__normalize_specs(ptr->qualifiers);
+ return 0;
+ case CDECL_DECL_FUNCTION:
+ return postproc_function(d);
+ case CDECL_DECL_ARRAY:
+ if (d->child && d->child->type == CDECL_DECL_FUNCTION) {
+ /* function returning array is never allowed. */
+ cdecl__errmsg(CDECL__ERETARRAY);
+ return -1;
+ }
+ return 0;
+ }
+
+ return 0;
+}
+
+/*
+ * Traverse the parse tree, calling a function on every declarator in a
+ * depth-first preorder traversal. The function is given a pointer to the
+ * declarator as well as to the pointer which was used to reach that
+ * declarator: this can be used to rewrite entire subtrees.
+ *
+ * The called function may return a negative value to indicate an error
+ * which terminates traversal.
+ *
+ * Returns 0 on success, or a negative value on failure.
+ */
+static int forall_declarators(struct cdecl *decl,
+ int f(struct cdecl_declarator **, struct cdecl_declarator *))
+{
+ struct cdecl_declarator *d, **p;
+
+ for (p = &decl->declarators; *p; p = &d->child) {
+ int rc;
+
+ rc = f(p, *p);
+ if (rc < 0)
+ return rc;
+ d = *p;
+
+ if (d->type == CDECL_DECL_FUNCTION) {
+ struct cdecl *i;
+
+ for (i = d->u.function.parameters; i; i = i->next) {
+ rc = forall_declarators(i, f);
+ if (rc < 0)
+ return rc;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static struct cdecl *do_parse(const char *str, int english_mode)