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