From 35113fe484b0055597943c44eac7c3fb20cf5252 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Fri, 18 Oct 2024 11:16:27 +0800 Subject: [PATCH] [expressionsem.d] factorise some commonalities of BinExp semantic --- compiler/src/dmd/expressionsem.d | 171 +++++++------------------------ 1 file changed, 36 insertions(+), 135 deletions(-) diff --git a/compiler/src/dmd/expressionsem.d b/compiler/src/dmd/expressionsem.d index cce8a5638695..3c6760c9c2f4 100644 --- a/compiler/src/dmd/expressionsem.d +++ b/compiler/src/dmd/expressionsem.d @@ -12753,35 +12753,37 @@ 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) @@ -12789,15 +12791,28 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor if (!isArrayOpValid(exp)) { result = arrayOpInvalidError(exp); - return; + return true; } 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; @@ -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; @@ -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; @@ -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()) { @@ -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; @@ -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; @@ -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()) { @@ -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);