Skip to content

Join logic for unpickling top level and term tasty #4546

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 5 commits into from
Jul 18, 2018

Conversation

nicolasstucki
Copy link
Contributor

No description provided.

@nicolasstucki nicolasstucki requested a review from smarter May 17, 2018 21:27
@nicolasstucki
Copy link
Contributor Author

Improvement proposed in #4538

val rdr = new TreeReader(reader).fork
ownerTree = new OwnerTree(NoAddr, 0, rdr.fork, reader.endAddr)
rdr.readTerm()
if (isTopLevel)
Copy link
Member

Choose a reason for hiding this comment

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

Can't we infer whether this is top level or not based on the tag of the first tree node?

@@ -110,7 +110,8 @@ object PickledQuotes {
/** Unpickle TASTY bytes into it's tree */
private def unpickle(bytes: Array[Byte], splices: Seq[Any])(implicit ctx: Context): Tree = {
val unpickler = new TastyUnpickler(bytes, splices)
val tree = unpickler.unpickleExpr()
unpickler.enter(Set(ctx.owner))
Copy link
Member

Choose a reason for hiding this comment

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

Is entering ctx.owner actually useful here? Otherwise you could just do Set.empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will have to try.

}

def isTopLevel(implicit ctx: Context): Boolean =
nextByte == IMPORT || nextByte == PACKAGE

def readTopLevel()(implicit ctx: Context): List[Tree] = {
@tailrec def read(acc: ListBuffer[Tree]): List[Tree] = nextByte match {
case IMPORT | PACKAGE =>
Copy link
Member

Choose a reason for hiding this comment

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

Could use isTopLevel here. Also I wonder if we couldn't simplify that to return a single Tree instead of a List[Tree] if we never pickle a list of trees.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We may have problems tasty files that have top-level imports

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we even export those?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I put a top level import I get

import scala.collection.mutable.TreeMap
...
     0: PACKAGE(80)
     2:   TERMREFpkg 1 [<empty>]
     4:   IMPORT(8)
     6:     SELECT 2 [mutable]
     8:       SELECT 3 [collection]
    10:         TERMREFpkg 4 [scala]
    12:     IMPORTED 5 [TreeMap]
    14:   PACKAGE(66)
    16:     TERMREFpkg 6 [foo]
    ...

same with

package bar
import scala.collection.mutable.TreeMap
     0: PACKAGE(80)
     2:   TERMREFpkg 1 [bar]
     4:   IMPORT(8)
     6:     SELECT 2 [mutable]
     8:       SELECT 3 [collection]
    10:         TERMREFpkg 4 [scala]
    12:     IMPORTED 5 [TreeMap]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we may go further and do

def isTopLevel(implicit ctx: Context): Boolean = 
  nextByte == PACKAGE

@nicolasstucki
Copy link
Contributor Author

Rebased

@@ -15,10 +15,13 @@ object TastyUnpickler {
* @param bytes the bytearray containing the Tasty file from which we unpickle
* @param splices splices that will fill the holes in the quote
*/
class TastyUnpickler(bytes: Array[Byte], splices: Seq[Any]) extends DottyUnpickler(bytes) {
class TastyUnpickler(bytes: Array[Byte], splices: Seq[Any], isTypeTree: Boolean) extends DottyUnpickler(bytes) {
Copy link
Member

Choose a reason for hiding this comment

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

Woudln't it make more sense to call this class QuoteUnpickler ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I will change it.

@@ -1273,6 +1273,13 @@ class TreeUnpickler(reader: TastyReader,

object TreeUnpickler {

sealed trait UnpickleMode
Copy link
Member

Choose a reason for hiding this comment

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

Needs documentation for this trait and all its children.

@@ -40,14 +40,13 @@ class DottyUnpickler(bytes: Array[Byte]) extends ClassfileParser.Embedded with t
private val posUnpicklerOpt = unpickler.unpickle(new PositionsSectionUnpickler)
private val treeUnpickler = unpickler.unpickle(treeSectionUnpickler(posUnpicklerOpt)).get

protected def mode: TreeUnpickler.UnpickleMode = TreeUnpickler.UnpickleMode.TopLevel
Copy link
Member

Choose a reason for hiding this comment

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

This should be a val I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it should

import DottyUnpickler._
import TastyUnpickler._

override protected def mode: TreeUnpickler.UnpickleMode =
Copy link
Member

Choose a reason for hiding this comment

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

protected override, like three lines below :)

@nicolasstucki
Copy link
Contributor Author

Rebased

@nicolasstucki
Copy link
Contributor Author

@smarter don't forget to review this one.

@@ -46,14 +46,13 @@ class DottyUnpickler(bytes: Array[Byte]) extends ClassfileParser.Embedded with t
private val commentUnpicklerOpt = unpickler.unpickle(new CommentsSectionUnpickler)
private val treeUnpickler = unpickler.unpickle(treeSectionUnpickler(posUnpicklerOpt, commentUnpicklerOpt)).get

protected val mode: TreeUnpickler.UnpickleMode = TreeUnpickler.UnpickleMode.TopLevel
Copy link
Member

Choose a reason for hiding this comment

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

Maybe instead of a field this could be a class parameter: class DottyUnpickler(bytes: Array[Byte], mode: UnpickleMode = UnpickleMode.TopLevel)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done.

@@ -37,7 +38,7 @@ object DottyUnpickler {
/** A class for unpickling Tasty trees and symbols.
* @param bytes the bytearray containing the Tasty file from which we unpickle
*/
class DottyUnpickler(bytes: Array[Byte]) extends ClassfileParser.Embedded with tpd.TreeProvider {
class DottyUnpickler(bytes: Array[Byte], mode: UnpickleMode = UnpickleMode.TopLevel) extends ClassfileParser.Embedded with tpd.TreeProvider {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you update the class documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

@nicolasstucki nicolasstucki merged commit bfbd1af into scala:master Jul 18, 2018
@allanrenucci allanrenucci deleted the join-unpicklings branch July 18, 2018 11:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants