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

Side by Side Diff: pkg/analyzer/lib/src/summary/link.dart

Issue 1850673004: Add support for type inference involving generics. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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 unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/summarize_ast_strong_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 /** 5 /**
6 * This library is capable of producing linked summaries from unlinked 6 * This library is capable of producing linked summaries from unlinked
7 * ones (or prelinked ones). It functions by building a miniature 7 * ones (or prelinked ones). It functions by building a miniature
8 * element model to represent the contents of the summaries, and then 8 * element model to represent the contents of the summaries, and then
9 * scanning the element model to gather linked information and adding 9 * scanning the element model to gather linked information and adding
10 * it to the summary data structures. 10 * it to the summary data structures.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 new _Linker(libraries, getDependency, getUnit, strong).link(); 121 new _Linker(libraries, getDependency, getUnit, strong).link();
122 } 122 }
123 123
124 /** 124 /**
125 * Create an [EntityRefBuilder] representing the given [type], in a form 125 * Create an [EntityRefBuilder] representing the given [type], in a form
126 * suitable for inclusion in [LinkedUnit.types]. [compilationUnit] is the 126 * suitable for inclusion in [LinkedUnit.types]. [compilationUnit] is the
127 * compilation unit in which the type will be used. If [slot] is provided, it 127 * compilation unit in which the type will be used. If [slot] is provided, it
128 * is stored in [EntityRefBuilder.slot]. 128 * is stored in [EntityRefBuilder.slot].
129 */ 129 */
130 EntityRefBuilder _createLinkedType( 130 EntityRefBuilder _createLinkedType(
131 DartType type, CompilationUnitElementInBuildUnit compilationUnit, 131 DartType type,
132 CompilationUnitElementInBuildUnit compilationUnit,
133 TypeParameterContext typeParameterContext,
132 {int slot}) { 134 {int slot}) {
133 EntityRefBuilder result = new EntityRefBuilder(slot: slot); 135 EntityRefBuilder result = new EntityRefBuilder(slot: slot);
134 if (type is InterfaceType) { 136 if (type is InterfaceType) {
135 ClassElementForLink element = type.element; 137 ClassElementForLink element = type.element;
136 int dependency = compilationUnit.library.addDependency(element.library); 138 result.reference = compilationUnit.addReference(element);
137 result.reference = compilationUnit.addReference(dependency, element.name, 139 if (type.typeArguments.isNotEmpty) {
138 element.typeParameters.length, element.enclosingElement.unitNum); 140 result.typeArguments = type.typeArguments
139 if (element.typeParameters.isNotEmpty) { 141 .map((DartType t) =>
140 // TODO(paulberry): implement. 142 _createLinkedType(t, compilationUnit, typeParameterContext))
141 throw new UnimplementedError(); 143 .toList();
142 } 144 }
143 return result; 145 return result;
144 } else if (type is VoidTypeImpl) { 146 } else if (type is VoidTypeImpl) {
145 result.reference = compilationUnit.addReference(0, 'void', 0, 0); 147 result.reference = compilationUnit.addRawReference('void');
146 return result; 148 return result;
149 } else if (type is TypeParameterType) {
150 TypeParameterElementForLink element = type.element;
151 result.paramReference =
152 typeParameterContext.typeParameterNestingLevel - element.nestingLevel;
153 return result;
154 } else if (type is FunctionType) {
155 Element element = type.element;
156 if (element is FunctionElementForLink_FunctionTypedParam) {
157 result.reference =
158 compilationUnit.addReference(element.enclosingExecutable);
159 result.implicitFunctionTypeIndices = element.implicitFunctionTypeIndices;
160 if (type.typeArguments.isNotEmpty) {
161 result.typeArguments = type.typeArguments
162 .map((DartType t) =>
163 _createLinkedType(t, compilationUnit, typeParameterContext))
164 .toList();
165 }
166 return result;
167 }
168 // TODO(paulberry): implement other cases.
169 throw new UnimplementedError('${element.runtimeType}');
147 } 170 }
171 // TODO(paulberry): implement other cases.
148 throw new UnimplementedError('${type.runtimeType}'); 172 throw new UnimplementedError('${type.runtimeType}');
149 } 173 }
150 174
151 /** 175 /**
152 * Type of the callback used by [link] and [relink] to request 176 * Type of the callback used by [link] and [relink] to request
153 * [LinkedLibrary] objects from other build units. 177 * [LinkedLibrary] objects from other build units.
154 */ 178 */
155 typedef LinkedLibrary GetDependencyCallback(String absoluteUri); 179 typedef LinkedLibrary GetDependencyCallback(String absoluteUri);
156 180
157 /** 181 /**
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 393
370 @override 394 @override
371 InterfaceType get supertype { 395 InterfaceType get supertype {
372 if (isObject) { 396 if (isObject) {
373 return null; 397 return null;
374 } 398 }
375 return _supertype ??= _computeInterfaceType(_unlinkedClass.supertype); 399 return _supertype ??= _computeInterfaceType(_unlinkedClass.supertype);
376 } 400 }
377 401
378 @override 402 @override
403 DartType get type =>
404 _type ??= buildType((int i) => typeParameterTypes[i], null);
405
406 @override
379 ConstructorElementForLink get unnamedConstructor { 407 ConstructorElementForLink get unnamedConstructor {
380 if (!_unnamedConstructorComputed) { 408 if (!_unnamedConstructorComputed) {
381 for (ConstructorElementForLink constructor in constructors) { 409 for (ConstructorElementForLink constructor in constructors) {
382 if (constructor.name.isEmpty) { 410 if (constructor.name.isEmpty) {
383 _unnamedConstructor = constructor; 411 _unnamedConstructor = constructor;
384 break; 412 break;
385 } 413 }
386 } 414 }
387 _unnamedConstructorComputed = true; 415 _unnamedConstructorComputed = true;
388 } 416 }
389 return _unnamedConstructor; 417 return _unnamedConstructor;
390 } 418 }
391 419
392 @override 420 @override
393 List<UnlinkedTypeParam> get _unlinkedTypeParams => 421 List<UnlinkedTypeParam> get _unlinkedTypeParams =>
394 _unlinkedClass.typeParameters; 422 _unlinkedClass.typeParameters;
395 423
396 @override 424 @override
397 DartType buildType( 425 DartType buildType(
398 DartType getTypeArgument(int i), List<int> implicitFunctionTypeIndices) { 426 DartType getTypeArgument(int i), List<int> implicitFunctionTypeIndices) {
399 int numTypeParameters = _unlinkedClass.typeParameters.length; 427 int numTypeParameters = _unlinkedClass.typeParameters.length;
400 if (numTypeParameters != 0) { 428 if (numTypeParameters != 0) {
401 return new InterfaceTypeImpl(this); 429 List<DartType> typeArguments = new List<DartType>(numTypeParameters);
430 for (int i = 0; i < numTypeParameters; i++) {
431 typeArguments[i] = getTypeArgument(i);
432 }
433 return new InterfaceTypeImpl.elementWithNameAndArgs(
434 this, name, typeArguments);
402 } else { 435 } else {
403 if (_type == null) { 436 return _type ??= new InterfaceTypeImpl(this);
404 List<DartType> typeArguments = new List<DartType>(numTypeParameters);
405 for (int i = 0; i < numTypeParameters; i++) {
406 typeArguments[i] = getTypeArgument(i);
407 }
408 _type = new InterfaceTypeImpl.elementWithNameAndArgs(
409 this, name, typeArguments);
410 }
411 return _type;
412 } 437 }
413 } 438 }
414 439
415 @override 440 @override
416 void link(CompilationUnitElementInBuildUnit compilationUnit) { 441 void link(CompilationUnitElementInBuildUnit compilationUnit) {
417 for (ConstructorElementForLink constructorElement in constructors) { 442 for (ConstructorElementForLink constructorElement in constructors) {
418 constructorElement.link(compilationUnit); 443 constructorElement.link(compilationUnit);
419 } 444 }
420 for (MethodElementForLink methodElement in methods) { 445 for (MethodElementForLink methodElement in methods) {
421 methodElement.link(compilationUnit); 446 methodElement.link(compilationUnit);
422 } 447 }
423 for (PropertyAccessorElementForLink propertyAccessorElement in accessors) { 448 for (PropertyAccessorElementForLink propertyAccessorElement in accessors) {
424 propertyAccessorElement.link(compilationUnit); 449 propertyAccessorElement.link(compilationUnit);
425 } 450 }
426 for (FieldElementForLink_ClassField fieldElement in fields) { 451 for (FieldElementForLink_ClassField fieldElement in fields) {
427 fieldElement.link(compilationUnit); 452 fieldElement.link(compilationUnit);
428 } 453 }
429 } 454 }
430 455
431 @override 456 @override
432 void unlink() { 457 void unlink() {
433 hasBeenInferred = false; 458 hasBeenInferred = false;
459 for (ConstructorElementForLink constructorElement in constructors) {
460 constructorElement.unlink();
461 }
434 for (MethodElementForLink methodElement in methods) { 462 for (MethodElementForLink methodElement in methods) {
435 methodElement.unlink(); 463 methodElement.unlink();
436 } 464 }
437 for (PropertyAccessorElementForLink propertyAccessorElement in accessors) { 465 for (PropertyAccessorElementForLink propertyAccessorElement in accessors) {
438 propertyAccessorElement.unlink(); 466 propertyAccessorElement.unlink();
439 } 467 }
440 for (FieldElementForLink_ClassField fieldElement in fields) { 468 for (FieldElementForLink_ClassField fieldElement in fields) {
441 fieldElement.unlink(); 469 fieldElement.unlink();
442 } 470 }
443 } 471 }
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
725 : super(unlinkedUnit, unitNum); 753 : super(unlinkedUnit, unitNum);
726 754
727 @override 755 @override
728 bool get isInBuildUnit => true; 756 bool get isInBuildUnit => true;
729 757
730 @override 758 @override
731 LibraryElementInBuildUnit get library => enclosingElement; 759 LibraryElementInBuildUnit get library => enclosingElement;
732 760
733 /** 761 /**
734 * If this compilation unit already has a reference in its references table 762 * If this compilation unit already has a reference in its references table
735 * matching [dependency], [name], [numTypeParameters], and [unitNum], return 763 * matching [dependency], [name], [numTypeParameters], [unitNum],
736 * its index. Otherwise add a new reference to table and return its index. 764 * [containingReference], and [kind], return its index. Otherwise add a new r eference to
765 * the table and return its index.
737 */ 766 */
738 int addReference( 767 int addRawReference(String name,
739 int dependency, String name, int numTypeParameters, int unitNum) { 768 {int dependency: 0,
769 int numTypeParameters: 0,
770 int unitNum: 0,
771 int containingReference: 0,
772 ReferenceKind kind: ReferenceKind.classOrEnum}) {
740 List<LinkedReferenceBuilder> linkedReferences = _linkedUnit.references; 773 List<LinkedReferenceBuilder> linkedReferences = _linkedUnit.references;
741 List<UnlinkedReference> unlinkedReferences = _unlinkedUnit.references; 774 List<UnlinkedReference> unlinkedReferences = _unlinkedUnit.references;
742 for (int i = 0; i < linkedReferences.length; i++) { 775 for (int i = 0; i < linkedReferences.length; i++) {
743 LinkedReferenceBuilder linkedReference = linkedReferences[i]; 776 LinkedReferenceBuilder linkedReference = linkedReferences[i];
744 if (linkedReference.dependency == dependency && 777 if (linkedReference.dependency == dependency &&
745 (i < unlinkedReferences.length 778 (i < unlinkedReferences.length
746 ? unlinkedReferences[i].name 779 ? unlinkedReferences[i].name
747 : linkedReference.name) == 780 : linkedReference.name) ==
748 name && 781 name &&
749 linkedReference.numTypeParameters == numTypeParameters && 782 linkedReference.numTypeParameters == numTypeParameters &&
750 linkedReference.unit == unitNum) { 783 linkedReference.unit == unitNum &&
784 (i < unlinkedReferences.length
785 ? unlinkedReferences[i].prefixReference
786 : linkedReference.containingReference) ==
787 containingReference &&
788 linkedReference.kind == kind) {
751 return i; 789 return i;
752 } 790 }
753 } 791 }
754 int result = linkedReferences.length; 792 int result = linkedReferences.length;
755 linkedReferences.add(new LinkedReferenceBuilder( 793 linkedReferences.add(new LinkedReferenceBuilder(
756 dependency: dependency, 794 dependency: dependency,
757 name: name, 795 name: name,
758 numTypeParameters: numTypeParameters, 796 numTypeParameters: numTypeParameters,
759 unit: unitNum)); 797 unit: unitNum,
798 containingReference: containingReference,
799 kind: kind));
760 return result; 800 return result;
761 } 801 }
762 802
763 /** 803 /**
804 * If this compilation unit already has a reference in its references table
805 * to [element], return its index. Otherwise add a new reference to the table
806 * and return its index.
807 */
808 int addReference(Element element) {
809 if (element is ClassElementForLink) {
810 return addRawReference(element.name,
811 dependency: library.addDependency(element.library),
812 numTypeParameters: element.typeParameters.length,
813 unitNum: element.enclosingElement.unitNum);
814 } else if (element is ExecutableElementForLink) {
815 // TODO(paulberry): will this code ever be executed for an executable
816 // element that's not inside a class?
817 assert(element.enclosingElement is ClassElementForLink_Class);
818 ReferenceKind kind;
819 switch (element._unlinkedExecutable.kind) {
820 case UnlinkedExecutableKind.functionOrMethod:
821 kind = ReferenceKind.method;
822 break;
823 case UnlinkedExecutableKind.setter:
824 kind = ReferenceKind.propertyAccessor;
825 break;
826 default:
827 // TODO(paulberry): implement other cases as necessary
828 throw new UnimplementedError('${element._unlinkedExecutable.kind}');
829 }
830 return addRawReference(element.name,
831 numTypeParameters: element.typeParameters.length,
832 containingReference: addReference(element.enclosingElement),
833 kind: kind);
834 }
835 // TODO(paulberry): implement other cases
836 throw new UnimplementedError('${element.runtimeType}');
837 }
838
839 /**
764 * Perform type inference and const cycle detection on this 840 * Perform type inference and const cycle detection on this
765 * compilation unit. 841 * compilation unit.
766 */ 842 */
767 void link() { 843 void link() {
768 if (library._linker.strongMode) { 844 if (library._linker.strongMode) {
769 new InstanceMemberInferrer(enclosingElement._linker.typeProvider, 845 new InstanceMemberInferrer(enclosingElement._linker.typeProvider,
770 enclosingElement.inheritanceManager) 846 enclosingElement.inheritanceManager)
771 .inferCompilationUnit(this); 847 .inferCompilationUnit(this);
772 } 848 }
773 for (ClassElementForLink classElement in types) { 849 for (ClassElementForLink classElement in types) {
(...skipping 18 matching lines...) Expand all
792 * that is part of a cycle. 868 * that is part of a cycle.
793 */ 869 */
794 void _storeConstCycle(int slot) { 870 void _storeConstCycle(int slot) {
795 _linkedUnit.constCycles.add(slot); 871 _linkedUnit.constCycles.add(slot);
796 } 872 }
797 873
798 /** 874 /**
799 * Store the given [linkedType] in the given [slot] of the this compilation 875 * Store the given [linkedType] in the given [slot] of the this compilation
800 * unit's linked type list. 876 * unit's linked type list.
801 */ 877 */
802 void _storeLinkedType(int slot, DartType linkedType) { 878 void _storeLinkedType(int slot, DartType linkedType,
879 TypeParameterContext typeParameterContext) {
803 if (slot != 0) { 880 if (slot != 0) {
804 if (linkedType != null && !linkedType.isDynamic) { 881 if (linkedType != null && !linkedType.isDynamic) {
805 _linkedUnit.types.add(_createLinkedType(linkedType, this, slot: slot)); 882 _linkedUnit.types.add(_createLinkedType(
883 linkedType, this, typeParameterContext,
884 slot: slot));
806 } 885 }
807 } 886 }
808 } 887 }
809 } 888 }
810 889
811 /** 890 /**
812 * Element representing a compilation unit which is depended upon 891 * Element representing a compilation unit which is depended upon
813 * (either directly or indirectly) by the build unit being linked. 892 * (either directly or indirectly) by the build unit being linked.
814 * 893 *
815 * TODO(paulberry): ensure that inferred types in dependencies are properly 894 * TODO(paulberry): ensure that inferred types in dependencies are properly
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 parameterElement._unlinkedParam.defaultValue, 1122 parameterElement._unlinkedParam.defaultValue,
1044 parameterElement.compilationUnit); 1123 parameterElement.compilationUnit);
1045 return dependencies; 1124 return dependencies;
1046 } 1125 }
1047 } 1126 }
1048 1127
1049 /** 1128 /**
1050 * Element representing a constructor resynthesized from a summary 1129 * Element representing a constructor resynthesized from a summary
1051 * during linking. 1130 * during linking.
1052 */ 1131 */
1053 class ConstructorElementForLink 1132 class ConstructorElementForLink extends ExecutableElementForLink
1054 implements ConstructorElementImpl, ReferenceableElementForLink { 1133 implements ConstructorElementImpl, ReferenceableElementForLink {
1055 /** 1134 /**
1056 * The unlinked representation of the constructor in the summary.
1057 */
1058 final UnlinkedExecutable _unlinkedExecutable;
1059
1060 /**
1061 * If this is a `const` constructor and the enclosing library is 1135 * If this is a `const` constructor and the enclosing library is
1062 * part of the build unit being linked, the constructor's node in 1136 * part of the build unit being linked, the constructor's node in
1063 * the constant evaluation dependency graph. Otherwise `null`. 1137 * the constant evaluation dependency graph. Otherwise `null`.
1064 */ 1138 */
1065 ConstConstructorNode _constNode; 1139 ConstConstructorNode _constNode;
1066 1140
1067 @override 1141 ConstructorElementForLink(ClassElementForLink_Class enclosingElement,
1068 final ClassElementForLink_Class enclosingElement; 1142 UnlinkedExecutable unlinkedExecutable)
1069 1143 : super(enclosingElement, unlinkedExecutable) {
1070 List<ParameterElementForLink> _parameters;
1071
1072 ConstructorElementForLink(this.enclosingElement, this._unlinkedExecutable) {
1073 if (enclosingElement.enclosingElement.isInBuildUnit && 1144 if (enclosingElement.enclosingElement.isInBuildUnit &&
1074 _unlinkedExecutable.constCycleSlot != 0) { 1145 _unlinkedExecutable.constCycleSlot != 0) {
1075 _constNode = new ConstConstructorNode(this); 1146 _constNode = new ConstConstructorNode(this);
1076 } 1147 }
1077 } 1148 }
1078 1149
1079 @override 1150 @override
1080 ConstructorElementForLink get asConstructor => this; 1151 ConstructorElementForLink get asConstructor => this;
1081 1152
1082 @override 1153 @override
1083 ConstVariableNode get asConstVariable => null; 1154 ConstVariableNode get asConstVariable => null;
1084 1155
1085 @override 1156 @override
1086 bool get isCycleFree { 1157 bool get isCycleFree {
1087 if (!_constNode.isEvaluated) { 1158 if (!_constNode.isEvaluated) {
1088 new ConstDependencyWalker().walk(_constNode); 1159 new ConstDependencyWalker().walk(_constNode);
1089 } 1160 }
1090 return _constNode.isCycleFree; 1161 return _constNode.isCycleFree;
1091 } 1162 }
1092 1163
1093 @override 1164 @override
1094 String get name => _unlinkedExecutable.name;
1095
1096 @override
1097 List<ParameterElementForLink> get parameters {
1098 if (_parameters == null) {
1099 _parameters = <ParameterElementForLink>[];
1100 for (UnlinkedParam unlinkedParam in _unlinkedExecutable.parameters) {
1101 _parameters.add(new ParameterElementForLink(unlinkedParam,
1102 enclosingElement, enclosingElement.enclosingElement));
1103 }
1104 }
1105 return _parameters;
1106 }
1107
1108 @override
1109 DartType buildType(DartType getTypeArgument(int i), 1165 DartType buildType(DartType getTypeArgument(int i),
1110 List<int> implicitFunctionTypeIndices) => 1166 List<int> implicitFunctionTypeIndices) =>
1111 DynamicTypeImpl.instance; 1167 DynamicTypeImpl.instance;
1112 1168
1113 @override 1169 @override
1114 ReferenceableElementForLink getContainedName(String name) => 1170 ReferenceableElementForLink getContainedName(String name) =>
1115 UndefinedElementForLink.instance; 1171 UndefinedElementForLink.instance;
1116 1172
1117 /** 1173 /**
1118 * Perform const cycle detection on this constructor. 1174 * Perform const cycle detection on this constructor.
1119 */ 1175 */
1120 void link(CompilationUnitElementInBuildUnit compilationUnit) { 1176 void link(CompilationUnitElementInBuildUnit compilationUnit) {
1121 if (_constNode != null && !isCycleFree) { 1177 if (_constNode != null && !isCycleFree) {
1122 compilationUnit._storeConstCycle(_unlinkedExecutable.constCycleSlot); 1178 compilationUnit._storeConstCycle(_unlinkedExecutable.constCycleSlot);
1123 } 1179 }
1180 // TODO(paulberry): call super.
1124 } 1181 }
1125 1182
1126 @override 1183 @override
1127 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 1184 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
1128 } 1185 }
1129 1186
1130 /** 1187 /**
1131 * Instance of [ConstNode] representing a constant field or constant 1188 * Instance of [ConstNode] representing a constant field or constant
1132 * top level variable. 1189 * top level variable.
1133 */ 1190 */
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 1374
1318 @override 1375 @override
1319 LibraryElementForLink get library => enclosingElement.library; 1376 LibraryElementForLink get library => enclosingElement.library;
1320 1377
1321 @override 1378 @override
1322 String get name => _unlinkedExecutable.name; 1379 String get name => _unlinkedExecutable.name;
1323 1380
1324 @override 1381 @override
1325 List<ParameterElementForLink> get parameters { 1382 List<ParameterElementForLink> get parameters {
1326 if (_parameters == null) { 1383 if (_parameters == null) {
1327 _parameters = <ParameterElementForLink>[]; 1384 int numParameters = _unlinkedExecutable.parameters.length;
1328 for (UnlinkedParam unlinkedParam in _unlinkedExecutable.parameters) { 1385 _parameters = new List<ParameterElementForLink>(numParameters);
1329 _parameters.add(new ParameterElementForLink( 1386 for (int i = 0; i < numParameters; i++) {
1330 unlinkedParam, this, enclosingElement.enclosingElement)); 1387 UnlinkedParam unlinkedParam = _unlinkedExecutable.parameters[i];
1388 _parameters[i] = new ParameterElementForLink(
1389 this, unlinkedParam, this, enclosingElement.enclosingElement, i);
1331 } 1390 }
1332 } 1391 }
1333 return _parameters; 1392 return _parameters;
1334 } 1393 }
1335 1394
1336 @override 1395 @override
1337 DartType get returnType { 1396 DartType get returnType {
1338 if (_inferredReturnType != null) { 1397 if (_inferredReturnType != null) {
1339 return _inferredReturnType; 1398 return _inferredReturnType;
1340 } else if (_declaredReturnType == null) { 1399 } else if (_declaredReturnType == null) {
1341 if (_unlinkedExecutable.returnType == null) { 1400 if (_unlinkedExecutable.returnType == null) {
1342 // In strong mode, setters without an explicit return type are 1401 if (_unlinkedExecutable.kind == UnlinkedExecutableKind.constructor) {
1343 // considered to return `void`. 1402 // TODO(paulberry): implement.
1344 if (_unlinkedExecutable.kind == UnlinkedExecutableKind.setter && 1403 throw new UnimplementedError();
1404 } else if (_unlinkedExecutable.kind == UnlinkedExecutableKind.setter &&
1345 library._linker.strongMode) { 1405 library._linker.strongMode) {
1406 // In strong mode, setters without an explicit return type are
1407 // considered to return `void`.
1346 _declaredReturnType = VoidTypeImpl.instance; 1408 _declaredReturnType = VoidTypeImpl.instance;
1347 } else { 1409 } else {
1348 _declaredReturnType = DynamicTypeImpl.instance; 1410 _declaredReturnType = DynamicTypeImpl.instance;
1349 } 1411 }
1350 } else { 1412 } else {
1351 _declaredReturnType = enclosingElement.enclosingElement 1413 _declaredReturnType = enclosingElement.enclosingElement
1352 ._resolveTypeRef(_unlinkedExecutable.returnType, this); 1414 ._resolveTypeRef(_unlinkedExecutable.returnType, this);
1353 } 1415 }
1354 } 1416 }
1355 return _declaredReturnType; 1417 return _declaredReturnType;
1356 } 1418 }
1357 1419
1358 @override 1420 @override
1359 void set returnType(DartType inferredType) { 1421 void set returnType(DartType inferredType) {
1360 assert(_inferredReturnType == null); 1422 assert(_inferredReturnType == null);
1361 _inferredReturnType = inferredType; 1423 _inferredReturnType = inferredType;
1362 } 1424 }
1363 1425
1364 @override 1426 @override
1365 FunctionTypeImpl get type => _type ??= new FunctionTypeImpl(this); 1427 FunctionTypeImpl get type => _type ??= new FunctionTypeImpl(this);
1366 1428
1367 @override 1429 @override
1368 List<TypeParameterElementForLink> get typeParameters { 1430 List<UnlinkedTypeParam> get _unlinkedTypeParams =>
1369 if (_typeParameters == null) { 1431 _unlinkedExecutable.typeParameters;
1370 _typeParameters = _unlinkedExecutable.typeParameters
1371 .map((UnlinkedTypeParam p) => new TypeParameterElementForLink(p))
1372 .toList();
1373 }
1374 return _typeParameters;
1375 }
1376 1432
1377 @override 1433 @override
1378 bool isAccessibleIn(LibraryElement library) => 1434 bool isAccessibleIn(LibraryElement library) =>
1379 !Identifier.isPrivateName(name) || identical(this.library, library); 1435 !Identifier.isPrivateName(name) || identical(this.library, library);
1380 1436
1381 /** 1437 /**
1382 * Store the results of type inference for this method in [compilationUnit]. 1438 * Store the results of type inference for this method in [compilationUnit].
1383 */ 1439 */
1384 void link(CompilationUnitElementInBuildUnit compilationUnit) { 1440 void link(CompilationUnitElementInBuildUnit compilationUnit) {
1385 compilationUnit._storeLinkedType( 1441 compilationUnit._storeLinkedType(
1386 _unlinkedExecutable.inferredReturnTypeSlot, returnType); 1442 _unlinkedExecutable.inferredReturnTypeSlot, returnType, this);
1387 for (ParameterElementForLink parameterElement in parameters) { 1443 for (ParameterElementForLink parameterElement in parameters) {
1388 parameterElement.link(compilationUnit); 1444 parameterElement.link(compilationUnit);
1389 } 1445 }
1390 } 1446 }
1391 1447
1392 /** 1448 /**
1393 * Throw away any information produced by type inference. 1449 * Throw away any information produced by type inference.
1394 */ 1450 */
1395 void unlink() { 1451 void unlink() {
1396 for (ParameterElementForLink parameterElement in parameters) { 1452 for (ParameterElementForLink parameterElement in parameters) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
1446 assert(_inferredType == null); 1502 assert(_inferredType == null);
1447 _inferredType = inferredType; 1503 _inferredType = inferredType;
1448 } 1504 }
1449 1505
1450 /** 1506 /**
1451 * Store the results of type inference for this field in 1507 * Store the results of type inference for this field in
1452 * [compilationUnit]. 1508 * [compilationUnit].
1453 */ 1509 */
1454 void link(CompilationUnitElementInBuildUnit compilationUnit) { 1510 void link(CompilationUnitElementInBuildUnit compilationUnit) {
1455 compilationUnit._storeLinkedType( 1511 compilationUnit._storeLinkedType(
1456 unlinkedVariable.inferredTypeSlot, _inferredType); 1512 unlinkedVariable.inferredTypeSlot, _inferredType, enclosingElement);
1457 } 1513 }
1458 1514
1459 /** 1515 /**
1460 * Throw away any information produced by type inference. 1516 * Throw away any information produced by type inference.
1461 */ 1517 */
1462 void unlink() { 1518 void unlink() {
1463 _inferredType = null; 1519 _inferredType = null;
1464 } 1520 }
1465 } 1521 }
1466 1522
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1504 DynamicTypeImpl.instance; 1560 DynamicTypeImpl.instance;
1505 1561
1506 @override 1562 @override
1507 ReferenceableElementForLink getContainedName(String name) => 1563 ReferenceableElementForLink getContainedName(String name) =>
1508 UndefinedElementForLink.instance; 1564 UndefinedElementForLink.instance;
1509 1565
1510 @override 1566 @override
1511 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 1567 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
1512 } 1568 }
1513 1569
1570 /**
1571 * Element representing a function-typed parameter resynthesied from a summary
1572 * during linking.
1573 */
1574 class FunctionElementForLink_FunctionTypedParam implements FunctionElement {
1575 @override
1576 final ParameterElementForLink enclosingElement;
1577
1578 /**
1579 * The executable element containing this function-typed parameter.
1580 */
1581 final Element enclosingExecutable;
1582
1583 /**
1584 * The appropriate integer list to store in
1585 * [EntityRef.implicitFunctionTypeIndices] to refer to this function-typed
1586 * parameter.
1587 */
1588 final List<int> implicitFunctionTypeIndices;
1589
1590 DartType _returnType;
1591
1592 FunctionElementForLink_FunctionTypedParam(this.enclosingElement,
1593 this.enclosingExecutable, this.implicitFunctionTypeIndices);
1594
1595 @override
1596 DartType get returnType {
1597 if (_returnType == null) {
1598 if (enclosingElement._unlinkedParam.type == null) {
1599 _returnType = DynamicTypeImpl.instance;
1600 } else {
1601 _returnType = enclosingElement.compilationUnit._resolveTypeRef(
1602 enclosingElement._unlinkedParam.type,
1603 enclosingElement._typeParameterContext);
1604 }
1605 }
1606 return _returnType;
1607 }
1608
1609 @override
1610 List<TypeParameterElement> get typeParameters => const [];
1611
1612 @override
1613 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
1614 }
1615
1514 class FunctionElementForLink_Initializer implements FunctionElementImpl { 1616 class FunctionElementForLink_Initializer implements FunctionElementImpl {
1515 @override 1617 @override
1516 DartType get returnType { 1618 DartType get returnType {
1517 // TODO(paulberry): for type inference, compute and return the type of the 1619 // TODO(paulberry): for type inference, compute and return the type of the
1518 // initializer expression. 1620 // initializer expression.
1519 return DynamicTypeImpl.instance; 1621 return DynamicTypeImpl.instance;
1520 } 1622 }
1521 1623
1522 @override 1624 @override
1523 void set returnType(DartType newType) { 1625 void set returnType(DartType newType) {
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1815 * is part of the build unit being linked, the parameter's node in 1917 * is part of the build unit being linked, the parameter's node in
1816 * the constant evaluation dependency graph. Otherwise `null`. 1918 * the constant evaluation dependency graph. Otherwise `null`.
1817 */ 1919 */
1818 ConstNode _constNode; 1920 ConstNode _constNode;
1819 1921
1820 /** 1922 /**
1821 * The compilation unit in which this parameter appears. 1923 * The compilation unit in which this parameter appears.
1822 */ 1924 */
1823 final CompilationUnitElementForLink compilationUnit; 1925 final CompilationUnitElementForLink compilationUnit;
1824 1926
1927 /**
1928 * The index of this parameter within [enclosingElement]'s parameter list.
1929 */
1930 final int _parameterIndex;
1931
1932 @override
1933 final ExecutableElementForLink enclosingElement;
1934
1825 DartType _inferredType; 1935 DartType _inferredType;
1826 DartType _declaredType; 1936 DartType _declaredType;
1827 1937
1828 ParameterElementForLink( 1938 ParameterElementForLink(this.enclosingElement, this._unlinkedParam,
1829 this._unlinkedParam, this._typeParameterContext, this.compilationUnit) { 1939 this._typeParameterContext, this.compilationUnit, this._parameterIndex) {
1830 if (_unlinkedParam.defaultValue != null) { 1940 if (_unlinkedParam.defaultValue != null) {
1831 _constNode = new ConstParameterNode(this); 1941 _constNode = new ConstParameterNode(this);
1832 } 1942 }
1833 } 1943 }
1834 1944
1835 @override 1945 @override
1836 bool get hasImplicitType => 1946 bool get hasImplicitType =>
1837 !_unlinkedParam.isFunctionTyped && _unlinkedParam.type == null; 1947 !_unlinkedParam.isFunctionTyped && _unlinkedParam.type == null;
1838 1948
1839 @override 1949 @override
1950 String get name => _unlinkedParam.name;
1951
1952 @override
1840 ParameterKind get parameterKind { 1953 ParameterKind get parameterKind {
1841 switch (_unlinkedParam.kind) { 1954 switch (_unlinkedParam.kind) {
1842 case UnlinkedParamKind.required: 1955 case UnlinkedParamKind.required:
1843 return ParameterKind.REQUIRED; 1956 return ParameterKind.REQUIRED;
1844 case UnlinkedParamKind.positional: 1957 case UnlinkedParamKind.positional:
1845 return ParameterKind.POSITIONAL; 1958 return ParameterKind.POSITIONAL;
1846 case UnlinkedParamKind.named: 1959 case UnlinkedParamKind.named:
1847 return ParameterKind.NAMED; 1960 return ParameterKind.NAMED;
1848 } 1961 }
1849 } 1962 }
1850 1963
1851 @override 1964 @override
1852 DartType get type { 1965 DartType get type {
1853 if (_inferredType != null) { 1966 if (_inferredType != null) {
1854 return _inferredType; 1967 return _inferredType;
1855 } else if (_declaredType == null) { 1968 } else if (_declaredType == null) {
1856 if (_unlinkedParam.isFunctionTyped) { 1969 if (_unlinkedParam.isFunctionTyped) {
1857 // TODO(paulberry): implement. 1970 // TODO(paulberry): implement.
1858 throw new UnimplementedError(); 1971 _declaredType = new FunctionTypeImpl(
1972 new FunctionElementForLink_FunctionTypedParam(
1973 this, enclosingElement, <int>[_parameterIndex]));
1859 } else if (_unlinkedParam.type == null) { 1974 } else if (_unlinkedParam.type == null) {
1860 _declaredType = DynamicTypeImpl.instance; 1975 _declaredType = DynamicTypeImpl.instance;
1861 } else { 1976 } else {
1862 _declaredType = compilationUnit._resolveTypeRef( 1977 _declaredType = compilationUnit._resolveTypeRef(
1863 _unlinkedParam.type, _typeParameterContext); 1978 _unlinkedParam.type, _typeParameterContext);
1864 } 1979 }
1865 } 1980 }
1866 return _declaredType; 1981 return _declaredType;
1867 } 1982 }
1868 1983
1869 @override 1984 @override
1870 void set type(DartType inferredType) { 1985 void set type(DartType inferredType) {
1871 assert(_inferredType == null); 1986 assert(_inferredType == null);
1872 _inferredType = inferredType; 1987 _inferredType = inferredType;
1873 } 1988 }
1874 1989
1875 /** 1990 /**
1876 * Store the results of type inference for this parameter in 1991 * Store the results of type inference for this parameter in
1877 * [compilationUnit]. 1992 * [compilationUnit].
1878 */ 1993 */
1879 void link(CompilationUnitElementInBuildUnit compilationUnit) { 1994 void link(CompilationUnitElementInBuildUnit compilationUnit) {
1880 compilationUnit._storeLinkedType( 1995 compilationUnit._storeLinkedType(
1881 _unlinkedParam.inferredTypeSlot, _inferredType); 1996 _unlinkedParam.inferredTypeSlot, _inferredType, _typeParameterContext);
1882 } 1997 }
1883 1998
1884 @override 1999 @override
1885 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 2000 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
1886 2001
1887 /** 2002 /**
1888 * Throw away any information produced by type inference. 2003 * Throw away any information produced by type inference.
1889 */ 2004 */
1890 void unlink() { 2005 void unlink() {
1891 _inferredType = null; 2006 _inferredType = null;
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
1995 @override 2110 @override
1996 bool get isStatic => true; 2111 bool get isStatic => true;
1997 } 2112 }
1998 2113
1999 /** 2114 /**
2000 * Interface representing elements which can serve as the context within which 2115 * Interface representing elements which can serve as the context within which
2001 * type parameter indices are interpreted. 2116 * type parameter indices are interpreted.
2002 */ 2117 */
2003 abstract class TypeParameterContext { 2118 abstract class TypeParameterContext {
2004 /** 2119 /**
2120 * Find out how many type parameters are in scope in this context.
2121 */
2122 int get typeParameterNestingLevel;
2123
2124 /**
2005 * Convert the given [index] into a type parameter type. 2125 * Convert the given [index] into a type parameter type.
2006 */ 2126 */
2007 TypeParameterType getTypeParameterType(int index); 2127 TypeParameterType getTypeParameterType(int index);
2008 } 2128 }
2009 2129
2010 /** 2130 /**
2011 * Element representing a type parameter resynthesized from a summary during 2131 * Element representing a type parameter resynthesized from a summary during
2012 * linking. 2132 * linking.
2013 */ 2133 */
2014 class TypeParameterElementForLink implements TypeParameterElement { 2134 class TypeParameterElementForLink implements TypeParameterElement {
2015 /** 2135 /**
2016 * The unlinked representation of the type parameter in the summary. 2136 * The unlinked representation of the type parameter in the summary.
2017 */ 2137 */
2018 final UnlinkedTypeParam _unlinkedTypeParam; 2138 final UnlinkedTypeParam _unlinkedTypeParam;
2019 2139
2020 TypeParameterElementForLink(this._unlinkedTypeParam); 2140 /**
2141 * The number of type parameters whose scope overlaps this one, and which are
2142 * declared earlier in the file.
2143 */
2144 final int nestingLevel;
2145
2146 TypeParameterTypeImpl _type;
2147
2148 TypeParameterElementForLink(this._unlinkedTypeParam, this.nestingLevel);
2021 2149
2022 @override 2150 @override
2023 String get name => _unlinkedTypeParam.name; 2151 String get name => _unlinkedTypeParam.name;
2024 2152
2025 @override 2153 @override
2154 TypeParameterTypeImpl get type => _type ??= new TypeParameterTypeImpl(this);
2155
2156 @override
2026 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 2157 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
2027 } 2158 }
2028 2159
2029 /** 2160 /**
2030 * Mixin representing an element which can have type parameters. 2161 * Mixin representing an element which can have type parameters.
2031 */ 2162 */
2032 abstract class TypeParameterizedElementForLink 2163 abstract class TypeParameterizedElementForLink
2033 implements TypeParameterizedElement, TypeParameterContext { 2164 implements TypeParameterizedElement, TypeParameterContext {
2034 List<TypeParameterType> _typeParameterTypes; 2165 List<TypeParameterType> _typeParameterTypes;
2035 List<TypeParameterElementForLink> _typeParameters; 2166 List<TypeParameterElementForLink> _typeParameters;
2167 int _nestingLevel;
2036 2168
2037 /** 2169 /**
2038 * Get the type parameter context enclosing this one, if any. 2170 * Get the type parameter context enclosing this one, if any.
2039 */ 2171 */
2040 TypeParameterContext get enclosingTypeParameterContext; 2172 TypeParameterContext get enclosingTypeParameterContext;
2041 2173
2174 @override
2175 int get typeParameterNestingLevel =>
2176 _nestingLevel ??= _unlinkedTypeParams.length +
2177 (enclosingTypeParameterContext?.typeParameterNestingLevel ?? 0);
2178
2042 List<TypeParameterElementForLink> get typeParameters { 2179 List<TypeParameterElementForLink> get typeParameters {
2043 if (_typeParameters == null) { 2180 if (_typeParameters == null) {
2044 _typeParameters = _unlinkedTypeParams 2181 int enclosingNestingLevel =
2045 .map((UnlinkedTypeParam p) => new TypeParameterElementForLink(p)) 2182 enclosingTypeParameterContext?.typeParameterNestingLevel ?? 0;
2046 .toList(); 2183 int numTypeParameters = _unlinkedTypeParams.length;
2184 _typeParameters =
2185 new List<TypeParameterElementForLink>(numTypeParameters);
2186 for (int i = 0; i < numTypeParameters; i++) {
2187 _typeParameters[i] = new TypeParameterElementForLink(
2188 _unlinkedTypeParams[i], enclosingNestingLevel + i);
2189 }
2047 } 2190 }
2048 return _typeParameters; 2191 return _typeParameters;
2049 } 2192 }
2050 2193
2051 /** 2194 /**
2052 * Get a list of [TypeParameterType] objects corresponding to the 2195 * Get a list of [TypeParameterType] objects corresponding to the
2053 * element's type parameters. 2196 * element's type parameters.
2054 */ 2197 */
2055 List<TypeParameterType> get typeParameterTypes { 2198 List<TypeParameterType> get typeParameterTypes {
2056 if (_typeParameterTypes == null) { 2199 if (_typeParameterTypes == null) {
2057 _typeParameterTypes = typeParameters 2200 _typeParameterTypes = typeParameters
2058 .map((TypeParameterElementForLink e) => new TypeParameterTypeImpl(e)) 2201 .map((TypeParameterElementForLink e) => e.type)
2059 .toList(); 2202 .toList();
2060 } 2203 }
2061 return _typeParameterTypes; 2204 return _typeParameterTypes;
2062 } 2205 }
2063 2206
2064 /** 2207 /**
2065 * Get the [UnlinkedTypeParam]s representing the type parameters declared by 2208 * Get the [UnlinkedTypeParam]s representing the type parameters declared by
2066 * this element. 2209 * this element.
2067 */ 2210 */
2068 List<UnlinkedTypeParam> get _unlinkedTypeParams; 2211 List<UnlinkedTypeParam> get _unlinkedTypeParams;
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after
2420 2563
2421 /** 2564 /**
2422 * Throw away any information produced by a previous call to [link]. 2565 * Throw away any information produced by a previous call to [link].
2423 */ 2566 */
2424 void unlink() { 2567 void unlink() {
2425 for (LibraryElementInBuildUnit library in _librariesInBuildUnit) { 2568 for (LibraryElementInBuildUnit library in _librariesInBuildUnit) {
2426 library.unlink(); 2569 library.unlink();
2427 } 2570 }
2428 } 2571 }
2429 } 2572 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/summarize_ast_strong_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698