|
| 1 | +package dotty.tools.dotc |
| 2 | +package transform |
| 3 | + |
| 4 | +import core._ |
| 5 | +import DenotTransformers.SymTransformer |
| 6 | +import Contexts.Context |
| 7 | +import Symbols._ |
| 8 | +import Scopes._ |
| 9 | +import Flags._ |
| 10 | +import StdNames._ |
| 11 | +import SymDenotations._ |
| 12 | +import Types._ |
| 13 | +import collection.mutable |
| 14 | +import TreeTransforms._ |
| 15 | +import Decorators._ |
| 16 | +import ast.Trees._ |
| 17 | +import TreeTransforms.TransformerInfo |
| 18 | + |
| 19 | +/** The preceding lambda lift and flatten phases move symbols to different scopes |
| 20 | + * and rename them. This miniphase cleans up afterwards and makes sure that all |
| 21 | + * class scopes contain the symbols defined in them. |
| 22 | + */ |
| 23 | +class PrivateToStatic extends MiniPhase with SymTransformer { thisTransform => |
| 24 | + import ast.tpd._ |
| 25 | + override def phaseName = "privateToStatic" |
| 26 | + override def relaxedTyping = true |
| 27 | + |
| 28 | + private val Immovable = Deferred | Accessor | JavaStatic |
| 29 | + |
| 30 | + def shouldBeStatic(sd: SymDenotation)(implicit ctx: Context) = |
| 31 | + sd.current(ctx.withPhase(thisTransform)).asInstanceOf[SymDenotation] |
| 32 | + .is(PrivateMethod, butNot = Immovable) && |
| 33 | + (sd.owner.is(Trait) || sd.is(NotJavaPrivate)) |
| 34 | + |
| 35 | + override def transformSym(sd: SymDenotation)(implicit ctx: Context): SymDenotation = |
| 36 | + if (shouldBeStatic(sd)) { |
| 37 | + val mt @ MethodType(pnames, ptypes) = sd.info |
| 38 | + sd.copySymDenotation( |
| 39 | + initFlags = sd.flags | JavaStatic, |
| 40 | + info = MethodType(nme.SELF :: pnames, sd.owner.thisType :: ptypes, mt.resultType)) |
| 41 | + } |
| 42 | + else sd |
| 43 | + |
| 44 | + val treeTransform = new Transform(NoSymbol) |
| 45 | + |
| 46 | + class Transform(thisParam: Symbol) extends TreeTransform { |
| 47 | + def phase = thisTransform |
| 48 | + override def treeTransformPhase = thisTransform.next |
| 49 | + |
| 50 | + override def prepareForDefDef(tree: DefDef)(implicit ctx: Context) = |
| 51 | + if (shouldBeStatic(tree.symbol)) { |
| 52 | + val selfParam = ctx.newSymbol(tree.symbol, nme.SELF, Param, tree.symbol.owner.thisType, coord = tree.pos) |
| 53 | + new Transform(selfParam) |
| 54 | + } |
| 55 | + else this |
| 56 | + |
| 57 | + override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo) = |
| 58 | + if (shouldBeStatic(tree.symbol)) { |
| 59 | + val thisParamDef = ValDef(thisParam.asTerm) |
| 60 | + val vparams :: Nil = tree.vparamss |
| 61 | + cpy.DefDef(tree)( |
| 62 | + mods = tree.mods | JavaStatic, |
| 63 | + vparamss = (thisParamDef :: vparams) :: Nil) |
| 64 | + } |
| 65 | + else tree |
| 66 | + |
| 67 | + override def transformThis(tree: This)(implicit ctx: Context, info: TransformerInfo) = |
| 68 | + if (shouldBeStatic(ctx.owner.enclosingMethod)) ref(thisParam).withPos(tree.pos) |
| 69 | + else tree |
| 70 | + |
| 71 | + override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo) = |
| 72 | + tree.fun match { |
| 73 | + case fun @ Select(qual, name) if shouldBeStatic(fun.symbol) => |
| 74 | + println(i"mapping $tree to ${cpy.Ident(fun)(name)} (${qual :: tree.args}%, %)") |
| 75 | + cpy.Apply(tree)(ref(fun.symbol).withPos(fun.pos), qual :: tree.args) |
| 76 | + case _ => |
| 77 | + tree |
| 78 | + } |
| 79 | + |
| 80 | + override def transformClosure(tree: Closure)(implicit ctx: Context, info: TransformerInfo) = |
| 81 | + tree.meth match { |
| 82 | + case meth @ Select(qual, name) if shouldBeStatic(meth.symbol) => |
| 83 | + cpy.Closure(tree)( |
| 84 | + env = qual :: tree.env, |
| 85 | + meth = ref(meth.symbol).withPos(meth.pos)) |
| 86 | + case _ => |
| 87 | + tree |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments