@@ -92,13 +92,21 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
92
92
private def reorderAndComplete (stats : List [Tree ])(implicit ctx : Context ): List [Tree ] = {
93
93
val moduleClassDefs, singleClassDefs = mutable.Map [Name , Tree ]()
94
94
95
- def reorder (stats : List [Tree ], revPrefix : List [Tree ] = Nil ): List [Tree ] = stats match {
95
+ /* Returns the result of reordering stats and prepending revPrefix in reverse order to it.
96
+ * The result of reorder is equivalent to reorder(stats, revPrefix) = revPrefix.reverse ::: reorder(stats, Nil).
97
+ * This implementation is tail recursive as long as the element is not a module TypeDef.
98
+ */
99
+ def reorder (stats : List [Tree ], revPrefix : List [Tree ]): List [Tree ] = stats match {
96
100
case (stat : TypeDef ) :: stats1 if stat.symbol.isClass =>
97
101
if (stat.symbol is Flags .Module ) {
102
+ def pushOnTop (xs : List [Tree ], ys : List [Tree ]): List [Tree ] = xs match {
103
+ case x :: xs1 => pushOnTop(xs1, x :: ys)
104
+ case Nil => ys
105
+ }
98
106
moduleClassDefs += (stat.name -> stat)
99
107
singleClassDefs -= stat.name.stripModuleClassSuffix
100
- val stats1r = reorder(stats1)
101
- revPrefix.reverse ::: ( if (moduleClassDefs contains stat.name) stat :: stats1r else stats1r)
108
+ val stats1r = reorder(stats1, Nil )
109
+ pushOnTop( revPrefix, if (moduleClassDefs contains stat.name) stat :: stats1r else stats1r)
102
110
} else {
103
111
reorder(
104
112
stats1,
@@ -137,7 +145,7 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
137
145
case stat => stat
138
146
}
139
147
140
- addMissingCompanions(reorder(stats))
148
+ addMissingCompanions(reorder(stats, Nil ))
141
149
}
142
150
143
151
private def newCompanion (name : TermName , forClass : Symbol )(implicit ctx : Context ) = {
0 commit comments