Skip to content

Commit 014ad9b

Browse files
committed
Maintain source files in pickled info
So far: Only one source file is recorded. Should evaluate whether more are needed. Will programs composed from several source files be pickled? They will certainly be generated after inlining, but maybe all that happens after pickling?
1 parent 512689c commit 014ad9b

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

src/dotty/tools/dotc/CompilationUnit.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class CompilationUnit(val source: SourceFile) {
2525
*/
2626
var picklers: Map[ClassSymbol, TastyPickler] = Map()
2727

28-
/**
28+
/** TODO: I'd prefer we do not put this in CompilationUnit
2929
* Addresses in TASTY file of trees, stored by pickling.
3030
* Note that trees are checked for reference equality,
31-
* so one can reliably use this function only dirrectly after `pickler`
31+
* so one can reliably use this function only directly after `pickler`
3232
*/
3333
var addrOfTree: tpd.Tree => Option[Addr] = (_ => None)
3434

35-
/**
35+
/** TODO: I'd prefer we do not put this in CompilationUnit
3636
* Addresses in TASTY file of symbols, stored by pickling.
3737
* Note that trees are checked for reference equality,
3838
* so one can reliably use this function only dirrectly after `pickler`

src/dotty/tools/dotc/FromTasty.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,9 @@ object FromTasty extends Driver {
6666
case info: ClassfileLoader =>
6767
info.load(clsd) match {
6868
case Some(unpickler: DottyUnpickler) =>
69-
val (List(unpickled), sources) = unpickler.body(readPositions = false)
70-
val unit1 = sources.get(unpickled) match {
71-
case Some(source) => new CompilationUnit(source)
72-
case _ => unit
73-
}
69+
val (List(unpickled), source) = unpickler.body(readPositions = false)
70+
val unit1 = new CompilationUnit(source)
7471
unit1.tpdTree = unpickled
75-
unit1.embeddedSources = sources - unpickled
7672
unit1
7773
case _ =>
7874
cannotUnpickle(s"it does not have a TASTY attribute")

src/dotty/tools/dotc/core/pickling/DottyUnpickler.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Contexts._, SymDenotations._
77
import dotty.tools.dotc.ast.tpd
88
import TastyUnpickler._, TastyBuffer._
99
import util.Positions._
10+
import util.{SourceFile, NoSource}
1011
import PositionUnpickler._
1112

1213
object DottyUnpickler {
@@ -30,14 +31,20 @@ class DottyUnpickler(bytes: Array[Byte]) extends ClassfileParser.Embedded {
3031
def enter(roots: Set[SymDenotation])(implicit ctx: Context): Unit =
3132
treeUnpickler.enterTopLevel(roots)
3233

33-
/** The unpickled trees
34+
/** The unpickled trees, and the source file they come from
3435
* @param readPositions if true, trees get decorated with position information.
3536
*/
36-
def body(readPositions: Boolean = false)(implicit ctx: Context): List[Tree] = {
37+
def body(readPositions: Boolean = false)(implicit ctx: Context): (List[Tree], SourceFile) = {
38+
val source = unpickler.unpickle(new SourceFileUnpickler).getOrElse(NoSource)
3739
if (readPositions)
38-
for ((totalRange, positions) <- unpickler.unpickle(new PositionsSectionUnpickler()))
40+
for ((totalRange, positions) <- unpickler.unpickle(new PositionsSectionUnpickler))
3941
treeUnpickler.usePositions(totalRange, positions)
40-
treeUnpickler.unpickle()
42+
(treeUnpickler.unpickle(), source)
43+
}
44+
45+
private class SourceFileUnpickler extends SectionUnpickler[SourceFile]("Sourcefile") {
46+
def unpickle(reader: TastyReader, tastyName: TastyName.Table) =
47+
new SourceFile(tastyName(reader.readNameRef()).toString, Seq())
4148
}
4249

4350
private class TreeSectionUnpickler extends SectionUnpickler[TreeUnpickler]("ASTs") {

src/dotty/tools/dotc/core/pickling/PickleFormat.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ Note: Tree tags are grouped into 5 categories that determine what follows, and t
185185
Category 4 (tags 112-127): tag Nat AST
186186
Category 5 (tags 128-255): tag Length <payload>
187187
188+
Standard Section: "Sourcefile" sourcefile_NameRef
189+
188190
Standard Section: "Positions" sourceLength_Nat Assoc*
189191
190192
Assoc = addr_Delta offset_Delta offset_Delta?

src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Periods._
1111
import Phases._
1212
import Symbols._
1313
import Flags.Module
14+
import util.SourceFile
1415
import collection.mutable
1516

1617
/** This phase pickles trees */
@@ -47,6 +48,8 @@ class Pickler extends Phase {
4748
treePkl.pickle(tree :: Nil)
4849
unit.addrOfTree = treePkl.buf.addrOfTree
4950
unit.addrOfSym = treePkl.addrOfSym
51+
if (unit.source.exists)
52+
pickleSourcefile(pickler, unit.source)
5053
if (tree.pos.exists)
5154
new PositionPickler(pickler, treePkl.buf.addrOfTree).picklePositions(tree :: Nil, tree.pos)
5255

@@ -62,6 +65,12 @@ class Pickler extends Phase {
6265
}
6366
}
6467

68+
private def pickleSourcefile(pickler: TastyPickler, source: SourceFile): Unit = {
69+
val buf = new TastyBuffer(10)
70+
pickler.newSection("Sourcefile", buf)
71+
buf.writeNat(pickler.nameBuffer.nameIndex(source.file.path).index)
72+
}
73+
6574
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = {
6675
val result = super.runOn(units)
6776
if (ctx.settings.YtestPickler.value)
@@ -80,16 +89,16 @@ class Pickler extends Phase {
8089
}
8190
pickling.println("************* entered toplevel ***********")
8291
for ((cls, unpickler) <- unpicklers) {
83-
val unpickled = unpickler.body(readPositions = false)
84-
testSame(i"$unpickled%\n%", beforePickling(cls), cls)
92+
val (unpickled, source) = unpickler.body(readPositions = false)
93+
testSame(i"$unpickled%\n%", beforePickling(cls), cls, source)
8594
}
8695
}
8796

88-
private def testSame(unpickled: String, previous: String, cls: ClassSymbol)(implicit ctx: Context) =
97+
private def testSame(unpickled: String, previous: String, cls: ClassSymbol, source: SourceFile)(implicit ctx: Context) =
8998
if (previous != unpickled) {
9099
output("before-pickling.txt", previous)
91100
output("after-pickling.txt", unpickled)
92-
ctx.error(s"""pickling difference for ${cls.fullName}, for details:
101+
ctx.error(s"""pickling difference for ${cls.fullName} in $source, for details:
93102
|
94103
| diff before-pickling.txt after-pickling.txt""".stripMargin)
95104
}

0 commit comments

Comments
 (0)