summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/transform/Statics.scala
blob: 776805fd9f1ca068579731b83b71bdf97d48d468 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package scala.tools.nsc
package transform

abstract class Statics extends Transform with ast.TreeDSL {
  import global._

  trait StaticsTransformer extends Transformer {
    /** generate a static constructor with symbol fields inits, or an augmented existing static ctor
      */
    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(pos)(DefDef(staticCtorSym, rhs))
      }
  }
}