Skip to content

Commit 18d8417

Browse files
committed
TreeUnpickler unpickle only top-level packages and imports.
1 parent a3b10d1 commit 18d8417

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import ast.{tpd, Trees, untpd}
1010
import Trees._
1111
import Decorators._
1212
import TastyUnpickler._, TastyBuffer._, PositionPickler._
13-
import annotation.switch
13+
import scala.annotation.{tailrec, switch}
14+
import scala.collection.mutable.ListBuffer
1415
import scala.collection.{ mutable, immutable }
1516
import typer.Mode
1617
import config.Printers.pickling
@@ -65,7 +66,7 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
6566
def unpickle()(implicit ctx: Context): List[Tree] = {
6667
assert(roots != null, "unpickle without previous enterTopLevel")
6768
val stats = new TreeReader(reader)
68-
.readIndexedStats(NoSymbol, reader.endAddr)(ctx.addMode(Mode.AllowDependentFunctions))
69+
.readTopLevel()(ctx.addMode(Mode.AllowDependentFunctions))
6970
normalizePos(stats, totalRange)
7071
stats
7172
}
@@ -676,6 +677,29 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
676677
.withType(localDummy.nonMemberTermRef))
677678
}
678679

680+
def skipToplevel()(implicit ctx: Context): Unit= {
681+
if (!isAtEnd)
682+
nextByte match {
683+
case IMPORT | PACKAGE =>
684+
skipTree()
685+
skipToplevel()
686+
case _ =>
687+
}
688+
}
689+
690+
def readTopLevel()(implicit ctx: Context): List[Tree] = {
691+
@tailrec def read(acc: ListBuffer[Tree]): List[Tree] = nextByte match {
692+
case IMPORT | PACKAGE =>
693+
acc += readIndexedStat(NoSymbol)
694+
if (!isAtEnd)
695+
read(acc)
696+
else acc.toList
697+
case _ => // top-level trees which are not imports or packages are not part of tree
698+
acc.toList
699+
}
700+
read(new ListBuffer[tpd.Tree])
701+
}
702+
679703
def readIndexedStat(exprOwner: Symbol)(implicit ctx: Context): Tree = nextByte match {
680704
case TYPEDEF | VALDEF | DEFDEF =>
681705
readIndexedDef()

0 commit comments

Comments
 (0)