Skip to content

Commit 9ff94c9

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 8031978 commit 9ff94c9

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
*
@@ -129,7 +132,24 @@ class Driver {
129132
* if compilation succeeded.
130133
*/
131134
def process(args: Array[String], rootCtx: Context): Reporter = {
132-
val (fileNames, ctx) = setup(args, rootCtx)
135+
val (fileNames0, ctx0) = setup(args, rootCtx)
136+
val (fileNames, ctx) =
137+
if (ctx0.settings.fromTasty.value(ctx0)) {
138+
// Resolve classpath and class names of tasty files
139+
val (classPaths, classNames) = fileNames0.map { name =>
140+
val path = Paths.get(name)
141+
if (!name.endsWith(".tasty")) ("", name)
142+
else if (Files.exists(path)) TastyFileUtil.getClassName(path)
143+
else {
144+
ctx0.error(s"File $name does not exist.")
145+
("", name)
146+
}
147+
}.unzip
148+
val ctx1 = ctx0.fresh
149+
val classPaths1 = classPaths.distinct.filter(_ != "")
150+
ctx1.setSetting(ctx1.settings.classpath, (ctx1.settings.classpath.value(ctx1) :: classPaths1).mkString(":"))
151+
(classNames, ctx1)
152+
} else (fileNames0, ctx0)
133153
doCompile(newCompiler(ctx), fileNames)(ctx)
134154
}
135155

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)