-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Link from tasty #2910
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
Link from tasty #2910
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b0b245
Load compilation units from tasty
nicolasstucki 96747c8
Add link tests
nicolasstucki da829a3
Unify tree loaders
nicolasstucki 7bbc52e
Move loadCompilationUnit out of FromTasty
nicolasstucki 23e2821
Remove empty packages loaded from tasty
nicolasstucki f52541e
Update tests
nicolasstucki eb9afe2
Remove unitTree and use tree instead
nicolasstucki a2d0783
Cleanup after check test failure
nicolasstucki 78548e0
Extract -Xlink-optimise tests from vulpix
nicolasstucki 7906928
Do not force trees when linking
nicolasstucki 5115c7f
Add documentation and improve code
nicolasstucki 86e0e32
Add example with Template inside parent
nicolasstucki 1f3c305
Add documentation
nicolasstucki 4f0b070
Add clear separation between flags and classpath in tests
nicolasstucki 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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package dotty.tools.dotc.transform | ||
|
||
import dotty.tools.dotc.CompilationUnit | ||
import dotty.tools.dotc.ast.Trees._ | ||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.core.SymDenotations.ClassDenotation | ||
import dotty.tools.dotc.core.Symbols._ | ||
import dotty.tools.dotc.core.Flags._ | ||
import dotty.tools.dotc.transform.TreeTransforms._ | ||
|
||
/** Loads all potentially reachable trees from tasty. ▲ | ||
* Only performed on whole world optimization mode. ▲ ▲ | ||
* | ||
* TODO: Next step is to only load compilation units reachable in the call graph | ||
*/ | ||
class LinkAll extends MiniPhaseTransform { | ||
import tpd._ | ||
import LinkAll._ | ||
|
||
override def phaseName = "linkAll" | ||
|
||
/** Do not transform the any tree, runOn will traverse the trees and reload compilation units if needed */ | ||
override def prepareForUnit(tree: tpd.Tree)(implicit ctx: Context): TreeTransform = NoTransform | ||
|
||
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = { | ||
/** Loads and processes new compilation units, possibly loading more units. */ | ||
def allUnits(processed: Set[CompilationUnit], unprocessed: Set[CompilationUnit], loadedClasses: Set[ClassDenotation])(implicit ctx: Context): List[CompilationUnit] = { | ||
if (unprocessed.isEmpty) processed.toList | ||
else { | ||
val accum = new ClassesToLoadAccumulator | ||
val classesToLoad = unprocessed.foldLeft(Set.empty[ClassDenotation])((acc, unit) => accum.apply(acc, unit.tpdTree)) -- loadedClasses | ||
val loadedUnits = classesToLoad.flatMap(cls => loadCompilationUnit(cls)) | ||
allUnits(processed ++ unprocessed, loadedUnits, loadedClasses ++ classesToLoad) | ||
} | ||
} | ||
|
||
if (ctx.settings.XlinkOptimise.value) super.runOn(allUnits(Set.empty, units.toSet, Set.empty)) | ||
else super.runOn(units) | ||
} | ||
|
||
/** Collects all class denotations that may need to be loaded. */ | ||
private class ClassesToLoadAccumulator extends TreeAccumulator[Set[ClassDenotation]] { | ||
private var inParents = false | ||
override def apply(acc: Set[ClassDenotation], tree: tpd.Tree)(implicit ctx: Context): Set[ClassDenotation] = tree match { | ||
case New(tpt) => accum(acc, tpt.tpe.classSymbol) | ||
case AppliedTypeTree(tpt, _) if inParents => accum(acc, tpt.symbol) | ||
case tree: RefTree if inParents || tree.symbol.is(Module) => | ||
foldOver(accum(acc, tree.symbol), tree) | ||
case tree @ Template(constr, parents, self, _) => | ||
val acc1 = this(acc, constr) | ||
inParents = true | ||
val acc2 = this(acc1, parents) | ||
inParents = false | ||
this(this(acc2, self), tree.body) | ||
case _ => foldOver(acc, tree) | ||
} | ||
|
||
/** Accumulate class denotation for `sym` if needed */ | ||
private def accum(acc: Set[ClassDenotation], sym: Symbol)(implicit ctx: Context): Set[ClassDenotation] = { | ||
val topClass = sym.topLevelClass.denot.asClass | ||
if (topClass.is(JavaDefined) || topClass.is(Scala2x) || topClass.symbol == defn.ObjectClass) acc | ||
else acc + topClass | ||
} | ||
} | ||
} | ||
|
||
object LinkAll { | ||
|
||
private[LinkAll] def loadCompilationUnit(clsd: ClassDenotation)(implicit ctx: Context): Option[CompilationUnit] = { | ||
assert(ctx.settings.XlinkOptimise.value) | ||
val tree = clsd.symbol.asClass.tree | ||
if (tree.isEmpty) None | ||
else { | ||
ctx.log("Loading compilation unit for: " + clsd) | ||
Some(CompilationUnit.mkCompilationUnit(clsd, tree, forceTrees = false)) | ||
} | ||
} | ||
|
||
} |
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.
Don't think this is correct in general, you assume that inParents will be false before you set it to true, but you can have a Template inside the parents of a Template, e.g:
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.
This works, I added a test for it.