Skip to content

Commit 868a795

Browse files
committed
Introduce a new class between TypeDecl and NominalTypeDecl named GenericTypeDecl.
This factors the DeclContext and generic signature behavior out of NTD, allowing it to be reused in the future. NFC.
1 parent 9a5c3ea commit 868a795

File tree

18 files changed

+226
-180
lines changed

18 files changed

+226
-180
lines changed

include/swift/AST/Decl.h

Lines changed: 108 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,23 +2328,86 @@ class TypeDecl : public ValueDecl {
23282328
}
23292329
};
23302330

2331+
/// A type declaration that can have generic parameters attached to it. Because
2332+
/// it has these generic parameters, it is always a DeclContext.
2333+
class GenericTypeDecl : public TypeDecl, public DeclContext {
2334+
GenericParamList *GenericParams = nullptr;
2335+
2336+
/// \brief The generic signature of this type.
2337+
///
2338+
/// This is the semantic representation of a generic parameters and the
2339+
/// requirements placed on them.
2340+
///
2341+
/// FIXME: The generic parameters here are also derivable from
2342+
/// \c GenericParams. However, we likely want to make \c GenericParams
2343+
/// the parsed representation, and not part of the module file.
2344+
GenericSignature *GenericSig = nullptr;
2345+
//friend class DeclContext;
2346+
2347+
public:
2348+
GenericTypeDecl(DeclKind K, DeclContext *DC,
2349+
Identifier name, SourceLoc nameLoc,
2350+
MutableArrayRef<TypeLoc> inherited,
2351+
GenericParamList *GenericParams);
2352+
2353+
GenericParamList *getGenericParams() const { return GenericParams; }
2354+
2355+
/// Provide the set of parameters to a generic type, or null if
2356+
/// this function is not generic.
2357+
void setGenericParams(GenericParamList *params);
2358+
2359+
/// Set the generic signature of this type.
2360+
void setGenericSignature(GenericSignature *sig);
2361+
2362+
/// Retrieve the innermost generic parameter types.
2363+
ArrayRef<GenericTypeParamType *> getInnermostGenericParamTypes() const {
2364+
if (!GenericSig)
2365+
return { };
2366+
2367+
return GenericSig->getInnermostGenericParams();
2368+
}
2369+
2370+
/// Retrieve the generic requirements.
2371+
ArrayRef<Requirement> getGenericRequirements() const {
2372+
if (!GenericSig)
2373+
return { };
2374+
2375+
return GenericSig->getRequirements();
2376+
}
2377+
2378+
/// Retrieve the generic signature.
2379+
GenericSignature *getGenericSignature() const {
2380+
return GenericSig;
2381+
}
2382+
2383+
// Resolve ambiguity due to multiple base classes.
2384+
using TypeDecl::getASTContext;
2385+
using DeclContext::operator new;
2386+
using TypeDecl::getDeclaredInterfaceType;
2387+
2388+
static bool classof(const DeclContext *C) {
2389+
return C->getContextKind() == DeclContextKind::GenericTypeDecl;
2390+
}
2391+
};
2392+
2393+
2394+
23312395
/// TypeAliasDecl - This is a declaration of a typealias, for example:
23322396
///
2333-
/// typealias foo = int
2397+
/// typealias Foo = Int
23342398
///
23352399
/// TypeAliasDecl's always have 'MetatypeType' type.
23362400
///
23372401
class TypeAliasDecl : public TypeDecl {
23382402
/// The type that represents this (sugared) name alias.
23392403
mutable NameAliasType *AliasTy;
23402404

2341-
SourceLoc TypeAliasLoc; // The location of the 'typealias' keyword
2405+
SourceLoc TypeAliasLoc; // The location of the 'typealias' keyword
23422406
TypeLoc UnderlyingTy;
23432407

23442408
public:
23452409
TypeAliasDecl(SourceLoc TypeAliasLoc, Identifier Name,
2346-
SourceLoc NameLoc, TypeLoc UnderlyingTy,
2347-
DeclContext *DC);
2410+
SourceLoc NameLoc, TypeLoc UnderlyingTy, DeclContext *DC);
23482411

23492412
SourceLoc getStartLoc() const { return TypeAliasLoc; }
23502413
SourceRange getSourceRange() const;
@@ -2581,29 +2644,15 @@ enum PointerTypeKind : unsigned {
25812644
/// These are not added to their enclosing type unless forced.
25822645
typedef std::function<void(SmallVectorImpl<Decl *> &)> DelayedDecl;
25832646

2584-
/// NominalTypeDecl - a declaration of a nominal type, like a struct. This
2585-
/// decl is always a DeclContext.
2586-
class NominalTypeDecl : public TypeDecl, public DeclContext,
2587-
public IterableDeclContext {
2647+
/// NominalTypeDecl - a declaration of a nominal type, like a struct.
2648+
class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
25882649
SourceRange Braces;
25892650

25902651
/// \brief The set of implicit members and protocols added to imported enum
25912652
/// types. These members and protocols are added to the NominalDecl only if
25922653
/// the nominal type is directly or indirectly referenced.
25932654
std::unique_ptr<DelayedDecl> DelayedMembers;
25942655

2595-
GenericParamList *GenericParams;
2596-
2597-
/// \brief The generic signature of this type.
2598-
///
2599-
/// This is the semantic representation of a generic parameters and the
2600-
/// requirements placed on them.
2601-
///
2602-
/// FIXME: The generic parameters here are also derivable from
2603-
/// \c GenericParams. However, we likely want to make \c GenericParams
2604-
/// the parsed representation, and not part of the module file.
2605-
GenericSignature *GenericSig = nullptr;
2606-
26072656
/// \brief The first extension of this type.
26082657
ExtensionDecl *FirstExtension = nullptr;
26092658

@@ -2693,10 +2742,9 @@ class NominalTypeDecl : public TypeDecl, public DeclContext,
26932742
SourceLoc NameLoc,
26942743
MutableArrayRef<TypeLoc> inherited,
26952744
GenericParamList *GenericParams) :
2696-
TypeDecl(K, DC, name, NameLoc, inherited),
2697-
DeclContext(DeclContextKind::NominalTypeDecl, DC),
2745+
GenericTypeDecl(K, DC, name, NameLoc, inherited, GenericParams),
26982746
IterableDeclContext(IterableDeclContextKind::NominalTypeDecl),
2699-
GenericParams(nullptr), DeclaredTy(nullptr)
2747+
DeclaredTy(nullptr)
27002748
{
27012749
setGenericParams(GenericParams);
27022750
NominalTypeDeclBits.HasDelayedMembers = false;
@@ -2711,8 +2759,6 @@ class NominalTypeDecl : public TypeDecl, public DeclContext,
27112759
friend class ProtocolType;
27122760

27132761
public:
2714-
using TypeDecl::getASTContext;
2715-
27162762
DeclRange getMembers(bool forceDelayedMembers = true) const;
27172763
SourceRange getBraces() const { return Braces; }
27182764

@@ -2778,36 +2824,6 @@ class NominalTypeDecl : public TypeDecl, public DeclContext,
27782824
return SearchedForFailableInits;
27792825
}
27802826

2781-
GenericParamList *getGenericParams() const { return GenericParams; }
2782-
2783-
/// Provide the set of parameters to a generic type, or null if
2784-
/// this function is not generic.
2785-
void setGenericParams(GenericParamList *params);
2786-
2787-
/// Set the generic signature of this type.
2788-
void setGenericSignature(GenericSignature *sig);
2789-
2790-
/// Retrieve the innermost generic parameter types.
2791-
ArrayRef<GenericTypeParamType *> getInnermostGenericParamTypes() const {
2792-
if (!GenericSig)
2793-
return { };
2794-
2795-
return GenericSig->getInnermostGenericParams();
2796-
}
2797-
2798-
/// Retrieve the generic requirements.
2799-
ArrayRef<Requirement> getGenericRequirements() const {
2800-
if (!GenericSig)
2801-
return { };
2802-
2803-
return GenericSig->getRequirements();
2804-
}
2805-
2806-
/// Retrieve the generic signature.
2807-
GenericSignature *getGenericSignature() const {
2808-
return GenericSig;
2809-
}
2810-
28112827
/// getDeclaredType - Retrieve the type declared by this entity.
28122828
Type getDeclaredType() const { return DeclaredTy; }
28132829

@@ -2895,8 +2911,6 @@ class NominalTypeDecl : public TypeDecl, public DeclContext,
28952911

28962912
void setConformanceLoader(LazyMemberLoader *resolver, uint64_t contextData);
28972913

2898-
using TypeDecl::getDeclaredInterfaceType;
2899-
29002914
/// classifyAsOptionalType - Decide whether this declaration is one
29012915
/// of the library-intrinsic Optional<T> or ImplicitlyUnwrappedOptional<T> types.
29022916
OptionalTypeKind classifyAsOptionalType() const;
@@ -2936,17 +2950,21 @@ class NominalTypeDecl : public TypeDecl, public DeclContext,
29362950
return D->getKind() >= DeclKind::First_NominalTypeDecl &&
29372951
D->getKind() <= DeclKind::Last_NominalTypeDecl;
29382952
}
2953+
static bool classof(const GenericTypeDecl *D) {
2954+
return D->getKind() >= DeclKind::First_NominalTypeDecl &&
2955+
D->getKind() <= DeclKind::Last_NominalTypeDecl;
2956+
}
2957+
29392958
static bool classof(const DeclContext *C) {
2940-
return C->getContextKind() == DeclContextKind::NominalTypeDecl;
2959+
auto GTD = dyn_cast<GenericTypeDecl>(C);
2960+
return GTD && classof(GTD);
29412961
}
29422962
static bool classof(const IterableDeclContext *C) {
29432963
return C->getIterableContextKind()
29442964
== IterableDeclContextKind::NominalTypeDecl;
29452965
}
29462966
static bool classof(const NominalTypeDecl *D) { return true; }
29472967
static bool classof(const ExtensionDecl *D) { return false; }
2948-
2949-
using DeclContext::operator new;
29502968
};
29512969

29522970
/// \brief This is the declaration of an enum.
@@ -2960,8 +2978,8 @@ class NominalTypeDecl : public TypeDecl, public DeclContext,
29602978
/// }
29612979
///
29622980
/// enum Optional<T> {
2963-
/// case None
2964-
/// case Some(T)
2981+
/// case none
2982+
/// case some(T)
29652983
/// }
29662984
/// \endcode
29672985
///
@@ -3027,14 +3045,19 @@ class EnumDecl : public NominalTypeDecl {
30273045
static bool classof(const Decl *D) {
30283046
return D->getKind() == DeclKind::Enum;
30293047
}
3048+
static bool classof(const GenericTypeDecl *D) {
3049+
return D->getKind() == DeclKind::Enum;
3050+
}
30303051
static bool classof(const NominalTypeDecl *D) {
30313052
return D->getKind() == DeclKind::Enum;
30323053
}
30333054
static bool classof(const DeclContext *C) {
3034-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3055+
auto GTD = dyn_cast<GenericTypeDecl>(C);
3056+
return GTD && classof(static_cast<const Decl*>(GTD));
30353057
}
30363058
static bool classof(const IterableDeclContext *C) {
3037-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3059+
auto NTD = dyn_cast<NominalTypeDecl>(C);
3060+
return NTD && classof(NTD);
30383061
}
30393062

30403063
/// Determine whether this enum declares a raw type in its inheritance clause.
@@ -3086,14 +3109,19 @@ class StructDecl : public NominalTypeDecl {
30863109
static bool classof(const Decl *D) {
30873110
return D->getKind() == DeclKind::Struct;
30883111
}
3112+
static bool classof(const GenericTypeDecl *D) {
3113+
return D->getKind() == DeclKind::Struct;
3114+
}
30893115
static bool classof(const NominalTypeDecl *D) {
30903116
return D->getKind() == DeclKind::Struct;
30913117
}
30923118
static bool classof(const DeclContext *C) {
3093-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3119+
auto GTD = dyn_cast<GenericTypeDecl>(C);
3120+
return GTD && classof(static_cast<const Decl*>(GTD));
30943121
}
30953122
static bool classof(const IterableDeclContext *C) {
3096-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3123+
auto NTD = dyn_cast<NominalTypeDecl>(C);
3124+
return NTD && classof(NTD);
30973125
}
30983126

30993127
/// Does this struct contain unreferenceable storage, such as C fields that
@@ -3282,14 +3310,19 @@ class ClassDecl : public NominalTypeDecl {
32823310
static bool classof(const Decl *D) {
32833311
return D->getKind() == DeclKind::Class;
32843312
}
3313+
static bool classof(const GenericTypeDecl *D) {
3314+
return D->getKind() == DeclKind::Class;
3315+
}
32853316
static bool classof(const NominalTypeDecl *D) {
32863317
return D->getKind() == DeclKind::Class;
32873318
}
32883319
static bool classof(const DeclContext *C) {
3289-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3320+
auto GTD = dyn_cast<GenericTypeDecl>(C);
3321+
return GTD && classof(static_cast<const Decl*>(GTD));
32903322
}
32913323
static bool classof(const IterableDeclContext *C) {
3292-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3324+
auto NTD = dyn_cast<NominalTypeDecl>(C);
3325+
return NTD && classof(NTD);
32933326
}
32943327
};
32953328

@@ -3544,14 +3577,19 @@ class ProtocolDecl : public NominalTypeDecl {
35443577
static bool classof(const Decl *D) {
35453578
return D->getKind() == DeclKind::Protocol;
35463579
}
3580+
static bool classof(const GenericTypeDecl *D) {
3581+
return D->getKind() == DeclKind::Protocol;
3582+
}
35473583
static bool classof(const NominalTypeDecl *D) {
35483584
return D->getKind() == DeclKind::Protocol;
35493585
}
35503586
static bool classof(const DeclContext *C) {
3551-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3587+
auto GTD = dyn_cast<GenericTypeDecl>(C);
3588+
return GTD && classof(static_cast<const Decl*>(GTD));
35523589
}
35533590
static bool classof(const IterableDeclContext *C) {
3554-
return isa<NominalTypeDecl>(C) && classof(cast<NominalTypeDecl>(C));
3591+
auto NTD = dyn_cast<NominalTypeDecl>(C);
3592+
return NTD && classof(NTD);
35553593
}
35563594
};
35573595

include/swift/AST/DeclContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ enum class DeclContextKind : uint8_t {
7373

7474
Module,
7575
FileUnit,
76-
NominalTypeDecl,
76+
GenericTypeDecl,
7777
ExtensionDecl,
7878
Last_DeclContextKind = ExtensionDecl
7979
};
@@ -229,7 +229,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
229229
/// \returns true if this is a type context, e.g., a struct, a class, an
230230
/// enum, a protocol, or an extension.
231231
bool isTypeContext() const {
232-
return getContextKind() == DeclContextKind::NominalTypeDecl ||
232+
return getContextKind() == DeclContextKind::GenericTypeDecl ||
233233
getContextKind() == DeclContextKind::ExtensionDecl;
234234
}
235235

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,8 +1076,8 @@ static void printContext(raw_ostream &os, DeclContext *dc) {
10761076
break;
10771077
}
10781078

1079-
case DeclContextKind::NominalTypeDecl:
1080-
printName(os, cast<NominalTypeDecl>(dc)->getName());
1079+
case DeclContextKind::GenericTypeDecl:
1080+
printName(os, cast<GenericTypeDecl>(dc)->getName());
10811081
break;
10821082

10831083
case DeclContextKind::ExtensionDecl:

lib/AST/ASTPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,8 +2794,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
27942794
// FIXME: print closures somehow.
27952795
return;
27962796

2797-
case DeclContextKind::NominalTypeDecl:
2798-
visit(cast<NominalTypeDecl>(DC)->getType());
2797+
case DeclContextKind::GenericTypeDecl:
2798+
visit(cast<GenericTypeDecl>(DC)->getType());
27992799
return;
28002800

28012801
case DeclContextKind::ExtensionDecl:

lib/AST/ASTVerifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,8 @@ struct ASTNodeBase {};
520520
case DeclContextKind::AbstractFunctionDecl:
521521
return cast<AbstractFunctionDecl>(dc)->getGenericParams();
522522

523-
case DeclContextKind::NominalTypeDecl:
524-
return cast<NominalTypeDecl>(dc)->getGenericParams();
523+
case DeclContextKind::GenericTypeDecl:
524+
return cast<GenericTypeDecl>(dc)->getGenericParams();
525525

526526
case DeclContextKind::ExtensionDecl:
527527
return cast<ExtensionDecl>(dc)->getGenericParams();
@@ -1598,7 +1598,7 @@ struct ASTNodeBase {};
15981598
return false;
15991599

16001600
case DeclContextKind::Initializer:
1601-
case DeclContextKind::NominalTypeDecl:
1601+
case DeclContextKind::GenericTypeDecl:
16021602
case DeclContextKind::ExtensionDecl:
16031603
case DeclContextKind::SubscriptDecl:
16041604
return hasEnclosingFunctionContext(dc->getParent());

0 commit comments

Comments
 (0)