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 interface TreeElements { | 5 interface TreeElements { |
6 Element operator[](Node node); | 6 Element operator[](Node node); |
7 Selector getSelector(Send send); | 7 Selector getSelector(Send send); |
8 Type getType(TypeAnnotation annotation); | 8 Type getType(TypeAnnotation annotation); |
9 } | 9 } |
10 | 10 |
(...skipping 1339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1350 world.registerInstantiatedClass(cls); | 1350 world.registerInstantiatedClass(cls); |
1351 cls.forEachInstanceField( | 1351 cls.forEachInstanceField( |
1352 includeBackendMembers: false, | 1352 includeBackendMembers: false, |
1353 includeSuperMembers: true, | 1353 includeSuperMembers: true, |
1354 f: (ClassElement enclosingClass, Element member) { | 1354 f: (ClassElement enclosingClass, Element member) { |
1355 world.addToWorkList(member); | 1355 world.addToWorkList(member); |
1356 }); | 1356 }); |
1357 return null; | 1357 return null; |
1358 } | 1358 } |
1359 | 1359 |
1360 TypeAnnotation getTypeAnnotationFromSend(Send send) { | |
1361 if (send.selector.asTypeAnnotation() !== null) { | |
1362 return send.selector; | |
1363 } else if (send.selector.asSend() !== null) { | |
1364 Send selector = send.selector; | |
1365 if (selector.receiver.asTypeAnnotation() !== null) { | |
1366 return selector.receiver; | |
1367 } | |
1368 } else { | |
1369 compiler.internalError("malformed send in new expression"); | |
1370 } | |
1371 } | |
1372 | |
1373 FunctionElement resolveConstructor(NewExpression node) { | 1360 FunctionElement resolveConstructor(NewExpression node) { |
1374 FunctionElement constructor = | 1361 FunctionElement constructor = |
1375 node.accept(new ConstructorResolver(compiler, this)); | 1362 node.accept(new ConstructorResolver(compiler, this)); |
1376 TypeAnnotation annotation = getTypeAnnotationFromSend(node.send); | 1363 TypeAnnotation annotation = node.getTypeAnnotation(); |
| 1364 if (annotation == null) { |
| 1365 compiler.internalError("malformed send in new expression"); |
| 1366 } |
1377 Type type = resolveTypeRequired(annotation); | 1367 Type type = resolveTypeRequired(annotation); |
1378 if (constructor === null) { | 1368 if (constructor === null) { |
1379 Element resolved = (type != null) ? type.element : null; | 1369 Element resolved = (type != null) ? type.element : null; |
1380 if (resolved !== null && resolved.kind === ElementKind.TYPE_VARIABLE) { | 1370 if (resolved !== null && resolved.kind === ElementKind.TYPE_VARIABLE) { |
1381 error(node, MessageKind.TYPE_VARIABLE_AS_CONSTRUCTOR); | 1371 error(node, MessageKind.TYPE_VARIABLE_AS_CONSTRUCTOR); |
1382 return null; | 1372 return null; |
1383 } else { | 1373 } else { |
1384 error(node.send, MessageKind.CANNOT_FIND_CONSTRUCTOR, [node.send]); | 1374 error(node.send, MessageKind.CANNOT_FIND_CONSTRUCTOR, [node.send]); |
1385 return null; | 1375 return null; |
1386 } | 1376 } |
| 1377 } else { |
| 1378 useType(annotation, type); |
1387 } | 1379 } |
1388 return constructor; | 1380 return constructor; |
1389 } | 1381 } |
1390 | 1382 |
1391 Type resolveTypeRequired(TypeAnnotation node) { | 1383 Type resolveTypeRequired(TypeAnnotation node) { |
1392 bool old = typeRequired; | 1384 bool old = typeRequired; |
1393 typeRequired = true; | 1385 typeRequired = true; |
1394 Type result = resolveTypeAnnotation(node); | 1386 Type result = resolveTypeAnnotation(node); |
1395 typeRequired = old; | 1387 typeRequired = old; |
1396 return result; | 1388 return result; |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1722 addDefaultConstructorIfNeeded(classElement); | 1714 addDefaultConstructorIfNeeded(classElement); |
1723 return classElement.computeType(compiler); | 1715 return classElement.computeType(compiler); |
1724 } | 1716 } |
1725 | 1717 |
1726 Type visitTypeAnnotation(TypeAnnotation node) { | 1718 Type visitTypeAnnotation(TypeAnnotation node) { |
1727 Type type = visit(node.typeName); | 1719 Type type = visit(node.typeName); |
1728 if (type is! InterfaceType) { | 1720 if (type is! InterfaceType) { |
1729 // TODO(johnniwinther): Handle function types. | 1721 // TODO(johnniwinther): Handle function types. |
1730 return type; | 1722 return type; |
1731 } | 1723 } |
| 1724 int typeParameterCount = type.element.typeParameters.length; |
1732 if (node.typeArguments === null) { | 1725 if (node.typeArguments === null) { |
1733 if (type.arguments.isEmpty()) { | 1726 if (type.typeArguments.isEmpty() || typeParameterCount == 0) { |
1734 return type; | 1727 return type; |
1735 } | 1728 } |
1736 // Return the 'raw' version, for example List for List<E>. | 1729 // Return the 'raw' version, for example List for List<E>. |
1737 return new InterfaceType(type.element); | 1730 return new InterfaceType(type.element); |
1738 } | 1731 } |
| 1732 int typeArgumentCount = node.typeArguments.length(); |
| 1733 if (typeArgumentCount < typeParameterCount) { |
| 1734 error(node.typeArguments, MessageKind.MISSING_TYPE_ARGUMENT); |
| 1735 } else if (typeArgumentCount > typeParameterCount) { |
| 1736 error(node.typeArguments, MessageKind.ADDITIONAL_TYPE_ARGUMENT); |
| 1737 } |
1739 var typeArguments = new LinkBuilder<Type>(); | 1738 var typeArguments = new LinkBuilder<Type>(); |
1740 var link = node.typeArguments.nodes; | 1739 var link = node.typeArguments.nodes; |
1741 while (!link.isEmpty()) { | 1740 while (!link.isEmpty()) { |
1742 typeArguments.addLast(visit(link.head)); | 1741 typeArguments.addLast(visit(link.head)); |
1743 link = link.tail; | 1742 link = link.tail; |
1744 } | 1743 } |
1745 // TODO(johnniwinther): Check argument count and bounds. | 1744 // TODO(johnniwinther): Check argument count and bounds. |
1746 return new InterfaceType(type.element, typeArguments.toLink()); | 1745 return new InterfaceType(type.element, typeArguments.toLink()); |
1747 } | 1746 } |
1748 | 1747 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1789 PrefixElement prefixElement = element; | 1788 PrefixElement prefixElement = element; |
1790 Identifier selector = node.selector.asIdentifier(); | 1789 Identifier selector = node.selector.asIdentifier(); |
1791 var e = prefixElement.lookupLocalMember(selector.source); | 1790 var e = prefixElement.lookupLocalMember(selector.source); |
1792 if (e === null || !e.impliesType()) { | 1791 if (e === null || !e.impliesType()) { |
1793 error(node.selector, MessageKind.CANNOT_RESOLVE_TYPE, [node.selector]); | 1792 error(node.selector, MessageKind.CANNOT_RESOLVE_TYPE, [node.selector]); |
1794 return null; | 1793 return null; |
1795 } | 1794 } |
1796 return e.computeType(compiler); | 1795 return e.computeType(compiler); |
1797 } | 1796 } |
1798 | 1797 |
1799 Link<Type> getOrCalculateAllSupertypes(ClassElement cls, | 1798 Link<InterfaceType> getOrCalculateAllSupertypes(ClassElement cls, |
1800 [Set<ClassElement> seen]) { | 1799 [Set<ClassElement> seen]) { |
1801 Link<Type> allSupertypes = cls.allSupertypes; | 1800 Link<InterfaceType> allSupertypes = cls.allSupertypes; |
1802 if (allSupertypes !== null) return allSupertypes; | 1801 if (allSupertypes !== null) return allSupertypes; |
1803 if (seen === null) { | 1802 if (seen === null) { |
1804 seen = new Set<ClassElement>(); | 1803 seen = new Set<ClassElement>(); |
1805 } | 1804 } |
1806 if (seen.contains(cls)) { | 1805 if (seen.contains(cls)) { |
1807 error(cls.parseNode(compiler), | 1806 error(cls.parseNode(compiler), |
1808 MessageKind.CYCLIC_CLASS_HIERARCHY, | 1807 MessageKind.CYCLIC_CLASS_HIERARCHY, |
1809 [cls.name]); | 1808 [cls.name]); |
1810 cls.allSupertypes = const EmptyLink<Type>(); | 1809 cls.allSupertypes = const EmptyLink<InterfaceType>(); |
1811 } else { | 1810 } else { |
1812 cls.ensureResolved(compiler); | 1811 cls.ensureResolved(compiler); |
1813 calculateAllSupertypes(cls, seen); | 1812 calculateAllSupertypes(cls, seen); |
1814 } | 1813 } |
1815 return cls.allSupertypes; | 1814 return cls.allSupertypes; |
1816 } | 1815 } |
1817 | 1816 |
1818 void calculateAllSupertypes(ClassElement cls, Set<ClassElement> seen) { | 1817 void calculateAllSupertypes(ClassElement cls, Set<ClassElement> seen) { |
1819 // TODO(karlklose): check if type arguments match, if a classelement occurs | 1818 // TODO(karlklose): check if type arguments match, if a classelement occurs |
1820 // more than once in the supertypes. | 1819 // more than once in the supertypes. |
1821 if (cls.allSupertypes !== null) return; | 1820 if (cls.allSupertypes !== null) return; |
1822 final InterfaceType supertype = cls.supertype; | 1821 final InterfaceType supertype = cls.supertype; |
1823 if (seen.contains(cls)) { | 1822 if (seen.contains(cls)) { |
1824 error(cls.parseNode(compiler), | 1823 error(cls.parseNode(compiler), |
1825 MessageKind.CYCLIC_CLASS_HIERARCHY, | 1824 MessageKind.CYCLIC_CLASS_HIERARCHY, |
1826 [cls.name]); | 1825 [cls.name]); |
1827 cls.allSupertypes = const EmptyLink<Type>(); | 1826 cls.allSupertypes = const EmptyLink<InterfaceType>(); |
1828 } else if (supertype != null) { | 1827 } else if (supertype != null) { |
1829 seen.add(cls); | 1828 seen.add(cls); |
1830 ClassElement supertypeElement = supertype.element; | 1829 ClassElement supertypeElement = supertype.element; |
1831 Link<Type> superSupertypes = | 1830 Link<InterfaceType> superSupertypes = |
1832 getOrCalculateAllSupertypes(supertypeElement, seen); | 1831 getOrCalculateAllSupertypes(supertypeElement, seen); |
1833 var superTypesBuilder = new LinkBuilder<Type>(); | 1832 var superTypesBuilder = new LinkBuilder<InterfaceType>(); |
1834 superTypesBuilder.addLast(supertype); | 1833 superTypesBuilder.addLast(supertype); |
1835 | 1834 |
1836 // Substitute type variables in supertypes. | 1835 // Substitute type variables in supertypes. |
1837 var superTypeParameters = <Type>[]; | |
1838 var typeVariableElements = supertypeElement.typeParameters.getValues(); | |
1839 for (TypeVariableElement typeVariableElement in typeVariableElements) { | |
1840 superTypeParameters.add(typeVariableElement.type); | |
1841 } | |
1842 for (Type superSupertype in superSupertypes) { | 1836 for (Type superSupertype in superSupertypes) { |
1843 superTypesBuilder.addLast(superSupertype.subst( | 1837 superTypesBuilder.addLast(superSupertype.subst(compiler, |
1844 supertype.arguments, superTypeParameters)); | 1838 supertype.typeArguments, supertypeElement.typeParameters)); |
1845 } | 1839 } |
1846 | 1840 |
1847 Link<Type> supertypes = superTypesBuilder.toLink(); | 1841 Link<InterfaceType> supertypes = superTypesBuilder.toLink(); |
1848 for (Link<Type> interfaces = cls.interfaces; | 1842 for (Link<InterfaceType> interfaces = cls.interfaces; |
1849 !interfaces.isEmpty(); | 1843 !interfaces.isEmpty(); |
1850 interfaces = interfaces.tail) { | 1844 interfaces = interfaces.tail) { |
1851 Element element = interfaces.head.element; | 1845 Element element = interfaces.head.element; |
1852 Link<Type> interfaceSupertypes = | 1846 Link<InterfaceType> interfaceSupertypes = |
1853 getOrCalculateAllSupertypes(element, seen); | 1847 getOrCalculateAllSupertypes(element, seen); |
1854 supertypes = supertypes.reversePrependAll(interfaceSupertypes); | 1848 supertypes = supertypes.reversePrependAll(interfaceSupertypes); |
1855 supertypes = supertypes.prepend(interfaces.head); | 1849 supertypes = supertypes.prepend(interfaces.head); |
1856 } | 1850 } |
1857 seen.remove(cls); | 1851 seen.remove(cls); |
1858 cls.allSupertypes = supertypes; | 1852 cls.allSupertypes = supertypes; |
1859 } else { | 1853 } else { |
1860 cls.allSupertypes = const EmptyLink<Type>(); | 1854 cls.allSupertypes = const EmptyLink<InterfaceType>(); |
1861 } | 1855 } |
1862 } | 1856 } |
1863 | 1857 |
1864 /** | 1858 /** |
1865 * Add a synthetic nullary constructor if there are no other | 1859 * Add a synthetic nullary constructor if there are no other |
1866 * constructors. | 1860 * constructors. |
1867 */ | 1861 */ |
1868 void addDefaultConstructorIfNeeded(ClassElement element) { | 1862 void addDefaultConstructorIfNeeded(ClassElement element) { |
1869 if (element.constructors.length != 0) return; | 1863 if (element.constructors.length != 0) return; |
1870 SynthesizedConstructorElement constructor = | 1864 SynthesizedConstructorElement constructor = |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2243 | 2237 |
2244 TopScope(LibraryElement library) : super(null, library); | 2238 TopScope(LibraryElement library) : super(null, library); |
2245 Element lookup(SourceString name) { | 2239 Element lookup(SourceString name) { |
2246 return library.find(name); | 2240 return library.find(name); |
2247 } | 2241 } |
2248 | 2242 |
2249 Element add(Element newElement) { | 2243 Element add(Element newElement) { |
2250 throw "Cannot add an element in the top scope"; | 2244 throw "Cannot add an element in the top scope"; |
2251 } | 2245 } |
2252 } | 2246 } |
OLD | NEW |