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 import 'package:analyzer/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
6 import 'package:analyzer/dart/ast/visitor.dart'; | 6 import 'package:analyzer/dart/ast/visitor.dart'; |
7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
8 import 'package:analyzer/src/generated/constant.dart'; | 8 import 'package:analyzer/src/generated/constant.dart'; |
9 import 'package:analyzer/src/generated/error.dart' | 9 import 'package:analyzer/src/generated/error.dart' |
10 show AnalysisErrorListener, ErrorReporter; | 10 show AnalysisErrorListener, ErrorReporter; |
11 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 11 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
12 import 'package:analyzer/src/generated/source.dart' show Source; | 12 import 'package:analyzer/src/generated/source.dart' show Source; |
| 13 import 'package:analyzer/src/dart/ast/ast.dart'; |
13 | 14 |
14 /// True is the expression can be evaluated multiple times without causing | 15 /// True is the expression can be evaluated multiple times without causing |
15 /// code execution. This is true for final fields. This can be true for local | 16 /// code execution. This is true for final fields. This can be true for local |
16 /// variables, if: | 17 /// variables, if: |
17 /// * they are not assigned within the [context]. | 18 /// * they are not assigned within the [context]. |
18 /// * they are not assigned in a function closure anywhere. | 19 /// * they are not assigned in a function closure anywhere. |
19 /// True is the expression can be evaluated multiple times without causing | 20 /// True is the expression can be evaluated multiple times without causing |
20 /// code execution. This is true for final fields. This can be true for local | 21 /// code execution. This is true for final fields. This can be true for local |
21 /// variables, if: | 22 /// variables, if: |
22 /// | 23 /// |
(...skipping 24 matching lines...) Expand all Loading... |
47 } | 48 } |
48 } | 49 } |
49 } | 50 } |
50 return false; | 51 return false; |
51 } | 52 } |
52 | 53 |
53 /// Returns true if the local variable is potentially mutated within [context]. | 54 /// Returns true if the local variable is potentially mutated within [context]. |
54 /// This accounts for closures that may have been created outside of [context]. | 55 /// This accounts for closures that may have been created outside of [context]. |
55 bool _isPotentiallyMutated(FunctionBody function, VariableElement e, | 56 bool _isPotentiallyMutated(FunctionBody function, VariableElement e, |
56 [AstNode context]) { | 57 [AstNode context]) { |
| 58 if (function is FunctionBodyImpl && function.localVariableInfo == null) { |
| 59 // TODO(jmesserly): this is a caching bug in Analyzer. They don't restore |
| 60 // this info in some cases. |
| 61 return true; |
| 62 } |
| 63 |
57 if (function.isPotentiallyMutatedInClosure(e)) return true; | 64 if (function.isPotentiallyMutatedInClosure(e)) return true; |
58 if (function.isPotentiallyMutatedInScope(e)) { | 65 if (function.isPotentiallyMutatedInScope(e)) { |
59 // Need to visit the context looking for assignment to this local. | 66 // Need to visit the context looking for assignment to this local. |
60 if (context != null) { | 67 if (context != null) { |
61 var visitor = new _AssignmentFinder(e); | 68 var visitor = new _AssignmentFinder(e); |
62 context.accept(visitor); | 69 context.accept(visitor); |
63 return visitor._potentiallyMutated; | 70 return visitor._potentiallyMutated; |
64 } | 71 } |
65 return true; | 72 return true; |
66 } | 73 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // ConstantEvaluator will not compute constants for non-const fields, | 129 // ConstantEvaluator will not compute constants for non-const fields, |
123 // so run ConstantVisitor for those to figure out if the initializer is | 130 // so run ConstantVisitor for those to figure out if the initializer is |
124 // actually a constant (and therefore side effect free to evaluate). | 131 // actually a constant (and therefore side effect free to evaluate). |
125 assert(!field.isConst); | 132 assert(!field.isConst); |
126 | 133 |
127 var initializer = field.initializer; | 134 var initializer = field.initializer; |
128 if (initializer == null) return null; | 135 if (initializer == null) return null; |
129 return initializer.accept(_constantVisitor); | 136 return initializer.accept(_constantVisitor); |
130 } | 137 } |
131 } | 138 } |
OLD | NEW |