Skip to content

Commit 22922c0

Browse files
authored
Merge pull request #14876 from davezarzycki/nfc_bypass_getContextKind
2 parents 7fe3c5c + d3b004e commit 22922c0

File tree

3 files changed

+65
-36
lines changed

3 files changed

+65
-36
lines changed

include/swift/AST/Decl.h

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,9 @@ class ExtensionDecl final : public GenericContext, public Decl,
17121712
return D->getKind() == DeclKind::Extension;
17131713
}
17141714
static bool classof(const DeclContext *C) {
1715-
return C->getContextKind() == DeclContextKind::ExtensionDecl;
1715+
if (auto D = C->getAsDeclOrDeclExtensionContext())
1716+
return classof(D);
1717+
return false;
17161718
}
17171719
static bool classof(const IterableDeclContext *C) {
17181720
return C->getIterableContextKind()
@@ -2003,7 +2005,9 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
20032005
return D->getKind() == DeclKind::TopLevelCode;
20042006
}
20052007
static bool classof(const DeclContext *C) {
2006-
return C->getContextKind() == DeclContextKind::TopLevelCodeDecl;
2008+
if (auto D = C->getAsDeclOrDeclExtensionContext())
2009+
return classof(D);
2010+
return false;
20072011
}
20082012

20092013
using DeclContext::operator new;
@@ -2465,7 +2469,9 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
24652469
using TypeDecl::getDeclaredInterfaceType;
24662470

24672471
static bool classof(const DeclContext *C) {
2468-
return C->getContextKind() == DeclContextKind::GenericTypeDecl;
2472+
if (auto D = C->getAsDeclOrDeclExtensionContext())
2473+
return classof(D);
2474+
return false;
24692475
}
24702476
static bool classof(const Decl *D) {
24712477
return D->getKind() >= DeclKind::First_GenericTypeDecl &&
@@ -2533,8 +2539,9 @@ class TypeAliasDecl : public GenericTypeDecl {
25332539
return D->getKind() == DeclKind::TypeAlias;
25342540
}
25352541
static bool classof(const DeclContext *C) {
2536-
auto GTD = dyn_cast<GenericTypeDecl>(C);
2537-
return GTD && classof(GTD);
2542+
if (auto D = C->getAsDeclOrDeclExtensionContext())
2543+
return classof(D);
2544+
return false;
25382545
}
25392546
};
25402547

@@ -3038,8 +3045,9 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
30383045
}
30393046

30403047
static bool classof(const DeclContext *C) {
3041-
auto GTD = dyn_cast<GenericTypeDecl>(C);
3042-
return GTD && classof(GTD);
3048+
if (auto D = C->getAsDeclOrDeclExtensionContext())
3049+
return classof(D);
3050+
return false;
30433051
}
30443052
static bool classof(const IterableDeclContext *C) {
30453053
return C->getIterableContextKind()
@@ -3183,8 +3191,9 @@ class EnumDecl final : public NominalTypeDecl {
31833191
return D->getKind() == DeclKind::Enum;
31843192
}
31853193
static bool classof(const DeclContext *C) {
3186-
auto GTD = dyn_cast<GenericTypeDecl>(C);
3187-
return GTD && classof(static_cast<const Decl*>(GTD));
3194+
if (auto D = C->getAsDeclOrDeclExtensionContext())
3195+
return classof(D);
3196+
return false;
31883197
}
31893198
static bool classof(const IterableDeclContext *C) {
31903199
auto NTD = dyn_cast<NominalTypeDecl>(C);
@@ -3252,8 +3261,9 @@ class StructDecl final : public NominalTypeDecl {
32523261
return D->getKind() == DeclKind::Struct;
32533262
}
32543263
static bool classof(const DeclContext *C) {
3255-
auto GTD = dyn_cast<GenericTypeDecl>(C);
3256-
return GTD && classof(static_cast<const Decl*>(GTD));
3264+
if (auto D = C->getAsDeclOrDeclExtensionContext())
3265+
return classof(D);
3266+
return false;
32573267
}
32583268
static bool classof(const IterableDeclContext *C) {
32593269
auto NTD = dyn_cast<NominalTypeDecl>(C);
@@ -3509,8 +3519,9 @@ class ClassDecl final : public NominalTypeDecl {
35093519
return D->getKind() == DeclKind::Class;
35103520
}
35113521
static bool classof(const DeclContext *C) {
3512-
auto GTD = dyn_cast<GenericTypeDecl>(C);
3513-
return GTD && classof(static_cast<const Decl*>(GTD));
3522+
if (auto D = C->getAsDeclOrDeclExtensionContext())
3523+
return classof(D);
3524+
return false;
35143525
}
35153526
static bool classof(const IterableDeclContext *C) {
35163527
auto NTD = dyn_cast<NominalTypeDecl>(C);
@@ -3827,8 +3838,9 @@ class ProtocolDecl final : public NominalTypeDecl {
38273838
return D->getKind() == DeclKind::Protocol;
38283839
}
38293840
static bool classof(const DeclContext *C) {
3830-
auto GTD = dyn_cast<GenericTypeDecl>(C);
3831-
return GTD && classof(static_cast<const Decl*>(GTD));
3841+
if (auto D = C->getAsDeclOrDeclExtensionContext())
3842+
return classof(D);
3843+
return false;
38323844
}
38333845
static bool classof(const IterableDeclContext *C) {
38343846
auto NTD = dyn_cast<NominalTypeDecl>(C);
@@ -4847,7 +4859,9 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
48474859
}
48484860

48494861
static bool classof(const DeclContext *DC) {
4850-
return DC->getContextKind() == DeclContextKind::SubscriptDecl;
4862+
if (auto D = DC->getAsDeclOrDeclExtensionContext())
4863+
return classof(D);
4864+
return false;
48514865
}
48524866

48534867
using DeclContext::operator new;
@@ -5230,7 +5244,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
52305244
}
52315245

52325246
static bool classof(const DeclContext *DC) {
5233-
return DC->getContextKind() == DeclContextKind::AbstractFunctionDecl;
5247+
if (auto D = DC->getAsDeclOrDeclExtensionContext())
5248+
return classof(D);
5249+
return false;
52345250
}
52355251

52365252
/// True if the declaration is forced to be statically dispatched.
@@ -5496,8 +5512,8 @@ class FuncDecl : public AbstractFunctionDecl {
54965512
return classof(static_cast<const Decl*>(D));
54975513
}
54985514
static bool classof(const DeclContext *DC) {
5499-
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
5500-
return classof(fn);
5515+
if (auto D = DC->getAsDeclOrDeclExtensionContext())
5516+
return classof(D);
55015517
return false;
55025518
}
55035519

@@ -5617,8 +5633,8 @@ class AccessorDecl final : public FuncDecl {
56175633
return classof(static_cast<const Decl*>(D));
56185634
}
56195635
static bool classof(const DeclContext *DC) {
5620-
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
5621-
return classof(fn);
5636+
if (auto D = DC->getAsDeclOrDeclExtensionContext())
5637+
return classof(D);
56225638
return false;
56235639
}
56245640
};
@@ -6036,8 +6052,8 @@ class ConstructorDecl : public AbstractFunctionDecl {
60366052
return classof(static_cast<const Decl*>(D));
60376053
}
60386054
static bool classof(const DeclContext *DC) {
6039-
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
6040-
return classof(fn);
6055+
if (auto D = DC->getAsDeclOrDeclExtensionContext())
6056+
return classof(D);
60416057
return false;
60426058
}
60436059
};
@@ -6080,8 +6096,8 @@ class DestructorDecl : public AbstractFunctionDecl {
60806096
return classof(static_cast<const Decl*>(D));
60816097
}
60826098
static bool classof(const DeclContext *DC) {
6083-
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
6084-
return classof(fn);
6099+
if (auto D = DC->getAsDeclOrDeclExtensionContext())
6100+
return classof(D);
60856101
return false;
60866102
}
60876103
};
@@ -6584,6 +6600,12 @@ inline const GenericContext *Decl::getAsGenericContext() const {
65846600
}
65856601
}
65866602

6603+
inline bool DeclContext::isExtensionContext() const {
6604+
if (auto D = getAsDeclOrDeclExtensionContext())
6605+
return ExtensionDecl::classof(D);
6606+
return false;
6607+
}
6608+
65876609
inline bool DeclContext::classof(const Decl *D) {
65886610
switch (D->getKind()) { //
65896611
default: return false;

include/swift/AST/DeclContext.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,18 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
262262
}
263263

264264
/// isModuleContext - Return true if this is a subclass of Module.
265-
bool isModuleContext() const {
266-
return getContextKind() == DeclContextKind::Module;
267-
}
265+
bool isModuleContext() const; // see swift/AST/Module.h
268266

269267
/// \returns true if this is a context with module-wide scope, e.g. a module
270268
/// or a source file.
271-
bool isModuleScopeContext() const {
272-
return getContextKind() == DeclContextKind::Module ||
273-
getContextKind() == DeclContextKind::FileUnit;
274-
}
269+
bool isModuleScopeContext() const; // see swift/AST/Module.h
275270

276271
/// \returns true if this is a type context, e.g., a struct, a class, an
277272
/// enum, a protocol, or an extension.
278273
bool isTypeContext() const;
279274

280275
/// \brief Determine whether this is an extension context.
281-
bool isExtensionContext() const {
282-
return getContextKind() == DeclContextKind::ExtensionDecl;
283-
}
276+
bool isExtensionContext() const; // see swift/AST/Decl.h
284277

285278
/// If this DeclContext is a NominalType declaration or an
286279
/// extension thereof, return the NominalTypeDecl.

include/swift/AST/Module.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
492492
SourceRange getSourceRange() const { return SourceRange(); }
493493

494494
static bool classof(const DeclContext *DC) {
495-
return DC->getContextKind() == DeclContextKind::Module;
495+
if (auto D = DC->getAsDeclOrDeclExtensionContext())
496+
return classof(D);
497+
return false;
496498
}
497499

498500
static bool classof(const Decl *D) {
@@ -1219,6 +1221,18 @@ class ModuleEntity {
12191221
explicit operator bool() const { return !Mod.isNull(); }
12201222
};
12211223

1224+
inline bool DeclContext::isModuleContext() const {
1225+
if (auto D = getAsDeclOrDeclExtensionContext())
1226+
return ModuleDecl::classof(D);
1227+
return false;
1228+
}
1229+
1230+
inline bool DeclContext::isModuleScopeContext() const {
1231+
if (ParentAndKind.getInt() == ASTHierarchy::FileUnit)
1232+
return true;
1233+
return isModuleContext();
1234+
}
1235+
12221236
} // end namespace swift
12231237

12241238
namespace llvm {

0 commit comments

Comments
 (0)