Skip to content

Commit 00dc18b

Browse files
committed
Allow tasty files to be the input of -from-tasty
Also works for -decompile and any other compiler that has the -form-tatsy flag.
1 parent 9eed6e6 commit 00dc18b

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package dotty.tools.dotc
22

3+
import java.nio.file.{Files, Paths}
4+
35
import dotty.tools.FatalError
46
import config.CompilerCommand
57
import core.Comments.{ContextDoc, ContextDocstrings}
68
import core.Contexts.{Context, ContextBase}
79
import core.Mode
810
import reporting._
11+
912
import scala.util.control.NonFatal
10-
import fromtasty.TASTYCompiler
13+
import fromtasty.{TASTYCompiler, TastyFileUtil}
1114

1215
/** Run the Dotty compiler.
1316
*
@@ -132,7 +135,24 @@ class Driver {
132135
* if compilation succeeded.
133136
*/
134137
def process(args: Array[String], rootCtx: Context): Reporter = {
135-
val (fileNames, ctx) = setup(args, rootCtx)
138+
val (fileNames0, ctx0) = setup(args, rootCtx)
139+
val (fileNames, ctx) =
140+
if (ctx0.settings.fromTasty.value(ctx0)) {
141+
// Resolve classpath and class names of tasty files
142+
val (classPaths, classNames) = fileNames0.map { name =>
143+
val path = Paths.get(name)
144+
if (!name.endsWith(".tasty")) ("", name)
145+
else if (Files.exists(path)) TastyFileUtil.getClassName(path)
146+
else {
147+
ctx0.error(s"File $name does not exist.")
148+
("", name)
149+
}
150+
}.unzip
151+
val ctx1 = ctx0.fresh
152+
val classPaths1 = classPaths.distinct.filter(_ != "")
153+
ctx1.setSetting(ctx1.settings.classpath, (ctx1.settings.classpath.value(ctx1) :: classPaths1).mkString(":"))
154+
(classNames, ctx1)
155+
} else (fileNames0, ctx0)
136156
doCompile(newCompiler(ctx), fileNames)(ctx)
137157
}
138158

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package dotty.tools.dotc.fromtasty
2+
3+
import java.nio.file.{Files, Path, Paths}
4+
5+
import dotty.tools.dotc.core.Contexts.Context
6+
import dotty.tools.dotc.core.NameKinds
7+
import dotty.tools.dotc.core.Names.SimpleName
8+
import dotty.tools.dotc.core.StdNames.nme
9+
import dotty.tools.dotc.core.tasty.TastyUnpickler
10+
11+
object TastyFileUtil {
12+
13+
/** Get the class path and the class name including packages
14+
*
15+
* If
16+
* ```scala
17+
* package foo
18+
* class Foo
19+
* ```
20+
* then `getClassName("./out/foo/Foo.tasty") returns `("./out", "foo.Foo")`
21+
*/
22+
def getClassName(path: Path): (String, String) = {
23+
assert(path.toString.endsWith(".tasty"))
24+
assert(Files.exists(path))
25+
val bytes = Files.readAllBytes(path)
26+
val unpickler: TastyUnpickler = new TastyUnpickler(bytes)
27+
val className =
28+
unpickler.nameAtRef.contents.iterator.takeWhile {
29+
case name: SimpleName => name != nme.CONSTRUCTOR
30+
case name => !name.is(NameKinds.ModuleClassName)
31+
}.collect {
32+
case name: SimpleName => name.toString
33+
}.drop(1).toList
34+
35+
val classpath = path.toString.replace(className.mkString("", "/", ".tasty"), "")
36+
(classpath, className.mkString("."))
37+
}
38+
39+
}

0 commit comments

Comments
 (0)