Skip to content

Commit 1dae662

Browse files
committed
Separate NamerPhase
1 parent a3d928e commit 1dae662

File tree

10 files changed

+102
-19
lines changed

10 files changed

+102
-19
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import ast.{tpd, untpd}
1111
import tpd.{Tree, TreeTraverser}
1212
import typer.PrepareInlineable.InlineAccessors
1313
import typer.Nullables
14+
import transform.AccessProxies
1415
import transform.SymUtils._
1516
import core.Decorators._
1617
import config.SourceVersion
@@ -62,7 +63,7 @@ class CompilationUnit protected (val source: SourceFile) {
6263
/** Can this compilation unit be suspended */
6364
def isSuspendable: Boolean = true
6465

65-
/** Suspends the compilation unit by thowing a SuspendException
66+
/** Suspends the compilation unit by throwing a SuspendException
6667
* and recording the suspended compilation unit
6768
*/
6869
def suspend()(using Context): Nothing =
@@ -85,6 +86,9 @@ class CompilationUnit protected (val source: SourceFile) {
8586
def assignmentSpans(using Context): Map[Int, List[Span]] =
8687
if myAssignmentSpans == null then myAssignmentSpans = Nullables.assignmentSpans
8788
myAssignmentSpans
89+
90+
private[dotc] var inlineAccessors: InlineAccessors = null
91+
private[dotc] var protectedAccessors: AccessProxies = null
8892
}
8993

9094
object CompilationUnit {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33

44
import core._
55
import Contexts._
6-
import typer.{TyperPhase, RefChecks}
6+
import typer.{NamerPhase, TyperPhase, RefChecks}
77
import parsing.Parser
88
import Phases.Phase
99
import transform._
@@ -38,7 +38,8 @@ class Compiler {
3838
/** Phases dealing with the frontend up to trees ready for TASTY pickling */
3939
protected def frontendPhases: List[List[Phase]] =
4040
List(new Parser) :: // Compiler frontend: scanner, parser
41-
List(new TyperPhase) :: // Compiler frontend: namer, typer
41+
List(new NamerPhase) :: // Compiler frontend: namer
42+
List(new TyperPhase) :: // Compiler frontend: typer
4243
List(new YCheckPositions) :: // YCheck positions
4344
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
4445
List(new semanticdb.ExtractSemanticDB) :: // Extract info into .semanticdb files

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class InteractiveCompiler extends Compiler {
1414
// after each phase group instead of waiting for the pipeline to finish.
1515
override def phases: List[List[Phase]] = List(
1616
List(new Parser),
17+
List(new NamerPhase),
1718
List(new TyperPhase),
1819
List(new transform.SetRootTree),
1920
List(new transform.CookComments)

compiler/src/dotty/tools/dotc/transform/ProtectedAccessors.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ class ProtectedAccessors extends MiniPhase {
5757
ctx.property(AccessorsKey).get
5858

5959
override def prepareForUnit(tree: Tree)(using Context): Context =
60-
ctx.fresh.setProperty(AccessorsKey, new Accessors)
60+
var acc = ctx.compilationUnit.protectedAccessors.asInstanceOf[Accessors]
61+
if (acc == null)
62+
acc = new Accessors()
63+
ctx.compilationUnit.protectedAccessors = acc
64+
ctx.fresh.setProperty(AccessorsKey, acc)
6165

6266
private class Accessors extends AccessProxies {
6367
val insert: Insert = new Insert {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ object Inliner {
6868
&& StagingContext.level == 0
6969
&& (
7070
ctx.phase == Phases.inliningPhase
71-
|| (ctx.phase == Phases.typerPhase && needsTransparentInlining(tree))
71+
|| (ctx.phase.isTyper && needsTransparentInlining(tree))
7272
)
7373
&& !ctx.typer.hasInliningErrors
7474
&& !ctx.base.stopInlining
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package dotty.tools
2+
package dotc
3+
package typer
4+
5+
import core.*
6+
import Phases.*
7+
import Contexts.*
8+
import Symbols.*
9+
import Decorators.*
10+
import ImportInfo.withRootImports
11+
import parsing.JavaParsers.JavaParser
12+
import parsing.Parsers.Parser
13+
import parsing.Parser as ParserPhase
14+
import config.Config
15+
import config.Printers.{ default, typr }
16+
import util.Stats.*
17+
import util.{ NoSourcePosition, SourcePosition }
18+
import scala.util.control.NonFatal
19+
20+
import ast.Trees.*
21+
import dotty.tools.dotc.core.Denotations.SingleDenotation
22+
23+
/**
24+
*
25+
* @param addRootImports Set to false in the REPL. Calling [[ImportInfo.withRootImports]] on the [[Context]]
26+
* for each [[CompilationUnit]] causes dotty.tools.repl.ScriptedTests to fail.
27+
*/
28+
class NamerPhase(addRootImports: Boolean = true) extends Phase {
29+
30+
override def phaseName: String = NamerPhase.name
31+
override def isTyper: Boolean = true
32+
33+
// We run TreeChecker only after type checking
34+
override def isCheckable: Boolean = false
35+
36+
override def allowsImplicitSearch: Boolean = true
37+
38+
// Run regardless of parsing errors
39+
override def isRunnable(implicit ctx: Context): Boolean = true
40+
41+
def enterSyms(using Context): Unit = monitor("indexing") {
42+
val unit = ctx.compilationUnit
43+
ctx.typer.index(unit.untpdTree)
44+
typr.println("entered: " + unit.source)
45+
}
46+
47+
override def runOn(units: List[CompilationUnit])(using Context): List[CompilationUnit] =
48+
val unitContexts =
49+
for unit <- units yield
50+
val newCtx0 = ctx.fresh.setPhase(this.start).setCompilationUnit(unit)
51+
val newCtx = PrepareInlineable.initContext(newCtx0)
52+
report.inform(s"naming ${unit.source}")
53+
if (addRootImports)
54+
newCtx.withRootImports
55+
else
56+
newCtx
57+
58+
unitContexts.foreach(enterSyms(using _))
59+
60+
ctx.base.parserPhase match {
61+
case p: ParserPhase =>
62+
if p.firstXmlPos.exists && !defn.ScalaXmlPackageClass.exists then
63+
report.error(
64+
"""To support XML literals, your project must depend on scala-xml.
65+
|See https://github.com/scala/scala-xml for more information.""".stripMargin,
66+
p.firstXmlPos)
67+
case _ =>
68+
}
69+
70+
71+
unitContexts.map(_.compilationUnit)
72+
73+
def run(using Context): Unit = unsupported("run")
74+
}
75+
76+
object NamerPhase {
77+
val name: String = "namer"
78+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ object PrepareInlineable {
2929
private val InlineAccessorsKey = new Property.Key[InlineAccessors]
3030

3131
def initContext(ctx: Context): Context =
32-
ctx.fresh.setProperty(InlineAccessorsKey, new InlineAccessors)
32+
var acc = ctx.compilationUnit.inlineAccessors
33+
if (acc == null)
34+
ctx.compilationUnit.inlineAccessors = new InlineAccessors()
35+
acc = ctx.compilationUnit.inlineAccessors
36+
ctx.fresh.setProperty(InlineAccessorsKey, acc)
3337

3438
def makeInlineable(tree: Tree)(using Context): Tree =
3539
ctx.property(InlineAccessorsKey).get.makeInlineable(tree)

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ class TyperPhase(addRootImports: Boolean = true) extends Phase {
7373
else
7474
newCtx
7575

76-
unitContexts.foreach(enterSyms(using _))
77-
78-
ctx.base.parserPhase match {
79-
case p: ParserPhase =>
80-
if p.firstXmlPos.exists && !defn.ScalaXmlPackageClass.exists then
81-
report.error(
82-
"""To support XML literals, your project must depend on scala-xml.
83-
|See https://github.com/scala/scala-xml for more information.""".stripMargin,
84-
p.firstXmlPos)
85-
case _ =>
86-
}
8776

8877
unitContexts.foreach(typeCheck(using _))
8978
record("total trees after typer", ast.Trees.ntrees)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import dotty.tools.dotc.core.Symbols._
1414
import dotty.tools.dotc.reporting.Diagnostic
1515
import dotty.tools.dotc.transform.{PostTyper, Staging}
1616
import dotty.tools.dotc.typer.ImportInfo._
17-
import dotty.tools.dotc.typer.TyperPhase
17+
import dotty.tools.dotc.typer.{NamerPhase, TyperPhase}
1818
import dotty.tools.dotc.util.Spans._
1919
import dotty.tools.dotc.util.{ParsedComment, SourceFile}
2020
import dotty.tools.dotc.{CompilationUnit, Compiler, Run}
@@ -33,6 +33,7 @@ import scala.collection.mutable
3333
class ReplCompiler extends Compiler {
3434

3535
override protected def frontendPhases: List[List[Phase]] = List(
36+
List(new NamerPhase(addRootImports = false)),
3637
List(new TyperPhase(addRootImports = false)),
3738
List(new CollectTopLevelImports),
3839
List(new PostTyper),

docs/docs/internals/overall-structure.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ phases. The current list of phases is specified in class [Compiler] as follows:
9898
/** Phases dealing with the frontend up to trees ready for TASTY pickling */
9999
protected def frontendPhases: List[List[Phase]] =
100100
List(new Parser) :: // scanner, parser
101-
List(new TyperPhase) :: // namer, typer
101+
List(new NamerPhase) :: // namer
102+
List(new TyperPhase) :: // typer
102103
List(new YCheckPositions) :: // YCheck positions
103104
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
104105
List(new semanticdb.ExtractSemanticDB) :: // Extract info into .semanticdb files

0 commit comments

Comments
 (0)