summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/transform/Statics.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler/scala/tools/nsc/transform/Statics.scala')
-rw-r--r--src/compiler/scala/tools/nsc/transform/Statics.scala59
1 files changed, 21 insertions, 38 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/Statics.scala b/src/compiler/scala/tools/nsc/transform/Statics.scala
index 4673be6de7..776805fd9f 100644
--- a/src/compiler/scala/tools/nsc/transform/Statics.scala
+++ b/src/compiler/scala/tools/nsc/transform/Statics.scala
@@ -1,49 +1,32 @@
package scala.tools.nsc
package transform
-import collection.mutable.Buffer
-
abstract class Statics extends Transform with ast.TreeDSL {
import global._
- class StaticsTransformer extends Transformer {
-
- /** finds the static ctor DefDef tree within the template if it exists. */
- def findStaticCtor(template: Template): Option[Tree] =
- template.body find {
- case defdef @ DefDef(_, nme.CONSTRUCTOR, _, _, _, _) => defdef.symbol.hasStaticFlag
- case _ => false
- }
-
- /** changes the template for the class so that it contains a static constructor with symbol fields inits,
- * augments an existing static ctor if one already existed.
+ trait StaticsTransformer extends Transformer {
+ /** generate a static constructor with symbol fields inits, or an augmented existing static ctor
*/
- def addStaticInits(template: Template, newStaticInits: Buffer[Tree], localTyper: analyzer.Typer): Template = {
- if (newStaticInits.isEmpty)
- template
- else {
- val newCtor = findStaticCtor(template) match {
- // in case there already were static ctors - augment existing ones
- // currently, however, static ctors aren't being generated anywhere else
- case Some(ctor @ DefDef(_,_,_,_,_,_)) =>
- // modify existing static ctor
- deriveDefDef(ctor) {
- case block @ Block(stats, expr) =>
- // need to add inits to existing block
- treeCopy.Block(block, newStaticInits.toList ::: stats, expr)
- case term: TermTree =>
- // need to create a new block with inits and the old term
- treeCopy.Block(term, newStaticInits.toList, term)
- }
- case _ =>
- // create new static ctor
- val staticCtorSym = currentClass.newStaticConstructor(template.pos)
- val rhs = Block(newStaticInits.toList, Literal(Constant(())))
+ def staticConstructor(body: List[Tree], localTyper: analyzer.Typer, pos: Position)(newStaticInits: List[Tree]): Tree =
+ body.collectFirst {
+ // If there already was a static ctor - augment existing one
+ // currently, however, static ctors aren't being generated anywhere else (!!!)
+ case ctor@DefDef(_, nme.CONSTRUCTOR, _, _, _, _) if ctor.symbol.hasStaticFlag =>
+ // modify existing static ctor
+ deriveDefDef(ctor) {
+ case block@Block(stats, expr) =>
+ // need to add inits to existing block
+ treeCopy.Block(block, newStaticInits ::: stats, expr)
+ case term: TermTree =>
+ // need to create a new block with inits and the old term
+ treeCopy.Block(term, newStaticInits, term)
+ }
+ } getOrElse {
+ // create new static ctor
+ val staticCtorSym = currentClass.newStaticConstructor(pos)
+ val rhs = Block(newStaticInits, Literal(Constant(())))
- localTyper.typedPos(template.pos)(DefDef(staticCtorSym, rhs))
- }
- deriveTemplate(template)(newCtor :: _)
+ localTyper.typedPos(pos)(DefDef(staticCtorSym, rhs))
}
- }
}
}