Cleanups for DeclGroup.

llvm-svn: 67922
This commit is contained in:
Chris Lattner 2009-03-28 06:26:18 +00:00
parent dca83c4676
commit 23b88b73e1
3 changed files with 41 additions and 35 deletions

View File

@ -58,7 +58,7 @@ public:
class DeclGroupRef { class DeclGroupRef {
protected: protected:
enum Kind { DeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 }; enum Kind { SingleDeclKind=0x0, DeclGroupKind=0x1, Mask=0x1 };
Decl* D; Decl* D;
Kind getKind() const { Kind getKind() const {
@ -75,31 +75,49 @@ public:
typedef Decl** iterator; typedef Decl** iterator;
typedef Decl* const * const_iterator; typedef Decl* const * const_iterator;
bool hasSolitaryDecl() const { bool isSingleDecl() const { return getKind() == SingleDeclKind; }
return getKind() == DeclKind; bool isDeclGroup() const { return getKind() == DeclGroupKind; }
Decl *getSingleDecl() {
assert(isSingleDecl() && "Isn't a declgroup");
return D;
}
const Decl *getSingleDecl() const {
return const_cast<DeclGroupRef*>(this)->getSingleDecl();
} }
DeclGroup &getDeclGroup() {
assert(isDeclGroup() && "Isn't a declgroup");
return *((DeclGroup*)(reinterpret_cast<uintptr_t>(D) & ~Mask));
}
const DeclGroup &getDeclGroup() const {
return const_cast<DeclGroupRef*>(this)->getDeclGroup();
}
iterator begin() { iterator begin() {
if (getKind() == DeclKind) return D ? &D : 0; if (isSingleDecl())
DeclGroup& G = *((DeclGroup*) (reinterpret_cast<uintptr_t>(D) & ~Mask)); return D ? &D : 0;
return &G[0]; return &getDeclGroup()[0];
} }
iterator end() { iterator end() {
if (getKind() == DeclKind) return D ? &D + 1 : 0; if (isSingleDecl())
DeclGroup& G = *((DeclGroup*) (reinterpret_cast<uintptr_t>(D) & ~Mask)); return D ? &D+1 : 0;
DeclGroup &G = getDeclGroup();
return &G[0] + G.size(); return &G[0] + G.size();
} }
const_iterator begin() const { const_iterator begin() const {
if (getKind() == DeclKind) return D ? &D : 0; if (isSingleDecl())
DeclGroup& G = *((DeclGroup*) (reinterpret_cast<uintptr_t>(D) & ~Mask)); return D ? &D : 0;
return &G[0]; return &getDeclGroup()[0];
} }
const_iterator end() const { const_iterator end() const {
if (getKind() == DeclKind) return D ? &D + 1 : 0; if (isSingleDecl())
DeclGroup& G = *((DeclGroup*) (reinterpret_cast<uintptr_t>(D) & ~Mask)); return D ? &D+1 : 0;
const DeclGroup &G = getDeclGroup();
return &G[0] + G.size(); return &G[0] + G.size();
} }

View File

@ -235,23 +235,14 @@ public:
virtual void Destroy(ASTContext& Ctx); virtual void Destroy(ASTContext& Ctx);
// hasSolitaryDecl - This method returns true if this DeclStmt refers /// hasSolitaryDecl - This method returns true if this DeclStmt refers
// to a single Decl. /// to a single Decl.
bool hasSolitaryDecl() const { bool hasSolitaryDecl() const {
return DG.hasSolitaryDecl(); return DG.isSingleDecl();
} }
const Decl* getSolitaryDecl() const { const Decl* getSolitaryDecl() const { return DG.getSingleDecl(); }
assert (hasSolitaryDecl() && Decl *getSolitaryDecl() { return DG.getSingleDecl(); }
"Caller assumes this DeclStmt points to one Decl*");
return *DG.begin();
}
Decl* getSolitaryDecl() {
assert (hasSolitaryDecl() &&
"Caller assumes this DeclStmt points to one Decl*");
return *DG.begin();
}
SourceLocation getStartLoc() const { return StartLoc; } SourceLocation getStartLoc() const { return StartLoc; }
SourceLocation getEndLoc() const { return EndLoc; } SourceLocation getEndLoc() const { return EndLoc; }

View File

@ -17,7 +17,6 @@
#include "llvm/Support/Allocator.h" #include "llvm/Support/Allocator.h"
#include "llvm/Bitcode/Serialize.h" #include "llvm/Bitcode/Serialize.h"
#include "llvm/Bitcode/Deserialize.h" #include "llvm/Bitcode/Deserialize.h"
using namespace clang; using namespace clang;
DeclGroup* DeclGroup::Create(ASTContext& C, unsigned numdecls, Decl** decls) { DeclGroup* DeclGroup::Create(ASTContext& C, unsigned numdecls, Decl** decls) {
@ -48,8 +47,8 @@ DeclGroup* DeclGroup::Read(llvm::Deserializer& D, ASTContext& C) {
} }
DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) { DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) : NumDecls(numdecls) {
assert (numdecls > 0); assert(numdecls > 0);
assert (decls); assert(decls);
memcpy(this+1, decls, numdecls * sizeof(*decls)); memcpy(this+1, decls, numdecls * sizeof(*decls));
} }
@ -59,14 +58,12 @@ void DeclGroup::Destroy(ASTContext& C) {
} }
void DeclGroupRef::Emit(llvm::Serializer& S) const { void DeclGroupRef::Emit(llvm::Serializer& S) const {
if (getKind() == DeclKind) { if (isSingleDecl()) {
S.EmitBool(false); S.EmitBool(false);
S.EmitPtr(D); S.EmitPtr(D);
} } else {
else {
S.EmitBool(true); S.EmitBool(true);
S.EmitPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D) S.EmitPtr(&getDeclGroup());
& ~Mask));
} }
} }