From: Chris Lattner Date: Fri, 27 Mar 2009 18:46:15 +0000 (+0000) Subject: change Decl::DeclCtx to use a PointerIntPair instead of hand bitmangling. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=10d8379676f32b493740a5509e2cd7f0d6433139;p=clang change Decl::DeclCtx to use a PointerIntPair instead of hand bitmangling. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67858 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 5ee607b88e..002bab79df 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -109,19 +109,25 @@ private: /// } /// void A::f(); // SemanticDC == namespace 'A' /// // LexicalDC == global namespace - uintptr_t DeclCtx; + llvm::PointerIntPair 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(DeclCtx & ~0x1); + assert(isOutOfSemaDC() && "Invalid accessor"); + return static_cast(DeclCtx.getPointer()); } + inline DeclContext *getSemanticDC() const { + assert(isInSemaDC() && "Invalid accessor"); + return static_cast(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(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(DeclCtx); - return getMultipleDC()->SemanticDC; + return const_cast(this)->getDeclContext(); } DeclContext *getDeclContext() { - return const_cast( - const_cast(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(DeclCtx); + return getSemanticDC(); return getMultipleDC()->LexicalDC; } - DeclContext *getLexicalDeclContext() { - return const_cast( - const_cast(this)->getLexicalDeclContext()); + const DeclContext *getLexicalDeclContext() const { + return const_cast(this)->getLexicalDeclContext(); } - + void setLexicalDeclContext(DeclContext *DC); /// getNextDeclarator - If this decl was part of a multi-declarator diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index cb12618443..47059d952c 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -121,7 +121,8 @@ void Decl::setDeclContext(DeclContext *DC) { if (isOutOfSemaDC()) delete getMultipleDC(); - DeclCtx = reinterpret_cast(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(MDC) | 0x1; + DeclCtx.setPointer(MDC); + DeclCtx.setInt(true); } else { getMultipleDC()->LexicalDC = DC; } diff --git a/lib/AST/DeclSerialization.cpp b/lib/AST/DeclSerialization.cpp index e4534a2686..4b32d81936 100644 --- a/lib/AST/DeclSerialization.cpp +++ b/lib/AST/DeclSerialization.cpp @@ -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(X)); } else { MultipleDC *MDC = new MultipleDC(); - Dcl->DeclCtx = reinterpret_cast(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);