Skip to content

Remove AbstractTypeParamDecl #61856

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
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
32 changes: 4 additions & 28 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,8 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
);

SWIFT_INLINE_BITFIELD_EMPTY(TypeDecl, ValueDecl);
SWIFT_INLINE_BITFIELD_EMPTY(AbstractTypeParamDecl, TypeDecl);

SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, AbstractTypeParamDecl, 16+16+1+1,
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, TypeDecl, 16+16+1+1,
: NumPadBits,

Depth : 16,
Expand Down Expand Up @@ -3182,29 +3181,6 @@ class TypeAliasDecl : public GenericTypeDecl {
}
};

/// Abstract class describing generic type parameters and associated types,
/// whose common purpose is to anchor the abstract type parameter and specify
/// requirements for any corresponding type argument.
class AbstractTypeParamDecl : public TypeDecl {
protected:
AbstractTypeParamDecl(DeclKind kind, DeclContext *dc, Identifier name,
SourceLoc NameLoc)
: TypeDecl(kind, dc, name, NameLoc, { }) { }

public:
/// Return the superclass of the generic parameter.
Type getSuperclass() const;

/// Retrieve the set of protocols to which this abstract type
/// parameter conforms.
ArrayRef<ProtocolDecl *> getConformingProtocols() const;

static bool classof(const Decl *D) {
return D->getKind() >= DeclKind::First_AbstractTypeParamDecl &&
D->getKind() <= DeclKind::Last_AbstractTypeParamDecl;
}
};

/// A declaration of a generic type parameter.
///
/// A generic type parameter introduces a new, named type parameter along
Expand All @@ -3219,7 +3195,7 @@ class AbstractTypeParamDecl : public TypeDecl {
/// func min<T : Comparable>(x : T, y : T) -> T { ... }
/// \endcode
class GenericTypeParamDecl final
: public AbstractTypeParamDecl,
: public TypeDecl,
private llvm::TrailingObjects<GenericTypeParamDecl, TypeRepr *,
SourceLoc> {
friend TrailingObjects;
Expand Down Expand Up @@ -3446,7 +3422,7 @@ class GenericTypeParamDecl final
/// func getNext() -> Element?
/// }
/// \endcode
class AssociatedTypeDecl : public AbstractTypeParamDecl {
class AssociatedTypeDecl : public TypeDecl {
/// The location of the initial keyword.
SourceLoc KeywordLoc;

Expand Down Expand Up @@ -3510,7 +3486,7 @@ class AssociatedTypeDecl : public AbstractTypeParamDecl {
/// Retrieve the (first) overridden associated type declaration, if any.
AssociatedTypeDecl *getOverriddenDecl() const {
return cast_or_null<AssociatedTypeDecl>(
AbstractTypeParamDecl::getOverriddenDecl());
TypeDecl::getOverriddenDecl());
}

/// Retrieve the set of associated types overridden by this associated
Expand Down
6 changes: 2 additions & 4 deletions include/swift/AST/DeclNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ ABSTRACT_DECL(Value, Decl)
GENERIC_VALUE_DECL(OpaqueType, GenericTypeDecl)
GENERIC_VALUE_DECL(TypeAlias, GenericTypeDecl)
DECL_RANGE(GenericType, Enum, TypeAlias)
ABSTRACT_DECL(AbstractTypeParam, TypeDecl)
VALUE_DECL(GenericTypeParam, AbstractTypeParamDecl)
VALUE_DECL(AssociatedType, AbstractTypeParamDecl)
DECL_RANGE(AbstractTypeParam, GenericTypeParam, AssociatedType)
VALUE_DECL(GenericTypeParam, TypeDecl)
VALUE_DECL(AssociatedType, TypeDecl)
CONTEXT_VALUE_DECL(Module, TypeDecl)
DECL_RANGE(Type, Enum, Module)
ABSTRACT_DECL(AbstractStorage, ValueDecl)
Expand Down
9 changes: 2 additions & 7 deletions lib/AST/ASTDumper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,19 +648,14 @@ namespace {
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

void printAbstractTypeParamCommon(AbstractTypeParamDecl *decl,
const char *name) {
printCommon(decl, name);
}

void visitGenericTypeParamDecl(GenericTypeParamDecl *decl) {
printAbstractTypeParamCommon(decl, "generic_type_param");
printCommon(decl, "generic_type_param");
OS << " depth=" << decl->getDepth() << " index=" << decl->getIndex();
PrintWithColorRAII(OS, ParenthesisColor) << ')';
}

void visitAssociatedTypeDecl(AssociatedTypeDecl *decl) {
printAbstractTypeParamCommon(decl, "associated_type_decl");
printCommon(decl, "associated_type_decl");
if (auto defaultDef = decl->getDefaultDefinitionType()) {
OS << " default=";
defaultDef.print(OS);
Expand Down
17 changes: 7 additions & 10 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6559,16 +6559,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {

// Print based on the type.
Printer << "some ";
if (!decl->getConformingProtocols().empty()) {
llvm::interleave(decl->getConformingProtocols(), Printer, [&](ProtocolDecl *proto){
if (auto printType = proto->getDeclaredType())
printType->print(Printer, Options);
else
Printer << proto->getNameStr();
}, " & ");
} else {
Printer << "Any";
}
auto archetypeType = decl->getDeclContext()->mapTypeIntoContext(
decl->getDeclaredInterfaceType())->castTo<ArchetypeType>();
auto constraintType = archetypeType->getExistentialType();
if (auto *existentialType = constraintType->getAs<ExistentialType>())
constraintType = existentialType->getConstraintType();

constraintType->print(Printer, Options);
return;
}

Expand Down
26 changes: 16 additions & 10 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,23 +275,29 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return false;
}

bool visitAbstractTypeParamDecl(AbstractTypeParamDecl *TPD) {
for (const auto &Inherit: TPD->getInherited()) {
bool visitGenericTypeParamDecl(GenericTypeParamDecl *GTPD) {
for (const auto &Inherit: GTPD->getInherited()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}
return false;
}

if (const auto ATD = dyn_cast<AssociatedTypeDecl>(TPD)) {
if (const auto DefaultTy = ATD->getDefaultDefinitionTypeRepr())
if (doIt(DefaultTy))
bool visitAssociatedTypeDecl(AssociatedTypeDecl *ATD) {
for (const auto &Inherit: ATD->getInherited()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}
if (const auto DefaultTy = ATD->getDefaultDefinitionTypeRepr())
if (doIt(DefaultTy))
return true;

if (auto *WhereClause = ATD->getTrailingWhereClause()) {
for (auto &Req: WhereClause->getRequirements()) {
if (doIt(Req))
return true;
}
if (auto *WhereClause = ATD->getTrailingWhereClause()) {
for (auto &Req: WhereClause->getRequirements()) {
if (doIt(Req))
return true;
}
}
return false;
Expand Down
37 changes: 6 additions & 31 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ bool Decl::canHaveComment() const {
return !this->hasClangNode() &&
(isa<ValueDecl>(this) || isa<ExtensionDecl>(this)) &&
!isa<ParamDecl>(this) &&
(!isa<AbstractTypeParamDecl>(this) || isa<AssociatedTypeDecl>(this));
!isa<GenericTypeParamDecl>(this);
}

ModuleDecl *Decl::getModuleContext() const {
Expand Down Expand Up @@ -4623,36 +4623,11 @@ Type TypeAliasDecl::getStructuralType() const {
return ErrorType::get(ctx);
}

Type AbstractTypeParamDecl::getSuperclass() const {
auto *genericEnv = getDeclContext()->getGenericEnvironmentOfContext();
assert(genericEnv != nullptr && "Too much circularity");

auto contextTy = genericEnv->mapTypeIntoContext(getDeclaredInterfaceType());
if (auto *archetype = contextTy->getAs<ArchetypeType>())
return archetype->getSuperclass();

// FIXME: Assert that this is never queried.
return nullptr;
}

ArrayRef<ProtocolDecl *>
AbstractTypeParamDecl::getConformingProtocols() const {
auto *genericEnv = getDeclContext()->getGenericEnvironmentOfContext();
assert(genericEnv != nullptr && "Too much circularity");

auto contextTy = genericEnv->mapTypeIntoContext(getDeclaredInterfaceType());
if (auto *archetype = contextTy->getAs<ArchetypeType>())
return archetype->getConformsTo();

// FIXME: Assert that this is never queried.
return { };
}

GenericTypeParamDecl::GenericTypeParamDecl(
DeclContext *dc, Identifier name, SourceLoc nameLoc, SourceLoc ellipsisLoc,
unsigned depth, unsigned index, bool isParameterPack, bool isOpaqueType,
TypeRepr *opaqueTypeRepr)
: AbstractTypeParamDecl(DeclKind::GenericTypeParam, dc, name, nameLoc) {
: TypeDecl(DeclKind::GenericTypeParam, dc, name, nameLoc, { }) {
assert(!(ellipsisLoc && !isParameterPack) &&
"Ellipsis always means type parameter pack");

Expand Down Expand Up @@ -4736,7 +4711,7 @@ AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
Identifier name, SourceLoc nameLoc,
TypeRepr *defaultDefinition,
TrailingWhereClause *trailingWhere)
: AbstractTypeParamDecl(DeclKind::AssociatedType, dc, name, nameLoc),
: TypeDecl(DeclKind::AssociatedType, dc, name, nameLoc, { }),
KeywordLoc(keywordLoc), DefaultDefinition(defaultDefinition),
TrailingWhere(trailingWhere) {}

Expand All @@ -4745,7 +4720,7 @@ AssociatedTypeDecl::AssociatedTypeDecl(DeclContext *dc, SourceLoc keywordLoc,
TrailingWhereClause *trailingWhere,
LazyMemberLoader *definitionResolver,
uint64_t resolverData)
: AbstractTypeParamDecl(DeclKind::AssociatedType, dc, name, nameLoc),
: TypeDecl(DeclKind::AssociatedType, dc, name, nameLoc, { }),
KeywordLoc(keywordLoc), DefaultDefinition(nullptr),
TrailingWhere(trailingWhere), Resolver(definitionResolver),
ResolverContextData(resolverData) {
Expand Down Expand Up @@ -4781,7 +4756,7 @@ AssociatedTypeDecl::getOverriddenDecls() const {
if (auto cached = request.getCachedResult())
overridden = std::move(*cached);
else
overridden = AbstractTypeParamDecl::getOverriddenDecls();
overridden = TypeDecl::getOverriddenDecls();

llvm::TinyPtrVector<AssociatedTypeDecl *> assocTypes;
for (auto decl : overridden) {
Expand Down Expand Up @@ -4809,7 +4784,7 @@ static AssociatedTypeDecl *getAssociatedTypeAnchor(
auto anchor = getAssociatedTypeAnchor(assocType, searched);
if (!anchor)
continue;
if (!bestAnchor || AbstractTypeParamDecl::compare(anchor, bestAnchor) < 0)
if (!bestAnchor || TypeDecl::compare(anchor, bestAnchor) < 0)
bestAnchor = anchor;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,8 @@ resolveTypeDeclsToNominal(Evaluator &evaluator,
}

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

return nominalDecls;
Expand Down
29 changes: 7 additions & 22 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsClangImporter.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/GenericParamList.h"
#include "swift/AST/GenericSignature.h"
#include "swift/AST/Module.h"
Expand Down Expand Up @@ -1071,29 +1072,13 @@ namespace {
importedTypeArgs.push_back(importedTypeArg);
}
} else {
for (auto typeParam : imported->getGenericParams()->getParams()) {
if (typeParam->getSuperclass() &&
typeParam->getConformingProtocols().empty()) {
importedTypeArgs.push_back(typeParam->getSuperclass());
continue;
}

SmallVector<Type, 4> memberTypes;

if (auto superclassType = typeParam->getSuperclass())
memberTypes.push_back(superclassType);
auto *genericEnv = imported->getGenericEnvironment();

for (auto protocolDecl : typeParam->getConformingProtocols())
memberTypes.push_back(protocolDecl->getDeclaredInterfaceType());

bool hasExplicitAnyObject = false;
if (memberTypes.empty())
hasExplicitAnyObject = true;

Type importedTypeArg = ExistentialType::get(
ProtocolCompositionType::get(
Impl.SwiftContext, memberTypes,
hasExplicitAnyObject));
for (auto typeParam : imported->getGenericParams()->getParams()) {
Type importedTypeArg = genericEnv->mapTypeIntoContext(
typeParam->getDeclaredInterfaceType())
->castTo<ArchetypeType>()
->getExistentialType();
importedTypeArgs.push_back(importedTypeArg);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/CodeCompletionResultBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ using namespace swift::ide;

static bool shouldCopyAssociatedUSRForDecl(const ValueDecl *VD) {
// Avoid trying to generate a USR for some declaration types.
if (isa<AbstractTypeParamDecl>(VD) && !isa<AssociatedTypeDecl>(VD))
if (isa<GenericTypeParamDecl>(VD))
return false;
if (isa<ParamDecl>(VD))
return false;
Expand Down
6 changes: 4 additions & 2 deletions lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,10 @@ class ModuleWriter {
return;
} else if (auto ED = dyn_cast<EnumDecl>(TD)) {
forwardDeclare(ED);
} else if (isa<AbstractTypeParamDecl>(TD)) {
llvm_unreachable("should not see type params here");
} else if (isa<GenericTypeParamDecl>(TD)) {
llvm_unreachable("should not see generic parameters here");
} else if (isa<AssociatedTypeDecl>(TD)) {
llvm_unreachable("should not see associated types here");
} else if (isa<StructDecl>(TD) &&
TD->getModuleContext()->isStdlibModule()) {
// stdlib has some @_cdecl functions with structs.
Expand Down
3 changes: 2 additions & 1 deletion lib/SILGen/SILGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
void visitPrecedenceGroupDecl(PrecedenceGroupDecl *d) {}
void visitTypeAliasDecl(TypeAliasDecl *d) {}
void visitOpaqueTypeDecl(OpaqueTypeDecl *d) {}
void visitAbstractTypeParamDecl(AbstractTypeParamDecl *d) {}
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
void visitConstructorDecl(ConstructorDecl *d) {}
void visitDestructorDecl(DestructorDecl *d) {}
void visitModuleDecl(ModuleDecl *d) { }
Expand Down
6 changes: 4 additions & 2 deletions lib/SILGen/SILGenType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,8 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
//===--------------------------------------------------------------------===//
void visitTypeAliasDecl(TypeAliasDecl *tad) {}
void visitOpaqueTypeDecl(OpaqueTypeDecl *otd) {}
void visitAbstractTypeParamDecl(AbstractTypeParamDecl *tpd) {}
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
void visitModuleDecl(ModuleDecl *md) {}
void visitMissingMemberDecl(MissingMemberDecl *) {}
void visitNominalTypeDecl(NominalTypeDecl *ntd) {
Expand Down Expand Up @@ -1276,7 +1277,8 @@ class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
//===--------------------------------------------------------------------===//
void visitTypeAliasDecl(TypeAliasDecl *tad) {}
void visitOpaqueTypeDecl(OpaqueTypeDecl *tad) {}
void visitAbstractTypeParamDecl(AbstractTypeParamDecl *tpd) {}
void visitGenericTypeParamDecl(GenericTypeParamDecl *d) {}
void visitAssociatedTypeDecl(AssociatedTypeDecl *d) {}
void visitModuleDecl(ModuleDecl *md) {}
void visitMissingMemberDecl(MissingMemberDecl *) {}
void visitNominalTypeDecl(NominalTypeDecl *ntd) {
Expand Down
Loading