| 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 /** | 5 /** |
| 6 * The [ConstantHandler] keeps track of compile-time constants, | 6 * The [ConstantHandler] keeps track of compile-time constants, |
| 7 * initializations of global and static fields, and default values of | 7 * initializations of global and static fields, and default values of |
| 8 * optional parameters. | 8 * optional parameters. |
| 9 */ | 9 */ |
| 10 class ConstantHandler extends CompilerTask { | 10 class ConstantHandler extends CompilerTask { |
| (...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 if (folded === null) return signalNotCompileTimeConstant(send); | 528 if (folded === null) return signalNotCompileTimeConstant(send); |
| 529 return folded; | 529 return folded; |
| 530 } | 530 } |
| 531 return signalNotCompileTimeConstant(send); | 531 return signalNotCompileTimeConstant(send); |
| 532 } | 532 } |
| 533 | 533 |
| 534 Constant visitSendSet(SendSet node) { | 534 Constant visitSendSet(SendSet node) { |
| 535 return signalNotCompileTimeConstant(node); | 535 return signalNotCompileTimeConstant(node); |
| 536 } | 536 } |
| 537 | 537 |
| 538 /** Returns the list of constants that are passed to the static function. */ | 538 /** |
| 539 * Returns the list of constants that are passed to the static function. |
| 540 * |
| 541 * Invariant: [target] must be an implementation element. |
| 542 */ |
| 539 List<Constant> evaluateArgumentsToConstructor(Node node, | 543 List<Constant> evaluateArgumentsToConstructor(Node node, |
| 540 Selector selector, | 544 Selector selector, |
| 541 Link<Node> arguments, | 545 Link<Node> arguments, |
| 542 FunctionElement target) { | 546 FunctionElement target) { |
| 547 assert(invariant(node, target.isImplementation)); |
| 543 List<Constant> compiledArguments = <Constant>[]; | 548 List<Constant> compiledArguments = <Constant>[]; |
| 544 | 549 |
| 545 Function compileArgument = evaluateConstant; | 550 Function compileArgument = evaluateConstant; |
| 546 Function compileConstant = compiler.compileConstant; | 551 Function compileConstant = compiler.compileConstant; |
| 547 bool succeeded = selector.addArgumentsToList(arguments, | 552 bool succeeded = selector.addArgumentsToList(arguments, |
| 548 compiledArguments, | 553 compiledArguments, |
| 549 target, | 554 target, |
| 550 compileArgument, | 555 compileArgument, |
| 551 compileConstant, | 556 compileConstant, |
| 552 compiler); | 557 compiler); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 564 } | 569 } |
| 565 | 570 |
| 566 Send send = node.send; | 571 Send send = node.send; |
| 567 FunctionElement constructor = elements[send]; | 572 FunctionElement constructor = elements[send]; |
| 568 ClassElement classElement = constructor.getEnclosingClass(); | 573 ClassElement classElement = constructor.getEnclosingClass(); |
| 569 if (classElement.isInterface()) { | 574 if (classElement.isInterface()) { |
| 570 compiler.resolver.resolveMethodElement(constructor); | 575 compiler.resolver.resolveMethodElement(constructor); |
| 571 constructor = constructor.defaultImplementation; | 576 constructor = constructor.defaultImplementation; |
| 572 classElement = constructor.getEnclosingClass(); | 577 classElement = constructor.getEnclosingClass(); |
| 573 } | 578 } |
| 579 // The constructor must be an implementation to ensure that field |
| 580 // initializers are handled correctly. |
| 581 constructor = constructor.implementation; |
| 582 assert(invariant(node, constructor.isImplementation)); |
| 574 | 583 |
| 575 Selector selector = elements.getSelector(send); | 584 Selector selector = elements.getSelector(send); |
| 576 List<Constant> arguments = evaluateArgumentsToConstructor( | 585 List<Constant> arguments = evaluateArgumentsToConstructor( |
| 577 node, selector, send.arguments, constructor); | 586 node, selector, send.arguments, constructor); |
| 578 ConstructorEvaluator evaluator = | 587 ConstructorEvaluator evaluator = |
| 579 new ConstructorEvaluator(constructor, constantSystem, compiler); | 588 new ConstructorEvaluator(node, constructor, constantSystem, compiler); |
| 580 evaluator.evaluateConstructorFieldValues(arguments); | 589 evaluator.evaluateConstructorFieldValues(arguments); |
| 581 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); | 590 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); |
| 582 | 591 |
| 583 compiler.enqueuer.codegen.registerInstantiatedClass(classElement); | 592 compiler.enqueuer.codegen.registerInstantiatedClass(classElement); |
| 584 // TODO(floitsch): take generic types into account. | 593 // TODO(floitsch): take generic types into account. |
| 585 DartType type = classElement.computeType(compiler); | 594 DartType type = classElement.computeType(compiler); |
| 586 Constant constant = new ConstructedConstant(type, jsNewArguments); | 595 Constant constant = new ConstructedConstant(type, jsNewArguments); |
| 587 compiler.constantHandler.registerCompileTimeConstant(constant); | 596 compiler.constantHandler.registerCompileTimeConstant(constant); |
| 588 return constant; | 597 return constant; |
| 589 } | 598 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 619 : super(constantSystem, elements, compiler, isConst: true); | 628 : super(constantSystem, elements, compiler, isConst: true); |
| 620 | 629 |
| 621 error(Node node) { | 630 error(Node node) { |
| 622 // Just fail without reporting it anywhere. | 631 // Just fail without reporting it anywhere. |
| 623 throw new CompileTimeConstantError( | 632 throw new CompileTimeConstantError( |
| 624 MessageKind.NOT_A_COMPILE_TIME_CONSTANT, const []); | 633 MessageKind.NOT_A_COMPILE_TIME_CONSTANT, const []); |
| 625 } | 634 } |
| 626 } | 635 } |
| 627 | 636 |
| 628 class ConstructorEvaluator extends CompileTimeConstantEvaluator { | 637 class ConstructorEvaluator extends CompileTimeConstantEvaluator { |
| 629 FunctionElement constructor; | 638 final FunctionElement constructor; |
| 630 final Map<Element, Constant> definitions; | 639 final Map<Element, Constant> definitions; |
| 631 final Map<Element, Constant> fieldValues; | 640 final Map<Element, Constant> fieldValues; |
| 632 | 641 |
| 633 ConstructorEvaluator(FunctionElement constructor, | 642 /** |
| 643 * Documentation wanted -- johnniwinther |
| 644 * |
| 645 * Invariant: [constructor] must be an implementation element. |
| 646 */ |
| 647 ConstructorEvaluator(Node node, |
| 648 FunctionElement constructor, |
| 634 ConstantSystem constantSystem, | 649 ConstantSystem constantSystem, |
| 635 Compiler compiler) | 650 Compiler compiler) |
| 636 : this.constructor = constructor, | 651 : this.constructor = constructor, |
| 637 this.definitions = new Map<Element, Constant>(), | 652 this.definitions = new Map<Element, Constant>(), |
| 638 this.fieldValues = new Map<Element, Constant>(), | 653 this.fieldValues = new Map<Element, Constant>(), |
| 639 super(constantSystem, | 654 super(constantSystem, |
| 640 compiler.resolver.resolveMethodElement(constructor), | 655 compiler.resolver.resolveMethodElement(constructor.declaration), |
| 641 compiler, | 656 compiler, |
| 642 isConst: true); | 657 isConst: true) { |
| 658 assert(invariant(node, constructor.isImplementation)); |
| 659 } |
| 643 | 660 |
| 644 Constant visitSend(Send send) { | 661 Constant visitSend(Send send) { |
| 645 Element element = elements[send]; | 662 Element element = elements[send]; |
| 646 if (Elements.isLocal(element)) { | 663 if (Elements.isLocal(element)) { |
| 647 Constant constant = definitions[element]; | 664 Constant constant = definitions[element]; |
| 648 if (constant === null) { | 665 if (constant === null) { |
| 649 compiler.internalError("Local variable without value", node: send); | 666 compiler.internalError("Local variable without value", node: send); |
| 650 } | 667 } |
| 651 return constant; | 668 return constant; |
| 652 } | 669 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 694 } | 711 } |
| 695 | 712 |
| 696 void evaluateSuperOrRedirectSend(Node currentNode, | 713 void evaluateSuperOrRedirectSend(Node currentNode, |
| 697 Selector selector, | 714 Selector selector, |
| 698 Link<Node> arguments, | 715 Link<Node> arguments, |
| 699 FunctionElement targetConstructor) { | 716 FunctionElement targetConstructor) { |
| 700 List<Constant> compiledArguments = evaluateArgumentsToConstructor( | 717 List<Constant> compiledArguments = evaluateArgumentsToConstructor( |
| 701 currentNode, selector, arguments, targetConstructor); | 718 currentNode, selector, arguments, targetConstructor); |
| 702 | 719 |
| 703 ConstructorEvaluator evaluator = new ConstructorEvaluator( | 720 ConstructorEvaluator evaluator = new ConstructorEvaluator( |
| 704 targetConstructor, constantSystem, compiler); | 721 currentNode, targetConstructor, constantSystem, compiler); |
| 705 evaluator.evaluateConstructorFieldValues(compiledArguments); | 722 evaluator.evaluateConstructorFieldValues(compiledArguments); |
| 706 // Copy over the fieldValues from the super/redirect-constructor. | 723 // Copy over the fieldValues from the super/redirect-constructor. |
| 707 // No need to go through [updateFieldValue] because the | 724 // No need to go through [updateFieldValue] because the |
| 708 // assignments have already been checked in checked mode. | 725 // assignments have already been checked in checked mode. |
| 709 evaluator.fieldValues.forEach((key, value) => fieldValues[key] = value); | 726 evaluator.fieldValues.forEach((key, value) => fieldValues[key] = value); |
| 710 } | 727 } |
| 711 | 728 |
| 712 /** | 729 /** |
| 713 * Runs through the initializers of the given [constructor] and updates | 730 * Runs through the initializers of the given [constructor] and updates |
| 714 * the [fieldValues] map. | 731 * the [fieldValues] map. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 Constant fieldValue = fieldValues[field]; | 808 Constant fieldValue = fieldValues[field]; |
| 792 if (fieldValue === null) { | 809 if (fieldValue === null) { |
| 793 // Use the default value. | 810 // Use the default value. |
| 794 fieldValue = compiler.compileConstant(field); | 811 fieldValue = compiler.compileConstant(field); |
| 795 } | 812 } |
| 796 jsNewArguments.add(fieldValue); | 813 jsNewArguments.add(fieldValue); |
| 797 }); | 814 }); |
| 798 return jsNewArguments; | 815 return jsNewArguments; |
| 799 } | 816 } |
| 800 } | 817 } |
| OLD | NEW |