Skip to content

Commit e475b08

Browse files
committed
AST: Remove AssociatedType
1 parent cea72e3 commit e475b08

15 files changed

+58
-128
lines changed

include/swift/AST/ProtocolAssociations.h

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,51 +23,6 @@
2323

2424
namespace swift {
2525

26-
/// A type associated with a protocol.
27-
///
28-
/// This struct exists largely so that we can maybe eventually
29-
/// generalize it to an arbitrary path.
30-
class AssociatedType {
31-
AssociatedTypeDecl *Association;
32-
using AssociationInfo = llvm::DenseMapInfo<AssociatedTypeDecl*>;
33-
34-
struct SpecialValue {};
35-
explicit AssociatedType(SpecialValue _, AssociatedTypeDecl *specialValue)
36-
: Association(specialValue) {}
37-
38-
public:
39-
explicit AssociatedType(AssociatedTypeDecl *association)
40-
: Association(association) {
41-
assert(association);
42-
}
43-
44-
ProtocolDecl *getSourceProtocol() const {
45-
return Association->getProtocol();
46-
}
47-
48-
AssociatedTypeDecl *getAssociation() const {
49-
return Association;
50-
}
51-
52-
friend bool operator==(AssociatedType lhs, AssociatedType rhs) {
53-
return lhs.Association == rhs.Association;
54-
}
55-
friend bool operator!=(AssociatedType lhs, AssociatedType rhs) {
56-
return !(lhs == rhs);
57-
}
58-
59-
unsigned getHashValue() const {
60-
return llvm::hash_value(Association);
61-
}
62-
63-
static AssociatedType getEmptyKey() {
64-
return AssociatedType(SpecialValue(), AssociationInfo::getEmptyKey());
65-
}
66-
static AssociatedType getTombstoneKey() {
67-
return AssociatedType(SpecialValue(), AssociationInfo::getTombstoneKey());
68-
}
69-
};
70-
7126
/// A base conformance of a protocol.
7227
class BaseConformance {
7328
ProtocolDecl *Source;
@@ -147,24 +102,6 @@ class AssociatedConformance {
147102
} // end namespace swift
148103

149104
namespace llvm {
150-
template <> struct DenseMapInfo<swift::AssociatedType> {
151-
static inline swift::AssociatedType getEmptyKey() {
152-
return swift::AssociatedType::getEmptyKey();
153-
}
154-
155-
static inline swift::AssociatedType getTombstoneKey() {
156-
return swift::AssociatedType::getTombstoneKey();
157-
}
158-
159-
static unsigned getHashValue(swift::AssociatedType val) {
160-
return val.getHashValue();
161-
}
162-
163-
static bool isEqual(swift::AssociatedType lhs, swift::AssociatedType rhs) {
164-
return lhs == rhs;
165-
}
166-
};
167-
168105
template <> struct DenseMapInfo<swift::AssociatedConformance> {
169106
static inline swift::AssociatedConformance getEmptyKey() {
170107
return swift::AssociatedConformance::getEmptyKey();

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
100100
// If this is a new associated type (which does not override an
101101
// existing associated type), add it.
102102
if (associatedType->getOverriddenDecls().empty())
103-
asDerived().addAssociatedType(AssociatedType(associatedType));
103+
asDerived().addAssociatedType(associatedType);
104104
}
105105

106106
if (asDerived().shouldVisitRequirementSignatureOnly())

lib/IRGen/GenArchetype.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ irgen::emitArchetypeTypeMetadataRef(IRGenFunction &IGF,
103103
auto parent = cast<ArchetypeType>(
104104
archetype->getGenericEnvironment()->mapTypeIntoContext(
105105
member->getBase())->getCanonicalType());
106-
AssociatedType association(member->getAssocType());
106+
auto *assocType = member->getAssocType();
107107

108108
MetadataResponse response =
109-
emitAssociatedTypeMetadataRef(IGF, parent, association, request);
109+
emitAssociatedTypeMetadataRef(IGF, parent, assocType, request);
110110

111111
setTypeMetadataName(IGF.IGM, response.getMetadata(), archetype);
112112

@@ -321,19 +321,19 @@ llvm::Value *irgen::emitArchetypeWitnessTableRef(IRGenFunction &IGF,
321321
MetadataResponse
322322
irgen::emitAssociatedTypeMetadataRef(IRGenFunction &IGF,
323323
CanArchetypeType origin,
324-
AssociatedType association,
324+
AssociatedTypeDecl *assocType,
325325
DynamicMetadataRequest request) {
326326
// Find the conformance of the origin to the associated type's protocol.
327327
llvm::Value *wtable = emitArchetypeWitnessTableRef(IGF, origin,
328-
association.getSourceProtocol());
328+
assocType->getProtocol());
329329

330330
// Find the origin's type metadata.
331331
llvm::Value *originMetadata =
332332
emitArchetypeTypeMetadataRef(IGF, origin, MetadataState::Abstract)
333333
.getMetadata();
334334

335335
return emitAssociatedTypeMetadataRef(IGF, originMetadata, wtable,
336-
association, request);
336+
assocType, request);
337337
}
338338

339339
const TypeInfo *TypeConverter::convertArchetypeType(ArchetypeType *archetype) {

lib/IRGen/GenArchetype.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace llvm {
2525
}
2626

2727
namespace swift {
28-
class AssociatedType;
28+
class AssociatedTypeDecl;
2929
class ProtocolDecl;
3030
class SILType;
3131

@@ -51,7 +51,7 @@ namespace irgen {
5151
/// Emit a metadata reference for an associated type of an archetype.
5252
MetadataResponse emitAssociatedTypeMetadataRef(IRGenFunction &IGF,
5353
CanArchetypeType origin,
54-
AssociatedType association,
54+
AssociatedTypeDecl *assocType,
5555
DynamicMetadataRequest request);
5656

5757
/// Emit a dynamic metatype lookup for the given archetype.

lib/IRGen/GenMeta.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -953,8 +953,7 @@ namespace {
953953
auto flags = Flags(Flags::Kind::AssociatedTypeAccessFunction);
954954
if (auto &schema = IGM.getOptions().PointerAuth
955955
.ProtocolAssociatedTypeAccessFunctions) {
956-
addDiscriminator(flags, schema,
957-
AssociatedType(entry.getAssociatedType()));
956+
addDiscriminator(flags, schema, entry.getAssociatedType());
958957
}
959958

960959
// Look for a default witness.

lib/IRGen/GenPointerAuth.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ struct IRGenModule::PointerAuthCachesType {
160160
llvm::DenseMap<SILDeclRef, llvm::ConstantInt*> Decls;
161161
llvm::DenseMap<CanType, llvm::ConstantInt*> Types;
162162
llvm::DenseMap<CanType, llvm::ConstantInt*> YieldTypes;
163-
llvm::DenseMap<AssociatedType, llvm::ConstantInt*> AssociatedTypes;
163+
llvm::DenseMap<AssociatedTypeDecl *, llvm::ConstantInt*> AssociatedTypes;
164164
llvm::DenseMap<AssociatedConformance, llvm::ConstantInt*> AssociatedConformances;
165165
};
166166

@@ -336,9 +336,9 @@ static llvm::ConstantInt *getDiscriminatorForString(IRGenModule &IGM,
336336
return getDiscriminatorForHash(IGM, rawHash);
337337
}
338338

339-
static std::string mangle(AssociatedType association) {
340-
return IRGenMangler(association.getAssociation()->getASTContext())
341-
.mangleAssociatedTypeAccessFunctionDiscriminator(association);
339+
static std::string mangle(AssociatedTypeDecl *assocType) {
340+
return IRGenMangler(assocType->getASTContext())
341+
.mangleAssociatedTypeAccessFunctionDiscriminator(assocType);
342342
}
343343

344344
static std::string mangle(const AssociatedConformance &association) {
@@ -430,7 +430,7 @@ PointerAuthEntity::getDeclDiscriminator(IRGenModule &IGM) const {
430430
}
431431

432432
case Kind::AssociatedType: {
433-
auto association = Storage.get<AssociatedType>(StoredKind);
433+
auto association = Storage.get<AssociatedTypeDecl *>(StoredKind);
434434
llvm::ConstantInt *&cache =
435435
IGM.getPointerAuthCaches().AssociatedTypes[association];
436436
if (cache) return cache;

lib/IRGen/GenPointerAuth.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class PointerAuthEntity {
8686
using Members = ExternalUnionMembers<void,
8787
Special,
8888
ValueWitness,
89-
AssociatedType,
89+
AssociatedTypeDecl *,
9090
AssociatedConformance,
9191
CanSILFunctionType,
9292
SILDeclRef,
@@ -105,7 +105,7 @@ class PointerAuthEntity {
105105
case Kind::SILDeclRef:
106106
return Members::indexOf<SILDeclRef>();
107107
case Kind::AssociatedType:
108-
return Members::indexOf<AssociatedType>();
108+
return Members::indexOf<AssociatedTypeDecl *>();
109109
case Kind::AssociatedConformance:
110110
return Members::indexOf<AssociatedConformance>();
111111
case Kind::SILFunction:
@@ -139,9 +139,9 @@ class PointerAuthEntity {
139139
assert(isValueWitnessFunction(witness));
140140
Storage.emplace<ValueWitness>(StoredKind, witness);
141141
}
142-
PointerAuthEntity(AssociatedType association)
142+
PointerAuthEntity(AssociatedTypeDecl *assocType)
143143
: StoredKind(Kind::AssociatedType) {
144-
Storage.emplaceAggregate<AssociatedType>(StoredKind, association);
144+
Storage.emplaceAggregate<AssociatedTypeDecl *>(StoredKind, assocType);
145145
}
146146
PointerAuthEntity(const AssociatedConformance &association)
147147
: StoredKind(Kind::AssociatedConformance) {

lib/IRGen/GenProto.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -946,11 +946,11 @@ namespace {
946946
}
947947
}
948948

949-
void addAssociatedType(AssociatedType requirement) {
949+
void addAssociatedType(AssociatedTypeDecl *assocType) {
950950
// In Embedded Swift witness tables don't have associated-types entries.
951-
if (requirement.getAssociation()->getASTContext().LangOpts.hasFeature(Feature::Embedded))
951+
if (assocType->getASTContext().LangOpts.hasFeature(Feature::Embedded))
952952
return;
953-
Entries.push_back(WitnessTableEntry::forAssociatedType(requirement));
953+
Entries.push_back(WitnessTableEntry::forAssociatedType(assocType));
954954
}
955955

956956
void addAssociatedConformance(const AssociatedConformance &req) {
@@ -1330,8 +1330,8 @@ mapConformanceIntoContext(const RootProtocolConformance *conf) {
13301330

13311331
WitnessIndex ProtocolInfo::getAssociatedTypeIndex(
13321332
IRGenModule &IGM,
1333-
AssociatedType assocType) const {
1334-
assert(!IGM.isResilient(assocType.getSourceProtocol(),
1333+
AssociatedTypeDecl *assocType) const {
1334+
assert(!IGM.isResilient(assocType->getProtocol(),
13351335
ResilienceExpansion::Maximal) &&
13361336
"Cannot ask for the associated type index of non-resilient protocol");
13371337
for (auto &witness : getWitnessEntries()) {
@@ -1707,7 +1707,7 @@ class AccessorConformanceInfo : public ConformanceInfo {
17071707
llvm_unreachable("cannot emit a witness table with placeholders in it");
17081708
}
17091709

1710-
void addAssociatedType(AssociatedType requirement) {
1710+
void addAssociatedType(AssociatedTypeDecl *assocType) {
17111711
auto &entry = SILEntries.front();
17121712
SILEntries = SILEntries.slice(1);
17131713

@@ -1718,34 +1718,32 @@ class AccessorConformanceInfo : public ConformanceInfo {
17181718
#ifndef NDEBUG
17191719
assert(entry.getKind() == SILWitnessTable::AssociatedType
17201720
&& "sil witness table does not match protocol");
1721-
assert(entry.getAssociatedTypeWitness().Requirement
1722-
== requirement.getAssociation()
1721+
assert(entry.getAssociatedTypeWitness().Requirement == assocType
17231722
&& "sil witness table does not match protocol");
1724-
auto piIndex = PI.getAssociatedTypeIndex(IGM, requirement);
1723+
auto piIndex = PI.getAssociatedTypeIndex(IGM, assocType);
17251724
assert((size_t)piIndex.getValue() ==
17261725
Table.size() - WitnessTableFirstRequirementOffset &&
17271726
"offset doesn't match ProtocolInfo layout");
17281727
#else
17291728
(void)entry;
17301729
#endif
17311730

1732-
auto associate =
1733-
Conformance.getTypeWitness(requirement.getAssociation());
1734-
llvm::Constant *witness =
1731+
auto typeWitness = Conformance.getTypeWitness(assocType);
1732+
llvm::Constant *typeWitnessAddr =
17351733
IGM.getAssociatedTypeWitness(
1736-
associate,
1734+
typeWitness,
17371735
Conformance.getDeclContext()->getGenericSignatureOfContext(),
17381736
/*inProtocolContext=*/false);
1739-
witness = llvm::ConstantExpr::getBitCast(witness, IGM.Int8PtrTy);
1737+
typeWitnessAddr = llvm::ConstantExpr::getBitCast(typeWitnessAddr, IGM.Int8PtrTy);
17401738

17411739
if (isRelative) {
1742-
Table.addRelativeAddress(witness);
1740+
Table.addRelativeAddress(typeWitnessAddr);
17431741
return;
17441742
}
17451743

17461744
auto &schema = IGM.getOptions().PointerAuth
17471745
.ProtocolAssociatedTypeAccessFunctions;
1748-
Table.addSignedPointer(witness, schema, requirement);
1746+
Table.addSignedPointer(typeWitnessAddr, schema, assocType);
17491747
}
17501748

17511749
void addAssociatedConformance(AssociatedConformance requirement) {
@@ -3513,7 +3511,7 @@ MetadataResponse MetadataPath::followComponent(IRGenFunction &IGF,
35133511
SubstitutionMap::getProtocolSubstitutions(sourceConformance))
35143512
->getCanonicalType();
35153513
if (auto archetypeType = dyn_cast<ArchetypeType>(baseSubstType)) {
3516-
AssociatedType baseAssocType(depMemType->getAssocType());
3514+
auto *baseAssocType = depMemType->getAssocType();
35173515

35183516
MetadataResponse response =
35193517
emitAssociatedTypeMetadataRef(IGF, archetypeType, baseAssocType,
@@ -4538,18 +4536,18 @@ MetadataResponse
45384536
irgen::emitAssociatedTypeMetadataRef(IRGenFunction &IGF,
45394537
llvm::Value *parentMetadata,
45404538
llvm::Value *wtable,
4541-
AssociatedType associatedType,
4539+
AssociatedTypeDecl *assocType,
45424540
DynamicMetadataRequest request) {
45434541
auto &IGM = IGF.IGM;
45444542

45454543
// Extract the requirements base descriptor.
45464544
auto reqBaseDescriptor =
45474545
IGM.getAddrOfProtocolRequirementsBaseDescriptor(
4548-
associatedType.getSourceProtocol());
4546+
assocType->getProtocol());
45494547

45504548
// Extract the associated type descriptor.
45514549
auto assocTypeDescriptor =
4552-
IGM.getAddrOfAssociatedTypeDescriptor(associatedType.getAssociation());
4550+
IGM.getAddrOfAssociatedTypeDescriptor(assocType);
45534551
// Call swift_getAssociatedTypeWitness().
45544552
auto call =
45554553
IGF.IGM.IRGen.Opts.UseRelativeProtocolWitnessTables ?

lib/IRGen/GenProto.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace llvm {
2929

3030
namespace swift {
3131
class AssociatedConformance;
32-
class AssociatedType;
32+
class AssociatedTypeDecl;
3333
class CanType;
3434
class FuncDecl;
3535
enum class MetadataState : size_t;
@@ -90,11 +90,11 @@ namespace irgen {
9090
///
9191
/// \param parentMetadata - the type metadata for T
9292
/// \param wtable - the witness table witnessing the conformance of T to P
93-
/// \param associatedType - the declaration of X; a member of P
93+
/// \param assocType - the declaration of X; a member of P
9494
MetadataResponse emitAssociatedTypeMetadataRef(IRGenFunction &IGF,
9595
llvm::Value *parentMetadata,
9696
llvm::Value *wtable,
97-
AssociatedType associatedType,
97+
AssociatedTypeDecl *assocType,
9898
DynamicMetadataRequest request);
9999

100100
// Return the offset one should do on a witness table pointer to retrieve the

lib/IRGen/IRGenMangler.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,10 +435,10 @@ class IRGenMangler : public Mangle::ASTMangler {
435435
}
436436

437437
std::string
438-
mangleAssociatedTypeAccessFunctionDiscriminator(AssociatedType association) {
438+
mangleAssociatedTypeAccessFunctionDiscriminator(AssociatedTypeDecl *assocDecl) {
439439
beginMangling();
440-
appendAnyGenericType(association.getSourceProtocol());
441-
appendIdentifier(association.getAssociation()->getNameStr());
440+
appendAnyGenericType(assocDecl->getProtocol());
441+
appendIdentifier(assocDecl->getNameStr());
442442
return finalize();
443443
}
444444

lib/IRGen/IRGenModule.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,8 @@ static void checkPointerAuthAssociatedTypeDiscriminator(IRGenModule &IGM, ArrayR
187187
auto &schema = IGM.getOptions().PointerAuth.ProtocolAssociatedTypeAccessFunctions;
188188
if (!schema.isEnabled()) return;
189189

190-
auto decl = dyn_cast_or_null<AssociatedTypeDecl>(lookupSimple(IGM.getSwiftModule(), declPath));
191-
assert(decl && "decl not found");
192-
auto discriminator = PointerAuthInfo::getOtherDiscriminator(IGM, schema, AssociatedType(decl));
190+
auto decl = cast<AssociatedTypeDecl>(lookupSimple(IGM.getSwiftModule(), declPath));
191+
auto discriminator = PointerAuthInfo::getOtherDiscriminator(IGM, schema, decl);
193192
assert(discriminator->getZExtValue() == expected && "discriminator value doesn't match");
194193
}
195194

lib/IRGen/IRGenModule.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ namespace clang {
9292
namespace swift {
9393
class GenericSignature;
9494
class AssociatedConformance;
95-
class AssociatedType;
9695
class ASTContext;
9796
class BaseConformance;
9897
class BraceStmt;

0 commit comments

Comments
 (0)