Skip to content

Commit 6b65711

Browse files
committed
Inline and simplify foldBinop
1 parent 257c6f3 commit 6b65711

File tree

1 file changed

+84
-82
lines changed

1 file changed

+84
-82
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/ConstFold.scala

+84-82
Original file line numberDiff line numberDiff line change
@@ -93,89 +93,91 @@ object ConstFold:
9393
end foldUnop
9494

9595
private def foldBinop(op: Name, x: Constant, y: Constant)(using Context): Constant | Null =
96-
def foldBooleanOp(op: Name, x: Constant, y: Constant): Constant | Null = op match
97-
case nme.ZOR => Constant(x.booleanValue | y.booleanValue)
98-
case nme.OR => Constant(x.booleanValue | y.booleanValue)
99-
case nme.XOR => Constant(x.booleanValue ^ y.booleanValue)
100-
case nme.ZAND => Constant(x.booleanValue & y.booleanValue)
101-
case nme.AND => Constant(x.booleanValue & y.booleanValue)
102-
case nme.EQ => Constant(x.booleanValue == y.booleanValue)
103-
case nme.NE => Constant(x.booleanValue != y.booleanValue)
96+
val nme = StdNames.nme
97+
import nme.{Constant as _, *}
98+
inline def foldBooleanOp: Constant | Null = op match
99+
case ZOR => Constant(x.booleanValue | y.booleanValue)
100+
case OR => Constant(x.booleanValue | y.booleanValue)
101+
case XOR => Constant(x.booleanValue ^ y.booleanValue)
102+
case ZAND => Constant(x.booleanValue & y.booleanValue)
103+
case AND => Constant(x.booleanValue & y.booleanValue)
104+
case EQ => Constant(x.booleanValue == y.booleanValue)
105+
case NE => Constant(x.booleanValue != y.booleanValue)
104106
case _ => null
105-
def foldSubrangeOp(op: Name, x: Constant, y: Constant): Constant | Null = op match
106-
case nme.OR => Constant(x.intValue | y.intValue)
107-
case nme.XOR => Constant(x.intValue ^ y.intValue)
108-
case nme.AND => Constant(x.intValue & y.intValue)
109-
case nme.LSL => Constant(x.intValue << y.intValue)
110-
case nme.LSR => Constant(x.intValue >>> y.intValue)
111-
case nme.ASR => Constant(x.intValue >> y.intValue)
112-
case nme.EQ => Constant(x.intValue == y.intValue)
113-
case nme.NE => Constant(x.intValue != y.intValue)
114-
case nme.LT => Constant(x.intValue < y.intValue)
115-
case nme.GT => Constant(x.intValue > y.intValue)
116-
case nme.LE => Constant(x.intValue <= y.intValue)
117-
case nme.GE => Constant(x.intValue >= y.intValue)
118-
case nme.ADD => Constant(x.intValue + y.intValue)
119-
case nme.SUB => Constant(x.intValue - y.intValue)
120-
case nme.MUL => Constant(x.intValue * y.intValue)
121-
case nme.DIV => Constant(x.intValue / y.intValue)
122-
case nme.MOD => Constant(x.intValue % y.intValue)
107+
inline def foldSubrangeOp: Constant | Null = op match
108+
case OR => Constant(x.intValue | y.intValue)
109+
case XOR => Constant(x.intValue ^ y.intValue)
110+
case AND => Constant(x.intValue & y.intValue)
111+
case LSL => Constant(x.intValue << y.intValue)
112+
case LSR => Constant(x.intValue >>> y.intValue)
113+
case ASR => Constant(x.intValue >> y.intValue)
114+
case EQ => Constant(x.intValue == y.intValue)
115+
case NE => Constant(x.intValue != y.intValue)
116+
case LT => Constant(x.intValue < y.intValue)
117+
case GT => Constant(x.intValue > y.intValue)
118+
case LE => Constant(x.intValue <= y.intValue)
119+
case GE => Constant(x.intValue >= y.intValue)
120+
case ADD => Constant(x.intValue + y.intValue)
121+
case SUB => Constant(x.intValue - y.intValue)
122+
case MUL => Constant(x.intValue * y.intValue)
123+
case DIV => Constant(x.intValue / y.intValue)
124+
case MOD => Constant(x.intValue % y.intValue)
123125
case _ => null
124-
def foldLongOp(op: Name, x: Constant, y: Constant): Constant | Null = op match
125-
case nme.OR => Constant(x.longValue | y.longValue)
126-
case nme.XOR => Constant(x.longValue ^ y.longValue)
127-
case nme.AND => Constant(x.longValue & y.longValue)
128-
case nme.LSL => if (x.tag <= IntTag) Constant(x.intValue << y.longValue.toInt) else Constant(x.longValue << y.longValue)
129-
case nme.LSR => if (x.tag <= IntTag) Constant(x.intValue >>> y.longValue.toInt) else Constant(x.longValue >>> y.longValue)
130-
case nme.ASR => if (x.tag <= IntTag) Constant(x.intValue >> y.longValue.toInt) else Constant(x.longValue >> y.longValue)
131-
case nme.EQ => Constant(x.longValue == y.longValue)
132-
case nme.NE => Constant(x.longValue != y.longValue)
133-
case nme.LT => Constant(x.longValue < y.longValue)
134-
case nme.GT => Constant(x.longValue > y.longValue)
135-
case nme.LE => Constant(x.longValue <= y.longValue)
136-
case nme.GE => Constant(x.longValue >= y.longValue)
137-
case nme.ADD => Constant(x.longValue + y.longValue)
138-
case nme.SUB => Constant(x.longValue - y.longValue)
139-
case nme.MUL => Constant(x.longValue * y.longValue)
140-
case nme.DIV => Constant(x.longValue / y.longValue)
141-
case nme.MOD => Constant(x.longValue % y.longValue)
126+
inline def foldLongOp: Constant | Null = op match
127+
case OR => Constant(x.longValue | y.longValue)
128+
case XOR => Constant(x.longValue ^ y.longValue)
129+
case AND => Constant(x.longValue & y.longValue)
130+
case LSL => if (x.tag <= IntTag) Constant(x.intValue << y.longValue.toInt) else Constant(x.longValue << y.longValue)
131+
case LSR => if (x.tag <= IntTag) Constant(x.intValue >>> y.longValue.toInt) else Constant(x.longValue >>> y.longValue)
132+
case ASR => if (x.tag <= IntTag) Constant(x.intValue >> y.longValue.toInt) else Constant(x.longValue >> y.longValue)
133+
case EQ => Constant(x.longValue == y.longValue)
134+
case NE => Constant(x.longValue != y.longValue)
135+
case LT => Constant(x.longValue < y.longValue)
136+
case GT => Constant(x.longValue > y.longValue)
137+
case LE => Constant(x.longValue <= y.longValue)
138+
case GE => Constant(x.longValue >= y.longValue)
139+
case ADD => Constant(x.longValue + y.longValue)
140+
case SUB => Constant(x.longValue - y.longValue)
141+
case MUL => Constant(x.longValue * y.longValue)
142+
case DIV => Constant(x.longValue / y.longValue)
143+
case MOD => Constant(x.longValue % y.longValue)
142144
case _ => null
143-
def foldFloatOp(op: Name, x: Constant, y: Constant): Constant | Null = op match
144-
case nme.EQ => Constant(x.floatValue == y.floatValue)
145-
case nme.NE => Constant(x.floatValue != y.floatValue)
146-
case nme.LT => Constant(x.floatValue < y.floatValue)
147-
case nme.GT => Constant(x.floatValue > y.floatValue)
148-
case nme.LE => Constant(x.floatValue <= y.floatValue)
149-
case nme.GE => Constant(x.floatValue >= y.floatValue)
150-
case nme.ADD => Constant(x.floatValue + y.floatValue)
151-
case nme.SUB => Constant(x.floatValue - y.floatValue)
152-
case nme.MUL => Constant(x.floatValue * y.floatValue)
153-
case nme.DIV => Constant(x.floatValue / y.floatValue)
154-
case nme.MOD => Constant(x.floatValue % y.floatValue)
145+
inline def foldFloatOp: Constant | Null = op match
146+
case EQ => Constant(x.floatValue == y.floatValue)
147+
case NE => Constant(x.floatValue != y.floatValue)
148+
case LT => Constant(x.floatValue < y.floatValue)
149+
case GT => Constant(x.floatValue > y.floatValue)
150+
case LE => Constant(x.floatValue <= y.floatValue)
151+
case GE => Constant(x.floatValue >= y.floatValue)
152+
case ADD => Constant(x.floatValue + y.floatValue)
153+
case SUB => Constant(x.floatValue - y.floatValue)
154+
case MUL => Constant(x.floatValue * y.floatValue)
155+
case DIV => Constant(x.floatValue / y.floatValue)
156+
case MOD => Constant(x.floatValue % y.floatValue)
155157
case _ => null
156-
def foldDoubleOp(op: Name, x: Constant, y: Constant): Constant | Null = op match
157-
case nme.EQ => Constant(x.doubleValue == y.doubleValue)
158-
case nme.NE => Constant(x.doubleValue != y.doubleValue)
159-
case nme.LT => Constant(x.doubleValue < y.doubleValue)
160-
case nme.GT => Constant(x.doubleValue > y.doubleValue)
161-
case nme.LE => Constant(x.doubleValue <= y.doubleValue)
162-
case nme.GE => Constant(x.doubleValue >= y.doubleValue)
163-
case nme.ADD => Constant(x.doubleValue + y.doubleValue)
164-
case nme.SUB => Constant(x.doubleValue - y.doubleValue)
165-
case nme.MUL => Constant(x.doubleValue * y.doubleValue)
166-
case nme.DIV => Constant(x.doubleValue / y.doubleValue)
167-
case nme.MOD => Constant(x.doubleValue % y.doubleValue)
158+
inline def foldDoubleOp: Constant | Null = op match
159+
case EQ => Constant(x.doubleValue == y.doubleValue)
160+
case NE => Constant(x.doubleValue != y.doubleValue)
161+
case LT => Constant(x.doubleValue < y.doubleValue)
162+
case GT => Constant(x.doubleValue > y.doubleValue)
163+
case LE => Constant(x.doubleValue <= y.doubleValue)
164+
case GE => Constant(x.doubleValue >= y.doubleValue)
165+
case ADD => Constant(x.doubleValue + y.doubleValue)
166+
case SUB => Constant(x.doubleValue - y.doubleValue)
167+
case MUL => Constant(x.doubleValue * y.doubleValue)
168+
case DIV => Constant(x.doubleValue / y.doubleValue)
169+
case MOD => Constant(x.doubleValue % y.doubleValue)
168170
case _ => null
169-
def foldStringOp(op: Name, x: Constant, y: Constant): Constant | Null = op match
170-
case nme.ADD => Constant(x.stringValue + y.stringValue)
171-
case nme.EQ => Constant(x.stringValue == y.stringValue)
172-
case nme.NE => Constant(x.stringValue != y.stringValue)
171+
inline def foldStringOp: Constant | Null = op match
172+
case ADD => Constant(x.stringValue + y.stringValue)
173+
case EQ => Constant(x.stringValue == y.stringValue)
174+
case NE => Constant(x.stringValue != y.stringValue)
173175
case _ => null
174-
def foldNullOp(op: Name, x: Constant, y: Constant): Constant | Null =
176+
inline def foldNullOp: Constant | Null =
175177
assert(x.tag == NullTag || y.tag == NullTag)
176178
op match
177-
case nme.EQ => Constant(x.tag == y.tag)
178-
case nme.NE => Constant(x.tag != y.tag)
179+
case EQ => Constant(x.tag == y.tag)
180+
case NE => Constant(x.tag != y.tag)
179181
case _ => null
180182

181183
// begin foldBinop
@@ -186,13 +188,13 @@ object ConstFold:
186188
else NoTag
187189

188190
try optag match
189-
case BooleanTag => foldBooleanOp(op, x, y)
190-
case ByteTag | ShortTag | CharTag | IntTag => foldSubrangeOp(op, x, y)
191-
case LongTag => foldLongOp(op, x, y)
192-
case FloatTag => foldFloatOp(op, x, y)
193-
case DoubleTag => foldDoubleOp(op, x, y)
194-
case StringTag => foldStringOp(op, x, y)
195-
case NullTag => foldNullOp(op, x, y)
191+
case BooleanTag => foldBooleanOp
192+
case ByteTag | ShortTag | CharTag | IntTag => foldSubrangeOp
193+
case LongTag => foldLongOp
194+
case FloatTag => foldFloatOp
195+
case DoubleTag => foldDoubleOp
196+
case StringTag => foldStringOp
197+
case NullTag => foldNullOp
196198
case _ => null
197199
catch case ex: ArithmeticException => null // the code will crash at runtime,
198200
// but that is better than the

0 commit comments

Comments
 (0)