| 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 library logical_rewriter; | 5 part of tree_ir.optimization; |
| 6 | |
| 7 import '../constants/values.dart' as values; | |
| 8 import '../tree_ir/tree_ir_nodes.dart'; | |
| 9 | 6 |
| 10 /// Rewrites logical expressions to be more compact in the Tree IR. | 7 /// Rewrites logical expressions to be more compact in the Tree IR. |
| 11 /// | 8 /// |
| 12 /// 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 |
| 13 /// its result is immediately applied to boolean conversion. | 10 /// its result is immediately applied to boolean conversion. |
| 14 /// | 11 /// |
| 15 /// IF STATEMENTS: | 12 /// IF STATEMENTS: |
| 16 /// | 13 /// |
| 17 /// We apply the following two rules to [If] statements (see [visitIf]). | 14 /// We apply the following two rules to [If] statements (see [visitIf]). |
| 18 /// | 15 /// |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 /// | 47 /// |
| 51 /// 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) |
| 52 /// | 49 /// |
| 53 /// The following sequence of rewrites demonstrates the merit of these rules: | 50 /// The following sequence of rewrites demonstrates the merit of these rules: |
| 54 /// | 51 /// |
| 55 /// x ? (y ? true : false) : false | 52 /// x ? (y ? true : false) : false |
| 56 /// x ? !!y : false (double negation introduced by [toBoolean]) | 53 /// x ? !!y : false (double negation introduced by [toBoolean]) |
| 57 /// x && !!y (!!y validated by [isBooleanValued]) | 54 /// x && !!y (!!y validated by [isBooleanValued]) |
| 58 /// x && y (double negation removed by [putInBooleanContext]) | 55 /// x && y (double negation removed by [putInBooleanContext]) |
| 59 /// | 56 /// |
| 60 class LogicalRewriter extends Visitor<Statement, Expression> { | 57 class LogicalRewriter extends Visitor<Statement, Expression> implements Pass { |
| 61 | 58 |
| 62 /// Statement to be executed next by natural fallthrough. Although fallthrough | 59 /// Statement to be executed next by natural fallthrough. Although fallthrough |
| 63 /// 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 |
| 64 /// evaluating the benefit of swapping the branches of an [If]. | 61 /// evaluating the benefit of swapping the branches of an [If]. |
| 65 Statement fallthrough; | 62 Statement fallthrough; |
| 66 | 63 |
| 67 void rewrite(FunctionDefinition definition) { | 64 void rewrite(FunctionDefinition definition) { |
| 68 if (definition.isAbstract) return; | 65 if (definition.isAbstract) return; |
| 69 | 66 |
| 70 definition.body = visitStatement(definition.body); | 67 definition.body = visitStatement(definition.body); |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 } | 441 } |
| 445 | 442 |
| 446 /// Destructively updates each entry of [l] with the result of visiting it. | 443 /// Destructively updates each entry of [l] with the result of visiting it. |
| 447 void _rewriteList(List<Expression> l) { | 444 void _rewriteList(List<Expression> l) { |
| 448 for (int i = 0; i < l.length; i++) { | 445 for (int i = 0; i < l.length; i++) { |
| 449 l[i] = visitExpression(l[i]); | 446 l[i] = visitExpression(l[i]); |
| 450 } | 447 } |
| 451 } | 448 } |
| 452 } | 449 } |
| 453 | 450 |
| OLD | NEW |