Index: pkg/compiler/lib/src/cps_ir/parent_visitor.dart |
diff --git a/pkg/compiler/lib/src/cps_ir/parent_visitor.dart b/pkg/compiler/lib/src/cps_ir/parent_visitor.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..33896e0e834e96e7fd73b805f98d44c10d28590f |
--- /dev/null |
+++ b/pkg/compiler/lib/src/cps_ir/parent_visitor.dart |
@@ -0,0 +1,43 @@ |
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+library cps_ir.parent_visitor; |
+ |
+import 'cps_ir_nodes.dart'; |
+ |
+/// Traverses the CPS term and sets node.parent for each visited node. |
+class ParentVisitor extends DeepRecursiveVisitor { |
+ static void setParents(Node node) { |
+ ParentVisitor visitor = new ParentVisitor._make(); |
+ visitor._worklist.add(node); |
+ visitor.trampoline(); |
+ } |
+ |
+ /// Private to avoid accidental `new ParentVisitor().visit(node)` calls. |
+ ParentVisitor._make(); |
+ |
+ Node _parent; |
+ final List<Node> _worklist = <Node>[]; |
+ |
+ void trampoline() { |
+ while (_worklist.isNotEmpty) { |
+ _parent = _worklist.removeLast(); |
+ _parent.accept(this); |
+ } |
+ } |
+ |
+ @override |
+ visit(Node node) { |
+ _worklist.add(node); |
+ assert(_parent != node); |
+ assert(_parent != null); |
+ node.parent = _parent; |
+ } |
+ |
+ @override |
+ processReference(Reference node) { |
+ node.parent = _parent; |
+ } |
+} |
+ |