Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(529)

Unified Diff: pkg/analyzer/lib/src/summary/resynthesize.dart

Issue 1657033002: Resynthesize instance creation expressions. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/summary/resynthesize.dart
diff --git a/pkg/analyzer/lib/src/summary/resynthesize.dart b/pkg/analyzer/lib/src/summary/resynthesize.dart
index 9f24549b4f963c3950f5f4eb8b901aa14f86a74f..ec1401ef45e3094f8c3846173f5e857e8e37ee85 100644
--- a/pkg/analyzer/lib/src/summary/resynthesize.dart
+++ b/pkg/analyzer/lib/src/summary/resynthesize.dart
@@ -422,6 +422,8 @@ class _ConstExprBuilder {
}
break;
case UnlinkedConstOperation.invokeConstructor:
+ _pushInstanceCreation();
+ break;
case UnlinkedConstOperation.length:
return AstFactory.nullLiteral();
// throw new StateError('Unsupported constant operation $operation');
@@ -480,6 +482,65 @@ class _ConstExprBuilder {
_push(AstFactory.binaryExpression(left, operator, right));
}
+ void _pushInstanceCreation() {
+ EntityRef ref = uc.references[refPtr++];
+ _ReferenceInfo info = resynthesizer.referenceInfos[ref.reference];
+ // prepare ClassElement / ConstructorElement
+ String className;
+ ClassElement classElement;
+ String constructorName;
+ ConstructorElement constructorElement;
+ if (info.element is ConstructorElement) {
+ constructorName = info.name;
+ constructorElement = info.element;
+ className = info.enclosing.name;
+ classElement = info.enclosing.element as ClassElement;
+ } else if (info.element is ClassElement) {
+ className = info.name;
+ classElement = info.element;
+ constructorName = null;
+ constructorElement = new ConstructorElementHandle(
+ resynthesizer.summaryResynthesizer,
+ new ElementLocationImpl.con3(
+ classElement.location.components.toList()..add('')));
+ } else {
+ throw new StateError('Unsupported element for invokeConstructor '
+ '${info.element?.runtimeType}');
+ }
+ // prepare arguments
+ List<Expression> arguments;
+ {
+ int numNamedArgs = uc.ints[intPtr++];
+ int numPositionalArgs = uc.ints[intPtr++];
+ int numArgs = numNamedArgs + numPositionalArgs;
+ arguments = _removeTopItems(numArgs);
+ // add names to the named arguments
+ for (int i = 0; i < numNamedArgs; i++) {
+ String name = uc.strings[stringPtr++];
+ int index = numPositionalArgs + i;
+ arguments[index] = AstFactory.namedExpression2(name, arguments[index]);
+ }
+ }
+ // create TypeName
+ SimpleIdentifier typeNameNode = AstFactory.identifier3(className);
+ typeNameNode.staticElement = classElement;
+ TypeName typeNode = AstFactory.typeName3(typeNameNode);
+ // create ConstructorName
+ ConstructorName constructorNode;
+ if (constructorName != null) {
+ constructorNode = AstFactory.constructorName(typeNode, info.name);
+ constructorNode.name.staticElement = constructorElement;
+ } else {
+ constructorNode = AstFactory.constructorName(typeNode, null);
+ }
+ constructorNode.staticElement = constructorElement;
+ // create InstanceCreationExpression
+ InstanceCreationExpression instanceCreation = AstFactory
+ .instanceCreationExpression(Keyword.CONST, constructorNode, arguments);
+ instanceCreation.staticElement = constructorElement;
+ _push(instanceCreation);
+ }
+
void _pushList(TypeArgumentList typeArguments) {
int count = uc.ints[intPtr++];
List<Expression> elements = <Expression>[];
@@ -504,6 +565,14 @@ class _ConstExprBuilder {
Expression operand = _pop();
_push(AstFactory.prefixExpression(operator, operand));
}
+
+ List<Expression> _removeTopItems(int count) {
+ int start = stack.length - count;
+ int end = stack.length;
+ List<Expression> items = stack.getRange(start, end).toList();
+ stack.removeRange(start, end);
+ return items;
+ }
}
/**
@@ -1451,6 +1520,7 @@ class _LibraryResynthesizer {
referenceInfos = new List<_ReferenceInfo>(numLinkedReferences);
for (int i = 0; i < numLinkedReferences; i++) {
LinkedReference linkedReference = linkedUnit.references[i];
+ _ReferenceInfo enclosingInfo = null;
String name;
int containingReference;
if (i < numUnlinkedReferences) {
@@ -1476,11 +1546,9 @@ class _LibraryResynthesizer {
if (containingReference != 0 &&
referenceInfos[containingReference].element is ClassElement) {
String identifier = _getElementIdentifier(name, linkedReference.kind);
- locationComponents = referenceInfos[containingReference]
- .element
- .location
- .components
- .toList();
+ enclosingInfo = referenceInfos[containingReference];
+ locationComponents =
+ enclosingInfo.element.location.components.toList();
locationComponents.add(identifier);
} else {
String identifier = _getElementIdentifier(name, linkedReference.kind);
@@ -1501,6 +1569,11 @@ class _LibraryResynthesizer {
element = new PropertyAccessorElementHandle(
summaryResynthesizer, location);
break;
+ case ReferenceKind.constructor:
+ assert(location.components.length == 4);
+ element =
+ new ConstructorElementHandle(summaryResynthesizer, location);
+ break;
case ReferenceKind.propertyAccessor:
assert(location.components.length == 4);
element = new PropertyAccessorElementHandle(
@@ -1517,8 +1590,8 @@ class _LibraryResynthesizer {
break;
}
}
- referenceInfos[i] = new _ReferenceInfo(
- name, element, type, linkedReference.numTypeParameters);
+ referenceInfos[i] = new _ReferenceInfo(enclosingInfo, name, element, type,
+ linkedReference.numTypeParameters);
}
}
@@ -1600,6 +1673,11 @@ class _LibraryResynthesizer {
*/
class _ReferenceInfo {
/**
+ * The enclosing [_ReferenceInfo], or `null` for top-level elements.
+ */
+ final _ReferenceInfo enclosing;
+
+ /**
* The name of the entity referred to by this reference.
*/
final String name;
@@ -1632,8 +1710,8 @@ class _ReferenceInfo {
* the type itself. Otherwise, pass `null` and the type will be computed
* when appropriate.
*/
- _ReferenceInfo(
- this.name, this.element, DartType specialType, this.numTypeParameters) {
+ _ReferenceInfo(this.enclosing, this.name, this.element, DartType specialType,
+ this.numTypeParameters) {
if (specialType != null) {
type = specialType;
} else {
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/resynthesize_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698