| 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/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 1539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1550 return typeArguments; | 1550 return typeArguments; |
| 1551 } | 1551 } |
| 1552 } | 1552 } |
| 1553 | 1553 |
| 1554 class _ResynthesizerContext implements ResynthesizerContext { | 1554 class _ResynthesizerContext implements ResynthesizerContext { |
| 1555 final _UnitResynthesizer _unitResynthesizer; | 1555 final _UnitResynthesizer _unitResynthesizer; |
| 1556 | 1556 |
| 1557 _ResynthesizerContext(this._unitResynthesizer); | 1557 _ResynthesizerContext(this._unitResynthesizer); |
| 1558 | 1558 |
| 1559 @override | 1559 @override |
| 1560 ElementAnnotationImpl buildAnnotation(UnlinkedConst uc) { |
| 1561 return _unitResynthesizer.buildAnnotation(uc); |
| 1562 } |
| 1563 |
| 1564 @override |
| 1560 DartType resolveTypeRef( | 1565 DartType resolveTypeRef( |
| 1561 EntityRef type, TypeParameterizedElementMixin typeParameterContext, | 1566 EntityRef type, TypeParameterizedElementMixin typeParameterContext, |
| 1562 {bool defaultVoid: false, bool instantiateToBoundsAllowed: true}) { | 1567 {bool defaultVoid: false, bool instantiateToBoundsAllowed: true}) { |
| 1563 return _unitResynthesizer.buildType(type, typeParameterContext, | 1568 return _unitResynthesizer.buildType(type, typeParameterContext, |
| 1564 defaultVoid: defaultVoid, | 1569 defaultVoid: defaultVoid, |
| 1565 instantiateToBoundsAllowed: instantiateToBoundsAllowed); | 1570 instantiateToBoundsAllowed: instantiateToBoundsAllowed); |
| 1566 } | 1571 } |
| 1567 } | 1572 } |
| 1568 | 1573 |
| 1569 /** | 1574 /** |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1683 _currentTypeParameterizedElement = | 1688 _currentTypeParameterizedElement = |
| 1684 new _CurrentTypeParameterizedElement(this); | 1689 new _CurrentTypeParameterizedElement(this); |
| 1685 } | 1690 } |
| 1686 | 1691 |
| 1687 SummaryResynthesizer get summaryResynthesizer => | 1692 SummaryResynthesizer get summaryResynthesizer => |
| 1688 libraryResynthesizer.summaryResynthesizer; | 1693 libraryResynthesizer.summaryResynthesizer; |
| 1689 | 1694 |
| 1690 TypeProvider get typeProvider => summaryResynthesizer.typeProvider; | 1695 TypeProvider get typeProvider => summaryResynthesizer.typeProvider; |
| 1691 | 1696 |
| 1692 /** | 1697 /** |
| 1698 * Build [ElementAnnotationImpl] for the given [UnlinkedConst]. |
| 1699 */ |
| 1700 ElementAnnotationImpl buildAnnotation(UnlinkedConst uc) { |
| 1701 ElementAnnotationImpl elementAnnotation = new ElementAnnotationImpl(unit); |
| 1702 Expression constExpr = _buildConstExpression(uc); |
| 1703 if (constExpr is Identifier) { |
| 1704 elementAnnotation.element = constExpr.staticElement; |
| 1705 elementAnnotation.annotationAst = AstFactory.annotation(constExpr); |
| 1706 } else if (constExpr is InstanceCreationExpression) { |
| 1707 elementAnnotation.element = constExpr.staticElement; |
| 1708 Identifier typeName = constExpr.constructorName.type.name; |
| 1709 SimpleIdentifier constructorName = constExpr.constructorName.name; |
| 1710 if (typeName is SimpleIdentifier && constructorName != null) { |
| 1711 // E.g. `@cls.ctor()`. Since `cls.ctor` would have been parsed as |
| 1712 // a PrefixedIdentifier, we need to resynthesize it as one. |
| 1713 typeName = AstFactory.identifier(typeName, constructorName); |
| 1714 constructorName = null; |
| 1715 } |
| 1716 elementAnnotation.annotationAst = AstFactory.annotation2( |
| 1717 typeName, constructorName, constExpr.argumentList); |
| 1718 } else { |
| 1719 throw new StateError( |
| 1720 'Unexpected annotation type: ${constExpr.runtimeType}'); |
| 1721 } |
| 1722 return elementAnnotation; |
| 1723 } |
| 1724 |
| 1725 /** |
| 1693 * Build the annotations for the given [element]. | 1726 * Build the annotations for the given [element]. |
| 1694 */ | 1727 */ |
| 1695 void buildAnnotations( | 1728 void buildAnnotations( |
| 1696 ElementImpl element, List<UnlinkedConst> serializedAnnotations) { | 1729 ElementImpl element, List<UnlinkedConst> serializedAnnotations) { |
| 1697 if (serializedAnnotations.isNotEmpty) { | 1730 if (serializedAnnotations.isNotEmpty) { |
| 1698 element.metadata = serializedAnnotations.map((UnlinkedConst a) { | 1731 element.metadata = serializedAnnotations.map(buildAnnotation).toList(); |
| 1699 ElementAnnotationImpl elementAnnotation = | |
| 1700 new ElementAnnotationImpl(this.unit); | |
| 1701 Expression constExpr = _buildConstExpression(a); | |
| 1702 if (constExpr is Identifier) { | |
| 1703 elementAnnotation.element = constExpr.staticElement; | |
| 1704 elementAnnotation.annotationAst = AstFactory.annotation(constExpr); | |
| 1705 } else if (constExpr is InstanceCreationExpression) { | |
| 1706 elementAnnotation.element = constExpr.staticElement; | |
| 1707 Identifier typeName = constExpr.constructorName.type.name; | |
| 1708 SimpleIdentifier constructorName = constExpr.constructorName.name; | |
| 1709 if (typeName is SimpleIdentifier && constructorName != null) { | |
| 1710 // E.g. `@cls.ctor()`. Since `cls.ctor` would have been parsed as | |
| 1711 // a PrefixedIdentifier, we need to resynthesize it as one. | |
| 1712 typeName = AstFactory.identifier(typeName, constructorName); | |
| 1713 constructorName = null; | |
| 1714 } | |
| 1715 elementAnnotation.annotationAst = AstFactory.annotation2( | |
| 1716 typeName, constructorName, constExpr.argumentList); | |
| 1717 } else { | |
| 1718 throw new StateError( | |
| 1719 'Unexpected annotation type: ${constExpr.runtimeType}'); | |
| 1720 } | |
| 1721 return elementAnnotation; | |
| 1722 }).toList(); | |
| 1723 } | 1732 } |
| 1724 } | 1733 } |
| 1725 | 1734 |
| 1726 /** | 1735 /** |
| 1727 * Resynthesize a [ClassElement] and place it in [unitHolder]. | 1736 * Resynthesize a [ClassElement] and place it in [unitHolder]. |
| 1728 */ | 1737 */ |
| 1729 void buildClass(UnlinkedClass serializedClass) { | 1738 void buildClass(UnlinkedClass serializedClass) { |
| 1730 ClassElement classElement; | 1739 ClassElement classElement; |
| 1731 if (libraryResynthesizer.isCoreLibrary && | 1740 if (libraryResynthesizer.isCoreLibrary && |
| 1732 serializedClass.supertype == null) { | 1741 serializedClass.supertype == null) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1815 } | 1824 } |
| 1816 classElement.interfaces = serializedClass.interfaces | 1825 classElement.interfaces = serializedClass.interfaces |
| 1817 .map((EntityRef t) => buildType(t, classElement)) | 1826 .map((EntityRef t) => buildType(t, classElement)) |
| 1818 .toList(); | 1827 .toList(); |
| 1819 classElement.mixins = serializedClass.mixins | 1828 classElement.mixins = serializedClass.mixins |
| 1820 .map((EntityRef t) => buildType(t, classElement)) | 1829 .map((EntityRef t) => buildType(t, classElement)) |
| 1821 .toList(); | 1830 .toList(); |
| 1822 // TODO(scheglov) move to ClassElementImpl | 1831 // TODO(scheglov) move to ClassElementImpl |
| 1823 correspondingType.typeArguments = classElement.typeParameterTypes; | 1832 correspondingType.typeArguments = classElement.typeParameterTypes; |
| 1824 classElement.type = correspondingType; | 1833 classElement.type = correspondingType; |
| 1825 buildAnnotations(classElement, serializedClass.annotations); | |
| 1826 assert(currentTypeParameters.isEmpty); | 1834 assert(currentTypeParameters.isEmpty); |
| 1827 // TODO(scheglov) Somehow Observatory shows too much time spent here | 1835 // TODO(scheglov) Somehow Observatory shows too much time spent here |
| 1828 // during DDC run on the large codebase. I would expect only Object here. | 1836 // during DDC run on the large codebase. I would expect only Object here. |
| 1829 if (handle == null) { | 1837 if (handle == null) { |
| 1830 buildClassExecutables(classElement, serializedClass); | 1838 buildClassExecutables(classElement, serializedClass); |
| 1831 } | 1839 } |
| 1832 fields = null; | 1840 fields = null; |
| 1833 constructors = null; | 1841 constructors = null; |
| 1834 return classElement; | 1842 return classElement; |
| 1835 } | 1843 } |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2109 serializedExecutable.inferredReturnTypeSlot, | 2117 serializedExecutable.inferredReturnTypeSlot, |
| 2110 _currentTypeParameterizedElement) ?? | 2118 _currentTypeParameterizedElement) ?? |
| 2111 buildType( | 2119 buildType( |
| 2112 serializedExecutable.returnType, _currentTypeParameterizedElement, | 2120 serializedExecutable.returnType, _currentTypeParameterizedElement, |
| 2113 defaultVoid: isSetter && summaryResynthesizer.strongMode); | 2121 defaultVoid: isSetter && summaryResynthesizer.strongMode); |
| 2114 executableElement.hasImplicitReturnType = | 2122 executableElement.hasImplicitReturnType = |
| 2115 serializedExecutable.returnType == null; | 2123 serializedExecutable.returnType == null; |
| 2116 } | 2124 } |
| 2117 executableElement.type = new FunctionTypeImpl.elementWithNameAndArgs( | 2125 executableElement.type = new FunctionTypeImpl.elementWithNameAndArgs( |
| 2118 executableElement, null, getCurrentTypeArguments(skipLevels: 1), false); | 2126 executableElement, null, getCurrentTypeArguments(skipLevels: 1), false); |
| 2119 buildAnnotations(executableElement, serializedExecutable.annotations); | |
| 2120 executableElement.functions = | 2127 executableElement.functions = |
| 2121 serializedExecutable.localFunctions.map(buildLocalFunction).toList(); | 2128 serializedExecutable.localFunctions.map(buildLocalFunction).toList(); |
| 2122 executableElement.labels = | 2129 executableElement.labels = |
| 2123 serializedExecutable.localLabels.map(buildLocalLabel).toList(); | 2130 serializedExecutable.localLabels.map(buildLocalLabel).toList(); |
| 2124 executableElement.localVariables = | 2131 executableElement.localVariables = |
| 2125 serializedExecutable.localVariables.map(buildLocalVariable).toList(); | 2132 serializedExecutable.localVariables.map(buildLocalVariable).toList(); |
| 2126 currentTypeParameters.removeLast(); | 2133 currentTypeParameters.removeLast(); |
| 2127 } | 2134 } |
| 2128 | 2135 |
| 2129 /** | 2136 /** |
| (...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2442 new FunctionTypeAliasElementImpl.forSerialized( | 2449 new FunctionTypeAliasElementImpl.forSerialized( |
| 2443 _resynthesizerContext, serializedTypedef); | 2450 _resynthesizerContext, serializedTypedef); |
| 2444 functionTypeAliasElement.typeParameters = | 2451 functionTypeAliasElement.typeParameters = |
| 2445 buildTypeParameters(serializedTypedef.typeParameters); | 2452 buildTypeParameters(serializedTypedef.typeParameters); |
| 2446 functionTypeAliasElement.parameters = | 2453 functionTypeAliasElement.parameters = |
| 2447 serializedTypedef.parameters.map(buildParameter).toList(); | 2454 serializedTypedef.parameters.map(buildParameter).toList(); |
| 2448 functionTypeAliasElement.returnType = buildType( | 2455 functionTypeAliasElement.returnType = buildType( |
| 2449 serializedTypedef.returnType, _currentTypeParameterizedElement); | 2456 serializedTypedef.returnType, _currentTypeParameterizedElement); |
| 2450 functionTypeAliasElement.type = | 2457 functionTypeAliasElement.type = |
| 2451 new FunctionTypeImpl.forTypedef(functionTypeAliasElement); | 2458 new FunctionTypeImpl.forTypedef(functionTypeAliasElement); |
| 2452 buildAnnotations(functionTypeAliasElement, serializedTypedef.annotations); | |
| 2453 unitHolder.addTypeAlias(functionTypeAliasElement); | 2459 unitHolder.addTypeAlias(functionTypeAliasElement); |
| 2454 currentTypeParameters.removeLast(); | 2460 currentTypeParameters.removeLast(); |
| 2455 assert(currentTypeParameters.isEmpty); | 2461 assert(currentTypeParameters.isEmpty); |
| 2456 } | 2462 } |
| 2457 | 2463 |
| 2458 /** | 2464 /** |
| 2459 * Resynthesize a [TypeParameterElement], handling all parts of its except | 2465 * Resynthesize a [TypeParameterElement], handling all parts of its except |
| 2460 * its bound. | 2466 * its bound. |
| 2461 * | 2467 * |
| 2462 * The bound is deferred until later since it may refer to other type | 2468 * The bound is deferred until later since it may refer to other type |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2850 static String _getElementIdentifier(String name, ReferenceKind kind) { | 2856 static String _getElementIdentifier(String name, ReferenceKind kind) { |
| 2851 if (kind == ReferenceKind.topLevelPropertyAccessor || | 2857 if (kind == ReferenceKind.topLevelPropertyAccessor || |
| 2852 kind == ReferenceKind.propertyAccessor) { | 2858 kind == ReferenceKind.propertyAccessor) { |
| 2853 if (!name.endsWith('=')) { | 2859 if (!name.endsWith('=')) { |
| 2854 return name + '?'; | 2860 return name + '?'; |
| 2855 } | 2861 } |
| 2856 } | 2862 } |
| 2857 return name; | 2863 return name; |
| 2858 } | 2864 } |
| 2859 } | 2865 } |
| OLD | NEW |