Skip to content

Keep loaded pickled tasty when compiling from tasty #3492

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 3 commits into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -604,22 +604,38 @@ object Symbols {

/** If this is either:
* - a top-level class and `-Yretain-trees` is set
* - a top-level class loaded from TASTY and `-Xlink-optimise` is set
* - a top-level class loaded from TASTY and `-tasty` or `-Xlink` is set
* then return the TypeDef tree (possibly wrapped inside PackageDefs) for this class, otherwise EmptyTree.
* This will force the info of the class.
*/
def tree(implicit ctx: Context): tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = {
load()
myTree
}

/** If this is either:
* - a top-level class loaded from TASTY with `-tasty`
* then return the bytes of the tasty.
*/
def pickled(implicit ctx: Context): Array[Byte] = {
load()
myTasty
}

private def load()(implicit ctx: Context): Unit = {
denot.info
// TODO: Consider storing this tree like we store lazy trees for inline functions
if (unpickler != null && !denot.isAbsent) {
assert(myTree.isEmpty)
val body = unpickler.body(ctx.addMode(Mode.ReadPositions))
myTree = body.headOption.getOrElse(tpd.EmptyTree)
if (ctx.settings.tasty.value)
myTasty = unpickler.unpickler.bytes
unpickler = null
}
myTree
}
private[this] var myTree: tpd.Tree /* tpd.PackageDef | tpd.TypeDef | tpd.EmptyTree */ = tpd.EmptyTree
private[this] var myTasty: Array[Byte] = _
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, I would rather not add a field to every ClassSymbol for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alternative is to keep the unpickler longer. I will go back to that solution.

private[dotc] var unpickler: tasty.DottyUnpickler = _

private[dotc] def registerTree(tree: tpd.TypeDef)(implicit ctx: Context): Unit = {
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,6 @@ class TastyUnpickler(reader: TastyReader) {
def unpickle[R](sec: SectionUnpickler[R]): Option[R] =
for (reader <- sectionReader.get(sec.name)) yield
sec.unpickle(reader, nameAtRef)

private[dotc] def bytes: Array[Byte] = reader.bytes
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ class ReadTastyTreesFromClasses extends FrontEnd {
tree(className).flatMap {
case (clsd, unpickled) =>
if (unpickled.isEmpty) None
else Some(CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true))

else {
val unit = CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true)
val cls = clsd.symbol.asClass
unit.pickled += (cls -> cls.pickled)
Some(unit)
}
}
}
// The TASTY section in a/b/C.class may either contain a class a.b.C, an object a.b.C, or both.
Expand All @@ -41,12 +45,13 @@ class ReadTastyTreesFromClasses extends FrontEnd {
val clsd = ctx.base.staticRef(className)
ctx.base.staticRef(className) match {
case clsd: ClassDenotation =>
val cls = clsd.symbol.asClass
def cannotUnpickle(reason: String) =
ctx.error(s"class $className cannot be unpickled because $reason")
def tryToLoad = clsd.infoOrCompleter match {
case info: ClassfileLoader =>
info.load(clsd)
Option(clsd.symbol.asClass.tree).orElse {
Option(cls.tree).orElse {
cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
None
}
Expand All @@ -55,7 +60,7 @@ class ReadTastyTreesFromClasses extends FrontEnd {
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
None
}
Option(clsd.symbol.asClass.tree).orElse(tryToLoad).map(tree => (clsd, tree))
Option(cls.tree).orElse(tryToLoad).map(tree => (clsd, tree))

case _ =>
ctx.error(s"class not found: $className")
Expand Down
5 changes: 1 addition & 4 deletions compiler/src/dotty/tools/dotc/fromtasty/TASTYCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class TASTYCompiler extends Compiler {
case List(_: Pickler) => false
case _ => true
}.tail

List(new ReadTastyTreesFromClasses) ::
List(new Pickler) ::
backendPhases
List(new ReadTastyTreesFromClasses) :: backendPhases
}

override def newRun(implicit ctx: Context): Run = {
Expand Down