Skip to content

Commit 953479a

Browse files
committed
Add requiredXYZ symbols to TASTy reflect context
1 parent 760deff commit 953479a

File tree

5 files changed

+44
-15
lines changed

5 files changed

+44
-15
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
5959
def Context_GADT_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type =
6060
self.gadt.approximation(sym, fromBelow)
6161

62+
def Context_requiredPackage(self: Context)(path: String): Symbol = self.requiredPackage(path)
63+
def Context_requiredClass(self: Context)(path: String): Symbol = self.requiredClass(path)
64+
def Context_requiredModule(self: Context)(path: String): Symbol = self.requiredModule(path)
65+
def Context_requiredMethod(self: Context)(path: String): Symbol = self.requiredMethod(path)
66+
6267
//
6368
// REPORTING
6469
//

library/src/scala/internal/quoted/Matcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ private[quoted] object Matcher {
366366
*/
367367
private def patternsMatches(scrutinee: Tree, pattern: Tree)(given Context, Env): (Env, Matching) = (scrutinee, pattern) match {
368368
case (v1: Term, Unapply(TypeApply(Select(patternHole @ Ident("patternHole"), "unapply"), List(tpt)), Nil, Nil))
369-
if patternHole.symbol.owner.fullName == "scala.runtime.quoted.Matcher$" =>
369+
if patternHole.symbol.owner == summon[Context].requiredModule("scala.runtime.quoted.Matcher") =>
370370
(summon[Env], matched(v1.seal))
371371

372372
case (Ident("_"), Ident("_")) =>

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,18 @@ trait CompilerInterface {
148148
def Context_GADT_addToConstraint(self: Context)(syms: List[Symbol]): Boolean
149149
def Context_GADT_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type
150150

151+
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
152+
def Context_requiredPackage(self: Context)(path: String): Symbol
153+
154+
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
155+
def Context_requiredClass(self: Context)(path: String): Symbol
156+
157+
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
158+
def Context_requiredModule(self: Context)(path: String): Symbol
159+
160+
/** Get method symbol if method is either defined in current compilation run or present on classpath. */
161+
def Context_requiredMethod(self: Context)(path: String): Symbol
162+
151163
//
152164
// REPORTING
153165
//

library/src/scala/tasty/reflect/ContextOps.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ trait ContextOps extends Core {
1010
/** Returns the source file being compiled. The path is relative to the current working directory. */
1111
def source: java.nio.file.Path = internal.Context_source(self)
1212

13+
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
14+
def requiredPackage(path: String): Symbol = internal.Context_requiredPackage(self)(path)
15+
16+
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
17+
def requiredClass(path: String): Symbol = internal.Context_requiredClass(self)(path)
18+
19+
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
20+
def requiredModule(path: String): Symbol = internal.Context_requiredModule(self)(path)
21+
22+
/** Get method symbol if method is either defined in current compilation run or present on classpath. */
23+
def requiredMethod(path: String): Symbol = internal.Context_requiredMethod(self)(path)
24+
1325
}
1426

1527
/** Context of the macro expansion */

library/src/scala/tasty/reflect/SourceCodePrinter.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
356356
this += "throw "
357357
printTree(expr)
358358

359-
case Apply(fn, args) if fn.symbol.fullName == "scala.internal.Quoted$.exprQuote" =>
359+
case Apply(fn, args) if fn.symbol == ctx.requiredMethod("scala.internal.Quoted.exprQuote") =>
360360
args.head match {
361361
case Block(stats, expr) =>
362362
this += "'{"
@@ -371,12 +371,12 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
371371
this += "}"
372372
}
373373

374-
case TypeApply(fn, args) if fn.symbol.fullName == "scala.internal.Quoted$.typeQuote" =>
374+
case TypeApply(fn, args) if fn.symbol == ctx.requiredMethod("scala.internal.Quoted.typeQuote") =>
375375
this += "'["
376376
printTypeTree(args.head)
377377
this += "]"
378378

379-
case Apply(fn, arg :: Nil) if fn.symbol.fullName == "scala.internal.Quoted$.exprSplice" =>
379+
case Apply(fn, arg :: Nil) if fn.symbol == ctx.requiredMethod("scala.internal.Quoted.exprSplice") =>
380380
this += "${"
381381
printTree(arg)
382382
this += "}"
@@ -573,7 +573,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
573573
def printFlatBlock(stats: List[Statement], expr: Term)(given elideThis: Option[Symbol]): Buffer = {
574574
val (stats1, expr1) = flatBlock(stats, expr)
575575
val stats2 = stats1.filter {
576-
case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.owner.fullName == "scala.internal.Quoted$.quoteTypeTag")
576+
case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.owner == ctx.requiredClass("scala.internal.Quoted.quoteTypeTag"))
577577
case _ => true
578578
}
579579
if (stats2.isEmpty) {
@@ -971,7 +971,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
971971
printTypeAndAnnots(tp)
972972
this += " "
973973
printAnnotation(annot)
974-
case tpe: TypeRef if tpe.typeSymbol.fullName == "scala.runtime.Null$" || tpe.typeSymbol.fullName == "scala.runtime.Nothing$" =>
974+
case tpe: TypeRef if tpe.typeSymbol == ctx.requiredClass("scala.runtime.Null$") || tpe.typeSymbol == ctx.requiredClass("scala.runtime.Nothing$") =>
975975
// scala.runtime.Null$ and scala.runtime.Nothing$ are not modules, those are their actual names
976976
printType(tpe)
977977
case tpe: TermRef if tpe.termSymbol.isClassDef && tpe.termSymbol.name.endsWith("$") =>
@@ -1228,7 +1228,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12281228

12291229
def printAnnotation(annot: Term)(given elideThis: Option[Symbol]): Buffer = {
12301230
val Annotation(ref, args) = annot
1231-
if (annot.symbol.maybeOwner.fullName == "scala.internal.quoted.showName") this
1231+
if (annot.symbol.maybeOwner == ctx.requiredClass("scala.internal.quoted.showName")) this
12321232
else {
12331233
this += "@"
12341234
printTypeTree(ref)
@@ -1243,8 +1243,8 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12431243
val annots = definition.symbol.annots.filter {
12441244
case Annotation(annot, _) =>
12451245
annot.tpe match {
1246-
case TypeRef(prefix: TermRef, _) if prefix.termSymbol.fullName == "scala.annotation.internal" => false
1247-
case TypeRef(prefix: TypeRef, _) if prefix.typeSymbol.fullName == "scala.annotation.internal" => false
1246+
case TypeRef(prefix: TermRef, _) if prefix.termSymbol == ctx.requiredPackage("scala.annotation.internal") => false
1247+
case TypeRef(prefix: TypeRef, _) if prefix.typeSymbol == ctx.requiredPackage("scala.annotation.internal") => false
12481248
case TypeRef(Types.ScalaPackage(), "forceInline") => false
12491249
case _ => true
12501250
}
@@ -1404,7 +1404,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14041404
}
14051405

14061406
private def splicedName(sym: Symbol)(given ctx: Context): Option[String] = {
1407-
sym.annots.find(_.symbol.owner.fullName == "scala.internal.quoted.showName").flatMap {
1407+
sym.annots.find(_.symbol.owner == ctx.requiredClass("scala.internal.quoted.showName")).flatMap {
14081408
case Apply(_, Literal(Constant(c: String)) :: Nil) => Some(c)
14091409
case Apply(_, Inlined(_, _, Literal(Constant(c: String))) :: Nil) => Some(c)
14101410
case annot => None
@@ -1437,23 +1437,23 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14371437

14381438
object JavaLangObject {
14391439
def unapply(tpe: Type)(given ctx: Context): Boolean = tpe match {
1440-
case TypeRef(prefix: TermRef, "Object") => prefix.typeSymbol.fullName == "java.lang"
1440+
case TypeRef(prefix: TermRef, "Object") => prefix.typeSymbol == ctx.requiredPackage("java.lang")
14411441
case _ => false
14421442
}
14431443
}
14441444

14451445
object Sequence {
14461446
def unapply(tpe: Type)(given ctx: Context): Option[Type] = tpe match {
1447-
case AppliedType(TypeRef(prefix: TermRef, "Seq"), (tp: Type) :: Nil) if prefix.termSymbol.fullName == "scala.collection" => Some(tp)
1448-
case AppliedType(TypeRef(prefix: TypeRef, "Seq"), (tp: Type) :: Nil) if prefix.typeSymbol.fullName == "scala.collection" => Some(tp)
1447+
case AppliedType(TypeRef(prefix: TermRef, "Seq"), (tp: Type) :: Nil) if prefix.termSymbol == ctx.requiredPackage("scala.collection") => Some(tp)
1448+
case AppliedType(TypeRef(prefix: TypeRef, "Seq"), (tp: Type) :: Nil) if prefix.typeSymbol == ctx.requiredPackage("scala.collection") => Some(tp)
14491449
case _ => None
14501450
}
14511451
}
14521452

14531453
object RepeatedAnnotation {
14541454
def unapply(tpe: Type)(given ctx: Context): Boolean = tpe match {
1455-
case TypeRef(prefix: TermRef, "Repeated") => prefix.termSymbol.fullName == "scala.annotation.internal"
1456-
case TypeRef(prefix: TypeRef, "Repeated") => prefix.typeSymbol.fullName == "scala.annotation.internal"
1455+
case TypeRef(prefix: TermRef, "Repeated") => prefix.termSymbol == ctx.requiredPackage("scala.annotation.internal")
1456+
case TypeRef(prefix: TypeRef, "Repeated") => prefix.typeSymbol == ctx.requiredPackage("scala.annotation.internal")
14571457
case _ => false
14581458
}
14591459
}

0 commit comments

Comments
 (0)