Skip to content

Commit b51600d

Browse files
committed
[AST] Add a way to check whether TypeRepr is an inverse of the given invertible protocol
1 parent d1b4a0c commit b51600d

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
137137
/// Is this type representation a protocol?
138138
bool isProtocolOrProtocolComposition(DeclContext *dc);
139139

140+
/// Is this `~<target>` representation.
141+
bool isInverseOf(InvertibleProtocolKind target,
142+
DeclContext *dc);
143+
140144
/// Is this type representation known to be invalid?
141145
bool isInvalid() const { return Bits.TypeRepr.Invalid; }
142146

lib/AST/NameLookup.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3305,6 +3305,29 @@ bool TypeRepr::isProtocolOrProtocolComposition(DeclContext *dc){
33053305
return declsAreProtocols(directReferencesForTypeRepr(ctx.evaluator, ctx, this, dc));
33063306
}
33073307

3308+
bool TypeRepr::isInverseOf(InvertibleProtocolKind target, DeclContext *dc) {
3309+
if (auto inverseTypeRepr = dyn_cast<InverseTypeRepr>(this)) {
3310+
auto *constraint = inverseTypeRepr->getConstraint();
3311+
3312+
auto &ctx = dc->getASTContext();
3313+
return llvm::any_of(
3314+
directReferencesForTypeRepr(ctx.evaluator, ctx, constraint, dc),
3315+
[&](const TypeDecl *decl) {
3316+
if (auto *P = dyn_cast<ProtocolDecl>(decl))
3317+
return P->getInvertibleProtocolKind() == target;
3318+
return false;
3319+
});
3320+
}
3321+
3322+
if (auto *composition = dyn_cast<CompositionTypeRepr>(this)) {
3323+
return llvm::any_of(composition->getTypes(), [&](TypeRepr *member) {
3324+
return member->isInverseOf(target, dc);
3325+
});
3326+
}
3327+
3328+
return false;
3329+
}
3330+
33083331
static GenericParamList *
33093332
createExtensionGenericParams(ASTContext &ctx,
33103333
ExtensionDecl *ext,

0 commit comments

Comments
 (0)