]> granicus.if.org Git - clang/commitdiff
switch DeclBase::DeclCtx to the new happy and type-safe
authorChris Lattner <sabre@nondot.org>
Sun, 29 Mar 2009 06:06:59 +0000 (06:06 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 29 Mar 2009 06:06:59 +0000 (06:06 +0000)
llvm::PointerUnion class.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67988 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclBase.h
lib/AST/DeclBase.cpp
lib/AST/DeclSerialization.cpp

index e98a37850206df687afa33ce1777b76ec2ddfb23..aaec16ee447fa6550910af5a25f1ad646e20c3dc 100644 (file)
@@ -19,6 +19,7 @@
 // FIXME: Layering violation
 #include "clang/Parse/AccessSpecifier.h"
 #include "llvm/Support/PrettyStackTrace.h"
+#include "llvm/ADT/PointerUnion.h"
 
 namespace clang {
 class DeclContext;
@@ -113,6 +114,12 @@ private:
 
   friend class DeclContext;
 
+  struct MultipleDC {
+    DeclContext *SemanticDC;
+    DeclContext *LexicalDC;
+  };
+  
+  
   /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
   /// For declarations that don't contain C++ scope specifiers, it contains
   /// the DeclContext where the Decl was declared.
@@ -126,23 +133,15 @@ private:
   ///   }
   ///   void A::f(); // SemanticDC == namespace 'A'
   ///                // LexicalDC == global namespace
-  llvm::PointerIntPair<DeclContext*, 1, bool> DeclCtx;
+  llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
 
-  struct MultipleDC {
-    DeclContext *SemanticDC;
-    DeclContext *LexicalDC;
-  };
-
-  inline bool isInSemaDC() const { return DeclCtx.getInt() == 0; }
-  inline bool isOutOfSemaDC() const { return DeclCtx.getInt() != 0; }
+  inline bool isInSemaDC() const    { return DeclCtx.is<DeclContext*>(); }
+  inline bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
   inline MultipleDC *getMultipleDC() const {
-    assert(isOutOfSemaDC() && "Invalid accessor");
-    return reinterpret_cast<MultipleDC*>(DeclCtx.getPointer());
+    return DeclCtx.get<MultipleDC*>();
   }
-
   inline DeclContext *getSemanticDC() const {
-    assert(isInSemaDC() && "Invalid accessor");
-    return static_cast<DeclContext*>(DeclCtx.getPointer());
+    return DeclCtx.get<DeclContext*>();
   }
   
   /// Loc - The location that this decl.
@@ -178,7 +177,7 @@ protected:
 
   Decl(Kind DK, DeclContext *DC, SourceLocation L) 
     : NextDeclarator(0), NextDeclInContext(0), 
-      DeclCtx(DC, 0), 
+      DeclCtx(DC), 
       Loc(L), DeclKind(DK), InvalidDecl(0),
       HasAttrs(false), Implicit(false), 
       IdentifierNamespace(getIdentifierNamespaceForKind(DK)), Access(AS_none) {
index 9ab269d305f674ff62da28e4b56284643bbda71e..d45ad4177c71a00c89f2aa9f856c22ec17d39bc1 100644 (file)
@@ -129,8 +129,7 @@ void Decl::setDeclContext(DeclContext *DC) {
   if (isOutOfSemaDC())
     delete getMultipleDC();
   
-  DeclCtx.setPointer(DC);
-  DeclCtx.setInt(false);
+  DeclCtx = DC;
 }
 
 void Decl::setLexicalDeclContext(DeclContext *DC) {
@@ -141,8 +140,7 @@ void Decl::setLexicalDeclContext(DeclContext *DC) {
     MultipleDC *MDC = new MultipleDC();
     MDC->SemanticDC = getDeclContext();
     MDC->LexicalDC = DC;
-    DeclCtx.setPointer(reinterpret_cast<DeclContext*>(MDC));
-    DeclCtx.setInt(true);
+    DeclCtx = MDC;
   } else {
     getMultipleDC()->LexicalDC = DC;
   }
index 965874502d0c80eadec7750ee1850c6b195f7dec..8e3ef08d2c8b8f8778286c4aa303cbbcceef410d 100644 (file)
@@ -135,12 +135,10 @@ Decl* Decl::Create(Deserializer& D, ASTContext& C) {
     // *object* for back-patching. Its actual value will get filled in later.
     uintptr_t X;
     D.ReadUIntPtr(X, SemaDCPtrID); 
-    Dcl->DeclCtx.setFromOpaqueValue(reinterpret_cast<void*>(X));
-  }
-  else {
+    Dcl->DeclCtx = reinterpret_cast<DeclContext*>(X);
+  } else {
     MultipleDC *MDC = new MultipleDC();
-    Dcl->DeclCtx.setPointer(reinterpret_cast<DeclContext*>(MDC));
-    Dcl->DeclCtx.setInt(true);
+    Dcl->DeclCtx = MDC;
     // Allow back-patching.  Observe that we register the variable of the
     // *object* for back-patching. Its actual value will get filled in later.
     D.ReadPtr(MDC->SemanticDC, SemaDCPtrID);