Skip to content

Commit

Permalink
Add VarDeclaration.expandInitializer to factor out common code
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Sep 30, 2015
1 parent a989653 commit b362ceb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
24 changes: 23 additions & 1 deletion src/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -2113,11 +2113,12 @@ public:

/*******************************************
* If variable has a constant expression initializer, get it.
* Otherwise, return NULL.
* Otherwise, return null.
*/
final Expression getConstInitializer(bool needFullType = true)
{
assert(type && _init);

// Ungag errors when not speculative
uint oldgag = global.gag;
if (global.gag)
Expand All @@ -2126,18 +2127,39 @@ public:
if (sym && !sym.isSpeculative())
global.gag = 0;
}

if (_scope)
{
inuse++;
_init = _init.semantic(_scope, type, INITinterpret);
_scope = null;
inuse--;
}

Expression e = _init.toExpression(needFullType ? type : null);
global.gag = oldgag;
return e;
}

/*******************************************
* Helper function for the expansion of manifest constant.
*/
final Expression expandInitializer(Loc loc)
{
assert((storage_class & STCmanifest) && _init);

auto e = getConstInitializer();
if (!e)
{
.error(loc, "cannot make expression out of initializer for %s", toChars());
return new ErrorExp();
}

e = e.copy();
e.loc = loc; // for better error message
return e;
}

override final void checkCtorConstInit()
{
version (none)
Expand Down
41 changes: 2 additions & 39 deletions src/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -3752,23 +3752,7 @@ public:
.error(loc, "circular initialization of %s", v.toChars());
return new ErrorExp();
}
if (v._scope)
{
v.inuse++;
v._init = v._init.semantic(v._scope, v.type, INITinterpret);
v._scope = null;
v.inuse--;
}

e = v._init.toExpression(v.type);
if (!e)
{
.error(loc, "cannot make expression out of initializer for %s", v.toChars());
return new ErrorExp();
}
e = e.copy();
e.loc = loc; // for better error message

e = v.expandInitializer(loc);
v.inuse++;
e = e.semantic(sc);
v.inuse--;
Expand Down Expand Up @@ -5308,28 +5292,7 @@ public:
error("recursive expansion of %s '%s'", ti.kind(), ti.toPrettyChars());
return new ErrorExp();
}
//if (v.inuse) // This is the point.
//{
// .error(loc, "circular initialization of %s", v.toChars());
// return new ErrorExp();
//}
if (v._scope)
{
v.inuse++;
v._init = v._init.semantic(v._scope, v.type, INITinterpret);
v._scope = null;
v.inuse--;
}

auto e = v._init.toExpression(v.type);
if (!e)
{
error("cannot make expression out of initializer for %s", v.toChars());
return new ErrorExp();
}
e = e.copy();
e.loc = loc; // for better error message

auto e = v.expandInitializer(loc);
ti.inuse++;
e = e.semantic(sc);
ti.inuse--;
Expand Down

0 comments on commit b362ceb

Please sign in to comment.