-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Transform/private to static #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d2670a7
Made LambdaLift capable of having minitransforms run after it.
odersky ad45e2e
New miniphase: Flatten
odersky 252b6d9
Generalize lift behavior between Flatten and LambdaLift
odersky 936e83f
New phase: RestoreScopes
odersky d907f26
Bugfix for superaccessors
odersky 250418e
New phase: PrivateToStatic
odersky 74ec188
Fixes to documentation.
odersky 3da5448
Making print statement a debuglog
odersky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import core._ | ||
import DenotTransformers.SymTransformer | ||
import Phases.Phase | ||
import Contexts.Context | ||
import Flags._ | ||
import SymUtils._ | ||
import SymDenotations.SymDenotation | ||
import collection.mutable | ||
import TreeTransforms.MiniPhaseTransform | ||
import dotty.tools.dotc.transform.TreeTransforms.TransformerInfo | ||
|
||
class Flatten extends MiniPhaseTransform with SymTransformer { thisTransform => | ||
import ast.tpd._ | ||
override def phaseName = "flatten" | ||
|
||
def transformSym(ref: SymDenotation)(implicit ctx: Context) = { | ||
if (ref.isClass && !ref.is(Package) && !ref.owner.is(Package)) { | ||
ref.copySymDenotation( | ||
name = ref.flatName, | ||
owner = ref.enclosingPackageClass) | ||
} | ||
else ref | ||
} | ||
|
||
override def treeTransformPhase = thisTransform.next | ||
|
||
private val liftedDefs = new mutable.ListBuffer[Tree] | ||
|
||
private def liftIfNested(tree: Tree)(implicit ctx: Context, info: TransformerInfo) = | ||
if (ctx.owner is Package) tree | ||
else { | ||
transformFollowing(tree).foreachInThicket(liftedDefs += _) | ||
EmptyTree | ||
} | ||
|
||
override def transformStats(stats: List[Tree])(implicit ctx: Context, info: TransformerInfo) = | ||
if (ctx.owner is Package) { | ||
val liftedStats = stats ++ liftedDefs | ||
liftedDefs.clear | ||
liftedStats | ||
} | ||
else stats | ||
|
||
override def transformTypeDef(tree: TypeDef)(implicit ctx: Context, info: TransformerInfo) = | ||
liftIfNested(tree) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
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) || sd.is(NotJavaPrivate)) | ||
|
||
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 treeTransformPhase = thisTransform.next | ||
|
||
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)( | ||
mods = tree.mods | JavaStatic, | ||
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) = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get what's happening here. Could you please include documentation for this method? |
||
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 | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package dotty.tools.dotc | ||
package transform | ||
|
||
import core._ | ||
import DenotTransformers.IdentityDenotTransformer | ||
import Contexts.Context | ||
import Symbols._ | ||
import Scopes._ | ||
import collection.mutable | ||
import TreeTransforms.MiniPhaseTransform | ||
import ast.Trees._ | ||
import TreeTransforms.TransformerInfo | ||
|
||
/** The preceding lambda lift and flatten phases move symbols to different scopes | ||
* and rename them. This miniphase cleans up afterwards and makes sure that all | ||
* class scopes contain the symbols defined in them. | ||
*/ | ||
class RestoreScopes extends MiniPhaseTransform with IdentityDenotTransformer { thisTransform => | ||
import ast.tpd._ | ||
override def phaseName = "restoreScopes" | ||
|
||
override def treeTransformPhase = thisTransform.next | ||
|
||
override def transformTypeDef(tree: TypeDef)(implicit ctx: Context, info: TransformerInfo) = { | ||
val TypeDef(_, _, Template(constr, _, _, body)) = tree | ||
val restoredDecls = newScope | ||
for (stat <- constr :: body) | ||
if (stat.isInstanceOf[MemberDef] && stat.symbol.exists) | ||
restoredDecls.enter(stat.symbol) | ||
val cinfo = tree.symbol.asClass.classInfo | ||
tree.symbol.copySymDenotation( | ||
info = cinfo.derivedClassInfo( // Dotty deviation: Cannot expand cinfo inline without a type error | ||
decls = restoredDecls: Scope)).installAfter(thisTransform) | ||
tree | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't rhs be also transformed to use new argument instead of
this
in the similar way howtailrec
andextensionMethods
does it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I've got it. You are doing it already and you don't need handling of complicated types as you are after erasure.