From 4fbd471c98d86bb27dd240fe245fc9da67317072 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Sat, 22 Nov 2014 12:01:54 -0800 Subject: [PATCH] changeArrayLiteralLength() now returns UnionExp --- src/ctfe.h | 2 +- src/ctfeexpr.c | 12 +++++++----- src/interpret.c | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/ctfe.h b/src/ctfe.h index 7b3190225d5d..10e673e07969 100644 --- a/src/ctfe.h +++ b/src/ctfe.h @@ -177,7 +177,7 @@ Expression *assignAssocArrayElement(Loc loc, AssocArrayLiteralExp *aae, /// Given array literal oldval of type ArrayLiteralExp or StringExp, of length /// oldlen, change its length to newlen. If the newlen is longer than oldlen, /// all new elements will be set to the default initializer for the element type. -Expression *changeArrayLiteralLength(Loc loc, TypeArray *arrayType, +UnionExp changeArrayLiteralLength(Loc loc, TypeArray *arrayType, Expression *oldval, size_t oldlen, size_t newlen); diff --git a/src/ctfeexpr.c b/src/ctfeexpr.c index 60dcb6697d7b..6fad0fe8c890 100644 --- a/src/ctfeexpr.c +++ b/src/ctfeexpr.c @@ -1909,9 +1909,10 @@ Expression *assignAssocArrayElement(Loc loc, AssocArrayLiteralExp *aae, /// Given array literal oldval of type ArrayLiteralExp or StringExp, of length /// oldlen, change its length to newlen. If the newlen is longer than oldlen, /// all new elements will be set to the default initializer for the element type. -Expression *changeArrayLiteralLength(Loc loc, TypeArray *arrayType, +UnionExp changeArrayLiteralLength(Loc loc, TypeArray *arrayType, Expression *oldval, size_t oldlen, size_t newlen) { + UnionExp ue; Type *elemType = arrayType->next; assert(elemType); Expression *defaultElem = elemType->defaultInitLiteral(loc); @@ -1942,12 +1943,12 @@ Expression *changeArrayLiteralLength(Loc loc, TypeArray *arrayType, default: assert(0); } } - StringExp *se = new StringExp(loc, s, newlen); + new(&ue) StringExp(loc, s, newlen); + StringExp *se = (StringExp *)ue.exp(); se->type = arrayType; se->sz = oldse->sz; se->committed = oldse->committed; se->ownedByCtfe = true; - return se; } else { @@ -1969,11 +1970,12 @@ Expression *changeArrayLiteralLength(Loc loc, TypeArray *arrayType, for (size_t i = copylen; i < newlen; i++) (*elements)[i] = defaultElem; } - ArrayLiteralExp *aae = new ArrayLiteralExp(loc, elements); + new(&ue) ArrayLiteralExp(loc, elements); + ArrayLiteralExp *aae = (ArrayLiteralExp *)ue.exp(); aae->type = arrayType; aae->ownedByCtfe = true; - return aae; } + return ue; } diff --git a/src/interpret.c b/src/interpret.c index 8e6e5e05e067..a8c79040b926 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -3733,7 +3733,7 @@ class Interpreter : public Visitor if (t->ty == Tarray) { newval = changeArrayLiteralLength(e->loc, (TypeArray *)t, oldval, - oldlen, newlen); + oldlen, newlen).copy(); // We have changed it into a reference assignment // Note that returnValue is still the new length. wantRef = true;