aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/dotty/tools/dotc/transform/PrivateToStatic.scala.disabled
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-11-02 11:08:28 +0100
committerGuillaume Martres <smarter@ubuntu.com>2016-11-22 01:35:07 +0100
commit8a61ff432543a29234193cd1f7c14abd3f3d31a0 (patch)
treea8147561d307af862c295cfc8100d271063bb0dd /compiler/src/dotty/tools/dotc/transform/PrivateToStatic.scala.disabled
parent6a455fe6da5ff9c741d91279a2dc6fe2fb1b472f (diff)
downloaddotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.tar.gz
dotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.tar.bz2
dotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.zip
Move compiler and compiler tests to compiler dir
Diffstat (limited to 'compiler/src/dotty/tools/dotc/transform/PrivateToStatic.scala.disabled')
-rw-r--r--compiler/src/dotty/tools/dotc/transform/PrivateToStatic.scala.disabled94
1 files changed, 94 insertions, 0 deletions
diff --git a/compiler/src/dotty/tools/dotc/transform/PrivateToStatic.scala.disabled b/compiler/src/dotty/tools/dotc/transform/PrivateToStatic.scala.disabled
new file mode 100644
index 000000000..218839d01
--- /dev/null
+++ b/compiler/src/dotty/tools/dotc/transform/PrivateToStatic.scala.disabled
@@ -0,0 +1,94 @@
+package dotty.tools.dotc
+package transform
+
+import core._
+import DenotTransformers.SymTransformer
+import Contexts.Context
+import Symbols._
+import Scopes._
+import Flags._
+import StdNames._
+import SymDenotations._
+import Types._
+import collection.mutable
+import TreeTransforms._
+import Decorators._
+import ast.Trees._
+import TreeTransforms.TransformerInfo
+
+/** Makes private methods static, provided they not deferred, accessors, or static,
+ * by rewriting a method `m` in class `C` as follows:
+ *
+ * private def m(ps) = e
+ *
+ * --> private static def($this: C, ps) = [this -> $this] e
+ */
+class PrivateToStatic extends MiniPhase with SymTransformer { thisTransform =>
+ import ast.tpd._
+ override def phaseName = "privateToStatic"
+ override def relaxedTyping = true
+
+ private val Immovable = Deferred | Accessor | JavaStatic
+
+ def shouldBeStatic(sd: SymDenotation)(implicit ctx: Context) =
+ sd.current(ctx.withPhase(thisTransform)).asSymDenotation
+ .is(PrivateMethod, butNot = Immovable) &&
+ sd.owner.is(Trait)
+
+ override def transformSym(sd: SymDenotation)(implicit ctx: Context): SymDenotation =
+ if (shouldBeStatic(sd)) {
+ val mt @ MethodType(pnames, ptypes) = sd.info
+ sd.copySymDenotation(
+ initFlags = sd.flags | JavaStatic,
+ info = MethodType(nme.SELF :: pnames, sd.owner.thisType :: ptypes, mt.resultType))
+ }
+ else sd
+
+ val treeTransform = new Transform(NoSymbol)
+
+ class Transform(thisParam: Symbol) extends TreeTransform {
+ def phase = thisTransform
+
+ override def prepareForDefDef(tree: DefDef)(implicit ctx: Context) =
+ if (shouldBeStatic(tree.symbol)) {
+ val selfParam = ctx.newSymbol(tree.symbol, nme.SELF, Param, tree.symbol.owner.thisType, coord = tree.pos)
+ new Transform(selfParam)
+ }
+ else this
+
+ override def transformDefDef(tree: DefDef)(implicit ctx: Context, info: TransformerInfo) =
+ if (shouldBeStatic(tree.symbol)) {
+ val thisParamDef = ValDef(thisParam.asTerm)
+ val vparams :: Nil = tree.vparamss
+ cpy.DefDef(tree)(vparamss = (thisParamDef :: vparams) :: Nil)
+ }
+ else tree
+
+ override def transformThis(tree: This)(implicit ctx: Context, info: TransformerInfo) =
+ if (shouldBeStatic(ctx.owner.enclosingMethod)) ref(thisParam).withPos(tree.pos)
+ else tree
+
+ /** Rwrites a call to a method `m` which is made static as folows:
+ *
+ * qual.m(args) --> m(qual, args)
+ */
+ override def transformApply(tree: Apply)(implicit ctx: Context, info: TransformerInfo) =
+ tree.fun match {
+ case fun @ Select(qual, name) if shouldBeStatic(fun.symbol) =>
+ ctx.debuglog(i"mapping $tree to ${cpy.Ident(fun)(name)} (${qual :: tree.args}%, %)")
+ cpy.Apply(tree)(ref(fun.symbol).withPos(fun.pos), qual :: tree.args)
+ case _ =>
+ tree
+ }
+
+ override def transformClosure(tree: Closure)(implicit ctx: Context, info: TransformerInfo) =
+ tree.meth match {
+ case meth @ Select(qual, name) if shouldBeStatic(meth.symbol) =>
+ cpy.Closure(tree)(
+ env = qual :: tree.env,
+ meth = ref(meth.symbol).withPos(meth.pos))
+ case _ =>
+ tree
+ }
+ }
+}