| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of tree_ir.optimization; | 5 part of tree_ir.optimization; |
| 6 | 6 |
| 7 /// Rewrites logical expressions to be more compact in the Tree IR. | 7 /// Rewrites logical expressions to be more compact in the Tree IR. |
| 8 /// | 8 /// |
| 9 /// In this class an expression is said to occur in "boolean context" if | 9 /// In this class an expression is said to occur in "boolean context" if |
| 10 /// its result is immediately applied to boolean conversion. | 10 /// its result is immediately applied to boolean conversion. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 /// | 47 /// |
| 48 /// x ? y : false ==> x && y (if y is known to be a boolean) | 48 /// x ? y : false ==> x && y (if y is known to be a boolean) |
| 49 /// | 49 /// |
| 50 /// The following sequence of rewrites demonstrates the merit of these rules: | 50 /// The following sequence of rewrites demonstrates the merit of these rules: |
| 51 /// | 51 /// |
| 52 /// x ? (y ? true : false) : false | 52 /// x ? (y ? true : false) : false |
| 53 /// x ? !!y : false (double negation introduced by [toBoolean]) | 53 /// x ? !!y : false (double negation introduced by [toBoolean]) |
| 54 /// x && !!y (!!y validated by [isBooleanValued]) | 54 /// x && !!y (!!y validated by [isBooleanValued]) |
| 55 /// x && y (double negation removed by [putInBooleanContext]) | 55 /// x && y (double negation removed by [putInBooleanContext]) |
| 56 /// | 56 /// |
| 57 class LogicalRewriter extends Visitor<Statement, Expression> implements Pass { | 57 class LogicalRewriter extends Visitor<Statement, Expression> with PassMixin { |
| 58 | 58 |
| 59 /// Statement to be executed next by natural fallthrough. Although fallthrough | 59 /// Statement to be executed next by natural fallthrough. Although fallthrough |
| 60 /// is not introduced in this phase, we need to reason about fallthrough when | 60 /// is not introduced in this phase, we need to reason about fallthrough when |
| 61 /// evaluating the benefit of swapping the branches of an [If]. | 61 /// evaluating the benefit of swapping the branches of an [If]. |
| 62 Statement fallthrough; | 62 Statement fallthrough; |
| 63 | 63 |
| 64 void rewrite(FunctionDefinition definition) { | 64 void rewriteExecutableDefinition(ExecutableDefinition root) { |
| 65 if (definition.isAbstract) return; | 65 root.body = visitStatement(root.body); |
| 66 | |
| 67 definition.body = visitStatement(definition.body); | |
| 68 } | 66 } |
| 69 | 67 |
| 70 Statement visitLabeledStatement(LabeledStatement node) { | 68 Statement visitLabeledStatement(LabeledStatement node) { |
| 71 Statement savedFallthrough = fallthrough; | 69 Statement savedFallthrough = fallthrough; |
| 72 fallthrough = node.next; | 70 fallthrough = node.next; |
| 73 node.body = visitStatement(node.body); | 71 node.body = visitStatement(node.body); |
| 74 fallthrough = savedFallthrough; | 72 fallthrough = savedFallthrough; |
| 75 node.next = visitStatement(node.next); | 73 node.next = visitStatement(node.next); |
| 76 return node; | 74 return node; |
| 77 } | 75 } |
| (...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 } | 439 } |
| 442 | 440 |
| 443 /// Destructively updates each entry of [l] with the result of visiting it. | 441 /// Destructively updates each entry of [l] with the result of visiting it. |
| 444 void _rewriteList(List<Expression> l) { | 442 void _rewriteList(List<Expression> l) { |
| 445 for (int i = 0; i < l.length; i++) { | 443 for (int i = 0; i < l.length; i++) { |
| 446 l[i] = visitExpression(l[i]); | 444 l[i] = visitExpression(l[i]); |
| 447 } | 445 } |
| 448 } | 446 } |
| 449 } | 447 } |
| 450 | 448 |
| OLD | NEW |