| 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 91 } |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 class ConstFieldVisitor { | 95 class ConstFieldVisitor { |
| 96 final ConstantVisitor _constantVisitor; | 96 final ConstantVisitor _constantVisitor; |
| 97 | 97 |
| 98 ConstFieldVisitor(TypeProvider types, CompilationUnit unit) | 98 ConstFieldVisitor(TypeProvider types, CompilationUnit unit) |
| 99 // TODO(jmesserly): support -D variables on the command line | 99 // TODO(jmesserly): support -D variables on the command line |
| 100 : _constantVisitor = new ConstantVisitor( | 100 : _constantVisitor = new ConstantVisitor( |
| 101 new ConstantEvaluationEngine(types, new DeclaredVariables()), | 101 new ConstantEvaluationEngine(types, new DeclaredVariables()), |
| 102 new ErrorReporter(new RecordingErrorListener(), unit.element.source)); | 102 new ErrorReporter( |
| 103 new RecordingErrorListener(), unit.element.source)); |
| 103 | 104 |
| 104 // 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 |
| 105 // 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 |
| 106 // 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 |
| 107 // to use as field initializers. | 108 // to use as field initializers. |
| 108 bool isFieldInitConstant(VariableDeclaration field) => | 109 bool isFieldInitConstant(VariableDeclaration field) => |
| 109 field.initializer == null || computeConstant(field) != null; | 110 field.initializer == null || computeConstant(field) != null; |
| 110 | 111 |
| 111 DartObjectImpl computeConstant(VariableDeclaration field) { | 112 DartObjectImpl computeConstant(VariableDeclaration field) { |
| 112 // If the constant is already computed by ConstantEvaluator, just return it. | 113 // If the constant is already computed by ConstantEvaluator, just return it. |
| 113 VariableElementImpl element = field.element; | 114 VariableElementImpl element = field.element; |
| 114 var result = element.evaluationResult; | 115 var result = element.evaluationResult; |
| 115 if (result != null) return result.value; | 116 if (result != null) return result.value; |
| 116 | 117 |
| 117 // ConstantEvaluator will not compute constants for non-const fields, | 118 // ConstantEvaluator will not compute constants for non-const fields, |
| 118 // 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 |
| 119 // actually a constant (and therefore side effect free to evaluate). | 120 // actually a constant (and therefore side effect free to evaluate). |
| 120 assert(!field.isConst); | 121 assert(!field.isConst); |
| 121 | 122 |
| 122 var initializer = field.initializer; | 123 var initializer = field.initializer; |
| 123 if (initializer == null) return null; | 124 if (initializer == null) return null; |
| 124 return initializer.accept(_constantVisitor); | 125 return initializer.accept(_constantVisitor); |
| 125 } | 126 } |
| 126 } | 127 } |
| OLD | NEW |