Skip to content

Commit 2974f54

Browse files
committed
Reimplement ArchetypeType::getParent() in terms of interface type + environment
Rather that requiring parent archetypes to be stored at the time of construction, map the base of the interface type through the generic environment.
1 parent 9b4d804 commit 2974f54

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

include/swift/AST/Types.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5470,6 +5470,10 @@ class ArchetypeType : public SubstitutableType,
54705470
/// Register a nested type with the given name.
54715471
void registerNestedType(Identifier name, Type nested);
54725472

5473+
/// Retrieve the parent of this archetype, or null if this is a
5474+
/// primary archetype.
5475+
ArchetypeType *getParent() const;
5476+
54735477
/// Return the root archetype parent of this archetype.
54745478
ArchetypeType *getRoot() const;
54755479

@@ -5696,25 +5700,17 @@ class NestedArchetypeType final : public ArchetypeType,
56965700
friend TrailingObjects;
56975701
friend ArchetypeType;
56985702

5699-
ArchetypeType *Parent;
5700-
57015703
public:
57025704
/// getNew - Create a new nested archetype with the given associated type.
57035705
///
57045706
/// The ConformsTo array will be copied into the ASTContext by this routine.
57055707
static CanTypeWrapper<NestedArchetypeType>
5706-
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
5708+
getNew(const ASTContext &Ctx,
57075709
DependentMemberType *InterfaceType,
57085710
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
57095711
Type Superclass, LayoutConstraint Layout,
57105712
GenericEnvironment *Environment);
57115713

5712-
/// Retrieve the parent of this archetype, or null if this is a
5713-
/// primary archetype.
5714-
ArchetypeType *getParent() const {
5715-
return Parent;
5716-
}
5717-
57185714
AssociatedTypeDecl *getAssocType() const;
57195715

57205716
static bool classof(const TypeBase *T) {
@@ -5727,7 +5723,6 @@ class NestedArchetypeType final : public ArchetypeType,
57275723

57285724
private:
57295725
NestedArchetypeType(const ASTContext &Ctx,
5730-
ArchetypeType *Parent,
57315726
Type InterfaceType,
57325727
ArrayRef<ProtocolDecl *> ConformsTo,
57335728
Type Superclass, LayoutConstraint Layout,

lib/AST/GenericEnvironment.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ GenericEnvironment::getOrCreateArchetypeFromInterfaceType(Type depType) {
279279

280280
if (parentArchetype) {
281281
auto *depMemTy = requirements.anchor->castTo<DependentMemberType>();
282-
result = NestedArchetypeType::getNew(ctx, parentArchetype, depMemTy,
282+
result = NestedArchetypeType::getNew(ctx, depMemTy,
283283
requirements.protos, superclass,
284284
requirements.layout, this);
285285
parentArchetype->registerNestedType(depMemTy->getName(), result);

lib/AST/Type.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3210,6 +3210,15 @@ ArchetypeType::ArchetypeType(TypeKind Kind,
32103210
getSubclassTrailingObjects<ProtocolDecl *>());
32113211
}
32123212

3213+
ArchetypeType *ArchetypeType::getParent() const {
3214+
if (auto depMemTy = getInterfaceType()->getAs<DependentMemberType>()) {
3215+
return getGenericEnvironment()->mapTypeIntoContext(depMemTy->getBase())
3216+
->castTo<ArchetypeType>();
3217+
}
3218+
3219+
return nullptr;
3220+
}
3221+
32133222
ArchetypeType *ArchetypeType::getRoot() const {
32143223
auto gp = InterfaceType->getRootGenericParam();
32153224
assert(gp && "Missing root generic parameter?");
@@ -3265,16 +3274,16 @@ OpenedArchetypeType::OpenedArchetypeType(const ASTContext &Ctx,
32653274
}
32663275

32673276
NestedArchetypeType::NestedArchetypeType(const ASTContext &Ctx,
3268-
ArchetypeType *Parent,
32693277
Type InterfaceType,
32703278
ArrayRef<ProtocolDecl *> ConformsTo,
32713279
Type Superclass,
32723280
LayoutConstraint Layout,
32733281
GenericEnvironment *Environment)
32743282
: ArchetypeType(TypeKind::NestedArchetype, Ctx,
3275-
Parent->getRecursiveProperties(),
3276-
InterfaceType, ConformsTo, Superclass, Layout, Environment),
3277-
Parent(Parent)
3283+
Environment->mapTypeIntoContext(
3284+
InterfaceType->getRootGenericParam())
3285+
->getRecursiveProperties(),
3286+
InterfaceType, ConformsTo, Superclass, Layout, Environment)
32783287
{
32793288
}
32803289

@@ -3590,7 +3599,6 @@ operator()(CanType maybeOpaqueType, Type replacementType,
35903599

35913600
CanNestedArchetypeType NestedArchetypeType::getNew(
35923601
const ASTContext &Ctx,
3593-
ArchetypeType *Parent,
35943602
DependentMemberType *InterfaceType,
35953603
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
35963604
Type Superclass,
@@ -3608,7 +3616,7 @@ CanNestedArchetypeType NestedArchetypeType::getNew(
36083616
alignof(NestedArchetypeType), arena);
36093617

36103618
return CanNestedArchetypeType(::new (mem) NestedArchetypeType(
3611-
Ctx, Parent, InterfaceType, ConformsTo, Superclass, Layout, Environment));
3619+
Ctx, InterfaceType, ConformsTo, Superclass, Layout, Environment));
36123620
}
36133621

36143622
CanPrimaryArchetypeType

0 commit comments

Comments
 (0)