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 1469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1480 PrefixElement prefixElement = element; | 1480 PrefixElement prefixElement = element; |
1481 Identifier selector = node.selector.asIdentifier(); | 1481 Identifier selector = node.selector.asIdentifier(); |
1482 var e = prefixElement.lookupLocalMember(selector.source); | 1482 var e = prefixElement.lookupLocalMember(selector.source); |
1483 if (e === null || !e.impliesType()) { | 1483 if (e === null || !e.impliesType()) { |
1484 error(node.selector, MessageKind.CANNOT_RESOLVE_TYPE, [node.selector]); | 1484 error(node.selector, MessageKind.CANNOT_RESOLVE_TYPE, [node.selector]); |
1485 return null; | 1485 return null; |
1486 } | 1486 } |
1487 return e.computeType(compiler); | 1487 return e.computeType(compiler); |
1488 } | 1488 } |
1489 | 1489 |
1490 Link<Type> getOrCalculateAllSupertypes(ClassElement classElement, | 1490 Link<Type> getOrCalculateAllSupertypes(ClassElement cls, |
1491 [Set<ClassElement> seen]) { | 1491 [Set<ClassElement> seen]) { |
1492 Link<Type> allSupertypes = classElement.allSupertypes; | 1492 Link<Type> allSupertypes = cls.allSupertypes; |
1493 if (allSupertypes !== null) return allSupertypes; | 1493 if (allSupertypes !== null) return allSupertypes; |
1494 if (seen === null) { | 1494 if (seen === null) { |
1495 seen = new Set<ClassElement>(); | 1495 seen = new Set<ClassElement>(); |
1496 } | 1496 } |
1497 if (seen.contains(classElement)) { | 1497 if (seen.contains(cls)) { |
1498 error(classElement.parseNode(compiler), | 1498 error(cls.parseNode(compiler), |
1499 MessageKind.CYCLIC_CLASS_HIERARCHY, | 1499 MessageKind.CYCLIC_CLASS_HIERARCHY, |
1500 [classElement.name]); | 1500 [cls.name]); |
1501 classElement.allSupertypes = const EmptyLink<Type>(); | 1501 cls.allSupertypes = const EmptyLink<Type>(); |
1502 } else { | 1502 } else { |
1503 classElement.ensureResolved(compiler); | 1503 cls.ensureResolved(compiler); |
1504 calculateAllSupertypes(classElement, seen); | 1504 calculateAllSupertypes(cls, seen); |
1505 } | 1505 } |
1506 return classElement.allSupertypes; | 1506 return cls.allSupertypes; |
1507 } | 1507 } |
1508 | 1508 |
1509 void calculateAllSupertypes(ClassElement classElement, | 1509 void calculateAllSupertypes(ClassElement cls, Set<ClassElement> seen) { |
1510 Set<ClassElement> seen) { | |
1511 // TODO(karlklose): substitute type variables. | 1510 // TODO(karlklose): substitute type variables. |
1512 // TODO(karlklose): check if type arguments match, if a classelement occurs | 1511 // TODO(karlklose): check if type arguments match, if a classelement occurs |
1513 // more than once in the supertypes. | 1512 // more than once in the supertypes. |
1514 if (classElement.allSupertypes !== null) return; | 1513 if (cls.allSupertypes !== null) return; |
1515 final Type supertype = classElement.supertype; | 1514 final Type supertype = cls.supertype; |
1516 if (seen.contains(classElement)) { | 1515 if (seen.contains(cls)) { |
1517 error(classElement.parseNode(compiler), | 1516 error(cls.parseNode(compiler), |
1518 MessageKind.CYCLIC_CLASS_HIERARCHY, | 1517 MessageKind.CYCLIC_CLASS_HIERARCHY, |
1519 [classElement.name]); | 1518 [cls.name]); |
1520 classElement.allSupertypes = const EmptyLink<Type>(); | 1519 cls.allSupertypes = const EmptyLink<Type>(); |
1521 } else if (supertype != null) { | 1520 } else if (supertype != null) { |
1522 seen.add(classElement); | 1521 seen.add(cls); |
1523 Link<Type> superSupertypes = | 1522 Link<Type> superSupertypes = |
1524 getOrCalculateAllSupertypes(supertype.element, seen); | 1523 getOrCalculateAllSupertypes(supertype.element, seen); |
1525 Link<Type> supertypes = new Link<Type>(supertype, superSupertypes); | 1524 Link<Type> supertypes = new Link<Type>(supertype, superSupertypes); |
1526 for (Link<Type> interfaces = classElement.interfaces; | 1525 for (Link<Type> interfaces = cls.interfaces; |
1527 !interfaces.isEmpty(); | 1526 !interfaces.isEmpty(); |
1528 interfaces = interfaces.tail) { | 1527 interfaces = interfaces.tail) { |
1529 Element element = interfaces.head.element; | 1528 Element element = interfaces.head.element; |
1530 Link<Type> interfaceSupertypes = | 1529 Link<Type> interfaceSupertypes = |
1531 getOrCalculateAllSupertypes(element, seen); | 1530 getOrCalculateAllSupertypes(element, seen); |
1532 supertypes = supertypes.reversePrependAll(interfaceSupertypes); | 1531 supertypes = supertypes.reversePrependAll(interfaceSupertypes); |
1533 supertypes = supertypes.prepend(interfaces.head); | 1532 supertypes = supertypes.prepend(interfaces.head); |
1534 } | 1533 } |
1535 seen.remove(classElement); | 1534 seen.remove(cls); |
1536 classElement.allSupertypes = supertypes; | 1535 cls.allSupertypes = supertypes; |
1537 } else { | 1536 } else { |
1538 classElement.allSupertypes = const EmptyLink<Type>(); | 1537 cls.allSupertypes = const EmptyLink<Type>(); |
1539 } | 1538 } |
1540 } | 1539 } |
1541 | 1540 |
1542 /** | 1541 /** |
1543 * Add a synthetic nullary constructor if there are no other | 1542 * Add a synthetic nullary constructor if there are no other |
1544 * constructors. | 1543 * constructors. |
1545 */ | 1544 */ |
1546 void addDefaultConstructorIfNeeded(ClassElement element) { | 1545 void addDefaultConstructorIfNeeded(ClassElement element) { |
1547 if (element.constructors.length != 0) return; | 1546 if (element.constructors.length != 0) return; |
1548 SynthesizedConstructorElement constructor = | 1547 SynthesizedConstructorElement constructor = |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1853 final Element element; | 1852 final Element element; |
1854 final Scope parent; | 1853 final Scope parent; |
1855 | 1854 |
1856 Scope(this.parent, this.element); | 1855 Scope(this.parent, this.element); |
1857 abstract Element add(Element element); | 1856 abstract Element add(Element element); |
1858 abstract Element lookup(SourceString name); | 1857 abstract Element lookup(SourceString name); |
1859 } | 1858 } |
1860 | 1859 |
1861 class TypeVariablesScope extends Scope { | 1860 class TypeVariablesScope extends Scope { |
1862 TypeVariablesScope(parent, ClassElement element) : super(parent, element); | 1861 TypeVariablesScope(parent, ClassElement element) : super(parent, element); |
1863 Element add(Element element) { | 1862 Element add(Element newElement) { |
1864 throw "Cannot add element to TypeVariableScope"; | 1863 throw "Cannot add element to TypeVariableScope"; |
1865 } | 1864 } |
1866 Element lookup(SourceString name) { | 1865 Element lookup(SourceString name) { |
1867 ClassElement cls = element; | 1866 ClassElement cls = element; |
1868 Element result = cls.lookupTypeParameter(name); | 1867 Element result = cls.lookupTypeParameter(name); |
1869 if (result !== null) return result; | 1868 if (result !== null) return result; |
1870 if (parent !== null) return parent.lookup(name); | 1869 if (parent !== null) return parent.lookup(name); |
1871 } | 1870 } |
1872 } | 1871 } |
1873 | 1872 |
1874 class MethodScope extends Scope { | 1873 class MethodScope extends Scope { |
1875 final Map<SourceString, Element> elements; | 1874 final Map<SourceString, Element> elements; |
1876 | 1875 |
1877 MethodScope(Scope parent, Element element) | 1876 MethodScope(Scope parent, Element element) |
1878 : super(parent, element), this.elements = new Map<SourceString, Element>(); | 1877 : super(parent, element), this.elements = new Map<SourceString, Element>(); |
1879 | 1878 |
1880 Element lookup(SourceString name) { | 1879 Element lookup(SourceString name) { |
1881 Element element = elements[name]; | 1880 Element found = elements[name]; |
1882 if (element !== null) return element; | 1881 if (found !== null) return found; |
1883 return parent.lookup(name); | 1882 return parent.lookup(name); |
1884 } | 1883 } |
1885 | 1884 |
1886 Element add(Element element) { | 1885 Element add(Element newElement) { |
1887 if (elements.containsKey(element.name)) return elements[element.name]; | 1886 if (elements.containsKey(newElement.name)) { |
1888 elements[element.name] = element; | 1887 return elements[newElement.name]; |
1889 return element; | 1888 } |
| 1889 elements[newElement.name] = newElement; |
| 1890 return newElement; |
1890 } | 1891 } |
1891 } | 1892 } |
1892 | 1893 |
1893 class BlockScope extends MethodScope { | 1894 class BlockScope extends MethodScope { |
1894 BlockScope(Scope parent) : super(parent, parent.element); | 1895 BlockScope(Scope parent) : super(parent, parent.element); |
1895 } | 1896 } |
1896 | 1897 |
1897 class ClassScope extends Scope { | 1898 class ClassScope extends Scope { |
1898 ClassScope(ClassElement element, LibraryElement library) | 1899 ClassScope(ClassElement element, LibraryElement library) |
1899 : super(new TopScope(library), element); | 1900 : super(new TopScope(library), element); |
1900 | 1901 |
1901 Element lookup(SourceString name) { | 1902 Element lookup(SourceString name) { |
1902 ClassElement cls = element; | 1903 ClassElement cls = element; |
1903 Element result = cls.lookupLocalMember(name); | 1904 Element result = cls.lookupLocalMember(name); |
1904 if (result !== null) return result; | 1905 if (result !== null) return result; |
1905 result = cls.lookupTypeParameter(name); | 1906 result = cls.lookupTypeParameter(name); |
1906 if (result !== null) return result; | 1907 if (result !== null) return result; |
1907 result = parent.lookup(name); | 1908 result = parent.lookup(name); |
1908 if (result != null) return result; | 1909 if (result != null) return result; |
1909 return cls.lookupSuperMember(name); | 1910 return cls.lookupSuperMember(name); |
1910 } | 1911 } |
1911 | 1912 |
1912 Element add(Element element) { | 1913 Element add(Element newElement) { |
1913 throw "Cannot add an element in a class scope"; | 1914 throw "Cannot add an element in a class scope"; |
1914 } | 1915 } |
1915 } | 1916 } |
1916 | 1917 |
1917 class TopScope extends Scope { | 1918 class TopScope extends Scope { |
1918 LibraryElement get library() => element; | 1919 LibraryElement get library() => element; |
1919 | 1920 |
1920 TopScope(LibraryElement library) : super(null, library); | 1921 TopScope(LibraryElement library) : super(null, library); |
1921 Element lookup(SourceString name) { | 1922 Element lookup(SourceString name) { |
1922 return library.find(name); | 1923 return library.find(name); |
1923 } | 1924 } |
1924 | 1925 |
1925 Element add(Element element) { | 1926 Element add(Element newElement) { |
1926 throw "Cannot add an element in the top scope"; | 1927 throw "Cannot add an element in the top scope"; |
1927 } | 1928 } |
1928 } | 1929 } |
OLD | NEW |