Skip to content

Commit 9579c9e

Browse files
committed
AST: Archetypes store an interface type instead of an Identifier or AssociatedTypeDecl
1 parent 6def545 commit 9579c9e

File tree

3 files changed

+28
-20
lines changed

3 files changed

+28
-20
lines changed

include/swift/AST/Types.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,7 +4041,7 @@ class ArchetypeType final : public SubstitutableType,
40414041

40424042
llvm::PointerUnion3<ArchetypeType *, TypeBase *,
40434043
GenericEnvironment *> ParentOrOpenedOrEnvironment;
4044-
llvm::PointerUnion<AssociatedTypeDecl *, Identifier> AssocTypeOrName;
4044+
Type InterfaceType;
40454045
MutableArrayRef<std::pair<Identifier, Type>> NestedTypes;
40464046

40474047
void populateNestedTypes() const;
@@ -4053,7 +4053,7 @@ class ArchetypeType final : public SubstitutableType,
40534053
/// The ConformsTo array will be copied into the ASTContext by this routine.
40544054
static CanTypeWrapper<ArchetypeType>
40554055
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
4056-
AssociatedTypeDecl *AssocType,
4056+
DependentMemberType *InterfaceType,
40574057
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
40584058
Type Superclass, LayoutConstraint Layout);
40594059

@@ -4063,8 +4063,8 @@ class ArchetypeType final : public SubstitutableType,
40634063
/// by this routine.
40644064
static CanTypeWrapper<ArchetypeType>
40654065
getNew(const ASTContext &Ctx,
4066-
GenericEnvironment *genericEnvironment,
4067-
Identifier Name,
4066+
GenericEnvironment *GenericEnv,
4067+
GenericTypeParamType *InterfaceType,
40684068
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
40694069
Type Superclass, LayoutConstraint Layout);
40704070

@@ -4108,15 +4108,17 @@ class ArchetypeType final : public SubstitutableType,
41084108
/// Note: opened archetypes currently don't have generic environments.
41094109
GenericEnvironment *getGenericEnvironment() const;
41104110

4111+
/// Retrieve the interface type of this associated type, which will either
4112+
/// be a GenericTypeParamType or a DependentMemberType.
4113+
Type getInterfaceType() const { return InterfaceType; }
4114+
41114115
/// Retrieve the associated type to which this archetype (if it is a nested
41124116
/// archetype) corresponds.
41134117
///
41144118
/// This associated type will have the same name as the archetype and will
41154119
/// be a member of one of the protocols to which the parent archetype
41164120
/// conforms.
4117-
AssociatedTypeDecl *getAssocType() const {
4118-
return AssocTypeOrName.dyn_cast<AssociatedTypeDecl *>();
4119-
}
4121+
AssociatedTypeDecl *getAssocType() const;
41204122

41214123
/// getConformsTo - Retrieve the set of protocols to which this substitutable
41224124
/// type shall conform.
@@ -4224,7 +4226,7 @@ class ArchetypeType final : public SubstitutableType,
42244226
const ASTContext &Ctx,
42254227
llvm::PointerUnion<ArchetypeType *, GenericEnvironment *>
42264228
ParentOrGenericEnv,
4227-
llvm::PointerUnion<AssociatedTypeDecl *, Identifier> AssocTypeOrName,
4229+
Type InterfaceType,
42284230
ArrayRef<ProtocolDecl *> ConformsTo,
42294231
Type Superclass, LayoutConstraint Layout);
42304232

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,16 +1856,16 @@ Type EquivalenceClass::getTypeInContext(GenericSignatureBuilder &builder,
18561856
ASTContext &ctx = builder.getASTContext();
18571857
if (parentArchetype) {
18581858
// Create a nested archetype.
1859-
archetype = ArchetypeType::getNew(ctx, parentArchetype, assocType, protos,
1859+
auto *depMemTy = anchor->castTo<DependentMemberType>();
1860+
archetype = ArchetypeType::getNew(ctx, parentArchetype, depMemTy, protos,
18601861
superclass, layout);
18611862

18621863
// Register this archetype with its parent.
18631864
parentArchetype->registerNestedType(assocType->getName(), archetype);
18641865
} else {
18651866
// Create a top-level archetype.
18661867
auto genericParam = anchor->castTo<GenericTypeParamType>();
1867-
Identifier name = genericParam->getName();
1868-
archetype = ArchetypeType::getNew(ctx, genericEnv, name, protos,
1868+
archetype = ArchetypeType::getNew(ctx, genericEnv, genericParam, protos,
18691869
superclass, layout);
18701870

18711871
// Register the archetype with the generic environment.

lib/AST/Type.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2577,12 +2577,12 @@ Type TupleType::getVarArgsBaseType() const {
25772577
ArchetypeType::ArchetypeType(
25782578
const ASTContext &Ctx,
25792579
llvm::PointerUnion<ArchetypeType *, GenericEnvironment *> ParentOrGenericEnv,
2580-
llvm::PointerUnion<AssociatedTypeDecl *, Identifier> AssocTypeOrName,
2580+
Type InterfaceType,
25812581
ArrayRef<ProtocolDecl *> ConformsTo,
25822582
Type Superclass, LayoutConstraint Layout)
25832583
: SubstitutableType(TypeKind::Archetype, &Ctx,
25842584
RecursiveTypeProperties::HasArchetype),
2585-
AssocTypeOrName(AssocTypeOrName) {
2585+
InterfaceType(InterfaceType) {
25862586
// Record the parent/generic environment.
25872587
if (auto parent = ParentOrGenericEnv.dyn_cast<ArchetypeType *>()) {
25882588
ParentOrOpenedOrEnvironment = parent;
@@ -2644,7 +2644,7 @@ ArchetypeType::ArchetypeType(const ASTContext &Ctx, Type Existential,
26442644
CanArchetypeType ArchetypeType::getNew(
26452645
const ASTContext &Ctx,
26462646
ArchetypeType *Parent,
2647-
AssociatedTypeDecl *AssocType,
2647+
DependentMemberType *InterfaceType,
26482648
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
26492649
Type Superclass,
26502650
LayoutConstraint Layout) {
@@ -2660,18 +2660,18 @@ CanArchetypeType ArchetypeType::getNew(
26602660
alignof(ArchetypeType), arena);
26612661

26622662
return CanArchetypeType(new (mem) ArchetypeType(
2663-
Ctx, Parent, AssocType, ConformsTo, Superclass, Layout));
2663+
Ctx, Parent, InterfaceType, ConformsTo, Superclass, Layout));
26642664
}
26652665

26662666
CanArchetypeType
26672667
ArchetypeType::getNew(const ASTContext &Ctx,
2668-
GenericEnvironment *genericEnvironment,
2669-
Identifier Name,
2668+
GenericEnvironment *GenericEnv,
2669+
GenericTypeParamType *InterfaceType,
26702670
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
26712671
Type Superclass,
26722672
LayoutConstraint Layout) {
26732673
assert(!Superclass || Superclass->getClassOrBoundGenericClass());
2674-
assert(genericEnvironment && "missing generic environment for archetype");
2674+
assert(GenericEnv && "missing generic environment for archetype");
26752675

26762676
// Gather the set of protocol declarations to which this archetype conforms.
26772677
ProtocolType::canonicalizeProtocols(ConformsTo);
@@ -2683,7 +2683,7 @@ ArchetypeType::getNew(const ASTContext &Ctx,
26832683
alignof(ArchetypeType), arena);
26842684

26852685
return CanArchetypeType(new (mem) ArchetypeType(
2686-
Ctx, genericEnvironment, Name, ConformsTo, Superclass, Layout));
2686+
Ctx, GenericEnv, InterfaceType, ConformsTo, Superclass, Layout));
26872687
}
26882688

26892689
bool ArchetypeType::requiresClass() const {
@@ -2827,11 +2827,17 @@ static void collectFullName(const ArchetypeType *Archetype,
28272827
Archetype->getName().str().end());
28282828
}
28292829

2830+
AssociatedTypeDecl *ArchetypeType::getAssocType() const {
2831+
if (auto *depMemTy = InterfaceType->getAs<DependentMemberType>())
2832+
return depMemTy->getAssocType();
2833+
return nullptr;
2834+
}
2835+
28302836
Identifier ArchetypeType::getName() const {
28312837
if (auto assocType = getAssocType())
28322838
return assocType->getName();
28332839

2834-
return AssocTypeOrName.get<Identifier>();
2840+
return InterfaceType->castTo<GenericTypeParamType>()->getName();
28352841
}
28362842

28372843
std::string ArchetypeType::getFullName() const {

0 commit comments

Comments
 (0)