Skip to content

Commit

Permalink
Add convenient functions getElement and copyElements to ArrayLiteralExp
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Sep 22, 2015
1 parent 51ecaf8 commit 872e0a7
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 94 deletions.
94 changes: 17 additions & 77 deletions src/constfold.d
Expand Up @@ -760,12 +760,8 @@ extern (C++) UnionExp Equal(TOK op, Loc loc, Type type, Expression e1, Expressio
{
for (size_t i = 0; i < es1.elements.dim; i++)
{
Expression ee1 = (*es1.elements)[i];
Expression ee2 = (*es2.elements)[i];
if (!ee1)
ee1 = es1.basis;
if (!ee2)
ee2 = es2.basis;
auto ee1 = es1.getElement(i);
auto ee2 = es2.getElement(i);
ue = Equal(TOKequal, loc, Type.tint32, ee1, ee2);
if (CTFEExp.isCantExp(ue.exp()))
return ue;
Expand Down Expand Up @@ -798,9 +794,7 @@ extern (C++) UnionExp Equal(TOK op, Loc loc, Type type, Expression e1, Expressio
for (size_t i = 0; i < dim1; i++)
{
uinteger_t c = es1.charAt(i);
Expression ee2 = (*es2.elements)[i];
if (!ee2)
ee2 = es2.basis;
auto ee2 = es2.getElement(i);
if (ee2.isConst() != 1)
{
emplaceExp!(CTFEExp)(&ue, TOKcantexp);
Expand Down Expand Up @@ -1436,9 +1430,7 @@ extern (C++) UnionExp Index(Type type, Expression e1, Expression e2)
else if (e1.op == TOKarrayliteral)
{
ArrayLiteralExp ale = cast(ArrayLiteralExp)e1;
Expression e = (*ale.elements)[cast(size_t)i];
if (!e)
e = ale.basis;
auto e = ale.getElement(cast(size_t)i);
e.type = type;
e.loc = loc;
if (hasSideEffect(e))
Expand All @@ -1462,9 +1454,7 @@ extern (C++) UnionExp Index(Type type, Expression e1, Expression e2)
}
else
{
Expression e = (*ale.elements)[cast(size_t)i];
if (!e)
e = ale.basis;
auto e = ale.getElement(cast(size_t)i);
e.type = type;
e.loc = loc;
if (hasSideEffect(e))
Expand Down Expand Up @@ -1610,10 +1600,7 @@ extern (C++) void sliceAssignStringFromArrayLiteral(StringExp existingSE, ArrayL
void* s = existingSE.string;
for (size_t j = 0; j < newae.elements.dim; j++)
{
auto el = (*newae.elements)[j];
if (!el)
el = newae.basis;
uint val = cast(uint)el.toInteger();
uint val = cast(uint)newae.getElement(j).toInteger();
switch (existingSE.sz)
{
case 1:
Expand Down Expand Up @@ -1663,10 +1650,7 @@ extern (C++) int sliceCmpStringWithArray(StringExp se1, ArrayLiteralExp ae2, siz
size_t sz = se1.sz;
for (size_t j = 0; j < len; j++)
{
auto el = (*ae2.elements)[j + lo2];
if (!el)
el = ae2.basis;
uint val2 = cast(uint)el.toInteger();
uint val2 = cast(uint)ae2.getElement(j + lo2).toInteger();
uint val1;
switch (sz)
{
Expand Down Expand Up @@ -1813,10 +1797,7 @@ extern (C++) UnionExp Cat(Type type, Expression e1, Expression e2)
elems.setDim(len);
for (size_t i = 0; i < ea.elements.dim; ++i)
{
auto el = (*ea.elements)[i];
if (!el)
el = ea.basis;
(*elems)[i] = el;
(*elems)[i] = ea.getElement(i);
}
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, elems);
ArrayLiteralExp dest = cast(ArrayLiteralExp)ue.exp();
Expand All @@ -1835,10 +1816,7 @@ extern (C++) UnionExp Cat(Type type, Expression e1, Expression e2)
elems.setDim(len);
for (size_t i = 0; i < ea.elements.dim; ++i)
{
auto el = (*ea.elements)[i];
if (!el)
el = ea.basis;
(*elems)[es.len + i] = el;
(*elems)[es.len + i] = ea.getElement(i);
}
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, elems);
ArrayLiteralExp dest = cast(ArrayLiteralExp)ue.exp();
Expand Down Expand Up @@ -1898,23 +1876,9 @@ extern (C++) UnionExp Cat(Type type, Expression e1, Expression e2)
else if (e1.op == TOKarrayliteral && e2.op == TOKarrayliteral && t1.nextOf().equals(t2.nextOf()))
{
// Concatenate the arrays
ArrayLiteralExp es1 = cast(ArrayLiteralExp)e1;
ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;

auto elems = es1.elements.copy();
elems.append(es2.elements);
foreach (ref el; (*elems)[][0 .. es1.elements.dim])
{
if (!el)
el = es1.basis;
}
foreach (ref el; (*elems)[][es1.elements.dim .. elems.dim])
{
if (!el)
el = es2.basis;
}
auto elems = ArrayLiteralExp.copyElements(e1, e2);

emplaceExp!(ArrayLiteralExp)(&ue, es1.loc, elems);
emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, elems);

e = ue.exp();
if (type.toBasetype().ty == Tsarray)
Expand All @@ -1936,16 +1900,9 @@ extern (C++) UnionExp Cat(Type type, Expression e1, Expression e2)
e = e2;
L3:
// Concatenate the array with null
ArrayLiteralExp es = cast(ArrayLiteralExp)e;

auto elems = es.elements.copy();
foreach (ref el; *elems)
{
if (!el)
el = es.basis;
}
auto elems = ArrayLiteralExp.copyElements(e);

emplaceExp!(ArrayLiteralExp)(&ue, es.loc, elems);
emplaceExp!(ArrayLiteralExp)(&ue, e.loc, elems);

e = ue.exp();
if (type.toBasetype().ty == Tsarray)
Expand All @@ -1959,17 +1916,8 @@ extern (C++) UnionExp Cat(Type type, Expression e1, Expression e2)
}
else if ((e1.op == TOKarrayliteral || e1.op == TOKnull) && e1.type.toBasetype().nextOf() && e1.type.toBasetype().nextOf().equals(e2.type))
{
auto elems = new Expressions();
if (e1.op == TOKarrayliteral)
{
auto es1 = cast(ArrayLiteralExp)e1;
elems.append(es1.elements);
foreach (ref el; *elems)
{
if (!el)
el = es1.basis;
}
}
auto elems = (e1.op == TOKarrayliteral)
? ArrayLiteralExp.copyElements(e1) : new Expressions();
elems.push(e2);

emplaceExp!(ArrayLiteralExp)(&ue, e1.loc, elems);
Expand All @@ -1986,17 +1934,9 @@ extern (C++) UnionExp Cat(Type type, Expression e1, Expression e2)
}
else if (e2.op == TOKarrayliteral && e2.type.toBasetype().nextOf().equals(e1.type))
{
ArrayLiteralExp es2 = cast(ArrayLiteralExp)e2;
auto elems = new Expressions();
elems.push(e1);
elems.append(es2.elements);
foreach (ref el; (*elems)[][1 .. elems.dim])
{
if (!el)
el = es2.basis;
}
auto elems = ArrayLiteralExp.copyElements(e1, e2);

emplaceExp!(ArrayLiteralExp)(&ue, es2.loc, elems);
emplaceExp!(ArrayLiteralExp)(&ue, e2.loc, elems);

e = ue.exp();
if (type.toBasetype().ty == Tsarray)
Expand Down
4 changes: 1 addition & 3 deletions src/dcast.d
Expand Up @@ -2430,9 +2430,7 @@ extern (C++) bool isVoidArrayLiteral(Expression e, Type other)
while (e.op == TOKarrayliteral && e.type.ty == Tarray && ((cast(ArrayLiteralExp)e).elements.dim == 1))
{
auto ale = cast(ArrayLiteralExp)e;
e = (*ale.elements)[0];
if (!e)
e = ale.basis;
e = ale.getElement(0);
if (other.ty == Tsarray || other.ty == Tarray)
other = other.nextOf();
else
Expand Down
5 changes: 1 addition & 4 deletions src/dmangle.d
Expand Up @@ -739,10 +739,7 @@ public:
buf.printf("A%u", dim);
for (size_t i = 0; i < dim; i++)
{
auto el = (*e.elements)[i];
if (!el)
el = e.basis;
el.accept(this);
e.getElement(i).accept(this);
}
}

Expand Down
55 changes: 52 additions & 3 deletions src/expression.d
Expand Up @@ -4628,6 +4628,57 @@ public:
return false;
}

final Expression getElement(size_t i)
{
auto el = (*elements)[i];
if (!el)
el = basis;
return el;
}

/* Copy element `Expressions` in the parameters when they're `ArrayLiteralExp`s.
* Params:
* e1 = If it's ArrayLiteralExp, its `elements` will be copied.
* Otherwise, `e1` itself will be pushed into the new `Expressions`.
* e2 = If it's not `null`, it will be pushed/appended to the new
* `Expressions` by the same way with `e1`.
* Returns:
* Newly allocated `Expresions. Note that it points the original
* `Expression` values in e1 and e2.
*/
static Expressions* copyElements(Expression e1, Expression e2 = null)
{
auto elems = new Expressions();

void append(ArrayLiteralExp ale)
{
if (!ale.elements)
return;
auto d = elems.dim;
elems.append(ale.elements);
foreach (ref el; (*elems)[][d .. elems.dim])
{
if (!el)
el = ale.basis;
}
}

if (e1.op == TOKarrayliteral)
append(cast(ArrayLiteralExp)e1);
else
elems.push(e1);

if (e2)
{
if (e2.op == TOKarrayliteral)
append(cast(ArrayLiteralExp)e2);
else
elems.push(e2);
}

return elems;
}

override Expression semantic(Scope* sc)
{
static if (LOGSEMANTIC)
Expand Down Expand Up @@ -4681,9 +4732,7 @@ public:
{
for (size_t i = 0; i < elements.dim; ++i)
{
Expression ch = (*elements)[i];
if (!ch)
ch = basis;
auto ch = getElement(i);
if (ch.op != TOKint64)
return null;
if (sz == 1)
Expand Down
3 changes: 3 additions & 0 deletions src/expression.h
Expand Up @@ -420,6 +420,9 @@ class ArrayLiteralExp : public Expression

Expression *syntaxCopy();
bool equals(RootObject *o);
Expression *getElement(size_t i);
static Expressions* copyElements(Expression *e1, Expression *e2 = NULL);
Expressions *copyElements();
Expression *semantic(Scope *sc);
bool isBool(bool result);
StringExp *toStringExp();
Expand Down
9 changes: 2 additions & 7 deletions src/todt.c
Expand Up @@ -418,10 +418,7 @@ dt_t **Expression_toDt(Expression *e, dt_t **pdt)
dt_t **pdtend = &d;
for (size_t i = 0; i < e->elements->dim; i++)
{
Expression *el = (*e->elements)[i];
if (!el)
el = e->basis;
pdtend = Expression_toDt(el, pdtend);
pdtend = Expression_toDt(e->getElement(i), pdtend);
}
Type *t = e->type->toBasetype();

Expand Down Expand Up @@ -532,9 +529,7 @@ dt_t **Expression_toDt(Expression *e, dt_t **pdt)
if (e->e1->op == TOKarrayliteral)
{
ArrayLiteralExp *ale = (ArrayLiteralExp *)e->e1;
elem = (*ale->elements)[i];
if (!elem)
elem = ale->basis;
elem = ale->getElement(i);
}
else
elem = e->e1;
Expand Down

0 comments on commit 872e0a7

Please sign in to comment.