| 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 class Constant implements Hashable { | 5 class Constant implements Hashable { |
| 6 const Constant(); | 6 const Constant(); |
| 7 | 7 |
| 8 bool isNull() => false; | 8 bool isNull() => false; |
| 9 bool isBool() => false; | 9 bool isBool() => false; |
| 10 bool isTrue() => false; | 10 bool isTrue() => false; |
| (...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 assert(work.element.kind == ElementKind.FIELD | 488 assert(work.element.kind == ElementKind.FIELD |
| 489 || work.element.kind == ElementKind.PARAMETER | 489 || work.element.kind == ElementKind.PARAMETER |
| 490 || work.element.kind == ElementKind.FIELD_PARAMETER); | 490 || work.element.kind == ElementKind.FIELD_PARAMETER); |
| 491 VariableElement element = work.element; | 491 VariableElement element = work.element; |
| 492 // Shortcut if it has already been compiled. | 492 // Shortcut if it has already been compiled. |
| 493 if (initialVariableValues.containsKey(element)) return; | 493 if (initialVariableValues.containsKey(element)) return; |
| 494 compileVariableWithDefinitions(element, work.resolutionTree); | 494 compileVariableWithDefinitions(element, work.resolutionTree); |
| 495 assert(pendingVariables.isEmpty()); | 495 assert(pendingVariables.isEmpty()); |
| 496 } | 496 } |
| 497 | 497 |
| 498 compileVariable(VariableElement element) { | 498 Constant compileVariable(VariableElement element) { |
| 499 // TODO(floitsch): wrap this method in 'measure'. |
| 499 if (initialVariableValues.containsKey(element)) { | 500 if (initialVariableValues.containsKey(element)) { |
| 500 Constant result = initialVariableValues[element]; | 501 Constant result = initialVariableValues[element]; |
| 501 return result; | 502 return result; |
| 502 } | 503 } |
| 503 // TODO(floitsch): keep track of currently compiling elements so that we | 504 // TODO(floitsch): keep track of currently compiling elements so that we |
| 504 // don't end up in an infinite loop: final x = y; final y = x; | 505 // don't end up in an infinite loop: final x = y; final y = x; |
| 505 TreeElements definitions = compiler.analyzeElement(element); | 506 TreeElements definitions = compiler.analyzeElement(element); |
| 506 Constant constant = compileVariableWithDefinitions(element, definitions); | 507 Constant constant = compileVariableWithDefinitions(element, definitions); |
| 507 return constant; | 508 return constant; |
| 508 } | 509 } |
| 509 | 510 |
| 510 Constant compileVariableWithDefinitions(VariableElement element, | 511 Constant compileVariableWithDefinitions(VariableElement element, |
| 511 TreeElements definitions) { | 512 TreeElements definitions) { |
| 512 return measure(() { | 513 return measure(() { |
| 513 Node node = element.parseNode(compiler); | 514 Node node = element.parseNode(compiler); |
| 514 assert(node !== null); | 515 if (pendingVariables.contains(element)) { |
| 516 MessageKind kind = MessageKind.CYCLIC_COMPILE_TIME_CONSTANTS; |
| 517 compiler.reportError(node, |
| 518 new CompileTimeConstantError(kind, const [])); |
| 519 } |
| 520 pendingVariables.add(element); |
| 521 |
| 515 SendSet assignment = node.asSendSet(); | 522 SendSet assignment = node.asSendSet(); |
| 516 var value; | 523 Constant value; |
| 517 if (assignment === null) { | 524 if (assignment === null) { |
| 518 // No initial value. | 525 // No initial value. |
| 519 value = new NullConstant(); | 526 value = new NullConstant(); |
| 520 } else { | 527 } else { |
| 521 if (pendingVariables.contains(element)) { | |
| 522 MessageKind kind = MessageKind.CYCLIC_COMPILE_TIME_CONSTANTS; | |
| 523 compiler.reportError(node, | |
| 524 new CompileTimeConstantError(kind, const [])); | |
| 525 } | |
| 526 pendingVariables.add(element); | |
| 527 | |
| 528 Node right = assignment.arguments.head; | 528 Node right = assignment.arguments.head; |
| 529 CompileTimeConstantEvaluator evaluator = | 529 value = compileNodeWithDefinitions(right, definitions); |
| 530 new CompileTimeConstantEvaluator(this, definitions, compiler); | |
| 531 value = evaluator.evaluate(right); | |
| 532 | |
| 533 pendingVariables.remove(element); | |
| 534 } | 530 } |
| 535 initialVariableValues[element] = value; | 531 initialVariableValues[element] = value; |
| 532 pendingVariables.remove(element); |
| 536 return value; | 533 return value; |
| 537 }); | 534 }); |
| 538 } | 535 } |
| 539 | 536 |
| 537 Constant compileNodeWithDefinitions(Node node, TreeElements definitions) { |
| 538 return measure(() { |
| 539 assert(node !== null); |
| 540 CompileTimeConstantEvaluator evaluator = |
| 541 new CompileTimeConstantEvaluator(this, definitions, compiler); |
| 542 return evaluator.evaluate(node); |
| 543 }); |
| 544 } |
| 545 |
| 540 /** | 546 /** |
| 541 * Returns a [List] of static non final fields that need to be initialized. | 547 * Returns a [List] of static non final fields that need to be initialized. |
| 542 * The list must be evaluated in order since the fields might depend on each | 548 * The list must be evaluated in order since the fields might depend on each |
| 543 * other. | 549 * other. |
| 544 */ | 550 */ |
| 545 List<VariableElement> getStaticNonFinalFieldsForEmission() { | 551 List<VariableElement> getStaticNonFinalFieldsForEmission() { |
| 546 return initialVariableValues.getKeys().filter((element) { | 552 return initialVariableValues.getKeys().filter((element) { |
| 547 return element.kind == ElementKind.FIELD | 553 return element.kind == ElementKind.FIELD |
| 548 && !element.isInstanceMember() | 554 && !element.isInstanceMember() |
| 549 && !element.modifiers.isFinal(); | 555 && !element.modifiers.isFinal(); |
| (...skipping 493 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 return constant; | 1049 return constant; |
| 1044 } | 1050 } |
| 1045 | 1051 |
| 1046 error(Node node) { | 1052 error(Node node) { |
| 1047 // TODO(floitsch): get the list of constants that are currently compiled | 1053 // TODO(floitsch): get the list of constants that are currently compiled |
| 1048 // and present some kind of stack-trace. | 1054 // and present some kind of stack-trace. |
| 1049 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; | 1055 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; |
| 1050 compiler.reportError(node, new CompileTimeConstantError(kind, const [])); | 1056 compiler.reportError(node, new CompileTimeConstantError(kind, const [])); |
| 1051 } | 1057 } |
| 1052 } | 1058 } |
| OLD | NEW |