| 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 /// A [ConstantEnvironment] provides access for constants compiled for variable | 7 /// A [ConstantEnvironment] provides access for constants compiled for variable |
| 8 /// initializers. | 8 /// initializers. |
| 9 abstract class ConstantEnvironment { | 9 abstract class ConstantEnvironment { |
| 10 /// Returns the constant for the initializer of [element]. | 10 /// Returns the constant for the initializer of [element]. |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 * error. | 128 * error. |
| 129 */ | 129 */ |
| 130 ConstantExpression compileVariableWithDefinitions(VariableElement element, | 130 ConstantExpression compileVariableWithDefinitions(VariableElement element, |
| 131 TreeElements definitions, | 131 TreeElements definitions, |
| 132 {bool isConst: false}) { | 132 {bool isConst: false}) { |
| 133 Node node = element.node; | 133 Node node = element.node; |
| 134 if (pendingVariables.contains(element)) { | 134 if (pendingVariables.contains(element)) { |
| 135 if (isConst) { | 135 if (isConst) { |
| 136 compiler.reportError( | 136 compiler.reportError( |
| 137 node, MessageKind.CYCLIC_COMPILE_TIME_CONSTANTS); | 137 node, MessageKind.CYCLIC_COMPILE_TIME_CONSTANTS); |
| 138 // TODO(ahe): Don't throw, recover from error. | 138 return new ErroneousConstantExpression(); |
| 139 throw new CompilerCancelledException(null); | |
| 140 } | 139 } |
| 141 return null; | 140 return null; |
| 142 } | 141 } |
| 143 pendingVariables.add(element); | 142 pendingVariables.add(element); |
| 144 | 143 |
| 145 Expression initializer = element.initializer; | 144 Expression initializer = element.initializer; |
| 146 ConstantExpression value; | 145 ConstantExpression value; |
| 147 if (initializer == null) { | 146 if (initializer == null) { |
| 148 // No initial value. | 147 // No initial value. |
| 149 value = new PrimitiveConstantExpression(new NullConstantValue()); | 148 value = new PrimitiveConstantExpression(new NullConstantValue()); |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 AstConstant visitConditional(Conditional node) { | 612 AstConstant visitConditional(Conditional node) { |
| 614 AstConstant condition = evaluate(node.condition); | 613 AstConstant condition = evaluate(node.condition); |
| 615 if (condition == null) { | 614 if (condition == null) { |
| 616 return null; | 615 return null; |
| 617 } else if (!condition.value.isBool) { | 616 } else if (!condition.value.isBool) { |
| 618 DartType conditionType = condition.value.getType(compiler.coreTypes); | 617 DartType conditionType = condition.value.getType(compiler.coreTypes); |
| 619 if (isEvaluatingConstant) { | 618 if (isEvaluatingConstant) { |
| 620 compiler.reportError( | 619 compiler.reportError( |
| 621 node.condition, MessageKind.NOT_ASSIGNABLE, | 620 node.condition, MessageKind.NOT_ASSIGNABLE, |
| 622 {'fromType': conditionType, 'toType': compiler.boolClass.rawType}); | 621 {'fromType': conditionType, 'toType': compiler.boolClass.rawType}); |
| 623 // TODO(ahe): Don't throw, recover from error. | 622 return new ErroneousAstConstant(context, node); |
| 624 throw new CompilerCancelledException(null); | |
| 625 } | 623 } |
| 626 return null; | 624 return null; |
| 627 } | 625 } |
| 628 AstConstant thenExpression = evaluate(node.thenExpression); | 626 AstConstant thenExpression = evaluate(node.thenExpression); |
| 629 AstConstant elseExpression = evaluate(node.elseExpression); | 627 AstConstant elseExpression = evaluate(node.elseExpression); |
| 630 if (thenExpression == null || elseExpression == null) { | 628 if (thenExpression == null || elseExpression == null) { |
| 631 return null; | 629 return null; |
| 632 } | 630 } |
| 633 BoolConstantValue boolCondition = condition.value; | 631 BoolConstantValue boolCondition = condition.value; |
| 634 return new AstConstant( | 632 return new AstConstant( |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 } | 664 } |
| 667 target.computeSignature(compiler); | 665 target.computeSignature(compiler); |
| 668 | 666 |
| 669 if (!selector.applies(target, compiler.world)) { | 667 if (!selector.applies(target, compiler.world)) { |
| 670 String name = Elements.constructorNameForDiagnostics( | 668 String name = Elements.constructorNameForDiagnostics( |
| 671 target.enclosingClass.name, target.name); | 669 target.enclosingClass.name, target.name); |
| 672 compiler.reportError( | 670 compiler.reportError( |
| 673 node, | 671 node, |
| 674 MessageKind.INVALID_CONSTRUCTOR_ARGUMENTS, | 672 MessageKind.INVALID_CONSTRUCTOR_ARGUMENTS, |
| 675 {'constructorName': name}); | 673 {'constructorName': name}); |
| 676 // TODO(ahe): Don't throw, recover from error. | 674 |
| 677 throw new CompilerCancelledException(null); | 675 return new List<AstConstant>.filled( |
| 676 target.functionSignature.parameterCount, |
| 677 new ErroneousAstConstant(context, node)); |
| 678 } | 678 } |
| 679 return selector.makeArgumentsList2(arguments, | 679 return selector.makeArgumentsList2(arguments, |
| 680 target, | 680 target, |
| 681 compileArgument, | 681 compileArgument, |
| 682 compileDefaultValue); | 682 compileDefaultValue); |
| 683 } | 683 } |
| 684 | 684 |
| 685 AstConstant visitNewExpression(NewExpression node) { | 685 AstConstant visitNewExpression(NewExpression node) { |
| 686 if (!node.isConst) { | 686 if (!node.isConst) { |
| 687 return signalNotCompileTimeConstant(node); | 687 return signalNotCompileTimeConstant(node); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 819 static AstConstant makeConstructedConstant( | 819 static AstConstant makeConstructedConstant( |
| 820 Compiler compiler, | 820 Compiler compiler, |
| 821 ConstantCompilerBase handler, | 821 ConstantCompilerBase handler, |
| 822 Element context, | 822 Element context, |
| 823 Node node, | 823 Node node, |
| 824 InterfaceType type, | 824 InterfaceType type, |
| 825 ConstructorElement constructor, | 825 ConstructorElement constructor, |
| 826 Selector selector, | 826 Selector selector, |
| 827 List<AstConstant> concreteArguments, | 827 List<AstConstant> concreteArguments, |
| 828 List<AstConstant> normalizedArguments) { | 828 List<AstConstant> normalizedArguments) { |
| 829 assert(invariant(node, selector.applies(constructor, compiler.world), | 829 assert(invariant(node, selector.applies(constructor, compiler.world) || |
| 830 compiler.compilationFailed, |
| 830 message: "Selector $selector does not apply to constructor " | 831 message: "Selector $selector does not apply to constructor " |
| 831 "$constructor.")); | 832 "$constructor.")); |
| 832 | 833 |
| 833 // The redirection chain of this element may not have been resolved through | 834 // The redirection chain of this element may not have been resolved through |
| 834 // a post-process action, so we have to make sure it is done here. | 835 // a post-process action, so we have to make sure it is done here. |
| 835 compiler.resolver.resolveRedirectionChain(constructor, node); | 836 compiler.resolver.resolveRedirectionChain(constructor, node); |
| 836 InterfaceType constructedType = | 837 InterfaceType constructedType = |
| 837 constructor.computeEffectiveTargetType(type); | 838 constructor.computeEffectiveTargetType(type); |
| 838 ConstructorElement target = constructor.effectiveTarget; | 839 ConstructorElement target = constructor.effectiveTarget; |
| 839 ClassElement classElement = target.enclosingClass; | 840 ClassElement classElement = target.enclosingClass; |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1110 ConstantExpression constant) { | 1111 ConstantExpression constant) { |
| 1111 return new AstConstant( | 1112 return new AstConstant( |
| 1112 element, | 1113 element, |
| 1113 element.initializer != null ? element.initializer : element.node, | 1114 element.initializer != null ? element.initializer : element.node, |
| 1114 constant); | 1115 constant); |
| 1115 } | 1116 } |
| 1116 | 1117 |
| 1117 ConstantValue get value => expression.value; | 1118 ConstantValue get value => expression.value; |
| 1118 | 1119 |
| 1119 String toString() => expression.toString(); | 1120 String toString() => expression.toString(); |
| 1120 } | 1121 } |
| 1122 |
| 1123 /// A synthetic constant used to recover from errors. |
| 1124 class ErroneousAstConstant extends AstConstant { |
| 1125 ErroneousAstConstant(Element element, Node node) |
| 1126 : super(element, node, new ErroneousConstantExpression()); |
| 1127 } |
| OLD | NEW |