Skip to content

Commit

Permalink
Merge pull request #4158 from WalterBright/voidinitliteral
Browse files Browse the repository at this point in the history
convert voidInitLiteral()
  • Loading branch information
9rnsr committed Nov 22, 2014
2 parents ac8ace7 + 326309e commit ac828cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/ctfe.h
Expand Up @@ -78,7 +78,7 @@ class VoidInitExp : public Expression
};

// Create an appropriate void initializer
Expression *voidInitLiteral(Type *t, VarDeclaration *var);
UnionExp voidInitLiteral(Type *t, VarDeclaration *var);

/** Fake class which holds the thrown exception.
Used for implementing exception handling.
Expand Down
23 changes: 13 additions & 10 deletions src/ctfeexpr.c
Expand Up @@ -279,7 +279,7 @@ Expression *copyLiteral(Expression *e)
VarDeclaration *v = sd->fields[i];
// If it is a void assignment, use the default initializer
if (!m)
m = voidInitLiteral(v->type, v);
m = voidInitLiteral(v->type, v).copy();
if ((v->type->ty != m->type->ty) && v->type->ty == Tsarray)
{
// Block assignment from inside struct literals
Expand Down Expand Up @@ -2219,12 +2219,13 @@ void showCtfeExpr(Expression *e, int level)

/*************************** Void initialization ***************************/

Expression *voidInitLiteral(Type *t, VarDeclaration *var)
UnionExp voidInitLiteral(Type *t, VarDeclaration *var)
{
UnionExp ue;
if (t->ty == Tsarray)
{
TypeSArray *tsa = (TypeSArray *)t;
Expression *elem = voidInitLiteral(tsa->next, var);
Expression *elem = voidInitLiteral(tsa->next, var).copy();

// For aggregate value types (structs, static arrays) we must
// create an a separate copy for each element.
Expand All @@ -2239,24 +2240,26 @@ Expression *voidInitLiteral(Type *t, VarDeclaration *var)
elem = copyLiteral(elem);
(*elements)[i] = elem;
}
ArrayLiteralExp *ae = new ArrayLiteralExp(var->loc, elements);
new(&ue) ArrayLiteralExp(var->loc, elements);
ArrayLiteralExp *ae = (ArrayLiteralExp *)ue.exp();
ae->type = tsa;
ae->ownedByCtfe = true;
return ae;
}
if (t->ty == Tstruct)
else if (t->ty == Tstruct)
{
TypeStruct *ts = (TypeStruct *)t;
Expressions *exps = new Expressions();
exps->setDim(ts->sym->fields.dim);
for (size_t i = 0; i < ts->sym->fields.dim; i++)
{
(*exps)[i] = voidInitLiteral(ts->sym->fields[i]->type, ts->sym->fields[i]);
(*exps)[i] = voidInitLiteral(ts->sym->fields[i]->type, ts->sym->fields[i]).copy();
}
StructLiteralExp *se = new StructLiteralExp(var->loc, ts->sym, exps);
new(&ue) StructLiteralExp(var->loc, ts->sym, exps);
StructLiteralExp *se = (StructLiteralExp *)ue.exp();
se->type = ts;
se->ownedByCtfe = true;
return se;
}
return new VoidInitExp(var, t);
else
new(&ue) VoidInitExp(var, t);
return ue;
}
10 changes: 5 additions & 5 deletions src/interpret.c
Expand Up @@ -2571,7 +2571,7 @@ class Interpreter : public Visitor
}
else if (v2->init->isVoidInitializer())
{
setValue(v2, voidInitLiteral(v2->type, v2));
setValue(v2, voidInitLiteral(v2->type, v2).copy());
}
else
{
Expand All @@ -2593,7 +2593,7 @@ class Interpreter : public Visitor
result = ie->exp->interpret(istate, goal);
else if (v->init->isVoidInitializer())
{
result = voidInitLiteral(v->type, v);
result = voidInitLiteral(v->type, v).copy();
// There is no AssignExp for void initializers,
// so set it here.
setValue(v, result);
Expand Down Expand Up @@ -2926,7 +2926,7 @@ class Interpreter : public Visitor
* leave CTFE, causing another memory allocation if we use this
* same struct literal again.
*
* ex = voidInitLiteral(sd->fields[i]->type, sd->fields[i]);
* ex = voidInitLiteral(sd->fields[i]->type, sd->fields[i]).copy();
*/
ex = NULL;
}
Expand Down Expand Up @@ -3107,7 +3107,7 @@ class Interpreter : public Visitor
if (v->init)
{
if (v->init->isVoidInitializer())
m = voidInitLiteral(v->type, v);
m = voidInitLiteral(v->type, v).copy();
else
m = v->getConstInitializer(true);
}
Expand Down Expand Up @@ -4124,7 +4124,7 @@ class Interpreter : public Visitor
continue;
Expression **exp = &(*se->elements)[i];
if ((*exp)->op != TOKvoid)
*exp = voidInitLiteral((*exp)->type, member);
*exp = voidInitLiteral((*exp)->type, member).copy();
}
}

Expand Down

0 comments on commit ac828cc

Please sign in to comment.