Skip to content

Commit 14626e4

Browse files
committed
[AST] Add a way to erase marker protocols from a protocol composition
1 parent c84a346 commit 14626e4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

include/swift/AST/Types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6050,6 +6050,10 @@ class ProtocolCompositionType final : public TypeBase,
60506050
return Bits.ProtocolCompositionType.HasExplicitAnyObject;
60516051
}
60526052

6053+
/// Produce a new type (potentially not be a protoocl composition)
6054+
/// which drops all of the marker protocol types associated with this one.
6055+
Type withoutMarkerProtocols() const;
6056+
60536057
// Implement isa/cast/dyncast/etc.
60546058
static bool classof(const TypeBase *T) {
60556059
return T->getKind() == TypeKind::ProtocolComposition;

lib/AST/Type.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3704,6 +3704,20 @@ Type ProtocolCompositionType::getInverseOf(const ASTContext &C,
37043704
/*HasExplicitAnyObject=*/false);
37053705
}
37063706

3707+
Type ProtocolCompositionType::withoutMarkerProtocols() const {
3708+
SmallVector<Type, 4> newMembers;
3709+
llvm::copy_if(getMembers(), std::back_inserter(newMembers), [](Type member) {
3710+
auto *P = member->getAs<ProtocolType>();
3711+
return !(P && P->getDecl()->isMarkerProtocol());
3712+
});
3713+
3714+
if (newMembers.size() == getMembers().size())
3715+
return Type(const_cast<ProtocolCompositionType *>(this));
3716+
3717+
return ProtocolCompositionType::get(getASTContext(), newMembers,
3718+
getInverses(), hasExplicitAnyObject());
3719+
}
3720+
37073721
Type ProtocolCompositionType::get(const ASTContext &C,
37083722
ArrayRef<Type> Members,
37093723
InvertibleProtocolSet Inverses,

0 commit comments

Comments
 (0)