LLParser.h revision 1971556cc271710b683bc3ca8327c903791c910f
19d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
29d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//
39d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//                     The LLVM Compiler Infrastructure
49d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//
59d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner// This file is distributed under the University of Illinois Open Source
69d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner// License. See LICENSE.TXT for details.
79d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//
89d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//===----------------------------------------------------------------------===//
99d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//
109d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//  This file defines the parser class for .ll files.
119d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//
129d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner//===----------------------------------------------------------------------===//
139d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
149d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner#ifndef LLVM_ASMPARSER_LLPARSER_H
159d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner#define LLVM_ASMPARSER_LLPARSER_H
169d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
179d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner#include "LLLexer.h"
189d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner#include "llvm/Module.h"
199d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner#include "llvm/Type.h"
209d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner#include <map>
219d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
229d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattnernamespace llvm {
239d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class Module;
249d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class OpaqueType;
259d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class Function;
269d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class Value;
279d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class BasicBlock;
289d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class Instruction;
299d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class Constant;
309d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class GlobalValue;
319d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class MetadataBase;
329d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class MDString;
339d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  class MDNode;
349d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
359d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  /// ValID - Represents a reference of a definition of some sort with no type.
369d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  /// There are several cases where we have to parse the value but where the
379d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  /// type can depend on later context.  This may either be a numeric reference
389d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  /// or a symbolic (%var) reference.  This is just a discriminated union.
399d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  struct ValID {
40266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner    enum {
41266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner      t_LocalID, t_GlobalID,      // ID in UIntVal.
42266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner      t_LocalName, t_GlobalName,  // Name in StrVal.
43266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner      t_APSInt, t_APFloat,        // Value in APSIntVal/APFloatVal.
44266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner      t_Null, t_Undef, t_Zero,    // No value.
45bb9dbb7d6b4f39b74b755795e31b219a5518dd77Eli Friedman      t_EmptyArray,               // No value:  []
46266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner      t_Constant,                 // Value in ConstantVal.
47266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner      t_InlineAsm,                // Value in StrVal/StrVal2/UIntVal.
48266451dd95b12323fad9418df9b217918ec7e9e0Chris Lattner      t_Metadata                  // Value in MetadataVal.
499d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    } Kind;
5022e522e08640be459f237796009cf7666d6d75e7Chris Lattner
5122e522e08640be459f237796009cf7666d6d75e7Chris Lattner    LLLexer::LocTy Loc;
529d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    unsigned UIntVal;
539d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    std::string StrVal, StrVal2;
549d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    APSInt APSIntVal;
559d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    APFloat APFloatVal;
569d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    Constant *ConstantVal;
579d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    MetadataBase *MetadataVal;
589d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    ValID() : APFloatVal(0.0) {}
599d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
609d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool operator<(const ValID &RHS) const {
6122e522e08640be459f237796009cf7666d6d75e7Chris Lattner      if (Kind == t_LocalID || Kind == t_GlobalID)
629d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner        return UIntVal < RHS.UIntVal;
6322e522e08640be459f237796009cf7666d6d75e7Chris Lattner      assert((Kind == t_LocalName || Kind == t_GlobalName) &&
649d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner             "Ordering not defined for this ValID kind yet");
659d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      return StrVal < RHS.StrVal;
66e6f1355c38fb66b0bee35cb4d6ec93f07196d961Benjamin Kramer    }
67bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis  };
68bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis
69bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis  class LLParser {
7079976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner  public:
7179976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    typedef LLLexer::LocTy LocTy;
7279976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner  private:
7379976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    LLVMContext& Context;
740db235a2b0ed6ae5c3c870012061906054b6dbc4Argyrios Kyrtzidis    LLLexer Lex;
7579976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    Module *M;
7679976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner
7779976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    // Type resolution handling data structures.
7879976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
79bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis    std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
8079976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    std::vector<PATypeHolder> NumberedTypes;
8179976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    /// MetadataCache - This map keeps track of parsed metadata constants.
8279976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    std::map<unsigned, WeakVH> MetadataCache;
8379976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    std::map<unsigned, std::pair<WeakVH, LocTy> > ForwardRefMDNodes;
8479976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
8579976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner    struct UpRefRecord {
8679976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner      /// Loc - This is the location of the upref.
8779976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner      LocTy Loc;
8879976a40728c8baa8cd16de90173fe2c48937e22Chris Lattner
89bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      /// NestingLevel - The number of nesting levels that need to be popped
90bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      /// before this type is resolved.
91bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      unsigned NestingLevel;
92bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis
93bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      /// LastContainedTy - This is the type at the current binding level for
94bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      /// the type.  Every time we reduce the nesting level, this gets updated.
95bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      const Type *LastContainedTy;
96bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis
979d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      /// UpRefTy - This is the actual opaque type that the upreference is
989d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      /// represented with.
999d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      OpaqueType *UpRefTy;
1009d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
1019d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
1029d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner        : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
1039d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner          UpRefTy(URTy) {}
10422e522e08640be459f237796009cf7666d6d75e7Chris Lattner    };
1059d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    std::vector<UpRefRecord> UpRefs;
10622e522e08640be459f237796009cf7666d6d75e7Chris Lattner
1079d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    // Global Value reference information.
1089d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
1099d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
1109d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    std::vector<GlobalValue*> NumberedVals;
1119d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
1129d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    // References to blockaddress.  The key is the function ValID, the value is
1139d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    // a list of references to blocks in that function.
1149d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
1159d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      ForwardRefBlockAddresses;
1169d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
1179d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    Function *MallocF;
1189d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner  public:
1199d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
1209d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
1219d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      M(m), MallocF(NULL) {}
1229d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool Run();
1239d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
1249d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    LLVMContext& getContext() { return Context; }
1259d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
12622e522e08640be459f237796009cf7666d6d75e7Chris Lattner  private:
1279d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
1289d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool Error(LocTy L, const std::string &Msg) const {
1299d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      return Lex.Error(L, Msg);
1309d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    }
1319d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool TokError(const std::string &Msg) const {
1329d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      return Error(Lex.getLoc(), Msg);
1339d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    }
1349d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
1359d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    /// GetGlobalVal - Get a value with the specified name or ID, creating a
1369d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    /// forward reference record if needed.  This can return null if the value
137840635741f132a9a10f052cbf3b21e14bc74835aChris Lattner    /// exists but does not have the right type.
1389d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
1399d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
1409d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner
1419d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    // Helper Routines.
1429d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool ParseToken(lltok::Kind T, const char *ErrMsg);
1439d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool EatIfPresent(lltok::Kind T) {
1449d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      if (Lex.getKind() != T) return false;
145bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      Lex.Lex();
146bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      return true;
147bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis    }
148bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis    bool ParseOptionalToken(lltok::Kind T, bool &Present) {
149bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      if (Lex.getKind() != T) {
150bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis        Present = false;
151bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      } else {
152bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis        Lex.Lex();
153bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis        Present = true;
154bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      }
155bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis      return false;
156bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis    }
157bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis    bool ParseStringConstant(std::string &Result);
158bb07f21c76f011d8dde491104ff242af30bfb4abArgyrios Kyrtzidis    bool ParseUInt32(unsigned &Val);
1599d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool ParseUInt32(unsigned &Val, LocTy &Loc) {
1609d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      Loc = Lex.getLoc();
1619d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner      return ParseUInt32(Val);
1629d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    }
1639d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool ParseOptionalAddrSpace(unsigned &AddrSpace);
1649d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
1659d69d4aadd4a58aba5634d5c3d2c2a6d8d284134Chris Lattner    bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
166    bool ParseOptionalLinkage(unsigned &Linkage) {
167      bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
168    }
169    bool ParseOptionalVisibility(unsigned &Visibility);
170    bool ParseOptionalCallingConv(CallingConv::ID &CC);
171    bool ParseOptionalAlignment(unsigned &Alignment);
172    bool ParseOptionalCustomMetadata();
173    bool ParseOptionalInfo(unsigned &Alignment);
174    bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
175
176    // Top-Level Entities
177    bool ParseTopLevelEntities();
178    bool ValidateEndOfModule();
179    bool ParseTargetDefinition();
180    bool ParseDepLibs();
181    bool ParseModuleAsm();
182    bool ParseUnnamedType();
183    bool ParseNamedType();
184    bool ParseDeclare();
185    bool ParseDefine();
186
187    bool ParseGlobalType(bool &IsConstant);
188    bool ParseUnnamedGlobal();
189    bool ParseNamedGlobal();
190    bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
191                     bool HasLinkage, unsigned Visibility);
192    bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
193    bool ParseStandaloneMetadata();
194    bool ParseNamedMetadata();
195    bool ParseMDString(MetadataBase *&S);
196    bool ParseMDNode(MetadataBase *&N);
197
198    // Type Parsing.
199    bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
200    bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
201      Loc = Lex.getLoc();
202      return ParseType(Result, AllowVoid);
203    }
204    bool ParseTypeRec(PATypeHolder &H);
205    bool ParseStructType(PATypeHolder &H, bool Packed);
206    bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
207    bool ParseFunctionType(PATypeHolder &Result);
208    PATypeHolder HandleUpRefs(const Type *Ty);
209
210    // Constants.
211    bool ParseValID(ValID &ID);
212    bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
213    bool ParseGlobalValue(const Type *Ty, Constant *&V);
214    bool ParseGlobalTypeAndValue(Constant *&V);
215    bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
216    bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
217
218
219    // Function Semantic Analysis.
220    class PerFunctionState {
221      LLParser &P;
222      Function &F;
223      std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
224      std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
225      std::vector<Value*> NumberedVals;
226
227      /// FunctionNumber - If this is an unnamed function, this is the slot
228      /// number of it, otherwise it is -1.
229      int FunctionNumber;
230    public:
231      PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
232      ~PerFunctionState();
233
234      Function &getFunction() const { return F; }
235
236      bool FinishFunction();
237
238      /// GetVal - Get a value with the specified name or ID, creating a
239      /// forward reference record if needed.  This can return null if the value
240      /// exists but does not have the right type.
241      Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
242      Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
243
244      /// SetInstName - After an instruction is parsed and inserted into its
245      /// basic block, this installs its name.
246      bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
247                       Instruction *Inst);
248
249      /// GetBB - Get a basic block with the specified name or ID, creating a
250      /// forward reference record if needed.  This can return null if the value
251      /// is not a BasicBlock.
252      BasicBlock *GetBB(const std::string &Name, LocTy Loc);
253      BasicBlock *GetBB(unsigned ID, LocTy Loc);
254
255      /// DefineBB - Define the specified basic block, which is either named or
256      /// unnamed.  If there is an error, this returns null otherwise it returns
257      /// the block being defined.
258      BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
259    };
260
261    bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
262                             PerFunctionState &PFS);
263
264    bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
265    bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
266                    PerFunctionState &PFS) {
267      Loc = Lex.getLoc();
268      return ParseValue(Ty, V, PFS);
269    }
270
271    bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
272    bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
273      Loc = Lex.getLoc();
274      return ParseTypeAndValue(V, PFS);
275    }
276    bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
277                                PerFunctionState &PFS);
278    bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
279      LocTy Loc;
280      return ParseTypeAndBasicBlock(BB, Loc, PFS);
281    }
282
283    bool ParseInlineMetadata(Value *&V, PerFunctionState &PFS);
284
285    struct ParamInfo {
286      LocTy Loc;
287      Value *V;
288      unsigned Attrs;
289      ParamInfo(LocTy loc, Value *v, unsigned attrs)
290        : Loc(loc), V(v), Attrs(attrs) {}
291    };
292    bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
293                            PerFunctionState &PFS);
294
295    // Function Parsing.
296    struct ArgInfo {
297      LocTy Loc;
298      PATypeHolder Type;
299      unsigned Attrs;
300      std::string Name;
301      ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
302        : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
303    };
304    bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
305                           bool &isVarArg, bool inType);
306    bool ParseFunctionHeader(Function *&Fn, bool isDefine);
307    bool ParseFunctionBody(Function &Fn);
308    bool ParseBasicBlock(PerFunctionState &PFS);
309
310    // Instruction Parsing.
311    bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
312                          PerFunctionState &PFS);
313    bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
314
315    bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
316    bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
317    bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
318    bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
319    bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
320
321    bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
322                         unsigned OperandType);
323    bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
324    bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
325    bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
326    bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
327    bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
328    bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
329    bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
330    bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
331    bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
332    bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
333    bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
334                    BasicBlock *BB = 0, bool isAlloca = true);
335    bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
336    bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
337    bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
338    bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
339    bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
340    bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
341    bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
342
343    bool ResolveForwardRefBlockAddresses(Function *TheFn,
344                             std::vector<std::pair<ValID, GlobalValue*> > &Refs,
345                                         PerFunctionState *PFS);
346  };
347} // End llvm namespace
348
349#endif
350