| 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 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 277 | 277 |
| 278 int intPtr = 0; | 278 int intPtr = 0; |
| 279 int doublePtr = 0; | 279 int doublePtr = 0; |
| 280 int stringPtr = 0; | 280 int stringPtr = 0; |
| 281 int refPtr = 0; | 281 int refPtr = 0; |
| 282 final List<Expression> stack = <Expression>[]; | 282 final List<Expression> stack = <Expression>[]; |
| 283 | 283 |
| 284 _ConstExprBuilder(this.resynthesizer, this.uc); | 284 _ConstExprBuilder(this.resynthesizer, this.uc); |
| 285 | 285 |
| 286 Expression build() { | 286 Expression build() { |
| 287 Expression expr = _build(); | |
| 288 if (uc.name.isNotEmpty) { | |
| 289 return AstFactory.namedExpression2(uc.name, expr); | |
| 290 } | |
| 291 return expr; | |
| 292 } | |
| 293 | |
| 294 Expression _build() { | |
| 295 if (!uc.isValidConst) { | 287 if (!uc.isValidConst) { |
| 296 return AstFactory.identifier3(r'$$invalidConstExpr$$'); | 288 return AstFactory.identifier3(r'$$invalidConstExpr$$'); |
| 297 } | 289 } |
| 298 for (UnlinkedConstOperation operation in uc.operations) { | 290 for (UnlinkedConstOperation operation in uc.operations) { |
| 299 switch (operation) { | 291 switch (operation) { |
| 300 case UnlinkedConstOperation.pushNull: | 292 case UnlinkedConstOperation.pushNull: |
| 301 _push(AstFactory.nullLiteral()); | 293 _push(AstFactory.nullLiteral()); |
| 302 break; | 294 break; |
| 303 // bool | 295 // bool |
| 304 case UnlinkedConstOperation.pushFalse: | 296 case UnlinkedConstOperation.pushFalse: |
| (...skipping 1419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1724 } else { | 1716 } else { |
| 1725 HideElementCombinatorImpl combinator = new HideElementCombinatorImpl(); | 1717 HideElementCombinatorImpl combinator = new HideElementCombinatorImpl(); |
| 1726 // Note: we call toList() so that we don't retain a reference to the | 1718 // Note: we call toList() so that we don't retain a reference to the |
| 1727 // deserialized data structure. | 1719 // deserialized data structure. |
| 1728 combinator.hiddenNames = serializedCombinator.hides.toList(); | 1720 combinator.hiddenNames = serializedCombinator.hides.toList(); |
| 1729 return combinator; | 1721 return combinator; |
| 1730 } | 1722 } |
| 1731 } | 1723 } |
| 1732 | 1724 |
| 1733 /** | 1725 /** |
| 1734 * Resynthesize the [ConstructorInitializer] in context of | |
| 1735 * [currentConstructor], which is used to resolve constructor parameter names. | |
| 1736 */ | |
| 1737 ConstructorInitializer buildConstantInitializer( | |
| 1738 UnlinkedConstructorInitializer serialized) { | |
| 1739 UnlinkedConstructorInitializerKind kind = serialized.kind; | |
| 1740 String name = serialized.name; | |
| 1741 List<Expression> arguments = | |
| 1742 serialized.arguments.map(_buildConstExpression).toList(); | |
| 1743 switch (kind) { | |
| 1744 case UnlinkedConstructorInitializerKind.field: | |
| 1745 return AstFactory.constructorFieldInitializer( | |
| 1746 false, name, _buildConstExpression(serialized.expression)); | |
| 1747 case UnlinkedConstructorInitializerKind.superInvocation: | |
| 1748 return AstFactory.superConstructorInvocation2( | |
| 1749 name.isNotEmpty ? name : null, arguments); | |
| 1750 case UnlinkedConstructorInitializerKind.thisInvocation: | |
| 1751 return AstFactory.redirectingConstructorInvocation2( | |
| 1752 name.isNotEmpty ? name : null, arguments); | |
| 1753 } | |
| 1754 } | |
| 1755 | |
| 1756 /** | |
| 1757 * Resynthesize a [ConstructorElement] and place it in the given [holder]. | 1726 * Resynthesize a [ConstructorElement] and place it in the given [holder]. |
| 1758 * [classType] is the type of the class for which this element is a | 1727 * [classType] is the type of the class for which this element is a |
| 1759 * constructor. | 1728 * constructor. |
| 1760 */ | 1729 */ |
| 1761 void buildConstructor(UnlinkedExecutable serializedExecutable, | 1730 void buildConstructor(UnlinkedExecutable serializedExecutable, |
| 1762 ElementHolder holder, InterfaceType classType) { | 1731 ElementHolder holder, InterfaceType classType) { |
| 1763 assert(serializedExecutable.kind == UnlinkedExecutableKind.constructor); | 1732 assert(serializedExecutable.kind == UnlinkedExecutableKind.constructor); |
| 1764 currentConstructor = new ConstructorElementImpl( | 1733 currentConstructor = new ConstructorElementImpl( |
| 1765 serializedExecutable.name, serializedExecutable.nameOffset); | 1734 serializedExecutable.name, serializedExecutable.nameOffset); |
| 1766 currentConstructor.isCycleFree = serializedExecutable.isConst && | 1735 currentConstructor.isCycleFree = serializedExecutable.isConst && |
| 1767 !constCycles.contains(serializedExecutable.constCycleSlot); | 1736 !constCycles.contains(serializedExecutable.constCycleSlot); |
| 1768 if (serializedExecutable.name.isEmpty) { | 1737 if (serializedExecutable.name.isEmpty) { |
| 1769 currentConstructor.nameEnd = | 1738 currentConstructor.nameEnd = |
| 1770 serializedExecutable.nameOffset + classType.name.length; | 1739 serializedExecutable.nameOffset + classType.name.length; |
| 1771 } else { | 1740 } else { |
| 1772 currentConstructor.nameEnd = serializedExecutable.nameEnd; | 1741 currentConstructor.nameEnd = serializedExecutable.nameEnd; |
| 1773 currentConstructor.periodOffset = serializedExecutable.periodOffset; | 1742 currentConstructor.periodOffset = serializedExecutable.periodOffset; |
| 1774 } | 1743 } |
| 1775 constructors[serializedExecutable.name] = currentConstructor; | 1744 constructors[serializedExecutable.name] = currentConstructor; |
| 1776 currentConstructor.returnType = classType; | 1745 currentConstructor.returnType = classType; |
| 1777 buildExecutableCommonParts(currentConstructor, serializedExecutable); | 1746 buildExecutableCommonParts(currentConstructor, serializedExecutable); |
| 1778 currentConstructor.factory = serializedExecutable.isFactory; | 1747 currentConstructor.factory = serializedExecutable.isFactory; |
| 1779 currentConstructor.const2 = serializedExecutable.isConst; | 1748 currentConstructor.const2 = serializedExecutable.isConst; |
| 1780 currentConstructor.constantInitializers = serializedExecutable | 1749 currentConstructor.constantInitializers = serializedExecutable |
| 1781 .constantInitializers | 1750 .constantInitializers |
| 1782 .map(buildConstantInitializer) | 1751 .map(buildConstructorInitializer) |
| 1783 .toList(); | 1752 .toList(); |
| 1784 if (serializedExecutable.isRedirectedConstructor) { | 1753 if (serializedExecutable.isRedirectedConstructor) { |
| 1785 if (serializedExecutable.isFactory) { | 1754 if (serializedExecutable.isFactory) { |
| 1786 EntityRef redirectedConstructor = | 1755 EntityRef redirectedConstructor = |
| 1787 serializedExecutable.redirectedConstructor; | 1756 serializedExecutable.redirectedConstructor; |
| 1788 _ReferenceInfo info = referenceInfos[redirectedConstructor.reference]; | 1757 _ReferenceInfo info = referenceInfos[redirectedConstructor.reference]; |
| 1789 List<EntityRef> typeArguments = redirectedConstructor.typeArguments; | 1758 List<EntityRef> typeArguments = redirectedConstructor.typeArguments; |
| 1790 currentConstructor.redirectedConstructor = _createConstructorElement( | 1759 currentConstructor.redirectedConstructor = _createConstructorElement( |
| 1791 _createConstructorDefiningType(info, typeArguments), info); | 1760 _createConstructorDefiningType(info, typeArguments), info); |
| 1792 } else { | 1761 } else { |
| 1793 List<String> locationComponents = unit.location.components.toList(); | 1762 List<String> locationComponents = unit.location.components.toList(); |
| 1794 locationComponents.add(classType.name); | 1763 locationComponents.add(classType.name); |
| 1795 locationComponents.add(serializedExecutable.redirectedConstructorName); | 1764 locationComponents.add(serializedExecutable.redirectedConstructorName); |
| 1796 currentConstructor.redirectedConstructor = | 1765 currentConstructor.redirectedConstructor = |
| 1797 new _DeferredConstructorElement._( | 1766 new _DeferredConstructorElement._( |
| 1798 classType, | 1767 classType, |
| 1799 serializedExecutable.redirectedConstructorName, | 1768 serializedExecutable.redirectedConstructorName, |
| 1800 new ElementLocationImpl.con3(locationComponents)); | 1769 new ElementLocationImpl.con3(locationComponents)); |
| 1801 } | 1770 } |
| 1802 } | 1771 } |
| 1803 holder.addConstructor(currentConstructor); | 1772 holder.addConstructor(currentConstructor); |
| 1804 currentConstructor = null; | 1773 currentConstructor = null; |
| 1805 } | 1774 } |
| 1806 | 1775 |
| 1807 /** | 1776 /** |
| 1777 * Resynthesize the [ConstructorInitializer] in context of |
| 1778 * [currentConstructor], which is used to resolve constructor parameter names. |
| 1779 */ |
| 1780 ConstructorInitializer buildConstructorInitializer( |
| 1781 UnlinkedConstructorInitializer serialized) { |
| 1782 UnlinkedConstructorInitializerKind kind = serialized.kind; |
| 1783 String name = serialized.name; |
| 1784 List<Expression> arguments = <Expression>[]; |
| 1785 { |
| 1786 int numArguments = serialized.arguments.length; |
| 1787 int numNames = serialized.argumentNames.length; |
| 1788 for (int i = 0; i < numArguments; i++) { |
| 1789 Expression expression = _buildConstExpression(serialized.arguments[i]); |
| 1790 int nameIndex = numNames + i - numArguments; |
| 1791 if (nameIndex >= 0) { |
| 1792 expression = AstFactory.namedExpression2( |
| 1793 serialized.argumentNames[nameIndex], expression); |
| 1794 } |
| 1795 arguments.add(expression); |
| 1796 } |
| 1797 } |
| 1798 switch (kind) { |
| 1799 case UnlinkedConstructorInitializerKind.field: |
| 1800 return AstFactory.constructorFieldInitializer( |
| 1801 false, name, _buildConstExpression(serialized.expression)); |
| 1802 case UnlinkedConstructorInitializerKind.superInvocation: |
| 1803 return AstFactory.superConstructorInvocation2( |
| 1804 name.isNotEmpty ? name : null, arguments); |
| 1805 case UnlinkedConstructorInitializerKind.thisInvocation: |
| 1806 return AstFactory.redirectingConstructorInvocation2( |
| 1807 name.isNotEmpty ? name : null, arguments); |
| 1808 } |
| 1809 } |
| 1810 |
| 1811 /** |
| 1808 * Build the documentation for the given [element]. Does nothing if | 1812 * Build the documentation for the given [element]. Does nothing if |
| 1809 * [serializedDocumentationComment] is `null`. | 1813 * [serializedDocumentationComment] is `null`. |
| 1810 */ | 1814 */ |
| 1811 void buildDocumentation(ElementImpl element, | 1815 void buildDocumentation(ElementImpl element, |
| 1812 UnlinkedDocumentationComment serializedDocumentationComment) { | 1816 UnlinkedDocumentationComment serializedDocumentationComment) { |
| 1813 if (serializedDocumentationComment != null) { | 1817 if (serializedDocumentationComment != null) { |
| 1814 element.documentationComment = serializedDocumentationComment.text; | 1818 element.documentationComment = serializedDocumentationComment.text; |
| 1815 element.setDocRange(serializedDocumentationComment.offset, | 1819 element.setDocRange(serializedDocumentationComment.offset, |
| 1816 serializedDocumentationComment.length); | 1820 serializedDocumentationComment.length); |
| 1817 } | 1821 } |
| (...skipping 896 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2714 static String _getElementIdentifier(String name, ReferenceKind kind) { | 2718 static String _getElementIdentifier(String name, ReferenceKind kind) { |
| 2715 if (kind == ReferenceKind.topLevelPropertyAccessor || | 2719 if (kind == ReferenceKind.topLevelPropertyAccessor || |
| 2716 kind == ReferenceKind.propertyAccessor) { | 2720 kind == ReferenceKind.propertyAccessor) { |
| 2717 if (!name.endsWith('=')) { | 2721 if (!name.endsWith('=')) { |
| 2718 return name + '?'; | 2722 return name + '?'; |
| 2719 } | 2723 } |
| 2720 } | 2724 } |
| 2721 return name; | 2725 return name; |
| 2722 } | 2726 } |
| 2723 } | 2727 } |
| OLD | NEW |