| 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; | 9 import 'common/tasks.dart' show CompilerTask; |
| 10 import 'compiler.dart' show Compiler; | 10 import 'compiler.dart' show Compiler; |
| 11 import 'constant_system_dart.dart'; | 11 import 'constant_system_dart.dart'; |
| 12 import 'constants/constant_system.dart'; | 12 import 'constants/constant_system.dart'; |
| 13 import 'constants/evaluation.dart'; | 13 import 'constants/evaluation.dart'; |
| 14 import 'constants/expressions.dart'; | 14 import 'constants/expressions.dart'; |
| 15 import 'constants/values.dart'; | 15 import 'constants/values.dart'; |
| 16 import 'core_types.dart' show CoreTypes; | 16 import 'core_types.dart' show CoreTypes; |
| 17 import 'dart_types.dart'; | 17 import 'dart_types.dart'; |
| 18 import 'elements/elements.dart'; | 18 import 'elements/elements.dart'; |
| 19 import 'elements/modelx.dart' show FieldElementX, FunctionElementX; | 19 import 'elements/modelx.dart' show FieldElementX, FunctionElementX, ConstantVari
ableMixin; |
| 20 import 'resolution/tree_elements.dart' show TreeElements; | 20 import 'resolution/tree_elements.dart' show TreeElements; |
| 21 import 'resolution/operators.dart'; | 21 import 'resolution/operators.dart'; |
| 22 import 'tree/tree.dart'; | 22 import 'tree/tree.dart'; |
| 23 import 'util/util.dart' show Link; | 23 import 'util/util.dart' show Link; |
| 24 import 'universe/call_structure.dart' show CallStructure; | 24 import 'universe/call_structure.dart' show CallStructure; |
| 25 | 25 |
| 26 /// A [ConstantEnvironment] provides access for constants compiled for variable | 26 /// A [ConstantEnvironment] provides access for constants compiled for variable |
| 27 /// initializers. | 27 /// initializers. |
| 28 abstract class ConstantEnvironment { | 28 abstract class ConstantEnvironment { |
| 29 /// The [ConstantSystem] used by this environment. | 29 /// The [ConstantSystem] used by this environment. |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 180 |
| 181 /// Compile [element] into a constant expression. If [isConst] is true, | 181 /// Compile [element] into a constant expression. If [isConst] is true, |
| 182 /// then [element] is a constant variable. If [checkType] is true, then | 182 /// then [element] is a constant variable. If [checkType] is true, then |
| 183 /// report an error if [element] does not typecheck. | 183 /// report an error if [element] does not typecheck. |
| 184 ConstantExpression internalCompileVariable( | 184 ConstantExpression internalCompileVariable( |
| 185 VariableElement element, bool isConst, bool checkType) { | 185 VariableElement element, bool isConst, bool checkType) { |
| 186 if (initialVariableValues.containsKey(element.declaration)) { | 186 if (initialVariableValues.containsKey(element.declaration)) { |
| 187 ConstantExpression result = initialVariableValues[element.declaration]; | 187 ConstantExpression result = initialVariableValues[element.declaration]; |
| 188 return result; | 188 return result; |
| 189 } | 189 } |
| 190 if (element.hasConstant) { |
| 191 if (element.constant != null && |
| 192 compiler.serialization.supportsDeserialization) { |
| 193 evaluate(element.constant); |
| 194 } |
| 195 return element.constant; |
| 196 } |
| 190 AstElement currentElement = element.analyzableElement; | 197 AstElement currentElement = element.analyzableElement; |
| 191 return reporter.withCurrentElement(currentElement, () { | 198 return reporter.withCurrentElement(element, () { |
| 192 // TODO(johnniwinther): Avoid this eager analysis. | 199 // TODO(johnniwinther): Avoid this eager analysis. |
| 193 compiler.resolution.ensureResolved(currentElement.declaration); | 200 compiler.resolution.ensureResolved(currentElement.declaration); |
| 194 | 201 |
| 195 ConstantExpression constant = compileVariableWithDefinitions( | 202 ConstantExpression constant = compileVariableWithDefinitions( |
| 196 element, currentElement.resolvedAst.elements, | 203 element, currentElement.resolvedAst.elements, |
| 197 isConst: isConst, checkType: checkType); | 204 isConst: isConst, checkType: checkType); |
| 198 return constant; | 205 return constant; |
| 199 }); | 206 }); |
| 200 } | 207 } |
| 201 | 208 |
| 202 /** | 209 /** |
| 203 * Returns the a compile-time constant if the variable could be compiled | 210 * Returns the a compile-time constant if the variable could be compiled |
| 204 * eagerly. If the variable needs to be initialized lazily returns `null`. | 211 * eagerly. If the variable needs to be initialized lazily returns `null`. |
| 205 * If the variable is `const` but cannot be compiled eagerly reports an | 212 * If the variable is `const` but cannot be compiled eagerly reports an |
| 206 * error. | 213 * error. |
| 207 */ | 214 */ |
| 208 ConstantExpression compileVariableWithDefinitions( | 215 ConstantExpression compileVariableWithDefinitions( |
| 209 VariableElement element, TreeElements definitions, | 216 ConstantVariableMixin element, TreeElements definitions, |
| 210 {bool isConst: false, bool checkType: true}) { | 217 {bool isConst: false, bool checkType: true}) { |
| 211 Node node = element.node; | 218 Node node = element.node; |
| 212 if (pendingVariables.contains(element)) { | 219 if (pendingVariables.contains(element)) { |
| 213 if (isConst) { | 220 if (isConst) { |
| 214 reporter.reportErrorMessage( | 221 reporter.reportErrorMessage( |
| 215 node, MessageKind.CYCLIC_COMPILE_TIME_CONSTANTS); | 222 node, MessageKind.CYCLIC_COMPILE_TIME_CONSTANTS); |
| 216 ConstantExpression expression = new ErroneousConstantExpression(); | 223 ConstantExpression expression = new ErroneousConstantExpression(); |
| 217 constantValueMap[expression] = constantSystem.createNull(); | 224 constantValueMap[expression] = constantSystem.createNull(); |
| 218 return expression; | 225 return expression; |
| 219 } | 226 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } else { | 262 } else { |
| 256 // If the field cannot be lazily initialized, we will throw | 263 // If the field cannot be lazily initialized, we will throw |
| 257 // the exception at runtime. | 264 // the exception at runtime. |
| 258 expression = null; | 265 expression = null; |
| 259 } | 266 } |
| 260 } | 267 } |
| 261 } | 268 } |
| 262 } | 269 } |
| 263 } | 270 } |
| 264 if (expression != null) { | 271 if (expression != null) { |
| 272 element.constant = expression; |
| 265 initialVariableValues[element.declaration] = expression; | 273 initialVariableValues[element.declaration] = expression; |
| 266 } else { | 274 } else { |
| 267 assert(invariant(element, !isConst, | 275 assert(invariant(element, !isConst, |
| 268 message: "Variable $element does not compile to a constant.")); | 276 message: "Variable $element does not compile to a constant.")); |
| 269 } | 277 } |
| 270 pendingVariables.remove(element); | 278 pendingVariables.remove(element); |
| 271 return expression; | 279 return expression; |
| 272 } | 280 } |
| 273 | 281 |
| 274 void cacheConstantValue(ConstantExpression expression, ConstantValue value) { | 282 void cacheConstantValue(ConstantExpression expression, ConstantValue value) { |
| (...skipping 998 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1273 }); | 1281 }); |
| 1274 } | 1282 } |
| 1275 | 1283 |
| 1276 /// Builds a normalized list of the constant values for each field in the | 1284 /// Builds a normalized list of the constant values for each field in the |
| 1277 /// inheritance chain of [classElement]. | 1285 /// inheritance chain of [classElement]. |
| 1278 Map<FieldElement, AstConstant> buildFieldConstants( | 1286 Map<FieldElement, AstConstant> buildFieldConstants( |
| 1279 ClassElement classElement) { | 1287 ClassElement classElement) { |
| 1280 Map<FieldElement, AstConstant> fieldConstants = | 1288 Map<FieldElement, AstConstant> fieldConstants = |
| 1281 <FieldElement, AstConstant>{}; | 1289 <FieldElement, AstConstant>{}; |
| 1282 classElement.implementation.forEachInstanceField( | 1290 classElement.implementation.forEachInstanceField( |
| 1283 (ClassElement enclosing, FieldElementX field) { | 1291 (ClassElement enclosing, FieldElement field) { |
| 1284 AstConstant fieldValue = fieldValues[field]; | 1292 AstConstant fieldValue = fieldValues[field]; |
| 1285 if (fieldValue == null) { | 1293 if (fieldValue == null) { |
| 1286 // Use the default value. | 1294 // Use the default value. |
| 1287 ConstantExpression fieldExpression = | 1295 ConstantExpression fieldExpression = |
| 1288 handler.internalCompileVariable(field, true, false); | 1296 handler.internalCompileVariable(field, true, false); |
| 1289 field.constant = fieldExpression; | |
| 1290 fieldValue = new AstConstant.fromDefaultValue( | 1297 fieldValue = new AstConstant.fromDefaultValue( |
| 1291 field, fieldExpression, handler.getConstantValue(fieldExpression)); | 1298 field, fieldExpression, handler.getConstantValue(fieldExpression)); |
| 1292 // TODO(het): If the field value doesn't typecheck due to the type | 1299 // TODO(het): If the field value doesn't typecheck due to the type |
| 1293 // variable in the constructor invocation, then report the error on the | 1300 // variable in the constructor invocation, then report the error on the |
| 1294 // invocation rather than the field. | 1301 // invocation rather than the field. |
| 1295 potentiallyCheckType(field, fieldValue); | 1302 potentiallyCheckType(field, fieldValue); |
| 1296 } | 1303 } |
| 1297 fieldConstants[field] = fieldValue; | 1304 fieldConstants[field] = fieldValue; |
| 1298 }, includeSuperAndInjectedMembers: true); | 1305 }, includeSuperAndInjectedMembers: true); |
| 1299 return fieldConstants; | 1306 return fieldConstants; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1343 class _CompilerEnvironment implements Environment { | 1350 class _CompilerEnvironment implements Environment { |
| 1344 final Compiler compiler; | 1351 final Compiler compiler; |
| 1345 | 1352 |
| 1346 _CompilerEnvironment(this.compiler); | 1353 _CompilerEnvironment(this.compiler); |
| 1347 | 1354 |
| 1348 @override | 1355 @override |
| 1349 String readFromEnvironment(String name) { | 1356 String readFromEnvironment(String name) { |
| 1350 return compiler.fromEnvironment(name); | 1357 return compiler.fromEnvironment(name); |
| 1351 } | 1358 } |
| 1352 } | 1359 } |
| OLD | NEW |