[cfe-commits] r64630 - in /cfe/trunk: include/clang/AST/DeclBase.h lib/AST/DeclBase.cpp

Argiris Kirtzidis akyrtzi at gmail.com
Mon Feb 16 06:29:29 PST 2009


Author: akirtzidis
Date: Mon Feb 16 08:29:28 2009
New Revision: 64630

URL: http://llvm.org/viewvc/llvm-project?rev=64630&view=rev
Log:
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.

Modified:
    cfe/trunk/include/clang/AST/DeclBase.h
    cfe/trunk/lib/AST/DeclBase.cpp

Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=64630&r1=64629&r2=64630&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Mon Feb 16 08:29:28 2009
@@ -371,33 +371,6 @@
   /// 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<typename T> struct KindTrait {
-    static Decl::Kind getKind(const T *D) { return D->getKind(); }
-  };
-
-  // Used only by the ToDecl and FromDecl methods
-  template<typename To, typename From>
-  static To *CastTo(const From *D) {
-    Decl::Kind DK = KindTrait<From>::getKind(D);
-    switch(DK) {
-#define DECL_CONTEXT(Name) \
-      case Decl::Name:     \
-        return static_cast<Name##Decl*>(const_cast<From*>(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<Name##Decl*>(const_cast<From*>(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 @@
     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 @@
   friend class Decl;
 };
 
-template<> struct DeclContext::KindTrait<DeclContext> {
-  static Decl::Kind getKind(const DeclContext *D) { return D->DeclKind; }
-};
-
 inline bool Decl::isTemplateParameter() const {
   return getKind() == TemplateTypeParm || getKind() == NonTypeTemplateParm;
 }

Modified: cfe/trunk/lib/AST/DeclBase.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclBase.cpp?rev=64630&r1=64629&r2=64630&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Mon Feb 16 08:29:28 2009
@@ -213,17 +213,61 @@
 }
 
 Decl *Decl::castFromDeclContext (const DeclContext *D) {
-  return DeclContext::CastTo<Decl>(D);
+  Decl::Kind DK = D->getDeclKind();
+  switch(DK) {
+#define DECL_CONTEXT(Name) \
+    case Decl::Name:     \
+      return static_cast<Name##Decl*>(const_cast<DeclContext*>(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<Name##Decl*>(const_cast<DeclContext*>(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<DeclContext>(D);
+  Decl::Kind DK = D->getKind();
+  switch(DK) {
+#define DECL_CONTEXT(Name) \
+    case Decl::Name:     \
+      return static_cast<Name##Decl*>(const_cast<Decl*>(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<Name##Decl*>(const_cast<Decl*>(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<Decl>(this))
     return D->getDeclContext();





More information about the cfe-commits mailing list