Skip to content

Commit 0c92a87

Browse files
Merge pull request #9545 from dotty-staging/move-out-methods-form-reflect-context
Move out methods form reflect context
2 parents 32bd2aa + 9409d66 commit 0c92a87

File tree

10 files changed

+122
-101
lines changed

10 files changed

+122
-101
lines changed

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,30 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
4949

5050
def Context_owner(self: Context): Symbol = self.owner
5151

52-
def Context_source(self: Context): java.nio.file.Path = self.compilationUnit.source.file.jpath
5352

54-
def Context_GADT_setFreshGADTBounds(self: Context): Context =
53+
/////////////////
54+
// Constraints //
55+
/////////////////
56+
57+
def Constraints_init(self: Context): Context =
5558
self.fresh.setFreshGADTBounds.addMode(Mode.GadtConstraintInference)
5659

57-
def Context_GADT_addToConstraint(self: Context)(syms: List[Symbol]): Boolean =
60+
def Constraints_add(self: Context)(syms: List[Symbol]): Boolean =
5861
self.gadt.addToConstraint(syms)
5962

60-
def Context_GADT_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type =
63+
def Constraints_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type =
6164
self.gadt.approximation(sym, fromBelow)
6265

63-
def Context_requiredPackage(self: Context)(path: String): Symbol = requiredPackage(path)(using self)
64-
def Context_requiredClass(self: Context)(path: String): Symbol = requiredClass(path)(using self)
65-
def Context_requiredModule(self: Context)(path: String): Symbol = requiredModule(path)(using self)
66-
def Context_requiredMethod(self: Context)(path: String): Symbol = requiredMethod(path)(using self)
67-
def Context_isJavaCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.JavaCompilationUnit]
68-
def Context_isScala2CompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.Scala2CompilationUnit]
69-
def Context_isAlreadyLoadedCompilationUnit(self: Context): Boolean = self.compilationUnit.isInstanceOf[fromtasty.AlreadyLoadedCompilationUnit]
70-
def Context_compilationUnitClassname(self: Context): String =
71-
self.compilationUnit match {
66+
////////////
67+
// Source //
68+
////////////
69+
70+
def Source_path(using Context): java.nio.file.Path = ctx.compilationUnit.source.file.jpath
71+
def Source_isJavaCompilationUnit(using Context): Boolean = ctx.compilationUnit.isInstanceOf[fromtasty.JavaCompilationUnit]
72+
def Source_isScala2CompilationUnit(using Context): Boolean = ctx.compilationUnit.isInstanceOf[fromtasty.Scala2CompilationUnit]
73+
def Source_isAlreadyLoadedCompilationUnit(using Context): Boolean = ctx.compilationUnit.isInstanceOf[fromtasty.AlreadyLoadedCompilationUnit]
74+
def Source_compilationUnitClassname(using Context): String =
75+
ctx.compilationUnit match {
7276
case cu: fromtasty.JavaCompilationUnit => cu.className
7377
case cu: fromtasty.Scala2CompilationUnit => cu.className
7478
case cu: fromtasty.AlreadyLoadedCompilationUnit => cu.className
@@ -1779,6 +1783,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
17791783

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

1786+
def Symbol_requiredPackage(path: String)(using Context): Symbol = requiredPackage(path)
1787+
def Symbol_requiredClass(path: String)(using Context): Symbol = requiredClass(path)
1788+
def Symbol_requiredModule(path: String)(using Context): Symbol = requiredModule(path)
1789+
def Symbol_requiredMethod(path: String)(using Context): Symbol = requiredMethod(path)
1790+
17821791
def Symbol_of(fullName: String)(using Context): Symbol =
17831792
requiredClass(fullName)
17841793

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ object Matcher {
147147
def termMatch(scrutineeTerm: Term, patternTerm: Term, hasTypeSplices: Boolean): Option[Tuple] = {
148148
given Env = Map.empty
149149
if (hasTypeSplices) {
150-
val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
150+
val ctx: Context = internal.Constraints_init(rootContext)
151151
given Context = ctx
152152
val matchings = scrutineeTerm =?= patternTerm
153153
// After matching and doing all subtype checks, we have to approximate all the type bindings
154154
// that we have found and seal them in a quoted.Type
155155
matchings.asOptionOfTuple.map { tup =>
156156
Tuple.fromArray(tup.toArray.map { // TODO improve performance
157-
case x: SymBinding => internal.Context_GADT_approximation(summon[Context])(x.sym, !x.fromAbove).seal
157+
case x: SymBinding => internal.Constraints_approximation(summon[Context])(x.sym, !x.fromAbove).seal
158158
case x => x
159159
})
160160
}
@@ -168,14 +168,14 @@ object Matcher {
168168
def typeTreeMatch(scrutineeTypeTree: TypeTree, patternTypeTree: TypeTree, hasTypeSplices: Boolean): Option[Tuple] = {
169169
given Env = Map.empty
170170
if (hasTypeSplices) {
171-
val ctx: Context = internal.Context_GADT_setFreshGADTBounds(rootContext)
171+
val ctx: Context = internal.Constraints_init(rootContext)
172172
given Context = ctx
173173
val matchings = scrutineeTypeTree =?= patternTypeTree
174174
// After matching and doing all subtype checks, we have to approximate all the type bindings
175175
// that we have found and seal them in a quoted.Type
176176
matchings.asOptionOfTuple.map { tup =>
177177
Tuple.fromArray(tup.toArray.map { // TODO improve performance
178-
case x: SymBinding => internal.Context_GADT_approximation(summon[Context])(x.sym, !x.fromAbove).seal
178+
case x: SymBinding => internal.Constraints_approximation(summon[Context])(x.sym, !x.fromAbove).seal
179179
case x => x
180180
})
181181
}
@@ -323,7 +323,7 @@ object Matcher {
323323
fn1 =?= fn2 &&& args1 =?= args2
324324

325325
case (Block(stats1, expr1), Block(binding :: stats2, expr2)) if isTypeBinding(binding) =>
326-
qctx.tasty.internal.Context_GADT_addToConstraint(summon[Context])(binding.symbol :: Nil)
326+
qctx.tasty.internal.Constraints_add(summon[Context])(binding.symbol :: Nil)
327327
matched(new SymBinding(binding.symbol, hasFromAboveAnnotation(binding.symbol))) &&& Block(stats1, expr1) =?= Block(stats2, expr2)
328328

329329
/* Match block */
@@ -340,7 +340,7 @@ object Matcher {
340340

341341
case (scrutinee, Block(typeBindings, expr2)) if typeBindings.forall(isTypeBinding) =>
342342
val bindingSymbols = typeBindings.map(_.symbol)
343-
qctx.tasty.internal.Context_GADT_addToConstraint(summon[Context])(bindingSymbols)
343+
qctx.tasty.internal.Constraints_add(summon[Context])(bindingSymbols)
344344
bindingSymbols.foldRight(scrutinee =?= expr2)((x, acc) => matched(new SymBinding(x, hasFromAboveAnnotation(x))) &&& acc)
345345

346346
/* Match if */

library/src/scala/tasty/Reflection.scala

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -426,35 +426,32 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
426426
extension (self: Context):
427427
/** Returns the owner of the context */
428428
def owner: Symbol = internal.Context_owner(self)
429+
end extension
430+
end Context
429431

430-
/** Returns the source file being compiled. The path is relative to the current working directory. */
431-
def source: java.nio.file.Path = internal.Context_source(self)
432432

433-
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
434-
def requiredPackage(path: String): Symbol = internal.Context_requiredPackage(self)(path)
433+
///////////////
434+
// Source //
435+
///////////////
435436

436-
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
437-
def requiredClass(path: String): Symbol = internal.Context_requiredClass(self)(path)
437+
object Source:
438438

439-
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
440-
def requiredModule(path: String): Symbol = internal.Context_requiredModule(self)(path)
439+
/** Returns the source file being compiled. The path is relative to the current working directory. */
440+
def path(using ctx: Context): java.nio.file.Path = internal.Source_path
441441

442-
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
443-
def requiredMethod(path: String): Symbol = internal.Context_requiredMethod(self)(path)
442+
/** Returns true if we've tried to reflect on a Java class. */
443+
def isJavaCompilationUnit(using ctx: Context): Boolean = internal.Source_isJavaCompilationUnit
444444

445-
/** Returns true if we've tried to reflect on a Java class. */
446-
def isJavaCompilationUnit(): Boolean = internal Context_isJavaCompilationUnit(self)
445+
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
446+
def isScala2CompilationUnit(using ctx: Context): Boolean = internal.Source_isScala2CompilationUnit
447447

448-
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
449-
def isScala2CompilationUnit(): Boolean = internal Context_isScala2CompilationUnit(self)
448+
/** Returns true if we've tried to reflect on a class that's already loaded (e.g. Option). */
449+
def isAlreadyLoadedCompilationUnit(using ctx: Context): Boolean = internal.Source_isAlreadyLoadedCompilationUnit
450450

451-
/** Returns true if we've tried to reflect on a class that's already loaded (e.g. Option). */
452-
def isAlreadyLoadedCompilationUnit(): Boolean = internal.Context_isAlreadyLoadedCompilationUnit(self)
451+
/** Class name of the current CompilationUnit */
452+
def compilationUnitClassname(using ctx: Context): String = internal.Source_compilationUnitClassname
453453

454-
/** Class name of the current CompilationUnit */
455-
def compilationUnitClassname(): String = internal.Context_compilationUnitClassname(self)
456-
end extension
457-
end Context
454+
end Source
458455

459456

460457
///////////////
@@ -2306,6 +2303,19 @@ class Reflection(private[scala] val internal: CompilerInterface) { self =>
23062303
given SymbolOps as Symbol.type = Symbol
23072304

23082305
object Symbol:
2306+
2307+
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
2308+
def requiredPackage(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredPackage(path)
2309+
2310+
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
2311+
def requiredClass(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredClass(path)
2312+
2313+
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
2314+
def requiredModule(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredModule(path)
2315+
2316+
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
2317+
def requiredMethod(path: String)(using ctx: Context): Symbol = internal.Symbol_requiredMethod(path)
2318+
23092319
/** The class Symbol of a global class definition */
23102320
def classSymbol(fullName: String)(using ctx: Context): Symbol =
23112321
internal.Symbol_of(fullName)

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,37 +141,32 @@ trait CompilerInterface {
141141
/** Returns the owner of the context */
142142
def Context_owner(self: Context): Symbol
143143

144-
/** Returns the source file being compiled. The path is relative to the current working directory. */
145-
def Context_source(self: Context): java.nio.file.Path
146-
147-
def Context_GADT_setFreshGADTBounds(self: Context): Context
148-
def Context_GADT_addToConstraint(self: Context)(syms: List[Symbol]): Boolean
149-
def Context_GADT_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type
150-
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
144+
/////////////////
145+
// Constraints //
146+
/////////////////
153147

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
148+
def Constraints_init(self: Context): Context
149+
def Constraints_add(self: Context)(syms: List[Symbol]): Boolean
150+
def Constraints_approximation(self: Context)(sym: Symbol, fromBelow: Boolean): Type
156151

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
152+
////////////
153+
// Source //
154+
////////////
159155

160-
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
161-
def Context_requiredMethod(self: Context)(path: String): Symbol
156+
/** Returns the source file being compiled. The path is relative to the current working directory. */
157+
def Source_path(using ctx: Context): java.nio.file.Path
162158

163159
/** Returns true if we've tried to reflect on a Java class. */
164-
def Context_isJavaCompilationUnit(self: Context): Boolean
160+
def Source_isJavaCompilationUnit(using ctx: Context): Boolean
165161

166162
/** Returns true if we've tried to reflect on a Scala2 (non-Tasty) class. */
167-
def Context_isScala2CompilationUnit(self: Context): Boolean
163+
def Source_isScala2CompilationUnit(using ctx: Context): Boolean
168164

169165
/** Returns true if we've tried to reflect on a class that's already loaded (e.g. Option). */
170-
def Context_isAlreadyLoadedCompilationUnit(self: Context): Boolean
166+
def Source_isAlreadyLoadedCompilationUnit(using ctx: Context): Boolean
171167

172168
/** Class name of the current CompilationUnit */
173-
def Context_compilationUnitClassname(self: Context): String
174-
169+
def Source_compilationUnitClassname(using ctx: Context): String
175170

176171
///////////////
177172
// REPORTING //
@@ -1330,6 +1325,18 @@ trait CompilerInterface {
13301325
/** Fields of a case class type -- only the ones declared in primary constructor */
13311326
def Symbol_caseFields(self: Symbol)(using ctx: Context): List[Symbol]
13321327

1328+
/** Get package symbol if package is either defined in current compilation run or present on classpath. */
1329+
def Symbol_requiredPackage(path: String)(using ctx: Context): Symbol
1330+
1331+
/** Get class symbol if class is either defined in current compilation run or present on classpath. */
1332+
def Symbol_requiredClass(path: String)(using ctx: Context): Symbol
1333+
1334+
/** Get module symbol if module is either defined in current compilation run or present on classpath. */
1335+
def Symbol_requiredModule(path: String)(using ctx: Context): Symbol
1336+
1337+
/** Get method symbol if method is either defined in current compilation run or present on classpath. Throws if the method has an overload. */
1338+
def Symbol_requiredMethod(path: String)(using ctx: Context): Symbol
1339+
13331340
def Symbol_of(fullName: String)(using ctx: Context): Symbol
13341341

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

0 commit comments

Comments
 (0)