@@ -1082,14 +1082,36 @@ namespace {
1082
1082
};
1083
1083
}
1084
1084
1085
+ enum class DirectlyReferencedTypeLookupFlags {
1086
+ // / Include results that are `@inlinable` or `@usableFromInline`.
1087
+ AllowUsableFromInline = 1 << 0 ,
1088
+
1089
+ // / Discard 'Self' requirements in protocol extension `where` clauses.
1090
+ RHSOfSelfRequirement = 1 << 1 ,
1091
+
1092
+ // / Include results that are members of protocols to which the contextual
1093
+ // / type conforms.
1094
+ AllowProtocolMembers = 1 << 2 ,
1095
+ };
1096
+
1097
+ using DirectlyReferencedTypeLookupOptions =
1098
+ OptionSet<DirectlyReferencedTypeLookupFlags>;
1099
+
1100
+ DirectlyReferencedTypeLookupOptions defaultDirectlyReferencedTypeLookupOptions =
1101
+ DirectlyReferencedTypeLookupFlags::AllowProtocolMembers;
1102
+
1103
+ inline DirectlyReferencedTypeLookupOptions
1104
+ operator |(DirectlyReferencedTypeLookupFlags flag1,
1105
+ DirectlyReferencedTypeLookupFlags flag2) {
1106
+ return DirectlyReferencedTypeLookupOptions (flag1) | flag2;
1107
+ }
1108
+
1085
1109
// / Retrieve the set of type declarations that are directly referenced from
1086
1110
// / the given parsed type representation.
1087
1111
static DirectlyReferencedTypeDecls
1088
1112
directReferencesForTypeRepr (Evaluator &evaluator, ASTContext &ctx,
1089
1113
TypeRepr *typeRepr, DeclContext *dc,
1090
- bool allowUsableFromInline,
1091
- bool rhsOfSelfRequirement,
1092
- bool allowProtocolMembers);
1114
+ DirectlyReferencedTypeLookupOptions options);
1093
1115
1094
1116
// / Retrieve the set of type declarations that are directly referenced from
1095
1117
// / the given type.
@@ -1147,11 +1169,12 @@ SelfBounds SelfBoundsFromWhereClauseRequest::evaluate(
1147
1169
// Resolve the right-hand side.
1148
1170
DirectlyReferencedTypeDecls rhsDecls;
1149
1171
if (auto typeRepr = req.getConstraintRepr ()) {
1172
+ auto typeLookupOptions =
1173
+ defaultDirectlyReferencedTypeLookupOptions |
1174
+ DirectlyReferencedTypeLookupFlags::RHSOfSelfRequirement;
1150
1175
rhsDecls = directReferencesForTypeRepr (evaluator, ctx, typeRepr,
1151
1176
const_cast <DeclContext *>(dc),
1152
- /* allowUsableFromInline=*/ false ,
1153
- /* rhsOfSelfRequirement=*/ true ,
1154
- /* allowProtocolMembers=*/ true );
1177
+ typeLookupOptions);
1155
1178
}
1156
1179
1157
1180
SmallVector<ModuleDecl *, 2 > modulesFound;
@@ -1213,10 +1236,9 @@ TypeDeclsFromWhereClauseRequest::evaluate(Evaluator &evaluator,
1213
1236
1214
1237
DirectlyReferencedTypeDecls result;
1215
1238
auto resolve = [&](TypeRepr *typeRepr) {
1216
- auto decls = directReferencesForTypeRepr (evaluator, ctx, typeRepr, ext,
1217
- /* allowUsableFromInline=*/ false ,
1218
- /* rhsOfSelfRequirement=*/ false ,
1219
- /* allowProtocolMembers=*/ true );
1239
+ auto decls =
1240
+ directReferencesForTypeRepr (evaluator, ctx, typeRepr, ext,
1241
+ defaultDirectlyReferencedTypeLookupOptions);
1220
1242
result.first .insert (result.first .end (),
1221
1243
decls.first .begin (),
1222
1244
decls.first .end ());
@@ -2880,20 +2902,19 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
2880
2902
}
2881
2903
2882
2904
// / Perform unqualified name lookup for types at the given location.
2883
- static DirectlyReferencedTypeDecls
2884
- directReferencesForUnqualifiedTypeLookup (DeclNameRef name,
2885
- SourceLoc loc, DeclContext *dc,
2886
- LookupOuterResults lookupOuter,
2887
- bool allowUsableFromInline,
2888
- bool rhsOfSelfRequirement,
2889
- bool allowProtocolMembers) {
2905
+ static DirectlyReferencedTypeDecls directReferencesForUnqualifiedTypeLookup (
2906
+ DeclNameRef name, SourceLoc loc, DeclContext *dc,
2907
+ LookupOuterResults lookupOuter,
2908
+ DirectlyReferencedTypeLookupOptions typeLookupOptions) {
2890
2909
UnqualifiedLookupOptions options = UnqualifiedLookupFlags::TypeLookup;
2891
- if (allowProtocolMembers)
2892
- options |= UnqualifiedLookupFlags::AllowProtocolMembers;
2910
+ if (typeLookupOptions.contains (
2911
+ DirectlyReferencedTypeLookupFlags::AllowProtocolMembers))
2912
+ options |= UnqualifiedLookupFlags::AllowProtocolMembers;
2893
2913
if (lookupOuter == LookupOuterResults::Included)
2894
2914
options |= UnqualifiedLookupFlags::IncludeOuterResults;
2895
2915
2896
- if (allowUsableFromInline)
2916
+ if (typeLookupOptions.contains (
2917
+ DirectlyReferencedTypeLookupFlags::AllowUsableFromInline))
2897
2918
options |= UnqualifiedLookupFlags::IncludeUsableFromInline;
2898
2919
2899
2920
// Manually exclude macro expansions here since the source location
@@ -2911,7 +2932,8 @@ directReferencesForUnqualifiedTypeLookup(DeclNameRef name,
2911
2932
// To avoid cycles when resolving the right-hand side, we perform the
2912
2933
// lookup in the parent context (for a protocol), or a special mode where
2913
2934
// we disregard 'Self' requirements (for a protocol extension).
2914
- if (rhsOfSelfRequirement) {
2935
+ if (typeLookupOptions.contains (
2936
+ DirectlyReferencedTypeLookupFlags::RHSOfSelfRequirement)) {
2915
2937
if (dc->getExtendedProtocolDecl ())
2916
2938
options |= UnqualifiedLookupFlags::DisregardSelfBounds;
2917
2939
else {
@@ -2999,37 +3021,32 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
2999
3021
}
3000
3022
3001
3023
// / Determine the types directly referenced by the given identifier type.
3002
- static DirectlyReferencedTypeDecls
3003
- directReferencesForDeclRefTypeRepr (Evaluator &evaluator, ASTContext &ctx,
3004
- DeclRefTypeRepr *repr, DeclContext *dc,
3005
- bool allowUsableFromInline,
3006
- bool rhsOfSelfRequirement,
3007
- bool allowProtocolMembers) {
3024
+ static DirectlyReferencedTypeDecls directReferencesForDeclRefTypeRepr (
3025
+ Evaluator &evaluator, ASTContext &ctx, DeclRefTypeRepr *repr,
3026
+ DeclContext *dc, DirectlyReferencedTypeLookupOptions options) {
3008
3027
if (auto *qualIdentTR = dyn_cast<QualifiedIdentTypeRepr>(repr)) {
3009
3028
auto result = directReferencesForTypeRepr (
3010
- evaluator, ctx, qualIdentTR->getBase (), dc,
3011
- allowUsableFromInline, rhsOfSelfRequirement, allowProtocolMembers);
3029
+ evaluator, ctx, qualIdentTR->getBase (), dc, options);
3012
3030
3013
3031
// For a qualified identifier, perform qualified name lookup.
3014
3032
result.first = directReferencesForQualifiedTypeLookup (
3015
3033
evaluator, ctx, result.first , repr->getNameRef (), dc, repr->getLoc (),
3016
- allowUsableFromInline);
3034
+ options.contains (
3035
+ DirectlyReferencedTypeLookupFlags::AllowUsableFromInline));
3017
3036
3018
3037
return result;
3019
3038
}
3020
3039
3021
3040
// For an unqualified identifier, perform unqualified name lookup.
3022
3041
return directReferencesForUnqualifiedTypeLookup (
3023
3042
repr->getNameRef (), repr->getLoc (), dc, LookupOuterResults::Excluded,
3024
- allowUsableFromInline, rhsOfSelfRequirement, allowProtocolMembers );
3043
+ options );
3025
3044
}
3026
3045
3027
3046
static DirectlyReferencedTypeDecls
3028
- directReferencesForTypeRepr (Evaluator &evaluator,
3029
- ASTContext &ctx, TypeRepr *typeRepr,
3030
- DeclContext *dc, bool allowUsableFromInline,
3031
- bool rhsOfSelfRequirement,
3032
- bool allowProtocolMembers) {
3047
+ directReferencesForTypeRepr (Evaluator &evaluator, ASTContext &ctx,
3048
+ TypeRepr *typeRepr, DeclContext *dc,
3049
+ DirectlyReferencedTypeLookupOptions options) {
3033
3050
DirectlyReferencedTypeDecls result;
3034
3051
3035
3052
switch (typeRepr->getKind ()) {
@@ -3040,20 +3057,14 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3040
3057
case TypeReprKind::Attributed: {
3041
3058
auto attributed = cast<AttributedTypeRepr>(typeRepr);
3042
3059
return directReferencesForTypeRepr (evaluator, ctx,
3043
- attributed->getTypeRepr (), dc,
3044
- allowUsableFromInline,
3045
- rhsOfSelfRequirement,
3046
- allowProtocolMembers);
3060
+ attributed->getTypeRepr (), dc, options);
3047
3061
}
3048
3062
3049
3063
case TypeReprKind::Composition: {
3050
3064
auto composition = cast<CompositionTypeRepr>(typeRepr);
3051
3065
for (auto component : composition->getTypes ()) {
3052
3066
auto componentResult =
3053
- directReferencesForTypeRepr (evaluator, ctx, component, dc,
3054
- allowUsableFromInline,
3055
- rhsOfSelfRequirement,
3056
- allowProtocolMembers);
3067
+ directReferencesForTypeRepr (evaluator, ctx, component, dc, options);
3057
3068
result.first .insert (result.first .end (),
3058
3069
componentResult.first .begin (),
3059
3070
componentResult.first .end ());
@@ -3066,11 +3077,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3066
3077
3067
3078
case TypeReprKind::QualifiedIdent:
3068
3079
case TypeReprKind::UnqualifiedIdent:
3069
- return directReferencesForDeclRefTypeRepr (evaluator, ctx,
3070
- cast<DeclRefTypeRepr>(typeRepr),
3071
- dc, allowUsableFromInline,
3072
- rhsOfSelfRequirement,
3073
- allowProtocolMembers);
3080
+ return directReferencesForDeclRefTypeRepr (
3081
+ evaluator, ctx, cast<DeclRefTypeRepr>(typeRepr), dc, options);
3074
3082
3075
3083
case TypeReprKind::Dictionary:
3076
3084
result.first .push_back (ctx.getDictionaryDecl ());
@@ -3079,11 +3087,8 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3079
3087
case TypeReprKind::Tuple: {
3080
3088
auto tupleRepr = cast<TupleTypeRepr>(typeRepr);
3081
3089
if (tupleRepr->isParenType ()) {
3082
- result = directReferencesForTypeRepr (evaluator, ctx,
3083
- tupleRepr->getElementType (0 ), dc,
3084
- allowUsableFromInline,
3085
- rhsOfSelfRequirement,
3086
- allowProtocolMembers);
3090
+ result = directReferencesForTypeRepr (
3091
+ evaluator, ctx, tupleRepr->getElementType (0 ), dc, options);
3087
3092
} else {
3088
3093
result.first .push_back (ctx.getBuiltinTupleDecl ());
3089
3094
}
@@ -3092,40 +3097,28 @@ directReferencesForTypeRepr(Evaluator &evaluator,
3092
3097
3093
3098
case TypeReprKind::Vararg: {
3094
3099
auto packExpansionRepr = cast<VarargTypeRepr>(typeRepr);
3095
- return directReferencesForTypeRepr (evaluator, ctx,
3096
- packExpansionRepr->getElementType (), dc,
3097
- allowUsableFromInline,
3098
- rhsOfSelfRequirement,
3099
- allowProtocolMembers);
3100
+ return directReferencesForTypeRepr (
3101
+ evaluator, ctx, packExpansionRepr->getElementType (), dc, options);
3100
3102
}
3101
3103
3102
3104
case TypeReprKind::PackExpansion: {
3103
3105
auto packExpansionRepr = cast<PackExpansionTypeRepr>(typeRepr);
3104
- return directReferencesForTypeRepr (evaluator, ctx,
3105
- packExpansionRepr->getPatternType (), dc,
3106
- allowUsableFromInline,
3107
- rhsOfSelfRequirement,
3108
- allowProtocolMembers);
3106
+ return directReferencesForTypeRepr (
3107
+ evaluator, ctx, packExpansionRepr->getPatternType (), dc, options);
3109
3108
}
3110
3109
3111
3110
case TypeReprKind::PackElement: {
3112
3111
auto packReferenceRepr = cast<PackElementTypeRepr>(typeRepr);
3113
- return directReferencesForTypeRepr (evaluator, ctx,
3114
- packReferenceRepr->getPackType (), dc,
3115
- allowUsableFromInline,
3116
- rhsOfSelfRequirement,
3117
- allowProtocolMembers);
3112
+ return directReferencesForTypeRepr (
3113
+ evaluator, ctx, packReferenceRepr->getPackType (), dc, options);
3118
3114
}
3119
3115
3120
3116
case TypeReprKind::Inverse: {
3121
3117
// If ~P references a protocol P with a known inverse kind, record it in
3122
3118
// our set of inverses, otherwise just ignore it. We'll diagnose it later.
3123
3119
auto *inverseRepr = cast<InverseTypeRepr>(typeRepr);
3124
- auto innerResult = directReferencesForTypeRepr (evaluator, ctx,
3125
- inverseRepr->getConstraint (), dc,
3126
- allowUsableFromInline,
3127
- rhsOfSelfRequirement,
3128
- allowProtocolMembers);
3120
+ auto innerResult = directReferencesForTypeRepr (
3121
+ evaluator, ctx, inverseRepr->getConstraint (), dc, options);
3129
3122
if (innerResult.first .size () == 1 ) {
3130
3123
if (auto *proto = dyn_cast<ProtocolDecl>(innerResult.first [0 ])) {
3131
3124
if (auto ip = proto->getInvertibleProtocolKind ()) {
@@ -3237,13 +3230,13 @@ DirectlyReferencedTypeDecls InheritedDeclsReferencedRequest::evaluate(
3237
3230
// If looking at a protocol's inheritance list,
3238
3231
// do not look at protocol members to avoid circularity.
3239
3232
// Protocols cannot inherit from any protocol members anyway.
3240
- bool allowProtocolMembers = (dc->getSelfProtocolDecl () == nullptr );
3233
+ DirectlyReferencedTypeLookupOptions options;
3234
+ if (dc->getSelfProtocolDecl () == nullptr ) {
3235
+ options |= DirectlyReferencedTypeLookupFlags::AllowProtocolMembers;
3236
+ }
3241
3237
3242
3238
return directReferencesForTypeRepr (evaluator, dc->getASTContext (), typeRepr,
3243
- const_cast <DeclContext *>(dc),
3244
- /* allowUsableFromInline=*/ false ,
3245
- /* rhsOfSelfRequirement=*/ false ,
3246
- allowProtocolMembers);
3239
+ const_cast <DeclContext *>(dc), options);
3247
3240
}
3248
3241
3249
3242
// Fall back to semantic types.
@@ -3261,11 +3254,9 @@ DirectlyReferencedTypeDecls UnderlyingTypeDeclsReferencedRequest::evaluate(
3261
3254
TypeAliasDecl *typealias) const {
3262
3255
// Prefer syntactic information when we have it.
3263
3256
if (auto typeRepr = typealias->getUnderlyingTypeRepr ()) {
3264
- return directReferencesForTypeRepr (evaluator, typealias->getASTContext (),
3265
- typeRepr, typealias,
3266
- /* allowUsableFromInline=*/ false ,
3267
- /* rhsOfSelfRequirement=*/ false ,
3268
- /* allowProtocolMembers=*/ true );
3257
+ return directReferencesForTypeRepr (
3258
+ evaluator, typealias->getASTContext (), typeRepr, typealias,
3259
+ defaultDirectlyReferencedTypeLookupOptions);
3269
3260
}
3270
3261
3271
3262
// Fall back to semantic types.
@@ -3421,11 +3412,12 @@ ExtendedNominalRequest::evaluate(Evaluator &evaluator,
3421
3412
}
3422
3413
3423
3414
ASTContext &ctx = ext->getASTContext ();
3424
- DirectlyReferencedTypeDecls referenced =
3425
- directReferencesForTypeRepr (evaluator, ctx, typeRepr, ext->getParent (),
3426
- ext->isInSpecializeExtensionContext (),
3427
- /* rhsOfSelfRequirement=*/ false ,
3428
- /* allowProtocolMembers=*/ true );
3415
+ auto options = defaultDirectlyReferencedTypeLookupOptions;
3416
+ if (ext->isInSpecializeExtensionContext ()) {
3417
+ options |= DirectlyReferencedTypeLookupFlags::AllowUsableFromInline;
3418
+ }
3419
+ DirectlyReferencedTypeDecls referenced = directReferencesForTypeRepr (
3420
+ evaluator, ctx, typeRepr, ext->getParent (), options);
3429
3421
3430
3422
// Resolve those type declarations to nominal type declarations.
3431
3423
SmallVector<ModuleDecl *, 2 > modulesFound;
@@ -3473,10 +3465,8 @@ static bool declsAreProtocols(ArrayRef<TypeDecl *> decls) {
3473
3465
3474
3466
bool TypeRepr::isProtocolOrProtocolComposition (DeclContext *dc) {
3475
3467
auto &ctx = dc->getASTContext ();
3476
- auto references = directReferencesForTypeRepr (ctx.evaluator , ctx, this , dc,
3477
- /* allowUsableFromInline=*/ false ,
3478
- /* rhsOfSelfRequirement=*/ false ,
3479
- /* allowProtocolMembers=*/ true );
3468
+ auto references = directReferencesForTypeRepr (
3469
+ ctx.evaluator , ctx, this , dc, defaultDirectlyReferencedTypeLookupOptions);
3480
3470
return declsAreProtocols (references.first );
3481
3471
}
3482
3472
@@ -3506,13 +3496,9 @@ static GenericParamList *
3506
3496
createTupleExtensionGenericParams (ASTContext &ctx,
3507
3497
ExtensionDecl *ext,
3508
3498
TypeRepr *extendedTypeRepr) {
3509
- DirectlyReferencedTypeDecls referenced =
3510
- directReferencesForTypeRepr (ctx.evaluator , ctx,
3511
- extendedTypeRepr,
3512
- ext->getParent (),
3513
- /* allowUsableFromInline=*/ false ,
3514
- /* rhsOfSelfRequirement=*/ false ,
3515
- /* allowProtocolMembers=*/ true );
3499
+ DirectlyReferencedTypeDecls referenced = directReferencesForTypeRepr (
3500
+ ctx.evaluator , ctx, extendedTypeRepr, ext->getParent (),
3501
+ defaultDirectlyReferencedTypeLookupOptions);
3516
3502
assert (referenced.second .empty () && " Implement me" );
3517
3503
if (referenced.first .size () != 1 || !isa<TypeAliasDecl>(referenced.first [0 ]))
3518
3504
return nullptr ;
@@ -3784,11 +3770,9 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
3784
3770
auto &ctx = dc->getASTContext ();
3785
3771
DirectlyReferencedTypeDecls decls;
3786
3772
if (auto *typeRepr = attr->getTypeRepr ()) {
3787
- decls = directReferencesForTypeRepr (
3788
- evaluator, ctx, typeRepr, dc,
3789
- /* allowUsableFromInline=*/ false ,
3790
- /* rhsOfSelfRequirement=*/ false ,
3791
- /* allowProtocolMembers=*/ true );
3773
+ decls =
3774
+ directReferencesForTypeRepr (evaluator, ctx, typeRepr, dc,
3775
+ defaultDirectlyReferencedTypeLookupOptions);
3792
3776
} else if (Type type = attr->getType ()) {
3793
3777
decls = directReferencesForType (type);
3794
3778
}
@@ -3818,9 +3802,7 @@ CustomAttrNominalRequest::evaluate(Evaluator &evaluator,
3818
3802
anyObject = false ;
3819
3803
decls = directReferencesForUnqualifiedTypeLookup (
3820
3804
name, loc, dc, LookupOuterResults::Included,
3821
- /* allowUsableFromInline=*/ false ,
3822
- /* rhsOfSelfRequirement=*/ false ,
3823
- /* allowProtocolMembers*/ true );
3805
+ defaultDirectlyReferencedTypeLookupOptions);
3824
3806
nominals = resolveTypeDeclsToNominal (evaluator, ctx, decls.first ,
3825
3807
ResolveToNominalOptions (),
3826
3808
modulesFound, anyObject);
@@ -4056,11 +4038,8 @@ ProtocolDecl *ImplementsAttrProtocolRequest::evaluate(
4056
4038
auto typeRepr = attr->getProtocolTypeRepr ();
4057
4039
4058
4040
ASTContext &ctx = dc->getASTContext ();
4059
- DirectlyReferencedTypeDecls referenced =
4060
- directReferencesForTypeRepr (evaluator, ctx, typeRepr, dc,
4061
- /* allowUsableFromInline=*/ false ,
4062
- /* rhsOfSelfRequirement=*/ false ,
4063
- /* allowProtocolMembers=*/ true );
4041
+ DirectlyReferencedTypeDecls referenced = directReferencesForTypeRepr (
4042
+ evaluator, ctx, typeRepr, dc, defaultDirectlyReferencedTypeLookupOptions);
4064
4043
4065
4044
// Resolve those type declarations to nominal type declarations.
4066
4045
SmallVector<ModuleDecl *, 2 > modulesFound;
0 commit comments