Added prototype serialization code for DeclGroup.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57222 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ted Kremenek 2008-10-06 23:49:24 +00:00
Родитель 4021a84eb1
Коммит c7b07c19da
2 изменённых файлов: 94 добавлений и 5 удалений

Просмотреть файл

@ -15,6 +15,7 @@
#define LLVM_CLANG_AST_DECLGROUP_H
#include "llvm/Support/DataTypes.h"
#include "llvm/Bitcode/SerializationFwd.h"
#include <cassert>
namespace clang {
@ -29,6 +30,7 @@ class DeclGroup {
unsigned NumDecls;
private:
DeclGroup() : NumDecls(0) {}
DeclGroup(unsigned numdecls, Decl** decls);
public:
@ -40,8 +42,18 @@ public:
assert (i < NumDecls && "Out-of-bounds access.");
return *((Decl**) (this+1));
}
};
const Decl*& operator[](unsigned i) const {
assert (i < NumDecls && "Out-of-bounds access.");
return *((const Decl**) (this+1));
}
/// Emit - Serialize a DeclGroup to Bitcode.
void Emit(llvm::Serializer& S) const;
/// Read - Deserialize a DeclGroup from Bitcode.
static DeclGroup* Create(llvm::Deserializer& D, ASTContext& C);
};
class DeclGroupRef {
protected:
@ -57,7 +69,7 @@ public:
explicit DeclGroupRef(Decl* d) : D(d) {}
explicit DeclGroupRef(DeclGroup* dg)
: D((Decl*) (reinterpret_cast<uintptr_t>(D) | DeclGroupKind)) {}
: D((Decl*) (reinterpret_cast<uintptr_t>(dg) | DeclGroupKind)) {}
typedef Decl** iterator;
@ -71,22 +83,37 @@ public:
if (getKind() == DeclKind) return D ? &D + 1 : 0;
DeclGroup& G = *((DeclGroup*) (reinterpret_cast<uintptr_t>(D) & ~Mask));
return &G[0] + G.size();
}
}
/// Emit - Serialize a DeclGroupRef to Bitcode.
void Emit(llvm::Serializer& S) const;
/// Read - Deserialize a DeclGroupRef from Bitcode.
static DeclGroupRef ReadVal(llvm::Deserializer& D);
};
class DeclGroupOwningRef : public DeclGroupRef {
public:
explicit DeclGroupOwningRef(Decl* d) : DeclGroupRef(d) {}
explicit DeclGroupOwningRef(DeclGroup* dg) : DeclGroupRef(dg) {}
~DeclGroupOwningRef();
void Destroy(ASTContext& C);
explicit DeclGroupOwningRef(DeclGroupOwningRef& R)
DeclGroupOwningRef(DeclGroupOwningRef& R)
: DeclGroupRef(R) { R.D = 0; }
DeclGroupOwningRef& operator=(DeclGroupOwningRef& R) {
D = R.D;
R.D = 0;
return *this;
}
/// Emit - Serialize a DeclGroupOwningRef to Bitcode.
void Emit(llvm::Serializer& S) const;
/// Read - Deserialize a DeclGroupOwningRef from Bitcode.
static DeclGroupOwningRef ReadVal(llvm::Deserializer& D, ASTContext& C);
};
} // end clang namespace

Просмотреть файл

@ -15,6 +15,8 @@
#include "clang/AST/Decl.h"
#include "clang/AST/ASTContext.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Bitcode/Serialize.h"
#include "llvm/Bitcode/Deserialize.h"
using namespace clang;
@ -26,6 +28,24 @@ DeclGroup* DeclGroup::Create(ASTContext& C, unsigned numdecls, Decl** decls) {
return static_cast<DeclGroup*>(mem);
}
/// Emit - Serialize a DeclGroup to Bitcode.
void DeclGroup::Emit(llvm::Serializer& S) const {
S.EmitInt(NumDecls);
S.BatchEmitOwnedPtrs(NumDecls, &(*this)[0]);
}
/// Read - Deserialize a DeclGroup from Bitcode.
DeclGroup* DeclGroup::Create(llvm::Deserializer& D, ASTContext& C) {
unsigned NumDecls = (unsigned) D.ReadInt();
unsigned size = sizeof(DeclGroup) + sizeof(Decl*) * NumDecls;
unsigned alignment = llvm::AlignOf<DeclGroup>::Alignment;
DeclGroup* DG = (DeclGroup*) C.getAllocator().Allocate(size, alignment);
new (DG) DeclGroup();
DG->NumDecls = NumDecls;
D.BatchReadOwnedPtrs(NumDecls, &(*DG)[0], C);
return DG;
}
DeclGroup::DeclGroup(unsigned numdecls, Decl** decls) {
assert (numdecls > 0);
assert (decls);
@ -58,3 +78,45 @@ void DeclGroupOwningRef::Destroy(ASTContext& C) {
D = 0;
}
void DeclGroupRef::Emit(llvm::Serializer& S) const {
if (getKind() == DeclKind) {
S.EmitBool(false);
S.EmitPtr(D);
}
else {
S.EmitBool(true);
S.EmitPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D)
& ~Mask));
}
}
DeclGroupRef DeclGroupRef::ReadVal(llvm::Deserializer& D) {
if (D.ReadBool())
return DeclGroupRef(D.ReadPtr<Decl>());
return DeclGroupRef(D.ReadPtr<DeclGroup>());
}
void DeclGroupOwningRef::Emit(llvm::Serializer& S) const {
if (getKind() == DeclKind) {
S.EmitBool(false);
S.EmitOwnedPtr(D);
}
else {
S.EmitBool(true);
S.EmitOwnedPtr(reinterpret_cast<DeclGroup*>(reinterpret_cast<uintptr_t>(D)
& ~Mask));
}
}
DeclGroupOwningRef DeclGroupOwningRef::ReadVal(llvm::Deserializer& D,
ASTContext& C) {
if (D.ReadBool()) {
DeclGroupOwningRef DG(D.ReadOwnedPtr<Decl>(C));
return DG;
}
DeclGroupOwningRef DG(D.ReadOwnedPtr<DeclGroup>(C));
return DG;
}