Skip to content

Commit ef81ccf

Browse files
committed
Expose AbstractFile only in Run
1 parent 5185d4d commit ef81ccf

File tree

22 files changed

+196
-173
lines changed

22 files changed

+196
-173
lines changed

bench/src/main/scala/Benchmarks.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import scala.collection.JavaConverters._
1717
import scala.io.Source
1818
import scala.util.Using
1919

20+
import dotty.tools.io.AbstractFile
21+
2022
object Bench {
2123
val COMPILE_OPTS_FILE = "compile.txt"
2224

@@ -93,11 +95,11 @@ class CompilerOptions {
9395

9496
class Worker extends Driver {
9597
// override to avoid printing summary information
96-
override def doCompile(compiler: Compiler, fileNames: List[String])(implicit ctx: Context): Reporter =
97-
if (fileNames.nonEmpty)
98+
override def doCompile(compiler: Compiler, files: List[AbstractFile])(implicit ctx: Context): Reporter =
99+
if (files.nonEmpty)
98100
try {
99101
val run = compiler.newRun
100-
run.compile(fileNames)
102+
run.compile(files)
101103
ctx.reporter
102104
}
103105
catch {

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

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

44
import core.Contexts._
55
import reporting.Reporter
6+
import io.AbstractFile
67

78
import scala.annotation.internal.sharable
89

@@ -19,12 +20,12 @@ object Bench extends Driver:
1920

2021
@sharable private var times: Array[Int] = _
2122

22-
override def doCompile(compiler: Compiler, fileNames: List[String])(using Context): Reporter =
23+
override def doCompile(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
2324
times = new Array[Int](numRuns)
2425
var reporter: Reporter = emptyReporter
2526
for i <- 0 until numRuns do
2627
val start = System.nanoTime()
27-
reporter = super.doCompile(compiler, fileNames)
28+
reporter = super.doCompile(compiler, files)
2829
times(i) = ((System.nanoTime - start) / 1000000).toInt
2930
println(s"time elapsed: ${times(i)}ms")
3031
if ctx.settings.Xprompt.value then

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

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import core.Contexts._
99
import core.{MacroClassLoader, Mode, TypeError}
1010
import core.StdNames.nme
1111
import dotty.tools.dotc.ast.Positioned
12-
import dotty.tools.io.{File, AbstractFile}
12+
import dotty.tools.io.AbstractFile
1313
import reporting._
1414
import core.Decorators._
1515
import config.Feature
@@ -32,28 +32,11 @@ 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
51-
52-
protected def doCompileFiles(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
35+
protected def doCompile(compiler: Compiler, files: List[AbstractFile])(using Context): Reporter =
5336
if files.nonEmpty then
5437
try
5538
val run = compiler.newRun
56-
run.compileFiles(files)
39+
run.compile(files)
5740
finish(compiler, run)
5841
catch
5942
case ex: FatalError =>
@@ -81,7 +64,7 @@ class Driver {
8164

8265
protected def sourcesRequired: Boolean = true
8366

84-
def setup(args: Array[String], rootCtx: Context): (List[String], Context) = {
67+
def setup(args: Array[String], rootCtx: Context): (List[AbstractFile], Context) = {
8568
val ictx = rootCtx.fresh
8669
val summary = CompilerCommand.distill(args)(using ictx)
8770
ictx.setSettings(summary.sstate)
@@ -92,43 +75,37 @@ class Driver {
9275
if !ctx.settings.YdropComments.value || ctx.mode.is(Mode.ReadComments) then
9376
ictx.setProperty(ContextDoc, new ContextDocstrings)
9477
val fileNames = CompilerCommand.checkUsage(summary, sourcesRequired)
95-
fromTastySetup(fileNames, ctx)
78+
val files = fileNames.map(ctx.getFile)
79+
(files, fromTastySetup(files))
9680
}
9781
}
9882

99-
/** Setup extra classpath and figure out class names for tasty file inputs */
100-
protected def fromTastySetup(fileNames0: List[String], ctx0: Context): (List[String], Context) =
101-
given Context = ctx0
102-
if (ctx0.settings.fromTasty.value) {
103-
val fromTastyIgnoreList = ctx0.settings.YfromTastyIgnoreList.value.toSet
104-
// Resolve classpath and class names of tasty files
105-
val (classPaths, classNames) = fileNames0.flatMap { name =>
106-
val path = Paths.get(name)
107-
if !Files.exists(path) then
108-
report.error(s"File does not exist: $name")
109-
Nil
110-
else if name.endsWith(".jar") then
111-
new dotty.tools.io.Jar(File(name)).toList.collect {
112-
case e if e.getName.endsWith(".tasty") && !fromTastyIgnoreList(e.getName) =>
113-
(name, e.getName.stripSuffix(".tasty").replace("/", "."))
114-
}
115-
else if name.endsWith(".tasty") then
116-
TastyFileUtil.getClassName(path) match
117-
case Some(res) => res :: Nil
83+
/** Setup extra classpath of tasty and jar files */
84+
protected def fromTastySetup(files: List[AbstractFile])(using Context): Context =
85+
if ctx.settings.fromTasty.value then
86+
val newEntries: List[String] = files
87+
.flatMap { file =>
88+
if !file.exists then
89+
report.error(s"File does not exist: ${file.path}")
90+
None
91+
else file.extension match
92+
case "jar" => Some(file.path)
93+
case "tasty" =>
94+
TastyFileUtil.getClassPath(file) match
95+
case Some(classpath) => Some(classpath)
96+
case _ =>
97+
report.error(s"Could not load classname from: ${file.path}")
98+
None
11899
case _ =>
119-
report.error(s"Could not load classname from: $name")
120-
Nil
121-
else
122-
report.error(s"File extension is not `tasty` or `jar`: $name")
123-
Nil
124-
}.unzip
125-
val ctx1 = ctx0.fresh
126-
val classPaths1 = classPaths.distinct.filter(_ != "")
127-
val fullClassPath = (classPaths1 :+ ctx1.settings.classpath.value(using ctx1)).mkString(java.io.File.pathSeparator)
100+
report.error(s"File extension is not `tasty` or `jar`: ${file.path}")
101+
None
102+
}
103+
.distinct
104+
val ctx1 = ctx.fresh
105+
val fullClassPath =
106+
(newEntries :+ ctx.settings.classpath.value).mkString(java.io.File.pathSeparator)
128107
ctx1.setSetting(ctx1.settings.classpath, fullClassPath)
129-
(classNames, ctx1)
130-
}
131-
else (fileNames0, ctx0)
108+
else ctx
132109

133110
/** Entry point to the compiler that can be conveniently used with Java reflection.
134111
*
@@ -205,8 +182,8 @@ class Driver {
205182
* if compilation succeeded.
206183
*/
207184
def process(args: Array[String], rootCtx: Context): Reporter = {
208-
val (fileNames, compileCtx) = setup(args, rootCtx)
209-
doCompile(newCompiler(using compileCtx), fileNames)(using compileCtx)
185+
val (files, compileCtx) = setup(args, rootCtx)
186+
doCompile(newCompiler(using compileCtx), files)(using compileCtx)
210187
}
211188

212189
def main(args: Array[String]): Unit = {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class Resident extends Driver {
4040

4141
final override def process(args: Array[String], rootCtx: Context): Reporter = {
4242
@tailrec def loop(args: Array[String], prevCtx: Context): Reporter = {
43-
var (fileNames, ctx) = setup(args, prevCtx)
44-
inContext(ctx){doCompile(residentCompiler, fileNames)}
43+
var (files, ctx) = setup(args, prevCtx)
44+
inContext(ctx) { doCompile(residentCompiler, files) }
4545
var nextCtx = ctx
4646
var line = getLine()
4747
while (line == reset) {

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ 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 =
132-
val sources = files.map(runContext.getSource(_))
133-
compileSources(sources)
127+
def compile(files: List[AbstractFile]): Unit =
128+
try
129+
val sources = files.map(runContext.getSource(_))
130+
compileSources(sources)
131+
catch
132+
case NonFatal(ex) =>
133+
if units != null then report.echo(i"exception occurred while compiling $units%, %")
134+
else report.echo(s"exception occurred while compiling ${files.map(_.name).mkString(", ")}")
135+
throw ex
134136

135137
/** TODO: There's a fundamental design problem here: We assemble phases using `fusePhases`
136138
* when we first build the compiler. But we modify them with -Yskip, -Ystop
@@ -139,15 +141,11 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
139141
* account. I think the latter would be preferable.
140142
*/
141143
def compileSources(sources: List[SourceFile]): Unit =
142-
try
143-
if sources forall (_.exists) then
144-
units = sources.map(CompilationUnit(_))
145-
compileUnits()
146-
catch
147-
case NonFatal(ex) =>
148-
if units != null then report.echo(i"exception occurred while compiling $units%, %")
149-
else report.echo(s"exception occurred while compiling ${sources.map(_.name).mkString(", ")}")
150-
throw ex
144+
if (sources forall (_.exists)) {
145+
units = sources.map(CompilationUnit(_))
146+
compileUnits()
147+
}
148+
151149

152150
def compileUnits(us: List[CompilationUnit]): Unit = {
153151
units = us
@@ -220,7 +218,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
220218
if (!files.contains(file) && !lateFiles.contains(file)) {
221219
lateFiles += file
222220

223-
val unit = CompilationUnit(ctx.getSource(file.path))
221+
val unit = CompilationUnit(ctx.getSource(file))
224222
val unitCtx = runContext.fresh
225223
.setCompilationUnit(unit)
226224
.withRootImports

compiler/src/dotty/tools/dotc/config/CompilerCommand.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object CompilerCommand {
3333

3434
def versionMsg: String = s"Scala compiler $versionString -- $copyrightString"
3535

36-
def shouldStopWithInfo(using ctx: Context) = {
36+
def shouldStopWithInfo(using Context): Boolean = {
3737
val settings = ctx.settings
3838
import settings._
3939
Set(help, Xhelp, Yhelp, showPlugins, XshowPhases) exists (_.value)

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -266,25 +266,32 @@ 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 {
271-
case Some(source) =>
272-
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-
}
269+
/** SourceFile with given path name, memoized */
270+
def getSource(path: TermName): SourceFile = getFile(path) match
271+
case NoAbstractFile => NoSource
272+
case file => getSource(file)
284273

285-
/** Sourcefile with given path, memoized */
274+
/** SourceFile with given path, memoized */
286275
def getSource(path: String): SourceFile = getSource(path.toTermName)
287276

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

290297
private def lookup(key: Phase | SourceFile): Context =
@@ -841,9 +848,9 @@ object Contexts {
841848
private var _nextSymId: Int = 0
842849
def nextSymId: Int = { _nextSymId += 1; _nextSymId }
843850

844-
/** Sources that were loaded */
851+
/** Sources and Files that were loaded */
845852
val sources: util.HashMap[AbstractFile, SourceFile] = util.HashMap[AbstractFile, SourceFile]()
846-
val sourceNamed: util.HashMap[TermName, SourceFile] = util.HashMap[TermName, SourceFile]()
853+
val files: util.HashMap[TermName, AbstractFile] = util.HashMap()
847854

848855
// Types state
849856
/** A table for hash consing unique types */
@@ -927,7 +934,7 @@ object Contexts {
927934
emptyWildcardBounds = null
928935
errorTypeMsg.clear()
929936
sources.clear()
930-
sourceNamed.clear()
937+
files.clear()
931938
comparers.clear() // forces re-evaluation of top and bottom classes in TypeComparer
932939

933940
// Test that access is single threaded

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ object SymbolLoaders {
173173
Nil)
174174
}
175175

176-
val unit = CompilationUnit(ctx.getSource(src.path))
176+
val unit = CompilationUnit(ctx.getSource(src))
177177
enterScanned(unit)(using ctx.fresh.setCompilationUnit(unit))
178178

179179
/** The package objects of scala and scala.reflect should always

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dotty.tools.dotc.core.Contexts._
66
import dotty.tools.dotc.core._
77
import dotty.tools.dotc.core.tasty.TastyHTMLPrinter
88
import dotty.tools.dotc.reporting._
9+
import dotty.tools.io.AbstractFile
910

1011
import scala.quoted.runtime.impl.QuotesImpl
1112

@@ -25,13 +26,13 @@ class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
2526

2627
private val decompiler = new PartialTASTYDecompiler
2728

28-
def run(className: String): (String, String) = {
29+
def run(tastyFile: AbstractFile): (String, String) = {
2930
val reporter = new StoreReporter(null) with HideNonSensicalMessages
3031

3132
val run = decompiler.newRun(using myInitCtx.fresh.setReporter(reporter))
3233

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.nio.file.Files
44

55
import dotty.tools.dotc
66
import dotty.tools.dotc.core.Contexts._
7+
import dotty.tools.io.AbstractFile
78

89
/** Main class of the `dotc -decompiler` decompiler.
910
*
@@ -17,7 +18,7 @@ object Main extends dotc.Driver {
1718
new TASTYDecompiler
1819
}
1920

20-
override def setup(args0: Array[String], rootCtx: Context): (List[String], Context) = {
21+
override def setup(args0: Array[String], rootCtx: Context): (List[AbstractFile], Context) = {
2122
var args = args0.filter(a => a != "-decompile")
2223
if (!args.contains("-from-tasty")) args = "-from-tasty" +: args
2324
if (args.contains("-d")) args = "-color:never" +: args

0 commit comments

Comments
 (0)