Chromium Code Reviews| Index: pkg/compiler/lib/src/js_backend/codegen/unsugar.dart |
| diff --git a/pkg/compiler/lib/src/js_backend/codegen/unsugar.dart b/pkg/compiler/lib/src/js_backend/codegen/unsugar.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ec8303d256f016fb75518c73e45a3327c61635e3 |
| --- /dev/null |
| +++ b/pkg/compiler/lib/src/js_backend/codegen/unsugar.dart |
| @@ -0,0 +1,48 @@ |
| +library dart2js.unsugar_cps; |
| + |
| +import '../../cps_ir/cps_ir_nodes.dart'; |
| + |
| +// TODO(karlklose): share the [ParentVisitor]. |
| +import 'package:compiler/src/cps_ir/optimizers.dart'; |
| + |
| +class Boolify extends IsTrue { |
| + Boolify(Primitive expression) : super(expression); |
| + accept(Visitor visitor) => visitor.visitBoolify(this); |
| +} |
| + |
| +/// Rewrites the initial CPS IR to make Dart semantics explicit. |
| +/// |
| +/// Performs the following rewrites: |
| +/// - Branch(IsTrue(v), c1, c2) -> Branch(Boolify(v), c1, c2) |
| +class UnsugarVisitor extends RecursiveVisitor { |
| + const UnsugarVisitor(); |
| + |
| + void rewrite(FunctionDefinition function) { |
| + // Set all parent pointers. |
| + new ParentVisitor().visit(function); |
| + visit(function); |
| + } |
| + |
| + @override |
| + Node visit(Node node) { |
| + Node result = node.accept(this); |
| + return result != null ? result : node; |
| + } |
| + |
| + visitBranch(Branch node) { |
| + // Create new node. |
| + IsTrue condition = node.condition; |
| + Branch newNode = new Branch( |
| + new Boolify(condition.value.definition), |
| + node.trueContinuation.definition, |
| + node.falseContinuation.definition); |
| + // Replace the node in the parent context. |
| + InteriorNode parent = node.parent; |
| + parent.body = newNode; |
| + newNode.parent = parent; |
| + // Remove stale referenced. |
|
sigurdm
2014/11/20 12:47:28
reference*s*
|
| + condition.value.unlink(); |
| + node.trueContinuation.unlink(); |
| + node.falseContinuation.unlink(); |
| + } |
| +} |