Switch the bitcode reader interface to take a MemoryBuffer instead of knowing
[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/LLVMBitCodes.h"
21 #include <vector>
22
23 namespace llvm {
24   class BitstreamReader;
25   class MemoryBuffer;
26   
27 class BitcodeReaderValueList : public User {
28   std::vector<Use> Uses;
29 public:
30   BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
31   
32   // vector compatibility methods
33   unsigned size() const { return getNumOperands(); }
34   void push_back(Value *V) {
35     Uses.push_back(Use(V, this));
36     OperandList = &Uses[0];
37     ++NumOperands;
38   }
39   
40   Value *operator[](unsigned i) const { return getOperand(i); }
41   
42   Value *back() const { return Uses.back(); }
43   void pop_back() { Uses.pop_back(); --NumOperands; }
44   bool empty() const { return NumOperands == 0; }
45   void shrinkTo(unsigned N) {
46     assert(N < NumOperands && "Invalid shrinkTo request!");
47     Uses.resize(N);
48     NumOperands = N;
49   }
50   virtual void print(std::ostream&) const {}
51   
52   Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
53   void initVal(unsigned Idx, Value *V) {
54     assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
55     Uses[Idx].init(V, this);
56   }
57 };
58   
59
60 class BitcodeReader : public ModuleProvider {
61   MemoryBuffer *Buffer;
62   const char *ErrorString;
63   
64   std::vector<PATypeHolder> TypeList;
65   BitcodeReaderValueList ValueList;
66   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
67   std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
68 public:
69   BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {}
70   ~BitcodeReader();
71   
72   
73   /// releaseMemoryBuffer - This causes the reader to completely forget about
74   /// the memory buffer it contains, which prevents the buffer from being
75   /// destroyed when it is deleted.
76   void releaseMemoryBuffer() {
77     Buffer = 0;
78   }
79   
80   virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
81     // FIXME: TODO
82     return false;
83   }
84   
85   virtual Module *materializeModule(std::string *ErrInfo = 0) {
86     // FIXME: TODO
87     //if (ParseAllFunctionBodies(ErrMsg))
88     //  return 0;
89     return TheModule;
90   }
91   
92   bool Error(const char *Str) {
93     ErrorString = Str;
94     return true;
95   }
96   const char *getErrorString() const { return ErrorString; }
97   
98   /// @brief Main interface to parsing a bitcode buffer.
99   /// @returns true if an error occurred.
100   bool ParseBitcode();
101 private:
102   const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
103   
104   bool ParseModule(BitstreamReader &Stream, const std::string &ModuleID);
105   bool ParseTypeTable(BitstreamReader &Stream);
106   bool ParseTypeSymbolTable(BitstreamReader &Stream);
107   bool ParseValueSymbolTable(BitstreamReader &Stream);
108   bool ParseConstants(BitstreamReader &Stream);
109   bool ResolveGlobalAndAliasInits();
110 };
111   
112 } // End llvm namespace
113
114 #endif