1 //=- Deserialize.h - Generic Object Deserialization from Bitcode --*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interface for generic object deserialization from
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_BITCODE_SERIALIZE_INPUT
16 #define LLVM_BITCODE_SERIALIZE_INPUT
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"
30 //===----------------------------------------------------------===//
31 // Internal type definitions.
32 //===----------------------------------------------------------===//
38 BPNode(BPNode* n, uintptr_t& pref)
39 : Next(n), PtrRef(pref) {
45 union { BPNode* Head; void* Ptr; };
47 BPEntry() : Head(NULL) {}
49 static inline bool isPod() { return true; }
51 void SetPtr(BPNode*& FreeList, void* P);
58 BPKey(unsigned PtrId) : Raw(PtrId << 1) { assert (PtrId > 0); }
59 BPKey(unsigned code, unsigned) : Raw(code) {}
61 void MarkFinal() { Raw |= 0x1; }
62 bool hasFinalPtr() const { return Raw & 0x1 ? true : false; }
63 unsigned getID() const { return Raw >> 1; }
65 static inline BPKey getEmptyKey() { return BPKey(0,0); }
66 static inline BPKey getTombstoneKey() { return BPKey(1,0); }
67 static inline unsigned getHashValue(const BPKey& K) { return K.Raw & ~0x1; }
69 static bool isEqual(const BPKey& K1, const BPKey& K2) {
70 return (K1.Raw ^ K2.Raw) & ~0x1 ? false : true;
73 static bool isPod() { return true; }
76 typedef llvm::DenseMap<BPKey,BPEntry,BPKey,BPEntry> MapTy;
78 //===----------------------------------------------------------===//
79 // Internal data members.
80 //===----------------------------------------------------------===//
82 BitstreamReader& Stream;
83 SmallVector<uint64_t,10> Record;
85 BumpPtrAllocator Allocator;
89 //===----------------------------------------------------------===//
91 //===----------------------------------------------------------===//
94 Deserializer(BitstreamReader& stream);
99 return ReadInt() ? true : false;
102 template <typename T>
103 inline T& Read(T& X) {
104 SerializeTrait<T>::Read(*this,X);
108 template <typename T>
109 inline T* Materialize() {
110 return SerializeTrait<T>::Materialize(*this);
113 char* ReadCStr(char* cstr = NULL, unsigned MaxLen=0, bool isNullTerm=true);
114 void ReadCStr(std::vector<char>& buff, bool isNullTerm=false);
116 template <typename T>
117 inline T* ReadOwnedPtr() {
118 unsigned PtrId = ReadInt();
123 T* x = SerializeTrait<T>::Materialize(*this);
124 RegisterPtr(PtrId,x);
128 template <typename T>
129 inline void ReadOwnedPtr(T*& Ptr) {
130 Ptr = ReadOwnedPtr<T>();
133 template <typename T>
134 void ReadPtr(T*& PtrRef) {
135 ReadUIntPtr(reinterpret_cast<uintptr_t&>(PtrRef));
138 template <typename T>
139 void ReadPtr(const T*& PtrRef) {
140 ReadPtr(const_cast<T*&>(PtrRef));
143 void ReadUIntPtr(uintptr_t& PtrRef);
145 template <typename T>
147 T* p = reinterpret_cast<T*>(ReadInternalRefPtr());
151 void RegisterPtr(unsigned PtrId, const void* Ptr);
153 void RegisterPtr(const void* Ptr) {
154 RegisterPtr(ReadInt(),Ptr);
162 uintptr_t ReadInternalRefPtr();
164 static inline bool HasFinalPtr(MapTy::value_type& V) {
165 return V.first.hasFinalPtr();
168 static inline uintptr_t GetFinalPtr(MapTy::value_type& V) {
169 return reinterpret_cast<uintptr_t>(V.second.Ptr);
172 static inline BPNode* GetBPNode(MapTy::value_type& V) {
173 return V.second.Head;
176 static inline void SetBPNode(MapTy::value_type& V, BPNode* N) {
180 void SetPtr(MapTy::value_type& V, const void* P) {
182 V.second.SetPtr(FreeList,const_cast<void*>(P));
186 } // end namespace llvm