1 //===- BitcodeReader.h - Internal BitcodeReader impl ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This header defines the BitcodeReader class.
12 //===----------------------------------------------------------------------===//
14 #ifndef BITCODE_READER_H
15 #define BITCODE_READER_H
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/Type.h"
19 #include "llvm/User.h"
20 #include "llvm/Bitcode/LLVMBitCodes.h"
24 class BitstreamReader;
27 class BitcodeReaderValueList : public User {
28 std::vector<Use> Uses;
30 BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {}
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];
40 Value *operator[](unsigned i) const { return getOperand(i); }
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!");
50 virtual void print(std::ostream&) const {}
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);
60 class BitcodeReader : public ModuleProvider {
62 const char *ErrorString;
64 std::vector<PATypeHolder> TypeList;
65 BitcodeReaderValueList ValueList;
66 std::vector<std::pair<GlobalVariable*, unsigned> > GlobalInits;
67 std::vector<std::pair<GlobalAlias*, unsigned> > AliasInits;
69 BitcodeReader(MemoryBuffer *buffer) : Buffer(buffer), ErrorString(0) {}
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() {
80 virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
85 virtual Module *materializeModule(std::string *ErrInfo = 0) {
87 //if (ParseAllFunctionBodies(ErrMsg))
92 bool Error(const char *Str) {
96 const char *getErrorString() const { return ErrorString; }
98 /// @brief Main interface to parsing a bitcode buffer.
99 /// @returns true if an error occurred.
102 const Type *getTypeByID(unsigned ID, bool isTypeTable = false);
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();
112 } // End llvm namespace