From c208fe98d47b64b480d64a065861f9c115c9932e Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 16 Mar 2014 17:00:22 +0100 Subject: Refactor TreeInfo#defPath Uses a TreeAccumulator, rather than ad-hoc descent through `productIterator`. --- src/dotty/tools/dotc/ast/TreeInfo.scala | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) (limited to 'src/dotty/tools/dotc/ast/TreeInfo.scala') 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 -- cgit v1.2.3