Skip to content

Add lowering field to ConstructExp #21706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions compiler/src/dmd/dinterpret.d
Original file line number Diff line number Diff line change
Expand Up @@ -4701,34 +4701,6 @@ public:
result = CTFEExp.voidexp;
return;
}
else if (isArrayConstruction(fd.ident))
{
// In expressionsem.d, `T[x] ea = eb;` was lowered to:
// `_d_array{,set}ctor(ea[], eb[]);`.
// The following code will rewrite it back to `ea = eb` and
// then interpret that expression.

if (fd.ident == Id._d_arrayctor)
assert(e.arguments.length == 3);
else
assert(e.arguments.length == 2);

Expression ea = (*e.arguments)[0];
if (ea.isCastExp)
ea = ea.isCastExp.e1;

Expression eb = (*e.arguments)[1];
if (eb.isCastExp() && fd.ident == Id._d_arrayctor)
eb = eb.isCastExp.e1;

ConstructExp ce = new ConstructExp(e.loc, ea, eb);
ce.type = ea.type;

ce.type = ea.type;
result = interpret(ce, istate);

return;
}
}
else if (auto soe = ecall.isSymOffExp())
{
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -4098,6 +4098,8 @@ extern (C++) final class LoweredAssignExp : AssignExp
*/
extern (C++) final class ConstructExp : AssignExp
{
Expression lowering;

extern (D) this(Loc loc, Expression e1, Expression e2) @safe
{
super(loc, EXP.construct, e1, e2);
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ class AssignExp : public BinExp
class ConstructExp final : public AssignExp
{
public:
Expression *lowering;
void accept(Visitor *v) override { v->visit(this); }
};

Expand Down
18 changes: 10 additions & 8 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -12043,16 +12043,16 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
return setError();

// Lower to object._d_array{,set}ctor(e1, e2)
Expression id = new IdentifierExp(exp.loc, Id.empty);
id = new DotIdExp(exp.loc, id, Id.object);
id = new DotIdExp(exp.loc, id, func);
Expression lowering = new IdentifierExp(ae.loc, Id.empty);
lowering = new DotIdExp(ae.loc, lowering, Id.object);
lowering = new DotIdExp(ae.loc, lowering, func);

auto arguments = new Expressions(new CastExp(ae.loc, ae.e1, t1e.arrayOf).expressionSemantic(sc));
if (lowerToArrayCtor)
{
arguments.push(new CastExp(ae.loc, rhs, t2b.nextOf.arrayOf).expressionSemantic(sc));
Expression ce = new CallExp(exp.loc, id, arguments);
res = ce.expressionSemantic(sc);
lowering = new CallExp(ae.loc, lowering, arguments);
lowering = lowering.expressionSemantic(sc);
}
else
{
Expand All @@ -12067,12 +12067,14 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
else
arguments.push(ae.e2);

Expression ce = new CallExp(exp.loc, id, arguments);
res = Expression.combine(e0, ce).expressionSemantic(sc);
lowering = new CallExp(ae.loc, lowering, arguments);
lowering = Expression.combine(e0, lowering).expressionSemantic(sc);
}

ae.lowering = lowering;

if (global.params.v.verbose)
message("lowered %s =>\n %s", exp.toChars(), res.toChars());
message("lowered %s =>\n %s", exp.toChars(), lowering.toChars());
}
}
else if (auto ae = res.isAssignExp())
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -2835,6 +2835,7 @@ class CondExp final : public BinExp
class ConstructExp final : public AssignExp
{
public:
Expression* lowering;
void accept(Visitor* v) override;
};

Expand Down
55 changes: 54 additions & 1 deletion compiler/src/dmd/glue/e2ir.d
Original file line number Diff line number Diff line change
Expand Up @@ -2841,6 +2841,59 @@ elem* toElem(Expression e, ref IRState irs)
assert(0);
}

/***************************************
*/

elem* visitConstruct(ConstructExp ae)
{
Type t1b = ae.e1.type.toBasetype();
if (t1b.ty != Tsarray && t1b.ty != Tarray)
return visitAssign(ae);

// only non-trivial array constructions have been lowered (non-POD elements basically)
Type t1e = t1b.nextOf();
TypeStruct ts = t1e.baseElemOf().isTypeStruct();
if (!ts || !(ts.sym.postblit || ts.sym.hasCopyCtor || ts.sym.dtor))
return visitAssign(ae);

// ref-constructions etc. don't have lowering
if (!(t1b.ty == Tsarray || ae.e1.isSliceExp) ||
(ae.e1.isVarExp && ae.e1.isVarExp.var.isVarDeclaration.isReference))
return visitAssign(ae);

// Construction from an equivalent other array?
Type t2b = ae.e2.type.toBasetype();
// skip over a (possibly implicit) cast of a static array RHS to a slice
Expression rhs = ae.e2;
Type rhsType = t2b;
if (t2b.ty == Tarray)
{
auto ce = rhs.isCastExp();
auto ct = ce ? ce.e1.type.toBasetype() : null;
if (ct && ct.ty == Tsarray)
{
rhs = ce.e1;
rhsType = ct;
}
}
const lowerToArrayCtor =
((rhsType.ty == Tarray && !rhs.isArrayLiteralExp) ||
(rhsType.ty == Tsarray && rhs.isLvalue)) &&
t1e.equivalent(t2b.nextOf);


// Construction from a single element?
const lowerToArraySetCtor = !lowerToArrayCtor && t1e.equivalent(t2b);

if (!lowerToArrayCtor && !lowerToArraySetCtor)
return visitAssign(ae);

const hookName = lowerToArrayCtor ? "_d_arrayctor" : "_d_arraysetctor";
assert(ae.lowering, "This case should have been rewritten to `" ~ hookName ~ "` in the semantic phase");
return toElem(ae.lowering, irs);
}


elem* visitLoweredAssign(LoweredAssignExp e)
{
return toElem(e.lowering, irs);
Expand Down Expand Up @@ -4182,7 +4235,7 @@ elem* toElem(Expression e, ref IRState irs)
case EXP.identity: return visitIdentity(e.isIdentityExp());
case EXP.in_: return visitIn(e.isInExp());
case EXP.assign: return visitAssign(e.isAssignExp());
case EXP.construct: return visitAssign(e.isConstructExp());
case EXP.construct: return visitConstruct(e.isConstructExp());
case EXP.blit: return visitAssign(e.isBlitExp());
case EXP.loweredAssignExp: return visitLoweredAssign(e.isLoweredAssignExp());
case EXP.addAssign: return visitAddAssign(e.isAddAssignExp());
Expand Down
20 changes: 20 additions & 0 deletions compiler/src/dmd/inline.d
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,18 @@ public:
visit(cast(BinExp)e);
}

override void visit(ConstructExp e)
{
if (e.lowering)
{
auto ce = cast(ConstructExp)e.copy();
ce.lowering = doInlineAs!Expression(ce.lowering, ids);
result = ce;
}
else
visit(cast(AssignExp) e);
}

override void visit(LoweredAssignExp e)
{
result = doInlineAs!Expression(e.lowering, ids);
Expand Down Expand Up @@ -1399,6 +1411,14 @@ public:
visit(cast(BinExp)e);
}

override void visit(ConstructExp e)
{
if (auto lowering = e.lowering)
inlineScan(lowering);
else
visit(cast(AssignExp) e);
}

override void visit(LoweredAssignExp e)
{
inlineScan(e.lowering);
Expand Down
Loading