Skip to content

Commit 56c877c

Browse files
committed
Refactor TermRef extractor
Make it closer to NamedTermRef extractor
1 parent 963719e commit 56c877c

File tree

13 files changed

+65
-62
lines changed

13 files changed

+65
-62
lines changed

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,8 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11141114

11151115
def Type_typeSymbol(self: Type) given Context: Symbol = self.typeSymbol
11161116

1117+
def Type_termSymbol(self: Type) given Context: Symbol = self.termSymbol
1118+
11171119
def Type_isSingleton(self: Type) given Context: Boolean = self.isSingleton
11181120

11191121
def Type_memberType(self: Type)(member: Symbol) given Context: Type =
@@ -1158,14 +1160,7 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11581160

11591161
def TermRef_qualifier(self: TermRef) given Context: TypeOrBounds = self.prefix
11601162

1161-
def matchTermRef_unapply(tpe: TypeOrBounds) given Context: Option[(Symbol, Type | NoPrefix)] = tpe match {
1162-
case tpe: Types.NamedType =>
1163-
tpe.designator match {
1164-
case sym: Symbol if sym.isTerm => Some((sym, tpe.prefix))
1165-
case _ => None
1166-
}
1167-
case _ => None
1168-
}
1163+
def TermRef_name(self: TermRef) given Context: String = self.name.toString
11691164

11701165
type TypeRef = Types.NamedType
11711166

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,12 @@ trait CompilerInterface {
878878
*/
879879
def Type_dealias(self: Type) given (ctx: Context): Type
880880

881-
def Type_classSymbol(self: Type) given (ctx: Context): Option[ClassDefSymbol]
881+
def Type_classSymbol(self: Type) given (ctx: Context): Option[ClassDefSymbol] // TODO remove Option and use NoSymbol
882882

883883
def Type_typeSymbol(self: Type) given (ctx: Context): Symbol
884884

885+
def Type_termSymbol(self: Type) given (ctx: Context): Symbol
886+
885887
def Type_isSingleton(self: Type) given (ctx: Context): Boolean
886888

887889
def Type_memberType(self: Type)(member: Symbol) given (ctx: Context): Type
@@ -931,9 +933,9 @@ trait CompilerInterface {
931933

932934
def matchTermRef(tpe: TypeOrBounds) given (ctx: Context): Option[TermRef]
933935

934-
def matchTermRef_unapply(tpe: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)]
935-
936936
def TermRef_qualifier(self: TermRef) given (ctx: Context): TypeOrBounds
937+
def TermRef_name(self: TermRef) given (ctx: Context): String
938+
937939

938940
/** Type of a reference to a type symbol */
939941
type TypeRef <: Type

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ trait Printers
308308
def visitType(x: TypeOrBounds): Buffer = x match {
309309
case Type.ConstantType(value) =>
310310
this += "Type.ConstantType(" += value += ")"
311-
case Type.TermRef(sym, qual) =>
312-
this += "Type.TermRef(" += sym += ", " += qual += ")"
311+
case Type.TermRef(qual, name) =>
312+
this += "Type.TermRef(" += qual+= ", \"" += name += "\")"
313313
case Type.TypeRef(sym, qual) =>
314314
this += "Type.TypeRef(" += sym += ", " += qual += ")"
315315
case Type.NamedTermRef(name, qual) =>
@@ -1423,10 +1423,10 @@ trait Printers
14231423
case Type.TypeRef(IsClassDefSymbol(sym), _) if sym.fullName == "scala.runtime.Null$" || sym.fullName == "scala.runtime.Nothing$" =>
14241424
// scala.runtime.Null$ and scala.runtime.Nothing$ are not modules, those are their actual names
14251425
printType(tpe)
1426-
case tpe @ Type.TermRef(IsClassDefSymbol(sym), _) if sym.name.endsWith("$") =>
1426+
case Type.IsTermRef(tpe) if tpe.termSymbol.isClass && tpe.termSymbol.name.endsWith("$") =>
14271427
printType(tpe)
14281428
this += ".type"
1429-
case tpe @ Type.TypeRef(IsClassDefSymbol(sym), _) if sym.name.endsWith("$") =>
1429+
case Type.IsTypeRef(tpe) if tpe.typeSymbol.isClass && tpe.typeSymbol.name.endsWith("$") =>
14301430
printType(tpe)
14311431
this += ".type"
14321432
case tpe @ Type.TermRef(sym, _) =>
@@ -1519,8 +1519,9 @@ trait Printers
15191519
case Type.ConstantType(const) =>
15201520
printConstant(const)
15211521

1522-
case Type.TypeRef(sym, prefix) =>
1523-
prefix match {
1522+
case Type.IsTypeRef(tpe) =>
1523+
val sym = tpe.typeSymbol
1524+
tpe.qualifier match {
15241525
case Type.ThisType(Types.EmptyPackage() | Types.RootPackage()) =>
15251526
case NoPrefix() =>
15261527
if (sym.owner.flags.is(Flags.Package)) {
@@ -1529,7 +1530,7 @@ trait Printers
15291530
if (packagePath != "")
15301531
this += packagePath += "."
15311532
}
1532-
case IsType(prefix @ Type.TermRef(IsClassDefSymbol(_), _)) =>
1533+
case Type.IsTermRef(prefix) if prefix.termSymbol.isClass =>
15331534
printType(prefix)
15341535
this += "#"
15351536
case IsType(prefix @ Type.TypeRef(IsClassDefSymbol(_), _)) =>
@@ -1543,14 +1544,14 @@ trait Printers
15431544
}
15441545
this += highlightTypeDef(sym.name.stripSuffix("$"))
15451546

1546-
case Type.TermRef(sym, prefix) =>
1547+
case Type.TermRef(prefix, name) =>
15471548
prefix match {
15481549
case NoPrefix() | Type.ThisType(Types.EmptyPackage() | Types.RootPackage()) =>
1549-
this += highlightTypeDef(sym.name)
1550+
this += highlightTypeDef(name)
15501551
case _ =>
15511552
printTypeOrBound(prefix)
1552-
if (sym.name != "package")
1553-
this += "." += highlightTypeDef(sym.name)
1553+
if (name != "package")
1554+
this += "." += highlightTypeDef(name)
15541555
this
15551556
}
15561557

@@ -1706,8 +1707,8 @@ trait Printers
17061707
val annots = definition.symbol.annots.filter {
17071708
case Annotation(annot, _) =>
17081709
annot.tpe match {
1709-
case Type.NamedTypeRef(_, Type.TermRef(sym, _)) if sym.fullName == "scala.annotation.internal" => false
1710-
case Type.NamedTypeRef(_, Type.TypeRef(sym, _)) if sym.fullName == "scala.annotation.internal" => false
1710+
case Type.NamedTypeRef(_, Type.IsTermRef(prefix)) if prefix.termSymbol.fullName == "scala.annotation.internal" => false
1711+
case Type.NamedTypeRef(_, Type.IsTypeRef(prefix)) if prefix.typeSymbol.fullName == "scala.annotation.internal" => false
17111712
case Type.NamedTypeRef("forceInline", Types.ScalaPackage()) => false
17121713
case _ => true
17131714
}
@@ -1871,8 +1872,8 @@ trait Printers
18711872
def unapply(arg: Tree) given (ctx: Context): Option[(String, List[Term])] = arg match {
18721873
case IsTerm(arg @ Apply(fn, args)) =>
18731874
fn.tpe match {
1874-
case Type.TermRef(IsDefDefSymbol(sym), Type.ThisType(Type.TypeRef(sym2, _))) if sym2.name == "<special-ops>" =>
1875-
Some((sym.tree.name, args))
1875+
case tpe @ Type.TermRef(Type.ThisType(Type.TypeRef(sym2, _)), _) if sym2.name == "<special-ops>" =>
1876+
Some((tpe.termSymbol.asDefDef.tree.name, args))
18761877
case _ => None
18771878
}
18781879
case _ => None
@@ -1893,23 +1894,23 @@ trait Printers
18931894

18941895
object JavaLangObject {
18951896
def unapply(tpe: Type) given (ctx: Context): Boolean = tpe match {
1896-
case Type.NamedTypeRef("Object", Type.TermRef(sym, _)) => sym.fullName == "java.lang"
1897+
case Type.NamedTypeRef("Object", Type.IsTermRef(prefix)) => prefix.typeSymbol.fullName == "java.lang"
18971898
case _ => false
18981899
}
18991900
}
19001901

19011902
object Sequence {
19021903
def unapply(tpe: Type) given (ctx: Context): Option[Type] = tpe match {
1903-
case Type.AppliedType(Type.NamedTypeRef("Seq", Type.TermRef(sym, _)), IsType(tp) :: Nil) if sym.fullName == "scala.collection" => Some(tp)
1904-
case Type.AppliedType(Type.NamedTypeRef("Seq", Type.TypeRef(sym, _)), IsType(tp) :: Nil) if sym.fullName == "scala.collection" => Some(tp)
1904+
case Type.AppliedType(Type.NamedTypeRef("Seq", Type.IsTermRef(prefix)), IsType(tp) :: Nil) if prefix.termSymbol.fullName == "scala.collection" => Some(tp)
1905+
case Type.AppliedType(Type.NamedTypeRef("Seq", Type.IsTypeRef(prefix)), IsType(tp) :: Nil) if prefix.typeSymbol.fullName == "scala.collection" => Some(tp)
19051906
case _ => None
19061907
}
19071908
}
19081909

19091910
object RepeatedAnnotation {
19101911
def unapply(tpe: Type) given (ctx: Context): Boolean = tpe match {
1911-
case Type.NamedTypeRef("Repeated", Type.TermRef(sym, _)) => sym.fullName == "scala.annotation.internal"
1912-
case Type.NamedTypeRef("Repeated", Type.TypeRef(sym, _)) => sym.fullName == "scala.annotation.internal"
1912+
case Type.NamedTypeRef("Repeated", Type.IsTermRef(prefix)) => prefix.termSymbol.fullName == "scala.annotation.internal"
1913+
case Type.NamedTypeRef("Repeated", Type.IsTypeRef(prefix)) => prefix.typeSymbol.fullName == "scala.annotation.internal"
19131914
case _ => false
19141915
}
19151916
}
@@ -1923,21 +1924,21 @@ trait Printers
19231924

19241925
object ScalaPackage {
19251926
def unapply(tpe: TypeOrBounds) given (ctx: Context): Boolean = tpe match {
1926-
case Type.TermRef(sym, _) => sym == definitions.ScalaPackage
1927+
case Type.IsTermRef(tpe) => tpe.termSymbol == definitions.ScalaPackage
19271928
case _ => false
19281929
}
19291930
}
19301931

19311932
object RootPackage {
19321933
def unapply(tpe: TypeOrBounds) given (ctx: Context): Boolean = tpe match {
1933-
case Type.TypeRef(sym, _) => sym.fullName == "<root>" // TODO use Symbol.==
1934+
case Type.IsTypeRef(tpe) => tpe.typeSymbol.fullName == "<root>" // TODO use Symbol.==
19341935
case _ => false
19351936
}
19361937
}
19371938

19381939
object EmptyPackage {
19391940
def unapply(tpe: TypeOrBounds) given (ctx: Context): Boolean = tpe match {
1940-
case Type.TypeRef(sym, _) => sym.fullName == "<empty>"
1941+
case Type.IsTypeRef(tpe) => tpe.typeSymbol.fullName == "<empty>"
19411942
case _ => false
19421943
}
19431944
}

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ trait SymbolOps extends Core {
8585

8686
def isType given (ctx: Context): Boolean = internal.matchTypeSymbol(self).isDefined
8787
def isTerm given (ctx: Context): Boolean = internal.matchTermSymbol(self).isDefined
88+
def isValDef given (ctx: Context): Boolean = internal.matchValDefSymbol(self).isDefined
89+
def isDefDef given (ctx: Context): Boolean = internal.matchDefDefSymbol(self).isDefined
90+
def isClass given (ctx: Context): Boolean = internal.matchClassDefSymbol(self).isDefined
8891
}
8992

9093
// PackageSymbol

library/src/scala/tasty/reflect/TypeOrBoundsOps.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ trait TypeOrBoundsOps extends Core {
2020

2121
def classSymbol given (ctx: Context): Option[ClassDefSymbol] = internal.Type_classSymbol(self)
2222
def typeSymbol given (ctx: Context): Symbol = internal.Type_typeSymbol(self)
23+
def termSymbol given (ctx: Context): Symbol = internal.Type_termSymbol(self)
2324
def isSingleton given (ctx: Context): Boolean = internal.Type_isSingleton(self)
2425
def memberType(member: Symbol) given (ctx: Context): Type = internal.Type_memberType(self)(member)
2526

@@ -84,8 +85,8 @@ trait TypeOrBoundsOps extends Core {
8485
}
8586

8687
object TermRef {
87-
def unapply(typeOrBounds: TypeOrBounds) given (ctx: Context): Option[(Symbol, TypeOrBounds /* Type | NoPrefix */)] =
88-
internal.matchTermRef_unapply(typeOrBounds)
88+
def unapply(typeOrBounds: TypeOrBounds) given (ctx: Context): Option[(TypeOrBounds /* Type | NoPrefix */, String)] =
89+
internal.matchTermRef(typeOrBounds).map(x => (x.qualifier, x.name))
8990
}
9091

9192
object IsTypeRef {
@@ -299,6 +300,7 @@ trait TypeOrBoundsOps extends Core {
299300

300301
implicit class Type_TermRefAPI(self: TermRef) {
301302
def qualifier given (ctx: Context): TypeOrBounds /* Type | NoPrefix */ = internal.TermRef_qualifier(self)
303+
def name given (ctx: Context): String = internal.TermRef_name(self)
302304
}
303305

304306
implicit class Type_TypeRefAPI(self: TypeRef) {

tests/neg-macros/tasty-macro-assert-1/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Asserts {
1818
val tree = cond.unseal
1919

2020
def isOps(tpe: TypeOrBounds): Boolean = tpe match {
21-
case Type.TermRef(IsDefDefSymbol(sym), _) => sym.name == "Ops" // TODO check that the parent is Asserts
21+
case Type.IsTermRef(tpe) => tpe.termSymbol.isDefDef && tpe.name == "Ops"// TODO check that the parent is Asserts
2222
case _ => false
2323
}
2424

tests/neg-macros/tasty-macro-assert-2/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object Asserts {
1818
val tree = cond.unseal
1919

2020
def isOps(tpe: TypeOrBounds): Boolean = tpe match {
21-
case Type.TermRef(IsDefDefSymbol(sym), _) => sym.name == "Ops" // TODO check that the parent is Asserts
21+
case Type.IsTermRef(tpe) => tpe.termSymbol.isDefDef && tpe.name == "Ops"// TODO check that the parent is Asserts
2222
case _ => false
2323
}
2424

tests/run-macros/tasty-eval/quoted_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ object Macros {
2121
import qctx.tasty._
2222

2323
e.unseal.tpe match {
24-
case Type.TermRef(IsValDefSymbol(sym), pre) =>
25-
sym.tree.tpt.tpe match {
24+
case Type.IsTermRef(pre) if pre.termSymbol.isValDef =>
25+
pre.termSymbol.asValDef.tree.tpt.tpe match {
2626
case Type.ConstantType(Constant(i: Int)) => Some(i)
2727
case _ => None
2828
}

tests/run-macros/tasty-extractors-1.check

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ Inlined(None, Nil, Apply(Ident("println"), List(Literal(Constant("abc")))))
2020
Type.TypeRef(IsClassDefSymbol(<scala.Unit>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
2121

2222
Inlined(None, Nil, Typed(Literal(Constant(8)), TypeIdent("Int")))
23-
Type.TypeRef(IsClassDefSymbol(<scala.Int>), Type.TermRef(IsPackageDefSymbol(<scala>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix()))))
23+
Type.TypeRef(IsClassDefSymbol(<scala.Int>), Type.TermRef(Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix())), "scala"))
2424

2525
Inlined(None, Nil, Typed(Literal(Constant(8: Byte)), TypeIdent("Byte")))
26-
Type.TypeRef(IsClassDefSymbol(<scala.Byte>), Type.TermRef(IsPackageDefSymbol(<scala>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix()))))
26+
Type.TypeRef(IsClassDefSymbol(<scala.Byte>), Type.TermRef(Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix())), "scala"))
2727

2828
Inlined(None, Nil, Typed(Literal(Constant(8: Short)), TypeIdent("Short")))
29-
Type.TypeRef(IsClassDefSymbol(<scala.Short>), Type.TermRef(IsPackageDefSymbol(<scala>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix()))))
29+
Type.TypeRef(IsClassDefSymbol(<scala.Short>), Type.TermRef(Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix())), "scala"))
3030

3131
Inlined(None, Nil, Literal(Constant('a')))
3232
Type.ConstantType(Constant('a'))
@@ -83,7 +83,7 @@ Inlined(None, Nil, Apply(Select(Ident("Int"), "box"), List(NamedArg("x", Literal
8383
Type.TypeRef(IsClassDefSymbol(<java.lang.Integer>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<java.lang>), NoPrefix())))
8484

8585
Inlined(None, Nil, Apply(TypeApply(Select(Ident("Ordering"), "apply"), List(TypeIdent("Int"))), List(Ident("Int"))))
86-
Type.AppliedType(Type.TypeRef(IsClassDefSymbol(<scala.math.Ordering>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala.math>), NoPrefix()))), List(Type.TypeRef(IsClassDefSymbol(<scala.Int>), Type.TermRef(IsPackageDefSymbol(<scala>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix()))))))
86+
Type.AppliedType(Type.TypeRef(IsClassDefSymbol(<scala.math.Ordering>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala.math>), NoPrefix()))), List(Type.TypeRef(IsClassDefSymbol(<scala.Int>), Type.TermRef(Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix())), "scala"))))
8787

8888
Inlined(None, Nil, Block(List(ValDef("a", TypeIdent("Int"), Some(Literal(Constant(3))))), Literal(Constant(()))))
8989
Type.TypeRef(IsClassDefSymbol(<scala.Unit>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
@@ -113,7 +113,7 @@ Inlined(None, Nil, Block(List(DefDef("f7", List(TypeDef("T", TypeBoundsTree(Infe
113113
Type.TypeRef(IsClassDefSymbol(<scala.Unit>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))
114114

115115
Inlined(None, Nil, Block(List(DefDef("f8", Nil, List(List(ValDef("i", Annotated(Applied(Inferred(), List(TypeIdent("Int"))), Apply(Select(New(Inferred()), "<init>"), Nil)), None))), TypeIdent("Int"), Some(Literal(Constant(9))))), Apply(Ident("f8"), List(Typed(Repeated(List(Literal(Constant(1)), Literal(Constant(2)), Literal(Constant(3))), Inferred()), Inferred())))))
116-
Type.TypeRef(IsClassDefSymbol(<scala.Int>), Type.TermRef(IsPackageDefSymbol(<scala>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix()))))
116+
Type.TypeRef(IsClassDefSymbol(<scala.Int>), Type.TermRef(Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<<root>>), NoPrefix())), "scala"))
117117

118118
Inlined(None, Nil, Block(List(DefDef("f9", Nil, List(List(ValDef("i", ByName(TypeIdent("Int")), None))), TypeIdent("Int"), Some(Ident("i")))), Literal(Constant(()))))
119119
Type.TypeRef(IsClassDefSymbol(<scala.Unit>), Type.ThisType(Type.TypeRef(IsPackageDefSymbol(<scala>), NoPrefix())))

0 commit comments

Comments
 (0)