Skip to content

Commit

Permalink
5865 __dollar cannot be read at compile time
Browse files Browse the repository at this point in the history
Now that $ is constant-folded correctly during the optimize step, the limited
constant-folding in dsymbol can be removed. It had the effect of creating
__dollar=0 in zombie cases where constant folding wasn't quite possible.
A big mess.

Here's a very simple new scheme: __dollar is still initialized for tuples, but
for arrays, it is ALWAYS void-initialized. Later, it might be turned into an
integer in the optimize step; otherwise, it becomes a runtime value.
  • Loading branch information
Don Clugston committed Apr 20, 2011
1 parent 6d1a0ff commit 03e022f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
35 changes: 10 additions & 25 deletions src/dsymbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -1171,40 +1171,25 @@ Dsymbol *ArrayScopeSymbol::search(Loc loc, Identifier *ident, int flags)
* multiple times, it gets set only once.
*/
if (!*pvar) // if not already initialized
{ /* Create variable v and set it to the value of $,
* which will be a constant.
{ /* Create variable v and set it to the value of $
*/
VarDeclaration *v = new VarDeclaration(loc, Type::tsize_t, Id::dollar, NULL);

if (ce->op == TOKvar)
{ // if ce is const, get its initializer
ce = fromConstInitializer(WANTvalue, ce);
}

if (ce->op == TOKstring)
{ /* It is for a string literal, so the
* length will be a const.
*/
Expression *e = new IntegerExp(0, ((StringExp *)ce)->len, Type::tsize_t);
v->init = new ExpInitializer(0, e);
v->storage_class |= STCstatic | STCconst;
}
else if (ce->op == TOKarrayliteral)
{ /* It is for an array literal, so the
* length will be a const.
*/
Expression *e = new IntegerExp(0, ((ArrayLiteralExp *)ce)->elements->dim, Type::tsize_t);
v->init = new ExpInitializer(0, e);
v->storage_class |= STCstatic | STCconst;
}
else if (ce->op == TOKtuple)
if (ce->op == TOKtuple)
{ /* It is for an expression tuple, so the
* length will be a const.
*/
Expression *e = new IntegerExp(0, ((TupleExp *)ce)->exps->dim, Type::tsize_t);
v->init = new ExpInitializer(0, e);
v->storage_class |= STCstatic | STCconst;
}
else
{ /* For arrays, $ will either be a compile-time constant
* (in which case its value in set during constant-folding),
* or a variable (in which case an expression is created in
* toir.c).
*/
v->init = new VoidInitializer(0);
}
*pvar = v;
}
(*pvar)->semantic(sc);
Expand Down
4 changes: 2 additions & 2 deletions src/inline.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ Expression *IndexExp::doInline(InlineDoState *ids)
ids->from.push(vd);
ids->to.push(vto);

if (vd->init)
if (vd->init && !vd->init->isVoidInitializer())
{
ie = vd->init->isExpInitializer();
assert(ie);
Expand Down Expand Up @@ -702,7 +702,7 @@ Expression *SliceExp::doInline(InlineDoState *ids)
ids->from.push(vd);
ids->to.push(vto);

if (vd->init)
if (vd->init && !vd->init->isVoidInitializer())
{
ie = vd->init->isExpInitializer();
assert(ie);
Expand Down
4 changes: 3 additions & 1 deletion src/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,10 @@ Expression *IdentityExp::optimize(int result)
*/
void setLengthVarIfKnown(VarDeclaration *lengthVar, Expression *arr)
{
if (!lengthVar || lengthVar->init)
if (!lengthVar)
return;
if (lengthVar->init && !lengthVar->init->isVoidInitializer())
return; // we have previously calculated the length
size_t len;
if (arr->op == TOKstring)
len = ((StringExp *)arr)->len;
Expand Down

0 comments on commit 03e022f

Please sign in to comment.