]> granicus.if.org Git - clang/commitdiff
change Decl::DeclCtx to use a PointerIntPair instead of hand bitmangling.
authorChris Lattner <sabre@nondot.org>
Fri, 27 Mar 2009 18:46:15 +0000 (18:46 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 27 Mar 2009 18:46:15 +0000 (18:46 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67858 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 5ee607b88e5d74cc19afa5b64dec5e89b2b6f81e..002bab79dfa7185088697dc6822120b922a307c4 100644 (file)
@@ -109,19 +109,25 @@ private:
   ///   }
   ///   void A::f(); // SemanticDC == namespace 'A'
   ///                // LexicalDC == global namespace
-  uintptr_t DeclCtx;
+  llvm::PointerIntPair<void*, 1, bool> DeclCtx;
 
   struct MultipleDC {
     DeclContext *SemanticDC;
     DeclContext *LexicalDC;
   };
 
-  inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; }
-  inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; }
+  inline bool isInSemaDC() const { return DeclCtx.getInt() == 0; }
+  inline bool isOutOfSemaDC() const { return DeclCtx.getInt() != 0; }
   inline MultipleDC *getMultipleDC() const {
-    return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1);
+    assert(isOutOfSemaDC() && "Invalid accessor");
+    return static_cast<MultipleDC*>(DeclCtx.getPointer());
   }
 
+  inline DeclContext *getSemanticDC() const {
+    assert(isInSemaDC() && "Invalid accessor");
+    return static_cast<DeclContext*>(DeclCtx.getPointer());
+  }
+  
   /// Loc - The location that this decl.
   SourceLocation Loc;
   
@@ -152,7 +158,7 @@ protected:
 
   Decl(Kind DK, DeclContext *DC, SourceLocation L) 
     : NextDeclarator(0), NextDeclInScope(0), 
-      DeclCtx(reinterpret_cast<uintptr_t>(DC)), 
+      DeclCtx(DC, 0), 
       Loc(L), DeclKind(DK), InvalidDecl(0),
       HasAttrs(false), Implicit(false), Access(AS_none) {
     if (Decl::CollectingStats()) addDeclKind(DK);
@@ -172,13 +178,12 @@ public:
   const char *getDeclKindName() const;
   
   const DeclContext *getDeclContext() const {
-    if (isInSemaDC())
-      return reinterpret_cast<DeclContext*>(DeclCtx);
-    return getMultipleDC()->SemanticDC;
+    return const_cast<Decl*>(this)->getDeclContext();
   }
   DeclContext *getDeclContext() {
-    return const_cast<DeclContext*>(
-                         const_cast<const Decl*>(this)->getDeclContext());
+    if (isInSemaDC())
+      return getSemanticDC();
+    return getMultipleDC()->SemanticDC;
   }
 
   void setAccess(AccessSpecifier AS) {
@@ -281,16 +286,15 @@ public:
   ///   }
   ///   void A::f(); // SemanticDC == namespace 'A'
   ///                // LexicalDC == global namespace
-  const DeclContext *getLexicalDeclContext() const {
+  DeclContext *getLexicalDeclContext() {
     if (isInSemaDC())
-      return reinterpret_cast<DeclContext*>(DeclCtx);
+      return getSemanticDC();
     return getMultipleDC()->LexicalDC;
   }
-  DeclContext *getLexicalDeclContext() {
-    return const_cast<DeclContext*>(
-                  const_cast<const Decl*>(this)->getLexicalDeclContext());
+  const DeclContext *getLexicalDeclContext() const {
+    return const_cast<Decl*>(this)->getLexicalDeclContext();
   }
-
+  
   void setLexicalDeclContext(DeclContext *DC);
 
   /// getNextDeclarator - If this decl was part of a multi-declarator
index cb12618443506bca5e0322bb9eacf10491036ab0..47059d952c105e4761f6741b28e81160f46d5d3d 100644 (file)
@@ -121,7 +121,8 @@ void Decl::setDeclContext(DeclContext *DC) {
   if (isOutOfSemaDC())
     delete getMultipleDC();
   
-  DeclCtx = reinterpret_cast<uintptr_t>(DC);
+  DeclCtx.setPointer(DC);
+  DeclCtx.setInt(false);
 }
 
 void Decl::setLexicalDeclContext(DeclContext *DC) {
@@ -132,7 +133,8 @@ void Decl::setLexicalDeclContext(DeclContext *DC) {
     MultipleDC *MDC = new MultipleDC();
     MDC->SemanticDC = getDeclContext();
     MDC->LexicalDC = DC;
-    DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
+    DeclCtx.setPointer(MDC);
+    DeclCtx.setInt(true);
   } else {
     getMultipleDC()->LexicalDC = DC;
   }
index e4534a26865e985e4b00c9fd34f4c6882ce162a1..4b32d819365b6b2826d81ec5c197089a46c143a1 100644 (file)
@@ -125,7 +125,7 @@ Decl* Decl::Create(Deserializer& D, ASTContext& C) {
   Dcl->Implicit = D.ReadBool();
   Dcl->Access = D.ReadInt();
 
-  assert(Dcl->DeclCtx == 0);
+  assert(Dcl->DeclCtx.getOpaqueValue() == 0);
 
   const SerializedPtrID &SemaDCPtrID = D.ReadPtrID();
   const SerializedPtrID &LexicalDCPtrID = D.ReadPtrID();
@@ -133,11 +133,14 @@ Decl* Decl::Create(Deserializer& D, ASTContext& C) {
   if (SemaDCPtrID == LexicalDCPtrID) {
     // Allow back-patching.  Observe that we register the variable of the
     // *object* for back-patching. Its actual value will get filled in later.
-    D.ReadUIntPtr(Dcl->DeclCtx, SemaDCPtrID); 
+    uintptr_t X;
+    D.ReadUIntPtr(X, SemaDCPtrID); 
+    Dcl->DeclCtx.setFromOpaqueValue(reinterpret_cast<void*>(X));
   }
   else {
     MultipleDC *MDC = new MultipleDC();
-    Dcl->DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
+    Dcl->DeclCtx.setPointer(MDC);
+    Dcl->DeclCtx.setInt(true);
     // 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);