reject PR3281:crash07.ll with:
[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/Type.h"
19 #include <map>
20
21 namespace llvm {
22   class Module;
23   class OpaqueType;
24   class Function;
25   class Value;
26   class BasicBlock;
27   class Instruction;
28   class Constant;
29   class GlobalValue;
30   struct ValID;
31
32   class LLParser {
33   public:
34     typedef LLLexer::LocTy LocTy;
35   private:
36
37     LLLexer Lex;
38     Module *M;
39
40     // Type resolution handling data structures.
41     std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
42     std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
43     std::vector<PATypeHolder> NumberedTypes;
44
45     struct UpRefRecord {
46       /// Loc - This is the location of the upref.
47       LocTy Loc;
48
49       /// NestingLevel - The number of nesting levels that need to be popped
50       /// before this type is resolved.
51       unsigned NestingLevel;
52
53       /// LastContainedTy - This is the type at the current binding level for
54       /// the type.  Every time we reduce the nesting level, this gets updated.
55       const Type *LastContainedTy;
56
57       /// UpRefTy - This is the actual opaque type that the upreference is
58       /// represented with.
59       OpaqueType *UpRefTy;
60
61       UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
62         : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
63           UpRefTy(URTy) {}
64     };
65     std::vector<UpRefRecord> UpRefs;
66
67     // Global Value reference information.
68     std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
69     std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
70     std::vector<GlobalValue*> NumberedVals;
71   public:
72     LLParser(MemoryBuffer *F, ParseError &Err, Module *m) : Lex(F, Err), M(m) {}
73     bool Run();
74
75   private:
76
77     bool Error(LocTy L, const std::string &Msg) const {
78       return Lex.Error(L, Msg);
79     }
80     bool TokError(const std::string &Msg) const {
81       return Error(Lex.getLoc(), Msg);
82     }
83
84     /// GetGlobalVal - Get a value with the specified name or ID, creating a
85     /// forward reference record if needed.  This can return null if the value
86     /// exists but does not have the right type.
87     GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
88     GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
89
90     // Helper Routines.
91     bool ParseToken(lltok::Kind T, const char *ErrMsg);
92     bool EatIfPresent(lltok::Kind T) {
93       if (Lex.getKind() != T) return false;
94       Lex.Lex();
95       return true;
96     }
97     bool ParseOptionalToken(lltok::Kind T, bool &Present) {
98       if (Lex.getKind() != T) {
99         Present = false;
100       } else {
101         Lex.Lex();
102         Present = true;
103       }
104       return false;
105     }
106     bool ParseStringConstant(std::string &Result);
107     bool ParseUInt32(unsigned &Val);
108     bool ParseUInt32(unsigned &Val, LocTy &Loc) {
109       Loc = Lex.getLoc();
110       return ParseUInt32(Val);
111     }
112     bool ParseOptionalAddrSpace(unsigned &AddrSpace);
113     bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
114     bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
115     bool ParseOptionalLinkage(unsigned &Linkage) {
116       bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
117     }
118     bool ParseOptionalVisibility(unsigned &Visibility);
119     bool ParseOptionalCallingConv(unsigned &CC);
120     bool ParseOptionalAlignment(unsigned &Alignment);
121     bool ParseOptionalCommaAlignment(unsigned &Alignment);
122     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
123
124     // Top-Level Entities
125     bool ParseTopLevelEntities();
126     bool ValidateEndOfModule();
127     bool ParseTargetDefinition();
128     bool ParseDepLibs();
129     bool ParseModuleAsm();
130     bool ParseUnnamedType();
131     bool ParseNamedType();
132     bool ParseDeclare();
133     bool ParseDefine();
134
135     bool ParseGlobalType(bool &IsConstant);
136     bool ParseNamedGlobal();
137     bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
138                      bool HasLinkage, unsigned Visibility);
139     bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
140
141     // Type Parsing.
142     bool ParseType(PATypeHolder &Result);
143     bool ParseType(PATypeHolder &Result, LocTy &Loc) {
144       Loc = Lex.getLoc();
145       return ParseType(Result);
146     }
147     bool ParseTypeRec(PATypeHolder &H);
148     bool ParseStructType(PATypeHolder &H, bool Packed);
149     bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
150     bool ParseFunctionType(PATypeHolder &Result);
151     PATypeHolder HandleUpRefs(const Type *Ty);
152
153     // Constants.
154     bool ParseValID(ValID &ID);
155     bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
156     bool ParseGlobalValue(const Type *Ty, Constant *&V);
157     bool ParseGlobalTypeAndValue(Constant *&V);
158     bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
159
160
161     // Function Semantic Analysis.
162     class PerFunctionState {
163       LLParser &P;
164       Function &F;
165       std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
166       std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
167       std::vector<Value*> NumberedVals;
168     public:
169       PerFunctionState(LLParser &p, Function &f);
170       ~PerFunctionState();
171
172       Function &getFunction() const { return F; }
173
174       bool VerifyFunctionComplete();
175
176       /// GetVal - Get a value with the specified name or ID, creating a
177       /// forward reference record if needed.  This can return null if the value
178       /// exists but does not have the right type.
179       Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
180       Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
181
182       /// SetInstName - After an instruction is parsed and inserted into its
183       /// basic block, this installs its name.
184       bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
185                        Instruction *Inst);
186
187       /// GetBB - Get a basic block with the specified name or ID, creating a
188       /// forward reference record if needed.  This can return null if the value
189       /// is not a BasicBlock.
190       BasicBlock *GetBB(const std::string &Name, LocTy Loc);
191       BasicBlock *GetBB(unsigned ID, LocTy Loc);
192
193       /// DefineBB - Define the specified basic block, which is either named or
194       /// unnamed.  If there is an error, this returns null otherwise it returns
195       /// the block being defined.
196       BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
197     };
198
199     bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
200                              PerFunctionState &PFS);
201
202     bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
203     bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
204                     PerFunctionState &PFS) {
205       Loc = Lex.getLoc();
206       return ParseValue(Ty, V, PFS);
207     }
208
209     bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
210     bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
211       Loc = Lex.getLoc();
212       return ParseTypeAndValue(V, PFS);
213     }
214
215     struct ParamInfo {
216       LocTy Loc;
217       Value *V;
218       unsigned Attrs;
219       ParamInfo(LocTy loc, Value *v, unsigned attrs)
220         : Loc(loc), V(v), Attrs(attrs) {}
221     };
222     bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
223                             PerFunctionState &PFS);
224
225     // Function Parsing.
226     struct ArgInfo {
227       LocTy Loc;
228       PATypeHolder Type;
229       unsigned Attrs;
230       std::string Name;
231       ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
232         : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
233     };
234     bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
235                            bool &isVarArg);
236     bool ParseFunctionHeader(Function *&Fn, bool isDefine);
237     bool ParseFunctionBody(Function &Fn);
238     bool ParseBasicBlock(PerFunctionState &PFS);
239
240     // Instruction Parsing.
241     bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
242                           PerFunctionState &PFS);
243     bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
244
245     bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
246     bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
247     bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
248     bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
249
250     bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
251     bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
252     bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
253     bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
254     bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
255     bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
256     bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
257     bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
258     bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
259     bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
260     bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
261     bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
262     bool ParseFree(Instruction *&I, PerFunctionState &PFS);
263     bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
264     bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
265     bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
266     bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
267     bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
268     bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
269   };
270 } // End llvm namespace
271
272 #endif