diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2012-11-24 09:15:36 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2012-11-24 09:15:36 +0100 |
commit | 49c169e123dc5e90bcc5f23f63f1415c645603d5 (patch) | |
tree | bd14904f49b2f71b342a88475f02ad885b3a67b1 /src/main/scala | |
parent | 535ca23858a233978ce874d5cbad7d586f1e8635 (diff) | |
download | scala-async-49c169e123dc5e90bcc5f23f63f1415c645603d5.tar.gz scala-async-49c169e123dc5e90bcc5f23f63f1415c645603d5.tar.bz2 scala-async-49c169e123dc5e90bcc5f23f63f1415c645603d5.zip |
Centralize naming.
Diffstat (limited to 'src/main/scala')
-rw-r--r-- | src/main/scala/scala/async/AnfTransform.scala | 14 | ||||
-rw-r--r-- | src/main/scala/scala/async/ExprBuilder.scala | 22 | ||||
-rw-r--r-- | src/main/scala/scala/async/TransformUtils.scala | 26 |
3 files changed, 33 insertions, 29 deletions
diff --git a/src/main/scala/scala/async/AnfTransform.scala b/src/main/scala/scala/async/AnfTransform.scala index 9241e7e..7d19da2 100644 --- a/src/main/scala/scala/async/AnfTransform.scala +++ b/src/main/scala/scala/async/AnfTransform.scala @@ -38,7 +38,7 @@ private[async] final case class AnfTransform[C <: Context](override val c: C) ex val trans = super.transform(defTree) val origName = defTree.symbol.name val sym = defTree.symbol.asInstanceOf[symtab.Symbol] - val fresh = c.fresh("" + sym.name + "$") + val fresh = name.fresh(sym.name.toString) sym.name = defTree.symbol.name match { case _: TermName => symtab.newTermName(fresh) case _: TypeName => symtab.newTypeName(fresh) @@ -92,7 +92,7 @@ private[async] final case class AnfTransform[C <: Context](override val c: C) ex val stats :+ expr = anf.transformToList(tree) expr match { case Apply(fun, args) if isAwait(fun) => - val valDef = defineVal("await", expr, tree.pos) + val valDef = defineVal(name.await, expr, tree.pos) stats :+ valDef :+ Ident(valDef.name) case If(cond, thenp, elsep) => @@ -101,7 +101,7 @@ private[async] final case class AnfTransform[C <: Context](override val c: C) ex if (expr.tpe =:= definitions.UnitTpe) { stats :+ expr :+ Literal(Constant(())) } else { - val varDef = defineVar("ifres", expr.tpe, tree.pos) + val varDef = defineVar(name.ifRes, expr.tpe, tree.pos) def branchWithAssign(orig: Tree) = orig match { case Block(thenStats, thenExpr) => Block(thenStats, Assign(Ident(varDef.name), thenExpr)) case _ => Assign(Ident(varDef.name), orig) @@ -117,7 +117,7 @@ private[async] final case class AnfTransform[C <: Context](override val c: C) ex stats :+ expr :+ Literal(Constant(())) } else { - val varDef = defineVar("matchres", expr.tpe, tree.pos) + val varDef = defineVar(name.matchRes, expr.tpe, tree.pos) val casesWithAssign = cases map { case cd@CaseDef(pat, guard, Block(caseStats, caseExpr)) => attachCopy.CaseDef(cd)(pat, guard, Block(caseStats, Assign(Ident(varDef.name), caseExpr))) @@ -141,16 +141,14 @@ private[async] final case class AnfTransform[C <: Context](override val c: C) ex case stats :+ expr => Block(stats, expr) } - def liftedName(prefix: String) = c.fresh(prefix + "$") - private def defineVar(prefix: String, tp: Type, pos: Position): ValDef = { - val vd = ValDef(Modifiers(Flag.MUTABLE), liftedName(prefix), TypeTree(tp), defaultValue(tp)) + val vd = ValDef(Modifiers(Flag.MUTABLE), name.fresh(prefix), TypeTree(tp), defaultValue(tp)) vd.setPos(pos) vd } private def defineVal(prefix: String, lhs: Tree, pos: Position): ValDef = { - val vd = ValDef(NoMods, liftedName(prefix), TypeTree(), lhs) + val vd = ValDef(NoMods, name.fresh(prefix), TypeTree(), lhs) vd.setPos(pos) vd } diff --git a/src/main/scala/scala/async/ExprBuilder.scala b/src/main/scala/scala/async/ExprBuilder.scala index d716163..3a8a649 100644 --- a/src/main/scala/scala/async/ExprBuilder.scala +++ b/src/main/scala/scala/async/ExprBuilder.scala @@ -10,32 +10,14 @@ import collection.mutable /* * @author Philipp Haller */ -final case class ExprBuilder[C <: Context, FS <: FutureSystem](override val c: C, val futureSystem: FS) +private[async] final case class ExprBuilder[C <: Context, FS <: FutureSystem](override val c: C, val futureSystem: FS) extends TransformUtils(c) { builder => import c.universe._ import defn._ - private[async] object name { - def suffix(string: String) = string + "$async" - - def suffixedName(prefix: String) = newTermName(suffix(prefix)) - - val state = suffixedName("state") - val result = suffixedName("result") - val resume = suffixedName("resume") - val execContext = suffixedName("execContext") - - // TODO do we need to freshen any of these? - val x1 = newTermName("x$1") - val tr = newTermName("tr") - val onCompleteHandler = suffixedName("onCompleteHandler") - - def fresh(name: TermName) = if (name.toString.contains("$")) name else newTermName(c.fresh("" + name + "$")) - } - - private[async] lazy val futureSystemOps = futureSystem.mkOps(c) + lazy val futureSystemOps = futureSystem.mkOps(c) private def resetDuplicate(tree: Tree) = c.resetAllAttrs(tree.duplicate) diff --git a/src/main/scala/scala/async/TransformUtils.scala b/src/main/scala/scala/async/TransformUtils.scala index 5095875..9a8814f 100644 --- a/src/main/scala/scala/async/TransformUtils.scala +++ b/src/main/scala/scala/async/TransformUtils.scala @@ -9,10 +9,34 @@ import reflect.ClassTag /** * Utilities used in both `ExprBuilder` and `AnfTransform`. */ -class TransformUtils[C <: Context](val c: C) { +private[async] class TransformUtils[C <: Context](val c: C) { import c.universe._ + private[async] object name { + def suffix(string: String) = string + "$async" + + def suffixedName(prefix: String) = newTermName(suffix(prefix)) + + val state = suffixedName("state") + val result = suffixedName("result") + val resume = suffixedName("resume") + val execContext = suffixedName("execContext") + + // TODO do we need to freshen any of these? + val x1 = newTermName("x$1") + val tr = newTermName("tr") + val onCompleteHandler = suffixedName("onCompleteHandler") + + val matchRes = "matchres" + val ifRes = "ifres" + val await = "await" + + def fresh(name: TermName): TermName = newTermName(fresh(name.toString)) + + def fresh(name: String): String = if (name.toString.contains("$")) name else c.fresh("" + name + "$") + } + protected def defaultValue(tpe: Type): Literal = { val defaultValue: Any = if (tpe <:< definitions.BooleanTpe) false |