Skip to content

Commit 3117430

Browse files
committed
Unify tree loaders
1 parent 0546afb commit 3117430

File tree

6 files changed

+43
-42
lines changed

6 files changed

+43
-42
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,11 @@ object FromTasty extends Driver {
9999

100100
def loadCompilationUnit(clsd: ClassDenotation)(implicit ctx: Context): Option[CompilationUnit] = {
101101
assert(ctx.settings.XlinkOptimise.value)
102-
clsd.dottyUnpickler.flatMap { unpickler =>
102+
val tree = clsd.symbol.asClass.unitTree
103+
if (tree.isEmpty) None
104+
else {
103105
ctx.log("Loading compilation unit for: " + clsd)
104-
val body = unpickler.body(ctx.addMode(Mode.ReadPositions))
105-
body.headOption.map(unpickled => mkUnit(clsd, unpickled))
106+
Some(mkUnit(clsd, tree))
106107
}
107108
}
108109

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import java.util.WeakHashMap
1919
import config.Config
2020
import config.Printers.{completions, incremental, noPrinter}
2121

22-
import dotty.tools.dotc.core.tasty.DottyUnpickler
23-
2422
trait SymDenotations { this: Context =>
2523
import SymDenotations._
2624

@@ -141,8 +139,6 @@ object SymDenotations {
141139
private[this] var myPrivateWithin: Symbol = initPrivateWithin
142140
private[this] var myAnnotations: List[Annotation] = Nil
143141

144-
private[dotc] var dottyUnpickler: Option[DottyUnpickler] = None
145-
146142
/** The owner of the symbol; overridden in NoDenotation */
147143
def owner: Symbol = ownerIfExists
148144

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,15 @@ class ClassfileLoader(val classfile: AbstractFile) extends SymbolLoader {
323323

324324
def load(root: SymDenotation)(implicit ctx: Context): Unit = {
325325
val (classRoot, moduleRoot) = rootDenots(root.asClass)
326-
(new ClassfileParser(classfile, classRoot, moduleRoot)(ctx)).run() match {
327-
case Some(unpickler: tasty.DottyUnpickler) if ctx.settings.YretainTrees.value =>
328-
classRoot.symbol.asClass.unpickler = unpickler
329-
moduleRoot.symbol.asClass.unpickler = unpickler
330-
case _ =>
326+
val classfileParser = new ClassfileParser(classfile, classRoot, moduleRoot)(ctx)
327+
val result = classfileParser.run()
328+
if (ctx.settings.YretainTrees.value || ctx.settings.XlinkOptimise.value) {
329+
result match {
330+
case Some(unpickler: tasty.DottyUnpickler) =>
331+
classRoot.symbol.asClass.unpickler = unpickler
332+
moduleRoot.symbol.asClass.unpickler = unpickler
333+
case _ =>
334+
}
331335
}
332336
}
333337
}

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -558,32 +558,44 @@ object Symbols {
558558
/** If this is a top-level class, and if `-Yretain-trees` is set, return the TypeDef tree
559559
* for this class, otherwise EmptyTree. This will force the info of the class.
560560
*/
561-
def tree(implicit ctx: Context): tpd.Tree /* tpd.TypeDef | tpd.EmptyTree */ = {
561+
def tree(implicit ctx: Context): tpd.Tree /* tpd.TypeDef | tpd.EmptyTree */ = {
562+
import ast.Trees._
563+
def findTree(tree: tpd.Tree): Option[tpd.TypeDef] = tree match {
564+
case PackageDef(_, stats) =>
565+
stats.flatMap(findTree).headOption
566+
case tree: tpd.TypeDef if tree.symbol == this =>
567+
Some(tree)
568+
case _ =>
569+
None
570+
}
571+
val t = unitTree
572+
if (t.isEmpty) t
573+
else findTree(tree).get
574+
}
575+
576+
/** If this is a top-level class, and if `-Yretain-trees` or `-YlinkOptimise` is set,
577+
* return the PackageDef tree for this class, otherwise EmptyTree.
578+
* This will force the info of the class.
579+
*/
580+
def unitTree(implicit ctx: Context): tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = {
562581
denot.info
563582
// TODO: Consider storing this tree like we store lazy trees for inline functions
564583
if (unpickler != null && !denot.isAbsent) {
565584
assert(myTree.isEmpty)
566-
567-
import ast.Trees._
568-
569-
def findTree(tree: tpd.Tree): Option[tpd.TypeDef] = tree match {
570-
case PackageDef(_, stats) =>
571-
stats.flatMap(findTree).headOption
572-
case tree: tpd.TypeDef if tree.symbol == this =>
573-
Some(tree)
574-
case _ =>
575-
None
576-
}
577-
val List(unpickledTree) = unpickler.body(ctx.addMode(Mode.ReadPositions))
585+
val body = unpickler.body(ctx.addMode(Mode.ReadPositions))
586+
myTree = body.headOption.getOrElse(tpd.EmptyTree)
578587
unpickler = null
579-
580-
myTree = findTree(unpickledTree).get
581588
}
582589
myTree
583590
}
584-
private[dotc] var myTree: tpd.Tree = tpd.EmptyTree
591+
private var myTree: tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = tpd.EmptyTree
585592
private[dotc] var unpickler: tasty.DottyUnpickler = _
586593

594+
private[dotc] def registerTree(tree: tpd.TypeDef)(implicit ctx: Context): Unit = {
595+
if (ctx.settings.YretainTrees.value)
596+
myTree = tree
597+
}
598+
587599
/** The source or class file from which this class was generated, null if not applicable. */
588600
override def associatedFile(implicit ctx: Context): AbstractFile =
589601
if (assocFile != null || (this.owner is PackageClass) || this.isEffectiveRoot) assocFile

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import scala.annotation.switch
1515
import typer.Checking.checkNonCyclic
1616
import io.AbstractFile
1717
import scala.util.control.NonFatal
18-
import tasty.DottyUnpickler
1918

2019
object ClassfileParser {
2120
/** Marker trait for unpicklers that can be embedded in classfiles. */
@@ -163,16 +162,6 @@ class ClassfileParser(
163162
}
164163
}
165164

166-
if (ctx.settings.XlinkOptimise.value) {
167-
// Save references to DottyUnpickler to be able to load compilation units from tasty
168-
result match {
169-
case result@Some(_: DottyUnpickler) =>
170-
classRoot.dottyUnpickler = result.asInstanceOf[Option[DottyUnpickler]]
171-
moduleRoot.dottyUnpickler = result.asInstanceOf[Option[DottyUnpickler]]
172-
case _ =>
173-
}
174-
}
175-
176165
// eager load java enum definitions for exhaustivity check of pattern match
177166
if (isEnum) {
178167
instanceScope.toList.map(_.ensureCompleted())

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,9 +1429,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
14291429
// check value class constraints
14301430
checkDerivedValueClass(cls, body1)
14311431

1432-
if (ctx.settings.YretainTrees.value) {
1433-
cls.myTree = cdef1
1434-
}
1432+
cls.registerTree(cdef1)
1433+
14351434
cdef1
14361435

14371436
// todo later: check that

0 commit comments

Comments
 (0)