Skip to content

Commit 44a92a9

Browse files
committed
[NFC] GenericSignatureImpl: Spell conformsToProtocol & getConformsTo in terms of requirements
1 parent 4e7569e commit 44a92a9

File tree

16 files changed

+47
-46
lines changed

16 files changed

+47
-46
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class GenericSignature {
152152
TypeArrayView<GenericTypeParamType> genericParams,
153153
ArrayRef<Requirement> requirements);
154154
public:
155-
using ConformsToArray = SmallVector<ProtocolDecl *, 2>;
155+
using RequiredProtocols = SmallVector<ProtocolDecl *, 2>;
156156

157157
private:
158158
// Direct comparison is disabled for generic signatures. Canonicalize them
@@ -318,12 +318,13 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
318318
/// Determine the superclass bound on the given dependent type.
319319
Type getSuperclassBound(Type type) const;
320320

321-
/// Determine the set of protocols to which the given dependent type
322-
/// must conform.
323-
GenericSignature::ConformsToArray getConformsTo(Type type) const;
321+
/// Determine the set of protocols to which the given type parameter is
322+
/// required to conform.
323+
GenericSignature::RequiredProtocols getRequiredProtocols(Type type) const;
324324

325-
/// Determine whether the given dependent type conforms to this protocol.
326-
bool conformsToProtocol(Type type, ProtocolDecl *proto) const;
325+
/// Determine whether the given type parameter is required to conform to
326+
/// the given protocol.
327+
bool requiresProtocol(Type type, ProtocolDecl *proto) const;
327328

328329
/// Determine whether the given dependent type is equal to a concrete type.
329330
bool isConcreteType(Type type) const;

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3723,11 +3723,8 @@ OpaqueTypeArchetypeType::get(OpaqueTypeDecl *Decl,
37233723
superclass = superclass.subst(Substitutions);
37243724
}
37253725
#endif
3726-
SmallVector<ProtocolDecl*, 4> protos;
3727-
for (auto proto : signature->getConformsTo(opaqueInterfaceTy)) {
3728-
protos.push_back(proto);
3729-
}
3730-
3726+
const auto protos = signature->getRequiredProtocols(opaqueInterfaceTy);
3727+
37313728
auto mem = ctx.Allocate(
37323729
OpaqueTypeArchetypeType::totalSizeToAlloc<ProtocolDecl *, Type, LayoutConstraint>(
37333730
protos.size(), superclass ? 1 : 0, layout ? 1 : 0),

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,8 +2539,9 @@ void ASTMangler::appendGenericSignatureParts(
25392539
DependentMemberType *
25402540
ASTMangler::dropProtocolFromAssociatedType(DependentMemberType *dmt) {
25412541
auto baseTy = dmt->getBase();
2542-
bool unambiguous = (!dmt->getAssocType() ||
2543-
CurGenericSignature->getConformsTo(baseTy).size() <= 1);
2542+
bool unambiguous =
2543+
(!dmt->getAssocType() ||
2544+
CurGenericSignature->getRequiredProtocols(baseTy).size() <= 1);
25442545

25452546
if (auto *baseDMT = baseTy->getAs<DependentMemberType>())
25462547
baseTy = dropProtocolFromAssociatedType(baseDMT);
@@ -2573,8 +2574,8 @@ void ASTMangler::appendAssociatedTypeName(DependentMemberType *dmt) {
25732574
// If the base type is known to have a single protocol conformance
25742575
// in the current generic context, then we don't need to disambiguate the
25752576
// associated type name by protocol.
2576-
if (!OptimizeProtocolNames || !CurGenericSignature
2577-
|| CurGenericSignature->getConformsTo(dmt->getBase()).size() > 1) {
2577+
if (!OptimizeProtocolNames || !CurGenericSignature ||
2578+
CurGenericSignature->getRequiredProtocols(dmt->getBase()).size() > 1) {
25782579
appendAnyGenericType(assocTy->getProtocol());
25792580
}
25802581
return;

lib/AST/GenericSignature.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -424,10 +424,10 @@ Type GenericSignatureImpl::getSuperclassBound(Type type) const {
424424
return equivClass->superclass;
425425
}
426426

427-
/// Determine the set of protocols to which the given dependent type
428-
/// must conform.
429-
SmallVector<ProtocolDecl *, 2>
430-
GenericSignatureImpl::getConformsTo(Type type) const {
427+
/// Determine the set of protocols to which the given type parameter is
428+
/// required to conform.
429+
GenericSignature::RequiredProtocols
430+
GenericSignatureImpl::getRequiredProtocols(Type type) const {
431431
if (!type->isTypeParameter()) return { };
432432

433433
auto &builder = *getGenericSignatureBuilder();
@@ -437,12 +437,12 @@ GenericSignatureImpl::getConformsTo(Type type) const {
437437
ArchetypeResolutionKind::CompleteWellFormed);
438438
if (!equivClass) return { };
439439

440-
// If this type was mapped to a concrete type, then there are no
441-
// requirements.
440+
// If this type parameter was mapped to a concrete type, then there
441+
// are no requirements.
442442
if (equivClass->concreteType) return { };
443443

444444
// Retrieve the protocols to which this type conforms.
445-
SmallVector<ProtocolDecl *, 2> result;
445+
GenericSignature::RequiredProtocols result;
446446
for (const auto &conforms : equivClass->conformsTo)
447447
result.push_back(conforms.first);
448448

@@ -452,8 +452,8 @@ GenericSignatureImpl::getConformsTo(Type type) const {
452452
return result;
453453
}
454454

455-
bool GenericSignatureImpl::conformsToProtocol(Type type,
456-
ProtocolDecl *proto) const {
455+
bool GenericSignatureImpl::requiresProtocol(Type type,
456+
ProtocolDecl *proto) const {
457457
assert(type->isTypeParameter() && "Expected a type parameter");
458458

459459
auto &builder = *getGenericSignatureBuilder();
@@ -463,7 +463,11 @@ bool GenericSignatureImpl::conformsToProtocol(Type type,
463463
ArchetypeResolutionKind::CompleteWellFormed);
464464
if (!equivClass) return false;
465465

466-
// FIXME: Deal with concrete conformances here?
466+
// FIXME: Optionally deal with concrete conformances here
467+
// or have a separate method do that additionally?
468+
//
469+
// If this type parameter was mapped to a concrete type, then there
470+
// are no requirements.
467471
if (equivClass->concreteType) return false;
468472

469473
// Check whether the representative conforms to this protocol.
@@ -541,7 +545,7 @@ bool GenericSignatureImpl::isRequirementSatisfied(
541545
auto protocol = protocolType->getDecl();
542546

543547
if (canFirstType->isTypeParameter())
544-
return conformsToProtocol(canFirstType, protocol);
548+
return requiresProtocol(canFirstType, protocol);
545549
else
546550
return (bool)GSB->lookupConformance(/*dependentType=*/CanType(),
547551
canFirstType, protocol);

lib/AST/SubstitutionMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ SubstitutionMap::lookupConformance(CanType type, ProtocolDecl *proto) const {
368368

369369
// If the type doesn't conform to this protocol, the result isn't formed
370370
// from these requirements.
371-
if (!genericSig->conformsToProtocol(type, proto))
371+
if (!genericSig->requiresProtocol(type, proto))
372372
return ProtocolConformanceRef::forInvalid();
373373

374374
auto accessPath =

lib/ClangImporter/ImportType.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2416,7 +2416,7 @@ bool ClangImporter::Implementation::matchesHashableBound(Type type) {
24162416
if (auto *generic = genericTy->getDecl()) {
24172417
auto genericSig =
24182418
generic->getDeclContext()->getGenericSignatureOfContext();
2419-
if (genericSig && genericSig->getConformsTo(type).empty()) {
2419+
if (genericSig && genericSig->getRequiredProtocols(type).empty()) {
24202420
type = genericSig->getSuperclassBound(type);
24212421
if (!type)
24222422
return false;

lib/IDE/CodeCompletion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,7 +2344,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
23442344
}
23452345

23462346
if (t->isTypeParameter()) {
2347-
auto protos = genericSig->getConformsTo(t);
2347+
const auto protos = genericSig->getRequiredProtocols(t);
23482348
if (!protos.empty())
23492349
return buildProtocolComposition(protos);
23502350
}
@@ -4729,7 +4729,7 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
47294729
bool hasExplicitAnyObject = false;
47304730
if (auto superTy = genericSig->getSuperclassBound(ResultT))
47314731
opaqueTypes.push_back(superTy);
4732-
for (auto proto : genericSig->getConformsTo(ResultT))
4732+
for (const auto proto : genericSig->getRequiredProtocols(ResultT))
47334733
opaqueTypes.push_back(proto->getDeclaredInterfaceType());
47344734
if (auto layout = genericSig->getLayoutConstraint(ResultT))
47354735
hasExplicitAnyObject = layout->isClass();

lib/IRGen/Fulfillment.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class FulfillmentMap {
6868
virtual bool hasLimitedInterestingConformances(CanType type) const = 0;
6969

7070
/// Return the limited interesting conformances for an interesting type.
71-
virtual GenericSignature::ConformsToArray
71+
virtual GenericSignature::RequiredProtocols
7272
getInterestingConformances(CanType type) const = 0;
7373

7474
/// Return the limited interesting conformances for an interesting type.

lib/IRGen/GenProto.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class PolymorphicConvention {
101101

102102
FulfillmentMap Fulfillments;
103103

104-
GenericSignature::ConformsToArray getConformsTo(Type t) {
105-
return Generics->getConformsTo(t);
104+
GenericSignature::RequiredProtocols getRequiredProtocols(Type t) {
105+
return Generics->getRequiredProtocols(t);
106106
}
107107

108108
CanType getSuperclassBound(Type t) {
@@ -166,9 +166,9 @@ class PolymorphicConvention {
166166
bool hasLimitedInterestingConformances(CanType type) const override {
167167
return true;
168168
}
169-
GenericSignature::ConformsToArray
169+
GenericSignature::RequiredProtocols
170170
getInterestingConformances(CanType type) const override {
171-
return Self.getConformsTo(type);
171+
return Self.getRequiredProtocols(type);
172172
}
173173
CanType getSuperclassBound(CanType type) const override {
174174
return Self.getSuperclassBound(type);
@@ -1203,7 +1203,7 @@ class AccessorConformanceInfo : public ConformanceInfo {
12031203
bool hasLimitedInterestingConformances(CanType type) const override {
12041204
return false;
12051205
}
1206-
GenericSignature::ConformsToArray
1206+
GenericSignature::RequiredProtocols
12071207
getInterestingConformances(CanType type) const override {
12081208
llvm_unreachable("no limits");
12091209
}

lib/IRGen/LocalTypeData.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ void LocalTypeDataCache::addAbstractForTypeMetadata(IRGenFunction &IGF,
497497
bool hasLimitedInterestingConformances(CanType type) const override {
498498
return false;
499499
}
500-
GenericSignature::ConformsToArray
500+
GenericSignature::RequiredProtocols
501501
getInterestingConformances(CanType type) const override {
502502
llvm_unreachable("no limits");
503503
}

lib/IRGen/MetadataRequest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ bool irgen::isNominalGenericContextTypeMetadataAccessTrivial(
741741

742742
auto allWitnessTablesAreReferenceable = llvm::all_of(environment->getGenericParams(), [&](auto parameter) {
743743
auto signature = environment->getGenericSignature();
744-
auto protocols = signature->getConformsTo(parameter);
744+
const auto protocols = signature->getRequiredProtocols(parameter);
745745
auto argument = ((Type *)parameter)->subst(substitutions);
746746
auto canonicalType = argument->getCanonicalType();
747747
auto witnessTablesAreReferenceable = [&]() {

lib/PrintAsObjC/ModuleContentsWriter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ class ReferencedTypeFinder : public TypeDeclFinder {
7171
if (sig->getSuperclassBound(paramTy))
7272
return true;
7373

74-
auto conformsTo = sig->getConformsTo(paramTy);
75-
return !conformsTo.empty();
74+
return !sig->getRequiredProtocols(paramTy).empty();
7675
}
7776

7877
Action visitBoundGenericType(BoundGenericType *boundGeneric) override {

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,9 +2923,8 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
29232923
require(selfRequirement &&
29242924
selfRequirement->getKind() == RequirementKind::Conformance,
29252925
"first non-same-typerequirement should be conformance requirement");
2926-
auto conformsTo = genericSig->getConformsTo(selfGenericParam);
2927-
require(std::find(conformsTo.begin(), conformsTo.end(), protocol)
2928-
!= conformsTo.end(),
2926+
const auto protos = genericSig->getRequiredProtocols(selfGenericParam);
2927+
require(std::find(protos.begin(), protos.end(), protocol) != protos.end(),
29292928
"requirement Self parameter must conform to called protocol");
29302929

29312930
auto lookupType = AMI->getLookupType();

lib/Sema/TypeCheckAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2750,7 +2750,7 @@ TypeEraserHasViableInitRequest::evaluate(Evaluator &evaluator,
27502750
ParamDecl *param = *init->getParameters()->begin();
27512751
if (param->getArgumentName() != ctx.Id_erasing ||
27522752
!param->getInterfaceType()->isEqual(genericParamType) ||
2753-
!genericSignature->conformsToProtocol(genericParamType, protocol))
2753+
!genericSignature->requiresProtocol(genericParamType, protocol))
27542754
return false;
27552755

27562756
// Allow other constraints as long as the init can be called with any

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3707,7 +3707,7 @@ CheckTypeWitnessResult swift::checkTypeWitness(DeclContext *dc,
37073707
auto *module = dc->getParentModule();
37083708

37093709
// Check protocol conformances.
3710-
for (auto reqProto : genericSig->getConformsTo(depTy)) {
3710+
for (const auto reqProto : genericSig->getRequiredProtocols(depTy)) {
37113711
if (module->lookupConformance(contextType, reqProto)
37123712
.isInvalid())
37133713
return CheckTypeWitnessResult(reqProto->getDeclaredType());

tools/swift-ast-script/ASTScriptEvaluator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ASTScriptWalker : public ASTWalker {
6060
auto paramResultType = paramFnType->getResult();
6161
if (!paramResultType->isTypeParameter()) continue;
6262
auto sig = fn->getGenericSignature();
63-
if (!sig->conformsToProtocol(paramResultType, ViewProtocol)) continue;
63+
if (!sig->requiresProtocol(paramResultType, ViewProtocol)) continue;
6464

6565
// The parameter must not be a @ViewBuilder parameter.
6666
if (param->getFunctionBuilderType()) continue;

0 commit comments

Comments
 (0)