diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2012-11-23 14:38:28 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2012-11-23 14:38:28 +0100 |
commit | d2129dfbda50e83d31e300913f7845ba61d038e6 (patch) | |
tree | c9240bffb3662609b1e71c650f47f46dceaf2ea3 /src/main/scala/scala/async/TransformUtils.scala | |
parent | 848d98a89e4ff25f2d1373021bef23059ff04ab2 (diff) | |
download | scala-async-d2129dfbda50e83d31e300913f7845ba61d038e6.tar.gz scala-async-d2129dfbda50e83d31e300913f7845ba61d038e6.tar.bz2 scala-async-d2129dfbda50e83d31e300913f7845ba61d038e6.zip |
Preserve positions and attachments.
Not quite sure how to test this yet;
the original trees coming from toolbox
don't seem to come with position to start
with.
But this is a start.
Diffstat (limited to 'src/main/scala/scala/async/TransformUtils.scala')
-rw-r--r-- | src/main/scala/scala/async/TransformUtils.scala | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main/scala/scala/async/TransformUtils.scala b/src/main/scala/scala/async/TransformUtils.scala index 103c8d2..b79be87 100644 --- a/src/main/scala/scala/async/TransformUtils.scala +++ b/src/main/scala/scala/async/TransformUtils.scala @@ -4,6 +4,7 @@ package scala.async import scala.reflect.macros.Context +import reflect.ClassTag /** * Utilities used in both `ExprBuilder` and `AnfTransform`. @@ -117,4 +118,43 @@ class TransformUtils[C <: Context](val c: C) { case s: SymTree if s.symbol.isMethod => s.symbol }.headOption.getOrElse(sys.error(s"Unable to find a method symbol in ${apply.tree}")) } + + /** Using [[scala.reflect.api.Trees.TreeCopier]] copies more than we would like: + * we don't want to copy types and symbols to the new trees in some cases. + * + * Instead, we just copy positions and attachments. + */ + object attachCopy { + def copyAttach[T <: Tree](orig: Tree, tree: T): tree.type = { + tree.setPos(orig.pos) + for (att <- orig.attachments.all) + tree.updateAttachment[Any](att)(ClassTag.apply[Any](att.getClass)) + tree + } + + def Apply(tree: Tree)(fun: Tree, args: List[Tree]): Apply = + copyAttach(tree, c.universe.Apply(fun, args)) + + def Assign(tree: Tree)(lhs: Tree, rhs: Tree): Assign = + copyAttach(tree, c.universe.Assign(lhs, rhs)) + + def CaseDef(tree: Tree)(pat: Tree, guard: Tree, block: Tree): CaseDef = + copyAttach(tree, c.universe.CaseDef(pat, guard, block)) + + def If(tree: Tree)(cond: Tree, thenp: Tree, elsep: Tree): If = + copyAttach(tree, c.universe.If(cond, thenp, elsep)) + + def Match(tree: Tree)(selector: Tree, cases: List[CaseDef]): Match = + copyAttach(tree, c.universe.Match(selector, cases)) + + def Select(tree: Tree)(qual: Tree, name: Name): Select = + copyAttach(tree, c.universe.Select(qual, name)) + + def TypeApply(tree: Tree)(fun: Tree, args: List[Tree]): TypeApply = { + copyAttach(tree, c.universe.TypeApply(fun, args)) + } + + def ValDef(tree: Tree)(mods: Modifiers, name: TermName, tpt: Tree, rhs: Tree): ValDef = + copyAttach(tree, c.universe.ValDef(mods, name, tpt, rhs)) + } } |