|
| 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 | +/** Makes private methods static, provided they not deferred, accessors, or static, |
| 20 | + * by rewriting a method `m` in class `C` as follows: |
| 21 | + * |
| 22 | + * private def m(ps) = e |
| 23 | + * |
| 24 | + * --> private static def($this: C, ps) = [this -> $this] e |
| 25 | + */ |
| 26 | +class PrivateToStatic extends MiniPhase with SymTransformer { thisTransform => |
| 27 | + import ast.tpd._ |
| 28 | + override def phaseName = "privateToStatic" |
| 29 | + override def relaxedTyping = true |
| 30 | + |
| 31 | + private val Immovable = Deferred | Accessor | JavaStatic |
| 32 | + |
| 33 | + def shouldBeStatic(sd: SymDenotation)(implicit ctx: Context) = |
| 34 | + sd.current(ctx.withPhase(thisTransform)).asSymDenotation |
| 35 | + .is(PrivateMethod, butNot = Immovable) && |
| 36 | + (sd.owner.is(Trait) || sd.is(NotJavaPrivate)) |
| 37 | + |
| 38 | + override def transformSym(sd: SymDenotation)(implicit ctx: Context): SymDenotation = |
| 39 | + if (shouldBeStatic(sd)) { |
| 40 | + val mt @ MethodType(pnames, ptypes) = sd.info |
| 41 | + sd.copySymDenotation( |
| 42 | + initFlags = sd.flags | JavaStatic, |
| 43 | + info = MethodType(nme.SELF :: pnames, sd.owner.thisType :: ptypes, mt.resultType)) |
| 44 | + } |
| 45 | + else sd |
| 46 | + |
| 47 | + val treeTransform = new Transform(NoSymbol) |
| 48 | + |
| 49 | + class Transform(thisParam: Symbol) extends TreeTransform { |
| 50 | + def phase = thisTransform |
| 51 | + override def treeTransformPhase = thisTransform.next |
| 52 | + |
| 53 | + override def prepareForDefDef(tree: DefDef)(implicit ctx: Context) = |
| 54 | + if (shouldBeStatic(tree.symbol)) { |
| 55 | + val selfParam = ctx.newSymbol(tree.symbol, nme.SELF, Param, tree.symbol.owner.thisType, coord = tree.pos) |
| 56 | + new Transform(selfParam) |
| 57 | + } |
| 58 | + else this |
| 59 | + |
| 60 | + override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo) = |
| 61 | + if (shouldBeStatic(tree.symbol)) { |
| 62 | + val thisParamDef = ValDef(thisParam.asTerm) |
| 63 | + val vparams :: Nil = tree.vparamss |
| 64 | + cpy.DefDef(tree)( |
| 65 | + mods = tree.mods | JavaStatic, |
| 66 | + vparamss = (thisParamDef :: vparams) :: Nil) |
| 67 | + } |
| 68 | + else tree |
| 69 | + |
| 70 | + override def transformThis(tree: This)(implicit ctx: Context, info: TransformerInfo) = |
| 71 | + if (shouldBeStatic(ctx.owner.enclosingMethod)) ref(thisParam).withPos(tree.pos) |
| 72 | + else tree |
| 73 | + |
| 74 | + /** Rwrites a call to a method `m` which is made static as folows: |
| 75 | + * |
| 76 | + * qual.m(args) --> m(qual, args) |
| 77 | + */ |
| 78 | + override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo) = |
| 79 | + tree.fun match { |
| 80 | + case fun @ Select(qual, name) if shouldBeStatic(fun.symbol) => |
| 81 | + ctx.debuglog(i"mapping $tree to ${cpy.Ident(fun)(name)} (${qual :: tree.args}%, %)") |
| 82 | + cpy.Apply(tree)(ref(fun.symbol).withPos(fun.pos), qual :: tree.args) |
| 83 | + case _ => |
| 84 | + tree |
| 85 | + } |
| 86 | + |
| 87 | + override def transformClosure(tree: Closure)(implicit ctx: Context, info: TransformerInfo) = |
| 88 | + tree.meth match { |
| 89 | + case meth @ Select(qual, name) if shouldBeStatic(meth.symbol) => |
| 90 | + cpy.Closure(tree)( |
| 91 | + env = qual :: tree.env, |
| 92 | + meth = ref(meth.symbol).withPos(meth.pos)) |
| 93 | + case _ => |
| 94 | + tree |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments