Fix PR1434 and test/Linker/link-archive.ll, this is a regression from 1.9.
[oota-llvm.git] / lib / Bitcode / Reader / BitcodeReader.h
1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License.  See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This header defines the BitcodeReader class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef BITCODE_READER_H
15 #define BITCODE_READER_H
16
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Type.h"
19 #include "llvm/User.h"
20 #include "llvm/Bitcode/BitstreamReader.h"
21 #include "llvm/Bitcode/LLVMBitCodes.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include <vector>
24
25 namespace llvm {
26   class MemoryBuffer;
27   class ParamAttrsList;
28   
29 class BitcodeReaderValueList : public User {
30   std::vector<Use> Uses;
31 public:
32   BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
33   
34   // vector compatibility methods
35   unsigned size() const { return getNumOperands(); }
36   void push_back(Value *V) {
37     Uses.push_back(Use(V, this));
38     OperandList = &Uses[0];
39     ++NumOperands;
40   }
41   
42   void clear() {
43     std::vector<Use>().swap(Uses);
44   }
45   
46   Value *operator[](unsigned i) const { return getOperand(i); }
47   
48   Value *back() const { return Uses.back(); }
49   void pop_back() { Uses.pop_back(); --NumOperands; }
50   bool empty() const { return NumOperands == 0; }
51   void shrinkTo(unsigned N) {
52     assert(N <= NumOperands && "Invalid shrinkTo request!");
53     Uses.resize(N);
54     NumOperands = N;
55   }
56   virtual void print(std::ostream&) const {}
57   
58   Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
59   Value *getValueFwdRef(unsigned Idx, const Type *Ty);
60   
61   void AssignValue(Value *V, unsigned Idx) {
62     if (Idx == size()) {
63       push_back(V);
64     } else if (Value *OldV = getOperand(Idx)) {
65       // If there was a forward reference to this value, replace it.
66       setOperand(Idx, V);
67       OldV->replaceAllUsesWith(V);
68       delete OldV;
69     } else {
70       initVal(Idx, V);
71     }
72   }
73   
74 private:
75   void initVal(unsigned Idx, Value *V) {
76     assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
77     Uses[Idx].init(V, this);
78   }
79 };
80   
81
82 class BitcodeReader : public ModuleProvider {
83   MemoryBuffer *Buffer;
84   BitstreamReader Stream;
85   
86   const char *ErrorString;
87   
88   std::vector<PATypeHolder> TypeList;
89   BitcodeReaderValueList ValueList;
90   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
91   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
92   
93   /// ParamAttrs - The set of parameter attributes by index.  Index zero in the
94   /// file is for null, and is thus not represented here.  As such all indices
95   /// are off by one.
96   std::vector<const ParamAttrsList*> ParamAttrs;
97   
98   /// FunctionBBs - While parsing a function body, this is a list of the basic
99   /// blocks for the function.
100   std::vector<BasicBlock*> FunctionBBs;
101   
102   // When reading the module header, this list is populated with functions that
103   // have bodies later in the file.
104   std::vector<Function*> FunctionsWithBodies;
105   
106   // After the module header has been read, the FunctionsWithBodies list is 
107   // reversed.  This keeps track of whether we've done this yet.
108   bool HasReversedFunctionsWithBodies;
109   
110   /// DeferredFunctionInfo - When function bodies are initially scanned, this
111   /// map contains info about where to find deferred function body (in the
112   /// stream) and what linkage the original function had.
113   DenseMap<Function*, std::pair<uint64_t, unsigned> > DeferredFunctionInfo;
114 public:
115   BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {
116     HasReversedFunctionsWithBodies = false;
117   }
118   ~BitcodeReader() {
119     FreeState();
120   }
121   
122   void FreeState();
123   
124   /// releaseMemoryBuffer - This causes the reader to completely forget about
125   /// the memory buffer it contains, which prevents the buffer from being
126   /// destroyed when it is deleted.
127   void releaseMemoryBuffer() {
128     Buffer = 0;
129   }
130   
131   virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0);
132   virtual Module *materializeModule(std::string *ErrInfo = 0);
133   virtual void dematerializeFunction(Function *F);
134   virtual Module *releaseModule(std::string *ErrInfo = 0);
135
136   bool Error(const char *Str) {
137     ErrorString = Str;
138     return true;
139   }
140   const char *getErrorString() const { return ErrorString; }
141   
142   /// @brief Main interface to parsing a bitcode buffer.
143   /// @returns true if an error occurred.
144   bool ParseBitcode();
145 private:
146   const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
147   Value *getFnValueByID(unsigned ID, const Type *Ty) {
148     return ValueList.getValueFwdRef(ID, Ty);
149   }
150   BasicBlock *getBasicBlock(unsigned ID) const {
151     if (ID >= FunctionBBs.size()) return 0; // Invalid ID
152     return FunctionBBs[ID];
153   }
154   const ParamAttrsList *getParamAttrs(unsigned i) const {
155     if (i-1 < ParamAttrs.size())
156       return ParamAttrs[i-1];
157     return 0;
158   }
159   
160   /// getValueTypePair - Read a value/type pair out of the specified record from
161   /// slot 'Slot'.  Increment Slot past the number of slots used in the record.
162   /// Return true on failure.
163   bool getValueTypePair(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
164                         unsigned InstNum, Value *&ResVal) {
165     if (Slot == Record.size()) return true;
166     unsigned ValNo = (unsigned)Record[Slot++];
167     if (ValNo < InstNum) {
168       // If this is not a forward reference, just return the value we already
169       // have.
170       ResVal = getFnValueByID(ValNo, 0);
171       return ResVal == 0;
172     } else if (Slot == Record.size()) {
173       return true;
174     }
175     
176     unsigned TypeNo = (unsigned)Record[Slot++];
177     ResVal = getFnValueByID(ValNo, getTypeByID(TypeNo));
178     return ResVal == 0;
179   }
180   bool getValue(SmallVector<uint64_t, 64> &Record, unsigned &Slot,
181                 const Type *Ty, Value *&ResVal) {
182     if (Slot == Record.size()) return true;
183     unsigned ValNo = (unsigned)Record[Slot++];
184     ResVal = getFnValueByID(ValNo, Ty);
185     return ResVal == 0;
186   }
187
188   
189   bool ParseModule(const std::string &ModuleID);
190   bool ParseParamAttrBlock();
191   bool ParseTypeTable();
192   bool ParseTypeSymbolTable();
193   bool ParseValueSymbolTable();
194   bool ParseConstants();
195   bool RememberAndSkipFunctionBody();
196   bool ParseFunctionBody(Function *F);
197   bool ResolveGlobalAndAliasInits();
198 };
199   
200 } // End llvm namespace
201
202 #endif