Skip to content

[AST] Clean up SubstitutableType #5805

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions include/swift/AST/TypeNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,9 @@ ALWAYS_CANONICAL_TYPE(Module, Type)
TYPE(DynamicSelf, Type)
ABSTRACT_TYPE(Substitutable, Type)
ALWAYS_CANONICAL_TYPE(Archetype, SubstitutableType)
ABSTRACT_TYPE(AbstractTypeParam, SubstitutableType)
TYPE(GenericTypeParam, AbstractTypeParamType)
SUGARED_TYPE(AssociatedType, AbstractTypeParamType)
TYPE_RANGE(AbstractTypeParam, GenericTypeParam, AssociatedType)
TYPE_RANGE(Substitutable, Archetype, AssociatedType)
TYPE(GenericTypeParam, SubstitutableType)
TYPE_RANGE(Substitutable, Archetype, GenericTypeParam)
SUGARED_TYPE(AssociatedType, Type)
SUGARED_TYPE(Substituted, Type)
TYPE(DependentMember, Type)
ABSTRACT_TYPE(AnyFunction, Type)
Expand Down
63 changes: 10 additions & 53 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3763,13 +3763,6 @@ class SubstitutableType : public TypeBase {
: TypeBase(K, C, properties) { }

public:
/// \brief Retrieve the name of this type.
Identifier getName() const;

/// \brief Retrieve the parent of this type, or null if this is a
/// primary type.
SubstitutableType *getParent() const;

// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
return T->getKind() >= TypeKind::First_SubstitutableType &&
Expand Down Expand Up @@ -4033,29 +4026,10 @@ CanArchetypeType getParent() const {
}
END_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)

/// Abstract class used to describe the type of a generic type parameter
/// or associated type.
///
/// \sa AbstractTypeParamDecl
class AbstractTypeParamType : public SubstitutableType {
protected:
AbstractTypeParamType(TypeKind kind, const ASTContext *ctx,
RecursiveTypeProperties properties)
: SubstitutableType(kind, ctx, properties) { }

public:
// Implement isa/cast/dyncast/etc.
static bool classof(const TypeBase *T) {
return T->getKind() >= TypeKind::First_AbstractTypeParamType &&
T->getKind() <= TypeKind::Last_AbstractTypeParamType;
}
};
DEFINE_EMPTY_CAN_TYPE_WRAPPER(AbstractTypeParamType, SubstitutableType)

/// Describes the type of a generic parameter.
///
/// \sa GenericTypeParamDecl
class GenericTypeParamType : public AbstractTypeParamType {
class GenericTypeParamType : public SubstitutableType {
using DepthIndexTy = llvm::PointerEmbeddedInt<unsigned, 31>;

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

explicit GenericTypeParamType(GenericTypeParamDecl *param)
: AbstractTypeParamType(TypeKind::GenericTypeParam, nullptr,
RecursiveTypeProperties::HasTypeParameter),
: SubstitutableType(TypeKind::GenericTypeParam, nullptr,
RecursiveTypeProperties::HasTypeParameter),
ParamOrDepthIndex(param) { }

explicit GenericTypeParamType(unsigned depth,
unsigned index,
const ASTContext &ctx)
: AbstractTypeParamType(TypeKind::GenericTypeParam, &ctx,
RecursiveTypeProperties::HasTypeParameter),
: SubstitutableType(TypeKind::GenericTypeParam, &ctx,
RecursiveTypeProperties::HasTypeParameter),
ParamOrDepthIndex(depth << 16 | index) { }
};
BEGIN_CAN_TYPE_WRAPPER(GenericTypeParamType, AbstractTypeParamType)
BEGIN_CAN_TYPE_WRAPPER(GenericTypeParamType, SubstitutableType)
static CanGenericTypeParamType get(unsigned depth, unsigned index,
const ASTContext &C) {
return CanGenericTypeParamType(GenericTypeParamType::get(depth, index, C));
}
END_CAN_TYPE_WRAPPER(GenericTypeParamType, AbstractTypeParamType)
END_CAN_TYPE_WRAPPER(GenericTypeParamType, SubstitutableType)

/// Describes the type of an associated type.
///
/// \sa AssociatedTypeDecl
class AssociatedTypeType : public AbstractTypeParamType {
class AssociatedTypeType : public TypeBase {
/// The generic type parameter.
AssociatedTypeDecl *AssocType;

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

AssociatedTypeType(AssociatedTypeDecl *assocType)
: AbstractTypeParamType(TypeKind::AssociatedType, nullptr,
RecursiveTypeProperties()),
: TypeBase(TypeKind::AssociatedType, nullptr, RecursiveTypeProperties()),
AssocType(assocType) { }
};
DEFINE_EMPTY_CAN_TYPE_WRAPPER(AssociatedTypeType, AbstractTypeParamType)
DEFINE_EMPTY_CAN_TYPE_WRAPPER(AssociatedTypeType, Type)

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

inline Identifier SubstitutableType::getName() const {
if (auto Archetype = dyn_cast<ArchetypeType>(this))
return Archetype->getName();
if (auto GenericParam = dyn_cast<GenericTypeParamType>(this))
return GenericParam->getName();

llvm_unreachable("Not a substitutable type");
}

inline SubstitutableType *SubstitutableType::getParent() const {
if (auto Archetype = dyn_cast<ArchetypeType>(this))
return Archetype->getParent();

return nullptr;
}

inline CanType Type::getCanonicalTypeOrNull() const {
return isNull() ? CanType() : getPointer()->getCanonicalType();
}
Expand Down
4 changes: 2 additions & 2 deletions include/swift/SIL/AbstractionPattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ class AbstractionPattern {
auto type = getType();
if (isa<ArchetypeType>(type) ||
isa<DependentMemberType>(type) ||
isa<AbstractTypeParamType>(type)) {
isa<GenericTypeParamType>(type)) {
return true;
}
return false;
Expand Down Expand Up @@ -636,7 +636,7 @@ class AbstractionPattern {
if (auto archetype = dyn_cast<ArchetypeType>(type))
return archetype->requiresClass();
else if (isa<DependentMemberType>(type) ||
isa<AbstractTypeParamType>(type)) {
isa<GenericTypeParamType>(type)) {
assert(GenericSig &&
"Dependent type in pattern without generic signature?");
return GenericSig->requiresClass(type, module);
Expand Down
15 changes: 8 additions & 7 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2897,9 +2897,13 @@ static Type substType(
return SubstitutedType::get(type, known->second,
type->getASTContext());

// For archetypes, we can substitute the parent (if present).
auto archetype = substOrig->getAs<ArchetypeType>();
if (!archetype) return type;

// If we don't have a substitution for this type and it doesn't have a
// parent, then we're not substituting it.
auto parent = substOrig->getParent();
auto parent = archetype->getParent();
if (!parent)
return type;

Expand All @@ -2911,13 +2915,10 @@ static Type substType(
return type;

// Get the associated type reference from a child archetype.
AssociatedTypeDecl *assocType = nullptr;
if (auto archetype = substOrig->getAs<ArchetypeType>()) {
assocType = archetype->getAssocType();
}

AssociatedTypeDecl *assocType = archetype->getAssocType();

return getMemberForBaseType(conformances, parent, substParent,
assocType, substOrig->getName(),
assocType, archetype->getName(),
options);
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/AST/TypeWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Traversal : public TypeVisitor<Traversal, bool>
return doIt(ty->getSelfType());
}
bool visitSubstitutableType(SubstitutableType *ty) { return false; }
bool visitAssociatedTypeType(AssociatedTypeType *ty) { return false; }

bool visitSubstitutedType(SubstitutedType *ty) {
if (Walker.shouldVisitOriginalSubstitutedType())
Expand Down