Skip to content

Commit 62be430

Browse files
Merge pull request #4250 from dotty-staging/decompile-while-loops
Decompile while loops and remove empty packages
2 parents 2fcb7b0 + b78ca99 commit 62be430

12 files changed

+82
-29
lines changed

compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package dotty.tools.dotc.printing
22

3-
import dotty.tools.dotc.ast.Trees.{Closure, DefDef, Untyped, ValDef}
3+
import dotty.tools.dotc.ast.Trees._
44
import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef}
55
import dotty.tools.dotc.ast.{Trees, untpd}
66
import dotty.tools.dotc.printing.Texts._
77
import dotty.tools.dotc.core.Contexts._
88
import dotty.tools.dotc.core.Flags._
99
import dotty.tools.dotc.core.Symbols._
10+
import dotty.tools.dotc.core.StdNames._
1011

1112
import scala.language.implicitConversions
1213

@@ -16,7 +17,13 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
1617
annots.filter(_.tpe != defn.SourceFileAnnotType)
1718

1819
override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = {
19-
super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
20+
trees match {
21+
case DefDef(_, _, _, _, Trees.If(cond, Trees.Block(body :: Nil, _), _)) :: y :: Nil if y.symbol.name == nme.WHILE_PREFIX =>
22+
keywordText("while") ~ " (" ~ toText(cond) ~ ")" ~ toText(body)
23+
case DefDef(_, _, _, _, Trees.Block(body :: Nil, Trees.If(cond, _, _))) :: y :: Nil if y.symbol.name == nme.DO_WHILE_PREFIX =>
24+
keywordText("do") ~ toText(body) ~ keywordText("while") ~ " (" ~ toText(cond) ~ ")"
25+
case _ => super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
26+
}
2027
}
2128

2229
override protected def packageDefText(tree: PackageDef): Text = {
@@ -29,8 +36,10 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
2936
case _ => toTextGlobal(stats, "\n")
3037
}
3138
val bodyText =
32-
if (currentPrecedence == TopLevelPrec) "\n" ~ statsText else " {" ~ statsText ~ "}"
33-
keywordStr("package ") ~ toTextPackageId(tree.pid) ~ bodyText
39+
if (tree.pid.symbol.isEmptyPackage) statsText
40+
else if (currentPrecedence == TopLevelPrec) "\n" ~ statsText
41+
else " {" ~ statsText ~ "}"
42+
(keywordStr("package ") ~ toTextPackageId(tree.pid)).provided(!tree.symbol.isEmptyPackage) ~ bodyText
3443
}
3544

3645
override protected def templateText(tree: TypeDef, impl: Template): Text = {
@@ -40,6 +49,11 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
4049
decl ~~ typeText(nameIdText(tree)) ~ withEnclosingDef(tree) { toTextTemplate(impl) } ~ ""
4150
}
4251

52+
override protected def toTextTemplate(impl: Template, ofNew: Boolean = false): Text = {
53+
val impl1 = impl.copy(parents = impl.parents.filterNot(_.symbol.maybeOwner == defn.ObjectClass))
54+
super.toTextTemplate(impl1, ofNew)
55+
}
56+
4357
override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
4458
import untpd.{modsDeco => _, _}
4559
dclTextOr(tree) {

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
654654

655655
val bodyText = "{" ~~ selfText ~~ toTextGlobal(primaryConstrs ::: body, "\n") ~ "}"
656656

657-
prefix ~ (keywordText(" extends") provided !ofNew) ~~ parentsText ~~ bodyText
657+
prefix ~ (keywordText(" extends") provided (!ofNew && parents.nonEmpty)) ~~ parentsText ~~ bodyText
658658
}
659659

660660
protected def templateText(tree: TypeDef, impl: Template): Text = {

tests/pos/lambda.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
out/posTestFromTasty/pos/lambda/foo/Foo.class
33
--------------------------------------------------------------------------------
44
package foo {
5-
class Foo() extends Object() {
5+
class Foo() {
66
val a: Int => Int =
77
{
88
(x: Int) => x.*(x)

tests/pos/methodTypes.decompiled

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
================================================================================
22
out/posTestFromTasty/pos/methodTypes/Foo.class
33
--------------------------------------------------------------------------------
4-
package <empty> {
5-
class Foo() extends Object() {
6-
val x: Int = 1
7-
def y: Int = 2
8-
def z(): Int = 3
9-
}
4+
class Foo() {
5+
val x: Int = 1
6+
def y: Int = 2
7+
def z(): Int = 3
108
}
119
--------------------------------------------------------------------------------

tests/pos/simpleCaseObject.decompiled

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
out/posTestFromTasty/pos/simpleCaseObject/foo/Foo.class
33
--------------------------------------------------------------------------------
44
package foo {
5-
case object Foo() extends Object() with _root_.scala.Product {
6-
this: foo.Foo.type =>
7-
5+
case object Foo() extends _root_.scala.Product { this: foo.Foo.type =>
86
override def hashCode(): Int = 1045991777
97
override def toString(): String = "Foo"
108
override def canEqual(that: Any): Boolean = that.isInstanceOf[foo.Foo]

tests/pos/simpleClass-2.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ package foo {
99
out/posTestFromTasty/pos/simpleClass-2/foo/B.class
1010
--------------------------------------------------------------------------------
1111
package foo {
12-
class B() extends Object() {}
12+
class B() {}
1313
}
1414
--------------------------------------------------------------------------------

tests/pos/simpleClass.decompiled

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
out/posTestFromTasty/pos/simpleClass/foo/A.class
33
--------------------------------------------------------------------------------
44
package foo {
5-
class A() extends Object() {}
5+
class A() {}
66
}
77
--------------------------------------------------------------------------------
88
================================================================================

tests/pos/simpleDoWhile.decompiled

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
================================================================================
2+
out/posTestFromTasty/pos/simpleDoWhile/Foo.class
3+
--------------------------------------------------------------------------------
4+
class Foo() {
5+
def foo: Unit =
6+
{
7+
var i: Int = 1
8+
do
9+
{
10+
i = 0
11+
}
12+
while (i.!=(0))
13+
}
14+
}
15+
--------------------------------------------------------------------------------

tests/pos/simpleDoWhile.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Foo {
2+
def foo = {
3+
var i = 1
4+
do {
5+
i = 0
6+
} while (i != 0)
7+
}
8+
}

tests/pos/simpleWhile.decompiled

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
================================================================================
2+
out/posTestFromTasty/pos/simpleWhile/Foo.class
3+
--------------------------------------------------------------------------------
4+
class Foo() {
5+
def foo: Unit =
6+
{
7+
var i: Int = 1
8+
while (i.!=(0))
9+
{
10+
i = 0
11+
}
12+
}
13+
}
14+
--------------------------------------------------------------------------------

tests/pos/simpleWhile.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Foo {
2+
def foo = {
3+
var i = 1
4+
while (i != 0) {
5+
i = 0
6+
}
7+
}
8+
}

tests/run/puzzle.decompiled

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
================================================================================
22
out/runTestFromTasty/run/puzzle/Test.class
33
--------------------------------------------------------------------------------
4-
package <empty> {
5-
object Test() extends Object() { this: Test.type =>
6-
def main(args: Array[String]): Unit =
7-
{
8-
println(if false then 5.0 else 53.0)
9-
val x: Double = if false then 5.0 else 53.0
10-
println(x)
11-
val z: Long = 1L
12-
val y: Float = Long.long2float(z)
13-
()
14-
}
15-
}
4+
object Test() { this: Test.type =>
5+
def main(args: Array[String]): Unit =
6+
{
7+
println(if false then 5.0 else 53.0)
8+
val x: Double = if false then 5.0 else 53.0
9+
println(x)
10+
val z: Long = 1L
11+
val y: Float = Long.long2float(z)
12+
()
13+
}
1614
}
1715
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)