@@ -9,6 +9,8 @@ import config.Printers.{noPrinter, pickling}
99import java .io .PrintStream
1010import Periods ._
1111import Phases ._
12+ import Symbols ._
13+ import Flags .Module
1214import collection .mutable
1315
1416/** This phase pickles trees */
@@ -23,28 +25,41 @@ class Pickler extends Phase {
2325 s.close
2426 }
2527
26- private val beforePickling = new mutable.HashMap [CompilationUnit , String ]
28+ private val beforePickling = new mutable.HashMap [ClassSymbol , String ]
29+
30+ /** Drop any elements of this list that are linked module classes of other elements in the list */
31+ private def dropCompanionModuleClasses (clss : List [ClassSymbol ])(implicit ctx : Context ): List [ClassSymbol ] = {
32+ val companionModuleClasses =
33+ clss.filterNot(_ is Module ).map(_.linkedClass).filterNot(_.isAbsent)
34+ clss.filterNot(companionModuleClasses.contains)
35+ }
2736
2837 override def run (implicit ctx : Context ): Unit = {
2938 val unit = ctx.compilationUnit
30- val tree = unit.tpdTree
3139 pickling.println(i " unpickling in run ${ctx.runId}" )
32- if (ctx.settings.YtestPickler .value) beforePickling(unit) = tree.show
3340
34- val pickler = unit.pickler
35- val treePkl = new TreePickler (pickler)
36- treePkl.pickle(tree :: Nil )
37- unit.addrOfTree = treePkl.buf.addrOfTree
38- unit.addrOfSym = treePkl.addrOfSym
39- if (tree.pos.exists)
40- new PositionPickler (pickler, treePkl.buf.addrOfTree).picklePositions(tree :: Nil , tree.pos)
41+ for { cls <- dropCompanionModuleClasses(topLevelClasses(unit.tpdTree))
42+ tree <- sliceTopLevel(unit.tpdTree, cls) } {
43+ if (ctx.settings.YtestPickler .value) beforePickling(cls) = tree.show
44+ val pickler = new TastyPickler ()
45+ unit.picklers += (cls -> pickler)
46+ val treePkl = new TreePickler (pickler)
47+ treePkl.pickle(tree :: Nil )
48+ unit.addrOfTree = treePkl.buf.addrOfTree
49+ unit.addrOfSym = treePkl.addrOfSym
50+ if (tree.pos.exists)
51+ new PositionPickler (pickler, treePkl.buf.addrOfTree).picklePositions(tree :: Nil , tree.pos)
4152
42- def rawBytes = // not needed right now, but useful to print raw format.
43- unit.pickler.assembleParts().iterator.grouped(10 ).toList.zipWithIndex.map {
44- case (row, i) => s " ${i}0: ${row.mkString(" " )}"
53+ def rawBytes = // not needed right now, but useful to print raw format.
54+ pickler.assembleParts().iterator.grouped(10 ).toList.zipWithIndex.map {
55+ case (row, i) => s " ${i}0: ${row.mkString(" " )}"
56+ }
57+ // println(i"rawBytes = \n$rawBytes%\n%") // DEBUG
58+ if (pickling ne noPrinter) {
59+ println(i " **** pickled info of $cls" )
60+ new TastyPrinter (pickler.assembleParts()).printContents()
4561 }
46- // println(i"rawBytes = \n$rawBytes%\n%") // DEBUG
47- if (pickling ne noPrinter) new TastyPrinter (pickler.assembleParts()).printContents()
62+ }
4863 }
4964
5065 override def runOn (units : List [CompilationUnit ])(implicit ctx : Context ): List [CompilationUnit ] = {
@@ -58,23 +73,23 @@ class Pickler extends Phase {
5873 pickling.println(i " testing unpickler at run ${ctx.runId}" )
5974 ctx.definitions.init
6075 val unpicklers =
61- for (unit <- units) yield {
62- val unpickler = new DottyUnpickler (unit. pickler.assembleParts())
76+ for (unit <- units; (cls, pickler) <- unit.picklers ) yield {
77+ val unpickler = new DottyUnpickler (pickler.assembleParts())
6378 unpickler.enter(roots = Set ())
64- unpickler
79+ cls -> unpickler
6580 }
6681 pickling.println(" ************* entered toplevel ***********" )
67- for ((unpickler, unit ) <- unpicklers zip units ) {
82+ for ((cls, unpickler ) <- unpicklers) {
6883 val unpickled = unpickler.body(readPositions = false )
69- testSame(i " $unpickled% \n % " , beforePickling(unit ), unit )
84+ testSame(i " $unpickled% \n % " , beforePickling(cls ), cls )
7085 }
7186 }
7287
73- private def testSame (unpickled : String , previous : String , unit : CompilationUnit )(implicit ctx : Context ) =
88+ private def testSame (unpickled : String , previous : String , cls : ClassSymbol )(implicit ctx : Context ) =
7489 if (previous != unpickled) {
7590 output(" before-pickling.txt" , previous)
7691 output(" after-pickling.txt" , unpickled)
77- ctx.error(s """ pickling difference for $unit , for details:
92+ ctx.error(s """ pickling difference for ${cls.fullName} , for details:
7893 |
7994 | diff before-pickling.txt after-pickling.txt """ .stripMargin)
8095 }
0 commit comments