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/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
6 import 'package:analyzer/dart/ast/visitor.dart'; | 6 import 'package:analyzer/dart/ast/visitor.dart'; |
7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
8 import 'package:analyzer/src/generated/constant.dart'; | 8 import 'package:analyzer/src/generated/constant.dart'; |
9 import 'package:analyzer/src/generated/error.dart' | 9 import 'package:analyzer/src/generated/error.dart' |
10 show AnalysisErrorListener, ErrorReporter; | 10 show AnalysisErrorListener, ErrorReporter; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 | 100 |
101 if (node.inSetterContext() && node.staticElement == _variable) { | 101 if (node.inSetterContext() && node.staticElement == _variable) { |
102 _potentiallyMutated = true; | 102 _potentiallyMutated = true; |
103 } | 103 } |
104 } | 104 } |
105 } | 105 } |
106 | 106 |
107 class ConstFieldVisitor { | 107 class ConstFieldVisitor { |
108 final ConstantVisitor _constantVisitor; | 108 final ConstantVisitor _constantVisitor; |
109 | 109 |
110 ConstFieldVisitor(TypeProvider types, Source source) | 110 ConstFieldVisitor(TypeProvider types, {Source dummySource}) |
111 // TODO(jmesserly): support -D variables on the command line | 111 // TODO(jmesserly): support -D variables on the command line |
112 : _constantVisitor = new ConstantVisitor( | 112 : _constantVisitor = new ConstantVisitor( |
113 new ConstantEvaluationEngine(types, new DeclaredVariables()), | 113 new ConstantEvaluationEngine(types, new DeclaredVariables()), |
114 new ErrorReporter(AnalysisErrorListener.NULL_LISTENER, source)); | 114 new ErrorReporter( |
| 115 AnalysisErrorListener.NULL_LISTENER, dummySource)); |
115 | 116 |
116 // TODO(jmesserly): this is used to determine if the field initialization is | 117 // TODO(jmesserly): this is used to determine if the field initialization is |
117 // side effect free. We should make the check more general, as things like | 118 // side effect free. We should make the check more general, as things like |
118 // list/map literals/regexp are also side effect free and fairly common | 119 // list/map literals/regexp are also side effect free and fairly common |
119 // to use as field initializers. | 120 // to use as field initializers. |
120 bool isFieldInitConstant(VariableDeclaration field) => | 121 bool isFieldInitConstant(VariableDeclaration field) => |
121 field.initializer == null || computeConstant(field) != null; | 122 field.initializer == null || computeConstant(field) != null; |
122 | 123 |
123 DartObject computeConstant(VariableDeclaration field) { | 124 DartObject computeConstant(VariableDeclaration field) { |
124 // If the constant is already computed by ConstantEvaluator, just return it. | 125 // If the constant is already computed by ConstantEvaluator, just return it. |
125 VariableElement element = field.element; | 126 VariableElement element = field.element; |
126 var result = element.constantValue; | 127 var result = element.constantValue; |
127 if (result != null) return result; | 128 if (result != null) return result; |
128 | 129 |
129 // ConstantEvaluator will not compute constants for non-const fields, | 130 // ConstantEvaluator will not compute constants for non-const fields, |
130 // so run ConstantVisitor for those to figure out if the initializer is | 131 // so run ConstantVisitor for those to figure out if the initializer is |
131 // actually a constant (and therefore side effect free to evaluate). | 132 // actually a constant (and therefore side effect free to evaluate). |
132 assert(!field.isConst); | 133 assert(!field.isConst); |
133 | 134 |
134 var initializer = field.initializer; | 135 var initializer = field.initializer; |
135 if (initializer == null) return null; | 136 if (initializer == null) return null; |
136 return initializer.accept(_constantVisitor); | 137 return initializer.accept(_constantVisitor); |
137 } | 138 } |
138 } | 139 } |
OLD | NEW |