6ee09aaa34465d85ca2bd33d0dbd5915dda725fb
[oota-llvm.git] / include / llvm / Bitcode / Deserialize.h
1 //=- Deserialize.h - Generic Object Deserialization from 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 deserialization from
11 // LLVM bitcode.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_BITCODE_SERIALIZE_INPUT
16 #define LLVM_BITCODE_SERIALIZE_INPUT
17
18 #include "llvm/Bitcode/BitstreamReader.h"
19 #include "llvm/Bitcode/Serialization.h"
20 #include <vector>
21
22 namespace llvm {
23   
24 class Deserializer {  
25   BitstreamReader& Stream;
26   SmallVector<uint64_t,10> Record;
27   unsigned RecIdx;
28 public:
29   Deserializer(BitstreamReader& stream);
30   ~Deserializer();
31   
32   template <typename T>
33   inline T& Read(T& X) {
34     SerializeTrait<T>::Read(*this,X);
35     return X;
36   }
37   
38   template <typename T>
39   inline T* Materialize() {
40     return SerializeTrait<T>::Materialize(*this);
41   }
42     
43   uint64_t ReadInt();
44   bool ReadBool() { return ReadInt() ? true : false; }
45   
46   // FIXME: Substitute a better implementation which calculates the minimum
47   // number of bits needed to serialize the enum.
48   template <typename EnumT>
49   EnumT ReadEnum(unsigned MinVal, unsigned MaxVal) { 
50     return static_cast<EnumT>(ReadInt(32));
51   }
52   
53   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
54   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
55   
56 private:
57   void ReadRecord();
58   
59   inline bool inRecord() { 
60     if (Record.size() > 0) {
61       if (RecIdx >= Record.size()) {
62         RecIdx = 0;
63         Record.clear();
64         return false;
65       }
66       else return true;
67     }
68     else return false;
69   }
70 };
71   
72 } // end namespace llvm
73
74 #endif