Skip to content

Commit 35f6e27

Browse files
committed
Implement TypeOrBounds in kernel
1 parent 5796d2f commit 35f6e27

File tree

5 files changed

+325
-481
lines changed

5 files changed

+325
-481
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package tastyreflect
33

44
import dotty.tools.dotc.ast.{Trees, tpd, untpd}
55
import dotty.tools.dotc.ast.tpd.TreeOps
6-
import dotty.tools.dotc.core.{Constants, NameKinds, Types}
6+
import dotty.tools.dotc.core.{Constants, NameKinds, Names, Types}
77
import dotty.tools.dotc.core.Flags._
88
import dotty.tools.dotc.core.StdNames.nme
99
import dotty.tools.dotc.core.quoted.PickledQuotes
@@ -957,13 +957,27 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
957957

958958
type NoPrefix = Types.NoPrefix.type
959959

960+
def isNoPrefix(x: TypeOrBounds)(implicit ctx: Context): Option[NoPrefix] =
961+
if (x == Types.NoPrefix) Some(Types.NoPrefix) else None
962+
960963
type TypeBounds = Types.TypeBounds
961964

965+
def isTypeBounds(x: TypeOrBounds)(implicit ctx: Context): Option[TypeBounds] = x match {
966+
case x: Types.TypeBounds => Some(x)
967+
case _ => None
968+
}
969+
962970
def TypeBounds_low(self: TypeBounds)(implicit ctx: Context): Type = self.lo
963971
def TypeBounds_hi(self: TypeBounds)(implicit ctx: Context): Type = self.hi
964972

965973
type Type = Types.Type
966974

975+
def isType(x: TypeOrBounds)(implicit ctx: Context): Option[Type] = x match {
976+
case x: TypeBounds => None
977+
case x if x == Types.NoPrefix => None
978+
case _ => Some(x)
979+
}
980+
967981
def `Type_=:=`(self: Type)(that: Type)(implicit ctx: Context): Boolean = self =:= that
968982

969983
def `Type_<:<`(self: Type)(that: Type)(implicit ctx: Context): Boolean = self <:< that
@@ -991,84 +1005,196 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
9911005

9921006
type ConstantType = Types.ConstantType
9931007

994-
def ConstantType_value(self: ConstantType)(implicit ctx: Context): Any = self.value
1008+
def isConstantType(tpe: TypeOrBounds)(implicit ctx: Context): Option[ConstantType] = tpe match {
1009+
case tpe: Types.ConstantType => Some(tpe)
1010+
case _ => None
1011+
}
1012+
1013+
def ConstantType_constant(self: ConstantType)(implicit ctx: Context): Constant = self.value
9951014

9961015
type SymRef = Types.NamedType
9971016

1017+
def isSymRef(tpe: TypeOrBounds)(implicit ctx: Context): Option[SymRef] = tpe match {
1018+
case tp: Types.NamedType =>
1019+
tp.designator match {
1020+
case sym: Symbol => Some(tp)
1021+
case _ => None
1022+
}
1023+
case _ => None
1024+
}
1025+
9981026
def SymRef_qualifier(self: SymRef)(implicit ctx: Context): TypeOrBounds = self.prefix
9991027

1028+
// TODO remove this method. May require splitting SymRef into TypeSymRef and TermSymRef
1029+
def isSymRef_unapply(tpe: TypeOrBounds)(implicit ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)] = tpe match {
1030+
case tpe: Types.NamedType =>
1031+
tpe.designator match {
1032+
case sym: Symbol => Some((sym, tpe.prefix))
1033+
case _ => None
1034+
}
1035+
case _ => None
1036+
}
1037+
10001038
type TermRef = Types.NamedType
10011039

1040+
def isTermRef(tpe: TypeOrBounds)(implicit ctx: Context): Option[TermRef] = tpe match {
1041+
case tpe: Types.NamedType =>
1042+
tpe.designator match {
1043+
case name: Names.TermName => Some(tpe)
1044+
case _ => None
1045+
}
1046+
case _ => None
1047+
}
1048+
1049+
def TermRef_name(self: TermRef)(implicit ctx: Context): String = self.name.toString
10021050
def TermRef_qualifier(self: TermRef)(implicit ctx: Context): TypeOrBounds = self.prefix
10031051

1052+
def TermRef_apply(qual: TypeOrBounds, name: String)(implicit ctx: Context): TermRef =
1053+
Types.TermRef(qual, name.toTermName)
1054+
10041055
type TypeRef = Types.NamedType
10051056

1057+
def isTypeRef(tpe: TypeOrBounds)(implicit ctx: Context): Option[TypeRef] = tpe match {
1058+
case tpe: Types.NamedType =>
1059+
tpe.designator match {
1060+
case name: Names.TypeName => Some(tpe)
1061+
case _ => None
1062+
}
1063+
case _ => None
1064+
}
1065+
10061066
def TypeRef_name(self: TypeRef)(implicit ctx: Context): String = self.name.toString
10071067
def TypeRef_qualifier(self: TypeRef)(implicit ctx: Context): TypeOrBounds = self.prefix
10081068

10091069
type SuperType = Types.SuperType
10101070

1071+
def isSuperType(tpe: TypeOrBounds)(implicit ctx: Context): Option[SuperType] = tpe match {
1072+
case tpe: Types.SuperType => Some(tpe)
1073+
case _ => None
1074+
}
1075+
10111076
def SuperType_thistpe(self: SuperType)(implicit ctx: Context): Type = self.thistpe
10121077
def SuperType_supertpe(self: SuperType)(implicit ctx: Context): Type = self.supertpe
10131078

10141079
type Refinement = Types.RefinedType
10151080

1081+
def isRefinement(tpe: TypeOrBounds)(implicit ctx: Context): Option[Refinement] = tpe match {
1082+
case tpe: Types.RefinedType => Some(tpe)
1083+
case _ => None
1084+
}
1085+
10161086
def Refinement_parent(self: Refinement)(implicit ctx: Context): Type = self.parent
10171087
def Refinement_name(self: Refinement)(implicit ctx: Context): String = self.refinedName.toString
10181088
def Refinement_info(self: Refinement)(implicit ctx: Context): TypeOrBounds = self.refinedInfo
10191089

10201090
type AppliedType = Types.AppliedType
10211091

1092+
def isAppliedType(tpe: TypeOrBounds)(implicit ctx: Context): Option[AppliedType] = tpe match {
1093+
case tpe: Types.AppliedType => Some(tpe)
1094+
case _ => None
1095+
}
1096+
10221097
def AppliedType_tycon(self: AppliedType)(implicit ctx: Context): Type = self.tycon
10231098
def AppliedType_args(self: AppliedType)(implicit ctx: Context): List[TypeOrBounds] = self.args
10241099

10251100
type AnnotatedType = Types.AnnotatedType
10261101

1102+
def isAnnotatedType(tpe: TypeOrBounds)(implicit ctx: Context): Option[AnnotatedType] = tpe match {
1103+
case tpe: Types.AnnotatedType => Some(tpe)
1104+
case _ => None
1105+
}
1106+
10271107
def AnnotatedType_underlying(self: AnnotatedType)(implicit ctx: Context): Type = self.underlying.stripTypeVar
10281108
def AnnotatedType_annot(self: AnnotatedType)(implicit ctx: Context): Term = self.annot.tree
10291109

10301110
type AndType = Types.AndType
10311111

1112+
def isAndType(tpe: TypeOrBounds)(implicit ctx: Context): Option[AndType] = tpe match {
1113+
case tpe: Types.AndType => Some(tpe)
1114+
case _ => None
1115+
}
1116+
10321117
def AndType_left(self: AndType)(implicit ctx: Context): Type = self.tp1.stripTypeVar
10331118
def AndType_right(self: AndType)(implicit ctx: Context): Type = self.tp2.stripTypeVar
10341119

10351120
type OrType = Types.OrType
10361121

1122+
def isOrType(tpe: TypeOrBounds)(implicit ctx: Context): Option[OrType] = tpe match {
1123+
case tpe: Types.OrType => Some(tpe)
1124+
case _ => None
1125+
}
1126+
10371127
def OrType_left(self: OrType)(implicit ctx: Context): Type = self.tp1.stripTypeVar
10381128
def OrType_right(self: OrType)(implicit ctx: Context): Type = self.tp2.stripTypeVar
10391129

10401130
type MatchType = Types.MatchType
10411131

1132+
def isMatchType(tpe: TypeOrBounds)(implicit ctx: Context): Option[MatchType] = tpe match {
1133+
case tpe: Types.MatchType => Some(tpe)
1134+
case _ => None
1135+
}
1136+
10421137
def MatchType_bound(self: MatchType)(implicit ctx: Context): Type = self.bound
10431138
def MatchType_scrutinee(self: MatchType)(implicit ctx: Context): Type = self.scrutinee
10441139
def MatchType_cases(self: MatchType)(implicit ctx: Context): List[Type] = self.cases
10451140

10461141
type ByNameType = Types.ExprType
10471142

1143+
def isByNameType(tpe: TypeOrBounds)(implicit ctx: Context): Option[ByNameType] = tpe match {
1144+
case tpe: Types.ExprType => Some(tpe)
1145+
case _ => None
1146+
}
1147+
10481148
def ByNameType_underlying(self: ByNameType)(implicit ctx: Context): Type = self.resType.stripTypeVar
10491149

10501150
type ParamRef = Types.ParamRef
10511151

1152+
def isParamRef(tpe: TypeOrBounds)(implicit ctx: Context): Option[ParamRef] = tpe match {
1153+
case tpe: Types.TypeParamRef => Some(tpe)
1154+
case tpe: Types.TermParamRef => Some(tpe)
1155+
case _ => None
1156+
}
1157+
10521158
def ParamRef_binder(self: ParamRef)(implicit ctx: Context): LambdaType[TypeOrBounds] =
10531159
self.binder.asInstanceOf[LambdaType[TypeOrBounds]] // Cast to tpd
10541160
def ParamRef_paramNum(self: ParamRef)(implicit ctx: Context): Int = self.paramNum
10551161

10561162
type ThisType = Types.ThisType
10571163

1058-
def ThisType_underlying(self: ThisType)(implicit ctx: Context): Type = self.underlying
1164+
def isThisType(tpe: TypeOrBounds)(implicit ctx: Context): Option[ThisType] = tpe match {
1165+
case tpe: Types.ThisType => Some(tpe)
1166+
case _ => None
1167+
}
1168+
1169+
def ThisType_tref(self: ThisType)(implicit ctx: Context): Type = self.tref
10591170

10601171
type RecursiveThis = Types.RecThis
10611172

1173+
def isRecursiveThis(tpe: TypeOrBounds)(implicit ctx: Context): Option[RecursiveThis] = tpe match {
1174+
case tpe: Types.RecThis => Some(tpe)
1175+
case _ => None
1176+
}
1177+
10621178
def RecursiveThis_binder(self: RecursiveThis)(implicit ctx: Context): RecursiveType = self.binder
10631179

10641180
type RecursiveType = Types.RecType
10651181

1182+
def isRecursiveType(tpe: TypeOrBounds)(implicit ctx: Context): Option[RecursiveType] = tpe match {
1183+
case tpe: Types.RecType => Some(tpe)
1184+
case _ => None
1185+
}
1186+
10661187
def RecursiveType_underlying(self: RecursiveType)(implicit ctx: Context): Type = self.underlying.stripTypeVar
10671188

10681189
type LambdaType[ParamInfo] = Types.LambdaType { type PInfo = ParamInfo }
10691190

10701191
type MethodType = Types.MethodType
10711192

1193+
def isMethodType(tpe: TypeOrBounds)(implicit ctx: Context): Option[MethodType] = tpe match {
1194+
case tpe: Types.MethodType => Some(tpe)
1195+
case _ => None
1196+
}
1197+
10721198
def MethodType_isErased(self: MethodType): Boolean = self.isErasedMethod
10731199
def MethodType_isImplicit(self: MethodType): Boolean = self.isImplicitMethod
10741200
def MethodType_paramNames(self: MethodType)(implicit ctx: Context): List[String] = self.paramNames.map(_.toString)
@@ -1077,12 +1203,22 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
10771203

10781204
type PolyType = Types.PolyType
10791205

1206+
def isPolyType(tpe: TypeOrBounds)(implicit ctx: Context): Option[PolyType] = tpe match {
1207+
case tpe: Types.PolyType => Some(tpe)
1208+
case _ => None
1209+
}
1210+
10801211
def PolyType_paramNames(self: PolyType)(implicit ctx: Context): List[String] = self.paramNames.map(_.toString)
10811212
def PolyType_paramBounds(self: PolyType)(implicit ctx: Context): List[TypeBounds] = self.paramInfos
10821213
def PolyType_resType(self: PolyType)(implicit ctx: Context): Type = self.resType
10831214

10841215
type TypeLambda = Types.TypeLambda
10851216

1217+
def isTypeLambda(tpe: TypeOrBounds)(implicit ctx: Context): Option[TypeLambda] = tpe match {
1218+
case tpe: Types.TypeLambda => Some(tpe)
1219+
case _ => None
1220+
}
1221+
10861222
def TypeLambda_paramNames(self: TypeLambda)(implicit ctx: Context): List[String] = self.paramNames.map(_.toString)
10871223
def TypeLambda_paramBounds(self: TypeLambda)(implicit ctx: Context): List[TypeBounds] = self.paramInfos
10881224
def TypeLambda_resType(self: TypeLambda)(implicit ctx: Context): Type = self.resType

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionImpl.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import dotty.tools.dotc.util.{SourcePosition, Spans}
66
class ReflectionImpl private (ctx: Contexts.Context, pos: SourcePosition)
77
extends scala.tasty.Reflection
88
with CoreImpl
9-
with SymbolOpsImpl
10-
with TypeOrBoundsOpsImpl {
9+
with SymbolOpsImpl {
1110

1211
val kernel: KernelImpl = new KernelImpl(ctx, pos)
1312

0 commit comments

Comments
 (0)