Skip to content

Commit 4a5c690

Browse files
committed
Join logic for unpickling top level and term tasty
1 parent f9f9f4a commit 4a5c690

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ class ClassfileParser(
758758

759759
def unpickleTASTY(bytes: Array[Byte]): Some[Embedded] = {
760760
val unpickler = new tasty.DottyUnpickler(bytes)
761-
unpickler.enter(roots = Set(classRoot, moduleRoot, moduleRoot.sourceModule))
761+
unpickler.enter(roots = Set(classRoot, moduleRoot, moduleRoot.sourceModule), isTopLevel = true)
762762
Some(unpickler)
763763
}
764764

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ object PickledQuotes {
110110
/** Unpickle TASTY bytes into it's tree */
111111
private def unpickle(bytes: Array[Byte], splices: Seq[Any])(implicit ctx: Context): Tree = {
112112
val unpickler = new TastyUnpickler(bytes, splices)
113-
val tree = unpickler.unpickleExpr()
113+
unpickler.enter(Set(ctx.owner), isTopLevel = false)
114+
val tree = unpickler.tree
114115
if (pickling ne noPrinter) {
115116
println(i"**** unpickled quote for \n${tree.show}")
116117
new TastyPrinter(bytes).printContents()

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@ class DottyUnpickler(bytes: Array[Byte]) extends ClassfileParser.Embedded with t
4343
/** Enter all toplevel classes and objects into their scopes
4444
* @param roots a set of SymDenotations that should be overwritten by unpickling
4545
*/
46-
def enter(roots: Set[SymDenotation])(implicit ctx: Context): Unit =
47-
treeUnpickler.enterTopLevel(roots)
48-
49-
def unpickleExpr()(implicit ctx: Context): Tree =
50-
treeUnpickler.unpickleExpr()
46+
def enter(roots: Set[SymDenotation], isTopLevel: Boolean)(implicit ctx: Context): Unit =
47+
treeUnpickler.enter(roots, isTopLevel)
5148

5249
protected def treeSectionUnpickler(posUnpicklerOpt: Option[PositionUnpickler]): TreeSectionUnpickler = {
5350
new TreeSectionUnpickler(posUnpicklerOpt)

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class TreeUnpickler(reader: TastyReader,
5252
*/
5353
private val typeAtAddr = new mutable.HashMap[Addr, Type]
5454

55+
/** If the Tasty corresponds to a top level tree. `flase` is used used for quotes */
56+
private[this] var isTopLevel: Boolean = true
57+
5558
/** The root symbol denotation which are defined by the Tasty file associated with this
5659
* TreeUnpickler. Set by `enterTopLevel`.
5760
*/
@@ -71,24 +74,21 @@ class TreeUnpickler(reader: TastyReader,
7174
/** Enter all toplevel classes and objects into their scopes
7275
* @param roots a set of SymDenotations that should be overwritten by unpickling
7376
*/
74-
def enterTopLevel(roots: Set[SymDenotation])(implicit ctx: Context): Unit = {
77+
def enter(roots: Set[SymDenotation], isTopLevel: Boolean)(implicit ctx: Context): Unit = {
78+
this.isTopLevel = isTopLevel
7579
this.roots = roots
7680
val rdr = new TreeReader(reader).fork
7781
ownerTree = new OwnerTree(NoAddr, 0, rdr.fork, reader.endAddr)
78-
rdr.indexStats(reader.endAddr)
79-
}
80-
81-
def unpickleExpr()(implicit ctx: Context): Tree = {
82-
this.roots = Set(ctx.owner)
83-
val rdr = new TreeReader(reader).fork
84-
ownerTree = new OwnerTree(NoAddr, 0, rdr.fork, reader.endAddr)
85-
rdr.readTerm()
82+
if (isTopLevel)
83+
rdr.indexStats(reader.endAddr)
8684
}
8785

8886
/** The unpickled trees */
8987
def unpickle()(implicit ctx: Context): List[Tree] = {
9088
assert(roots != null, "unpickle without previous enterTopLevel")
91-
new TreeReader(reader).readTopLevel()
89+
val rdr = new TreeReader(reader)
90+
if (isTopLevel) rdr.readTopLevel()
91+
else rdr.readTerm() :: Nil
9292
}
9393

9494
class Completer(owner: Symbol, reader: TastyReader) extends LazyType {

compiler/src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Pickler extends Phase {
9494
val unpicklers =
9595
for ((cls, pickler) <- picklers) yield {
9696
val unpickler = new DottyUnpickler(pickler.assembleParts())
97-
unpickler.enter(roots = Set())
97+
unpickler.enter(roots = Set(), isTopLevel = true)
9898
cls -> unpickler
9999
}
100100
pickling.println("************* entered toplevel ***********")

0 commit comments

Comments
 (0)