Skip to content

Decompile while loops and remove empty packages #4250

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 4 commits into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions compiler/src/dotty/tools/dotc/printing/DecompilerPrinter.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package dotty.tools.dotc.printing

import dotty.tools.dotc.ast.Trees.{Closure, DefDef, Untyped, ValDef}
import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.ast.untpd.{PackageDef, Template, TypeDef}
import dotty.tools.dotc.ast.{Trees, untpd}
import dotty.tools.dotc.printing.Texts._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.StdNames._

import scala.language.implicitConversions

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

override protected def blockText[T >: Untyped](trees: List[Trees.Tree[T]]): Text = {
super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
trees match {
case DefDef(_, _, _, _, Trees.If(cond, Trees.Block(body :: Nil, _), _)) :: y :: Nil if y.symbol.name == nme.WHILE_PREFIX =>
keywordText("while") ~ " (" ~ toText(cond) ~ ")" ~ toText(body)
case DefDef(_, _, _, _, Trees.Block(body :: Nil, Trees.If(cond, _, _))) :: y :: Nil if y.symbol.name == nme.DO_WHILE_PREFIX =>
keywordText("do") ~ toText(body) ~ keywordText("while") ~ " (" ~ toText(cond) ~ ")"
case _ => super.blockText(trees.filterNot(_.isInstanceOf[Closure[_]]))
}
}

override protected def packageDefText(tree: PackageDef): Text = {
Expand All @@ -29,8 +36,10 @@ class DecompilerPrinter(_ctx: Context) extends RefinedPrinter(_ctx) {
case _ => toTextGlobal(stats, "\n")
}
val bodyText =
if (currentPrecedence == TopLevelPrec) "\n" ~ statsText else " {" ~ statsText ~ "}"
keywordStr("package ") ~ toTextPackageId(tree.pid) ~ bodyText
if (tree.pid.symbol.isEmptyPackage) statsText
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this case redundant since you discard it on line 42 if tree.pid.symbol.isEmptyPackage?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The body is the only part that is not discarded

else if (currentPrecedence == TopLevelPrec) "\n" ~ statsText
else " {" ~ statsText ~ "}"
(keywordStr("package ") ~ toTextPackageId(tree.pid)).provided(!tree.symbol.isEmptyPackage) ~ bodyText
}

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

override protected def toTextTemplate(impl: Template, ofNew: Boolean = false): Text = {
val impl1 = impl.copy(parents = impl.parents.filterNot(_.symbol.maybeOwner == defn.ObjectClass))
super.toTextTemplate(impl1, ofNew)
}

override protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
import untpd.{modsDeco => _, _}
dclTextOr(tree) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {

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

prefix ~ (keywordText(" extends") provided !ofNew) ~~ parentsText ~~ bodyText
prefix ~ (keywordText(" extends") provided (!ofNew && parents.nonEmpty)) ~~ parentsText ~~ bodyText
}

protected def templateText(tree: TypeDef, impl: Template): Text = {
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/lambda.decompiled
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
out/posTestFromTasty/pos/lambda/foo/Foo.class
--------------------------------------------------------------------------------
package foo {
class Foo() extends Object() {
class Foo() {
val a: Int => Int =
{
(x: Int) => x.*(x)
Expand Down
10 changes: 4 additions & 6 deletions tests/pos/methodTypes.decompiled
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
================================================================================
out/posTestFromTasty/pos/methodTypes/Foo.class
--------------------------------------------------------------------------------
package <empty> {
class Foo() extends Object() {
val x: Int = 1
def y: Int = 2
def z(): Int = 3
}
class Foo() {
val x: Int = 1
def y: Int = 2
def z(): Int = 3
}
--------------------------------------------------------------------------------
4 changes: 1 addition & 3 deletions tests/pos/simpleCaseObject.decompiled
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
out/posTestFromTasty/pos/simpleCaseObject/foo/Foo.class
--------------------------------------------------------------------------------
package foo {
case object Foo() extends Object() with _root_.scala.Product {
this: foo.Foo.type =>

case object Foo() extends _root_.scala.Product { this: foo.Foo.type =>
override def hashCode(): Int = 1045991777
override def toString(): String = "Foo"
override def canEqual(that: Any): Boolean = that.isInstanceOf[foo.Foo]
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/simpleClass-2.decompiled
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ package foo {
out/posTestFromTasty/pos/simpleClass-2/foo/B.class
--------------------------------------------------------------------------------
package foo {
class B() extends Object() {}
class B() {}
}
--------------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion tests/pos/simpleClass.decompiled
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
out/posTestFromTasty/pos/simpleClass/foo/A.class
--------------------------------------------------------------------------------
package foo {
class A() extends Object() {}
class A() {}
}
--------------------------------------------------------------------------------
================================================================================
Expand Down
15 changes: 15 additions & 0 deletions tests/pos/simpleDoWhile.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
================================================================================
out/posTestFromTasty/pos/simpleDoWhile/Foo.class
--------------------------------------------------------------------------------
class Foo() {
def foo: Unit =
{
var i: Int = 1
do
{
i = 0
}
while (i.!=(0))
}
}
--------------------------------------------------------------------------------
8 changes: 8 additions & 0 deletions tests/pos/simpleDoWhile.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Foo {
def foo = {
var i = 1
do {
i = 0
} while (i != 0)
}
}
14 changes: 14 additions & 0 deletions tests/pos/simpleWhile.decompiled
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
================================================================================
out/posTestFromTasty/pos/simpleWhile/Foo.class
--------------------------------------------------------------------------------
class Foo() {
def foo: Unit =
{
var i: Int = 1
while (i.!=(0))
{
i = 0
}
}
}
--------------------------------------------------------------------------------
8 changes: 8 additions & 0 deletions tests/pos/simpleWhile.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Foo {
def foo = {
var i = 1
while (i != 0) {
i = 0
}
}
}
22 changes: 10 additions & 12 deletions tests/run/puzzle.decompiled
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
================================================================================
out/runTestFromTasty/run/puzzle/Test.class
--------------------------------------------------------------------------------
package <empty> {
object Test() extends Object() { this: Test.type =>
def main(args: Array[String]): Unit =
{
println(if false then 5.0 else 53.0)
val x: Double = if false then 5.0 else 53.0
println(x)
val z: Long = 1L
val y: Float = Long.long2float(z)
()
}
}
object Test() { this: Test.type =>
def main(args: Array[String]): Unit =
{
println(if false then 5.0 else 53.0)
val x: Double = if false then 5.0 else 53.0
println(x)
val z: Long = 1L
val y: Float = Long.long2float(z)
()
}
}
--------------------------------------------------------------------------------