be5adb85eaad729817d7912963be12cee4b45d0e
[oota-llvm.git] / include / llvm / Bitcode / Serialize.h
1 //==- Serialize.h - Generic Object Serialization to Bitcode -------*- C++ -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Ted Kremenek and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interface for generic object serialization to
11 // LLVM bitcode.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_BITCODE_SERIALIZE_OUTPUT
16 #define LLVM_BITCODE_SERIALIZE_OUTPUT
17
18 #include "llvm/Bitcode/Serialization.h"
19 #include "llvm/Bitcode/BitstreamWriter.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/DenseMap.h"
22
23 namespace llvm {
24
25 class Serializer {
26   BitstreamWriter& Stream;
27   SmallVector<uint64_t,10> Record;
28   bool inBlock;
29   
30   typedef DenseMap<const void*,unsigned> MapTy;
31   MapTy PtrMap;
32   
33 public:
34   Serializer(BitstreamWriter& stream, unsigned BlockID = 0);
35   ~Serializer();
36   
37   template <typename T>
38   inline void Emit(const T& X) { SerializeTrait<T>::Emit(*this,X); }
39   
40   void EmitInt(unsigned X);
41   void EmitBool(bool X) { EmitInt(X); }
42   void EmitCStr(const char* beg, const char* end);
43   void EmitCStr(const char* cstr);
44   
45   void EmitPtr(const void* ptr) { EmitInt(getPtrId(ptr)); }
46   
47   template <typename T>
48   void EmitRef(const T& ref) { EmitPtr(&ref); }
49   
50   template <typename T>
51   void EmitOwnedPtr(T* ptr) {
52     EmitPtr(ptr);
53     if (ptr) SerializeTrait<T>::Emit(*this,*ptr);
54   }
55
56   void Flush() { if (inRecord()) EmitRecord(); }
57   
58 private:
59   void EmitRecord();
60   inline bool inRecord() { return Record.size() > 0; }
61   unsigned getPtrId(const void* ptr);
62 };
63
64 } // end namespace llvm
65 #endif