Skip to content

Commit ec0a28f

Browse files
committed
Move type to JVM tag conversion to Definitions
1 parent a112315 commit ec0a28f

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ class Definitions {
392392
def ArrayModule(implicit ctx: Context) = ArrayModuleType.symbol.moduleClass.asClass
393393

394394

395-
lazy val UnitType: TypeRef = valueTypeRef("scala.Unit", BoxedUnitType, java.lang.Void.TYPE, UnitEnc)
395+
lazy val UnitType: TypeRef = valueTypeRef("scala.Unit", BoxedUnitType, java.lang.Void.TYPE, UnitEnc, nme.specializedTypeNames.Void)
396396
def UnitClass(implicit ctx: Context) = UnitType.symbol.asClass
397-
lazy val BooleanType = valueTypeRef("scala.Boolean", BoxedBooleanType, java.lang.Boolean.TYPE, BooleanEnc)
397+
lazy val BooleanType = valueTypeRef("scala.Boolean", BoxedBooleanType, java.lang.Boolean.TYPE, BooleanEnc, nme.specializedTypeNames.Boolean)
398398
def BooleanClass(implicit ctx: Context) = BooleanType.symbol.asClass
399399
lazy val Boolean_notR = BooleanClass.requiredMethodRef(nme.UNARY_!)
400400
def Boolean_! = Boolean_notR.symbol
@@ -413,13 +413,13 @@ class Definitions {
413413
})
414414
def Boolean_!= = Boolean_neqeqR.symbol
415415

416-
lazy val ByteType: TypeRef = valueTypeRef("scala.Byte", BoxedByteType, java.lang.Byte.TYPE, ByteEnc)
416+
lazy val ByteType: TypeRef = valueTypeRef("scala.Byte", BoxedByteType, java.lang.Byte.TYPE, ByteEnc, nme.specializedTypeNames.Byte)
417417
def ByteClass(implicit ctx: Context) = ByteType.symbol.asClass
418-
lazy val ShortType: TypeRef = valueTypeRef("scala.Short", BoxedShortType, java.lang.Short.TYPE, ShortEnc)
418+
lazy val ShortType: TypeRef = valueTypeRef("scala.Short", BoxedShortType, java.lang.Short.TYPE, ShortEnc, nme.specializedTypeNames.Short)
419419
def ShortClass(implicit ctx: Context) = ShortType.symbol.asClass
420-
lazy val CharType: TypeRef = valueTypeRef("scala.Char", BoxedCharType, java.lang.Character.TYPE, CharEnc)
420+
lazy val CharType: TypeRef = valueTypeRef("scala.Char", BoxedCharType, java.lang.Character.TYPE, CharEnc, nme.specializedTypeNames.Char)
421421
def CharClass(implicit ctx: Context) = CharType.symbol.asClass
422-
lazy val IntType: TypeRef = valueTypeRef("scala.Int", BoxedIntType, java.lang.Integer.TYPE, IntEnc)
422+
lazy val IntType: TypeRef = valueTypeRef("scala.Int", BoxedIntType, java.lang.Integer.TYPE, IntEnc, nme.specializedTypeNames.Int)
423423
def IntClass(implicit ctx: Context) = IntType.symbol.asClass
424424
lazy val Int_minusR = IntClass.requiredMethodRef(nme.MINUS, List(IntType))
425425
def Int_- = Int_minusR.symbol
@@ -435,7 +435,7 @@ class Definitions {
435435
def Int_>= = Int_geR.symbol
436436
lazy val Int_leR = IntClass.requiredMethodRef(nme.LE, List(IntType))
437437
def Int_<= = Int_leR.symbol
438-
lazy val LongType: TypeRef = valueTypeRef("scala.Long", BoxedLongType, java.lang.Long.TYPE, LongEnc)
438+
lazy val LongType: TypeRef = valueTypeRef("scala.Long", BoxedLongType, java.lang.Long.TYPE, LongEnc, nme.specializedTypeNames.Long)
439439
def LongClass(implicit ctx: Context) = LongType.symbol.asClass
440440
lazy val Long_XOR_Long = LongType.member(nme.XOR).requiredSymbol(
441441
x => (x is Method) && (x.info.firstParamTypes.head isRef defn.LongClass)
@@ -450,9 +450,9 @@ class Definitions {
450450
lazy val Long_divR = LongClass.requiredMethodRef(nme.DIV, List(LongType))
451451
def Long_/ = Long_divR.symbol
452452

453-
lazy val FloatType: TypeRef = valueTypeRef("scala.Float", BoxedFloatType, java.lang.Float.TYPE, FloatEnc)
453+
lazy val FloatType: TypeRef = valueTypeRef("scala.Float", BoxedFloatType, java.lang.Float.TYPE, FloatEnc, nme.specializedTypeNames.Float)
454454
def FloatClass(implicit ctx: Context) = FloatType.symbol.asClass
455-
lazy val DoubleType: TypeRef = valueTypeRef("scala.Double", BoxedDoubleType, java.lang.Double.TYPE, DoubleEnc)
455+
lazy val DoubleType: TypeRef = valueTypeRef("scala.Double", BoxedDoubleType, java.lang.Double.TYPE, DoubleEnc, nme.specializedTypeNames.Double)
456456
def DoubleClass(implicit ctx: Context) = DoubleType.symbol.asClass
457457

458458
lazy val BoxedUnitType: TypeRef = ctx.requiredClassRef("scala.runtime.BoxedUnit")
@@ -979,15 +979,17 @@ class Definitions {
979979

980980
private val boxedTypes = mutable.Map[TypeName, TypeRef]()
981981
private val valueTypeEnc = mutable.Map[TypeName, PrimitiveClassEnc]()
982+
private val typeTags = mutable.Map[TypeName, Name]().withDefaultValue(nme.specializedTypeNames.Object)
982983

983984
// private val unboxedTypeRef = mutable.Map[TypeName, TypeRef]()
984985
// private val javaTypeToValueTypeRef = mutable.Map[Class[_], TypeRef]()
985986
// private val valueTypeNameToJavaType = mutable.Map[TypeName, Class[_]]()
986987

987-
private def valueTypeRef(name: String, boxed: TypeRef, jtype: Class[_], enc: Int): TypeRef = {
988+
private def valueTypeRef(name: String, boxed: TypeRef, jtype: Class[_], enc: Int, tag: Name): TypeRef = {
988989
val vcls = ctx.requiredClassRef(name)
989990
boxedTypes(vcls.name) = boxed
990991
valueTypeEnc(vcls.name) = enc
992+
typeTags(vcls.name) = tag
991993
// unboxedTypeRef(boxed.name) = vcls
992994
// javaTypeToValueTypeRef(jtype) = vcls
993995
// valueTypeNameToJavaType(vcls.name) = jtype
@@ -997,6 +999,9 @@ class Definitions {
997999
/** The type of the boxed class corresponding to primitive value type `tp`. */
9981000
def boxedType(tp: Type)(implicit ctx: Context): TypeRef = boxedTypes(scalaClassName(tp))
9991001

1002+
/** The JVM tag for `tp` if it's a primitive, `java.lang.Object` otherwise. */
1003+
def typeTag(tp: Type)(implicit ctx: Context): Name = typeTags(scalaClassName(tp))
1004+
10001005
type PrimitiveClassEnc = Int
10011006

10021007
val ByteEnc = 2

compiler/src/dotty/tools/dotc/core/NameOps.scala

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,12 @@ object NameOps {
231231
case nme.clone_ => nme.clone_
232232
}
233233

234-
private def typeToTag(tp: Types.Type)(implicit ctx: Context): Name =
235-
tp.classSymbol match {
236-
case t if t eq defn.IntClass => nme.specializedTypeNames.Int
237-
case t if t eq defn.BooleanClass => nme.specializedTypeNames.Boolean
238-
case t if t eq defn.ByteClass => nme.specializedTypeNames.Byte
239-
case t if t eq defn.LongClass => nme.specializedTypeNames.Long
240-
case t if t eq defn.ShortClass => nme.specializedTypeNames.Short
241-
case t if t eq defn.FloatClass => nme.specializedTypeNames.Float
242-
case t if t eq defn.UnitClass => nme.specializedTypeNames.Void
243-
case t if t eq defn.DoubleClass => nme.specializedTypeNames.Double
244-
case t if t eq defn.CharClass => nme.specializedTypeNames.Char
245-
case _ => nme.specializedTypeNames.Object
246-
}
247-
248234
/** This method is to be used on **type parameters** from a class, since
249235
* this method does sorting based on their names
250236
*/
251237
def specializedFor(classTargs: List[Types.Type], classTargsNames: List[Name], methodTargs: List[Types.Type], methodTarsNames: List[Name])(implicit ctx: Context): Name = {
252-
val methodTags: Seq[Name] = (methodTargs zip methodTarsNames).sortBy(_._2).map(x => typeToTag(x._1))
253-
val classTags: Seq[Name] = (classTargs zip classTargsNames).sortBy(_._2).map(x => typeToTag(x._1))
238+
val methodTags: Seq[Name] = (methodTargs zip methodTarsNames).sortBy(_._2).map(x => defn.typeTag(x._1))
239+
val classTags: Seq[Name] = (classTargs zip classTargsNames).sortBy(_._2).map(x => defn.typeTag(x._1))
254240

255241
name.likeSpaced(name ++ nme.specializedTypeNames.prefix ++
256242
methodTags.fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.separator ++
@@ -265,8 +251,8 @@ object NameOps {
265251
*/
266252
def specializedFunction(ret: Types.Type, args: List[Types.Type])(implicit ctx: Context): Name =
267253
name ++ nme.specializedTypeNames.prefix ++
268-
nme.specializedTypeNames.separator ++ typeToTag(ret) ++
269-
args.map(typeToTag).fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix
254+
nme.specializedTypeNames.separator ++ defn.typeTag(ret) ++
255+
args.map(defn.typeTag).fold(nme.EMPTY)(_ ++ _) ++ nme.specializedTypeNames.suffix
270256

271257
/** If name length exceeds allowable limit, replace part of it by hash */
272258
def compactified(implicit ctx: Context): TermName = termName(compactify(name.toString))

0 commit comments

Comments
 (0)