Moved StaticForeach.lowerArrayAggregate to expressionsem (#20955)

This commit is contained in:
Matthew Qiu 2025-03-06 02:59:41 -05:00 committed by GitHub
parent cc6f184a66
commit 57e7419c4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 46 deletions

View file

@ -24,7 +24,7 @@ import dmd.dscope;
import dmd.dsymbol;
import dmd.errors;
import dmd.expression;
import dmd.expressionsem : expressionSemantic, evalStaticCondition;
import dmd.expressionsem : evalStaticCondition;
import dmd.globals;
import dmd.identifier;
import dmd.location;
@ -144,51 +144,6 @@ extern (C++) final class StaticForeach : RootObject
);
}
/*****************************************
* Turn an aggregate which is an array into an expression tuple
* of its elements. I.e., lower
* static foreach (x; [1, 2, 3, 4]) { ... }
* to
* static foreach (x; AliasSeq!(1, 2, 3, 4)) { ... }
*/
extern(D) void lowerArrayAggregate(Scope* sc)
{
auto aggr = aggrfe.aggr;
Expression el = new ArrayLengthExp(aggr.loc, aggr);
sc = sc.startCTFE();
el = el.expressionSemantic(sc);
sc = sc.endCTFE();
el = el.optimize(WANTvalue);
el = el.ctfeInterpret();
if (el.op != EXP.int64)
{
aggrfe.aggr = ErrorExp.get();
return;
}
Expressions* es;
if (auto ale = aggr.isArrayLiteralExp())
{
// Directly use the elements of the array for the TupleExp creation
es = ale.elements;
}
else
{
const length = cast(size_t)el.toInteger();
es = new Expressions(length);
foreach (i; 0 .. length)
{
auto index = new IntegerExp(loc, i, Type.tsize_t);
auto value = new IndexExp(aggr.loc, aggr, index);
(*es)[i] = value;
}
}
aggrfe.aggr = new TupleExp(aggr.loc, es);
aggrfe.aggr = aggrfe.aggr.expressionSemantic(sc);
aggrfe.aggr = aggrfe.aggr.optimize(WANTvalue);
aggrfe.aggr = aggrfe.aggr.ctfeInterpret();
}
/*****************************************
* Wrap a statement into a function literal and call it.
*

View file

@ -17614,3 +17614,48 @@ extern(D) void prepare(StaticForeach sfe, Scope* sc)
}
}
}
/*****************************************
* Turn an aggregate which is an array into an expression tuple
* of its elements. I.e., lower
* static foreach (x; [1, 2, 3, 4]) { ... }
* to
* static foreach (x; AliasSeq!(1, 2, 3, 4)) { ... }
*/
extern(D) void lowerArrayAggregate(StaticForeach sfe, Scope* sc)
{
auto aggr = sfe.aggrfe.aggr;
Expression el = new ArrayLengthExp(aggr.loc, aggr);
sc = sc.startCTFE();
el = el.expressionSemantic(sc);
sc = sc.endCTFE();
el = el.optimize(WANTvalue);
el = el.ctfeInterpret();
if (el.op != EXP.int64)
{
sfe.aggrfe.aggr = ErrorExp.get();
return;
}
Expressions* es;
if (auto ale = aggr.isArrayLiteralExp())
{
// Directly use the elements of the array for the TupleExp creation
es = ale.elements;
}
else
{
const length = cast(size_t)el.toInteger();
es = new Expressions(length);
foreach (i; 0 .. length)
{
auto index = new IntegerExp(sfe.loc, i, Type.tsize_t);
auto value = new IndexExp(aggr.loc, aggr, index);
(*es)[i] = value;
}
}
sfe.aggrfe.aggr = new TupleExp(aggr.loc, es);
sfe.aggrfe.aggr = sfe.aggrfe.aggr.expressionSemantic(sc);
sfe.aggrfe.aggr = sfe.aggrfe.aggr.optimize(WANTvalue);
sfe.aggrfe.aggr = sfe.aggrfe.aggr.ctfeInterpret();
}