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

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

Issue 2813543007: Pass ElementImpl as a context for resynthesizing types. (Closed)
Patch Set: Created 3 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
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 665 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 } 676 }
677 677
678 @override 678 @override
679 String toString() => '$enclosingElement.$name'; 679 String toString() => '$enclosingElement.$name';
680 680
681 /** 681 /**
682 * Convert [typeRef] into an [InterfaceType]. 682 * Convert [typeRef] into an [InterfaceType].
683 */ 683 */
684 InterfaceType _computeInterfaceType(EntityRef typeRef) { 684 InterfaceType _computeInterfaceType(EntityRef typeRef) {
685 if (typeRef != null) { 685 if (typeRef != null) {
686 DartType type = enclosingElement.resolveTypeRef(typeRef, this); 686 DartType type = enclosingElement.resolveTypeRef(this, typeRef);
687 if (type is InterfaceType && !type.element.isEnum) { 687 if (type is InterfaceType && !type.element.isEnum) {
688 return type; 688 return type;
689 } 689 }
690 // In the event that the `typeRef` isn't an interface type (which may 690 // In the event that the `typeRef` isn't an interface type (which may
691 // happen in the event of erroneous code) just fall through and pretend 691 // happen in the event of erroneous code) just fall through and pretend
692 // the supertype is `Object`. 692 // the supertype is `Object`.
693 } 693 }
694 return enclosingElement.enclosingElement._linker.typeProvider.objectType; 694 return enclosingElement.enclosingElement._linker.typeProvider.objectType;
695 } 695 }
696 } 696 }
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 } else { 1069 } else {
1070 LibraryElementForLink dependency = 1070 LibraryElementForLink dependency =
1071 enclosingElement._getDependency(linkedReference.dependency); 1071 enclosingElement._getDependency(linkedReference.dependency);
1072 _references[index] = dependency.getContainedName(name); 1072 _references[index] = dependency.getContainedName(name);
1073 } 1073 }
1074 } 1074 }
1075 return _references[index]; 1075 return _references[index];
1076 } 1076 }
1077 1077
1078 @override 1078 @override
1079 DartType resolveTypeRef( 1079 DartType resolveTypeRef(ElementImpl context, EntityRef type,
1080 EntityRef type, TypeParameterizedElementMixin typeParameterContext,
1081 {bool defaultVoid: false, 1080 {bool defaultVoid: false,
1082 bool instantiateToBoundsAllowed: true, 1081 bool instantiateToBoundsAllowed: true,
1083 bool declaredType: false}) { 1082 bool declaredType: false}) {
1084 if (type == null) { 1083 if (type == null) {
1085 if (defaultVoid) { 1084 if (defaultVoid) {
1086 return VoidTypeImpl.instance; 1085 return VoidTypeImpl.instance;
1087 } else { 1086 } else {
1088 return DynamicTypeImpl.instance; 1087 return DynamicTypeImpl.instance;
1089 } 1088 }
1090 } 1089 }
1091 if (type.paramReference != 0) { 1090 if (type.paramReference != 0) {
1092 return typeParameterContext.getTypeParameterType(type.paramReference); 1091 return context.typeParameterContext
1092 .getTypeParameterType(type.paramReference);
1093 } else if (type.syntheticReturnType != null) { 1093 } else if (type.syntheticReturnType != null) {
1094 // TODO(paulberry): implement. 1094 // TODO(paulberry): implement.
1095 throw new UnimplementedError(); 1095 throw new UnimplementedError();
1096 } else if (type.implicitFunctionTypeIndices.isNotEmpty) { 1096 } else if (type.implicitFunctionTypeIndices.isNotEmpty) {
1097 // TODO(paulberry): implement. 1097 // TODO(paulberry): implement.
1098 throw new UnimplementedError(); 1098 throw new UnimplementedError();
1099 } else { 1099 } else {
1100 DartType getTypeArgument(int i) { 1100 DartType getTypeArgument(int i) {
1101 if (i < type.typeArguments.length) { 1101 if (i < type.typeArguments.length) {
1102 return resolveTypeRef(type.typeArguments[i], typeParameterContext); 1102 return resolveTypeRef(context, type.typeArguments[i]);
1103 } else if (!instantiateToBoundsAllowed) { 1103 } else if (!instantiateToBoundsAllowed) {
1104 // Do not allow buildType to instantiate the bounds; force dynamic. 1104 // Do not allow buildType to instantiate the bounds; force dynamic.
1105 return DynamicTypeImpl.instance; 1105 return DynamicTypeImpl.instance;
1106 } else { 1106 } else {
1107 return null; 1107 return null;
1108 } 1108 }
1109 } 1109 }
1110 1110
1111 ReferenceableElementForLink element = resolveRef(type.reference); 1111 ReferenceableElementForLink element = resolveRef(type.reference);
1112 return element.buildType( 1112 return element.buildType(
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 } 1402 }
1403 } 1403 }
1404 1404
1405 @override 1405 @override
1406 bool get isInBuildUnit => false; 1406 bool get isInBuildUnit => false;
1407 1407
1408 @override 1408 @override
1409 DartType getLinkedType( 1409 DartType getLinkedType(
1410 int slot, TypeParameterizedElementMixin typeParameterContext) { 1410 int slot, TypeParameterizedElementMixin typeParameterContext) {
1411 if (slot < _linkedTypeRefs.length) { 1411 if (slot < _linkedTypeRefs.length) {
1412 return resolveTypeRef(_linkedTypeRefs[slot], typeParameterContext); 1412 return resolveTypeRef(this, _linkedTypeRefs[slot]);
1413 } else { 1413 } else {
1414 return DynamicTypeImpl.instance; 1414 return DynamicTypeImpl.instance;
1415 } 1415 }
1416 } 1416 }
1417 } 1417 }
1418 1418
1419 /** 1419 /**
1420 * Instance of [ConstNode] representing a constant constructor. 1420 * Instance of [ConstNode] representing a constant constructor.
1421 */ 1421 */
1422 class ConstConstructorNode extends ConstNode { 1422 class ConstConstructorNode extends ConstNode {
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after
1825 1825
1826 /** 1826 /**
1827 * If the executable element had an explicitly declared return type, return 1827 * If the executable element had an explicitly declared return type, return
1828 * it. Otherwise return `null`. 1828 * it. Otherwise return `null`.
1829 */ 1829 */
1830 DartType get declaredReturnType { 1830 DartType get declaredReturnType {
1831 if (_unlinkedExecutable.returnType == null) { 1831 if (_unlinkedExecutable.returnType == null) {
1832 return null; 1832 return null;
1833 } else { 1833 } else {
1834 return _declaredReturnType ??= 1834 return _declaredReturnType ??=
1835 compilationUnit.resolveTypeRef(_unlinkedExecutable.returnType, this); 1835 compilationUnit.resolveTypeRef(this, _unlinkedExecutable.returnType);
1836 } 1836 }
1837 } 1837 }
1838 1838
1839 @override 1839 @override
1840 String get displayName { 1840 String get displayName {
1841 if (_displayName == null) { 1841 if (_displayName == null) {
1842 _displayName = _unlinkedExecutable.name; 1842 _displayName = _unlinkedExecutable.name;
1843 if (_unlinkedExecutable.kind == UnlinkedExecutableKind.setter) { 1843 if (_unlinkedExecutable.kind == UnlinkedExecutableKind.setter) {
1844 _displayName = _displayName.substring(0, _displayName.length - 1); 1844 _displayName = _displayName.substring(0, _displayName.length - 1);
1845 } 1845 }
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
2320 2320
2321 EntityRef ref = _getNextRef(); 2321 EntityRef ref = _getNextRef();
2322 ReferenceableElementForLink refElement = unit.resolveRef(ref.reference); 2322 ReferenceableElementForLink refElement = unit.resolveRef(ref.reference);
2323 ConstructorElementForLink constructorElement = refElement.asConstructor; 2323 ConstructorElementForLink constructorElement = refElement.asConstructor;
2324 2324
2325 if (constructorElement != null) { 2325 if (constructorElement != null) {
2326 stack.add(() { 2326 stack.add(() {
2327 if (ref.typeArguments.isNotEmpty) { 2327 if (ref.typeArguments.isNotEmpty) {
2328 return constructorElement.enclosingClass.buildType((int i) { 2328 return constructorElement.enclosingClass.buildType((int i) {
2329 if (i < ref.typeArguments.length) { 2329 if (i < ref.typeArguments.length) {
2330 return unit.resolveTypeRef( 2330 return unit.resolveTypeRef(function, ref.typeArguments[i]);
2331 ref.typeArguments[i], function.typeParameterContext);
2332 } else { 2331 } else {
2333 return null; 2332 return null;
2334 } 2333 }
2335 }, const <int>[]); 2334 }, const <int>[]);
2336 } else { 2335 } else {
2337 FunctionType rawType = StaticTypeAnalyzer 2336 FunctionType rawType = StaticTypeAnalyzer
2338 .constructorToGenericFunctionType(constructorElement); 2337 .constructorToGenericFunctionType(constructorElement);
2339 FunctionType inferredType = _inferExecutableType( 2338 FunctionType inferredType = _inferExecutableType(
2340 rawType, 2339 rawType,
2341 numNamed, 2340 numNamed,
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
2521 List<String> _getNextStrings(int n) { 2520 List<String> _getNextStrings(int n) {
2522 List<String> result = new List<String>(n); 2521 List<String> result = new List<String>(n);
2523 for (int i = 0; i < n; i++) { 2522 for (int i = 0; i < n; i++) {
2524 result[i] = _getNextString(); 2523 result[i] = _getNextString();
2525 } 2524 }
2526 return result; 2525 return result;
2527 } 2526 }
2528 2527
2529 DartType _getNextTypeRef() { 2528 DartType _getNextTypeRef() {
2530 EntityRef ref = _getNextRef(); 2529 EntityRef ref = _getNextRef();
2531 return unit.resolveTypeRef(ref, function.typeParameterContext); 2530 return unit.resolveTypeRef(function, ref);
2532 } 2531 }
2533 2532
2534 List<DartType> _getTypeArguments() { 2533 List<DartType> _getTypeArguments() {
2535 int numTypeArguments = _getNextInt(); 2534 int numTypeArguments = _getNextInt();
2536 List<DartType> typeArguments = new List<DartType>(numTypeArguments); 2535 List<DartType> typeArguments = new List<DartType>(numTypeArguments);
2537 for (int i = 0; i < numTypeArguments; i++) { 2536 for (int i = 0; i < numTypeArguments; i++) {
2538 typeArguments[i] = _getNextTypeRef(); 2537 typeArguments[i] = _getNextTypeRef();
2539 } 2538 }
2540 return typeArguments; 2539 return typeArguments;
2541 } 2540 }
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
2878 return _implicitFunctionTypeIndices; 2877 return _implicitFunctionTypeIndices;
2879 } 2878 }
2880 2879
2881 @override 2880 @override
2882 DartType get returnType { 2881 DartType get returnType {
2883 if (_returnType == null) { 2882 if (_returnType == null) {
2884 if (enclosingElement._unlinkedParam.type == null) { 2883 if (enclosingElement._unlinkedParam.type == null) {
2885 _returnType = DynamicTypeImpl.instance; 2884 _returnType = DynamicTypeImpl.instance;
2886 } else { 2885 } else {
2887 _returnType = enclosingElement.compilationUnit.resolveTypeRef( 2886 _returnType = enclosingElement.compilationUnit.resolveTypeRef(
2888 enclosingElement._unlinkedParam.type, typeParameterContext); 2887 enclosingElement, enclosingElement._unlinkedParam.type);
2889 } 2888 }
2890 } 2889 }
2891 return _returnType; 2890 return _returnType;
2892 } 2891 }
2893 2892
2894 @override 2893 @override
2895 List<TypeParameterElement> get typeParameters => const []; 2894 List<TypeParameterElement> get typeParameters => const [];
2896 2895
2897 @override 2896 @override
2898 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 2897 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
3193 bool get isSynthetic => false; 3192 bool get isSynthetic => false;
3194 3193
3195 @override 3194 @override
3196 LibraryElementForLink get library => enclosingElement.library; 3195 LibraryElementForLink get library => enclosingElement.library;
3197 3196
3198 @override 3197 @override
3199 String get name => _unlinkedTypedef.name; 3198 String get name => _unlinkedTypedef.name;
3200 3199
3201 @override 3200 @override
3202 DartType get returnType => _returnType ??= 3201 DartType get returnType => _returnType ??=
3203 enclosingElement.resolveTypeRef(_unlinkedTypedef.returnType, this); 3202 enclosingElement.resolveTypeRef(this, _unlinkedTypedef.returnType);
3204 3203
3205 @override 3204 @override
3206 TypeParameterizedElementMixin get typeParameterContext => this; 3205 TypeParameterizedElementMixin get typeParameterContext => this;
3207 3206
3208 @override 3207 @override
3209 List<UnlinkedParam> get unlinkedParameters => _unlinkedTypedef.parameters; 3208 List<UnlinkedParam> get unlinkedParameters => _unlinkedTypedef.parameters;
3210 3209
3211 @override 3210 @override
3212 List<UnlinkedTypeParam> get unlinkedTypeParams => 3211 List<UnlinkedTypeParam> get unlinkedTypeParams =>
3213 _unlinkedTypedef.typeParameters; 3212 _unlinkedTypedef.typeParameters;
(...skipping 978 matching lines...) Expand 10 before | Expand all | Expand 10 after
4192 this, _typeParameterContext, _unlinkedParam.parameters)); 4191 this, _typeParameterContext, _unlinkedParam.parameters));
4193 } else if (_unlinkedParam.type == null) { 4192 } else if (_unlinkedParam.type == null) {
4194 if (!compilationUnit.isInBuildUnit) { 4193 if (!compilationUnit.isInBuildUnit) {
4195 _inferredType = compilationUnit.getLinkedType( 4194 _inferredType = compilationUnit.getLinkedType(
4196 _unlinkedParam.inferredTypeSlot, _typeParameterContext); 4195 _unlinkedParam.inferredTypeSlot, _typeParameterContext);
4197 return _inferredType; 4196 return _inferredType;
4198 } else { 4197 } else {
4199 _declaredType = DynamicTypeImpl.instance; 4198 _declaredType = DynamicTypeImpl.instance;
4200 } 4199 }
4201 } else { 4200 } else {
4202 _declaredType = compilationUnit.resolveTypeRef( 4201 _declaredType =
4203 _unlinkedParam.type, _typeParameterContext); 4202 compilationUnit.resolveTypeRef(this, _unlinkedParam.type);
4204 } 4203 }
4205 } 4204 }
4206 return _declaredType; 4205 return _declaredType;
4207 } 4206 }
4208 4207
4209 @override 4208 @override
4210 void set type(DartType inferredType) { 4209 void set type(DartType inferredType) {
4211 assert(_inferredType == null); 4210 assert(_inferredType == null);
4212 _inferredType = inferredType; 4211 _inferredType = inferredType;
4213 } 4212 }
4214 4213
4214 @override
4215 TypeParameterizedElementMixin get typeParameterContext {
4216 return _typeParameterContext;
4217 }
4218
4215 /** 4219 /**
4216 * Store the results of type inference for this parameter in 4220 * Store the results of type inference for this parameter in
4217 * [compilationUnit]. 4221 * [compilationUnit].
4218 */ 4222 */
4219 void link(CompilationUnitElementInBuildUnit compilationUnit) { 4223 void link(CompilationUnitElementInBuildUnit compilationUnit) {
4220 compilationUnit._storeLinkedType( 4224 compilationUnit._storeLinkedType(
4221 _unlinkedParam.inferredTypeSlot, _inferredType, _typeParameterContext); 4225 _unlinkedParam.inferredTypeSlot, _inferredType, _typeParameterContext);
4222 compilationUnit._storeLinkedTypeError( 4226 compilationUnit._storeLinkedTypeError(
4223 _unlinkedParam.inferredTypeSlot, _inferenceError); 4227 _unlinkedParam.inferredTypeSlot, _inferenceError);
4224 if (inheritsCovariant) { 4228 if (inheritsCovariant) {
(...skipping 954 matching lines...) Expand 10 before | Expand all | Expand 10 after
5179 } 5183 }
5180 5184
5181 /** 5185 /**
5182 * If the variable has an explicitly declared return type, return it. 5186 * If the variable has an explicitly declared return type, return it.
5183 * Otherwise return `null`. 5187 * Otherwise return `null`.
5184 */ 5188 */
5185 DartType get declaredType { 5189 DartType get declaredType {
5186 if (unlinkedVariable.type == null) { 5190 if (unlinkedVariable.type == null) {
5187 return null; 5191 return null;
5188 } else { 5192 } else {
5189 return _declaredType ??= compilationUnit.resolveTypeRef( 5193 return _declaredType ??=
5190 unlinkedVariable.type, _typeParameterContext); 5194 compilationUnit.resolveTypeRef(this, unlinkedVariable.type);
5191 } 5195 }
5192 } 5196 }
5193 5197
5194 @override 5198 @override
5195 String get displayName => unlinkedVariable.name; 5199 String get displayName => unlinkedVariable.name;
5196 5200
5197 @override 5201 @override
5198 PropertyAccessorElementForLink_Variable get getter => 5202 PropertyAccessorElementForLink_Variable get getter =>
5199 _getter ??= new PropertyAccessorElementForLink_Variable(this, false); 5203 _getter ??= new PropertyAccessorElementForLink_Variable(this, false);
5200 5204
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
5278 } 5282 }
5279 5283
5280 @override 5284 @override
5281 DartType get type => declaredType ?? inferredType; 5285 DartType get type => declaredType ?? inferredType;
5282 5286
5283 @override 5287 @override
5284 void set type(DartType newType) { 5288 void set type(DartType newType) {
5285 // TODO(paulberry): store inferred type. 5289 // TODO(paulberry): store inferred type.
5286 } 5290 }
5287 5291
5292 @override
5293 TypeParameterizedElementMixin get typeParameterContext {
5294 return _typeParameterContext;
5295 }
5296
5288 /** 5297 /**
5289 * The context in which type parameters should be interpreted, or `null` if 5298 * The context in which type parameters should be interpreted, or `null` if
5290 * there are no type parameters in scope. 5299 * there are no type parameters in scope.
5291 */ 5300 */
5292 TypeParameterizedElementMixin get _typeParameterContext; 5301 TypeParameterizedElementMixin get _typeParameterContext;
5293 5302
5294 @override 5303 @override
5295 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 5304 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
5296 5305
5297 @override 5306 @override
5298 String toString() => '$enclosingElement.$name'; 5307 String toString() => '$enclosingElement.$name';
5299 } 5308 }
5300 5309
5301 /** 5310 /**
5302 * This exception is thrown when [ExprTypeComputer] cannot inference the type. 5311 * This exception is thrown when [ExprTypeComputer] cannot inference the type.
5303 */ 5312 */
5304 class _InferenceFailedError { 5313 class _InferenceFailedError {
5305 final String message; 5314 final String message;
5306 5315
5307 _InferenceFailedError(this.message); 5316 _InferenceFailedError(this.message);
5308 } 5317 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/dart/element/element.dart ('k') | pkg/analyzer/lib/src/summary/resynthesize.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698