diff options
author | Dmitry Petrashko <dark@d-d.me> | 2016-08-15 16:58:03 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-15 16:58:03 +0200 |
commit | a7fab6d84854c5b1006427cca8b1ed32b8172689 (patch) | |
tree | 3e756a40382c5b08e273d6dfb786dd9571d690f8 /src | |
parent | 3179b03969ad95363094b343618a29e0e961126b (diff) | |
parent | 376fa067b9233838aa4a7e3e74fba3c9bf260b6b (diff) | |
download | dotty-a7fab6d84854c5b1006427cca8b1ed32b8172689.tar.gz dotty-a7fab6d84854c5b1006427cca8b1ed32b8172689.tar.bz2 dotty-a7fab6d84854c5b1006427cca8b1ed32b8172689.zip |
Merge pull request #1445 from dotty-staging/fix-#1442
Fix #1442: add new phase, SelectStatic
Diffstat (limited to 'src')
-rw-r--r-- | src/dotty/tools/dotc/Compiler.scala | 1 | ||||
-rw-r--r-- | src/dotty/tools/dotc/transform/SelectStatic.scala | 60 |
2 files changed, 61 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/Compiler.scala b/src/dotty/tools/dotc/Compiler.scala index d447c3826..d1f126860 100644 --- a/src/dotty/tools/dotc/Compiler.scala +++ b/src/dotty/tools/dotc/Compiler.scala @@ -91,6 +91,7 @@ class Compiler { new Flatten, // Lift all inner classes to package scope new RestoreScopes), // Repair scopes rendered invalid by moving definitions in prior phases of the group List(new ExpandPrivate, // Widen private definitions accessed from nested classes + new SelectStatic, // get rid of selects that would be compiled into GetStatic new CollectEntryPoints, // Find classes with main methods new CollectSuperCalls, // Find classes that are called with super new MoveStatics, // Move static methods to companion classes diff --git a/src/dotty/tools/dotc/transform/SelectStatic.scala b/src/dotty/tools/dotc/transform/SelectStatic.scala new file mode 100644 index 000000000..504a66c2f --- /dev/null +++ b/src/dotty/tools/dotc/transform/SelectStatic.scala @@ -0,0 +1,60 @@ +package dotty.tools.dotc +package transform + +import dotty.tools.dotc.ast.Trees._ +import dotty.tools.dotc.ast.tpd +import dotty.tools.dotc.core.Contexts.Context +import dotty.tools.dotc.core.DenotTransformers.IdentityDenotTransformer +import dotty.tools.dotc.core.Flags._ +import dotty.tools.dotc.core.Symbols._ +import dotty.tools.dotc.core._ +import dotty.tools.dotc.transform.TreeTransforms._ + +/** Removes selects that would be compiled into GetStatic + * otherwise backend needs to be aware that some qualifiers need to be dropped. + * Similar transformation seems to be performed by flatten in nsc + * @author Dmytro Petrashko + */ +class SelectStatic extends MiniPhaseTransform with IdentityDenotTransformer { thisTransform => + import ast.tpd._ + + override def phaseName: String = "selectStatic" + private val isPackage = FlagConjunction(PackageCreationFlags.bits) + + override def transformSelect(tree: tpd.Select)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { + val sym = tree.symbol + val r1 = + if (!sym.is(isPackage) && !sym.maybeOwner.is(isPackage) && + ( + ((sym is Flags.Module) && sym.maybeOwner.isStaticOwner) || + (sym is Flags.JavaStatic) || + (sym.maybeOwner is Flags.ImplClass) || + sym.hasAnnotation(ctx.definitions.ScalaStaticAnnot) + ) + ) + if (!tree.qualifier.symbol.is(JavaModule) && !tree.qualifier.isType) + Block(List(tree.qualifier), ref(sym)) + else tree + else tree + + normalize(r1) + } + + private def normalize(t: Tree)(implicit ctx: Context) = t match { + case Select(Block(stats, qual), nm) => + Block(stats, cpy.Select(t)(qual, nm)) + case Apply(Block(stats, qual), nm) => + Block(stats, Apply(qual, nm)) + case TypeApply(Block(stats, qual), nm) => + Block(stats, TypeApply(qual, nm)) + case _ => t + } + + override def transformApply(tree: tpd.Apply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { + normalize(tree) + } + + override def transformTypeApply(tree: tpd.TypeApply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { + normalize(tree) + } +} |