| 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 FunctionElementX; | 19 import 'elements/modelx.dart' show FieldElementX, FunctionElementX; |
| 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. |
| 30 ConstantSystem get constantSystem; | 30 ConstantSystem get constantSystem; |
| 31 | 31 |
| 32 /// Returns the constant value computed for [expression]. | 32 /// Returns the constant value computed for [expression]. |
| 33 // TODO(johnniwinther): Support directly evaluation of [expression]. | 33 // TODO(johnniwinther): Support directly evaluation of [expression]. |
| 34 ConstantValue getConstantValue(ConstantExpression expression); | 34 ConstantValue getConstantValue(ConstantExpression expression); |
| 35 | 35 |
| 36 /// Returns the constant value for the initializer of [element]. | 36 /// Returns the constant value for the initializer of [element]. |
| 37 @deprecated |
| 37 ConstantValue getConstantValueForVariable(VariableElement element); | 38 ConstantValue getConstantValueForVariable(VariableElement element); |
| 38 | |
| 39 /// Returns the constant for the initializer of [element]. | |
| 40 ConstantExpression getConstantForVariable(VariableElement element); | |
| 41 } | 39 } |
| 42 | 40 |
| 43 /// A class that can compile and provide constants for variables, nodes and | 41 /// A class that can compile and provide constants for variables, nodes and |
| 44 /// metadata. | 42 /// metadata. |
| 45 abstract class ConstantCompiler extends ConstantEnvironment { | 43 abstract class ConstantCompiler extends ConstantEnvironment { |
| 46 /// Compiles the compile-time constant for the initializer of [element], or | 44 /// Compiles the compile-time constant for the initializer of [element], or |
| 47 /// reports an error if the initializer is not a compile-time constant. | 45 /// reports an error if the initializer is not a compile-time constant. |
| 48 /// | 46 /// |
| 49 /// Depending on implementation, the constant compiler might also compute | 47 /// Depending on implementation, the constant compiler might also compute |
| 50 /// the compile-time constant for the backend interpretation of constants. | 48 /// the compile-time constant for the backend interpretation of constants. |
| 51 /// | 49 /// |
| 52 /// The returned constant is always of the frontend interpretation. | 50 /// The returned constant is always of the frontend interpretation. |
| 53 ConstantExpression compileConstant(VariableElement element); | 51 ConstantExpression compileConstant(VariableElement element); |
| 54 | 52 |
| 55 /// Computes the compile-time constant for the variable initializer, | 53 /// Computes the compile-time constant for the variable initializer, |
| 56 /// if possible. | 54 /// if possible. |
| 57 void compileVariable(VariableElement element); | 55 ConstantExpression compileVariable(VariableElement element); |
| 58 | 56 |
| 59 /// Compiles the constant for [node]. | 57 /// Compiles the constant for [node]. |
| 60 /// | 58 /// |
| 61 /// Reports an error if [node] is not a compile-time constant and | 59 /// Reports an error if [node] is not a compile-time constant and |
| 62 /// [enforceConst]. | 60 /// [enforceConst]. |
| 63 /// | 61 /// |
| 64 /// If `!enforceConst`, then if [node] is a "runtime constant" (for example | 62 /// If `!enforceConst`, then if [node] is a "runtime constant" (for example |
| 65 /// a reference to a deferred constant) it will be returned - otherwise null | 63 /// a reference to a deferred constant) it will be returned - otherwise null |
| 66 /// is returned. | 64 /// is returned. |
| 67 /// | 65 /// |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 final Map<ConstantExpression, ConstantValue> constantValueMap = | 147 final Map<ConstantExpression, ConstantValue> constantValueMap = |
| 150 <ConstantExpression, ConstantValue>{}; | 148 <ConstantExpression, ConstantValue>{}; |
| 151 | 149 |
| 152 ConstantCompilerBase(this.compiler, this.constantSystem); | 150 ConstantCompilerBase(this.compiler, this.constantSystem); |
| 153 | 151 |
| 154 DiagnosticReporter get reporter => compiler.reporter; | 152 DiagnosticReporter get reporter => compiler.reporter; |
| 155 | 153 |
| 156 CoreTypes get coreTypes => compiler.coreTypes; | 154 CoreTypes get coreTypes => compiler.coreTypes; |
| 157 | 155 |
| 158 @override | 156 @override |
| 157 @deprecated |
| 159 ConstantValue getConstantValueForVariable(VariableElement element) { | 158 ConstantValue getConstantValueForVariable(VariableElement element) { |
| 160 return getConstantValue(initialVariableValues[element.declaration]); | 159 return getConstantValue(initialVariableValues[element.declaration]); |
| 161 } | 160 } |
| 162 | 161 |
| 163 @override | |
| 164 ConstantExpression getConstantForVariable(VariableElement element) { | |
| 165 return initialVariableValues[element.declaration]; | |
| 166 } | |
| 167 | |
| 168 ConstantExpression compileConstant(VariableElement element) { | 162 ConstantExpression compileConstant(VariableElement element) { |
| 169 return internalCompileVariable(element, true, true); | 163 return internalCompileVariable(element, true, true); |
| 170 } | 164 } |
| 171 | 165 |
| 172 @override | 166 @override |
| 173 void evaluate(ConstantExpression constant) { | 167 void evaluate(ConstantExpression constant) { |
| 174 constantValueMap.putIfAbsent(constant, () { | 168 constantValueMap.putIfAbsent(constant, () { |
| 175 return constant.evaluate( | 169 return constant.evaluate( |
| 176 new _CompilerEnvironment(compiler), constantSystem); | 170 new _CompilerEnvironment(compiler), constantSystem); |
| 177 }); | 171 }); |
| (...skipping 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1270 }); | 1264 }); |
| 1271 } | 1265 } |
| 1272 | 1266 |
| 1273 /// Builds a normalized list of the constant values for each field in the | 1267 /// Builds a normalized list of the constant values for each field in the |
| 1274 /// inheritance chain of [classElement]. | 1268 /// inheritance chain of [classElement]. |
| 1275 Map<FieldElement, AstConstant> buildFieldConstants( | 1269 Map<FieldElement, AstConstant> buildFieldConstants( |
| 1276 ClassElement classElement) { | 1270 ClassElement classElement) { |
| 1277 Map<FieldElement, AstConstant> fieldConstants = | 1271 Map<FieldElement, AstConstant> fieldConstants = |
| 1278 <FieldElement, AstConstant>{}; | 1272 <FieldElement, AstConstant>{}; |
| 1279 classElement.implementation.forEachInstanceField( | 1273 classElement.implementation.forEachInstanceField( |
| 1280 (ClassElement enclosing, FieldElement field) { | 1274 (ClassElement enclosing, FieldElementX field) { |
| 1281 AstConstant fieldValue = fieldValues[field]; | 1275 AstConstant fieldValue = fieldValues[field]; |
| 1282 if (fieldValue == null) { | 1276 if (fieldValue == null) { |
| 1283 // Use the default value. | 1277 // Use the default value. |
| 1284 ConstantExpression fieldExpression = | 1278 ConstantExpression fieldExpression = |
| 1285 handler.internalCompileVariable(field, true, false); | 1279 handler.internalCompileVariable(field, true, false); |
| 1280 field.constant = fieldExpression; |
| 1286 fieldValue = new AstConstant.fromDefaultValue( | 1281 fieldValue = new AstConstant.fromDefaultValue( |
| 1287 field, fieldExpression, handler.getConstantValue(fieldExpression)); | 1282 field, fieldExpression, handler.getConstantValue(fieldExpression)); |
| 1288 // TODO(het): If the field value doesn't typecheck due to the type | 1283 // TODO(het): If the field value doesn't typecheck due to the type |
| 1289 // variable in the constructor invocation, then report the error on the | 1284 // variable in the constructor invocation, then report the error on the |
| 1290 // invocation rather than the field. | 1285 // invocation rather than the field. |
| 1291 potentiallyCheckType(field, fieldValue); | 1286 potentiallyCheckType(field, fieldValue); |
| 1292 } | 1287 } |
| 1293 fieldConstants[field] = fieldValue; | 1288 fieldConstants[field] = fieldValue; |
| 1294 }, includeSuperAndInjectedMembers: true); | 1289 }, includeSuperAndInjectedMembers: true); |
| 1295 return fieldConstants; | 1290 return fieldConstants; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1345 class _CompilerEnvironment implements Environment { | 1340 class _CompilerEnvironment implements Environment { |
| 1346 final Compiler compiler; | 1341 final Compiler compiler; |
| 1347 | 1342 |
| 1348 _CompilerEnvironment(this.compiler); | 1343 _CompilerEnvironment(this.compiler); |
| 1349 | 1344 |
| 1350 @override | 1345 @override |
| 1351 String readFromEnvironment(String name) { | 1346 String readFromEnvironment(String name) { |
| 1352 return compiler.fromEnvironment(name); | 1347 return compiler.fromEnvironment(name); |
| 1353 } | 1348 } |
| 1354 } | 1349 } |
| OLD | NEW |