[cfe-commits] r57222 - in /cfe/trunk: include/clang/AST/DeclGroup.h lib/AST/DeclGroup.cpp

Ted Kremenek kremenek at apple.com
Mon Oct 6 16:49:26 PDT 2008


Author: kremenek
Date: Mon Oct  6 18:49:24 2008
New Revision: 57222

URL: http://llvm.org/viewvc/llvm-project?rev=57222&view=rev
Log:
Added prototype serialization code for DeclGroup.

Modified:
    cfe/trunk/include/clang/AST/DeclGroup.h
    cfe/trunk/lib/AST/DeclGroup.cpp

Modified: cfe/trunk/include/clang/AST/DeclGroup.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclGroup.h?rev=57222&r1=57221&r2=57222&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclGroup.h (original)
+++ cfe/trunk/include/clang/AST/DeclGroup.h Mon Oct  6 18:49:24 2008
@@ -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 @@
   unsigned NumDecls;
   
 private:
+  DeclGroup() : NumDecls(0) {}
   DeclGroup(unsigned numdecls, Decl** decls);
 
 public:
@@ -40,8 +42,18 @@
     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 @@
   
   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 @@
     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

Modified: cfe/trunk/lib/AST/DeclGroup.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclGroup.cpp?rev=57222&r1=57221&r2=57222&view=diff

==============================================================================
--- cfe/trunk/lib/AST/DeclGroup.cpp (original)
+++ cfe/trunk/lib/AST/DeclGroup.cpp Mon Oct  6 18:49:24 2008
@@ -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 @@
   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 @@
   
   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;
+}





More information about the cfe-commits mailing list