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/src/generated/ast.dart'; | 5 import 'package:analyzer/src/generated/ast.dart'; |
6 import 'package:analyzer/src/generated/constant.dart'; | 6 import 'package:analyzer/src/generated/constant.dart'; |
7 import 'package:analyzer/src/generated/element.dart'; | 7 import 'package:analyzer/src/generated/element.dart'; |
8 import 'package:analyzer/src/generated/error.dart' show ErrorReporter; | 8 import 'package:analyzer/src/generated/error.dart' |
9 import 'package:analyzer/src/generated/engine.dart' show RecordingErrorListener; | 9 show AnalysisErrorListener, ErrorReporter; |
10 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 10 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
| 11 import 'package:analyzer/src/generated/source.dart' show Source; |
11 | 12 |
12 /// True is the expression can be evaluated multiple times without causing | 13 /// True is the expression can be evaluated multiple times without causing |
13 /// code execution. This is true for final fields. This can be true for local | 14 /// code execution. This is true for final fields. This can be true for local |
14 /// variables, if: | 15 /// variables, if: |
15 /// * they are not assigned within the [context]. | 16 /// * they are not assigned within the [context]. |
16 /// * they are not assigned in a function closure anywhere. | 17 /// * they are not assigned in a function closure anywhere. |
17 /// True is the expression can be evaluated multiple times without causing | 18 /// True is the expression can be evaluated multiple times without causing |
18 /// code execution. This is true for final fields. This can be true for local | 19 /// code execution. This is true for final fields. This can be true for local |
19 /// variables, if: | 20 /// variables, if: |
20 /// | 21 /// |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 | 91 |
91 if (node.inSetterContext() && node.staticElement == _variable) { | 92 if (node.inSetterContext() && node.staticElement == _variable) { |
92 _potentiallyMutated = true; | 93 _potentiallyMutated = true; |
93 } | 94 } |
94 } | 95 } |
95 } | 96 } |
96 | 97 |
97 class ConstFieldVisitor { | 98 class ConstFieldVisitor { |
98 final ConstantVisitor _constantVisitor; | 99 final ConstantVisitor _constantVisitor; |
99 | 100 |
100 ConstFieldVisitor(TypeProvider types, CompilationUnit unit) | 101 ConstFieldVisitor(TypeProvider types, Source source) |
101 // TODO(jmesserly): support -D variables on the command line | 102 // TODO(jmesserly): support -D variables on the command line |
102 : _constantVisitor = new ConstantVisitor( | 103 : _constantVisitor = new ConstantVisitor( |
103 new ConstantEvaluationEngine(types, new DeclaredVariables()), | 104 new ConstantEvaluationEngine(types, new DeclaredVariables()), |
104 new ErrorReporter( | 105 new ErrorReporter(AnalysisErrorListener.NULL_LISTENER, source)); |
105 new RecordingErrorListener(), unit.element.source)); | |
106 | 106 |
107 // TODO(jmesserly): this is used to determine if the field initialization is | 107 // TODO(jmesserly): this is used to determine if the field initialization is |
108 // side effect free. We should make the check more general, as things like | 108 // side effect free. We should make the check more general, as things like |
109 // list/map literals/regexp are also side effect free and fairly common | 109 // list/map literals/regexp are also side effect free and fairly common |
110 // to use as field initializers. | 110 // to use as field initializers. |
111 bool isFieldInitConstant(VariableDeclaration field) => | 111 bool isFieldInitConstant(VariableDeclaration field) => |
112 field.initializer == null || computeConstant(field) != null; | 112 field.initializer == null || computeConstant(field) != null; |
113 | 113 |
114 DartObject computeConstant(VariableDeclaration field) { | 114 DartObject computeConstant(VariableDeclaration field) { |
115 // If the constant is already computed by ConstantEvaluator, just return it. | 115 // If the constant is already computed by ConstantEvaluator, just return it. |
116 VariableElementImpl element = field.element; | 116 VariableElementImpl element = field.element; |
117 var result = element.evaluationResult; | 117 var result = element.evaluationResult; |
118 if (result != null) return result.value; | 118 if (result != null) return result.value; |
119 | 119 |
120 // ConstantEvaluator will not compute constants for non-const fields, | 120 // ConstantEvaluator will not compute constants for non-const fields, |
121 // so run ConstantVisitor for those to figure out if the initializer is | 121 // so run ConstantVisitor for those to figure out if the initializer is |
122 // actually a constant (and therefore side effect free to evaluate). | 122 // actually a constant (and therefore side effect free to evaluate). |
123 assert(!field.isConst); | 123 assert(!field.isConst); |
124 | 124 |
125 var initializer = field.initializer; | 125 var initializer = field.initializer; |
126 if (initializer == null) return null; | 126 if (initializer == null) return null; |
127 return initializer.accept(_constantVisitor); | 127 return initializer.accept(_constantVisitor); |
128 } | 128 } |
129 } | 129 } |
OLD | NEW |