From 57e7419c4d958c79782381a9f7f5aea4358d5aa4 Mon Sep 17 00:00:00 2001 From: Matthew Qiu <93230055+MatthewQiu-5@users.noreply.github.com> Date: Thu, 6 Mar 2025 02:59:41 -0500 Subject: [PATCH] Moved StaticForeach.lowerArrayAggregate to expressionsem (#20955) --- compiler/src/dmd/cond.d | 47 +------------------------------- compiler/src/dmd/expressionsem.d | 45 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 46 deletions(-) diff --git a/compiler/src/dmd/cond.d b/compiler/src/dmd/cond.d index 716b3ba926..80e4043ce0 100644 --- a/compiler/src/dmd/cond.d +++ b/compiler/src/dmd/cond.d @@ -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. * diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index 6c0970e703..1fffc38b79 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -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(); +}