Skip to content

Commit d492a1f

Browse files
committed
Use symbol instead of definition tree
1 parent 33afaa2 commit d492a1f

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import dotty.tools.dotc.core.Symbols._
55
trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with TastyCoreImpl {
66

77
def SymbolDeco(symbol: Symbol): SymbolAPI = new SymbolAPI {
8-
def isEmpty: Boolean = symbol eq NoSymbol
8+
def isEmpty(implicit ctx: Context): Boolean = symbol eq NoSymbol
9+
def isClass(implicit ctx: Context): Boolean = symbol.isClass
10+
def name(implicit ctx: Context): String = symbol.name.toString
11+
def fullName(implicit ctx: Context): String = symbol.fullName.toString
912
def localContext(implicit ctx: Context): Context = ctx.withOwner(symbol)
1013
def tree(implicit ctx: Context): Option[Definition] =
1114
if (isEmpty) None else Some(FromSymbol.definitionFromSym(symbol))

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ package reflect
55
trait SymbolOps extends TastyCore {
66

77
trait SymbolAPI {
8-
def isEmpty: Boolean
8+
def isEmpty(implicit ctx: Context): Boolean
9+
def isClass(implicit ctx: Context): Boolean
10+
def name(implicit ctx: Context): String
11+
def fullName(implicit ctx: Context): String
912
def localContext(implicit ctx: Context): Context
1013
def tree(implicit ctx: Context): Option[Definition]
1114
}

library/src/scala/tasty/util/ShowSourceCode.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,10 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
743743
printTypeAndAnnots(tp)
744744
this += " "
745745
printAnnotation(annot)
746-
case Type.SymRef(ClassDef("Null$" | "Nothing$", _, _, _, _), Type.ThisType(Type.SymRef(PackageDef("runtime", _), NoPrefix()))) =>
746+
case Type.SymRef(sym, Type.ThisType(Type.SymRef(PackageDef("runtime", _), NoPrefix()))) if sym.symbol.isClass && (sym.symbol.name == "Null$" || sym.symbol.name == "Nothing$") =>
747747
// scala.runtime.Null$ and scala.runtime.Nothing$ are not modules, those are their actual names
748748
printType(tpe)
749-
case tpe @ Type.SymRef(ClassDef(name, _, _, _, _), _) if name.endsWith("$") =>
749+
case tpe @ Type.SymRef(sym, _) if sym.symbol.isClass && sym.name.endsWith("$") =>
750750
printType(tpe)
751751
this += ".type"
752752
case tpe => printType(tpe)
@@ -826,7 +826,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
826826
case Type.SymRef(sym, prefix) =>
827827
prefix match {
828828
case Types.EmptyPrefix() =>
829-
case IsType(prefix @ Type.SymRef(ClassDef(_, _, _, _, _), _)) =>
829+
case IsType(prefix @ Type.SymRef(sym, _)) if sym.symbol.isClass =>
830830
printType(prefix)
831831
this += "#"
832832
case IsType(prefix) =>
@@ -892,7 +892,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
892892

893893
case Type.ThisType(tp) =>
894894
tp match {
895-
case Type.SymRef(cdef @ ClassDef(_, _, _, _, _), _) if !cdef.flags.isObject =>
895+
case Type.SymRef(cdef, _) if !cdef.flags.isObject =>
896896
printFullClassName(tp)
897897
this += ".this"
898898
case Type.TypeRef(name, prefix) if name.endsWith("$") =>
@@ -958,7 +958,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
958958
val annots = definition.annots.filter {
959959
case Annotation(annot, _) =>
960960
annot.tpe match {
961-
case Type.TypeRef(_, Type.SymRef(PackageDef("internal", _), Type.ThisType(Type.SymRef(PackageDef("annotation", _), NoPrefix())))) => false
961+
case Type.TypeRef(_, Type.SymRef(sym, _)) if sym.symbol.fullName == "scala.annotation.internal" => false
962962
case Type.TypeRef("forceInline", Types.ScalaPackage()) => false
963963
case _ => true
964964
}
@@ -1055,7 +1055,8 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
10551055
def printProtectedOrPrivate(definition: Definition): Boolean = {
10561056
var prefixWasPrinted = false
10571057
def printWithin(within: Type) = within match {
1058-
case Type.SymRef(PackageDef(name, _), _) => this += name
1058+
case Type.SymRef(sym @ PackageDef(name, _), _) =>
1059+
this += sym.name
10591060
case _ => printFullClassName(within)
10601061
}
10611062
if (definition.flags.isProtected) {
@@ -1082,14 +1083,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
10821083

10831084
def printFullClassName(tp: TypeOrBounds): Unit = {
10841085
def printClassPrefix(prefix: TypeOrBounds): Unit = prefix match {
1085-
case Type.SymRef(ClassDef(name, _, _, _, _), prefix2) =>
1086+
case Type.SymRef(sym, prefix2) if sym.symbol.isClass =>
10861087
printClassPrefix(prefix2)
1087-
this += name += "."
1088+
this += sym.name += "."
10881089
case _ =>
10891090
}
1090-
val Type.SymRef(ClassDef(name, _, _, _, _), prefix) = tp
1091+
val Type.SymRef(sym, prefix) = tp
10911092
printClassPrefix(prefix)
1092-
this += name
1093+
this += sym.name
10931094
}
10941095

10951096
def +=(x: Boolean): this.type = { sb.append(x); this }
@@ -1121,7 +1122,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
11211122
def unapply(arg: Tree)(implicit ctx: Context): Option[(String, List[Term])] = arg match {
11221123
case IsTerm(arg @ Term.Apply(fn, args)) =>
11231124
fn.tpe match {
1124-
case Type.SymRef(DefDef(op, _, _, _, _), Type.ThisType(Type.SymRef(PackageDef("<special-ops>", _), NoPrefix()))) =>
1125+
case Type.SymRef(DefDef(op, _, _, _, _), Type.ThisType(Type.SymRef(sym, _))) if sym.name == "<special-ops>" =>
11251126
Some((op, args))
11261127
case _ => None
11271128
}
@@ -1143,7 +1144,7 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
11431144

11441145
object JavaLangObject {
11451146
def unapply(tpe: Type)(implicit ctx: Context): Boolean = tpe match {
1146-
case Type.TypeRef("Object", Type.SymRef(PackageDef("lang", _), Type.ThisType(Type.SymRef(PackageDef("java", _), NoPrefix())))) => true
1147+
case Type.TypeRef("Object", Type.SymRef(sym, _)) if sym.symbol.fullName == "java.lang" => true
11471148
case _ => false
11481149
}
11491150
}

0 commit comments

Comments
 (0)