blob: 29e6b07a76b0cb417e80b38822c886b02de1e66c [file] [log] [blame]
Chris Lattnerac161bf2009-01-02 07:01:27 +00001//===-- LLParser.cpp - Parser Class ---------------------------------------===//
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#include "LLParser.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/None.h"
17#include "llvm/ADT/Optional.h"
David Blaikieadbda4b2015-08-03 20:08:41 +000018#include "llvm/ADT/STLExtras.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "llvm/ADT/SmallPtrSet.h"
Alex Lorenz8955f7d2015-06-23 17:10:10 +000020#include "llvm/AsmParser/SlotMapping.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000021#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000022#include "llvm/IR/Argument.h"
Chandler Carruth91065212014-03-05 10:34:14 +000023#include "llvm/IR/AutoUpgrade.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000024#include "llvm/IR/BasicBlock.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/CallingConv.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000026#include "llvm/IR/Comdat.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000028#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/DerivedTypes.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000030#include "llvm/IR/Function.h"
31#include "llvm/IR/GlobalIFunc.h"
32#include "llvm/IR/GlobalObject.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/InlineAsm.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000034#include "llvm/IR/Instruction.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/Instructions.h"
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +000036#include "llvm/IR/Intrinsics.h"
Manman Ren209b17c2013-09-28 00:22:27 +000037#include "llvm/IR/LLVMContext.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000038#include "llvm/IR/Metadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000039#include "llvm/IR/Module.h"
40#include "llvm/IR/Operator.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000041#include "llvm/IR/Type.h"
42#include "llvm/IR/Value.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000043#include "llvm/IR/ValueSymbolTable.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000044#include "llvm/Support/Casting.h"
Torok Edwin56d06592009-07-11 20:10:48 +000045#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000046#include "llvm/Support/MathExtras.h"
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +000047#include "llvm/Support/SaveAndRestore.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000048#include "llvm/Support/raw_ostream.h"
Eugene Zelenko1804a772016-08-25 00:45:04 +000049#include <algorithm>
50#include <cassert>
51#include <cstring>
52#include <iterator>
53#include <vector>
54
Chris Lattnerac161bf2009-01-02 07:01:27 +000055using namespace llvm;
56
Chris Lattner229907c2011-07-18 04:54:35 +000057static std::string getTypeString(Type *T) {
Alp Tokere69170a2014-06-26 22:52:05 +000058 std::string Result;
59 raw_string_ostream Tmp(Result);
60 Tmp << *T;
61 return Tmp.str();
Chris Lattner0f214eb2011-06-18 21:18:23 +000062}
63
Chris Lattner3822f632009-01-02 08:05:26 +000064/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000065bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000066 // Prime the lexer.
67 Lex.Lex();
68
Mehdi Amini50af49f2016-04-02 03:46:17 +000069 if (Context.shouldDiscardValueNames())
Mehdi Amini09b4a8d2016-03-10 01:28:54 +000070 return Error(
71 Lex.getLoc(),
72 "Can't read textual IR with a Context that discards named Values");
73
Chris Lattnerad6f3352009-01-04 20:44:11 +000074 return ParseTopLevelEntities() ||
75 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000076}
77
Alex Lorenz1de2acd2015-08-21 21:32:39 +000078bool LLParser::parseStandaloneConstantValue(Constant *&C,
79 const SlotMapping *Slots) {
80 restoreParsingState(Slots);
Alex Lorenzd2255952015-07-17 22:07:03 +000081 Lex.Lex();
82
83 Type *Ty = nullptr;
84 if (ParseType(Ty) || parseConstantValue(Ty, C))
85 return true;
86 if (Lex.getKind() != lltok::Eof)
87 return Error(Lex.getLoc(), "expected end of string");
88 return false;
89}
90
Quentin Colombetdafed5d2016-03-08 00:37:07 +000091bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read,
92 const SlotMapping *Slots) {
Quentin Colombet81e72b42016-03-07 22:09:05 +000093 restoreParsingState(Slots);
94 Lex.Lex();
95
Quentin Colombetdafed5d2016-03-08 00:37:07 +000096 Read = 0;
97 SMLoc Start = Lex.getLoc();
Quentin Colombet81e72b42016-03-07 22:09:05 +000098 Ty = nullptr;
99 if (ParseType(Ty))
100 return true;
Quentin Colombetdafed5d2016-03-08 00:37:07 +0000101 SMLoc End = Lex.getLoc();
102 Read = End.getPointer() - Start.getPointer();
103
Quentin Colombet81e72b42016-03-07 22:09:05 +0000104 return false;
105}
106
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000107void LLParser::restoreParsingState(const SlotMapping *Slots) {
108 if (!Slots)
109 return;
110 NumberedVals = Slots->GlobalValues;
111 NumberedMetadata = Slots->MetadataNodes;
112 for (const auto &I : Slots->NamedTypes)
113 NamedTypes.insert(
114 std::make_pair(I.getKey(), std::make_pair(I.second, LocTy())));
115 for (const auto &I : Slots->Types)
116 NumberedTypes.insert(
117 std::make_pair(I.first, std::make_pair(I.second, LocTy())));
118}
119
Chris Lattnerac161bf2009-01-02 07:01:27 +0000120/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
121/// module.
122bool LLParser::ValidateEndOfModule() {
Bill Wendlingb32b0412013-02-08 06:32:06 +0000123 // Handle any function attribute group forward references.
Saleem Abdulrasool17995672016-12-27 18:35:22 +0000124 for (const auto &RAG : ForwardRefAttrGroups) {
125 Value *V = RAG.first;
126 const std::vector<unsigned> &Attrs = RAG.second;
Bill Wendlingb32b0412013-02-08 06:32:06 +0000127 AttrBuilder B;
128
Saleem Abdulrasool17995672016-12-27 18:35:22 +0000129 for (const auto &Attr : Attrs)
130 B.merge(NumberedAttrBuilders[Attr]);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000131
132 if (Function *Fn = dyn_cast<Function>(V)) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000133 AttributeList AS = Fn->getAttributes();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000134 AttrBuilder FnAttrs(AS.getFnAttributes());
135 AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000136
137 FnAttrs.merge(B);
138
139 // If the alignment was parsed as an attribute, move to the alignment
140 // field.
141 if (FnAttrs.hasAlignmentAttr()) {
142 Fn->setAlignment(FnAttrs.getAlignment());
143 FnAttrs.removeAttribute(Attribute::Alignment);
144 }
145
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000146 AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
147 AttributeSet::get(Context, FnAttrs));
Bill Wendlingb32b0412013-02-08 06:32:06 +0000148 Fn->setAttributes(AS);
149 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000150 AttributeList AS = CI->getAttributes();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000151 AttrBuilder FnAttrs(AS.getFnAttributes());
152 AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
Bill Wendling59dce372013-02-12 10:13:06 +0000153 FnAttrs.merge(B);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000154 AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
155 AttributeSet::get(Context, FnAttrs));
Bill Wendlingb32b0412013-02-08 06:32:06 +0000156 CI->setAttributes(AS);
157 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000158 AttributeList AS = II->getAttributes();
Reid Klecknereb9dd5b2017-04-10 23:31:05 +0000159 AttrBuilder FnAttrs(AS.getFnAttributes());
160 AS = AS.removeAttributes(Context, AttributeList::FunctionIndex);
Bill Wendling59dce372013-02-12 10:13:06 +0000161 FnAttrs.merge(B);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000162 AS = AS.addAttributes(Context, AttributeList::FunctionIndex,
163 AttributeSet::get(Context, FnAttrs));
Bill Wendlingb32b0412013-02-08 06:32:06 +0000164 II->setAttributes(AS);
Javed Absarf3d79042017-05-11 12:28:08 +0000165 } else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
166 AttrBuilder Attrs(GV->getAttributes());
167 Attrs.merge(B);
168 GV->setAttributes(AttributeSet::get(Context,Attrs));
Bill Wendlingb32b0412013-02-08 06:32:06 +0000169 } else {
170 llvm_unreachable("invalid object with forward attribute group reference");
171 }
172 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000173
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000174 // If there are entries in ForwardRefBlockAddresses at this point, the
175 // function was never defined.
176 if (!ForwardRefBlockAddresses.empty())
177 return Error(ForwardRefBlockAddresses.begin()->first.Loc,
178 "expected function name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000179
David Majnemer19b51052015-02-11 07:43:56 +0000180 for (const auto &NT : NumberedTypes)
181 if (NT.second.second.isValid())
182 return Error(NT.second.second,
183 "use of undefined type '%" + Twine(NT.first) + "'");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000184
185 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
186 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
187 if (I->second.second.isValid())
188 return Error(I->second.second,
189 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000190
David Majnemerdad0a642014-06-27 18:19:56 +0000191 if (!ForwardRefComdats.empty())
192 return Error(ForwardRefComdats.begin()->second,
193 "use of undefined comdat '$" +
194 ForwardRefComdats.begin()->first + "'");
195
Chris Lattnerac161bf2009-01-02 07:01:27 +0000196 if (!ForwardRefVals.empty())
197 return Error(ForwardRefVals.begin()->second.second,
198 "use of undefined value '@" + ForwardRefVals.begin()->first +
199 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000200
Chris Lattnerac161bf2009-01-02 07:01:27 +0000201 if (!ForwardRefValIDs.empty())
202 return Error(ForwardRefValIDs.begin()->second.second,
203 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000204 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000205
Devang Pateld2541152009-07-08 19:23:54 +0000206 if (!ForwardRefMDNodes.empty())
207 return Error(ForwardRefMDNodes.begin()->second.second,
208 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000209 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000210
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000211 // Resolve metadata cycles.
David Majnemer19b51052015-02-11 07:43:56 +0000212 for (auto &N : NumberedMetadata) {
213 if (N.second && !N.second->isResolved())
214 N.second->resolveCycles();
215 }
Devang Pateld2541152009-07-08 19:23:54 +0000216
Mehdi Aminie4709272016-09-14 22:29:59 +0000217 for (auto *Inst : InstsWithTBAATag) {
218 MDNode *MD = Inst->getMetadata(LLVMContext::MD_tbaa);
219 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
220 auto *UpgradedMD = UpgradeTBAANode(*MD);
221 if (MD != UpgradedMD)
222 Inst->setMetadata(LLVMContext::MD_tbaa, UpgradedMD);
223 }
Duncan P. N. Exon Smith29883862016-04-06 02:06:40 +0000224
Chris Lattnerac161bf2009-01-02 07:01:27 +0000225 // Look for intrinsic functions and CallInst that need to be upgraded
226 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
Duncan P. N. Exon Smithac331fb2015-10-20 01:12:49 +0000227 UpgradeCallsToIntrinsic(&*FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000228
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000229 // Some types could be renamed during loading if several modules are
230 // loaded in the same LLVMContext (LTO scenario). In this case we should
231 // remangle intrinsics names as well.
232 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) {
233 Function *F = &*FI++;
234 if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
235 F->replaceAllUsesWith(Remangled.getValue());
236 F->eraseFromParent();
237 }
238 }
239
Adrian Prantla8b2ddb2017-10-02 18:31:29 +0000240 if (UpgradeDebugInfo)
241 llvm::UpgradeDebugInfo(*M);
Manman Ren8b4306c2013-12-02 21:29:56 +0000242
Manman Renb5d7ff42016-05-25 23:14:48 +0000243 UpgradeModuleFlags(*M);
Saleem Abdulrasool46a59fd2017-10-06 18:06:59 +0000244 UpgradeSectionAttributes(*M);
Manman Renb5d7ff42016-05-25 23:14:48 +0000245
Alex Lorenz8955f7d2015-06-23 17:10:10 +0000246 if (!Slots)
247 return false;
248 // Initialize the slot mapping.
249 // Because by this point we've parsed and validated everything, we can "steal"
250 // the mapping from LLParser as it doesn't need it anymore.
251 Slots->GlobalValues = std::move(NumberedVals);
252 Slots->MetadataNodes = std::move(NumberedMetadata);
Alex Lorenz1de2acd2015-08-21 21:32:39 +0000253 for (const auto &I : NamedTypes)
254 Slots->NamedTypes.insert(std::make_pair(I.getKey(), I.second.first));
255 for (const auto &I : NumberedTypes)
256 Slots->Types.insert(std::make_pair(I.first, I.second.first));
Alex Lorenz8955f7d2015-06-23 17:10:10 +0000257
Chris Lattnerac161bf2009-01-02 07:01:27 +0000258 return false;
259}
260
261//===----------------------------------------------------------------------===//
262// Top-Level Entities
263//===----------------------------------------------------------------------===//
264
265bool LLParser::ParseTopLevelEntities() {
Eugene Zelenko1804a772016-08-25 00:45:04 +0000266 while (true) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000267 switch (Lex.getKind()) {
268 default: return TokError("expected top-level entity");
269 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000270 case lltok::kw_declare: if (ParseDeclare()) return true; break;
271 case lltok::kw_define: if (ParseDefine()) return true; break;
272 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
273 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Teresa Johnson83c517c2016-03-30 18:15:08 +0000274 case lltok::kw_source_filename:
275 if (ParseSourceFileName())
276 return true;
277 break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000278 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000279 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000280 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000281 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000282 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
David Majnemerdad0a642014-06-27 18:19:56 +0000283 case lltok::ComdatVar: if (parseComdat()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000284 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000285 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Bill Wendlinga7c38772013-02-09 15:48:49 +0000286 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000287 case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break;
288 case lltok::kw_uselistorder_bb:
Davide Italianof4e16612016-08-26 18:05:03 +0000289 if (ParseUseListOrderBB())
290 return true;
291 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000292 }
293 }
294}
295
Chris Lattnerac161bf2009-01-02 07:01:27 +0000296/// toplevelentity
297/// ::= 'module' 'asm' STRINGCONSTANT
298bool LLParser::ParseModuleAsm() {
299 assert(Lex.getKind() == lltok::kw_module);
300 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000301
302 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000303 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
304 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000305
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000306 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000307 return false;
308}
309
310/// toplevelentity
311/// ::= 'target' 'triple' '=' STRINGCONSTANT
312/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
313bool LLParser::ParseTargetDefinition() {
314 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000315 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000316 switch (Lex.Lex()) {
317 default: return TokError("unknown target property");
318 case lltok::kw_triple:
319 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000320 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
321 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000322 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000323 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000324 return false;
325 case lltok::kw_datalayout:
326 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000327 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
328 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000329 return true;
Yaxun Liuc00d81e2018-01-30 22:32:39 +0000330 if (DataLayoutStr.empty())
331 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000332 return false;
333 }
334}
335
Bill Wendling706d3d62012-11-28 08:41:48 +0000336/// toplevelentity
Teresa Johnson83c517c2016-03-30 18:15:08 +0000337/// ::= 'source_filename' '=' STRINGCONSTANT
338bool LLParser::ParseSourceFileName() {
339 assert(Lex.getKind() == lltok::kw_source_filename);
340 std::string Str;
341 Lex.Lex();
342 if (ParseToken(lltok::equal, "expected '=' after source_filename") ||
343 ParseStringConstant(Str))
344 return true;
345 M->setSourceFileName(Str);
346 return false;
347}
348
349/// toplevelentity
Bill Wendling706d3d62012-11-28 08:41:48 +0000350/// ::= 'deplibs' '=' '[' ']'
351/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
352/// FIXME: Remove in 4.0. Currently parse, but ignore.
353bool LLParser::ParseDepLibs() {
354 assert(Lex.getKind() == lltok::kw_deplibs);
355 Lex.Lex();
356 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
357 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
358 return true;
359
360 if (EatIfPresent(lltok::rsquare))
361 return false;
362
363 do {
364 std::string Str;
365 if (ParseStringConstant(Str)) return true;
366 } while (EatIfPresent(lltok::comma));
367
368 return ParseToken(lltok::rsquare, "expected ']' at end of list");
369}
370
Dan Gohman466876b2009-08-12 23:32:33 +0000371/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000372/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000373bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000374 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000375 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000376 Lex.Lex(); // eat LocalVarID;
377
378 if (ParseToken(lltok::equal, "expected '=' after name") ||
379 ParseToken(lltok::kw_type, "expected 'type' after '='"))
380 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000381
Craig Topper2617dcc2014-04-15 06:32:26 +0000382 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000383 if (ParseStructDefinition(TypeLoc, "",
384 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000385
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000386 if (!isa<StructType>(Result)) {
387 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
388 if (Entry.first)
389 return Error(TypeLoc, "non-struct types may not be recursive");
390 Entry.first = Result;
391 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000392 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000393
Chris Lattnerac161bf2009-01-02 07:01:27 +0000394 return false;
395}
396
397/// toplevelentity
398/// ::= LocalVar '=' 'type' type
399bool LLParser::ParseNamedType() {
400 std::string Name = Lex.getStrVal();
401 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000402 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000403
Chris Lattner3822f632009-01-02 08:05:26 +0000404 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000405 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000406 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000407
Craig Topper2617dcc2014-04-15 06:32:26 +0000408 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000409 if (ParseStructDefinition(NameLoc, Name,
410 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000411
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000412 if (!isa<StructType>(Result)) {
413 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
414 if (Entry.first)
415 return Error(NameLoc, "non-struct types may not be recursive");
416 Entry.first = Result;
417 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000418 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000419
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000420 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000421}
422
Chris Lattnerac161bf2009-01-02 07:01:27 +0000423/// toplevelentity
424/// ::= 'declare' FunctionHeader
425bool LLParser::ParseDeclare() {
426 assert(Lex.getKind() == lltok::kw_declare);
427 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000428
Peter Collingbourne21521892016-06-21 23:42:48 +0000429 std::vector<std::pair<unsigned, MDNode *>> MDs;
430 while (Lex.getKind() == lltok::MetadataVar) {
431 unsigned MDK;
432 MDNode *N;
433 if (ParseMetadataAttachment(MDK, N))
434 return true;
435 MDs.push_back({MDK, N});
436 }
437
Chris Lattnerac161bf2009-01-02 07:01:27 +0000438 Function *F;
Peter Collingbourne21521892016-06-21 23:42:48 +0000439 if (ParseFunctionHeader(F, false))
440 return true;
441 for (auto &MD : MDs)
442 F->addMetadata(MD.first, *MD.second);
443 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000444}
445
446/// toplevelentity
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000447/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
Chris Lattnerac161bf2009-01-02 07:01:27 +0000448bool LLParser::ParseDefine() {
449 assert(Lex.getKind() == lltok::kw_define);
450 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000451
Chris Lattnerac161bf2009-01-02 07:01:27 +0000452 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000453 return ParseFunctionHeader(F, true) ||
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000454 ParseOptionalFunctionMetadata(*F) ||
Chris Lattner3822f632009-01-02 08:05:26 +0000455 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000456}
457
Chris Lattner3822f632009-01-02 08:05:26 +0000458/// ParseGlobalType
459/// ::= 'constant'
460/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000461bool LLParser::ParseGlobalType(bool &IsConstant) {
462 if (Lex.getKind() == lltok::kw_constant)
463 IsConstant = true;
464 else if (Lex.getKind() == lltok::kw_global)
465 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000466 else {
467 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000468 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000469 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000470 Lex.Lex();
471 return false;
472}
473
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000474bool LLParser::ParseOptionalUnnamedAddr(
475 GlobalVariable::UnnamedAddr &UnnamedAddr) {
476 if (EatIfPresent(lltok::kw_unnamed_addr))
477 UnnamedAddr = GlobalValue::UnnamedAddr::Global;
478 else if (EatIfPresent(lltok::kw_local_unnamed_addr))
479 UnnamedAddr = GlobalValue::UnnamedAddr::Local;
480 else
481 UnnamedAddr = GlobalValue::UnnamedAddr::None;
482 return false;
483}
484
Dan Gohman466876b2009-08-12 23:32:33 +0000485/// ParseUnnamedGlobal:
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000486/// OptionalVisibility (ALIAS | IFUNC) ...
Sean Fertilec70d28b2017-10-26 15:00:26 +0000487/// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
488/// OptionalDLLStorageClass
Nico Rieck7157bb72014-01-14 15:22:47 +0000489/// ... -> global variable
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000490/// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
Sean Fertilec70d28b2017-10-26 15:00:26 +0000491/// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
492/// OptionalDLLStorageClass
Nico Rieck7157bb72014-01-14 15:22:47 +0000493/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000494bool LLParser::ParseUnnamedGlobal() {
495 unsigned VarID = NumberedVals.size();
496 std::string Name;
497 LocTy NameLoc = Lex.getLoc();
498
499 // Handle the GlobalID form.
500 if (Lex.getKind() == lltok::GlobalID) {
501 if (Lex.getUIntVal() != VarID)
502 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000503 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000504 Lex.Lex(); // eat GlobalID;
505
506 if (ParseToken(lltok::equal, "expected '=' after name"))
507 return true;
508 }
509
510 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000511 unsigned Linkage, Visibility, DLLStorageClass;
Sean Fertilec70d28b2017-10-26 15:00:26 +0000512 bool DSOLocal;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000513 GlobalVariable::ThreadLocalMode TLM;
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000514 GlobalVariable::UnnamedAddr UnnamedAddr;
Sean Fertilec70d28b2017-10-26 15:00:26 +0000515 if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
516 DSOLocal) ||
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000517 ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
Dan Gohman466876b2009-08-12 23:32:33 +0000518 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000519
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000520 if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
Nico Rieck7157bb72014-01-14 15:22:47 +0000521 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000522 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000523
524 return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000525 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
Dan Gohman466876b2009-08-12 23:32:33 +0000526}
527
Chris Lattnerac161bf2009-01-02 07:01:27 +0000528/// ParseNamedGlobal:
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000529/// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
Sean Fertilec70d28b2017-10-26 15:00:26 +0000530/// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
531/// OptionalVisibility OptionalDLLStorageClass
Nico Rieck7157bb72014-01-14 15:22:47 +0000532/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000533bool LLParser::ParseNamedGlobal() {
534 assert(Lex.getKind() == lltok::GlobalVar);
535 LocTy NameLoc = Lex.getLoc();
536 std::string Name = Lex.getStrVal();
537 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000538
Chris Lattnerac161bf2009-01-02 07:01:27 +0000539 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000540 unsigned Linkage, Visibility, DLLStorageClass;
Sean Fertilec70d28b2017-10-26 15:00:26 +0000541 bool DSOLocal;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000542 GlobalVariable::ThreadLocalMode TLM;
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000543 GlobalVariable::UnnamedAddr UnnamedAddr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000544 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
Sean Fertilec70d28b2017-10-26 15:00:26 +0000545 ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
546 DSOLocal) ||
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000547 ParseOptionalThreadLocal(TLM) || ParseOptionalUnnamedAddr(UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000548 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000549
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000550 if (Lex.getKind() != lltok::kw_alias && Lex.getKind() != lltok::kw_ifunc)
Nico Rieck7157bb72014-01-14 15:22:47 +0000551 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000552 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000553
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000554 return parseIndirectSymbol(Name, NameLoc, Linkage, Visibility,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000555 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000556}
557
David Majnemerdad0a642014-06-27 18:19:56 +0000558bool LLParser::parseComdat() {
559 assert(Lex.getKind() == lltok::ComdatVar);
560 std::string Name = Lex.getStrVal();
561 LocTy NameLoc = Lex.getLoc();
562 Lex.Lex();
563
564 if (ParseToken(lltok::equal, "expected '=' here"))
565 return true;
566
567 if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
568 return TokError("expected comdat type");
569
570 Comdat::SelectionKind SK;
571 switch (Lex.getKind()) {
572 default:
573 return TokError("unknown selection kind");
574 case lltok::kw_any:
575 SK = Comdat::Any;
576 break;
577 case lltok::kw_exactmatch:
578 SK = Comdat::ExactMatch;
579 break;
580 case lltok::kw_largest:
581 SK = Comdat::Largest;
582 break;
583 case lltok::kw_noduplicates:
584 SK = Comdat::NoDuplicates;
585 break;
586 case lltok::kw_samesize:
587 SK = Comdat::SameSize;
588 break;
589 }
590 Lex.Lex();
591
592 // See if the comdat was forward referenced, if so, use the comdat.
593 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
594 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
595 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
596 return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
597
598 Comdat *C;
599 if (I != ComdatSymTab.end())
600 C = &I->second;
601 else
602 C = M->getOrInsertComdat(Name);
603 C->setSelectionKind(SK);
604
605 return false;
606}
607
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000608// MDString:
609// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000610bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000611 std::string Str;
612 if (ParseStringConstant(Str)) return true;
Chris Lattner1797fc72009-12-29 21:53:55 +0000613 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000614 return false;
615}
616
617// MDNode:
618// ::= '!' MDNodeNumber
Chris Lattner6dac02a2009-12-30 04:15:23 +0000619bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000620 // !{ ..., !42, ... }
Duncan P. N. Exon Smith29883862016-04-06 02:06:40 +0000621 LocTy IDLoc = Lex.getLoc();
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000622 unsigned MID = 0;
Duncan P. N. Exon Smitha8d9a022015-01-12 21:14:38 +0000623 if (ParseUInt32(MID))
624 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000625
Chris Lattner8eff0152010-04-01 05:14:45 +0000626 // If not a forward reference, just return it now.
David Majnemer19b51052015-02-11 07:43:56 +0000627 if (NumberedMetadata.count(MID)) {
Duncan P. N. Exon Smitha8d9a022015-01-12 21:14:38 +0000628 Result = NumberedMetadata[MID];
629 return false;
630 }
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000631
Chris Lattner8eff0152010-04-01 05:14:45 +0000632 // Otherwise, create MDNode forward reference.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000633 auto &FwdRef = ForwardRefMDNodes[MID];
Duncan P. N. Exon Smith29883862016-04-06 02:06:40 +0000634 FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), IDLoc);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000635
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000636 Result = FwdRef.first.get();
637 NumberedMetadata[MID].reset(Result);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000638 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000639}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000640
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000641/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000642/// !foo = !{ !1, !2 }
643bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000644 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000645 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000646 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000647
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000648 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000649 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000650 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000651 return true;
652
Dan Gohman2637cc12010-07-21 23:38:33 +0000653 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000654 if (Lex.getKind() != lltok::rbrace)
655 do {
Craig Topper2617dcc2014-04-15 06:32:26 +0000656 MDNode *N = nullptr;
Reid Kleckner6d353342017-08-23 20:31:27 +0000657 // Parse DIExpressions inline as a special case. They are still MDNodes,
658 // so they can still appear in named metadata. Remove this logic if they
659 // become plain Metadata.
660 if (Lex.getKind() == lltok::MetadataVar &&
661 Lex.getStrVal() == "DIExpression") {
662 if (ParseDIExpression(N, /*IsDistinct=*/false))
663 return true;
664 } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
665 ParseMDNodeID(N)) {
666 return true;
667 }
Dan Gohman2637cc12010-07-21 23:38:33 +0000668 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000669 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000670
Rafael Espindolac7818fb2015-05-26 20:37:36 +0000671 return ParseToken(lltok::rbrace, "expected end of metadata node");
Devang Patelbe626972009-07-29 00:34:02 +0000672}
673
Devang Patel39e64d42009-07-01 19:21:12 +0000674/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000675/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000676bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000677 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000678 Lex.Lex();
679 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000680
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000681 MDNode *Init;
Chris Lattner278bc952009-12-29 22:40:21 +0000682 if (ParseUInt32(MetadataID) ||
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000683 ParseToken(lltok::equal, "expected '=' here"))
684 return true;
685
686 // Detect common error, from old metadata syntax.
687 if (Lex.getKind() == lltok::Type)
688 return TokError("unexpected type in metadata definition");
689
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +0000690 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000691 if (Lex.getKind() == lltok::MetadataVar) {
692 if (ParseSpecializedMDNode(Init, IsDistinct))
693 return true;
694 } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
695 ParseMDTuple(Init, IsDistinct))
Devang Patele059ba6e2009-07-23 01:07:34 +0000696 return true;
697
Chris Lattnerfc58af22009-12-30 04:51:58 +0000698 // See if this was forward referenced, if so, handle it.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000699 auto FI = ForwardRefMDNodes.find(MetadataID);
Devang Pateld2541152009-07-08 19:23:54 +0000700 if (FI != ForwardRefMDNodes.end()) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000701 FI->second.first->replaceAllUsesWith(Init);
Devang Pateld2541152009-07-08 19:23:54 +0000702 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000703
Chris Lattnerfc58af22009-12-30 04:51:58 +0000704 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
705 } else {
David Majnemer19b51052015-02-11 07:43:56 +0000706 if (NumberedMetadata.count(MetadataID))
Chris Lattnerfc58af22009-12-30 04:51:58 +0000707 return TokError("Metadata id is already used");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000708 NumberedMetadata[MetadataID].reset(Init);
Devang Pateld2541152009-07-08 19:23:54 +0000709 }
710
Devang Patel39e64d42009-07-01 19:21:12 +0000711 return false;
712}
713
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000714static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
715 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
716 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
717}
718
Rafael Espindolae4b02312018-01-11 22:15:05 +0000719// If there was an explicit dso_local, update GV. In the absence of an explicit
720// dso_local we keep the default value.
721static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
722 if (DSOLocal)
723 GV.setDSOLocal(true);
724}
725
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000726/// parseIndirectSymbol:
Sean Fertilec70d28b2017-10-26 15:00:26 +0000727/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
728/// OptionalVisibility OptionalDLLStorageClass
729/// OptionalThreadLocal OptionalUnnamedAddr
730// 'alias|ifunc' IndirectSymbol
Rafael Espindola6b238632014-05-16 19:35:39 +0000731///
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000732/// IndirectSymbol
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000733/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000734///
Eric Christopher536f0a92015-05-28 23:07:39 +0000735/// Everything through OptionalUnnamedAddr has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000736///
Sean Fertilec70d28b2017-10-26 15:00:26 +0000737bool LLParser::parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
738 unsigned L, unsigned Visibility,
739 unsigned DLLStorageClass, bool DSOLocal,
740 GlobalVariable::ThreadLocalMode TLM,
741 GlobalVariable::UnnamedAddr UnnamedAddr) {
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000742 bool IsAlias;
743 if (Lex.getKind() == lltok::kw_alias)
744 IsAlias = true;
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000745 else if (Lex.getKind() == lltok::kw_ifunc)
746 IsAlias = false;
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000747 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000748 llvm_unreachable("Not an alias or ifunc!");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000749 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000750
Rafael Espindola78527052013-10-06 15:10:43 +0000751 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
752
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000753 if(IsAlias && !GlobalAlias::isValidLinkage(Linkage))
Rafael Espindola464fe022014-07-30 22:51:54 +0000754 return Error(NameLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000755
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000756 if (!isValidVisibilityForLinkage(Visibility, L))
Rafael Espindola464fe022014-07-30 22:51:54 +0000757 return Error(NameLoc,
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000758 "symbol with local linkage must have default visibility");
759
David Blaikie2f408302015-09-11 03:22:04 +0000760 Type *Ty;
761 LocTy ExplicitTypeLoc = Lex.getLoc();
762 if (ParseType(Ty) ||
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000763 ParseToken(lltok::comma, "expected comma after alias or ifunc's type"))
David Blaikie2f408302015-09-11 03:22:04 +0000764 return true;
765
Rafael Espindola64c1e182014-06-03 02:41:57 +0000766 Constant *Aliasee;
767 LocTy AliaseeLoc = Lex.getLoc();
768 if (Lex.getKind() != lltok::kw_bitcast &&
769 Lex.getKind() != lltok::kw_getelementptr &&
770 Lex.getKind() != lltok::kw_addrspacecast &&
771 Lex.getKind() != lltok::kw_inttoptr) {
772 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000773 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000774 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000775 // The bitcast dest type is not present, it is implied by the dest type.
776 ValID ID;
777 if (ParseValID(ID))
778 return true;
779 if (ID.Kind != ValID::t_Constant)
780 return Error(AliaseeLoc, "invalid aliasee");
781 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000782 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000783
Rafael Espindola64c1e182014-06-03 02:41:57 +0000784 Type *AliaseeType = Aliasee->getType();
785 auto *PTy = dyn_cast<PointerType>(AliaseeType);
786 if (!PTy)
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000787 return Error(AliaseeLoc, "An alias or ifunc must have pointer type");
David Blaikie16a2f3e2015-09-14 18:01:59 +0000788 unsigned AddrSpace = PTy->getAddressSpace();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000789
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000790 if (IsAlias && Ty != PTy->getElementType())
David Blaikie2f408302015-09-11 03:22:04 +0000791 return Error(
792 ExplicitTypeLoc,
793 "explicit pointee type doesn't match operand's pointee type");
794
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000795 if (!IsAlias && !PTy->getElementType()->isFunctionTy())
796 return Error(
797 ExplicitTypeLoc,
798 "explicit pointee type should be a function type");
799
Peter Collingbourne463ff6d2015-11-25 02:54:07 +0000800 GlobalValue *GVal = nullptr;
801
802 // See if the alias was forward referenced, if so, prepare to replace the
803 // forward reference.
804 if (!Name.empty()) {
805 GVal = M->getNamedValue(Name);
806 if (GVal) {
807 if (!ForwardRefVals.erase(Name))
808 return Error(NameLoc, "redefinition of global '@" + Name + "'");
809 }
810 } else {
811 auto I = ForwardRefValIDs.find(NumberedVals.size());
812 if (I != ForwardRefValIDs.end()) {
813 GVal = I->second.first;
814 ForwardRefValIDs.erase(I);
815 }
816 }
817
Chris Lattnerac161bf2009-01-02 07:01:27 +0000818 // Okay, create the alias but do not insert it into the module yet.
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000819 std::unique_ptr<GlobalIndirectSymbol> GA;
820 if (IsAlias)
821 GA.reset(GlobalAlias::create(Ty, AddrSpace,
822 (GlobalValue::LinkageTypes)Linkage, Name,
823 Aliasee, /*Parent*/ nullptr));
824 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000825 GA.reset(GlobalIFunc::create(Ty, AddrSpace,
826 (GlobalValue::LinkageTypes)Linkage, Name,
827 Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000828 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000829 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000830 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000831 GA->setUnnamedAddr(UnnamedAddr);
Rafael Espindolae4b02312018-01-11 22:15:05 +0000832 maybeSetDSOLocal(DSOLocal, *GA);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000833
Rafael Espindola54fc2982015-06-17 17:53:31 +0000834 if (Name.empty())
835 NumberedVals.push_back(GA.get());
836
Peter Collingbourne463ff6d2015-11-25 02:54:07 +0000837 if (GVal) {
838 // Verify that types agree.
839 if (GVal->getType() != GA->getType())
840 return Error(
841 ExplicitTypeLoc,
842 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000843
Chris Lattnerac161bf2009-01-02 07:01:27 +0000844 // If they agree, just RAUW the old value with the alias and remove the
845 // forward ref info.
Peter Collingbourne463ff6d2015-11-25 02:54:07 +0000846 GVal->replaceAllUsesWith(GA.get());
847 GVal->eraseFromParent();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000848 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000849
Chris Lattnerac161bf2009-01-02 07:01:27 +0000850 // Insert into the module, we know its name won't collide now.
Dmitry Polukhina3d5b0b2016-04-05 08:47:51 +0000851 if (IsAlias)
852 M->getAliasList().push_back(cast<GlobalAlias>(GA.get()));
853 else
Dmitry Polukhina1feff72016-04-07 12:32:19 +0000854 M->getIFuncList().push_back(cast<GlobalIFunc>(GA.get()));
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000855 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000856
Rafael Espindolaaa273822014-05-09 21:49:17 +0000857 // The module owns this now
858 GA.release();
859
Chris Lattnerac161bf2009-01-02 07:01:27 +0000860 return false;
861}
862
863/// ParseGlobal
Sean Fertilec70d28b2017-10-26 15:00:26 +0000864/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
865/// OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000866/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
Javed Absarf3d79042017-05-11 12:28:08 +0000867/// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
Sean Fertilec70d28b2017-10-26 15:00:26 +0000868/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
869/// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
870/// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
871/// Const OptionalAttrs
Chris Lattnerac161bf2009-01-02 07:01:27 +0000872///
Eric Christopher536f0a92015-05-28 23:07:39 +0000873/// Everything up to and including OptionalUnnamedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000874/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000875///
876bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
877 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000878 unsigned Visibility, unsigned DLLStorageClass,
Sean Fertilec70d28b2017-10-26 15:00:26 +0000879 bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000880 GlobalVariable::UnnamedAddr UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000881 if (!isValidVisibilityForLinkage(Visibility, Linkage))
882 return Error(NameLoc,
883 "symbol with local linkage must have default visibility");
884
Chris Lattnerac161bf2009-01-02 07:01:27 +0000885 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000886 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000887 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000888 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000889
Craig Topper2617dcc2014-04-15 06:32:26 +0000890 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000891 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000892 ParseOptionalToken(lltok::kw_externally_initialized,
893 IsExternallyInitialized,
894 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000895 ParseGlobalType(IsConstant) ||
896 ParseType(Ty, TyLoc))
897 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000898
Chris Lattnerac161bf2009-01-02 07:01:27 +0000899 // If the linkage is specified and is external, then no initializer is
900 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000901 Constant *Init = nullptr;
Rafael Espindola4787ba32016-05-11 13:51:39 +0000902 if (!HasLinkage ||
903 !GlobalValue::isValidDeclarationLinkage(
904 (GlobalValue::LinkageTypes)Linkage)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000905 if (ParseGlobalValue(Ty, Init))
906 return true;
907 }
908
David Majnemer49b3d9b2015-02-16 08:41:08 +0000909 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000910 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000911
David Majnemer598bd052014-12-09 05:56:09 +0000912 GlobalValue *GVal = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000913
914 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000915 if (!Name.empty()) {
David Majnemer598bd052014-12-09 05:56:09 +0000916 GVal = M->getNamedValue(Name);
917 if (GVal) {
Peter Collingbourne463ff6d2015-11-25 02:54:07 +0000918 if (!ForwardRefVals.erase(Name))
Chris Lattnere38317f2009-10-25 23:22:50 +0000919 return Error(NameLoc, "redefinition of global '@" + Name + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +0000920 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000921 } else {
David Blaikie9ebdc692015-09-21 21:07:50 +0000922 auto I = ForwardRefValIDs.find(NumberedVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000923 if (I != ForwardRefValIDs.end()) {
David Majnemer598bd052014-12-09 05:56:09 +0000924 GVal = I->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000925 ForwardRefValIDs.erase(I);
926 }
927 }
928
David Majnemer598bd052014-12-09 05:56:09 +0000929 GlobalVariable *GV;
930 if (!GVal) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000931 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
932 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000933 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000934 } else {
David Blaikie8f27ae42015-05-13 22:55:01 +0000935 if (GVal->getValueType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000936 return Error(TyLoc,
937 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000938
David Majnemer598bd052014-12-09 05:56:09 +0000939 GV = cast<GlobalVariable>(GVal);
940
Chris Lattnerac161bf2009-01-02 07:01:27 +0000941 // Move the forward-reference to the correct spot in the module.
942 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
943 }
944
945 if (Name.empty())
946 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000947
Chris Lattnerac161bf2009-01-02 07:01:27 +0000948 // Set the parsed properties on the global.
949 if (Init)
950 GV->setInitializer(Init);
951 GV->setConstant(IsConstant);
952 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
Rafael Espindolae4b02312018-01-11 22:15:05 +0000953 maybeSetDSOLocal(DSOLocal, *GV);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000954 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000955 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000956 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000957 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000958 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000959
Chris Lattnerac161bf2009-01-02 07:01:27 +0000960 // Parse attributes on the global.
961 while (Lex.getKind() == lltok::comma) {
962 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000963
Chris Lattnerac161bf2009-01-02 07:01:27 +0000964 if (Lex.getKind() == lltok::kw_section) {
965 Lex.Lex();
966 GV->setSection(Lex.getStrVal());
967 if (ParseToken(lltok::StringConstant, "expected global section string"))
968 return true;
969 } else if (Lex.getKind() == lltok::kw_align) {
970 unsigned Alignment;
971 if (ParseOptionalAlignment(Alignment)) return true;
972 GV->setAlignment(Alignment);
Peter Collingbournecceae7f2016-05-31 23:01:54 +0000973 } else if (Lex.getKind() == lltok::MetadataVar) {
974 if (ParseGlobalObjectMetadataAttachment(*GV))
975 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000976 } else {
David Majnemerdad0a642014-06-27 18:19:56 +0000977 Comdat *C;
Rafael Espindola83a362c2015-01-06 22:55:16 +0000978 if (parseOptionalComdat(Name, C))
David Majnemerdad0a642014-06-27 18:19:56 +0000979 return true;
980 if (C)
981 GV->setComdat(C);
982 else
983 return TokError("unknown global variable property!");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000984 }
985 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000986
Javed Absarf3d79042017-05-11 12:28:08 +0000987 AttrBuilder Attrs;
988 LocTy BuiltinLoc;
989 std::vector<unsigned> FwdRefAttrGrps;
990 if (ParseFnAttributeValuePairs(Attrs, FwdRefAttrGrps, false, BuiltinLoc))
991 return true;
992 if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
993 GV->setAttributes(AttributeSet::get(Context, Attrs));
994 ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
995 }
996
Chris Lattnerac161bf2009-01-02 07:01:27 +0000997 return false;
998}
999
Bill Wendling63b88192013-02-06 06:52:58 +00001000/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +00001001/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +00001002bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +00001003 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +00001004 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +00001005 Lex.Lex();
1006
David Majnemerb39e22b2014-12-09 18:33:57 +00001007 if (Lex.getKind() != lltok::AttrGrpID)
1008 return TokError("expected attribute group id");
1009
Bill Wendling63b88192013-02-06 06:52:58 +00001010 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +00001011 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +00001012 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +00001013 Lex.Lex();
1014
1015 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +00001016 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00001017 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +00001018 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +00001019 ParseToken(lltok::rbrace, "expected end of attribute group"))
1020 return true;
1021
Bill Wendlingb32b0412013-02-08 06:32:06 +00001022 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +00001023 return Error(AttrGrpLoc, "attribute group has no attributes");
1024
1025 return false;
1026}
1027
Bill Wendling8b0321d2013-02-08 00:52:31 +00001028/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +00001029/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +00001030bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
1031 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +00001032 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +00001033 bool HaveError = false;
1034
1035 B.clear();
1036
Bill Wendling63b88192013-02-06 06:52:58 +00001037 while (true) {
1038 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +00001039 if (Token == lltok::kw_builtin)
1040 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +00001041 switch (Token) {
1042 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001043 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +00001044 return Error(Lex.getLoc(), "unterminated attribute group");
1045 case lltok::rbrace:
1046 // Finished.
1047 return false;
1048
Bill Wendlingb32b0412013-02-08 06:32:06 +00001049 case lltok::AttrGrpID: {
1050 // Allow a function to reference an attribute group:
1051 //
1052 // define void @foo() #1 { ... }
1053 if (inAttrGrp)
1054 HaveError |=
1055 Error(Lex.getLoc(),
1056 "cannot have an attribute group reference in an attribute group");
1057
1058 unsigned AttrGrpNum = Lex.getUIntVal();
1059 if (inAttrGrp) break;
1060
1061 // Save the reference to the attribute group. We'll fill it in later.
1062 FwdRefAttrGrps.push_back(AttrGrpNum);
1063 break;
1064 }
Bill Wendling63b88192013-02-06 06:52:58 +00001065 // Target-dependent attributes:
1066 case lltok::StringConstant: {
Artur Pilipenko17376c42015-08-03 14:31:49 +00001067 if (ParseStringAttribute(B))
Bill Wendling63b88192013-02-06 06:52:58 +00001068 return true;
Bill Wendlingb1ea9802013-02-10 10:12:50 +00001069 continue;
Bill Wendling63b88192013-02-06 06:52:58 +00001070 }
1071
1072 // Target-independent attributes:
1073 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +00001074 // As a hack, we allow function alignment to be initially parsed as an
1075 // attribute on a function declaration/definition or added to an attribute
1076 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +00001077 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +00001078 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +00001079 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +00001080 if (ParseToken(lltok::equal, "expected '=' here") ||
1081 ParseUInt32(Alignment))
1082 return true;
1083 } else {
1084 if (ParseOptionalAlignment(Alignment))
1085 return true;
1086 }
Bill Wendling63b88192013-02-06 06:52:58 +00001087 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +00001088 continue;
Bill Wendling63b88192013-02-06 06:52:58 +00001089 }
1090 case lltok::kw_alignstack: {
1091 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +00001092 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +00001093 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +00001094 if (ParseToken(lltok::equal, "expected '=' here") ||
1095 ParseUInt32(Alignment))
1096 return true;
1097 } else {
1098 if (ParseOptionalStackAlignment(Alignment))
1099 return true;
1100 }
Bill Wendling63b88192013-02-06 06:52:58 +00001101 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +00001102 continue;
Bill Wendling63b88192013-02-06 06:52:58 +00001103 }
George Burgess IV278199f2016-04-12 01:05:35 +00001104 case lltok::kw_allocsize: {
1105 unsigned ElemSizeArg;
1106 Optional<unsigned> NumElemsArg;
1107 // inAttrGrp doesn't matter; we only support allocsize(a[, b])
1108 if (parseAllocSizeArguments(ElemSizeArg, NumElemsArg))
1109 return true;
1110 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1111 continue;
1112 }
Igor Laevsky39d662f2015-07-11 10:30:36 +00001113 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
1114 case lltok::kw_argmemonly: B.addAttribute(Attribute::ArgMemOnly); break;
1115 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
1116 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
1117 case lltok::kw_convergent: B.addAttribute(Attribute::Convergent); break;
Vaivaswatha Nagarajfb3f4902015-12-16 16:16:19 +00001118 case lltok::kw_inaccessiblememonly:
1119 B.addAttribute(Attribute::InaccessibleMemOnly); break;
1120 case lltok::kw_inaccessiblemem_or_argmemonly:
1121 B.addAttribute(Attribute::InaccessibleMemOrArgMemOnly); break;
Igor Laevsky39d662f2015-07-11 10:30:36 +00001122 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
1123 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
1124 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
1125 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
1126 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
1127 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
1128 case lltok::kw_noimplicitfloat:
1129 B.addAttribute(Attribute::NoImplicitFloat); break;
1130 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
1131 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
1132 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
1133 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
James Molloye6f87ca2015-11-06 10:32:53 +00001134 case lltok::kw_norecurse: B.addAttribute(Attribute::NoRecurse); break;
Igor Laevsky39d662f2015-07-11 10:30:36 +00001135 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
1136 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
1137 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
1138 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1139 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
1140 case lltok::kw_returns_twice:
1141 B.addAttribute(Attribute::ReturnsTwice); break;
Matt Arsenaultb19b57e2017-04-28 20:25:27 +00001142 case lltok::kw_speculatable: B.addAttribute(Attribute::Speculatable); break;
Igor Laevsky39d662f2015-07-11 10:30:36 +00001143 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
1144 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
1145 case lltok::kw_sspstrong:
1146 B.addAttribute(Attribute::StackProtectStrong); break;
1147 case lltok::kw_safestack: B.addAttribute(Attribute::SafeStack); break;
1148 case lltok::kw_sanitize_address:
1149 B.addAttribute(Attribute::SanitizeAddress); break;
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +00001150 case lltok::kw_sanitize_hwaddress:
1151 B.addAttribute(Attribute::SanitizeHWAddress); break;
Igor Laevsky39d662f2015-07-11 10:30:36 +00001152 case lltok::kw_sanitize_thread:
1153 B.addAttribute(Attribute::SanitizeThread); break;
1154 case lltok::kw_sanitize_memory:
1155 B.addAttribute(Attribute::SanitizeMemory); break;
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +00001156 case lltok::kw_strictfp: B.addAttribute(Attribute::StrictFP); break;
Igor Laevsky39d662f2015-07-11 10:30:36 +00001157 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +00001158 case lltok::kw_writeonly: B.addAttribute(Attribute::WriteOnly); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +00001159
1160 // Error handling.
1161 case lltok::kw_inreg:
1162 case lltok::kw_signext:
1163 case lltok::kw_zeroext:
1164 HaveError |=
1165 Error(Lex.getLoc(),
1166 "invalid use of attribute on a function");
1167 break;
1168 case lltok::kw_byval:
Hal Finkelb0407ba2014-07-18 15:51:28 +00001169 case lltok::kw_dereferenceable:
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001170 case lltok::kw_dereferenceable_or_null:
Reid Klecknera534a382013-12-19 02:14:12 +00001171 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001172 case lltok::kw_nest:
1173 case lltok::kw_noalias:
1174 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001175 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +00001176 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001177 case lltok::kw_sret:
Manman Ren9bfd0d02016-04-01 21:41:15 +00001178 case lltok::kw_swifterror:
Manman Renf46262e2016-03-29 17:37:21 +00001179 case lltok::kw_swiftself:
Bill Wendling8b0321d2013-02-08 00:52:31 +00001180 HaveError |=
1181 Error(Lex.getLoc(),
1182 "invalid use of parameter-only attribute on a function");
1183 break;
Bill Wendling63b88192013-02-06 06:52:58 +00001184 }
1185
1186 Lex.Lex();
1187 }
1188}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001189
1190//===----------------------------------------------------------------------===//
1191// GlobalValue Reference/Resolution Routines.
1192//===----------------------------------------------------------------------===//
1193
Karl Schimpf77729782015-09-03 18:06:44 +00001194static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy,
1195 const std::string &Name) {
1196 if (auto *FT = dyn_cast<FunctionType>(PTy->getElementType()))
1197 return Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
1198 else
1199 return new GlobalVariable(*M, PTy->getElementType(), false,
1200 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1201 nullptr, GlobalVariable::NotThreadLocal,
1202 PTy->getAddressSpace());
1203}
1204
Chris Lattnerac161bf2009-01-02 07:01:27 +00001205/// GetGlobalVal - Get a value with the specified name or ID, creating a
1206/// forward reference record if needed. This can return null if the value
1207/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001208GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001209 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001210 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001211 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001212 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001213 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001214 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001215
Chris Lattnerac161bf2009-01-02 07:01:27 +00001216 // Look this name up in the normal function symbol table.
1217 GlobalValue *Val =
1218 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001219
Chris Lattnerac161bf2009-01-02 07:01:27 +00001220 // If this is a forward reference for the value, see if we already created a
1221 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001222 if (!Val) {
David Blaikie9ebdc692015-09-21 21:07:50 +00001223 auto I = ForwardRefVals.find(Name);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001224 if (I != ForwardRefVals.end())
1225 Val = I->second.first;
1226 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001227
Chris Lattnerac161bf2009-01-02 07:01:27 +00001228 // If we have the value in the symbol table or fwd-ref table, return it.
1229 if (Val) {
1230 if (Val->getType() == Ty) return Val;
1231 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001232 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001233 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001234 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001235
Chris Lattnerac161bf2009-01-02 07:01:27 +00001236 // Otherwise, create a new forward reference for this value and remember it.
Karl Schimpf77729782015-09-03 18:06:44 +00001237 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, Name);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001238 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1239 return FwdVal;
1240}
1241
Chris Lattner229907c2011-07-18 04:54:35 +00001242GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1243 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001244 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001245 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001246 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001247 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001248
Craig Topper2617dcc2014-04-15 06:32:26 +00001249 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001250
Chris Lattnerac161bf2009-01-02 07:01:27 +00001251 // If this is a forward reference for the value, see if we already created a
1252 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001253 if (!Val) {
David Blaikie9ebdc692015-09-21 21:07:50 +00001254 auto I = ForwardRefValIDs.find(ID);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001255 if (I != ForwardRefValIDs.end())
1256 Val = I->second.first;
1257 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001258
Chris Lattnerac161bf2009-01-02 07:01:27 +00001259 // If we have the value in the symbol table or fwd-ref table, return it.
1260 if (Val) {
1261 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001262 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001263 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001264 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001265 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001266
Chris Lattnerac161bf2009-01-02 07:01:27 +00001267 // Otherwise, create a new forward reference for this value and remember it.
Karl Schimpf77729782015-09-03 18:06:44 +00001268 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy, "");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001269 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1270 return FwdVal;
1271}
1272
Chris Lattnerac161bf2009-01-02 07:01:27 +00001273//===----------------------------------------------------------------------===//
David Majnemerdad0a642014-06-27 18:19:56 +00001274// Comdat Reference/Resolution Routines.
1275//===----------------------------------------------------------------------===//
1276
1277Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1278 // Look this name up in the comdat symbol table.
1279 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1280 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1281 if (I != ComdatSymTab.end())
1282 return &I->second;
1283
1284 // Otherwise, create a new forward reference for this value and remember it.
1285 Comdat *C = M->getOrInsertComdat(Name);
1286 ForwardRefComdats[Name] = Loc;
1287 return C;
1288}
1289
David Majnemerdad0a642014-06-27 18:19:56 +00001290//===----------------------------------------------------------------------===//
Chris Lattnerac161bf2009-01-02 07:01:27 +00001291// Helper Routines.
1292//===----------------------------------------------------------------------===//
1293
1294/// ParseToken - If the current token has the specified kind, eat it and return
1295/// success. Otherwise, emit the specified error and return failure.
1296bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1297 if (Lex.getKind() != T)
1298 return TokError(ErrMsg);
1299 Lex.Lex();
1300 return false;
1301}
1302
Chris Lattner3822f632009-01-02 08:05:26 +00001303/// ParseStringConstant
1304/// ::= StringConstant
1305bool LLParser::ParseStringConstant(std::string &Result) {
1306 if (Lex.getKind() != lltok::StringConstant)
1307 return TokError("expected string constant");
1308 Result = Lex.getStrVal();
1309 Lex.Lex();
1310 return false;
1311}
1312
1313/// ParseUInt32
1314/// ::= uint32
Leny Kholodov5fcc4182016-09-06 10:46:28 +00001315bool LLParser::ParseUInt32(uint32_t &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001316 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1317 return TokError("expected integer");
1318 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1319 if (Val64 != unsigned(Val64))
1320 return TokError("expected 32-bit integer (too large)");
1321 Val = Val64;
1322 Lex.Lex();
1323 return false;
1324}
1325
Hal Finkelb0407ba2014-07-18 15:51:28 +00001326/// ParseUInt64
1327/// ::= uint64
1328bool LLParser::ParseUInt64(uint64_t &Val) {
1329 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1330 return TokError("expected integer");
1331 Val = Lex.getAPSIntVal().getLimitedValue();
1332 Lex.Lex();
1333 return false;
1334}
1335
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001336/// ParseTLSModel
1337/// := 'localdynamic'
1338/// := 'initialexec'
1339/// := 'localexec'
1340bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1341 switch (Lex.getKind()) {
1342 default:
1343 return TokError("expected localdynamic, initialexec or localexec");
1344 case lltok::kw_localdynamic:
1345 TLM = GlobalVariable::LocalDynamicTLSModel;
1346 break;
1347 case lltok::kw_initialexec:
1348 TLM = GlobalVariable::InitialExecTLSModel;
1349 break;
1350 case lltok::kw_localexec:
1351 TLM = GlobalVariable::LocalExecTLSModel;
1352 break;
1353 }
1354
1355 Lex.Lex();
1356 return false;
1357}
1358
1359/// ParseOptionalThreadLocal
1360/// := /*empty*/
1361/// := 'thread_local'
1362/// := 'thread_local' '(' tlsmodel ')'
1363bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1364 TLM = GlobalVariable::NotThreadLocal;
1365 if (!EatIfPresent(lltok::kw_thread_local))
1366 return false;
1367
1368 TLM = GlobalVariable::GeneralDynamicTLSModel;
1369 if (Lex.getKind() == lltok::lparen) {
1370 Lex.Lex();
1371 return ParseTLSModel(TLM) ||
1372 ParseToken(lltok::rparen, "expected ')' after thread local model");
1373 }
1374 return false;
1375}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001376
1377/// ParseOptionalAddrSpace
1378/// := /*empty*/
1379/// := 'addrspace' '(' uint32 ')'
1380bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1381 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001382 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001383 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001384 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001385 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001386 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001387}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001388
Artur Pilipenko17376c42015-08-03 14:31:49 +00001389/// ParseStringAttribute
1390/// := StringConstant
1391/// := StringConstant '=' StringConstant
1392bool LLParser::ParseStringAttribute(AttrBuilder &B) {
1393 std::string Attr = Lex.getStrVal();
1394 Lex.Lex();
1395 std::string Val;
1396 if (EatIfPresent(lltok::equal) && ParseStringConstant(Val))
1397 return true;
1398 B.addAttribute(Attr, Val);
1399 return false;
1400}
1401
Bill Wendling34c2eb22012-12-04 23:40:58 +00001402/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1403bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1404 bool HaveError = false;
1405
1406 B.clear();
1407
Eugene Zelenko1804a772016-08-25 00:45:04 +00001408 while (true) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001409 lltok::Kind Token = Lex.getKind();
1410 switch (Token) {
1411 default: // End of attributes.
1412 return HaveError;
Artur Pilipenko17376c42015-08-03 14:31:49 +00001413 case lltok::StringConstant: {
1414 if (ParseStringAttribute(B))
1415 return true;
1416 continue;
1417 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001418 case lltok::kw_align: {
1419 unsigned Alignment;
1420 if (ParseOptionalAlignment(Alignment))
1421 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001422 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001423 continue;
1424 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001425 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001426 case lltok::kw_dereferenceable: {
1427 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001428 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001429 return true;
1430 B.addDereferenceableAttr(Bytes);
1431 continue;
1432 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001433 case lltok::kw_dereferenceable_or_null: {
1434 uint64_t Bytes;
1435 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1436 return true;
1437 B.addDereferenceableOrNullAttr(Bytes);
1438 continue;
1439 }
Reid Klecknera534a382013-12-19 02:14:12 +00001440 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001441 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1442 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1443 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1444 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001445 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001446 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1447 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001448 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001449 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1450 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
Manman Ren9bfd0d02016-04-01 21:41:15 +00001451 case lltok::kw_swifterror: B.addAttribute(Attribute::SwiftError); break;
Manman Renf46262e2016-03-29 17:37:21 +00001452 case lltok::kw_swiftself: B.addAttribute(Attribute::SwiftSelf); break;
Nicolai Haehnle84c9f992016-07-04 08:01:29 +00001453 case lltok::kw_writeonly: B.addAttribute(Attribute::WriteOnly); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001454 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001455
Stephen Lin7577ed52013-04-20 13:16:13 +00001456 case lltok::kw_alignstack:
1457 case lltok::kw_alwaysinline:
Igor Laevsky39d662f2015-07-11 10:30:36 +00001458 case lltok::kw_argmemonly:
Michael Gottesman41748d72013-06-27 00:25:01 +00001459 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001460 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001461 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001462 case lltok::kw_minsize:
1463 case lltok::kw_naked:
1464 case lltok::kw_nobuiltin:
1465 case lltok::kw_noduplicate:
1466 case lltok::kw_noimplicitfloat:
1467 case lltok::kw_noinline:
1468 case lltok::kw_nonlazybind:
1469 case lltok::kw_noredzone:
1470 case lltok::kw_noreturn:
1471 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001472 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001473 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001474 case lltok::kw_returns_twice:
1475 case lltok::kw_sanitize_address:
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +00001476 case lltok::kw_sanitize_hwaddress:
Stephen Lin7577ed52013-04-20 13:16:13 +00001477 case lltok::kw_sanitize_memory:
1478 case lltok::kw_sanitize_thread:
1479 case lltok::kw_ssp:
1480 case lltok::kw_sspreq:
1481 case lltok::kw_sspstrong:
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001482 case lltok::kw_safestack:
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +00001483 case lltok::kw_strictfp:
Stephen Lin7577ed52013-04-20 13:16:13 +00001484 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001485 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1486 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001487 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001488
Bill Wendling34c2eb22012-12-04 23:40:58 +00001489 Lex.Lex();
1490 }
1491}
1492
1493/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1494bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1495 bool HaveError = false;
1496
1497 B.clear();
1498
Eugene Zelenko1804a772016-08-25 00:45:04 +00001499 while (true) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001500 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001501 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001502 default: // End of attributes.
1503 return HaveError;
Artur Pilipenko17376c42015-08-03 14:31:49 +00001504 case lltok::StringConstant: {
1505 if (ParseStringAttribute(B))
1506 return true;
1507 continue;
1508 }
Hal Finkelb0407ba2014-07-18 15:51:28 +00001509 case lltok::kw_dereferenceable: {
1510 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001511 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001512 return true;
1513 B.addDereferenceableAttr(Bytes);
1514 continue;
1515 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001516 case lltok::kw_dereferenceable_or_null: {
1517 uint64_t Bytes;
1518 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1519 return true;
1520 B.addDereferenceableOrNullAttr(Bytes);
1521 continue;
1522 }
Artur Pilipenko84bc62f2015-09-18 12:33:31 +00001523 case lltok::kw_align: {
1524 unsigned Alignment;
1525 if (ParseOptionalAlignment(Alignment))
1526 return true;
1527 B.addAlignmentAttr(Alignment);
1528 continue;
1529 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001530 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1531 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001532 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001533 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1534 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001535
Bill Wendling34c2eb22012-12-04 23:40:58 +00001536 // Error handling.
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001537 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001538 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001539 case lltok::kw_nest:
1540 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001541 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001542 case lltok::kw_sret:
Manman Ren9bfd0d02016-04-01 21:41:15 +00001543 case lltok::kw_swifterror:
Manman Renf46262e2016-03-29 17:37:21 +00001544 case lltok::kw_swiftself:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001545 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001546 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001547
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001548 case lltok::kw_alignstack:
1549 case lltok::kw_alwaysinline:
Igor Laevsky39d662f2015-07-11 10:30:36 +00001550 case lltok::kw_argmemonly:
Michael Gottesman41748d72013-06-27 00:25:01 +00001551 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001552 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001553 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001554 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001555 case lltok::kw_minsize:
1556 case lltok::kw_naked:
1557 case lltok::kw_nobuiltin:
1558 case lltok::kw_noduplicate:
1559 case lltok::kw_noimplicitfloat:
1560 case lltok::kw_noinline:
1561 case lltok::kw_nonlazybind:
1562 case lltok::kw_noredzone:
1563 case lltok::kw_noreturn:
1564 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001565 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001566 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001567 case lltok::kw_returns_twice:
1568 case lltok::kw_sanitize_address:
Evgeniy Stepanovc667c1f2017-12-09 00:21:41 +00001569 case lltok::kw_sanitize_hwaddress:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001570 case lltok::kw_sanitize_memory:
1571 case lltok::kw_sanitize_thread:
1572 case lltok::kw_ssp:
1573 case lltok::kw_sspreq:
1574 case lltok::kw_sspstrong:
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001575 case lltok::kw_safestack:
Andrew Kaylor53a5fbb2017-08-14 21:15:13 +00001576 case lltok::kw_strictfp:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001577 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001578 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001579 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001580
1581 case lltok::kw_readnone:
1582 case lltok::kw_readonly:
1583 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001584 }
1585
Chris Lattnerac161bf2009-01-02 07:01:27 +00001586 Lex.Lex();
1587 }
1588}
1589
Rafael Espindolac6269912016-05-10 17:16:45 +00001590static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
1591 HasLinkage = true;
1592 switch (Kind) {
1593 default:
1594 HasLinkage = false;
1595 return GlobalValue::ExternalLinkage;
1596 case lltok::kw_private:
1597 return GlobalValue::PrivateLinkage;
1598 case lltok::kw_internal:
1599 return GlobalValue::InternalLinkage;
1600 case lltok::kw_weak:
1601 return GlobalValue::WeakAnyLinkage;
1602 case lltok::kw_weak_odr:
1603 return GlobalValue::WeakODRLinkage;
1604 case lltok::kw_linkonce:
1605 return GlobalValue::LinkOnceAnyLinkage;
1606 case lltok::kw_linkonce_odr:
1607 return GlobalValue::LinkOnceODRLinkage;
1608 case lltok::kw_available_externally:
1609 return GlobalValue::AvailableExternallyLinkage;
1610 case lltok::kw_appending:
1611 return GlobalValue::AppendingLinkage;
1612 case lltok::kw_common:
1613 return GlobalValue::CommonLinkage;
1614 case lltok::kw_extern_weak:
1615 return GlobalValue::ExternalWeakLinkage;
1616 case lltok::kw_external:
1617 return GlobalValue::ExternalLinkage;
1618 }
1619}
1620
Chris Lattnerac161bf2009-01-02 07:01:27 +00001621/// ParseOptionalLinkage
1622/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001623/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001624/// ::= 'internal'
1625/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001626/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001627/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001628/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001629/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001630/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001631/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001632/// ::= 'extern_weak'
1633/// ::= 'external'
Rafael Espindola2615c9e2016-05-12 12:37:52 +00001634bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage,
1635 unsigned &Visibility,
Sean Fertilec70d28b2017-10-26 15:00:26 +00001636 unsigned &DLLStorageClass,
1637 bool &DSOLocal) {
Rafael Espindolac6269912016-05-10 17:16:45 +00001638 Res = parseOptionalLinkageAux(Lex.getKind(), HasLinkage);
1639 if (HasLinkage)
1640 Lex.Lex();
Sean Fertilec70d28b2017-10-26 15:00:26 +00001641 ParseOptionalDSOLocal(DSOLocal);
Rafael Espindola2615c9e2016-05-12 12:37:52 +00001642 ParseOptionalVisibility(Visibility);
1643 ParseOptionalDLLStorageClass(DLLStorageClass);
Sean Fertilec70d28b2017-10-26 15:00:26 +00001644
1645 if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
1646 return Error(Lex.getLoc(), "dso_location and DLL-StorageClass mismatch");
1647 }
1648
Chris Lattnerac161bf2009-01-02 07:01:27 +00001649 return false;
1650}
1651
Sean Fertilec70d28b2017-10-26 15:00:26 +00001652void LLParser::ParseOptionalDSOLocal(bool &DSOLocal) {
1653 switch (Lex.getKind()) {
1654 default:
1655 DSOLocal = false;
1656 break;
1657 case lltok::kw_dso_local:
1658 DSOLocal = true;
1659 Lex.Lex();
1660 break;
1661 case lltok::kw_dso_preemptable:
1662 DSOLocal = false;
1663 Lex.Lex();
1664 break;
1665 }
1666}
1667
Chris Lattnerac161bf2009-01-02 07:01:27 +00001668/// ParseOptionalVisibility
1669/// ::= /*empty*/
1670/// ::= 'default'
1671/// ::= 'hidden'
1672/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001673///
Rafael Espindola2615c9e2016-05-12 12:37:52 +00001674void LLParser::ParseOptionalVisibility(unsigned &Res) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001675 switch (Lex.getKind()) {
Rafael Espindola2615c9e2016-05-12 12:37:52 +00001676 default:
1677 Res = GlobalValue::DefaultVisibility;
1678 return;
1679 case lltok::kw_default:
1680 Res = GlobalValue::DefaultVisibility;
1681 break;
1682 case lltok::kw_hidden:
1683 Res = GlobalValue::HiddenVisibility;
1684 break;
1685 case lltok::kw_protected:
1686 Res = GlobalValue::ProtectedVisibility;
1687 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001688 }
1689 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001690}
1691
Nico Rieck7157bb72014-01-14 15:22:47 +00001692/// ParseOptionalDLLStorageClass
1693/// ::= /*empty*/
1694/// ::= 'dllimport'
1695/// ::= 'dllexport'
1696///
Rafael Espindola2615c9e2016-05-12 12:37:52 +00001697void LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
Nico Rieck7157bb72014-01-14 15:22:47 +00001698 switch (Lex.getKind()) {
Rafael Espindola2615c9e2016-05-12 12:37:52 +00001699 default:
1700 Res = GlobalValue::DefaultStorageClass;
1701 return;
1702 case lltok::kw_dllimport:
1703 Res = GlobalValue::DLLImportStorageClass;
1704 break;
1705 case lltok::kw_dllexport:
1706 Res = GlobalValue::DLLExportStorageClass;
1707 break;
Nico Rieck7157bb72014-01-14 15:22:47 +00001708 }
1709 Lex.Lex();
Nico Rieck7157bb72014-01-14 15:22:47 +00001710}
1711
Chris Lattnerac161bf2009-01-02 07:01:27 +00001712/// ParseOptionalCallingConv
1713/// ::= /*empty*/
1714/// ::= 'ccc'
1715/// ::= 'fastcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001716/// ::= 'intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001717/// ::= 'coldcc'
1718/// ::= 'x86_stdcallcc'
1719/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001720/// ::= 'x86_thiscallcc'
Reid Kleckner9ccce992014-10-28 01:29:26 +00001721/// ::= 'x86_vectorcallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001722/// ::= 'arm_apcscc'
1723/// ::= 'arm_aapcscc'
1724/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001725/// ::= 'msp430_intrcc'
Dylan McKay4fd0d4a2016-03-03 10:08:02 +00001726/// ::= 'avr_intrcc'
1727/// ::= 'avr_signalcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001728/// ::= 'ptx_kernel'
1729/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001730/// ::= 'spir_func'
1731/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001732/// ::= 'x86_64_sysvcc'
Martin Storsjo2f24e932017-07-17 20:05:19 +00001733/// ::= 'win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001734/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001735/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001736/// ::= 'preserve_mostcc'
1737/// ::= 'preserve_allcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001738/// ::= 'ghccc'
Manman Renf8bdd882016-04-05 22:41:47 +00001739/// ::= 'swiftcc'
Amjad Aboud60b5e1b2015-12-21 14:07:14 +00001740/// ::= 'x86_intrcc'
Maksim Panchenkocce239c2015-09-29 22:09:16 +00001741/// ::= 'hhvmcc'
1742/// ::= 'hhvm_ccc'
Manman Ren19c7bbe2015-12-04 17:40:13 +00001743/// ::= 'cxx_fast_tlscc'
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001744/// ::= 'amdgpu_vs'
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001745/// ::= 'amdgpu_ls'
Marek Olsaka302a7362017-05-02 15:41:10 +00001746/// ::= 'amdgpu_hs'
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001747/// ::= 'amdgpu_es'
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001748/// ::= 'amdgpu_gs'
1749/// ::= 'amdgpu_ps'
1750/// ::= 'amdgpu_cs'
Nikolay Haustov1f7732a2016-05-06 09:07:29 +00001751/// ::= 'amdgpu_kernel'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001752/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001753///
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001754bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001755 switch (Lex.getKind()) {
1756 default: CC = CallingConv::C; return false;
1757 case lltok::kw_ccc: CC = CallingConv::C; break;
1758 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1759 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1760 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1761 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Oren Ben Simhon92ccbf22016-10-13 07:53:43 +00001762 case lltok::kw_x86_regcallcc: CC = CallingConv::X86_RegCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001763 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +00001764 case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001765 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1766 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1767 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001768 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Dylan McKay4fd0d4a2016-03-03 10:08:02 +00001769 case lltok::kw_avr_intrcc: CC = CallingConv::AVR_INTR; break;
1770 case lltok::kw_avr_signalcc: CC = CallingConv::AVR_SIGNAL; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001771 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1772 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001773 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1774 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001775 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001776 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
Martin Storsjo2f24e932017-07-17 20:05:19 +00001777 case lltok::kw_win64cc: CC = CallingConv::Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001778 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001779 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001780 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1781 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Reid Kleckner35fc3632014-12-01 21:04:44 +00001782 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
Manman Renf8bdd882016-04-05 22:41:47 +00001783 case lltok::kw_swiftcc: CC = CallingConv::Swift; break;
Amjad Aboud60b5e1b2015-12-21 14:07:14 +00001784 case lltok::kw_x86_intrcc: CC = CallingConv::X86_INTR; break;
Maksim Panchenkocce239c2015-09-29 22:09:16 +00001785 case lltok::kw_hhvmcc: CC = CallingConv::HHVM; break;
1786 case lltok::kw_hhvm_ccc: CC = CallingConv::HHVM_C; break;
Manman Ren19c7bbe2015-12-04 17:40:13 +00001787 case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001788 case lltok::kw_amdgpu_vs: CC = CallingConv::AMDGPU_VS; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001789 case lltok::kw_amdgpu_ls: CC = CallingConv::AMDGPU_LS; break;
Marek Olsaka302a7362017-05-02 15:41:10 +00001790 case lltok::kw_amdgpu_hs: CC = CallingConv::AMDGPU_HS; break;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001791 case lltok::kw_amdgpu_es: CC = CallingConv::AMDGPU_ES; break;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001792 case lltok::kw_amdgpu_gs: CC = CallingConv::AMDGPU_GS; break;
1793 case lltok::kw_amdgpu_ps: CC = CallingConv::AMDGPU_PS; break;
1794 case lltok::kw_amdgpu_cs: CC = CallingConv::AMDGPU_CS; break;
Nikolay Haustov1f7732a2016-05-06 09:07:29 +00001795 case lltok::kw_amdgpu_kernel: CC = CallingConv::AMDGPU_KERNEL; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001796 case lltok::kw_cc: {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001797 Lex.Lex();
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001798 return ParseUInt32(CC);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001799 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001800 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001801
Chris Lattnerac161bf2009-01-02 07:01:27 +00001802 Lex.Lex();
1803 return false;
1804}
1805
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001806/// ParseMetadataAttachment
1807/// ::= !dbg !42
1808bool LLParser::ParseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
1809 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
1810
1811 std::string Name = Lex.getStrVal();
1812 Kind = M->getMDKindID(Name);
1813 Lex.Lex();
1814
1815 return ParseMDNode(MD);
1816}
1817
Chris Lattner5c427632009-12-30 05:31:19 +00001818/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001819/// ::= !dbg !42 (',' !dbg !57)*
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001820bool LLParser::ParseInstructionMetadata(Instruction &Inst) {
Chris Lattner5c427632009-12-30 05:31:19 +00001821 do {
1822 if (Lex.getKind() != lltok::MetadataVar)
1823 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001824
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001825 unsigned MDK;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00001826 MDNode *N;
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001827 if (ParseMetadataAttachment(MDK, N))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001828 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001829
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001830 Inst.setMetadata(MDK, N);
Manman Ren209b17c2013-09-28 00:22:27 +00001831 if (MDK == LLVMContext::MD_tbaa)
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001832 InstsWithTBAATag.push_back(&Inst);
Manman Ren209b17c2013-09-28 00:22:27 +00001833
Chris Lattner596760d2009-12-29 21:25:40 +00001834 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001835 } while (EatIfPresent(lltok::comma));
1836 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001837}
1838
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001839/// ParseGlobalObjectMetadataAttachment
1840/// ::= !dbg !57
1841bool LLParser::ParseGlobalObjectMetadataAttachment(GlobalObject &GO) {
1842 unsigned MDK;
1843 MDNode *N;
1844 if (ParseMetadataAttachment(MDK, N))
1845 return true;
1846
Peter Collingbourne382d81c2016-06-01 01:17:57 +00001847 GO.addMetadata(MDK, *N);
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001848 return false;
1849}
1850
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001851/// ParseOptionalFunctionMetadata
1852/// ::= (!dbg !57)*
1853bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
Peter Collingbournecceae7f2016-05-31 23:01:54 +00001854 while (Lex.getKind() == lltok::MetadataVar)
1855 if (ParseGlobalObjectMetadataAttachment(F))
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001856 return true;
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001857 return false;
1858}
1859
Chris Lattnerac161bf2009-01-02 07:01:27 +00001860/// ParseOptionalAlignment
1861/// ::= /* empty */
1862/// ::= 'align' 4
1863bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1864 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001865 if (!EatIfPresent(lltok::kw_align))
1866 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001867 LocTy AlignLoc = Lex.getLoc();
1868 if (ParseUInt32(Alignment)) return true;
1869 if (!isPowerOf2_32(Alignment))
1870 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001871 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001872 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001873 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001874}
1875
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001876/// ParseOptionalDerefAttrBytes
Hal Finkelb0407ba2014-07-18 15:51:28 +00001877/// ::= /* empty */
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001878/// ::= AttrKind '(' 4 ')'
1879///
1880/// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
1881bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
1882 uint64_t &Bytes) {
1883 assert((AttrKind == lltok::kw_dereferenceable ||
1884 AttrKind == lltok::kw_dereferenceable_or_null) &&
1885 "contract!");
1886
Hal Finkelb0407ba2014-07-18 15:51:28 +00001887 Bytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001888 if (!EatIfPresent(AttrKind))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001889 return false;
1890 LocTy ParenLoc = Lex.getLoc();
1891 if (!EatIfPresent(lltok::lparen))
1892 return Error(ParenLoc, "expected '('");
1893 LocTy DerefLoc = Lex.getLoc();
1894 if (ParseUInt64(Bytes)) return true;
1895 ParenLoc = Lex.getLoc();
1896 if (!EatIfPresent(lltok::rparen))
1897 return Error(ParenLoc, "expected ')'");
1898 if (!Bytes)
1899 return Error(DerefLoc, "dereferenceable bytes must be non-zero");
1900 return false;
1901}
1902
Chris Lattnerb2f39502009-12-30 05:44:30 +00001903/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001904/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001905/// ::= ',' align 4
1906///
1907/// This returns with AteExtraComma set to true if it ate an excess comma at the
1908/// end.
1909bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1910 bool &AteExtraComma) {
1911 AteExtraComma = false;
1912 while (EatIfPresent(lltok::comma)) {
1913 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001914 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001915 AteExtraComma = true;
1916 return false;
1917 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001918
Chris Lattner95b0ff42010-04-23 00:50:50 +00001919 if (Lex.getKind() != lltok::kw_align)
1920 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001921
Chris Lattner95b0ff42010-04-23 00:50:50 +00001922 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001923 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001924
Devang Patelea8a4b92009-09-17 23:04:48 +00001925 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001926}
1927
Matt Arsenault3c1fc762017-04-10 22:27:50 +00001928/// ParseOptionalCommaAddrSpace
1929/// ::=
1930/// ::= ',' addrspace(1)
1931///
1932/// This returns with AteExtraComma set to true if it ate an excess comma at the
1933/// end.
1934bool LLParser::ParseOptionalCommaAddrSpace(unsigned &AddrSpace,
1935 LocTy &Loc,
1936 bool &AteExtraComma) {
1937 AteExtraComma = false;
1938 while (EatIfPresent(lltok::comma)) {
1939 // Metadata at the end is an early exit.
1940 if (Lex.getKind() == lltok::MetadataVar) {
1941 AteExtraComma = true;
1942 return false;
1943 }
1944
1945 Loc = Lex.getLoc();
1946 if (Lex.getKind() != lltok::kw_addrspace)
1947 return Error(Lex.getLoc(), "expected metadata or 'addrspace'");
1948
1949 if (ParseOptionalAddrSpace(AddrSpace))
1950 return true;
1951 }
1952
1953 return false;
1954}
1955
George Burgess IV278199f2016-04-12 01:05:35 +00001956bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
1957 Optional<unsigned> &HowManyArg) {
1958 Lex.Lex();
1959
1960 auto StartParen = Lex.getLoc();
1961 if (!EatIfPresent(lltok::lparen))
1962 return Error(StartParen, "expected '('");
1963
1964 if (ParseUInt32(BaseSizeArg))
1965 return true;
1966
1967 if (EatIfPresent(lltok::comma)) {
1968 auto HowManyAt = Lex.getLoc();
1969 unsigned HowMany;
1970 if (ParseUInt32(HowMany))
1971 return true;
1972 if (HowMany == BaseSizeArg)
1973 return Error(HowManyAt,
1974 "'allocsize' indices can't refer to the same parameter");
1975 HowManyArg = HowMany;
1976 } else
1977 HowManyArg = None;
1978
1979 auto EndParen = Lex.getLoc();
1980 if (!EatIfPresent(lltok::rparen))
1981 return Error(EndParen, "expected ')'");
1982 return false;
1983}
1984
Eli Friedmanfee02c62011-07-25 23:16:38 +00001985/// ParseScopeAndOrdering
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001986/// if isAtomic: ::= SyncScope? AtomicOrdering
Eli Friedmanfee02c62011-07-25 23:16:38 +00001987/// else: ::=
1988///
1989/// This sets Scope and Ordering to the parsed values.
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001990bool LLParser::ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID,
Eli Friedmanfee02c62011-07-25 23:16:38 +00001991 AtomicOrdering &Ordering) {
1992 if (!isAtomic)
1993 return false;
1994
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001995 return ParseScope(SSID) || ParseOrdering(Ordering);
1996}
Tim Northovere94a5182014-03-11 10:48:52 +00001997
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00001998/// ParseScope
1999/// ::= syncscope("singlethread" | "<target scope>")?
2000///
2001/// This sets synchronization scope ID to the ID of the parsed value.
2002bool LLParser::ParseScope(SyncScope::ID &SSID) {
2003 SSID = SyncScope::System;
2004 if (EatIfPresent(lltok::kw_syncscope)) {
2005 auto StartParenAt = Lex.getLoc();
2006 if (!EatIfPresent(lltok::lparen))
2007 return Error(StartParenAt, "Expected '(' in syncscope");
2008
2009 std::string SSN;
2010 auto SSNAt = Lex.getLoc();
2011 if (ParseStringConstant(SSN))
2012 return Error(SSNAt, "Expected synchronization scope name");
2013
2014 auto EndParenAt = Lex.getLoc();
2015 if (!EatIfPresent(lltok::rparen))
2016 return Error(EndParenAt, "Expected ')' in syncscope");
2017
2018 SSID = Context.getOrInsertSyncScopeID(SSN);
2019 }
2020
2021 return false;
Tim Northovere94a5182014-03-11 10:48:52 +00002022}
2023
2024/// ParseOrdering
2025/// ::= AtomicOrdering
2026///
2027/// This sets Ordering to the parsed value.
2028bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00002029 switch (Lex.getKind()) {
2030 default: return TokError("Expected ordering on atomic instruction");
JF Bastien800f87a2016-04-06 21:19:33 +00002031 case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break;
2032 case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break;
2033 // Not specified yet:
2034 // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
2035 case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break;
2036 case lltok::kw_release: Ordering = AtomicOrdering::Release; break;
2037 case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break;
2038 case lltok::kw_seq_cst:
2039 Ordering = AtomicOrdering::SequentiallyConsistent;
2040 break;
Eli Friedmanfee02c62011-07-25 23:16:38 +00002041 }
2042 Lex.Lex();
2043 return false;
2044}
2045
Charles Davisbe5557e2010-02-12 00:31:15 +00002046/// ParseOptionalStackAlignment
2047/// ::= /* empty */
2048/// ::= 'alignstack' '(' 4 ')'
2049bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
2050 Alignment = 0;
2051 if (!EatIfPresent(lltok::kw_alignstack))
2052 return false;
2053 LocTy ParenLoc = Lex.getLoc();
2054 if (!EatIfPresent(lltok::lparen))
2055 return Error(ParenLoc, "expected '('");
2056 LocTy AlignLoc = Lex.getLoc();
2057 if (ParseUInt32(Alignment)) return true;
2058 ParenLoc = Lex.getLoc();
2059 if (!EatIfPresent(lltok::rparen))
2060 return Error(ParenLoc, "expected ')'");
2061 if (!isPowerOf2_32(Alignment))
2062 return Error(AlignLoc, "stack alignment is not a power of two");
2063 return false;
2064}
Devang Patelea8a4b92009-09-17 23:04:48 +00002065
Chris Lattner28f1eeb2009-12-30 05:14:00 +00002066/// ParseIndexList - This parses the index list for an insert/extractvalue
2067/// instruction. This sets AteExtraComma in the case where we eat an extra
2068/// comma at the end of the line and find that it is followed by metadata.
2069/// Clients that don't allow metadata can call the version of this function that
2070/// only takes one argument.
2071///
Chris Lattnerac161bf2009-01-02 07:01:27 +00002072/// ParseIndexList
2073/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00002074///
2075bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
2076 bool &AteExtraComma) {
2077 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002078
Chris Lattnerac161bf2009-01-02 07:01:27 +00002079 if (Lex.getKind() != lltok::comma)
2080 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002081
Chris Lattner3822f632009-01-02 08:05:26 +00002082 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00002083 if (Lex.getKind() == lltok::MetadataVar) {
David Majnemer7ccc34d2015-02-16 09:18:13 +00002084 if (Indices.empty()) return TokError("expected index");
Chris Lattner28f1eeb2009-12-30 05:14:00 +00002085 AteExtraComma = true;
2086 return false;
2087 }
Nick Lewycky83e47112010-09-29 23:32:20 +00002088 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00002089 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002090 Indices.push_back(Idx);
2091 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002092
Chris Lattnerac161bf2009-01-02 07:01:27 +00002093 return false;
2094}
2095
2096//===----------------------------------------------------------------------===//
2097// Type Parsing.
2098//===----------------------------------------------------------------------===//
2099
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002100/// ParseType - Parse a type.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00002101bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002102 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002103 switch (Lex.getKind()) {
2104 default:
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00002105 return TokError(Msg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002106 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002107 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002108 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002109 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002110 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002111 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002112 // Type ::= StructType
2113 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002114 return true;
2115 break;
2116 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002117 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002118 Lex.Lex(); // eat the lsquare.
2119 if (ParseArrayVectorType(Result, false))
2120 return true;
2121 break;
2122 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002123 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00002124 Lex.Lex();
2125 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002126 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002127 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002128 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002129 } else if (ParseArrayVectorType(Result, true))
2130 return true;
2131 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002132 case lltok::LocalVar: {
2133 // Type ::= %foo
2134 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002135
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002136 // If the type hasn't been defined yet, create a forward definition and
2137 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00002138 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00002139 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002140 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002141 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002142 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002143 Lex.Lex();
2144 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002145 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002146
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002147 case lltok::LocalVarID: {
2148 // Type ::= %4
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002149 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002150
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002151 // If the type hasn't been defined yet, create a forward definition and
2152 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00002153 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00002154 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002155 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002156 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002157 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002158 Lex.Lex();
2159 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002160 }
2161 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002162
2163 // Parse the type suffixes.
Eugene Zelenko1804a772016-08-25 00:45:04 +00002164 while (true) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002165 switch (Lex.getKind()) {
2166 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002167 default:
2168 if (!AllowVoid && Result->isVoidTy())
2169 return Error(TypeLoc, "void type only allowed for function results");
2170 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002171
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002172 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002173 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002174 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002175 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002176 if (Result->isVoidTy())
2177 return TokError("pointers to void are invalid - use i8* instead");
2178 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002179 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002180 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002181 Lex.Lex();
2182 break;
2183
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002184 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002185 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002186 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002187 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002188 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00002189 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002190 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002191 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002192 unsigned AddrSpace;
2193 if (ParseOptionalAddrSpace(AddrSpace) ||
2194 ParseToken(lltok::star, "expected '*' in address space"))
2195 return true;
2196
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002197 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002198 break;
2199 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002200
Chris Lattnerac161bf2009-01-02 07:01:27 +00002201 /// Types '(' ArgTypeListI ')' OptFuncAttrs
2202 case lltok::lparen:
2203 if (ParseFunctionType(Result))
2204 return true;
2205 break;
2206 }
2207 }
2208}
2209
2210/// ParseParameterList
2211/// ::= '(' ')'
2212/// ::= '(' Arg (',' Arg)* ')'
2213/// Arg
2214/// ::= Type OptionalAttributes Value OptionalAttributes
2215bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +00002216 PerFunctionState &PFS, bool IsMustTailCall,
2217 bool InVarArgsFunc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002218 if (ParseToken(lltok::lparen, "expected '(' in call"))
2219 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002220
Chris Lattnerac161bf2009-01-02 07:01:27 +00002221 while (Lex.getKind() != lltok::rparen) {
2222 // If this isn't the first argument, we need a comma.
2223 if (!ArgList.empty() &&
2224 ParseToken(lltok::comma, "expected ',' in argument list"))
2225 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002226
Reid Kleckner83498642014-08-26 00:33:28 +00002227 // Parse an ellipsis if this is a musttail call in a variadic function.
2228 if (Lex.getKind() == lltok::dotdotdot) {
2229 const char *Msg = "unexpected ellipsis in argument list for ";
2230 if (!IsMustTailCall)
2231 return TokError(Twine(Msg) + "non-musttail call");
2232 if (!InVarArgsFunc)
2233 return TokError(Twine(Msg) + "musttail call in non-varargs function");
2234 Lex.Lex(); // Lex the '...', it is purely for readability.
2235 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
2236 }
2237
Chris Lattnerac161bf2009-01-02 07:01:27 +00002238 // Parse the argument.
2239 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00002240 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00002241 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002242 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00002243 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002244 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00002245
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00002246 if (ArgTy->isMetadataTy()) {
2247 if (ParseMetadataAsValue(V, PFS))
2248 return true;
2249 } else {
2250 // Otherwise, handle normal operands.
2251 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
2252 return true;
2253 }
Reid Klecknerb5180542017-03-21 16:57:19 +00002254 ArgList.push_back(ParamInfo(
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002255 ArgLoc, V, AttributeSet::get(V->getContext(), ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002256 }
2257
Reid Kleckner83498642014-08-26 00:33:28 +00002258 if (IsMustTailCall && InVarArgsFunc)
2259 return TokError("expected '...' at end of argument list for musttail call "
2260 "in varargs function");
2261
Chris Lattnerac161bf2009-01-02 07:01:27 +00002262 Lex.Lex(); // Lex the ')'.
2263 return false;
2264}
2265
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002266/// ParseOptionalOperandBundles
2267/// ::= /*empty*/
2268/// ::= '[' OperandBundle [, OperandBundle ]* ']'
2269///
2270/// OperandBundle
2271/// ::= bundle-tag '(' ')'
2272/// ::= bundle-tag '(' Type Value [, Type Value ]* ')'
2273///
2274/// bundle-tag ::= String Constant
2275bool LLParser::ParseOptionalOperandBundles(
2276 SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
2277 LocTy BeginLoc = Lex.getLoc();
2278 if (!EatIfPresent(lltok::lsquare))
2279 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002280
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002281 while (Lex.getKind() != lltok::rsquare) {
2282 // If this isn't the first operand bundle, we need a comma.
2283 if (!BundleList.empty() &&
2284 ParseToken(lltok::comma, "expected ',' in input list"))
2285 return true;
2286
2287 std::string Tag;
2288 if (ParseStringConstant(Tag))
2289 return true;
2290
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002291 if (ParseToken(lltok::lparen, "expected '(' in operand bundle"))
2292 return true;
2293
Sanjoy Dasf79d3442015-11-18 08:30:07 +00002294 std::vector<Value *> Inputs;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002295 while (Lex.getKind() != lltok::rparen) {
2296 // If this isn't the first input, we need a comma.
Sanjoy Dasf79d3442015-11-18 08:30:07 +00002297 if (!Inputs.empty() &&
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002298 ParseToken(lltok::comma, "expected ',' in input list"))
2299 return true;
2300
2301 Type *Ty = nullptr;
2302 Value *Input = nullptr;
2303 if (ParseType(Ty) || ParseValue(Ty, Input, PFS))
2304 return true;
Sanjoy Dasf79d3442015-11-18 08:30:07 +00002305 Inputs.push_back(Input);
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002306 }
2307
Sanjoy Dasf79d3442015-11-18 08:30:07 +00002308 BundleList.emplace_back(std::move(Tag), std::move(Inputs));
2309
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00002310 Lex.Lex(); // Lex the ')'.
2311 }
2312
2313 if (BundleList.empty())
2314 return Error(BeginLoc, "operand bundle set must not be empty");
2315
2316 Lex.Lex(); // Lex the ']'.
2317 return false;
2318}
Chris Lattnerac161bf2009-01-02 07:01:27 +00002319
Chris Lattner2ed06b42009-01-05 18:34:07 +00002320/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002321/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00002322/// ::= '(' ArgTypeListI ')'
2323/// ArgTypeListI
2324/// ::= /*empty*/
2325/// ::= '...'
2326/// ::= ArgTypeList ',' '...'
2327/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00002328///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002329bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
2330 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00002331 isVarArg = false;
2332 assert(Lex.getKind() == lltok::lparen);
2333 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002334
Chris Lattnerac161bf2009-01-02 07:01:27 +00002335 if (Lex.getKind() == lltok::rparen) {
2336 // empty
2337 } else if (Lex.getKind() == lltok::dotdotdot) {
2338 isVarArg = true;
2339 Lex.Lex();
2340 } else {
2341 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002342 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00002343 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002344 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002345
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002346 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00002347 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002348
Chris Lattnerfdd87902009-10-05 05:54:46 +00002349 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00002350 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002351
Chris Lattnerdef19492011-06-17 06:36:20 +00002352 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002353 Name = Lex.getStrVal();
2354 Lex.Lex();
2355 }
Chris Lattner3822f632009-01-02 08:05:26 +00002356
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002357 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00002358 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002359
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00002360 ArgList.emplace_back(TypeLoc, ArgTy,
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002361 AttributeSet::get(ArgTy->getContext(), Attrs),
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00002362 std::move(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002363
Chris Lattner3822f632009-01-02 08:05:26 +00002364 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002365 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00002366 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002367 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002368 break;
2369 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002370
Chris Lattnerac161bf2009-01-02 07:01:27 +00002371 // Otherwise must be an argument type.
2372 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00002373 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00002374
Chris Lattnerfdd87902009-10-05 05:54:46 +00002375 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00002376 return Error(TypeLoc, "argument can not have void type");
2377
Chris Lattnerdef19492011-06-17 06:36:20 +00002378 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002379 Name = Lex.getStrVal();
2380 Lex.Lex();
2381 } else {
2382 Name = "";
2383 }
Chris Lattner3822f632009-01-02 08:05:26 +00002384
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002385 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00002386 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002387
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00002388 ArgList.emplace_back(TypeLoc, ArgTy,
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002389 AttributeSet::get(ArgTy->getContext(), Attrs),
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00002390 std::move(Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002391 }
2392 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002393
Chris Lattner3822f632009-01-02 08:05:26 +00002394 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002395}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002396
Chris Lattnerac161bf2009-01-02 07:01:27 +00002397/// ParseFunctionType
2398/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002399bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002400 assert(Lex.getKind() == lltok::lparen);
2401
Chris Lattnerce473c72009-01-05 08:04:33 +00002402 if (!FunctionType::isValidReturnType(Result))
2403 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002404
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002405 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002406 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002407 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002408 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002409
Chris Lattnerac161bf2009-01-02 07:01:27 +00002410 // Reject names on the arguments lists.
2411 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
2412 if (!ArgList[i].Name.empty())
2413 return Error(ArgList[i].Loc, "argument name invalid in function type");
Reid Klecknerc2cb5602017-04-12 00:38:00 +00002414 if (ArgList[i].Attrs.hasAttributes())
Chris Lattner6bc5c892011-06-17 17:37:13 +00002415 return Error(ArgList[i].Loc,
2416 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002417 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002418
Jay Foadb804a2b2011-07-12 14:06:48 +00002419 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002420 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002421 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002422
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002423 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002424 return false;
2425}
2426
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002427/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
2428/// other structs.
2429bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
2430 SmallVector<Type*, 8> Elts;
2431 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002432
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002433 Result = StructType::get(Context, Elts, Packed);
2434 return false;
2435}
2436
2437/// ParseStructDefinition - Parse a struct in a 'type' definition.
2438bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
2439 std::pair<Type*, LocTy> &Entry,
2440 Type *&ResultTy) {
2441 // If the type was already defined, diagnose the redefinition.
2442 if (Entry.first && !Entry.second.isValid())
2443 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002444
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002445 // If we have opaque, just return without filling in the definition for the
2446 // struct. This counts as a definition as far as the .ll file goes.
2447 if (EatIfPresent(lltok::kw_opaque)) {
2448 // This type is being defined, so clear the location to indicate this.
2449 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002450
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002451 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002452 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002453 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002454 ResultTy = Entry.first;
2455 return false;
2456 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002457
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002458 // If the type starts with '<', then it is either a packed struct or a vector.
2459 bool isPacked = EatIfPresent(lltok::less);
2460
2461 // If we don't have a struct, then we have a random type alias, which we
2462 // accept for compatibility with old files. These types are not allowed to be
2463 // forward referenced and not allowed to be recursive.
2464 if (Lex.getKind() != lltok::lbrace) {
2465 if (Entry.first)
2466 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002467
Craig Topper2617dcc2014-04-15 06:32:26 +00002468 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002469 if (isPacked)
2470 return ParseArrayVectorType(ResultTy, true);
2471 return ParseType(ResultTy);
2472 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002473
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002474 // This type is being defined, so clear the location to indicate this.
2475 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002476
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002477 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002478 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002479 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002480
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002481 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002482
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002483 SmallVector<Type*, 8> Body;
2484 if (ParseStructBody(Body) ||
2485 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
2486 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002487
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002488 STy->setBody(Body, isPacked);
2489 ResultTy = STy;
2490 return false;
2491}
2492
Chris Lattnerac161bf2009-01-02 07:01:27 +00002493/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002494/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00002495/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002496/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002497/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002498/// ::= '<' '{' Type (',' Type)* '}' '>'
2499bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002500 assert(Lex.getKind() == lltok::lbrace);
2501 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002502
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002503 // Handle the empty struct.
2504 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002505 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002506
Chris Lattnerf880ca22009-03-09 04:49:14 +00002507 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002508 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002509 if (ParseType(Ty)) return true;
2510 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002511
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002512 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002513 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002514
Chris Lattner3822f632009-01-02 08:05:26 +00002515 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00002516 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002517 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002518
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002519 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002520 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002521
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002522 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002523 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002524
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002525 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002526}
2527
2528/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2529/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002530/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002531/// ::= '[' APSINTVAL 'x' Types ']'
2532/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002533bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002534 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2535 Lex.getAPSIntVal().getBitWidth() > 64)
2536 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002537
Chris Lattnerac161bf2009-01-02 07:01:27 +00002538 LocTy SizeLoc = Lex.getLoc();
2539 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002540 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002541
Chris Lattner3822f632009-01-02 08:05:26 +00002542 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2543 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002544
2545 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002546 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002547 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002548
Chris Lattner3822f632009-01-02 08:05:26 +00002549 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2550 "expected end of sequential type"))
2551 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002552
Chris Lattnerac161bf2009-01-02 07:01:27 +00002553 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002554 if (Size == 0)
2555 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002556 if ((unsigned)Size != Size)
2557 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002558 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002559 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002560 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002561 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002562 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002563 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002564 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002565 }
2566 return false;
2567}
2568
2569//===----------------------------------------------------------------------===//
2570// Function Semantic Analysis.
2571//===----------------------------------------------------------------------===//
2572
Chris Lattner3432c622009-10-28 03:39:23 +00002573LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2574 int functionNumber)
2575 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002576
2577 // Insert unnamed arguments into the NumberedVals list.
Duncan P. N. Exon Smithac331fb2015-10-20 01:12:49 +00002578 for (Argument &A : F.args())
2579 if (!A.hasName())
2580 NumberedVals.push_back(&A);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002581}
2582
2583LLParser::PerFunctionState::~PerFunctionState() {
2584 // If there were any forward referenced non-basicblock values, delete them.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002585
David Blaikie9ebdc692015-09-21 21:07:50 +00002586 for (const auto &P : ForwardRefVals) {
2587 if (isa<BasicBlock>(P.second.first))
2588 continue;
2589 P.second.first->replaceAllUsesWith(
2590 UndefValue::get(P.second.first->getType()));
Reid Kleckner96ab8722017-05-18 17:24:10 +00002591 P.second.first->deleteValue();
David Blaikie9ebdc692015-09-21 21:07:50 +00002592 }
2593
2594 for (const auto &P : ForwardRefValIDs) {
2595 if (isa<BasicBlock>(P.second.first))
2596 continue;
2597 P.second.first->replaceAllUsesWith(
2598 UndefValue::get(P.second.first->getType()));
Reid Kleckner96ab8722017-05-18 17:24:10 +00002599 P.second.first->deleteValue();
David Blaikie9ebdc692015-09-21 21:07:50 +00002600 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002601}
2602
Chris Lattner3432c622009-10-28 03:39:23 +00002603bool LLParser::PerFunctionState::FinishFunction() {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002604 if (!ForwardRefVals.empty())
2605 return P.Error(ForwardRefVals.begin()->second.second,
2606 "use of undefined value '%" + ForwardRefVals.begin()->first +
2607 "'");
2608 if (!ForwardRefValIDs.empty())
2609 return P.Error(ForwardRefValIDs.begin()->second.second,
2610 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002611 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002612 return false;
2613}
2614
Alexander Richardsonc11ae182018-02-27 11:15:11 +00002615static bool isValidVariableType(Module *M, Type *Ty, Value *Val, bool IsCall) {
2616 if (Val->getType() == Ty)
2617 return true;
2618 // For calls we also accept variables in the program address space
2619 if (IsCall && isa<PointerType>(Ty)) {
2620 Type *TyInProgAS = cast<PointerType>(Ty)->getElementType()->getPointerTo(
2621 M->getDataLayout().getProgramAddressSpace());
2622 if (Val->getType() == TyInProgAS)
2623 return true;
2624 }
2625 return false;
2626}
2627
Chris Lattnerac161bf2009-01-02 07:01:27 +00002628/// GetVal - Get a value with the specified name or ID, creating a
2629/// forward reference record if needed. This can return null if the value
2630/// exists but does not have the right type.
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002631Value *LLParser::PerFunctionState::GetVal(const std::string &Name, Type *Ty,
Alexander Richardsonc11ae182018-02-27 11:15:11 +00002632 LocTy Loc, bool IsCall) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002633 // Look this name up in the normal function symbol table.
Mehdi Aminia53d49e2016-09-17 06:00:02 +00002634 Value *Val = F.getValueSymbolTable()->lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002635
Chris Lattnerac161bf2009-01-02 07:01:27 +00002636 // If this is a forward reference for the value, see if we already created a
2637 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002638 if (!Val) {
David Blaikie9ebdc692015-09-21 21:07:50 +00002639 auto I = ForwardRefVals.find(Name);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002640 if (I != ForwardRefVals.end())
2641 Val = I->second.first;
2642 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002643
Chris Lattnerac161bf2009-01-02 07:01:27 +00002644 // If we have the value in the symbol table or fwd-ref table, return it.
2645 if (Val) {
Alexander Richardsonc11ae182018-02-27 11:15:11 +00002646 if (isValidVariableType(P.M, Ty, Val, IsCall))
2647 return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002648 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002649 P.Error(Loc, "'%" + Name + "' is not a basic block");
2650 else
2651 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002652 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002653 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002654 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002655
Chris Lattnerac161bf2009-01-02 07:01:27 +00002656 // Don't make placeholders with invalid type.
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002657 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002658 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002659 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002660 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002661
Chris Lattnerac161bf2009-01-02 07:01:27 +00002662 // Otherwise, create a new forward reference for this value and remember it.
2663 Value *FwdVal;
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002664 if (Ty->isLabelTy()) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002665 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002666 } else {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002667 FwdVal = new Argument(Ty, Name);
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002668 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002669
Chris Lattnerac161bf2009-01-02 07:01:27 +00002670 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2671 return FwdVal;
2672}
2673
Alexander Richardsonc11ae182018-02-27 11:15:11 +00002674Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty, LocTy Loc,
2675 bool IsCall) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002676 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002677 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002678
Chris Lattnerac161bf2009-01-02 07:01:27 +00002679 // If this is a forward reference for the value, see if we already created a
2680 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002681 if (!Val) {
David Blaikie9ebdc692015-09-21 21:07:50 +00002682 auto I = ForwardRefValIDs.find(ID);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002683 if (I != ForwardRefValIDs.end())
2684 Val = I->second.first;
2685 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002686
Chris Lattnerac161bf2009-01-02 07:01:27 +00002687 // If we have the value in the symbol table or fwd-ref table, return it.
2688 if (Val) {
Alexander Richardsonc11ae182018-02-27 11:15:11 +00002689 if (isValidVariableType(P.M, Ty, Val, IsCall))
2690 return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002691 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002692 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002693 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002694 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002695 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002696 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002697 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002698
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002699 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002700 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002701 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002702 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002703
Chris Lattnerac161bf2009-01-02 07:01:27 +00002704 // Otherwise, create a new forward reference for this value and remember it.
2705 Value *FwdVal;
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002706 if (Ty->isLabelTy()) {
Owen Anderson55f1c092009-08-13 21:58:54 +00002707 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002708 } else {
David Majnemer8a1c45d2015-12-12 05:38:55 +00002709 FwdVal = new Argument(Ty);
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002710 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002711
Chris Lattnerac161bf2009-01-02 07:01:27 +00002712 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2713 return FwdVal;
2714}
2715
2716/// SetInstName - After an instruction is parsed and inserted into its
2717/// basic block, this installs its name.
2718bool LLParser::PerFunctionState::SetInstName(int NameID,
2719 const std::string &NameStr,
2720 LocTy NameLoc, Instruction *Inst) {
2721 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002722 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002723 if (NameID != -1 || !NameStr.empty())
2724 return P.Error(NameLoc, "instructions returning void cannot have a name");
2725 return false;
2726 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002727
Chris Lattnerac161bf2009-01-02 07:01:27 +00002728 // If this was a numbered instruction, verify that the instruction is the
2729 // expected value and resolve any forward references.
2730 if (NameStr.empty()) {
2731 // If neither a name nor an ID was specified, just use the next ID.
2732 if (NameID == -1)
2733 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002734
Chris Lattnerac161bf2009-01-02 07:01:27 +00002735 if (unsigned(NameID) != NumberedVals.size())
2736 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002737 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002738
David Blaikie9ebdc692015-09-21 21:07:50 +00002739 auto FI = ForwardRefValIDs.find(NameID);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002740 if (FI != ForwardRefValIDs.end()) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002741 Value *Sentinel = FI->second.first;
2742 if (Sentinel->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002743 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002744 getTypeString(FI->second.first->getType()) + "'");
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002745
2746 Sentinel->replaceAllUsesWith(Inst);
Reid Kleckner96ab8722017-05-18 17:24:10 +00002747 Sentinel->deleteValue();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002748 ForwardRefValIDs.erase(FI);
2749 }
2750
2751 NumberedVals.push_back(Inst);
2752 return false;
2753 }
2754
2755 // Otherwise, the instruction had a name. Resolve forward refs and set it.
David Blaikie9ebdc692015-09-21 21:07:50 +00002756 auto FI = ForwardRefVals.find(NameStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002757 if (FI != ForwardRefVals.end()) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002758 Value *Sentinel = FI->second.first;
2759 if (Sentinel->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002760 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002761 getTypeString(FI->second.first->getType()) + "'");
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00002762
2763 Sentinel->replaceAllUsesWith(Inst);
Reid Kleckner96ab8722017-05-18 17:24:10 +00002764 Sentinel->deleteValue();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002765 ForwardRefVals.erase(FI);
2766 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002767
Chris Lattnerac161bf2009-01-02 07:01:27 +00002768 // Set the name on the instruction.
2769 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002770
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002771 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002772 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002773 NameStr + "'");
2774 return false;
2775}
2776
2777/// GetBB - Get a basic block with the specified name or ID, creating a
2778/// forward reference record if needed.
2779BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2780 LocTy Loc) {
Alexander Richardsonc11ae182018-02-27 11:15:11 +00002781 return dyn_cast_or_null<BasicBlock>(
2782 GetVal(Name, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002783}
2784
2785BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Alexander Richardsonc11ae182018-02-27 11:15:11 +00002786 return dyn_cast_or_null<BasicBlock>(
2787 GetVal(ID, Type::getLabelTy(F.getContext()), Loc, /*IsCall=*/false));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002788}
2789
2790/// DefineBB - Define the specified basic block, which is either named or
2791/// unnamed. If there is an error, this returns null otherwise it returns
2792/// the block being defined.
2793BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2794 LocTy Loc) {
2795 BasicBlock *BB;
2796 if (Name.empty())
2797 BB = GetBB(NumberedVals.size(), Loc);
2798 else
2799 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002800 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002801
Chris Lattnerac161bf2009-01-02 07:01:27 +00002802 // Move the block to the end of the function. Forward ref'd blocks are
2803 // inserted wherever they happen to be referenced.
2804 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002805
Chris Lattnerac161bf2009-01-02 07:01:27 +00002806 // Remove the block from forward ref sets.
2807 if (Name.empty()) {
2808 ForwardRefValIDs.erase(NumberedVals.size());
2809 NumberedVals.push_back(BB);
2810 } else {
2811 // BB forward references are already in the function symbol table.
2812 ForwardRefVals.erase(Name);
2813 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002814
Chris Lattnerac161bf2009-01-02 07:01:27 +00002815 return BB;
2816}
2817
2818//===----------------------------------------------------------------------===//
2819// Constants.
2820//===----------------------------------------------------------------------===//
2821
2822/// ParseValID - Parse an abstract value that doesn't necessarily have a
2823/// type implied. For example, if we parse "4" we don't know what integer type
2824/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002825/// sanity. PFS is used to convert function-local operands of metadata (since
2826/// metadata operands are not just parsed here but also converted to values).
2827/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002828bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002829 ID.Loc = Lex.getLoc();
2830 switch (Lex.getKind()) {
2831 default: return TokError("expected value token");
2832 case lltok::GlobalID: // @42
2833 ID.UIntVal = Lex.getUIntVal();
2834 ID.Kind = ValID::t_GlobalID;
2835 break;
2836 case lltok::GlobalVar: // @foo
2837 ID.StrVal = Lex.getStrVal();
2838 ID.Kind = ValID::t_GlobalName;
2839 break;
2840 case lltok::LocalVarID: // %42
2841 ID.UIntVal = Lex.getUIntVal();
2842 ID.Kind = ValID::t_LocalID;
2843 break;
2844 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002845 ID.StrVal = Lex.getStrVal();
2846 ID.Kind = ValID::t_LocalName;
2847 break;
2848 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002849 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002850 ID.Kind = ValID::t_APSInt;
2851 break;
2852 case lltok::APFloat:
2853 ID.APFloatVal = Lex.getAPFloatVal();
2854 ID.Kind = ValID::t_APFloat;
2855 break;
2856 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002857 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002858 ID.Kind = ValID::t_Constant;
2859 break;
2860 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002861 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002862 ID.Kind = ValID::t_Constant;
2863 break;
2864 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2865 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2866 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
David Majnemerf0f224d2015-11-11 21:57:16 +00002867 case lltok::kw_none: ID.Kind = ValID::t_None; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002868
Chris Lattnerac161bf2009-01-02 07:01:27 +00002869 case lltok::lbrace: {
2870 // ValID ::= '{' ConstVector '}'
2871 Lex.Lex();
2872 SmallVector<Constant*, 16> Elts;
2873 if (ParseGlobalValueVector(Elts) ||
2874 ParseToken(lltok::rbrace, "expected end of struct constant"))
2875 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002876
David Blaikieadbda4b2015-08-03 20:08:41 +00002877 ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002878 ID.UIntVal = Elts.size();
David Blaikieadbda4b2015-08-03 20:08:41 +00002879 memcpy(ID.ConstantStructElts.get(), Elts.data(),
2880 Elts.size() * sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002881 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002882 return false;
2883 }
2884 case lltok::less: {
2885 // ValID ::= '<' ConstVector '>' --> Vector.
2886 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2887 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002888 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002889
Chris Lattnerac161bf2009-01-02 07:01:27 +00002890 SmallVector<Constant*, 16> Elts;
2891 LocTy FirstEltLoc = Lex.getLoc();
2892 if (ParseGlobalValueVector(Elts) ||
2893 (isPackedStruct &&
2894 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2895 ParseToken(lltok::greater, "expected end of constant"))
2896 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002897
Chris Lattnerac161bf2009-01-02 07:01:27 +00002898 if (isPackedStruct) {
David Blaikieadbda4b2015-08-03 20:08:41 +00002899 ID.ConstantStructElts = make_unique<Constant *[]>(Elts.size());
2900 memcpy(ID.ConstantStructElts.get(), Elts.data(),
2901 Elts.size() * sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002902 ID.UIntVal = Elts.size();
2903 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002904 return false;
2905 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002906
Chris Lattnerac161bf2009-01-02 07:01:27 +00002907 if (Elts.empty())
2908 return Error(ID.Loc, "constant vector must not be empty");
2909
Duncan Sands9dff9be2010-02-15 16:12:20 +00002910 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002911 !Elts[0]->getType()->isFloatingPointTy() &&
2912 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002913 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002914 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002915
Chris Lattnerac161bf2009-01-02 07:01:27 +00002916 // Verify that all the vector elements have the same type.
2917 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2918 if (Elts[i]->getType() != Elts[0]->getType())
2919 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002920 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002921 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002922
Chris Lattner69229312011-02-15 00:14:00 +00002923 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002924 ID.Kind = ValID::t_Constant;
2925 return false;
2926 }
2927 case lltok::lsquare: { // Array Constant
2928 Lex.Lex();
2929 SmallVector<Constant*, 16> Elts;
2930 LocTy FirstEltLoc = Lex.getLoc();
2931 if (ParseGlobalValueVector(Elts) ||
2932 ParseToken(lltok::rsquare, "expected end of array constant"))
2933 return true;
2934
2935 // Handle empty element.
2936 if (Elts.empty()) {
2937 // Use undef instead of an array because it's inconvenient to determine
2938 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002939 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002940 return false;
2941 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002942
Chris Lattnerac161bf2009-01-02 07:01:27 +00002943 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002944 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002945 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002946
Owen Anderson4056ca92009-07-29 22:17:13 +00002947 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002948
Chris Lattnerac161bf2009-01-02 07:01:27 +00002949 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002950 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002951 if (Elts[i]->getType() != Elts[0]->getType())
2952 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002953 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002954 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002955 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002956
Jay Foad83be3612011-06-22 09:24:39 +00002957 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002958 ID.Kind = ValID::t_Constant;
2959 return false;
2960 }
2961 case lltok::kw_c: // c "foo"
2962 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002963 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2964 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002965 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2966 ID.Kind = ValID::t_Constant;
2967 return false;
2968
2969 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002970 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2971 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002972 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002973 Lex.Lex();
2974 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002975 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002976 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002977 ParseStringConstant(ID.StrVal) ||
2978 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002979 ParseToken(lltok::StringConstant, "expected constraint string"))
2980 return true;
2981 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002982 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002983 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002984 ID.Kind = ValID::t_InlineAsm;
2985 return false;
2986 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002987
Chris Lattner3432c622009-10-28 03:39:23 +00002988 case lltok::kw_blockaddress: {
2989 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2990 Lex.Lex();
2991
2992 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002993
Chris Lattner3432c622009-10-28 03:39:23 +00002994 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2995 ParseValID(Fn) ||
2996 ParseToken(lltok::comma, "expected comma in block address expression")||
2997 ParseValID(Label) ||
2998 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2999 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003000
Chris Lattner3432c622009-10-28 03:39:23 +00003001 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
3002 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00003003 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00003004 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003005
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00003006 // Try to find the function (but skip it if it's forward-referenced).
3007 GlobalValue *GV = nullptr;
3008 if (Fn.Kind == ValID::t_GlobalID) {
3009 if (Fn.UIntVal < NumberedVals.size())
3010 GV = NumberedVals[Fn.UIntVal];
3011 } else if (!ForwardRefVals.count(Fn.StrVal)) {
3012 GV = M->getNamedValue(Fn.StrVal);
3013 }
3014 Function *F = nullptr;
3015 if (GV) {
3016 // Confirm that it's actually a function with a definition.
3017 if (!isa<Function>(GV))
3018 return Error(Fn.Loc, "expected function name in blockaddress");
3019 F = cast<Function>(GV);
3020 if (F->isDeclaration())
3021 return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
3022 }
3023
3024 if (!F) {
3025 // Make a global variable as a placeholder for this reference.
David Blaikieb9cc6592015-03-04 01:40:07 +00003026 GlobalValue *&FwdRef =
David Blaikie871b4112015-08-03 20:55:00 +00003027 ForwardRefBlockAddresses.insert(std::make_pair(
3028 std::move(Fn),
3029 std::map<ValID, GlobalValue *>()))
David Blaikieb9cc6592015-03-04 01:40:07 +00003030 .first->second.insert(std::make_pair(std::move(Label), nullptr))
3031 .first->second;
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00003032 if (!FwdRef)
3033 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
3034 GlobalValue::InternalLinkage, nullptr, "");
3035 ID.ConstantVal = FwdRef;
3036 ID.Kind = ValID::t_Constant;
3037 return false;
3038 }
3039
3040 // We found the function; now find the basic block. Don't use PFS, since we
3041 // might be inside a constant expression.
3042 BasicBlock *BB;
3043 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
3044 if (Label.Kind == ValID::t_LocalID)
3045 BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
3046 else
3047 BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
3048 if (!BB)
3049 return Error(Label.Loc, "referenced value is not a basic block");
3050 } else {
3051 if (Label.Kind == ValID::t_LocalID)
3052 return Error(Label.Loc, "cannot take address of numeric label after "
3053 "the function is defined");
3054 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +00003055 F->getValueSymbolTable()->lookup(Label.StrVal));
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00003056 if (!BB)
3057 return Error(Label.Loc, "referenced value is not a basic block");
3058 }
3059
3060 ID.ConstantVal = BlockAddress::get(F, BB);
Chris Lattner3432c622009-10-28 03:39:23 +00003061 ID.Kind = ValID::t_Constant;
3062 return false;
3063 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003064
Chris Lattnerac161bf2009-01-02 07:01:27 +00003065 case lltok::kw_trunc:
3066 case lltok::kw_zext:
3067 case lltok::kw_sext:
3068 case lltok::kw_fptrunc:
3069 case lltok::kw_fpext:
3070 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00003071 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003072 case lltok::kw_uitofp:
3073 case lltok::kw_sitofp:
3074 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003075 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003076 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003077 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003078 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00003079 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003080 Constant *SrcVal;
3081 Lex.Lex();
3082 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
3083 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00003084 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003085 ParseType(DestTy) ||
3086 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
3087 return true;
3088 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
3089 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003090 getTypeString(SrcVal->getType()) + "' to '" +
3091 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003092 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00003093 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003094 ID.Kind = ValID::t_Constant;
3095 return false;
3096 }
3097 case lltok::kw_extractvalue: {
3098 Lex.Lex();
3099 Constant *Val;
3100 SmallVector<unsigned, 4> Indices;
3101 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
3102 ParseGlobalTypeAndValue(Val) ||
3103 ParseIndexList(Indices) ||
3104 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
3105 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00003106
Chris Lattner392be582010-02-12 20:49:41 +00003107 if (!Val->getType()->isAggregateType())
3108 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00003109 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00003110 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00003111 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003112 ID.Kind = ValID::t_Constant;
3113 return false;
3114 }
3115 case lltok::kw_insertvalue: {
3116 Lex.Lex();
3117 Constant *Val0, *Val1;
3118 SmallVector<unsigned, 4> Indices;
3119 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
3120 ParseGlobalTypeAndValue(Val0) ||
3121 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
3122 ParseGlobalTypeAndValue(Val1) ||
3123 ParseIndexList(Indices) ||
3124 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
3125 return true;
Chris Lattner392be582010-02-12 20:49:41 +00003126 if (!Val0->getType()->isAggregateType())
3127 return Error(ID.Loc, "insertvalue operand must be aggregate type");
David Majnemereba692d2015-02-23 07:13:52 +00003128 Type *IndexedType =
3129 ExtractValueInst::getIndexedType(Val0->getType(), Indices);
3130 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003131 return Error(ID.Loc, "invalid indices for insertvalue");
David Majnemereba692d2015-02-23 07:13:52 +00003132 if (IndexedType != Val1->getType())
3133 return Error(ID.Loc, "insertvalue operand and field disagree in type: '" +
3134 getTypeString(Val1->getType()) +
3135 "' instead of '" + getTypeString(IndexedType) +
3136 "'");
Jay Foad57aa6362011-07-13 10:26:04 +00003137 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003138 ID.Kind = ValID::t_Constant;
3139 return false;
3140 }
3141 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003142 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003143 unsigned PredVal, Opc = Lex.getUIntVal();
3144 Constant *Val0, *Val1;
3145 Lex.Lex();
3146 if (ParseCmpPredicate(PredVal, Opc) ||
3147 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
3148 ParseGlobalTypeAndValue(Val0) ||
3149 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
3150 ParseGlobalTypeAndValue(Val1) ||
3151 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
3152 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003153
Chris Lattnerac161bf2009-01-02 07:01:27 +00003154 if (Val0->getType() != Val1->getType())
3155 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003156
Chris Lattnerac161bf2009-01-02 07:01:27 +00003157 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003158
Chris Lattnerac161bf2009-01-02 07:01:27 +00003159 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00003160 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003161 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00003162 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00003163 } else {
3164 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003165 if (!Val0->getType()->isIntOrIntVectorTy() &&
Craig Topper95d23472017-07-09 07:04:00 +00003166 !Val0->getType()->isPtrOrPtrVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003167 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00003168 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003169 }
3170 ID.Kind = ValID::t_Constant;
3171 return false;
3172 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003173
Chris Lattnerac161bf2009-01-02 07:01:27 +00003174 // Binary Operators.
3175 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00003176 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003177 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00003178 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003179 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00003180 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00003181 case lltok::kw_udiv:
3182 case lltok::kw_sdiv:
3183 case lltok::kw_fdiv:
3184 case lltok::kw_urem:
3185 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003186 case lltok::kw_frem:
3187 case lltok::kw_shl:
3188 case lltok::kw_lshr:
3189 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00003190 bool NUW = false;
3191 bool NSW = false;
3192 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003193 unsigned Opc = Lex.getUIntVal();
3194 Constant *Val0, *Val1;
3195 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00003196 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00003197 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
3198 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00003199 if (EatIfPresent(lltok::kw_nuw))
3200 NUW = true;
3201 if (EatIfPresent(lltok::kw_nsw)) {
3202 NSW = true;
3203 if (EatIfPresent(lltok::kw_nuw))
3204 NUW = true;
3205 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00003206 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
3207 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00003208 if (EatIfPresent(lltok::kw_exact))
3209 Exact = true;
3210 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003211 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
3212 ParseGlobalTypeAndValue(Val0) ||
3213 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
3214 ParseGlobalTypeAndValue(Val1) ||
3215 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
3216 return true;
3217 if (Val0->getType() != Val1->getType())
3218 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003219 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00003220 if (NUW)
3221 return Error(ModifierLoc, "nuw only applies to integer operations");
3222 if (NSW)
3223 return Error(ModifierLoc, "nsw only applies to integer operations");
3224 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00003225 // Check that the type is valid for the operator.
3226 switch (Opc) {
3227 case Instruction::Add:
3228 case Instruction::Sub:
3229 case Instruction::Mul:
3230 case Instruction::UDiv:
3231 case Instruction::SDiv:
3232 case Instruction::URem:
3233 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00003234 case Instruction::Shl:
3235 case Instruction::AShr:
3236 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00003237 if (!Val0->getType()->isIntOrIntVectorTy())
3238 return Error(ID.Loc, "constexpr requires integer operands");
3239 break;
3240 case Instruction::FAdd:
3241 case Instruction::FSub:
3242 case Instruction::FMul:
3243 case Instruction::FDiv:
3244 case Instruction::FRem:
3245 if (!Val0->getType()->isFPOrFPVectorTy())
3246 return Error(ID.Loc, "constexpr requires fp operands");
3247 break;
3248 default: llvm_unreachable("Unknown binary operator!");
3249 }
Dan Gohman1b849082009-09-07 23:54:19 +00003250 unsigned Flags = 0;
3251 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
3252 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00003253 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00003254 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00003255 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003256 ID.Kind = ValID::t_Constant;
3257 return false;
3258 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003259
Chris Lattnerac161bf2009-01-02 07:01:27 +00003260 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00003261 case lltok::kw_and:
3262 case lltok::kw_or:
3263 case lltok::kw_xor: {
3264 unsigned Opc = Lex.getUIntVal();
3265 Constant *Val0, *Val1;
3266 Lex.Lex();
3267 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
3268 ParseGlobalTypeAndValue(Val0) ||
3269 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
3270 ParseGlobalTypeAndValue(Val1) ||
3271 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
3272 return true;
3273 if (Val0->getType() != Val1->getType())
3274 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00003275 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003276 return Error(ID.Loc,
3277 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00003278 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003279 ID.Kind = ValID::t_Constant;
3280 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003281 }
3282
Chris Lattnerac161bf2009-01-02 07:01:27 +00003283 case lltok::kw_getelementptr:
3284 case lltok::kw_shufflevector:
3285 case lltok::kw_insertelement:
3286 case lltok::kw_extractelement:
3287 case lltok::kw_select: {
3288 unsigned Opc = Lex.getUIntVal();
3289 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00003290 bool InBounds = false;
David Blaikief72d05b2015-03-13 18:20:45 +00003291 Type *Ty;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003292 Lex.Lex();
David Blaikief72d05b2015-03-13 18:20:45 +00003293
Dan Gohman1639c392009-07-27 21:53:46 +00003294 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00003295 InBounds = EatIfPresent(lltok::kw_inbounds);
David Blaikief72d05b2015-03-13 18:20:45 +00003296
3297 if (ParseToken(lltok::lparen, "expected '(' in constantexpr"))
3298 return true;
3299
3300 LocTy ExplicitTypeLoc = Lex.getLoc();
3301 if (Opc == Instruction::GetElementPtr) {
3302 if (ParseType(Ty) ||
3303 ParseToken(lltok::comma, "expected comma after getelementptr's type"))
3304 return true;
3305 }
3306
Peter Collingbourned93620b2016-11-10 22:34:55 +00003307 Optional<unsigned> InRangeOp;
3308 if (ParseGlobalValueVector(
3309 Elts, Opc == Instruction::GetElementPtr ? &InRangeOp : nullptr) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003310 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
3311 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003312
Chris Lattnerac161bf2009-01-02 07:01:27 +00003313 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00003314 if (Elts.size() == 0 ||
Craig Topper95d23472017-07-09 07:04:00 +00003315 !Elts[0]->getType()->isPtrOrPtrVectorTy())
David Majnemer00303b62015-02-22 23:14:52 +00003316 return Error(ID.Loc, "base of getelementptr must be a pointer");
3317
3318 Type *BaseType = Elts[0]->getType();
3319 auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
David Blaikief72d05b2015-03-13 18:20:45 +00003320 if (Ty != BasePointerType->getElementType())
3321 return Error(
3322 ExplicitTypeLoc,
3323 "explicit pointee type doesn't match operand's pointee type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003324
Michael Kuperstein88f15ee2016-12-21 18:29:47 +00003325 unsigned GEPWidth =
3326 BaseType->isVectorTy() ? BaseType->getVectorNumElements() : 0;
3327
Jay Foaded8db7d2011-07-21 14:31:17 +00003328 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
David Majnemer00303b62015-02-22 23:14:52 +00003329 for (Constant *Val : Indices) {
3330 Type *ValTy = Val->getType();
Craig Topper95d23472017-07-09 07:04:00 +00003331 if (!ValTy->isIntOrIntVectorTy())
David Majnemer00303b62015-02-22 23:14:52 +00003332 return Error(ID.Loc, "getelementptr index must be an integer");
David Majnemer00303b62015-02-22 23:14:52 +00003333 if (ValTy->isVectorTy()) {
Elena Demikhovsky37a4da82015-07-09 07:42:48 +00003334 unsigned ValNumEl = ValTy->getVectorNumElements();
Michael Kuperstein88f15ee2016-12-21 18:29:47 +00003335 if (GEPWidth && (ValNumEl != GEPWidth))
David Majnemer00303b62015-02-22 23:14:52 +00003336 return Error(
3337 ID.Loc,
3338 "getelementptr vector index has a wrong number of elements");
Michael Kuperstein88f15ee2016-12-21 18:29:47 +00003339 // GEPWidth may have been unknown because the base is a scalar,
3340 // but it is known now.
3341 GEPWidth = ValNumEl;
David Majnemer00303b62015-02-22 23:14:52 +00003342 }
3343 }
3344
Craig Toppere3dcce92015-08-01 22:20:21 +00003345 SmallPtrSet<Type*, 4> Visited;
David Blaikiee169e822015-04-22 16:37:35 +00003346 if (!Indices.empty() && !Ty->isSized(&Visited))
David Majnemer00303b62015-02-22 23:14:52 +00003347 return Error(ID.Loc, "base element of getelementptr must be sized");
3348
David Blaikie4a2e73b2015-04-02 18:55:32 +00003349 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
David Majnemer00303b62015-02-22 23:14:52 +00003350 return Error(ID.Loc, "invalid getelementptr indices");
Peter Collingbourned93620b2016-11-10 22:34:55 +00003351
3352 if (InRangeOp) {
3353 if (*InRangeOp == 0)
3354 return Error(ID.Loc,
3355 "inrange keyword may not appear on pointer operand");
3356 --*InRangeOp;
3357 }
3358
3359 ID.ConstantVal = ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices,
3360 InBounds, InRangeOp);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003361 } else if (Opc == Instruction::Select) {
3362 if (Elts.size() != 3)
3363 return Error(ID.Loc, "expected three operands to select");
3364 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
3365 Elts[2]))
3366 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00003367 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003368 } else if (Opc == Instruction::ShuffleVector) {
3369 if (Elts.size() != 3)
3370 return Error(ID.Loc, "expected three operands to shufflevector");
3371 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
3372 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00003373 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00003374 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003375 } else if (Opc == Instruction::ExtractElement) {
3376 if (Elts.size() != 2)
3377 return Error(ID.Loc, "expected two operands to extractelement");
3378 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
3379 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00003380 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003381 } else {
3382 assert(Opc == Instruction::InsertElement && "Unknown opcode");
3383 if (Elts.size() != 3)
3384 return Error(ID.Loc, "expected three operands to insertelement");
3385 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
3386 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00003387 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00003388 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003389 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003390
Chris Lattnerac161bf2009-01-02 07:01:27 +00003391 ID.Kind = ValID::t_Constant;
3392 return false;
3393 }
3394 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003395
Chris Lattnerac161bf2009-01-02 07:01:27 +00003396 Lex.Lex();
3397 return false;
3398}
3399
3400/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00003401bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003402 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003403 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00003404 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003405 bool Parsed = ParseValID(ID) ||
Alexander Richardsonc11ae182018-02-27 11:15:11 +00003406 ConvertValIDToValue(Ty, ID, V, nullptr, /*IsCall=*/false);
Victor Hernandez9d75c962010-01-11 22:31:58 +00003407 if (V && !(C = dyn_cast<Constant>(V)))
3408 return Error(ID.Loc, "global values must be constants");
3409 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003410}
3411
Victor Hernandez9d75c962010-01-11 22:31:58 +00003412bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00003413 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003414 return ParseType(Ty) ||
3415 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00003416}
3417
Rafael Espindola83a362c2015-01-06 22:55:16 +00003418bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
David Majnemerdad0a642014-06-27 18:19:56 +00003419 C = nullptr;
Rafael Espindola83a362c2015-01-06 22:55:16 +00003420
3421 LocTy KwLoc = Lex.getLoc();
David Majnemerdad0a642014-06-27 18:19:56 +00003422 if (!EatIfPresent(lltok::kw_comdat))
3423 return false;
Rafael Espindola83a362c2015-01-06 22:55:16 +00003424
3425 if (EatIfPresent(lltok::lparen)) {
3426 if (Lex.getKind() != lltok::ComdatVar)
3427 return TokError("expected comdat variable");
3428 C = getComdat(Lex.getStrVal(), Lex.getLoc());
3429 Lex.Lex();
3430 if (ParseToken(lltok::rparen, "expected ')' after comdat var"))
3431 return true;
3432 } else {
3433 if (GlobalName.empty())
3434 return TokError("comdat cannot be unnamed");
3435 C = getComdat(GlobalName, KwLoc);
3436 }
3437
David Majnemerdad0a642014-06-27 18:19:56 +00003438 return false;
3439}
3440
Victor Hernandez9d75c962010-01-11 22:31:58 +00003441/// ParseGlobalValueVector
3442/// ::= /*empty*/
Peter Collingbourned93620b2016-11-10 22:34:55 +00003443/// ::= [inrange] TypeAndValue (',' [inrange] TypeAndValue)*
3444bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
3445 Optional<unsigned> *InRangeOp) {
Victor Hernandez9d75c962010-01-11 22:31:58 +00003446 // Empty list.
3447 if (Lex.getKind() == lltok::rbrace ||
3448 Lex.getKind() == lltok::rsquare ||
3449 Lex.getKind() == lltok::greater ||
3450 Lex.getKind() == lltok::rparen)
3451 return false;
3452
Peter Collingbourned93620b2016-11-10 22:34:55 +00003453 do {
3454 if (InRangeOp && !*InRangeOp && EatIfPresent(lltok::kw_inrange))
3455 *InRangeOp = Elts.size();
Victor Hernandez9d75c962010-01-11 22:31:58 +00003456
Peter Collingbourned93620b2016-11-10 22:34:55 +00003457 Constant *C;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003458 if (ParseGlobalTypeAndValue(C)) return true;
3459 Elts.push_back(C);
Peter Collingbourned93620b2016-11-10 22:34:55 +00003460 } while (EatIfPresent(lltok::comma));
Victor Hernandez9d75c962010-01-11 22:31:58 +00003461
3462 return false;
3463}
3464
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +00003465bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003466 SmallVector<Metadata *, 16> Elts;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003467 if (ParseMDNodeVector(Elts))
Dan Gohmanc828c542010-08-24 02:24:03 +00003468 return true;
3469
Duncan P. N. Exon Smith0b31dd12015-01-12 22:27:39 +00003470 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00003471 return false;
3472}
3473
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003474/// MDNode:
3475/// ::= !{ ... }
3476/// ::= !7
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003477/// ::= !DILocation(...)
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003478bool LLParser::ParseMDNode(MDNode *&N) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003479 if (Lex.getKind() == lltok::MetadataVar)
3480 return ParseSpecializedMDNode(N);
3481
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003482 return ParseToken(lltok::exclaim, "expected '!' here") ||
3483 ParseMDNodeTail(N);
3484}
3485
3486bool LLParser::ParseMDNodeTail(MDNode *&N) {
3487 // !{ ... }
3488 if (Lex.getKind() == lltok::lbrace)
3489 return ParseMDTuple(N);
3490
3491 // !42
3492 return ParseMDNodeID(N);
3493}
3494
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003495namespace {
3496
3497/// Structure to represent an optional metadata field.
3498template <class FieldTy> struct MDFieldImpl {
3499 typedef MDFieldImpl ImplTy;
3500 FieldTy Val;
3501 bool Seen;
3502
3503 void assign(FieldTy Val) {
3504 Seen = true;
3505 this->Val = std::move(Val);
3506 }
3507
3508 explicit MDFieldImpl(FieldTy Default)
3509 : Val(std::move(Default)), Seen(false) {}
3510};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003511
Sander de Smalenfdf40912018-01-24 09:56:07 +00003512/// Structure to represent an optional metadata field that
3513/// can be of either type (A or B) and encapsulates the
3514/// MD<typeofA>Field and MD<typeofB>Field structs, so not
3515/// to reimplement the specifics for representing each Field.
3516template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
3517 typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
3518 FieldTypeA A;
3519 FieldTypeB B;
3520 bool Seen;
3521
3522 enum {
3523 IsInvalid = 0,
3524 IsTypeA = 1,
3525 IsTypeB = 2
3526 } WhatIs;
3527
3528 void assign(FieldTypeA A) {
3529 Seen = true;
3530 this->A = std::move(A);
3531 WhatIs = IsTypeA;
3532 }
3533
3534 void assign(FieldTypeB B) {
3535 Seen = true;
3536 this->B = std::move(B);
3537 WhatIs = IsTypeB;
3538 }
3539
3540 explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
3541 : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
3542 WhatIs(IsInvalid) {}
3543};
3544
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003545struct MDUnsignedField : public MDFieldImpl<uint64_t> {
3546 uint64_t Max;
3547
3548 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
3549 : ImplTy(Default), Max(Max) {}
3550};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003551
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003552struct LineField : public MDUnsignedField {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +00003553 LineField() : MDUnsignedField(0, UINT32_MAX) {}
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003554};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003555
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003556struct ColumnField : public MDUnsignedField {
3557 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
3558};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003559
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003560struct DwarfTagField : public MDUnsignedField {
Duncan P. N. Exon Smitha81f7e12015-02-06 22:29:35 +00003561 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003562 DwarfTagField(dwarf::Tag DefaultTag)
3563 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003564};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003565
Amjad Abouda9bcf162015-12-10 12:56:35 +00003566struct DwarfMacinfoTypeField : public MDUnsignedField {
3567 DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
3568 DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
3569 : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
3570};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003571
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003572struct DwarfAttEncodingField : public MDUnsignedField {
3573 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
3574};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003575
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003576struct DwarfVirtualityField : public MDUnsignedField {
3577 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
3578};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003579
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003580struct DwarfLangField : public MDUnsignedField {
3581 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
3582};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003583
Reid Klecknerde3d8b52016-06-08 20:34:29 +00003584struct DwarfCCField : public MDUnsignedField {
3585 DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
3586};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003587
Adrian Prantlb939a252016-03-31 23:56:58 +00003588struct EmissionKindField : public MDUnsignedField {
3589 EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
3590};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003591
Leny Kholodov5fcc4182016-09-06 10:46:28 +00003592struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
3593 DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003594};
3595
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003596struct MDSignedField : public MDFieldImpl<int64_t> {
3597 int64_t Min;
3598 int64_t Max;
3599
3600 MDSignedField(int64_t Default = 0)
3601 : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {}
3602 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
3603 : ImplTy(Default), Min(Min), Max(Max) {}
3604};
3605
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003606struct MDBoolField : public MDFieldImpl<bool> {
3607 MDBoolField(bool Default = false) : ImplTy(Default) {}
3608};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003609
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003610struct MDField : public MDFieldImpl<Metadata *> {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003611 bool AllowNull;
3612
3613 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003614};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003615
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003616struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
3617 MDConstant() : ImplTy(nullptr) {}
3618};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003619
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003620struct MDStringField : public MDFieldImpl<MDString *> {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003621 bool AllowEmpty;
3622 MDStringField(bool AllowEmpty = true)
3623 : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003624};
Eugene Zelenko1804a772016-08-25 00:45:04 +00003625
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003626struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
3627 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
3628};
3629
Amjad Aboud7faeecc2016-12-25 10:12:09 +00003630struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
Amjad Aboud7faeecc2016-12-25 10:12:09 +00003631 ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
3632};
3633
Sander de Smalenfdf40912018-01-24 09:56:07 +00003634struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
3635 MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
3636 : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
3637
3638 MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
3639 bool AllowNull = true)
3640 : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
3641
3642 bool isMDSignedField() const { return WhatIs == IsTypeA; }
3643 bool isMDField() const { return WhatIs == IsTypeB; }
3644 int64_t getMDSignedValue() const {
3645 assert(isMDSignedField() && "Wrong field type");
3646 return A.Val;
3647 }
3648 Metadata *getMDFieldValue() const {
3649 assert(isMDField() && "Wrong field type");
3650 return B.Val;
3651 }
3652};
3653
Momchil Velikov08dc66e2018-02-12 16:10:09 +00003654struct MDSignedOrUnsignedField
3655 : MDEitherFieldImpl<MDSignedField, MDUnsignedField> {
3656 MDSignedOrUnsignedField() : ImplTy(MDSignedField(0), MDUnsignedField(0)) {}
3657
3658 bool isMDSignedField() const { return WhatIs == IsTypeA; }
3659 bool isMDUnsignedField() const { return WhatIs == IsTypeB; }
3660 int64_t getMDSignedValue() const {
3661 assert(isMDSignedField() && "Wrong field type");
3662 return A.Val;
3663 }
3664 uint64_t getMDUnsignedValue() const {
3665 assert(isMDUnsignedField() && "Wrong field type");
3666 return B.Val;
3667 }
3668};
3669
Eugene Zelenko1804a772016-08-25 00:45:04 +00003670} // end anonymous namespace
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003671
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00003672namespace llvm {
3673
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003674template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003675bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003676 MDUnsignedField &Result) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003677 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3678 return TokError("expected unsigned integer");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003679
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003680 auto &U = Lex.getAPSIntVal();
3681 if (U.ugt(Result.Max))
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003682 return TokError("value for '" + Name + "' too large, limit is " +
3683 Twine(Result.Max));
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003684 Result.assign(U.getZExtValue());
3685 assert(Result.Val <= Result.Max && "Expected value in range");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003686 Lex.Lex();
3687 return false;
3688}
3689
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003690template <>
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003691bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) {
3692 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3693}
3694template <>
3695bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
3696 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3697}
3698
3699template <>
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003700bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
3701 if (Lex.getKind() == lltok::APSInt)
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003702 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003703
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003704 if (Lex.getKind() != lltok::DwarfTag)
3705 return TokError("expected DWARF tag");
3706
3707 unsigned Tag = dwarf::getTag(Lex.getStrVal());
3708 if (Tag == dwarf::DW_TAG_invalid)
3709 return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
Duncan P. N. Exon Smithfad631a2015-02-04 22:02:18 +00003710 assert(Tag <= Result.Max && "Expected valid DWARF tag");
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003711
3712 Result.assign(Tag);
3713 Lex.Lex();
3714 return false;
3715}
3716
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003717template <>
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003718bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Amjad Abouda9bcf162015-12-10 12:56:35 +00003719 DwarfMacinfoTypeField &Result) {
3720 if (Lex.getKind() == lltok::APSInt)
3721 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3722
3723 if (Lex.getKind() != lltok::DwarfMacinfo)
3724 return TokError("expected DWARF macinfo type");
3725
3726 unsigned Macinfo = dwarf::getMacinfo(Lex.getStrVal());
3727 if (Macinfo == dwarf::DW_MACINFO_invalid)
3728 return TokError(
3729 "invalid DWARF macinfo type" + Twine(" '") + Lex.getStrVal() + "'");
3730 assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
3731
3732 Result.assign(Macinfo);
3733 Lex.Lex();
3734 return false;
3735}
3736
3737template <>
3738bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003739 DwarfVirtualityField &Result) {
3740 if (Lex.getKind() == lltok::APSInt)
3741 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3742
3743 if (Lex.getKind() != lltok::DwarfVirtuality)
3744 return TokError("expected DWARF virtuality code");
3745
3746 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
Peter Collingbournea1f86252016-03-17 23:58:03 +00003747 if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003748 return TokError("invalid DWARF virtuality code" + Twine(" '") +
3749 Lex.getStrVal() + "'");
3750 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
3751 Result.assign(Virtuality);
3752 Lex.Lex();
3753 return false;
3754}
3755
3756template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003757bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
3758 if (Lex.getKind() == lltok::APSInt)
3759 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3760
3761 if (Lex.getKind() != lltok::DwarfLang)
3762 return TokError("expected DWARF language");
3763
3764 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
3765 if (!Lang)
3766 return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
3767 "'");
3768 assert(Lang <= Result.Max && "Expected valid DWARF language");
3769 Result.assign(Lang);
3770 Lex.Lex();
3771 return false;
3772}
3773
3774template <>
Reid Klecknerde3d8b52016-06-08 20:34:29 +00003775bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
3776 if (Lex.getKind() == lltok::APSInt)
3777 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3778
3779 if (Lex.getKind() != lltok::DwarfCC)
3780 return TokError("expected DWARF calling convention");
3781
3782 unsigned CC = dwarf::getCallingConvention(Lex.getStrVal());
3783 if (!CC)
3784 return TokError("invalid DWARF calling convention" + Twine(" '") + Lex.getStrVal() +
3785 "'");
3786 assert(CC <= Result.Max && "Expected valid DWARF calling convention");
3787 Result.assign(CC);
3788 Lex.Lex();
3789 return false;
3790}
3791
3792template <>
Adrian Prantlb939a252016-03-31 23:56:58 +00003793bool LLParser::ParseMDField(LocTy Loc, StringRef Name, EmissionKindField &Result) {
3794 if (Lex.getKind() == lltok::APSInt)
3795 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3796
3797 if (Lex.getKind() != lltok::EmissionKind)
3798 return TokError("expected emission kind");
3799
3800 auto Kind = DICompileUnit::getEmissionKind(Lex.getStrVal());
3801 if (!Kind)
3802 return TokError("invalid emission kind" + Twine(" '") + Lex.getStrVal() +
3803 "'");
3804 assert(*Kind <= Result.Max && "Expected valid emission kind");
3805 Result.assign(*Kind);
3806 Lex.Lex();
3807 return false;
3808}
3809
3810template <>
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003811bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003812 DwarfAttEncodingField &Result) {
3813 if (Lex.getKind() == lltok::APSInt)
3814 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3815
3816 if (Lex.getKind() != lltok::DwarfAttEncoding)
3817 return TokError("expected DWARF type attribute encoding");
3818
3819 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
3820 if (!Encoding)
3821 return TokError("invalid DWARF type attribute encoding" + Twine(" '") +
3822 Lex.getStrVal() + "'");
3823 assert(Encoding <= Result.Max && "Expected valid DWARF language");
3824 Result.assign(Encoding);
3825 Lex.Lex();
3826 return false;
3827}
3828
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003829/// DIFlagField
3830/// ::= uint32
3831/// ::= DIFlagVector
3832/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
3833template <>
3834bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003835
3836 // Parser for a single flag.
Leny Kholodov5fcc4182016-09-06 10:46:28 +00003837 auto parseFlag = [&](DINode::DIFlags &Val) {
3838 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
3839 uint32_t TempVal = static_cast<uint32_t>(Val);
3840 bool Res = ParseUInt32(TempVal);
3841 Val = static_cast<DINode::DIFlags>(TempVal);
3842 return Res;
3843 }
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003844
3845 if (Lex.getKind() != lltok::DIFlag)
3846 return TokError("expected debug info flag");
3847
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003848 Val = DINode::getFlag(Lex.getStrVal());
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003849 if (!Val)
3850 return TokError(Twine("invalid debug info flag flag '") +
3851 Lex.getStrVal() + "'");
3852 Lex.Lex();
3853 return false;
3854 };
3855
3856 // Parse the flags and combine them together.
Leny Kholodov5fcc4182016-09-06 10:46:28 +00003857 DINode::DIFlags Combined = DINode::FlagZero;
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003858 do {
Leny Kholodov5fcc4182016-09-06 10:46:28 +00003859 DINode::DIFlags Val;
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003860 if (parseFlag(Val))
3861 return true;
3862 Combined |= Val;
3863 } while (EatIfPresent(lltok::bar));
3864
3865 Result.assign(Combined);
3866 return false;
3867}
3868
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003869template <>
3870bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003871 MDSignedField &Result) {
3872 if (Lex.getKind() != lltok::APSInt)
3873 return TokError("expected signed integer");
3874
3875 auto &S = Lex.getAPSIntVal();
3876 if (S < Result.Min)
3877 return TokError("value for '" + Name + "' too small, limit is " +
3878 Twine(Result.Min));
3879 if (S > Result.Max)
3880 return TokError("value for '" + Name + "' too large, limit is " +
3881 Twine(Result.Max));
3882 Result.assign(S.getExtValue());
3883 assert(Result.Val >= Result.Min && "Expected value in range");
3884 assert(Result.Val <= Result.Max && "Expected value in range");
3885 Lex.Lex();
3886 return false;
3887}
3888
3889template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003890bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
3891 switch (Lex.getKind()) {
3892 default:
3893 return TokError("expected 'true' or 'false'");
3894 case lltok::kw_true:
3895 Result.assign(true);
3896 break;
3897 case lltok::kw_false:
3898 Result.assign(false);
3899 break;
3900 }
3901 Lex.Lex();
3902 return false;
3903}
3904
3905template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003906bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003907 if (Lex.getKind() == lltok::kw_null) {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003908 if (!Result.AllowNull)
3909 return TokError("'" + Name + "' cannot be null");
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003910 Lex.Lex();
3911 Result.assign(nullptr);
3912 return false;
3913 }
3914
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003915 Metadata *MD;
3916 if (ParseMetadata(MD, nullptr))
3917 return true;
3918
3919 Result.assign(MD);
3920 return false;
3921}
3922
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003923template <>
Sander de Smalenfdf40912018-01-24 09:56:07 +00003924bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3925 MDSignedOrMDField &Result) {
3926 // Try to parse a signed int.
3927 if (Lex.getKind() == lltok::APSInt) {
3928 MDSignedField Res = Result.A;
3929 if (!ParseMDField(Loc, Name, Res)) {
3930 Result.assign(Res);
3931 return false;
3932 }
3933 return true;
3934 }
3935
3936 // Otherwise, try to parse as an MDField.
3937 MDField Res = Result.B;
3938 if (!ParseMDField(Loc, Name, Res)) {
3939 Result.assign(Res);
3940 return false;
3941 }
3942
3943 return true;
3944}
3945
3946template <>
Momchil Velikov08dc66e2018-02-12 16:10:09 +00003947bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3948 MDSignedOrUnsignedField &Result) {
3949 if (Lex.getKind() != lltok::APSInt)
3950 return false;
3951
3952 if (Lex.getAPSIntVal().isSigned()) {
3953 MDSignedField Res = Result.A;
3954 if (ParseMDField(Loc, Name, Res))
3955 return true;
3956 Result.assign(Res);
3957 return false;
3958 }
3959
3960 MDUnsignedField Res = Result.B;
3961 if (ParseMDField(Loc, Name, Res))
3962 return true;
3963 Result.assign(Res);
3964 return false;
3965}
3966
3967template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003968bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003969 LocTy ValueLoc = Lex.getLoc();
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003970 std::string S;
3971 if (ParseStringConstant(S))
3972 return true;
3973
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003974 if (!Result.AllowEmpty && S.empty())
3975 return Error(ValueLoc, "'" + Name + "' cannot be empty");
3976
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003977 Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003978 return false;
3979}
3980
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003981template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003982bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
3983 SmallVector<Metadata *, 4> MDs;
3984 if (ParseMDNodeVector(MDs))
3985 return true;
3986
3987 Result.assign(std::move(MDs));
3988 return false;
3989}
3990
Amjad Aboud7faeecc2016-12-25 10:12:09 +00003991template <>
3992bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3993 ChecksumKindField &Result) {
Scott Linder71603842018-02-12 19:45:54 +00003994 Optional<DIFile::ChecksumKind> CSKind =
3995 DIFile::getChecksumKind(Lex.getStrVal());
3996
3997 if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
Amjad Aboud7faeecc2016-12-25 10:12:09 +00003998 return TokError(
3999 "invalid checksum kind" + Twine(" '") + Lex.getStrVal() + "'");
4000
Scott Linder71603842018-02-12 19:45:54 +00004001 Result.assign(*CSKind);
Amjad Aboud7faeecc2016-12-25 10:12:09 +00004002 Lex.Lex();
4003 return false;
4004}
4005
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00004006} // end namespace llvm
4007
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004008template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00004009bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004010 do {
4011 if (Lex.getKind() != lltok::LabelStr)
4012 return TokError("expected field label here");
4013
4014 if (parseField())
4015 return true;
4016 } while (EatIfPresent(lltok::comma));
4017
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00004018 return false;
4019}
4020
4021template <class ParserTy>
4022bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
4023 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4024 Lex.Lex();
4025
4026 if (ParseToken(lltok::lparen, "expected '(' here"))
4027 return true;
4028 if (Lex.getKind() != lltok::rparen)
4029 if (ParseMDFieldsImplBody(parseField))
4030 return true;
4031
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +00004032 ClosingLoc = Lex.getLoc();
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004033 return ParseToken(lltok::rparen, "expected ')' here");
4034}
4035
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00004036template <class FieldTy>
4037bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
4038 if (Result.Seen)
4039 return TokError("field '" + Name + "' cannot be specified more than once");
4040
4041 LocTy Loc = Lex.getLoc();
4042 Lex.Lex();
4043 return ParseMDField(Loc, Name, Result);
4044}
4045
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004046bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
4047 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004048
4049#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004050 if (Lex.getStrVal() == #CLASS) \
4051 return Parse##CLASS(N, IsDistinct);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004052#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004053
4054 return TokError("expected metadata type");
4055}
4056
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00004057#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
4058#define NOP_FIELD(NAME, TYPE, INIT)
4059#define REQUIRE_FIELD(NAME, TYPE, INIT) \
4060 if (!NAME.Seen) \
4061 return Error(ClosingLoc, "missing required field '" #NAME "'");
4062#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00004063 if (Lex.getStrVal() == #NAME) \
4064 return ParseMDField(#NAME, NAME);
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00004065#define PARSE_MD_FIELDS() \
4066 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
4067 do { \
4068 LocTy ClosingLoc; \
4069 if (ParseMDFieldsImpl([&]() -> bool { \
4070 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
4071 return TokError(Twine("invalid field '") + Lex.getStrVal() + "'"); \
4072 }, ClosingLoc)) \
4073 return true; \
4074 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
4075 } while (false)
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00004076#define GET_OR_DISTINCT(CLASS, ARGS) \
4077 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004078
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004079/// ParseDILocationFields:
4080/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6)
4081bool LLParser::ParseDILocation(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00004082#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00004083 OPTIONAL(line, LineField, ); \
4084 OPTIONAL(column, ColumnField, ); \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00004085 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00004086 OPTIONAL(inlinedAt, MDField, );
4087 PARSE_MD_FIELDS();
4088#undef VISIT_MD_FIELDS
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004089
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +00004090 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004091 DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004092 return false;
4093}
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00004094
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004095/// ParseGenericDINode:
4096/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
4097bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00004098#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00004099 REQUIRED(tag, DwarfTagField, ); \
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00004100 OPTIONAL(header, MDStringField, ); \
4101 OPTIONAL(operands, MDFieldList, );
4102 PARSE_MD_FIELDS();
4103#undef VISIT_MD_FIELDS
4104
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004105 Result = GET_OR_DISTINCT(GenericDINode,
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00004106 (Context, tag.Val, header.Val, operands.Val));
4107 return false;
4108}
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004109
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004110/// ParseDISubrange:
4111/// ::= !DISubrange(count: 30, lowerBound: 2)
Sander de Smalenfdf40912018-01-24 09:56:07 +00004112/// ::= !DISubrange(count: !node, lowerBound: 2)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004113bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00004114#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Sander de Smalenfdf40912018-01-24 09:56:07 +00004115 REQUIRED(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00004116 OPTIONAL(lowerBound, MDSignedField, );
4117 PARSE_MD_FIELDS();
4118#undef VISIT_MD_FIELDS
4119
Sander de Smalenfdf40912018-01-24 09:56:07 +00004120 if (count.isMDSignedField())
4121 Result = GET_OR_DISTINCT(
4122 DISubrange, (Context, count.getMDSignedValue(), lowerBound.Val));
4123 else if (count.isMDField())
4124 Result = GET_OR_DISTINCT(
4125 DISubrange, (Context, count.getMDFieldValue(), lowerBound.Val));
4126 else
4127 return true;
4128
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00004129 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004130}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00004131
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004132/// ParseDIEnumerator:
Momchil Velikov08dc66e2018-02-12 16:10:09 +00004133/// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004134bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00004135#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smithcd8fb602015-02-18 21:16:33 +00004136 REQUIRED(name, MDStringField, ); \
Momchil Velikov08dc66e2018-02-12 16:10:09 +00004137 REQUIRED(value, MDSignedOrUnsignedField, ); \
4138 OPTIONAL(isUnsigned, MDBoolField, (false));
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00004139 PARSE_MD_FIELDS();
4140#undef VISIT_MD_FIELDS
4141
Momchil Velikov08dc66e2018-02-12 16:10:09 +00004142 if (isUnsigned.Val && value.isMDSignedField())
4143 return TokError("unsigned enumerator with negative value");
4144
4145 int64_t Value = value.isMDSignedField()
4146 ? value.getMDSignedValue()
4147 : static_cast<int64_t>(value.getMDUnsignedValue());
4148 Result =
4149 GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
4150
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00004151 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004152}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00004153
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004154/// ParseDIBasicType:
4155/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32)
4156bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00004157#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00004158 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00004159 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00004160 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
Victor Leschuk197aa312016-10-18 14:31:22 +00004161 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00004162 OPTIONAL(encoding, DwarfAttEncodingField, );
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00004163 PARSE_MD_FIELDS();
4164#undef VISIT_MD_FIELDS
4165
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004166 Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00004167 align.Val, encoding.Val));
4168 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004169}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00004170
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004171/// ParseDIDerivedType:
4172/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004173/// line: 7, scope: !1, baseType: !2, size: 32,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00004174/// align: 32, offset: 0, flags: 0, extraData: !3,
4175/// dwarfAddressSpace: 3)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004176bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004177#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4178 REQUIRED(tag, DwarfTagField, ); \
4179 OPTIONAL(name, MDStringField, ); \
4180 OPTIONAL(file, MDField, ); \
4181 OPTIONAL(line, LineField, ); \
4182 OPTIONAL(scope, MDField, ); \
4183 REQUIRED(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00004184 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
Victor Leschuk197aa312016-10-18 14:31:22 +00004185 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00004186 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00004187 OPTIONAL(flags, DIFlagField, ); \
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00004188 OPTIONAL(extraData, MDField, ); \
4189 OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004190 PARSE_MD_FIELDS();
4191#undef VISIT_MD_FIELDS
4192
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00004193 Optional<unsigned> DWARFAddressSpace;
4194 if (dwarfAddressSpace.Val != UINT32_MAX)
4195 DWARFAddressSpace = dwarfAddressSpace.Val;
4196
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004197 Result = GET_OR_DISTINCT(DIDerivedType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004198 (Context, tag.Val, name.Val, file.Val, line.Val,
4199 scope.Val, baseType.Val, size.Val, align.Val,
Konstantin Zhuravlyovd5561e02017-03-08 23:55:44 +00004200 offset.Val, DWARFAddressSpace, flags.Val,
4201 extraData.Val));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004202 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004203}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004204
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004205bool LLParser::ParseDICompositeType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004206#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4207 REQUIRED(tag, DwarfTagField, ); \
4208 OPTIONAL(name, MDStringField, ); \
4209 OPTIONAL(file, MDField, ); \
4210 OPTIONAL(line, LineField, ); \
4211 OPTIONAL(scope, MDField, ); \
4212 OPTIONAL(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00004213 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
Victor Leschuk197aa312016-10-18 14:31:22 +00004214 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00004215 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00004216 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004217 OPTIONAL(elements, MDField, ); \
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00004218 OPTIONAL(runtimeLang, DwarfLangField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004219 OPTIONAL(vtableHolder, MDField, ); \
4220 OPTIONAL(templateParams, MDField, ); \
Adrian Prantl8c599212018-02-06 23:45:59 +00004221 OPTIONAL(identifier, MDStringField, ); \
4222 OPTIONAL(discriminator, MDField, );
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004223 PARSE_MD_FIELDS();
4224#undef VISIT_MD_FIELDS
4225
Duncan P. N. Exon Smith97386022016-04-19 18:00:19 +00004226 // If this has an identifier try to build an ODR type.
4227 if (identifier.Val)
4228 if (auto *CT = DICompositeType::buildODRType(
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +00004229 Context, *identifier.Val, tag.Val, name.Val, file.Val, line.Val,
4230 scope.Val, baseType.Val, size.Val, align.Val, offset.Val, flags.Val,
4231 elements.Val, runtimeLang.Val, vtableHolder.Val,
Adrian Prantl8c599212018-02-06 23:45:59 +00004232 templateParams.Val, discriminator.Val)) {
Duncan P. N. Exon Smith0b0271e2016-04-19 14:55:09 +00004233 Result = CT;
4234 return false;
4235 }
Duncan P. N. Exon Smith5ab2be02016-04-17 03:58:21 +00004236
4237 // Create a new node, and save it in the context if it belongs in the type
4238 // map.
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004239 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004240 DICompositeType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004241 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
4242 size.Val, align.Val, offset.Val, flags.Val, elements.Val,
Adrian Prantl8c599212018-02-06 23:45:59 +00004243 runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val,
4244 discriminator.Val));
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004245 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004246}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00004247
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004248bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00004249#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00004250 OPTIONAL(flags, DIFlagField, ); \
Reid Klecknerde3d8b52016-06-08 20:34:29 +00004251 OPTIONAL(cc, DwarfCCField, ); \
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00004252 REQUIRED(types, MDField, );
4253 PARSE_MD_FIELDS();
4254#undef VISIT_MD_FIELDS
4255
Reid Klecknerde3d8b52016-06-08 20:34:29 +00004256 Result = GET_OR_DISTINCT(DISubroutineType,
4257 (Context, flags.Val, cc.Val, types.Val));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00004258 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004259}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00004260
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004261/// ParseDIFileType:
Scott Linder16c7bda2018-02-23 23:01:06 +00004262/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
Amjad Aboud7faeecc2016-12-25 10:12:09 +00004263/// checksumkind: CSK_MD5,
Scott Linder16c7bda2018-02-23 23:01:06 +00004264/// checksum: "000102030405060708090a0b0c0d0e0f",
4265/// source: "source file contents")
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004266bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
Scott Linder71603842018-02-12 19:45:54 +00004267 // The default constructed value for checksumkind is required, but will never
4268 // be used, as the parser checks if the field was actually Seen before using
4269 // the Val.
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00004270#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4271 REQUIRED(filename, MDStringField, ); \
Amjad Aboud7faeecc2016-12-25 10:12:09 +00004272 REQUIRED(directory, MDStringField, ); \
Scott Linder71603842018-02-12 19:45:54 +00004273 OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \
Scott Linder16c7bda2018-02-23 23:01:06 +00004274 OPTIONAL(checksum, MDStringField, ); \
4275 OPTIONAL(source, MDStringField, );
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00004276 PARSE_MD_FIELDS();
4277#undef VISIT_MD_FIELDS
4278
Scott Linder71603842018-02-12 19:45:54 +00004279 Optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
4280 if (checksumkind.Seen && checksum.Seen)
4281 OptChecksum.emplace(checksumkind.Val, checksum.Val);
4282 else if (checksumkind.Seen || checksum.Seen)
4283 return Lex.Error("'checksumkind' and 'checksum' must be provided together");
4284
Scott Linder16c7bda2018-02-23 23:01:06 +00004285 Optional<MDString *> OptSource;
4286 if (source.Seen)
4287 OptSource = source.Val;
Amjad Aboud7faeecc2016-12-25 10:12:09 +00004288 Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val,
Scott Linder16c7bda2018-02-23 23:01:06 +00004289 OptChecksum, OptSource));
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00004290 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004291}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00004292
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004293/// ParseDICompileUnit:
4294/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004295/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
Adrian Prantlb939a252016-03-31 23:56:58 +00004296/// splitDebugFilename: "abc.debug",
Adrian Prantl75819ae2016-04-15 15:57:41 +00004297/// emissionKind: FullDebug, enums: !1, retainedTypes: !2,
Amjad Abouda9bcf162015-12-10 12:56:35 +00004298/// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004299bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00004300 if (!IsDistinct)
4301 return Lex.Error("missing 'distinct', required for !DICompileUnit");
4302
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004303#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4304 REQUIRED(language, DwarfLangField, ); \
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +00004305 REQUIRED(file, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004306 OPTIONAL(producer, MDStringField, ); \
4307 OPTIONAL(isOptimized, MDBoolField, ); \
4308 OPTIONAL(flags, MDStringField, ); \
4309 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
4310 OPTIONAL(splitDebugFilename, MDStringField, ); \
Adrian Prantlb939a252016-03-31 23:56:58 +00004311 OPTIONAL(emissionKind, EmissionKindField, ); \
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004312 OPTIONAL(enums, MDField, ); \
4313 OPTIONAL(retainedTypes, MDField, ); \
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004314 OPTIONAL(globals, MDField, ); \
Adrian Prantl1f599f92015-05-21 20:37:30 +00004315 OPTIONAL(imports, MDField, ); \
Amjad Abouda9bcf162015-12-10 12:56:35 +00004316 OPTIONAL(macros, MDField, ); \
David Blaikiea01f2952016-08-24 18:29:49 +00004317 OPTIONAL(dwoId, MDUnsignedField, ); \
Dehao Chen0944a8c2017-02-01 22:45:09 +00004318 OPTIONAL(splitDebugInlining, MDBoolField, = true); \
Peter Collingbourneb52e2362017-09-12 21:50:41 +00004319 OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \
4320 OPTIONAL(gnuPubnames, MDBoolField, = false);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004321 PARSE_MD_FIELDS();
4322#undef VISIT_MD_FIELDS
4323
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +00004324 Result = DICompileUnit::getDistinct(
4325 Context, language.Val, file.Val, producer.Val, isOptimized.Val, flags.Val,
4326 runtimeVersion.Val, splitDebugFilename.Val, emissionKind.Val, enums.Val,
David Blaikiea01f2952016-08-24 18:29:49 +00004327 retainedTypes.Val, globals.Val, imports.Val, macros.Val, dwoId.Val,
Peter Collingbourneb52e2362017-09-12 21:50:41 +00004328 splitDebugInlining.Val, debugInfoForProfiling.Val, gnuPubnames.Val);
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004329 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004330}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00004331
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004332/// ParseDISubprogram:
4333/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004334/// file: !1, line: 7, type: !2, isLocal: false,
4335/// isDefinition: true, scopeLine: 8, containingType: !3,
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00004336/// virtuality: DW_VIRTUALTIY_pure_virtual,
Reid Klecknerb5af11d2016-07-01 02:41:21 +00004337/// virtualIndex: 10, thisAdjustment: 4, flags: 11,
Peter Collingbourned4bff302015-11-05 22:03:56 +00004338/// isOptimized: false, templateParams: !4, declaration: !5,
Adrian Prantl1d12b882017-04-26 22:56:44 +00004339/// variables: !6, thrownTypes: !7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004340bool LLParser::ParseDISubprogram(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith814b8e92015-08-28 20:26:49 +00004341 auto Loc = Lex.getLoc();
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004342#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4343 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00004344 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004345 OPTIONAL(linkageName, MDStringField, ); \
4346 OPTIONAL(file, MDField, ); \
4347 OPTIONAL(line, LineField, ); \
4348 OPTIONAL(type, MDField, ); \
4349 OPTIONAL(isLocal, MDBoolField, ); \
4350 OPTIONAL(isDefinition, MDBoolField, (true)); \
4351 OPTIONAL(scopeLine, LineField, ); \
4352 OPTIONAL(containingType, MDField, ); \
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00004353 OPTIONAL(virtuality, DwarfVirtualityField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004354 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
Reid Klecknerb5af11d2016-07-01 02:41:21 +00004355 OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00004356 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004357 OPTIONAL(isOptimized, MDBoolField, ); \
Adrian Prantl75819ae2016-04-15 15:57:41 +00004358 OPTIONAL(unit, MDField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004359 OPTIONAL(templateParams, MDField, ); \
4360 OPTIONAL(declaration, MDField, ); \
Adrian Prantl1d12b882017-04-26 22:56:44 +00004361 OPTIONAL(variables, MDField, ); \
4362 OPTIONAL(thrownTypes, MDField, );
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004363 PARSE_MD_FIELDS();
4364#undef VISIT_MD_FIELDS
4365
Duncan P. N. Exon Smith814b8e92015-08-28 20:26:49 +00004366 if (isDefinition.Val && !IsDistinct)
4367 return Lex.Error(
4368 Loc,
4369 "missing 'distinct', required for !DISubprogram when 'isDefinition'");
4370
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004371 Result = GET_OR_DISTINCT(
Adrian Prantl1d12b882017-04-26 22:56:44 +00004372 DISubprogram,
4373 (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
4374 type.Val, isLocal.Val, isDefinition.Val, scopeLine.Val,
4375 containingType.Val, virtuality.Val, virtualIndex.Val, thisAdjustment.Val,
4376 flags.Val, isOptimized.Val, unit.Val, templateParams.Val,
4377 declaration.Val, variables.Val, thrownTypes.Val));
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004378 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004379}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004380
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004381/// ParseDILexicalBlock:
4382/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
4383bool LLParser::ParseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00004384#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00004385 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00004386 OPTIONAL(file, MDField, ); \
4387 OPTIONAL(line, LineField, ); \
4388 OPTIONAL(column, ColumnField, );
4389 PARSE_MD_FIELDS();
4390#undef VISIT_MD_FIELDS
4391
4392 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004393 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00004394 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004395}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00004396
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004397/// ParseDILexicalBlockFile:
4398/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
4399bool LLParser::ParseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00004400#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00004401 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00004402 OPTIONAL(file, MDField, ); \
4403 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
4404 PARSE_MD_FIELDS();
4405#undef VISIT_MD_FIELDS
4406
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004407 Result = GET_OR_DISTINCT(DILexicalBlockFile,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00004408 (Context, scope.Val, file.Val, discriminator.Val));
4409 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004410}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00004411
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004412/// ParseDINamespace:
4413/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
4414bool LLParser::ParseDINamespace(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00004415#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4416 REQUIRED(scope, MDField, ); \
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00004417 OPTIONAL(name, MDStringField, ); \
Adrian Prantldbfda632016-11-03 19:42:02 +00004418 OPTIONAL(exportSymbols, MDBoolField, );
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00004419 PARSE_MD_FIELDS();
4420#undef VISIT_MD_FIELDS
4421
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004422 Result = GET_OR_DISTINCT(DINamespace,
Adrian Prantlfed4f392017-04-28 22:25:46 +00004423 (Context, scope.Val, name.Val, exportSymbols.Val));
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00004424 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004425}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00004426
Amjad Abouda9bcf162015-12-10 12:56:35 +00004427/// ParseDIMacro:
4428/// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value: "SomeValue")
4429bool LLParser::ParseDIMacro(MDNode *&Result, bool IsDistinct) {
4430#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4431 REQUIRED(type, DwarfMacinfoTypeField, ); \
Adrian Prantl58c19102016-12-22 00:29:00 +00004432 OPTIONAL(line, LineField, ); \
Amjad Abouda9bcf162015-12-10 12:56:35 +00004433 REQUIRED(name, MDStringField, ); \
4434 OPTIONAL(value, MDStringField, );
4435 PARSE_MD_FIELDS();
4436#undef VISIT_MD_FIELDS
4437
4438 Result = GET_OR_DISTINCT(DIMacro,
4439 (Context, type.Val, line.Val, name.Val, value.Val));
4440 return false;
4441}
4442
4443/// ParseDIMacroFile:
4444/// ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
4445bool LLParser::ParseDIMacroFile(MDNode *&Result, bool IsDistinct) {
4446#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4447 OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \
Adrian Prantl58c19102016-12-22 00:29:00 +00004448 OPTIONAL(line, LineField, ); \
Amjad Abouda9bcf162015-12-10 12:56:35 +00004449 REQUIRED(file, MDField, ); \
4450 OPTIONAL(nodes, MDField, );
4451 PARSE_MD_FIELDS();
4452#undef VISIT_MD_FIELDS
4453
4454 Result = GET_OR_DISTINCT(DIMacroFile,
4455 (Context, type.Val, line.Val, file.Val, nodes.Val));
4456 return false;
4457}
4458
Adrian Prantlab1243f2015-06-29 23:03:47 +00004459/// ParseDIModule:
4460/// ::= !DIModule(scope: !0, name: "SomeModule", configMacros: "-DNDEBUG",
4461/// includePath: "/usr/include", isysroot: "/")
4462bool LLParser::ParseDIModule(MDNode *&Result, bool IsDistinct) {
4463#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4464 REQUIRED(scope, MDField, ); \
4465 REQUIRED(name, MDStringField, ); \
4466 OPTIONAL(configMacros, MDStringField, ); \
4467 OPTIONAL(includePath, MDStringField, ); \
4468 OPTIONAL(isysroot, MDStringField, );
4469 PARSE_MD_FIELDS();
4470#undef VISIT_MD_FIELDS
4471
4472 Result = GET_OR_DISTINCT(DIModule, (Context, scope.Val, name.Val,
4473 configMacros.Val, includePath.Val, isysroot.Val));
4474 return false;
4475}
4476
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004477/// ParseDITemplateTypeParameter:
4478/// ::= !DITemplateTypeParameter(name: "Ty", type: !1)
4479bool LLParser::ParseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004480#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004481 OPTIONAL(name, MDStringField, ); \
4482 REQUIRED(type, MDField, );
4483 PARSE_MD_FIELDS();
4484#undef VISIT_MD_FIELDS
4485
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00004486 Result =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004487 GET_OR_DISTINCT(DITemplateTypeParameter, (Context, name.Val, type.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004488 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004489}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004490
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004491/// ParseDITemplateValueParameter:
4492/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00004493/// name: "V", type: !1, value: i32 7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004494bool LLParser::ParseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004495#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00004496 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004497 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00004498 OPTIONAL(type, MDField, ); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004499 REQUIRED(value, MDField, );
4500 PARSE_MD_FIELDS();
4501#undef VISIT_MD_FIELDS
4502
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004503 Result = GET_OR_DISTINCT(DITemplateValueParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00004504 (Context, tag.Val, name.Val, type.Val, value.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004505 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004506}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00004507
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004508/// ParseDIGlobalVariable:
4509/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004510/// file: !1, line: 7, type: !2, isLocal: false,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00004511/// isDefinition: true, declaration: !3, align: 8)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004512bool LLParser::ParseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004513#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00004514 REQUIRED(name, MDStringField, (/* AllowEmpty */ false)); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004515 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004516 OPTIONAL(linkageName, MDStringField, ); \
4517 OPTIONAL(file, MDField, ); \
4518 OPTIONAL(line, LineField, ); \
4519 OPTIONAL(type, MDField, ); \
4520 OPTIONAL(isLocal, MDBoolField, ); \
4521 OPTIONAL(isDefinition, MDBoolField, (true)); \
Victor Leschuk2ede1262016-10-20 00:13:12 +00004522 OPTIONAL(declaration, MDField, ); \
4523 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004524 PARSE_MD_FIELDS();
4525#undef VISIT_MD_FIELDS
4526
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004527 Result = GET_OR_DISTINCT(DIGlobalVariable,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004528 (Context, scope.Val, name.Val, linkageName.Val,
4529 file.Val, line.Val, type.Val, isLocal.Val,
Adrian Prantlbceaaa92016-12-20 02:09:43 +00004530 isDefinition.Val, declaration.Val, align.Val));
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004531 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004532}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00004533
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004534/// ParseDILocalVariable:
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00004535/// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
Victor Leschuk2ede1262016-10-20 00:13:12 +00004536/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
4537/// align: 8)
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00004538/// ::= !DILocalVariable(scope: !0, name: "foo",
Victor Leschuk2ede1262016-10-20 00:13:12 +00004539/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
4540/// align: 8)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004541bool LLParser::ParseDILocalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00004542#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00004543 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00004544 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00004545 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00004546 OPTIONAL(file, MDField, ); \
4547 OPTIONAL(line, LineField, ); \
4548 OPTIONAL(type, MDField, ); \
Victor Leschuk2ede1262016-10-20 00:13:12 +00004549 OPTIONAL(flags, DIFlagField, ); \
4550 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX));
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00004551 PARSE_MD_FIELDS();
4552#undef VISIT_MD_FIELDS
4553
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004554 Result = GET_OR_DISTINCT(DILocalVariable,
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00004555 (Context, scope.Val, name.Val, file.Val, line.Val,
Victor Leschuk2ede1262016-10-20 00:13:12 +00004556 type.Val, arg.Val, flags.Val, align.Val));
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00004557 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004558}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00004559
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004560/// ParseDIExpression:
4561/// ::= !DIExpression(0, 7, -1)
4562bool LLParser::ParseDIExpression(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00004563 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
4564 Lex.Lex();
4565
4566 if (ParseToken(lltok::lparen, "expected '(' here"))
4567 return true;
4568
4569 SmallVector<uint64_t, 8> Elements;
4570 if (Lex.getKind() != lltok::rparen)
4571 do {
4572 if (Lex.getKind() == lltok::DwarfOp) {
4573 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
4574 Lex.Lex();
4575 Elements.push_back(Op);
4576 continue;
4577 }
4578 return TokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
4579 }
4580
4581 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
4582 return TokError("expected unsigned integer");
4583
4584 auto &U = Lex.getAPSIntVal();
4585 if (U.ugt(UINT64_MAX))
4586 return TokError("element too large, limit is " + Twine(UINT64_MAX));
4587 Elements.push_back(U.getZExtValue());
4588 Lex.Lex();
4589 } while (EatIfPresent(lltok::comma));
4590
4591 if (ParseToken(lltok::rparen, "expected ')' here"))
4592 return true;
4593
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004594 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00004595 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004596}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00004597
Adrian Prantlbceaaa92016-12-20 02:09:43 +00004598/// ParseDIGlobalVariableExpression:
4599/// ::= !DIGlobalVariableExpression(var: !0, expr: !1)
4600bool LLParser::ParseDIGlobalVariableExpression(MDNode *&Result,
4601 bool IsDistinct) {
4602#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4603 REQUIRED(var, MDField, ); \
Adrian Prantl05782212017-08-30 18:06:51 +00004604 REQUIRED(expr, MDField, );
Adrian Prantlbceaaa92016-12-20 02:09:43 +00004605 PARSE_MD_FIELDS();
4606#undef VISIT_MD_FIELDS
4607
4608 Result =
4609 GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
4610 return false;
4611}
4612
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004613/// ParseDIObjCProperty:
4614/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00004615/// getter: "getFoo", attributes: 7, type: !2)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004616bool LLParser::ParseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00004617#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00004618 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00004619 OPTIONAL(file, MDField, ); \
4620 OPTIONAL(line, LineField, ); \
4621 OPTIONAL(setter, MDStringField, ); \
4622 OPTIONAL(getter, MDStringField, ); \
4623 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
4624 OPTIONAL(type, MDField, );
4625 PARSE_MD_FIELDS();
4626#undef VISIT_MD_FIELDS
4627
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004628 Result = GET_OR_DISTINCT(DIObjCProperty,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00004629 (Context, name.Val, file.Val, line.Val, setter.Val,
4630 getter.Val, attributes.Val, type.Val));
4631 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004632}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00004633
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004634/// ParseDIImportedEntity:
4635/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00004636/// line: 7, name: "foo")
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004637bool LLParser::ParseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00004638#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
4639 REQUIRED(tag, DwarfTagField, ); \
4640 REQUIRED(scope, MDField, ); \
4641 OPTIONAL(entity, MDField, ); \
Adrian Prantld63bfd22017-07-19 00:09:54 +00004642 OPTIONAL(file, MDField, ); \
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00004643 OPTIONAL(line, LineField, ); \
4644 OPTIONAL(name, MDStringField, );
4645 PARSE_MD_FIELDS();
4646#undef VISIT_MD_FIELDS
4647
Adrian Prantld63bfd22017-07-19 00:09:54 +00004648 Result = GET_OR_DISTINCT(
4649 DIImportedEntity,
4650 (Context, tag.Val, scope.Val, entity.Val, file.Val, line.Val, name.Val));
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00004651 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00004652}
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00004653
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004654#undef PARSE_MD_FIELD
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00004655#undef NOP_FIELD
4656#undef REQUIRE_FIELD
4657#undef DECLARE_FIELD
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004658
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004659/// ParseMetadataAsValue
4660/// ::= metadata i32 %local
4661/// ::= metadata i32 @global
4662/// ::= metadata i32 7
4663/// ::= metadata !0
4664/// ::= metadata !{...}
4665/// ::= metadata !"string"
4666bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
4667 // Note: the type 'metadata' has already been parsed.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004668 Metadata *MD;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004669 if (ParseMetadata(MD, &PFS))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004670 return true;
4671
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004672 V = MetadataAsValue::get(Context, MD);
4673 return false;
4674}
4675
4676/// ParseValueAsMetadata
4677/// ::= i32 %local
4678/// ::= i32 @global
4679/// ::= i32 7
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004680bool LLParser::ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
4681 PerFunctionState *PFS) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004682 Type *Ty;
4683 LocTy Loc;
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004684 if (ParseType(Ty, TypeMsg, Loc))
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004685 return true;
4686 if (Ty->isMetadataTy())
4687 return Error(Loc, "invalid metadata-value-metadata roundtrip");
4688
4689 Value *V;
4690 if (ParseValue(Ty, V, PFS))
4691 return true;
4692
4693 MD = ValueAsMetadata::get(V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004694 return false;
4695}
4696
4697/// ParseMetadata
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004698/// ::= i32 %local
4699/// ::= i32 @global
4700/// ::= i32 7
Dan Gohman8939ba332010-07-14 18:26:50 +00004701/// ::= !42
4702/// ::= !{...}
4703/// ::= !"string"
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00004704/// ::= !DILocation(...)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004705bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00004706 if (Lex.getKind() == lltok::MetadataVar) {
4707 MDNode *N;
4708 if (ParseSpecializedMDNode(N))
4709 return true;
4710 MD = N;
4711 return false;
4712 }
4713
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004714 // ValueAsMetadata:
4715 // <type> <value>
4716 if (Lex.getKind() != lltok::exclaim)
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00004717 return ParseValueAsMetadata(MD, "expected metadata operand", PFS);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00004718
4719 // '!'.
4720 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
4721 Lex.Lex();
Dan Gohman8939ba332010-07-14 18:26:50 +00004722
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00004723 // MDString:
4724 // ::= '!' STRINGCONSTANT
4725 if (Lex.getKind() == lltok::StringConstant) {
4726 MDString *S;
4727 if (ParseMDString(S))
4728 return true;
4729 MD = S;
4730 return false;
4731 }
4732
Dan Gohman8939ba332010-07-14 18:26:50 +00004733 // MDNode:
4734 // !{ ... }
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00004735 // !7
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00004736 MDNode *N;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00004737 if (ParseMDNodeTail(N))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00004738 return true;
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00004739 MD = N;
Dan Gohman8939ba332010-07-14 18:26:50 +00004740 return false;
4741}
4742
Victor Hernandez9d75c962010-01-11 22:31:58 +00004743//===----------------------------------------------------------------------===//
4744// Function Parsing.
4745//===----------------------------------------------------------------------===//
4746
Chris Lattner229907c2011-07-18 04:54:35 +00004747bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Alexander Richardsonc11ae182018-02-27 11:15:11 +00004748 PerFunctionState *PFS, bool IsCall) {
Duncan Sands19d0b472010-02-16 11:11:14 +00004749 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004750 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004751
Chris Lattnerac161bf2009-01-02 07:01:27 +00004752 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004753 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00004754 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
Alexander Richardsonc11ae182018-02-27 11:15:11 +00004755 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc, IsCall);
Craig Topper2617dcc2014-04-15 06:32:26 +00004756 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004757 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00004758 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
Alexander Richardsonc11ae182018-02-27 11:15:11 +00004759 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc, IsCall);
Craig Topper2617dcc2014-04-15 06:32:26 +00004760 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00004761 case ValID::t_InlineAsm: {
Karl Schimpf44876c52015-09-03 16:18:32 +00004762 if (!ID.FTy || !InlineAsm::Verify(ID.FTy, ID.StrVal2))
Victor Hernandez9d75c962010-01-11 22:31:58 +00004763 return Error(ID.Loc, "invalid type for inline asm constraint string");
David Blaikie41ba2b42015-07-27 23:32:19 +00004764 V = InlineAsm::get(ID.FTy, ID.StrVal, ID.StrVal2, ID.UIntVal & 1,
4765 (ID.UIntVal >> 1) & 1,
4766 (InlineAsm::AsmDialect(ID.UIntVal >> 2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00004767 return false;
4768 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004769 case ValID::t_GlobalName:
4770 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00004771 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004772 case ValID::t_GlobalID:
4773 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00004774 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004775 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00004776 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004777 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00004778 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00004779 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004780 return false;
4781 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00004782 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004783 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
4784 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004785
Dan Gohman518cda42011-12-17 00:04:22 +00004786 // The lexer has no type info, so builds all half, float, and double FP
4787 // constants as double. Fix this here. Long double does not need this.
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004788 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004789 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00004790 if (Ty->isHalfTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004791 ID.APFloatVal.convert(APFloat::IEEEhalf(), APFloat::rmNearestTiesToEven,
Dan Gohman518cda42011-12-17 00:04:22 +00004792 &Ignored);
4793 else if (Ty->isFloatTy())
Stephan Bergmann17c7f702016-12-14 11:57:17 +00004794 ID.APFloatVal.convert(APFloat::IEEEsingle(), APFloat::rmNearestTiesToEven,
Dan Gohman518cda42011-12-17 00:04:22 +00004795 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004796 }
Owen Anderson69c464d2009-07-27 20:59:43 +00004797 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004798
Chris Lattner8f57d29e2009-01-05 18:24:23 +00004799 if (V->getType() != Ty)
4800 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004801 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004802
Chris Lattnerac161bf2009-01-02 07:01:27 +00004803 return false;
4804 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00004805 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004806 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00004807 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004808 return false;
4809 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00004810 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004811 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00004812 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00004813 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004814 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00004815 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00004816 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00004817 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00004818 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00004819 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004820 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00004821 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00004822 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004823 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00004824 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004825 return false;
David Majnemerf0f224d2015-11-11 21:57:16 +00004826 case ValID::t_None:
4827 if (!Ty->isTokenTy())
4828 return Error(ID.Loc, "invalid type for none constant");
4829 V = Constant::getNullValue(Ty);
4830 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004831 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00004832 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004833 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00004834
Chris Lattnerac161bf2009-01-02 07:01:27 +00004835 V = ID.ConstantVal;
4836 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004837 case ValID::t_ConstantStruct:
4838 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00004839 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004840 if (ST->getNumElements() != ID.UIntVal)
4841 return Error(ID.Loc,
4842 "initializer with struct type has wrong # elements");
4843 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
4844 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004845
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004846 // Verify that the elements are compatible with the structtype.
4847 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
4848 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
4849 return Error(ID.Loc, "element " + Twine(i) +
4850 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004851
David Blaikieadbda4b2015-08-03 20:08:41 +00004852 V = ConstantStruct::get(
4853 ST, makeArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004854 } else
4855 return Error(ID.Loc, "constant expression type mismatch");
4856 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004857 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00004858 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004859}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004860
Alex Lorenzd2255952015-07-17 22:07:03 +00004861bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
4862 C = nullptr;
4863 ValID ID;
4864 auto Loc = Lex.getLoc();
4865 if (ParseValID(ID, /*PFS=*/nullptr))
4866 return true;
4867 switch (ID.Kind) {
4868 case ValID::t_APSInt:
4869 case ValID::t_APFloat:
Alex Lorenzb9a68db2015-09-09 13:44:33 +00004870 case ValID::t_Undef:
Alex Lorenzd2255952015-07-17 22:07:03 +00004871 case ValID::t_Constant:
4872 case ValID::t_ConstantStruct:
4873 case ValID::t_PackedConstantStruct: {
4874 Value *V;
Alexander Richardsonc11ae182018-02-27 11:15:11 +00004875 if (ConvertValIDToValue(Ty, ID, V, /*PFS=*/nullptr, /*IsCall=*/false))
Alex Lorenzd2255952015-07-17 22:07:03 +00004876 return true;
4877 assert(isa<Constant>(V) && "Expected a constant value");
4878 C = cast<Constant>(V);
4879 return false;
4880 }
Eric Christopherb9c56d12017-03-30 22:34:20 +00004881 case ValID::t_Null:
4882 C = Constant::getNullValue(Ty);
4883 return false;
Alex Lorenzd2255952015-07-17 22:07:03 +00004884 default:
4885 return Error(Loc, "expected a constant value");
4886 }
4887}
4888
David Majnemer8a1c45d2015-12-12 05:38:55 +00004889bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004890 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004891 ValID ID;
Alexander Richardsonc11ae182018-02-27 11:15:11 +00004892 return ParseValID(ID, PFS) ||
4893 ConvertValIDToValue(Ty, ID, V, PFS, /*IsCall=*/false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004894}
4895
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004896bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004897 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004898 return ParseType(Ty) ||
4899 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004900}
4901
Chris Lattner3ed871f2009-10-27 19:13:16 +00004902bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
4903 PerFunctionState &PFS) {
4904 Value *V;
4905 Loc = Lex.getLoc();
4906 if (ParseTypeAndValue(V, PFS)) return true;
4907 if (!isa<BasicBlock>(V))
4908 return Error(Loc, "expected a basic block");
4909 BB = cast<BasicBlock>(V);
4910 return false;
4911}
4912
Chris Lattnerac161bf2009-01-02 07:01:27 +00004913/// FunctionHeader
Sean Fertilec70d28b2017-10-26 15:00:26 +00004914/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
4915/// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
4916/// '(' ArgList ')' OptFuncAttrs OptSection OptionalAlign OptGC
4917/// OptionalPrefix OptionalPrologue OptPersonalityFn
Chris Lattnerac161bf2009-01-02 07:01:27 +00004918bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
4919 // Parse the linkage.
4920 LocTy LinkageLoc = Lex.getLoc();
4921 unsigned Linkage;
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00004922 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00004923 unsigned DLLStorageClass;
Sean Fertilec70d28b2017-10-26 15:00:26 +00004924 bool DSOLocal;
Bill Wendling50d27842012-10-15 20:35:56 +00004925 AttrBuilder RetAttrs;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004926 unsigned CC;
Rafael Espindola2615c9e2016-05-12 12:37:52 +00004927 bool HasLinkage;
Craig Topper2617dcc2014-04-15 06:32:26 +00004928 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004929 LocTy RetTypeLoc = Lex.getLoc();
Sean Fertilec70d28b2017-10-26 15:00:26 +00004930 if (ParseOptionalLinkage(Linkage, HasLinkage, Visibility, DLLStorageClass,
4931 DSOLocal) ||
Rafael Espindola2615c9e2016-05-12 12:37:52 +00004932 ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004933 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004934 return true;
4935
4936 // Verify that the linkage is ok.
4937 switch ((GlobalValue::LinkageTypes)Linkage) {
4938 case GlobalValue::ExternalLinkage:
4939 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00004940 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004941 if (isDefine)
4942 return Error(LinkageLoc, "invalid linkage for function definition");
4943 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00004944 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004945 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00004946 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00004947 case GlobalValue::LinkOnceAnyLinkage:
4948 case GlobalValue::LinkOnceODRLinkage:
4949 case GlobalValue::WeakAnyLinkage:
4950 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004951 if (!isDefine)
4952 return Error(LinkageLoc, "invalid linkage for function declaration");
4953 break;
4954 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00004955 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004956 return Error(LinkageLoc, "invalid function linkage type");
4957 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004958
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00004959 if (!isValidVisibilityForLinkage(Visibility, Linkage))
4960 return Error(LinkageLoc,
4961 "symbol with local linkage must have default visibility");
4962
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004963 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004964 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004965
Chris Lattnerac161bf2009-01-02 07:01:27 +00004966 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00004967
4968 std::string FunctionName;
4969 if (Lex.getKind() == lltok::GlobalVar) {
4970 FunctionName = Lex.getStrVal();
4971 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
4972 unsigned NameID = Lex.getUIntVal();
4973
4974 if (NameID != NumberedVals.size())
4975 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00004976 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00004977 } else {
4978 return TokError("expected function name");
4979 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004980
Chris Lattner3822f632009-01-02 08:05:26 +00004981 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004982
Chris Lattner3822f632009-01-02 08:05:26 +00004983 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004984 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004985
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004986 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004987 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00004988 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004989 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004990 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004991 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004992 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00004993 std::string GC;
Peter Collingbourne96efdd62016-06-14 21:01:22 +00004994 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
Craig Topper2617dcc2014-04-15 06:32:26 +00004995 Constant *Prefix = nullptr;
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004996 Constant *Prologue = nullptr;
David Majnemer7fddecc2015-06-17 20:52:32 +00004997 Constant *PersonalityFn = nullptr;
David Majnemerdad0a642014-06-27 18:19:56 +00004998 Comdat *C;
Chris Lattner3822f632009-01-02 08:05:26 +00004999
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005000 if (ParseArgumentList(ArgList, isVarArg) ||
Peter Collingbourne96efdd62016-06-14 21:01:22 +00005001 ParseOptionalUnnamedAddr(UnnamedAddr) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00005002 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00005003 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00005004 (EatIfPresent(lltok::kw_section) &&
5005 ParseStringConstant(Section)) ||
Rafael Espindola83a362c2015-01-06 22:55:16 +00005006 parseOptionalComdat(FunctionName, C) ||
Chris Lattner3822f632009-01-02 08:05:26 +00005007 ParseOptionalAlignment(Alignment) ||
5008 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00005009 ParseStringConstant(GC)) ||
5010 (EatIfPresent(lltok::kw_prefix) &&
Peter Collingbourne51d2de72014-12-03 02:08:38 +00005011 ParseGlobalTypeAndValue(Prefix)) ||
5012 (EatIfPresent(lltok::kw_prologue) &&
David Majnemer7fddecc2015-06-17 20:52:32 +00005013 ParseGlobalTypeAndValue(Prologue)) ||
5014 (EatIfPresent(lltok::kw_personality) &&
5015 ParseGlobalTypeAndValue(PersonalityFn)))
Chris Lattner3822f632009-01-02 08:05:26 +00005016 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005017
Michael Gottesman41748d72013-06-27 00:25:01 +00005018 if (FuncAttrs.contains(Attribute::Builtin))
5019 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00005020
Chris Lattnerac161bf2009-01-02 07:01:27 +00005021 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00005022 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00005023 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005024 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005025 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005026
Chris Lattnerac161bf2009-01-02 07:01:27 +00005027 // Okay, if we got here, the function is syntactically valid. Convert types
5028 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00005029 std::vector<Type*> ParamTypeList;
Reid Klecknerc2cb5602017-04-12 00:38:00 +00005030 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005031
Chris Lattnerac161bf2009-01-02 07:01:27 +00005032 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005033 ParamTypeList.push_back(ArgList[i].Ty);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00005034 Attrs.push_back(ArgList[i].Attrs);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005035 }
5036
Reid Kleckner7f720332017-04-13 00:58:09 +00005037 AttributeList PAL =
5038 AttributeList::get(Context, AttributeSet::get(Context, FuncAttrs),
5039 AttributeSet::get(Context, RetAttrs), Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005040
Bill Wendling749a43d2012-12-30 13:50:49 +00005041 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005042 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
5043
Chris Lattner229907c2011-07-18 04:54:35 +00005044 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00005045 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00005046 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005047
Craig Topper2617dcc2014-04-15 06:32:26 +00005048 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005049 if (!FunctionName.empty()) {
5050 // If this was a definition of a forward reference, remove the definition
5051 // from the forward reference table and fill in the forward ref.
David Blaikie9ebdc692015-09-21 21:07:50 +00005052 auto FRVI = ForwardRefVals.find(FunctionName);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005053 if (FRVI != ForwardRefVals.end()) {
5054 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00005055 if (!Fn)
5056 return Error(FRVI->second.second, "invalid forward reference to "
5057 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00005058 if (Fn->getType() != PFT)
5059 return Error(FRVI->second.second, "invalid forward reference to "
5060 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005061
Chris Lattnerac161bf2009-01-02 07:01:27 +00005062 ForwardRefVals.erase(FRVI);
5063 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00005064 // Reject redefinitions.
5065 return Error(NameLoc, "invalid redefinition of function '" +
5066 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00005067 } else if (M->getNamedValue(FunctionName)) {
5068 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005069 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005070
Dan Gohman399d6ae2009-08-29 23:37:49 +00005071 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005072 // If this is a definition of a forward referenced function, make sure the
5073 // types agree.
David Blaikie9ebdc692015-09-21 21:07:50 +00005074 auto I = ForwardRefValIDs.find(NumberedVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00005075 if (I != ForwardRefValIDs.end()) {
5076 Fn = cast<Function>(I->second.first);
5077 if (Fn->getType() != PFT)
5078 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00005079 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005080 ForwardRefValIDs.erase(I);
5081 }
5082 }
5083
Craig Topper2617dcc2014-04-15 06:32:26 +00005084 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005085 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
5086 else // Move the forward-reference to the correct spot in the module.
5087 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
5088
5089 if (FunctionName.empty())
5090 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005091
Chris Lattnerac161bf2009-01-02 07:01:27 +00005092 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
Rafael Espindolae4b02312018-01-11 22:15:05 +00005093 maybeSetDSOLocal(DSOLocal, *Fn);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005094 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00005095 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005096 Fn->setCallingConv(CC);
5097 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00005098 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005099 Fn->setAlignment(Alignment);
5100 Fn->setSection(Section);
David Majnemerdad0a642014-06-27 18:19:56 +00005101 Fn->setComdat(C);
David Majnemer7fddecc2015-06-17 20:52:32 +00005102 Fn->setPersonalityFn(PersonalityFn);
Benjamin Kramer728f4442016-05-29 10:46:35 +00005103 if (!GC.empty()) Fn->setGC(GC);
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00005104 Fn->setPrefixData(Prefix);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00005105 Fn->setPrologueData(Prologue);
Bill Wendlingb32b0412013-02-08 06:32:06 +00005106 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005107
Chris Lattnerac161bf2009-01-02 07:01:27 +00005108 // Add all of the arguments we parsed to the function.
5109 Function::arg_iterator ArgIt = Fn->arg_begin();
5110 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
5111 // If the argument has a name, insert it into the argument symbol table.
5112 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005113
Chris Lattnerac161bf2009-01-02 07:01:27 +00005114 // Set the name, if it conflicted, it will be auto-renamed.
5115 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005116
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00005117 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005118 return Error(ArgList[i].Loc, "redefinition of argument '%" +
5119 ArgList[i].Name + "'");
5120 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005121
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00005122 if (isDefine)
5123 return false;
5124
Robin Morisset039781e2014-08-29 21:53:01 +00005125 // Check the declaration has no block address forward references.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00005126 ValID ID;
5127 if (FunctionName.empty()) {
5128 ID.Kind = ValID::t_GlobalID;
5129 ID.UIntVal = NumberedVals.size() - 1;
5130 } else {
5131 ID.Kind = ValID::t_GlobalName;
5132 ID.StrVal = FunctionName;
5133 }
5134 auto Blocks = ForwardRefBlockAddresses.find(ID);
5135 if (Blocks != ForwardRefBlockAddresses.end())
5136 return Error(Blocks->first.Loc,
5137 "cannot take blockaddress inside a declaration");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005138 return false;
5139}
5140
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00005141bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
5142 ValID ID;
5143 if (FunctionNumber == -1) {
5144 ID.Kind = ValID::t_GlobalName;
5145 ID.StrVal = F.getName();
5146 } else {
5147 ID.Kind = ValID::t_GlobalID;
5148 ID.UIntVal = FunctionNumber;
5149 }
5150
5151 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
5152 if (Blocks == P.ForwardRefBlockAddresses.end())
5153 return false;
5154
5155 for (const auto &I : Blocks->second) {
5156 const ValID &BBID = I.first;
5157 GlobalValue *GV = I.second;
5158
5159 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
5160 "Expected local id or name");
5161 BasicBlock *BB;
5162 if (BBID.Kind == ValID::t_LocalName)
5163 BB = GetBB(BBID.StrVal, BBID.Loc);
5164 else
5165 BB = GetBB(BBID.UIntVal, BBID.Loc);
5166 if (!BB)
5167 return P.Error(BBID.Loc, "referenced value is not a basic block");
5168
5169 GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
5170 GV->eraseFromParent();
5171 }
5172
5173 P.ForwardRefBlockAddresses.erase(Blocks);
5174 return false;
5175}
Chris Lattnerac161bf2009-01-02 07:01:27 +00005176
5177/// ParseFunctionBody
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00005178/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00005179bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00005180 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005181 return TokError("expected '{' in function body");
5182 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005183
Chris Lattner3432c622009-10-28 03:39:23 +00005184 int FunctionNumber = -1;
5185 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005186
Chris Lattner3432c622009-10-28 03:39:23 +00005187 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005188
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00005189 // Resolve block addresses and allow basic blocks to be forward-declared
5190 // within this function.
5191 if (PFS.resolveForwardRefBlockAddresses())
5192 return true;
5193 SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
5194
Chris Lattnerbbddd962010-01-09 19:20:07 +00005195 // We need at least one basic block.
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00005196 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
Chris Lattnerbbddd962010-01-09 19:20:07 +00005197 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005198
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00005199 while (Lex.getKind() != lltok::rbrace &&
5200 Lex.getKind() != lltok::kw_uselistorder)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005201 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005202
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00005203 while (Lex.getKind() != lltok::rbrace)
5204 if (ParseUseListOrder(&PFS))
5205 return true;
5206
Chris Lattnerac161bf2009-01-02 07:01:27 +00005207 // Eat the }.
5208 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005209
Chris Lattnerac161bf2009-01-02 07:01:27 +00005210 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00005211 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00005212}
5213
5214/// ParseBasicBlock
5215/// ::= LabelStr? Instruction*
5216bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
5217 // If this basic block starts out with a name, remember it.
5218 std::string Name;
5219 LocTy NameLoc = Lex.getLoc();
5220 if (Lex.getKind() == lltok::LabelStr) {
5221 Name = Lex.getStrVal();
5222 Lex.Lex();
5223 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005224
Chris Lattnerac161bf2009-01-02 07:01:27 +00005225 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Owen Anderson576a9a22015-03-02 05:25:09 +00005226 if (!BB)
5227 return Error(NameLoc,
5228 "unable to create block named '" + Name + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005229
Chris Lattnerac161bf2009-01-02 07:01:27 +00005230 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005231
Chris Lattnerac161bf2009-01-02 07:01:27 +00005232 // Parse the instructions in this block until we get a terminator.
5233 Instruction *Inst;
5234 do {
5235 // This instruction may have three possibilities for a name: a) none
5236 // specified, b) name specified "%foo =", c) number specified: "%4 =".
5237 LocTy NameLoc = Lex.getLoc();
5238 int NameID = -1;
5239 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005240
Chris Lattnerac161bf2009-01-02 07:01:27 +00005241 if (Lex.getKind() == lltok::LocalVarID) {
5242 NameID = Lex.getUIntVal();
5243 Lex.Lex();
5244 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
5245 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00005246 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005247 NameStr = Lex.getStrVal();
5248 Lex.Lex();
5249 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
5250 return true;
5251 }
Devang Patelea8a4b92009-09-17 23:04:48 +00005252
Chris Lattner77b89dc2009-12-30 05:23:43 +00005253 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00005254 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00005255 case InstError: return true;
5256 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00005257 BB->getInstList().push_back(Inst);
5258
Chris Lattner77b89dc2009-12-30 05:23:43 +00005259 // With a normal result, we check to see if the instruction is followed by
5260 // a comma and metadata.
5261 if (EatIfPresent(lltok::comma))
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00005262 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00005263 return true;
5264 break;
5265 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00005266 BB->getInstList().push_back(Inst);
5267
Chris Lattner77b89dc2009-12-30 05:23:43 +00005268 // If the instruction parser ate an extra comma at the end of it, it
5269 // *must* be followed by metadata.
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00005270 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00005271 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005272 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00005273 }
Devang Patelea8a4b92009-09-17 23:04:48 +00005274
Chris Lattnerac161bf2009-01-02 07:01:27 +00005275 // Set the name on the instruction.
5276 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
5277 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005278
Chris Lattnerac161bf2009-01-02 07:01:27 +00005279 return false;
5280}
5281
5282//===----------------------------------------------------------------------===//
5283// Instruction Parsing.
5284//===----------------------------------------------------------------------===//
5285
5286/// ParseInstruction - Parse one of the many different instructions.
5287///
Chris Lattner77b89dc2009-12-30 05:23:43 +00005288int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
5289 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005290 lltok::Kind Token = Lex.getKind();
5291 if (Token == lltok::Eof)
5292 return TokError("found end of file when expecting more instructions");
5293 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00005294 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00005295 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005296
Chris Lattnerac161bf2009-01-02 07:01:27 +00005297 switch (Token) {
5298 default: return Error(Loc, "expected instruction opcode");
5299 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00005300 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005301 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
5302 case lltok::kw_br: return ParseBr(Inst, PFS);
5303 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00005304 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005305 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00005306 case lltok::kw_resume: return ParseResume(Inst, PFS);
David Majnemer654e1302015-07-31 17:58:14 +00005307 case lltok::kw_cleanupret: return ParseCleanupRet(Inst, PFS);
5308 case lltok::kw_catchret: return ParseCatchRet(Inst, PFS);
David Majnemer8a1c45d2015-12-12 05:38:55 +00005309 case lltok::kw_catchswitch: return ParseCatchSwitch(Inst, PFS);
5310 case lltok::kw_catchpad: return ParseCatchPad(Inst, PFS);
David Majnemer8a1c45d2015-12-12 05:38:55 +00005311 case lltok::kw_cleanuppad: return ParseCleanupPad(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005312 // Binary Operators.
5313 case lltok::kw_add:
5314 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00005315 case lltok::kw_mul:
5316 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00005317 bool NUW = EatIfPresent(lltok::kw_nuw);
5318 bool NSW = EatIfPresent(lltok::kw_nsw);
5319 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005320
Chris Lattnera676c0f2011-02-07 16:40:21 +00005321 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005322
Chris Lattnera676c0f2011-02-07 16:40:21 +00005323 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
5324 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
5325 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00005326 }
Dan Gohmana5b96452009-06-04 22:49:04 +00005327 case lltok::kw_fadd:
5328 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00005329 case lltok::kw_fmul:
5330 case lltok::kw_fdiv:
5331 case lltok::kw_frem: {
5332 FastMathFlags FMF = EatFastMathFlagsIfPresent();
5333 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
5334 if (Res != 0)
5335 return Res;
5336 if (FMF.any())
5337 Inst->setFastMathFlags(FMF);
5338 return 0;
5339 }
Dan Gohmana5b96452009-06-04 22:49:04 +00005340
Chris Lattner35315d02011-02-06 21:44:57 +00005341 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00005342 case lltok::kw_udiv:
5343 case lltok::kw_lshr:
5344 case lltok::kw_ashr: {
5345 bool Exact = EatIfPresent(lltok::kw_exact);
5346
5347 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
5348 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
5349 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00005350 }
5351
Chris Lattnerac161bf2009-01-02 07:01:27 +00005352 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00005353 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005354 case lltok::kw_and:
5355 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00005356 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
James Molloy88eb5352015-07-10 12:52:00 +00005357 case lltok::kw_icmp: return ParseCompare(Inst, PFS, KeywordVal);
5358 case lltok::kw_fcmp: {
5359 FastMathFlags FMF = EatFastMathFlagsIfPresent();
5360 int Res = ParseCompare(Inst, PFS, KeywordVal);
5361 if (Res != 0)
5362 return Res;
5363 if (FMF.any())
5364 Inst->setFastMathFlags(FMF);
5365 return 0;
5366 }
5367
Chris Lattnerac161bf2009-01-02 07:01:27 +00005368 // Casts.
5369 case lltok::kw_trunc:
5370 case lltok::kw_zext:
5371 case lltok::kw_sext:
5372 case lltok::kw_fptrunc:
5373 case lltok::kw_fpext:
5374 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00005375 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00005376 case lltok::kw_uitofp:
5377 case lltok::kw_sitofp:
5378 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005379 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00005380 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00005381 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005382 // Other.
5383 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00005384 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005385 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
5386 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
5387 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
5388 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00005389 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00005390 // Call.
5391 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
5392 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
5393 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Akira Hatanaka5cfcce122015-11-06 23:55:38 +00005394 case lltok::kw_notail: return ParseCall(Inst, PFS, CallInst::TCK_NoTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005395 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00005396 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00005397 case lltok::kw_load: return ParseLoad(Inst, PFS);
5398 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00005399 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
5400 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00005401 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005402 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
5403 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
5404 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
5405 }
5406}
5407
5408/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
5409bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00005410 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005411 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00005412 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005413 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
5414 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
5415 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
5416 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
5417 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
5418 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
5419 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
5420 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
5421 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
5422 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
5423 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
5424 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
5425 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
5426 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
5427 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
5428 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
5429 }
5430 } else {
5431 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00005432 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005433 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
5434 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
5435 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
5436 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
5437 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
5438 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
5439 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
5440 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
5441 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
5442 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
5443 }
5444 }
5445 Lex.Lex();
5446 return false;
5447}
5448
5449//===----------------------------------------------------------------------===//
5450// Terminator Instructions.
5451//===----------------------------------------------------------------------===//
5452
5453/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00005454/// ::= 'ret' void (',' !dbg, !1)*
5455/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00005456bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005457 PerFunctionState &PFS) {
5458 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00005459 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00005460 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005461
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005462 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005463
Chris Lattnerfdd87902009-10-05 05:54:46 +00005464 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005465 if (!ResType->isVoidTy())
5466 return Error(TypeLoc, "value doesn't match function result type '" +
5467 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005468
Owen Anderson55f1c092009-08-13 21:58:54 +00005469 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005470 return false;
5471 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005472
Chris Lattnerac161bf2009-01-02 07:01:27 +00005473 Value *RV;
5474 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005475
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005476 if (ResType != RV->getType())
5477 return Error(TypeLoc, "value doesn't match function result type '" +
5478 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005479
Owen Anderson55f1c092009-08-13 21:58:54 +00005480 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00005481 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005482}
5483
Chris Lattnerac161bf2009-01-02 07:01:27 +00005484/// ParseBr
5485/// ::= 'br' TypeAndValue
5486/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5487bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
5488 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00005489 Value *Op0;
5490 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005491 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005492
Chris Lattnerac161bf2009-01-02 07:01:27 +00005493 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
5494 Inst = BranchInst::Create(BB);
5495 return false;
5496 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005497
Owen Anderson55f1c092009-08-13 21:58:54 +00005498 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005499 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005500
Chris Lattnerac161bf2009-01-02 07:01:27 +00005501 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00005502 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005503 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00005504 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005505 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005506
Chris Lattner3ed871f2009-10-27 19:13:16 +00005507 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005508 return false;
5509}
5510
5511/// ParseSwitch
5512/// Instruction
5513/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
5514/// JumpTable
5515/// ::= (TypeAndValue ',' TypeAndValue)*
5516bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
5517 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00005518 Value *Cond;
5519 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005520 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
5521 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00005522 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005523 ParseToken(lltok::lsquare, "expected '[' with switch table"))
5524 return true;
5525
Duncan Sands19d0b472010-02-16 11:11:14 +00005526 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005527 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005528
Chris Lattnerac161bf2009-01-02 07:01:27 +00005529 // Parse the jump table pairs.
5530 SmallPtrSet<Value*, 32> SeenCases;
5531 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
5532 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00005533 Value *Constant;
5534 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005535
Chris Lattnerac161bf2009-01-02 07:01:27 +00005536 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
5537 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00005538 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005539 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005540
David Blaikie70573dc2014-11-19 07:49:26 +00005541 if (!SeenCases.insert(Constant).second)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005542 return Error(CondLoc, "duplicate case value in switch");
5543 if (!isa<ConstantInt>(Constant))
5544 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005545
Chris Lattner3ed871f2009-10-27 19:13:16 +00005546 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00005547 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005548
Chris Lattnerac161bf2009-01-02 07:01:27 +00005549 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005550
Chris Lattner3ed871f2009-10-27 19:13:16 +00005551 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00005552 for (unsigned i = 0, e = Table.size(); i != e; ++i)
5553 SI->addCase(Table[i].first, Table[i].second);
5554 Inst = SI;
5555 return false;
5556}
5557
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00005558/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00005559/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00005560/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
5561bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00005562 LocTy AddrLoc;
5563 Value *Address;
5564 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00005565 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
5566 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00005567 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005568
Duncan Sands19d0b472010-02-16 11:11:14 +00005569 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00005570 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005571
Chris Lattner3ed871f2009-10-27 19:13:16 +00005572 // Parse the destination list.
5573 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005574
Chris Lattner3ed871f2009-10-27 19:13:16 +00005575 if (Lex.getKind() != lltok::rsquare) {
5576 BasicBlock *DestBB;
5577 if (ParseTypeAndBasicBlock(DestBB, PFS))
5578 return true;
5579 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005580
Chris Lattner3ed871f2009-10-27 19:13:16 +00005581 while (EatIfPresent(lltok::comma)) {
5582 if (ParseTypeAndBasicBlock(DestBB, PFS))
5583 return true;
5584 DestList.push_back(DestBB);
5585 }
5586 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005587
Chris Lattner3ed871f2009-10-27 19:13:16 +00005588 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
5589 return true;
5590
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00005591 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00005592 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
5593 IBI->addDestination(DestList[i]);
5594 Inst = IBI;
5595 return false;
5596}
5597
Chris Lattnerac161bf2009-01-02 07:01:27 +00005598/// ParseInvoke
5599/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
5600/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
5601bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
5602 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00005603 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00005604 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00005605 LocTy NoBuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00005606 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00005607 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005608 LocTy RetTypeLoc;
5609 ValID CalleeID;
5610 SmallVector<ParamInfo, 16> ArgList;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005611 SmallVector<OperandBundleDef, 2> BundleList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005612
Chris Lattner3ed871f2009-10-27 19:13:16 +00005613 BasicBlock *NormalBB, *UnwindBB;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005614 if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00005615 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005616 ParseValID(CalleeID) || ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00005617 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
5618 NoBuiltinLoc) ||
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005619 ParseOptionalOperandBundles(BundleList, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005620 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00005621 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005622 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00005623 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005624 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005625
Chris Lattnerac161bf2009-01-02 07:01:27 +00005626 // If RetType is a non-function pointer type, then this is the short syntax
5627 // for the call, which means that RetType is just the return type. Infer the
5628 // rest of the function argument types from the arguments that are present.
David Blaikie445e3fb2015-04-24 19:32:54 +00005629 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
5630 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005631 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00005632 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005633 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
5634 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005635
Chris Lattnerac161bf2009-01-02 07:01:27 +00005636 if (!FunctionType::isValidReturnType(RetType))
5637 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005638
Owen Anderson4056ca92009-07-29 22:17:13 +00005639 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005640 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005641
David Blaikie41ba2b42015-07-27 23:32:19 +00005642 CalleeID.FTy = Ty;
5643
Chris Lattnerac161bf2009-01-02 07:01:27 +00005644 // Look up the callee.
5645 Value *Callee;
Alexander Richardsonc11ae182018-02-27 11:15:11 +00005646 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS,
5647 /*IsCall=*/true))
David Blaikie445e3fb2015-04-24 19:32:54 +00005648 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005649
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005650 // Set up the Attribute for the function.
Reid Kleckner7f720332017-04-13 00:58:09 +00005651 SmallVector<Value *, 8> Args;
5652 SmallVector<AttributeSet, 8> ArgAttrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005653
Chris Lattnerac161bf2009-01-02 07:01:27 +00005654 // Loop through FunctionType's arguments and ensure they are specified
5655 // correctly. Also, gather any parameter attributes.
5656 FunctionType::param_iterator I = Ty->param_begin();
5657 FunctionType::param_iterator E = Ty->param_end();
5658 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005659 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005660 if (I != E) {
5661 ExpectedTy = *I++;
5662 } else if (!Ty->isVarArg()) {
5663 return Error(ArgList[i].Loc, "too many arguments specified");
5664 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005665
Chris Lattnerac161bf2009-01-02 07:01:27 +00005666 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
5667 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00005668 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005669 Args.push_back(ArgList[i].V);
Reid Kleckner7f720332017-04-13 00:58:09 +00005670 ArgAttrs.push_back(ArgList[i].Attrs);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005671 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005672
Chris Lattnerac161bf2009-01-02 07:01:27 +00005673 if (I != E)
5674 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005675
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00005676 if (FnAttrs.hasAlignmentAttr())
5677 return Error(CallLoc, "invoke instructions may not have an alignment");
David Majnemer8d22abd2015-02-23 00:01:32 +00005678
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005679 // Finish off the Attribute and check them
Reid Kleckner7f720332017-04-13 00:58:09 +00005680 AttributeList PAL =
5681 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
5682 AttributeSet::get(Context, RetAttrs), ArgAttrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005683
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00005684 InvokeInst *II =
5685 InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args, BundleList);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005686 II->setCallingConv(CC);
5687 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00005688 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005689 Inst = II;
5690 return false;
5691}
5692
Bill Wendlingf891bf82011-07-31 06:30:59 +00005693/// ParseResume
5694/// ::= 'resume' TypeAndValue
5695bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
5696 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00005697 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
5698 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005699
Bill Wendlingf891bf82011-07-31 06:30:59 +00005700 ResumeInst *RI = ResumeInst::Create(Exn);
5701 Inst = RI;
5702 return false;
5703}
Chris Lattnerac161bf2009-01-02 07:01:27 +00005704
David Majnemer654e1302015-07-31 17:58:14 +00005705bool LLParser::ParseExceptionArgs(SmallVectorImpl<Value *> &Args,
5706 PerFunctionState &PFS) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00005707 if (ParseToken(lltok::lsquare, "expected '[' in catchpad/cleanuppad"))
David Majnemer654e1302015-07-31 17:58:14 +00005708 return true;
5709
5710 while (Lex.getKind() != lltok::rsquare) {
5711 // If this isn't the first argument, we need a comma.
5712 if (!Args.empty() &&
5713 ParseToken(lltok::comma, "expected ',' in argument list"))
5714 return true;
5715
5716 // Parse the argument.
5717 LocTy ArgLoc;
5718 Type *ArgTy = nullptr;
5719 if (ParseType(ArgTy, ArgLoc))
5720 return true;
5721
5722 Value *V;
5723 if (ArgTy->isMetadataTy()) {
5724 if (ParseMetadataAsValue(V, PFS))
5725 return true;
5726 } else {
5727 if (ParseValue(ArgTy, V, PFS))
5728 return true;
5729 }
5730 Args.push_back(V);
5731 }
5732
5733 Lex.Lex(); // Lex the ']'.
5734 return false;
5735}
5736
5737/// ParseCleanupRet
David Majnemer8a1c45d2015-12-12 05:38:55 +00005738/// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
David Majnemer654e1302015-07-31 17:58:14 +00005739bool LLParser::ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00005740 Value *CleanupPad = nullptr;
David Majnemer654e1302015-07-31 17:58:14 +00005741
David Majnemer8a1c45d2015-12-12 05:38:55 +00005742 if (ParseToken(lltok::kw_from, "expected 'from' after cleanupret"))
5743 return true;
5744
5745 if (ParseValue(Type::getTokenTy(Context), CleanupPad, PFS))
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00005746 return true;
David Majnemer654e1302015-07-31 17:58:14 +00005747
5748 if (ParseToken(lltok::kw_unwind, "expected 'unwind' in cleanupret"))
5749 return true;
5750
5751 BasicBlock *UnwindBB = nullptr;
5752 if (Lex.getKind() == lltok::kw_to) {
5753 Lex.Lex();
5754 if (ParseToken(lltok::kw_caller, "expected 'caller' in cleanupret"))
5755 return true;
5756 } else {
5757 if (ParseTypeAndBasicBlock(UnwindBB, PFS)) {
5758 return true;
5759 }
5760 }
5761
David Majnemer8a1c45d2015-12-12 05:38:55 +00005762 Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
David Majnemer654e1302015-07-31 17:58:14 +00005763 return false;
5764}
5765
5766/// ParseCatchRet
David Majnemer8a1c45d2015-12-12 05:38:55 +00005767/// ::= 'catchret' from Parent Value 'to' TypeAndValue
David Majnemer654e1302015-07-31 17:58:14 +00005768bool LLParser::ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00005769 Value *CatchPad = nullptr;
David Majnemer0bc0eef2015-08-15 02:46:08 +00005770
David Majnemer8a1c45d2015-12-12 05:38:55 +00005771 if (ParseToken(lltok::kw_from, "expected 'from' after catchret"))
5772 return true;
5773
5774 if (ParseValue(Type::getTokenTy(Context), CatchPad, PFS))
David Majnemer0bc0eef2015-08-15 02:46:08 +00005775 return true;
5776
David Majnemer0bc0eef2015-08-15 02:46:08 +00005777 BasicBlock *BB;
5778 if (ParseToken(lltok::kw_to, "expected 'to' in catchret") ||
5779 ParseTypeAndBasicBlock(BB, PFS))
5780 return true;
5781
David Majnemer8a1c45d2015-12-12 05:38:55 +00005782 Inst = CatchReturnInst::Create(CatchPad, BB);
5783 return false;
5784}
5785
5786/// ParseCatchSwitch
5787/// ::= 'catchswitch' within Parent
5788bool LLParser::ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
5789 Value *ParentPad;
David Majnemer8a1c45d2015-12-12 05:38:55 +00005790
5791 if (ParseToken(lltok::kw_within, "expected 'within' after catchswitch"))
5792 return true;
5793
5794 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
5795 Lex.getKind() != lltok::LocalVarID)
5796 return TokError("expected scope value for catchswitch");
5797
5798 if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
5799 return true;
5800
5801 if (ParseToken(lltok::lsquare, "expected '[' with catchswitch labels"))
5802 return true;
5803
5804 SmallVector<BasicBlock *, 32> Table;
5805 do {
5806 BasicBlock *DestBB;
5807 if (ParseTypeAndBasicBlock(DestBB, PFS))
5808 return true;
5809 Table.push_back(DestBB);
5810 } while (EatIfPresent(lltok::comma));
5811
5812 if (ParseToken(lltok::rsquare, "expected ']' after catchswitch labels"))
5813 return true;
5814
5815 if (ParseToken(lltok::kw_unwind,
5816 "expected 'unwind' after catchswitch scope"))
5817 return true;
5818
5819 BasicBlock *UnwindBB = nullptr;
5820 if (EatIfPresent(lltok::kw_to)) {
5821 if (ParseToken(lltok::kw_caller, "expected 'caller' in catchswitch"))
5822 return true;
5823 } else {
5824 if (ParseTypeAndBasicBlock(UnwindBB, PFS))
5825 return true;
5826 }
5827
5828 auto *CatchSwitch =
5829 CatchSwitchInst::Create(ParentPad, UnwindBB, Table.size());
5830 for (BasicBlock *DestBB : Table)
5831 CatchSwitch->addHandler(DestBB);
5832 Inst = CatchSwitch;
David Majnemer654e1302015-07-31 17:58:14 +00005833 return false;
5834}
5835
5836/// ParseCatchPad
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00005837/// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
David Majnemer654e1302015-07-31 17:58:14 +00005838bool LLParser::ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00005839 Value *CatchSwitch = nullptr;
5840
5841 if (ParseToken(lltok::kw_within, "expected 'within' after catchpad"))
5842 return true;
5843
5844 if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
5845 return TokError("expected scope value for catchpad");
5846
5847 if (ParseValue(Type::getTokenTy(Context), CatchSwitch, PFS))
5848 return true;
5849
David Majnemer654e1302015-07-31 17:58:14 +00005850 SmallVector<Value *, 8> Args;
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00005851 if (ParseExceptionArgs(Args, PFS))
David Majnemer654e1302015-07-31 17:58:14 +00005852 return true;
5853
David Majnemer8a1c45d2015-12-12 05:38:55 +00005854 Inst = CatchPadInst::Create(CatchSwitch, Args);
David Majnemer654e1302015-07-31 17:58:14 +00005855 return false;
5856}
5857
David Majnemer654e1302015-07-31 17:58:14 +00005858/// ParseCleanupPad
David Majnemer8a1c45d2015-12-12 05:38:55 +00005859/// ::= 'cleanuppad' within Parent ParamList
David Majnemer654e1302015-07-31 17:58:14 +00005860bool LLParser::ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
David Majnemer8a1c45d2015-12-12 05:38:55 +00005861 Value *ParentPad = nullptr;
5862
5863 if (ParseToken(lltok::kw_within, "expected 'within' after cleanuppad"))
5864 return true;
5865
5866 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
5867 Lex.getKind() != lltok::LocalVarID)
5868 return TokError("expected scope value for cleanuppad");
5869
5870 if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
5871 return true;
5872
David Majnemer654e1302015-07-31 17:58:14 +00005873 SmallVector<Value *, 8> Args;
Joseph Tremoulet8220bcc2015-08-23 00:26:33 +00005874 if (ParseExceptionArgs(Args, PFS))
David Majnemer654e1302015-07-31 17:58:14 +00005875 return true;
5876
David Majnemer8a1c45d2015-12-12 05:38:55 +00005877 Inst = CleanupPadInst::Create(ParentPad, Args);
Joseph Tremoulet9ce71f72015-09-03 09:09:43 +00005878 return false;
5879}
5880
Chris Lattnerac161bf2009-01-02 07:01:27 +00005881//===----------------------------------------------------------------------===//
5882// Binary Operators.
5883//===----------------------------------------------------------------------===//
5884
5885/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00005886/// ::= ArithmeticOps TypeAndValue ',' Value
5887///
5888/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
5889/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00005890bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00005891 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005892 LocTy Loc; Value *LHS, *RHS;
5893 if (ParseTypeAndValue(LHS, Loc, PFS) ||
5894 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
5895 ParseValue(LHS->getType(), RHS, PFS))
5896 return true;
5897
Chris Lattnereeefa9a2009-01-05 08:24:46 +00005898 bool Valid;
5899 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00005900 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00005901 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00005902 Valid = LHS->getType()->isIntOrIntVectorTy() ||
5903 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00005904 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00005905 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
5906 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00005907 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005908
Chris Lattnereeefa9a2009-01-05 08:24:46 +00005909 if (!Valid)
5910 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005911
Chris Lattnerac161bf2009-01-02 07:01:27 +00005912 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
5913 return false;
5914}
5915
5916/// ParseLogical
5917/// ::= ArithmeticOps TypeAndValue ',' Value {
5918bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
5919 unsigned Opc) {
5920 LocTy Loc; Value *LHS, *RHS;
5921 if (ParseTypeAndValue(LHS, Loc, PFS) ||
5922 ParseToken(lltok::comma, "expected ',' in logical operation") ||
5923 ParseValue(LHS->getType(), RHS, PFS))
5924 return true;
5925
Duncan Sands9dff9be2010-02-15 16:12:20 +00005926 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005927 return Error(Loc,"instruction requires integer or integer vector operands");
5928
5929 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
5930 return false;
5931}
5932
Chris Lattnerac161bf2009-01-02 07:01:27 +00005933/// ParseCompare
5934/// ::= 'icmp' IPredicates TypeAndValue ',' Value
5935/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00005936bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
5937 unsigned Opc) {
5938 // Parse the integer/fp comparison predicate.
5939 LocTy Loc;
5940 unsigned Pred;
5941 Value *LHS, *RHS;
5942 if (ParseCmpPredicate(Pred, Opc) ||
5943 ParseTypeAndValue(LHS, Loc, PFS) ||
5944 ParseToken(lltok::comma, "expected ',' after compare value") ||
5945 ParseValue(LHS->getType(), RHS, PFS))
5946 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005947
Chris Lattnerac161bf2009-01-02 07:01:27 +00005948 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00005949 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005950 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00005951 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00005952 } else {
5953 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00005954 if (!LHS->getType()->isIntOrIntVectorTy() &&
Craig Topper95d23472017-07-09 07:04:00 +00005955 !LHS->getType()->isPtrOrPtrVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005956 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00005957 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005958 }
5959 return false;
5960}
5961
5962//===----------------------------------------------------------------------===//
5963// Other Instructions.
5964//===----------------------------------------------------------------------===//
5965
5966
5967/// ParseCast
5968/// ::= CastOpc TypeAndValue 'to' Type
5969bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
5970 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005971 LocTy Loc;
5972 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00005973 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005974 if (ParseTypeAndValue(Op, Loc, PFS) ||
5975 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
5976 ParseType(DestTy))
5977 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005978
Chris Lattner89d856e2009-03-01 00:53:13 +00005979 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
5980 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005981 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00005982 getTypeString(Op->getType()) + "' to '" +
5983 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00005984 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005985 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
5986 return false;
5987}
5988
5989/// ParseSelect
5990/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5991bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
5992 LocTy Loc;
5993 Value *Op0, *Op1, *Op2;
5994 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5995 ParseToken(lltok::comma, "expected ',' after select condition") ||
5996 ParseTypeAndValue(Op1, PFS) ||
5997 ParseToken(lltok::comma, "expected ',' after select value") ||
5998 ParseTypeAndValue(Op2, PFS))
5999 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006000
Chris Lattnerac161bf2009-01-02 07:01:27 +00006001 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
6002 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006003
Chris Lattnerac161bf2009-01-02 07:01:27 +00006004 Inst = SelectInst::Create(Op0, Op1, Op2);
6005 return false;
6006}
6007
Chris Lattnerb55ab542009-01-05 08:18:44 +00006008/// ParseVA_Arg
6009/// ::= 'va_arg' TypeAndValue ',' Type
6010bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00006011 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00006012 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00006013 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006014 if (ParseTypeAndValue(Op, PFS) ||
6015 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00006016 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006017 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006018
Chris Lattnerb55ab542009-01-05 08:18:44 +00006019 if (!EltTy->isFirstClassType())
6020 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00006021
6022 Inst = new VAArgInst(Op, EltTy);
6023 return false;
6024}
6025
6026/// ParseExtractElement
6027/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
6028bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
6029 LocTy Loc;
6030 Value *Op0, *Op1;
6031 if (ParseTypeAndValue(Op0, Loc, PFS) ||
6032 ParseToken(lltok::comma, "expected ',' after extract value") ||
6033 ParseTypeAndValue(Op1, PFS))
6034 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006035
Chris Lattnerac161bf2009-01-02 07:01:27 +00006036 if (!ExtractElementInst::isValidOperands(Op0, Op1))
6037 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006038
Eric Christopherc9742252009-07-25 02:28:41 +00006039 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00006040 return false;
6041}
6042
6043/// ParseInsertElement
6044/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
6045bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
6046 LocTy Loc;
6047 Value *Op0, *Op1, *Op2;
6048 if (ParseTypeAndValue(Op0, Loc, PFS) ||
6049 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
6050 ParseTypeAndValue(Op1, PFS) ||
6051 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
6052 ParseTypeAndValue(Op2, PFS))
6053 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006054
Chris Lattnerac161bf2009-01-02 07:01:27 +00006055 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00006056 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006057
Chris Lattnerac161bf2009-01-02 07:01:27 +00006058 Inst = InsertElementInst::Create(Op0, Op1, Op2);
6059 return false;
6060}
6061
6062/// ParseShuffleVector
6063/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
6064bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
6065 LocTy Loc;
6066 Value *Op0, *Op1, *Op2;
6067 if (ParseTypeAndValue(Op0, Loc, PFS) ||
6068 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
6069 ParseTypeAndValue(Op1, PFS) ||
6070 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
6071 ParseTypeAndValue(Op2, PFS))
6072 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006073
Chris Lattnerac161bf2009-01-02 07:01:27 +00006074 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00006075 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006076
Chris Lattnerac161bf2009-01-02 07:01:27 +00006077 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
6078 return false;
6079}
6080
6081/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00006082/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00006083int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00006084 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006085 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006086
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00006087 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00006088 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
6089 ParseValue(Ty, Op0, PFS) ||
6090 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00006091 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00006092 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
6093 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006094
Chris Lattnerf4f03422009-12-30 05:27:33 +00006095 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006096 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
Eugene Zelenko1804a772016-08-25 00:45:04 +00006097
6098 while (true) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00006099 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006100
Chris Lattner3822f632009-01-02 08:05:26 +00006101 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006102 break;
6103
Chris Lattnerf4f03422009-12-30 05:27:33 +00006104 if (Lex.getKind() == lltok::MetadataVar) {
6105 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00006106 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00006107 }
Devang Patel8f842d32009-10-16 18:45:49 +00006108
Chris Lattner3822f632009-01-02 08:05:26 +00006109 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00006110 ParseValue(Ty, Op0, PFS) ||
6111 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00006112 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00006113 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
6114 return true;
6115 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006116
Chris Lattnerac161bf2009-01-02 07:01:27 +00006117 if (!Ty->isFirstClassType())
6118 return Error(TypeLoc, "phi node must have first class type");
6119
Jay Foad52131342011-03-30 11:28:46 +00006120 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00006121 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
6122 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
6123 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00006124 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006125}
6126
Bill Wendlingfae14752011-08-12 20:24:12 +00006127/// ParseLandingPad
6128/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
6129/// Clause
6130/// ::= 'catch' TypeAndValue
6131/// ::= 'filter'
6132/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
6133bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00006134 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00006135
David Majnemer7fddecc2015-06-17 20:52:32 +00006136 if (ParseType(Ty, TyLoc))
Bill Wendlingfae14752011-08-12 20:24:12 +00006137 return true;
6138
David Majnemer7fddecc2015-06-17 20:52:32 +00006139 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
Bill Wendlingfae14752011-08-12 20:24:12 +00006140 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
6141
6142 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
6143 LandingPadInst::ClauseType CT;
6144 if (EatIfPresent(lltok::kw_catch))
6145 CT = LandingPadInst::Catch;
6146 else if (EatIfPresent(lltok::kw_filter))
6147 CT = LandingPadInst::Filter;
6148 else
6149 return TokError("expected 'catch' or 'filter' clause type");
6150
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00006151 Value *V;
6152 LocTy VLoc;
Owen Andersonf8f259d2015-03-09 07:13:42 +00006153 if (ParseTypeAndValue(V, VLoc, PFS))
Bill Wendlingfae14752011-08-12 20:24:12 +00006154 return true;
Bill Wendlingfae14752011-08-12 20:24:12 +00006155
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00006156 // A 'catch' type expects a non-array constant. A filter clause expects an
6157 // array constant.
6158 if (CT == LandingPadInst::Catch) {
6159 if (isa<ArrayType>(V->getType()))
6160 Error(VLoc, "'catch' clause has an invalid type");
6161 } else {
6162 if (!isa<ArrayType>(V->getType()))
6163 Error(VLoc, "'filter' clause has an invalid type");
6164 }
6165
Owen Andersonf8f259d2015-03-09 07:13:42 +00006166 Constant *CV = dyn_cast<Constant>(V);
6167 if (!CV)
6168 return Error(VLoc, "clause argument must be a constant");
6169 LP->addClause(CV);
Bill Wendlingfae14752011-08-12 20:24:12 +00006170 }
6171
Owen Andersonf8f259d2015-03-09 07:13:42 +00006172 Inst = LP.release();
Bill Wendlingfae14752011-08-12 20:24:12 +00006173 return false;
6174}
6175
Chris Lattnerac161bf2009-01-02 07:01:27 +00006176/// ParseCall
Sanjay Patelfa54ace2015-12-14 21:59:03 +00006177/// ::= 'call' OptionalFastMathFlags OptionalCallingConv
6178/// OptionalAttrs Type Value ParameterList OptionalAttrs
6179/// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
6180/// OptionalAttrs Type Value ParameterList OptionalAttrs
6181/// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
6182/// OptionalAttrs Type Value ParameterList OptionalAttrs
6183/// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv
6184/// OptionalAttrs Type Value ParameterList OptionalAttrs
Chris Lattnerac161bf2009-01-02 07:01:27 +00006185bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00006186 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00006187 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00006188 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00006189 LocTy BuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00006190 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00006191 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006192 LocTy RetTypeLoc;
6193 ValID CalleeID;
6194 SmallVector<ParamInfo, 16> ArgList;
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00006195 SmallVector<OperandBundleDef, 2> BundleList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006196 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006197
Sanjay Patelfa54ace2015-12-14 21:59:03 +00006198 if (TCK != CallInst::TCK_None &&
6199 ParseToken(lltok::kw_call,
6200 "expected 'tail call', 'musttail call', or 'notail call'"))
6201 return true;
6202
6203 FastMathFlags FMF = EatFastMathFlagsIfPresent();
6204
6205 if (ParseOptionalCallingConv(CC) || ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00006206 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00006207 ParseValID(CalleeID) ||
Reid Kleckner83498642014-08-26 00:33:28 +00006208 ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
6209 PFS.getFunction().isVarArg()) ||
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00006210 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false, BuiltinLoc) ||
6211 ParseOptionalOperandBundles(BundleList, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006212 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006213
Sanjay Patelfa54ace2015-12-14 21:59:03 +00006214 if (FMF.any() && !RetType->isFPOrFPVectorTy())
6215 return Error(CallLoc, "fast-math-flags specified for call without "
6216 "floating-point scalar or vector return type");
6217
Chris Lattnerac161bf2009-01-02 07:01:27 +00006218 // If RetType is a non-function pointer type, then this is the short syntax
6219 // for the call, which means that RetType is just the return type. Infer the
6220 // rest of the function argument types from the arguments that are present.
David Blaikie23af6482015-04-16 23:24:18 +00006221 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
6222 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00006223 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00006224 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00006225 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
6226 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006227
Chris Lattnerac161bf2009-01-02 07:01:27 +00006228 if (!FunctionType::isValidReturnType(RetType))
6229 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006230
Owen Anderson4056ca92009-07-29 22:17:13 +00006231 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00006232 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006233
David Blaikie41ba2b42015-07-27 23:32:19 +00006234 CalleeID.FTy = Ty;
6235
Chris Lattnerac161bf2009-01-02 07:01:27 +00006236 // Look up the callee.
6237 Value *Callee;
Alexander Richardsonc11ae182018-02-27 11:15:11 +00006238 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS,
6239 /*IsCall=*/true))
David Blaikie23af6482015-04-16 23:24:18 +00006240 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006241
Bill Wendling3d7b0b82012-12-19 07:18:57 +00006242 // Set up the Attribute for the function.
Reid Klecknerc2cb5602017-04-12 00:38:00 +00006243 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006244
Chris Lattnerac161bf2009-01-02 07:01:27 +00006245 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006246
Chris Lattnerac161bf2009-01-02 07:01:27 +00006247 // Loop through FunctionType's arguments and ensure they are specified
6248 // correctly. Also, gather any parameter attributes.
6249 FunctionType::param_iterator I = Ty->param_begin();
6250 FunctionType::param_iterator E = Ty->param_end();
6251 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00006252 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006253 if (I != E) {
6254 ExpectedTy = *I++;
6255 } else if (!Ty->isVarArg()) {
6256 return Error(ArgList[i].Loc, "too many arguments specified");
6257 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006258
Chris Lattnerac161bf2009-01-02 07:01:27 +00006259 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
6260 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00006261 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00006262 Args.push_back(ArgList[i].V);
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00006263 Attrs.push_back(ArgList[i].Attrs);
Chris Lattnerac161bf2009-01-02 07:01:27 +00006264 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006265
Chris Lattnerac161bf2009-01-02 07:01:27 +00006266 if (I != E)
6267 return Error(CallLoc, "not enough parameters specified for call");
6268
Reid Klecknereb9dd5b2017-04-10 23:31:05 +00006269 if (FnAttrs.hasAlignmentAttr())
6270 return Error(CallLoc, "call instructions may not have an alignment");
David Majnemer8d22abd2015-02-23 00:01:32 +00006271
Bill Wendling3d7b0b82012-12-19 07:18:57 +00006272 // Finish off the Attribute and check them
Reid Kleckner7f720332017-04-13 00:58:09 +00006273 AttributeList PAL =
6274 AttributeList::get(Context, AttributeSet::get(Context, FnAttrs),
6275 AttributeSet::get(Context, RetAttrs), Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006276
Sanjoy Dasb513a9f2015-09-24 23:34:52 +00006277 CallInst *CI = CallInst::Create(Ty, Callee, Args, BundleList);
Reid Kleckner5772b772014-04-24 20:14:34 +00006278 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00006279 CI->setCallingConv(CC);
Sanjay Patelfa54ace2015-12-14 21:59:03 +00006280 if (FMF.any())
6281 CI->setFastMathFlags(FMF);
Chris Lattnerac161bf2009-01-02 07:01:27 +00006282 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00006283 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006284 Inst = CI;
6285 return false;
6286}
6287
6288//===----------------------------------------------------------------------===//
6289// Memory Instructions.
6290//===----------------------------------------------------------------------===//
6291
6292/// ParseAlloc
Manman Ren9bfd0d02016-04-01 21:41:15 +00006293/// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
Yaxun Liuadde4e42017-10-14 03:23:18 +00006294/// (',' 'align' i32)? (',', 'addrspace(n))?
Chris Lattner78103722011-06-17 03:16:47 +00006295int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00006296 Value *Size = nullptr;
Matt Arsenault3c1fc762017-04-10 22:27:50 +00006297 LocTy SizeLoc, TyLoc, ASLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006298 unsigned Alignment = 0;
Matt Arsenault3c1fc762017-04-10 22:27:50 +00006299 unsigned AddrSpace = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00006300 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00006301
6302 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
Manman Ren9bfd0d02016-04-01 21:41:15 +00006303 bool IsSwiftError = EatIfPresent(lltok::kw_swifterror);
David Majnemerc4ab61c2014-03-09 06:41:58 +00006304
David Majnemera3b0eb22015-02-16 08:38:03 +00006305 if (ParseType(Ty, TyLoc)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006306
David Majnemera3b0eb22015-02-16 08:38:03 +00006307 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
6308 return Error(TyLoc, "invalid type for alloca");
David Majnemerfad5a312015-02-11 09:13:11 +00006309
Chris Lattnerb2f39502009-12-30 05:44:30 +00006310 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00006311 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00006312 if (Lex.getKind() == lltok::kw_align) {
Matt Arsenault3c1fc762017-04-10 22:27:50 +00006313 if (ParseOptionalAlignment(Alignment))
6314 return true;
6315 if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
6316 return true;
6317 } else if (Lex.getKind() == lltok::kw_addrspace) {
6318 ASLoc = Lex.getLoc();
6319 if (ParseOptionalAddrSpace(AddrSpace))
6320 return true;
David Majnemerc4ab61c2014-03-09 06:41:58 +00006321 } else if (Lex.getKind() == lltok::MetadataVar) {
6322 AteExtraComma = true;
6323 } else {
Yaxun Liuadde4e42017-10-14 03:23:18 +00006324 if (ParseTypeAndValue(Size, SizeLoc, PFS))
David Majnemerc4ab61c2014-03-09 06:41:58 +00006325 return true;
Yaxun Liuadde4e42017-10-14 03:23:18 +00006326 if (EatIfPresent(lltok::comma)) {
6327 if (Lex.getKind() == lltok::kw_align) {
6328 if (ParseOptionalAlignment(Alignment))
6329 return true;
6330 if (ParseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
6331 return true;
6332 } else if (Lex.getKind() == lltok::kw_addrspace) {
6333 ASLoc = Lex.getLoc();
6334 if (ParseOptionalAddrSpace(AddrSpace))
6335 return true;
6336 } else if (Lex.getKind() == lltok::MetadataVar) {
6337 AteExtraComma = true;
6338 }
6339 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00006340 }
6341 }
6342
Dan Gohman2140a742010-05-28 01:14:11 +00006343 if (Size && !Size->getType()->isIntegerTy())
6344 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00006345
Yaxun Liuc00d81e2018-01-30 22:32:39 +00006346 AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, Alignment);
Reid Kleckner436c42e2014-01-17 23:58:17 +00006347 AI->setUsedWithInAlloca(IsInAlloca);
Manman Ren9bfd0d02016-04-01 21:41:15 +00006348 AI->setSwiftError(IsSwiftError);
Reid Kleckner436c42e2014-01-17 23:58:17 +00006349 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00006350 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006351}
6352
6353/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00006354/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00006355/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00006356/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00006357int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00006358 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00006359 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00006360 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00006361 bool isAtomic = false;
JF Bastien800f87a2016-04-06 21:19:33 +00006362 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006363 SyncScope::ID SSID = SyncScope::System;
Eli Friedman02e737b2011-08-12 22:50:01 +00006364
6365 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00006366 isAtomic = true;
6367 Lex.Lex();
6368 }
6369
Chris Lattnerbc639292011-11-27 06:56:53 +00006370 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00006371 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00006372 isVolatile = true;
6373 Lex.Lex();
6374 }
6375
David Blaikie15d9a4c2015-04-06 20:59:48 +00006376 Type *Ty;
David Blaikiea79ac142015-02-27 21:17:42 +00006377 LocTy ExplicitTypeLoc = Lex.getLoc();
6378 if (ParseType(Ty) ||
6379 ParseToken(lltok::comma, "expected comma after load's type") ||
6380 ParseTypeAndValue(Val, Loc, PFS) ||
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006381 ParseScopeAndOrdering(isAtomic, SSID, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00006382 ParseOptionalCommaAlign(Alignment, AteExtraComma))
6383 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006384
David Blaikie15d9a4c2015-04-06 20:59:48 +00006385 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
Chris Lattnerac161bf2009-01-02 07:01:27 +00006386 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00006387 if (isAtomic && !Alignment)
6388 return Error(Loc, "atomic load must have explicit non-zero alignment");
JF Bastien800f87a2016-04-06 21:19:33 +00006389 if (Ordering == AtomicOrdering::Release ||
6390 Ordering == AtomicOrdering::AcquireRelease)
Eli Friedman59b66882011-08-09 23:02:53 +00006391 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006392
David Blaikiea79ac142015-02-27 21:17:42 +00006393 if (Ty != cast<PointerType>(Val->getType())->getElementType())
6394 return Error(ExplicitTypeLoc,
6395 "explicit pointee type doesn't match operand's pointee type");
6396
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006397 Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, SSID);
Chris Lattnerb2f39502009-12-30 05:44:30 +00006398 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006399}
6400
6401/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00006402
6403/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
6404/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00006405/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00006406int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00006407 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00006408 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00006409 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00006410 bool isAtomic = false;
JF Bastien800f87a2016-04-06 21:19:33 +00006411 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006412 SyncScope::ID SSID = SyncScope::System;
Eli Friedman02e737b2011-08-12 22:50:01 +00006413
6414 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00006415 isAtomic = true;
6416 Lex.Lex();
6417 }
6418
Chris Lattnerbc639292011-11-27 06:56:53 +00006419 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00006420 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00006421 isVolatile = true;
6422 Lex.Lex();
6423 }
6424
Chris Lattnerac161bf2009-01-02 07:01:27 +00006425 if (ParseTypeAndValue(Val, Loc, PFS) ||
6426 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00006427 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006428 ParseScopeAndOrdering(isAtomic, SSID, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00006429 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006430 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00006431
Duncan Sands19d0b472010-02-16 11:11:14 +00006432 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00006433 return Error(PtrLoc, "store operand must be a pointer");
6434 if (!Val->getType()->isFirstClassType())
6435 return Error(Loc, "store operand must be a first class value");
6436 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
6437 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00006438 if (isAtomic && !Alignment)
6439 return Error(Loc, "atomic store must have explicit non-zero alignment");
JF Bastien800f87a2016-04-06 21:19:33 +00006440 if (Ordering == AtomicOrdering::Acquire ||
6441 Ordering == AtomicOrdering::AcquireRelease)
Eli Friedman59b66882011-08-09 23:02:53 +00006442 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006443
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006444 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, SSID);
Chris Lattnerb2f39502009-12-30 05:44:30 +00006445 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006446}
6447
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006448/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00006449/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
6450/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00006451int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006452 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
6453 bool AteExtraComma = false;
JF Bastien800f87a2016-04-06 21:19:33 +00006454 AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
6455 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006456 SyncScope::ID SSID = SyncScope::System;
Eli Friedman02e737b2011-08-12 22:50:01 +00006457 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00006458 bool isWeak = false;
6459
6460 if (EatIfPresent(lltok::kw_weak))
6461 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00006462
6463 if (EatIfPresent(lltok::kw_volatile))
6464 isVolatile = true;
6465
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006466 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
6467 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
6468 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
6469 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
6470 ParseTypeAndValue(New, NewLoc, PFS) ||
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006471 ParseScopeAndOrdering(true /*Always atomic*/, SSID, SuccessOrdering) ||
Tim Northovere94a5182014-03-11 10:48:52 +00006472 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006473 return true;
6474
JF Bastien800f87a2016-04-06 21:19:33 +00006475 if (SuccessOrdering == AtomicOrdering::Unordered ||
6476 FailureOrdering == AtomicOrdering::Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006477 return TokError("cmpxchg cannot be unordered");
JF Bastien800f87a2016-04-06 21:19:33 +00006478 if (isStrongerThan(FailureOrdering, SuccessOrdering))
6479 return TokError("cmpxchg failure argument shall be no stronger than the "
6480 "success argument");
6481 if (FailureOrdering == AtomicOrdering::Release ||
6482 FailureOrdering == AtomicOrdering::AcquireRelease)
6483 return TokError(
6484 "cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006485 if (!Ptr->getType()->isPointerTy())
6486 return Error(PtrLoc, "cmpxchg operand must be a pointer");
6487 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
6488 return Error(CmpLoc, "compare value and pointer type do not match");
6489 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
6490 return Error(NewLoc, "new value and pointer type do not match");
Philip Reames1960cfd2016-02-19 00:06:41 +00006491 if (!New->getType()->isFirstClassType())
6492 return Error(NewLoc, "cmpxchg operand must be a first class value");
Tim Northover420a2162014-06-13 14:24:07 +00006493 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006494 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006495 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00006496 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006497 Inst = CXI;
6498 return AteExtraComma ? InstExtraComma : InstNormal;
6499}
6500
6501/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00006502/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
6503/// 'singlethread'? AtomicOrdering
6504int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006505 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
6506 bool AteExtraComma = false;
JF Bastien800f87a2016-04-06 21:19:33 +00006507 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006508 SyncScope::ID SSID = SyncScope::System;
Eli Friedman02e737b2011-08-12 22:50:01 +00006509 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006510 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00006511
6512 if (EatIfPresent(lltok::kw_volatile))
6513 isVolatile = true;
6514
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006515 switch (Lex.getKind()) {
6516 default: return TokError("expected binary operation in atomicrmw");
6517 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
6518 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
6519 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
6520 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
6521 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
6522 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
6523 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
6524 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
6525 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
6526 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
6527 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
6528 }
6529 Lex.Lex(); // Eat the operation.
6530
6531 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
6532 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
6533 ParseTypeAndValue(Val, ValLoc, PFS) ||
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006534 ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006535 return true;
6536
JF Bastien800f87a2016-04-06 21:19:33 +00006537 if (Ordering == AtomicOrdering::Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006538 return TokError("atomicrmw cannot be unordered");
6539 if (!Ptr->getType()->isPointerTy())
6540 return Error(PtrLoc, "atomicrmw operand must be a pointer");
6541 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
6542 return Error(ValLoc, "atomicrmw value and pointer type do not match");
6543 if (!Val->getType()->isIntegerTy())
6544 return Error(ValLoc, "atomicrmw operand must be an integer");
6545 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
6546 if (Size < 8 || (Size & (Size - 1)))
6547 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
6548 " integer");
6549
6550 AtomicRMWInst *RMWI =
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006551 new AtomicRMWInst(Operation, Ptr, Val, Ordering, SSID);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00006552 RMWI->setVolatile(isVolatile);
6553 Inst = RMWI;
6554 return AteExtraComma ? InstExtraComma : InstNormal;
6555}
6556
Eli Friedmanfee02c62011-07-25 23:16:38 +00006557/// ParseFence
6558/// ::= 'fence' 'singlethread'? AtomicOrdering
6559int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
JF Bastien800f87a2016-04-06 21:19:33 +00006560 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006561 SyncScope::ID SSID = SyncScope::System;
6562 if (ParseScopeAndOrdering(true /*Always atomic*/, SSID, Ordering))
Eli Friedmanfee02c62011-07-25 23:16:38 +00006563 return true;
6564
JF Bastien800f87a2016-04-06 21:19:33 +00006565 if (Ordering == AtomicOrdering::Unordered)
Eli Friedmanfee02c62011-07-25 23:16:38 +00006566 return TokError("fence cannot be unordered");
JF Bastien800f87a2016-04-06 21:19:33 +00006567 if (Ordering == AtomicOrdering::Monotonic)
Eli Friedmanfee02c62011-07-25 23:16:38 +00006568 return TokError("fence cannot be monotonic");
6569
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00006570 Inst = new FenceInst(Context, Ordering, SSID);
Eli Friedmanfee02c62011-07-25 23:16:38 +00006571 return InstNormal;
6572}
6573
Chris Lattnerac161bf2009-01-02 07:01:27 +00006574/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00006575/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00006576int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00006577 Value *Ptr = nullptr;
6578 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00006579 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00006580
Dan Gohman16cbbe42009-07-29 15:58:36 +00006581 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00006582
David Blaikie79e6c742015-02-27 19:29:02 +00006583 Type *Ty = nullptr;
6584 LocTy ExplicitTypeLoc = Lex.getLoc();
6585 if (ParseType(Ty) ||
6586 ParseToken(lltok::comma, "expected comma after getelementptr's type") ||
6587 ParseTypeAndValue(Ptr, Loc, PFS))
6588 return true;
6589
Eli Benderskyd9806682013-04-22 17:03:42 +00006590 Type *BaseType = Ptr->getType();
6591 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
6592 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00006593 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006594
David Blaikie8d757942015-03-09 23:08:44 +00006595 if (Ty != BasePointerType->getElementType())
6596 return Error(ExplicitTypeLoc,
6597 "explicit pointee type doesn't match operand's pointee type");
6598
Chris Lattnerac161bf2009-01-02 07:01:27 +00006599 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00006600 bool AteExtraComma = false;
Elena Demikhovsky37a4da82015-07-09 07:42:48 +00006601 // GEP returns a vector of pointers if at least one of parameters is a vector.
6602 // All vector parameters should have the same vector width.
6603 unsigned GEPWidth = BaseType->isVectorTy() ?
6604 BaseType->getVectorNumElements() : 0;
6605
Chris Lattner3822f632009-01-02 08:05:26 +00006606 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00006607 if (Lex.getKind() == lltok::MetadataVar) {
6608 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00006609 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00006610 }
Chris Lattner3822f632009-01-02 08:05:26 +00006611 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Craig Topper95d23472017-07-09 07:04:00 +00006612 if (!Val->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00006613 return Error(EltLoc, "getelementptr index must be an integer");
Elena Demikhovsky37a4da82015-07-09 07:42:48 +00006614
Nadav Rotem3924cb02011-12-05 06:29:09 +00006615 if (Val->getType()->isVectorTy()) {
Elena Demikhovsky37a4da82015-07-09 07:42:48 +00006616 unsigned ValNumEl = Val->getType()->getVectorNumElements();
6617 if (GEPWidth && GEPWidth != ValNumEl)
Nadav Rotem3924cb02011-12-05 06:29:09 +00006618 return Error(EltLoc,
6619 "getelementptr vector index has a wrong number of elements");
Elena Demikhovsky37a4da82015-07-09 07:42:48 +00006620 GEPWidth = ValNumEl;
Nadav Rotem3924cb02011-12-05 06:29:09 +00006621 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00006622 Indices.push_back(Val);
6623 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006624
Craig Toppere3dcce92015-08-01 22:20:21 +00006625 SmallPtrSet<Type*, 4> Visited;
David Blaikied33bad32015-04-17 22:32:13 +00006626 if (!Indices.empty() && !Ty->isSized(&Visited))
Eli Benderskyd9806682013-04-22 17:03:42 +00006627 return Error(Loc, "base element of getelementptr must be sized");
6628
David Blaikied33bad32015-04-17 22:32:13 +00006629 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006630 return Error(Loc, "invalid getelementptr indices");
David Blaikiecd7b97e2015-03-14 21:11:24 +00006631 Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00006632 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00006633 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00006634 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006635}
6636
6637/// ParseExtractValue
6638/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00006639int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00006640 Value *Val; LocTy Loc;
6641 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00006642 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006643 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00006644 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006645 return true;
6646
Chris Lattner392be582010-02-12 20:49:41 +00006647 if (!Val->getType()->isAggregateType())
6648 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00006649
Jay Foad57aa6362011-07-13 10:26:04 +00006650 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006651 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00006652 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00006653 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006654}
6655
6656/// ParseInsertValue
6657/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00006658int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00006659 Value *Val0, *Val1; LocTy Loc0, Loc1;
6660 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00006661 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006662 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
6663 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
6664 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00006665 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00006666 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00006667
Chris Lattner392be582010-02-12 20:49:41 +00006668 if (!Val0->getType()->isAggregateType())
6669 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00006670
David Majnemer30074532015-02-11 07:43:58 +00006671 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
6672 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00006673 return Error(Loc0, "invalid indices for insertvalue");
David Majnemer30074532015-02-11 07:43:58 +00006674 if (IndexedType != Val1->getType())
6675 return Error(Loc1, "insertvalue operand and field disagree in type: '" +
6676 getTypeString(Val1->getType()) + "' instead of '" +
6677 getTypeString(IndexedType) + "'");
Jay Foad57aa6362011-07-13 10:26:04 +00006678 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00006679 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00006680}
Nick Lewycky49f89192009-04-04 07:22:01 +00006681
6682//===----------------------------------------------------------------------===//
6683// Embedded metadata.
6684//===----------------------------------------------------------------------===//
6685
6686/// ParseMDNodeVector
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00006687/// ::= { Element (',' Element)* }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00006688/// Element
6689/// ::= 'null' | TypeAndValue
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00006690bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
David Majnemer06f960d2014-12-11 20:44:09 +00006691 if (ParseToken(lltok::lbrace, "expected '{' here"))
6692 return true;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00006693
Dan Gohman1e0213a2010-07-13 19:33:27 +00006694 // Check for an empty list.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00006695 if (EatIfPresent(lltok::rbrace))
Dan Gohman1e0213a2010-07-13 19:33:27 +00006696 return false;
6697
Nick Lewycky49f89192009-04-04 07:22:01 +00006698 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00006699 // Null is a special case since it is typeless.
6700 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00006701 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00006702 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00006703 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00006704
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00006705 Metadata *MD;
6706 if (ParseMetadata(MD, nullptr))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00006707 return true;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00006708 Elts.push_back(MD);
Nick Lewycky49f89192009-04-04 07:22:01 +00006709 } while (EatIfPresent(lltok::comma));
6710
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00006711 return ParseToken(lltok::rbrace, "expected end of metadata node");
Nick Lewycky49f89192009-04-04 07:22:01 +00006712}
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00006713
6714//===----------------------------------------------------------------------===//
6715// Use-list order directives.
6716//===----------------------------------------------------------------------===//
6717bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
6718 SMLoc Loc) {
6719 if (V->use_empty())
6720 return Error(Loc, "value has no uses");
6721
6722 unsigned NumUses = 0;
6723 SmallDenseMap<const Use *, unsigned, 16> Order;
6724 for (const Use &U : V->uses()) {
6725 if (++NumUses > Indexes.size())
6726 break;
6727 Order[&U] = Indexes[NumUses - 1];
6728 }
6729 if (NumUses < 2)
6730 return Error(Loc, "value only has one use");
6731 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
6732 return Error(Loc, "wrong number of indexes, expected " +
6733 Twine(std::distance(V->use_begin(), V->use_end())));
6734
6735 V->sortUseList([&](const Use &L, const Use &R) {
6736 return Order.lookup(&L) < Order.lookup(&R);
6737 });
6738 return false;
6739}
6740
6741/// ParseUseListOrderIndexes
6742/// ::= '{' uint32 (',' uint32)+ '}'
6743bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
6744 SMLoc Loc = Lex.getLoc();
6745 if (ParseToken(lltok::lbrace, "expected '{' here"))
6746 return true;
6747 if (Lex.getKind() == lltok::rbrace)
6748 return Lex.Error("expected non-empty list of uselistorder indexes");
6749
6750 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
6751 // indexes should be distinct numbers in the range [0, size-1], and should
6752 // not be in order.
6753 unsigned Offset = 0;
6754 unsigned Max = 0;
6755 bool IsOrdered = true;
6756 assert(Indexes.empty() && "Expected empty order vector");
6757 do {
6758 unsigned Index;
6759 if (ParseUInt32(Index))
6760 return true;
6761
6762 // Update consistency checks.
6763 Offset += Index - Indexes.size();
6764 Max = std::max(Max, Index);
6765 IsOrdered &= Index == Indexes.size();
6766
6767 Indexes.push_back(Index);
6768 } while (EatIfPresent(lltok::comma));
6769
6770 if (ParseToken(lltok::rbrace, "expected '}' here"))
6771 return true;
6772
6773 if (Indexes.size() < 2)
6774 return Error(Loc, "expected >= 2 uselistorder indexes");
6775 if (Offset != 0 || Max >= Indexes.size())
6776 return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
6777 if (IsOrdered)
6778 return Error(Loc, "expected uselistorder indexes to change the order");
6779
6780 return false;
6781}
6782
6783/// ParseUseListOrder
6784/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
6785bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
6786 SMLoc Loc = Lex.getLoc();
6787 if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
6788 return true;
6789
6790 Value *V;
6791 SmallVector<unsigned, 16> Indexes;
6792 if (ParseTypeAndValue(V, PFS) ||
6793 ParseToken(lltok::comma, "expected comma in uselistorder directive") ||
6794 ParseUseListOrderIndexes(Indexes))
6795 return true;
6796
6797 return sortUseListOrder(V, Indexes, Loc);
6798}
6799
6800/// ParseUseListOrderBB
6801/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
6802bool LLParser::ParseUseListOrderBB() {
6803 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
6804 SMLoc Loc = Lex.getLoc();
6805 Lex.Lex();
6806
6807 ValID Fn, Label;
6808 SmallVector<unsigned, 16> Indexes;
6809 if (ParseValID(Fn) ||
6810 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
6811 ParseValID(Label) ||
6812 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
6813 ParseUseListOrderIndexes(Indexes))
6814 return true;
6815
6816 // Check the function.
6817 GlobalValue *GV;
6818 if (Fn.Kind == ValID::t_GlobalName)
6819 GV = M->getNamedValue(Fn.StrVal);
6820 else if (Fn.Kind == ValID::t_GlobalID)
6821 GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
6822 else
6823 return Error(Fn.Loc, "expected function name in uselistorder_bb");
6824 if (!GV)
6825 return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
6826 auto *F = dyn_cast<Function>(GV);
6827 if (!F)
6828 return Error(Fn.Loc, "expected function name in uselistorder_bb");
6829 if (F->isDeclaration())
6830 return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
6831
6832 // Check the basic block.
6833 if (Label.Kind == ValID::t_LocalID)
6834 return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
6835 if (Label.Kind != ValID::t_LocalName)
6836 return Error(Label.Loc, "expected basic block name in uselistorder_bb");
Mehdi Aminia53d49e2016-09-17 06:00:02 +00006837 Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00006838 if (!V)
6839 return Error(Label.Loc, "invalid basic block in uselistorder_bb");
6840 if (!isa<BasicBlock>(V))
6841 return Error(Label.Loc, "expected basic block in uselistorder_bb");
6842
6843 return sortUseListOrder(V, Indexes, Loc);
6844}