Silenced a VC++ warning.
[oota-llvm.git] / include / llvm / Bitcode / Serialization.h
1  //=- Serialization.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
16 #define LLVM_BITCODE_SERIALIZE
17
18 #include "llvm/Bitcode/BitstreamWriter.h"
19 #include "llvm/Bitcode/BitstreamReader.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include <vector>
22
23 namespace llvm {
24
25 template <typename T> struct SerializeTrait;
26   
27 class Serializer {
28   BitstreamWriter& Stream;
29   SmallVector<uint64_t,10> Record;
30   bool inBlock;
31 public:
32   Serializer(BitstreamWriter& stream, unsigned BlockID = 0);
33   ~Serializer();
34   
35   template <typename T>
36   inline void Emit(const T& X) { SerializeTrait<T>::Serialize(*this,X); }
37     
38   void EmitInt(unsigned X, unsigned bits);
39   
40   // FIXME: Substitute a better implementation which calculates the minimum
41   // number of bits needed to serialize the enum.
42   void EmitEnum(unsigned X, unsigned MinVal, unsigned MaxVal) { EmitInt(X,32); }
43   
44   void EmitCString(const char* cstr);
45
46   void Flush() { if (inRecord()) EmitRecord(); }
47   
48 private:
49   void EmitRecord();
50   inline bool inRecord() { return Record.size() > 0; }  
51 };
52   
53   
54 class Deserializer {
55   BitstreamReader& Stream;
56   SmallVector<uint64_t,10> Record;
57   unsigned RecIdx;
58 public:
59   Deserializer(BitstreamReader& stream);
60   ~Deserializer();
61
62   template <typename T>
63   inline T& Read(T& X) { SerializeTrait<T>::Deserialize(*this,X); return X; }
64
65   template <typename T>
66   inline T* Materialize() {
67     T* X = SerializeTrait<T>::Instantiate();
68     Read(*X);
69     return X;
70   }
71   
72   uint64_t ReadInt(unsigned bits = 32);
73   bool ReadBool() { return ReadInt(1) ? true : false; }
74   
75   // FIXME: Substitute a better implementation which calculates the minimum
76   // number of bits needed to serialize the enum.
77   template <typename EnumT>
78   EnumT ReadEnum(unsigned MinVal, unsigned MaxVal) { 
79     return static_cast<EnumT>(ReadInt(32));
80   }
81   
82   char* ReadCString(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
83   void ReadCString(std::vector<char>& buff, bool isNullTerm=false);
84   
85 private:
86   void ReadRecord();
87
88   inline bool inRecord() { 
89     if (Record.size() > 0) {
90       if (RecIdx >= Record.size()) {
91         RecIdx = 0;
92         Record.clear();
93         return false;
94       }
95       else return true;
96     }
97     else return false;
98   }
99 };
100  
101
102 template <typename uintty, unsigned Bits> 
103 struct SerializeIntTrait {
104   static inline void Serialize(Serializer& S, uintty X) {
105     S.EmitInt(X,Bits);
106   }
107   
108   static inline void Deserialize(Deserializer& S, uintty& X) {
109     X = (uintty) S.ReadInt(Bits);
110   }
111 };
112   
113 template <> struct SerializeTrait<bool>
114   : public SerializeIntTrait<bool,1> {};
115
116 template <> struct SerializeTrait<char>
117   : public SerializeIntTrait<char,8> {};
118   
119 template <> struct SerializeTrait<short>
120   : public SerializeIntTrait<short,16> {};
121
122 template <> struct SerializeTrait<unsigned>
123   : public SerializeIntTrait<unsigned,32> {};
124
125   
126   
127 } // end namespace llvm
128 #endif