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 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The [ConstantHandler] keeps track of compile-time constants, | 8 * The [ConstantHandler] keeps track of compile-time constants, |
| 9 * initializations of global and static fields, and default values of | 9 * initializations of global and static fields, and default values of |
| 10 * optional parameters. | 10 * optional parameters. |
| (...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 Constant visitNewExpression(NewExpression node) { | 621 Constant visitNewExpression(NewExpression node) { |
| 622 if (!node.isConst()) { | 622 if (!node.isConst()) { |
| 623 return signalNotCompileTimeConstant(node); | 623 return signalNotCompileTimeConstant(node); |
| 624 } | 624 } |
| 625 | 625 |
| 626 Send send = node.send; | 626 Send send = node.send; |
| 627 FunctionElement constructor = elements[send]; | 627 FunctionElement constructor = elements[send]; |
| 628 if (Elements.isUnresolved(constructor)) { | 628 if (Elements.isUnresolved(constructor)) { |
| 629 return signalNotCompileTimeConstant(node); | 629 return signalNotCompileTimeConstant(node); |
| 630 } | 630 } |
| 631 | |
| 632 // Deferred types can not be used in const instance creation expressions. | |
| 633 // Check if the constructor comes from a deferred library. | |
| 634 Send selectorSend = node.send.selector.asSend(); | |
| 635 if (selectorSend != null) { | |
| 636 Identifier receiver = selectorSend.receiver.asIdentifier(); | |
| 637 if (receiver != null) { | |
| 638 Element element = elements[receiver]; | |
| 639 if (element.isPrefix() && (element as PrefixElement).isDeferred) { | |
| 640 return signalNotCompileTimeConstant(node, | |
| 641 message: MessageKind.DEFERRED_COMPILE_TIME_CONSTANT); | |
| 642 } | |
| 643 } | |
| 644 } | |
| 645 | |
| 631 // TODO(ahe): This is nasty: we must eagerly analyze the | 646 // TODO(ahe): This is nasty: we must eagerly analyze the |
| 632 // constructor to ensure the redirectionTarget has been computed | 647 // constructor to ensure the redirectionTarget has been computed |
| 633 // correctly. Find a way to avoid this. | 648 // correctly. Find a way to avoid this. |
| 634 compiler.analyzeElement(constructor.declaration); | 649 compiler.analyzeElement(constructor.declaration); |
| 635 | 650 |
| 636 InterfaceType type = elements.getType(node); | 651 InterfaceType type = elements.getType(node); |
| 637 List<Constant> evaluateArguments(FunctionElement constructor) { | 652 List<Constant> evaluateArguments(FunctionElement constructor) { |
| 638 Selector selector = elements.getSelector(send); | 653 Selector selector = elements.getSelector(send); |
| 639 return evaluateArgumentsToConstructor( | 654 return evaluateArgumentsToConstructor( |
| 640 node, selector, send.arguments, constructor); | 655 node, selector, send.arguments, constructor); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 732 evaluator.evaluateConstructorFieldValues(arguments); | 747 evaluator.evaluateConstructorFieldValues(arguments); |
| 733 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); | 748 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); |
| 734 | 749 |
| 735 return new ConstructedConstant(constructedType, jsNewArguments); | 750 return new ConstructedConstant(constructedType, jsNewArguments); |
| 736 } | 751 } |
| 737 | 752 |
| 738 Constant visitParenthesizedExpression(ParenthesizedExpression node) { | 753 Constant visitParenthesizedExpression(ParenthesizedExpression node) { |
| 739 return node.expression.accept(this); | 754 return node.expression.accept(this); |
| 740 } | 755 } |
| 741 | 756 |
| 742 error(Node node) { | 757 error(Node node, MessageKind message) { |
| 743 // TODO(floitsch): get the list of constants that are currently compiled | 758 // TODO(floitsch): get the list of constants that are currently compiled |
| 744 // and present some kind of stack-trace. | 759 // and present some kind of stack-trace. |
| 745 compiler.reportFatalError( | 760 compiler.reportFatalError( |
|
floitsch
2014/03/06 15:32:58
move on same line.
sigurdm
2014/03/07 12:50:49
Done.
| |
| 746 node, MessageKind.NOT_A_COMPILE_TIME_CONSTANT); | 761 node, message); |
| 747 } | 762 } |
| 748 | 763 |
| 749 Constant signalNotCompileTimeConstant(Node node) { | 764 Constant signalNotCompileTimeConstant(Node node, |
| 765 {MessageKind message: MessageKind.NOT_A_COMPILE_TIME_CONSTANT}) { | |
| 750 if (isEvaluatingConstant) { | 766 if (isEvaluatingConstant) { |
| 751 error(node); | 767 error(node, message); |
| 752 } | 768 } |
| 753 // Else we don't need to do anything. The final handler is only | 769 // Else we don't need to do anything. The final handler is only |
| 754 // optimistically trying to compile constants. So it is normal that we | 770 // optimistically trying to compile constants. So it is normal that we |
| 755 // sometimes see non-compile time constants. | 771 // sometimes see non-compile time constants. |
| 756 // Simply return [:null:] which is used to propagate a failing | 772 // Simply return [:null:] which is used to propagate a failing |
| 757 // compile-time compilation. | 773 // compile-time compilation. |
| 758 return null; | 774 return null; |
| 759 } | 775 } |
| 760 } | 776 } |
| 761 | 777 |
| 762 class TryCompileTimeConstantEvaluator extends CompileTimeConstantEvaluator { | 778 class TryCompileTimeConstantEvaluator extends CompileTimeConstantEvaluator { |
| 763 TryCompileTimeConstantEvaluator(ConstantHandler handler, | 779 TryCompileTimeConstantEvaluator(ConstantHandler handler, |
| 764 TreeElements elements, | 780 TreeElements elements, |
| 765 Compiler compiler) | 781 Compiler compiler) |
| 766 : super(handler, elements, compiler, isConst: true); | 782 : super(handler, elements, compiler, isConst: true); |
| 767 | 783 |
| 768 error(Node node) { | 784 error(Node node, MessageKind message) { |
| 769 // Just fail without reporting it anywhere. | 785 // Just fail without reporting it anywhere. |
| 770 throw new CompileTimeConstantError( | 786 throw new CompileTimeConstantError( |
| 771 MessageKind.NOT_A_COMPILE_TIME_CONSTANT, const {}, | 787 message, const {}, |
| 772 compiler.terseDiagnostics); | 788 compiler.terseDiagnostics); |
|
floitsch
2014/03/06 15:32:58
move one line up.
sigurdm
2014/03/07 12:50:49
Done.
| |
| 773 } | 789 } |
| 774 } | 790 } |
| 775 | 791 |
| 776 class CompileTimeConstantError { | 792 class CompileTimeConstantError { |
| 777 final Message message; | 793 final Message message; |
| 778 CompileTimeConstantError(MessageKind kind, Map arguments, bool terse) | 794 CompileTimeConstantError(MessageKind kind, Map arguments, bool terse) |
| 779 : message = new Message(kind, arguments, terse); | 795 : message = new Message(kind, arguments, terse); |
| 780 String toString() => message.toString(); | 796 String toString() => message.toString(); |
| 781 } | 797 } |
| 782 | 798 |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 960 if (fieldValue == null) { | 976 if (fieldValue == null) { |
| 961 // Use the default value. | 977 // Use the default value. |
| 962 fieldValue = handler.compileConstant(field); | 978 fieldValue = handler.compileConstant(field); |
| 963 } | 979 } |
| 964 jsNewArguments.add(fieldValue); | 980 jsNewArguments.add(fieldValue); |
| 965 }, | 981 }, |
| 966 includeSuperAndInjectedMembers: true); | 982 includeSuperAndInjectedMembers: true); |
| 967 return jsNewArguments; | 983 return jsNewArguments; |
| 968 } | 984 } |
| 969 } | 985 } |
| OLD | NEW |