11package scala .async .internal
22
3+ import scala .collection .mutable
4+
35trait Lifter {
46 self : AsyncMacro =>
57 import c .universe ._
@@ -37,7 +39,7 @@ trait Lifter {
3739 }
3840
3941
40- val defs : Map [Tree , Int ] = {
42+ val defs : mutable. LinkedHashMap [Tree , Int ] = {
4143 /** Collect the DefTrees directly enclosed within `t` that have the same owner */
4244 def collectDirectlyEnclosedDefs (t : Tree ): List [DefTree ] = t match {
4345 case ld : LabelDef => Nil
@@ -48,33 +50,33 @@ trait Lifter {
4850 companionship.record(childDefs)
4951 childDefs
5052 }
51- asyncStates.flatMap {
53+ mutable. LinkedHashMap ( asyncStates.flatMap {
5254 asyncState =>
5355 val defs = collectDirectlyEnclosedDefs(Block (asyncState.allStats: _* ))
5456 defs.map((_, asyncState.state))
55- }.toMap
57+ }: _* )
5658 }
5759
5860 // In which block are these symbols defined?
59- val symToDefiningState : Map [Symbol , Int ] = defs.map {
61+ val symToDefiningState : mutable. LinkedHashMap [Symbol , Int ] = defs.map {
6062 case (k, v) => (k.symbol, v)
6163 }
6264
6365 // The definitions trees
64- val symToTree : Map [Symbol , Tree ] = defs.map {
66+ val symToTree : mutable. LinkedHashMap [Symbol , Tree ] = defs.map {
6567 case (k, v) => (k.symbol, k)
6668 }
6769
6870 // The direct references of each definition tree
69- val defSymToReferenced : Map [Symbol , List [Symbol ]] = defs.keys .map {
70- case tree => (tree.symbol, tree.collect {
71+ val defSymToReferenced : mutable. LinkedHashMap [Symbol , List [Symbol ]] = defs.map {
72+ case ( tree, _) => (tree.symbol, tree.collect {
7173 case rt : RefTree if symToDefiningState.contains(rt.symbol) => rt.symbol
7274 })
73- }.toMap
75+ }
7476
7577 // The direct references of each block, excluding references of `DefTree`-s which
7678 // are already accounted for.
77- val stateIdToDirectlyReferenced : Map [Int , List [Symbol ]] = {
79+ val stateIdToDirectlyReferenced : mutable. LinkedHashMap [Int , List [Symbol ]] = {
7880 val refs : List [(Int , Symbol )] = asyncStates.flatMap(
7981 asyncState => asyncState.stats.filterNot(t => t.isDef && ! isLabel(t.symbol)).flatMap(_.collect {
8082 case rt : RefTree
@@ -84,8 +86,8 @@ trait Lifter {
8486 toMultiMap(refs)
8587 }
8688
87- def liftableSyms : Set [Symbol ] = {
88- val liftableMutableSet = collection. mutable.Set [Symbol ]()
89+ def liftableSyms : mutable. LinkedHashSet [Symbol ] = {
90+ val liftableMutableSet = mutable.LinkedHashSet [Symbol ]()
8991 def markForLift (sym : Symbol ): Unit = {
9092 if (! liftableMutableSet(sym)) {
9193 liftableMutableSet += sym
@@ -97,19 +99,19 @@ trait Lifter {
9799 }
98100 }
99101 // Start things with DefTrees directly referenced from statements from other states...
100- val liftableStatementRefs : List [Symbol ] = stateIdToDirectlyReferenced.toList .flatMap {
102+ val liftableStatementRefs : List [Symbol ] = stateIdToDirectlyReferenced.iterator .flatMap {
101103 case (i, syms) => syms.filter(sym => symToDefiningState(sym) != i)
102- }
104+ }.toList
103105 // .. and likewise for DefTrees directly referenced by other DefTrees from other states
104106 val liftableRefsOfDefTrees = defSymToReferenced.toList.flatMap {
105107 case (referee, referents) => referents.filter(sym => symToDefiningState(sym) != symToDefiningState(referee))
106108 }
107109 // Mark these for lifting, which will follow transitive references.
108110 (liftableStatementRefs ++ liftableRefsOfDefTrees).foreach(markForLift)
109- liftableMutableSet.toSet
111+ liftableMutableSet
110112 }
111113
112- val lifted = liftableSyms.map(symToTree).toList .map {
114+ liftableSyms.iterator. map(symToTree).map {
113115 t =>
114116 val sym = t.symbol
115117 val treeLifted = t match {
@@ -147,7 +149,6 @@ trait Lifter {
147149 treeCopy.TypeDef (td, Modifiers (sym.flags), sym.name, tparams, rhs)
148150 }
149151 atPos(t.pos)(treeLifted)
150- }
151- lifted
152+ }.toList
152153 }
153154}
0 commit comments