-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Decompile .tasty files with VS Code Extension #5513
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
227e1c8
Make TastyPrinter return a String instead of printing to sys.out
c1b2b12
Make remote interface more generic to allow extensions
e0bd562
Create Decompiler Driver for IDE
2b64b6c
Add language server feature to handle decompile requests
15ffcc8
Add feature to decompile .tasty files to vscode extension
53d89a5
Fix style issues
360e0d7
Apply suggested changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
16 changes: 16 additions & 0 deletions
16
compiler/src/dotty/tools/dotc/core/tasty/TastyHTMLPrinter.scala
This file contains hidden or 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 | ||
package core | ||
package tasty | ||
|
||
import Contexts._, Decorators._ | ||
import Names.Name | ||
import TastyUnpickler._ | ||
import TastyBuffer.NameRef | ||
import util.Positions.offsetToInt | ||
import printing.Highlighting._ | ||
|
||
class TastyHTMLPrinter(bytes: Array[Byte])(implicit ctx: Context) extends TastyPrinter(bytes) { | ||
override protected def nameColor(str: String): String = s"<span class='name'>$str</span>" | ||
override protected def treeColor(str: String): String = s"<span class='tree'>$str</span>" | ||
override protected def lengthColor(str: String): String = s"<span class='length'>$str</span>" | ||
} |
This file contains hidden or 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 hidden or 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
44 changes: 44 additions & 0 deletions
44
compiler/src/dotty/tools/dotc/decompiler/IDEDecompilerDriver.scala
This file contains hidden or 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,44 @@ | ||
package dotty.tools | ||
package dotc | ||
package decompiler | ||
|
||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.core._ | ||
import dotty.tools.dotc.core.tasty.TastyHTMLPrinter | ||
import dotty.tools.dotc.reporting._ | ||
import dotty.tools.dotc.tastyreflect.ReflectionImpl | ||
|
||
/** | ||
* Decompiler to be used with IDEs | ||
*/ | ||
class IDEDecompilerDriver(val settings: List[String]) extends dotc.Driver { | ||
|
||
private val myInitCtx: Context = { | ||
val rootCtx = initCtx.fresh.addMode(Mode.Interactive).addMode(Mode.ReadPositions).addMode(Mode.ReadComments) | ||
rootCtx.setSetting(rootCtx.settings.YretainTrees, true) | ||
rootCtx.setSetting(rootCtx.settings.fromTasty, true) | ||
val ctx = setup(settings.toArray :+ "dummy.scala", rootCtx)._2 | ||
ctx.initialize()(ctx) | ||
ctx | ||
} | ||
|
||
private val decompiler = new PartialTASTYDecompiler | ||
|
||
def run(className: String): (String, String) = { | ||
val reporter = new StoreReporter(null) with HideNonSensicalMessages | ||
|
||
val run = decompiler.newRun(myInitCtx.fresh.setReporter(reporter)) | ||
|
||
implicit val ctx = run.runContext | ||
|
||
run.compile(List(className)) | ||
run.printSummary() | ||
val unit = ctx.run.units.head | ||
|
||
val decompiled = new ReflectionImpl(ctx).showSourceCode.showTree(unit.tpdTree) | ||
val tree = new TastyHTMLPrinter(unit.pickled.head._2).printContents() | ||
|
||
reporter.removeBufferedMessages.foreach(message => System.err.println(message)) | ||
(tree, decompiled) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
compiler/src/dotty/tools/dotc/decompiler/PartialTASTYDecompiler.scala
This file contains hidden or 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,12 @@ | ||
package dotty.tools.dotc.decompiler | ||
|
||
import dotty.tools.dotc.core.Phases.Phase | ||
|
||
/** Partial TASTYDecompiler that doesn't execute the backendPhases | ||
* allowing to control decompiler output by manually running it | ||
* on the CompilationUnits | ||
*/ | ||
class PartialTASTYDecompiler extends TASTYDecompiler { | ||
override def phases: List[List[Phase]] = | ||
tuvior marked this conversation as resolved.
Show resolved
Hide resolved
|
||
frontendPhases ::: picklerPhases ::: transformPhases | ||
} |
6 changes: 6 additions & 0 deletions
6
language-server/src/dotty/tools/languageserver/DottyClient.scala
This file contains hidden or 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,6 @@ | ||
package dotty.tools.languageserver | ||
|
||
/** | ||
* A `LanguageClient` that regroups all language server features | ||
*/ | ||
trait DottyClient extends worksheet.WorksheetClient |
This file contains hidden or 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 hidden or 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
23 changes: 23 additions & 0 deletions
23
language-server/src/dotty/tools/languageserver/decompiler/TastyDecompilerMessages.scala
This file contains hidden or 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,23 @@ | ||
package dotty.tools.languageserver.decompiler | ||
|
||
import org.eclipse.lsp4j.TextDocumentIdentifier | ||
|
||
// All case classes in this file should have zero-parameters secondary | ||
// constructors to allow Gson to reflectively create instances on | ||
// deserialization without relying on sun.misc.Unsafe. | ||
|
||
/** The parameter for the `tasty/decompile` request. */ | ||
case class TastyDecompileParams(textDocument: TextDocumentIdentifier) { | ||
def this() = this(null) | ||
} | ||
|
||
/** The response to a `tasty/decompile` request. */ | ||
case class TastyDecompileResult(tastyTree: String = null, scala: String = null, error: Int = 0) { | ||
def this() = this(null, null, 0) | ||
} | ||
|
||
object TastyDecompileResult { | ||
val ErrorTastyVersion = 1 | ||
val ErrorClassNotFound = 2 | ||
val ErrorOther = -1 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.