Chromium Code Reviews| 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(target, target.isImplementation)); | |
|
ahe
2012/09/20 11:12:07
First argument should be node.
Johnni Winther
2012/09/21 09:18:25
Done.
| |
| 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(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); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 |
| 642 /** | |
| 643 * Documentation wanted -- johnniwinther | |
| 644 * | |
| 645 * Invariant: [constructor] must be an implementation element. | |
| 646 */ | |
| 633 ConstructorEvaluator(FunctionElement constructor, | 647 ConstructorEvaluator(FunctionElement constructor, |
| 634 ConstantSystem constantSystem, | 648 ConstantSystem constantSystem, |
| 635 Compiler compiler) | 649 Compiler compiler) |
| 636 : this.constructor = constructor, | 650 : this.constructor = constructor, |
| 637 this.definitions = new Map<Element, Constant>(), | 651 this.definitions = new Map<Element, Constant>(), |
| 638 this.fieldValues = new Map<Element, Constant>(), | 652 this.fieldValues = new Map<Element, Constant>(), |
| 639 super(constantSystem, | 653 super(constantSystem, |
| 640 compiler.resolver.resolveMethodElement(constructor), | 654 compiler.resolver.resolveMethodElement(constructor.declaration), |
| 641 compiler, | 655 compiler, |
| 642 isConst: true); | 656 isConst: true) { |
| 657 assert(invariant(constructor, constructor.isImplementation)); | |
| 658 } | |
| 643 | 659 |
| 644 Constant visitSend(Send send) { | 660 Constant visitSend(Send send) { |
| 645 Element element = elements[send]; | 661 Element element = elements[send]; |
| 646 if (Elements.isLocal(element)) { | 662 if (Elements.isLocal(element)) { |
| 647 Constant constant = definitions[element]; | 663 Constant constant = definitions[element]; |
| 648 if (constant === null) { | 664 if (constant === null) { |
| 649 compiler.internalError("Local variable without value", node: send); | 665 compiler.internalError("Local variable without value", node: send); |
| 650 } | 666 } |
| 651 return constant; | 667 return constant; |
| 652 } | 668 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 791 Constant fieldValue = fieldValues[field]; | 807 Constant fieldValue = fieldValues[field]; |
| 792 if (fieldValue === null) { | 808 if (fieldValue === null) { |
| 793 // Use the default value. | 809 // Use the default value. |
| 794 fieldValue = compiler.compileConstant(field); | 810 fieldValue = compiler.compileConstant(field); |
| 795 } | 811 } |
| 796 jsNewArguments.add(fieldValue); | 812 jsNewArguments.add(fieldValue); |
| 797 }); | 813 }); |
| 798 return jsNewArguments; | 814 return jsNewArguments; |
| 799 } | 815 } |
| 800 } | 816 } |
| OLD | NEW |