Skip to content

Commit 2204b3b

Browse files
committed
Implement decompiler from Tasty relect
1 parent 4e5cf82 commit 2204b3b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+931
-216
lines changed

compiler/src/dotty/tools/dotc/decompiler/DecompilationPrinter.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dotty.tools.dotc.core.Contexts._
77
import dotty.tools.dotc.core.Phases.Phase
88
import dotty.tools.dotc.core.tasty.TastyPrinter
99
import dotty.tools.dotc.printing.DecompilerPrinter
10+
import dotty.tools.dotc.tasty.TastyImpl
1011
import dotty.tools.io.{File, Path}
1112

1213
/** Phase that prints the trees in all loaded compilation units.
@@ -36,12 +37,9 @@ class DecompilationPrinter extends Phase {
3637

3738
private def printToOutput(out: PrintStream)(implicit ctx: Context): Unit = {
3839
val unit = ctx.compilationUnit
39-
val pageWidth = ctx.settings.pageWidth.value
40-
val printLines = ctx.settings.printLines.value
4140

4241
out.println(s"/** Decompiled from $unit */")
43-
val printer = new DecompilerPrinter(ctx)
44-
out.println(printer.toText(unit.tpdTree).mkString(pageWidth, printLines))
42+
out.print(TastyImpl.showSourceCode.showTree(unit.tpdTree)(ctx))
4543

4644
if (ctx.settings.printTasty.value) {
4745
out.println("/*")

compiler/src/dotty/tools/dotc/quoted/QuoteDriver.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import dotty.tools.dotc.Driver
55
import dotty.tools.dotc.core.Contexts.Context
66
import dotty.tools.io.{AbstractFile, Directory, PlainDirectory, VirtualDirectory}
77
import dotty.tools.repl.AbstractFileClassLoader
8-
import dotty.tools.dotc.printing.DecompilerPrinter
98

109
import scala.quoted.{Expr, Type}
11-
1210
import java.net.URLClassLoader
1311

14-
import Toolbox.{Settings, Run, Show}
12+
import Toolbox.{Run, Settings, Show}
13+
import dotty.tools.dotc.tasty.TastyImpl
1514

1615
class QuoteDriver extends Driver {
1716
import tpd._
@@ -42,10 +41,8 @@ class QuoteDriver extends Driver {
4241

4342
def show(expr: Expr[_], settings: Settings[Show]): String = {
4443
def show(tree: Tree, ctx: Context): String = {
45-
val printer = new DecompilerPrinter(ctx)
46-
val pageWidth = ctx.settings.pageWidth.value(ctx)
4744
val tree1 = if (settings.rawTree) tree else (new TreeCleaner).transform(tree)(ctx)
48-
printer.toText(tree1).mkString(pageWidth, false)
45+
TastyImpl.showSourceCode.showTree(tree1)(ctx)
4946
}
5047
withTree(expr, show, settings)
5148
}

compiler/src/dotty/tools/dotc/tasty/FlagSet.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class FlagSet(flags: Flags.FlagSet) extends scala.tasty.FlagSet {
3131
def isScala2X: Boolean = flags.is(Scala2x)
3232
def isDefaultParameterized: Boolean = flags.is(DefaultParameterized)
3333
def isStable: Boolean = flags.is(Stable)
34+
def isParam: Boolean = flags.is(Param)
35+
def isParamAccessor: Boolean = flags.is(ParamAccessor)
3436

3537
override def toString: String = {
3638
val flags = List.newBuilder[String]
@@ -60,6 +62,8 @@ class FlagSet(flags: Flags.FlagSet) extends scala.tasty.FlagSet {
6062
if (isScala2X) flags += "scala2x"
6163
if (isDefaultParameterized) flags += "defaultParameterized"
6264
if (isStable) flags += "stable"
65+
if (isParam) flags += "param"
66+
if (isParamAccessor) flags += "paramAccessor"
6367
flags.result().mkString("<", ",", ">")
6468
}
6569

compiler/src/dotty/tools/dotc/tasty/FromSymbol.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ object FromSymbol {
2020
def packageDef(sym: Symbol)(implicit ctx: Context): PackageDefinition = PackageDefinitionImpl(sym)
2121

2222
def classDef(cls: ClassSymbol)(implicit ctx: Context): tpd.Tree = {
23-
val constr = tpd.DefDef(cls.unforcedDecls.find(_.isPrimaryConstructor).asTerm)
23+
val constrSym = cls.unforcedDecls.find(_.isPrimaryConstructor)
24+
if (!constrSym.exists) return tpd.EmptyTree
25+
val constr = tpd.DefDef(constrSym.asTerm)
2426
val body = cls.unforcedDecls.filter(!_.isPrimaryConstructor).map(s => definition(s))
2527
val superArgs = Nil // TODO
2628
tpd.ClassDef(cls, constr, body, superArgs)

compiler/src/dotty/tools/dotc/tasty/TastyImpl.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import dotty.tools.dotc.core.Decorators._
99
import dotty.tools.dotc.core.quoted.PickledQuotes
1010
import dotty.tools.dotc.util.SourcePosition
1111

12-
import scala.quoted
12+
import scala.{quoted, tasty}
1313
import scala.reflect.ClassTag
14-
import scala.tasty.util.{Show, ShowExtractors}
14+
import scala.tasty.util.{Show, ShowExtractors, ShowSourceCode}
1515

1616
object TastyImpl extends scala.tasty.Tasty {
1717

@@ -31,8 +31,7 @@ object TastyImpl extends scala.tasty.Tasty {
3131

3232
def showExtractors: Show[this.type] = new ShowExtractors(this)
3333

34-
// TODO
35-
// def showSourceCode: Show[this.type] = ???
34+
def showSourceCode: Show[this.type] = new ShowSourceCode(this)
3635

3736
// ===== Contexts =================================================
3837

@@ -127,10 +126,18 @@ object TastyImpl extends scala.tasty.Tasty {
127126

128127
type Definition = tpd.Tree
129128

129+
object Definition extends DefinitionExtractor {
130+
def unapply(x: Definition)(implicit ctx: Context): Boolean =
131+
x.isInstanceOf[Trees.MemberDef[_]]
132+
}
133+
130134
def DefinitionDeco(x: Definition): AbstractDefinition = new AbstractDefinition {
131135

132136
def owner(implicit ctx: Context): Definition = FromSymbol.definition(x.symbol.owner)
133137

138+
def flags(implicit ctx: Contexts.Context): FlagSet =
139+
new FlagSet(x.symbol.flags)
140+
134141
def mods(implicit ctx: Context): List[Modifier] = {
135142
val privateWithin = x.symbol.privateWithin
136143
val isProtected = x.symbol.is(core.Flags.Protected)

compiler/test/dotty/tools/dotc/FromTastyTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class FromTastyTests extends ParallelTesting {
3838
"t3612.scala",
3939
),
4040
recompilationBlacklist = Set(
41-
"simpleCaseObject"
41+
"simpleCaseObject",
42+
"annot-bootstrap.scala",
4243
)
4344
).checkCompile()
4445
}

library/src/scala/tasty/FlagSet.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ trait FlagSet {
2727
def isScala2X: Boolean // Imported from Scala2.x
2828
def isDefaultParameterized: Boolean // Method with default parameters
2929
def isStable: Boolean // Method that is assumed to be stable
30+
def isParam: Boolean
31+
def isParamAccessor: Boolean
3032
}

library/src/scala/tasty/Tasty.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ abstract class Tasty { tasty =>
2323

2424
def showExtractors: Show[tasty.type]
2525

26-
// TODO
27-
// def showSourceCode: Show[tasty.type]
26+
def showSourceCode: Show[tasty.type]
2827

2928
// ===== Contexts =================================================
3029

@@ -108,9 +107,15 @@ abstract class Tasty { tasty =>
108107

109108
type Definition <: Statement
110109

110+
val Definition: DefinitionExtractor
111+
abstract class DefinitionExtractor {
112+
def unapply(x: Definition)(implicit ctx: Context): Boolean
113+
}
114+
111115
implicit def definitionClassTag: ClassTag[Definition]
112116

113117
trait AbstractDefinition {
118+
def flags(implicit ctx: Context): FlagSet
114119
def mods(implicit ctx: Context): List[Modifier]
115120
def owner(implicit ctx: Context): Definition
116121
def localContext(implicit ctx: Context): Context

0 commit comments

Comments
 (0)