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/dart/ast/ast.dart'; |
| 6 import 'package:analyzer/dart/ast/visitor.dart'; |
| 7 import 'package:analyzer/dart/element/element.dart'; |
6 import 'package:analyzer/src/generated/constant.dart'; | 8 import 'package:analyzer/src/generated/constant.dart'; |
7 import 'package:analyzer/src/generated/element.dart'; | |
8 import 'package:analyzer/src/generated/error.dart' | 9 import 'package:analyzer/src/generated/error.dart' |
9 show AnalysisErrorListener, ErrorReporter; | 10 show AnalysisErrorListener, ErrorReporter; |
10 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 11 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
11 import 'package:analyzer/src/generated/source.dart' show Source; | 12 import 'package:analyzer/src/generated/source.dart' show Source; |
12 | 13 |
13 /// True is the expression can be evaluated multiple times without causing | 14 /// True is the expression can be evaluated multiple times without causing |
14 /// code execution. This is true for final fields. This can be true for local | 15 /// code execution. This is true for final fields. This can be true for local |
15 /// variables, if: | 16 /// variables, if: |
16 /// * they are not assigned within the [context]. | 17 /// * they are not assigned within the [context]. |
17 /// * they are not assigned in a function closure anywhere. | 18 /// * they are not assigned in a function closure anywhere. |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 108 |
108 // TODO(jmesserly): this is used to determine if the field initialization is | 109 // TODO(jmesserly): this is used to determine if the field initialization is |
109 // side effect free. We should make the check more general, as things like | 110 // side effect free. We should make the check more general, as things like |
110 // list/map literals/regexp are also side effect free and fairly common | 111 // list/map literals/regexp are also side effect free and fairly common |
111 // to use as field initializers. | 112 // to use as field initializers. |
112 bool isFieldInitConstant(VariableDeclaration field) => | 113 bool isFieldInitConstant(VariableDeclaration field) => |
113 field.initializer == null || computeConstant(field) != null; | 114 field.initializer == null || computeConstant(field) != null; |
114 | 115 |
115 DartObject computeConstant(VariableDeclaration field) { | 116 DartObject computeConstant(VariableDeclaration field) { |
116 // If the constant is already computed by ConstantEvaluator, just return it. | 117 // If the constant is already computed by ConstantEvaluator, just return it. |
117 VariableElementImpl element = field.element; | 118 VariableElement element = field.element; |
118 var result = element.evaluationResult; | 119 var result = element.constantValue; |
119 if (result != null) return result.value; | 120 if (result != null) return result; |
120 | 121 |
121 // ConstantEvaluator will not compute constants for non-const fields, | 122 // ConstantEvaluator will not compute constants for non-const fields, |
122 // so run ConstantVisitor for those to figure out if the initializer is | 123 // so run ConstantVisitor for those to figure out if the initializer is |
123 // actually a constant (and therefore side effect free to evaluate). | 124 // actually a constant (and therefore side effect free to evaluate). |
124 assert(!field.isConst); | 125 assert(!field.isConst); |
125 | 126 |
126 var initializer = field.initializer; | 127 var initializer = field.initializer; |
127 if (initializer == null) return null; | 128 if (initializer == null) return null; |
128 return initializer.accept(_constantVisitor); | 129 return initializer.accept(_constantVisitor); |
129 } | 130 } |
130 } | 131 } |
OLD | NEW |