Skip to content

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?

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 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

* @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.


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.

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.

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.

* @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