Skip to content

Commit 401e1d9

Browse files
committed
Tweaks to Tasty pickling and unpickling
- some fixes to format docs - some refactorings for better legibility
1 parent c73a21b commit 401e1d9

File tree

3 files changed

+68
-62
lines changed

3 files changed

+68
-62
lines changed

compiler/src/dotty/tools/dotc/core/tasty/TastyFormat.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ Standard-Section: "ASTs" TopLevelStat*
6565
6666
// Imports are for scala.meta, they are not used in the backend
6767
68-
TypeParam = TYPEPARAM Length NameRef Type Modifier*
68+
TypeParam = TYPEPARAM Length NameRef type_Term Modifier*
6969
Params = PARAMS Length Param*
70-
Param = PARAM Length NameRef Type rhs_Term? Modifier* // rhs_Term is present in the case of an aliased class parameter
70+
Param = PARAM Length NameRef type_Term rhs_Term? Modifier* // rhs_Term is present in the case of an aliased class parameter
7171
Template = TEMPLATE Length TypeParam* Param* parent_Term* Self? Stat* // Stat* always starts with the primary constructor.
72-
Self = SELFDEF selfName_NameRef selfType_Type
72+
Self = SELFDEF selfName_NameRef selfType_Term
7373
7474
Term = Path
7575
IDENT NameRef Type // used when term ident’s type is not a TermRef

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,7 @@ class TreePickler(pickler: TastyPickler) {
504504
}
505505
case Import(expr, selectors) =>
506506
writeByte(IMPORT)
507-
withLength {
508-
pickleTree(expr)
509-
selectors foreach {
510-
case Thicket((from @ Ident(_)) :: (to @ Ident(_)) :: Nil) =>
511-
pickleSelector(IMPORTED, from)
512-
pickleSelector(RENAMED, to)
513-
case id @ Ident(_) =>
514-
pickleSelector(IMPORTED, id)
515-
}
516-
}
507+
withLength { pickleTree(expr); pickleSelectors(selectors) }
517508
case PackageDef(pid, stats) =>
518509
writeByte(PACKAGE)
519510
withLength { pickleType(pid.tpe); pickleStats(stats) }
@@ -569,6 +560,15 @@ class TreePickler(pickler: TastyPickler) {
569560
}
570561
}
571562

563+
def pickleSelectors(selectors: List[untpd.Tree])(implicit ctx: Context): Unit =
564+
selectors foreach {
565+
case Thicket((from @ Ident(_)) :: (to @ Ident(_)) :: Nil) =>
566+
pickleSelector(IMPORTED, from)
567+
pickleSelector(RENAMED, to)
568+
case id @ Ident(_) =>
569+
pickleSelector(IMPORTED, id)
570+
}
571+
572572
def pickleSelector(tag: Int, id: untpd.Ident)(implicit ctx: Context): Unit = {
573573
registerTreeAddr(id)
574574
writeByte(tag)
@@ -600,15 +600,15 @@ class TreePickler(pickler: TastyPickler) {
600600
if (sym.isTerm) {
601601
if (flags is Implicit) writeByte(IMPLICIT)
602602
if (flags is Erased) writeByte(ERASED)
603-
if ((flags is Lazy) && !(sym is Module)) writeByte(LAZY)
603+
if (flags.is(Lazy, butNot = Module)) writeByte(LAZY)
604604
if (flags is AbsOverride) { writeByte(ABSTRACT); writeByte(OVERRIDE) }
605605
if (flags is Mutable) writeByte(MUTABLE)
606606
if (flags is Accessor) writeByte(FIELDaccessor)
607607
if (flags is CaseAccessor) writeByte(CASEaccessor)
608608
if (flags is DefaultParameterized) writeByte(DEFAULTparameterized)
609609
if (flags is Stable) writeByte(STABLE)
610610
if ((flags is ParamAccessor) && sym.isSetter) writeByte(PARAMsetter)
611-
if ((flags is Label)) writeByte(LABEL)
611+
if (flags is Label) writeByte(LABEL)
612612
} else {
613613
if (flags is Sealed) writeByte(SEALED)
614614
if (flags is Abstract) writeByte(ABSTRACT)

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,39 @@ class TreeUnpickler(reader: TastyReader,
228228
createSymbol()
229229
}
230230

231+
def readConstant(tag: Int)(implicit ctx: Context): Constant = (tag: @switch) match {
232+
case UNITconst =>
233+
Constant(())
234+
case TRUEconst =>
235+
Constant(true)
236+
case FALSEconst =>
237+
Constant(false)
238+
case BYTEconst =>
239+
Constant(readInt().toByte)
240+
case SHORTconst =>
241+
Constant(readInt().toShort)
242+
case CHARconst =>
243+
Constant(readNat().toChar)
244+
case INTconst =>
245+
Constant(readInt())
246+
case LONGconst =>
247+
Constant(readLongInt())
248+
case FLOATconst =>
249+
Constant(java.lang.Float.intBitsToFloat(readInt()))
250+
case DOUBLEconst =>
251+
Constant(java.lang.Double.longBitsToDouble(readLongInt()))
252+
case STRINGconst =>
253+
Constant(readName().toString)
254+
case NULLconst =>
255+
Constant(null)
256+
case CLASSconst =>
257+
Constant(readType())
258+
case ENUMconst =>
259+
Constant(readTermRef().termSymbol)
260+
case SYMBOLconst =>
261+
Constant(scala.Symbol(readName().toString))
262+
}
263+
231264
/** Read a type */
232265
def readType()(implicit ctx: Context): Type = {
233266
val start = currentAddr
@@ -313,10 +346,6 @@ class TreeUnpickler(reader: TastyReader,
313346
readTypeRef() match {
314347
case binder: LambdaType => binder.paramRefs(readNat())
315348
}
316-
case CLASSconst =>
317-
ConstantType(Constant(readType()))
318-
case ENUMconst =>
319-
ConstantType(Constant(readTermRef().termSymbol))
320349
case HOLE =>
321350
readHole(end, isType = true).tpe
322351
}
@@ -356,38 +385,10 @@ class TreeUnpickler(reader: TastyReader,
356385
case SHAREDtype =>
357386
val ref = readAddr()
358387
typeAtAddr.getOrElseUpdate(ref, forkAt(ref).readType())
359-
case UNITconst =>
360-
ConstantType(Constant(()))
361-
case TRUEconst =>
362-
ConstantType(Constant(true))
363-
case FALSEconst =>
364-
ConstantType(Constant(false))
365-
case BYTEconst =>
366-
ConstantType(Constant(readInt().toByte))
367-
case SHORTconst =>
368-
ConstantType(Constant(readInt().toShort))
369-
case CHARconst =>
370-
ConstantType(Constant(readNat().toChar))
371-
case INTconst =>
372-
ConstantType(Constant(readInt()))
373-
case LONGconst =>
374-
ConstantType(Constant(readLongInt()))
375-
case FLOATconst =>
376-
ConstantType(Constant(java.lang.Float.intBitsToFloat(readInt())))
377-
case DOUBLEconst =>
378-
ConstantType(Constant(java.lang.Double.longBitsToDouble(readLongInt())))
379-
case STRINGconst =>
380-
ConstantType(Constant(readName().toString))
381-
case NULLconst =>
382-
ConstantType(Constant(null))
383-
case CLASSconst =>
384-
ConstantType(Constant(readType()))
385-
case ENUMconst =>
386-
ConstantType(Constant(readTermRef().termSymbol))
387-
case SYMBOLconst =>
388-
ConstantType(Constant(scala.Symbol(readName().toString)))
389388
case BYNAMEtype =>
390389
ExprType(readType())
390+
case _ =>
391+
ConstantType(readConstant(tag))
391392
}
392393

393394
if (tag < firstLengthTreeTag) readSimpleType() else readLengthType()
@@ -420,7 +421,7 @@ class TreeUnpickler(reader: TastyReader,
420421

421422
// ------ Reading definitions -----------------------------------------------------
422423

423-
private def noRhs(end: Addr): Boolean =
424+
private def nothingButMods(end: Addr): Boolean =
424425
currentAddr == end || isModifierTag(nextByte)
425426

426427
private def localContext(owner: Symbol)(implicit ctx: Context) =
@@ -510,7 +511,7 @@ class TreeUnpickler(reader: TastyReader,
510511
val templateStart = currentAddr
511512
skipTree() // tpt
512513
val rhsStart = currentAddr
513-
val rhsIsEmpty = noRhs(end)
514+
val rhsIsEmpty = nothingButMods(end)
514515
if (!rhsIsEmpty) skipTree()
515516
val (givenFlags, annots, privateWithin) = readModifiers(end)
516517
pickling.println(i"creating symbol $name at $start with flags $givenFlags")
@@ -562,7 +563,7 @@ class TreeUnpickler(reader: TastyReader,
562563
*/
563564
def readModifiers(end: Addr)(implicit ctx: Context): (FlagSet, List[Annotation], Symbol) = {
564565
var flags: FlagSet = EmptyFlags
565-
var annots = new mutable.ListBuffer[Annotation]
566+
var annots: List[Annotation] = Nil
566567
var privateWithin: Symbol = NoSymbol
567568
while (currentAddr.index != end.index) {
568569
def addFlag(flag: FlagSet) = {
@@ -613,18 +614,23 @@ class TreeUnpickler(reader: TastyReader,
613614
addFlag(Protected)
614615
privateWithin = readType().typeSymbol
615616
case ANNOTATION =>
616-
readByte()
617-
val end = readEnd()
618-
val tp = readType()
619-
val lazyAnnotTree = readLater(end, rdr => ctx => rdr.readTerm()(ctx))
620-
annots += Annotation.deferredSymAndTree(
621-
implicit ctx => tp.typeSymbol,
622-
implicit ctx => lazyAnnotTree.complete)
617+
annots = readAnnot(ctx) :: annots
623618
case tag =>
624619
assert(false, s"illegal modifier tag $tag at $currentAddr, end = $end")
625620
}
626621
}
627-
(flags, annots.toList, privateWithin)
622+
(flags, annots.reverse, privateWithin)
623+
}
624+
625+
private val readAnnot: Context => Annotation = {
626+
implicit ctx =>
627+
readByte()
628+
val end = readEnd()
629+
val tp = readType()
630+
val lazyAnnotTree = readLater(end, rdr => ctx => rdr.readTerm()(ctx))
631+
Annotation.deferredSymAndTree(
632+
implicit ctx => tp.typeSymbol,
633+
implicit ctx => lazyAnnotTree.complete)
628634
}
629635

630636
/** Create symbols for the definitions in the statement sequence between
@@ -721,7 +727,7 @@ class TreeUnpickler(reader: TastyReader,
721727
val localCtx = localContext(sym)
722728

723729
def readRhs(implicit ctx: Context) =
724-
if (noRhs(end)) EmptyTree
730+
if (nothingButMods(end)) EmptyTree
725731
else readLater(end, rdr => ctx => rdr.readTerm()(ctx.retractMode(Mode.InSuperCall)))
726732

727733
def ValDef(tpt: Tree) =
@@ -786,7 +792,7 @@ class TreeUnpickler(reader: TastyReader,
786792
}
787793
case PARAM =>
788794
val tpt = readTpt()(localCtx)
789-
if (noRhs(end)) {
795+
if (nothingButMods(end)) {
790796
sym.info = tpt.tpe
791797
ValDef(tpt)
792798
}

0 commit comments

Comments
 (0)