diff options
author | phaller <hallerp@gmail.com> | 2012-11-16 10:12:45 +0100 |
---|---|---|
committer | phaller <hallerp@gmail.com> | 2012-11-16 10:12:45 +0100 |
commit | a0f1a6e7fa65634fb18eeecb9ac233ea18597320 (patch) | |
tree | c71446688474dbe6e18a77dd32418f6396135a1e /src/main/scala | |
parent | 7e1441cd468191a1bbe9a03439951b7e3c2b508f (diff) | |
download | scala-async-a0f1a6e7fa65634fb18eeecb9ac233ea18597320.tar.gz scala-async-a0f1a6e7fa65634fb18eeecb9ac233ea18597320.tar.bz2 scala-async-a0f1a6e7fa65634fb18eeecb9ac233ea18597320.zip |
Add TransformUtils class for shared utilities
Diffstat (limited to 'src/main/scala')
-rw-r--r-- | src/main/scala/scala/async/AnfTransform.scala | 12 | ||||
-rw-r--r-- | src/main/scala/scala/async/ExprBuilder.scala | 12 | ||||
-rw-r--r-- | src/main/scala/scala/async/TransformUtils.scala | 22 |
3 files changed, 25 insertions, 21 deletions
diff --git a/src/main/scala/scala/async/AnfTransform.scala b/src/main/scala/scala/async/AnfTransform.scala index aa62b77..2dd6f8c 100644 --- a/src/main/scala/scala/async/AnfTransform.scala +++ b/src/main/scala/scala/async/AnfTransform.scala @@ -2,21 +2,11 @@ package scala.async import scala.reflect.macros.Context -class AnfTransform[C <: Context](val c: C) { +class AnfTransform[C <: Context](override val c: C) extends TransformUtils(c) { import c.universe._ import AsyncUtils._ object inline { - //TODO: DRY - private def defaultValue(tpe: Type): Literal = { - val defaultValue: Any = - if (tpe <:< definitions.BooleanTpe) false - else if (definitions.ScalaNumericValueClasses.exists(tpe <:< _.toType)) 0 - else if (tpe <:< definitions.AnyValTpe) 0 - else null - Literal(Constant(defaultValue)) - } - def transformToList(tree: Tree): List[Tree] = { val stats :+ expr = anf.transformToList(tree) expr match { diff --git a/src/main/scala/scala/async/ExprBuilder.scala b/src/main/scala/scala/async/ExprBuilder.scala index 467169c..2f20a1d 100644 --- a/src/main/scala/scala/async/ExprBuilder.scala +++ b/src/main/scala/scala/async/ExprBuilder.scala @@ -11,7 +11,8 @@ import AsyncUtils.vprintln /* * @author Philipp Haller */ -final class ExprBuilder[C <: Context, FS <: FutureSystem](val c: C, val futureSystem: FS) { +final class ExprBuilder[C <: Context, FS <: FutureSystem](override val c: C, val futureSystem: FS) + extends TransformUtils(c) { builder => import c.universe._ @@ -50,15 +51,6 @@ final class ExprBuilder[C <: Context, FS <: FutureSystem](val c: C, val futureSy private def mkStateTree(nextState: Tree): c.Tree = Assign(Ident(name.state), nextState) - private def defaultValue(tpe: Type): Literal = { - val defaultValue: Any = - if (tpe <:< definitions.BooleanTpe) false - else if (definitions.ScalaNumericValueClasses.exists(tpe <:< _.toType)) 0 - else if (tpe <:< definitions.AnyValTpe) 0 - else null - Literal(Constant(defaultValue)) - } - private def mkVarDefTree(resultType: Type, resultName: TermName): c.Tree = { ValDef(Modifiers(Flag.MUTABLE), resultName, TypeTree(resultType), defaultValue(resultType)) } diff --git a/src/main/scala/scala/async/TransformUtils.scala b/src/main/scala/scala/async/TransformUtils.scala new file mode 100644 index 0000000..d36c277 --- /dev/null +++ b/src/main/scala/scala/async/TransformUtils.scala @@ -0,0 +1,22 @@ +/** + * Copyright (C) 2012 Typesafe Inc. <http://www.typesafe.com> + */ +package scala.async + +import scala.reflect.macros.Context + +/** + * Utilities used in both `ExprBuilder` and `AnfTransform`. + */ +class TransformUtils[C <: Context](val c: C) { + import c.universe._ + + protected def defaultValue(tpe: Type): Literal = { + val defaultValue: Any = + if (tpe <:< definitions.BooleanTpe) false + else if (definitions.ScalaNumericValueClasses.exists(tpe <:< _.toType)) 0 + else if (tpe <:< definitions.AnyValTpe) 0 + else null + Literal(Constant(defaultValue)) + } +} |