aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/transform/TraitConstructors.scala
blob: 99ae3e187f07d9840d7a6c75b5269bac382c73f2 (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
38
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))
      // TODO: Someone needs to carefully check if name clashes are possible with this mangling scheme
      sym.copySymDenotation(name = nme.INITIALIZER_PREFIX ++ sym.owner.fullNameSeparated("$"))
    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
  }

}