Substantial rewrite (again) of decl handling.

The decl.* code now no longer interacts with Symbols, but just returns
names, types, initializer expressions, etc., as needed.  This makes the
code a bit more understandable.

Fixes issues #171 and #130.
This commit is contained in:
Matt Pharr
2012-04-12 17:28:30 -07:00
parent d88dbf3612
commit 5ece6fec04
15 changed files with 396 additions and 423 deletions

View File

@@ -631,7 +631,9 @@ declaration_statement
if ($1->declarators[i] == NULL)
Assert(m->errorCount > 0);
else
m->AddTypeDef($1->declarators[i]->GetSymbol());
m->AddTypeDef($1->declarators[i]->name,
$1->declarators[i]->type,
$1->declarators[i]->pos);
}
$$ = NULL;
}
@@ -1174,7 +1176,7 @@ direct_declarator
: TOKEN_IDENTIFIER
{
Declarator *d = new Declarator(DK_BASE, @1);
d->sym = new Symbol(yytext, @1);
d->name = yytext;
$$ = d;
}
| '(' declarator ')'
@@ -1349,8 +1351,10 @@ type_name
{
if ($1 == NULL || $2 == NULL)
$$ = NULL;
else
$$ = $2->GetType($1, NULL);
else {
$2->InitFromType($1, NULL);
$$ = $2->type;
}
}
;
@@ -1854,11 +1858,14 @@ function_definition
}
compound_statement
{
std::vector<Symbol *> args;
if ($2 != NULL) {
Symbol *sym = $2->GetFunctionInfo($1, &args);
if (sym != NULL)
m->AddFunctionDefinition(sym, args, $4);
$2->InitFromDeclSpecs($1);
const FunctionType *funcType =
dynamic_cast<const FunctionType *>($2->type);
if (funcType == NULL)
Assert(m->errorCount > 0);
else
m->AddFunctionDefinition($2->name, funcType, $4);
}
m->symbolTable->PopScope(); // push in lAddFunctionParams();
}
@@ -1968,35 +1975,27 @@ lAddDeclaration(DeclSpecs *ds, Declarator *decl) {
// Error happened earlier during parsing
return;
decl->InitFromDeclSpecs(ds);
if (ds->storageClass == SC_TYPEDEF)
m->AddTypeDef(decl->GetSymbol());
m->AddTypeDef(decl->name, decl->type, decl->pos);
else {
const Type *t = decl->GetType(ds);
if (t == NULL) {
if (decl->type == NULL) {
Assert(m->errorCount > 0);
return;
}
Symbol *sym = decl->GetSymbol();
if (sym == NULL) {
Assert(m->errorCount > 0);
return;
}
const FunctionType *ft = dynamic_cast<const FunctionType *>(t);
decl->type = decl->type->ResolveUnboundVariability(Variability::Varying);
const FunctionType *ft = dynamic_cast<const FunctionType *>(decl->type);
if (ft != NULL) {
sym->type = ft;
sym->storageClass = ds->storageClass;
bool isInline = (ds->typeQualifiers & TYPEQUAL_INLINE);
m->AddFunctionDeclaration(sym, isInline);
m->AddFunctionDeclaration(decl->name, ft, ds->storageClass,
isInline, decl->pos);
}
else {
if (sym->type == NULL)
Assert(m->errorCount > 0);
else
sym->type = sym->type->ResolveUnboundVariability(Variability::Varying);
bool isConst = (ds->typeQualifiers & TYPEQUAL_CONST) != 0;
m->AddGlobalVariable(sym, decl->initExpr, isConst);
m->AddGlobalVariable(decl->name, decl->type, decl->initExpr,
isConst, decl->storageClass, decl->pos);
}
}
}
@@ -2025,16 +2024,13 @@ lAddFunctionParams(Declarator *decl) {
// now loop over its parameters and add them to the symbol table
for (unsigned int i = 0; i < decl->functionParams.size(); ++i) {
Declaration *pdecl = decl->functionParams[i];
if (pdecl == NULL || pdecl->declarators.size() == 0)
// zero size declarators array corresponds to an anonymous
// parameter
continue;
Assert(pdecl->declarators.size() == 1);
Symbol *sym = pdecl->declarators[0]->GetSymbol();
if (sym == NULL || sym->type == NULL)
Assert(pdecl != NULL && pdecl->declarators.size() == 1);
Declarator *declarator = pdecl->declarators[0];
if (declarator == NULL)
Assert(m->errorCount > 0);
else {
sym->type = sym->type->ResolveUnboundVariability(Variability::Varying);
Symbol *sym = new Symbol(declarator->name, declarator->pos,
declarator->type, declarator->storageClass);
#ifndef NDEBUG
bool ok = m->symbolTable->AddVariable(sym);
if (ok == false)