Skip to content

Commit 1a7dc1e

Browse files
committed
Move typeToTypeKind
1 parent 91de509 commit 1a7dc1e

File tree

2 files changed

+85
-84
lines changed

2 files changed

+85
-84
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeHelpers.scala

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ import dotty.tools.dotc.ast.tpd
1313
import dotty.tools.dotc.ast.Trees
1414
import dotty.tools.dotc.core.Annotations.Annotation
1515
import dotty.tools.dotc.core.Constants._
16-
import dotty.tools.dotc.core.Symbols._
17-
import dotty.tools.dotc.core.StdNames._
1816
import dotty.tools.dotc.core.Decorators._
1917
import dotty.tools.dotc.core.Flags
2018
import dotty.tools.dotc.core.Names.Name
2119
import dotty.tools.dotc.core.NameKinds.ExpandedName
20+
import dotty.tools.dotc.core.Symbols._
21+
import dotty.tools.dotc.core.StdNames._
22+
import dotty.tools.dotc.core.Types
2223

2324
/*
2425
* Traits encapsulating functionality to convert Scala AST Trees into ASM ClassNodes.
@@ -697,6 +698,88 @@ trait BCodeHelpers extends BCodeIdiomatic with BytecodeWriters {
697698
}
698699

699700
} // end of trait JAndroidBuilder
701+
702+
/**
703+
* This method returns the BType for a type reference, for example a parameter type.
704+
*
705+
* If the result is a ClassBType for a nested class, it is added to the innerClassBufferASM.
706+
*
707+
* If `t` references a class, toTypeKind ensures that the class is not an implementation class.
708+
* See also comment on getClassBTypeAndRegisterInnerClass, which is invoked for implementation
709+
* classes.
710+
*/
711+
private def typeToTypeKind(tp: Type)(ct: BCodeHelpers)(storage: ct.BCInnerClassGen): ct.bTypes.BType = {
712+
import ct.bTypes._
713+
val defn = ctx.definitions
714+
import coreBTypes._
715+
import Types._
716+
/**
717+
* Primitive types are represented as TypeRefs to the class symbol of, for example, scala.Int.
718+
* The `primitiveTypeMap` maps those class symbols to the corresponding PrimitiveBType.
719+
*/
720+
def primitiveOrClassToBType(sym: Symbol): BType = {
721+
assert(sym.isClass, sym)
722+
assert(sym != defn.ArrayClass || ctx.compilationUnit.source.file.name == "Array.scala", sym)
723+
primitiveTypeMap.getOrElse(sym.asInstanceOf[ct.bTypes.coreBTypes.bTypes.int.Symbol],
724+
storage.getClassBTypeAndRegisterInnerClass(sym.asInstanceOf[ct.int.Symbol])).asInstanceOf[BType]
725+
}
726+
727+
/**
728+
* When compiling Array.scala, the type parameter T is not erased and shows up in method
729+
* signatures, e.g. `def apply(i: Int): T`. A TyperRef to T is replaced by ObjectReference.
730+
*/
731+
def nonClassTypeRefToBType(sym: Symbol): ClassBType = {
732+
assert(sym.isType && ctx.compilationUnit.source.file.name == "Array.scala", sym)
733+
ObjectReference.asInstanceOf[ct.bTypes.ClassBType]
734+
}
735+
736+
tp.widenDealias match {
737+
case JavaArrayType(el) =>ArrayBType(typeToTypeKind(el)(ct)(storage)) // Array type such as Array[Int] (kept by erasure)
738+
case t: TypeRef =>
739+
t.info match {
740+
741+
case _ =>
742+
if (!t.symbol.isClass) nonClassTypeRefToBType(t.symbol) // See comment on nonClassTypeRefToBType
743+
else primitiveOrClassToBType(t.symbol) // Common reference to a type such as scala.Int or java.lang.String
744+
}
745+
case Types.ClassInfo(_, sym, _, _, _) => primitiveOrClassToBType(sym) // We get here, for example, for genLoadModule, which invokes toTypeKind(moduleClassSymbol.info)
746+
747+
/* AnnotatedType should (probably) be eliminated by erasure. However we know it happens for
748+
* meta-annotated annotations (@(ann @getter) val x = 0), so we don't emit a warning.
749+
* The type in the AnnotationInfo is an AnnotatedTpe. Tested in jvm/annotations.scala.
750+
*/
751+
case a @ AnnotatedType(t, _) =>
752+
ctx.debuglog(s"typeKind of annotated type $a")
753+
typeToTypeKind(t)(ct)(storage)
754+
755+
/* ExistentialType should (probably) be eliminated by erasure. We know they get here for
756+
* classOf constants:
757+
* class C[T]
758+
* class T { final val k = classOf[C[_]] }
759+
*/
760+
/* case e @ ExistentialType(_, t) =>
761+
debuglog(s"typeKind of existential type $e")
762+
t.toTypeKind(ctx)(storage)*/
763+
764+
/* The cases below should probably never occur. They are kept for now to avoid introducing
765+
* new compiler crashes, but we added a warning. The compiler / library bootstrap and the
766+
* test suite don't produce any warning.
767+
*/
768+
769+
case tp =>
770+
ctx.warning(
771+
s"an unexpected type representation reached the compiler backend while compiling ${ctx.compilationUnit}: $tp. " +
772+
"If possible, please file a bug on https://github.com/lampepfl/dotty/issues")
773+
774+
tp match {
775+
case tp: ThisType if tp.cls == defn.ArrayClass => ObjectReference.asInstanceOf[ct.bTypes.ClassBType] // was introduced in 9b17332f11 to fix SI-999, but this code is not reached in its test, or any other test
776+
case tp: ThisType => storage.getClassBTypeAndRegisterInnerClass(tp.cls.asInstanceOf[ct.int.Symbol])
777+
// case t: SingletonType => primitiveOrClassToBType(t.classSymbol)
778+
case t: SingletonType => typeToTypeKind(t.underlying)(ct)(storage)
779+
case t: RefinedType => typeToTypeKind(t.parent)(ct)(storage) //parents.map(_.toTypeKind(ct)(storage).asClassBType).reduceLeft((a, b) => a.jvmWiseLUB(b))
780+
}
781+
}
782+
}
700783
}
701784

702785
object BCodeHelpers {

compiler/src/dotty/tools/backend/jvm/DottyBackendInterface.scala

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -382,88 +382,6 @@ class DottyBackendInterface(val outputDirectory: AbstractFile, val superCallsMap
382382
buffer.toList
383383
}
384384

385-
/**
386-
* This method returns the BType for a type reference, for example a parameter type.
387-
*
388-
* If the result is a ClassBType for a nested class, it is added to the innerClassBufferASM.
389-
*
390-
* If `t` references a class, toTypeKind ensures that the class is not an implementation class.
391-
* See also comment on getClassBTypeAndRegisterInnerClass, which is invoked for implementation
392-
* classes.
393-
*/
394-
def typeToTypeKind(tp: Type)(ct: BCodeHelpers)(storage: ct.BCInnerClassGen): ct.bTypes.BType = {
395-
import ct.bTypes._
396-
val defn = ctx.definitions
397-
import coreBTypes._
398-
import Types._
399-
/**
400-
* Primitive types are represented as TypeRefs to the class symbol of, for example, scala.Int.
401-
* The `primitiveTypeMap` maps those class symbols to the corresponding PrimitiveBType.
402-
*/
403-
def primitiveOrClassToBType(sym: Symbol): BType = {
404-
assert(sym.isClass, sym)
405-
assert(sym != defn.ArrayClass || ctx.compilationUnit.source.file.name == "Array.scala", sym)
406-
primitiveTypeMap.getOrElse(sym.asInstanceOf[ct.bTypes.coreBTypes.bTypes.int.Symbol],
407-
storage.getClassBTypeAndRegisterInnerClass(sym.asInstanceOf[ct.int.Symbol])).asInstanceOf[BType]
408-
}
409-
410-
/**
411-
* When compiling Array.scala, the type parameter T is not erased and shows up in method
412-
* signatures, e.g. `def apply(i: Int): T`. A TyperRef to T is replaced by ObjectReference.
413-
*/
414-
def nonClassTypeRefToBType(sym: Symbol): ClassBType = {
415-
assert(sym.isType && ctx.compilationUnit.source.file.name == "Array.scala", sym)
416-
ObjectReference.asInstanceOf[ct.bTypes.ClassBType]
417-
}
418-
419-
tp.widenDealias match {
420-
case JavaArrayType(el) =>ArrayBType(typeToTypeKind(el)(ct)(storage)) // Array type such as Array[Int] (kept by erasure)
421-
case t: TypeRef =>
422-
t.info match {
423-
424-
case _ =>
425-
if (!t.symbol.isClass) nonClassTypeRefToBType(t.symbol) // See comment on nonClassTypeRefToBType
426-
else primitiveOrClassToBType(t.symbol) // Common reference to a type such as scala.Int or java.lang.String
427-
}
428-
case Types.ClassInfo(_, sym, _, _, _) => primitiveOrClassToBType(sym) // We get here, for example, for genLoadModule, which invokes toTypeKind(moduleClassSymbol.info)
429-
430-
/* AnnotatedType should (probably) be eliminated by erasure. However we know it happens for
431-
* meta-annotated annotations (@(ann @getter) val x = 0), so we don't emit a warning.
432-
* The type in the AnnotationInfo is an AnnotatedTpe. Tested in jvm/annotations.scala.
433-
*/
434-
case a @ AnnotatedType(t, _) =>
435-
ctx.debuglog(s"typeKind of annotated type $a")
436-
typeToTypeKind(t)(ct)(storage)
437-
438-
/* ExistentialType should (probably) be eliminated by erasure. We know they get here for
439-
* classOf constants:
440-
* class C[T]
441-
* class T { final val k = classOf[C[_]] }
442-
*/
443-
/* case e @ ExistentialType(_, t) =>
444-
debuglog(s"typeKind of existential type $e")
445-
t.toTypeKind(ctx)(storage)*/
446-
447-
/* The cases below should probably never occur. They are kept for now to avoid introducing
448-
* new compiler crashes, but we added a warning. The compiler / library bootstrap and the
449-
* test suite don't produce any warning.
450-
*/
451-
452-
case tp =>
453-
ctx.warning(
454-
s"an unexpected type representation reached the compiler backend while compiling ${ctx.compilationUnit}: $tp. " +
455-
"If possible, please file a bug on https://github.com/lampepfl/dotty/issues")
456-
457-
tp match {
458-
case tp: ThisType if tp.cls == defn.ArrayClass => ObjectReference.asInstanceOf[ct.bTypes.ClassBType] // was introduced in 9b17332f11 to fix SI-999, but this code is not reached in its test, or any other test
459-
case tp: ThisType => storage.getClassBTypeAndRegisterInnerClass(tp.cls.asInstanceOf[ct.int.Symbol])
460-
// case t: SingletonType => primitiveOrClassToBType(t.classSymbol)
461-
case t: SingletonType => typeToTypeKind(t.underlying)(ct)(storage)
462-
case t: RefinedType => typeToTypeKind(t.parent)(ct)(storage) //parents.map(_.toTypeKind(ct)(storage).asClassBType).reduceLeft((a, b) => a.jvmWiseLUB(b))
463-
}
464-
}
465-
}
466-
467385
object SelectBI extends DeconstructorCommon[tpd.Tree] {
468386

469387
var desugared: tpd.Select = null

0 commit comments

Comments
 (0)