OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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 dev_compiler.src.codegen.side_effect_analysis; | 5 library dev_compiler.src.codegen.side_effect_analysis; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
9 | 9 |
10 /// True is the expression can be evaluated multiple times without causing | 10 /// True is the expression can be evaluated multiple times without causing |
(...skipping 10 matching lines...) Expand all Loading... |
21 /// | 21 /// |
22 /// This method is used to avoid creating temporaries in cases where we know | 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 | 23 /// we can safely re-evaluate [node] multiple times in [context]. This lets |
24 /// us generate prettier code. | 24 /// us generate prettier code. |
25 /// | 25 /// |
26 /// This method is conservative: it should never return `true` unless it is | 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 | 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 | 28 /// correctness of a `true` value. However it may return `false` for things |
29 /// that are in fact, stateless. | 29 /// that are in fact, stateless. |
30 bool isStateless(Expression node, [AstNode context]) { | 30 bool isStateless(Expression node, [AstNode context]) { |
| 31 // `this` and `super` cannot be reassigned. |
| 32 if (node is ThisExpression || node is SuperExpression) return true; |
31 if (node is SimpleIdentifier) { | 33 if (node is SimpleIdentifier) { |
32 var e = node.staticElement; | 34 var e = node.staticElement; |
33 if (e is PropertyAccessorElement) e = e.variable; | 35 if (e is PropertyAccessorElement) e = e.variable; |
34 if (e is VariableElement && !e.isSynthetic) { | 36 if (e is VariableElement && !e.isSynthetic) { |
35 if (e.isFinal) return true; | 37 if (e.isFinal) return true; |
36 if (e is LocalVariableElement || e is ParameterElement) { | 38 if (e is LocalVariableElement || e is ParameterElement) { |
37 // make sure the local isn't mutated in the context. | 39 // make sure the local isn't mutated in the context. |
38 return !_isPotentiallyMutated(e, context); | 40 return !_isPotentiallyMutated(e, context); |
39 } | 41 } |
40 } | 42 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 if (parent is MethodInvocation && | 79 if (parent is MethodInvocation && |
78 identical(parent.methodName, node)) return; | 80 identical(parent.methodName, node)) return; |
79 if (parent is ConstructorName) return; | 81 if (parent is ConstructorName) return; |
80 if (parent is Label) return; | 82 if (parent is Label) return; |
81 | 83 |
82 if (node.inSetterContext() && node.staticElement == _variable) { | 84 if (node.inSetterContext() && node.staticElement == _variable) { |
83 _potentiallyMutated = true; | 85 _potentiallyMutated = true; |
84 } | 86 } |
85 } | 87 } |
86 } | 88 } |
OLD | NEW |