Skip to content

Commit 227e1c8

Browse files
author
Tobias Bordenca
committed
Make TastyPrinter return a String instead of printing to sys.out
1 parent d56af69 commit 227e1c8

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ object PickledQuotes {
8989
val pickled = pickler.assembleParts()
9090

9191
if (quotePickling ne noPrinter)
92-
new TastyPrinter(pickled).printContents()
92+
println(new TastyPrinter(pickled).printContents())
9393

9494
pickled
9595
}
@@ -98,7 +98,7 @@ object PickledQuotes {
9898
private def unpickle(bytes: Array[Byte], splices: Seq[Any], isType: Boolean)(implicit ctx: Context): Tree = {
9999
if (quotePickling ne noPrinter) {
100100
println(i"**** unpickling quote from TASTY")
101-
new TastyPrinter(bytes).printContents()
101+
println(new TastyPrinter(bytes).printContents())
102102
}
103103

104104
val mode = if (isType) UnpickleMode.TypeTree else UnpickleMode.Term

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

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import printing.Highlighting._
1111

1212
class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) {
1313

14+
private[this] val sb: StringBuilder = new StringBuilder
15+
1416
val unpickler: TastyUnpickler = new TastyUnpickler(bytes)
1517
import unpickler.{nameAtRef, unpickle}
1618

@@ -21,41 +23,56 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) {
2123
def printNames(): Unit =
2224
for ((name, idx) <- nameAtRef.contents.zipWithIndex) {
2325
val index = nameColor("%4d".format(idx))
24-
println(index + ": " + nameToString(name))
26+
sb.append(index + ": " + nameToString(name) + "\n")
2527
}
2628

27-
def printContents(): Unit = {
28-
println("Names:")
29+
def printContents(): String = {
30+
sb.append("Names:\n")
2931
printNames()
30-
println()
31-
println("Trees:")
32-
unpickle(new TreeSectionUnpickler)
33-
unpickle(new PositionSectionUnpickler)
34-
unpickle(new CommentSectionUnpickler)
32+
sb.append("\n")
33+
sb.append("Trees:\n")
34+
unpickle(new TreeSectionUnpickler) match {
35+
case Some(s) => sb.append(s)
36+
case _ => Unit
37+
}
38+
sb.append("\n\n")
39+
unpickle(new PositionSectionUnpickler) match {
40+
case Some(s) => sb.append(s)
41+
case _ => Unit
42+
}
43+
sb.append("\n\n")
44+
unpickle(new CommentSectionUnpickler) match {
45+
case Some(s) => sb.append(s)
46+
case _ => Unit
47+
}
48+
sb.result
3549
}
3650

37-
class TreeSectionUnpickler extends SectionUnpickler[Unit](TreePickler.sectionName) {
51+
class TreeSectionUnpickler extends SectionUnpickler[String](TreePickler.sectionName) {
3852
import TastyFormat._
39-
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
53+
54+
private[this] val sb: StringBuilder = new StringBuilder
55+
56+
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
4057
import reader._
4158
var indent = 0
4259
def newLine() = {
4360
val length = treeColor("%5d".format(index(currentAddr) - index(startAddr)))
44-
print(s"\n $length:" + " " * indent)
61+
sb.append(s"\n $length:" + " " * indent)
4562
}
46-
def printNat() = print(Yellow(" " + readNat()).show)
63+
def printNat() = sb.append(Yellow(" " + readNat()).show)
4764
def printName() = {
4865
val idx = readNat()
49-
print(nameColor(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]"))
66+
sb.append(nameColor(" " + idx + " [" + nameRefToString(NameRef(idx)) + "]"))
5067
}
5168
def printTree(): Unit = {
5269
newLine()
5370
val tag = readByte()
54-
print(" ");print(astTagToString(tag))
71+
sb.append(" ");sb.append(astTagToString(tag))
5572
indent += 2
5673
if (tag >= firstLengthTreeTag) {
5774
val len = readNat()
58-
print(s"(${lengthColor(len.toString)})")
75+
sb.append(s"(${lengthColor(len.toString)})")
5976
val end = currentAddr + len
6077
def printTrees() = until(end)(printTree())
6178
tag match {
@@ -76,7 +93,7 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) {
7693
printTrees()
7794
}
7895
if (currentAddr != end) {
79-
println(s"incomplete read, current = $currentAddr, end = $end")
96+
sb.append(s"incomplete read, current = $currentAddr, end = $end\n")
8097
goto(end)
8198
}
8299
}
@@ -96,42 +113,51 @@ class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) {
96113
}
97114
indent -= 2
98115
}
99-
println(i"start = ${reader.startAddr}, base = $base, current = $currentAddr, end = $endAddr")
100-
println(s"${endAddr.index - startAddr.index} bytes of AST, base = $currentAddr")
116+
sb.append(i"start = ${reader.startAddr}, base = $base, current = $currentAddr, end = $endAddr\n")
117+
sb.append(s"${endAddr.index - startAddr.index} bytes of AST, base = $currentAddr\n")
101118
while (!isAtEnd) {
102119
printTree()
103120
newLine()
104121
}
122+
sb.result
105123
}
106124
}
107125

108-
class PositionSectionUnpickler extends SectionUnpickler[Unit]("Positions") {
109-
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
110-
print(s" ${reader.endAddr.index - reader.currentAddr.index}")
126+
class PositionSectionUnpickler extends SectionUnpickler[String]("Positions") {
127+
128+
private[this] val sb: StringBuilder = new StringBuilder
129+
130+
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
131+
sb.append(s" ${reader.endAddr.index - reader.currentAddr.index}")
111132
val positions = new PositionUnpickler(reader).positions
112-
println(s" position bytes:")
133+
sb.append(s" position bytes:\n")
113134
val sorted = positions.toSeq.sortBy(_._1.index)
114135
for ((addr, pos) <- sorted) {
115-
print(treeColor("%10d".format(addr.index)))
116-
println(s": ${offsetToInt(pos.start)} .. ${pos.end}")
136+
sb.append(treeColor("%10d".format(addr.index)))
137+
sb.append(s": ${offsetToInt(pos.start)} .. ${pos.end}\n")
117138
}
139+
sb.result
118140
}
119141
}
120142

121-
class CommentSectionUnpickler extends SectionUnpickler[Unit]("Comments") {
122-
def unpickle(reader: TastyReader, tastyName: NameTable): Unit = {
123-
print(s" ${reader.endAddr.index - reader.currentAddr.index}")
143+
class CommentSectionUnpickler extends SectionUnpickler[String]("Comments") {
144+
145+
private[this] val sb: StringBuilder = new StringBuilder
146+
147+
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
148+
sb.append(s" ${reader.endAddr.index - reader.currentAddr.index}")
124149
val comments = new CommentUnpickler(reader).comments
125-
println(s" comment bytes:")
150+
sb.append(s" comment bytes:\n")
126151
val sorted = comments.toSeq.sortBy(_._1.index)
127152
for ((addr, cmt) <- sorted) {
128-
print(treeColor("%10d".format(addr.index)))
129-
println(s": ${cmt.raw} (expanded = ${cmt.isExpanded})")
153+
sb.append(treeColor("%10d".format(addr.index)))
154+
sb.append(s": ${cmt.raw} (expanded = ${cmt.isExpanded})\n")
130155
}
156+
sb.result
131157
}
132158
}
133159

134-
private def nameColor(str: String): String = Magenta(str).show
135-
private def treeColor(str: String): String = Yellow(str).show
136-
private def lengthColor(str: String): String = Cyan(str).show
160+
protected def nameColor(str: String): String = Magenta(str).show
161+
protected def treeColor(str: String): String = Yellow(str).show
162+
protected def lengthColor(str: String): String = Cyan(str).show
137163
}

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class DecompilationPrinter extends Phase {
3939
private def printToOutput(out: PrintStream)(implicit ctx: Context): Unit = {
4040
val unit = ctx.compilationUnit
4141
if (ctx.settings.printTasty.value) {
42-
new TastyPrinter(unit.pickled.head._2).printContents()
42+
println(new TastyPrinter(unit.pickled.head._2).printContents())
4343
} else {
4444
val unitFile = unit.source.toString.replace("\\", "/").replace(".class", ".tasty")
4545
out.println(s"/** Decompiled from $unitFile */")

0 commit comments

Comments
 (0)