]> git.draconx.ca Git - cdecl99.git/blobdiff - src/parse-decl.c
Initial support for function declarators.
[cdecl99.git] / src / parse-decl.c
index bf70a60ab857df06a4cfeef482e65e448502d58d..25ace5b008c89bbc61626a0883abc58df9b4cf2f 100644 (file)
@@ -17,6 +17,7 @@
  */
 #include <stdio.h>
 #include <assert.h>
+#include <stdbool.h>
 
 #include "cdecl.h"
 #include "typemap.h"
@@ -66,9 +67,207 @@ static int verify_declspecs(struct cdecl_declspec *s)
        return 0;
 }
 
-static int verify_decl(struct cdecl *decl)
+/*
+ * 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.
+ *
+ *   - The function declarator has a null child declarator.
+ *   - The function declarator has exactly one parameter, and is not variadic.
+ *   - The function parameter has a type specifier, and it is a typedef name.
+ *   - The function parameter has no other declaration specifiers.
+ *   - The function parameter does not declare an identifier.
+ *
+ * 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_declspec   *spec = param->specifiers;
+       struct cdecl_declarator *decl = param->declarators;
+       struct cdecl_declarator *last;
+
+       for (last = decl; last && last->type != CDECL_DECL_NULL;)
+               last = last->child;
+
+       if (!last)
+               return NULL;
+
+       last->type = CDECL_DECL_IDENT;
+       last->u.ident = spec->ident;
+       free(param);
+       free(spec);
+
+       return decl;
+}
+
+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) */
+
+       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->child);
+       free(d);
+
+       return 1;
+}
+
+/*
+ * The 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 an identifier enclosed 3 times in parentheses.  This is complicated by
+ * the fact that things like (((int))) are not valid anywhere.
+ *
+ * To avoid ambiguities, the parser actually emits a "function" declarator for
+ * every pair of parentheses.  The ones that can't reasonably be functions
+ * consist of a single "parameter" with no declaration specifiers (note that
+ * every valid function parameter will have at least one type specifier).
+ *
+ * This pass is to remove these fake functions from the parse tree.  We take
+ * care to avoid turning invalid things like ((int)) into valid things like
+ * (int) by observing that the only valid function declarators that appear
+ * in these "fake" parentheses are those that have a non-null child declarator
+ * (for instance, int ((*)(int)) *or* those that will be eliminated by the
+ * simplify_functions pass.
+ */
+
+static int
+reduce_parentheses(struct cdecl_declarator **p, struct cdecl_declarator *d)
+{
+       struct cdecl *param;
+
+       if (d->type != CDECL_DECL_FUNCTION)
+               return 0;
+
+       param = d->u.function.parameters;
+       if (param && param->specifiers == NULL) {
+               struct cdecl_declarator *decl;
+
+               assert(!param->next);
+
+               decl = param->declarators;
+               if (decl->type == CDECL_DECL_NULL) {
+                       free(decl);
+                       free(param);
+                       d->u.function.parameters = NULL;
+                       return 0;
+               }
+
+               if (d->child->type != CDECL_DECL_NULL) {
+                       fprintf(stderr, "invalid function parameter\n");
+                       return -1;
+               }
+
+               free(d->child);
+               free(param);
+               free(d);
+               *p = decl;
+
+               /*
+                * We may have replaced d with another fake function which
+                * also needs to be eliminated.
+                */
+               if (reduce_parentheses(p, decl) < 0)
+                       return -1;
+
+               /*
+                * If the remaining declarator is a function, make sure it's
+                * valid by checking its reducibility.
+                */
+               decl = *p;
+               if (decl->type == CDECL_DECL_FUNCTION
+                   && decl->child->type == CDECL_DECL_NULL
+                   && !function_is_reducible(decl)) {
+                       fprintf(stderr, "too many parentheses in function\n");
+                       return -1;
+               }
+
+               return 1;
+       }
+
+       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.
+ */
+static bool forall_declarators(struct cdecl *decl,
+       int f(struct cdecl_declarator **, struct cdecl_declarator *))
 {
-       return verify_declspecs(decl->specifiers);
+       struct cdecl_declarator *d, **p;
+
+       for (p = &decl->declarators, d = *p; d; p = &d->child, d = *p) {
+               switch (f(p, d)) {
+               case 0:
+                       break;
+               case 1:
+                       d = *p;
+                       break;
+               case -1:
+                       return false;
+               default:
+                       assert(0);
+               }
+
+               if (d->type == CDECL_DECL_FUNCTION) {
+                       struct cdecl *i;
+
+                       for (i = d->u.function.parameters; i; i = i->next) {
+                               if (!forall_declarators(i, f))
+                                       return false;
+                       }
+               }
+       }
+
+       return true;
 }
 
 struct cdecl *cdecl_parse_decl(const char *declstr)
@@ -84,11 +283,18 @@ struct cdecl *cdecl_parse_decl(const char *declstr)
        if (rc != 0)
                return NULL;
 
-       rc = verify_decl(decl);
-       if (rc != 0) {
-               cdecl_free(decl);
-               return NULL;
+       if (verify_declspecs(decl->specifiers) != 0)
+               goto err;
+
+       for (struct cdecl *i = decl; i; i = i->next) {
+               if (!forall_declarators(i, reduce_parentheses))
+                       goto err;
+               if (!forall_declarators(i, simplify_functions))
+                       goto err;
        }
 
        return decl;
+err:
+       cdecl_free(decl);
+       return NULL;
 }