f5adcffe25c183ec82c3af2b8f6fc50f5329d49f
[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 "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/Support/Allocator.h"
23 #include <vector>
24
25 namespace llvm {
26   
27 class Deserializer {  
28   BitstreamReader& Stream;
29   SmallVector<uint64_t,10> Record;
30   unsigned RecIdx;
31   BumpPtrAllocator Allocator;
32   
33   struct PtrIdInfo {
34     static inline unsigned getEmptyKey() { return ~((unsigned) 0x0); }
35     static inline unsigned getTombstoneKey() { return getEmptyKey()-1; }
36     static inline unsigned getHashValue(unsigned X) { return X; }
37     static inline bool isEqual(unsigned X, unsigned Y) { return X == Y; }
38     static inline bool isPod() { return true; }
39   };
40   
41   struct BPatchNode {
42     BPatchNode* const Next;
43     void*& PtrRef;    
44     BPatchNode(BPatchNode* n, void*& pref) : Next(n), PtrRef(pref) {}
45   };
46   
47   struct BPatchEntry {
48     BPatchNode* Head;
49     void* Ptr;    
50     BPatchEntry() : Head(NULL), Ptr(NULL) {}
51     static inline bool isPod() { return true; }
52   };  
53   
54   typedef llvm::DenseMap<unsigned,BPatchEntry,PtrIdInfo,BPatchEntry> MapTy;
55
56   MapTy BPatchMap;  
57   
58 public:
59   Deserializer(BitstreamReader& stream);
60   ~Deserializer();
61
62   uint64_t ReadInt();
63   bool ReadBool() {
64     return ReadInt() ? true : false;
65   }
66
67   template <typename T>
68   inline T& Read(T& X) {
69     SerializeTrait<T>::Read(*this,X);
70     return X;
71   }
72   
73   template <typename T>
74   inline T ReadVal() {
75     return SerializeTrait<T>::ReadVal(*this);
76   }
77
78   template <typename T>
79   inline T* Materialize() {
80     return SerializeTrait<T>::Materialize(*this);
81   }
82   
83   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
84   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
85
86   template <typename T>
87   inline T* ReadOwnedPtr() {    
88     unsigned PtrId = ReadInt();
89     T* x = SerializeTrait<T>::Materialize(*this);
90     RegisterPtr(PtrId,x);
91     return x;
92   }
93   
94   void ReadPtr(void*& PtrRef);  
95   void RegisterPtr(unsigned PtrId, void* Ptr);
96
97
98   void BackpatchPointers();
99 private:
100   void ReadRecord();  
101   bool inRecord();
102 };
103     
104 } // end namespace llvm
105
106 #endif