Skip to content

Commit 977eb84

Browse files
committed
Refactor getSource and withSource
1 parent 2c45db3 commit 977eb84

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,8 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
9999
/** Actions that need to be performed at the end of the current compilation run */
100100
private[this] var finalizeActions = mutable.ListBuffer[() => Unit]()
101101

102-
def getSource(fileName: String): SourceFile = {
103-
val f = new PlainFile(io.Path(fileName))
104-
if (f.isDirectory) {
105-
ctx.error(s"expected file, received directory '$fileName'")
106-
NoSource
107-
}
108-
else if (f.exists)
109-
ctx.getSource(f)
110-
else {
111-
ctx.error(s"not found: $fileName")
112-
NoSource
113-
}
114-
}
115-
116102
def compile(fileNames: List[String]): Unit = try {
117-
val sources = fileNames map getSource
103+
val sources = fileNames.map(ctx.getSource)
118104
compileSources(sources)
119105
} catch {
120106
case NonFatal(ex) =>
@@ -206,7 +192,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
206192
def lateCompile(file: AbstractFile, typeCheck: Boolean)(implicit ctx: Context): Unit =
207193
if (!files.contains(file) && !lateFiles.contains(file)) {
208194
lateFiles += file
209-
val unit = new CompilationUnit(getSource(file.path))
195+
val unit = new CompilationUnit(ctx.getSource(file.path))
210196
def process()(implicit ctx: Context) = {
211197
unit.untpdTree =
212198
if (unit.isJava) new JavaParser(unit.source).parse()

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import config.Settings._
2020
import config.Config
2121
import reporting._
2222
import reporting.diagnostic.Message
23-
import io.AbstractFile
23+
import io.{AbstractFile, PlainFile, Path}
2424
import scala.io.Codec
2525
import collection.mutable
2626
import printing._
@@ -268,6 +268,20 @@ object Contexts {
268268
def getSource(file: AbstractFile, codec: => Codec = Codec(settings.encoding.value)) =
269269
base.sources.getOrElseUpdate(file, new SourceFile(file, codec))
270270

271+
def getSource(fileName: String): SourceFile = {
272+
val f = new PlainFile(Path(fileName))
273+
if (f.isDirectory) {
274+
error(s"expected file, received directory '$fileName'")
275+
NoSource
276+
}
277+
else if (f.exists)
278+
getSource(f)
279+
else {
280+
error(s"not found: $fileName")
281+
NoSource
282+
}
283+
}
284+
271285
/** Those fields are used to cache phases created in withPhase.
272286
* phasedCtx is first phase with altered phase ever requested.
273287
* phasedCtxs is array that uses phaseId's as indexes,
@@ -452,8 +466,19 @@ object Contexts {
452466
final def withOwner(owner: Symbol): Context =
453467
if (owner ne this.owner) fresh.setOwner(owner) else this
454468

469+
private[this] var sourceCtx: SimpleIdentityMap[SourceFile, Context] = SimpleIdentityMap.Empty
470+
455471
final def withSource(source: SourceFile): Context =
456-
if (source ne this.source) fresh.setSource(source) else this
472+
if (source `eq` this.source) this
473+
else {
474+
val prev = sourceCtx(source)
475+
if (prev != null) prev
476+
else {
477+
val newCtx = fresh.setSource(source)
478+
sourceCtx = sourceCtx.updated(source, newCtx)
479+
newCtx
480+
}
481+
}
457482

458483
final def withProperty[T](key: Key[T], value: Option[T]): Context =
459484
if (property(key) == value) this

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ object SymbolLoaders {
159159
Nil)
160160
}
161161

162-
val unit = new CompilationUnit(ctx.run.getSource(src.path))
162+
val unit = new CompilationUnit(ctx.getSource(src.path))
163163
enterScanned(unit)(ctx.run.runContext.fresh.setCompilationUnit(unit))
164164
}
165165
}

0 commit comments

Comments
 (0)