-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
Improvement proposed in #4538 |
val rdr = new TreeReader(reader).fork | ||
ownerTree = new OwnerTree(NoAddr, 0, rdr.fork, reader.endAddr) | ||
rdr.readTerm() | ||
if (isTopLevel) |
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.
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)) |
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.
Is entering ctx.owner actually useful here? Otherwise you could just do Set.empty
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.
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 => |
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.
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.
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.
We may have problems tasty files that have top-level imports
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.
Do we even export those?
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.
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]
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.
I think we may go further and do
def isTopLevel(implicit ctx: Context): Boolean =
nextByte == PACKAGE
7ccdee0
to
b88ade9
Compare
b88ade9
to
d1a55d9
Compare
Rebased |
d1a55d9
to
fb56b95
Compare
@@ -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) { |
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.
Woudln't it make more sense to call this class QuoteUnpickler
?
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.
Yes. I will change it.
@@ -1273,6 +1273,13 @@ class TreeUnpickler(reader: TastyReader, | |||
|
|||
object TreeUnpickler { | |||
|
|||
sealed trait UnpickleMode |
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.
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 |
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 should be a val I think.
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.
Yes, it should
import DottyUnpickler._ | ||
import TastyUnpickler._ | ||
|
||
override protected def mode: TreeUnpickler.UnpickleMode = |
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.
protected override, like three lines below :)
eb7649a
to
2794be3
Compare
2794be3
to
ad111dd
Compare
Rebased |
@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 |
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.
Maybe instead of a field this could be a class parameter: class DottyUnpickler(bytes: Array[Byte], mode: UnpickleMode = UnpickleMode.TopLevel)
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.
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 { |
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.
Can you update the class documentation?
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.
Added
No description provided.