Skip to content

Commit e6fc268

Browse files
committed
New phase trait constructors.
Renames constructors in traits so that backend will call them with invokeInterface, instead of invokeSpecial Also makes sure that renamed constructor bodies conforms to type of method
1 parent 01fae5d commit e6fc268

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Compiler {
6767
List(new LambdaLift,
6868
new Flatten,
6969
new RestoreScopes),
70-
List(/*new PrivateToStatic,*/ new CollectEntryPoints, new LabelDefs, new ElimWildcardIdents),
70+
List(/*new PrivateToStatic,*/ new CollectEntryPoints, new LabelDefs, new ElimWildcardIdents, new TraitConstructors),
7171
List(new GenBCode)
7272
)
7373

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dotty.tools.dotc.transform
2+
3+
import dotty.tools.dotc.ast.tpd
4+
import dotty.tools.dotc.core.Contexts.Context
5+
import dotty.tools.dotc.core.DenotTransformers.{SymTransformer, DenotTransformer}
6+
import dotty.tools.dotc.core.Denotations.SingleDenotation
7+
import dotty.tools.dotc.core.Phases.Phase
8+
import dotty.tools.dotc.core.StdNames._
9+
import dotty.tools.dotc.core.SymDenotations.SymDenotation
10+
import dotty.tools.dotc.core._
11+
import dotty.tools.dotc.transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo}
12+
13+
/***
14+
* Renames constructors in traits so that backend will call them with invokeInterface
15+
* Also makes sure that renamed constructor bodies conforms to type of method
16+
*/
17+
class TraitConstructors extends MiniPhaseTransform with SymTransformer {
18+
import dotty.tools.dotc.ast.tpd._
19+
def phaseName: String = "traitConstructors"
20+
21+
22+
override def treeTransformPhase: Phase = this.phase
23+
24+
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation = {
25+
if(sym.isPrimaryConstructor && (sym.owner is Flags.Trait))
26+
sym.copySymDenotation(name = nme.INITIALIZER_PREFIX ++ sym.owner.fullName)
27+
else sym
28+
}
29+
30+
override def transformDefDef(tree: tpd.DefDef)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
31+
val sym = tree.symbol
32+
if (sym.isPrimaryConstructor && (sym.owner is Flags.Trait))
33+
cpy.DefDef(tree)(rhs = Block(List(tree.rhs), This(tree.symbol.enclosingClass.asClass)))
34+
else tree
35+
}
36+
37+
}

0 commit comments

Comments
 (0)