66
77package viper .silver .ast
88
9+ import scala .collection .mutable
910import scala .reflect .ClassTag
1011import pretty .FastPrettyPrinter
1112import utility ._
@@ -48,7 +49,7 @@ Some design choices:
4849 * Note that all but Program are transitive subtypes of `Node` via `Hashable`. The reason is
4950 * that AST node hashes may depend on the entire program, not just their sub-AST.
5051 */
51- trait Node extends Traversable [Node ] with Rewritable {
52+ trait Node extends Iterable [Node ] with Rewritable {
5253
5354 /** @see [[Nodes.subnodes() ]] */
5455 def subnodes = Nodes .subnodes(this )
@@ -61,8 +62,18 @@ trait Node extends Traversable[Node] with Rewritable {
6162 Visitor .reduceWithContext(this , Nodes .subnodes)(context, enter, combine)
6263 }
6364
65+ /** Apply the given function on all the subnodes, avoiding the performance costs of the `iterator` method. */
6466 override def foreach [A ](f : Node => A ) = Visitor .visit(this , Nodes .subnodes) { case a : Node => f(a) }
6567
68+ /** Builds a new collection with all the AST nodes and returns an iterator over it. */
69+ def iterator : Iterator [Node ] = {
70+ val elements = mutable.Queue .empty[Node ]
71+ for (x <- this ) {
72+ elements.append(x)
73+ }
74+ elements.iterator
75+ }
76+
6677 /** @see [[Visitor.visit() ]] */
6778 def visit [A ](f : PartialFunction [Node , A ]): Unit = {
6879 Visitor .visit(this , Nodes .subnodes)(f)
0 commit comments