diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2014-03-16 17:00:22 +0100 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2014-03-16 17:00:51 +0100 |
commit | c208fe98d47b64b480d64a065861f9c115c9932e (patch) | |
tree | 72bfdde373059a8835d2adf268542620a6512aa2 | |
parent | fd1214cfff0bf8a257a85e1ef44047ed6de000a2 (diff) | |
download | dotty-c208fe98d47b64b480d64a065861f9c115c9932e.tar.gz dotty-c208fe98d47b64b480d64a065861f9c115c9932e.tar.bz2 dotty-c208fe98d47b64b480d64a065861f9c115c9932e.zip |
Refactor TreeInfo#defPath
Uses a TreeAccumulator, rather than ad-hoc descent through `productIterator`.
-rw-r--r-- | src/dotty/tools/dotc/ast/TreeInfo.scala | 33 |
1 files changed, 11 insertions, 22 deletions
diff --git a/src/dotty/tools/dotc/ast/TreeInfo.scala b/src/dotty/tools/dotc/ast/TreeInfo.scala index a1dd37e27..734963ea3 100644 --- a/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -421,30 +421,19 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] => * Pre: `sym` must have a position. */ def defPath(sym: Symbol, root: Tree)(implicit ctx: Context): List[Tree] = ctx.debugTraceIndented(s"defpath($sym with position ${sym.pos}, ${root.show})") { - def show(from: Any): String = from match { - case tree: Trees.Tree[_] => s"${tree.show} with attachments ${tree.allAttachments}" - case x: printing.Showable => x.show - case x => x.toString - } - - def search(from: Any): List[Tree] = ctx.debugTraceIndented(s"search(${show(from)})") { - from match { - case tree: Tree => // Dotty problem: cannot write Tree @ unchecked, this currently gives a syntax error - if (definedSym(tree) == sym) tree :: Nil - else if (tree.envelope.contains(sym.pos)) { - val p = search(tree.productIterator) - if (p.isEmpty) p else tree :: p - } else Nil - case xs: Iterable[_] => - search(xs.iterator) - case xs: Iterator[_] => - xs.map(search).find(_.nonEmpty).getOrElse(Nil) - case _ => - Nil + require(sym.pos.exists) + object accum extends TreeAccumulator[List[Tree]] { + def apply(x: List[Tree], tree: Tree): List[Tree] = { + if (tree.envelope.contains(sym.pos)) + if (definedSym(tree) == sym) tree :: x + else { + val x1 = foldOver(x, tree) + if (x1 ne x) tree :: x1 else x1 + } + else x } } - require(sym.pos.exists) - search(root) + accum(Nil, root) } /** The statement sequence that contains a definition of `sym`, or Nil |