ref: af9f64086ce4a32c58c096e56b6ca8432d1fcf88
parent: ac1f52ccad9cb7919726a64c458763eb78f5dd4a
author: Roberto E. Vargas Caballero <[email protected]>
date: Tue May 13 13:23:24 EDT 2014
Remove ns field of symbols This field was added in order to can define a namespace for every aggregate, but it was a bad way of doing it. It is a good idea to create a new context only for the declaration of aggregates, because we don't want to create symbols for them, we only want to take the symbol name and the type in order to create the fields of the struct.
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
@@ -23,10 +23,9 @@
enum {
NS_IDEN = 0,
- NS_LABEL,
NS_TAG,
- NR_NAMESPACES,
- NS_FREE
+ NS_LABEL,
+ NR_NAMESPACES
};
struct funpars;
@@ -71,7 +70,6 @@
short id;
uint8_t ctx;
uint8_t token;
- uint8_t ns;
struct {
bool isglobal : 1;
bool isstatic : 1;
@@ -83,7 +81,7 @@
int i;
char *s;
struct symbol *sym;
- uint8_t ns, token;
+ uint8_t token;
} u;
struct symbol *next;
struct symbol *hash;
@@ -106,6 +104,7 @@
typedef void Ctxfun(Symbol *, Symbol *, Caselist *);
extern Ctxfun compound;
+extern Type *aggregate(Type *(*fun)(void));
extern void context(Ctxfun *fun,
Symbol *lbreak, Symbol *lcont, Caselist *lswitch);
--- a/cc1/decl.c
+++ b/cc1/decl.c
@@ -22,8 +22,7 @@
} u;
};
-static struct dcldata
- *declarator0(struct dcldata *dp, uint8_t ns, int8_t flags);
+static struct dcldata *declarator0(struct dcldata *dp, int8_t flags);
static struct dcldata *
arydcl(struct dcldata *dp)
@@ -50,7 +49,7 @@
}
static Symbol *
-newiden(uint8_t ns)
+newiden(void)
{
Symbol *sym;
extern uint8_t curctx;
@@ -57,27 +56,27 @@
if (yylval.sym && yylval.sym->ctx == curctx)
error("redeclaration of '%s'", yytext);
- sym = install(yytext, ns);
+ sym = install(yytext, NS_IDEN);
next();
return sym;
}
static struct dcldata *
-directdcl(struct dcldata *dp, uint8_t ns, int8_t flags)
+directdcl(struct dcldata *dp, int8_t flags)
{
register Symbol *sym;
char *err;
if (accept('(')) {
- dp = declarator0(dp, ns, flags);
+ dp = declarator0(dp, flags);
expect(')');
} else if (flags) {
if (yytoken != IDEN) {
if (flags & ID_EXPECTED)
goto expected;
- sym = install("", ns);
+ sym = install("", NS_IDEN);
} else {
- sym = newiden(ns);
+ sym = newiden();
}
dp->op = IDEN;
dp->u.sym = sym;
@@ -97,7 +96,7 @@
}
static struct dcldata*
-declarator0(struct dcldata *dp, uint8_t ns, int8_t flags)
+declarator0(struct dcldata *dp, int8_t flags)
{
uint8_t buffer[NR_DECLARATORS];
register uint8_t *bp, n, qlf;
@@ -114,7 +113,7 @@
*bp++ = qlf;
}
- dp = directdcl(dp, ns, flags);
+ dp = directdcl(dp, flags);
bp = buffer;
while (n--) {
@@ -132,7 +131,7 @@
}
static Symbol *
-declarator(Type *tp, uint8_t ns, int8_t flags)
+declarator(Type *tp, int8_t flags)
{
struct dcldata data[NR_DECLARATORS+1];
register struct dcldata *bp;
@@ -141,7 +140,7 @@
memset(data, 0, sizeof(data));
data[NR_DECLARATORS].op = 255;
- for (bp = declarator0(data, ns, flags); bp >= data; --bp) {
+ for (bp = declarator0(data, flags); bp >= data; --bp) {
switch (bp->op) {
case PTR:
tp = qualifier(mktype(tp, PTR, NULL, 0), bp->u.qlf);
@@ -213,7 +212,7 @@
goto invalid_type;
*p = yylval.token;
if (dcl)
- tp = dcl();
+ tp = aggregate(dcl);
else
next();
}
@@ -295,7 +294,7 @@
}
static void
-fielddcl(Type *base, uint8_t ns)
+fielddcl(Type *base)
{
Type *tp;
Symbol *sym;
@@ -314,7 +313,7 @@
if (yytoken != ';') {
do {
- sym = declarator(tp, ns, ID_EXPECTED);
+ sym = declarator(tp, ID_EXPECTED);
newfield(base, sym);
} while (accept(','));
}
@@ -336,7 +335,6 @@
{
register Symbol *sym;
Type *tp;
- extern uint8_t namespace;
if (yytoken == IDEN) {
sym = lookup(yytext, NS_TAG);
@@ -351,7 +349,6 @@
sym = install("", NS_TAG);
}
tp = sym->type = mktype(NULL, tag, NULL, 0);
- sym->u.ns = ++namespace;
tp->sym = sym;
return tp;
@@ -363,19 +360,18 @@
structdcl(void)
{
Type *tp;
- uint8_t ns, tag;
+ uint8_t tag;
tag = yylval.token;
next();
tp = newtag(tag);
tp->u.fields = NULL;
- ns = tp->sym->u.ns;
if (accept('{')) {
if (tp->defined)
goto redefined;
tp->defined = 1;
while (!accept('}'))
- fielddcl(tp, ns);
+ fielddcl(tp);
}
return tp;
@@ -402,7 +398,7 @@
while (yytoken != '}') {
if (yytoken != IDEN)
goto iden_expected;
- sym = newiden(NS_IDEN);
+ sym = newiden();
sym->type = inttype;
if (accept('='))
initializer(inttype);
@@ -435,7 +431,7 @@
tp = specifier(&sclass);
if (yytoken != ';') {
do {
- sym = declarator(tp, NS_IDEN, ID_EXPECTED);
+ sym = declarator(tp, ID_EXPECTED);
switch (sclass) {
case REGISTER: sym->s.isregister = 1; break;
case STATIC: sym->s.isstatic = 1; break;
@@ -461,7 +457,7 @@
tp = specifier(&sclass);
if (sclass)
error("class storage in type name");
- sym = declarator(tp, NS_IDEN, 0);
+ sym = declarator(tp, 0);
return sym->type;
}
@@ -494,7 +490,7 @@
do {
Type *tp;
- sym = declarator(base, NS_IDEN, ID_EXPECTED);
+ sym = declarator(base, ID_EXPECTED);
tp = sym->type;
if (!(sclass & STATIC))
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
@@ -10,7 +10,6 @@
#define NR_SYM_HASH 32
uint8_t curctx;
-uint8_t namespace = NS_FREE + 1;
short symid;
static struct symtab {
@@ -34,7 +33,7 @@
static struct symtab *tbl;
register Symbol *sym, *next;
- tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns];
+ tbl = &symtab[ns];
for (sym = tbl->head; sym; sym = next) {
if (sym->ctx <= curctx)
break;
@@ -47,17 +46,24 @@
}
}
+Type *
+aggregate(Type * (*fun)(void))
+{
+ Type *tp;
+
+ ++curctx;
+ tp = (*fun)();
+ --curctx;
+ freesyms(NS_IDEN);
+ return tp;
+}
+
void
context(Ctxfun *fun, Symbol *lbreak, Symbol *lcont, Caselist *lswitch)
{
- uint8_t ns;
-
- ns = namespace;
++curctx;
(*fun)(lbreak, lcont, lswitch);
--curctx;
- namespace = ns;
-
freesyms(NS_IDEN);
freesyms(NS_TAG);
}
@@ -65,14 +71,13 @@
Symbol *
lookup(register char *s, uint8_t ns)
{
- extern union yystype yylval;
static struct symtab *tbl;
register Symbol *sym;
- tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns];
+ tbl = &symtab[ns];
for (sym = tbl->htab[hash(s)]; sym; sym = sym->hash) {
register char *t = sym->name;
- if (sym->ns == ns && t && *t == *s && !strcmp(t, s))
+ if (*t == *s && !strcmp(t, s))
return sym;
}
@@ -89,9 +94,8 @@
sym->name = xstrdup(s);
sym->ctx = curctx;
sym->token = IDEN;
- sym->ns = ns;
sym->id = symid++;
- tbl = &symtab[(ns >= NR_NAMESPACES) ? NS_IDEN : ns];
+ tbl = &symtab[ns];
sym->next = tbl->head;
tbl->head = sym;