change Decl::DeclCtx to use a PointerIntPair instead of hand bitmangling.

llvm-svn: 67858
This commit is contained in:
Chris Lattner 2009-03-27 18:46:15 +00:00
parent a2d609e2f1
commit 586c66d5ba
3 changed files with 30 additions and 21 deletions

View 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

View 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;
}

View 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);