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 library dart2js.typechecker; | 5 library dart2js.typechecker; |
6 | 6 |
7 import 'common/names.dart' show Identifiers; | 7 import 'common/names.dart' show Identifiers; |
8 import 'common/resolution.dart' show Resolution; | 8 import 'common/resolution.dart' show Resolution; |
9 import 'common/tasks.dart' show CompilerTask; | 9 import 'common/tasks.dart' show CompilerTask; |
10 import 'common.dart'; | 10 import 'common.dart'; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 void check(AstElement element) { | 57 void check(AstElement element) { |
58 if (element.isClass) return; | 58 if (element.isClass) return; |
59 if (element.isTypedef) return; | 59 if (element.isTypedef) return; |
60 ResolvedAst resolvedAst = element.resolvedAst; | 60 ResolvedAst resolvedAst = element.resolvedAst; |
61 reporter.withCurrentElement(element.implementation, () { | 61 reporter.withCurrentElement(element.implementation, () { |
62 measure(() { | 62 measure(() { |
63 TypeCheckerVisitor visitor = new TypeCheckerVisitor( | 63 TypeCheckerVisitor visitor = new TypeCheckerVisitor( |
64 compiler, resolvedAst.elements, compiler.types); | 64 compiler, resolvedAst.elements, compiler.types); |
65 if (element.isField) { | 65 if (element.isField) { |
66 visitor.analyzingInitializer = true; | 66 visitor.analyzingInitializer = true; |
| 67 DartType type = |
| 68 visitor.analyzeVariableTypeAnnotation(resolvedAst.node); |
| 69 visitor.analyzeVariableInitializer(element, type, resolvedAst.body); |
| 70 } else { |
| 71 resolvedAst.node.accept(visitor); |
67 } | 72 } |
68 resolvedAst.node.accept(visitor); | |
69 }); | 73 }); |
70 }); | 74 }); |
71 } | 75 } |
72 } | 76 } |
73 | 77 |
74 /** | 78 /** |
75 * Class used to report different warnings for different kinds of members. | 79 * Class used to report different warnings for different kinds of members. |
76 */ | 80 */ |
77 class MemberKind { | 81 class MemberKind { |
78 static const MemberKind METHOD = const MemberKind("method"); | 82 static const MemberKind METHOD = const MemberKind("method"); |
(...skipping 1555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1634 return constructorType; | 1638 return constructorType; |
1635 } | 1639 } |
1636 | 1640 |
1637 DartType visitNewExpression(NewExpression node) { | 1641 DartType visitNewExpression(NewExpression node) { |
1638 Element element = elements[node.send]; | 1642 Element element = elements[node.send]; |
1639 if (Elements.isUnresolved(element)) return const DynamicType(); | 1643 if (Elements.isUnresolved(element)) return const DynamicType(); |
1640 | 1644 |
1641 checkPrivateAccess(node, element, element.name); | 1645 checkPrivateAccess(node, element, element.name); |
1642 | 1646 |
1643 DartType newType = elements.getType(node); | 1647 DartType newType = elements.getType(node); |
| 1648 assert(invariant(node, newType != null, |
| 1649 message: "No new type registered in $elements.")); |
1644 DartType constructorType = computeConstructorType(element, newType); | 1650 DartType constructorType = computeConstructorType(element, newType); |
1645 analyzeArguments(node.send, element, constructorType); | 1651 analyzeArguments(node.send, element, constructorType); |
1646 return newType; | 1652 return newType; |
1647 } | 1653 } |
1648 | 1654 |
1649 DartType visitLiteralList(LiteralList node) { | 1655 DartType visitLiteralList(LiteralList node) { |
1650 InterfaceType listType = elements.getType(node); | 1656 InterfaceType listType = elements.getType(node); |
1651 DartType listElementType = firstType(listType.typeArguments); | 1657 DartType listElementType = firstType(listType.typeArguments); |
1652 for (Link<Node> link = node.elements.nodes; | 1658 for (Link<Node> link = node.elements.nodes; |
1653 !link.isEmpty; | 1659 !link.isEmpty; |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1751 } | 1757 } |
1752 // The static type of the result must be assignable to the declared type. | 1758 // The static type of the result must be assignable to the declared type. |
1753 checkAssignable(node, resultType, expectedReturnType); | 1759 checkAssignable(node, resultType, expectedReturnType); |
1754 return const StatementType(); | 1760 return const StatementType(); |
1755 } | 1761 } |
1756 | 1762 |
1757 DartType visitTypeAnnotation(TypeAnnotation node) { | 1763 DartType visitTypeAnnotation(TypeAnnotation node) { |
1758 return elements.getType(node); | 1764 return elements.getType(node); |
1759 } | 1765 } |
1760 | 1766 |
1761 DartType visitVariableDefinitions(VariableDefinitions node) { | 1767 DartType analyzeVariableTypeAnnotation(VariableDefinitions node) { |
1762 DartType type = analyzeWithDefault(node.type, const DynamicType()); | 1768 DartType type = analyzeWithDefault(node.type, const DynamicType()); |
1763 if (type.isVoid) { | 1769 if (type.isVoid) { |
1764 reportTypeWarning(node.type, MessageKind.VOID_VARIABLE); | 1770 reportTypeWarning(node.type, MessageKind.VOID_VARIABLE); |
1765 type = const DynamicType(); | 1771 type = const DynamicType(); |
1766 } | 1772 } |
| 1773 return type; |
| 1774 } |
| 1775 |
| 1776 void analyzeVariableInitializer( |
| 1777 Spannable spannable, DartType declaredType, Node initializer) { |
| 1778 if (initializer == null) return; |
| 1779 |
| 1780 DartType expressionType = analyzeNonVoid(initializer); |
| 1781 checkAssignable(spannable, expressionType, declaredType); |
| 1782 } |
| 1783 |
| 1784 DartType visitVariableDefinitions(VariableDefinitions node) { |
| 1785 DartType type = analyzeVariableTypeAnnotation(node); |
1767 for (Link<Node> link = node.definitions.nodes; | 1786 for (Link<Node> link = node.definitions.nodes; |
1768 !link.isEmpty; | 1787 !link.isEmpty; |
1769 link = link.tail) { | 1788 link = link.tail) { |
1770 Node definition = link.head; | 1789 Node definition = link.head; |
1771 invariant(definition, definition is Identifier || definition is SendSet, | 1790 invariant(definition, definition is Identifier || definition is SendSet, |
1772 message: 'expected identifier or initialization'); | 1791 message: 'expected identifier or initialization'); |
1773 if (definition is SendSet) { | 1792 if (definition is SendSet) { |
1774 SendSet initialization = definition; | 1793 SendSet initialization = definition; |
1775 DartType initializer = analyzeNonVoid(initialization.arguments.head); | 1794 analyzeVariableInitializer( |
1776 checkAssignable(initialization.assignmentOperator, initializer, type); | 1795 initialization.assignmentOperator, |
| 1796 type, |
| 1797 initialization.arguments.head); |
1777 // TODO(sigmund): explore inferring a type for `var` using the RHS (like | 1798 // TODO(sigmund): explore inferring a type for `var` using the RHS (like |
1778 // DDC does), for example: | 1799 // DDC does), for example: |
1779 // if (node.type == null && node.modifiers.isVar && | 1800 // if (node.type == null && node.modifiers.isVar && |
1780 // !initializer.isDynamic) { | 1801 // !initializer.isDynamic) { |
1781 // var variable = elements[definition]; | 1802 // var variable = elements[definition]; |
1782 // if (variable != null) { | 1803 // if (variable != null) { |
1783 // var typePromotion = new TypePromotion( | 1804 // var typePromotion = new TypePromotion( |
1784 // node, variable, initializer); | 1805 // node, variable, initializer); |
1785 // registerKnownTypePromotion(typePromotion); | 1806 // registerKnownTypePromotion(typePromotion); |
1786 // } | 1807 // } |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2028 | 2049 |
2029 visitTypedef(Typedef node) { | 2050 visitTypedef(Typedef node) { |
2030 // Do not typecheck [Typedef] nodes. | 2051 // Do not typecheck [Typedef] nodes. |
2031 } | 2052 } |
2032 | 2053 |
2033 visitNode(Node node) { | 2054 visitNode(Node node) { |
2034 reporter.internalError(node, | 2055 reporter.internalError(node, |
2035 'Unexpected node ${node.getObjectDescription()} in the type checker.'); | 2056 'Unexpected node ${node.getObjectDescription()} in the type checker.'); |
2036 } | 2057 } |
2037 } | 2058 } |
OLD | NEW |