@@ -4174,37 +4174,76 @@ object Types {
4174
4174
4175
4175
def tryCompiletimeConstantFold (using Context ): Type = tycon match {
4176
4176
case tycon : TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) =>
4177
- def constValue (tp : Type ): Option [Any ] = tp.dealias match {
4177
+ extension (tp : Type ) def fixForEvaluation : Type =
4178
+ tp.normalized.dealias match {
4179
+ case tp : TermRef => tp.underlying
4180
+ case tp => tp
4181
+ }
4182
+
4183
+ def constValue (tp : Type ): Option [Any ] = tp.fixForEvaluation match {
4178
4184
case ConstantType (Constant (n)) => Some (n)
4179
4185
case _ => None
4180
4186
}
4181
4187
4182
- def boolValue (tp : Type ): Option [Boolean ] = tp.dealias match {
4188
+ def boolValue (tp : Type ): Option [Boolean ] = tp.fixForEvaluation match {
4183
4189
case ConstantType (Constant (n : Boolean )) => Some (n)
4184
4190
case _ => None
4185
4191
}
4186
4192
4187
- def intValue (tp : Type ): Option [Int ] = tp.dealias match {
4193
+ def intValue (tp : Type ): Option [Int ] = tp.fixForEvaluation match {
4188
4194
case ConstantType (Constant (n : Int )) => Some (n)
4189
4195
case _ => None
4190
4196
}
4191
4197
4192
- def stringValue (tp : Type ): Option [String ] = tp.dealias match {
4193
- case ConstantType (Constant (n : String )) => Some (n)
4198
+ def longValue (tp : Type ): Option [Long ] = tp.fixForEvaluation match {
4199
+ case ConstantType (Constant (n : Long )) => Some (n)
4200
+ case _ => None
4201
+ }
4202
+
4203
+ def floatValue (tp : Type ): Option [Float ] = tp.fixForEvaluation match {
4204
+ case ConstantType (Constant (n : Float )) => Some (n)
4205
+ case _ => None
4206
+ }
4207
+
4208
+ def doubleValue (tp : Type ): Option [Double ] = tp.fixForEvaluation match {
4209
+ case ConstantType (Constant (n : Double )) => Some (n)
4194
4210
case _ => None
4195
4211
}
4196
4212
4213
+ def stringValue (tp : Type ): Option [String ] = tp.fixForEvaluation match {
4214
+ case ConstantType (Constant (n : String )) => Some (n)
4215
+ case _ => None
4216
+ }
4217
+ def isConst : Option [Type ] = args.head.fixForEvaluation match {
4218
+ case ConstantType (_) => Some (ConstantType (Constant (true )))
4219
+ case _ => Some (ConstantType (Constant (false )))
4220
+ }
4197
4221
def natValue (tp : Type ): Option [Int ] = intValue(tp).filter(n => n >= 0 && n < Int .MaxValue )
4198
4222
4199
4223
def constantFold1 [T ](extractor : Type => Option [T ], op : T => Any ): Option [Type ] =
4200
- extractor(args.head.normalized ).map(a => ConstantType (Constant (op(a))))
4224
+ extractor(args.head).map(a => ConstantType (Constant (op(a))))
4201
4225
4202
4226
def constantFold2 [T ](extractor : Type => Option [T ], op : (T , T ) => Any ): Option [Type ] =
4227
+ constantFold2AB(extractor, extractor, op)
4228
+
4229
+ def constantFold2AB [TA , TB ](extractorA : Type => Option [TA ], extractorB : Type => Option [TB ], op : (TA , TB ) => Any ): Option [Type ] =
4203
4230
for {
4204
- a <- extractor (args.head.normalized )
4205
- b <- extractor (args.tail.head.normalized )
4231
+ a <- extractorA (args.head)
4232
+ b <- extractorB (args.last )
4206
4233
} yield ConstantType (Constant (op(a, b)))
4207
4234
4235
+ def constantFold3 [TA , TB , TC ](
4236
+ extractorA : Type => Option [TA ],
4237
+ extractorB : Type => Option [TB ],
4238
+ extractorC : Type => Option [TC ],
4239
+ op : (TA , TB , TC ) => Any
4240
+ ): Option [Type ] =
4241
+ for {
4242
+ a <- extractorA(args.head)
4243
+ b <- extractorB(args(1 ))
4244
+ c <- extractorC(args.last)
4245
+ } yield ConstantType (Constant (op(a, b, c)))
4246
+
4208
4247
trace(i " compiletime constant fold $this" , typr, show = true ) {
4209
4248
val name = tycon.symbol.name
4210
4249
val owner = tycon.symbol.owner
@@ -4216,10 +4255,13 @@ object Types {
4216
4255
} else if (owner == defn.CompiletimeOpsAnyModuleClass ) name match {
4217
4256
case tpnme.Equals if nArgs == 2 => constantFold2(constValue, _ == _)
4218
4257
case tpnme.NotEquals if nArgs == 2 => constantFold2(constValue, _ != _)
4258
+ case tpnme.ToString if nArgs == 1 => constantFold1(constValue, _.toString)
4259
+ case tpnme.IsConst if nArgs == 1 => isConst
4219
4260
case _ => None
4220
4261
} else if (owner == defn.CompiletimeOpsIntModuleClass ) name match {
4221
4262
case tpnme.Abs if nArgs == 1 => constantFold1(intValue, _.abs)
4222
4263
case tpnme.Negate if nArgs == 1 => constantFold1(intValue, x => - x)
4264
+ // ToString is deprecated for ops.int, and moved to ops.any
4223
4265
case tpnme.ToString if nArgs == 1 => constantFold1(intValue, _.toString)
4224
4266
case tpnme.Plus if nArgs == 2 => constantFold2(intValue, _ + _)
4225
4267
case tpnme.Minus if nArgs == 2 => constantFold2(intValue, _ - _)
@@ -4244,9 +4286,85 @@ object Types {
4244
4286
case tpnme.LSR if nArgs == 2 => constantFold2(intValue, _ >>> _)
4245
4287
case tpnme.Min if nArgs == 2 => constantFold2(intValue, _ min _)
4246
4288
case tpnme.Max if nArgs == 2 => constantFold2(intValue, _ max _)
4289
+ case tpnme.NumberOfLeadingZeros if nArgs == 1 => constantFold1(intValue, Integer .numberOfLeadingZeros(_))
4290
+ case tpnme.ToLong if nArgs == 1 => constantFold1(intValue, _.toLong)
4291
+ case tpnme.ToFloat if nArgs == 1 => constantFold1(intValue, _.toFloat)
4292
+ case tpnme.ToDouble if nArgs == 1 => constantFold1(intValue, _.toDouble)
4293
+ case _ => None
4294
+ } else if (owner == defn.CompiletimeOpsLongModuleClass ) name match {
4295
+ case tpnme.Abs if nArgs == 1 => constantFold1(longValue, _.abs)
4296
+ case tpnme.Negate if nArgs == 1 => constantFold1(longValue, x => - x)
4297
+ case tpnme.Plus if nArgs == 2 => constantFold2(longValue, _ + _)
4298
+ case tpnme.Minus if nArgs == 2 => constantFold2(longValue, _ - _)
4299
+ case tpnme.Times if nArgs == 2 => constantFold2(longValue, _ * _)
4300
+ case tpnme.Div if nArgs == 2 => constantFold2(longValue, {
4301
+ case (_, 0L ) => throw new TypeError (" Division by 0" )
4302
+ case (a, b) => a / b
4303
+ })
4304
+ case tpnme.Mod if nArgs == 2 => constantFold2(longValue, {
4305
+ case (_, 0L ) => throw new TypeError (" Modulo by 0" )
4306
+ case (a, b) => a % b
4307
+ })
4308
+ case tpnme.Lt if nArgs == 2 => constantFold2(longValue, _ < _)
4309
+ case tpnme.Gt if nArgs == 2 => constantFold2(longValue, _ > _)
4310
+ case tpnme.Ge if nArgs == 2 => constantFold2(longValue, _ >= _)
4311
+ case tpnme.Le if nArgs == 2 => constantFold2(longValue, _ <= _)
4312
+ case tpnme.Xor if nArgs == 2 => constantFold2(longValue, _ ^ _)
4313
+ case tpnme.BitwiseAnd if nArgs == 2 => constantFold2(longValue, _ & _)
4314
+ case tpnme.BitwiseOr if nArgs == 2 => constantFold2(longValue, _ | _)
4315
+ case tpnme.ASR if nArgs == 2 => constantFold2(longValue, _ >> _)
4316
+ case tpnme.LSL if nArgs == 2 => constantFold2(longValue, _ << _)
4317
+ case tpnme.LSR if nArgs == 2 => constantFold2(longValue, _ >>> _)
4318
+ case tpnme.Min if nArgs == 2 => constantFold2(longValue, _ min _)
4319
+ case tpnme.Max if nArgs == 2 => constantFold2(longValue, _ max _)
4320
+ case tpnme.NumberOfLeadingZeros if nArgs == 1 =>
4321
+ constantFold1(longValue, java.lang.Long .numberOfLeadingZeros(_))
4322
+ case tpnme.ToInt if nArgs == 1 => constantFold1(longValue, _.toInt)
4323
+ case tpnme.ToFloat if nArgs == 1 => constantFold1(longValue, _.toFloat)
4324
+ case tpnme.ToDouble if nArgs == 1 => constantFold1(longValue, _.toDouble)
4325
+ case _ => None
4326
+ } else if (owner == defn.CompiletimeOpsFloatModuleClass ) name match {
4327
+ case tpnme.Abs if nArgs == 1 => constantFold1(floatValue, _.abs)
4328
+ case tpnme.Negate if nArgs == 1 => constantFold1(floatValue, x => - x)
4329
+ case tpnme.Plus if nArgs == 2 => constantFold2(floatValue, _ + _)
4330
+ case tpnme.Minus if nArgs == 2 => constantFold2(floatValue, _ - _)
4331
+ case tpnme.Times if nArgs == 2 => constantFold2(floatValue, _ * _)
4332
+ case tpnme.Div if nArgs == 2 => constantFold2(floatValue, _ / _)
4333
+ case tpnme.Mod if nArgs == 2 => constantFold2(floatValue, _ % _)
4334
+ case tpnme.Lt if nArgs == 2 => constantFold2(floatValue, _ < _)
4335
+ case tpnme.Gt if nArgs == 2 => constantFold2(floatValue, _ > _)
4336
+ case tpnme.Ge if nArgs == 2 => constantFold2(floatValue, _ >= _)
4337
+ case tpnme.Le if nArgs == 2 => constantFold2(floatValue, _ <= _)
4338
+ case tpnme.Min if nArgs == 2 => constantFold2(floatValue, _ min _)
4339
+ case tpnme.Max if nArgs == 2 => constantFold2(floatValue, _ max _)
4340
+ case tpnme.ToInt if nArgs == 1 => constantFold1(floatValue, _.toInt)
4341
+ case tpnme.ToLong if nArgs == 1 => constantFold1(floatValue, _.toLong)
4342
+ case tpnme.ToDouble if nArgs == 1 => constantFold1(floatValue, _.toDouble)
4343
+ case _ => None
4344
+ } else if (owner == defn.CompiletimeOpsDoubleModuleClass ) name match {
4345
+ case tpnme.Abs if nArgs == 1 => constantFold1(doubleValue, _.abs)
4346
+ case tpnme.Negate if nArgs == 1 => constantFold1(doubleValue, x => - x)
4347
+ case tpnme.Plus if nArgs == 2 => constantFold2(doubleValue, _ + _)
4348
+ case tpnme.Minus if nArgs == 2 => constantFold2(doubleValue, _ - _)
4349
+ case tpnme.Times if nArgs == 2 => constantFold2(doubleValue, _ * _)
4350
+ case tpnme.Div if nArgs == 2 => constantFold2(doubleValue, _ / _)
4351
+ case tpnme.Mod if nArgs == 2 => constantFold2(doubleValue, _ % _)
4352
+ case tpnme.Lt if nArgs == 2 => constantFold2(doubleValue, _ < _)
4353
+ case tpnme.Gt if nArgs == 2 => constantFold2(doubleValue, _ > _)
4354
+ case tpnme.Ge if nArgs == 2 => constantFold2(doubleValue, _ >= _)
4355
+ case tpnme.Le if nArgs == 2 => constantFold2(doubleValue, _ <= _)
4356
+ case tpnme.Min if nArgs == 2 => constantFold2(doubleValue, _ min _)
4357
+ case tpnme.Max if nArgs == 2 => constantFold2(doubleValue, _ max _)
4358
+ case tpnme.ToInt if nArgs == 1 => constantFold1(doubleValue, _.toInt)
4359
+ case tpnme.ToLong if nArgs == 1 => constantFold1(doubleValue, _.toLong)
4360
+ case tpnme.ToFloat if nArgs == 1 => constantFold1(doubleValue, _.toFloat)
4247
4361
case _ => None
4248
4362
} else if (owner == defn.CompiletimeOpsStringModuleClass ) name match {
4249
4363
case tpnme.Plus if nArgs == 2 => constantFold2(stringValue, _ + _)
4364
+ case tpnme.Length if nArgs == 1 => constantFold1(stringValue, _.length)
4365
+ case tpnme.Matches if nArgs == 2 => constantFold2(stringValue, _ matches _)
4366
+ case tpnme.Substring if nArgs == 3 =>
4367
+ constantFold3(stringValue, intValue, intValue, (s, b, e) => s.substring(b, e))
4250
4368
case _ => None
4251
4369
} else if (owner == defn.CompiletimeOpsBooleanModuleClass ) name match {
4252
4370
case tpnme.Not if nArgs == 1 => constantFold1(boolValue, x => ! x)
0 commit comments