Skip to content

Commit 0773731

Browse files
authored
Merge pull request swiftlang#5805 from DougGregor/substitutable-type-cleanup
2 parents 268aaee + 43d6321 commit 0773731

File tree

5 files changed

+24
-67
lines changed

5 files changed

+24
-67
lines changed

include/swift/AST/TypeNodes.def

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ ALWAYS_CANONICAL_TYPE(Module, Type)
116116
TYPE(DynamicSelf, Type)
117117
ABSTRACT_TYPE(Substitutable, Type)
118118
ALWAYS_CANONICAL_TYPE(Archetype, SubstitutableType)
119-
ABSTRACT_TYPE(AbstractTypeParam, SubstitutableType)
120-
TYPE(GenericTypeParam, AbstractTypeParamType)
121-
SUGARED_TYPE(AssociatedType, AbstractTypeParamType)
122-
TYPE_RANGE(AbstractTypeParam, GenericTypeParam, AssociatedType)
123-
TYPE_RANGE(Substitutable, Archetype, AssociatedType)
119+
TYPE(GenericTypeParam, SubstitutableType)
120+
TYPE_RANGE(Substitutable, Archetype, GenericTypeParam)
121+
SUGARED_TYPE(AssociatedType, Type)
124122
SUGARED_TYPE(Substituted, Type)
125123
TYPE(DependentMember, Type)
126124
ABSTRACT_TYPE(AnyFunction, Type)

include/swift/AST/Types.h

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,13 +3763,6 @@ class SubstitutableType : public TypeBase {
37633763
: TypeBase(K, C, properties) { }
37643764

37653765
public:
3766-
/// \brief Retrieve the name of this type.
3767-
Identifier getName() const;
3768-
3769-
/// \brief Retrieve the parent of this type, or null if this is a
3770-
/// primary type.
3771-
SubstitutableType *getParent() const;
3772-
37733766
// Implement isa/cast/dyncast/etc.
37743767
static bool classof(const TypeBase *T) {
37753768
return T->getKind() >= TypeKind::First_SubstitutableType &&
@@ -4033,29 +4026,10 @@ CanArchetypeType getParent() const {
40334026
}
40344027
END_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)
40354028

4036-
/// Abstract class used to describe the type of a generic type parameter
4037-
/// or associated type.
4038-
///
4039-
/// \sa AbstractTypeParamDecl
4040-
class AbstractTypeParamType : public SubstitutableType {
4041-
protected:
4042-
AbstractTypeParamType(TypeKind kind, const ASTContext *ctx,
4043-
RecursiveTypeProperties properties)
4044-
: SubstitutableType(kind, ctx, properties) { }
4045-
4046-
public:
4047-
// Implement isa/cast/dyncast/etc.
4048-
static bool classof(const TypeBase *T) {
4049-
return T->getKind() >= TypeKind::First_AbstractTypeParamType &&
4050-
T->getKind() <= TypeKind::Last_AbstractTypeParamType;
4051-
}
4052-
};
4053-
DEFINE_EMPTY_CAN_TYPE_WRAPPER(AbstractTypeParamType, SubstitutableType)
4054-
40554029
/// Describes the type of a generic parameter.
40564030
///
40574031
/// \sa GenericTypeParamDecl
4058-
class GenericTypeParamType : public AbstractTypeParamType {
4032+
class GenericTypeParamType : public SubstitutableType {
40594033
using DepthIndexTy = llvm::PointerEmbeddedInt<unsigned, 31>;
40604034

40614035
/// The generic type parameter or depth/index.
@@ -4108,28 +4082,28 @@ class GenericTypeParamType : public AbstractTypeParamType {
41084082
friend class GenericTypeParamDecl;
41094083

41104084
explicit GenericTypeParamType(GenericTypeParamDecl *param)
4111-
: AbstractTypeParamType(TypeKind::GenericTypeParam, nullptr,
4112-
RecursiveTypeProperties::HasTypeParameter),
4085+
: SubstitutableType(TypeKind::GenericTypeParam, nullptr,
4086+
RecursiveTypeProperties::HasTypeParameter),
41134087
ParamOrDepthIndex(param) { }
41144088

41154089
explicit GenericTypeParamType(unsigned depth,
41164090
unsigned index,
41174091
const ASTContext &ctx)
4118-
: AbstractTypeParamType(TypeKind::GenericTypeParam, &ctx,
4119-
RecursiveTypeProperties::HasTypeParameter),
4092+
: SubstitutableType(TypeKind::GenericTypeParam, &ctx,
4093+
RecursiveTypeProperties::HasTypeParameter),
41204094
ParamOrDepthIndex(depth << 16 | index) { }
41214095
};
4122-
BEGIN_CAN_TYPE_WRAPPER(GenericTypeParamType, AbstractTypeParamType)
4096+
BEGIN_CAN_TYPE_WRAPPER(GenericTypeParamType, SubstitutableType)
41234097
static CanGenericTypeParamType get(unsigned depth, unsigned index,
41244098
const ASTContext &C) {
41254099
return CanGenericTypeParamType(GenericTypeParamType::get(depth, index, C));
41264100
}
4127-
END_CAN_TYPE_WRAPPER(GenericTypeParamType, AbstractTypeParamType)
4101+
END_CAN_TYPE_WRAPPER(GenericTypeParamType, SubstitutableType)
41284102

41294103
/// Describes the type of an associated type.
41304104
///
41314105
/// \sa AssociatedTypeDecl
4132-
class AssociatedTypeType : public AbstractTypeParamType {
4106+
class AssociatedTypeType : public TypeBase {
41334107
/// The generic type parameter.
41344108
AssociatedTypeDecl *AssocType;
41354109

@@ -4151,11 +4125,10 @@ class AssociatedTypeType : public AbstractTypeParamType {
41514125
// These aren't classified as dependent for some reason.
41524126

41534127
AssociatedTypeType(AssociatedTypeDecl *assocType)
4154-
: AbstractTypeParamType(TypeKind::AssociatedType, nullptr,
4155-
RecursiveTypeProperties()),
4128+
: TypeBase(TypeKind::AssociatedType, nullptr, RecursiveTypeProperties()),
41564129
AssocType(assocType) { }
41574130
};
4158-
DEFINE_EMPTY_CAN_TYPE_WRAPPER(AssociatedTypeType, AbstractTypeParamType)
4131+
DEFINE_EMPTY_CAN_TYPE_WRAPPER(AssociatedTypeType, Type)
41594132

41604133
/// SubstitutedType - A type that has been substituted for some other type,
41614134
/// which implies that the replacement type meets all of the requirements of
@@ -4669,22 +4642,6 @@ ParameterTypeFlags::fromParameterType(Type paramTy, bool isVariadic) {
46694642
return {isVariadic, autoclosure, escaping};
46704643
}
46714644

4672-
inline Identifier SubstitutableType::getName() const {
4673-
if (auto Archetype = dyn_cast<ArchetypeType>(this))
4674-
return Archetype->getName();
4675-
if (auto GenericParam = dyn_cast<GenericTypeParamType>(this))
4676-
return GenericParam->getName();
4677-
4678-
llvm_unreachable("Not a substitutable type");
4679-
}
4680-
4681-
inline SubstitutableType *SubstitutableType::getParent() const {
4682-
if (auto Archetype = dyn_cast<ArchetypeType>(this))
4683-
return Archetype->getParent();
4684-
4685-
return nullptr;
4686-
}
4687-
46884645
inline CanType Type::getCanonicalTypeOrNull() const {
46894646
return isNull() ? CanType() : getPointer()->getCanonicalType();
46904647
}

include/swift/SIL/AbstractionPattern.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ class AbstractionPattern {
607607
auto type = getType();
608608
if (isa<ArchetypeType>(type) ||
609609
isa<DependentMemberType>(type) ||
610-
isa<AbstractTypeParamType>(type)) {
610+
isa<GenericTypeParamType>(type)) {
611611
return true;
612612
}
613613
return false;
@@ -636,7 +636,7 @@ class AbstractionPattern {
636636
if (auto archetype = dyn_cast<ArchetypeType>(type))
637637
return archetype->requiresClass();
638638
else if (isa<DependentMemberType>(type) ||
639-
isa<AbstractTypeParamType>(type)) {
639+
isa<GenericTypeParamType>(type)) {
640640
assert(GenericSig &&
641641
"Dependent type in pattern without generic signature?");
642642
return GenericSig->requiresClass(type, module);

lib/AST/Type.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,9 +2897,13 @@ static Type substType(
28972897
return SubstitutedType::get(type, known->second,
28982898
type->getASTContext());
28992899

2900+
// For archetypes, we can substitute the parent (if present).
2901+
auto archetype = substOrig->getAs<ArchetypeType>();
2902+
if (!archetype) return type;
2903+
29002904
// If we don't have a substitution for this type and it doesn't have a
29012905
// parent, then we're not substituting it.
2902-
auto parent = substOrig->getParent();
2906+
auto parent = archetype->getParent();
29032907
if (!parent)
29042908
return type;
29052909

@@ -2911,13 +2915,10 @@ static Type substType(
29112915
return type;
29122916

29132917
// Get the associated type reference from a child archetype.
2914-
AssociatedTypeDecl *assocType = nullptr;
2915-
if (auto archetype = substOrig->getAs<ArchetypeType>()) {
2916-
assocType = archetype->getAssocType();
2917-
}
2918-
2918+
AssociatedTypeDecl *assocType = archetype->getAssocType();
2919+
29192920
return getMemberForBaseType(conformances, parent, substParent,
2920-
assocType, substOrig->getName(),
2921+
assocType, archetype->getName(),
29212922
options);
29222923
});
29232924
}

lib/AST/TypeWalker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Traversal : public TypeVisitor<Traversal, bool>
6767
return doIt(ty->getSelfType());
6868
}
6969
bool visitSubstitutableType(SubstitutableType *ty) { return false; }
70+
bool visitAssociatedTypeType(AssociatedTypeType *ty) { return false; }
7071

7172
bool visitSubstitutedType(SubstitutedType *ty) {
7273
if (Walker.shouldVisitOriginalSubstitutedType())

0 commit comments

Comments
 (0)