Skip to content

[AST] NFC/perf: Bypass getContextKind() in more places #14876

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 1 commit into from
Feb 28, 2018
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
72 changes: 47 additions & 25 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1712,7 +1712,9 @@ class ExtensionDecl final : public GenericContext, public Decl,
return D->getKind() == DeclKind::Extension;
}
static bool classof(const DeclContext *C) {
return C->getContextKind() == DeclContextKind::ExtensionDecl;
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
static bool classof(const IterableDeclContext *C) {
return C->getIterableContextKind()
Expand Down Expand Up @@ -2003,7 +2005,9 @@ class TopLevelCodeDecl : public DeclContext, public Decl {
return D->getKind() == DeclKind::TopLevelCode;
}
static bool classof(const DeclContext *C) {
return C->getContextKind() == DeclContextKind::TopLevelCodeDecl;
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}

using DeclContext::operator new;
Expand Down Expand Up @@ -2465,7 +2469,9 @@ class GenericTypeDecl : public GenericContext, public TypeDecl {
using TypeDecl::getDeclaredInterfaceType;

static bool classof(const DeclContext *C) {
return C->getContextKind() == DeclContextKind::GenericTypeDecl;
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
static bool classof(const Decl *D) {
return D->getKind() >= DeclKind::First_GenericTypeDecl &&
Expand Down Expand Up @@ -2533,8 +2539,9 @@ class TypeAliasDecl : public GenericTypeDecl {
return D->getKind() == DeclKind::TypeAlias;
}
static bool classof(const DeclContext *C) {
auto GTD = dyn_cast<GenericTypeDecl>(C);
return GTD && classof(GTD);
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
};

Expand Down Expand Up @@ -3038,8 +3045,9 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
}

static bool classof(const DeclContext *C) {
auto GTD = dyn_cast<GenericTypeDecl>(C);
return GTD && classof(GTD);
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
static bool classof(const IterableDeclContext *C) {
return C->getIterableContextKind()
Expand Down Expand Up @@ -3183,8 +3191,9 @@ class EnumDecl final : public NominalTypeDecl {
return D->getKind() == DeclKind::Enum;
}
static bool classof(const DeclContext *C) {
auto GTD = dyn_cast<GenericTypeDecl>(C);
return GTD && classof(static_cast<const Decl*>(GTD));
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
static bool classof(const IterableDeclContext *C) {
auto NTD = dyn_cast<NominalTypeDecl>(C);
Expand Down Expand Up @@ -3252,8 +3261,9 @@ class StructDecl final : public NominalTypeDecl {
return D->getKind() == DeclKind::Struct;
}
static bool classof(const DeclContext *C) {
auto GTD = dyn_cast<GenericTypeDecl>(C);
return GTD && classof(static_cast<const Decl*>(GTD));
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
static bool classof(const IterableDeclContext *C) {
auto NTD = dyn_cast<NominalTypeDecl>(C);
Expand Down Expand Up @@ -3509,8 +3519,9 @@ class ClassDecl final : public NominalTypeDecl {
return D->getKind() == DeclKind::Class;
}
static bool classof(const DeclContext *C) {
auto GTD = dyn_cast<GenericTypeDecl>(C);
return GTD && classof(static_cast<const Decl*>(GTD));
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
static bool classof(const IterableDeclContext *C) {
auto NTD = dyn_cast<NominalTypeDecl>(C);
Expand Down Expand Up @@ -3827,8 +3838,9 @@ class ProtocolDecl final : public NominalTypeDecl {
return D->getKind() == DeclKind::Protocol;
}
static bool classof(const DeclContext *C) {
auto GTD = dyn_cast<GenericTypeDecl>(C);
return GTD && classof(static_cast<const Decl*>(GTD));
if (auto D = C->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
static bool classof(const IterableDeclContext *C) {
auto NTD = dyn_cast<NominalTypeDecl>(C);
Expand Down Expand Up @@ -4847,7 +4859,9 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
}

static bool classof(const DeclContext *DC) {
return DC->getContextKind() == DeclContextKind::SubscriptDecl;
if (auto D = DC->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}

using DeclContext::operator new;
Expand Down Expand Up @@ -5230,7 +5244,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
}

static bool classof(const DeclContext *DC) {
return DC->getContextKind() == DeclContextKind::AbstractFunctionDecl;
if (auto D = DC->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}

/// True if the declaration is forced to be statically dispatched.
Expand Down Expand Up @@ -5496,8 +5512,8 @@ class FuncDecl : public AbstractFunctionDecl {
return classof(static_cast<const Decl*>(D));
}
static bool classof(const DeclContext *DC) {
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
return classof(fn);
if (auto D = DC->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}

Expand Down Expand Up @@ -5617,8 +5633,8 @@ class AccessorDecl final : public FuncDecl {
return classof(static_cast<const Decl*>(D));
}
static bool classof(const DeclContext *DC) {
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
return classof(fn);
if (auto D = DC->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
};
Expand Down Expand Up @@ -6036,8 +6052,8 @@ class ConstructorDecl : public AbstractFunctionDecl {
return classof(static_cast<const Decl*>(D));
}
static bool classof(const DeclContext *DC) {
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
return classof(fn);
if (auto D = DC->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
};
Expand Down Expand Up @@ -6080,8 +6096,8 @@ class DestructorDecl : public AbstractFunctionDecl {
return classof(static_cast<const Decl*>(D));
}
static bool classof(const DeclContext *DC) {
if (auto fn = dyn_cast<AbstractFunctionDecl>(DC))
return classof(fn);
if (auto D = DC->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}
};
Expand Down Expand Up @@ -6584,6 +6600,12 @@ inline const GenericContext *Decl::getAsGenericContext() const {
}
}

inline bool DeclContext::isExtensionContext() const {
if (auto D = getAsDeclOrDeclExtensionContext())
return ExtensionDecl::classof(D);
return false;
}

inline bool DeclContext::classof(const Decl *D) {
switch (D->getKind()) { //
default: return false;
Expand Down
13 changes: 3 additions & 10 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -262,25 +262,18 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
}

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

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

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

/// \brief Determine whether this is an extension context.
bool isExtensionContext() const {
return getContextKind() == DeclContextKind::ExtensionDecl;
}
bool isExtensionContext() const; // see swift/AST/Decl.h

/// If this DeclContext is a NominalType declaration or an
/// extension thereof, return the NominalTypeDecl.
Expand Down
16 changes: 15 additions & 1 deletion include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
SourceRange getSourceRange() const { return SourceRange(); }

static bool classof(const DeclContext *DC) {
return DC->getContextKind() == DeclContextKind::Module;
if (auto D = DC->getAsDeclOrDeclExtensionContext())
return classof(D);
return false;
}

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

inline bool DeclContext::isModuleContext() const {
if (auto D = getAsDeclOrDeclExtensionContext())
return ModuleDecl::classof(D);
return false;
}

inline bool DeclContext::isModuleScopeContext() const {
if (ParentAndKind.getInt() == ASTHierarchy::FileUnit)
return true;
return isModuleContext();
}

} // end namespace swift

namespace llvm {
Expand Down