constified several pointer arguments for methods in the Deserializer.
[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 "llvm/Support/DataTypes.h"
24 #include <vector>
25
26 namespace llvm {
27   
28 class Deserializer {  
29
30   //===----------------------------------------------------------===//
31   // Internal type definitions.
32   //===----------------------------------------------------------===//
33
34   struct PtrIdInfo {
35     static inline unsigned getEmptyKey() { return ~((unsigned) 0x0); }
36     static inline unsigned getTombstoneKey() { return getEmptyKey()-1; }
37     static inline unsigned getHashValue(unsigned X) { return X; }
38     static inline bool isEqual(unsigned X, unsigned Y) { return X == Y; }
39     static inline bool isPod() { return true; }
40   };
41   
42   struct BPNode {
43     BPNode* Next;
44     uintptr_t& PtrRef;
45     BPNode(BPNode* n, uintptr_t& pref) 
46       : Next(n), PtrRef(pref) {
47         PtrRef = 0;
48       }
49   };
50   
51   class BPatchEntry {
52     uintptr_t Ptr;
53   public:
54     
55     BPatchEntry() : Ptr(0x1) {}
56       
57     BPatchEntry(void* P) : Ptr(reinterpret_cast<uintptr_t>(P)) {}
58
59     bool hasFinalPtr() const { return Ptr & 0x1 ? false : true; }
60     void setFinalPtr(BPNode*& FreeList, const void* P);
61
62     BPNode* getBPNode() const {
63       assert (!hasFinalPtr());
64       return reinterpret_cast<BPNode*>(Ptr & ~0x1);
65     }
66     
67     void setBPNode(BPNode* N) {
68       assert (!hasFinalPtr());
69       Ptr = reinterpret_cast<uintptr_t>(N) | 0x1;
70     }
71     
72     uintptr_t getFinalPtr() const {
73       assert (!(Ptr & 0x1) && "Backpatch pointer not yet deserialized.");
74       return Ptr;
75     }    
76
77     static inline bool isPod() { return true; }
78   };
79
80   typedef llvm::DenseMap<unsigned,BPatchEntry,PtrIdInfo,BPatchEntry> MapTy;
81
82   //===----------------------------------------------------------===//
83   // Internal data members.
84   //===----------------------------------------------------------===//
85   
86   BitstreamReader& Stream;
87   SmallVector<uint64_t,10> Record;
88   unsigned RecIdx;
89   BumpPtrAllocator Allocator;
90   BPNode* FreeList;
91   MapTy BPatchMap;  
92   
93   //===----------------------------------------------------------===//
94   // Public Interface.
95   //===----------------------------------------------------------===//
96   
97 public:
98   Deserializer(BitstreamReader& stream);
99   ~Deserializer();
100
101   uint64_t ReadInt();
102   bool ReadBool() {
103     return ReadInt() ? true : false;
104   }
105
106   template <typename T>
107   inline T& Read(T& X) {
108     SerializeTrait<T>::Read(*this,X);
109     return X;
110   }
111   
112   template <typename T>
113   inline T ReadVal() {
114     return SerializeTrait<T>::ReadVal(*this);
115   }
116
117   template <typename T>
118   inline T* Materialize() {
119     return SerializeTrait<T>::Materialize(*this);
120   }
121   
122   char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
123   void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
124
125   template <typename T>
126   inline T* ReadOwnedPtr() {    
127     unsigned PtrId = ReadInt();
128
129     if (PtrId == 0)
130       return NULL;
131     
132     T* x = SerializeTrait<T>::Materialize(*this);
133     RegisterPtr(PtrId,x);
134     return x;
135   }
136   
137   template <typename T>
138   void ReadPtr(T*& PtrRef) {
139     ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));
140   }
141   
142   template <typename T>
143   void ReadPtr(const T*& PtrRef) {
144     ReadPtr(const_cast<T*&>(PtrRef));
145   }            
146
147   void ReadUIntPtr(uintptr_t& PtrRef);
148   
149   template <typename T>
150   T& ReadRef() {
151     T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
152     return *p;
153   }
154
155   void RegisterPtr(unsigned PtrId, const void* Ptr);
156   
157   void RegisterPtr(const void* Ptr) {
158     RegisterPtr(ReadInt(),Ptr);
159   }
160
161 private:
162   void ReadRecord();  
163   bool inRecord();
164   uintptr_t ReadInternalRefPtr();
165 };
166     
167 } // end namespace llvm
168
169 #endif