| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 dart2js.compile_time_constant_evaluator; | 5 library dart2js.compile_time_constant_evaluator; |
| 6 | 6 |
| 7 import 'common.dart'; | 7 import 'common.dart'; |
| 8 import 'common/resolution.dart' show Resolution; | 8 import 'common/resolution.dart' show Resolution; |
| 9 import 'common/tasks.dart' show CompilerTask, Measurer; | 9 import 'common/tasks.dart' show CompilerTask, Measurer; |
| 10 import 'compiler.dart' show Compiler; | 10 import 'compiler.dart' show Compiler; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 * | 138 * |
| 139 * Must contain all static and global initializations of const fields. | 139 * Must contain all static and global initializations of const fields. |
| 140 * | 140 * |
| 141 * May contain eagerly compiled initial values for statics and instance | 141 * May contain eagerly compiled initial values for statics and instance |
| 142 * fields (if those are compile-time constants). | 142 * fields (if those are compile-time constants). |
| 143 * | 143 * |
| 144 * May contain default parameter values of optional arguments. | 144 * May contain default parameter values of optional arguments. |
| 145 * | 145 * |
| 146 * Invariant: The keys in this map are declarations. | 146 * Invariant: The keys in this map are declarations. |
| 147 */ | 147 */ |
| 148 final Map<VariableElement, ConstantExpression> initialVariableValues = | 148 final Map<VariableElement, ConstantExpression> _initialVariableValues = |
| 149 new Map<VariableElement, ConstantExpression>(); | 149 new Map<VariableElement, ConstantExpression>(); |
| 150 | 150 |
| 151 /** The set of variable elements that are in the process of being computed. */ | 151 /** The set of variable elements that are in the process of being computed. */ |
| 152 final Set<VariableElement> pendingVariables = new Set<VariableElement>(); | 152 final Set<VariableElement> pendingVariables = new Set<VariableElement>(); |
| 153 | 153 |
| 154 final Map<ConstantExpression, ConstantValue> constantValueMap = | 154 final Map<ConstantExpression, ConstantValue> constantValueMap = |
| 155 <ConstantExpression, ConstantValue>{}; | 155 <ConstantExpression, ConstantValue>{}; |
| 156 | 156 |
| 157 ConstantCompilerBase(this.compiler, this.constantSystem); | 157 ConstantCompilerBase(this.compiler, this.constantSystem); |
| 158 | 158 |
| 159 DiagnosticReporter get reporter => compiler.reporter; | 159 DiagnosticReporter get reporter => compiler.reporter; |
| 160 | 160 |
| 161 CoreTypes get coreTypes => compiler.coreTypes; | 161 CoreTypes get coreTypes => compiler.coreTypes; |
| 162 | 162 |
| 163 @override | 163 @override |
| 164 @deprecated | 164 @deprecated |
| 165 ConstantValue getConstantValueForVariable(VariableElement element) { | 165 ConstantValue getConstantValueForVariable(VariableElement element) { |
| 166 return getConstantValue(initialVariableValues[element.declaration]); | 166 return getConstantValue(_initialVariableValues[element.declaration]); |
| 167 } | 167 } |
| 168 | 168 |
| 169 ConstantExpression compileConstant(VariableElement element) { | 169 ConstantExpression compileConstant(VariableElement element) { |
| 170 return internalCompileVariable(element, true, true); | 170 return internalCompileVariable(element, true, true); |
| 171 } | 171 } |
| 172 | 172 |
| 173 @override | 173 @override |
| 174 void evaluate(ConstantExpression constant) { | 174 void evaluate(ConstantExpression constant) { |
| 175 constantValueMap.putIfAbsent(constant, () { | 175 constantValueMap.putIfAbsent(constant, () { |
| 176 return constant.evaluate( | 176 return constant.evaluate( |
| 177 new _CompilerEnvironment(compiler), constantSystem); | 177 new _CompilerEnvironment(compiler), constantSystem); |
| 178 }); | 178 }); |
| 179 } | 179 } |
| 180 | 180 |
| 181 ConstantExpression compileVariable(VariableElement element) { | 181 ConstantExpression compileVariable(VariableElement element) { |
| 182 return internalCompileVariable(element, false, true); | 182 return internalCompileVariable(element, false, true); |
| 183 } | 183 } |
| 184 | 184 |
| 185 /// Compile [element] into a constant expression. If [isConst] is true, | 185 /// Compile [element] into a constant expression. If [isConst] is true, |
| 186 /// then [element] is a constant variable. If [checkType] is true, then | 186 /// then [element] is a constant variable. If [checkType] is true, then |
| 187 /// report an error if [element] does not typecheck. | 187 /// report an error if [element] does not typecheck. |
| 188 ConstantExpression internalCompileVariable( | 188 ConstantExpression internalCompileVariable( |
| 189 VariableElement element, bool isConst, bool checkType) { | 189 VariableElement element, bool isConst, bool checkType) { |
| 190 if (initialVariableValues.containsKey(element.declaration)) { | 190 if (_initialVariableValues.containsKey(element.declaration)) { |
| 191 ConstantExpression result = initialVariableValues[element.declaration]; | 191 ConstantExpression result = _initialVariableValues[element.declaration]; |
| 192 return result; | 192 return result; |
| 193 } | 193 } |
| 194 if (element.hasConstant) { | 194 if (element.hasConstant) { |
| 195 if (element.constant != null) { | 195 if (element.constant != null) { |
| 196 if (compiler.serialization.supportsDeserialization) { | 196 if (compiler.serialization.supportsDeserialization) { |
| 197 evaluate(element.constant); | 197 evaluate(element.constant); |
| 198 } | 198 } |
| 199 assert(invariant(element, hasConstantValue(element.constant), | 199 assert(invariant(element, hasConstantValue(element.constant), |
| 200 message: "Constant expression has not been evaluated: " | 200 message: "Constant expression has not been evaluated: " |
| 201 "${element.constant.toStructuredText()}.")); | 201 "${element.constant.toStructuredText()}.")); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 // If the field cannot be lazily initialized, we will throw | 279 // If the field cannot be lazily initialized, we will throw |
| 280 // the exception at runtime. | 280 // the exception at runtime. |
| 281 expression = null; | 281 expression = null; |
| 282 } | 282 } |
| 283 } | 283 } |
| 284 } | 284 } |
| 285 } | 285 } |
| 286 } | 286 } |
| 287 if (expression != null) { | 287 if (expression != null) { |
| 288 element.constant = expression; | 288 element.constant = expression; |
| 289 initialVariableValues[element.declaration] = expression; | 289 _initialVariableValues[element.declaration] = expression; |
| 290 } else { | 290 } else { |
| 291 assert(invariant(element, !isConst, | 291 assert(invariant(element, !isConst, |
| 292 message: "Variable $element does not compile to a constant.")); | 292 message: "Variable $element does not compile to a constant.")); |
| 293 } | 293 } |
| 294 pendingVariables.remove(element); | 294 pendingVariables.remove(element); |
| 295 return expression; | 295 return expression; |
| 296 } | 296 } |
| 297 | 297 |
| 298 void cacheConstantValue(ConstantExpression expression, ConstantValue value) { | 298 void cacheConstantValue(ConstantExpression expression, ConstantValue value) { |
| 299 constantValueMap[expression] = value; | 299 constantValueMap[expression] = value; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 326 {bool enforceConst: true}) { | 326 {bool enforceConst: true}) { |
| 327 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); | 327 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); |
| 328 } | 328 } |
| 329 | 329 |
| 330 ConstantExpression compileMetadata( | 330 ConstantExpression compileMetadata( |
| 331 MetadataAnnotation metadata, Node node, TreeElements elements) { | 331 MetadataAnnotation metadata, Node node, TreeElements elements) { |
| 332 return compileNodeWithDefinitions(node, elements); | 332 return compileNodeWithDefinitions(node, elements); |
| 333 } | 333 } |
| 334 | 334 |
| 335 void forgetElement(Element element) { | 335 void forgetElement(Element element) { |
| 336 initialVariableValues.remove(element); | 336 _initialVariableValues.remove(element); |
| 337 if (element is ScopeContainerElement) { | 337 if (element is ScopeContainerElement) { |
| 338 element.forEachLocalMember(initialVariableValues.remove); | 338 element.forEachLocalMember(_initialVariableValues.remove); |
| 339 } | 339 } |
| 340 if (element is FunctionElement && element.hasFunctionSignature) { | 340 if (element is FunctionElement && element.hasFunctionSignature) { |
| 341 element.functionSignature.forEachParameter(this.forgetElement); | 341 element.functionSignature.forEachParameter(this.forgetElement); |
| 342 } | 342 } |
| 343 } | 343 } |
| 344 } | 344 } |
| 345 | 345 |
| 346 /// [ConstantCompiler] that uses the Dart semantics for the compile-time | 346 /// [ConstantCompiler] that uses the Dart semantics for the compile-time |
| 347 /// constant evaluation. | 347 /// constant evaluation. |
| 348 class DartConstantCompiler extends ConstantCompilerBase { | 348 class DartConstantCompiler extends ConstantCompilerBase { |
| (...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1366 class _CompilerEnvironment implements Environment { | 1366 class _CompilerEnvironment implements Environment { |
| 1367 final Compiler compiler; | 1367 final Compiler compiler; |
| 1368 | 1368 |
| 1369 _CompilerEnvironment(this.compiler); | 1369 _CompilerEnvironment(this.compiler); |
| 1370 | 1370 |
| 1371 @override | 1371 @override |
| 1372 String readFromEnvironment(String name) { | 1372 String readFromEnvironment(String name) { |
| 1373 return compiler.fromEnvironment(name); | 1373 return compiler.fromEnvironment(name); |
| 1374 } | 1374 } |
| 1375 } | 1375 } |
| OLD | NEW |