Skip to content

Commit 65cb973

Browse files
committed
Create DecompilerPrinter and ReplPrinter
1 parent 8ae50fe commit 65cb973

File tree

18 files changed

+309
-221
lines changed

18 files changed

+309
-221
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import java.io.{OutputStream, PrintStream}
66
import dotty.tools.dotc.core.Contexts._
77
import dotty.tools.dotc.core.Phases.Phase
88
import dotty.tools.dotc.core.tasty.TastyPrinter
9+
import dotty.tools.dotc.printing.DecompilerPrinter
910
import dotty.tools.io.{File, Path}
1011

1112
/** Phase that prints the trees in all loaded compilation units.
@@ -36,15 +37,17 @@ class DecompilationPrinter extends Phase {
3637
private def printToOutput(out: PrintStream)(implicit ctx: Context): Unit = {
3738
val unit = ctx.compilationUnit
3839
val pageWidth = ctx.settings.pageWidth.value
39-
40+
val printLines = ctx.settings.printLines.value
4041
val doubleLine = "=" * pageWidth
4142
val line = "-" * pageWidth
4243

4344
out.println(doubleLine)
4445
out.println(unit.source)
4546
out.println(line)
4647

47-
out.println(unit.tpdTree.show)
48+
val printer = new DecompilerPrinter(ctx)
49+
50+
out.println(printer.toText(unit.tpdTree).mkString(pageWidth, printLines))
4851
out.println(line)
4952

5053
if (ctx.settings.printTasty.value) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dotty.tools.dotc.printing
2+
3+
import dotty.tools.dotc.ast.Trees.{Closure, DefDef, Untyped, ValDef}
4+
import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef}
5+
import dotty.tools.dotc.ast.{Trees, untpd}
6+
import dotty.tools.dotc.printing.Texts._
7+
import dotty.tools.dotc.core.Contexts._
8+
import dotty.tools.dotc.core.Flags._
9+
import dotty.tools.dotc.core.Symbols._
10+
11+
import scala.language.implicitConversions
12+
13+
class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
14+
15+
override protected def modText(mods: untpd.Modifiers, kw: String): Text = { // DD
16+
val suppressKw = if (enclDefIsClass) mods is ParamAndLocal else mods is Param
17+
var flagMask =
18+
if (ctx.settings.YdebugFlags.value) AnyFlags
19+
else if (suppressKw) PrintableFlags &~ Private
20+
else PrintableFlags
21+
if (homogenizedView && mods.flags.isTypeFlags) flagMask &~= Implicit // drop implicit from classes
22+
val flags = mods.flags & flagMask
23+
val flagsText = if (flags.isEmpty) "" else keywordStr((mods.flags & flagMask).toString)
24+
val annotations = mods.annotations.filter(_.tpe != defn.SourceFileAnnotType)
25+
Text(annotations.map(annotText), " ") ~~ flagsText ~~ (Str(kw) provided !suppressKw)
26+
}
27+
28+
override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = {
29+
super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
30+
}
31+
32+
override protected def packageDefText(tree: PackageDef): Text = {
33+
val stats = tree.stats.filter {
34+
case vdef: ValDef[_] => !vdef.symbol.is(Module)
35+
case _ => true
36+
}
37+
val statsText = stats match {
38+
case (pdef: PackageDef) :: Nil => toText(pdef)
39+
case _ => toTextGlobal(stats, "\n")
40+
}
41+
val bodyText =
42+
if (currentPrecedence == TopLevelPrec) "\n" ~ statsText else " {" ~ statsText ~ "}"
43+
keywordStr("package ") ~ toTextPackageId(tree.pid) ~ bodyText
44+
}
45+
46+
override protected def templateText(tree: TypeDef, impl: Template): Text = {
47+
val decl =
48+
if (!tree.mods.is(Module)) modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
49+
else modText(tree.mods &~ (Final | Module), keywordStr("object"))
50+
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
51+
}
52+
53+
override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
54+
import untpd.{modsDeco => _, _}
55+
dclTextOr(tree) {
56+
val printLambda = tree.symbol.isAnonymousFunction
57+
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
58+
withEnclosingDef(tree) {
59+
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt).provided(!printLambda) ~
60+
optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _)
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)