Skip to content

Commit 0c97f08

Browse files
committed
Merge pull request #159 from dotty-staging/transform/elimLocals
Added elimLocals miniphase
2 parents 4231d7a + 24bc239 commit 0c97f08

File tree

6 files changed

+33
-9
lines changed

6 files changed

+33
-9
lines changed

src/dotty/tools/dotc/Compiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Compiler {
2222
List(new Companions),
2323
List(new SuperAccessors),
2424
// pickling and refchecks goes here
25-
List(new ElimRepeated/*, new ElimLocals*/),
25+
List(new ElimRepeated, new ElimLocals),
2626
List(new ExtensionMethods),
2727
List(new TailRec),
2828
List(new PatternMatcher,

src/dotty/tools/dotc/ElimLocals.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ package dotty.tools.dotc
22
package transform
33

44
import core._
5-
import TreeTransforms.{TransformerInfo, TreeTransform, TreeTransformer}
6-
import DenotTransformers._
5+
import DenotTransformers.SymTransformer
6+
import Phases.Phase
7+
import Contexts.Context
8+
import SymDenotations.SymDenotation
9+
import TreeTransforms.TreeTransform
10+
import Flags.Local
711

812
/** Widens all private[this] and protected[this] qualifiers to just private/protected */
9-
abstract class ElimLocals extends TreeTransform with InfoTransformer { thisTransformer =>
13+
class ElimLocals extends TreeTransform with SymTransformer { thisTransformer =>
14+
override def name = "elimlocals"
1015

11-
// TODO complete
16+
def transformSym(ref: SymDenotation)(implicit ctx: Context) =
17+
dropLocal(ref)
1218

19+
private def dropLocal(ref: SymDenotation)(implicit ctx: Context) =
20+
if (ref.flags is Local) ref.copySymDenotation(initFlags = ref.flags &~ Local)
21+
else ref
1322
}

src/dotty/tools/dotc/core/DenotTransformers.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ object DenotTransformers {
3232
def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation
3333
}
3434

35+
/** A transformer that only transforms the info field of denotations */
3536
trait InfoTransformer extends DenotTransformer {
3637

3738
def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type
3839

39-
/** The transformation method */
4040
def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation = {
4141
val info1 = transformInfo(ref.info, ref.symbol)
4242
if (info1 eq ref.info) ref
@@ -47,6 +47,17 @@ object DenotTransformers {
4747
}
4848
}
4949

50+
/** A transformer that only transforms SymDenotations */
51+
trait SymTransformer extends DenotTransformer {
52+
53+
def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation
54+
55+
def transform(ref: SingleDenotation)(implicit ctx: Context): SingleDenotation = ref match {
56+
case ref: SymDenotation => transformSym(ref)
57+
case _ => ref
58+
}
59+
}
60+
5061
/** A `DenotTransformer` trait that has the identity as its `transform` method.
5162
* You might want to inherit from this trait so that new denotations can be
5263
* installed using `installAfter` and `enteredAfter` at the end of the phase.

src/dotty/tools/dotc/transform/Companions.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import Names.Name
1414
import NameOps._
1515

1616

17-
/** A transformer that provides a convenient way to create companion objects
18-
*/
17+
/** A transformer that creates companion objects for all classes except module classes. */
1918
class Companions extends TreeTransform with IdentityDenotTransformer { thisTransformer =>
2019
import ast.tpd._
2120

src/dotty/tools/dotc/typer/ReTyper.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ class ReTyper extends Typer {
6666

6767
override def addTypedModifiersAnnotations(mods: untpd.Modifiers, sym: Symbol)(implicit ctx: Context): Modifiers =
6868
typedModifiers(mods, sym)
69+
70+
override def checkVariance(tree: Tree)(implicit ctx: Context) = ()
6971
}

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
844844
checkNoDoubleDefs(cls)
845845
val impl1 = cpy.Template(impl, constr1, parents1, self1, body1)
846846
.withType(dummy.termRef)
847-
VarianceChecker.check(impl1)
847+
checkVariance(impl1)
848848
assignType(cpy.TypeDef(cdef, mods1, name, impl1), cls)
849849

850850
// todo later: check that
@@ -856,6 +856,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
856856
// 4. Polymorphic type defs override nothing.
857857
}
858858

859+
/** Overridden in retyper */
860+
def checkVariance(tree: Tree)(implicit ctx: Context) = VarianceChecker.check(tree)
861+
859862
def localDummy(cls: ClassSymbol, impl: untpd.Template)(implicit ctx: Context): Symbol =
860863
ctx.newLocalDummy(cls, impl.pos)
861864

0 commit comments

Comments
 (0)