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 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 value = new NullConstant(); | 193 value = new NullConstant(); |
194 } else { | 194 } else { |
195 Node right = assignment.arguments.head; | 195 Node right = assignment.arguments.head; |
196 value = | 196 value = |
197 compileNodeWithDefinitions(right, definitions, isConst: isConst); | 197 compileNodeWithDefinitions(right, definitions, isConst: isConst); |
198 if (compiler.enableTypeAssertions | 198 if (compiler.enableTypeAssertions |
199 && value != null | 199 && value != null |
200 && element.isField()) { | 200 && element.isField()) { |
201 DartType elementType = element.computeType(compiler); | 201 DartType elementType = element.computeType(compiler); |
202 DartType constantType = value.computeType(compiler); | 202 DartType constantType = value.computeType(compiler); |
203 if (elementType.isMalformed || constantType.isMalformed || | 203 if (!constantSystem.isSubtype(compiler, constantType, elementType)) { |
204 !constantSystem.isSubtype(compiler, constantType, elementType)) { | |
205 if (isConst) { | 204 if (isConst) { |
206 compiler.reportError(node, new CompileTimeConstantError( | 205 compiler.reportError(node, new CompileTimeConstantError( |
207 MessageKind.NOT_ASSIGNABLE, | 206 MessageKind.NOT_ASSIGNABLE, |
208 {'fromType': constantType, 'toType': elementType})); | 207 {'fromType': constantType, 'toType': elementType})); |
209 } else { | 208 } else { |
210 // If the field can be lazily initialized, we will throw | 209 // If the field can be lazily initialized, we will throw |
211 // the exception at runtime. | 210 // the exception at runtime. |
212 value = null; | 211 value = null; |
213 } | 212 } |
214 } | 213 } |
(...skipping 571 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 } | 785 } |
787 return super.visitSend(send); | 786 return super.visitSend(send); |
788 } | 787 } |
789 | 788 |
790 void potentiallyCheckType(Node node, Element element, Constant constant) { | 789 void potentiallyCheckType(Node node, Element element, Constant constant) { |
791 if (compiler.enableTypeAssertions) { | 790 if (compiler.enableTypeAssertions) { |
792 DartType elementType = element.computeType(compiler); | 791 DartType elementType = element.computeType(compiler); |
793 DartType constantType = constant.computeType(compiler); | 792 DartType constantType = constant.computeType(compiler); |
794 // TODO(ngeoffray): Handle type parameters. | 793 // TODO(ngeoffray): Handle type parameters. |
795 if (elementType.element.isTypeVariable()) return; | 794 if (elementType.element.isTypeVariable()) return; |
796 if (elementType.isMalformed || constantType.isMalformed || | 795 if (!constantSystem.isSubtype(compiler, constantType, elementType)) { |
797 !constantSystem.isSubtype(compiler, constantType, elementType)) { | |
798 compiler.reportError(node, new CompileTimeConstantError( | 796 compiler.reportError(node, new CompileTimeConstantError( |
799 MessageKind.NOT_ASSIGNABLE, | 797 MessageKind.NOT_ASSIGNABLE, |
800 {'fromType': elementType, 'toType': constantType})); | 798 {'fromType': elementType, 'toType': constantType})); |
801 } | 799 } |
802 } | 800 } |
803 } | 801 } |
804 | 802 |
805 void updateFieldValue(Node node, Element element, Constant constant) { | 803 void updateFieldValue(Node node, Element element, Constant constant) { |
806 potentiallyCheckType(node, element, constant); | 804 potentiallyCheckType(node, element, constant); |
807 fieldValues[element] = constant; | 805 fieldValues[element] = constant; |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
926 if (fieldValue == null) { | 924 if (fieldValue == null) { |
927 // Use the default value. | 925 // Use the default value. |
928 fieldValue = handler.compileConstant(field); | 926 fieldValue = handler.compileConstant(field); |
929 } | 927 } |
930 jsNewArguments.add(fieldValue); | 928 jsNewArguments.add(fieldValue); |
931 }, | 929 }, |
932 includeSuperAndInjectedMembers: true); | 930 includeSuperAndInjectedMembers: true); |
933 return jsNewArguments; | 931 return jsNewArguments; |
934 } | 932 } |
935 } | 933 } |
OLD | NEW |