Skip to content

Commit 03ac6b2

Browse files
committed
[NFC] Add a utility to compute formal linkage for a generic signature
1 parent fa9be32 commit 03ac6b2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

include/swift/SIL/FormalLinkage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace swift {
1717

18+
class CanGenericSignature;
1819
class CanType;
1920
class RootProtocolConformance;
2021
class ValueDecl;
@@ -44,6 +45,7 @@ enum class FormalLinkage {
4445

4546
FormalLinkage getDeclLinkage(const ValueDecl *decl);
4647
FormalLinkage getTypeLinkage(CanType formalType);
48+
FormalLinkage getGenericSignatureLinkage(CanGenericSignature signature);
4749
SILLinkage getSILLinkage(FormalLinkage linkage,
4850
ForDefinition_t forDefinition);
4951
SILLinkage

lib/SIL/IR/SIL.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,42 @@ bool SILModule::isTypeMetadataAccessible(CanType type) {
147147
});
148148
}
149149

150+
/// Return the formal linkage of the component restrictions of this
151+
/// generic signature. Never returns PublicUnique.
152+
FormalLinkage swift::getGenericSignatureLinkage(CanGenericSignature sig) {
153+
// This can only be PublicNonUnique or HiddenUnique. Signatures can
154+
// never be PublicUnique in the first place, and we short-circuit on
155+
// Private. So we only ever update it when we see HiddenUnique linkage.
156+
FormalLinkage linkage = FormalLinkage::PublicNonUnique;
157+
158+
for (auto &req : sig.getRequirements()) {
159+
// The first type can be ignored because it should always be
160+
// a dependent type.
161+
162+
switch (req.getKind()) {
163+
case RequirementKind::Layout:
164+
continue;
165+
166+
case RequirementKind::Conformance:
167+
case RequirementKind::SameType:
168+
case RequirementKind::Superclass:
169+
switch (getTypeLinkage(CanType(req.getSecondType()))) {
170+
case FormalLinkage::PublicUnique:
171+
case FormalLinkage::PublicNonUnique:
172+
continue;
173+
case FormalLinkage::HiddenUnique:
174+
linkage = FormalLinkage::HiddenUnique;
175+
continue;
176+
case FormalLinkage::Private:
177+
// We can short-circuit with this.
178+
return linkage;
179+
}
180+
}
181+
}
182+
183+
return linkage;
184+
}
185+
150186
/// Return the minimum linkage structurally required to reference the given formal type.
151187
FormalLinkage swift::getTypeLinkage(CanType t) {
152188
assert(t->isLegalFormalType());

0 commit comments

Comments
 (0)