implement support for reading aggregate constants, including handling forward
[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   virtual void print(std::ostream&) const {}
45   
46   Constant *getConstantFwdRef(unsigned Idx, const Type *Ty);
47   void initVal(unsigned Idx, Value *V) {
48     assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!");
49     Uses[Idx].init(V, this);
50   }
51 };
52   
53
54 class BitcodeReader : public ModuleProvider {
55   const char *ErrorString;
56   
57   std::vector<PATypeHolder> TypeList;
58   BitcodeReaderValueList ValueList;
59   std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
60 public:
61   virtual ~BitcodeReader() {}
62   
63   virtual void FreeState() {}
64   
65   virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
66     // FIXME: TODO
67     return false;
68   }
69   
70   virtual Module *materializeModule(std::string *ErrInfo = 0) {
71     // FIXME: TODO
72     //if (ParseAllFunctionBodies(ErrMsg))
73     //  return 0;
74     return TheModule;
75   }
76   
77   bool Error(const char *Str) {
78     ErrorString = Str;
79     return true;
80   }
81   const char *getErrorString() const { return ErrorString; }
82   
83   /// @brief Main interface to parsing a bitcode buffer.
84   /// @returns true if an error occurred.
85   bool ParseBitcode(unsigned char *Buf, unsigned Length,
86                     const std::string &ModuleID);
87 private:
88   const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
89   
90   bool ParseModule(BitstreamReader &Stream, const std::string &ModuleID);
91   bool ParseTypeTable(BitstreamReader &Stream);
92   bool ParseTypeSymbolTable(BitstreamReader &Stream);
93   bool ParseValueSymbolTable(BitstreamReader &Stream);
94   bool ParseConstants(BitstreamReader &Stream);
95 };
96   
97 } // End llvm namespace
98
99 #endif