Skip to content

Commit 5aa59ac

Browse files
committed
Simplify handling of sourcefiles in Tasty info
Instead of separate source file sections, pickle SourceFile as an annotation of all toplevel classes. We represent it like this anyway when reading back Tasty-defined classes.
1 parent f9d27c9 commit 5aa59ac

File tree

5 files changed

+17
-33
lines changed

5 files changed

+17
-33
lines changed

src/dotty/tools/dotc/FromTasty.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ object FromTasty extends Driver {
8686
case info: ClassfileLoader =>
8787
info.load(clsd) match {
8888
case Some(unpickler: DottyUnpickler) =>
89-
val (List(unpickled), source) = unpickler.body(readPositions = true)
90-
val unit1 = new CompilationUnit(source)
89+
val List(unpickled) = unpickler.body(readPositions = true)
90+
val unit1 = new CompilationUnit(new SourceFile(clsd.symbol.sourceFile, Seq()))
9191
unit1.tpdTree = unpickled
9292
unit1.unpicklers += (clsd.classSymbol -> unpickler.unpickler)
9393
force.traverse(unit1.tpdTree)

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package tasty
66
import Contexts._, SymDenotations._, Symbols._
77
import dotty.tools.dotc.ast.tpd
88
import TastyUnpickler._, TastyBuffer._
9-
import dotty.tools.dotc.core.tasty.DottyUnpickler.{SourceFileUnpickler, TreeSectionUnpickler, PositionsSectionUnpickler}
109
import util.Positions._
1110
import util.{SourceFile, NoSource}
1211
import PositionUnpickler._
@@ -18,13 +17,6 @@ object DottyUnpickler {
1817
/** Exception thrown if classfile is corrupted */
1918
class BadSignature(msg: String) extends RuntimeException(msg)
2019

21-
class SourceFileUnpickler extends SectionUnpickler[SourceFile]("Sourcefile") {
22-
def unpickle(reader: TastyReader, tastyName: TastyName.Table) = {
23-
val TastyName.Simple(sourceName) = tastyName(reader.readNameRef())
24-
new SourceFile(sourceName.toString, Seq())
25-
}
26-
}
27-
2820
class TreeSectionUnpickler extends SectionUnpickler[TreeUnpickler]("ASTs") {
2921
def unpickle(reader: TastyReader, tastyName: TastyName.Table) =
3022
new TreeUnpickler(reader, tastyName)
@@ -41,27 +33,24 @@ object DottyUnpickler {
4133
*/
4234
class DottyUnpickler(bytes: Array[Byte]) extends ClassfileParser.Embedded {
4335
import tpd._
36+
import DottyUnpickler._
4437

4538
val unpickler = new TastyUnpickler(bytes)
4639
private val treeUnpickler = unpickler.unpickle(new TreeSectionUnpickler).get
47-
private val source = unpickler.unpickle(new SourceFileUnpickler).getOrElse(NoSource)
4840

4941
/** Enter all toplevel classes and objects into their scopes
5042
* @param roots a set of SymDenotations that should be overwritten by unpickling
5143
*/
52-
def enter(roots: Set[SymDenotation])(implicit ctx: Context): Unit = {
44+
def enter(roots: Set[SymDenotation])(implicit ctx: Context): Unit =
5345
treeUnpickler.enterTopLevel(roots)
54-
if (source.exists)
55-
for (root <- roots) root.addAnnotation(Annotation.makeSourceFile(source.path))
56-
}
5746

5847
/** The unpickled trees, and the source file they come from
5948
* @param readPositions if true, trees get decorated with position information.
6049
*/
61-
def body(readPositions: Boolean = false)(implicit ctx: Context): (List[Tree], SourceFile) = {
50+
def body(readPositions: Boolean = false)(implicit ctx: Context): List[Tree] = {
6251
if (readPositions)
6352
for ((totalRange, positions) <- unpickler.unpickle(new PositionsSectionUnpickler))
6453
treeUnpickler.usePositions(totalRange, positions)
65-
(treeUnpickler.unpickle(), source)
54+
treeUnpickler.unpickle()
6655
}
6756
}

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

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

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

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

1716
/** This phase pickles trees */
@@ -48,8 +47,6 @@ class Pickler extends Phase {
4847
treePkl.pickle(tree :: Nil)
4948
pickler.addrOfTree = treePkl.buf.addrOfTree
5049
pickler.addrOfSym = treePkl.addrOfSym
51-
if (unit.source.exists)
52-
pickleSourcefile(pickler, unit.source)
5350
if (tree.pos.exists)
5451
new PositionPickler(pickler, treePkl.buf.addrOfTree).picklePositions(tree :: Nil, tree.pos)
5552

@@ -65,12 +62,6 @@ class Pickler extends Phase {
6562
}
6663
}
6764

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-
7465
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] = {
7566
val result = super.runOn(units)
7667
if (ctx.settings.YtestPickler.value)
@@ -89,16 +80,16 @@ class Pickler extends Phase {
8980
}
9081
pickling.println("************* entered toplevel ***********")
9182
for ((cls, unpickler) <- unpicklers) {
92-
val (unpickled, source) = unpickler.body(readPositions = true)
93-
testSame(i"$unpickled%\n%", beforePickling(cls), cls, source)
83+
val unpickled = unpickler.body(readPositions = true)
84+
testSame(i"$unpickled%\n%", beforePickling(cls), cls)
9485
}
9586
}
9687

97-
private def testSame(unpickled: String, previous: String, cls: ClassSymbol, source: SourceFile)(implicit ctx: Context) =
88+
private def testSame(unpickled: String, previous: String, cls: ClassSymbol)(implicit ctx: Context) =
9889
if (previous != unpickled) {
9990
output("before-pickling.txt", previous)
10091
output("after-pickling.txt", unpickled)
101-
ctx.error(s"""pickling difference for ${cls.fullName} in $source, for details:
92+
ctx.error(s"""pickling difference for ${cls.fullName} in ${cls.sourceFile}, for details:
10293
|
10394
| diff before-pickling.txt after-pickling.txt""".stripMargin)
10495
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import Symbols._, TypeUtils._
3636
*
3737
* (8) Replaces self references by name with `this`
3838
*
39+
* (9) Adds SourceFile annotations to all top-level classes and objects
40+
*
3941
* The reason for making this a macro transform is that some functions (in particular
4042
* super and protected accessors and instantiation checks) are naturally top-down and
4143
* don't lend themselves to the bottom-up approach of a mini phase. The other two functions
@@ -224,7 +226,11 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisTran
224226
transformMemberDef(tree)
225227
val sym = tree.symbol
226228
val tree1 =
227-
if (sym.isClass) tree
229+
if (sym.isClass) {
230+
if (sym.owner.is(Package) && ctx.compilationUnit.source.exists)
231+
sym.addAnnotation(Annotation.makeSourceFile(ctx.compilationUnit.source.file.path))
232+
tree
233+
}
228234
else {
229235
Checking.typeChecker.traverse(tree.rhs)
230236
cpy.TypeDef(tree)(rhs = TypeTree(tree.symbol.info))

0 commit comments

Comments
 (0)