Skip to content

Commit f27bc4d

Browse files
committed
Rewrite vc support in Definitions; add def deepUnderlyingOfValueClass method
1 parent 673a38d commit f27bc4d

File tree

2 files changed

+45
-24
lines changed

2 files changed

+45
-24
lines changed

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

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -775,39 +775,49 @@ class Definitions {
775775
/** The type of the boxed class corresponding to primitive value type `tp`. */
776776
def boxedType(tp: Type)(implicit ctx: Context): TypeRef = boxedTypes(scalaClassName(tp))
777777

778-
lazy val vcPrototypeClassKeys: collection.Set[Symbol] =
779-
defn.ScalaNumericValueClasses() + defn.BooleanClass + defn.ObjectClass
778+
lazy val vcPrototypeTypeKeys: collection.Set[TypeRef] =
779+
defn.ScalaNumericValueTypes + defn.BooleanType + defn.ObjectType
780780

781-
lazy val vcPrototype: Map[Symbol, Symbol] =
782-
vcPrototypeClassKeys.map(vc => vc -> ctx.requiredClass(s"dotty.runtime.vc.VC${vc.name}Prototype")).toMap
783-
lazy val vcCompanion: Map[Symbol, Symbol] =
784-
vcPrototypeClassKeys.map(vc => vc -> ctx.requiredClass(s"dotty.runtime.vc.VC${vc.name}Companion")).toMap
785-
lazy val vcArray: Map[Symbol, Symbol] =
786-
vcPrototypeClassKeys.map(vc => vc -> ctx.requiredClass(s"dotty.runtime.vc.VC${vc.name}Array")).toMap
781+
lazy val vcPrototypeTypes: Map[TypeName, TypeRef] =
782+
vcPrototypeTypeKeys.map(vc => vc.name -> ctx.requiredClassRef(s"dotty.runtime.vc.VC${vc.name}Prototype")).toMap
783+
lazy val vcCompanionTypes: Map[TypeName, TypeRef] =
784+
vcPrototypeTypeKeys.map(vc => vc.name -> ctx.requiredClassRef(s"dotty.runtime.vc.VC${vc.name}Companion")).toMap
785+
lazy val vcArrayTypes: Map[TypeName, TypeRef] =
786+
vcPrototypeTypeKeys.map(vc => vc.name -> ctx.requiredClassRef(s"dotty.runtime.vc.VC${vc.name}Array")).toMap
787787

788-
lazy val VCArrayPrototypeClass = ctx.requiredClass(s"dotty.runtime.vc.VCArrayPrototype")
788+
//TODO: rewrite
789+
def vcPrototypeValues(implicit ctx: Context): Set[Symbol] = vcPrototypeTypes.values.toSet map {tr: TypeRef => tr.classSymbol}
789790

790-
lazy val vcPrototypeValues = vcPrototype.values.toSet
791-
792-
def VCArrayPrototypeType = VCArrayPrototypeClass.typeRef
791+
lazy val VCArrayPrototypeType = ctx.requiredClassRef(s"dotty.runtime.vc.VCArrayPrototype")
792+
def VCArrayPrototypeClass(implicit ctx: Context) = VCArrayPrototypeType.classSymbol
793793

794794
lazy val VCPrototypeType = ctx.requiredClassRef(s"dotty.runtime.vc.VCPrototype")
795-
def VCPrototypeClass = VCPrototypeType.classSymbol.asClass
795+
def VCPrototypeClass(implicit ctx: Context) = VCPrototypeType.classSymbol
796796

797-
def vcPrototypeOf(vc: ClassDenotation) = {
798-
val underlying = ValueClasses.valueClassUnbox(vc).info.classSymbol
799-
vcPrototype.getOrElse(underlying, vcPrototype(defn.ObjectClass))
797+
def vcRepresentationOf (vcReprs: Map[TypeName, TypeRef], vc: ClassDenotation)(
798+
undFn: ClassDenotation => Symbol)(implicit ctx: Context): Symbol = {
799+
val underlying = undFn(vc)
800+
(if (underlying.isPrimitiveValueClass && vcReprs.isDefinedAt(underlying.name.asTypeName))
801+
vcReprs(underlying.name.asTypeName) else vcReprs(defn.ObjectType.name)).classSymbol
800802
}
801803

802-
def vcCompanionOf(vc: ClassDenotation) = {
803-
val underlying = ValueClasses.valueClassUnbox(vc).info.classSymbol
804-
vcCompanion.getOrElse(underlying, vcCompanion(defn.ObjectClass))
805-
}
804+
def vcPrototypeOf(vc: ClassDenotation)(implicit ctx: Context) = vcRepresentationOf(vcPrototypeTypes, vc)(
805+
ValueClasses.underlyingOfValueClass(_).classSymbol)
806806

807-
def vcArrayOf(vc: ClassDenotation) = {
808-
val underlying = ValueClasses.valueClassUnbox(vc).info.classSymbol
809-
vcArray.getOrElse(underlying, vcArray(defn.ObjectClass))
810-
}
807+
def vcDeepPrototypeOf(vc: ClassDenotation)(implicit ctx: Context) = vcRepresentationOf(vcPrototypeTypes, vc)(
808+
ValueClasses.deepUnderlyingOfValueClass(_).classSymbol)
809+
810+
def vcCompanionOf(vc: ClassDenotation)(implicit ctx: Context) = vcRepresentationOf(vcCompanionTypes, vc)(
811+
ValueClasses.underlyingOfValueClass(_).classSymbol)
812+
813+
def vcDeepCompanionOf(vc: ClassDenotation)(implicit ctx: Context) = vcRepresentationOf(vcCompanionTypes, vc)(
814+
ValueClasses.deepUnderlyingOfValueClass(_).classSymbol)
815+
816+
def vcArrayOf(vc: ClassDenotation)(implicit ctx: Context) = vcRepresentationOf(vcArrayTypes, vc)(
817+
ValueClasses.underlyingOfValueClass(_).classSymbol)
818+
819+
def vcDeepArrayOf(vc: ClassDenotation)(implicit ctx: Context) = vcRepresentationOf(vcArrayTypes, vc)(
820+
ValueClasses.deepUnderlyingOfValueClass(_).classSymbol)
811821

812822
def wrapArrayMethodName(elemtp: Type): TermName = {
813823
val cls = elemtp.classSymbol

src/dotty/tools/dotc/transform/ValueClasses.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ object ValueClasses {
5656
def underlyingOfValueClass(d: ClassDenotation)(implicit ctx: Context): Type =
5757
valueClassUnbox(d).info.resultType
5858

59+
def deepUnderlyingOfValueClass(d: ClassDenotation)(implicit ctx: Context): Type = {
60+
import TypeErasure.ErasedValueType
61+
val und = underlyingOfValueClass(d)
62+
und match {
63+
case _ if isDerivedValueClass(und.classSymbol) => deepUnderlyingOfValueClass(und.classSymbol.asClass)
64+
case ErasedValueType(t, und) =>
65+
deepUnderlyingOfValueClass(t.classSymbol.asClass)
66+
case _ => und
67+
}
68+
}
69+
5970
def isVCCompanion[T](ct: ClassTag[T]) = ct match {
6071
case _: VCIntCompanion[_] | _: VCShortCompanion[_] |
6172
_: VCLongCompanion[_] | _: VCByteCompanion[_] |

0 commit comments

Comments
 (0)