Skip to content

Commit

Permalink
move lateCompile implementation to Namer
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Dec 13, 2021
1 parent 1f81426 commit 573b083
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
33 changes: 8 additions & 25 deletions compiler/src/dotty/tools/dotc/Run.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import rewrites.Rewrites

import profile.Profiler
import printing.XprintMode
import parsing.Parsers.Parser
import parsing.JavaParsers.JavaParser
import typer.ImplicitRunInfo
import config.Feature
import StdNames.nme
Expand Down Expand Up @@ -302,31 +300,16 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
.setCompilationUnit(unit)
.withRootImports

def process()(using Context) = {

def enterTrees()(using Context) =
ctx.typer.lateEnter(unit.untpdTree)
def typeCheckUnit()(using Context) =
unit.tpdTree = ctx.typer.typedExpr(unit.untpdTree)
val phase = new transform.SetRootTree()
phase.run
def process()(using Context) =
ctx.typer.lateEnterUnit(doTypeCheck =>
if typeCheck then
val typerCtx: Context =
// typer phase allows implicits to be searched
ctx.withPhase(Phases.typerPhase)
if compiling then finalizeActions += (() => typeCheckUnit()(using typerCtx))
else typeCheckUnit()(using typerCtx)

unit.untpdTree =
if (unit.isJava) new JavaParser(unit.source).parse()
else new Parser(unit.source).parse()
val namerCtx =
// inline body annotations are set in namer, capturing the current context
// we need to prepare the context for inlining.
if unit.isJava then ctx else PrepareInlineable.initContext(ctx)
enterTrees()(using namerCtx)
if compiling then finalizeActions += doTypeCheck
else doTypeCheck()
)

inContext(unitCtx) {
process()
}
process()(using unitCtx)
}

private sealed trait PrintedTree
Expand Down
47 changes: 39 additions & 8 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import tpd.tpes
import Variances.alwaysInvariant
import config.{Config, Feature}
import config.Printers.typr
import parsing.JavaParsers.JavaParser
import parsing.Parsers.Parser
import Annotations._
import Inferencing._
import transform.ValueClasses._
Expand Down Expand Up @@ -708,15 +710,44 @@ class Namer { typer: Typer =>
ctxWithStats
}

/** Index symbols in `tree` while asserting the `lateCompile` flag.
* This will cause any old top-level symbol with the same fully qualified
* name as a newly created symbol to be replaced.
/** Parse the source and index symbols in the compilation unit's untpdTree
* while asserting the `lateCompile` flag. This will cause any old
* top-level symbol with the same fully qualified name as a newly created
* symbol to be replaced.
*
* Will call the callback with an implementation of type checking
* That will set the tpdTree and root tree for the compilation unit.
*/
def lateEnter(tree: Tree)(using Context): Context = {
val saved = lateCompile
lateCompile = true
try index(tree :: Nil) finally lateCompile = saved
}
def lateEnterUnit(typeCheckCB: (() => Unit) => Unit)(using Context) =
val unit = ctx.compilationUnit

/** Index symbols in unit.untpdTree with lateCompile flag = true */
def lateEnter()(using Context): Context =
val saved = lateCompile
lateCompile = true
try index(unit.untpdTree :: Nil) finally lateCompile = saved

/** Set the tpdTree and root tree of the compilation unit */
def lateTypeCheck()(using Context) =
unit.tpdTree = typer.typedExpr(unit.untpdTree)
val phase = new transform.SetRootTree()
phase.run

unit.untpdTree =
if (unit.isJava) new JavaParser(unit.source).parse()
else new Parser(unit.source).parse()

atPhase(Phases.typerPhase) {
inContext(PrepareInlineable.initContext(ctx)) {
// inline body annotations are set in namer, capturing the current context
// we need to prepare the context for inlining.
lateEnter()
typeCheckCB { () =>
lateTypeCheck()
}
}
}
end lateEnterUnit

/** The type bound on wildcard imports of an import list, with special values
* Nothing if no wildcard imports of this kind exist
Expand Down

0 comments on commit 573b083

Please sign in to comment.