Skip to content

Commit 168a416

Browse files
committed
Expose AbstractFile only in Run
1 parent 528d3b6 commit 168a416

File tree

5 files changed

+38
-40
lines changed

5 files changed

+38
-40
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,15 @@ class Driver {
3232

3333
protected def emptyReporter: Reporter = new StoreReporter(null)
3434

35-
protected def doCompile(compiler: Compiler, fileNames: List[String])(using ctx: Context): Reporter =
36-
if fileNames.nonEmpty then
37-
try
38-
val run = compiler.newRun
39-
run.compile(fileNames)
40-
finish(compiler, run)
41-
catch
42-
case ex: FatalError =>
43-
report.error(ex.getMessage) // signals that we should fail compilation.
44-
case ex: TypeError =>
45-
println(s"${ex.toMessage} while compiling ${fileNames.mkString(", ")}")
46-
throw ex
47-
case ex: Throwable =>
48-
println(s"$ex while compiling ${fileNames.mkString(", ")}")
49-
throw ex
50-
ctx.reporter
35+
protected def doCompile(compiler: Compiler, fileNames: List[String])(using Context): Reporter =
36+
val files = fileNames.map(ctx.getFile)
37+
doCompileFiles(compiler, files)
5138

5239
protected def doCompileFiles(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
5340
if files.nonEmpty then
5441
try
5542
val run = compiler.newRun
56-
run.compileFiles(files)
43+
run.compile(files)
5744
finish(compiler, run)
5845
catch
5946
case ex: FatalError =>

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
124124
/** Actions that need to be performed at the end of the current compilation run */
125125
private var finalizeActions = mutable.ListBuffer[() => Unit]()
126126

127-
def compile(fileNames: List[String]): Unit =
128-
val sources = fileNames.map(runContext.getSource(_))
129-
compileSources(sources)
130-
131-
def compileFiles(files: List[AbstractFile]): Unit =
127+
def compile(files: List[AbstractFile]): Unit =
132128
val sources = files.map(runContext.getSource(_))
133129
compileSources(sources)
134130

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,36 @@ object Contexts {
266266
base.sources.getOrElseUpdate(file, new SourceFile(file, codec))
267267
}
268268

269-
/** Sourcefile with given path name, memoized */
270-
def getSource(path: TermName): SourceFile = base.sourceNamed.get(path) match {
269+
/** SourceFile with given path name, memoized */
270+
def getSource(path: TermName): SourceFile = base.sourceNamed.get(path) match
271271
case Some(source) =>
272272
source
273-
case None => try {
274-
val f = new PlainFile(Path(path.toString))
275-
val src = getSource(f)
276-
base.sourceNamed(path) = src
277-
src
278-
} catch {
279-
case ex: InvalidPathException =>
280-
report.error(s"invalid file path: ${ex.getMessage}")
281-
NoSource
282-
}
283-
}
273+
case None =>
274+
getFile(path) match
275+
case NoAbstractFile => NoSource
276+
case file => getSource(file)
284277

285-
/** Sourcefile with given path, memoized */
278+
/** SourceFile with given path, memoized */
286279
def getSource(path: String): SourceFile = getSource(path.toTermName)
287280

281+
/** AbstraFile with given path name, memoized */
282+
def getFile(path: TermName): AbstractFile = base.files.get(path) match
283+
case Some(file) =>
284+
file
285+
case None =>
286+
try
287+
val file = new PlainFile(Path(path.toString))
288+
base.files(path) = file
289+
file
290+
catch
291+
case ex: InvalidPathException =>
292+
report.error(s"invalid file path: ${ex.getMessage}")
293+
NoAbstractFile
294+
295+
/** AbstractFile with given path, memoized */
296+
def getFile(path: String): AbstractFile = getFile(path.toTermName)
297+
298+
288299
private var related: SimpleIdentityMap[Phase | SourceFile, Context] = null
289300

290301
private def lookup(key: Phase | SourceFile): Context =
@@ -842,9 +853,10 @@ object Contexts {
842853
private var _nextSymId: Int = 0
843854
def nextSymId: Int = { _nextSymId += 1; _nextSymId }
844855

845-
/** Sources that were loaded */
856+
/** Sources and Files that were loaded */
846857
val sources: util.HashMap[AbstractFile, SourceFile] = util.HashMap[AbstractFile, SourceFile]()
847858
val sourceNamed: util.HashMap[TermName, SourceFile] = util.HashMap[TermName, SourceFile]()
859+
val files: util.HashMap[TermName, AbstractFile] = util.HashMap()
848860

849861
// Types state
850862
/** A table for hash consing unique types */
@@ -929,6 +941,7 @@ object Contexts {
929941
errorTypeMsg.clear()
930942
sources.clear()
931943
sourceNamed.clear()
944+
files.clear()
932945
comparers.clear() // forces re-evaluation of top and bottom classes in TypeComparer
933946

934947
// Test that access is single threaded

compiler/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
3131
val run = decompiler.newRun(using myInitCtx.fresh.setReporter(reporter))
3232

3333
inContext(run.runContext) {
34-
run.compile(List(className))
34+
val classFile = ctx.getFile(className)
35+
run.compile(List(classFile))
3536
run.printSummary()
3637
val unit = ctx.run.units.head
3738

compiler/src/dotty/tools/dotc/fromtasty/TASTYRun.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package dotty.tools
22
package dotc
33
package fromtasty
44

5+
import io.AbstractFile
56
import core.Contexts._
67

78
class TASTYRun(comp: Compiler, ictx: Context) extends Run(comp, ictx) {
8-
override def compile(classNames: List[String]): Unit = {
9-
val units = classNames.map(new TASTYCompilationUnit(_))
9+
override def compile(classFiles: List[AbstractFile]): Unit = {
10+
val units = classFiles.map(file => new TASTYCompilationUnit(file.path))
1011
compileUnits(units)
1112
}
1213
}

0 commit comments

Comments
 (0)