blob: 6ef92c12b5205dcd5ec21318537df6a2308d7e35 [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===-- 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
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_ASMPARSER_LLPARSER_H
15#define LLVM_LIB_ASMPARSER_LLPARSER_H
Chris Lattnerac161bf2009-01-02 07:01:27 +000016
17#include "LLLexer.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000018#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringMap.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Attributes.h"
21#include "llvm/IR/Instructions.h"
22#include "llvm/IR/Module.h"
23#include "llvm/IR/Operator.h"
24#include "llvm/IR/Type.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000025#include "llvm/IR/ValueHandle.h"
Chris Lattner218b22f2009-12-29 21:43:58 +000026#include <map>
Chris Lattnerac161bf2009-01-02 07:01:27 +000027
28namespace llvm {
29 class Module;
30 class OpaqueType;
31 class Function;
32 class Value;
33 class BasicBlock;
34 class Instruction;
35 class Constant;
36 class GlobalValue;
David Majnemerdad0a642014-06-27 18:19:56 +000037 class Comdat;
Nick Lewycky49f89192009-04-04 07:22:01 +000038 class MDString;
39 class MDNode;
Alex Lorenz8955f7d2015-06-23 17:10:10 +000040 struct SlotMapping;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000041 class StructType;
Misha Brukman1d9a93d2009-01-02 22:46:48 +000042
Chris Lattner3432c622009-10-28 03:39:23 +000043 /// ValID - Represents a reference of a definition of some sort with no type.
44 /// There are several cases where we have to parse the value but where the
45 /// type can depend on later context. This may either be a numeric reference
46 /// or a symbolic (%var) reference. This is just a discriminated union.
Benjamin Kramer079b96e2013-09-11 18:05:11 +000047 struct ValID {
Chris Lattner3432c622009-10-28 03:39:23 +000048 enum {
49 t_LocalID, t_GlobalID, // ID in UIntVal.
50 t_LocalName, t_GlobalName, // Name in StrVal.
51 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
52 t_Null, t_Undef, t_Zero, // No value.
53 t_EmptyArray, // No value: []
54 t_Constant, // Value in ConstantVal.
David Blaikie41ba2b42015-07-27 23:32:19 +000055 t_InlineAsm, // Value in FTy/StrVal/StrVal2/UIntVal.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000056 t_ConstantStruct, // Value in ConstantStructElts.
57 t_PackedConstantStruct // Value in ConstantStructElts.
Chris Lattner3432c622009-10-28 03:39:23 +000058 } Kind;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Chris Lattner3432c622009-10-28 03:39:23 +000060 LLLexer::LocTy Loc;
61 unsigned UIntVal;
David Blaikie41ba2b42015-07-27 23:32:19 +000062 FunctionType *FTy;
Chris Lattner3432c622009-10-28 03:39:23 +000063 std::string StrVal, StrVal2;
64 APSInt APSIntVal;
65 APFloat APFloatVal;
66 Constant *ConstantVal;
Reid Kleckner2ae03e12015-03-04 18:31:10 +000067 Constant **ConstantStructElts;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000068
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000069 ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
Reid Kleckner2ae03e12015-03-04 18:31:10 +000070 ~ValID() {
71 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
72 delete [] ConstantStructElts;
73 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000074
Chris Lattner3432c622009-10-28 03:39:23 +000075 bool operator<(const ValID &RHS) const {
76 if (Kind == t_LocalID || Kind == t_GlobalID)
77 return UIntVal < RHS.UIntVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000078 assert((Kind == t_LocalName || Kind == t_GlobalName ||
Michael Ilseman26ee2b82012-11-15 22:34:00 +000079 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
Chris Lattner3432c622009-10-28 03:39:23 +000080 "Ordering not defined for this ValID kind yet");
81 return StrVal < RHS.StrVal;
82 }
83 };
Michael Ilseman26ee2b82012-11-15 22:34:00 +000084
Benjamin Kramer079b96e2013-09-11 18:05:11 +000085 class LLParser {
Chris Lattnerac161bf2009-01-02 07:01:27 +000086 public:
87 typedef LLLexer::LocTy LocTy;
88 private:
Chris Lattner2e664bd2010-04-07 04:08:57 +000089 LLVMContext &Context;
Chris Lattnerac161bf2009-01-02 07:01:27 +000090 LLLexer Lex;
91 Module *M;
Alex Lorenz8955f7d2015-06-23 17:10:10 +000092 SlotMapping *Slots;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000093
Chris Lattner8eff0152010-04-01 05:14:45 +000094 // Instruction metadata resolution. Each instruction can have a list of
95 // MDRef info associated with them.
Dan Gohman7c7f13a2010-08-24 14:31:06 +000096 //
97 // The simpler approach of just creating temporary MDNodes and then calling
98 // RAUW on them when the definition is processed doesn't work because some
99 // instruction metadata kinds, such as dbg, get stored in the IR in an
100 // "optimized" format which doesn't participate in the normal value use
101 // lists. This means that RAUW doesn't work, even on temporary MDNodes
102 // which otherwise support RAUW. Instead, we defer resolving MDNode
103 // references until the definitions have been processed.
Chris Lattner8eff0152010-04-01 05:14:45 +0000104 struct MDRef {
105 SMLoc Loc;
106 unsigned MDKind, MDSlot;
107 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000108
Manman Ren209b17c2013-09-28 00:22:27 +0000109 SmallVector<Instruction*, 64> InstsWithTBAATag;
110
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000111 // Type resolution handling data structures. The location is set when we
112 // have processed a use of the type but not a definition yet.
113 StringMap<std::pair<Type*, LocTy> > NamedTypes;
David Majnemer19b51052015-02-11 07:43:56 +0000114 std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000115
David Majnemer19b51052015-02-11 07:43:56 +0000116 std::map<unsigned, TrackingMDNodeRef> NumberedMetadata;
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000117 std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000118
119 // Global Value reference information.
120 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
121 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
122 std::vector<GlobalValue*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000123
David Majnemerdad0a642014-06-27 18:19:56 +0000124 // Comdat forward reference information.
125 std::map<std::string, LocTy> ForwardRefComdats;
126
Chris Lattner3432c622009-10-28 03:39:23 +0000127 // References to blockaddress. The key is the function ValID, the value is
128 // a list of references to blocks in that function.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000129 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
130 class PerFunctionState;
131 /// Reference to per-function state to allow basic blocks to be
132 /// forward-referenced by blockaddress instructions within the same
133 /// function.
134 PerFunctionState *BlockAddressPFS;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000135
Bill Wendling63b88192013-02-06 06:52:58 +0000136 // Attribute builder reference information.
Bill Wendlingb32b0412013-02-08 06:32:06 +0000137 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
138 std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
Bill Wendling63b88192013-02-06 06:52:58 +0000139
Chris Lattnerac161bf2009-01-02 07:01:27 +0000140 public:
Alex Lorenz8955f7d2015-06-23 17:10:10 +0000141 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
142 SlotMapping *Slots = nullptr)
143 : Context(M->getContext()), Lex(F, SM, Err, M->getContext()), M(M),
144 Slots(Slots), BlockAddressPFS(nullptr) {}
Chris Lattnerad6f3352009-01-04 20:44:11 +0000145 bool Run();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000146
Alex Lorenzd2255952015-07-17 22:07:03 +0000147 bool parseStandaloneConstantValue(Constant *&C);
148
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000149 LLVMContext &getContext() { return Context; }
Owen Anderson09063ce2009-07-02 17:04:01 +0000150
Chris Lattnerac161bf2009-01-02 07:01:27 +0000151 private:
152
Benjamin Kramerc7583112010-09-27 17:42:11 +0000153 bool Error(LocTy L, const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000154 return Lex.Error(L, Msg);
155 }
Benjamin Kramerc7583112010-09-27 17:42:11 +0000156 bool TokError(const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000157 return Error(Lex.getLoc(), Msg);
158 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000159
Chris Lattnerac161bf2009-01-02 07:01:27 +0000160 /// GetGlobalVal - Get a value with the specified name or ID, creating a
161 /// forward reference record if needed. This can return null if the value
162 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000163 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
164 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000165
David Majnemerdad0a642014-06-27 18:19:56 +0000166 /// Get a Comdat with the specified name, creating a forward reference
167 /// record if needed.
168 Comdat *getComdat(const std::string &N, LocTy Loc);
169
Chris Lattnerac161bf2009-01-02 07:01:27 +0000170 // Helper Routines.
171 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3822f632009-01-02 08:05:26 +0000172 bool EatIfPresent(lltok::Kind T) {
173 if (Lex.getKind() != T) return false;
174 Lex.Lex();
175 return true;
176 }
Michael Ilseman92053172012-11-27 00:42:44 +0000177
178 FastMathFlags EatFastMathFlagsIfPresent() {
179 FastMathFlags FMF;
180 while (true)
181 switch (Lex.getKind()) {
Michael Ilseman65f14352012-12-09 21:12:04 +0000182 case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue;
183 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
184 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
185 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
186 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
Michael Ilseman92053172012-11-27 00:42:44 +0000187 default: return FMF;
188 }
189 return FMF;
190 }
191
Craig Topperada08572014-04-16 04:21:27 +0000192 bool ParseOptionalToken(lltok::Kind T, bool &Present,
193 LocTy *Loc = nullptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000194 if (Lex.getKind() != T) {
195 Present = false;
196 } else {
Rafael Espindola026d1522011-01-13 01:30:30 +0000197 if (Loc)
198 *Loc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000199 Lex.Lex();
200 Present = true;
201 }
202 return false;
203 }
Chris Lattner3822f632009-01-02 08:05:26 +0000204 bool ParseStringConstant(std::string &Result);
205 bool ParseUInt32(unsigned &Val);
206 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000207 Loc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000208 return ParseUInt32(Val);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000209 }
Hal Finkelb0407ba2014-07-18 15:51:28 +0000210 bool ParseUInt64(uint64_t &Val);
211 bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
212 Loc = Lex.getLoc();
213 return ParseUInt64(Val);
214 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000215
216 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
217 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000218 bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
219 return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
220 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000221 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
Bill Wendling34c2eb22012-12-04 23:40:58 +0000222 bool ParseOptionalParamAttrs(AttrBuilder &B);
223 bool ParseOptionalReturnAttrs(AttrBuilder &B);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000224 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
225 bool ParseOptionalLinkage(unsigned &Linkage) {
226 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
227 }
228 bool ParseOptionalVisibility(unsigned &Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000229 bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
Alexey Samsonov17a9cff2014-09-10 18:00:17 +0000230 bool ParseOptionalCallingConv(unsigned &CC);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000231 bool ParseOptionalAlignment(unsigned &Alignment);
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000232 bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000233 bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
234 AtomicOrdering &Ordering);
Tim Northovere94a5182014-03-11 10:48:52 +0000235 bool ParseOrdering(AtomicOrdering &Ordering);
Charles Davisbe5557e2010-02-12 00:31:15 +0000236 bool ParseOptionalStackAlignment(unsigned &Alignment);
Chris Lattnerb2f39502009-12-30 05:44:30 +0000237 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000238 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
Chris Lattner28f1eeb2009-12-30 05:14:00 +0000239 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
240 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
241 bool AteExtraComma;
242 if (ParseIndexList(Indices, AteExtraComma)) return true;
243 if (AteExtraComma)
244 return TokError("expected index");
245 return false;
246 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000247
Chris Lattnerac161bf2009-01-02 07:01:27 +0000248 // Top-Level Entities
249 bool ParseTopLevelEntities();
250 bool ValidateEndOfModule();
251 bool ParseTargetDefinition();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000252 bool ParseModuleAsm();
Bill Wendling706d3d62012-11-28 08:41:48 +0000253 bool ParseDepLibs(); // FIXME: Remove in 4.0.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000254 bool ParseUnnamedType();
255 bool ParseNamedType();
256 bool ParseDeclare();
257 bool ParseDefine();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000258
Chris Lattnerac161bf2009-01-02 07:01:27 +0000259 bool ParseGlobalType(bool &IsConstant);
Dan Gohman466876b2009-08-12 23:32:33 +0000260 bool ParseUnnamedGlobal();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000261 bool ParseNamedGlobal();
262 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000263 bool HasLinkage, unsigned Visibility,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000264 unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000265 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000266 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Linkage,
267 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000268 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
David Majnemerdad0a642014-06-27 18:19:56 +0000269 bool parseComdat();
Devang Patel39e64d42009-07-01 19:21:12 +0000270 bool ParseStandaloneMetadata();
Devang Patelbe626972009-07-29 00:34:02 +0000271 bool ParseNamedMetadata();
Chris Lattner1797fc72009-12-29 21:53:55 +0000272 bool ParseMDString(MDString *&Result);
Chris Lattner6dac02a2009-12-30 04:15:23 +0000273 bool ParseMDNodeID(MDNode *&Result);
Bill Wendling63b88192013-02-06 06:52:58 +0000274 bool ParseUnnamedAttrGrp();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000275 bool ParseFnAttributeValuePairs(AttrBuilder &B,
276 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000277 bool inAttrGrp, LocTy &BuiltinLoc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000278
Chris Lattnerac161bf2009-01-02 07:01:27 +0000279 // Type Parsing.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000280 bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
281 bool ParseType(Type *&Result, bool AllowVoid = false) {
282 return ParseType(Result, "expected type", AllowVoid);
283 }
284 bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc,
285 bool AllowVoid = false) {
286 Loc = Lex.getLoc();
287 return ParseType(Result, Msg, AllowVoid);
288 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000289 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000290 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000291 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000292 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000293 bool ParseAnonStructType(Type *&Result, bool Packed);
294 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
295 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
296 std::pair<Type*, LocTy> &Entry,
297 Type *&ResultTy);
298
299 bool ParseArrayVectorType(Type *&Result, bool isVector);
300 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000301
Chris Lattnerac161bf2009-01-02 07:01:27 +0000302 // Function Semantic Analysis.
303 class PerFunctionState {
304 LLParser &P;
305 Function &F;
306 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
307 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
308 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000309
Chris Lattner3432c622009-10-28 03:39:23 +0000310 /// FunctionNumber - If this is an unnamed function, this is the slot
311 /// number of it, otherwise it is -1.
312 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000313 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000314 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000315 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000316
Chris Lattnerac161bf2009-01-02 07:01:27 +0000317 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000318
Chris Lattner3432c622009-10-28 03:39:23 +0000319 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000320
Chris Lattnerac161bf2009-01-02 07:01:27 +0000321 /// GetVal - Get a value with the specified name or ID, creating a
322 /// forward reference record if needed. This can return null if the value
323 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000324 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
325 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000326
Chris Lattnerac161bf2009-01-02 07:01:27 +0000327 /// SetInstName - After an instruction is parsed and inserted into its
328 /// basic block, this installs its name.
329 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
330 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000331
Chris Lattnerac161bf2009-01-02 07:01:27 +0000332 /// GetBB - Get a basic block with the specified name or ID, creating a
333 /// forward reference record if needed. This can return null if the value
334 /// is not a BasicBlock.
335 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
336 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000337
Chris Lattnerac161bf2009-01-02 07:01:27 +0000338 /// DefineBB - Define the specified basic block, which is either named or
339 /// unnamed. If there is an error, this returns null otherwise it returns
340 /// the block being defined.
341 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000342
343 bool resolveForwardRefBlockAddresses();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000344 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000345
Chris Lattner229907c2011-07-18 04:54:35 +0000346 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +0000347 PerFunctionState *PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000348
Alex Lorenzd2255952015-07-17 22:07:03 +0000349 bool parseConstantValue(Type *Ty, Constant *&C);
Chris Lattner229907c2011-07-18 04:54:35 +0000350 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
351 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000352 return ParseValue(Ty, V, &PFS);
353 }
Chris Lattner229907c2011-07-18 04:54:35 +0000354 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000355 PerFunctionState &PFS) {
356 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000357 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000358 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000359
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000360 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
361 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
362 return ParseTypeAndValue(V, &PFS);
363 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000364 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
365 Loc = Lex.getLoc();
366 return ParseTypeAndValue(V, PFS);
367 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000368 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
369 PerFunctionState &PFS);
370 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
371 LocTy Loc;
372 return ParseTypeAndBasicBlock(BB, Loc, PFS);
373 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000374
Chris Lattner392be582010-02-12 20:49:41 +0000375
Chris Lattnerac161bf2009-01-02 07:01:27 +0000376 struct ParamInfo {
377 LocTy Loc;
378 Value *V;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000379 AttributeSet Attrs;
380 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000381 : Loc(loc), V(v), Attrs(attrs) {}
382 };
383 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +0000384 PerFunctionState &PFS,
385 bool IsMustTailCall = false,
386 bool InVarArgsFunc = false);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000387
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000388 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000389 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000390 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000391 bool ParseGlobalTypeAndValue(Constant *&V);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000392 bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts);
Rafael Espindola83a362c2015-01-06 22:55:16 +0000393 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000394 bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +0000395 bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
396 PerFunctionState *PFS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000397 bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +0000398 bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false);
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +0000399 bool ParseMDNode(MDNode *&MD);
400 bool ParseMDNodeTail(MDNode *&MD);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000401 bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs);
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +0000402 bool ParseMetadataAttachment(unsigned &Kind, MDNode *&MD);
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +0000403 bool ParseInstructionMetadata(Instruction &Inst);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000404 bool ParseOptionalFunctionMetadata(Function &F);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000405
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +0000406 template <class FieldTy>
407 bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +0000408 template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000409 template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +0000410 bool ParseMDFieldsImplBody(ParserTy parseField);
411 template <class ParserTy>
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000412 bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000413 bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +0000414
415#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
416 bool Parse##CLASS(MDNode *&Result, bool IsDistinct);
417#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000418
Chris Lattnerac161bf2009-01-02 07:01:27 +0000419 // Function Parsing.
420 struct ArgInfo {
421 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000422 Type *Ty;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000423 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000424 std::string Name;
Bill Wendlingfe0021a2013-01-31 00:29:54 +0000425 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000426 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000427 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000428 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000429 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
430 bool ParseFunctionBody(Function &Fn);
431 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000432
Reid Kleckner5772b772014-04-24 20:14:34 +0000433 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
434
Chris Lattner77b89dc2009-12-30 05:23:43 +0000435 // Instruction Parsing. Each instruction parsing routine can return with a
436 // normal result, an error result, or return having eaten an extra comma.
437 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
438 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
439 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000440 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000441
Chris Lattner33de4272011-06-17 06:49:41 +0000442 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000443 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
444 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000445 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000446 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000447 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000448
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000449 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
450 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000451 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
452 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
453 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
454 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000455 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000456 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
457 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
458 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000459 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000460 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000461 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
462 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000463 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000464 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
465 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000466 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
467 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000468 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000469 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
470 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
471 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000472
473 // Use-list order directives.
474 bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
475 bool ParseUseListOrderBB();
476 bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
477 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000478 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000479} // End llvm namespace
Chris Lattnerac161bf2009-01-02 07:01:27 +0000480
481#endif