Skip to content

Commit 7354e84

Browse files
committed
Move symbol methods to Symbol
1 parent a2e7b73 commit 7354e84

File tree

5 files changed

+57
-55
lines changed

5 files changed

+57
-55
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
6262
def Context_GADT_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type =
6363
self.gadt.approximation(sym, fromBelow)
6464

65-
def Context_requiredPackage(self: Context)(path: String): Symbol = requiredPackage(path)(using self)
66-
def Context_requiredClass(self: Context)(path: String): Symbol = requiredClass(path)(using self)
67-
def Context_requiredModule(self: Context)(path: String): Symbol = requiredModule(path)(using self)
68-
def Context_requiredMethod(self: Context)(path: String): Symbol = requiredMethod(path)(using self)
6965
def Context_isJavaCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.JavaCompilationUnit]
7066
def Context_isScala2CompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.Scala2CompilationUnit]
7167
def Context_isAlreadyLoadedCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.AlreadyLoadedCompilationUnit]
@@ -1790,6 +1786,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
17901786

17911787
private def isField(sym: Symbol)(using Context): Boolean = sym.isTerm && !sym.is(Flags.Method)
17921788

1789+
def Symbol_requiredPackage(path: String)(using Context): Symbol = requiredPackage(path)
1790+
def Symbol_requiredClass(path: String)(using Context): Symbol = requiredClass(path)
1791+
def Symbol_requiredModule(path: String)(using Context): Symbol = requiredModule(path)
1792+
def Symbol_requiredMethod(path: String)(using Context): Symbol = requiredMethod(path)
1793+
17931794
def Symbol_of(fullName: String)(using Context): Symbol =
17941795
requiredClass(fullName)
17951796

library/src/scala/tasty/Reflection.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,18 +433,6 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
433433
/** Returns the source file being compiled. The path is relative to the current working directory. */
434434
def source: java.nio.file.Path = internal.Context_source(self)
435435

436-
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
437-
def requiredPackage(path: String): Symbol = internal.Context_requiredPackage(self)(path)
438-
439-
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
440-
def requiredClass(path: String): Symbol = internal.Context_requiredClass(self)(path)
441-
442-
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
443-
def requiredModule(path: String): Symbol = internal.Context_requiredModule(self)(path)
444-
445-
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
446-
def requiredMethod(path: String): Symbol = internal.Context_requiredMethod(self)(path)
447-
448436
/** Returns true if we've tried to reflect on a Java class. */
449437
def isJavaCompilationUnit(): Boolean = internal Context_isJavaCompilationUnit(self)
450438

@@ -2309,6 +2297,19 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
23092297
given SymbolOps as Symbol.type = Symbol
23102298

23112299
object Symbol:
2300+
2301+
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
2302+
def requiredPackage(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredPackage(path)
2303+
2304+
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
2305+
def requiredClass(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredClass(path)
2306+
2307+
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
2308+
def requiredModule(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredModule(path)
2309+
2310+
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
2311+
def requiredMethod(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredMethod(path)
2312+
23122313
/** The class Symbol of a global class definition */
23132314
def classSymbol(fullName: String)(using ctx: Context): Symbol =
23142315
internal.Symbol_of(fullName)

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

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

153-
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
154-
def Context_requiredPackage(self: Context)(path: String): Symbol
155-
156-
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
157-
def Context_requiredClass(self: Context)(path: String): Symbol
158-
159-
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
160-
def Context_requiredModule(self: Context)(path: String): Symbol
161-
162-
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
163-
def Context_requiredMethod(self: Context)(path: String): Symbol
164-
165153
/** Returns true if we've tried to reflect on a Java class. */
166154
def Context_isJavaCompilationUnit(self: Context): Boolean
167155

@@ -1342,6 +1330,18 @@ trait CompilerInterface {
13421330
/** Fields of a case class type -- only the ones declared in primary constructor */
13431331
def Symbol_caseFields(self: Symbol)(using ctx: Context): List[Symbol]
13441332

1333+
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
1334+
def Symbol_requiredPackage(path: String)(using ctx: Context): Symbol
1335+
1336+
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
1337+
def Symbol_requiredClass(path: String)(using ctx: Context): Symbol
1338+
1339+
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
1340+
def Symbol_requiredModule(path: String)(using ctx: Context): Symbol
1341+
1342+
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
1343+
def Symbol_requiredMethod(path: String)(using ctx: Context): Symbol
1344+
13451345
def Symbol_of(fullName: String)(using ctx: Context): Symbol
13461346

13471347
def Symbol_newMethod(parent: Symbol, name: String, flags: Flags, tpe: Type, privateWithin: Symbol)(using ctx: Context): Symbol

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
151151
}
152152

153153
val parents1 = parents.filter {
154-
case Apply(Select(New(tpt), _), _) => tpt.tpe.typeSymbol != ctx.requiredClass("java.lang.Object")
154+
case Apply(Select(New(tpt), _), _) => tpt.tpe.typeSymbol != Symbol.requiredClass("java.lang.Object")
155155
case TypeSelect(Select(Ident("_root_"), "scala"), "Product") => false
156156
case TypeSelect(Select(Ident("_root_"), "scala"), "Serializable") => false
157157
case _ => true
@@ -357,7 +357,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
357357
this += "throw "
358358
printTree(expr)
359359

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

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

380-
case Apply(fn, arg :: Nil) if fn.symbol == ctx.requiredMethod("scala.internal.Quoted.exprSplice") =>
380+
case Apply(fn, arg :: Nil) if fn.symbol == Symbol.requiredMethod("scala.internal.Quoted.exprSplice") =>
381381
this += "${"
382382
printTree(arg)
383383
this += "}"
@@ -582,7 +582,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
582582
def printFlatBlock(stats: List[Statement], expr: Term)(using elideThis: Option[Symbol]): Buffer = {
583583
val (stats1, expr1) = flatBlock(stats, expr)
584584
val stats2 = stats1.filter {
585-
case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.owner == ctx.requiredClass("scala.internal.Quoted.quoteTypeTag"))
585+
case tree: TypeDef => !tree.symbol.annots.exists(_.symbol.owner == Symbol.requiredClass("scala.internal.Quoted.quoteTypeTag"))
586586
case _ => true
587587
}
588588
if (stats2.isEmpty) {
@@ -980,7 +980,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
980980
printTypeAndAnnots(tp)
981981
this += " "
982982
printAnnotation(annot)
983-
case tpe: TypeRef if tpe.typeSymbol == ctx.requiredClass("scala.runtime.Null$") || tpe.typeSymbol == ctx.requiredClass("scala.runtime.Nothing$") =>
983+
case tpe: TypeRef if tpe.typeSymbol == Symbol.requiredClass("scala.runtime.Null$") || tpe.typeSymbol == Symbol.requiredClass("scala.runtime.Nothing$") =>
984984
// scala.runtime.Null$ and scala.runtime.Nothing$ are not modules, those are their actual names
985985
printType(tpe)
986986
case tpe: TermRef if tpe.termSymbol.isClassDef && tpe.termSymbol.name.endsWith("$") =>
@@ -1023,7 +1023,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
10231023
case Annotated(tpt, annot) =>
10241024
val Annotation(ref, args) = annot
10251025
ref.tpe match {
1026-
case tpe: TypeRef if tpe.typeSymbol == ctx.requiredClass("scala.annotation.internal.Repeated") =>
1026+
case tpe: TypeRef if tpe.typeSymbol == Symbol.requiredClass("scala.annotation.internal.Repeated") =>
10271027
val Types.Sequence(tp) = tpt.tpe
10281028
printType(tp)
10291029
this += highlightTypeDef("*")
@@ -1127,7 +1127,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
11271127
printType(tp)
11281128
this += ")"
11291129
inSquare(printTypesOrBounds(args, ", "))
1130-
case tp: TypeRef if tp.typeSymbol == ctx.requiredClass("scala.<repeated>") =>
1130+
case tp: TypeRef if tp.typeSymbol == Symbol.requiredClass("scala.<repeated>") =>
11311131
this += "_*"
11321132
case _ =>
11331133
printType(tp)
@@ -1240,7 +1240,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12401240

12411241
def printAnnotation(annot: Term)(using elideThis: Option[Symbol]): Buffer = {
12421242
val Annotation(ref, args) = annot
1243-
if (annot.symbol.maybeOwner == ctx.requiredClass("scala.internal.quoted.showName")) this
1243+
if (annot.symbol.maybeOwner == Symbol.requiredClass("scala.internal.quoted.showName")) this
12441244
else {
12451245
this += "@"
12461246
printTypeTree(ref)
@@ -1255,8 +1255,8 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
12551255
val annots = definition.symbol.annots.filter {
12561256
case Annotation(annot, _) =>
12571257
val sym = annot.tpe.typeSymbol
1258-
sym != ctx.requiredClass("scala.forceInline") &&
1259-
sym.maybeOwner != ctx.requiredPackage("scala.annotation.internal")
1258+
sym != Symbol.requiredClass("scala.forceInline") &&
1259+
sym.maybeOwner != Symbol.requiredPackage("scala.annotation.internal")
12601260
case x => throw new MatchError(x.showExtractors)
12611261
}
12621262
printAnnotations(annots)
@@ -1416,7 +1416,7 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14161416
private[this] val namesIndex = collection.mutable.Map.empty[String, Int]
14171417

14181418
private def splicedName(sym: Symbol)(using ctx: Context): Option[String] = {
1419-
sym.annots.find(_.symbol.owner == ctx.requiredClass("scala.internal.quoted.showName")).flatMap {
1419+
sym.annots.find(_.symbol.owner == Symbol.requiredClass("scala.internal.quoted.showName")).flatMap {
14201420
case Apply(_, Literal(Constant(c: String)) :: Nil) => Some(c)
14211421
case Apply(_, Inlined(_, _, Literal(Constant(c: String))) :: Nil) => Some(c)
14221422
case annot => None
@@ -1462,15 +1462,15 @@ class SourceCodePrinter[R <: Reflection & Singleton](val tasty: R)(syntaxHighlig
14621462
object Sequence {
14631463
def unapply(tpe: Type)(using ctx: Context): Option[Type] = tpe match {
14641464
case AppliedType(seq, (tp: Type) :: Nil)
1465-
if seq.typeSymbol == ctx.requiredClass("scala.collection.Seq") || seq.typeSymbol == ctx.requiredClass("scala.collection.immutable.Seq") =>
1465+
if seq.typeSymbol == Symbol.requiredClass("scala.collection.Seq") || seq.typeSymbol == Symbol.requiredClass("scala.collection.immutable.Seq") =>
14661466
Some(tp)
14671467
case _ => None
14681468
}
14691469
}
14701470

14711471
object Repeated {
14721472
def unapply(tpe: Type)(using ctx: Context): Option[Type] = tpe match {
1473-
case AppliedType(rep, (tp: Type) :: Nil) if rep.typeSymbol == ctx.requiredClass("scala.<repeated>") => Some(tp)
1473+
case AppliedType(rep, (tp: Type) :: Nil) if rep.typeSymbol == Symbol.requiredClass("scala.<repeated>") => Some(tp)
14741474
case _ => None
14751475
}
14761476
}

tests/run-macros/requiredSymbols/Macro_1.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ object Macro {
55
def fooImpl(using qctx: QuoteContext) : Expr[String] = {
66
import qctx.tasty._
77
val list = List(
8-
rootContext.requiredPackage("java"),
9-
rootContext.requiredPackage("java.lang"),
10-
rootContext.requiredPackage("scala"),
11-
rootContext.requiredPackage("scala.collection"),
8+
Symbol.requiredPackage("java"),
9+
Symbol.requiredPackage("java.lang"),
10+
Symbol.requiredPackage("scala"),
11+
Symbol.requiredPackage("scala.collection"),
1212

13-
rootContext.requiredClass("java.lang.Object"),
14-
rootContext.requiredClass("scala.Any"),
15-
rootContext.requiredClass("scala.AnyRef"),
16-
rootContext.requiredClass("scala.AnyVal"),
17-
rootContext.requiredClass("scala.Unit"),
18-
rootContext.requiredClass("scala.Null"),
13+
Symbol.requiredClass("java.lang.Object"),
14+
Symbol.requiredClass("scala.Any"),
15+
Symbol.requiredClass("scala.AnyRef"),
16+
Symbol.requiredClass("scala.AnyVal"),
17+
Symbol.requiredClass("scala.Unit"),
18+
Symbol.requiredClass("scala.Null"),
1919

20-
rootContext.requiredModule("scala.None"),
21-
rootContext.requiredModule("scala.Nil"),
20+
Symbol.requiredModule("scala.None"),
21+
Symbol.requiredModule("scala.Nil"),
2222

23-
rootContext.requiredMethod("scala.List.empty"),
23+
Symbol.requiredMethod("scala.List.empty"),
2424
)
2525
Expr(list.map(_.fullName).mkString("\n"))
2626
}

0 commit comments

Comments
 (0)