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

Chris Lattner sabre at nondot.org
Fri Mar 27 11:46:15 PDT 2009


Author: lattner
Date: Fri Mar 27 13:46:15 2009
New Revision: 67858

URL: http://llvm.org/viewvc/llvm-project?rev=67858&view=rev
Log:
change Decl::DeclCtx to use a PointerIntPair instead of hand bitmangling.

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

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

==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Fri Mar 27 13:46:15 2009
@@ -109,19 +109,25 @@
   ///   }
   ///   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 @@
 
   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 @@
   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 @@
   ///   }
   ///   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

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

==============================================================================
--- cfe/trunk/lib/AST/DeclBase.cpp (original)
+++ cfe/trunk/lib/AST/DeclBase.cpp Fri Mar 27 13:46:15 2009
@@ -121,7 +121,8 @@
   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 @@
     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;
   }

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

==============================================================================
--- cfe/trunk/lib/AST/DeclSerialization.cpp (original)
+++ cfe/trunk/lib/AST/DeclSerialization.cpp Fri Mar 27 13:46:15 2009
@@ -125,7 +125,7 @@
   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 @@
   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);





More information about the cfe-commits mailing list