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/constant.dart'; | 8 import 'package:analyzer/src/generated/constant.dart'; |
9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
10 import 'package:analyzer/src/generated/error.dart' show ErrorReporter; | 10 import 'package:analyzer/src/generated/error.dart' show ErrorReporter; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 new ErrorReporter( | 102 new ErrorReporter( |
103 new RecordingErrorListener(), unit.element.source)); | 103 new RecordingErrorListener(), unit.element.source)); |
104 | 104 |
105 // TODO(jmesserly): this is used to determine if the field initialization is | 105 // TODO(jmesserly): this is used to determine if the field initialization is |
106 // side effect free. We should make the check more general, as things like | 106 // side effect free. We should make the check more general, as things like |
107 // list/map literals/regexp are also side effect free and fairly common | 107 // list/map literals/regexp are also side effect free and fairly common |
108 // to use as field initializers. | 108 // to use as field initializers. |
109 bool isFieldInitConstant(VariableDeclaration field) => | 109 bool isFieldInitConstant(VariableDeclaration field) => |
110 field.initializer == null || computeConstant(field) != null; | 110 field.initializer == null || computeConstant(field) != null; |
111 | 111 |
112 DartObjectImpl computeConstant(VariableDeclaration field) { | 112 DartObject computeConstant(VariableDeclaration field) { |
113 // If the constant is already computed by ConstantEvaluator, just return it. | 113 // If the constant is already computed by ConstantEvaluator, just return it. |
114 VariableElementImpl element = field.element; | 114 VariableElementImpl element = field.element; |
115 var result = element.evaluationResult; | 115 var result = element.evaluationResult; |
116 if (result != null) return result.value; | 116 if (result != null) return result.value; |
117 | 117 |
118 // ConstantEvaluator will not compute constants for non-const fields, | 118 // ConstantEvaluator will not compute constants for non-const fields, |
119 // so run ConstantVisitor for those to figure out if the initializer is | 119 // so run ConstantVisitor for those to figure out if the initializer is |
120 // actually a constant (and therefore side effect free to evaluate). | 120 // actually a constant (and therefore side effect free to evaluate). |
121 assert(!field.isConst); | 121 assert(!field.isConst); |
122 | 122 |
123 var initializer = field.initializer; | 123 var initializer = field.initializer; |
124 if (initializer == null) return null; | 124 if (initializer == null) return null; |
125 return initializer.accept(_constantVisitor); | 125 return initializer.accept(_constantVisitor); |
126 } | 126 } |
127 } | 127 } |
OLD | NEW |