Skip to content

Commit

Permalink
[expressionsem.d] factorise some commonalities of BinExp semantic
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilsonator committed Oct 18, 2024
1 parent 23b71f1 commit 35113fe
Showing 1 changed file with 36 additions and 135 deletions.
171 changes: 36 additions & 135 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -12753,51 +12753,66 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
trySetCatExpLowering(result);
}

override void visit(MulExp exp)
bool commonBinOpSemantic(BinExp exp)
{
version (none)
{
printf("MulExp::semantic() %s\n", exp.toChars());
}
if (exp.type)
{
result = exp;
return;
return true;
}

if (Expression ex = binSemanticProp(exp, sc))
{
result = ex;
return;
return true;
}
Expression e = exp.op_overload(sc);
if (e)
{
result = e;
return;
return true;
}

if (Expression ex = typeCombine(exp, sc))
{
result = ex;
return;
return true;
}
return false;
}
bool commonArithBinOpSemantic(BinExp exp)
{
if (commonBinOpSemantic(exp))
return true;

Type tb = exp.type.toBasetype();
if (tb.ty == Tarray || tb.ty == Tsarray)
{
if (!isArrayOpValid(exp))
{
result = arrayOpInvalidError(exp);
return;
return true;

Check warning on line 12794 in compiler/src/dmd/expressionsem.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/expressionsem.d#L12794

Added line #L12794 was not covered by tests
}
result = exp;
return;
return true;
}

if (exp.checkArithmeticBin() || exp.checkSharedAccessBin(sc))
return setError();
{
setError();
return true;
}
return false;
}
override void visit(MulExp exp)
{
version (none)
{
printf("MulExp::semantic() %s\n", exp.toChars());
}

if (commonArithBinOpSemantic(exp))
return;
if (exp.type.isFloating())
{
Type t1 = exp.e1.type;
Expand Down Expand Up @@ -12836,7 +12851,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
// iy * iv = -yv
exp.e1.type = exp.type;
exp.e2.type = exp.type;
e = new NegExp(exp.loc, exp);
Expression e = new NegExp(exp.loc, exp);
e = e.expressionSemantic(sc);
result = e;
return;
Expand All @@ -12849,7 +12864,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
exp.type = t1; // t1 is complex
}
}
else if (!target.isVectorOpSupported(tb, exp.op, exp.e2.type.toBasetype()))
else if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype()))
{
result = exp.incompatibleTypes();
return;
Expand All @@ -12859,44 +12874,8 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

override void visit(DivExp exp)
{
if (exp.type)
{
result = exp;
if (commonArithBinOpSemantic(exp))
return;
}

if (Expression ex = binSemanticProp(exp, sc))
{
result = ex;
return;
}
Expression e = exp.op_overload(sc);
if (e)
{
result = e;
return;
}

if (Expression ex = typeCombine(exp, sc))
{
result = ex;
return;
}

Type tb = exp.type.toBasetype();
if (tb.ty == Tarray || tb.ty == Tsarray)
{
if (!isArrayOpValid(exp))
{
result = arrayOpInvalidError(exp);
return;
}
result = exp;
return;
}

if (exp.checkArithmeticBin() || exp.checkSharedAccessBin(sc))
return setError();

if (exp.type.isFloating())
{
Expand All @@ -12910,7 +12889,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
{
// x/iv = i(-x/v)
exp.e2.type = t1;
e = new NegExp(exp.loc, exp);
Expression e = new NegExp(exp.loc, exp);
e = e.expressionSemantic(sc);
result = e;
return;
Expand Down Expand Up @@ -12950,7 +12929,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
exp.type = t1; // t1 is complex
}
}
else if (!target.isVectorOpSupported(tb, exp.op, exp.e2.type.toBasetype()))
else if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype()))
{
result = exp.incompatibleTypes();
return;
Expand All @@ -12960,49 +12939,8 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

override void visit(ModExp exp)
{
if (exp.type)
{
result = exp;
if (commonArithBinOpSemantic(exp))
return;
}

if (Expression ex = binSemanticProp(exp, sc))
{
result = ex;
return;
}
Expression e = exp.op_overload(sc);
if (e)
{
result = e;
return;
}

if (Expression ex = typeCombine(exp, sc))
{
result = ex;
return;
}

Type tb = exp.type.toBasetype();
if (tb.ty == Tarray || tb.ty == Tsarray)
{
if (!isArrayOpValid(exp))
{
result = arrayOpInvalidError(exp);
return;
}
result = exp;
return;
}
if (!target.isVectorOpSupported(tb, exp.op, exp.e2.type.toBasetype()))
{
result = exp.incompatibleTypes();
return;
}

if (exp.checkArithmeticBin() || exp.checkSharedAccessBin(sc))
return setError();

if (exp.type.isFloating())
{
Expand All @@ -13018,54 +12956,17 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor

override void visit(PowExp exp)
{
if (exp.type)
{
result = exp;
return;
}

//printf("PowExp::semantic() %s\n", toChars());
if (Expression ex = binSemanticProp(exp, sc))
{
result = ex;
return;
}
Expression e = exp.op_overload(sc);
if (e)
{
result = e;
return;
}

if (Expression ex = typeCombine(exp, sc))
{
result = ex;
if (commonArithBinOpSemantic(exp))
return;
}

Type tb = exp.type.toBasetype();
if (tb.ty == Tarray || tb.ty == Tsarray)
{
if (!isArrayOpValid(exp))
{
result = arrayOpInvalidError(exp);
return;
}
result = exp;
return;
}

if (exp.checkArithmeticBin() || exp.checkSharedAccessBin(sc))
return setError();

if (!target.isVectorOpSupported(tb, exp.op, exp.e2.type.toBasetype()))
if (!target.isVectorOpSupported(exp.type.toBasetype(), exp.op, exp.e2.type.toBasetype()))
{
result = exp.incompatibleTypes();
return;
}

// First, attempt to fold the expression.
e = exp.optimize(WANTvalue);
Expression e = exp.optimize(WANTvalue);
if (e.op != EXP.pow)
{
e = e.expressionSemantic(sc);
Expand Down

0 comments on commit 35113fe

Please sign in to comment.