From 3d7641e090cf113dec64306a1597d3e4523e2a55 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Mon, 16 Feb 2009 14:29:28 +0000 Subject: [PATCH] DeclContext had its "casting machinery" inside the class definition so that if a new declaration context Decl appeared, the necessary changes would be in one place. Since, now, only DeclNodes.def needs to be modified, move things out-of-line and simplify the DeclContext class. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64630 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclBase.h | 47 +---------------------------------- lib/AST/DeclBase.cpp | 48 ++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 8172ab22c7..064fa310d3 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -371,33 +371,6 @@ class DeclContext { /// another pointer. Decl *LastDecl; - // Used in the CastTo template to get the DeclKind - // from a Decl or a DeclContext. DeclContext doesn't have a getKind() method - // to avoid 'ambiguous access' compiler errors. - template struct KindTrait { - static Decl::Kind getKind(const T *D) { return D->getKind(); } - }; - - // Used only by the ToDecl and FromDecl methods - template - static To *CastTo(const From *D) { - Decl::Kind DK = KindTrait::getKind(D); - switch(DK) { -#define DECL_CONTEXT(Name) \ - case Decl::Name: \ - return static_cast(const_cast(D)); -#define DECL_CONTEXT_BASE(Name) -#include "clang/AST/DeclNodes.def" - default: -#define DECL_CONTEXT_BASE(Name) \ - if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ - return static_cast(const_cast(D)); -#include "clang/AST/DeclNodes.def" - assert(false && "a decl that inherits DeclContext isn't handled"); - return 0; - } - } - /// isLookupMap - Determine if the lookup structure is a /// DenseMap. Othewise, it is an array. bool isLookupMap() const { return LookupPtr.getInt() == LookupIsMap; } @@ -780,21 +753,7 @@ public: return getUsingDirectives().second; } - static bool classof(const Decl *D) { - switch (D->getKind()) { -#define DECL_CONTEXT(Name) case Decl::Name: -#define DECL_CONTEXT_BASE(Name) -#include "clang/AST/DeclNodes.def" - return true; - default: -#define DECL_CONTEXT_BASE(Name) \ - if (D->getKind() >= Decl::Name##First && \ - D->getKind() <= Decl::Name##Last) \ - return true; -#include "clang/AST/DeclNodes.def" - return false; - } - } + static bool classof(const Decl *D); static bool classof(const DeclContext *D) { return true; } #define DECL_CONTEXT(Name) \ static bool classof(const Name##Decl *D) { return true; } @@ -810,10 +769,6 @@ private: friend class Decl; }; -template<> struct DeclContext::KindTrait { - static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; } -}; - inline bool Decl::isTemplateParameter() const { return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm; } diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index 91f2bf2765..a8ca34dca5 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -213,17 +213,61 @@ void Decl::Destroy(ASTContext& C) { } Decl *Decl::castFromDeclContext (const DeclContext *D) { - return DeclContext::CastTo(D); + Decl::Kind DK = D->getDeclKind(); + switch(DK) { +#define DECL_CONTEXT(Name) \ + case Decl::Name: \ + return static_cast(const_cast(D)); +#define DECL_CONTEXT_BASE(Name) +#include "clang/AST/DeclNodes.def" + default: +#define DECL_CONTEXT_BASE(Name) \ + if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ + return static_cast(const_cast(D)); +#include "clang/AST/DeclNodes.def" + assert(false && "a decl that inherits DeclContext isn't handled"); + return 0; + } } DeclContext *Decl::castToDeclContext(const Decl *D) { - return DeclContext::CastTo(D); + Decl::Kind DK = D->getKind(); + switch(DK) { +#define DECL_CONTEXT(Name) \ + case Decl::Name: \ + return static_cast(const_cast(D)); +#define DECL_CONTEXT_BASE(Name) +#include "clang/AST/DeclNodes.def" + default: +#define DECL_CONTEXT_BASE(Name) \ + if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \ + return static_cast(const_cast(D)); +#include "clang/AST/DeclNodes.def" + assert(false && "a decl that inherits DeclContext isn't handled"); + return 0; + } } //===----------------------------------------------------------------------===// // DeclContext Implementation //===----------------------------------------------------------------------===// +bool DeclContext::classof(const Decl *D) { + switch (D->getKind()) { +#define DECL_CONTEXT(Name) case Decl::Name: +#define DECL_CONTEXT_BASE(Name) +#include "clang/AST/DeclNodes.def" + return true; + default: +#define DECL_CONTEXT_BASE(Name) \ + if (D->getKind() >= Decl::Name##First && \ + D->getKind() <= Decl::Name##Last) \ + return true; +#include "clang/AST/DeclNodes.def" + return false; + } +} + const DeclContext *DeclContext::getParent() const { if (const Decl *D = dyn_cast(this)) return D->getDeclContext(); -- 2.40.0