Skip to content

Commit 0f0618b

Browse files
committed
Extract parser into its own phase
1 parent fcc35d2 commit 0f0618b

File tree

9 files changed

+55
-57
lines changed

9 files changed

+55
-57
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import transform._
99
import dotty.tools.backend.jvm.{CollectSuperCalls, GenBCode}
1010
import dotty.tools.backend.sjs
1111
import dotty.tools.dotc.transform.localopt.StringInterpolatorOpt
12+
import dotty.tools.dotc.parsing.Parsing
1213

1314
/** The central class of the dotc compiler. The job of a compiler is to create
1415
* runs, which process given `phases` in a given `rootContext`.
@@ -36,6 +37,7 @@ class Compiler {
3637

3738
/** Phases dealing with the frontend up to trees ready for TASTY pickling */
3839
protected def frontendPhases: List[List[Phase]] =
40+
List(new Parsing) ::
3941
List(new FrontEnd) :: // Compiler frontend: scanner, parser, namer, typer
4042
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
4143
List(new PostTyper) :: // Additional checks and cleanups after type checking

compiler/src/dotty/tools/dotc/Run.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
169169
Stats.trackTime(s"$phase ms ") {
170170
val start = System.currentTimeMillis
171171
val profileBefore = profiler.beforePhase(phase)
172-
units = phase.runOn(units)
172+
units = phase.runOn(units)(ctx.fresh.setPhase(phase.start))
173173
profiler.afterPhase(phase, profileBefore)
174174
if (ctx.settings.Xprint.value.containsPhase(phase)) {
175175
for (unit <- units) {
@@ -231,7 +231,10 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
231231
val unit = ctx.compilationUnit
232232
val prevPhase = ctx.phase.prev // can be a mini-phase
233233
val squashedPhase = ctx.base.squashed(prevPhase)
234-
val treeString = unit.tpdTree.show(ctx.withProperty(XprintMode, Some(())))
234+
val tree =
235+
if (ctx.isAfterTyper) unit.tpdTree
236+
else unit.untpdTree
237+
val treeString = tree.show(ctx.withProperty(XprintMode, Some(())))
235238

236239
ctx.echo(s"result of $unit after $squashedPhase:")
237240

compiler/src/dotty/tools/dotc/core/Phases.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Denotations._
1010
import Decorators._
1111
import config.Printers.config
1212
import scala.collection.mutable.ListBuffer
13+
import scala.util.control.NonFatal
1314
import dotty.tools.dotc.transform.MegaPhase._
1415
import dotty.tools.dotc.transform._
1516
import Periods._
@@ -293,7 +294,7 @@ object Phases {
293294
/** @pre `isRunnable` returns true */
294295
def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
295296
units.map { unit =>
296-
val unitCtx = ctx.fresh.setPhase(this.start).setCompilationUnit(unit)
297+
val unitCtx = ctx.fresh.setCompilationUnit(unit)
297298
run(unitCtx)
298299
unitCtx.compilationUnit
299300
}
@@ -308,7 +309,7 @@ object Phases {
308309
def checkPostCondition(tree: tpd.Tree)(implicit ctx: Context): Unit = ()
309310

310311
/** Is this phase the standard typerphase? True for FrontEnd, but
311-
* not for other first phases (such as FromTasty). The predicate
312+
* not for other first phases (such as ReadTasty). The predicate
312313
* is tested in some places that perform checks and corrections. It's
313314
* different from isAfterTyper (and cheaper to test).
314315
*/
@@ -388,6 +389,14 @@ object Phases {
388389
final def iterator: Iterator[Phase] =
389390
Iterator.iterate(this)(_.next) takeWhile (_.hasNext)
390391

392+
final def monitor(doing: String)(body: => Unit)(implicit ctx: Context): Unit =
393+
try body
394+
catch {
395+
case NonFatal(ex) =>
396+
ctx.echo(s"exception occurred while $doing ${ctx.compilationUnit}")
397+
throw ex
398+
}
399+
391400
override def toString: String = phaseName
392401
}
393402

compiler/src/dotty/tools/dotc/interactive/InteractiveCompiler.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ package interactive
55
import core._
66
import Phases._
77
import typer._
8+
import parsing.Parsing
89

910
class InteractiveCompiler extends Compiler {
1011
// TODO: Figure out what phases should be run in IDEs
1112
// More phases increase latency but allow us to report more errors.
1213
// This could be improved by reporting errors back to the IDE
1314
// after each phase group instead of waiting for the pipeline to finish.
1415
override def phases: List[List[Phase]] = List(
16+
List(new Parsing),
1517
List(new FrontEnd),
1618
List(new transform.SetRootTree),
1719
List(new transform.CookComments)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package dotty.tools.dotc.parsing
2+
3+
import dotty.tools.dotc.config.Config
4+
import dotty.tools.dotc.core.Contexts.Context
5+
import dotty.tools.dotc.core.Phases.Phase
6+
import dotty.tools.dotc.parsing.JavaParsers.JavaParser
7+
import dotty.tools.dotc.parsing.Parsers.Parser
8+
9+
class Parsing extends Phase {
10+
11+
override def phaseName: String = "parsing"
12+
13+
/** We run TreeChecker only after type checking */
14+
override def isCheckable: Boolean = false
15+
16+
override def isRunnable(implicit ctx: Context): Boolean =
17+
!ctx.settings.fromTasty.value
18+
19+
override def run(implicit ctx: Context): Unit = monitor("parsing") {
20+
val unit = ctx.compilationUnit
21+
unit.untpdTree =
22+
if (unit.isJava) new JavaParser(unit.source).parse()
23+
else new Parser(unit.source).parse()
24+
25+
if (Config.checkPositions)
26+
unit.untpdTree.checkPos(nonOverlapping = !unit.isJava && !ctx.reporter.hasErrors)
27+
}
28+
}

compiler/src/dotty/tools/dotc/typer/FrontEnd.scala

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ import core._
66
import Phases._
77
import Contexts._
88
import Symbols._
9-
import dotty.tools.dotc.parsing.JavaParsers.JavaParser
10-
import parsing.Parsers.Parser
119
import config.Config
1210
import config.Printers.{typr, default}
1311
import util.Stats._
14-
import scala.util.control.NonFatal
1512
import ast.Trees._
1613

1714
class FrontEnd extends Phase {
@@ -31,32 +28,13 @@ class FrontEnd extends Phase {
3128
def stillToBeEntered(name: String): Boolean =
3229
remaining.exists(_.compilationUnit.toString.endsWith(name + ".scala"))
3330

34-
def monitor(doing: String)(body: => Unit)(implicit ctx: Context): Unit =
35-
try body
36-
catch {
37-
case NonFatal(ex) =>
38-
ctx.echo(s"exception occurred while $doing ${ctx.compilationUnit}")
39-
throw ex
40-
}
41-
42-
def parse(implicit ctx: Context): Unit = monitor("parsing") {
43-
val unit = ctx.compilationUnit
44-
unit.untpdTree =
45-
if (unit.isJava) new JavaParser(unit.source).parse()
46-
else new Parser(unit.source).parse()
47-
val printer = if (ctx.settings.Xprint.value.contains("parser")) default else typr
48-
printer.println("parsed:\n" + unit.untpdTree.show)
49-
if (Config.checkPositions)
50-
unit.untpdTree.checkPos(nonOverlapping = !unit.isJava && !ctx.reporter.hasErrors)
51-
}
52-
53-
def enterSyms(implicit ctx: Context): Unit = monitor("indexing") {
31+
private def enterSyms(implicit ctx: Context): Unit = monitor("indexing") {
5432
val unit = ctx.compilationUnit
5533
ctx.typer.index(unit.untpdTree)
5634
typr.println("entered: " + unit.source)
5735
}
5836

59-
def typeCheck(implicit ctx: Context): Unit = monitor("typechecking") {
37+
private def typeCheck(implicit ctx: Context): Unit = monitor("typechecking") {
6038
val unit = ctx.compilationUnit
6139
unit.tpdTree = ctx.typer.typedExpr(unit.untpdTree)
6240
typr.println("typed: " + unit.source)
@@ -79,7 +57,7 @@ class FrontEnd extends Phase {
7957
ctx.inform(s"compiling ${unit.source}")
8058
ctx.fresh.setCompilationUnit(unit)
8159
}
82-
unitContexts foreach (parse(_))
60+
8361
record("parsedTrees", ast.Trees.ntrees)
8462
remaining = unitContexts
8563
while (remaining.nonEmpty) {

compiler/src/dotty/tools/repl/ReplCompiler.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import dotty.tools.dotc.core.StdNames._
1313
import dotty.tools.dotc.core.Symbols._
1414
import dotty.tools.dotc.reporting.diagnostic.messages
1515
import dotty.tools.dotc.transform.PostTyper
16-
import dotty.tools.dotc.typer.ImportInfo
16+
import dotty.tools.dotc.typer.{FrontEnd, ImportInfo}
1717
import dotty.tools.dotc.util.Positions._
1818
import dotty.tools.dotc.util.{ParsedComment, SourceFile}
1919
import dotty.tools.dotc.{CompilationUnit, Compiler, Run}
@@ -32,7 +32,7 @@ import scala.collection.mutable
3232
class ReplCompiler extends Compiler {
3333

3434
override protected def frontendPhases: List[List[Phase]] = List(
35-
List(new REPLFrontEnd),
35+
List(new FrontEnd),
3636
List(new CollectTopLevelImports),
3737
List(new PostTyper)
3838
)

compiler/src/dotty/tools/repl/ReplFrontEnd.scala

Lines changed: 0 additions & 26 deletions
This file was deleted.

doc-tool/src/dotty/tools/dottydoc/DocCompiler.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotc.typer.FrontEnd
1111

1212
import dotty.tools.dotc.fromtasty.{ReadTasty, TASTYRun}
1313
import dotty.tools.dotc.transform.CookComments
14+
import dotty.tools.dotc.parsing.Parsing
1415

1516
/** Custom Compiler with phases for the documentation tool
1617
*
@@ -53,6 +54,7 @@ class DocCompiler extends Compiler {
5354
}
5455

5556
override def phases: List[List[Phase]] = List(
57+
List(new Parsing),
5658
List(new DocFrontEnd),
5759
List(new ReadTasty),
5860
List(new CookComments),

0 commit comments

Comments
 (0)