|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import dotty.tools.dotc.ast.Trees._ |
| 5 | +import dotty.tools.dotc.ast.tpd |
| 6 | +import dotty.tools.dotc.core.Contexts.Context |
| 7 | +import dotty.tools.dotc.core.DenotTransformers.IdentityDenotTransformer |
| 8 | +import dotty.tools.dotc.core.Flags._ |
| 9 | +import dotty.tools.dotc.core.Symbols._ |
| 10 | +import dotty.tools.dotc.core._ |
| 11 | +import dotty.tools.dotc.transform.TreeTransforms._ |
| 12 | + |
| 13 | +/** Removes selects that would be compiled into GetStatic |
| 14 | + * otherwise backend needs to be aware that some qualifiers need to be dropped. |
| 15 | + * Similar transformation seems to be performed by flatten in nsc |
| 16 | + * @author Dmytro Petrashko |
| 17 | + */ |
| 18 | +class SelectStatic extends MiniPhaseTransform with IdentityDenotTransformer { thisTransform => |
| 19 | + import ast.tpd._ |
| 20 | + |
| 21 | + override def phaseName: String = "selectStatic" |
| 22 | + private val isPackage = FlagConjunction(PackageCreationFlags.bits) |
| 23 | + |
| 24 | + override def transformSelect(tree: tpd.Select)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { |
| 25 | + val sym = tree.symbol |
| 26 | + val r1 = |
| 27 | + if (!sym.is(isPackage) && !sym.maybeOwner.is(isPackage) && |
| 28 | + ( |
| 29 | + ((sym is Flags.Module) && sym.maybeOwner.isStaticOwner) || |
| 30 | + (sym is Flags.JavaStatic) || |
| 31 | + (sym.maybeOwner is Flags.ImplClass) || |
| 32 | + sym.hasAnnotation(ctx.definitions.ScalaStaticAnnot) |
| 33 | + ) |
| 34 | + ) |
| 35 | + if (!tree.qualifier.symbol.is(JavaModule) && !tree.qualifier.isType) |
| 36 | + Block(List(tree.qualifier), ref(sym)) |
| 37 | + else tree |
| 38 | + else tree |
| 39 | + |
| 40 | + normalize(r1) |
| 41 | + } |
| 42 | + |
| 43 | + private def normalize(t: Tree)(implicit ctx: Context) = t match { |
| 44 | + case Select(Block(stats, qual), nm) => |
| 45 | + Block(stats, cpy.Select(t)(qual, nm)) |
| 46 | + case Apply(Block(stats, qual), nm) => |
| 47 | + Block(stats, Apply(qual, nm)) |
| 48 | + case TypeApply(Block(stats, qual), nm) => |
| 49 | + Block(stats, TypeApply(qual, nm)) |
| 50 | + case _ => t |
| 51 | + } |
| 52 | + |
| 53 | + override def transformApply(tree: tpd.Apply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { |
| 54 | + normalize(tree) |
| 55 | + } |
| 56 | + |
| 57 | + override def transformTypeApply(tree: tpd.TypeApply)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { |
| 58 | + normalize(tree) |
| 59 | + } |
| 60 | +} |
0 commit comments