OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 summary_resynthesizer; | 5 library summary_resynthesizer; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; | 10 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; |
(...skipping 1176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1187 * The enclosing [_ReferenceInfo], or `null` for top-level elements. | 1187 * The enclosing [_ReferenceInfo], or `null` for top-level elements. |
1188 */ | 1188 */ |
1189 final _ReferenceInfo enclosing; | 1189 final _ReferenceInfo enclosing; |
1190 | 1190 |
1191 /** | 1191 /** |
1192 * The name of the entity referred to by this reference. | 1192 * The name of the entity referred to by this reference. |
1193 */ | 1193 */ |
1194 final String name; | 1194 final String name; |
1195 | 1195 |
1196 /** | 1196 /** |
| 1197 * Is `true` if the [element] can be used as a declared type. |
| 1198 */ |
| 1199 final bool isDeclarableType; |
| 1200 |
| 1201 /** |
1197 * The element referred to by this reference, or `null` if there is no | 1202 * The element referred to by this reference, or `null` if there is no |
1198 * associated element (e.g. because it is a reference to an undefined | 1203 * associated element (e.g. because it is a reference to an undefined |
1199 * entity). | 1204 * entity). |
1200 */ | 1205 */ |
1201 final Element element; | 1206 final Element element; |
1202 | 1207 |
1203 /** | 1208 /** |
1204 * If this reference refers to a non-generic type, the type it refers to. | 1209 * If this reference refers to a non-generic type, the type it refers to. |
1205 * Otherwise `null`. | 1210 * Otherwise `null`. |
1206 */ | 1211 */ |
1207 DartType type; | 1212 DartType type; |
1208 | 1213 |
1209 /** | 1214 /** |
1210 * The number of type parameters accepted by the entity referred to by this | 1215 * The number of type parameters accepted by the entity referred to by this |
1211 * reference, or zero if it doesn't accept any type parameters. | 1216 * reference, or zero if it doesn't accept any type parameters. |
1212 */ | 1217 */ |
1213 final int numTypeParameters; | 1218 final int numTypeParameters; |
1214 | 1219 |
1215 /** | 1220 /** |
1216 * Create a new [_ReferenceInfo] object referring to an element called [name] | 1221 * Create a new [_ReferenceInfo] object referring to an element called [name] |
1217 * via the element handle [element], and having [numTypeParameters] type | 1222 * via the element handle [element], and having [numTypeParameters] type |
1218 * parameters. | 1223 * parameters. |
1219 * | 1224 * |
1220 * For the special types `dynamic` and `void`, [specialType] should point to | 1225 * For the special types `dynamic` and `void`, [specialType] should point to |
1221 * the type itself. Otherwise, pass `null` and the type will be computed | 1226 * the type itself. Otherwise, pass `null` and the type will be computed |
1222 * when appropriate. | 1227 * when appropriate. |
1223 */ | 1228 */ |
1224 _ReferenceInfo(this.libraryResynthesizer, this.enclosing, this.name, | 1229 _ReferenceInfo( |
1225 this.element, DartType specialType, this.numTypeParameters) { | 1230 this.libraryResynthesizer, |
| 1231 this.enclosing, |
| 1232 this.name, |
| 1233 this.isDeclarableType, |
| 1234 this.element, |
| 1235 DartType specialType, |
| 1236 this.numTypeParameters) { |
1226 if (specialType != null) { | 1237 if (specialType != null) { |
1227 type = specialType; | 1238 type = specialType; |
1228 } else { | 1239 } else { |
1229 type = _buildType(true, 0, (_) => DynamicTypeImpl.instance, const []); | 1240 type = _buildType(true, 0, (_) => DynamicTypeImpl.instance, const []); |
1230 } | 1241 } |
1231 } | 1242 } |
1232 | 1243 |
1233 /** | 1244 /** |
1234 * Build a [DartType] corresponding to the result of applying some type | 1245 * Build a [DartType] corresponding to the result of applying some type |
1235 * arguments to the entity referred to by this [_ReferenceInfo]. The type | 1246 * arguments to the entity referred to by this [_ReferenceInfo]. The type |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1402 | 1413 |
1403 @override | 1414 @override |
1404 DartType resolveLinkedType( | 1415 DartType resolveLinkedType( |
1405 int slot, TypeParameterizedElementMixin typeParameterContext) { | 1416 int slot, TypeParameterizedElementMixin typeParameterContext) { |
1406 return _unitResynthesizer.buildLinkedType(slot, typeParameterContext); | 1417 return _unitResynthesizer.buildLinkedType(slot, typeParameterContext); |
1407 } | 1418 } |
1408 | 1419 |
1409 @override | 1420 @override |
1410 DartType resolveTypeRef( | 1421 DartType resolveTypeRef( |
1411 EntityRef type, TypeParameterizedElementMixin typeParameterContext, | 1422 EntityRef type, TypeParameterizedElementMixin typeParameterContext, |
1412 {bool defaultVoid: false, bool instantiateToBoundsAllowed: true}) { | 1423 {bool defaultVoid: false, |
| 1424 bool instantiateToBoundsAllowed: true, |
| 1425 bool declaredType: false}) { |
1413 return _unitResynthesizer.buildType(type, typeParameterContext, | 1426 return _unitResynthesizer.buildType(type, typeParameterContext, |
1414 defaultVoid: defaultVoid, | 1427 defaultVoid: defaultVoid, |
1415 instantiateToBoundsAllowed: instantiateToBoundsAllowed); | 1428 instantiateToBoundsAllowed: instantiateToBoundsAllowed, |
| 1429 declaredType: declaredType); |
1416 } | 1430 } |
1417 } | 1431 } |
1418 | 1432 |
1419 /** | 1433 /** |
1420 * An instance of [_UnitResynthesizer] is responsible for resynthesizing the | 1434 * An instance of [_UnitResynthesizer] is responsible for resynthesizing the |
1421 * elements in a single unit from that unit's summary. | 1435 * elements in a single unit from that unit's summary. |
1422 */ | 1436 */ |
1423 class _UnitResynthesizer { | 1437 class _UnitResynthesizer { |
1424 /** | 1438 /** |
1425 * The [_LibraryResynthesizer] which is being used to obtain summaries. | 1439 * The [_LibraryResynthesizer] which is being used to obtain summaries. |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1585 } | 1599 } |
1586 | 1600 |
1587 /** | 1601 /** |
1588 * Build a [DartType] object based on a [EntityRef]. This [DartType] | 1602 * Build a [DartType] object based on a [EntityRef]. This [DartType] |
1589 * may refer to elements in other libraries than the library being | 1603 * may refer to elements in other libraries than the library being |
1590 * deserialized, so handles are used to avoid having to deserialize other | 1604 * deserialized, so handles are used to avoid having to deserialize other |
1591 * libraries in the process. | 1605 * libraries in the process. |
1592 */ | 1606 */ |
1593 DartType buildType( | 1607 DartType buildType( |
1594 EntityRef type, TypeParameterizedElementMixin typeParameterContext, | 1608 EntityRef type, TypeParameterizedElementMixin typeParameterContext, |
1595 {bool defaultVoid: false, bool instantiateToBoundsAllowed: true}) { | 1609 {bool defaultVoid: false, |
| 1610 bool instantiateToBoundsAllowed: true, |
| 1611 bool declaredType: false}) { |
1596 if (type == null) { | 1612 if (type == null) { |
1597 if (defaultVoid) { | 1613 if (defaultVoid) { |
1598 return VoidTypeImpl.instance; | 1614 return VoidTypeImpl.instance; |
1599 } else { | 1615 } else { |
1600 return DynamicTypeImpl.instance; | 1616 return DynamicTypeImpl.instance; |
1601 } | 1617 } |
1602 } | 1618 } |
1603 if (type.paramReference != 0) { | 1619 if (type.paramReference != 0) { |
1604 return typeParameterContext.getTypeParameterType(type.paramReference); | 1620 return typeParameterContext.getTypeParameterType(type.paramReference); |
1605 } else if (type.syntheticReturnType != null) { | 1621 } else if (type.syntheticReturnType != null) { |
1606 FunctionElementImpl element = | 1622 FunctionElementImpl element = |
1607 new FunctionElementImpl_forLUB(unit, typeParameterContext, type); | 1623 new FunctionElementImpl_forLUB(unit, typeParameterContext, type); |
1608 return element.type; | 1624 return element.type; |
1609 } else { | 1625 } else { |
1610 DartType getTypeArgument(int i) { | 1626 DartType getTypeArgument(int i) { |
1611 if (i < type.typeArguments.length) { | 1627 if (i < type.typeArguments.length) { |
1612 return buildType(type.typeArguments[i], typeParameterContext); | 1628 return buildType(type.typeArguments[i], typeParameterContext); |
1613 } else { | 1629 } else { |
1614 return DynamicTypeImpl.instance; | 1630 return DynamicTypeImpl.instance; |
1615 } | 1631 } |
1616 } | 1632 } |
1617 | 1633 |
1618 _ReferenceInfo referenceInfo = getReferenceInfo(type.reference); | 1634 _ReferenceInfo referenceInfo = getReferenceInfo(type.reference); |
| 1635 if (declaredType && !referenceInfo.isDeclarableType) { |
| 1636 return DynamicTypeImpl.instance; |
| 1637 } |
1619 return referenceInfo.buildType( | 1638 return referenceInfo.buildType( |
1620 instantiateToBoundsAllowed, | 1639 instantiateToBoundsAllowed, |
1621 type.typeArguments.length, | 1640 type.typeArguments.length, |
1622 getTypeArgument, | 1641 getTypeArgument, |
1623 type.implicitFunctionTypeIndices); | 1642 type.implicitFunctionTypeIndices); |
1624 } | 1643 } |
1625 } | 1644 } |
1626 | 1645 |
1627 UnitExplicitTopLevelAccessors buildUnitExplicitTopLevelAccessors() { | 1646 UnitExplicitTopLevelAccessors buildUnitExplicitTopLevelAccessors() { |
1628 HashMap<String, TopLevelVariableElementImpl> implicitVariables = | 1647 HashMap<String, TopLevelVariableElementImpl> implicitVariables = |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1708 containingReference = unlinkedUnit.references[index].prefixReference; | 1727 containingReference = unlinkedUnit.references[index].prefixReference; |
1709 } else { | 1728 } else { |
1710 name = linkedUnit.references[index].name; | 1729 name = linkedUnit.references[index].name; |
1711 containingReference = linkedUnit.references[index].containingReference; | 1730 containingReference = linkedUnit.references[index].containingReference; |
1712 } | 1731 } |
1713 _ReferenceInfo enclosingInfo = containingReference != 0 | 1732 _ReferenceInfo enclosingInfo = containingReference != 0 |
1714 ? getReferenceInfo(containingReference) | 1733 ? getReferenceInfo(containingReference) |
1715 : null; | 1734 : null; |
1716 Element element; | 1735 Element element; |
1717 DartType type; | 1736 DartType type; |
| 1737 bool isDeclarableType = false; |
1718 int numTypeParameters = linkedReference.numTypeParameters; | 1738 int numTypeParameters = linkedReference.numTypeParameters; |
1719 if (linkedReference.kind == ReferenceKind.unresolved) { | 1739 if (linkedReference.kind == ReferenceKind.unresolved) { |
1720 type = UndefinedTypeImpl.instance; | 1740 type = UndefinedTypeImpl.instance; |
1721 element = null; | 1741 element = null; |
| 1742 isDeclarableType = true; |
1722 } else if (name == 'dynamic') { | 1743 } else if (name == 'dynamic') { |
1723 type = DynamicTypeImpl.instance; | 1744 type = DynamicTypeImpl.instance; |
1724 element = type.element; | 1745 element = type.element; |
| 1746 isDeclarableType = true; |
1725 } else if (name == 'void') { | 1747 } else if (name == 'void') { |
1726 type = VoidTypeImpl.instance; | 1748 type = VoidTypeImpl.instance; |
1727 element = type.element; | 1749 element = type.element; |
| 1750 isDeclarableType = true; |
1728 } else if (name == '*bottom*') { | 1751 } else if (name == '*bottom*') { |
1729 type = BottomTypeImpl.instance; | 1752 type = BottomTypeImpl.instance; |
1730 element = null; | 1753 element = null; |
| 1754 isDeclarableType = true; |
1731 } else { | 1755 } else { |
1732 List<String> locationComponents; | 1756 List<String> locationComponents; |
1733 if (enclosingInfo != null && enclosingInfo.element is ClassElement) { | 1757 if (enclosingInfo != null && enclosingInfo.element is ClassElement) { |
1734 String identifier = _getElementIdentifier(name, linkedReference.kind); | 1758 String identifier = _getElementIdentifier(name, linkedReference.kind); |
1735 locationComponents = | 1759 locationComponents = |
1736 enclosingInfo.element.location.components.toList(); | 1760 enclosingInfo.element.location.components.toList(); |
1737 locationComponents.add(identifier); | 1761 locationComponents.add(identifier); |
1738 } else { | 1762 } else { |
1739 String identifier = _getElementIdentifier(name, linkedReference.kind); | 1763 String identifier = _getElementIdentifier(name, linkedReference.kind); |
1740 locationComponents = | 1764 locationComponents = |
1741 libraryResynthesizer.getReferencedLocationComponents( | 1765 libraryResynthesizer.getReferencedLocationComponents( |
1742 linkedReference.dependency, linkedReference.unit, identifier); | 1766 linkedReference.dependency, linkedReference.unit, identifier); |
1743 } | 1767 } |
1744 if (!_resynthesizerContext.isStrongMode && | 1768 if (!_resynthesizerContext.isStrongMode && |
1745 locationComponents.length == 3 && | 1769 locationComponents.length == 3 && |
1746 locationComponents[0] == 'dart:async' && | 1770 locationComponents[0] == 'dart:async' && |
1747 locationComponents[2] == 'FutureOr') { | 1771 locationComponents[2] == 'FutureOr') { |
1748 type = typeProvider.dynamicType; | 1772 type = typeProvider.dynamicType; |
1749 numTypeParameters = 0; | 1773 numTypeParameters = 0; |
1750 } | 1774 } |
1751 ElementLocation location = | 1775 ElementLocation location = |
1752 new ElementLocationImpl.con3(locationComponents); | 1776 new ElementLocationImpl.con3(locationComponents); |
1753 if (enclosingInfo != null) { | 1777 if (enclosingInfo != null) { |
1754 numTypeParameters += enclosingInfo.numTypeParameters; | 1778 numTypeParameters += enclosingInfo.numTypeParameters; |
1755 } | 1779 } |
1756 switch (linkedReference.kind) { | 1780 switch (linkedReference.kind) { |
1757 case ReferenceKind.classOrEnum: | 1781 case ReferenceKind.classOrEnum: |
1758 element = new ClassElementHandle(summaryResynthesizer, location); | 1782 element = new ClassElementHandle(summaryResynthesizer, location); |
| 1783 isDeclarableType = true; |
1759 break; | 1784 break; |
1760 case ReferenceKind.constructor: | 1785 case ReferenceKind.constructor: |
1761 assert(location.components.length == 4); | 1786 assert(location.components.length == 4); |
1762 element = | 1787 element = |
1763 new ConstructorElementHandle(summaryResynthesizer, location); | 1788 new ConstructorElementHandle(summaryResynthesizer, location); |
1764 break; | 1789 break; |
1765 case ReferenceKind.method: | 1790 case ReferenceKind.method: |
1766 assert(location.components.length == 4); | 1791 assert(location.components.length == 4); |
1767 element = new MethodElementHandle(summaryResynthesizer, location); | 1792 element = new MethodElementHandle(summaryResynthesizer, location); |
1768 break; | 1793 break; |
1769 case ReferenceKind.propertyAccessor: | 1794 case ReferenceKind.propertyAccessor: |
1770 assert(location.components.length == 4); | 1795 assert(location.components.length == 4); |
1771 element = new PropertyAccessorElementHandle( | 1796 element = new PropertyAccessorElementHandle( |
1772 summaryResynthesizer, location); | 1797 summaryResynthesizer, location); |
1773 break; | 1798 break; |
1774 case ReferenceKind.topLevelFunction: | 1799 case ReferenceKind.topLevelFunction: |
1775 assert(location.components.length == 3); | 1800 assert(location.components.length == 3); |
1776 element = new FunctionElementHandle(summaryResynthesizer, location); | 1801 element = new FunctionElementHandle(summaryResynthesizer, location); |
1777 break; | 1802 break; |
1778 case ReferenceKind.topLevelPropertyAccessor: | 1803 case ReferenceKind.topLevelPropertyAccessor: |
1779 element = new PropertyAccessorElementHandle( | 1804 element = new PropertyAccessorElementHandle( |
1780 summaryResynthesizer, location); | 1805 summaryResynthesizer, location); |
1781 break; | 1806 break; |
1782 case ReferenceKind.typedef: | 1807 case ReferenceKind.typedef: |
1783 element = new FunctionTypeAliasElementHandle( | 1808 element = new FunctionTypeAliasElementHandle( |
1784 summaryResynthesizer, location); | 1809 summaryResynthesizer, location); |
| 1810 isDeclarableType = true; |
1785 break; | 1811 break; |
1786 case ReferenceKind.variable: | 1812 case ReferenceKind.variable: |
1787 Element enclosingElement = enclosingInfo.element; | 1813 Element enclosingElement = enclosingInfo.element; |
1788 if (enclosingElement is ExecutableElement) { | 1814 if (enclosingElement is ExecutableElement) { |
1789 element = new _DeferredLocalVariableElement( | 1815 element = new _DeferredLocalVariableElement( |
1790 enclosingElement, linkedReference.localIndex); | 1816 enclosingElement, linkedReference.localIndex); |
1791 } else { | 1817 } else { |
1792 throw new StateError('Unexpected element enclosing variable:' | 1818 throw new StateError('Unexpected element enclosing variable:' |
1793 ' ${enclosingElement.runtimeType}'); | 1819 ' ${enclosingElement.runtimeType}'); |
1794 } | 1820 } |
1795 break; | 1821 break; |
1796 case ReferenceKind.function: | 1822 case ReferenceKind.function: |
1797 Element enclosingElement = enclosingInfo.element; | 1823 Element enclosingElement = enclosingInfo.element; |
1798 if (enclosingElement is VariableElement) { | 1824 if (enclosingElement is VariableElement) { |
1799 element = new _DeferredInitializerElement(enclosingElement); | 1825 element = new _DeferredInitializerElement(enclosingElement); |
1800 } else if (enclosingElement is ExecutableElement) { | 1826 } else if (enclosingElement is ExecutableElement) { |
1801 element = new _DeferredLocalFunctionElement( | 1827 element = new _DeferredLocalFunctionElement( |
1802 enclosingElement, linkedReference.localIndex); | 1828 enclosingElement, linkedReference.localIndex); |
1803 } else { | 1829 } else { |
1804 throw new StateError('Unexpected element enclosing function:' | 1830 throw new StateError('Unexpected element enclosing function:' |
1805 ' ${enclosingElement.runtimeType}'); | 1831 ' ${enclosingElement.runtimeType}'); |
1806 } | 1832 } |
1807 break; | 1833 break; |
1808 case ReferenceKind.prefix: | 1834 case ReferenceKind.prefix: |
1809 case ReferenceKind.unresolved: | 1835 case ReferenceKind.unresolved: |
1810 break; | 1836 break; |
1811 } | 1837 } |
1812 } | 1838 } |
1813 result = new _ReferenceInfo(libraryResynthesizer, enclosingInfo, name, | 1839 result = new _ReferenceInfo(libraryResynthesizer, enclosingInfo, name, |
1814 element, type, numTypeParameters); | 1840 isDeclarableType, element, type, numTypeParameters); |
1815 referenceInfos[index] = result; | 1841 referenceInfos[index] = result; |
1816 } | 1842 } |
1817 return result; | 1843 return result; |
1818 } | 1844 } |
1819 | 1845 |
1820 Expression _buildConstExpression(ElementImpl context, UnlinkedExpr uc) { | 1846 Expression _buildConstExpression(ElementImpl context, UnlinkedExpr uc) { |
1821 return new _ConstExprBuilder(this, context, uc).build(); | 1847 return new _ConstExprBuilder(this, context, uc).build(); |
1822 } | 1848 } |
1823 | 1849 |
1824 /** | 1850 /** |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1890 static String _getElementIdentifier(String name, ReferenceKind kind) { | 1916 static String _getElementIdentifier(String name, ReferenceKind kind) { |
1891 if (kind == ReferenceKind.topLevelPropertyAccessor || | 1917 if (kind == ReferenceKind.topLevelPropertyAccessor || |
1892 kind == ReferenceKind.propertyAccessor) { | 1918 kind == ReferenceKind.propertyAccessor) { |
1893 if (!name.endsWith('=')) { | 1919 if (!name.endsWith('=')) { |
1894 return name + '?'; | 1920 return name + '?'; |
1895 } | 1921 } |
1896 } | 1922 } |
1897 return name; | 1923 return name; |
1898 } | 1924 } |
1899 } | 1925 } |
OLD | NEW |