Skip to content

Create DecompilerPrinter and ReplPrinter #3691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ compiler/test/debug/Gen.jar
compiler/before-pickling.txt
compiler/after-pickling.txt
*.dotty-ide-version

*.decompiled.out
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package dotty.tools.dotc
package decompiler

import java.io.{OutputStream, PrintStream}

import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Phases.Phase
import dotty.tools.dotc.core.tasty.TastyPrinter
import dotty.tools.dotc.printing.DecompilerPrinter
import dotty.tools.io.{File, Path}

/** Phase that prints the trees in all loaded compilation units.
*
Expand All @@ -14,23 +18,41 @@ class DecompilationPrinter extends Phase {
override def phaseName: String = "decompilationPrinter"

override def run(implicit ctx: Context): Unit = {
val unit = ctx.compilationUnit
val outputDir = ctx.settings.outputDir.value
if (outputDir == ".") printToOutput(System.out)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here we assume print to current folder means printing to standard console?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, as it is the default it is practical.

else {
var os: OutputStream = null
var ps: PrintStream = null
try {
os = File(outputDir + ".decompiled").outputStream()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dir + .decompiled: filename missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I use the outputDir as a file. Just like we do with JARs, where we write -d Foo.jar. We can change the format later if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mode is mostly intended for testing

ps = new PrintStream(os)
printToOutput(ps)
} finally {
if (os ne null) os.close()
if (ps ne null) ps.close()
}
}
}

private def printToOutput(out: PrintStream)(implicit ctx: Context): Unit = {
val unit = ctx.compilationUnit
val pageWidth = ctx.settings.pageWidth.value

val printLines = ctx.settings.printLines.value
val doubleLine = "=" * pageWidth
val line = "-" * pageWidth

println(doubleLine)
println(unit.source)
println(line)
out.println(doubleLine)
out.println(unit.source)
out.println(line)

val printer = new DecompilerPrinter(ctx)

println(unit.tpdTree.show)
println(line)
out.println(printer.toText(unit.tpdTree).mkString(pageWidth, printLines))
out.println(line)

if (ctx.settings.printTasty.value) {
new TastyPrinter(unit.pickled.head._2).printContents()
println(line)
out.println(line)
}
}
}
54 changes: 54 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dotty.tools.dotc.printing

import dotty.tools.dotc.ast.Trees.{Closure, DefDef, Untyped, ValDef}
import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef}
import dotty.tools.dotc.ast.{Trees, untpd}
import dotty.tools.dotc.printing.Texts._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Symbols._

import scala.language.implicitConversions

class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {

override protected def filterModTextAnnots(annots: List[untpd.Tree]): List[untpd.Tree] =
annots.filter(_.tpe != defn.SourceFileAnnotType)

override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = {
super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
}

override protected def packageDefText(tree: PackageDef): Text = {
val stats = tree.stats.filter {
case vdef: ValDef[_] => !vdef.symbol.is(Module)
case _ => true
}
val statsText = stats match {
case (pdef: PackageDef) :: Nil => toText(pdef)
case _ => toTextGlobal(stats, "\n")
}
val bodyText =
if (currentPrecedence == TopLevelPrec) "\n" ~ statsText else " {" ~ statsText ~ "}"
keywordStr("package ") ~ toTextPackageId(tree.pid) ~ bodyText
}

override protected def templateText(tree: TypeDef, impl: Template): Text = {
val decl =
if (!tree.mods.is(Module)) modText(tree.mods, keywordStr(if ((tree).mods is Trait) "trait" else "class"))
else modText(tree.mods &~ (Final | Module), keywordStr("object"))
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
}

override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
import untpd.{modsDeco => _, _}
dclTextOr(tree) {
val printLambda = tree.symbol.isAnonymousFunction
val prefix = modText(tree.mods, keywordStr("def")) ~~ valDefText(nameIdText(tree)) provided (!printLambda)
withEnclosingDef(tree) {
addVparamssText(prefix ~ tparamsText(tree.tparams), tree.vparamss) ~ optAscription(tree.tpt).provided(!printLambda) ~
optText(tree.rhs)((if (printLambda) " => " else " = ") ~ _)
}
}
}
}
Loading