| 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 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 158 |
| 159 ConstantCompilerBase(this.compiler, this.constantSystem); | 159 ConstantCompilerBase(this.compiler, this.constantSystem); |
| 160 | 160 |
| 161 DiagnosticReporter get reporter => compiler.reporter; | 161 DiagnosticReporter get reporter => compiler.reporter; |
| 162 | 162 |
| 163 CoreTypes get coreTypes => compiler.coreTypes; | 163 CoreTypes get coreTypes => compiler.coreTypes; |
| 164 | 164 |
| 165 @override | 165 @override |
| 166 @deprecated | 166 @deprecated |
| 167 ConstantValue getConstantValueForVariable(VariableElement element) { | 167 ConstantValue getConstantValueForVariable(VariableElement element) { |
| 168 ConstantExpression constant = initialVariableValues[element.declaration]; | 168 return getConstantValue(initialVariableValues[element.declaration]); |
| 169 // TODO(johnniwinther): Support eager evaluation of the constant. | |
| 170 return constant != null ? getConstantValue(constant) : null; | |
| 171 } | 169 } |
| 172 | 170 |
| 173 ConstantExpression compileConstant(VariableElement element) { | 171 ConstantExpression compileConstant(VariableElement element) { |
| 174 return internalCompileVariable(element, true, true); | 172 return internalCompileVariable(element, true, true); |
| 175 } | 173 } |
| 176 | 174 |
| 177 @override | 175 @override |
| 178 void evaluate(ConstantExpression constant) { | 176 void evaluate(ConstantExpression constant) { |
| 179 constantValueMap.putIfAbsent(constant, () { | 177 constantValueMap.putIfAbsent(constant, () { |
| 180 return constant.evaluate( | 178 return constant.evaluate( |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 cacheConstantValue(constant.expression, constant.value); | 313 cacheConstantValue(constant.expression, constant.value); |
| 316 return constant.expression; | 314 return constant.expression; |
| 317 } | 315 } |
| 318 return null; | 316 return null; |
| 319 } | 317 } |
| 320 | 318 |
| 321 bool hasConstantValue(ConstantExpression expression) { | 319 bool hasConstantValue(ConstantExpression expression) { |
| 322 return constantValueMap.containsKey(expression); | 320 return constantValueMap.containsKey(expression); |
| 323 } | 321 } |
| 324 | 322 |
| 325 @override | |
| 326 ConstantValue getConstantValue(ConstantExpression expression) { | 323 ConstantValue getConstantValue(ConstantExpression expression) { |
| 327 assert(invariant(CURRENT_ELEMENT_SPANNABLE, expression != null, | 324 return constantValueMap[expression]; |
| 328 message: "ConstantExpression is null in getConstantValue.")); | |
| 329 // TODO(johnniwinther): ensure expressions have been evaluated at this | |
| 330 // point. This can't be enabled today due to dartbug.com/26406. | |
| 331 if (compiler.serialization.supportsDeserialization) { | |
| 332 evaluate(expression); | |
| 333 } | |
| 334 ConstantValue value = constantValueMap[expression]; | |
| 335 if (value == null && | |
| 336 expression != null && | |
| 337 expression.kind == ConstantExpressionKind.ERRONEOUS) { | |
| 338 // TODO(johnniwinther): When the Dart constant system sees a constant | |
| 339 // expression as erroneous but the JavaScript constant system finds it ok | |
| 340 // we have store a constant value for the erroneous constant expression. | |
| 341 // Ensure the computed constant expressions are always the same; that only | |
| 342 // the constant values may be different. | |
| 343 value = new NullConstantValue(); | |
| 344 } | |
| 345 return value; | |
| 346 } | 325 } |
| 347 | 326 |
| 348 ConstantExpression compileNode(Node node, TreeElements elements, | 327 ConstantExpression compileNode(Node node, TreeElements elements, |
| 349 {bool enforceConst: true}) { | 328 {bool enforceConst: true}) { |
| 350 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); | 329 return compileNodeWithDefinitions(node, elements, isConst: enforceConst); |
| 351 } | 330 } |
| 352 | 331 |
| 353 ConstantExpression compileMetadata( | 332 ConstantExpression compileMetadata( |
| 354 MetadataAnnotation metadata, Node node, TreeElements elements) { | 333 MetadataAnnotation metadata, Node node, TreeElements elements) { |
| 355 return compileNodeWithDefinitions(node, elements); | 334 return compileNodeWithDefinitions(node, elements); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 373 : super(compiler, const DartConstantSystem()); | 352 : super(compiler, const DartConstantSystem()); |
| 374 | 353 |
| 375 ConstantExpression getConstantForNode(Node node, TreeElements definitions) { | 354 ConstantExpression getConstantForNode(Node node, TreeElements definitions) { |
| 376 return definitions.getConstant(node); | 355 return definitions.getConstant(node); |
| 377 } | 356 } |
| 378 | 357 |
| 379 ConstantExpression compileNodeWithDefinitions( | 358 ConstantExpression compileNodeWithDefinitions( |
| 380 Node node, TreeElements definitions, | 359 Node node, TreeElements definitions, |
| 381 {bool isConst: true}) { | 360 {bool isConst: true}) { |
| 382 ConstantExpression constant = definitions.getConstant(node); | 361 ConstantExpression constant = definitions.getConstant(node); |
| 383 if (constant != null && hasConstantValue(constant)) { | 362 if (constant != null && getConstantValue(constant) != null) { |
| 384 return constant; | 363 return constant; |
| 385 } | 364 } |
| 386 constant = | 365 constant = |
| 387 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); | 366 super.compileNodeWithDefinitions(node, definitions, isConst: isConst); |
| 388 if (constant != null) { | 367 if (constant != null) { |
| 389 definitions.setConstant(node, constant); | 368 definitions.setConstant(node, constant); |
| 390 } | 369 } |
| 391 return constant; | 370 return constant; |
| 392 } | 371 } |
| 393 } | 372 } |
| (...skipping 995 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1389 class _CompilerEnvironment implements Environment { | 1368 class _CompilerEnvironment implements Environment { |
| 1390 final Compiler compiler; | 1369 final Compiler compiler; |
| 1391 | 1370 |
| 1392 _CompilerEnvironment(this.compiler); | 1371 _CompilerEnvironment(this.compiler); |
| 1393 | 1372 |
| 1394 @override | 1373 @override |
| 1395 String readFromEnvironment(String name) { | 1374 String readFromEnvironment(String name) { |
| 1396 return compiler.fromEnvironment(name); | 1375 return compiler.fromEnvironment(name); |
| 1397 } | 1376 } |
| 1398 } | 1377 } |
| OLD | NEW |