forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
101 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package dotty.tools.dotc | ||
package decompiler | ||
|
||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.core.Phases.Phase | ||
|
||
/** Phase that prints the trees in all loaded compilation units. | ||
* | ||
* @author Nicolas Stucki | ||
*/ | ||
class DecompilationPrinter extends Phase { | ||
|
||
override def phaseName: String = "decompilationPrinter" | ||
|
||
override def run(implicit ctx: Context): Unit = { | ||
val unit = ctx.compilationUnit | ||
|
||
val pageWidth = ctx.settings.pageWidth.value | ||
|
||
val doubleLine = "=" * pageWidth | ||
val line = "-" * pageWidth | ||
|
||
println(doubleLine) | ||
println(unit.source) | ||
println(line) | ||
|
||
val code = unit.tpdTree.show | ||
println(if (ctx.useColors) printing.SyntaxHighlighting(code) else code) | ||
println(line) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dotty.tools.dotc.decompiler | ||
|
||
import dotty.tools.dotc | ||
import dotty.tools.dotc.core.Contexts._ | ||
|
||
/** Main class of the `dotc -decompiler` decompiler. | ||
* | ||
* @author Nicolas Stucki | ||
*/ | ||
object Main extends dotc.Driver { | ||
override protected def newCompiler(implicit ctx: Context): dotc.Compiler = { | ||
assert(ctx.settings.fromTasty.value) | ||
new TASTYDecompiler | ||
} | ||
|
||
override def setup(args0: Array[String], rootCtx: Context): (List[String], Context) = { | ||
var args = args0.filter(a => a != "-decompile") | ||
args = if (args.contains("-from-tasty")) args else "-from-tasty" +: args | ||
super.setup(args, rootCtx) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
compiler/src/dotty/tools/dotc/decompiler/TASTYDecompiler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dotty.tools.dotc.decompiler | ||
|
||
import dotty.tools.dotc.fromtasty._ | ||
import dotty.tools.dotc.core.Phases.Phase | ||
|
||
/** Compiler from tasty to user readable high text representation | ||
* of the compiled scala code. | ||
* | ||
* @author Nicolas Stucki | ||
*/ | ||
class TASTYDecompiler extends TASTYCompiler { | ||
override def phases: List[List[Phase]] = List( | ||
List(new ReadTastyTreesFromClasses), // Load classes from tasty | ||
List(new DecompilationPrinter) // Print all loaded classes | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters