Skip to content

Commit

Permalink
elide source file names
Browse files Browse the repository at this point in the history
  • Loading branch information
bishabosha committed Mar 13, 2024
1 parent 8920162 commit cfd9324
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 22 deletions.
75 changes: 56 additions & 19 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import dotty.tools.tasty.TastyFormat.{ASTsSection, PositionsSection, CommentsSec
import java.nio.file.{Files, Paths}
import dotty.tools.io.{JarArchive, Path}
import dotty.tools.tasty.TastyFormat.header
import scala.collection.immutable.BitSet

import scala.compiletime.uninitialized
import dotty.tools.tasty.TastyBuffer.Addr
import dotty.tools.dotc.core.Names.TermName

object TastyPrinter:

Expand Down Expand Up @@ -82,10 +84,6 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
private val unpickler: TastyPrinterUnpickler = new TastyPrinterUnpickler
import unpickler.{nameAtRef, unpickle}

private def nameToString(name: Name): String = name.debugString

private def nameRefToString(ref: NameRef): String = nameToString(nameAtRef(ref))

private def printHeader(sb: StringBuilder): Unit =
val header = unpickler.header
sb.append("Header:\n")
Expand All @@ -101,27 +99,34 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
end if
sb.append("\n")

private def printNames(sb: StringBuilder): Unit =
private def printNames(sb: StringBuilder)(using refs: NameRefs): Unit =
sb.append(s"Names (${unpickler.namesEnd.index - unpickler.namesStart.index} bytes, starting from ${unpickler.namesStart.index}):\n")
for ((name, idx) <- nameAtRef.contents.zipWithIndex) {
val index = nameStr("%6d".format(idx))
sb.append(index).append(": ").append(nameToString(name)).append("\n")
sb.append(index).append(": ").append(refs.nameRefToString(NameRef(idx))).append("\n")
}

def showContents(): String = {
val sb: StringBuilder = new StringBuilder
given NameRefs = unpickle0(new SourceFileUnpickler)(using NameRefs.empty).getOrElse(NameRefs.empty)
printHeader(sb)
printNames(sb)
unpickle(new TreeSectionUnpickler(sb))
unpickle(new PositionSectionUnpickler(sb))
unpickle(new CommentSectionUnpickler(sb))
unpickle(new AttributesSectionUnpickler(sb))
unpickle0(new TreeSectionUnpickler(sb))
unpickle0(new PositionSectionUnpickler(sb))
unpickle0(new CommentSectionUnpickler(sb))
unpickle0(new AttributesSectionUnpickler(sb))
sb.result
}

class TreeSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](ASTsSection) {
def unpickle0[R](sec: PrinterSectionUnpickler[R])(using NameRefs): Option[R] =
unpickle(new SectionUnpickler[R](sec.name) {
def unpickle(reader: TastyReader, nameAtRef: NameTable): R =
sec.unpickle0(reader.subReader(reader.startAddr, reader.endAddr)) // fork so we can visit multiple times
})

class TreeSectionUnpickler(sb: StringBuilder) extends PrinterSectionUnpickler[Unit](ASTsSection) {
import dotty.tools.tasty.TastyFormat.*
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
def unpickle0(reader: TastyReader)(using refs: NameRefs): Unit = {
import reader.*
var indent = 0
def newLine() = {
Expand All @@ -131,7 +136,7 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
def printNat() = sb.append(treeStr(" " + readNat()))
def printName() = {
val idx = readNat()
sb.append(nameStr(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]"))
sb.append(nameStr(" " + idx + " [" + refs.nameRefToString(NameRef(idx)) + "]"))
}
def printTree(): Unit = {
newLine()
Expand Down Expand Up @@ -190,8 +195,8 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
}
}

class PositionSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](PositionsSection) {
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
class PositionSectionUnpickler(sb: StringBuilder) extends PrinterSectionUnpickler[Unit](PositionsSection) {
def unpickle0(reader: TastyReader)(using tastyName: NameRefs): Unit = {
import reader.*
val posUnpickler = new PositionUnpickler(reader, tastyName)
sb.append(s"\n\nPositions (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
Expand Down Expand Up @@ -222,8 +227,8 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
}
}

class CommentSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](CommentsSection) {
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
class CommentSectionUnpickler(sb: StringBuilder) extends PrinterSectionUnpickler[Unit](CommentsSection) {
def unpickle0(reader: TastyReader)(using NameRefs): Unit = {
import reader.*
val comments = new CommentUnpickler(reader).comments
if !comments.isEmpty then
Expand All @@ -236,9 +241,9 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
}
}

class AttributesSectionUnpickler(sb: StringBuilder) extends SectionUnpickler[Unit](AttributesSection) {
class AttributesSectionUnpickler(sb: StringBuilder) extends PrinterSectionUnpickler[Unit](AttributesSection) {
import dotty.tools.tasty.TastyFormat.*
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
def unpickle0(reader: TastyReader)(using nameAtRef: NameRefs): Unit = {
import reader.*
sb.append(s"\n\nAttributes (${reader.endAddr.index - reader.startAddr.index} bytes, starting from $base):\n")
while !isAtEnd do
Expand All @@ -256,6 +261,38 @@ class TastyPrinter(bytes: Array[Byte], val testPickler: Boolean) {
}
}

class NameRefs(sourceFileRefs: Set[NameRef]) extends (NameRef => TermName):
private val isSourceFile = sourceFileRefs.map(_.index).to(BitSet)

def nameRefToString(ref: NameRef): String = this(ref).debugString

def apply(ref: NameRef): TermName =
if isSourceFile(ref.index) then
dotty.tools.dotc.core.Names.termName("<elided source file name>")
else
nameAtRef(ref)

object NameRefs:
val empty = NameRefs(Set.empty)


class SourceFileUnpickler extends PrinterSectionUnpickler[NameRefs](PositionsSection) {
def unpickle0(reader: TastyReader)(using nameAtRef: NameRefs): NameRefs = {
if !testPickler then return NameRefs.empty
val buf = Set.newBuilder[NameRef]
val posUnpickler = new PositionUnpickler(reader, nameAtRef)
val sources = posUnpickler.sourceNameRefs
for ((_, nameRef) <- sources.iterator) {
buf += nameRef
}
NameRefs(buf.result)
}
}

abstract class PrinterSectionUnpickler[T](val name: String) {
def unpickle0(reader: TastyReader)(using refs: NameRefs): T
}

protected def nameStr(str: String): String = str
protected def treeStr(str: String): String = str
protected def lengthStr(str: String): String = str
Expand Down
6 changes: 3 additions & 3 deletions tests/pos/i19806/J.tastycheck
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Names (217 bytes, starting from 80):
20: T
21: Nothing
22: Positions
23: tests/pos/i19806/J_SCALA_ONLY.java
23: <elided source file name>
24: Comments
25: Attributes

Expand Down Expand Up @@ -152,10 +152,10 @@ Positions (145 bytes, starting from 448):
140: 46 .. 46

source paths:
0: 23 [tests/pos/i19806/J_SCALA_ONLY.java]
0: 23 [<elided source file name>]


Attributes (4 bytes, starting from 597):
JAVAattr
OUTLINEattr
SOURCEFILEattr 23 [tests/pos/i19806/J_SCALA_ONLY.java]
SOURCEFILEattr 23 [<elided source file name>]

0 comments on commit cfd9324

Please sign in to comment.