Skip to content

Fix decompilation of while loops #4584

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
Jun 9, 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
80 changes: 61 additions & 19 deletions library/src/scala/tasty/util/ShowSourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,39 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this
}

case While(cond, stats) =>
this += "while ("
printTree(cond)
this += ") "
stats match {
case stat :: Nil =>
printTree(stat)
case stats =>
this += "{"
indented {
this += lineBreak()
printTrees(stats, lineBreak())
}
this += lineBreak() += "}"
}

case DoWhile(stats, cond) =>
this += "do "
stats match {
case stat :: Nil =>
printTree(stat)
case stats =>
this += "{"
indented {
this += lineBreak()
printTrees(stats, lineBreak())
}
this += lineBreak() += "}"
}
this += " while ("
printTree(cond)
this += ")"

case ddef@DefDef(name, targs, argss, tpt, rhs) =>
val flags = ddef.flags
if (flags.isOverride) sb.append("override ")
Expand Down Expand Up @@ -225,7 +258,14 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += " = "
printTree(rhs)

case Term.Block(stats, expr) =>
case Term.Block(stats0, expr) =>
def isLoopEntryPoint(tree: Tree): Boolean = tree match {
case Term.Apply(Term.Ident("while$" | "doWhile$"), _) => true
case _ => false
}

val stats = stats0.filterNot(isLoopEntryPoint)

expr match {
case Term.Lambda(_, _) =>
// Decompile lambda from { def annon$(...) = ...; closure(annon$, ...)}
Expand All @@ -235,31 +275,17 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
this += " => "
printTree(rhs)
this += ")"

case Term.Apply(Term.Ident("while$"), _) =>
val DefDef("while$", _, _, _, Some(Term.If(cond, Term.Block(body :: Nil, _), _))) = stats.head
this += "while ("
printTree(cond)
this += ") "
printTree(body)

case Term.Apply(Term.Ident("doWhile$"), _) =>
val DefDef("doWhile$", _, _, _, Some(Term.Block(List(body), Term.If(cond, _, _)))) = stats.head
this += "do "
printTree(body)
this += " while ("
printTree(cond)
this += ")"

case _ =>
this += "{"
indented {
if (!stats.isEmpty) {
this += lineBreak()
printTrees(stats, lineBreak())
}
this += lineBreak()
printTree(expr)
if (!isLoopEntryPoint(expr)) {
this += lineBreak()
printTree(expr)
}
}
this += lineBreak() += "}"
}
Expand Down Expand Up @@ -719,6 +745,22 @@ class ShowSourceCode[T <: Tasty with Singleton](tasty0: T) extends Show[T](tasty
}
}

private object While {
def unapply(arg: Tree)(implicit ctx: Context): Option[(Term, List[Statement])] = arg match {
case DefDef("while$", _, _, _, Some(Term.If(cond, Term.Block(bodyStats, _), _))) => Some((cond, bodyStats))
case Term.Block(List(tree), _) => unapply(tree)
case _ => None
}
}

private object DoWhile {
def unapply(arg: Tree)(implicit ctx: Context): Option[(List[Statement], Term)] = arg match {
case DefDef("doWhile$", _, _, _, Some(Term.Block(body, Term.If(cond, _, _)))) => Some((body, cond))
case Term.Block(List(tree), _) => unapply(tree)
case _ => None
}
}

// TODO Provide some of these in scala.tasty.Tasty.scala and implement them using checks on symbols for performance
private object Types {

Expand Down
31 changes: 31 additions & 0 deletions tests/run/quote-inline-function.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Normal function
{
var i: scala.Int = 0
val j: scala.Int = 5
while (i.<(j)) {
val x$1: scala.Int = i
f.apply(x$1)
i = i.+(1)
}
do {
val x$2: scala.Int = i
f.apply(x$2)
i = i.+(1)
} while (i.<(j))
}

By name function
{
var i: scala.Int = 0
val j: scala.Int = 5
while (i.<(j)) {
val x$1: scala.Int = i
scala.Predef.println(x$1)
i = i.+(1)
}
do {
val x$2: scala.Int = i
scala.Predef.println(x$2)
i = i.+(1)
} while (i.<(j))
}
25 changes: 25 additions & 0 deletions tests/run/quote-inline-function/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import scala.quoted._

import dotty.tools.dotc.quoted.Toolbox._

object Macros {

inline def foreach1(start: Int, end: Int, f: Int => Unit): String = ~impl('(start), '(end), '(f))
inline def foreach2(start: Int, end: Int, f: => Int => Unit): String = ~impl('(start), '(end), '(f))

def impl(start: Expr[Int], end: Expr[Int], f: Expr[Int => Unit]): Expr[String] = {
val res = '{
var i = ~start
val j = ~end
while (i < j) {
~f.apply('(i))
i += 1
}
do {
~f.apply('(i))
i += 1
} while (i < j)
}
res.show.toExpr
}
}
13 changes: 13 additions & 0 deletions tests/run/quote-inline-function/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.quoted._
import Macros._

object Test {
def main(args: Array[String]): Unit = {
println("Normal function")
println(foreach1(0, 5, x => println(x)))
println()

println("By name function")
println(foreach2(0, 5, x => println(x)))
}
}