Skip to content

Commit 2294e33

Browse files
committed
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.
1 parent 0948506 commit 2294e33

File tree

7 files changed

+27
-63
lines changed

7 files changed

+27
-63
lines changed

include/swift/AST/ArchetypeBuilder.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ class ArchetypeBuilder {
218218

219219
private:
220220
PotentialArchetype *addGenericParameter(GenericTypeParamType *GenericParam,
221-
ProtocolDecl *RootProtocol,
222221
Identifier ParamName);
223222

224223
public:
@@ -357,9 +356,6 @@ class ArchetypeBuilder::PotentialArchetype {
357356
/// archetype corresponds.
358357
llvm::PointerUnion<PotentialArchetype*, GenericTypeParamType*> ParentOrParam;
359358

360-
/// The root protocol with which this potential archetype is associated.
361-
ProtocolDecl *RootProtocol = nullptr;
362-
363359
/// \brief The name of this potential archetype or, for an
364360
/// associated type, the declaration of the associated type to which
365361
/// this potential archetype has been resolved. Or, for a type alias,
@@ -460,9 +456,8 @@ class ArchetypeBuilder::PotentialArchetype {
460456

461457
/// \brief Construct a new potential archetype for a generic parameter.
462458
PotentialArchetype(GenericTypeParamType *GenericParam,
463-
ProtocolDecl *RootProtocol,
464459
Identifier Name)
465-
: ParentOrParam(GenericParam), RootProtocol(RootProtocol),
460+
: ParentOrParam(GenericParam),
466461
NameOrAssociatedType(Name), Representative(this), IsRecursive(false),
467462
Invalid(false), SubstitutingConcreteType(false),
468463
RecursiveConcreteType(false), RecursiveSuperclassType(false),

include/swift/AST/Types.h

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3782,9 +3782,6 @@ class ArchetypeType final : public SubstitutableType,
37823782
friend TrailingObjects;
37833783

37843784
public:
3785-
typedef llvm::PointerUnion<AssociatedTypeDecl *, ProtocolDecl *>
3786-
AssocTypeOrProtocolType;
3787-
37883785
/// A nested type. Either a dependent associated archetype, or a concrete
37893786
/// type (which may be a bound archetype from an outer context).
37903787
class NestedType {
@@ -3831,7 +3828,7 @@ class ArchetypeType final : public SubstitutableType,
38313828
Type Superclass;
38323829

38333830
llvm::PointerUnion<ArchetypeType *, TypeBase *> ParentOrOpened;
3834-
AssocTypeOrProtocolType AssocTypeOrProto;
3831+
AssociatedTypeDecl *AssocType;
38353832
Identifier Name;
38363833
unsigned isRecursive: 1;
38373834
MutableArrayRef<std::pair<Identifier, NestedType>> NestedTypes;
@@ -3851,7 +3848,7 @@ class ArchetypeType final : public SubstitutableType,
38513848
/// The ConformsTo array will be copied into the ASTContext by this routine.
38523849
static CanTypeWrapper<ArchetypeType>
38533850
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
3854-
AssocTypeOrProtocolType AssocTypeOrProto,
3851+
AssociatedTypeDecl *AssocType,
38553852
Identifier Name, ArrayRef<Type> ConformsTo,
38563853
Type Superclass,
38573854
bool isRecursive = false);
@@ -3862,7 +3859,7 @@ class ArchetypeType final : public SubstitutableType,
38623859
/// by this routine.
38633860
static CanTypeWrapper<ArchetypeType>
38643861
getNew(const ASTContext &Ctx, ArchetypeType *Parent,
3865-
AssocTypeOrProtocolType AssocTypeOrProto,
3862+
AssociatedTypeDecl *AssocType,
38663863
Identifier Name,
38673864
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
38683865
Type Superclass,
@@ -3910,15 +3907,9 @@ class ArchetypeType final : public SubstitutableType,
39103907
/// be a member of one of the protocols to which the parent archetype
39113908
/// conforms.
39123909
AssociatedTypeDecl *getAssocType() const {
3913-
return AssocTypeOrProto.dyn_cast<AssociatedTypeDecl *>();
3910+
return AssocType;
39143911
}
39153912

3916-
/// Retrieve the protocol for which this archetype describes the 'Self'
3917-
/// parameter.
3918-
ProtocolDecl *getSelfProtocol() const {
3919-
return AssocTypeOrProto.dyn_cast<ProtocolDecl *>();
3920-
}
3921-
39223913
/// getConformsTo - Retrieve the set of protocols to which this substitutable
39233914
/// type shall conform.
39243915
ArrayRef<ProtocolDecl *> getConformsTo() const { return ConformsTo; }
@@ -3936,12 +3927,6 @@ class ArchetypeType final : public SubstitutableType,
39363927
return !getConformsTo().empty() || getSuperclass();
39373928
}
39383929

3939-
/// Retrieve either the associated type or the protocol to which this
3940-
/// associated type corresponds.
3941-
AssocTypeOrProtocolType getAssocTypeOrProtocol() const {
3942-
return AssocTypeOrProto;
3943-
}
3944-
39453930
/// \brief Retrieve the nested type with the given name.
39463931
NestedType getNestedType(Identifier Name) const;
39473932

@@ -3998,14 +3983,14 @@ class ArchetypeType final : public SubstitutableType,
39983983

39993984
private:
40003985
ArchetypeType(const ASTContext &Ctx, ArchetypeType *Parent,
4001-
AssocTypeOrProtocolType AssocTypeOrProto,
3986+
AssociatedTypeDecl *AssocType,
40023987
Identifier Name, ArrayRef<ProtocolDecl *> ConformsTo,
40033988
Type Superclass,
40043989
bool isRecursive = false)
40053990
: SubstitutableType(TypeKind::Archetype, &Ctx,
40063991
RecursiveTypeProperties::HasArchetype),
40073992
ConformsTo(ConformsTo), Superclass(Superclass), ParentOrOpened(Parent),
4008-
AssocTypeOrProto(AssocTypeOrProto), Name(Name),
3993+
AssocType(AssocType), Name(Name),
40093994
isRecursive(isRecursive) { }
40103995

40113996
ArchetypeType(const ASTContext &Ctx,
@@ -4018,6 +4003,7 @@ class ArchetypeType final : public SubstitutableType,
40184003
RecursiveTypeProperties::HasOpenedExistential)),
40194004
ConformsTo(ConformsTo), Superclass(Superclass),
40204005
ParentOrOpened(Existential.getPointer()),
4006+
AssocType(nullptr),
40214007
isRecursive(isRecursive) { }
40224008
};
40234009
BEGIN_CAN_TYPE_WRAPPER(ArchetypeType, SubstitutableType)

include/swift/Serialization/ModuleFormat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 283; // Last change: witness markers removed
57+
const uint16_t VERSION_MINOR = 284; // Last change: Self archetype protocol removed
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -626,7 +626,7 @@ namespace decls_block {
626626
ARCHETYPE_TYPE,
627627
IdentifierIDField, // name
628628
TypeIDField, // index if primary, parent if non-primary
629-
DeclIDField, // associated type or protocol decl
629+
DeclIDField, // associated type decl
630630
TypeIDField, // superclass
631631
BCArray<DeclIDField> // conformances
632632
// Trailed by the nested types record.

lib/AST/ArchetypeBuilder.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,12 @@ ArchetypeBuilder::PotentialArchetype::getType(ArchetypeBuilder &builder) {
614614
return representative->ArchetypeOrConcreteType;
615615
}
616616

617-
ArchetypeType::AssocTypeOrProtocolType assocTypeOrProto = RootProtocol;
617+
AssociatedTypeDecl *assocType = nullptr;
618+
618619
// Allocate a new archetype.
619620
ArchetypeType *ParentArchetype = nullptr;
620621
auto &mod = builder.getModule();
621622
if (auto parent = getParent()) {
622-
assert(assocTypeOrProto.isNull() &&
623-
"root protocol type given for non-root archetype");
624623
auto parentTy = parent->getType(builder);
625624
if (!parentTy)
626625
return NestedType::forConcreteType(
@@ -671,7 +670,7 @@ ArchetypeBuilder::PotentialArchetype::getType(ArchetypeBuilder &builder) {
671670
}
672671
}
673672

674-
assocTypeOrProto = getResolvedAssociatedType();
673+
assocType = getResolvedAssociatedType();
675674
}
676675

677676
// If we ended up building our parent archetype, then we'll have
@@ -714,7 +713,7 @@ ArchetypeBuilder::PotentialArchetype::getType(ArchetypeBuilder &builder) {
714713

715714
auto arch
716715
= ArchetypeType::getNew(builder.getASTContext(), ParentArchetype,
717-
assocTypeOrProto, getName(), Protos,
716+
assocType, getName(), Protos,
718717
superclass, isRecursive());
719718

720719
representative->ArchetypeOrConcreteType = NestedType::forArchetype(arch);
@@ -868,29 +867,22 @@ auto ArchetypeBuilder::resolveArchetype(Type type) -> PotentialArchetype * {
868867
}
869868

870869
auto ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam,
871-
ProtocolDecl *RootProtocol,
872870
Identifier ParamName)
873871
-> PotentialArchetype *
874872
{
875873
GenericTypeParamKey Key{GenericParam->getDepth(), GenericParam->getIndex()};
876874

877875
// Create a potential archetype for this type parameter.
878876
assert(!Impl->PotentialArchetypes[Key]);
879-
auto PA = new PotentialArchetype(GenericParam, RootProtocol, ParamName);
877+
auto PA = new PotentialArchetype(GenericParam, ParamName);
880878

881879
Impl->PotentialArchetypes[Key] = PA;
882880
return PA;
883881
}
884882

885883
void ArchetypeBuilder::addGenericParameter(GenericTypeParamDecl *GenericParam) {
886-
ProtocolDecl *RootProtocol = dyn_cast<ProtocolDecl>(GenericParam->getDeclContext());
887-
if (!RootProtocol) {
888-
if (auto Ext = dyn_cast<ExtensionDecl>(GenericParam->getDeclContext()))
889-
RootProtocol = dyn_cast_or_null<ProtocolDecl>(Ext->getExtendedType()->getAnyNominal());
890-
}
891884
addGenericParameter(
892885
GenericParam->getDeclaredType()->castTo<GenericTypeParamType>(),
893-
RootProtocol,
894886
GenericParam->getName());
895887
}
896888

@@ -912,7 +904,7 @@ void ArchetypeBuilder::addGenericParameter(GenericTypeParamType *GenericParam) {
912904
if (name.str().startswith("$"))
913905
name = Context.getIdentifier(name.str().slice(1, name.str().size()));
914906

915-
addGenericParameter(GenericParam, nullptr, name);
907+
addGenericParameter(GenericParam, name);
916908
}
917909

918910
bool ArchetypeBuilder::addConformanceRequirement(PotentialArchetype *PAT,

lib/AST/Type.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2492,7 +2492,7 @@ Type TupleType::getVarArgsBaseType() const {
24922492

24932493
CanArchetypeType ArchetypeType::getNew(const ASTContext &Ctx,
24942494
ArchetypeType *Parent,
2495-
AssocTypeOrProtocolType AssocTypeOrProto,
2495+
AssociatedTypeDecl *AssocType,
24962496
Identifier Name,
24972497
ArrayRef<Type> ConformsTo,
24982498
Type Superclass,
@@ -2506,14 +2506,14 @@ CanArchetypeType ArchetypeType::getNew(const ASTContext &Ctx,
25062506

25072507
auto arena = AllocationArena::Permanent;
25082508
return CanArchetypeType(
2509-
new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocTypeOrProto, Name,
2509+
new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocType, Name,
25102510
Ctx.AllocateCopy(ConformsToProtos),
25112511
Superclass, isRecursive));
25122512
}
25132513

25142514
CanArchetypeType
25152515
ArchetypeType::getNew(const ASTContext &Ctx, ArchetypeType *Parent,
2516-
AssocTypeOrProtocolType AssocTypeOrProto,
2516+
AssociatedTypeDecl *AssocType,
25172517
Identifier Name,
25182518
SmallVectorImpl<ProtocolDecl *> &ConformsTo,
25192519
Type Superclass, bool isRecursive) {
@@ -2522,7 +2522,7 @@ ArchetypeType::getNew(const ASTContext &Ctx, ArchetypeType *Parent,
25222522

25232523
auto arena = AllocationArena::Permanent;
25242524
return CanArchetypeType(
2525-
new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocTypeOrProto, Name,
2525+
new (Ctx, arena) ArchetypeType(Ctx, Parent, AssocType, Name,
25262526
Ctx.AllocateCopy(ConformsTo),
25272527
Superclass, isRecursive));
25282528
}

lib/Serialization/Deserialization.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3609,12 +3609,12 @@ Type ModuleFile::getType(TypeID TID) {
36093609
case decls_block::ARCHETYPE_TYPE: {
36103610
IdentifierID nameID;
36113611
TypeID parentID;
3612-
DeclID assocTypeOrProtoID;
3612+
DeclID assocTypeID;
36133613
TypeID superclassID;
36143614
ArrayRef<uint64_t> rawConformanceIDs;
36153615

36163616
decls_block::ArchetypeTypeLayout::readRecord(scratch, nameID, parentID,
3617-
assocTypeOrProtoID,
3617+
assocTypeID,
36183618
superclassID,
36193619
rawConformanceIDs);
36203620

@@ -3625,13 +3625,8 @@ Type ModuleFile::getType(TypeID TID) {
36253625
if (auto parentType = getType(parentID))
36263626
parent = parentType->castTo<ArchetypeType>();
36273627

3628-
ArchetypeType::AssocTypeOrProtocolType assocTypeOrProto;
3629-
auto assocTypeOrProtoDecl = getDecl(assocTypeOrProtoID);
3630-
if (auto assocType
3631-
= dyn_cast_or_null<AssociatedTypeDecl>(assocTypeOrProtoDecl))
3632-
assocTypeOrProto = assocType;
3633-
else
3634-
assocTypeOrProto = cast_or_null<ProtocolDecl>(assocTypeOrProtoDecl);
3628+
auto assocTypeDecl = dyn_cast_or_null<AssociatedTypeDecl>(
3629+
getDecl(assocTypeID));
36353630

36363631
superclass = getType(superclassID);
36373632

@@ -3642,7 +3637,7 @@ Type ModuleFile::getType(TypeID TID) {
36423637
if (typeOrOffset.isComplete())
36433638
break;
36443639

3645-
auto archetype = ArchetypeType::getNew(ctx, parent, assocTypeOrProto,
3640+
auto archetype = ArchetypeType::getNew(ctx, parent, assocTypeDecl,
36463641
getIdentifier(nameID), conformances,
36473642
superclass, false);
36483643
typeOrOffset = archetype;

lib/Serialization/Serialization.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,17 +2962,13 @@ void Serializer::writeType(Type ty) {
29622962
for (auto proto : archetypeTy->getConformsTo())
29632963
conformances.push_back(addDeclRef(proto));
29642964

2965-
DeclID assocTypeOrProtoID;
2966-
if (auto assocType = archetypeTy->getAssocType())
2967-
assocTypeOrProtoID = addDeclRef(assocType);
2968-
else
2969-
assocTypeOrProtoID = addDeclRef(archetypeTy->getSelfProtocol());
2965+
DeclID assocTypeID = addDeclRef(archetypeTy->getAssocType());
29702966

29712967
unsigned abbrCode = DeclTypeAbbrCodes[ArchetypeTypeLayout::Code];
29722968
ArchetypeTypeLayout::emitRecord(Out, ScratchRecord, abbrCode,
29732969
addIdentifierRef(archetypeTy->getName()),
29742970
parentID,
2975-
assocTypeOrProtoID,
2971+
assocTypeID,
29762972
addTypeRef(archetypeTy->getSuperclass()),
29772973
conformances);
29782974

0 commit comments

Comments
 (0)