]> git.draconx.ca Git - cdecl99.git/blobdiff - src/parse.y
libcdecl: Combine identifier and struct allocation.
[cdecl99.git] / src / parse.y
index ec7ea1b74b5e78f1738a240c84b459f39e051575..8631f3b4613fd028b720865f4b85ed03237c3384 100644 (file)
@@ -76,10 +76,10 @@ const char *cdecl__token_name(unsigned token);
        uintmax_t uintval;
        unsigned spectype;
        _Bool boolval;
-       char *strval;
        struct cdecl_declspec *declspec;
        struct cdecl_declarator *declarator;
        struct cdecl *decl;
+       struct parse_item *item;
 }
 
 %{
@@ -91,7 +91,6 @@ static void free_declspec(struct cdecl_declspec *x)
        struct cdecl_declspec *p;
        while (x) {
                p = x->next;
-               free(x->ident);
                free(x);
                x = p;
        }
@@ -106,16 +105,12 @@ static void free_declarator(struct cdecl_declarator *x)
 
                switch (x->type) {
                case CDECL_DECL_NULL:
-                       break;
                case CDECL_DECL_IDENT:
-                       free(x->u.ident);
+               case CDECL_DECL_ARRAY:
                        break;
                case CDECL_DECL_POINTER:
                        free_declspec(x->u.pointer.qualifiers);
                        break;
-               case CDECL_DECL_ARRAY:
-                       free(x->u.array.vla);
-                       break;
                case CDECL_DECL_FUNCTION:
                        free_decl(x->u.function.parameters);
                        break;
@@ -167,21 +162,20 @@ static void join_specs(struct cdecl_declspec *a, struct cdecl_declspec *b)
  * Alter an abstract declarator (type name) to declare an identifier instead,
  * used by the English parser rules to reduce "identifier as type" sequences.
  */
-static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
+static struct cdecl *insert_identifier(struct cdecl *decl, struct parse_item *ident)
 {
-       struct cdecl_declarator *d = decl->declarators;
+       struct cdecl_declarator *d, **p = &decl->declarators;
 
-       while (d->child)
-               d = d->child;
-
-       d->type = CDECL_DECL_IDENT;
-       d->u.ident = ident;
+       while ((d = *p)->child)
+               p = &d->child;
+       free(d);
 
+       *p = d = &ident->u.declarator;
        return decl;
 }
 %}
 
-%destructor { free($$); }            <strval>
+%destructor { free($$); }            <item>
 %destructor { free_declspec($$); }   <declspec>
 %destructor { free_declarator($$); } <declarator>
 %destructor { free_decl($$); }       <decl>
@@ -189,8 +183,8 @@ static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
 /* Magic tokens */
 %token T_LEX_ERROR
 
-%token <strval> T_IDENT "identifier"
-%token <uintval> T_UINT "integer constant"
+%token <item>    T_IDENT "identifier"
+%token <uintval> T_UINT  "integer constant"
 
 %token T_SEMICOLON ";"
 %token T_ASTERISK  "*"
@@ -244,7 +238,7 @@ static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
 %token T_AS        "as"
 %token T_VLA       "variable-length"
 
-%type <strval>     vla_ident
+%type <item>       vla_ident
 %type <uintval>    array_length
 %type <boolval>    varargs
 %type <spectype>   declspec_simple qualifier_simple
@@ -257,7 +251,7 @@ static struct cdecl *insert_identifier(struct cdecl *decl, char *ident)
 %type <decl>       declaration declarators declarator_wrap
 %type <decl>       parameter
 
-%type <strval>     english_vla
+%type <item>       english_vla
 %type <declspec>   storage_func_specs post_specs
 %type <declspec>   type_qual_spec type_qual_specs typedef_name_qual
 %type <declarator> english_declarator english_array english_function
@@ -366,16 +360,19 @@ qualifier: qualifier_simple {
 }
 
 typespec: typespec_noid | typespec_tagged T_IDENT {
-       ALLOC_STRUCT($$, struct cdecl_declspec,
-               .type  = $1,
-               .ident = $2);
+       /* Compiler should be able to elide this assignment. */
+       $2->u.declspec.ident = $2->u.declarator.u.ident;
+
+       $$ = &$2->u.declspec;
+       $$->type = $1;
 }
 
 declspec_noid: declspec_notype | typespec_noid
 
 vla_ident: T_IDENT | T_ASTERISK {
-       ALLOC($$, sizeof "");
-       *$$ = 0;
+       if (!($$ = cdecl__alloc_item(1)))
+               YYERROR;
+       *$$->s = 0;
 }
 
 array: T_LBRACKET array_length T_RBRACKET {
@@ -383,9 +380,10 @@ array: T_LBRACKET array_length T_RBRACKET {
                .type = CDECL_DECL_ARRAY,
                .u.array.length = $2);
 } | T_LBRACKET vla_ident T_RBRACKET {
-       ALLOC_STRUCT($$, struct cdecl_declarator,
-               .type = CDECL_DECL_ARRAY,
-               .u.array.vla = $2);
+       $$ = &$2->u.declarator;
+       $$->type = CDECL_DECL_ARRAY;
+       $$->u.array.vla = $$->u.ident;
+       $$->u.array.length = 0;
 }
 
 parameter: declspecs declarator {
@@ -444,9 +442,7 @@ 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);
+       $$ = &$1->u.declarator;
 } | direct_declarator postfix {
        $$ = $2;
        $$->child = $1;
@@ -530,10 +526,12 @@ english_parameter_list: english_parameter varargs {
 }
 
 typedef_name_qual: T_IDENT qualifiers {
-       ALLOC_STRUCT($$, struct cdecl_declspec,
-               .type = CDECL_TYPE_IDENT,
-               .ident = $1,
-               .next = $2);
+       /* Compiler should be able to elide this assignment. */
+       $1->u.declspec.ident = $1->u.declarator.u.ident;
+
+       $$ = &$1->u.declspec;
+       $$->type = CDECL_TYPE_IDENT;
+       $$->next = $2;
 }
 
 null_decl: {
@@ -564,9 +562,10 @@ english_parameter: english_declaration | typedef_name_qual null_decl {
 }
 
 english_array: T_VLA T_ARRAY english_vla T_OF {
-       ALLOC_STRUCT($$, struct cdecl_declarator,
-               .type = CDECL_DECL_ARRAY,
-               .u.array.vla = $3);
+       $$ = &$3->u.declarator;
+       $$->type = CDECL_DECL_ARRAY;
+       $$->u.array.vla = $$->u.ident;
+       $$->u.array.length = 0;
 } | T_ARRAY array_length T_OF {
        ALLOC_STRUCT($$, struct cdecl_declarator,
                .type = CDECL_DECL_ARRAY,
@@ -580,8 +579,9 @@ array_length: T_UINT {
 }
 
 english_vla: T_IDENT | {
-       ALLOC($$, sizeof "");
-       *$$ = 0;
+       if (!($$ = cdecl__alloc_item(1)))
+               YYERROR;
+       *$$->s = 0;
 }
 
 %%