@@ -13,7 +13,7 @@ import symtab.Flags._
13
13
/** This phase converts classes with parameters into Java-like classes with
14
14
* fields, which are assigned to from constructors.
15
15
*/
16
- abstract class Constructors extends Transform with ast.TreeDSL {
16
+ abstract class Constructors extends Statics with Transform with ast.TreeDSL {
17
17
import global ._
18
18
import definitions ._
19
19
@@ -199,7 +199,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
199
199
detectUsages walk auxConstructorBuf
200
200
}
201
201
}
202
- def mustbeKept (sym : Symbol ) = ! omittables(sym)
202
+ def mustBeKept (sym : Symbol ) = ! omittables(sym)
203
203
204
204
} // OmittablesHelper
205
205
@@ -458,7 +458,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
458
458
} // GuardianOfCtorStmts
459
459
460
460
private class TemplateTransformer (val unit : CompilationUnit , val impl : Template )
461
- extends Transformer
461
+ extends StaticsTransformer
462
462
with DelayedInitHelper
463
463
with OmittablesHelper
464
464
with GuardianOfCtorStmts {
@@ -607,7 +607,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
607
607
// follow the primary constructor
608
608
val auxConstructorBuf = new ListBuffer [Tree ]
609
609
610
- // The list of statements that go into constructor after and including the superclass constructor call
610
+ // The list of statements that go into the constructor after and including the superclass constructor call
611
611
val constrStatBuf = new ListBuffer [Tree ]
612
612
613
613
// The list of early initializer statements that go into constructor before the superclass constructor call
@@ -616,6 +616,9 @@ abstract class Constructors extends Transform with ast.TreeDSL {
616
616
// The early initialized field definitions of the class (these are the class members)
617
617
val presupers = treeInfo.preSuperFields(stats)
618
618
619
+ // The list of statements that go into the class initializer
620
+ val classInitStatBuf = new ListBuffer [Tree ]
621
+
619
622
// generate code to copy pre-initialized fields
620
623
for (stat <- constrBody.stats) {
621
624
constrStatBuf += stat
@@ -644,7 +647,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
644
647
else if (stat.symbol.isConstructor) auxConstructorBuf += stat
645
648
else defBuf += stat
646
649
}
647
- case ValDef (_ , _, _, rhs) =>
650
+ case ValDef (mods , _, _, rhs) if ! mods.hasStaticFlag =>
648
651
// val defs with constant right-hand sides are eliminated.
649
652
// for all other val defs, an empty valdef goes into the template and
650
653
// the initializer goes as an assignment into the constructor
@@ -659,6 +662,11 @@ abstract class Constructors extends Transform with ast.TreeDSL {
659
662
}
660
663
defBuf += deriveValDef(stat)(_ => EmptyTree )
661
664
}
665
+ case ValDef (_, _, _, rhs) =>
666
+ // Add static initializer statements to classInitStatBuf and remove the rhs from the val def.
667
+ classInitStatBuf += mkAssign(stat.symbol, rhs)
668
+ defBuf += deriveValDef(stat)(_ => EmptyTree )
669
+
662
670
case ClassDef (_, _, _, _) =>
663
671
// classes are treated recursively, and left in the template
664
672
defBuf += new ConstructorTransformer (unit).transform(stat)
@@ -670,7 +678,7 @@ abstract class Constructors extends Transform with ast.TreeDSL {
670
678
populateOmittables()
671
679
672
680
// Initialize all parameters fields that must be kept.
673
- val paramInits = paramAccessors filter mustbeKept map { acc =>
681
+ val paramInits = paramAccessors filter mustBeKept map { acc =>
674
682
// Check for conflicting symbol amongst parents: see bug #1960.
675
683
// It would be better to mangle the constructor parameter name since
676
684
// it can only be used internally, but I think we need more robust name
@@ -710,11 +718,13 @@ abstract class Constructors extends Transform with ast.TreeDSL {
710
718
defBuf ++= auxConstructorBuf
711
719
712
720
// Unlink all fields that can be dropped from class scope
713
- for (sym <- clazz.info.decls ; if ! mustbeKept (sym))
721
+ for (sym <- clazz.info.decls ; if ! mustBeKept (sym))
714
722
clazz.info.decls unlink sym
715
723
716
724
// Eliminate all field definitions that can be dropped from template
717
- val transformed : Template = deriveTemplate(impl)(_ => defBuf.toList filter (stat => mustbeKept(stat.symbol)))
725
+ val templateWithoutOmittables : Template = deriveTemplate(impl)(_ => defBuf.toList filter (stat => mustBeKept(stat.symbol)))
726
+ // Add the static initializers
727
+ val transformed : Template = addStaticInits(templateWithoutOmittables, classInitStatBuf, localTyper)
718
728
719
729
} // TemplateTransformer
720
730
0 commit comments