rewrite ParseMDNodeVector to follow the normal patter used in the .ll parser.
[oota-llvm.git] / lib / AsmParser / LLParser.h
1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the parser class for .ll files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_ASMPARSER_LLPARSER_H
15 #define LLVM_ASMPARSER_LLPARSER_H
16
17 #include "LLLexer.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/Support/ValueHandle.h"
21 #include <map>
22
23 namespace llvm {
24   class Module;
25   class OpaqueType;
26   class Function;
27   class Value;
28   class BasicBlock;
29   class Instruction;
30   class Constant;
31   class GlobalValue;
32   class MetadataBase;
33   class MDString;
34   class MDNode;
35
36   /// ValID - Represents a reference of a definition of some sort with no type.
37   /// There are several cases where we have to parse the value but where the
38   /// type can depend on later context.  This may either be a numeric reference
39   /// or a symbolic (%var) reference.  This is just a discriminated union.
40   struct ValID {
41     enum {
42       t_LocalID, t_GlobalID,      // ID in UIntVal.
43       t_LocalName, t_GlobalName,  // Name in StrVal.
44       t_APSInt, t_APFloat,        // Value in APSIntVal/APFloatVal.
45       t_Null, t_Undef, t_Zero,    // No value.
46       t_EmptyArray,               // No value:  []
47       t_Constant,                 // Value in ConstantVal.
48       t_InlineAsm,                // Value in StrVal/StrVal2/UIntVal.
49       t_MDNode,                   // Value in MDNodeVal.
50       t_MDString                  // Value in MDStringVal.
51     } Kind;
52     
53     LLLexer::LocTy Loc;
54     unsigned UIntVal;
55     std::string StrVal, StrVal2;
56     APSInt APSIntVal;
57     APFloat APFloatVal;
58     Constant *ConstantVal;
59     MDNode *MDNodeVal;
60     MDString *MDStringVal;
61     ValID() : APFloatVal(0.0) {}
62     
63     bool operator<(const ValID &RHS) const {
64       if (Kind == t_LocalID || Kind == t_GlobalID)
65         return UIntVal < RHS.UIntVal;
66       assert((Kind == t_LocalName || Kind == t_GlobalName) && 
67              "Ordering not defined for this ValID kind yet");
68       return StrVal < RHS.StrVal;
69     }
70   };
71   
72   class LLParser {
73   public:
74     typedef LLLexer::LocTy LocTy;
75   private:
76     LLVMContext& Context;
77     LLLexer Lex;
78     Module *M;
79
80     // Type resolution handling data structures.
81     std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
82     std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
83     std::vector<PATypeHolder> NumberedTypes;
84     /// MetadataCache - This map keeps track of parsed metadata constants.
85     std::map<unsigned, TrackingVH<MDNode> > MetadataCache;
86     std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
87     SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
88     struct UpRefRecord {
89       /// Loc - This is the location of the upref.
90       LocTy Loc;
91
92       /// NestingLevel - The number of nesting levels that need to be popped
93       /// before this type is resolved.
94       unsigned NestingLevel;
95
96       /// LastContainedTy - This is the type at the current binding level for
97       /// the type.  Every time we reduce the nesting level, this gets updated.
98       const Type *LastContainedTy;
99
100       /// UpRefTy - This is the actual opaque type that the upreference is
101       /// represented with.
102       OpaqueType *UpRefTy;
103
104       UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
105         : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
106           UpRefTy(URTy) {}
107     };
108     std::vector<UpRefRecord> UpRefs;
109
110     // Global Value reference information.
111     std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
112     std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
113     std::vector<GlobalValue*> NumberedVals;
114     
115     // References to blockaddress.  The key is the function ValID, the value is
116     // a list of references to blocks in that function.
117     std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
118       ForwardRefBlockAddresses;
119     
120     Function *MallocF;
121   public:
122     LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) : 
123       Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
124       M(m), MallocF(NULL) {}
125     bool Run();
126
127     LLVMContext& getContext() { return Context; }
128
129   private:
130
131     bool Error(LocTy L, const std::string &Msg) const {
132       return Lex.Error(L, Msg);
133     }
134     bool TokError(const std::string &Msg) const {
135       return Error(Lex.getLoc(), Msg);
136     }
137
138     /// GetGlobalVal - Get a value with the specified name or ID, creating a
139     /// forward reference record if needed.  This can return null if the value
140     /// exists but does not have the right type.
141     GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
142     GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
143
144     // Helper Routines.
145     bool ParseToken(lltok::Kind T, const char *ErrMsg);
146     bool EatIfPresent(lltok::Kind T) {
147       if (Lex.getKind() != T) return false;
148       Lex.Lex();
149       return true;
150     }
151     bool ParseOptionalToken(lltok::Kind T, bool &Present) {
152       if (Lex.getKind() != T) {
153         Present = false;
154       } else {
155         Lex.Lex();
156         Present = true;
157       }
158       return false;
159     }
160     bool ParseStringConstant(std::string &Result);
161     bool ParseUInt32(unsigned &Val);
162     bool ParseUInt32(unsigned &Val, LocTy &Loc) {
163       Loc = Lex.getLoc();
164       return ParseUInt32(Val);
165     }
166     bool ParseOptionalAddrSpace(unsigned &AddrSpace);
167     bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
168     bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
169     bool ParseOptionalLinkage(unsigned &Linkage) {
170       bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
171     }
172     bool ParseOptionalVisibility(unsigned &Visibility);
173     bool ParseOptionalCallingConv(CallingConv::ID &CC);
174     bool ParseOptionalAlignment(unsigned &Alignment);
175     bool ParseOptionalCustomMetadata();
176     bool ParseOptionalInfo(unsigned &Alignment);
177     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
178
179     // Top-Level Entities
180     bool ParseTopLevelEntities();
181     bool ValidateEndOfModule();
182     bool ParseTargetDefinition();
183     bool ParseDepLibs();
184     bool ParseModuleAsm();
185     bool ParseUnnamedType();
186     bool ParseNamedType();
187     bool ParseDeclare();
188     bool ParseDefine();
189
190     bool ParseGlobalType(bool &IsConstant);
191     bool ParseUnnamedGlobal();
192     bool ParseNamedGlobal();
193     bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
194                      bool HasLinkage, unsigned Visibility);
195     bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
196     bool ParseStandaloneMetadata();
197     bool ParseNamedMetadata();
198     bool ParseMDString(MDString *&Result);
199     bool ParseMDNodeID(MDNode *&Result);
200
201     // Type Parsing.
202     bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
203     bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
204       Loc = Lex.getLoc();
205       return ParseType(Result, AllowVoid);
206     }
207     bool ParseTypeRec(PATypeHolder &H);
208     bool ParseStructType(PATypeHolder &H, bool Packed);
209     bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
210     bool ParseFunctionType(PATypeHolder &Result);
211     PATypeHolder HandleUpRefs(const Type *Ty);
212
213     // Constants.
214     bool ParseValID(ValID &ID);
215     bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
216     bool ConvertGlobalOrMetadataValIDToValue(const Type *Ty, ValID &ID,
217                                              Value *&V);
218     bool ParseGlobalValue(const Type *Ty, Constant *&V);
219     bool ParseGlobalTypeAndValue(Constant *&V);
220     bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
221     bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
222
223
224     // Function Semantic Analysis.
225     class PerFunctionState {
226       LLParser &P;
227       Function &F;
228       std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
229       std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
230       std::vector<Value*> NumberedVals;
231       
232       /// FunctionNumber - If this is an unnamed function, this is the slot
233       /// number of it, otherwise it is -1.
234       int FunctionNumber;
235     public:
236       PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
237       ~PerFunctionState();
238
239       Function &getFunction() const { return F; }
240
241       bool FinishFunction();
242
243       /// GetVal - Get a value with the specified name or ID, creating a
244       /// forward reference record if needed.  This can return null if the value
245       /// exists but does not have the right type.
246       Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
247       Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
248
249       /// SetInstName - After an instruction is parsed and inserted into its
250       /// basic block, this installs its name.
251       bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
252                        Instruction *Inst);
253
254       /// GetBB - Get a basic block with the specified name or ID, creating a
255       /// forward reference record if needed.  This can return null if the value
256       /// is not a BasicBlock.
257       BasicBlock *GetBB(const std::string &Name, LocTy Loc);
258       BasicBlock *GetBB(unsigned ID, LocTy Loc);
259
260       /// DefineBB - Define the specified basic block, which is either named or
261       /// unnamed.  If there is an error, this returns null otherwise it returns
262       /// the block being defined.
263       BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
264     };
265
266     bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
267                              PerFunctionState &PFS);
268
269     bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
270     bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
271                     PerFunctionState &PFS) {
272       Loc = Lex.getLoc();
273       return ParseValue(Ty, V, PFS);
274     }
275
276     bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
277     bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
278       Loc = Lex.getLoc();
279       return ParseTypeAndValue(V, PFS);
280     }
281     bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
282                                 PerFunctionState &PFS);
283     bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
284       LocTy Loc;
285       return ParseTypeAndBasicBlock(BB, Loc, PFS);
286     }
287
288     struct ParamInfo {
289       LocTy Loc;
290       Value *V;
291       unsigned Attrs;
292       ParamInfo(LocTy loc, Value *v, unsigned attrs)
293         : Loc(loc), V(v), Attrs(attrs) {}
294     };
295     bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
296                             PerFunctionState &PFS);
297
298     // Function Parsing.
299     struct ArgInfo {
300       LocTy Loc;
301       PATypeHolder Type;
302       unsigned Attrs;
303       std::string Name;
304       ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
305         : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
306     };
307     bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
308                            bool &isVarArg, bool inType);
309     bool ParseFunctionHeader(Function *&Fn, bool isDefine);
310     bool ParseFunctionBody(Function &Fn);
311     bool ParseBasicBlock(PerFunctionState &PFS);
312
313     // Instruction Parsing.
314     bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
315                           PerFunctionState &PFS);
316     bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
317
318     bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
319     bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
320     bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
321     bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
322     bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
323
324     bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
325                          unsigned OperandType);
326     bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
327     bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
328     bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
329     bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
330     bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
331     bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
332     bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
333     bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
334     bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
335     bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
336     bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
337                     BasicBlock *BB = 0, bool isAlloca = true);
338     bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
339     bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
340     bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
341     bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
342     bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
343     bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
344     bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
345     
346     bool ResolveForwardRefBlockAddresses(Function *TheFn, 
347                              std::vector<std::pair<ValID, GlobalValue*> > &Refs,
348                                          PerFunctionState *PFS);
349   };
350 } // End llvm namespace
351
352 #endif