OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library cps_ir.parent_visitor; |
| 6 |
| 7 import 'cps_ir_nodes.dart'; |
| 8 |
| 9 /// Traverses the CPS term and sets node.parent for each visited node. |
| 10 class ParentVisitor extends DeepRecursiveVisitor { |
| 11 static void setParents(Node node) { |
| 12 ParentVisitor visitor = new ParentVisitor._make(); |
| 13 visitor._worklist.add(node); |
| 14 visitor.trampoline(); |
| 15 } |
| 16 |
| 17 /// Private to avoid accidental `new ParentVisitor().visit(node)` calls. |
| 18 ParentVisitor._make(); |
| 19 |
| 20 Node _parent; |
| 21 final List<Node> _worklist = <Node>[]; |
| 22 |
| 23 void trampoline() { |
| 24 while (_worklist.isNotEmpty) { |
| 25 _parent = _worklist.removeLast(); |
| 26 _parent.accept(this); |
| 27 } |
| 28 } |
| 29 |
| 30 @override |
| 31 visit(Node node) { |
| 32 _worklist.add(node); |
| 33 assert(_parent != node); |
| 34 assert(_parent != null); |
| 35 node.parent = _parent; |
| 36 } |
| 37 |
| 38 @override |
| 39 processReference(Reference node) { |
| 40 node.parent = _parent; |
| 41 } |
| 42 } |
| 43 |
OLD | NEW |