Skip to content

Commit a9baef2

Browse files
committed
Keep loaded pickled tasty when compiling from tasty
1 parent 6fd03ba commit a9baef2

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,22 +604,38 @@ object Symbols {
604604

605605
/** If this is either:
606606
* - a top-level class and `-Yretain-trees` is set
607-
* - a top-level class loaded from TASTY and `-Xlink-optimise` is set
607+
* - a top-level class loaded from TASTY and `-tasty` or `-Xlink` is set
608608
* then return the TypeDef tree (possibly wrapped inside PackageDefs) for this class, otherwise EmptyTree.
609609
* This will force the info of the class.
610610
*/
611611
def tree(implicit ctx: Context): tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = {
612+
load()
613+
myTree
614+
}
615+
616+
/** If this is either:
617+
* - a top-level class loaded from TASTY with `-tasty`
618+
* then return the bytes of the tasty.
619+
*/
620+
def pickled(implicit ctx: Context): Array[Byte] = {
621+
load()
622+
myTasty
623+
}
624+
625+
private def load()(implicit ctx: Context): Unit = {
612626
denot.info
613627
// TODO: Consider storing this tree like we store lazy trees for inline functions
614628
if (unpickler != null && !denot.isAbsent) {
615629
assert(myTree.isEmpty)
616630
val body = unpickler.body(ctx.addMode(Mode.ReadPositions))
617631
myTree = body.headOption.getOrElse(tpd.EmptyTree)
632+
if (ctx.settings.tasty.value)
633+
myTasty = unpickler.unpickler.bytes
618634
unpickler = null
619635
}
620-
myTree
621636
}
622637
private[this] var myTree: tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = tpd.EmptyTree
638+
private[this] var myTasty: Array[Byte] = _
623639
private[dotc] var unpickler: tasty.DottyUnpickler = _
624640

625641
private[dotc] def registerTree(tree: tpd.TypeDef)(implicit ctx: Context): Unit = {

compiler/src/dotty/tools/dotc/core/tasty/TastyUnpickler.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,6 @@ class TastyUnpickler(reader: TastyReader) {
9999
def unpickle[R](sec: SectionUnpickler[R]): Option[R] =
100100
for (reader <- sectionReader.get(sec.name)) yield
101101
sec.unpickle(reader, nameAtRef)
102+
103+
private[dotc] def bytes: Array[Byte] = reader.bytes
102104
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ class ReadTastyTreesFromClasses extends FrontEnd {
2525
tree(className).flatMap {
2626
case (clsd, unpickled) =>
2727
if (unpickled.isEmpty) None
28-
else Some(CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true))
29-
28+
else {
29+
val unit = CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true)
30+
val cls = clsd.symbol.asClass
31+
unit.pickled += (cls -> cls.pickled)
32+
Some(unit)
33+
}
3034
}
3135
}
3236
// The TASTY section in a/b/C.class may either contain a class a.b.C, an object a.b.C, or both.
@@ -41,12 +45,13 @@ class ReadTastyTreesFromClasses extends FrontEnd {
4145
val clsd = ctx.base.staticRef(className)
4246
ctx.base.staticRef(className) match {
4347
case clsd: ClassDenotation =>
48+
val cls = clsd.symbol.asClass
4449
def cannotUnpickle(reason: String) =
4550
ctx.error(s"class $className cannot be unpickled because $reason")
4651
def tryToLoad = clsd.infoOrCompleter match {
4752
case info: ClassfileLoader =>
4853
info.load(clsd)
49-
Option(clsd.symbol.asClass.tree).orElse {
54+
Option(cls.tree).orElse {
5055
cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
5156
None
5257
}
@@ -55,7 +60,7 @@ class ReadTastyTreesFromClasses extends FrontEnd {
5560
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
5661
None
5762
}
58-
Option(clsd.symbol.asClass.tree).orElse(tryToLoad).map(tree => (clsd, tree))
63+
Option(cls.tree).orElse(tryToLoad).map(tree => (clsd, tree))
5964

6065
case _ =>
6166
ctx.error(s"class not found: $className")

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ class TASTYCompiler extends Compiler {
1414
case List(_: Pickler) => false
1515
case _ => true
1616
}.tail
17-
18-
List(new ReadTastyTreesFromClasses) ::
19-
List(new Pickler) ::
20-
backendPhases
17+
List(new ReadTastyTreesFromClasses) :: backendPhases
2118
}
2219

2320
override def newRun(implicit ctx: Context): Run = {

0 commit comments

Comments
 (0)