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 dev_compiler.src.codegen.side_effect_analysis; |
| 6 |
| 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 |
| 10 /// True is the expression can be evaluated multiple times without causing |
| 11 /// code execution. This is true for final fields. This can be true for local |
| 12 /// variables, if: |
| 13 /// * they are not assigned within the [context]. |
| 14 /// * they are not assigned in a function closure anywhere. |
| 15 /// True is the expression can be evaluated multiple times without causing |
| 16 /// code execution. This is true for final fields. This can be true for local |
| 17 /// variables, if: |
| 18 /// |
| 19 /// * they are not assigned within the [context] scope. |
| 20 /// * they are not assigned in a function closure anywhere. |
| 21 /// |
| 22 /// This method is used to avoid creating temporaries in cases where we know |
| 23 /// we can safely re-evaluate [node] multiple times in [context]. This lets |
| 24 /// us generate prettier code. |
| 25 /// |
| 26 /// This method is conservative: it should never return `true` unless it is |
| 27 /// certain the [node] is stateless, because generated code may rely on the |
| 28 /// correctness of a `true` value. However it may return `false` for things |
| 29 /// that are in fact, stateless. |
| 30 bool isStateless(Expression node, [AstNode context]) { |
| 31 if (node is SimpleIdentifier) { |
| 32 var e = node.staticElement; |
| 33 if (e is PropertyAccessorElement) e = e.variable; |
| 34 if (e is VariableElement && !e.isSynthetic) { |
| 35 if (e.isFinal) return true; |
| 36 if (e is LocalVariableElement || e is ParameterElement) { |
| 37 // make sure the local isn't mutated in the context. |
| 38 return !_isPotentiallyMutated(e, context); |
| 39 } |
| 40 } |
| 41 } |
| 42 return false; |
| 43 } |
| 44 |
| 45 /// Returns true if the local variable is potentially mutated within [context]. |
| 46 /// This accounts for closures that may have been created outside of [context]. |
| 47 bool _isPotentiallyMutated(VariableElement e, [AstNode context]) { |
| 48 if (e.isPotentiallyMutatedInClosure) return true; |
| 49 if (e.isPotentiallyMutatedInScope) { |
| 50 // Need to visit the context looking for assignment to this local. |
| 51 if (context != null) { |
| 52 var visitor = new _AssignmentFinder(e); |
| 53 context.accept(visitor); |
| 54 return visitor._potentiallyMutated; |
| 55 } |
| 56 return true; |
| 57 } |
| 58 return false; |
| 59 } |
| 60 |
| 61 /// Adapted from VariableResolverVisitor. Finds an assignment to a given |
| 62 /// local variable. |
| 63 class _AssignmentFinder extends RecursiveAstVisitor { |
| 64 final VariableElement _variable; |
| 65 bool _potentiallyMutated = false; |
| 66 |
| 67 _AssignmentFinder(this._variable); |
| 68 |
| 69 @override |
| 70 visitSimpleIdentifier(SimpleIdentifier node) { |
| 71 // Ignore if qualified. |
| 72 AstNode parent = node.parent; |
| 73 if (parent is PrefixedIdentifier && |
| 74 identical(parent.identifier, node)) return; |
| 75 if (parent is PropertyAccess && |
| 76 identical(parent.propertyName, node)) return; |
| 77 if (parent is MethodInvocation && |
| 78 identical(parent.methodName, node)) return; |
| 79 if (parent is ConstructorName) return; |
| 80 if (parent is Label) return; |
| 81 |
| 82 if (node.inSetterContext() && node.staticElement == _variable) { |
| 83 _potentiallyMutated = true; |
| 84 } |
| 85 } |
| 86 } |
OLD | NEW |