commit 0916935c02c6513acbb0f36d625c7cb25315236f
Author:     Roberto E. Vargas Caballero <[email protected]>
AuthorDate: Sat Aug 8 18:35:13 2015 +0200
Commit:     Roberto E. Vargas Caballero <[email protected]>
CommitDate: Sat Aug 8 18:35:13 2015 +0200

    Remove dependence between install() and yylval.sym
    
    Install() is always called after a call to lookup(),
    so yylval.sym is always a correct Symbol related to
    the value of yytext. This condition makes hard use
    install() in some cases, so this patch adds a new
    parameter to install that is the Symbol to be
    installed.

diff --git a/cc1/cc1.h b/cc1/cc1.h
index e42b59b..7fd62f8 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -297,7 +297,7 @@ extern Type *duptype(Type *base);
 extern void dumpstab(char *msg);
 extern Symbol *lookup(unsigned ns);
 extern Symbol *nextsym(Symbol *sym, unsigned ns);
-extern Symbol *install(unsigned ns);
+extern Symbol *install(unsigned ns, Symbol *sym);
 extern Symbol *newsym(unsigned ns);
 extern void pushctx(void), popctx(void);
 extern void ikeywords(void);
diff --git a/cc1/decl.c b/cc1/decl.c
index 227848a..5b8d0df 100644
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -143,7 +143,7 @@ directdcl(struct dcldata *dp, unsigned ns)
                /* TODO: check type of the function */
                /* TODO: check function is not redefined */
                if (yytoken == IDEN || yytoken == TYPEIDEN) {
-                       if ((sym = install(ns)) == NULL)
+                       if ((sym = install(ns, yylval.sym)) == NULL)
                                error("redeclaration of '%s'", yytext);
                        next();
                } else {
@@ -333,7 +333,7 @@ newtag(void)
        case TYPEIDEN:
                sym = yylval.sym;
                if ((sym->flags & ISDEFINED) == 0)
-                       install(NS_TAG);
+                       install(NS_TAG, yylval.sym);
                next();
                break;
        default:
@@ -394,7 +394,7 @@ enumdcl(void)
        for (val = 0; yytoken != ')'; ++val) {
                if (yytoken != IDEN)
                        unexpected();
-               if ((sym = install(NS_IDEN)) == NULL) {
+               if ((sym = install(NS_IDEN, yylval.sym)) == NULL) {
                        error("'%s' redeclared as different kind of symbol",
                              yytext);
                }
diff --git a/cc1/symbol.c b/cc1/symbol.c
index 7e25a14..93648ec 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -185,25 +185,19 @@ nextsym(Symbol *sym, unsigned ns)
 }
 
 Symbol *
-install(unsigned ns)
+install(unsigned ns, Symbol *sym)
 {
-       Symbol *sym, **h;
-       /*
-        * install() is always called after a call to lookup(), so
-        * yylval.sym always points to a symbol with yytext name.
-        * if the symbol is an undefined symbol and in the same
-        * context, then it was generated in the previous lookup()
-        * call. If the symbol is defined and in the same context
-        * then there is a redefinition
-        */
-       if (yylval.sym->ctx == curctx) {
-               if (yylval.sym->flags & ISDEFINED)
+       if (sym->ctx == curctx) {
+               if (sym->flags & ISDEFINED)
                        return NULL;
-               yylval.sym->flags |= ISDEFINED;
+               sym->flags |= ISDEFINED;
                sym = yylval.sym;
        } else {
+               char *name = sym->name;
+               Symbol **h;
+
                sym = newsym(ns);
-               sym->name = xstrdup(yytext);
+               sym->name = xstrdup(name);
                h = &htab[hash(yytext)];
                sym->hash = *h;
                *h = sym;

Reply via email to