]> git.draconx.ca Git - cdecl99.git/blobdiff - src/parse.y
Initial support for function declarators.
[cdecl99.git] / src / parse.y
index 8840279b020b9ab8e945eb6ae812254a8297d759..63d5fa927dc4ec48e1289321798bcb68183a3acf 100644 (file)
@@ -1,3 +1,4 @@
+%code top {
 /*
  *  Parser for C declarations.
  *  Copyright © 2011 Nick Bowler
@@ -15,6 +16,7 @@
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
+}
 
 %parse-param {struct cdecl **out}
 %define api.pure
@@ -22,6 +24,9 @@
 %locations
 
 %{
+#include <assert.h>
+#include <stdbool.h>
+
 #include "scan.h"
 #include "cdecl.h"
 
@@ -53,6 +58,7 @@ int yyparse(struct cdecl **out);
 
 %union {
        uintmax_t uintval;
+       _Bool boolval;
        char *strval;
        struct cdecl_declspec *declspec;
        struct cdecl_declarator *declarator;
@@ -60,6 +66,8 @@ int yyparse(struct cdecl **out);
 }
 
 %{
+static void free_decl(struct cdecl *);
+
 static void free_declspec(struct cdecl_declspec *x)
 {
        struct cdecl_declspec *p;
@@ -74,39 +82,54 @@ static void free_declspec(struct cdecl_declspec *x)
 static void free_declarator(struct cdecl_declarator *x)
 {
        struct cdecl_declarator *p;
+
        while (x) {
-               p = x->next;
+               p = x->child;
+
                switch (x->type) {
+               case CDECL_DECL_NULL:
+                       break;
                case CDECL_DECL_IDENT:
                        free(x->u.ident);
                        break;
                case CDECL_DECL_POINTER:
                        free_declspec(x->u.pointer.qualifiers);
-                       free_declarator(x->u.pointer.declarator);
                        break;
                case CDECL_DECL_ARRAY:
                        free(x->u.array.vla);
-                       free_declarator(x->u.array.declarator);
+                       break;
+               case CDECL_DECL_FUNCTION:
+                       free_decl(x->u.function.parameters);
                        break;
                default:
-                       abort();
+                       assert(0);
                }
+
                free(x);
                x = p;
        }
 }
 
-static void free_decl(struct cdecl *decl)
+static void free_decl(struct cdecl *x)
 {
-       free_declspec(decl->specifiers);
-       free_declarator(decl->declarators);
-       free(decl);
+       struct cdecl *p;
+
+       while (x) {
+               p = x->next;
+
+               /* The specifiers may be shared by an entire chain. */
+               if (!p || p->specifiers != x->specifiers)
+                       free_declspec(x->specifiers);
+
+               free_declarator(x->declarators);
+               free(x);
+               x = p;
+       }
 }
 
 void cdecl_free(struct cdecl *decl)
 {
-       if (decl)
-               free_decl(decl);
+       free_decl(decl);
 }
 %}
 
@@ -127,6 +150,7 @@ void cdecl_free(struct cdecl *decl)
 %token T_LBRACKET  "["
 %token T_RBRACKET  "]"
 %token T_COMMA     ","
+%token T_ELLIPSIS  "."
 
 %token T_TYPEDEF  "typedef"
 %token T_EXTERN   "extern"
@@ -157,12 +181,15 @@ void cdecl_free(struct cdecl *decl)
 %token T_ENUM     "enum"
 
 %type <strval>     vla_ident
+%type <boolval>    varargs
 %type <uintval>    declspec_simple typespec_simple qualifier_simple
 %type <declspec>   declspec_notype declspec_noid typespec_noid typespec
-%type <declspec>   qualifier qualifiers pointer
+%type <declspec>   qualifier qualifiers
 %type <declspec>   declspecs declspecs_noid
-%type <declarator> direct_declarator declarator declarators array
-%type <decl>       declaration
+%type <declarator> direct_declarator declarator pointer array parens postfix
+%type <declarator> direct_declarator_ish declarator_ish parameter_type_list
+%type <decl>       declaration declarators declarator_wrap
+%type <decl>       parameter parameters
 
 %%
 
@@ -171,9 +198,10 @@ input: declaration {
 };
 
 declaration: declspecs declarators T_SEMICOLON {
-       ALLOC_STRUCT($$, struct cdecl,
-               .specifiers = $1,
-               .declarators = $2);
+       $$ = $2;
+
+       for (struct cdecl *i = $$; i; i = i->next)
+               i->specifiers = $1;
 };
 
 declspecs: declspec_notype declspecs {
@@ -194,10 +222,14 @@ qualifiers: { $$ = NULL; } | qualifiers qualifier {
        $$->next = $1;
 }
 
-declarators: declarator | declarator T_COMMA declarators {
+declarators: declarator_wrap | declarator_wrap T_COMMA declarators {
        $$ = $1;
        $$->next = $3;
-};
+}
+
+declarator_wrap: declarator {
+       ALLOC_STRUCT($$, struct cdecl, .declarators = $1);
+}
 
 declspec_simple: T_AUTO { $$ = CDECL_STOR_AUTO;     }
        | T_TYPEDEF     { $$ = CDECL_STOR_TYPEDEF;  }
@@ -254,8 +286,6 @@ typespec: typespec_noid | T_STRUCT T_IDENT {
 
 declspec_noid: declspec_notype | typespec_noid
 
-pointer: T_ASTERISK qualifiers { $$ = $2; }
-
 vla_ident: T_IDENT | T_ASTERISK {
        ALLOC($$, sizeof "");
        strcpy($$, "");
@@ -277,23 +307,78 @@ array: T_LBRACKET T_UINT T_RBRACKET {
                .type = CDECL_DECL_ARRAY);
 }
 
-declarator: direct_declarator | pointer direct_declarator {
+parameter: declspecs declarator {
+       ALLOC_STRUCT($$, struct cdecl,
+               .specifiers = $1,
+               .declarators = $2);
+}
+
+parameters: parameter | parameters T_COMMA parameter {
+       $$ = $3;
+       $$->next = $1;
+}
+
+varargs: { $$ = false; } | T_COMMA T_ELLIPSIS { $$ = true; }
+
+parameter_type_list: parameters varargs {
+       struct cdecl *p, *c, *n;
+
+       /* Parameters were accumulated in reverse order. */
+       for (p = NULL, c = $1; c; p = c, c = n) {
+               n = c->next;
+               c->next = p;
+       }
+
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_FUNCTION,
+               .u.function.parameters = p,
+               .u.function.variadic = $2);
+}
+
+parens: T_LPAREN parameter_type_list T_RPAREN {
+       $$ = $2;
+} | T_LPAREN declarator_ish T_RPAREN {
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_FUNCTION);
+       ALLOC_STRUCT($$->u.function.parameters, struct cdecl,
+               .declarators = $2);
+}
+
+pointer: T_ASTERISK qualifiers direct_declarator {
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_POINTER,
+               .u.pointer.qualifiers = $2,
+               .child = $3);
+} | T_ASTERISK qualifiers pointer {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_POINTER,
-               .u.pointer.qualifiers = $1,
-               .u.pointer.declarator = $2);
+               .u.pointer.qualifiers = $2,
+               .child = $3);
+}
+
+declarator: direct_declarator | pointer
+declarator_ish: direct_declarator_ish | pointer
+postfix: array | parens
+
+direct_declarator_ish: {
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_NULL);
+} | direct_declarator_ish postfix {
+       $$ = $2;
+       $$->child = $1;
 }
 
-direct_declarator: T_IDENT {
+direct_declarator: {
+       ALLOC_STRUCT($$, struct cdecl_declarator,
+               .type = CDECL_DECL_NULL);
+} | T_IDENT {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_IDENT,
                .u.ident = $1);
-} | direct_declarator array {
+} | direct_declarator postfix {
        $$ = $2;
-       $$->u.array.declarator = $1;
-} | T_LPAREN declarator T_RPAREN {
-       $$ = $2;
-};
+       $$->child = $1;
+}
 
 %%
 void yyerror(YYLTYPE *loc, struct cdecl **out, const char *err)