Skip to content

Commit e0bd562

Browse files
author
Tobias Bordenca
committed
Create Decompiler Driver for IDE
PartialTASTYDecompiler stops where the normal decompiler would traverse the tree
1 parent c1b2b12 commit e0bd562

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package dotty.tools.dotc
2+
package core
3+
package tasty
4+
5+
import Contexts._, Decorators._
6+
import Names.Name
7+
import TastyUnpickler._
8+
import TastyBuffer.NameRef
9+
import util.Positions.offsetToInt
10+
import printing.Highlighting._
11+
12+
class TastyHTMLPrinter(bytes: Array[Byte])(implicit ctx: Context) extends TastyPrinter(bytes) {
13+
override protected def nameColor(str: String): String = s"<span class='name'>$str</span>"
14+
override protected def treeColor(str: String): String = s"<span class='tree'>$str</span>"
15+
override protected def lengthColor(str: String): String = s"<span class='length'>$str</span>"
16+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package dotty.tools
2+
package dotc
3+
package decompiler
4+
5+
import dotty.tools.dotc.core.Contexts._
6+
import dotty.tools.dotc.core._
7+
import dotty.tools.dotc.core.tasty.TastyHTMLPrinter
8+
import dotty.tools.dotc.reporting._
9+
import dotty.tools.dotc.tastyreflect.ReflectionImpl
10+
11+
/**
12+
* Decompiler to be used with IDEs
13+
*/
14+
class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver {
15+
16+
private val myInitCtx: Context = {
17+
val rootCtx = initCtx.fresh.addMode(Mode.Interactive).addMode(Mode.ReadPositions).addMode(Mode.ReadComments)
18+
rootCtx.setSetting(rootCtx.settings.YretainTrees, true)
19+
rootCtx.setSetting(rootCtx.settings.fromTasty, true)
20+
val ctx = setup(settings.toArray :+ "dummy.scala", rootCtx)._2
21+
ctx.initialize()(ctx)
22+
ctx
23+
}
24+
25+
private val decompiler = new PartialTASTYDecompiler
26+
27+
def run(className: String): (String, String) = {
28+
val reporter = new StoreReporter(null) with HideNonSensicalMessages
29+
30+
val run = decompiler.newRun(myInitCtx.fresh.setReporter(reporter))
31+
32+
implicit val ctx = run.runContext
33+
34+
run.compile(List(className))
35+
run.printSummary()
36+
val unit = ctx.run.units.head
37+
38+
val decompiled = new ReflectionImpl(ctx).showSourceCode.showTree(unit.tpdTree)
39+
val tree = new TastyHTMLPrinter(unit.pickled.head._2).printContents()
40+
41+
reporter.removeBufferedMessages.foreach(message => System.err.println(message))
42+
(tree, decompiled)
43+
}
44+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dotty.tools.dotc.decompiler
2+
3+
import dotty.tools.dotc.core.Phases.Phase
4+
5+
/** Partial TASTYDecompiler that doesn't execute the backendPhases
6+
* allowing to control decompiler output by manually running it
7+
* on the CompilationUnits
8+
*/
9+
class PartialTASTYDecompiler extends TASTYDecompiler {
10+
override def phases: List[List[Phase]] =
11+
frontendPhases ::: picklerPhases ::: transformPhases
12+
}

0 commit comments

Comments
 (0)