Skip to content

Commit eb99052

Browse files
authored
Merge pull request #5868 from slavapestov/remove-archetype-self-protocol
Remove ArchetypeType::getSelfProtocol()
2 parents 564fde4 + 2294e33 commit eb99052

File tree

15 files changed

+103
-124
lines changed

15 files changed

+103
-124
lines changed

include/swift/AST/ASTPrinter.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ class ASTPrinter {
155155

156156
// Helper functions.
157157

158-
void printTypeRef(DynamicSelfType *T, Identifier Name);
159-
160158
void printSeparator(bool &first, StringRef separator) {
161159
if (first) {
162160
first = false;

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/Decl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4991,10 +4991,11 @@ class FuncDecl final : public AbstractFunctionDecl,
49914991
const TypeLoc &getBodyResultTypeLoc() const { return FnRetType; }
49924992

49934993
/// Retrieve the result type of this function.
4994-
///
4995-
/// \sa getBodyResultType
49964994
Type getResultType() const;
49974995

4996+
/// Retrieve the result interface type of this function.
4997+
Type getResultInterfaceType() const;
4998+
49984999
/// Retrieve the result type of this function for use within the function
49995000
/// definition.
50005001
///
@@ -5410,6 +5411,9 @@ class ConstructorDecl : public AbstractFunctionDecl {
54105411
/// \brief Get the type of the constructed object.
54115412
Type getResultType() const;
54125413

5414+
/// \brief Get the interface type of the constructed object.
5415+
Type getResultInterfaceType() const;
5416+
54135417
/// Get the interface type of the initializing constructor.
54145418
Type getInitializerInterfaceType();
54155419
void setInitializerInterfaceType(Type t);

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/ASTPrinter.cpp

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -555,31 +555,6 @@ void ASTPrinter::printTypeRef(Type T, const TypeDecl *RefTo, Identifier Name) {
555555
printName(Name, Context);
556556
}
557557

558-
void ASTPrinter::printTypeRef(DynamicSelfType *T, Identifier Name) {
559-
// Try to print as a reference to the static type so that we will get a USR,
560-
// in cursor info.
561-
if (auto staticSelfT = T->getSelfType()) {
562-
// Handle protocol 'Self', which is an archetype.
563-
if (auto AT = staticSelfT->getAs<ArchetypeType>()) {
564-
if (auto SelfP = AT->getSelfProtocol()) {
565-
if (auto GTD = SelfP->getProtocolSelf()) {
566-
assert(GTD->isProtocolSelf());
567-
printTypeRef(T, GTD, Name);
568-
return;
569-
}
570-
}
571-
572-
// Handle class 'Self', which is just a class type.
573-
} else if (auto *NTD = staticSelfT->getAnyNominal()) {
574-
printTypeRef(T, NTD, Name);
575-
return;
576-
}
577-
}
578-
579-
// If that fails, just print the name.
580-
printName(Name, PrintNameContext::ClassDynamicSelf);
581-
}
582-
583558
void ASTPrinter::printModuleRef(ModuleEntity Mod, Identifier Name) {
584559
printName(Name);
585560
}
@@ -892,9 +867,11 @@ class PrintAST : public ASTVisitor<PrintAST> {
892867
Options.TransformContext->getTypeBase()) {
893868
auto *DC = Current->getInnermostDeclContext();
894869

895-
// Get the interface type, since TypeLocs still have
896-
// contextual types in them.
897-
T = ArchetypeBuilder::mapTypeOutOfContext(DC, T);
870+
if (T->hasArchetype()) {
871+
// Get the interface type, since TypeLocs still have
872+
// contextual types in them.
873+
T = ArchetypeBuilder::mapTypeOutOfContext(DC, T);
874+
}
898875

899876
// Get the innermost nominal type context.
900877
DC = DC->getInnermostTypeContext();
@@ -2427,8 +2404,12 @@ void PrintAST::printOneParameter(const ParamDecl *param,
24272404

24282405
printArgName();
24292406

2430-
if (!TheTypeLoc.getTypeRepr() && param->hasType())
2431-
TheTypeLoc = TypeLoc::withoutLoc(param->getType());
2407+
if (!TheTypeLoc.getTypeRepr() && param->hasType()) {
2408+
// FIXME: ParamDecls should have interface types instead
2409+
auto *DC = Current->getInnermostDeclContext();
2410+
auto type = ArchetypeBuilder::mapTypeOutOfContext(DC, param->getType());
2411+
TheTypeLoc = TypeLoc::withoutLoc(type);
2412+
}
24322413

24332414
// If the parameter is variadic, we will print the "..." after it, but we have
24342415
// to strip off the added array type.
@@ -2516,7 +2497,7 @@ void PrintAST::printParameterList(ParameterList *PL, Type paramListTy,
25162497

25172498
void PrintAST::printFunctionParameters(AbstractFunctionDecl *AFD) {
25182499
auto BodyParams = AFD->getParameterLists();
2519-
auto curTy = AFD->hasType() ? AFD->getType() : nullptr;
2500+
auto curTy = AFD->hasType() ? AFD->getInterfaceType() : nullptr;
25202501

25212502
// Skip over the implicit 'self'.
25222503
if (AFD->getImplicitSelfDecl()) {
@@ -2684,7 +2665,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
26842665
printFunctionParameters(decl);
26852666
});
26862667

2687-
Type ResultTy = decl->getResultType();
2668+
Type ResultTy = decl->getResultInterfaceType();
26882669
if (ResultTy && !ResultTy->isVoid()) {
26892670
TypeLoc ResultTyLoc = decl->getBodyResultTypeLoc();
26902671
if (!ResultTyLoc.getTypeRepr())
@@ -3670,7 +3651,17 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
36703651
return;
36713652
}
36723653

3673-
Printer.printTypeRef(T, T->getASTContext().Id_Self);
3654+
// Try to print as a reference to the static type so that we will get a USR,
3655+
// in cursor info.
3656+
auto staticSelfT = T->getSelfType();
3657+
3658+
if (auto *NTD = staticSelfT->getAnyNominal()) {
3659+
auto Name = T->getASTContext().Id_Self;
3660+
Printer.printTypeRef(T, NTD, Name);
3661+
return;
3662+
}
3663+
3664+
visit(staticSelfT);
36743665
}
36753666

36763667
void printFunctionExtInfo(AnyFunctionType::ExtInfo info) {
@@ -3987,21 +3978,14 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
39873978
}
39883979
}
39893980

3990-
if (T->getName().empty())
3981+
auto Name = T->getName();
3982+
if (Name.empty())
39913983
Printer << "<anonymous>";
39923984
else {
3993-
// Print protocol 'Self' as a generic parameter so that it gets
3994-
// annotated in cursor info.
3995-
// FIXME: in a protocol extension, we really want the extension, not the
3996-
// protocol.
3997-
if (auto *P = T->getSelfProtocol()) {
3998-
auto *GTD = P->getProtocolSelf();
3999-
assert(GTD && GTD->isProtocolSelf());
4000-
Printer.printTypeRef(T, GTD, T->getName());
4001-
return;
4002-
}
4003-
4004-
Printer.printName(T->getName());
3985+
PrintNameContext context = PrintNameContext::Normal;
3986+
if (Name == T->getASTContext().Id_Self)
3987+
context = PrintNameContext::GenericParameter;
3988+
Printer.printName(Name, context);
40053989
}
40063990
}
40073991
}
@@ -4027,10 +4011,14 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
40274011
if (Name.empty())
40284012
Printer << "<anonymous>";
40294013
else {
4014+
if (T->getDecl() && T->getDecl()->isProtocolSelf()) {
4015+
Printer.printTypeRef(T, T->getDecl(), Name);
4016+
return;
4017+
}
4018+
40304019
PrintNameContext context = PrintNameContext::Normal;
4031-
if (T->getDecl() && T->getDecl()->isProtocolSelf())
4020+
if (Name == T->getASTContext().Id_Self)
40324021
context = PrintNameContext::GenericParameter;
4033-
40344022
Printer.printName(Name, context);
40354023
}
40364024
}

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,

0 commit comments

Comments
 (0)