Skip to content

Commit

Permalink
opover.d: deduplicate tryAliasThisForLhs and checkAliasThisForLhs (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel authored Jan 23, 2025
1 parent be6a7bb commit 3166ce1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 55 deletions.
41 changes: 2 additions & 39 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -11878,49 +11878,12 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
}
else
{
// Try alias this on first operand
static Expression tryAliasThisForLhs(BinAssignExp exp, Scope* sc)
{
AggregateDeclaration ad1 = isAggregate(exp.e1.type);
if (!ad1 || !ad1.aliasthis)
return null;

/* Rewrite (e1 op e2) as:
* (e1.aliasthis op e2)
*/
if (isRecursiveAliasThis(exp.att1, exp.e1.type))
return null;
//printf("att %s e1 = %s\n", Token.toChars(e.op), e.e1.type.toChars());
Expression e1 = new DotIdExp(exp.loc, exp.e1, ad1.aliasthis.ident);
BinExp be = cast(BinExp)exp.copy();
be.e1 = e1;
return be.trySemantic(sc);
}

// Try alias this on second operand
static Expression tryAliasThisForRhs(BinAssignExp exp, Scope* sc)
{
AggregateDeclaration ad2 = isAggregate(exp.e2.type);
if (!ad2 || !ad2.aliasthis)
return null;
/* Rewrite (e1 op e2) as:
* (e1 op e2.aliasthis)
*/
if (isRecursiveAliasThis(exp.att2, exp.e2.type))
return null;
//printf("att %s e2 = %s\n", Token.toChars(e.op), e.e2.type.toChars());
Expression e2 = new DotIdExp(exp.loc, exp.e2, ad2.aliasthis.ident);
BinExp be = cast(BinExp)exp.copy();
be.e2 = e2;
return be.trySemantic(sc);
}

Laliasthis:
result = tryAliasThisForLhs(exp, sc);
result = checkAliasThisForLhs(isAggregate(exp.e1.type), sc, exp);
if (result)
return;

result = tryAliasThisForRhs(exp, sc);
result = checkAliasThisForRhs(isAggregate(exp.e2.type), sc, exp);
if (result)
return;

Expand Down
20 changes: 4 additions & 16 deletions compiler/src/dmd/opover.d
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Objects* opToArg(Scope* sc, EXP op)
}

// Try alias this on first operand
private Expression checkAliasThisForLhs(AggregateDeclaration ad, Scope* sc, BinExp e)
Expression checkAliasThisForLhs(AggregateDeclaration ad, Scope* sc, BinExp e)
{
if (!ad || !ad.aliasthis)
return null;
Expand All @@ -149,17 +149,11 @@ private Expression checkAliasThisForLhs(AggregateDeclaration ad, Scope* sc, BinE
if (!be.e1)
return null;

Expression result;
if (be.op == EXP.concatenateAssign)
result = be.isBinAssignExp().opOverloadBinaryAssign(sc);
else
result = be.trySemantic(sc);

return result;
return be.trySemantic(sc);
}

// Try alias this on second operand
private Expression checkAliasThisForRhs(AggregateDeclaration ad, Scope* sc, BinExp e)
Expression checkAliasThisForRhs(AggregateDeclaration ad, Scope* sc, BinExp e)
{
if (!ad || !ad.aliasthis)
return null;
Expand All @@ -174,13 +168,7 @@ private Expression checkAliasThisForRhs(AggregateDeclaration ad, Scope* sc, BinE
if (!be.e2)
return null;

Expression result;
if (be.op == EXP.concatenateAssign)
result = be.isBinAssignExp().opOverloadBinaryAssign(sc);
else
result = be.trySemantic(sc);

return result;
return be.trySemantic(sc);
}

Expression opOverloadUnary(UnaExp e, Scope* sc)
Expand Down

0 comments on commit 3166ce1

Please sign in to comment.