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 576 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 TokenFactory.tokenFromType(TokenType.CLOSE_CURLY_BRACKET)); | 587 TokenFactory.tokenFromType(TokenType.CLOSE_CURLY_BRACKET)); |
588 } | 588 } |
589 } | 589 } |
590 | 590 |
591 /** | 591 /** |
592 * Convert the next reference to the [DartType] and return the AST | 592 * Convert the next reference to the [DartType] and return the AST |
593 * corresponding to this type. | 593 * corresponding to this type. |
594 */ | 594 */ |
595 TypeAnnotation _newTypeName() { | 595 TypeAnnotation _newTypeName() { |
596 EntityRef typeRef = uc.references[refPtr++]; | 596 EntityRef typeRef = uc.references[refPtr++]; |
597 DartType type = | 597 DartType type = resynthesizer.buildType(context, typeRef); |
598 resynthesizer.buildType(typeRef, context?.typeParameterContext); | |
599 return _buildTypeAst(type); | 598 return _buildTypeAst(type); |
600 } | 599 } |
601 | 600 |
602 Expression _pop() => stack.removeLast(); | 601 Expression _pop() => stack.removeLast(); |
603 | 602 |
604 void _push(Expression expr) { | 603 void _push(Expression expr) { |
605 stack.add(expr); | 604 stack.add(expr); |
606 } | 605 } |
607 | 606 |
608 void _pushBinary(TokenType operator) { | 607 void _pushBinary(TokenType operator) { |
(...skipping 27 matching lines...) Expand all Loading... |
636 constructorName = null; | 635 constructorName = null; |
637 } else { | 636 } else { |
638 List<Expression> arguments = _buildArguments(); | 637 List<Expression> arguments = _buildArguments(); |
639 SimpleIdentifier name = AstTestFactory.identifier3(info.name); | 638 SimpleIdentifier name = AstTestFactory.identifier3(info.name); |
640 name.staticElement = info.element; | 639 name.staticElement = info.element; |
641 name.setProperty(ARGUMENT_LIST, AstTestFactory.argumentList(arguments)); | 640 name.setProperty(ARGUMENT_LIST, AstTestFactory.argumentList(arguments)); |
642 _push(name); | 641 _push(name); |
643 return; | 642 return; |
644 } | 643 } |
645 InterfaceType definingType = resynthesizer._createConstructorDefiningType( | 644 InterfaceType definingType = resynthesizer._createConstructorDefiningType( |
646 context?.typeParameterContext, info, ref.typeArguments); | 645 context, info, ref.typeArguments); |
647 constructorElement = | 646 constructorElement = |
648 resynthesizer._getConstructorForInfo(definingType, info); | 647 resynthesizer._getConstructorForInfo(definingType, info); |
649 typeNode = _buildTypeAst(definingType); | 648 typeNode = _buildTypeAst(definingType); |
650 } else { | 649 } else { |
651 if (info.enclosing != null) { | 650 if (info.enclosing != null) { |
652 if (info.enclosing.enclosing != null) { | 651 if (info.enclosing.enclosing != null) { |
653 PrefixedIdentifier typeName = AstTestFactory.identifier5( | 652 PrefixedIdentifier typeName = AstTestFactory.identifier5( |
654 info.enclosing.enclosing.name, info.enclosing.name); | 653 info.enclosing.enclosing.name, info.enclosing.name); |
655 typeName.prefix.staticElement = info.enclosing.enclosing.element; | 654 typeName.prefix.staticElement = info.enclosing.enclosing.element; |
656 typeName.identifier.staticElement = info.enclosing.element; | 655 typeName.identifier.staticElement = info.enclosing.element; |
(...skipping 775 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1432 return _unitResynthesizer.parametersInheritingCovariant.contains(slot); | 1431 return _unitResynthesizer.parametersInheritingCovariant.contains(slot); |
1433 } | 1432 } |
1434 | 1433 |
1435 @override | 1434 @override |
1436 bool isInConstCycle(int slot) { | 1435 bool isInConstCycle(int slot) { |
1437 return _unitResynthesizer.constCycles.contains(slot); | 1436 return _unitResynthesizer.constCycles.contains(slot); |
1438 } | 1437 } |
1439 | 1438 |
1440 @override | 1439 @override |
1441 ConstructorElement resolveConstructorRef( | 1440 ConstructorElement resolveConstructorRef( |
1442 TypeParameterizedElementMixin typeParameterContext, EntityRef entry) { | 1441 ElementImpl context, EntityRef entry) { |
1443 return _unitResynthesizer._getConstructorForEntry( | 1442 return _unitResynthesizer._getConstructorForEntry(context, entry); |
1444 typeParameterContext, entry); | |
1445 } | 1443 } |
1446 | 1444 |
1447 @override | 1445 @override |
1448 DartType resolveLinkedType( | 1446 DartType resolveLinkedType(ElementImpl context, int slot) { |
1449 int slot, TypeParameterizedElementMixin typeParameterContext) { | 1447 return _unitResynthesizer.buildLinkedType(context, slot); |
1450 return _unitResynthesizer.buildLinkedType(slot, typeParameterContext); | |
1451 } | 1448 } |
1452 | 1449 |
1453 @override | 1450 @override |
1454 DartType resolveTypeRef( | 1451 DartType resolveTypeRef(ElementImpl context, EntityRef type, |
1455 EntityRef type, TypeParameterizedElementMixin typeParameterContext, | |
1456 {bool defaultVoid: false, | 1452 {bool defaultVoid: false, |
1457 bool instantiateToBoundsAllowed: true, | 1453 bool instantiateToBoundsAllowed: true, |
1458 bool declaredType: false}) { | 1454 bool declaredType: false}) { |
1459 return _unitResynthesizer.buildType(type, typeParameterContext, | 1455 return _unitResynthesizer.buildType(context, type, |
1460 defaultVoid: defaultVoid, | 1456 defaultVoid: defaultVoid, |
1461 instantiateToBoundsAllowed: instantiateToBoundsAllowed, | 1457 instantiateToBoundsAllowed: instantiateToBoundsAllowed, |
1462 declaredType: declaredType); | 1458 declaredType: declaredType); |
1463 } | 1459 } |
1464 } | 1460 } |
1465 | 1461 |
1466 /** | 1462 /** |
1467 * An instance of [_UnitResynthesizer] is responsible for resynthesizing the | 1463 * An instance of [_UnitResynthesizer] is responsible for resynthesizing the |
1468 * elements in a single unit from that unit's summary. | 1464 * elements in a single unit from that unit's summary. |
1469 */ | 1465 */ |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1618 PropertyAccessorElementImpl_ImplicitSetter setter = | 1614 PropertyAccessorElementImpl_ImplicitSetter setter = |
1619 new PropertyAccessorElementImpl_ImplicitSetter(property); | 1615 new PropertyAccessorElementImpl_ImplicitSetter(property); |
1620 setter.enclosingElement = property.enclosingElement; | 1616 setter.enclosingElement = property.enclosingElement; |
1621 return setter; | 1617 return setter; |
1622 } | 1618 } |
1623 | 1619 |
1624 /** | 1620 /** |
1625 * Build the appropriate [DartType] object corresponding to a slot id in the | 1621 * Build the appropriate [DartType] object corresponding to a slot id in the |
1626 * [LinkedUnit.types] table. | 1622 * [LinkedUnit.types] table. |
1627 */ | 1623 */ |
1628 DartType buildLinkedType( | 1624 DartType buildLinkedType(ElementImpl context, int slot) { |
1629 int slot, TypeParameterizedElementMixin typeParameterContext) { | |
1630 if (slot == 0) { | 1625 if (slot == 0) { |
1631 // A slot id of 0 means there is no [DartType] object to build. | 1626 // A slot id of 0 means there is no [DartType] object to build. |
1632 return null; | 1627 return null; |
1633 } | 1628 } |
1634 EntityRef type = linkedTypeMap[slot]; | 1629 EntityRef type = linkedTypeMap[slot]; |
1635 if (type == null) { | 1630 if (type == null) { |
1636 // A missing entry in [LinkedUnit.types] means there is no [DartType] | 1631 // A missing entry in [LinkedUnit.types] means there is no [DartType] |
1637 // stored in this slot. | 1632 // stored in this slot. |
1638 return null; | 1633 return null; |
1639 } | 1634 } |
1640 return buildType(type, typeParameterContext); | 1635 return buildType(context, type); |
1641 } | 1636 } |
1642 | 1637 |
1643 /** | 1638 /** |
1644 * Build a [DartType] object based on a [EntityRef]. This [DartType] | 1639 * Build a [DartType] object based on a [EntityRef]. This [DartType] |
1645 * may refer to elements in other libraries than the library being | 1640 * may refer to elements in other libraries than the library being |
1646 * deserialized, so handles are used to avoid having to deserialize other | 1641 * deserialized, so handles are used to avoid having to deserialize other |
1647 * libraries in the process. | 1642 * libraries in the process. |
1648 */ | 1643 */ |
1649 DartType buildType( | 1644 DartType buildType(ElementImpl context, EntityRef type, |
1650 EntityRef type, TypeParameterizedElementMixin typeParameterContext, | |
1651 {bool defaultVoid: false, | 1645 {bool defaultVoid: false, |
1652 bool instantiateToBoundsAllowed: true, | 1646 bool instantiateToBoundsAllowed: true, |
1653 bool declaredType: false}) { | 1647 bool declaredType: false}) { |
1654 if (type == null) { | 1648 if (type == null) { |
1655 if (defaultVoid) { | 1649 if (defaultVoid) { |
1656 return VoidTypeImpl.instance; | 1650 return VoidTypeImpl.instance; |
1657 } else { | 1651 } else { |
1658 return DynamicTypeImpl.instance; | 1652 return DynamicTypeImpl.instance; |
1659 } | 1653 } |
1660 } | 1654 } |
1661 if (type.paramReference != 0) { | 1655 if (type.paramReference != 0) { |
1662 return typeParameterContext.getTypeParameterType(type.paramReference); | 1656 return context.typeParameterContext |
| 1657 .getTypeParameterType(type.paramReference); |
1663 } else if (type.entityKind == EntityRefKind.genericFunctionType) { | 1658 } else if (type.entityKind == EntityRefKind.genericFunctionType) { |
1664 GenericFunctionTypeElement element = | 1659 GenericFunctionTypeElement element = |
1665 new GenericFunctionTypeElementImpl.forSerialized( | 1660 new GenericFunctionTypeElementImpl.forSerialized(context, type); |
1666 type, typeParameterContext); | |
1667 return element.type; | 1661 return element.type; |
1668 } else if (type.syntheticReturnType != null) { | 1662 } else if (type.syntheticReturnType != null) { |
1669 FunctionElementImpl element = | 1663 FunctionElementImpl element = new FunctionElementImpl_forLUB( |
1670 new FunctionElementImpl_forLUB(unit, typeParameterContext, type); | 1664 unit, context.typeParameterContext, type); |
1671 return element.type; | 1665 return element.type; |
1672 } else { | 1666 } else { |
1673 DartType getTypeArgument(int i) { | 1667 DartType getTypeArgument(int i) { |
1674 if (i < type.typeArguments.length) { | 1668 if (i < type.typeArguments.length) { |
1675 return buildType(type.typeArguments[i], typeParameterContext, | 1669 return buildType(context, type.typeArguments[i], |
1676 declaredType: declaredType); | 1670 declaredType: declaredType); |
1677 } else { | 1671 } else { |
1678 return DynamicTypeImpl.instance; | 1672 return DynamicTypeImpl.instance; |
1679 } | 1673 } |
1680 } | 1674 } |
1681 | 1675 |
1682 _ReferenceInfo referenceInfo = getReferenceInfo(type.reference); | 1676 _ReferenceInfo referenceInfo = getReferenceInfo(type.reference); |
1683 if (declaredType && !referenceInfo.isDeclarableType) { | 1677 if (declaredType && !referenceInfo.isDeclarableType) { |
1684 return DynamicTypeImpl.instance; | 1678 return DynamicTypeImpl.instance; |
1685 } | 1679 } |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1922 | 1916 |
1923 Expression _buildConstExpression(ElementImpl context, UnlinkedExpr uc) { | 1917 Expression _buildConstExpression(ElementImpl context, UnlinkedExpr uc) { |
1924 return new _ConstExprBuilder(this, context, uc).build(); | 1918 return new _ConstExprBuilder(this, context, uc).build(); |
1925 } | 1919 } |
1926 | 1920 |
1927 /** | 1921 /** |
1928 * Return the defining type for a [ConstructorElement] by applying | 1922 * Return the defining type for a [ConstructorElement] by applying |
1929 * [typeArgumentRefs] to the given linked [info]. Return [DynamicTypeImpl] | 1923 * [typeArgumentRefs] to the given linked [info]. Return [DynamicTypeImpl] |
1930 * if the [info] is unresolved. | 1924 * if the [info] is unresolved. |
1931 */ | 1925 */ |
1932 DartType _createConstructorDefiningType( | 1926 DartType _createConstructorDefiningType(ElementImpl context, |
1933 TypeParameterizedElementMixin typeParameterContext, | 1927 _ReferenceInfo info, List<EntityRef> typeArgumentRefs) { |
1934 _ReferenceInfo info, | |
1935 List<EntityRef> typeArgumentRefs) { | |
1936 bool isClass = info.element is ClassElement; | 1928 bool isClass = info.element is ClassElement; |
1937 _ReferenceInfo classInfo = isClass ? info : info.enclosing; | 1929 _ReferenceInfo classInfo = isClass ? info : info.enclosing; |
1938 if (classInfo == null) { | 1930 if (classInfo == null) { |
1939 return DynamicTypeImpl.instance; | 1931 return DynamicTypeImpl.instance; |
1940 } | 1932 } |
1941 List<DartType> typeArguments = typeArgumentRefs | 1933 List<DartType> typeArguments = |
1942 .map((t) => buildType(t, typeParameterContext)) | 1934 typeArgumentRefs.map((t) => buildType(context, t)).toList(); |
1943 .toList(); | |
1944 return classInfo.buildType(true, typeArguments.length, (i) { | 1935 return classInfo.buildType(true, typeArguments.length, (i) { |
1945 if (i < typeArguments.length) { | 1936 if (i < typeArguments.length) { |
1946 return typeArguments[i]; | 1937 return typeArguments[i]; |
1947 } else { | 1938 } else { |
1948 return DynamicTypeImpl.instance; | 1939 return DynamicTypeImpl.instance; |
1949 } | 1940 } |
1950 }, const <int>[]); | 1941 }, const <int>[]); |
1951 } | 1942 } |
1952 | 1943 |
1953 /** | 1944 /** |
1954 * Return the [ConstructorElement] corresponding to the given [entry]. | 1945 * Return the [ConstructorElement] corresponding to the given [entry]. |
1955 */ | 1946 */ |
1956 ConstructorElement _getConstructorForEntry( | 1947 ConstructorElement _getConstructorForEntry( |
1957 TypeParameterizedElementMixin typeParameterContext, EntityRef entry) { | 1948 ElementImpl context, EntityRef entry) { |
1958 _ReferenceInfo info = getReferenceInfo(entry.reference); | 1949 _ReferenceInfo info = getReferenceInfo(entry.reference); |
1959 DartType type = _createConstructorDefiningType( | 1950 DartType type = |
1960 typeParameterContext, info, entry.typeArguments); | 1951 _createConstructorDefiningType(context, info, entry.typeArguments); |
1961 if (type is InterfaceType) { | 1952 if (type is InterfaceType) { |
1962 return _getConstructorForInfo(type, info); | 1953 return _getConstructorForInfo(type, info); |
1963 } | 1954 } |
1964 return null; | 1955 return null; |
1965 } | 1956 } |
1966 | 1957 |
1967 /** | 1958 /** |
1968 * Return the [ConstructorElement] corresponding to the given linked [info], | 1959 * Return the [ConstructorElement] corresponding to the given linked [info], |
1969 * using the [classType] which has already been computed (e.g. by | 1960 * using the [classType] which has already been computed (e.g. by |
1970 * [_createConstructorDefiningType]). Both cases when [info] is a | 1961 * [_createConstructorDefiningType]). Both cases when [info] is a |
(...skipping 22 matching lines...) Expand all Loading... |
1993 static String _getElementIdentifier(String name, ReferenceKind kind) { | 1984 static String _getElementIdentifier(String name, ReferenceKind kind) { |
1994 if (kind == ReferenceKind.topLevelPropertyAccessor || | 1985 if (kind == ReferenceKind.topLevelPropertyAccessor || |
1995 kind == ReferenceKind.propertyAccessor) { | 1986 kind == ReferenceKind.propertyAccessor) { |
1996 if (!name.endsWith('=')) { | 1987 if (!name.endsWith('=')) { |
1997 return name + '?'; | 1988 return name + '?'; |
1998 } | 1989 } |
1999 } | 1990 } |
2000 return name; | 1991 return name; |
2001 } | 1992 } |
2002 } | 1993 } |
OLD | NEW |