@@ -14,6 +14,7 @@ import dotty.tools.dotc.core.Phases.Phase
14
14
import dotty .tools .dotc .core .StdNames ._
15
15
import dotty .tools .dotc .core .Symbols ._
16
16
import dotty .tools .dotc .reporting .diagnostic .messages
17
+ import dotty .tools .dotc .transform .PostTyper
17
18
import dotty .tools .dotc .typer .{FrontEnd , ImportInfo }
18
19
import dotty .tools .dotc .util .Positions ._
19
20
import dotty .tools .dotc .util .SourceFile
@@ -26,39 +27,43 @@ import scala.collection.mutable
26
27
/** This subclass of `Compiler` replaces the appropriate phases in order to
27
28
* facilitate the REPL
28
29
*
29
- * Specifically it replaces the front end with `REPLFrontEnd`, and adds a
30
- * custom subclass of `GenBCode`. The custom `GenBCode`, `REPLGenBCode`, works
31
- * in conjunction with a specialized class loader in order to load virtual
32
- * classfiles.
30
+ * Specifically it replaces the front end with `REPLFrontEnd`.
33
31
*/
34
32
class ReplCompiler extends Compiler {
35
- override protected def frontendPhases : List [List [Phase ]] =
36
- Phases .replace(classOf [FrontEnd ], _ => new REPLFrontEnd :: Nil , super .frontendPhases)
37
33
38
- def newRun (initCtx : Context , objectIndex : Int ) = new Run (this , initCtx) {
39
- override protected [this ] def rootContext (implicit ctx : Context ) =
40
- addMagicImports(super .rootContext)
34
+ override protected def frontendPhases : List [List [Phase ]] = List (
35
+ List (new REPLFrontEnd ),
36
+ List (new CollectTopLevelImports ),
37
+ List (new PostTyper )
38
+ )
41
39
42
- private def addMagicImports (initCtx : Context ): Context = {
43
- def addImport (path : TermName )(implicit ctx : Context ) = {
44
- val importInfo = ImportInfo .rootImport { () =>
45
- ctx.requiredModuleRef(path)
46
- }
47
- ctx.fresh.setNewScope.setImportInfo(importInfo)
48
- }
40
+ def newRunContext (initCtx : Context , state : State ): Context = {
41
+ def addUserDefinedImport (imp : tpd.Import )(implicit ctx : Context ) =
42
+ ctx.importContext(imp, imp.symbol)
49
43
50
- (1 to objectIndex)
51
- .foldLeft(initCtx) { (ictx, i) =>
52
- addImport(nme.EMPTY_PACKAGE ++ " ." ++ objectNames(i))(ictx)
53
- }
44
+ def importModule (path : TermName )(implicit ctx : Context ) = {
45
+ val importInfo = ImportInfo .rootImport(() =>
46
+ ctx.requiredModuleRef(path))
47
+ ctx.fresh.setNewScope.setImportInfo(importInfo)
48
+ }
49
+
50
+ val run = newRun(initCtx.fresh.setReporter(newStoreReporter))
51
+ (1 to state.objectIndex).foldLeft(run.runContext) { (ctx, i) =>
52
+ // we first import the wrapper object i
53
+ val path = nme.EMPTY_PACKAGE ++ " ." ++ objectNames(i)
54
+ val ctx0 = importModule(path)(ctx)
55
+ // then its user defined imports
56
+ val imports = state.imports.getOrElse(i, Nil )
57
+ if (imports.isEmpty) ctx0
58
+ else imports.foldLeft(ctx0.fresh.setNewScope)((ctx, imp) =>
59
+ addUserDefinedImport(imp)(ctx))
54
60
}
55
61
}
56
62
57
63
private [this ] val objectNames = mutable.Map .empty[Int , TermName ]
58
64
private def objectName (state : State ) =
59
- objectNames.getOrElseUpdate(state.objectIndex, {
60
- (str.REPL_SESSION_LINE + state.objectIndex).toTermName
61
- })
65
+ objectNames.getOrElseUpdate(state.objectIndex,
66
+ (str.REPL_SESSION_LINE + state.objectIndex).toTermName)
62
67
63
68
private case class Definitions (stats : List [untpd.Tree ], state : State )
64
69
@@ -86,7 +91,7 @@ class ReplCompiler extends Compiler {
86
91
}
87
92
88
93
Definitions (
89
- state.imports ++ defs,
94
+ defs,
90
95
state.copy(
91
96
objectIndex = state.objectIndex + (if (defs.isEmpty) 0 else 1 ),
92
97
valIndex = valIdx
@@ -130,7 +135,7 @@ class ReplCompiler extends Compiler {
130
135
131
136
private def runCompilationUnit (unit : CompilationUnit , state : State ): Result [(CompilationUnit , State )] = {
132
137
val ctx = state.context
133
- ctx.run.compileUnits(unit :: Nil )
138
+ ctx.run.compileUnits(unit :: Nil , ctx )
134
139
135
140
if (! ctx.reporter.hasErrors) (unit, state).result
136
141
else ctx.reporter.removeBufferedMessages(ctx).errors
@@ -158,19 +163,18 @@ class ReplCompiler extends Compiler {
158
163
def docOf (expr : String )(implicit state : State ): Result [String ] = {
159
164
implicit val ctx : Context = state.context
160
165
161
- /**
162
- * Extract the "selected" symbol from `tree`.
166
+ /** Extract the "selected" symbol from `tree`.
163
167
*
164
- * Because the REPL typechecks an expression, special syntax is needed to get the documentation
165
- * of certain symbols:
168
+ * Because the REPL typechecks an expression, special syntax is needed to get the documentation
169
+ * of certain symbols:
166
170
*
167
- * - To select the documentation of classes, the user needs to pass a call to the class' constructor
168
- * (e.g. `new Foo` to select `class Foo`)
169
- * - When methods are overloaded, the user needs to enter a lambda to specify which functions he wants
170
- * (e.g. `foo(_: Int)` to select `def foo(x: Int)` instead of `def foo(x: String)`
171
+ * - To select the documentation of classes, the user needs to pass a call to the class' constructor
172
+ * (e.g. `new Foo` to select `class Foo`)
173
+ * - When methods are overloaded, the user needs to enter a lambda to specify which functions he wants
174
+ * (e.g. `foo(_: Int)` to select `def foo(x: Int)` instead of `def foo(x: String)`
171
175
*
172
- * This function returns the right symbol for the received expression, and all the symbols that are
173
- * overridden.
176
+ * This function returns the right symbol for the received expression, and all the symbols that are
177
+ * overridden.
174
178
*/
175
179
def extractSymbols (tree : tpd.Tree ): Iterator [Symbol ] = {
176
180
val sym = tree match {
@@ -210,7 +214,7 @@ class ReplCompiler extends Compiler {
210
214
import untpd ._
211
215
212
216
val valdef = ValDef (" expr" .toTermName, TypeTree (), Block (trees, unitLiteral))
213
- val tmpl = Template (emptyConstructor, Nil , EmptyValDef , state.imports :+ valdef)
217
+ val tmpl = Template (emptyConstructor, Nil , EmptyValDef , List ( valdef) )
214
218
val wrapper = TypeDef (" $wrapper" .toTypeName, tmpl)
215
219
.withMods(Modifiers (Final ))
216
220
.withPos(Position (0 , expr.length))
@@ -261,9 +265,8 @@ class ReplCompiler extends Compiler {
261
265
262
266
if (errorsAllowed || ! ctx.reporter.hasErrors)
263
267
unwrapped(unit.tpdTree, src)
264
- else {
268
+ else
265
269
ctx.reporter.removeBufferedMessages.errors
266
- }
267
270
}
268
271
}
269
272
}
0 commit comments