aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scala/async/internal/AsyncMacro.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2015-07-23 23:15:37 +1000
committerJason Zaugg <jzaugg@gmail.com>2015-09-22 16:53:33 +1000
commite3ff0382ae4e015fc69da8335450718951714982 (patch)
tree3f89dace31be3cd125531c0ba24270aa45100d7e /src/main/scala/scala/async/internal/AsyncMacro.scala
parent93f207fee780652d08f93e1ea40e018db59fee99 (diff)
downloadscala-async-e3ff0382ae4e015fc69da8335450718951714982.tar.gz
scala-async-e3ff0382ae4e015fc69da8335450718951714982.tar.bz2
scala-async-e3ff0382ae4e015fc69da8335450718951714982.zip
Enable a compiler plugin to use the async transform after patmat
Currently, the async transformation is performed during the typer phase, like all other macros. We have to levy a few artificial restrictions on whern an async boundary may be: for instance we don't support await within a pattern guard. A more natural home for the transform would be after patterns have been translated. The test case in this commit shows how to use the async transform from a custom compiler phase after patmat. The remainder of the commit updates the implementation to handle the new tree shapes. For states that correspond to a label definition, we use `-symbol.id` as the state ID. This made it easier to emit the forward jumps to when processing the label application before we had seen the label definition. I've also made the transformation more efficient in the way it checks whether a given tree encloses an `await` call: we traverse the input tree at the start of the macro, and decorate it with tree attachments containig the answer to this question. Even after the ANF and state machine transforms introduce new layers of synthetic trees, the `containsAwait` code need only traverse shallowly through those trees to find a child that has the cached answer from the original traversal. I had to special case the ANF transform for expressions that always lead to a label jump: we avoids trying to push an assignment to a result variable into `if (cond) jump1() else jump2()`, in trees of the form: ``` % cat sandbox/jump.scala class Test { def test = { (null: Any) match { case _: String => "" case _ => "" } } } % qscalac -Xprint:patmat -Xprint-types sandbox/jump.scala def test: String = { case <synthetic> val x1: Any = (null{Null(null)}: Any){Any}; case5(){ if (x1.isInstanceOf{[T0]=> Boolean}[String]{Boolean}) matchEnd4{(x: String)String}(""{String("")}){String} else case6{()String}(){String}{String} }{String}; case6(){ matchEnd4{(x: String)String}(""{String("")}){String} }{String}; matchEnd4(x: String){ x{String} }{String} }{String} ```
Diffstat (limited to 'src/main/scala/scala/async/internal/AsyncMacro.scala')
-rw-r--r--src/main/scala/scala/async/internal/AsyncMacro.scala7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/main/scala/scala/async/internal/AsyncMacro.scala b/src/main/scala/scala/async/internal/AsyncMacro.scala
index e969f9b..e22407d 100644
--- a/src/main/scala/scala/async/internal/AsyncMacro.scala
+++ b/src/main/scala/scala/async/internal/AsyncMacro.scala
@@ -1,15 +1,17 @@
package scala.async.internal
object AsyncMacro {
- def apply(c0: reflect.macros.Context, base: AsyncBase): AsyncMacro { val c: c0.type } = {
+ def apply(c0: reflect.macros.Context, base: AsyncBase)(body0: c0.Tree): AsyncMacro { val c: c0.type } = {
import language.reflectiveCalls
new AsyncMacro { self =>
val c: c0.type = c0
+ val body: c.Tree = body0
// This member is required by `AsyncTransform`:
val asyncBase: AsyncBase = base
// These members are required by `ExprBuilder`:
val futureSystem: FutureSystem = base.futureSystem
val futureSystemOps: futureSystem.Ops {val c: self.c.type} = futureSystem.mkOps(c)
+ val containsAwait: c.Tree => Boolean = containsAwaitCached(body0)
}
}
}
@@ -19,7 +21,10 @@ private[async] trait AsyncMacro
with ExprBuilder with AsyncTransform with AsyncAnalysis with LiveVariables {
val c: scala.reflect.macros.Context
+ val body: c.Tree
+ val containsAwait: c.Tree => Boolean
lazy val macroPos = c.macroApplication.pos.makeTransparent
def atMacroPos(t: c.Tree) = c.universe.atPos(macroPos)(t)
+
}