| 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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 if (node.inSetterContext() && node.staticElement == _variable) { | 89 if (node.inSetterContext() && node.staticElement == _variable) { |
| 90 _potentiallyMutated = true; | 90 _potentiallyMutated = true; |
| 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 : _constantVisitor = new ConstantVisitor.con1(types, | 99 // TODO(jmesserly): support -D variables on the command line |
| 100 : _constantVisitor = new ConstantVisitor( |
| 101 new ConstantEvaluationEngine(types, new DeclaredVariables()), |
| 100 new ErrorReporter(new RecordingErrorListener(), unit.element.source)); | 102 new ErrorReporter(new RecordingErrorListener(), unit.element.source)); |
| 101 | 103 |
| 102 // TODO(jmesserly): this is used to determine if the field initialization is | 104 // TODO(jmesserly): this is used to determine if the field initialization is |
| 103 // side effect free. We should make the check more general, as things like | 105 // side effect free. We should make the check more general, as things like |
| 104 // list/map literals/regexp are also side effect free and fairly common | 106 // list/map literals/regexp are also side effect free and fairly common |
| 105 // to use as field initializers. | 107 // to use as field initializers. |
| 106 bool isFieldInitConstant(VariableDeclaration field) => | 108 bool isFieldInitConstant(VariableDeclaration field) => |
| 107 field.initializer == null || computeConstant(field) != null; | 109 field.initializer == null || computeConstant(field) != null; |
| 108 | 110 |
| 109 DartObjectImpl computeConstant(VariableDeclaration field) { | 111 DartObjectImpl computeConstant(VariableDeclaration field) { |
| 110 // If the constant is already computed by ConstantEvaluator, just return it. | 112 // If the constant is already computed by ConstantEvaluator, just return it. |
| 111 VariableElementImpl element = field.element; | 113 VariableElementImpl element = field.element; |
| 112 var result = element.evaluationResult; | 114 var result = element.evaluationResult; |
| 113 if (result != null) return result.value; | 115 if (result != null) return result.value; |
| 114 | 116 |
| 115 // ConstantEvaluator will not compute constants for non-const fields, | 117 // ConstantEvaluator will not compute constants for non-const fields, |
| 116 // so run ConstantVisitor for those to figure out if the initializer is | 118 // so run ConstantVisitor for those to figure out if the initializer is |
| 117 // actually a constant (and therefore side effect free to evaluate). | 119 // actually a constant (and therefore side effect free to evaluate). |
| 118 assert(!field.isConst); | 120 assert(!field.isConst); |
| 119 | 121 |
| 120 var initializer = field.initializer; | 122 var initializer = field.initializer; |
| 121 if (initializer == null) return null; | 123 if (initializer == null) return null; |
| 122 return initializer.accept(_constantVisitor); | 124 return initializer.accept(_constantVisitor); |
| 123 } | 125 } |
| 124 } | 126 } |
| OLD | NEW |