Skip to content

Commit

Permalink
[Refactoring] Variable interpretation from its initializer
Browse files Browse the repository at this point in the history
Reuse VarDeclaration::getConstInitializer.
  • Loading branch information
9rnsr committed May 9, 2013
1 parent 75d3660 commit 04dd7b7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 57 deletions.
21 changes: 10 additions & 11 deletions src/declaration.c
Expand Up @@ -2041,21 +2041,20 @@ ExpInitializer *VarDeclaration::getExpInitializer()
* Otherwise, return NULL.
*/

Expression *VarDeclaration::getConstInitializer()
Expression *VarDeclaration::getConstInitializer(bool needFullType)
{
if ((isConst() || isImmutable() || storage_class & STCmanifest) &&
storage_class & STCinit)
assert(type && init);

if (scope)
{
ExpInitializer *ei = getExpInitializer();
if (ei)
return ei->exp;
else if (init)
{
return init->toExpression();
}
inuse++;
init->semantic(scope, type, INITinterpret);
scope = NULL;
inuse--;
}
Expression *e = init->toExpression(needFullType ? type : NULL);

return NULL;
return e;
}

/*************************************
Expand Down
2 changes: 1 addition & 1 deletion src/declaration.h
Expand Up @@ -309,7 +309,7 @@ struct VarDeclaration : Declaration
#endif
Expression *callScopeDtor(Scope *sc);
ExpInitializer *getExpInitializer();
Expression *getConstInitializer();
Expression *getConstInitializer(bool needFullType = true);
void checkCtorConstInit();
void checkNestedReference(Scope *sc, Loc loc);
Dsymbol *toAlias();
Expand Down
14 changes: 3 additions & 11 deletions src/expression.c
Expand Up @@ -4381,19 +4381,11 @@ Expression *StructLiteralExp::semantic(Scope *sc)
else
{
if (v->init)
{ if (v->init->isVoidInitializer())
{
if (v->init->isVoidInitializer())
e = NULL;
else
{
if (v->scope)
{
v->inuse++;
v->init->semantic(v->scope, v->type, INITinterpret);
v->scope = NULL;
v->inuse--;
}
e = v->init->toExpression(/*vd->type*/);
}
e = v->getConstInitializer(false);
}
else if (v->type->needsNested() && ctorinit)
e = v->type->defaultInit(loc);
Expand Down
13 changes: 3 additions & 10 deletions src/mtype.c
Expand Up @@ -8027,18 +8027,11 @@ Expression *TypeStruct::defaultInitLiteral(Loc loc)
if (vd->offset < offset)
e = NULL;
else if (vd->init)
{ if (vd->init->isVoidInitializer())
{
if (vd->init->isVoidInitializer())
e = NULL;
else
{
if (vd->scope)
{
vd->inuse++;
vd->init->semantic(vd->scope, vd->type, INITinterpret);
vd->inuse--;
}
e = vd->init->toExpression(/*vd->type*/);
}
e = vd->getConstInitializer(false);
}
else
e = vd->type->defaultInitLiteral(loc);
Expand Down
9 changes: 1 addition & 8 deletions src/optimize.c
Expand Up @@ -68,14 +68,7 @@ Expression *expandVar(int result, VarDeclaration *v)
v->error("recursive initialization of constant");
goto L1;
}
if (v->scope)
{
v->inuse++;
v->init->semantic(v->scope, v->type, INITinterpret);
v->scope = NULL;
v->inuse--;
}
Expression *ei = v->init->toExpression(v->type);
Expression *ei = v->getConstInitializer();
if (!ei)
{ if (v->storage_class & STCmanifest)
v->error("enum cannot be initialized with %s", v->init->toChars());
Expand Down
18 changes: 2 additions & 16 deletions src/template.c
Expand Up @@ -182,14 +182,7 @@ Expression *getValue(Expression *e)
VarDeclaration *v = ((VarExp *)e)->var->isVarDeclaration();
if (v && v->storage_class & STCmanifest)
{
if (v->scope)
{
v->inuse++;
v->init->semantic(v->scope, v->type, INITinterpret);
v->scope = NULL;
v->inuse--;
}
e = v->init->toExpression(v->type);
e = v->getConstInitializer();
}
}
return e;
Expand All @@ -202,14 +195,7 @@ Expression *getValue(Dsymbol *&s)
VarDeclaration *v = s->isVarDeclaration();
if (v && v->storage_class & STCmanifest)
{
if (v->scope)
{
v->inuse++;
v->init->semantic(v->scope, v->type, INITinterpret);
v->scope = NULL;
v->inuse--;
}
e = v->init->toExpression(v->type);
e = v->getConstInitializer();
}
}
return e;
Expand Down

0 comments on commit 04dd7b7

Please sign in to comment.