scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 0916935c02c6513acbb0f36d625c7cb25315236f
parent bd3704ba7221b1432934c82a3ec6181849d8ebd8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat,  8 Aug 2015 18:35:13 +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.

Diffstat:
cc1/cc1.h | 2+-
cc1/decl.c | 6+++---
cc1/symbol.c | 22++++++++--------------
3 files changed, 12 insertions(+), 18 deletions(-)

diff --git 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 @@ -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 @@ -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;