blob: a3252ff5f19655b3e6b1d9bf94cd5c97f5badd5a [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"
George Burgess IV278199f2016-04-12 01:05:35 +000018#include "llvm/ADT/Optional.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000019#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 {
David Majnemerf0f224d2015-11-11 21:57:16 +000049 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, t_None, // No value.
53 t_EmptyArray, // No value: []
54 t_Constant, // Value in ConstantVal.
55 t_InlineAsm, // Value in FTy/StrVal/StrVal2/UIntVal.
56 t_ConstantStruct, // Value in ConstantStructElts.
57 t_PackedConstantStruct // Value in ConstantStructElts.
David Blaikieadbda4b2015-08-03 20:08:41 +000058 } Kind = t_LocalID;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000059
Chris Lattner3432c622009-10-28 03:39:23 +000060 LLLexer::LocTy Loc;
61 unsigned UIntVal;
Karl Schimpf44876c52015-09-03 16:18:32 +000062 FunctionType *FTy = nullptr;
Chris Lattner3432c622009-10-28 03:39:23 +000063 std::string StrVal, StrVal2;
64 APSInt APSIntVal;
David Blaikieadbda4b2015-08-03 20:08:41 +000065 APFloat APFloatVal{0.0};
Chris Lattner3432c622009-10-28 03:39:23 +000066 Constant *ConstantVal;
David Blaikieadbda4b2015-08-03 20:08:41 +000067 std::unique_ptr<Constant *[]> ConstantStructElts;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000068
David Blaikieadbda4b2015-08-03 20:08:41 +000069 ValID() = default;
David Blaikie69374412015-08-03 20:30:53 +000070 ValID(const ValID &RHS)
David Blaikieadbda4b2015-08-03 20:08:41 +000071 : Kind(RHS.Kind), Loc(RHS.Loc), UIntVal(RHS.UIntVal), FTy(RHS.FTy),
David Blaikie69374412015-08-03 20:30:53 +000072 StrVal(RHS.StrVal), StrVal2(RHS.StrVal2), APSIntVal(RHS.APSIntVal),
73 APFloatVal(RHS.APFloatVal), ConstantVal(RHS.ConstantVal) {
74 assert(!RHS.ConstantStructElts);
75 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +000076
Chris Lattner3432c622009-10-28 03:39:23 +000077 bool operator<(const ValID &RHS) const {
78 if (Kind == t_LocalID || Kind == t_GlobalID)
79 return UIntVal < RHS.UIntVal;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000080 assert((Kind == t_LocalName || Kind == t_GlobalName ||
Michael Ilseman26ee2b82012-11-15 22:34:00 +000081 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
Chris Lattner3432c622009-10-28 03:39:23 +000082 "Ordering not defined for this ValID kind yet");
83 return StrVal < RHS.StrVal;
84 }
85 };
Michael Ilseman26ee2b82012-11-15 22:34:00 +000086
Benjamin Kramer079b96e2013-09-11 18:05:11 +000087 class LLParser {
Chris Lattnerac161bf2009-01-02 07:01:27 +000088 public:
89 typedef LLLexer::LocTy LocTy;
90 private:
Chris Lattner2e664bd2010-04-07 04:08:57 +000091 LLVMContext &Context;
Chris Lattnerac161bf2009-01-02 07:01:27 +000092 LLLexer Lex;
93 Module *M;
Alex Lorenz8955f7d2015-06-23 17:10:10 +000094 SlotMapping *Slots;
Michael Ilseman26ee2b82012-11-15 22:34:00 +000095
Chris Lattner8eff0152010-04-01 05:14:45 +000096 // Instruction metadata resolution. Each instruction can have a list of
97 // MDRef info associated with them.
Dan Gohman7c7f13a2010-08-24 14:31:06 +000098 //
99 // The simpler approach of just creating temporary MDNodes and then calling
100 // RAUW on them when the definition is processed doesn't work because some
101 // instruction metadata kinds, such as dbg, get stored in the IR in an
102 // "optimized" format which doesn't participate in the normal value use
103 // lists. This means that RAUW doesn't work, even on temporary MDNodes
104 // which otherwise support RAUW. Instead, we defer resolving MDNode
105 // references until the definitions have been processed.
Chris Lattner8eff0152010-04-01 05:14:45 +0000106 struct MDRef {
107 SMLoc Loc;
108 unsigned MDKind, MDSlot;
109 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000110
Manman Ren209b17c2013-09-28 00:22:27 +0000111 SmallVector<Instruction*, 64> InstsWithTBAATag;
112
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000113 // Type resolution handling data structures. The location is set when we
114 // have processed a use of the type but not a definition yet.
115 StringMap<std::pair<Type*, LocTy> > NamedTypes;
David Majnemer19b51052015-02-11 07:43:56 +0000116 std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000117
David Majnemer19b51052015-02-11 07:43:56 +0000118 std::map<unsigned, TrackingMDNodeRef> NumberedMetadata;
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000119 std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000120
121 // Global Value reference information.
122 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
123 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
124 std::vector<GlobalValue*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000125
David Majnemerdad0a642014-06-27 18:19:56 +0000126 // Comdat forward reference information.
127 std::map<std::string, LocTy> ForwardRefComdats;
128
Chris Lattner3432c622009-10-28 03:39:23 +0000129 // References to blockaddress. The key is the function ValID, the value is
130 // a list of references to blocks in that function.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000131 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
132 class PerFunctionState;
133 /// Reference to per-function state to allow basic blocks to be
134 /// forward-referenced by blockaddress instructions within the same
135 /// function.
136 PerFunctionState *BlockAddressPFS;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000137
Bill Wendling63b88192013-02-06 06:52:58 +0000138 // Attribute builder reference information.
Bill Wendlingb32b0412013-02-08 06:32:06 +0000139 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
140 std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
Bill Wendling63b88192013-02-06 06:52:58 +0000141
Adrian Prantla8b2ddb2017-10-02 18:31:29 +0000142 /// Only the llvm-as tool may set this to false to bypass
143 /// UpgradeDebuginfo so it can generate broken bitcode.
144 bool UpgradeDebugInfo;
145
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000146 /// DataLayout string to override that in LLVM assembly.
147 StringRef DataLayoutStr;
148
Chris Lattnerac161bf2009-01-02 07:01:27 +0000149 public:
Alex Lorenz8955f7d2015-06-23 17:10:10 +0000150 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000151 SlotMapping *Slots = nullptr, bool UpgradeDebugInfo = true,
152 StringRef DataLayoutString = "")
Alex Lorenz8955f7d2015-06-23 17:10:10 +0000153 : Context(M->getContext()), Lex(F, SM, Err, M->getContext()), M(M),
Adrian Prantla8b2ddb2017-10-02 18:31:29 +0000154 Slots(Slots), BlockAddressPFS(nullptr),
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000155 UpgradeDebugInfo(UpgradeDebugInfo), DataLayoutStr(DataLayoutString) {
156 if (!DataLayoutStr.empty())
157 M->setDataLayout(DataLayoutStr);
158 }
Chris Lattnerad6f3352009-01-04 20:44:11 +0000159 bool Run();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000160
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000161 bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
Alex Lorenzd2255952015-07-17 22:07:03 +0000162
Quentin Colombetdafed5d2016-03-08 00:37:07 +0000163 bool parseTypeAtBeginning(Type *&Ty, unsigned &Read,
164 const SlotMapping *Slots);
Quentin Colombet81e72b42016-03-07 22:09:05 +0000165
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000166 LLVMContext &getContext() { return Context; }
Owen Anderson09063ce2009-07-02 17:04:01 +0000167
Chris Lattnerac161bf2009-01-02 07:01:27 +0000168 private:
169
Benjamin Kramerc7583112010-09-27 17:42:11 +0000170 bool Error(LocTy L, const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000171 return Lex.Error(L, Msg);
172 }
Benjamin Kramerc7583112010-09-27 17:42:11 +0000173 bool TokError(const Twine &Msg) const {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000174 return Error(Lex.getLoc(), Msg);
175 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000176
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000177 /// Restore the internal name and slot mappings using the mappings that
178 /// were created at an earlier parsing stage.
179 void restoreParsingState(const SlotMapping *Slots);
180
Chris Lattnerac161bf2009-01-02 07:01:27 +0000181 /// GetGlobalVal - Get a value with the specified name or ID, creating a
182 /// forward reference record if needed. This can return null if the value
183 /// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +0000184 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
185 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000186
David Majnemerdad0a642014-06-27 18:19:56 +0000187 /// Get a Comdat with the specified name, creating a forward reference
188 /// record if needed.
189 Comdat *getComdat(const std::string &N, LocTy Loc);
190
Chris Lattnerac161bf2009-01-02 07:01:27 +0000191 // Helper Routines.
192 bool ParseToken(lltok::Kind T, const char *ErrMsg);
Chris Lattner3822f632009-01-02 08:05:26 +0000193 bool EatIfPresent(lltok::Kind T) {
194 if (Lex.getKind() != T) return false;
195 Lex.Lex();
196 return true;
197 }
Michael Ilseman92053172012-11-27 00:42:44 +0000198
199 FastMathFlags EatFastMathFlagsIfPresent() {
200 FastMathFlags FMF;
201 while (true)
202 switch (Lex.getKind()) {
Sanjay Patel629c4112017-11-06 16:27:15 +0000203 case lltok::kw_fast: FMF.setFast(); Lex.Lex(); continue;
Michael Ilseman65f14352012-12-09 21:12:04 +0000204 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
205 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
206 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
207 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
Adam Nemetcd847a82017-03-28 20:11:52 +0000208 case lltok::kw_contract:
209 FMF.setAllowContract(true);
210 Lex.Lex();
211 continue;
Sanjay Patel629c4112017-11-06 16:27:15 +0000212 case lltok::kw_reassoc: FMF.setAllowReassoc(); Lex.Lex(); continue;
213 case lltok::kw_afn: FMF.setApproxFunc(); Lex.Lex(); continue;
Michael Ilseman92053172012-11-27 00:42:44 +0000214 default: return FMF;
215 }
216 return FMF;
217 }
218
Craig Topperada08572014-04-16 04:21:27 +0000219 bool ParseOptionalToken(lltok::Kind T, bool &Present,
220 LocTy *Loc = nullptr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000221 if (Lex.getKind() != T) {
222 Present = false;
223 } else {
Rafael Espindola026d1522011-01-13 01:30:30 +0000224 if (Loc)
225 *Loc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000226 Lex.Lex();
227 Present = true;
228 }
229 return false;
230 }
Chris Lattner3822f632009-01-02 08:05:26 +0000231 bool ParseStringConstant(std::string &Result);
232 bool ParseUInt32(unsigned &Val);
233 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000234 Loc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000235 return ParseUInt32(Val);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000236 }
Hal Finkelb0407ba2014-07-18 15:51:28 +0000237 bool ParseUInt64(uint64_t &Val);
238 bool ParseUInt64(uint64_t &Val, LocTy &Loc) {
239 Loc = Lex.getLoc();
240 return ParseUInt64(Val);
241 }
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000242
Artur Pilipenko17376c42015-08-03 14:31:49 +0000243 bool ParseStringAttribute(AttrBuilder &B);
244
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000245 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
246 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000247 bool ParseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr &UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000248 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
Bill Wendling34c2eb22012-12-04 23:40:58 +0000249 bool ParseOptionalParamAttrs(AttrBuilder &B);
250 bool ParseOptionalReturnAttrs(AttrBuilder &B);
Rafael Espindola2615c9e2016-05-12 12:37:52 +0000251 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000252 unsigned &Visibility, unsigned &DLLStorageClass,
253 bool &DSOLocal);
254 void ParseOptionalDSOLocal(bool &DSOLocal);
Rafael Espindola2615c9e2016-05-12 12:37:52 +0000255 void ParseOptionalVisibility(unsigned &Visibility);
256 void ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
Alexey Samsonov17a9cff2014-09-10 18:00:17 +0000257 bool ParseOptionalCallingConv(unsigned &CC);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000258 bool ParseOptionalAlignment(unsigned &Alignment);
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000259 bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000260 bool ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +0000261 AtomicOrdering &Ordering);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000262 bool ParseScope(SyncScope::ID &SSID);
Tim Northovere94a5182014-03-11 10:48:52 +0000263 bool ParseOrdering(AtomicOrdering &Ordering);
Charles Davisbe5557e2010-02-12 00:31:15 +0000264 bool ParseOptionalStackAlignment(unsigned &Alignment);
Chris Lattnerb2f39502009-12-30 05:44:30 +0000265 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
Matt Arsenault3c1fc762017-04-10 22:27:50 +0000266 bool ParseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
267 bool &AteExtraComma);
Reid Kleckner436c42e2014-01-17 23:58:17 +0000268 bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
George Burgess IV278199f2016-04-12 01:05:35 +0000269 bool parseAllocSizeArguments(unsigned &ElemSizeArg,
270 Optional<unsigned> &HowManyArg);
271 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,
272 bool &AteExtraComma);
Chris Lattner28f1eeb2009-12-30 05:14:00 +0000273 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
274 bool AteExtraComma;
275 if (ParseIndexList(Indices, AteExtraComma)) return true;
276 if (AteExtraComma)
277 return TokError("expected index");
278 return false;
279 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000280
Chris Lattnerac161bf2009-01-02 07:01:27 +0000281 // Top-Level Entities
282 bool ParseTopLevelEntities();
283 bool ValidateEndOfModule();
284 bool ParseTargetDefinition();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000285 bool ParseModuleAsm();
Teresa Johnson83c517c2016-03-30 18:15:08 +0000286 bool ParseSourceFileName();
Bill Wendling706d3d62012-11-28 08:41:48 +0000287 bool ParseDepLibs(); // FIXME: Remove in 4.0.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000288 bool ParseUnnamedType();
289 bool ParseNamedType();
290 bool ParseDeclare();
291 bool ParseDefine();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000292
Chris Lattnerac161bf2009-01-02 07:01:27 +0000293 bool ParseGlobalType(bool &IsConstant);
Dan Gohman466876b2009-08-12 23:32:33 +0000294 bool ParseUnnamedGlobal();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000295 bool ParseNamedGlobal();
296 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
Nico Rieck7157bb72014-01-14 15:22:47 +0000297 bool HasLinkage, unsigned Visibility,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000298 unsigned DLLStorageClass, bool DSOLocal,
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000299 GlobalVariable::ThreadLocalMode TLM,
300 GlobalVariable::UnnamedAddr UnnamedAddr);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000301 bool parseIndirectSymbol(const std::string &Name, LocTy Loc,
302 unsigned Linkage, unsigned Visibility,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000303 unsigned DLLStorageClass, bool DSOLocal,
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000304 GlobalVariable::ThreadLocalMode TLM,
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000305 GlobalVariable::UnnamedAddr UnnamedAddr);
David Majnemerdad0a642014-06-27 18:19:56 +0000306 bool parseComdat();
Devang Patel39e64d42009-07-01 19:21:12 +0000307 bool ParseStandaloneMetadata();
Devang Patelbe626972009-07-29 00:34:02 +0000308 bool ParseNamedMetadata();
Chris Lattner1797fc72009-12-29 21:53:55 +0000309 bool ParseMDString(MDString *&Result);
Chris Lattner6dac02a2009-12-30 04:15:23 +0000310 bool ParseMDNodeID(MDNode *&Result);
Bill Wendling63b88192013-02-06 06:52:58 +0000311 bool ParseUnnamedAttrGrp();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000312 bool ParseFnAttributeValuePairs(AttrBuilder &B,
313 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000314 bool inAttrGrp, LocTy &BuiltinLoc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000315
Chris Lattnerac161bf2009-01-02 07:01:27 +0000316 // Type Parsing.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000317 bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
318 bool ParseType(Type *&Result, bool AllowVoid = false) {
319 return ParseType(Result, "expected type", AllowVoid);
320 }
321 bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc,
322 bool AllowVoid = false) {
323 Loc = Lex.getLoc();
324 return ParseType(Result, Msg, AllowVoid);
325 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000326 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000327 Loc = Lex.getLoc();
Chris Lattnerf880ca22009-03-09 04:49:14 +0000328 return ParseType(Result, AllowVoid);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000329 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000330 bool ParseAnonStructType(Type *&Result, bool Packed);
331 bool ParseStructBody(SmallVectorImpl<Type*> &Body);
332 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
333 std::pair<Type*, LocTy> &Entry,
334 Type *&ResultTy);
335
336 bool ParseArrayVectorType(Type *&Result, bool isVector);
337 bool ParseFunctionType(Type *&Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000338
Chris Lattnerac161bf2009-01-02 07:01:27 +0000339 // Function Semantic Analysis.
340 class PerFunctionState {
341 LLParser &P;
342 Function &F;
343 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
344 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
345 std::vector<Value*> NumberedVals;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000346
Chris Lattner3432c622009-10-28 03:39:23 +0000347 /// FunctionNumber - If this is an unnamed function, this is the slot
348 /// number of it, otherwise it is -1.
349 int FunctionNumber;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000350 public:
Chris Lattner3432c622009-10-28 03:39:23 +0000351 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000352 ~PerFunctionState();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000353
Chris Lattnerac161bf2009-01-02 07:01:27 +0000354 Function &getFunction() const { return F; }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000355
Chris Lattner3432c622009-10-28 03:39:23 +0000356 bool FinishFunction();
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000357
Chris Lattnerac161bf2009-01-02 07:01:27 +0000358 /// GetVal - Get a value with the specified name or ID, creating a
359 /// forward reference record if needed. This can return null if the value
360 /// exists but does not have the right type.
David Majnemer8a1c45d2015-12-12 05:38:55 +0000361 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
362 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000363
Chris Lattnerac161bf2009-01-02 07:01:27 +0000364 /// SetInstName - After an instruction is parsed and inserted into its
365 /// basic block, this installs its name.
366 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
367 Instruction *Inst);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000368
Chris Lattnerac161bf2009-01-02 07:01:27 +0000369 /// GetBB - Get a basic block with the specified name or ID, creating a
370 /// forward reference record if needed. This can return null if the value
371 /// is not a BasicBlock.
372 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
373 BasicBlock *GetBB(unsigned ID, LocTy Loc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000374
Chris Lattnerac161bf2009-01-02 07:01:27 +0000375 /// DefineBB - Define the specified basic block, which is either named or
376 /// unnamed. If there is an error, this returns null otherwise it returns
377 /// the block being defined.
378 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000379
380 bool resolveForwardRefBlockAddresses();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000381 };
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000382
Chris Lattner229907c2011-07-18 04:54:35 +0000383 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
David Majnemer8a1c45d2015-12-12 05:38:55 +0000384 PerFunctionState *PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000385
Alex Lorenzd2255952015-07-17 22:07:03 +0000386 bool parseConstantValue(Type *Ty, Constant *&C);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000387 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
388 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
389 return ParseValue(Ty, V, &PFS);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000390 }
David Majnemer8a1c45d2015-12-12 05:38:55 +0000391
Chris Lattner229907c2011-07-18 04:54:35 +0000392 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
Chris Lattnerac161bf2009-01-02 07:01:27 +0000393 PerFunctionState &PFS) {
394 Loc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000395 return ParseValue(Ty, V, &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000396 }
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000397
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000398 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
399 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
400 return ParseTypeAndValue(V, &PFS);
401 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000402 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
403 Loc = Lex.getLoc();
404 return ParseTypeAndValue(V, PFS);
405 }
Chris Lattner3ed871f2009-10-27 19:13:16 +0000406 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
407 PerFunctionState &PFS);
408 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
409 LocTy Loc;
410 return ParseTypeAndBasicBlock(BB, Loc, PFS);
411 }
Victor Hernandezfa232232009-12-03 23:40:58 +0000412
Chris Lattner392be582010-02-12 20:49:41 +0000413
Chris Lattnerac161bf2009-01-02 07:01:27 +0000414 struct ParamInfo {
415 LocTy Loc;
416 Value *V;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000417 AttributeSet Attrs;
418 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
Reid Klecknerb5180542017-03-21 16:57:19 +0000419 : Loc(loc), V(v), Attrs(attrs) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000420 };
421 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +0000422 PerFunctionState &PFS,
423 bool IsMustTailCall = false,
424 bool InVarArgsFunc = false);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000425
Sanjoy Dasb513a9f2015-09-24 23:34:52 +0000426 bool
427 ParseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> &BundleList,
428 PerFunctionState &PFS);
429
David Majnemer654e1302015-07-31 17:58:14 +0000430 bool ParseExceptionArgs(SmallVectorImpl<Value *> &Args,
431 PerFunctionState &PFS);
432
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000433 // Constant Parsing.
Craig Topperada08572014-04-16 04:21:27 +0000434 bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
Chris Lattner229907c2011-07-18 04:54:35 +0000435 bool ParseGlobalValue(Type *Ty, Constant *&V);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000436 bool ParseGlobalTypeAndValue(Constant *&V);
Peter Collingbourned93620b2016-11-10 22:34:55 +0000437 bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
438 Optional<unsigned> *InRangeOp = nullptr);
Rafael Espindola83a362c2015-01-06 22:55:16 +0000439 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000440 bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS);
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +0000441 bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
442 PerFunctionState *PFS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000443 bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS);
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +0000444 bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false);
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +0000445 bool ParseMDNode(MDNode *&MD);
446 bool ParseMDNodeTail(MDNode *&MD);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000447 bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs);
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +0000448 bool ParseMetadataAttachment(unsigned &Kind, MDNode *&MD);
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +0000449 bool ParseInstructionMetadata(Instruction &Inst);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000450 bool ParseGlobalObjectMetadataAttachment(GlobalObject &GO);
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000451 bool ParseOptionalFunctionMetadata(Function &F);
Victor Hernandezdc6e65a2010-01-05 22:22:14 +0000452
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +0000453 template <class FieldTy>
454 bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +0000455 template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result);
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000456 template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +0000457 bool ParseMDFieldsImplBody(ParserTy parseField);
458 template <class ParserTy>
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +0000459 bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000460 bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +0000461
462#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
463 bool Parse##CLASS(MDNode *&Result, bool IsDistinct);
464#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000465
Chris Lattnerac161bf2009-01-02 07:01:27 +0000466 // Function Parsing.
467 struct ArgInfo {
468 LocTy Loc;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000469 Type *Ty;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000470 AttributeSet Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000471 std::string Name;
Reid Klecknerc2cb5602017-04-12 00:38:00 +0000472 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
Reid Klecknerb5180542017-03-21 16:57:19 +0000473 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000474 };
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000475 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000476 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
477 bool ParseFunctionBody(Function &Fn);
478 bool ParseBasicBlock(PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000479
Reid Kleckner5772b772014-04-24 20:14:34 +0000480 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
481
Chris Lattner77b89dc2009-12-30 05:23:43 +0000482 // Instruction Parsing. Each instruction parsing routine can return with a
483 // normal result, an error result, or return having eaten an extra comma.
484 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
485 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
486 PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000487 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000488
Chris Lattner33de4272011-06-17 06:49:41 +0000489 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000490 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
491 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +0000492 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000493 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +0000494 bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
David Majnemer654e1302015-07-31 17:58:14 +0000495 bool ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS);
496 bool ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS);
David Majnemer8a1c45d2015-12-12 05:38:55 +0000497 bool ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS);
David Majnemer654e1302015-07-31 17:58:14 +0000498 bool ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS);
David Majnemer654e1302015-07-31 17:58:14 +0000499 bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
Misha Brukman1d9a93d2009-01-02 22:46:48 +0000500
Chris Lattnereeefa9a2009-01-05 08:24:46 +0000501 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
502 unsigned OperandType);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000503 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
504 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
505 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
506 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +0000507 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000508 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
509 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
510 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000511 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +0000512 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +0000513 bool ParseCall(Instruction *&I, PerFunctionState &PFS,
514 CallInst::TailCallKind IsTail);
Chris Lattner78103722011-06-17 03:16:47 +0000515 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +0000516 int ParseLoad(Instruction *&I, PerFunctionState &PFS);
517 int ParseStore(Instruction *&I, PerFunctionState &PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +0000518 int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
519 int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +0000520 int ParseFence(Instruction *&I, PerFunctionState &PFS);
Chris Lattnerf4f03422009-12-30 05:27:33 +0000521 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
522 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
523 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000524
525 // Use-list order directives.
526 bool ParseUseListOrder(PerFunctionState *PFS = nullptr);
527 bool ParseUseListOrderBB();
528 bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
529 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000530 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000531} // End llvm namespace
Chris Lattnerac161bf2009-01-02 07:01:27 +0000532
533#endif