From c7b07c19daa861880ad4bbf257d0fb2c7961355f Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Mon, 6 Oct 2008 23:49:24 +0000 Subject: [PATCH] Added prototype serialization code for DeclGroup. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57222 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/DeclGroup.h | 37 ++++++++++++++++++--- lib/AST/DeclGroup.cpp | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/include/clang/AST/DeclGroup.h b/include/clang/AST/DeclGroup.h index 4818fc219a..0dac050ad2 100644 --- a/include/clang/AST/DeclGroup.h +++ b/include/clang/AST/DeclGroup.h @@ -15,6 +15,7 @@ #define LLVM_CLANG_AST_DECLGROUP_H #include "llvm/Support/DataTypes.h" +#include "llvm/Bitcode/SerializationFwd.h" #include 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(D) | DeclGroupKind)) {} + : D((Decl*) (reinterpret_cast(dg) | DeclGroupKind)) {} typedef Decl** iterator; @@ -71,22 +83,37 @@ public: if (getKind() == DeclKind) return D ? &D + 1 : 0; DeclGroup& G = *((DeclGroup*) (reinterpret_cast(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 diff --git a/lib/AST/DeclGroup.cpp b/lib/AST/DeclGroup.cpp index 34b37dada5..bd79fafc8f 100644 --- a/lib/AST/DeclGroup.cpp +++ b/lib/AST/DeclGroup.cpp @@ -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(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::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(reinterpret_cast(D) + & ~Mask)); + } +} + +DeclGroupRef DeclGroupRef::ReadVal(llvm::Deserializer& D) { + if (D.ReadBool()) + return DeclGroupRef(D.ReadPtr()); + + return DeclGroupRef(D.ReadPtr()); +} + +void DeclGroupOwningRef::Emit(llvm::Serializer& S) const { + if (getKind() == DeclKind) { + S.EmitBool(false); + S.EmitOwnedPtr(D); + } + else { + S.EmitBool(true); + S.EmitOwnedPtr(reinterpret_cast(reinterpret_cast(D) + & ~Mask)); + } +} + +DeclGroupOwningRef DeclGroupOwningRef::ReadVal(llvm::Deserializer& D, + ASTContext& C) { + if (D.ReadBool()) { + DeclGroupOwningRef DG(D.ReadOwnedPtr(C)); + return DG; + } + + DeclGroupOwningRef DG(D.ReadOwnedPtr(C)); + return DG; +} -- 2.40.0