Skip to content

Commit d201434

Browse files
committed
Refactor TASYy reflect TermRef, TypeRef and SymRef
1 parent 8b4509d commit d201434

File tree

16 files changed

+276
-216
lines changed

16 files changed

+276
-216
lines changed

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

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ class KernelImpl(val rootContext: core.Contexts.Context) extends Kernel {
271271

272272
def Ident_name(self: Ident) given Context: String = self.name.show
273273

274-
def Ident_apply(tmref: TermRef) given Context: Term =
274+
def Ident_apply(tmref: NamedTermRef) given Context: Term =
275275
withDefaultPos(tpd.ref(tmref).asInstanceOf[Term])
276276

277277
def Ident_copy(original: Tree)(name: String) given Context: Ident =
@@ -1134,32 +1134,53 @@ class KernelImpl(val rootContext: core.Contexts.Context) extends Kernel {
11341134

11351135
def ConstantType_constant(self: ConstantType) given Context: Constant = self.value
11361136

1137-
type SymRef = Types.NamedType
1137+
type SymTermRef = Types.NamedType
11381138

1139-
def matchSymRef(tpe: TypeOrBounds) given Context: Option[SymRef] = tpe match {
1139+
def matchSymTermRef(tpe: TypeOrBounds) given Context: Option[SymTermRef] = tpe match {
11401140
case tp: Types.NamedType =>
11411141
tp.designator match {
1142-
case sym: Symbol => Some(tp)
1142+
case sym: Symbol if sym.isTerm => Some(tp)
11431143
case _ => None
11441144
}
11451145
case _ => None
11461146
}
11471147

1148-
def SymRef_qualifier(self: SymRef) given Context: TypeOrBounds = self.prefix
1148+
def SymTermRef_qualifier(self: SymTermRef) given Context: TypeOrBounds = self.prefix
11491149

1150-
// TODO remove this method. May require splitting SymRef into TypeSymRef and TermSymRef
1151-
def matchSymRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
1150+
def matchSymTermRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
11521151
case tpe: Types.NamedType =>
11531152
tpe.designator match {
1154-
case sym: Symbol => Some((sym, tpe.prefix))
1153+
case sym: Symbol if sym.isTerm => Some((sym, tpe.prefix))
11551154
case _ => None
11561155
}
11571156
case _ => None
11581157
}
11591158

1160-
type TermRef = Types.NamedType
1159+
type SymTypeRef = Types.NamedType
11611160

1162-
def matchTermRef(tpe: TypeOrBounds) given Context: Option[TermRef] = tpe match {
1161+
def matchSymTypeRef(tpe: TypeOrBounds) given Context: Option[SymTypeRef] = tpe match {
1162+
case tp: Types.NamedType =>
1163+
tp.designator match {
1164+
case sym: Symbol if sym.isType => Some(tp)
1165+
case _ => None
1166+
}
1167+
case _ => None
1168+
}
1169+
1170+
def SymTypeRef_qualifier(self: SymTypeRef) given Context: TypeOrBounds = self.prefix
1171+
1172+
def matchSymTypeRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
1173+
case tpe: Types.NamedType =>
1174+
tpe.designator match {
1175+
case sym: Symbol if sym.isType => Some((sym, tpe.prefix))
1176+
case _ => None
1177+
}
1178+
case _ => None
1179+
}
1180+
1181+
type NamedTermRef = Types.NamedType
1182+
1183+
def matchNamedTermRef(tpe: TypeOrBounds) given Context: Option[NamedTermRef] = tpe match {
11631184
case tpe: Types.NamedType =>
11641185
tpe.designator match {
11651186
case name: Names.TermName => Some(tpe)
@@ -1168,15 +1189,15 @@ class KernelImpl(val rootContext: core.Contexts.Context) extends Kernel {
11681189
case _ => None
11691190
}
11701191

1171-
def TermRef_name(self: TermRef) given Context: String = self.name.toString
1172-
def TermRef_qualifier(self: TermRef) given Context: TypeOrBounds = self.prefix
1192+
def NamedTermRef_name(self: NamedTermRef) given Context: String = self.name.toString
1193+
def NamedTermRef_qualifier(self: NamedTermRef) given Context: TypeOrBounds = self.prefix
11731194

1174-
def TermRef_apply(qual: TypeOrBounds, name: String) given Context: TermRef =
1195+
def NamedTermRef_apply(qual: TypeOrBounds, name: String) given Context: NamedTermRef =
11751196
Types.TermRef(qual, name.toTermName)
11761197

1177-
type TypeRef = Types.NamedType
1198+
type NamedTypeRef = Types.NamedType
11781199

1179-
def matchTypeRef(tpe: TypeOrBounds) given Context: Option[TypeRef] = tpe match {
1200+
def matchNamedTypeRef(tpe: TypeOrBounds) given Context: Option[NamedTypeRef] = tpe match {
11801201
case tpe: Types.NamedType =>
11811202
tpe.designator match {
11821203
case name: Names.TypeName => Some(tpe)
@@ -1185,8 +1206,8 @@ class KernelImpl(val rootContext: core.Contexts.Context) extends Kernel {
11851206
case _ => None
11861207
}
11871208

1188-
def TypeRef_name(self: TypeRef) given Context: String = self.name.toString
1189-
def TypeRef_qualifier(self: TypeRef) given Context: TypeOrBounds = self.prefix
1209+
def NamedTypeRef_name(self: NamedTypeRef) given Context: String = self.name.toString
1210+
def NamedTypeRef_qualifier(self: NamedTypeRef) given Context: TypeOrBounds = self.prefix
11901211

11911212
type SuperType = Types.SuperType
11921213

library/src/scala/tasty/reflect/Core.scala

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,10 @@ package scala.tasty.reflect
6969
* +- TypeOrBounds -+- TypeBounds
7070
* |
7171
* +- Type -------+- ConstantType
72-
* +- SymRef
73-
* +- TermRef
74-
* +- TypeRef
72+
* +- SymTermRef
73+
* +- SymTypeRef
74+
* +- NamedTermRef
75+
* +- NamedTypeRef
7576
* +- SuperType
7677
* +- Refinement
7778
* +- AppliedType
@@ -331,14 +332,17 @@ trait Core {
331332
/** A singleton type representing a known constant value */
332333
type ConstantType = kernel.ConstantType
333334

334-
/** Type of a reference to a symbol */
335-
type SymRef = kernel.SymRef
335+
/** Type of a reference to a term symbol */
336+
type SymTermRef = kernel.SymTermRef
337+
338+
/** Type of a reference to a type symbol */
339+
type SymTypeRef = kernel.SymTypeRef
336340

337341
/** Type of a reference to a term */
338-
type TermRef = kernel.TermRef
342+
type NamedTermRef = kernel.NamedTermRef
339343

340344
/** Type of a reference to a type */
341-
type TypeRef = kernel.TypeRef
345+
type TypeRef = kernel.NamedTypeRef
342346

343347
/** Type of a `super` refernce */
344348
type SuperType = kernel.SuperType

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ import scala.quoted.QuoteContext
7070
* +- TypeOrBounds -+- TypeBounds
7171
* |
7272
* +- Type -------+- ConstantType
73-
* +- SymRef
74-
* +- TermRef
75-
* +- TypeRef
73+
* +- SymTermRef
74+
* +- SymTypeRef
75+
* +- NamedTermRef
76+
* +- NamedTypeRef
7677
* +- SuperType
7778
* +- Refinement
7879
* +- AppliedType
@@ -313,7 +314,7 @@ trait Kernel {
313314

314315
def Ident_name(self: Ident) given (ctx: Context): String
315316

316-
def Ident_apply(tmref: TermRef) given (ctx: Context): Term
317+
def Ident_apply(tmref: NamedTermRef) given (ctx: Context): Term
317318
def Ident_copy(original: Tree)(name: String) given (ctx: Context): Ident
318319

319320
/** Tree representing a selection of definition with a given name on a given prefix */
@@ -909,33 +910,41 @@ trait Kernel {
909910

910911
def ConstantType_constant(self: ConstantType) given (ctx: Context): Constant
911912

912-
/** Type of a reference to a symbol */
913-
type SymRef <: Type
913+
/** Type of a reference to a term symbol */
914+
type SymTermRef <: Type
914915

915-
def matchSymRef(tpe: TypeOrBounds) given (ctx: Context): Option[SymRef]
916+
def matchSymTermRef(tpe: TypeOrBounds) given (ctx: Context): Option[SymTermRef]
916917

917-
// TODO remove this method. May require splitting SymRef into TypeSymRef and TermSymRef
918-
def matchSymRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
918+
def matchSymTermRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
919919

920-
def SymRef_qualifier(self: SymRef) given (ctx: Context): TypeOrBounds
920+
def SymTermRef_qualifier(self: SymTermRef) given (ctx: Context): TypeOrBounds
921921

922-
/** Type of a reference to a term */
923-
type TermRef <: Type
922+
/** Type of a reference to a type symbol */
923+
type SymTypeRef <: Type
924924

925-
def matchTermRef(tpe: TypeOrBounds) given (ctx: Context): Option[TermRef]
925+
def matchSymTypeRef(tpe: TypeOrBounds) given (ctx: Context): Option[SymTypeRef]
926926

927-
def TermRef_name(self: TermRef) given (ctx: Context): String
928-
def TermRef_qualifier(self: TermRef) given (ctx: Context): TypeOrBounds
927+
def matchSymTypeRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
929928

930-
def TermRef_apply(qual: TypeOrBounds, name: String) given (ctx: Context): TermRef
929+
def SymTypeRef_qualifier(self: SymTypeRef) given (ctx: Context): TypeOrBounds
931930

932-
/** Type of a reference to a type */
933-
type TypeRef <: Type
931+
/** Type of a reference to a term by it's name */
932+
type NamedTermRef <: Type
934933

935-
def matchTypeRef(tpe: TypeOrBounds) given (ctx: Context): Option[TypeRef]
934+
def matchNamedTermRef(tpe: TypeOrBounds) given (ctx: Context): Option[NamedTermRef]
936935

937-
def TypeRef_name(self: TypeRef) given (ctx: Context): String
938-
def TypeRef_qualifier(self: TypeRef) given (ctx: Context): TypeOrBounds
936+
def NamedTermRef_name(self: NamedTermRef) given (ctx: Context): String
937+
def NamedTermRef_qualifier(self: NamedTermRef) given (ctx: Context): TypeOrBounds
938+
939+
def NamedTermRef_apply(qual: TypeOrBounds, name: String) given (ctx: Context): NamedTermRef
940+
941+
/** Type of a reference to a type by it's name */
942+
type NamedTypeRef <: Type
943+
944+
def matchNamedTypeRef(tpe: TypeOrBounds) given (ctx: Context): Option[NamedTypeRef]
945+
946+
def NamedTypeRef_name(self: NamedTypeRef) given (ctx: Context): String
947+
def NamedTypeRef_qualifier(self: NamedTypeRef) given (ctx: Context): TypeOrBounds
939948

940949
/** Type of a `super` refernce */
941950
type SuperType <: Type

0 commit comments

Comments
 (0)