AST: Remove ArchetypeType::getSelfProtocol()

Now that the last usages have been removed, we no longer need
special handling for the 'Self' archetype of a protocol.
This commit is contained in:
Slava Pestov
2016-11-18 23:52:48 -08:00
parent 0948506cc2
commit 2294e33604
7 changed files with 27 additions and 63 deletions

View File

@@ -218,7 +218,6 @@ public:
private: private:
PotentialArchetype *addGenericParameter(GenericTypeParamType *GenericParam, PotentialArchetype *addGenericParameter(GenericTypeParamType *GenericParam,
ProtocolDecl *RootProtocol,
Identifier ParamName); Identifier ParamName);
public: public:
@@ -357,9 +356,6 @@ class ArchetypeBuilder::PotentialArchetype {
/// archetype corresponds. /// archetype corresponds.
llvm::PointerUnion<PotentialArchetype*, GenericTypeParamType*> ParentOrParam; llvm::PointerUnion<PotentialArchetype*, GenericTypeParamType*> ParentOrParam;
/// The root protocol with which this potential archetype is associated.
ProtocolDecl *RootProtocol = nullptr;
/// \brief The name of this potential archetype or, for an /// \brief The name of this potential archetype or, for an
/// associated type, the declaration of the associated type to which /// associated type, the declaration of the associated type to which
/// this potential archetype has been resolved. Or, for a type alias, /// this potential archetype has been resolved. Or, for a type alias,
@@ -460,9 +456,8 @@ class ArchetypeBuilder::PotentialArchetype {
/// \brief Construct a new potential archetype for a generic parameter. /// \brief Construct a new potential archetype for a generic parameter.
PotentialArchetype(GenericTypeParamType *GenericParam, PotentialArchetype(GenericTypeParamType *GenericParam,
ProtocolDecl *RootProtocol,
Identifier Name) Identifier Name)
: ParentOrParam(GenericParam), RootProtocol(RootProtocol), : ParentOrParam(GenericParam),
NameOrAssociatedType(Name), Representative(this), IsRecursive(false), NameOrAssociatedType(Name), Representative(this), IsRecursive(false),
Invalid(false), SubstitutingConcreteType(false), Invalid(false), SubstitutingConcreteType(false),
RecursiveConcreteType(false), RecursiveSuperclassType(false), RecursiveConcreteType(false), RecursiveSuperclassType(false),

View File

@@ -3782,9 +3782,6 @@ class ArchetypeType final : public SubstitutableType,
friend TrailingObjects; friend TrailingObjects;
public: public:
typedef llvm::PointerUnion<AssociatedTypeDecl *, ProtocolDecl *>
AssocTypeOrProtocolType;
/// A nested type. Either a dependent associated archetype, or a concrete /// A nested type. Either a dependent associated archetype, or a concrete
/// type (which may be a bound archetype from an outer context). /// type (which may be a bound archetype from an outer context).
class NestedType { class NestedType {
@@ -3831,7 +3828,7 @@ private:
Type Superclass; Type Superclass;
llvm::PointerUnion<ArchetypeType *, TypeBase *> ParentOrOpened; llvm::PointerUnion<ArchetypeType *, TypeBase *> ParentOrOpened;
AssocTypeOrProtocolType AssocTypeOrProto; AssociatedTypeDecl *AssocType;
Identifier Name; Identifier Name;
unsigned isRecursive: 1; unsigned isRecursive: 1;
MutableArrayRef<std::pair<Identifier, NestedType>> NestedTypes; MutableArrayRef<std::pair<Identifier, NestedType>> NestedTypes;
@@ -3851,7 +3848,7 @@ public:
/// The ConformsTo array will be copied into the ASTContext by this routine. /// The ConformsTo array will be copied into the ASTContext by this routine.
static CanTypeWrapper<ArchetypeType> static CanTypeWrapper<ArchetypeType>
getNew(const ASTContext &Ctx, ArchetypeType *Parent, getNew(const ASTContext &Ctx, ArchetypeType *Parent,
AssocTypeOrProtocolType AssocTypeOrProto, AssociatedTypeDecl *AssocType,
Identifier Name, ArrayRef<Type> ConformsTo, Identifier Name, ArrayRef<Type> ConformsTo,
Type Superclass, Type Superclass,
bool isRecursive = false); bool isRecursive = false);
@@ -3862,7 +3859,7 @@ public:
/// by this routine. /// by this routine.
static CanTypeWrapper<ArchetypeType> static CanTypeWrapper<ArchetypeType>
getNew(const ASTContext &Ctx, ArchetypeType *Parent, getNew(const ASTContext &Ctx, ArchetypeType *Parent,
AssocTypeOrProtocolType AssocTypeOrProto, AssociatedTypeDecl *AssocType,
Identifier Name, Identifier Name,
SmallVectorImpl<ProtocolDecl *> &ConformsTo, SmallVectorImpl<ProtocolDecl *> &ConformsTo,
Type Superclass, Type Superclass,
@@ -3910,15 +3907,9 @@ public:
/// be a member of one of the protocols to which the parent archetype /// be a member of one of the protocols to which the parent archetype
/// conforms. /// conforms.
AssociatedTypeDecl *getAssocType() const { AssociatedTypeDecl *getAssocType() const {
return AssocTypeOrProto.dyn_cast<AssociatedTypeDecl *>(); return AssocType;
} }
/// Retrieve the protocol for which this archetype describes the 'Self'
/// parameter.
ProtocolDecl *getSelfProtocol() const {
return AssocTypeOrProto.dyn_cast<ProtocolDecl *>();
}
/// getConformsTo - Retrieve the set of protocols to which this substitutable /// getConformsTo - Retrieve the set of protocols to which this substitutable
/// type shall conform. /// type shall conform.
ArrayRef<ProtocolDecl *> getConformsTo() const { return ConformsTo; } ArrayRef<ProtocolDecl *> getConformsTo() const { return ConformsTo; }
@@ -3936,12 +3927,6 @@ public:
return !getConformsTo().empty() || getSuperclass(); return !getConformsTo().empty() || getSuperclass();
} }
/// Retrieve either the associated type or the protocol to which this
/// associated type corresponds.
AssocTypeOrProtocolType getAssocTypeOrProtocol() const {
return AssocTypeOrProto;
}
/// \brief Retrieve the nested type with the given name. /// \brief Retrieve the nested type with the given name.
NestedType getNestedType(Identifier Name) const; NestedType getNestedType(Identifier Name) const;
@@ -3998,14 +3983,14 @@ public:
private: private:
ArchetypeType(const ASTContext &Ctx, ArchetypeType *Parent, ArchetypeType(const ASTContext &Ctx, ArchetypeType *Parent,
AssocTypeOrProtocolType AssocTypeOrProto, AssociatedTypeDecl *AssocType,
Identifier Name, ArrayRef<ProtocolDecl *> ConformsTo, Identifier Name, ArrayRef<ProtocolDecl *> ConformsTo,
Type Superclass, Type Superclass,
bool isRecursive = false) bool isRecursive = false)
: SubstitutableType(TypeKind::Archetype, &Ctx, : SubstitutableType(TypeKind::Archetype, &Ctx,
RecursiveTypeProperties::HasArchetype), RecursiveTypeProperties::HasArchetype),
ConformsTo(ConformsTo), Superclass(Superclass), ParentOrOpened(Parent), ConformsTo(ConformsTo), Superclass(Superclass), ParentOrOpened(Parent),
AssocTypeOrProto(AssocTypeOrProto), Name(Name), AssocType(AssocType), Name(Name),
isRecursive(isRecursive) { } isRecursive(isRecursive) { }
ArchetypeType(const ASTContext &Ctx, ArchetypeType(const ASTContext &Ctx,
@@ -4018,6 +4003,7 @@ private:
RecursiveTypeProperties::HasOpenedExistential)), RecursiveTypeProperties::HasOpenedExistential)),
ConformsTo(ConformsTo), Superclass(Superclass), ConformsTo(ConformsTo), Superclass(Superclass),
ParentOrOpened(Existential.getPointer()), ParentOrOpened(Existential.getPointer()),
AssocType(nullptr),
isRecursive(isRecursive) { } isRecursive(isRecursive) { }
}; };
BEGIN_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType) BEGIN_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)

View File

@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
/// in source control, you should also update the comment to briefly /// in source control, you should also update the comment to briefly
/// describe what change you made. The content of this comment isn't important; /// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format. /// it just ensures a conflict if two people change the module format.
const uint16_t VERSION_MINOR = 283; // Last change: witness markers removed const uint16_t VERSION_MINOR = 284; // Last change: Self archetype protocol removed
using DeclID = PointerEmbeddedInt<unsigned, 31>; using DeclID = PointerEmbeddedInt<unsigned, 31>;
using DeclIDField = BCFixed<31>; using DeclIDField = BCFixed<31>;
@@ -626,7 +626,7 @@ namespace decls_block {
ARCHETYPE_TYPE, ARCHETYPE_TYPE,
IdentifierIDField, // name IdentifierIDField, // name
TypeIDField, // index if primary, parent if non-primary TypeIDField, // index if primary, parent if non-primary
DeclIDField, // associated type or protocol decl DeclIDField, // associated type decl
TypeIDField, // superclass TypeIDField, // superclass
BCArray<DeclIDField> // conformances BCArray<DeclIDField> // conformances
// Trailed by the nested types record. // Trailed by the nested types record.

View File

@@ -614,13 +614,12 @@ ArchetypeBuilder::PotentialArchetype::getType(ArchetypeBuilder &builder) {
return representative->ArchetypeOrConcreteType; return representative->ArchetypeOrConcreteType;
} }
ArchetypeType::AssocTypeOrProtocolType assocTypeOrProto = RootProtocol; AssociatedTypeDecl *assocType = nullptr;
// Allocate a new archetype. // Allocate a new archetype.
ArchetypeType *ParentArchetype = nullptr; ArchetypeType *ParentArchetype = nullptr;
auto &mod = builder.getModule(); auto &mod = builder.getModule();
if (auto parent = getParent()) { if (auto parent = getParent()) {
assert(assocTypeOrProto.isNull() &&
"root protocol type given for non-root archetype");
auto parentTy = parent->getType(builder); auto parentTy = parent->getType(builder);
if (!parentTy) if (!parentTy)
return NestedType::forConcreteType( return NestedType::forConcreteType(
@@ -671,7 +670,7 @@ ArchetypeBuilder::PotentialArchetype::getType(ArchetypeBuilder &builder) {
} }
} }
assocTypeOrProto = getResolvedAssociatedType(); assocType = getResolvedAssociatedType();
} }
// If we ended up building our parent archetype, then we'll have // If we ended up building our parent archetype, then we'll have
@@ -714,7 +713,7 @@ ArchetypeBuilder::PotentialArchetype::getType(ArchetypeBuilder &builder) {
auto arch auto arch
= ArchetypeType::getNew(builder.getASTContext(), ParentArchetype, = ArchetypeType::getNew(builder.getASTContext(), ParentArchetype,
assocTypeOrProto, getName(), Protos, assocType, getName(), Protos,
superclass, isRecursive()); superclass, isRecursive());
representative->ArchetypeOrConcreteType = NestedType::forArchetype(arch); representative->ArchetypeOrConcreteType = NestedType::forArchetype(arch);
@@ -868,7 +867,6 @@ auto ArchetypeBuilder::resolveArchetype(Type type) -> PotentialArchetype * {
} }
auto ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam, auto ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam,
ProtocolDecl *RootProtocol,
Identifier ParamName) Identifier ParamName)
-> PotentialArchetype * -> PotentialArchetype *
{ {
@@ -876,21 +874,15 @@ auto ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam,
// Create a potential archetype for this type parameter. // Create a potential archetype for this type parameter.
assert(!Impl->PotentialArchetypes[Key]); assert(!Impl->PotentialArchetypes[Key]);
auto PA = new PotentialArchetype(GenericParam, RootProtocol, ParamName); auto PA = new PotentialArchetype(GenericParam, ParamName);
Impl->PotentialArchetypes[Key] = PA; Impl->PotentialArchetypes[Key] = PA;
return PA; return PA;
} }
void ArchetypeBuilder::addGenericParameter(GenericTypeParamDecl *GenericParam) { void ArchetypeBuilder::addGenericParameter(GenericTypeParamDecl *GenericParam) {
ProtocolDecl *RootProtocol = dyn_cast<ProtocolDecl>(GenericParam->getDeclContext());
if (!RootProtocol) {
if (auto Ext = dyn_cast<ExtensionDecl>(GenericParam->getDeclContext()))
RootProtocol = dyn_cast_or_null<ProtocolDecl>(Ext->getExtendedType()->getAnyNominal());
}
addGenericParameter( addGenericParameter(
GenericParam->getDeclaredType()->castTo<GenericTypeParamType>(), GenericParam->getDeclaredType()->castTo<GenericTypeParamType>(),
RootProtocol,
GenericParam->getName()); GenericParam->getName());
} }
@@ -912,7 +904,7 @@ void ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam) {
if (name.str().startswith("$")) if (name.str().startswith("$"))
name = Context.getIdentifier(name.str().slice(1, name.str().size())); name = Context.getIdentifier(name.str().slice(1, name.str().size()));
addGenericParameter(GenericParam, nullptr, name); addGenericParameter(GenericParam, name);
} }
bool ArchetypeBuilder::addConformanceRequirement(PotentialArchetype *PAT, bool ArchetypeBuilder::addConformanceRequirement(PotentialArchetype *PAT,

View File

@@ -2492,7 +2492,7 @@ Type TupleType::getVarArgsBaseType() const {
CanArchetypeType ArchetypeType::getNew(const ASTContext &Ctx, CanArchetypeType ArchetypeType::getNew(const ASTContext &Ctx,
ArchetypeType *Parent, ArchetypeType *Parent,
AssocTypeOrProtocolType AssocTypeOrProto, AssociatedTypeDecl *AssocType,
Identifier Name, Identifier Name,
ArrayRef<Type> ConformsTo, ArrayRef<Type> ConformsTo,
Type Superclass, Type Superclass,
@@ -2506,14 +2506,14 @@ CanArchetypeType ArchetypeType::getNew(const ASTContext &Ctx,
auto arena = AllocationArena::Permanent; auto arena = AllocationArena::Permanent;
return CanArchetypeType( return CanArchetypeType(
new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocTypeOrProto, Name, new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocType, Name,
Ctx.AllocateCopy(ConformsToProtos), Ctx.AllocateCopy(ConformsToProtos),
Superclass, isRecursive)); Superclass, isRecursive));
} }
CanArchetypeType CanArchetypeType
ArchetypeType::getNew(const ASTContext &Ctx, ArchetypeType *Parent, ArchetypeType::getNew(const ASTContext &Ctx, ArchetypeType *Parent,
AssocTypeOrProtocolType AssocTypeOrProto, AssociatedTypeDecl *AssocType,
Identifier Name, Identifier Name,
SmallVectorImpl<ProtocolDecl *> &ConformsTo, SmallVectorImpl<ProtocolDecl *> &ConformsTo,
Type Superclass, bool isRecursive) { Type Superclass, bool isRecursive) {
@@ -2522,7 +2522,7 @@ ArchetypeType::getNew(const ASTContext &Ctx, ArchetypeType *Parent,
auto arena = AllocationArena::Permanent; auto arena = AllocationArena::Permanent;
return CanArchetypeType( return CanArchetypeType(
new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocTypeOrProto, Name, new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocType, Name,
Ctx.AllocateCopy(ConformsTo), Ctx.AllocateCopy(ConformsTo),
Superclass, isRecursive)); Superclass, isRecursive));
} }

View File

@@ -3609,12 +3609,12 @@ Type ModuleFile::getType(TypeID TID) {
case decls_block::ARCHETYPE_TYPE: { case decls_block::ARCHETYPE_TYPE: {
IdentifierID nameID; IdentifierID nameID;
TypeID parentID; TypeID parentID;
DeclID assocTypeOrProtoID; DeclID assocTypeID;
TypeID superclassID; TypeID superclassID;
ArrayRef<uint64_t> rawConformanceIDs; ArrayRef<uint64_t> rawConformanceIDs;
decls_block::ArchetypeTypeLayout::readRecord(scratch, nameID, parentID, decls_block::ArchetypeTypeLayout::readRecord(scratch, nameID, parentID,
assocTypeOrProtoID, assocTypeID,
superclassID, superclassID,
rawConformanceIDs); rawConformanceIDs);
@@ -3625,13 +3625,8 @@ Type ModuleFile::getType(TypeID TID) {
if (auto parentType = getType(parentID)) if (auto parentType = getType(parentID))
parent = parentType->castTo<ArchetypeType>(); parent = parentType->castTo<ArchetypeType>();
ArchetypeType::AssocTypeOrProtocolType assocTypeOrProto; auto assocTypeDecl = dyn_cast_or_null<AssociatedTypeDecl>(
auto assocTypeOrProtoDecl = getDecl(assocTypeOrProtoID); getDecl(assocTypeID));
if (auto assocType
= dyn_cast_or_null<AssociatedTypeDecl>(assocTypeOrProtoDecl))
assocTypeOrProto = assocType;
else
assocTypeOrProto = cast_or_null<ProtocolDecl>(assocTypeOrProtoDecl);
superclass = getType(superclassID); superclass = getType(superclassID);
@@ -3642,7 +3637,7 @@ Type ModuleFile::getType(TypeID TID) {
if (typeOrOffset.isComplete()) if (typeOrOffset.isComplete())
break; break;
auto archetype = ArchetypeType::getNew(ctx, parent, assocTypeOrProto, auto archetype = ArchetypeType::getNew(ctx, parent, assocTypeDecl,
getIdentifier(nameID), conformances, getIdentifier(nameID), conformances,
superclass, false); superclass, false);
typeOrOffset = archetype; typeOrOffset = archetype;

View File

@@ -2962,17 +2962,13 @@ void Serializer::writeType(Type ty) {
for (auto proto : archetypeTy->getConformsTo()) for (auto proto : archetypeTy->getConformsTo())
conformances.push_back(addDeclRef(proto)); conformances.push_back(addDeclRef(proto));
DeclID assocTypeOrProtoID; DeclID assocTypeID = addDeclRef(archetypeTy->getAssocType());
if (auto assocType = archetypeTy->getAssocType())
assocTypeOrProtoID = addDeclRef(assocType);
else
assocTypeOrProtoID = addDeclRef(archetypeTy->getSelfProtocol());
unsigned abbrCode = DeclTypeAbbrCodes[ArchetypeTypeLayout::Code]; unsigned abbrCode = DeclTypeAbbrCodes[ArchetypeTypeLayout::Code];
ArchetypeTypeLayout::emitRecord(Out, ScratchRecord, abbrCode, ArchetypeTypeLayout::emitRecord(Out, ScratchRecord, abbrCode,
addIdentifierRef(archetypeTy->getName()), addIdentifierRef(archetypeTy->getName()),
parentID, parentID,
assocTypeOrProtoID, assocTypeID,
addTypeRef(archetypeTy->getSuperclass()), addTypeRef(archetypeTy->getSuperclass()),
conformances); conformances);