-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
b80d85e
bfb38d3
8ae50fe
6f81983
241bd91
a5f967e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
* | ||
|
@@ -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) | ||
else { | ||
var os: OutputStream = null | ||
var ps: PrintStream = null | ||
try { | ||
os = File(outputDir + ".decompiled").outputStream() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I use the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
} | ||
} |
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 " = ") ~ _) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.