Skip to content

Commit 44e6cb1

Browse files
authored
Merge pull request #61856 from slavapestov/remove-abstract-type-param-decl
Remove AbstractTypeParamDecl
2 parents f877777 + 36b3f0e commit 44e6cb1

File tree

15 files changed

+76
-135
lines changed

15 files changed

+76
-135
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,8 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
499499
);
500500

501501
SWIFT_INLINE_BITFIELD_EMPTY(TypeDecl, ValueDecl);
502-
SWIFT_INLINE_BITFIELD_EMPTY(AbstractTypeParamDecl, TypeDecl);
503502

504-
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, AbstractTypeParamDecl, 16+16+1+1,
503+
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, TypeDecl, 16+16+1+1,
505504
: NumPadBits,
506505

507506
Depth : 16,
@@ -3182,29 +3181,6 @@ class TypeAliasDecl : public GenericTypeDecl {
31823181
}
31833182
};
31843183

3185-
/// Abstract class describing generic type parameters and associated types,
3186-
/// whose common purpose is to anchor the abstract type parameter and specify
3187-
/// requirements for any corresponding type argument.
3188-
class AbstractTypeParamDecl : public TypeDecl {
3189-
protected:
3190-
AbstractTypeParamDecl(DeclKind kind, DeclContext *dc, Identifier name,
3191-
SourceLoc NameLoc)
3192-
: TypeDecl(kind, dc, name, NameLoc, { }) { }
3193-
3194-
public:
3195-
/// Return the superclass of the generic parameter.
3196-
Type getSuperclass() const;
3197-
3198-
/// Retrieve the set of protocols to which this abstract type
3199-
/// parameter conforms.
3200-
ArrayRef<ProtocolDecl *> getConformingProtocols() const;
3201-
3202-
static bool classof(const Decl *D) {
3203-
return D->getKind() >= DeclKind::First_AbstractTypeParamDecl &&
3204-
D->getKind() <= DeclKind::Last_AbstractTypeParamDecl;
3205-
}
3206-
};
3207-
32083184
/// A declaration of a generic type parameter.
32093185
///
32103186
/// A generic type parameter introduces a new, named type parameter along
@@ -3219,7 +3195,7 @@ class AbstractTypeParamDecl : public TypeDecl {
32193195
/// func min<T : Comparable>(x : T, y : T) -> T { ... }
32203196
/// \endcode
32213197
class GenericTypeParamDecl final
3222-
: public AbstractTypeParamDecl,
3198+
: public TypeDecl,
32233199
private llvm::TrailingObjects<GenericTypeParamDecl, TypeRepr *,
32243200
SourceLoc> {
32253201
friend TrailingObjects;
@@ -3446,7 +3422,7 @@ class GenericTypeParamDecl final
34463422
/// func getNext() -> Element?
34473423
/// }
34483424
/// \endcode
3449-
class AssociatedTypeDecl : public AbstractTypeParamDecl {
3425+
class AssociatedTypeDecl : public TypeDecl {
34503426
/// The location of the initial keyword.
34513427
SourceLoc KeywordLoc;
34523428

@@ -3510,7 +3486,7 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
35103486
/// Retrieve the (first) overridden associated type declaration, if any.
35113487
AssociatedTypeDecl *getOverriddenDecl() const {
35123488
return cast_or_null<AssociatedTypeDecl>(
3513-
AbstractTypeParamDecl::getOverriddenDecl());
3489+
TypeDecl::getOverriddenDecl());
35143490
}
35153491

35163492
/// Retrieve the set of associated types overridden by this associated

include/swift/AST/DeclNodes.def

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,8 @@ ABSTRACT_DECL(Value, Decl)
156156
GENERIC_VALUE_DECL(OpaqueType, GenericTypeDecl)
157157
GENERIC_VALUE_DECL(TypeAlias, GenericTypeDecl)
158158
DECL_RANGE(GenericType, Enum, TypeAlias)
159-
ABSTRACT_DECL(AbstractTypeParam, TypeDecl)
160-
VALUE_DECL(GenericTypeParam, AbstractTypeParamDecl)
161-
VALUE_DECL(AssociatedType, AbstractTypeParamDecl)
162-
DECL_RANGE(AbstractTypeParam, GenericTypeParam, AssociatedType)
159+
VALUE_DECL(GenericTypeParam, TypeDecl)
160+
VALUE_DECL(AssociatedType, TypeDecl)
163161
CONTEXT_VALUE_DECL(Module, TypeDecl)
164162
DECL_RANGE(Type, Enum, Module)
165163
ABSTRACT_DECL(AbstractStorage, ValueDecl)

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -648,19 +648,14 @@ namespace {
648648
PrintWithColorRAII(OS, ParenthesisColor) << ')';
649649
}
650650

651-
void printAbstractTypeParamCommon(AbstractTypeParamDecl *decl,
652-
const char *name) {
653-
printCommon(decl, name);
654-
}
655-
656651
void visitGenericTypeParamDecl(GenericTypeParamDecl *decl) {
657-
printAbstractTypeParamCommon(decl, "generic_type_param");
652+
printCommon(decl, "generic_type_param");
658653
OS << " depth=" << decl->getDepth() << " index=" << decl->getIndex();
659654
PrintWithColorRAII(OS, ParenthesisColor) << ')';
660655
}
661656

662657
void visitAssociatedTypeDecl(AssociatedTypeDecl *decl) {
663-
printAbstractTypeParamCommon(decl, "associated_type_decl");
658+
printCommon(decl, "associated_type_decl");
664659
if (auto defaultDef = decl->getDefaultDefinitionType()) {
665660
OS << " default=";
666661
defaultDef.print(OS);

lib/AST/ASTPrinter.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6559,16 +6559,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
65596559

65606560
// Print based on the type.
65616561
Printer << "some ";
6562-
if (!decl->getConformingProtocols().empty()) {
6563-
llvm::interleave(decl->getConformingProtocols(), Printer, [&](ProtocolDecl *proto){
6564-
if (auto printType = proto->getDeclaredType())
6565-
printType->print(Printer, Options);
6566-
else
6567-
Printer << proto->getNameStr();
6568-
}, " & ");
6569-
} else {
6570-
Printer << "Any";
6571-
}
6562+
auto archetypeType = decl->getDeclContext()->mapTypeIntoContext(
6563+
decl->getDeclaredInterfaceType())->castTo<ArchetypeType>();
6564+
auto constraintType = archetypeType->getExistentialType();
6565+
if (auto *existentialType = constraintType->getAs<ExistentialType>())
6566+
constraintType = existentialType->getConstraintType();
6567+
6568+
constraintType->print(Printer, Options);
65726569
return;
65736570
}
65746571

lib/AST/ASTWalker.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,23 +275,29 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
275275
return false;
276276
}
277277

278-
bool visitAbstractTypeParamDecl(AbstractTypeParamDecl *TPD) {
279-
for (const auto &Inherit: TPD->getInherited()) {
278+
bool visitGenericTypeParamDecl(GenericTypeParamDecl *GTPD) {
279+
for (const auto &Inherit: GTPD->getInherited()) {
280280
if (auto *const TyR = Inherit.getTypeRepr())
281281
if (doIt(TyR))
282282
return true;
283283
}
284+
return false;
285+
}
284286

285-
if (const auto ATD = dyn_cast<AssociatedTypeDecl>(TPD)) {
286-
if (const auto DefaultTy = ATD->getDefaultDefinitionTypeRepr())
287-
if (doIt(DefaultTy))
287+
bool visitAssociatedTypeDecl(AssociatedTypeDecl *ATD) {
288+
for (const auto &Inherit: ATD->getInherited()) {
289+
if (auto *const TyR = Inherit.getTypeRepr())
290+
if (doIt(TyR))
288291
return true;
292+
}
293+
if (const auto DefaultTy = ATD->getDefaultDefinitionTypeRepr())
294+
if (doIt(DefaultTy))
295+
return true;
289296

290-
if (auto *WhereClause = ATD->getTrailingWhereClause()) {
291-
for (auto &Req: WhereClause->getRequirements()) {
292-
if (doIt(Req))
293-
return true;
294-
}
297+
if (auto *WhereClause = ATD->getTrailingWhereClause()) {
298+
for (auto &Req: WhereClause->getRequirements()) {
299+
if (doIt(Req))
300+
return true;
295301
}
296302
}
297303
return false;

lib/AST/Decl.cpp

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ bool Decl::canHaveComment() const {
547547
return !this->hasClangNode() &&
548548
(isa<ValueDecl>(this) || isa<ExtensionDecl>(this)) &&
549549
!isa<ParamDecl>(this) &&
550-
(!isa<AbstractTypeParamDecl>(this) || isa<AssociatedTypeDecl>(this));
550+
!isa<GenericTypeParamDecl>(this);
551551
}
552552

553553
ModuleDecl *Decl::getModuleContext() const {
@@ -4623,36 +4623,11 @@ Type TypeAliasDecl::getStructuralType() const {
46234623
return ErrorType::get(ctx);
46244624
}
46254625

4626-
Type AbstractTypeParamDecl::getSuperclass() const {
4627-
auto *genericEnv = getDeclContext()->getGenericEnvironmentOfContext();
4628-
assert(genericEnv != nullptr && "Too much circularity");
4629-
4630-
auto contextTy = genericEnv->mapTypeIntoContext(getDeclaredInterfaceType());
4631-
if (auto *archetype = contextTy->getAs<ArchetypeType>())
4632-
return archetype->getSuperclass();
4633-
4634-
// FIXME: Assert that this is never queried.
4635-
return nullptr;
4636-
}
4637-
4638-
ArrayRef<ProtocolDecl *>
4639-
AbstractTypeParamDecl::getConformingProtocols() const {
4640-
auto *genericEnv = getDeclContext()->getGenericEnvironmentOfContext();
4641-
assert(genericEnv != nullptr && "Too much circularity");
4642-
4643-
auto contextTy = genericEnv->mapTypeIntoContext(getDeclaredInterfaceType());
4644-
if (auto *archetype = contextTy->getAs<ArchetypeType>())
4645-
return archetype->getConformsTo();
4646-
4647-
// FIXME: Assert that this is never queried.
4648-
return { };
4649-
}
4650-
46514626
GenericTypeParamDecl::GenericTypeParamDecl(
46524627
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc ellipsisLoc,
46534628
unsigned depth, unsigned index, bool isParameterPack, bool isOpaqueType,
46544629
TypeRepr *opaqueTypeRepr)
4655-
: AbstractTypeParamDecl(DeclKind::GenericTypeParam, dc, name, nameLoc) {
4630+
: TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, { }) {
46564631
assert(!(ellipsisLoc && !isParameterPack) &&
46574632
"Ellipsis always means type parameter pack");
46584633

@@ -4736,7 +4711,7 @@ AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
47364711
Identifier name, SourceLoc nameLoc,
47374712
TypeRepr *defaultDefinition,
47384713
TrailingWhereClause *trailingWhere)
4739-
: AbstractTypeParamDecl(DeclKind::AssociatedType, dc, name, nameLoc),
4714+
: TypeDecl(DeclKind::AssociatedType, dc, name, nameLoc, { }),
47404715
KeywordLoc(keywordLoc), DefaultDefinition(defaultDefinition),
47414716
TrailingWhere(trailingWhere) {}
47424717

@@ -4745,7 +4720,7 @@ AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
47454720
TrailingWhereClause *trailingWhere,
47464721
LazyMemberLoader *definitionResolver,
47474722
uint64_t resolverData)
4748-
: AbstractTypeParamDecl(DeclKind::AssociatedType, dc, name, nameLoc),
4723+
: TypeDecl(DeclKind::AssociatedType, dc, name, nameLoc, { }),
47494724
KeywordLoc(keywordLoc), DefaultDefinition(nullptr),
47504725
TrailingWhere(trailingWhere), Resolver(definitionResolver),
47514726
ResolverContextData(resolverData) {
@@ -4781,7 +4756,7 @@ AssociatedTypeDecl::getOverriddenDecls() const {
47814756
if (auto cached = request.getCachedResult())
47824757
overridden = std::move(*cached);
47834758
else
4784-
overridden = AbstractTypeParamDecl::getOverriddenDecls();
4759+
overridden = TypeDecl::getOverriddenDecls();
47854760

47864761
llvm::TinyPtrVector<AssociatedTypeDecl *> assocTypes;
47874762
for (auto decl : overridden) {
@@ -4809,7 +4784,7 @@ static AssociatedTypeDecl *getAssociatedTypeAnchor(
48094784
auto anchor = getAssociatedTypeAnchor(assocType, searched);
48104785
if (!anchor)
48114786
continue;
4812-
if (!bestAnchor || AbstractTypeParamDecl::compare(anchor, bestAnchor) < 0)
4787+
if (!bestAnchor || TypeDecl::compare(anchor, bestAnchor) < 0)
48134788
bestAnchor = anchor;
48144789
}
48154790

lib/AST/NameLookup.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2300,7 +2300,8 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
23002300
}
23012301

23022302
// Make sure we didn't miss some interesting kind of type declaration.
2303-
assert(isa<AbstractTypeParamDecl>(typeDecl));
2303+
assert(isa<GenericTypeParamDecl>(typeDecl) ||
2304+
isa<AssociatedTypeDecl>(typeDecl));
23042305
}
23052306

23062307
return nominalDecls;

lib/ClangImporter/ImportType.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/DiagnosticEngine.h"
2525
#include "swift/AST/DiagnosticsClangImporter.h"
2626
#include "swift/AST/ExistentialLayout.h"
27+
#include "swift/AST/GenericEnvironment.h"
2728
#include "swift/AST/GenericParamList.h"
2829
#include "swift/AST/GenericSignature.h"
2930
#include "swift/AST/Module.h"
@@ -1071,29 +1072,13 @@ namespace {
10711072
importedTypeArgs.push_back(importedTypeArg);
10721073
}
10731074
} else {
1074-
for (auto typeParam : imported->getGenericParams()->getParams()) {
1075-
if (typeParam->getSuperclass() &&
1076-
typeParam->getConformingProtocols().empty()) {
1077-
importedTypeArgs.push_back(typeParam->getSuperclass());
1078-
continue;
1079-
}
1080-
1081-
SmallVector<Type, 4> memberTypes;
1082-
1083-
if (auto superclassType = typeParam->getSuperclass())
1084-
memberTypes.push_back(superclassType);
1075+
auto *genericEnv = imported->getGenericEnvironment();
10851076

1086-
for (auto protocolDecl : typeParam->getConformingProtocols())
1087-
memberTypes.push_back(protocolDecl->getDeclaredInterfaceType());
1088-
1089-
bool hasExplicitAnyObject = false;
1090-
if (memberTypes.empty())
1091-
hasExplicitAnyObject = true;
1092-
1093-
Type importedTypeArg = ExistentialType::get(
1094-
ProtocolCompositionType::get(
1095-
Impl.SwiftContext, memberTypes,
1096-
hasExplicitAnyObject));
1077+
for (auto typeParam : imported->getGenericParams()->getParams()) {
1078+
Type importedTypeArg = genericEnv->mapTypeIntoContext(
1079+
typeParam->getDeclaredInterfaceType())
1080+
->castTo<ArchetypeType>()
1081+
->getExistentialType();
10971082
importedTypeArgs.push_back(importedTypeArg);
10981083
}
10991084
}

lib/IDE/CodeCompletionResultBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using namespace swift::ide;
2727

2828
static bool shouldCopyAssociatedUSRForDecl(const ValueDecl *VD) {
2929
// Avoid trying to generate a USR for some declaration types.
30-
if (isa<AbstractTypeParamDecl>(VD) && !isa<AssociatedTypeDecl>(VD))
30+
if (isa<GenericTypeParamDecl>(VD))
3131
return false;
3232
if (isa<ParamDecl>(VD))
3333
return false;

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,10 @@ class ModuleWriter {
314314
return;
315315
} else if (auto ED = dyn_cast<EnumDecl>(TD)) {
316316
forwardDeclare(ED);
317-
} else if (isa<AbstractTypeParamDecl>(TD)) {
318-
llvm_unreachable("should not see type params here");
317+
} else if (isa<GenericTypeParamDecl>(TD)) {
318+
llvm_unreachable("should not see generic parameters here");
319+
} else if (isa<AssociatedTypeDecl>(TD)) {
320+
llvm_unreachable("should not see associated types here");
319321
} else if (isa<StructDecl>(TD) &&
320322
TD->getModuleContext()->isStdlibModule()) {
321323
// stdlib has some @_cdecl functions with structs.

lib/SILGen/SILGen.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
272272
void visitPrecedenceGroupDecl(PrecedenceGroupDecl *d) {}
273273
void visitTypeAliasDecl(TypeAliasDecl *d) {}
274274
void visitOpaqueTypeDecl(OpaqueTypeDecl *d) {}
275-
void visitAbstractTypeParamDecl(AbstractTypeParamDecl *d) {}
275+
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
276+
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
276277
void visitConstructorDecl(ConstructorDecl *d) {}
277278
void visitDestructorDecl(DestructorDecl *d) {}
278279
void visitModuleDecl(ModuleDecl *d) { }

lib/SILGen/SILGenType.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,8 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
11211121
//===--------------------------------------------------------------------===//
11221122
void visitTypeAliasDecl(TypeAliasDecl *tad) {}
11231123
void visitOpaqueTypeDecl(OpaqueTypeDecl *otd) {}
1124-
void visitAbstractTypeParamDecl(AbstractTypeParamDecl *tpd) {}
1124+
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
1125+
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
11251126
void visitModuleDecl(ModuleDecl *md) {}
11261127
void visitMissingMemberDecl(MissingMemberDecl *) {}
11271128
void visitNominalTypeDecl(NominalTypeDecl *ntd) {
@@ -1276,7 +1277,8 @@ class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
12761277
//===--------------------------------------------------------------------===//
12771278
void visitTypeAliasDecl(TypeAliasDecl *tad) {}
12781279
void visitOpaqueTypeDecl(OpaqueTypeDecl *tad) {}
1279-
void visitAbstractTypeParamDecl(AbstractTypeParamDecl *tpd) {}
1280+
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
1281+
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
12801282
void visitModuleDecl(ModuleDecl *md) {}
12811283
void visitMissingMemberDecl(MissingMemberDecl *) {}
12821284
void visitNominalTypeDecl(NominalTypeDecl *ntd) {

0 commit comments

Comments
 (0)