@@ -10,7 +10,8 @@ import ast.{tpd, Trees, untpd}
10
10
import Trees ._
11
11
import Decorators ._
12
12
import TastyUnpickler ._ , TastyBuffer ._ , PositionPickler ._
13
- import annotation .switch
13
+ import scala .annotation .{tailrec , switch }
14
+ import scala .collection .mutable .ListBuffer
14
15
import scala .collection .{ mutable , immutable }
15
16
import typer .Mode
16
17
import config .Printers .pickling
@@ -65,7 +66,7 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
65
66
def unpickle ()(implicit ctx : Context ): List [Tree ] = {
66
67
assert(roots != null , " unpickle without previous enterTopLevel" )
67
68
val stats = new TreeReader (reader)
68
- .readIndexedStats( NoSymbol , reader.endAddr )(ctx.addMode(Mode .AllowDependentFunctions ))
69
+ .readTopLevel( )(ctx.addMode(Mode .AllowDependentFunctions ))
69
70
normalizePos(stats, totalRange)
70
71
stats
71
72
}
@@ -676,6 +677,29 @@ class TreeUnpickler(reader: TastyReader, tastyName: TastyName.Table) {
676
677
.withType(localDummy.nonMemberTermRef))
677
678
}
678
679
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
+
679
703
def readIndexedStat (exprOwner : Symbol )(implicit ctx : Context ): Tree = nextByte match {
680
704
case TYPEDEF | VALDEF | DEFDEF =>
681
705
readIndexedDef()
0 commit comments