Skip to content

Commit dd80fe0

Browse files
committed
New phase: ElimStaticThis
1 parent 878b55a commit dd80fe0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Compiler {
6767
new Constructors,
6868
new FunctionalInterfaces),
6969
List(new LambdaLift, // in this mini-phase block scopes are incorrect. No phases that rely on scopes should be here
70+
new ElimStaticThis,
7071
new Flatten,
7172
new RestoreScopes),
7273
List(/*new PrivateToStatic,*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package dotty.tools.dotc
2+
package transform
3+
4+
import core._
5+
import Contexts.Context
6+
import Flags._
7+
import dotty.tools.dotc.ast.tpd
8+
import dotty.tools.dotc.core.StdNames._
9+
import dotty.tools.dotc.core.SymDenotations.SymDenotation
10+
import TreeTransforms.{MiniPhaseTransform, TransformerInfo}
11+
import dotty.tools.dotc.core.Types.{ThisType, TermRef}
12+
13+
/** Replace This references to module classes in static methods by global identifiers to the
14+
* corresponding modules.
15+
*/
16+
class ElimStaticThis extends MiniPhaseTransform {
17+
import ast.tpd._
18+
def phaseName: String = "elimStaticThis"
19+
20+
override def transformThis(tree: This)(implicit ctx: Context, info: TransformerInfo): Tree =
21+
if (!tree.symbol.is(Package) && ctx.owner.enclosingMethod.is(JavaStatic)) {
22+
assert(tree.symbol.is(ModuleClass))
23+
ref(tree.symbol.sourceModule)
24+
}
25+
else tree
26+
27+
override def transformIdent(tree: tpd.Ident)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
28+
if (ctx.owner.enclosingMethod.is(JavaStatic)) {
29+
tree.tpe match {
30+
case TermRef(thiz: ThisType, _) =>
31+
assert(thiz.underlying.typeSymbol.is(ModuleClass))
32+
ref(thiz.underlying.typeSymbol.sourceModule).select(tree.symbol)
33+
case _ => tree
34+
}
35+
}
36+
else tree
37+
}
38+
}

0 commit comments

Comments
 (0)