aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/transform/TraitConstructors.scala
blob: 8c92f1f7bf05a5da8b80b0bef4d5e88e38a29b26 (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
33
34
35
36
37
package dotty.tools.dotc.transform

import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.DenotTransformers.{SymTransformer, DenotTransformer}
import dotty.tools.dotc.core.Denotations.SingleDenotation
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.SymDenotations.SymDenotation
import dotty.tools.dotc.core._
import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo}

/***
  * Renames constructors in traits so that backend will call them with invokeInterface
  * Also makes sure that renamed constructor bodies conforms to type of method
 */
class TraitConstructors extends MiniPhaseTransform with SymTransformer {
  import dotty.tools.dotc.ast.tpd._
  def phaseName: String = "traitConstructors"


  override def treeTransformPhase: Phase = this.phase

  def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = {
    if(sym.isPrimaryConstructor && (sym.owner is Flags.Trait))
      sym.copySymDenotation(name = nme.INITIALIZER_PREFIX ++ sym.owner.fullName)
    else sym
  }

  override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
    val sym = tree.symbol
    if (sym.isPrimaryConstructor && (sym.owner is Flags.Trait))
      cpy.DefDef(tree)(rhs = Block(List(tree.rhs), This(tree.symbol.enclosingClass.asClass)))
    else tree
  }

}