blob: a121e59e1f1030941ff10cbee340011fe878d898 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth91065212014-03-05 10:34:14 +000016#include "llvm/IR/AutoUpgrade.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/CallingConv.h"
18#include "llvm/IR/Constants.h"
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +000019#include "llvm/IR/DebugInfo.h"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000020#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/DerivedTypes.h"
22#include "llvm/IR/InlineAsm.h"
23#include "llvm/IR/Instructions.h"
Manman Ren209b17c2013-09-28 00:22:27 +000024#include "llvm/IR/LLVMContext.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Module.h"
26#include "llvm/IR/Operator.h"
27#include "llvm/IR/ValueSymbolTable.h"
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +000028#include "llvm/Support/Dwarf.h"
Torok Edwin56d06592009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +000030#include "llvm/Support/SaveAndRestore.h"
Chris Lattnerac161bf2009-01-02 07:01:27 +000031#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33
Chris Lattner229907c2011-07-18 04:54:35 +000034static std::string getTypeString(Type *T) {
Alp Tokere69170a2014-06-26 22:52:05 +000035 std::string Result;
36 raw_string_ostream Tmp(Result);
37 Tmp << *T;
38 return Tmp.str();
Chris Lattner0f214eb2011-06-18 21:18:23 +000039}
40
Chris Lattner3822f632009-01-02 08:05:26 +000041/// Run: module ::= toplevelentity*
Chris Lattnerad6f3352009-01-04 20:44:11 +000042bool LLParser::Run() {
Chris Lattner3822f632009-01-02 08:05:26 +000043 // Prime the lexer.
44 Lex.Lex();
45
Chris Lattnerad6f3352009-01-04 20:44:11 +000046 return ParseTopLevelEntities() ||
47 ValidateEndOfModule();
Chris Lattnerac161bf2009-01-02 07:01:27 +000048}
49
50/// ValidateEndOfModule - Do final validity and sanity checks at the end of the
51/// module.
52bool LLParser::ValidateEndOfModule() {
Manman Ren209b17c2013-09-28 00:22:27 +000053 for (unsigned I = 0, E = InstsWithTBAATag.size(); I < E; I++)
54 UpgradeInstWithTBAATag(InstsWithTBAATag[I]);
55
Bill Wendlingb32b0412013-02-08 06:32:06 +000056 // Handle any function attribute group forward references.
57 for (std::map<Value*, std::vector<unsigned> >::iterator
58 I = ForwardRefAttrGroups.begin(), E = ForwardRefAttrGroups.end();
59 I != E; ++I) {
60 Value *V = I->first;
61 std::vector<unsigned> &Vec = I->second;
62 AttrBuilder B;
63
64 for (std::vector<unsigned>::iterator VI = Vec.begin(), VE = Vec.end();
65 VI != VE; ++VI)
66 B.merge(NumberedAttrBuilders[*VI]);
67
68 if (Function *Fn = dyn_cast<Function>(V)) {
69 AttributeSet AS = Fn->getAttributes();
70 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
71 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
72 AS.getFnAttributes());
73
74 FnAttrs.merge(B);
75
76 // If the alignment was parsed as an attribute, move to the alignment
77 // field.
78 if (FnAttrs.hasAlignmentAttr()) {
79 Fn->setAlignment(FnAttrs.getAlignment());
80 FnAttrs.removeAttribute(Attribute::Alignment);
81 }
82
83 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
84 AttributeSet::get(Context,
85 AttributeSet::FunctionIndex,
86 FnAttrs));
87 Fn->setAttributes(AS);
88 } else if (CallInst *CI = dyn_cast<CallInst>(V)) {
89 AttributeSet AS = CI->getAttributes();
90 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
91 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
92 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +000093 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +000094 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
95 AttributeSet::get(Context,
96 AttributeSet::FunctionIndex,
97 FnAttrs));
98 CI->setAttributes(AS);
99 } else if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
100 AttributeSet AS = II->getAttributes();
101 AttrBuilder FnAttrs(AS.getFnAttributes(), AttributeSet::FunctionIndex);
102 AS = AS.removeAttributes(Context, AttributeSet::FunctionIndex,
103 AS.getFnAttributes());
Bill Wendling59dce372013-02-12 10:13:06 +0000104 FnAttrs.merge(B);
Bill Wendlingb32b0412013-02-08 06:32:06 +0000105 AS = AS.addAttributes(Context, AttributeSet::FunctionIndex,
106 AttributeSet::get(Context,
107 AttributeSet::FunctionIndex,
108 FnAttrs));
109 II->setAttributes(AS);
110 } else {
111 llvm_unreachable("invalid object with forward attribute group reference");
112 }
113 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000114
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +0000115 // If there are entries in ForwardRefBlockAddresses at this point, the
116 // function was never defined.
117 if (!ForwardRefBlockAddresses.empty())
118 return Error(ForwardRefBlockAddresses.begin()->first.Loc,
119 "expected function name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000120
David Majnemer19b51052015-02-11 07:43:56 +0000121 for (const auto &NT : NumberedTypes)
122 if (NT.second.second.isValid())
123 return Error(NT.second.second,
124 "use of undefined type '%" + Twine(NT.first) + "'");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000125
126 for (StringMap<std::pair<Type*, LocTy> >::iterator I =
127 NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I)
128 if (I->second.second.isValid())
129 return Error(I->second.second,
130 "use of undefined type named '" + I->getKey() + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000131
David Majnemerdad0a642014-06-27 18:19:56 +0000132 if (!ForwardRefComdats.empty())
133 return Error(ForwardRefComdats.begin()->second,
134 "use of undefined comdat '$" +
135 ForwardRefComdats.begin()->first + "'");
136
Chris Lattnerac161bf2009-01-02 07:01:27 +0000137 if (!ForwardRefVals.empty())
138 return Error(ForwardRefVals.begin()->second.second,
139 "use of undefined value '@" + ForwardRefVals.begin()->first +
140 "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000141
Chris Lattnerac161bf2009-01-02 07:01:27 +0000142 if (!ForwardRefValIDs.empty())
143 return Error(ForwardRefValIDs.begin()->second.second,
144 "use of undefined value '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000145 Twine(ForwardRefValIDs.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000146
Devang Pateld2541152009-07-08 19:23:54 +0000147 if (!ForwardRefMDNodes.empty())
148 return Error(ForwardRefMDNodes.begin()->second.second,
149 "use of undefined metadata '!" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000150 Twine(ForwardRefMDNodes.begin()->first) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000151
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000152 // Resolve metadata cycles.
David Majnemer19b51052015-02-11 07:43:56 +0000153 for (auto &N : NumberedMetadata) {
154 if (N.second && !N.second->isResolved())
155 N.second->resolveCycles();
156 }
Devang Pateld2541152009-07-08 19:23:54 +0000157
Chris Lattnerac161bf2009-01-02 07:01:27 +0000158 // Look for intrinsic functions and CallInst that need to be upgraded
159 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
160 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000161
Manman Ren8b4306c2013-12-02 21:29:56 +0000162 UpgradeDebugInfo(*M);
163
Chris Lattnerac161bf2009-01-02 07:01:27 +0000164 return false;
165}
166
167//===----------------------------------------------------------------------===//
168// Top-Level Entities
169//===----------------------------------------------------------------------===//
170
171bool LLParser::ParseTopLevelEntities() {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000172 while (1) {
173 switch (Lex.getKind()) {
174 default: return TokError("expected top-level entity");
175 case lltok::Eof: return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000176 case lltok::kw_declare: if (ParseDeclare()) return true; break;
177 case lltok::kw_define: if (ParseDefine()) return true; break;
178 case lltok::kw_module: if (ParseModuleAsm()) return true; break;
179 case lltok::kw_target: if (ParseTargetDefinition()) return true; break;
Bill Wendling706d3d62012-11-28 08:41:48 +0000180 case lltok::kw_deplibs: if (ParseDepLibs()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000181 case lltok::LocalVarID: if (ParseUnnamedType()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000182 case lltok::LocalVar: if (ParseNamedType()) return true; break;
Dan Gohman466876b2009-08-12 23:32:33 +0000183 case lltok::GlobalID: if (ParseUnnamedGlobal()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000184 case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break;
David Majnemerdad0a642014-06-27 18:19:56 +0000185 case lltok::ComdatVar: if (parseComdat()) return true; break;
Chris Lattner1eed2d62009-12-30 04:56:59 +0000186 case lltok::exclaim: if (ParseStandaloneMetadata()) return true; break;
Bill Wendling63b88192013-02-06 06:52:58 +0000187 case lltok::MetadataVar:if (ParseNamedMetadata()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000188
189 // The Global variable production with no name can have many different
190 // optional leading prefixes, the production is:
Nico Rieck7157bb72014-01-14 15:22:47 +0000191 // GlobalVar ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000192 // OptionalThreadLocal OptionalAddrSpace OptionalUnnamedAddr
Rafael Espindola45e6c192011-01-08 16:42:36 +0000193 // ('constant'|'global') ...
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000194 case lltok::kw_private: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000195 case lltok::kw_internal: // OptionalLinkage
196 case lltok::kw_weak: // OptionalLinkage
197 case lltok::kw_weak_odr: // OptionalLinkage
198 case lltok::kw_linkonce: // OptionalLinkage
199 case lltok::kw_linkonce_odr: // OptionalLinkage
200 case lltok::kw_appending: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000201 case lltok::kw_common: // OptionalLinkage
Bill Wendling03bcd6e2010-07-01 21:55:59 +0000202 case lltok::kw_extern_weak: // OptionalLinkage
Rafael Espindola52b74422014-06-03 20:00:20 +0000203 case lltok::kw_external: // OptionalLinkage
204 case lltok::kw_default: // OptionalVisibility
205 case lltok::kw_hidden: // OptionalVisibility
206 case lltok::kw_protected: // OptionalVisibility
Rafael Espindola63e92fb2014-06-03 20:07:32 +0000207 case lltok::kw_dllimport: // OptionalDLLStorageClass
208 case lltok::kw_dllexport: // OptionalDLLStorageClass
Rafael Espindola52b74422014-06-03 20:00:20 +0000209 case lltok::kw_thread_local: // OptionalThreadLocal
210 case lltok::kw_addrspace: // OptionalAddrSpace
211 case lltok::kw_constant: // GlobalType
212 case lltok::kw_global: { // GlobalType
Nico Rieck7157bb72014-01-14 15:22:47 +0000213 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000214 bool UnnamedAddr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000215 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola52b74422014-06-03 20:00:20 +0000216 bool HasLinkage;
217 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000218 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000219 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000220 ParseOptionalThreadLocal(TLM) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000221 parseOptionalUnnamedAddr(UnnamedAddr) ||
Rafael Espindola52b74422014-06-03 20:00:20 +0000222 ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000223 DLLStorageClass, TLM, UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000224 return true;
225 break;
226 }
Bill Wendlinga7c38772013-02-09 15:48:49 +0000227
228 case lltok::kw_attributes: if (ParseUnnamedAttrGrp()) return true; break;
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +0000229 case lltok::kw_uselistorder: if (ParseUseListOrder()) return true; break;
230 case lltok::kw_uselistorder_bb:
231 if (ParseUseListOrderBB()) return true; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000232 }
233 }
234}
235
236
237/// toplevelentity
238/// ::= 'module' 'asm' STRINGCONSTANT
239bool LLParser::ParseModuleAsm() {
240 assert(Lex.getKind() == lltok::kw_module);
241 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000242
243 std::string AsmStr;
Chris Lattner3822f632009-01-02 08:05:26 +0000244 if (ParseToken(lltok::kw_asm, "expected 'module asm'") ||
245 ParseStringConstant(AsmStr)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000246
Rafael Espindola1e49a6d2011-03-02 04:14:42 +0000247 M->appendModuleInlineAsm(AsmStr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000248 return false;
249}
250
251/// toplevelentity
252/// ::= 'target' 'triple' '=' STRINGCONSTANT
253/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
254bool LLParser::ParseTargetDefinition() {
255 assert(Lex.getKind() == lltok::kw_target);
Chris Lattner3822f632009-01-02 08:05:26 +0000256 std::string Str;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000257 switch (Lex.Lex()) {
258 default: return TokError("unknown target property");
259 case lltok::kw_triple:
260 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000261 if (ParseToken(lltok::equal, "expected '=' after target triple") ||
262 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000263 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000264 M->setTargetTriple(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000265 return false;
266 case lltok::kw_datalayout:
267 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +0000268 if (ParseToken(lltok::equal, "expected '=' after target datalayout") ||
269 ParseStringConstant(Str))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000270 return true;
Chris Lattner3822f632009-01-02 08:05:26 +0000271 M->setDataLayout(Str);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000272 return false;
273 }
274}
275
Bill Wendling706d3d62012-11-28 08:41:48 +0000276/// toplevelentity
277/// ::= 'deplibs' '=' '[' ']'
278/// ::= 'deplibs' '=' '[' STRINGCONSTANT (',' STRINGCONSTANT)* ']'
279/// FIXME: Remove in 4.0. Currently parse, but ignore.
280bool LLParser::ParseDepLibs() {
281 assert(Lex.getKind() == lltok::kw_deplibs);
282 Lex.Lex();
283 if (ParseToken(lltok::equal, "expected '=' after deplibs") ||
284 ParseToken(lltok::lsquare, "expected '=' after deplibs"))
285 return true;
286
287 if (EatIfPresent(lltok::rsquare))
288 return false;
289
290 do {
291 std::string Str;
292 if (ParseStringConstant(Str)) return true;
293 } while (EatIfPresent(lltok::comma));
294
295 return ParseToken(lltok::rsquare, "expected ']' at end of list");
296}
297
Dan Gohman466876b2009-08-12 23:32:33 +0000298/// ParseUnnamedType:
Dan Gohman466876b2009-08-12 23:32:33 +0000299/// ::= LocalVarID '=' 'type' type
Chris Lattnerac161bf2009-01-02 07:01:27 +0000300bool LLParser::ParseUnnamedType() {
Chris Lattner07037362011-06-18 23:51:31 +0000301 LocTy TypeLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000302 unsigned TypeID = Lex.getUIntVal();
Chris Lattner8936d2b2011-06-19 00:03:46 +0000303 Lex.Lex(); // eat LocalVarID;
304
305 if (ParseToken(lltok::equal, "expected '=' after name") ||
306 ParseToken(lltok::kw_type, "expected 'type' after '='"))
307 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000308
Craig Topper2617dcc2014-04-15 06:32:26 +0000309 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000310 if (ParseStructDefinition(TypeLoc, "",
311 NumberedTypes[TypeID], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000312
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000313 if (!isa<StructType>(Result)) {
314 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
315 if (Entry.first)
316 return Error(TypeLoc, "non-struct types may not be recursive");
317 Entry.first = Result;
318 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000319 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000320
Chris Lattnerac161bf2009-01-02 07:01:27 +0000321 return false;
322}
323
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000324
Chris Lattnerac161bf2009-01-02 07:01:27 +0000325/// toplevelentity
326/// ::= LocalVar '=' 'type' type
327bool LLParser::ParseNamedType() {
328 std::string Name = Lex.getStrVal();
329 LocTy NameLoc = Lex.getLoc();
Chris Lattner3822f632009-01-02 08:05:26 +0000330 Lex.Lex(); // eat LocalVar.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000331
Chris Lattner3822f632009-01-02 08:05:26 +0000332 if (ParseToken(lltok::equal, "expected '=' after name") ||
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000333 ParseToken(lltok::kw_type, "expected 'type' after name"))
Chris Lattner3822f632009-01-02 08:05:26 +0000334 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000335
Craig Topper2617dcc2014-04-15 06:32:26 +0000336 Type *Result = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000337 if (ParseStructDefinition(NameLoc, Name,
338 NamedTypes[Name], Result)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000339
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000340 if (!isa<StructType>(Result)) {
341 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
342 if (Entry.first)
343 return Error(NameLoc, "non-struct types may not be recursive");
344 Entry.first = Result;
345 Entry.second = SMLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000346 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000347
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000348 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000349}
350
351
352/// toplevelentity
353/// ::= 'declare' FunctionHeader
354bool LLParser::ParseDeclare() {
355 assert(Lex.getKind() == lltok::kw_declare);
356 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000357
Chris Lattnerac161bf2009-01-02 07:01:27 +0000358 Function *F;
359 return ParseFunctionHeader(F, false);
360}
361
362/// toplevelentity
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000363/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
Chris Lattnerac161bf2009-01-02 07:01:27 +0000364bool LLParser::ParseDefine() {
365 assert(Lex.getKind() == lltok::kw_define);
366 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000367
Chris Lattnerac161bf2009-01-02 07:01:27 +0000368 Function *F;
Chris Lattner3822f632009-01-02 08:05:26 +0000369 return ParseFunctionHeader(F, true) ||
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000370 ParseOptionalFunctionMetadata(*F) ||
Chris Lattner3822f632009-01-02 08:05:26 +0000371 ParseFunctionBody(*F);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000372}
373
Chris Lattner3822f632009-01-02 08:05:26 +0000374/// ParseGlobalType
375/// ::= 'constant'
376/// ::= 'global'
Chris Lattnerac161bf2009-01-02 07:01:27 +0000377bool LLParser::ParseGlobalType(bool &IsConstant) {
378 if (Lex.getKind() == lltok::kw_constant)
379 IsConstant = true;
380 else if (Lex.getKind() == lltok::kw_global)
381 IsConstant = false;
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000382 else {
383 IsConstant = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000384 return TokError("expected 'global' or 'constant'");
Duncan Sandsd1de45a2009-02-10 16:24:55 +0000385 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000386 Lex.Lex();
387 return false;
388}
389
Dan Gohman466876b2009-08-12 23:32:33 +0000390/// ParseUnnamedGlobal:
391/// OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000392/// OptionalLinkage OptionalVisibility OptionalDLLStorageClass
393/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000394/// GlobalID '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000395/// GlobalID '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
396/// ... -> global variable
Dan Gohman466876b2009-08-12 23:32:33 +0000397bool LLParser::ParseUnnamedGlobal() {
398 unsigned VarID = NumberedVals.size();
399 std::string Name;
400 LocTy NameLoc = Lex.getLoc();
401
402 // Handle the GlobalID form.
403 if (Lex.getKind() == lltok::GlobalID) {
404 if (Lex.getUIntVal() != VarID)
405 return Error(Lex.getLoc(), "variable expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +0000406 Twine(VarID) + "'");
Dan Gohman466876b2009-08-12 23:32:33 +0000407 Lex.Lex(); // eat GlobalID;
408
409 if (ParseToken(lltok::equal, "expected '=' after name"))
410 return true;
411 }
412
413 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000414 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000415 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000416 bool UnnamedAddr;
Dan Gohman466876b2009-08-12 23:32:33 +0000417 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000418 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000419 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000420 ParseOptionalThreadLocal(TLM) ||
421 parseOptionalUnnamedAddr(UnnamedAddr))
Dan Gohman466876b2009-08-12 23:32:33 +0000422 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000423
Rafael Espindola464fe022014-07-30 22:51:54 +0000424 if (Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000425 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000426 DLLStorageClass, TLM, UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000427 return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000428 UnnamedAddr);
Dan Gohman466876b2009-08-12 23:32:33 +0000429}
430
Chris Lattnerac161bf2009-01-02 07:01:27 +0000431/// ParseNamedGlobal:
432/// GlobalVar '=' OptionalVisibility ALIAS ...
Nico Rieck7157bb72014-01-14 15:22:47 +0000433/// GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
434/// ... -> global variable
Chris Lattnerac161bf2009-01-02 07:01:27 +0000435bool LLParser::ParseNamedGlobal() {
436 assert(Lex.getKind() == lltok::GlobalVar);
437 LocTy NameLoc = Lex.getLoc();
438 std::string Name = Lex.getStrVal();
439 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000440
Chris Lattnerac161bf2009-01-02 07:01:27 +0000441 bool HasLinkage;
Nico Rieck7157bb72014-01-14 15:22:47 +0000442 unsigned Linkage, Visibility, DLLStorageClass;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000443 GlobalVariable::ThreadLocalMode TLM;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000444 bool UnnamedAddr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000445 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
446 ParseOptionalLinkage(Linkage, HasLinkage) ||
Nico Rieck7157bb72014-01-14 15:22:47 +0000447 ParseOptionalVisibility(Visibility) ||
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000448 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000449 ParseOptionalThreadLocal(TLM) ||
450 parseOptionalUnnamedAddr(UnnamedAddr))
Chris Lattnerac161bf2009-01-02 07:01:27 +0000451 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000452
Rafael Espindola464fe022014-07-30 22:51:54 +0000453 if (Lex.getKind() != lltok::kw_alias)
Nico Rieck7157bb72014-01-14 15:22:47 +0000454 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000455 DLLStorageClass, TLM, UnnamedAddr);
Rafael Espindola464fe022014-07-30 22:51:54 +0000456
457 return ParseAlias(Name, NameLoc, Linkage, Visibility, DLLStorageClass, TLM,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000458 UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000459}
460
David Majnemerdad0a642014-06-27 18:19:56 +0000461bool LLParser::parseComdat() {
462 assert(Lex.getKind() == lltok::ComdatVar);
463 std::string Name = Lex.getStrVal();
464 LocTy NameLoc = Lex.getLoc();
465 Lex.Lex();
466
467 if (ParseToken(lltok::equal, "expected '=' here"))
468 return true;
469
470 if (ParseToken(lltok::kw_comdat, "expected comdat keyword"))
471 return TokError("expected comdat type");
472
473 Comdat::SelectionKind SK;
474 switch (Lex.getKind()) {
475 default:
476 return TokError("unknown selection kind");
477 case lltok::kw_any:
478 SK = Comdat::Any;
479 break;
480 case lltok::kw_exactmatch:
481 SK = Comdat::ExactMatch;
482 break;
483 case lltok::kw_largest:
484 SK = Comdat::Largest;
485 break;
486 case lltok::kw_noduplicates:
487 SK = Comdat::NoDuplicates;
488 break;
489 case lltok::kw_samesize:
490 SK = Comdat::SameSize;
491 break;
492 }
493 Lex.Lex();
494
495 // See if the comdat was forward referenced, if so, use the comdat.
496 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
497 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
498 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(Name))
499 return Error(NameLoc, "redefinition of comdat '$" + Name + "'");
500
501 Comdat *C;
502 if (I != ComdatSymTab.end())
503 C = &I->second;
504 else
505 C = M->getOrInsertComdat(Name);
506 C->setSelectionKind(SK);
507
508 return false;
509}
510
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000511// MDString:
512// ::= '!' STRINGCONSTANT
Chris Lattner1797fc72009-12-29 21:53:55 +0000513bool LLParser::ParseMDString(MDString *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000514 std::string Str;
515 if (ParseStringConstant(Str)) return true;
Eli Bendersky5d5e18d2014-06-25 15:41:00 +0000516 llvm::UpgradeMDStringConstant(Str);
Chris Lattner1797fc72009-12-29 21:53:55 +0000517 Result = MDString::get(Context, Str);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000518 return false;
519}
520
521// MDNode:
522// ::= '!' MDNodeNumber
Chris Lattner6dac02a2009-12-30 04:15:23 +0000523bool LLParser::ParseMDNodeID(MDNode *&Result) {
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000524 // !{ ..., !42, ... }
525 unsigned MID = 0;
Duncan P. N. Exon Smitha8d9a022015-01-12 21:14:38 +0000526 if (ParseUInt32(MID))
527 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000528
Chris Lattner8eff0152010-04-01 05:14:45 +0000529 // If not a forward reference, just return it now.
David Majnemer19b51052015-02-11 07:43:56 +0000530 if (NumberedMetadata.count(MID)) {
Duncan P. N. Exon Smitha8d9a022015-01-12 21:14:38 +0000531 Result = NumberedMetadata[MID];
532 return false;
533 }
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000534
Chris Lattner8eff0152010-04-01 05:14:45 +0000535 // Otherwise, create MDNode forward reference.
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000536 auto &FwdRef = ForwardRefMDNodes[MID];
537 FwdRef = std::make_pair(MDTuple::getTemporary(Context, None), Lex.getLoc());
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000538
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000539 Result = FwdRef.first.get();
540 NumberedMetadata[MID].reset(Result);
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000541 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000542}
Devang Patel8ff0f8d2009-07-20 19:00:08 +0000543
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000544/// ParseNamedMetadata:
Devang Patelbe626972009-07-29 00:34:02 +0000545/// !foo = !{ !1, !2 }
546bool LLParser::ParseNamedMetadata() {
Chris Lattnereafe4de2009-12-30 05:02:06 +0000547 assert(Lex.getKind() == lltok::MetadataVar);
Devang Patelbe626972009-07-29 00:34:02 +0000548 std::string Name = Lex.getStrVal();
Chris Lattnereafe4de2009-12-30 05:02:06 +0000549 Lex.Lex();
Devang Patelbe626972009-07-29 00:34:02 +0000550
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000551 if (ParseToken(lltok::equal, "expected '=' here") ||
Chris Lattner1eed2d62009-12-30 04:56:59 +0000552 ParseToken(lltok::exclaim, "Expected '!' here") ||
Chris Lattnerf2fe7ff2009-12-29 22:35:39 +0000553 ParseToken(lltok::lbrace, "Expected '{' here"))
Devang Patelbe626972009-07-29 00:34:02 +0000554 return true;
555
Dan Gohman2637cc12010-07-21 23:38:33 +0000556 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000557 if (Lex.getKind() != lltok::rbrace)
558 do {
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000559 if (ParseToken(lltok::exclaim, "Expected '!' here"))
560 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000561
Craig Topper2617dcc2014-04-15 06:32:26 +0000562 MDNode *N = nullptr;
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000563 if (ParseMDNodeID(N)) return true;
Dan Gohman2637cc12010-07-21 23:38:33 +0000564 NMD->addOperand(N);
Dan Gohmanafd69cf2010-07-13 19:42:44 +0000565 } while (EatIfPresent(lltok::comma));
Devang Patelbe626972009-07-29 00:34:02 +0000566
Rafael Espindolac7818fb2015-05-26 20:37:36 +0000567 return ParseToken(lltok::rbrace, "expected end of metadata node");
Devang Patelbe626972009-07-29 00:34:02 +0000568}
569
Devang Patel39e64d42009-07-01 19:21:12 +0000570/// ParseStandaloneMetadata:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000571/// !42 = !{...}
Devang Patel39e64d42009-07-01 19:21:12 +0000572bool LLParser::ParseStandaloneMetadata() {
Chris Lattner1eed2d62009-12-30 04:56:59 +0000573 assert(Lex.getKind() == lltok::exclaim);
Devang Patel39e64d42009-07-01 19:21:12 +0000574 Lex.Lex();
575 unsigned MetadataID = 0;
Devang Patel39e64d42009-07-01 19:21:12 +0000576
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000577 MDNode *Init;
Chris Lattner278bc952009-12-29 22:40:21 +0000578 if (ParseUInt32(MetadataID) ||
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +0000579 ParseToken(lltok::equal, "expected '=' here"))
580 return true;
581
582 // Detect common error, from old metadata syntax.
583 if (Lex.getKind() == lltok::Type)
584 return TokError("unexpected type in metadata definition");
585
Duncan P. N. Exon Smith090a19b2015-01-08 22:38:29 +0000586 bool IsDistinct = EatIfPresent(lltok::kw_distinct);
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +0000587 if (Lex.getKind() == lltok::MetadataVar) {
588 if (ParseSpecializedMDNode(Init, IsDistinct))
589 return true;
590 } else if (ParseToken(lltok::exclaim, "Expected '!' here") ||
591 ParseMDTuple(Init, IsDistinct))
Devang Patele059ba6e2009-07-23 01:07:34 +0000592 return true;
593
Chris Lattnerfc58af22009-12-30 04:51:58 +0000594 // See if this was forward referenced, if so, handle it.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000595 auto FI = ForwardRefMDNodes.find(MetadataID);
Devang Pateld2541152009-07-08 19:23:54 +0000596 if (FI != ForwardRefMDNodes.end()) {
Duncan P. N. Exon Smith7d823132015-01-19 21:30:18 +0000597 FI->second.first->replaceAllUsesWith(Init);
Devang Pateld2541152009-07-08 19:23:54 +0000598 ForwardRefMDNodes.erase(FI);
Michael Ilseman26ee2b82012-11-15 22:34:00 +0000599
Chris Lattnerfc58af22009-12-30 04:51:58 +0000600 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
601 } else {
David Majnemer19b51052015-02-11 07:43:56 +0000602 if (NumberedMetadata.count(MetadataID))
Chris Lattnerfc58af22009-12-30 04:51:58 +0000603 return TokError("Metadata id is already used");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000604 NumberedMetadata[MetadataID].reset(Init);
Devang Pateld2541152009-07-08 19:23:54 +0000605 }
606
Devang Patel39e64d42009-07-01 19:21:12 +0000607 return false;
608}
609
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000610static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
611 return !GlobalValue::isLocalLinkage((GlobalValue::LinkageTypes)L) ||
612 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
613}
614
Chris Lattnerac161bf2009-01-02 07:01:27 +0000615/// ParseAlias:
Rafael Espindola464fe022014-07-30 22:51:54 +0000616/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility
617/// OptionalDLLStorageClass OptionalThreadLocal
Eric Christopher536f0a92015-05-28 23:07:39 +0000618/// OptionalUnnamedAddr 'alias' Aliasee
Rafael Espindola6b238632014-05-16 19:35:39 +0000619///
Chris Lattnerac161bf2009-01-02 07:01:27 +0000620/// Aliasee
Chris Lattner4c73d7a2009-04-25 21:26:00 +0000621/// ::= TypeAndValue
Chris Lattnerac161bf2009-01-02 07:01:27 +0000622///
Eric Christopher536f0a92015-05-28 23:07:39 +0000623/// Everything through OptionalUnnamedAddr has already been parsed.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000624///
Rafael Espindola464fe022014-07-30 22:51:54 +0000625bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc, unsigned L,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000626 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000627 GlobalVariable::ThreadLocalMode TLM,
628 bool UnnamedAddr) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000629 assert(Lex.getKind() == lltok::kw_alias);
630 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +0000631
Rafael Espindola78527052013-10-06 15:10:43 +0000632 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
633
Rafael Espindolacaa43562013-10-09 16:07:32 +0000634 if(!GlobalAlias::isValidLinkage(Linkage))
Rafael Espindola464fe022014-07-30 22:51:54 +0000635 return Error(NameLoc, "invalid linkage type for alias");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000636
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000637 if (!isValidVisibilityForLinkage(Visibility, L))
Rafael Espindola464fe022014-07-30 22:51:54 +0000638 return Error(NameLoc,
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000639 "symbol with local linkage must have default visibility");
640
Rafael Espindola64c1e182014-06-03 02:41:57 +0000641 Constant *Aliasee;
642 LocTy AliaseeLoc = Lex.getLoc();
643 if (Lex.getKind() != lltok::kw_bitcast &&
644 Lex.getKind() != lltok::kw_getelementptr &&
645 Lex.getKind() != lltok::kw_addrspacecast &&
646 Lex.getKind() != lltok::kw_inttoptr) {
647 if (ParseGlobalTypeAndValue(Aliasee))
Rafael Espindola6b238632014-05-16 19:35:39 +0000648 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000649 } else {
Rafael Espindola64c1e182014-06-03 02:41:57 +0000650 // The bitcast dest type is not present, it is implied by the dest type.
651 ValID ID;
652 if (ParseValID(ID))
653 return true;
654 if (ID.Kind != ValID::t_Constant)
655 return Error(AliaseeLoc, "invalid aliasee");
656 Aliasee = ID.ConstantVal;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000657 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000658
Rafael Espindola64c1e182014-06-03 02:41:57 +0000659 Type *AliaseeType = Aliasee->getType();
660 auto *PTy = dyn_cast<PointerType>(AliaseeType);
661 if (!PTy)
662 return Error(AliaseeLoc, "An alias must have pointer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000663
664 // Okay, create the alias but do not insert it into the module yet.
Rafael Espindola4fe00942014-05-16 13:34:04 +0000665 std::unique_ptr<GlobalAlias> GA(
David Blaikief64246b2015-04-29 21:22:39 +0000666 GlobalAlias::create(PTy, (GlobalValue::LinkageTypes)Linkage, Name,
667 Aliasee, /*Parent*/ nullptr));
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000668 GA->setThreadLocalMode(TLM);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000669 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000670 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000671 GA->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000672
Rafael Espindola54fc2982015-06-17 17:53:31 +0000673 if (Name.empty())
674 NumberedVals.push_back(GA.get());
675
Chris Lattnerac161bf2009-01-02 07:01:27 +0000676 // See if this value already exists in the symbol table. If so, it is either
677 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000678 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000679 // See if this was a redefinition. If so, there is no entry in
680 // ForwardRefVals.
681 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
682 I = ForwardRefVals.find(Name);
683 if (I == ForwardRefVals.end())
684 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
685
686 // Otherwise, this was a definition of forward ref. Verify that types
687 // agree.
688 if (Val->getType() != GA->getType())
689 return Error(NameLoc,
690 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000691
Chris Lattnerac161bf2009-01-02 07:01:27 +0000692 // If they agree, just RAUW the old value with the alias and remove the
693 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000694 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000695 Val->eraseFromParent();
696 ForwardRefVals.erase(I);
697 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000698
Chris Lattnerac161bf2009-01-02 07:01:27 +0000699 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000700 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000701 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000702
Rafael Espindolaaa273822014-05-09 21:49:17 +0000703 // The module owns this now
704 GA.release();
705
Chris Lattnerac161bf2009-01-02 07:01:27 +0000706 return false;
707}
708
709/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000710/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000711/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000712/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000713/// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000714/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000715/// OptionalExternallyInitialized GlobalType Type Const
Chris Lattnerac161bf2009-01-02 07:01:27 +0000716///
Eric Christopher536f0a92015-05-28 23:07:39 +0000717/// Everything up to and including OptionalUnnamedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000718/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000719///
720bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
721 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000722 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000723 GlobalVariable::ThreadLocalMode TLM,
724 bool UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000725 if (!isValidVisibilityForLinkage(Visibility, Linkage))
726 return Error(NameLoc,
727 "symbol with local linkage must have default visibility");
728
Chris Lattnerac161bf2009-01-02 07:01:27 +0000729 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000730 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000731 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000732 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000733
Craig Topper2617dcc2014-04-15 06:32:26 +0000734 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000735 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000736 ParseOptionalToken(lltok::kw_externally_initialized,
737 IsExternallyInitialized,
738 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000739 ParseGlobalType(IsConstant) ||
740 ParseType(Ty, TyLoc))
741 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000742
Chris Lattnerac161bf2009-01-02 07:01:27 +0000743 // If the linkage is specified and is external, then no initializer is
744 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000745 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000746 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000747 Linkage != GlobalValue::ExternalLinkage)) {
748 if (ParseGlobalValue(Ty, Init))
749 return true;
750 }
751
David Majnemer49b3d9b2015-02-16 08:41:08 +0000752 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000753 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000754
David Majnemer598bd052014-12-09 05:56:09 +0000755 GlobalValue *GVal = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000756
757 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000758 if (!Name.empty()) {
David Majnemer598bd052014-12-09 05:56:09 +0000759 GVal = M->getNamedValue(Name);
760 if (GVal) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000761 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
762 return Error(NameLoc, "redefinition of global '@" + Name + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +0000763 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000764 } else {
765 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
766 I = ForwardRefValIDs.find(NumberedVals.size());
767 if (I != ForwardRefValIDs.end()) {
David Majnemer598bd052014-12-09 05:56:09 +0000768 GVal = I->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000769 ForwardRefValIDs.erase(I);
770 }
771 }
772
David Majnemer598bd052014-12-09 05:56:09 +0000773 GlobalVariable *GV;
774 if (!GVal) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000775 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
776 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000777 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000778 } else {
David Blaikie8f27ae42015-05-13 22:55:01 +0000779 if (GVal->getValueType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000780 return Error(TyLoc,
781 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000782
David Majnemer598bd052014-12-09 05:56:09 +0000783 GV = cast<GlobalVariable>(GVal);
784
Chris Lattnerac161bf2009-01-02 07:01:27 +0000785 // Move the forward-reference to the correct spot in the module.
786 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
787 }
788
789 if (Name.empty())
790 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000791
Chris Lattnerac161bf2009-01-02 07:01:27 +0000792 // Set the parsed properties on the global.
793 if (Init)
794 GV->setInitializer(Init);
795 GV->setConstant(IsConstant);
796 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
797 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000798 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000799 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000800 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000801 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000802
Chris Lattnerac161bf2009-01-02 07:01:27 +0000803 // Parse attributes on the global.
804 while (Lex.getKind() == lltok::comma) {
805 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000806
Chris Lattnerac161bf2009-01-02 07:01:27 +0000807 if (Lex.getKind() == lltok::kw_section) {
808 Lex.Lex();
809 GV->setSection(Lex.getStrVal());
810 if (ParseToken(lltok::StringConstant, "expected global section string"))
811 return true;
812 } else if (Lex.getKind() == lltok::kw_align) {
813 unsigned Alignment;
814 if (ParseOptionalAlignment(Alignment)) return true;
815 GV->setAlignment(Alignment);
816 } else {
David Majnemerdad0a642014-06-27 18:19:56 +0000817 Comdat *C;
Rafael Espindola83a362c2015-01-06 22:55:16 +0000818 if (parseOptionalComdat(Name, C))
David Majnemerdad0a642014-06-27 18:19:56 +0000819 return true;
820 if (C)
821 GV->setComdat(C);
822 else
823 return TokError("unknown global variable property!");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000824 }
825 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000826
Chris Lattnerac161bf2009-01-02 07:01:27 +0000827 return false;
828}
829
Bill Wendling63b88192013-02-06 06:52:58 +0000830/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000831/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000832bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000833 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000834 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000835 Lex.Lex();
836
David Majnemerb39e22b2014-12-09 18:33:57 +0000837 if (Lex.getKind() != lltok::AttrGrpID)
838 return TokError("expected attribute group id");
839
Bill Wendling63b88192013-02-06 06:52:58 +0000840 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000841 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000842 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000843 Lex.Lex();
844
845 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000846 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000847 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000848 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000849 ParseToken(lltok::rbrace, "expected end of attribute group"))
850 return true;
851
Bill Wendlingb32b0412013-02-08 06:32:06 +0000852 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000853 return Error(AttrGrpLoc, "attribute group has no attributes");
854
855 return false;
856}
857
Bill Wendling8b0321d2013-02-08 00:52:31 +0000858/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000859/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000860bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
861 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000862 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000863 bool HaveError = false;
864
865 B.clear();
866
Bill Wendling63b88192013-02-06 06:52:58 +0000867 while (true) {
868 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000869 if (Token == lltok::kw_builtin)
870 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000871 switch (Token) {
872 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000873 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000874 return Error(Lex.getLoc(), "unterminated attribute group");
875 case lltok::rbrace:
876 // Finished.
877 return false;
878
Bill Wendlingb32b0412013-02-08 06:32:06 +0000879 case lltok::AttrGrpID: {
880 // Allow a function to reference an attribute group:
881 //
882 // define void @foo() #1 { ... }
883 if (inAttrGrp)
884 HaveError |=
885 Error(Lex.getLoc(),
886 "cannot have an attribute group reference in an attribute group");
887
888 unsigned AttrGrpNum = Lex.getUIntVal();
889 if (inAttrGrp) break;
890
891 // Save the reference to the attribute group. We'll fill it in later.
892 FwdRefAttrGrps.push_back(AttrGrpNum);
893 break;
894 }
Bill Wendling63b88192013-02-06 06:52:58 +0000895 // Target-dependent attributes:
896 case lltok::StringConstant: {
897 std::string Attr = Lex.getStrVal();
898 Lex.Lex();
899 std::string Val;
900 if (EatIfPresent(lltok::equal) &&
901 ParseStringConstant(Val))
902 return true;
903
904 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000905 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000906 }
907
908 // Target-independent attributes:
909 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000910 // As a hack, we allow function alignment to be initially parsed as an
911 // attribute on a function declaration/definition or added to an attribute
912 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000913 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000914 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000915 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000916 if (ParseToken(lltok::equal, "expected '=' here") ||
917 ParseUInt32(Alignment))
918 return true;
919 } else {
920 if (ParseOptionalAlignment(Alignment))
921 return true;
922 }
Bill Wendling63b88192013-02-06 06:52:58 +0000923 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000924 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000925 }
926 case lltok::kw_alignstack: {
927 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000928 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000929 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000930 if (ParseToken(lltok::equal, "expected '=' here") ||
931 ParseUInt32(Alignment))
932 return true;
933 } else {
934 if (ParseOptionalStackAlignment(Alignment))
935 return true;
936 }
Bill Wendling63b88192013-02-06 06:52:58 +0000937 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000938 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000939 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000940 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000941 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000942 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000943 case lltok::kw_convergent: B.addAttribute(Attribute::Convergent); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000944 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000945 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000946 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
947 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
948 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
949 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
950 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
951 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
952 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
953 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
954 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
955 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000956 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000957 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
958 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
959 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
960 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
961 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
962 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
963 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
Peter Collingbourne82437bf2015-06-15 21:07:11 +0000964 case lltok::kw_safestack: B.addAttribute(Attribute::SafeStack); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000965 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
966 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
967 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
968 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000969
970 // Error handling.
971 case lltok::kw_inreg:
972 case lltok::kw_signext:
973 case lltok::kw_zeroext:
974 HaveError |=
975 Error(Lex.getLoc(),
976 "invalid use of attribute on a function");
977 break;
978 case lltok::kw_byval:
Hal Finkelb0407ba2014-07-18 15:51:28 +0000979 case lltok::kw_dereferenceable:
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000980 case lltok::kw_dereferenceable_or_null:
Reid Klecknera534a382013-12-19 02:14:12 +0000981 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000982 case lltok::kw_nest:
983 case lltok::kw_noalias:
984 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000985 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +0000986 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000987 case lltok::kw_sret:
988 HaveError |=
989 Error(Lex.getLoc(),
990 "invalid use of parameter-only attribute on a function");
991 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000992 }
993
994 Lex.Lex();
995 }
996}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000997
998//===----------------------------------------------------------------------===//
999// GlobalValue Reference/Resolution Routines.
1000//===----------------------------------------------------------------------===//
1001
1002/// GetGlobalVal - Get a value with the specified name or ID, creating a
1003/// forward reference record if needed. This can return null if the value
1004/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001005GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001006 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001007 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001008 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001009 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001010 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001011 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001012
Chris Lattnerac161bf2009-01-02 07:01:27 +00001013 // Look this name up in the normal function symbol table.
1014 GlobalValue *Val =
1015 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001016
Chris Lattnerac161bf2009-01-02 07:01:27 +00001017 // If this is a forward reference for the value, see if we already created a
1018 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001019 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001020 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1021 I = ForwardRefVals.find(Name);
1022 if (I != ForwardRefVals.end())
1023 Val = I->second.first;
1024 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001025
Chris Lattnerac161bf2009-01-02 07:01:27 +00001026 // If we have the value in the symbol table or fwd-ref table, return it.
1027 if (Val) {
1028 if (Val->getType() == Ty) return Val;
1029 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001030 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001031 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001032 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001033
Chris Lattnerac161bf2009-01-02 07:01:27 +00001034 // Otherwise, create a new forward reference for this value and remember it.
1035 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001036 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001037 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001038 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001039 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001040 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1041 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001042 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001043
Chris Lattnerac161bf2009-01-02 07:01:27 +00001044 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1045 return FwdVal;
1046}
1047
Chris Lattner229907c2011-07-18 04:54:35 +00001048GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1049 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001050 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001051 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001052 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001053 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001054
Craig Topper2617dcc2014-04-15 06:32:26 +00001055 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001056
Chris Lattnerac161bf2009-01-02 07:01:27 +00001057 // If this is a forward reference for the value, see if we already created a
1058 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001059 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001060 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1061 I = ForwardRefValIDs.find(ID);
1062 if (I != ForwardRefValIDs.end())
1063 Val = I->second.first;
1064 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001065
Chris Lattnerac161bf2009-01-02 07:01:27 +00001066 // If we have the value in the symbol table or fwd-ref table, return it.
1067 if (Val) {
1068 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001069 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001070 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001071 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001072 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001073
Chris Lattnerac161bf2009-01-02 07:01:27 +00001074 // Otherwise, create a new forward reference for this value and remember it.
1075 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001076 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001077 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001078 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001079 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001080 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001081
Chris Lattnerac161bf2009-01-02 07:01:27 +00001082 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1083 return FwdVal;
1084}
1085
1086
1087//===----------------------------------------------------------------------===//
David Majnemerdad0a642014-06-27 18:19:56 +00001088// Comdat Reference/Resolution Routines.
1089//===----------------------------------------------------------------------===//
1090
1091Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1092 // Look this name up in the comdat symbol table.
1093 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1094 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1095 if (I != ComdatSymTab.end())
1096 return &I->second;
1097
1098 // Otherwise, create a new forward reference for this value and remember it.
1099 Comdat *C = M->getOrInsertComdat(Name);
1100 ForwardRefComdats[Name] = Loc;
1101 return C;
1102}
1103
1104
1105//===----------------------------------------------------------------------===//
Chris Lattnerac161bf2009-01-02 07:01:27 +00001106// Helper Routines.
1107//===----------------------------------------------------------------------===//
1108
1109/// ParseToken - If the current token has the specified kind, eat it and return
1110/// success. Otherwise, emit the specified error and return failure.
1111bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1112 if (Lex.getKind() != T)
1113 return TokError(ErrMsg);
1114 Lex.Lex();
1115 return false;
1116}
1117
Chris Lattner3822f632009-01-02 08:05:26 +00001118/// ParseStringConstant
1119/// ::= StringConstant
1120bool LLParser::ParseStringConstant(std::string &Result) {
1121 if (Lex.getKind() != lltok::StringConstant)
1122 return TokError("expected string constant");
1123 Result = Lex.getStrVal();
1124 Lex.Lex();
1125 return false;
1126}
1127
1128/// ParseUInt32
1129/// ::= uint32
1130bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001131 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1132 return TokError("expected integer");
1133 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1134 if (Val64 != unsigned(Val64))
1135 return TokError("expected 32-bit integer (too large)");
1136 Val = Val64;
1137 Lex.Lex();
1138 return false;
1139}
1140
Hal Finkelb0407ba2014-07-18 15:51:28 +00001141/// ParseUInt64
1142/// ::= uint64
1143bool LLParser::ParseUInt64(uint64_t &Val) {
1144 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1145 return TokError("expected integer");
1146 Val = Lex.getAPSIntVal().getLimitedValue();
1147 Lex.Lex();
1148 return false;
1149}
1150
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001151/// ParseTLSModel
1152/// := 'localdynamic'
1153/// := 'initialexec'
1154/// := 'localexec'
1155bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1156 switch (Lex.getKind()) {
1157 default:
1158 return TokError("expected localdynamic, initialexec or localexec");
1159 case lltok::kw_localdynamic:
1160 TLM = GlobalVariable::LocalDynamicTLSModel;
1161 break;
1162 case lltok::kw_initialexec:
1163 TLM = GlobalVariable::InitialExecTLSModel;
1164 break;
1165 case lltok::kw_localexec:
1166 TLM = GlobalVariable::LocalExecTLSModel;
1167 break;
1168 }
1169
1170 Lex.Lex();
1171 return false;
1172}
1173
1174/// ParseOptionalThreadLocal
1175/// := /*empty*/
1176/// := 'thread_local'
1177/// := 'thread_local' '(' tlsmodel ')'
1178bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1179 TLM = GlobalVariable::NotThreadLocal;
1180 if (!EatIfPresent(lltok::kw_thread_local))
1181 return false;
1182
1183 TLM = GlobalVariable::GeneralDynamicTLSModel;
1184 if (Lex.getKind() == lltok::lparen) {
1185 Lex.Lex();
1186 return ParseTLSModel(TLM) ||
1187 ParseToken(lltok::rparen, "expected ')' after thread local model");
1188 }
1189 return false;
1190}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001191
1192/// ParseOptionalAddrSpace
1193/// := /*empty*/
1194/// := 'addrspace' '(' uint32 ')'
1195bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1196 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001197 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001198 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001199 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001200 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001201 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001202}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001203
Bill Wendling34c2eb22012-12-04 23:40:58 +00001204/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1205bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1206 bool HaveError = false;
1207
1208 B.clear();
1209
1210 while (1) {
1211 lltok::Kind Token = Lex.getKind();
1212 switch (Token) {
1213 default: // End of attributes.
1214 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001215 case lltok::kw_align: {
1216 unsigned Alignment;
1217 if (ParseOptionalAlignment(Alignment))
1218 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001219 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001220 continue;
1221 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001222 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001223 case lltok::kw_dereferenceable: {
1224 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001225 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001226 return true;
1227 B.addDereferenceableAttr(Bytes);
1228 continue;
1229 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001230 case lltok::kw_dereferenceable_or_null: {
1231 uint64_t Bytes;
1232 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1233 return true;
1234 B.addDereferenceableOrNullAttr(Bytes);
1235 continue;
1236 }
Reid Klecknera534a382013-12-19 02:14:12 +00001237 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001238 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1239 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1240 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1241 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001242 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001243 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1244 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001245 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001246 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1247 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1248 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001249
Stephen Lin7577ed52013-04-20 13:16:13 +00001250 case lltok::kw_alignstack:
1251 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001252 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001253 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001254 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001255 case lltok::kw_minsize:
1256 case lltok::kw_naked:
1257 case lltok::kw_nobuiltin:
1258 case lltok::kw_noduplicate:
1259 case lltok::kw_noimplicitfloat:
1260 case lltok::kw_noinline:
1261 case lltok::kw_nonlazybind:
1262 case lltok::kw_noredzone:
1263 case lltok::kw_noreturn:
1264 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001265 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001266 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001267 case lltok::kw_returns_twice:
1268 case lltok::kw_sanitize_address:
1269 case lltok::kw_sanitize_memory:
1270 case lltok::kw_sanitize_thread:
1271 case lltok::kw_ssp:
1272 case lltok::kw_sspreq:
1273 case lltok::kw_sspstrong:
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001274 case lltok::kw_safestack:
Stephen Lin7577ed52013-04-20 13:16:13 +00001275 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001276 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1277 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001278 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001279
Bill Wendling34c2eb22012-12-04 23:40:58 +00001280 Lex.Lex();
1281 }
1282}
1283
1284/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1285bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1286 bool HaveError = false;
1287
1288 B.clear();
1289
1290 while (1) {
1291 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001292 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001293 default: // End of attributes.
1294 return HaveError;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001295 case lltok::kw_dereferenceable: {
1296 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001297 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001298 return true;
1299 B.addDereferenceableAttr(Bytes);
1300 continue;
1301 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001302 case lltok::kw_dereferenceable_or_null: {
1303 uint64_t Bytes;
1304 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1305 return true;
1306 B.addDereferenceableOrNullAttr(Bytes);
1307 continue;
1308 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001309 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1310 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001311 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001312 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1313 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001314
Bill Wendling34c2eb22012-12-04 23:40:58 +00001315 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001316 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001317 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001318 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001319 case lltok::kw_nest:
1320 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001321 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001322 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001323 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001324 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001325
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001326 case lltok::kw_alignstack:
1327 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001328 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001329 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001330 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001331 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001332 case lltok::kw_minsize:
1333 case lltok::kw_naked:
1334 case lltok::kw_nobuiltin:
1335 case lltok::kw_noduplicate:
1336 case lltok::kw_noimplicitfloat:
1337 case lltok::kw_noinline:
1338 case lltok::kw_nonlazybind:
1339 case lltok::kw_noredzone:
1340 case lltok::kw_noreturn:
1341 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001342 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001343 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001344 case lltok::kw_returns_twice:
1345 case lltok::kw_sanitize_address:
1346 case lltok::kw_sanitize_memory:
1347 case lltok::kw_sanitize_thread:
1348 case lltok::kw_ssp:
1349 case lltok::kw_sspreq:
1350 case lltok::kw_sspstrong:
Peter Collingbourne82437bf2015-06-15 21:07:11 +00001351 case lltok::kw_safestack:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001352 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001353 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001354 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001355
1356 case lltok::kw_readnone:
1357 case lltok::kw_readonly:
1358 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001359 }
1360
Chris Lattnerac161bf2009-01-02 07:01:27 +00001361 Lex.Lex();
1362 }
1363}
1364
1365/// ParseOptionalLinkage
1366/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001367/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001368/// ::= 'internal'
1369/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001370/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001371/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001372/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001373/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001374/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001375/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001376/// ::= 'extern_weak'
1377/// ::= 'external'
1378bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1379 HasLinkage = false;
1380 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001381 default: Res=GlobalValue::ExternalLinkage; return false;
1382 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001383 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1384 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1385 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1386 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1387 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001388 case lltok::kw_available_externally:
1389 Res = GlobalValue::AvailableExternallyLinkage;
1390 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001391 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001392 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001393 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1394 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001395 }
1396 Lex.Lex();
1397 HasLinkage = true;
1398 return false;
1399}
1400
1401/// ParseOptionalVisibility
1402/// ::= /*empty*/
1403/// ::= 'default'
1404/// ::= 'hidden'
1405/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001406///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001407bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1408 switch (Lex.getKind()) {
1409 default: Res = GlobalValue::DefaultVisibility; return false;
1410 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1411 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1412 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1413 }
1414 Lex.Lex();
1415 return false;
1416}
1417
Nico Rieck7157bb72014-01-14 15:22:47 +00001418/// ParseOptionalDLLStorageClass
1419/// ::= /*empty*/
1420/// ::= 'dllimport'
1421/// ::= 'dllexport'
1422///
1423bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1424 switch (Lex.getKind()) {
1425 default: Res = GlobalValue::DefaultStorageClass; return false;
1426 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1427 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1428 }
1429 Lex.Lex();
1430 return false;
1431}
1432
Chris Lattnerac161bf2009-01-02 07:01:27 +00001433/// ParseOptionalCallingConv
1434/// ::= /*empty*/
1435/// ::= 'ccc'
1436/// ::= 'fastcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001437/// ::= 'intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001438/// ::= 'coldcc'
1439/// ::= 'x86_stdcallcc'
1440/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001441/// ::= 'x86_thiscallcc'
Reid Kleckner9ccce992014-10-28 01:29:26 +00001442/// ::= 'x86_vectorcallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001443/// ::= 'arm_apcscc'
1444/// ::= 'arm_aapcscc'
1445/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001446/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001447/// ::= 'ptx_kernel'
1448/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001449/// ::= 'spir_func'
1450/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001451/// ::= 'x86_64_sysvcc'
1452/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001453/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001454/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001455/// ::= 'preserve_mostcc'
1456/// ::= 'preserve_allcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001457/// ::= 'ghccc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001458/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001459///
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001460bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001461 switch (Lex.getKind()) {
1462 default: CC = CallingConv::C; return false;
1463 case lltok::kw_ccc: CC = CallingConv::C; break;
1464 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1465 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1466 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1467 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001468 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +00001469 case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001470 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1471 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1472 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001473 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001474 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1475 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001476 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1477 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001478 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001479 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1480 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001481 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001482 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001483 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1484 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Reid Kleckner35fc3632014-12-01 21:04:44 +00001485 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001486 case lltok::kw_cc: {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001487 Lex.Lex();
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001488 return ParseUInt32(CC);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001489 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001490 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001491
Chris Lattnerac161bf2009-01-02 07:01:27 +00001492 Lex.Lex();
1493 return false;
1494}
1495
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001496/// ParseMetadataAttachment
1497/// ::= !dbg !42
1498bool LLParser::ParseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
1499 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
1500
1501 std::string Name = Lex.getStrVal();
1502 Kind = M->getMDKindID(Name);
1503 Lex.Lex();
1504
1505 return ParseMDNode(MD);
1506}
1507
Chris Lattner5c427632009-12-30 05:31:19 +00001508/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001509/// ::= !dbg !42 (',' !dbg !57)*
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001510bool LLParser::ParseInstructionMetadata(Instruction &Inst) {
Chris Lattner5c427632009-12-30 05:31:19 +00001511 do {
1512 if (Lex.getKind() != lltok::MetadataVar)
1513 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001514
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001515 unsigned MDK;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00001516 MDNode *N;
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001517 if (ParseMetadataAttachment(MDK, N))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001518 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001519
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001520 Inst.setMetadata(MDK, N);
Manman Ren209b17c2013-09-28 00:22:27 +00001521 if (MDK == LLVMContext::MD_tbaa)
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001522 InstsWithTBAATag.push_back(&Inst);
Manman Ren209b17c2013-09-28 00:22:27 +00001523
Chris Lattner596760d2009-12-29 21:25:40 +00001524 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001525 } while (EatIfPresent(lltok::comma));
1526 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001527}
1528
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001529/// ParseOptionalFunctionMetadata
1530/// ::= (!dbg !57)*
1531bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
1532 while (Lex.getKind() == lltok::MetadataVar) {
1533 unsigned MDK;
1534 MDNode *N;
1535 if (ParseMetadataAttachment(MDK, N))
1536 return true;
1537
1538 F.setMetadata(MDK, N);
1539 }
1540 return false;
1541}
1542
Chris Lattnerac161bf2009-01-02 07:01:27 +00001543/// ParseOptionalAlignment
1544/// ::= /* empty */
1545/// ::= 'align' 4
1546bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1547 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001548 if (!EatIfPresent(lltok::kw_align))
1549 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001550 LocTy AlignLoc = Lex.getLoc();
1551 if (ParseUInt32(Alignment)) return true;
1552 if (!isPowerOf2_32(Alignment))
1553 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001554 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001555 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001556 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001557}
1558
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001559/// ParseOptionalDerefAttrBytes
Hal Finkelb0407ba2014-07-18 15:51:28 +00001560/// ::= /* empty */
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001561/// ::= AttrKind '(' 4 ')'
1562///
1563/// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
1564bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
1565 uint64_t &Bytes) {
1566 assert((AttrKind == lltok::kw_dereferenceable ||
1567 AttrKind == lltok::kw_dereferenceable_or_null) &&
1568 "contract!");
1569
Hal Finkelb0407ba2014-07-18 15:51:28 +00001570 Bytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001571 if (!EatIfPresent(AttrKind))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001572 return false;
1573 LocTy ParenLoc = Lex.getLoc();
1574 if (!EatIfPresent(lltok::lparen))
1575 return Error(ParenLoc, "expected '('");
1576 LocTy DerefLoc = Lex.getLoc();
1577 if (ParseUInt64(Bytes)) return true;
1578 ParenLoc = Lex.getLoc();
1579 if (!EatIfPresent(lltok::rparen))
1580 return Error(ParenLoc, "expected ')'");
1581 if (!Bytes)
1582 return Error(DerefLoc, "dereferenceable bytes must be non-zero");
1583 return false;
1584}
1585
Chris Lattnerb2f39502009-12-30 05:44:30 +00001586/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001587/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001588/// ::= ',' align 4
1589///
1590/// This returns with AteExtraComma set to true if it ate an excess comma at the
1591/// end.
1592bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1593 bool &AteExtraComma) {
1594 AteExtraComma = false;
1595 while (EatIfPresent(lltok::comma)) {
1596 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001597 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001598 AteExtraComma = true;
1599 return false;
1600 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001601
Chris Lattner95b0ff42010-04-23 00:50:50 +00001602 if (Lex.getKind() != lltok::kw_align)
1603 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001604
Chris Lattner95b0ff42010-04-23 00:50:50 +00001605 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001606 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001607
Devang Patelea8a4b92009-09-17 23:04:48 +00001608 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001609}
1610
Eli Friedmanfee02c62011-07-25 23:16:38 +00001611/// ParseScopeAndOrdering
1612/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1613/// else: ::=
1614///
1615/// This sets Scope and Ordering to the parsed values.
1616bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1617 AtomicOrdering &Ordering) {
1618 if (!isAtomic)
1619 return false;
1620
1621 Scope = CrossThread;
1622 if (EatIfPresent(lltok::kw_singlethread))
1623 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001624
1625 return ParseOrdering(Ordering);
1626}
1627
1628/// ParseOrdering
1629/// ::= AtomicOrdering
1630///
1631/// This sets Ordering to the parsed value.
1632bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001633 switch (Lex.getKind()) {
1634 default: return TokError("Expected ordering on atomic instruction");
1635 case lltok::kw_unordered: Ordering = Unordered; break;
1636 case lltok::kw_monotonic: Ordering = Monotonic; break;
1637 case lltok::kw_acquire: Ordering = Acquire; break;
1638 case lltok::kw_release: Ordering = Release; break;
1639 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1640 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1641 }
1642 Lex.Lex();
1643 return false;
1644}
1645
Charles Davisbe5557e2010-02-12 00:31:15 +00001646/// ParseOptionalStackAlignment
1647/// ::= /* empty */
1648/// ::= 'alignstack' '(' 4 ')'
1649bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1650 Alignment = 0;
1651 if (!EatIfPresent(lltok::kw_alignstack))
1652 return false;
1653 LocTy ParenLoc = Lex.getLoc();
1654 if (!EatIfPresent(lltok::lparen))
1655 return Error(ParenLoc, "expected '('");
1656 LocTy AlignLoc = Lex.getLoc();
1657 if (ParseUInt32(Alignment)) return true;
1658 ParenLoc = Lex.getLoc();
1659 if (!EatIfPresent(lltok::rparen))
1660 return Error(ParenLoc, "expected ')'");
1661 if (!isPowerOf2_32(Alignment))
1662 return Error(AlignLoc, "stack alignment is not a power of two");
1663 return false;
1664}
Devang Patelea8a4b92009-09-17 23:04:48 +00001665
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001666/// ParseIndexList - This parses the index list for an insert/extractvalue
1667/// instruction. This sets AteExtraComma in the case where we eat an extra
1668/// comma at the end of the line and find that it is followed by metadata.
1669/// Clients that don't allow metadata can call the version of this function that
1670/// only takes one argument.
1671///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001672/// ParseIndexList
1673/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001674///
1675bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1676 bool &AteExtraComma) {
1677 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001678
Chris Lattnerac161bf2009-01-02 07:01:27 +00001679 if (Lex.getKind() != lltok::comma)
1680 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001681
Chris Lattner3822f632009-01-02 08:05:26 +00001682 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001683 if (Lex.getKind() == lltok::MetadataVar) {
David Majnemer7ccc34d2015-02-16 09:18:13 +00001684 if (Indices.empty()) return TokError("expected index");
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001685 AteExtraComma = true;
1686 return false;
1687 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001688 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001689 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001690 Indices.push_back(Idx);
1691 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001692
Chris Lattnerac161bf2009-01-02 07:01:27 +00001693 return false;
1694}
1695
1696//===----------------------------------------------------------------------===//
1697// Type Parsing.
1698//===----------------------------------------------------------------------===//
1699
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001700/// ParseType - Parse a type.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001701bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001702 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001703 switch (Lex.getKind()) {
1704 default:
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001705 return TokError(Msg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001706 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001707 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001708 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001709 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001710 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001711 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001712 // Type ::= StructType
1713 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001714 return true;
1715 break;
1716 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001717 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001718 Lex.Lex(); // eat the lsquare.
1719 if (ParseArrayVectorType(Result, false))
1720 return true;
1721 break;
1722 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001723 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001724 Lex.Lex();
1725 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001726 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001727 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001728 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001729 } else if (ParseArrayVectorType(Result, true))
1730 return true;
1731 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001732 case lltok::LocalVar: {
1733 // Type ::= %foo
1734 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001735
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001736 // If the type hasn't been defined yet, create a forward definition and
1737 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001738 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001739 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001740 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001741 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001742 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001743 Lex.Lex();
1744 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001745 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001746
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001747 case lltok::LocalVarID: {
1748 // Type ::= %4
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001749 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001750
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001751 // If the type hasn't been defined yet, create a forward definition and
1752 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001753 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001754 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001755 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001756 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001757 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001758 Lex.Lex();
1759 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001760 }
1761 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001762
1763 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001764 while (1) {
1765 switch (Lex.getKind()) {
1766 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001767 default:
1768 if (!AllowVoid && Result->isVoidTy())
1769 return Error(TypeLoc, "void type only allowed for function results");
1770 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001771
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001772 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001773 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001774 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001775 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001776 if (Result->isVoidTy())
1777 return TokError("pointers to void are invalid - use i8* instead");
1778 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001779 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001780 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001781 Lex.Lex();
1782 break;
1783
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001784 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001785 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001786 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001787 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001788 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001789 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001790 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001791 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001792 unsigned AddrSpace;
1793 if (ParseOptionalAddrSpace(AddrSpace) ||
1794 ParseToken(lltok::star, "expected '*' in address space"))
1795 return true;
1796
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001797 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001798 break;
1799 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001800
Chris Lattnerac161bf2009-01-02 07:01:27 +00001801 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1802 case lltok::lparen:
1803 if (ParseFunctionType(Result))
1804 return true;
1805 break;
1806 }
1807 }
1808}
1809
1810/// ParseParameterList
1811/// ::= '(' ')'
1812/// ::= '(' Arg (',' Arg)* ')'
1813/// Arg
1814/// ::= Type OptionalAttributes Value OptionalAttributes
1815bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +00001816 PerFunctionState &PFS, bool IsMustTailCall,
1817 bool InVarArgsFunc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001818 if (ParseToken(lltok::lparen, "expected '(' in call"))
1819 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001820
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001821 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001822 while (Lex.getKind() != lltok::rparen) {
1823 // If this isn't the first argument, we need a comma.
1824 if (!ArgList.empty() &&
1825 ParseToken(lltok::comma, "expected ',' in argument list"))
1826 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001827
Reid Kleckner83498642014-08-26 00:33:28 +00001828 // Parse an ellipsis if this is a musttail call in a variadic function.
1829 if (Lex.getKind() == lltok::dotdotdot) {
1830 const char *Msg = "unexpected ellipsis in argument list for ";
1831 if (!IsMustTailCall)
1832 return TokError(Twine(Msg) + "non-musttail call");
1833 if (!InVarArgsFunc)
1834 return TokError(Twine(Msg) + "musttail call in non-varargs function");
1835 Lex.Lex(); // Lex the '...', it is purely for readability.
1836 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
1837 }
1838
Chris Lattnerac161bf2009-01-02 07:01:27 +00001839 // Parse the argument.
1840 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001841 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001842 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001843 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001844 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001845 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001846
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001847 if (ArgTy->isMetadataTy()) {
1848 if (ParseMetadataAsValue(V, PFS))
1849 return true;
1850 } else {
1851 // Otherwise, handle normal operands.
1852 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
1853 return true;
1854 }
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001855 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1856 AttrIndex++,
1857 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001858 }
1859
Reid Kleckner83498642014-08-26 00:33:28 +00001860 if (IsMustTailCall && InVarArgsFunc)
1861 return TokError("expected '...' at end of argument list for musttail call "
1862 "in varargs function");
1863
Chris Lattnerac161bf2009-01-02 07:01:27 +00001864 Lex.Lex(); // Lex the ')'.
1865 return false;
1866}
1867
1868
1869
Chris Lattner2ed06b42009-01-05 18:34:07 +00001870/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001871/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001872/// ::= '(' ArgTypeListI ')'
1873/// ArgTypeListI
1874/// ::= /*empty*/
1875/// ::= '...'
1876/// ::= ArgTypeList ',' '...'
1877/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001878///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001879bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1880 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001881 isVarArg = false;
1882 assert(Lex.getKind() == lltok::lparen);
1883 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001884
Chris Lattnerac161bf2009-01-02 07:01:27 +00001885 if (Lex.getKind() == lltok::rparen) {
1886 // empty
1887 } else if (Lex.getKind() == lltok::dotdotdot) {
1888 isVarArg = true;
1889 Lex.Lex();
1890 } else {
1891 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001892 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001893 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001894 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001895
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001896 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001897 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001898
Chris Lattnerfdd87902009-10-05 05:54:46 +00001899 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001900 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001901
Chris Lattnerdef19492011-06-17 06:36:20 +00001902 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001903 Name = Lex.getStrVal();
1904 Lex.Lex();
1905 }
Chris Lattner3822f632009-01-02 08:05:26 +00001906
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001907 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001908 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001909
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001910 unsigned AttrIndex = 1;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001911 ArgList.emplace_back(TypeLoc, ArgTy, AttributeSet::get(ArgTy->getContext(),
1912 AttrIndex++, Attrs),
1913 std::move(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001914
Chris Lattner3822f632009-01-02 08:05:26 +00001915 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001916 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001917 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001918 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001919 break;
1920 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001921
Chris Lattnerac161bf2009-01-02 07:01:27 +00001922 // Otherwise must be an argument type.
1923 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001924 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001925
Chris Lattnerfdd87902009-10-05 05:54:46 +00001926 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001927 return Error(TypeLoc, "argument can not have void type");
1928
Chris Lattnerdef19492011-06-17 06:36:20 +00001929 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001930 Name = Lex.getStrVal();
1931 Lex.Lex();
1932 } else {
1933 Name = "";
1934 }
Chris Lattner3822f632009-01-02 08:05:26 +00001935
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001936 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001937 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001938
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001939 ArgList.emplace_back(
1940 TypeLoc, ArgTy,
1941 AttributeSet::get(ArgTy->getContext(), AttrIndex++, Attrs),
1942 std::move(Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001943 }
1944 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001945
Chris Lattner3822f632009-01-02 08:05:26 +00001946 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001947}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001948
Chris Lattnerac161bf2009-01-02 07:01:27 +00001949/// ParseFunctionType
1950/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001951bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001952 assert(Lex.getKind() == lltok::lparen);
1953
Chris Lattnerce473c72009-01-05 08:04:33 +00001954 if (!FunctionType::isValidReturnType(Result))
1955 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001956
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001957 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001958 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001959 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001960 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001961
Chris Lattnerac161bf2009-01-02 07:01:27 +00001962 // Reject names on the arguments lists.
1963 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1964 if (!ArgList[i].Name.empty())
1965 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001966 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001967 return Error(ArgList[i].Loc,
1968 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001969 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001970
Jay Foadb804a2b2011-07-12 14:06:48 +00001971 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001972 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001973 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001974
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001975 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001976 return false;
1977}
1978
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001979/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1980/// other structs.
1981bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1982 SmallVector<Type*, 8> Elts;
1983 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001984
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001985 Result = StructType::get(Context, Elts, Packed);
1986 return false;
1987}
1988
1989/// ParseStructDefinition - Parse a struct in a 'type' definition.
1990bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1991 std::pair<Type*, LocTy> &Entry,
1992 Type *&ResultTy) {
1993 // If the type was already defined, diagnose the redefinition.
1994 if (Entry.first && !Entry.second.isValid())
1995 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001996
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001997 // If we have opaque, just return without filling in the definition for the
1998 // struct. This counts as a definition as far as the .ll file goes.
1999 if (EatIfPresent(lltok::kw_opaque)) {
2000 // This type is being defined, so clear the location to indicate this.
2001 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002002
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002003 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002004 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002005 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002006 ResultTy = Entry.first;
2007 return false;
2008 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002009
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002010 // If the type starts with '<', then it is either a packed struct or a vector.
2011 bool isPacked = EatIfPresent(lltok::less);
2012
2013 // If we don't have a struct, then we have a random type alias, which we
2014 // accept for compatibility with old files. These types are not allowed to be
2015 // forward referenced and not allowed to be recursive.
2016 if (Lex.getKind() != lltok::lbrace) {
2017 if (Entry.first)
2018 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002019
Craig Topper2617dcc2014-04-15 06:32:26 +00002020 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002021 if (isPacked)
2022 return ParseArrayVectorType(ResultTy, true);
2023 return ParseType(ResultTy);
2024 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002025
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002026 // This type is being defined, so clear the location to indicate this.
2027 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002028
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002029 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002030 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002031 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002032
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002033 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002034
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002035 SmallVector<Type*, 8> Body;
2036 if (ParseStructBody(Body) ||
2037 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
2038 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002039
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002040 STy->setBody(Body, isPacked);
2041 ResultTy = STy;
2042 return false;
2043}
2044
2045
Chris Lattnerac161bf2009-01-02 07:01:27 +00002046/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002047/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00002048/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002049/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002050/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002051/// ::= '<' '{' Type (',' Type)* '}' '>'
2052bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002053 assert(Lex.getKind() == lltok::lbrace);
2054 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002055
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002056 // Handle the empty struct.
2057 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002058 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002059
Chris Lattnerf880ca22009-03-09 04:49:14 +00002060 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002061 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002062 if (ParseType(Ty)) return true;
2063 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002064
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002065 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002066 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002067
Chris Lattner3822f632009-01-02 08:05:26 +00002068 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00002069 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002070 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002071
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002072 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002073 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002074
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002075 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002076 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002077
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002078 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002079}
2080
2081/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2082/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002083/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002084/// ::= '[' APSINTVAL 'x' Types ']'
2085/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002086bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002087 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2088 Lex.getAPSIntVal().getBitWidth() > 64)
2089 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002090
Chris Lattnerac161bf2009-01-02 07:01:27 +00002091 LocTy SizeLoc = Lex.getLoc();
2092 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002093 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002094
Chris Lattner3822f632009-01-02 08:05:26 +00002095 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2096 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002097
2098 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002099 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002100 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002101
Chris Lattner3822f632009-01-02 08:05:26 +00002102 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2103 "expected end of sequential type"))
2104 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002105
Chris Lattnerac161bf2009-01-02 07:01:27 +00002106 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002107 if (Size == 0)
2108 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002109 if ((unsigned)Size != Size)
2110 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002111 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002112 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002113 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002114 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002115 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002116 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002117 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002118 }
2119 return false;
2120}
2121
2122//===----------------------------------------------------------------------===//
2123// Function Semantic Analysis.
2124//===----------------------------------------------------------------------===//
2125
Chris Lattner3432c622009-10-28 03:39:23 +00002126LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2127 int functionNumber)
2128 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002129
2130 // Insert unnamed arguments into the NumberedVals list.
2131 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2132 AI != E; ++AI)
2133 if (!AI->hasName())
2134 NumberedVals.push_back(AI);
2135}
2136
2137LLParser::PerFunctionState::~PerFunctionState() {
2138 // If there were any forward referenced non-basicblock values, delete them.
2139 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2140 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2141 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002142 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002143 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002144 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002145 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002146 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002147
Chris Lattnerac161bf2009-01-02 07:01:27 +00002148 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2149 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2150 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002151 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002152 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002153 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002154 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002155 }
2156}
2157
Chris Lattner3432c622009-10-28 03:39:23 +00002158bool LLParser::PerFunctionState::FinishFunction() {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002159 if (!ForwardRefVals.empty())
2160 return P.Error(ForwardRefVals.begin()->second.second,
2161 "use of undefined value '%" + ForwardRefVals.begin()->first +
2162 "'");
2163 if (!ForwardRefValIDs.empty())
2164 return P.Error(ForwardRefValIDs.begin()->second.second,
2165 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002166 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002167 return false;
2168}
2169
2170
2171/// GetVal - Get a value with the specified name or ID, creating a
2172/// forward reference record if needed. This can return null if the value
2173/// exists but does not have the right type.
2174Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002175 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002176 // Look this name up in the normal function symbol table.
2177 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002178
Chris Lattnerac161bf2009-01-02 07:01:27 +00002179 // If this is a forward reference for the value, see if we already created a
2180 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002181 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002182 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2183 I = ForwardRefVals.find(Name);
2184 if (I != ForwardRefVals.end())
2185 Val = I->second.first;
2186 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002187
Chris Lattnerac161bf2009-01-02 07:01:27 +00002188 // If we have the value in the symbol table or fwd-ref table, return it.
2189 if (Val) {
2190 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002191 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002192 P.Error(Loc, "'%" + Name + "' is not a basic block");
2193 else
2194 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002195 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002196 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002197 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002198
Chris Lattnerac161bf2009-01-02 07:01:27 +00002199 // Don't make placeholders with invalid type.
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002200 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002201 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002202 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002203 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002204
Chris Lattnerac161bf2009-01-02 07:01:27 +00002205 // Otherwise, create a new forward reference for this value and remember it.
2206 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002207 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002208 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002209 else
2210 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002211
Chris Lattnerac161bf2009-01-02 07:01:27 +00002212 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2213 return FwdVal;
2214}
2215
Chris Lattner229907c2011-07-18 04:54:35 +00002216Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002217 LocTy Loc) {
2218 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002219 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002220
Chris Lattnerac161bf2009-01-02 07:01:27 +00002221 // If this is a forward reference for the value, see if we already created a
2222 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002223 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002224 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2225 I = ForwardRefValIDs.find(ID);
2226 if (I != ForwardRefValIDs.end())
2227 Val = I->second.first;
2228 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002229
Chris Lattnerac161bf2009-01-02 07:01:27 +00002230 // If we have the value in the symbol table or fwd-ref table, return it.
2231 if (Val) {
2232 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002233 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002234 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002235 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002236 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002237 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002238 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002239 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002240
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002241 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002242 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002243 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002244 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002245
Chris Lattnerac161bf2009-01-02 07:01:27 +00002246 // Otherwise, create a new forward reference for this value and remember it.
2247 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002248 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002249 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002250 else
2251 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002252
Chris Lattnerac161bf2009-01-02 07:01:27 +00002253 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2254 return FwdVal;
2255}
2256
2257/// SetInstName - After an instruction is parsed and inserted into its
2258/// basic block, this installs its name.
2259bool LLParser::PerFunctionState::SetInstName(int NameID,
2260 const std::string &NameStr,
2261 LocTy NameLoc, Instruction *Inst) {
2262 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002263 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002264 if (NameID != -1 || !NameStr.empty())
2265 return P.Error(NameLoc, "instructions returning void cannot have a name");
2266 return false;
2267 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002268
Chris Lattnerac161bf2009-01-02 07:01:27 +00002269 // If this was a numbered instruction, verify that the instruction is the
2270 // expected value and resolve any forward references.
2271 if (NameStr.empty()) {
2272 // If neither a name nor an ID was specified, just use the next ID.
2273 if (NameID == -1)
2274 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002275
Chris Lattnerac161bf2009-01-02 07:01:27 +00002276 if (unsigned(NameID) != NumberedVals.size())
2277 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002278 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002279
Chris Lattnerac161bf2009-01-02 07:01:27 +00002280 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2281 ForwardRefValIDs.find(NameID);
2282 if (FI != ForwardRefValIDs.end()) {
2283 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002284 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002285 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002286 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002287 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002288 ForwardRefValIDs.erase(FI);
2289 }
2290
2291 NumberedVals.push_back(Inst);
2292 return false;
2293 }
2294
2295 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2296 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2297 FI = ForwardRefVals.find(NameStr);
2298 if (FI != ForwardRefVals.end()) {
2299 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002300 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002301 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002302 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002303 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002304 ForwardRefVals.erase(FI);
2305 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002306
Chris Lattnerac161bf2009-01-02 07:01:27 +00002307 // Set the name on the instruction.
2308 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002309
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002310 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002311 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002312 NameStr + "'");
2313 return false;
2314}
2315
2316/// GetBB - Get a basic block with the specified name or ID, creating a
2317/// forward reference record if needed.
2318BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2319 LocTy Loc) {
Owen Anderson576a9a22015-03-02 05:25:09 +00002320 return dyn_cast_or_null<BasicBlock>(GetVal(Name,
2321 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002322}
2323
2324BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson576a9a22015-03-02 05:25:09 +00002325 return dyn_cast_or_null<BasicBlock>(GetVal(ID,
2326 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002327}
2328
2329/// DefineBB - Define the specified basic block, which is either named or
2330/// unnamed. If there is an error, this returns null otherwise it returns
2331/// the block being defined.
2332BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2333 LocTy Loc) {
2334 BasicBlock *BB;
2335 if (Name.empty())
2336 BB = GetBB(NumberedVals.size(), Loc);
2337 else
2338 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002339 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002340
Chris Lattnerac161bf2009-01-02 07:01:27 +00002341 // Move the block to the end of the function. Forward ref'd blocks are
2342 // inserted wherever they happen to be referenced.
2343 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002344
Chris Lattnerac161bf2009-01-02 07:01:27 +00002345 // Remove the block from forward ref sets.
2346 if (Name.empty()) {
2347 ForwardRefValIDs.erase(NumberedVals.size());
2348 NumberedVals.push_back(BB);
2349 } else {
2350 // BB forward references are already in the function symbol table.
2351 ForwardRefVals.erase(Name);
2352 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002353
Chris Lattnerac161bf2009-01-02 07:01:27 +00002354 return BB;
2355}
2356
2357//===----------------------------------------------------------------------===//
2358// Constants.
2359//===----------------------------------------------------------------------===//
2360
2361/// ParseValID - Parse an abstract value that doesn't necessarily have a
2362/// type implied. For example, if we parse "4" we don't know what integer type
2363/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002364/// sanity. PFS is used to convert function-local operands of metadata (since
2365/// metadata operands are not just parsed here but also converted to values).
2366/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002367bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002368 ID.Loc = Lex.getLoc();
2369 switch (Lex.getKind()) {
2370 default: return TokError("expected value token");
2371 case lltok::GlobalID: // @42
2372 ID.UIntVal = Lex.getUIntVal();
2373 ID.Kind = ValID::t_GlobalID;
2374 break;
2375 case lltok::GlobalVar: // @foo
2376 ID.StrVal = Lex.getStrVal();
2377 ID.Kind = ValID::t_GlobalName;
2378 break;
2379 case lltok::LocalVarID: // %42
2380 ID.UIntVal = Lex.getUIntVal();
2381 ID.Kind = ValID::t_LocalID;
2382 break;
2383 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002384 ID.StrVal = Lex.getStrVal();
2385 ID.Kind = ValID::t_LocalName;
2386 break;
2387 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002388 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002389 ID.Kind = ValID::t_APSInt;
2390 break;
2391 case lltok::APFloat:
2392 ID.APFloatVal = Lex.getAPFloatVal();
2393 ID.Kind = ValID::t_APFloat;
2394 break;
2395 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002396 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002397 ID.Kind = ValID::t_Constant;
2398 break;
2399 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002400 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002401 ID.Kind = ValID::t_Constant;
2402 break;
2403 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2404 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2405 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002406
Chris Lattnerac161bf2009-01-02 07:01:27 +00002407 case lltok::lbrace: {
2408 // ValID ::= '{' ConstVector '}'
2409 Lex.Lex();
2410 SmallVector<Constant*, 16> Elts;
2411 if (ParseGlobalValueVector(Elts) ||
2412 ParseToken(lltok::rbrace, "expected end of struct constant"))
2413 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002414
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002415 ID.ConstantStructElts = new Constant*[Elts.size()];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002416 ID.UIntVal = Elts.size();
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002417 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002418 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002419 return false;
2420 }
2421 case lltok::less: {
2422 // ValID ::= '<' ConstVector '>' --> Vector.
2423 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2424 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002425 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002426
Chris Lattnerac161bf2009-01-02 07:01:27 +00002427 SmallVector<Constant*, 16> Elts;
2428 LocTy FirstEltLoc = Lex.getLoc();
2429 if (ParseGlobalValueVector(Elts) ||
2430 (isPackedStruct &&
2431 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2432 ParseToken(lltok::greater, "expected end of constant"))
2433 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002434
Chris Lattnerac161bf2009-01-02 07:01:27 +00002435 if (isPackedStruct) {
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002436 ID.ConstantStructElts = new Constant*[Elts.size()];
2437 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002438 ID.UIntVal = Elts.size();
2439 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002440 return false;
2441 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002442
Chris Lattnerac161bf2009-01-02 07:01:27 +00002443 if (Elts.empty())
2444 return Error(ID.Loc, "constant vector must not be empty");
2445
Duncan Sands9dff9be2010-02-15 16:12:20 +00002446 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002447 !Elts[0]->getType()->isFloatingPointTy() &&
2448 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002449 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002450 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002451
Chris Lattnerac161bf2009-01-02 07:01:27 +00002452 // Verify that all the vector elements have the same type.
2453 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2454 if (Elts[i]->getType() != Elts[0]->getType())
2455 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002456 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002457 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002458
Chris Lattner69229312011-02-15 00:14:00 +00002459 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002460 ID.Kind = ValID::t_Constant;
2461 return false;
2462 }
2463 case lltok::lsquare: { // Array Constant
2464 Lex.Lex();
2465 SmallVector<Constant*, 16> Elts;
2466 LocTy FirstEltLoc = Lex.getLoc();
2467 if (ParseGlobalValueVector(Elts) ||
2468 ParseToken(lltok::rsquare, "expected end of array constant"))
2469 return true;
2470
2471 // Handle empty element.
2472 if (Elts.empty()) {
2473 // Use undef instead of an array because it's inconvenient to determine
2474 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002475 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002476 return false;
2477 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002478
Chris Lattnerac161bf2009-01-02 07:01:27 +00002479 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002480 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002481 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002482
Owen Anderson4056ca92009-07-29 22:17:13 +00002483 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002484
Chris Lattnerac161bf2009-01-02 07:01:27 +00002485 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002486 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002487 if (Elts[i]->getType() != Elts[0]->getType())
2488 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002489 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002490 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002491 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002492
Jay Foad83be3612011-06-22 09:24:39 +00002493 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002494 ID.Kind = ValID::t_Constant;
2495 return false;
2496 }
2497 case lltok::kw_c: // c "foo"
2498 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002499 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2500 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002501 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2502 ID.Kind = ValID::t_Constant;
2503 return false;
2504
2505 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002506 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2507 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002508 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002509 Lex.Lex();
2510 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002511 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002512 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002513 ParseStringConstant(ID.StrVal) ||
2514 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002515 ParseToken(lltok::StringConstant, "expected constraint string"))
2516 return true;
2517 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002518 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002519 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002520 ID.Kind = ValID::t_InlineAsm;
2521 return false;
2522 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002523
Chris Lattner3432c622009-10-28 03:39:23 +00002524 case lltok::kw_blockaddress: {
2525 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2526 Lex.Lex();
2527
2528 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002529
Chris Lattner3432c622009-10-28 03:39:23 +00002530 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2531 ParseValID(Fn) ||
2532 ParseToken(lltok::comma, "expected comma in block address expression")||
2533 ParseValID(Label) ||
2534 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2535 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002536
Chris Lattner3432c622009-10-28 03:39:23 +00002537 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2538 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002539 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002540 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002541
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002542 // Try to find the function (but skip it if it's forward-referenced).
2543 GlobalValue *GV = nullptr;
2544 if (Fn.Kind == ValID::t_GlobalID) {
2545 if (Fn.UIntVal < NumberedVals.size())
2546 GV = NumberedVals[Fn.UIntVal];
2547 } else if (!ForwardRefVals.count(Fn.StrVal)) {
2548 GV = M->getNamedValue(Fn.StrVal);
2549 }
2550 Function *F = nullptr;
2551 if (GV) {
2552 // Confirm that it's actually a function with a definition.
2553 if (!isa<Function>(GV))
2554 return Error(Fn.Loc, "expected function name in blockaddress");
2555 F = cast<Function>(GV);
2556 if (F->isDeclaration())
2557 return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
2558 }
2559
2560 if (!F) {
2561 // Make a global variable as a placeholder for this reference.
David Blaikieb9cc6592015-03-04 01:40:07 +00002562 GlobalValue *&FwdRef =
2563 ForwardRefBlockAddresses.insert(std::make_pair(
2564 std::move(Fn),
2565 std::map<ValID, GlobalValue *>()))
2566 .first->second.insert(std::make_pair(std::move(Label), nullptr))
2567 .first->second;
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002568 if (!FwdRef)
2569 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
2570 GlobalValue::InternalLinkage, nullptr, "");
2571 ID.ConstantVal = FwdRef;
2572 ID.Kind = ValID::t_Constant;
2573 return false;
2574 }
2575
2576 // We found the function; now find the basic block. Don't use PFS, since we
2577 // might be inside a constant expression.
2578 BasicBlock *BB;
2579 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
2580 if (Label.Kind == ValID::t_LocalID)
2581 BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
2582 else
2583 BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
2584 if (!BB)
2585 return Error(Label.Loc, "referenced value is not a basic block");
2586 } else {
2587 if (Label.Kind == ValID::t_LocalID)
2588 return Error(Label.Loc, "cannot take address of numeric label after "
2589 "the function is defined");
2590 BB = dyn_cast_or_null<BasicBlock>(
2591 F->getValueSymbolTable().lookup(Label.StrVal));
2592 if (!BB)
2593 return Error(Label.Loc, "referenced value is not a basic block");
2594 }
2595
2596 ID.ConstantVal = BlockAddress::get(F, BB);
Chris Lattner3432c622009-10-28 03:39:23 +00002597 ID.Kind = ValID::t_Constant;
2598 return false;
2599 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002600
Chris Lattnerac161bf2009-01-02 07:01:27 +00002601 case lltok::kw_trunc:
2602 case lltok::kw_zext:
2603 case lltok::kw_sext:
2604 case lltok::kw_fptrunc:
2605 case lltok::kw_fpext:
2606 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002607 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002608 case lltok::kw_uitofp:
2609 case lltok::kw_sitofp:
2610 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002611 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002612 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002613 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002614 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002615 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002616 Constant *SrcVal;
2617 Lex.Lex();
2618 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2619 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002620 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002621 ParseType(DestTy) ||
2622 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2623 return true;
2624 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2625 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002626 getTypeString(SrcVal->getType()) + "' to '" +
2627 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002628 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002629 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002630 ID.Kind = ValID::t_Constant;
2631 return false;
2632 }
2633 case lltok::kw_extractvalue: {
2634 Lex.Lex();
2635 Constant *Val;
2636 SmallVector<unsigned, 4> Indices;
2637 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2638 ParseGlobalTypeAndValue(Val) ||
2639 ParseIndexList(Indices) ||
2640 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2641 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002642
Chris Lattner392be582010-02-12 20:49:41 +00002643 if (!Val->getType()->isAggregateType())
2644 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002645 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002646 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002647 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002648 ID.Kind = ValID::t_Constant;
2649 return false;
2650 }
2651 case lltok::kw_insertvalue: {
2652 Lex.Lex();
2653 Constant *Val0, *Val1;
2654 SmallVector<unsigned, 4> Indices;
2655 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2656 ParseGlobalTypeAndValue(Val0) ||
2657 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2658 ParseGlobalTypeAndValue(Val1) ||
2659 ParseIndexList(Indices) ||
2660 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2661 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002662 if (!Val0->getType()->isAggregateType())
2663 return Error(ID.Loc, "insertvalue operand must be aggregate type");
David Majnemereba692d2015-02-23 07:13:52 +00002664 Type *IndexedType =
2665 ExtractValueInst::getIndexedType(Val0->getType(), Indices);
2666 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002667 return Error(ID.Loc, "invalid indices for insertvalue");
David Majnemereba692d2015-02-23 07:13:52 +00002668 if (IndexedType != Val1->getType())
2669 return Error(ID.Loc, "insertvalue operand and field disagree in type: '" +
2670 getTypeString(Val1->getType()) +
2671 "' instead of '" + getTypeString(IndexedType) +
2672 "'");
Jay Foad57aa6362011-07-13 10:26:04 +00002673 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002674 ID.Kind = ValID::t_Constant;
2675 return false;
2676 }
2677 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002678 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002679 unsigned PredVal, Opc = Lex.getUIntVal();
2680 Constant *Val0, *Val1;
2681 Lex.Lex();
2682 if (ParseCmpPredicate(PredVal, Opc) ||
2683 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2684 ParseGlobalTypeAndValue(Val0) ||
2685 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2686 ParseGlobalTypeAndValue(Val1) ||
2687 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2688 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002689
Chris Lattnerac161bf2009-01-02 07:01:27 +00002690 if (Val0->getType() != Val1->getType())
2691 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002692
Chris Lattnerac161bf2009-01-02 07:01:27 +00002693 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002694
Chris Lattnerac161bf2009-01-02 07:01:27 +00002695 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002696 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002697 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002698 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002699 } else {
2700 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002701 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002702 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002703 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002704 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002705 }
2706 ID.Kind = ValID::t_Constant;
2707 return false;
2708 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002709
Chris Lattnerac161bf2009-01-02 07:01:27 +00002710 // Binary Operators.
2711 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002712 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002713 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002714 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002715 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002716 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002717 case lltok::kw_udiv:
2718 case lltok::kw_sdiv:
2719 case lltok::kw_fdiv:
2720 case lltok::kw_urem:
2721 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002722 case lltok::kw_frem:
2723 case lltok::kw_shl:
2724 case lltok::kw_lshr:
2725 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002726 bool NUW = false;
2727 bool NSW = false;
2728 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002729 unsigned Opc = Lex.getUIntVal();
2730 Constant *Val0, *Val1;
2731 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002732 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002733 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2734 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002735 if (EatIfPresent(lltok::kw_nuw))
2736 NUW = true;
2737 if (EatIfPresent(lltok::kw_nsw)) {
2738 NSW = true;
2739 if (EatIfPresent(lltok::kw_nuw))
2740 NUW = true;
2741 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002742 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2743 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002744 if (EatIfPresent(lltok::kw_exact))
2745 Exact = true;
2746 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002747 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2748 ParseGlobalTypeAndValue(Val0) ||
2749 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2750 ParseGlobalTypeAndValue(Val1) ||
2751 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2752 return true;
2753 if (Val0->getType() != Val1->getType())
2754 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002755 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002756 if (NUW)
2757 return Error(ModifierLoc, "nuw only applies to integer operations");
2758 if (NSW)
2759 return Error(ModifierLoc, "nsw only applies to integer operations");
2760 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002761 // Check that the type is valid for the operator.
2762 switch (Opc) {
2763 case Instruction::Add:
2764 case Instruction::Sub:
2765 case Instruction::Mul:
2766 case Instruction::UDiv:
2767 case Instruction::SDiv:
2768 case Instruction::URem:
2769 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002770 case Instruction::Shl:
2771 case Instruction::AShr:
2772 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002773 if (!Val0->getType()->isIntOrIntVectorTy())
2774 return Error(ID.Loc, "constexpr requires integer operands");
2775 break;
2776 case Instruction::FAdd:
2777 case Instruction::FSub:
2778 case Instruction::FMul:
2779 case Instruction::FDiv:
2780 case Instruction::FRem:
2781 if (!Val0->getType()->isFPOrFPVectorTy())
2782 return Error(ID.Loc, "constexpr requires fp operands");
2783 break;
2784 default: llvm_unreachable("Unknown binary operator!");
2785 }
Dan Gohman1b849082009-09-07 23:54:19 +00002786 unsigned Flags = 0;
2787 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2788 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002789 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002790 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002791 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002792 ID.Kind = ValID::t_Constant;
2793 return false;
2794 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002795
Chris Lattnerac161bf2009-01-02 07:01:27 +00002796 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002797 case lltok::kw_and:
2798 case lltok::kw_or:
2799 case lltok::kw_xor: {
2800 unsigned Opc = Lex.getUIntVal();
2801 Constant *Val0, *Val1;
2802 Lex.Lex();
2803 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2804 ParseGlobalTypeAndValue(Val0) ||
2805 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2806 ParseGlobalTypeAndValue(Val1) ||
2807 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2808 return true;
2809 if (Val0->getType() != Val1->getType())
2810 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002811 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002812 return Error(ID.Loc,
2813 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002814 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002815 ID.Kind = ValID::t_Constant;
2816 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002817 }
2818
Chris Lattnerac161bf2009-01-02 07:01:27 +00002819 case lltok::kw_getelementptr:
2820 case lltok::kw_shufflevector:
2821 case lltok::kw_insertelement:
2822 case lltok::kw_extractelement:
2823 case lltok::kw_select: {
2824 unsigned Opc = Lex.getUIntVal();
2825 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002826 bool InBounds = false;
David Blaikief72d05b2015-03-13 18:20:45 +00002827 Type *Ty;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002828 Lex.Lex();
David Blaikief72d05b2015-03-13 18:20:45 +00002829
Dan Gohman1639c392009-07-27 21:53:46 +00002830 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002831 InBounds = EatIfPresent(lltok::kw_inbounds);
David Blaikief72d05b2015-03-13 18:20:45 +00002832
2833 if (ParseToken(lltok::lparen, "expected '(' in constantexpr"))
2834 return true;
2835
2836 LocTy ExplicitTypeLoc = Lex.getLoc();
2837 if (Opc == Instruction::GetElementPtr) {
2838 if (ParseType(Ty) ||
2839 ParseToken(lltok::comma, "expected comma after getelementptr's type"))
2840 return true;
2841 }
2842
2843 if (ParseGlobalValueVector(Elts) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002844 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2845 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002846
Chris Lattnerac161bf2009-01-02 07:01:27 +00002847 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002848 if (Elts.size() == 0 ||
2849 !Elts[0]->getType()->getScalarType()->isPointerTy())
David Majnemer00303b62015-02-22 23:14:52 +00002850 return Error(ID.Loc, "base of getelementptr must be a pointer");
2851
2852 Type *BaseType = Elts[0]->getType();
2853 auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
David Blaikief72d05b2015-03-13 18:20:45 +00002854 if (Ty != BasePointerType->getElementType())
2855 return Error(
2856 ExplicitTypeLoc,
2857 "explicit pointee type doesn't match operand's pointee type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002858
Jay Foaded8db7d2011-07-21 14:31:17 +00002859 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
David Majnemer00303b62015-02-22 23:14:52 +00002860 for (Constant *Val : Indices) {
2861 Type *ValTy = Val->getType();
2862 if (!ValTy->getScalarType()->isIntegerTy())
2863 return Error(ID.Loc, "getelementptr index must be an integer");
2864 if (ValTy->isVectorTy() != BaseType->isVectorTy())
2865 return Error(ID.Loc, "getelementptr index type missmatch");
2866 if (ValTy->isVectorTy()) {
2867 unsigned ValNumEl = cast<VectorType>(ValTy)->getNumElements();
2868 unsigned PtrNumEl = cast<VectorType>(BaseType)->getNumElements();
2869 if (ValNumEl != PtrNumEl)
2870 return Error(
2871 ID.Loc,
2872 "getelementptr vector index has a wrong number of elements");
2873 }
2874 }
2875
Owen Andersone90f9922015-03-10 06:34:57 +00002876 SmallPtrSet<const Type*, 4> Visited;
David Blaikiee169e822015-04-22 16:37:35 +00002877 if (!Indices.empty() && !Ty->isSized(&Visited))
David Majnemer00303b62015-02-22 23:14:52 +00002878 return Error(ID.Loc, "base element of getelementptr must be sized");
2879
David Blaikie4a2e73b2015-04-02 18:55:32 +00002880 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
David Majnemer00303b62015-02-22 23:14:52 +00002881 return Error(ID.Loc, "invalid getelementptr indices");
David Blaikie4a2e73b2015-04-02 18:55:32 +00002882 ID.ConstantVal =
2883 ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices, InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002884 } else if (Opc == Instruction::Select) {
2885 if (Elts.size() != 3)
2886 return Error(ID.Loc, "expected three operands to select");
2887 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2888 Elts[2]))
2889 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002890 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002891 } else if (Opc == Instruction::ShuffleVector) {
2892 if (Elts.size() != 3)
2893 return Error(ID.Loc, "expected three operands to shufflevector");
2894 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2895 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002896 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002897 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002898 } else if (Opc == Instruction::ExtractElement) {
2899 if (Elts.size() != 2)
2900 return Error(ID.Loc, "expected two operands to extractelement");
2901 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2902 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002903 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002904 } else {
2905 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2906 if (Elts.size() != 3)
2907 return Error(ID.Loc, "expected three operands to insertelement");
2908 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2909 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002910 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002911 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002912 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002913
Chris Lattnerac161bf2009-01-02 07:01:27 +00002914 ID.Kind = ValID::t_Constant;
2915 return false;
2916 }
2917 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002918
Chris Lattnerac161bf2009-01-02 07:01:27 +00002919 Lex.Lex();
2920 return false;
2921}
2922
2923/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002924bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002925 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002926 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002927 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002928 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002929 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002930 if (V && !(C = dyn_cast<Constant>(V)))
2931 return Error(ID.Loc, "global values must be constants");
2932 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002933}
2934
Victor Hernandez9d75c962010-01-11 22:31:58 +00002935bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002936 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002937 return ParseType(Ty) ||
2938 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002939}
2940
Rafael Espindola83a362c2015-01-06 22:55:16 +00002941bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
David Majnemerdad0a642014-06-27 18:19:56 +00002942 C = nullptr;
Rafael Espindola83a362c2015-01-06 22:55:16 +00002943
2944 LocTy KwLoc = Lex.getLoc();
David Majnemerdad0a642014-06-27 18:19:56 +00002945 if (!EatIfPresent(lltok::kw_comdat))
2946 return false;
Rafael Espindola83a362c2015-01-06 22:55:16 +00002947
2948 if (EatIfPresent(lltok::lparen)) {
2949 if (Lex.getKind() != lltok::ComdatVar)
2950 return TokError("expected comdat variable");
2951 C = getComdat(Lex.getStrVal(), Lex.getLoc());
2952 Lex.Lex();
2953 if (ParseToken(lltok::rparen, "expected ')' after comdat var"))
2954 return true;
2955 } else {
2956 if (GlobalName.empty())
2957 return TokError("comdat cannot be unnamed");
2958 C = getComdat(GlobalName, KwLoc);
2959 }
2960
David Majnemerdad0a642014-06-27 18:19:56 +00002961 return false;
2962}
2963
Victor Hernandez9d75c962010-01-11 22:31:58 +00002964/// ParseGlobalValueVector
2965/// ::= /*empty*/
2966/// ::= TypeAndValue (',' TypeAndValue)*
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002967bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
Victor Hernandez9d75c962010-01-11 22:31:58 +00002968 // Empty list.
2969 if (Lex.getKind() == lltok::rbrace ||
2970 Lex.getKind() == lltok::rsquare ||
2971 Lex.getKind() == lltok::greater ||
2972 Lex.getKind() == lltok::rparen)
2973 return false;
2974
2975 Constant *C;
2976 if (ParseGlobalTypeAndValue(C)) return true;
2977 Elts.push_back(C);
2978
2979 while (EatIfPresent(lltok::comma)) {
2980 if (ParseGlobalTypeAndValue(C)) return true;
2981 Elts.push_back(C);
2982 }
2983
2984 return false;
2985}
2986
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +00002987bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002988 SmallVector<Metadata *, 16> Elts;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00002989 if (ParseMDNodeVector(Elts))
Dan Gohmanc828c542010-08-24 02:24:03 +00002990 return true;
2991
Duncan P. N. Exon Smith0b31dd12015-01-12 22:27:39 +00002992 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002993 return false;
2994}
2995
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00002996/// MDNode:
2997/// ::= !{ ... }
2998/// ::= !7
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002999/// ::= !DILocation(...)
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003000bool LLParser::ParseMDNode(MDNode *&N) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003001 if (Lex.getKind() == lltok::MetadataVar)
3002 return ParseSpecializedMDNode(N);
3003
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003004 return ParseToken(lltok::exclaim, "expected '!' here") ||
3005 ParseMDNodeTail(N);
3006}
3007
3008bool LLParser::ParseMDNodeTail(MDNode *&N) {
3009 // !{ ... }
3010 if (Lex.getKind() == lltok::lbrace)
3011 return ParseMDTuple(N);
3012
3013 // !42
3014 return ParseMDNodeID(N);
3015}
3016
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003017namespace {
3018
3019/// Structure to represent an optional metadata field.
3020template <class FieldTy> struct MDFieldImpl {
3021 typedef MDFieldImpl ImplTy;
3022 FieldTy Val;
3023 bool Seen;
3024
3025 void assign(FieldTy Val) {
3026 Seen = true;
3027 this->Val = std::move(Val);
3028 }
3029
3030 explicit MDFieldImpl(FieldTy Default)
3031 : Val(std::move(Default)), Seen(false) {}
3032};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003033
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003034struct MDUnsignedField : public MDFieldImpl<uint64_t> {
3035 uint64_t Max;
3036
3037 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
3038 : ImplTy(Default), Max(Max) {}
3039};
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003040struct LineField : public MDUnsignedField {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +00003041 LineField() : MDUnsignedField(0, UINT32_MAX) {}
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003042};
3043struct ColumnField : public MDUnsignedField {
3044 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
3045};
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003046struct DwarfTagField : public MDUnsignedField {
Duncan P. N. Exon Smitha81f7e12015-02-06 22:29:35 +00003047 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003048 DwarfTagField(dwarf::Tag DefaultTag)
3049 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003050};
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003051struct DwarfAttEncodingField : public MDUnsignedField {
3052 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
3053};
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003054struct DwarfVirtualityField : public MDUnsignedField {
3055 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
3056};
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003057struct DwarfLangField : public MDUnsignedField {
3058 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
3059};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003060
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003061struct DIFlagField : public MDUnsignedField {
3062 DIFlagField() : MDUnsignedField(0, UINT32_MAX) {}
3063};
3064
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003065struct MDSignedField : public MDFieldImpl<int64_t> {
3066 int64_t Min;
3067 int64_t Max;
3068
3069 MDSignedField(int64_t Default = 0)
3070 : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {}
3071 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
3072 : ImplTy(Default), Min(Min), Max(Max) {}
3073};
3074
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003075struct MDBoolField : public MDFieldImpl<bool> {
3076 MDBoolField(bool Default = false) : ImplTy(Default) {}
3077};
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003078struct MDField : public MDFieldImpl<Metadata *> {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003079 bool AllowNull;
3080
3081 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003082};
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003083struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
3084 MDConstant() : ImplTy(nullptr) {}
3085};
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003086struct MDStringField : public MDFieldImpl<MDString *> {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003087 bool AllowEmpty;
3088 MDStringField(bool AllowEmpty = true)
3089 : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003090};
3091struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
3092 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
3093};
3094
3095} // end namespace
3096
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00003097namespace llvm {
3098
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003099template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003100bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003101 MDUnsignedField &Result) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003102 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3103 return TokError("expected unsigned integer");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003104
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003105 auto &U = Lex.getAPSIntVal();
3106 if (U.ugt(Result.Max))
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003107 return TokError("value for '" + Name + "' too large, limit is " +
3108 Twine(Result.Max));
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003109 Result.assign(U.getZExtValue());
3110 assert(Result.Val <= Result.Max && "Expected value in range");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003111 Lex.Lex();
3112 return false;
3113}
3114
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003115template <>
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003116bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) {
3117 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3118}
3119template <>
3120bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
3121 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3122}
3123
3124template <>
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003125bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
3126 if (Lex.getKind() == lltok::APSInt)
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003127 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003128
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003129 if (Lex.getKind() != lltok::DwarfTag)
3130 return TokError("expected DWARF tag");
3131
3132 unsigned Tag = dwarf::getTag(Lex.getStrVal());
3133 if (Tag == dwarf::DW_TAG_invalid)
3134 return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
Duncan P. N. Exon Smithfad631a2015-02-04 22:02:18 +00003135 assert(Tag <= Result.Max && "Expected valid DWARF tag");
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003136
3137 Result.assign(Tag);
3138 Lex.Lex();
3139 return false;
3140}
3141
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003142template <>
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003143bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3144 DwarfVirtualityField &Result) {
3145 if (Lex.getKind() == lltok::APSInt)
3146 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3147
3148 if (Lex.getKind() != lltok::DwarfVirtuality)
3149 return TokError("expected DWARF virtuality code");
3150
3151 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
3152 if (!Virtuality)
3153 return TokError("invalid DWARF virtuality code" + Twine(" '") +
3154 Lex.getStrVal() + "'");
3155 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
3156 Result.assign(Virtuality);
3157 Lex.Lex();
3158 return false;
3159}
3160
3161template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003162bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
3163 if (Lex.getKind() == lltok::APSInt)
3164 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3165
3166 if (Lex.getKind() != lltok::DwarfLang)
3167 return TokError("expected DWARF language");
3168
3169 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
3170 if (!Lang)
3171 return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
3172 "'");
3173 assert(Lang <= Result.Max && "Expected valid DWARF language");
3174 Result.assign(Lang);
3175 Lex.Lex();
3176 return false;
3177}
3178
3179template <>
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003180bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003181 DwarfAttEncodingField &Result) {
3182 if (Lex.getKind() == lltok::APSInt)
3183 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3184
3185 if (Lex.getKind() != lltok::DwarfAttEncoding)
3186 return TokError("expected DWARF type attribute encoding");
3187
3188 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
3189 if (!Encoding)
3190 return TokError("invalid DWARF type attribute encoding" + Twine(" '") +
3191 Lex.getStrVal() + "'");
3192 assert(Encoding <= Result.Max && "Expected valid DWARF language");
3193 Result.assign(Encoding);
3194 Lex.Lex();
3195 return false;
3196}
3197
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003198/// DIFlagField
3199/// ::= uint32
3200/// ::= DIFlagVector
3201/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
3202template <>
3203bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
3204 assert(Result.Max == UINT32_MAX && "Expected only 32-bits");
3205
3206 // Parser for a single flag.
3207 auto parseFlag = [&](unsigned &Val) {
3208 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned())
3209 return ParseUInt32(Val);
3210
3211 if (Lex.getKind() != lltok::DIFlag)
3212 return TokError("expected debug info flag");
3213
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003214 Val = DINode::getFlag(Lex.getStrVal());
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003215 if (!Val)
3216 return TokError(Twine("invalid debug info flag flag '") +
3217 Lex.getStrVal() + "'");
3218 Lex.Lex();
3219 return false;
3220 };
3221
3222 // Parse the flags and combine them together.
3223 unsigned Combined = 0;
3224 do {
3225 unsigned Val;
3226 if (parseFlag(Val))
3227 return true;
3228 Combined |= Val;
3229 } while (EatIfPresent(lltok::bar));
3230
3231 Result.assign(Combined);
3232 return false;
3233}
3234
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003235template <>
3236bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003237 MDSignedField &Result) {
3238 if (Lex.getKind() != lltok::APSInt)
3239 return TokError("expected signed integer");
3240
3241 auto &S = Lex.getAPSIntVal();
3242 if (S < Result.Min)
3243 return TokError("value for '" + Name + "' too small, limit is " +
3244 Twine(Result.Min));
3245 if (S > Result.Max)
3246 return TokError("value for '" + Name + "' too large, limit is " +
3247 Twine(Result.Max));
3248 Result.assign(S.getExtValue());
3249 assert(Result.Val >= Result.Min && "Expected value in range");
3250 assert(Result.Val <= Result.Max && "Expected value in range");
3251 Lex.Lex();
3252 return false;
3253}
3254
3255template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003256bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
3257 switch (Lex.getKind()) {
3258 default:
3259 return TokError("expected 'true' or 'false'");
3260 case lltok::kw_true:
3261 Result.assign(true);
3262 break;
3263 case lltok::kw_false:
3264 Result.assign(false);
3265 break;
3266 }
3267 Lex.Lex();
3268 return false;
3269}
3270
3271template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003272bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003273 if (Lex.getKind() == lltok::kw_null) {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003274 if (!Result.AllowNull)
3275 return TokError("'" + Name + "' cannot be null");
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003276 Lex.Lex();
3277 Result.assign(nullptr);
3278 return false;
3279 }
3280
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003281 Metadata *MD;
3282 if (ParseMetadata(MD, nullptr))
3283 return true;
3284
3285 Result.assign(MD);
3286 return false;
3287}
3288
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003289template <>
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003290bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDConstant &Result) {
3291 Metadata *MD;
3292 if (ParseValueAsMetadata(MD, "expected constant", nullptr))
3293 return true;
3294
3295 Result.assign(cast<ConstantAsMetadata>(MD));
3296 return false;
3297}
3298
3299template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003300bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003301 LocTy ValueLoc = Lex.getLoc();
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003302 std::string S;
3303 if (ParseStringConstant(S))
3304 return true;
3305
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003306 if (!Result.AllowEmpty && S.empty())
3307 return Error(ValueLoc, "'" + Name + "' cannot be empty");
3308
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003309 Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003310 return false;
3311}
3312
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003313template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003314bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
3315 SmallVector<Metadata *, 4> MDs;
3316 if (ParseMDNodeVector(MDs))
3317 return true;
3318
3319 Result.assign(std::move(MDs));
3320 return false;
3321}
3322
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00003323} // end namespace llvm
3324
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003325template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00003326bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003327 do {
3328 if (Lex.getKind() != lltok::LabelStr)
3329 return TokError("expected field label here");
3330
3331 if (parseField())
3332 return true;
3333 } while (EatIfPresent(lltok::comma));
3334
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00003335 return false;
3336}
3337
3338template <class ParserTy>
3339bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
3340 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
3341 Lex.Lex();
3342
3343 if (ParseToken(lltok::lparen, "expected '(' here"))
3344 return true;
3345 if (Lex.getKind() != lltok::rparen)
3346 if (ParseMDFieldsImplBody(parseField))
3347 return true;
3348
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +00003349 ClosingLoc = Lex.getLoc();
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003350 return ParseToken(lltok::rparen, "expected ')' here");
3351}
3352
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00003353template <class FieldTy>
3354bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
3355 if (Result.Seen)
3356 return TokError("field '" + Name + "' cannot be specified more than once");
3357
3358 LocTy Loc = Lex.getLoc();
3359 Lex.Lex();
3360 return ParseMDField(Loc, Name, Result);
3361}
3362
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003363bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
3364 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003365
3366#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003367 if (Lex.getStrVal() == #CLASS) \
3368 return Parse##CLASS(N, IsDistinct);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003369#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003370
3371 return TokError("expected metadata type");
3372}
3373
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003374#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
3375#define NOP_FIELD(NAME, TYPE, INIT)
3376#define REQUIRE_FIELD(NAME, TYPE, INIT) \
3377 if (!NAME.Seen) \
3378 return Error(ClosingLoc, "missing required field '" #NAME "'");
3379#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00003380 if (Lex.getStrVal() == #NAME) \
3381 return ParseMDField(#NAME, NAME);
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003382#define PARSE_MD_FIELDS() \
3383 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
3384 do { \
3385 LocTy ClosingLoc; \
3386 if (ParseMDFieldsImpl([&]() -> bool { \
3387 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
3388 return TokError(Twine("invalid field '") + Lex.getStrVal() + "'"); \
3389 }, ClosingLoc)) \
3390 return true; \
3391 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
3392 } while (false)
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003393#define GET_OR_DISTINCT(CLASS, ARGS) \
3394 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003395
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003396/// ParseDILocationFields:
3397/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6)
3398bool LLParser::ParseDILocation(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003399#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003400 OPTIONAL(line, LineField, ); \
3401 OPTIONAL(column, ColumnField, ); \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003402 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003403 OPTIONAL(inlinedAt, MDField, );
3404 PARSE_MD_FIELDS();
3405#undef VISIT_MD_FIELDS
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003406
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +00003407 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003408 DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003409 return false;
3410}
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003411
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003412/// ParseGenericDINode:
3413/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
3414bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003415#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003416 REQUIRED(tag, DwarfTagField, ); \
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003417 OPTIONAL(header, MDStringField, ); \
3418 OPTIONAL(operands, MDFieldList, );
3419 PARSE_MD_FIELDS();
3420#undef VISIT_MD_FIELDS
3421
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003422 Result = GET_OR_DISTINCT(GenericDINode,
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003423 (Context, tag.Val, header.Val, operands.Val));
3424 return false;
3425}
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003426
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003427/// ParseDISubrange:
3428/// ::= !DISubrange(count: 30, lowerBound: 2)
3429bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003430#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +00003431 REQUIRED(count, MDSignedField, (-1, -1, INT64_MAX)); \
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003432 OPTIONAL(lowerBound, MDSignedField, );
3433 PARSE_MD_FIELDS();
3434#undef VISIT_MD_FIELDS
3435
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003436 Result = GET_OR_DISTINCT(DISubrange, (Context, count.Val, lowerBound.Val));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003437 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003438}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003439
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003440/// ParseDIEnumerator:
3441/// ::= !DIEnumerator(value: 30, name: "SomeKind")
3442bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003443#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smithcd8fb602015-02-18 21:16:33 +00003444 REQUIRED(name, MDStringField, ); \
3445 REQUIRED(value, MDSignedField, );
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003446 PARSE_MD_FIELDS();
3447#undef VISIT_MD_FIELDS
3448
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003449 Result = GET_OR_DISTINCT(DIEnumerator, (Context, value.Val, name.Val));
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003450 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003451}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003452
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003453/// ParseDIBasicType:
3454/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32)
3455bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003456#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003457 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003458 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003459 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3460 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003461 OPTIONAL(encoding, DwarfAttEncodingField, );
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003462 PARSE_MD_FIELDS();
3463#undef VISIT_MD_FIELDS
3464
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003465 Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003466 align.Val, encoding.Val));
3467 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003468}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003469
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003470/// ParseDIDerivedType:
3471/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003472/// line: 7, scope: !1, baseType: !2, size: 32,
3473/// align: 32, offset: 0, flags: 0, extraData: !3)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003474bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003475#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3476 REQUIRED(tag, DwarfTagField, ); \
3477 OPTIONAL(name, MDStringField, ); \
3478 OPTIONAL(file, MDField, ); \
3479 OPTIONAL(line, LineField, ); \
3480 OPTIONAL(scope, MDField, ); \
3481 REQUIRED(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003482 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3483 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
3484 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003485 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003486 OPTIONAL(extraData, MDField, );
3487 PARSE_MD_FIELDS();
3488#undef VISIT_MD_FIELDS
3489
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003490 Result = GET_OR_DISTINCT(DIDerivedType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003491 (Context, tag.Val, name.Val, file.Val, line.Val,
3492 scope.Val, baseType.Val, size.Val, align.Val,
3493 offset.Val, flags.Val, extraData.Val));
3494 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003495}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003496
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003497bool LLParser::ParseDICompositeType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003498#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3499 REQUIRED(tag, DwarfTagField, ); \
3500 OPTIONAL(name, MDStringField, ); \
3501 OPTIONAL(file, MDField, ); \
3502 OPTIONAL(line, LineField, ); \
3503 OPTIONAL(scope, MDField, ); \
3504 OPTIONAL(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003505 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3506 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
3507 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003508 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003509 OPTIONAL(elements, MDField, ); \
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003510 OPTIONAL(runtimeLang, DwarfLangField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003511 OPTIONAL(vtableHolder, MDField, ); \
3512 OPTIONAL(templateParams, MDField, ); \
3513 OPTIONAL(identifier, MDStringField, );
3514 PARSE_MD_FIELDS();
3515#undef VISIT_MD_FIELDS
3516
3517 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003518 DICompositeType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003519 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
3520 size.Val, align.Val, offset.Val, flags.Val, elements.Val,
3521 runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val));
3522 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003523}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003524
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003525bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003526#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003527 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003528 REQUIRED(types, MDField, );
3529 PARSE_MD_FIELDS();
3530#undef VISIT_MD_FIELDS
3531
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003532 Result = GET_OR_DISTINCT(DISubroutineType, (Context, flags.Val, types.Val));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003533 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003534}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003535
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003536/// ParseDIFileType:
3537/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir")
3538bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003539#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3540 REQUIRED(filename, MDStringField, ); \
3541 REQUIRED(directory, MDStringField, );
3542 PARSE_MD_FIELDS();
3543#undef VISIT_MD_FIELDS
3544
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003545 Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val));
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003546 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003547}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003548
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003549/// ParseDICompileUnit:
3550/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003551/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
3552/// splitDebugFilename: "abc.debug", emissionKind: 1,
3553/// enums: !1, retainedTypes: !2, subprograms: !3,
Adrian Prantl1f599f92015-05-21 20:37:30 +00003554/// globals: !4, imports: !5, dwoId: 0x0abcd)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003555bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003556#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3557 REQUIRED(language, DwarfLangField, ); \
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +00003558 REQUIRED(file, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003559 OPTIONAL(producer, MDStringField, ); \
3560 OPTIONAL(isOptimized, MDBoolField, ); \
3561 OPTIONAL(flags, MDStringField, ); \
3562 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
3563 OPTIONAL(splitDebugFilename, MDStringField, ); \
3564 OPTIONAL(emissionKind, MDUnsignedField, (0, UINT32_MAX)); \
3565 OPTIONAL(enums, MDField, ); \
3566 OPTIONAL(retainedTypes, MDField, ); \
3567 OPTIONAL(subprograms, MDField, ); \
3568 OPTIONAL(globals, MDField, ); \
Adrian Prantl1f599f92015-05-21 20:37:30 +00003569 OPTIONAL(imports, MDField, ); \
3570 OPTIONAL(dwoId, MDUnsignedField, );
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003571 PARSE_MD_FIELDS();
3572#undef VISIT_MD_FIELDS
3573
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003574 Result = GET_OR_DISTINCT(DICompileUnit,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003575 (Context, language.Val, file.Val, producer.Val,
3576 isOptimized.Val, flags.Val, runtimeVersion.Val,
3577 splitDebugFilename.Val, emissionKind.Val, enums.Val,
3578 retainedTypes.Val, subprograms.Val, globals.Val,
Adrian Prantl1f599f92015-05-21 20:37:30 +00003579 imports.Val, dwoId.Val));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003580 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003581}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003582
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003583/// ParseDISubprogram:
3584/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003585/// file: !1, line: 7, type: !2, isLocal: false,
3586/// isDefinition: true, scopeLine: 8, containingType: !3,
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003587/// virtuality: DW_VIRTUALTIY_pure_virtual,
3588/// virtualIndex: 10, flags: 11,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003589/// isOptimized: false, function: void ()* @_Z3foov,
3590/// templateParams: !4, declaration: !5, variables: !6)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003591bool LLParser::ParseDISubprogram(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003592#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3593 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00003594 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003595 OPTIONAL(linkageName, MDStringField, ); \
3596 OPTIONAL(file, MDField, ); \
3597 OPTIONAL(line, LineField, ); \
3598 OPTIONAL(type, MDField, ); \
3599 OPTIONAL(isLocal, MDBoolField, ); \
3600 OPTIONAL(isDefinition, MDBoolField, (true)); \
3601 OPTIONAL(scopeLine, LineField, ); \
3602 OPTIONAL(containingType, MDField, ); \
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003603 OPTIONAL(virtuality, DwarfVirtualityField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003604 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003605 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003606 OPTIONAL(isOptimized, MDBoolField, ); \
3607 OPTIONAL(function, MDConstant, ); \
3608 OPTIONAL(templateParams, MDField, ); \
3609 OPTIONAL(declaration, MDField, ); \
3610 OPTIONAL(variables, MDField, );
3611 PARSE_MD_FIELDS();
3612#undef VISIT_MD_FIELDS
3613
3614 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003615 DISubprogram, (Context, scope.Val, name.Val, linkageName.Val, file.Val,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003616 line.Val, type.Val, isLocal.Val, isDefinition.Val,
3617 scopeLine.Val, containingType.Val, virtuality.Val,
3618 virtualIndex.Val, flags.Val, isOptimized.Val, function.Val,
3619 templateParams.Val, declaration.Val, variables.Val));
3620 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003621}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003622
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003623/// ParseDILexicalBlock:
3624/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
3625bool LLParser::ParseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003626#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00003627 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003628 OPTIONAL(file, MDField, ); \
3629 OPTIONAL(line, LineField, ); \
3630 OPTIONAL(column, ColumnField, );
3631 PARSE_MD_FIELDS();
3632#undef VISIT_MD_FIELDS
3633
3634 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003635 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003636 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003637}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003638
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003639/// ParseDILexicalBlockFile:
3640/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
3641bool LLParser::ParseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003642#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00003643 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003644 OPTIONAL(file, MDField, ); \
3645 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
3646 PARSE_MD_FIELDS();
3647#undef VISIT_MD_FIELDS
3648
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003649 Result = GET_OR_DISTINCT(DILexicalBlockFile,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003650 (Context, scope.Val, file.Val, discriminator.Val));
3651 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003652}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003653
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003654/// ParseDINamespace:
3655/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
3656bool LLParser::ParseDINamespace(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003657#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3658 REQUIRED(scope, MDField, ); \
3659 OPTIONAL(file, MDField, ); \
3660 OPTIONAL(name, MDStringField, ); \
3661 OPTIONAL(line, LineField, );
3662 PARSE_MD_FIELDS();
3663#undef VISIT_MD_FIELDS
3664
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003665 Result = GET_OR_DISTINCT(DINamespace,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003666 (Context, scope.Val, file.Val, name.Val, line.Val));
3667 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003668}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003669
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003670/// ParseDITemplateTypeParameter:
3671/// ::= !DITemplateTypeParameter(name: "Ty", type: !1)
3672bool LLParser::ParseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003673#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003674 OPTIONAL(name, MDStringField, ); \
3675 REQUIRED(type, MDField, );
3676 PARSE_MD_FIELDS();
3677#undef VISIT_MD_FIELDS
3678
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003679 Result =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003680 GET_OR_DISTINCT(DITemplateTypeParameter, (Context, name.Val, type.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003681 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003682}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003683
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003684/// ParseDITemplateValueParameter:
3685/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003686/// name: "V", type: !1, value: i32 7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003687bool LLParser::ParseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003688#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003689 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003690 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003691 OPTIONAL(type, MDField, ); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003692 REQUIRED(value, MDField, );
3693 PARSE_MD_FIELDS();
3694#undef VISIT_MD_FIELDS
3695
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003696 Result = GET_OR_DISTINCT(DITemplateValueParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003697 (Context, tag.Val, name.Val, type.Val, value.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003698 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003699}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003700
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003701/// ParseDIGlobalVariable:
3702/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003703/// file: !1, line: 7, type: !2, isLocal: false,
3704/// isDefinition: true, variable: i32* @foo,
3705/// declaration: !3)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003706bool LLParser::ParseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003707#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003708 REQUIRED(name, MDStringField, (/* AllowEmpty */ false)); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003709 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003710 OPTIONAL(linkageName, MDStringField, ); \
3711 OPTIONAL(file, MDField, ); \
3712 OPTIONAL(line, LineField, ); \
3713 OPTIONAL(type, MDField, ); \
3714 OPTIONAL(isLocal, MDBoolField, ); \
3715 OPTIONAL(isDefinition, MDBoolField, (true)); \
3716 OPTIONAL(variable, MDConstant, ); \
3717 OPTIONAL(declaration, MDField, );
3718 PARSE_MD_FIELDS();
3719#undef VISIT_MD_FIELDS
3720
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003721 Result = GET_OR_DISTINCT(DIGlobalVariable,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003722 (Context, scope.Val, name.Val, linkageName.Val,
3723 file.Val, line.Val, type.Val, isLocal.Val,
3724 isDefinition.Val, variable.Val, declaration.Val));
3725 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003726}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003727
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003728/// ParseDILocalVariable:
3729/// ::= !DILocalVariable(tag: DW_TAG_arg_variable, scope: !0, name: "foo",
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003730/// file: !1, line: 7, type: !2, arg: 2, flags: 7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003731bool LLParser::ParseDILocalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003732#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3733 REQUIRED(tag, DwarfTagField, ); \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003734 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003735 OPTIONAL(name, MDStringField, ); \
3736 OPTIONAL(file, MDField, ); \
3737 OPTIONAL(line, LineField, ); \
3738 OPTIONAL(type, MDField, ); \
Duncan P. N. Exon Smith69488692015-06-02 17:17:44 +00003739 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003740 OPTIONAL(flags, DIFlagField, );
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003741 PARSE_MD_FIELDS();
3742#undef VISIT_MD_FIELDS
3743
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003744 Result = GET_OR_DISTINCT(DILocalVariable,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003745 (Context, tag.Val, scope.Val, name.Val, file.Val,
3746 line.Val, type.Val, arg.Val, flags.Val));
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003747 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003748}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003749
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003750/// ParseDIExpression:
3751/// ::= !DIExpression(0, 7, -1)
3752bool LLParser::ParseDIExpression(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003753 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
3754 Lex.Lex();
3755
3756 if (ParseToken(lltok::lparen, "expected '(' here"))
3757 return true;
3758
3759 SmallVector<uint64_t, 8> Elements;
3760 if (Lex.getKind() != lltok::rparen)
3761 do {
3762 if (Lex.getKind() == lltok::DwarfOp) {
3763 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
3764 Lex.Lex();
3765 Elements.push_back(Op);
3766 continue;
3767 }
3768 return TokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
3769 }
3770
3771 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3772 return TokError("expected unsigned integer");
3773
3774 auto &U = Lex.getAPSIntVal();
3775 if (U.ugt(UINT64_MAX))
3776 return TokError("element too large, limit is " + Twine(UINT64_MAX));
3777 Elements.push_back(U.getZExtValue());
3778 Lex.Lex();
3779 } while (EatIfPresent(lltok::comma));
3780
3781 if (ParseToken(lltok::rparen, "expected ')' here"))
3782 return true;
3783
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003784 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003785 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003786}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003787
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003788/// ParseDIObjCProperty:
3789/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003790/// getter: "getFoo", attributes: 7, type: !2)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003791bool LLParser::ParseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003792#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00003793 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003794 OPTIONAL(file, MDField, ); \
3795 OPTIONAL(line, LineField, ); \
3796 OPTIONAL(setter, MDStringField, ); \
3797 OPTIONAL(getter, MDStringField, ); \
3798 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
3799 OPTIONAL(type, MDField, );
3800 PARSE_MD_FIELDS();
3801#undef VISIT_MD_FIELDS
3802
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003803 Result = GET_OR_DISTINCT(DIObjCProperty,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003804 (Context, name.Val, file.Val, line.Val, setter.Val,
3805 getter.Val, attributes.Val, type.Val));
3806 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003807}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003808
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003809/// ParseDIImportedEntity:
3810/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003811/// line: 7, name: "foo")
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003812bool LLParser::ParseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003813#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3814 REQUIRED(tag, DwarfTagField, ); \
3815 REQUIRED(scope, MDField, ); \
3816 OPTIONAL(entity, MDField, ); \
3817 OPTIONAL(line, LineField, ); \
3818 OPTIONAL(name, MDStringField, );
3819 PARSE_MD_FIELDS();
3820#undef VISIT_MD_FIELDS
3821
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003822 Result = GET_OR_DISTINCT(DIImportedEntity, (Context, tag.Val, scope.Val,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003823 entity.Val, line.Val, name.Val));
3824 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003825}
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003826
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003827#undef PARSE_MD_FIELD
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003828#undef NOP_FIELD
3829#undef REQUIRE_FIELD
3830#undef DECLARE_FIELD
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003831
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003832/// ParseMetadataAsValue
3833/// ::= metadata i32 %local
3834/// ::= metadata i32 @global
3835/// ::= metadata i32 7
3836/// ::= metadata !0
3837/// ::= metadata !{...}
3838/// ::= metadata !"string"
3839bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
3840 // Note: the type 'metadata' has already been parsed.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003841 Metadata *MD;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003842 if (ParseMetadata(MD, &PFS))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003843 return true;
3844
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003845 V = MetadataAsValue::get(Context, MD);
3846 return false;
3847}
3848
3849/// ParseValueAsMetadata
3850/// ::= i32 %local
3851/// ::= i32 @global
3852/// ::= i32 7
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003853bool LLParser::ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
3854 PerFunctionState *PFS) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003855 Type *Ty;
3856 LocTy Loc;
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003857 if (ParseType(Ty, TypeMsg, Loc))
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003858 return true;
3859 if (Ty->isMetadataTy())
3860 return Error(Loc, "invalid metadata-value-metadata roundtrip");
3861
3862 Value *V;
3863 if (ParseValue(Ty, V, PFS))
3864 return true;
3865
3866 MD = ValueAsMetadata::get(V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003867 return false;
3868}
3869
3870/// ParseMetadata
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003871/// ::= i32 %local
3872/// ::= i32 @global
3873/// ::= i32 7
Dan Gohman8939ba332010-07-14 18:26:50 +00003874/// ::= !42
3875/// ::= !{...}
3876/// ::= !"string"
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003877/// ::= !DILocation(...)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003878bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003879 if (Lex.getKind() == lltok::MetadataVar) {
3880 MDNode *N;
3881 if (ParseSpecializedMDNode(N))
3882 return true;
3883 MD = N;
3884 return false;
3885 }
3886
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003887 // ValueAsMetadata:
3888 // <type> <value>
3889 if (Lex.getKind() != lltok::exclaim)
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003890 return ParseValueAsMetadata(MD, "expected metadata operand", PFS);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003891
3892 // '!'.
3893 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
3894 Lex.Lex();
Dan Gohman8939ba332010-07-14 18:26:50 +00003895
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003896 // MDString:
3897 // ::= '!' STRINGCONSTANT
3898 if (Lex.getKind() == lltok::StringConstant) {
3899 MDString *S;
3900 if (ParseMDString(S))
3901 return true;
3902 MD = S;
3903 return false;
3904 }
3905
Dan Gohman8939ba332010-07-14 18:26:50 +00003906 // MDNode:
3907 // !{ ... }
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003908 // !7
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003909 MDNode *N;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003910 if (ParseMDNodeTail(N))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003911 return true;
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003912 MD = N;
Dan Gohman8939ba332010-07-14 18:26:50 +00003913 return false;
3914}
3915
Victor Hernandez9d75c962010-01-11 22:31:58 +00003916
3917//===----------------------------------------------------------------------===//
3918// Function Parsing.
3919//===----------------------------------------------------------------------===//
3920
Chris Lattner229907c2011-07-18 04:54:35 +00003921bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00003922 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003923 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003924 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003925
Chris Lattnerac161bf2009-01-02 07:01:27 +00003926 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003927 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00003928 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
3929 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003930 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003931 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00003932 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
3933 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003934 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003935 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00003936 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003937 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00003938 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003939 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
3940 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00003941 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00003942 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00003943 return false;
3944 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003945 case ValID::t_GlobalName:
3946 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003947 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003948 case ValID::t_GlobalID:
3949 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003950 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003951 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00003952 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003953 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00003954 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00003955 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003956 return false;
3957 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00003958 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003959 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
3960 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003961
Dan Gohman518cda42011-12-17 00:04:22 +00003962 // The lexer has no type info, so builds all half, float, and double FP
3963 // constants as double. Fix this here. Long double does not need this.
3964 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003965 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00003966 if (Ty->isHalfTy())
3967 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
3968 &Ignored);
3969 else if (Ty->isFloatTy())
3970 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
3971 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003972 }
Owen Anderson69c464d2009-07-27 20:59:43 +00003973 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003974
Chris Lattner8f57d29e2009-01-05 18:24:23 +00003975 if (V->getType() != Ty)
3976 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003977 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003978
Chris Lattnerac161bf2009-01-02 07:01:27 +00003979 return false;
3980 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00003981 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003982 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003983 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003984 return false;
3985 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00003986 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003987 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00003988 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003989 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003990 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00003991 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00003992 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00003993 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003994 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00003995 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003996 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00003997 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00003998 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003999 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00004000 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004001 return false;
4002 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00004003 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004004 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00004005
Chris Lattnerac161bf2009-01-02 07:01:27 +00004006 V = ID.ConstantVal;
4007 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004008 case ValID::t_ConstantStruct:
4009 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00004010 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004011 if (ST->getNumElements() != ID.UIntVal)
4012 return Error(ID.Loc,
4013 "initializer with struct type has wrong # elements");
4014 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
4015 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004016
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004017 // Verify that the elements are compatible with the structtype.
4018 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
4019 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
4020 return Error(ID.Loc, "element " + Twine(i) +
4021 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004022
Reid Kleckner2ae03e12015-03-04 18:31:10 +00004023 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
4024 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004025 } else
4026 return Error(ID.Loc, "constant expression type mismatch");
4027 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004028 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00004029 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004030}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004031
Chris Lattner229907c2011-07-18 04:54:35 +00004032bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004033 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004034 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004035 return ParseValID(ID, PFS) ||
4036 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004037}
4038
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004039bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004040 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004041 return ParseType(Ty) ||
4042 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004043}
4044
Chris Lattner3ed871f2009-10-27 19:13:16 +00004045bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
4046 PerFunctionState &PFS) {
4047 Value *V;
4048 Loc = Lex.getLoc();
4049 if (ParseTypeAndValue(V, PFS)) return true;
4050 if (!isa<BasicBlock>(V))
4051 return Error(Loc, "expected a basic block");
4052 BB = cast<BasicBlock>(V);
4053 return false;
4054}
4055
4056
Chris Lattnerac161bf2009-01-02 07:01:27 +00004057/// FunctionHeader
4058/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00004059/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
David Majnemer7fddecc2015-06-17 20:52:32 +00004060/// OptionalAlign OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
Chris Lattnerac161bf2009-01-02 07:01:27 +00004061bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
4062 // Parse the linkage.
4063 LocTy LinkageLoc = Lex.getLoc();
4064 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004065
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00004066 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00004067 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00004068 AttrBuilder RetAttrs;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004069 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004070 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004071 LocTy RetTypeLoc = Lex.getLoc();
4072 if (ParseOptionalLinkage(Linkage) ||
4073 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00004074 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004075 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004076 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004077 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004078 return true;
4079
4080 // Verify that the linkage is ok.
4081 switch ((GlobalValue::LinkageTypes)Linkage) {
4082 case GlobalValue::ExternalLinkage:
4083 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00004084 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004085 if (isDefine)
4086 return Error(LinkageLoc, "invalid linkage for function definition");
4087 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00004088 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004089 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00004090 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00004091 case GlobalValue::LinkOnceAnyLinkage:
4092 case GlobalValue::LinkOnceODRLinkage:
4093 case GlobalValue::WeakAnyLinkage:
4094 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004095 if (!isDefine)
4096 return Error(LinkageLoc, "invalid linkage for function declaration");
4097 break;
4098 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00004099 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004100 return Error(LinkageLoc, "invalid function linkage type");
4101 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004102
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00004103 if (!isValidVisibilityForLinkage(Visibility, Linkage))
4104 return Error(LinkageLoc,
4105 "symbol with local linkage must have default visibility");
4106
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004107 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004108 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004109
Chris Lattnerac161bf2009-01-02 07:01:27 +00004110 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00004111
4112 std::string FunctionName;
4113 if (Lex.getKind() == lltok::GlobalVar) {
4114 FunctionName = Lex.getStrVal();
4115 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
4116 unsigned NameID = Lex.getUIntVal();
4117
4118 if (NameID != NumberedVals.size())
4119 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00004120 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00004121 } else {
4122 return TokError("expected function name");
4123 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004124
Chris Lattner3822f632009-01-02 08:05:26 +00004125 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004126
Chris Lattner3822f632009-01-02 08:05:26 +00004127 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004128 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004129
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004130 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004131 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00004132 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004133 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004134 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004135 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004136 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00004137 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00004138 bool UnnamedAddr;
4139 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00004140 Constant *Prefix = nullptr;
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004141 Constant *Prologue = nullptr;
David Majnemer7fddecc2015-06-17 20:52:32 +00004142 Constant *PersonalityFn = nullptr;
David Majnemerdad0a642014-06-27 18:19:56 +00004143 Comdat *C;
Chris Lattner3822f632009-01-02 08:05:26 +00004144
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004145 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00004146 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
4147 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004148 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004149 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00004150 (EatIfPresent(lltok::kw_section) &&
4151 ParseStringConstant(Section)) ||
Rafael Espindola83a362c2015-01-06 22:55:16 +00004152 parseOptionalComdat(FunctionName, C) ||
Chris Lattner3822f632009-01-02 08:05:26 +00004153 ParseOptionalAlignment(Alignment) ||
4154 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00004155 ParseStringConstant(GC)) ||
4156 (EatIfPresent(lltok::kw_prefix) &&
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004157 ParseGlobalTypeAndValue(Prefix)) ||
4158 (EatIfPresent(lltok::kw_prologue) &&
David Majnemer7fddecc2015-06-17 20:52:32 +00004159 ParseGlobalTypeAndValue(Prologue)) ||
4160 (EatIfPresent(lltok::kw_personality) &&
4161 ParseGlobalTypeAndValue(PersonalityFn)))
Chris Lattner3822f632009-01-02 08:05:26 +00004162 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004163
Michael Gottesman41748d72013-06-27 00:25:01 +00004164 if (FuncAttrs.contains(Attribute::Builtin))
4165 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00004166
Chris Lattnerac161bf2009-01-02 07:01:27 +00004167 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00004168 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00004169 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004170 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004171 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004172
Chris Lattnerac161bf2009-01-02 07:01:27 +00004173 // Okay, if we got here, the function is syntactically valid. Convert types
4174 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00004175 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00004176 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004177
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004178 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004179 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4180 AttributeSet::ReturnIndex,
4181 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004182
Chris Lattnerac161bf2009-01-02 07:01:27 +00004183 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004184 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004185 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4186 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004187 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4188 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004189 }
4190
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004191 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004192 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4193 AttributeSet::FunctionIndex,
4194 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004195
Bill Wendlinge94d8432012-12-07 23:16:57 +00004196 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004197
Bill Wendling749a43d2012-12-30 13:50:49 +00004198 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004199 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
4200
Chris Lattner229907c2011-07-18 04:54:35 +00004201 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00004202 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00004203 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004204
Craig Topper2617dcc2014-04-15 06:32:26 +00004205 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004206 if (!FunctionName.empty()) {
4207 // If this was a definition of a forward reference, remove the definition
4208 // from the forward reference table and fill in the forward ref.
4209 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
4210 ForwardRefVals.find(FunctionName);
4211 if (FRVI != ForwardRefVals.end()) {
4212 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00004213 if (!Fn)
4214 return Error(FRVI->second.second, "invalid forward reference to "
4215 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00004216 if (Fn->getType() != PFT)
4217 return Error(FRVI->second.second, "invalid forward reference to "
4218 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004219
Chris Lattnerac161bf2009-01-02 07:01:27 +00004220 ForwardRefVals.erase(FRVI);
4221 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00004222 // Reject redefinitions.
4223 return Error(NameLoc, "invalid redefinition of function '" +
4224 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00004225 } else if (M->getNamedValue(FunctionName)) {
4226 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004227 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004228
Dan Gohman399d6ae2009-08-29 23:37:49 +00004229 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004230 // If this is a definition of a forward referenced function, make sure the
4231 // types agree.
4232 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
4233 = ForwardRefValIDs.find(NumberedVals.size());
4234 if (I != ForwardRefValIDs.end()) {
4235 Fn = cast<Function>(I->second.first);
4236 if (Fn->getType() != PFT)
4237 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00004238 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004239 ForwardRefValIDs.erase(I);
4240 }
4241 }
4242
Craig Topper2617dcc2014-04-15 06:32:26 +00004243 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004244 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
4245 else // Move the forward-reference to the correct spot in the module.
4246 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
4247
4248 if (FunctionName.empty())
4249 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004250
Chris Lattnerac161bf2009-01-02 07:01:27 +00004251 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
4252 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00004253 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004254 Fn->setCallingConv(CC);
4255 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00004256 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004257 Fn->setAlignment(Alignment);
4258 Fn->setSection(Section);
David Majnemerdad0a642014-06-27 18:19:56 +00004259 Fn->setComdat(C);
David Majnemer7fddecc2015-06-17 20:52:32 +00004260 Fn->setPersonalityFn(PersonalityFn);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004261 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00004262 Fn->setPrefixData(Prefix);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004263 Fn->setPrologueData(Prologue);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004264 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004265
Chris Lattnerac161bf2009-01-02 07:01:27 +00004266 // Add all of the arguments we parsed to the function.
4267 Function::arg_iterator ArgIt = Fn->arg_begin();
4268 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
4269 // If the argument has a name, insert it into the argument symbol table.
4270 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004271
Chris Lattnerac161bf2009-01-02 07:01:27 +00004272 // Set the name, if it conflicted, it will be auto-renamed.
4273 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004274
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00004275 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004276 return Error(ArgList[i].Loc, "redefinition of argument '%" +
4277 ArgList[i].Name + "'");
4278 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004279
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004280 if (isDefine)
4281 return false;
4282
Robin Morisset039781e2014-08-29 21:53:01 +00004283 // Check the declaration has no block address forward references.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004284 ValID ID;
4285 if (FunctionName.empty()) {
4286 ID.Kind = ValID::t_GlobalID;
4287 ID.UIntVal = NumberedVals.size() - 1;
4288 } else {
4289 ID.Kind = ValID::t_GlobalName;
4290 ID.StrVal = FunctionName;
4291 }
4292 auto Blocks = ForwardRefBlockAddresses.find(ID);
4293 if (Blocks != ForwardRefBlockAddresses.end())
4294 return Error(Blocks->first.Loc,
4295 "cannot take blockaddress inside a declaration");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004296 return false;
4297}
4298
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004299bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
4300 ValID ID;
4301 if (FunctionNumber == -1) {
4302 ID.Kind = ValID::t_GlobalName;
4303 ID.StrVal = F.getName();
4304 } else {
4305 ID.Kind = ValID::t_GlobalID;
4306 ID.UIntVal = FunctionNumber;
4307 }
4308
4309 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
4310 if (Blocks == P.ForwardRefBlockAddresses.end())
4311 return false;
4312
4313 for (const auto &I : Blocks->second) {
4314 const ValID &BBID = I.first;
4315 GlobalValue *GV = I.second;
4316
4317 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
4318 "Expected local id or name");
4319 BasicBlock *BB;
4320 if (BBID.Kind == ValID::t_LocalName)
4321 BB = GetBB(BBID.StrVal, BBID.Loc);
4322 else
4323 BB = GetBB(BBID.UIntVal, BBID.Loc);
4324 if (!BB)
4325 return P.Error(BBID.Loc, "referenced value is not a basic block");
4326
4327 GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
4328 GV->eraseFromParent();
4329 }
4330
4331 P.ForwardRefBlockAddresses.erase(Blocks);
4332 return false;
4333}
Chris Lattnerac161bf2009-01-02 07:01:27 +00004334
4335/// ParseFunctionBody
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004336/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00004337bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00004338 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004339 return TokError("expected '{' in function body");
4340 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004341
Chris Lattner3432c622009-10-28 03:39:23 +00004342 int FunctionNumber = -1;
4343 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004344
Chris Lattner3432c622009-10-28 03:39:23 +00004345 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004346
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004347 // Resolve block addresses and allow basic blocks to be forward-declared
4348 // within this function.
4349 if (PFS.resolveForwardRefBlockAddresses())
4350 return true;
4351 SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
4352
Chris Lattnerbbddd962010-01-09 19:20:07 +00004353 // We need at least one basic block.
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004354 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
Chris Lattnerbbddd962010-01-09 19:20:07 +00004355 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004356
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004357 while (Lex.getKind() != lltok::rbrace &&
4358 Lex.getKind() != lltok::kw_uselistorder)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004359 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004360
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004361 while (Lex.getKind() != lltok::rbrace)
4362 if (ParseUseListOrder(&PFS))
4363 return true;
4364
Chris Lattnerac161bf2009-01-02 07:01:27 +00004365 // Eat the }.
4366 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004367
Chris Lattnerac161bf2009-01-02 07:01:27 +00004368 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00004369 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00004370}
4371
4372/// ParseBasicBlock
4373/// ::= LabelStr? Instruction*
4374bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
4375 // If this basic block starts out with a name, remember it.
4376 std::string Name;
4377 LocTy NameLoc = Lex.getLoc();
4378 if (Lex.getKind() == lltok::LabelStr) {
4379 Name = Lex.getStrVal();
4380 Lex.Lex();
4381 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004382
Chris Lattnerac161bf2009-01-02 07:01:27 +00004383 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Owen Anderson576a9a22015-03-02 05:25:09 +00004384 if (!BB)
4385 return Error(NameLoc,
4386 "unable to create block named '" + Name + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004387
Chris Lattnerac161bf2009-01-02 07:01:27 +00004388 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004389
Chris Lattnerac161bf2009-01-02 07:01:27 +00004390 // Parse the instructions in this block until we get a terminator.
4391 Instruction *Inst;
4392 do {
4393 // This instruction may have three possibilities for a name: a) none
4394 // specified, b) name specified "%foo =", c) number specified: "%4 =".
4395 LocTy NameLoc = Lex.getLoc();
4396 int NameID = -1;
4397 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004398
Chris Lattnerac161bf2009-01-02 07:01:27 +00004399 if (Lex.getKind() == lltok::LocalVarID) {
4400 NameID = Lex.getUIntVal();
4401 Lex.Lex();
4402 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
4403 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00004404 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004405 NameStr = Lex.getStrVal();
4406 Lex.Lex();
4407 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
4408 return true;
4409 }
Devang Patelea8a4b92009-09-17 23:04:48 +00004410
Chris Lattner77b89dc2009-12-30 05:23:43 +00004411 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00004412 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00004413 case InstError: return true;
4414 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00004415 BB->getInstList().push_back(Inst);
4416
Chris Lattner77b89dc2009-12-30 05:23:43 +00004417 // With a normal result, we check to see if the instruction is followed by
4418 // a comma and metadata.
4419 if (EatIfPresent(lltok::comma))
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00004420 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00004421 return true;
4422 break;
4423 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00004424 BB->getInstList().push_back(Inst);
4425
Chris Lattner77b89dc2009-12-30 05:23:43 +00004426 // If the instruction parser ate an extra comma at the end of it, it
4427 // *must* be followed by metadata.
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00004428 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00004429 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004430 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00004431 }
Devang Patelea8a4b92009-09-17 23:04:48 +00004432
Chris Lattnerac161bf2009-01-02 07:01:27 +00004433 // Set the name on the instruction.
4434 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
4435 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004436
Chris Lattnerac161bf2009-01-02 07:01:27 +00004437 return false;
4438}
4439
4440//===----------------------------------------------------------------------===//
4441// Instruction Parsing.
4442//===----------------------------------------------------------------------===//
4443
4444/// ParseInstruction - Parse one of the many different instructions.
4445///
Chris Lattner77b89dc2009-12-30 05:23:43 +00004446int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
4447 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004448 lltok::Kind Token = Lex.getKind();
4449 if (Token == lltok::Eof)
4450 return TokError("found end of file when expecting more instructions");
4451 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00004452 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00004453 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004454
Chris Lattnerac161bf2009-01-02 07:01:27 +00004455 switch (Token) {
4456 default: return Error(Loc, "expected instruction opcode");
4457 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00004458 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004459 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
4460 case lltok::kw_br: return ParseBr(Inst, PFS);
4461 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004462 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004463 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00004464 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004465 // Binary Operators.
4466 case lltok::kw_add:
4467 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00004468 case lltok::kw_mul:
4469 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00004470 bool NUW = EatIfPresent(lltok::kw_nuw);
4471 bool NSW = EatIfPresent(lltok::kw_nsw);
4472 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004473
Chris Lattnera676c0f2011-02-07 16:40:21 +00004474 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004475
Chris Lattnera676c0f2011-02-07 16:40:21 +00004476 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
4477 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
4478 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00004479 }
Dan Gohmana5b96452009-06-04 22:49:04 +00004480 case lltok::kw_fadd:
4481 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00004482 case lltok::kw_fmul:
4483 case lltok::kw_fdiv:
4484 case lltok::kw_frem: {
4485 FastMathFlags FMF = EatFastMathFlagsIfPresent();
4486 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
4487 if (Res != 0)
4488 return Res;
4489 if (FMF.any())
4490 Inst->setFastMathFlags(FMF);
4491 return 0;
4492 }
Dan Gohmana5b96452009-06-04 22:49:04 +00004493
Chris Lattner35315d02011-02-06 21:44:57 +00004494 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00004495 case lltok::kw_udiv:
4496 case lltok::kw_lshr:
4497 case lltok::kw_ashr: {
4498 bool Exact = EatIfPresent(lltok::kw_exact);
4499
4500 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
4501 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
4502 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00004503 }
4504
Chris Lattnerac161bf2009-01-02 07:01:27 +00004505 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00004506 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004507 case lltok::kw_and:
4508 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00004509 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004510 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004511 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004512 // Casts.
4513 case lltok::kw_trunc:
4514 case lltok::kw_zext:
4515 case lltok::kw_sext:
4516 case lltok::kw_fptrunc:
4517 case lltok::kw_fpext:
4518 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004519 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004520 case lltok::kw_uitofp:
4521 case lltok::kw_sitofp:
4522 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004523 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004524 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00004525 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004526 // Other.
4527 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00004528 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004529 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
4530 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
4531 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
4532 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00004533 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00004534 // Call.
4535 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
4536 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
4537 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004538 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00004539 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00004540 case lltok::kw_load: return ParseLoad(Inst, PFS);
4541 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00004542 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
4543 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00004544 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004545 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
4546 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
4547 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
4548 }
4549}
4550
4551/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
4552bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004553 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004554 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00004555 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004556 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
4557 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
4558 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
4559 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
4560 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
4561 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
4562 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
4563 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
4564 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
4565 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
4566 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
4567 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
4568 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
4569 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
4570 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
4571 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
4572 }
4573 } else {
4574 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00004575 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004576 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
4577 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
4578 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
4579 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
4580 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
4581 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
4582 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
4583 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
4584 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
4585 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
4586 }
4587 }
4588 Lex.Lex();
4589 return false;
4590}
4591
4592//===----------------------------------------------------------------------===//
4593// Terminator Instructions.
4594//===----------------------------------------------------------------------===//
4595
4596/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00004597/// ::= 'ret' void (',' !dbg, !1)*
4598/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00004599bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004600 PerFunctionState &PFS) {
4601 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00004602 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00004603 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004604
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004605 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004606
Chris Lattnerfdd87902009-10-05 05:54:46 +00004607 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004608 if (!ResType->isVoidTy())
4609 return Error(TypeLoc, "value doesn't match function result type '" +
4610 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004611
Owen Anderson55f1c092009-08-13 21:58:54 +00004612 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004613 return false;
4614 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004615
Chris Lattnerac161bf2009-01-02 07:01:27 +00004616 Value *RV;
4617 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004618
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004619 if (ResType != RV->getType())
4620 return Error(TypeLoc, "value doesn't match function result type '" +
4621 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004622
Owen Anderson55f1c092009-08-13 21:58:54 +00004623 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00004624 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004625}
4626
4627
4628/// ParseBr
4629/// ::= 'br' TypeAndValue
4630/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4631bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
4632 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00004633 Value *Op0;
4634 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004635 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004636
Chris Lattnerac161bf2009-01-02 07:01:27 +00004637 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
4638 Inst = BranchInst::Create(BB);
4639 return false;
4640 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004641
Owen Anderson55f1c092009-08-13 21:58:54 +00004642 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004643 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004644
Chris Lattnerac161bf2009-01-02 07:01:27 +00004645 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004646 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004647 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004648 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004649 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004650
Chris Lattner3ed871f2009-10-27 19:13:16 +00004651 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004652 return false;
4653}
4654
4655/// ParseSwitch
4656/// Instruction
4657/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
4658/// JumpTable
4659/// ::= (TypeAndValue ',' TypeAndValue)*
4660bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
4661 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00004662 Value *Cond;
4663 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004664 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
4665 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004666 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004667 ParseToken(lltok::lsquare, "expected '[' with switch table"))
4668 return true;
4669
Duncan Sands19d0b472010-02-16 11:11:14 +00004670 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004671 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004672
Chris Lattnerac161bf2009-01-02 07:01:27 +00004673 // Parse the jump table pairs.
4674 SmallPtrSet<Value*, 32> SeenCases;
4675 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
4676 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00004677 Value *Constant;
4678 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004679
Chris Lattnerac161bf2009-01-02 07:01:27 +00004680 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
4681 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004682 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004683 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004684
David Blaikie70573dc2014-11-19 07:49:26 +00004685 if (!SeenCases.insert(Constant).second)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004686 return Error(CondLoc, "duplicate case value in switch");
4687 if (!isa<ConstantInt>(Constant))
4688 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004689
Chris Lattner3ed871f2009-10-27 19:13:16 +00004690 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004691 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004692
Chris Lattnerac161bf2009-01-02 07:01:27 +00004693 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004694
Chris Lattner3ed871f2009-10-27 19:13:16 +00004695 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00004696 for (unsigned i = 0, e = Table.size(); i != e; ++i)
4697 SI->addCase(Table[i].first, Table[i].second);
4698 Inst = SI;
4699 return false;
4700}
4701
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004702/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00004703/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004704/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
4705bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00004706 LocTy AddrLoc;
4707 Value *Address;
4708 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004709 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
4710 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00004711 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004712
Duncan Sands19d0b472010-02-16 11:11:14 +00004713 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004714 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004715
Chris Lattner3ed871f2009-10-27 19:13:16 +00004716 // Parse the destination list.
4717 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004718
Chris Lattner3ed871f2009-10-27 19:13:16 +00004719 if (Lex.getKind() != lltok::rsquare) {
4720 BasicBlock *DestBB;
4721 if (ParseTypeAndBasicBlock(DestBB, PFS))
4722 return true;
4723 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004724
Chris Lattner3ed871f2009-10-27 19:13:16 +00004725 while (EatIfPresent(lltok::comma)) {
4726 if (ParseTypeAndBasicBlock(DestBB, PFS))
4727 return true;
4728 DestList.push_back(DestBB);
4729 }
4730 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004731
Chris Lattner3ed871f2009-10-27 19:13:16 +00004732 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
4733 return true;
4734
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004735 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00004736 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
4737 IBI->addDestination(DestList[i]);
4738 Inst = IBI;
4739 return false;
4740}
4741
4742
Chris Lattnerac161bf2009-01-02 07:01:27 +00004743/// ParseInvoke
4744/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
4745/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
4746bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
4747 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00004748 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004749 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00004750 LocTy NoBuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004751 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004752 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004753 LocTy RetTypeLoc;
4754 ValID CalleeID;
4755 SmallVector<ParamInfo, 16> ArgList;
4756
Chris Lattner3ed871f2009-10-27 19:13:16 +00004757 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004758 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004759 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004760 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004761 ParseValID(CalleeID) ||
4762 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004763 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
4764 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004765 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004766 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004767 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004768 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004769 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004770
Chris Lattnerac161bf2009-01-02 07:01:27 +00004771 // If RetType is a non-function pointer type, then this is the short syntax
4772 // for the call, which means that RetType is just the return type. Infer the
4773 // rest of the function argument types from the arguments that are present.
David Blaikie445e3fb2015-04-24 19:32:54 +00004774 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
4775 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004776 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004777 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004778 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4779 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004780
Chris Lattnerac161bf2009-01-02 07:01:27 +00004781 if (!FunctionType::isValidReturnType(RetType))
4782 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004783
Owen Anderson4056ca92009-07-29 22:17:13 +00004784 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004785 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004786
Chris Lattnerac161bf2009-01-02 07:01:27 +00004787 // Look up the callee.
4788 Value *Callee;
David Blaikie445e3fb2015-04-24 19:32:54 +00004789 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
4790 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004791
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004792 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004793 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004794 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004795 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4796 AttributeSet::ReturnIndex,
4797 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004798
Chris Lattnerac161bf2009-01-02 07:01:27 +00004799 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004800
Chris Lattnerac161bf2009-01-02 07:01:27 +00004801 // Loop through FunctionType's arguments and ensure they are specified
4802 // correctly. Also, gather any parameter attributes.
4803 FunctionType::param_iterator I = Ty->param_begin();
4804 FunctionType::param_iterator E = Ty->param_end();
4805 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004806 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004807 if (I != E) {
4808 ExpectedTy = *I++;
4809 } else if (!Ty->isVarArg()) {
4810 return Error(ArgList[i].Loc, "too many arguments specified");
4811 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004812
Chris Lattnerac161bf2009-01-02 07:01:27 +00004813 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4814 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004815 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004816 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004817 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4818 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004819 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4820 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004821 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004822
Chris Lattnerac161bf2009-01-02 07:01:27 +00004823 if (I != E)
4824 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004825
David Majnemer8d22abd2015-02-23 00:01:32 +00004826 if (FnAttrs.hasAttributes()) {
4827 if (FnAttrs.hasAlignmentAttr())
4828 return Error(CallLoc, "invoke instructions may not have an alignment");
4829
Bill Wendlingf5075a42013-01-27 02:24:02 +00004830 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4831 AttributeSet::FunctionIndex,
4832 FnAttrs));
David Majnemer8d22abd2015-02-23 00:01:32 +00004833 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004834
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004835 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004836 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004837
David Blaikie3e807092015-05-13 18:35:26 +00004838 InvokeInst *II = InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004839 II->setCallingConv(CC);
4840 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004841 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004842 Inst = II;
4843 return false;
4844}
4845
Bill Wendlingf891bf82011-07-31 06:30:59 +00004846/// ParseResume
4847/// ::= 'resume' TypeAndValue
4848bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
4849 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00004850 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
4851 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004852
Bill Wendlingf891bf82011-07-31 06:30:59 +00004853 ResumeInst *RI = ResumeInst::Create(Exn);
4854 Inst = RI;
4855 return false;
4856}
Chris Lattnerac161bf2009-01-02 07:01:27 +00004857
4858//===----------------------------------------------------------------------===//
4859// Binary Operators.
4860//===----------------------------------------------------------------------===//
4861
4862/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004863/// ::= ArithmeticOps TypeAndValue ',' Value
4864///
4865/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
4866/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00004867bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004868 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004869 LocTy Loc; Value *LHS, *RHS;
4870 if (ParseTypeAndValue(LHS, Loc, PFS) ||
4871 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
4872 ParseValue(LHS->getType(), RHS, PFS))
4873 return true;
4874
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004875 bool Valid;
4876 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004877 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004878 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00004879 Valid = LHS->getType()->isIntOrIntVectorTy() ||
4880 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004881 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00004882 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
4883 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004884 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004885
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004886 if (!Valid)
4887 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004888
Chris Lattnerac161bf2009-01-02 07:01:27 +00004889 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4890 return false;
4891}
4892
4893/// ParseLogical
4894/// ::= ArithmeticOps TypeAndValue ',' Value {
4895bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
4896 unsigned Opc) {
4897 LocTy Loc; Value *LHS, *RHS;
4898 if (ParseTypeAndValue(LHS, Loc, PFS) ||
4899 ParseToken(lltok::comma, "expected ',' in logical operation") ||
4900 ParseValue(LHS->getType(), RHS, PFS))
4901 return true;
4902
Duncan Sands9dff9be2010-02-15 16:12:20 +00004903 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004904 return Error(Loc,"instruction requires integer or integer vector operands");
4905
4906 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4907 return false;
4908}
4909
4910
4911/// ParseCompare
4912/// ::= 'icmp' IPredicates TypeAndValue ',' Value
4913/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004914bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
4915 unsigned Opc) {
4916 // Parse the integer/fp comparison predicate.
4917 LocTy Loc;
4918 unsigned Pred;
4919 Value *LHS, *RHS;
4920 if (ParseCmpPredicate(Pred, Opc) ||
4921 ParseTypeAndValue(LHS, Loc, PFS) ||
4922 ParseToken(lltok::comma, "expected ',' after compare value") ||
4923 ParseValue(LHS->getType(), RHS, PFS))
4924 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004925
Chris Lattnerac161bf2009-01-02 07:01:27 +00004926 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00004927 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004928 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00004929 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004930 } else {
4931 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00004932 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00004933 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004934 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00004935 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004936 }
4937 return false;
4938}
4939
4940//===----------------------------------------------------------------------===//
4941// Other Instructions.
4942//===----------------------------------------------------------------------===//
4943
4944
4945/// ParseCast
4946/// ::= CastOpc TypeAndValue 'to' Type
4947bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
4948 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004949 LocTy Loc;
4950 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004951 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004952 if (ParseTypeAndValue(Op, Loc, PFS) ||
4953 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
4954 ParseType(DestTy))
4955 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004956
Chris Lattner89d856e2009-03-01 00:53:13 +00004957 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
4958 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004959 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004960 getTypeString(Op->getType()) + "' to '" +
4961 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00004962 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004963 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
4964 return false;
4965}
4966
4967/// ParseSelect
4968/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4969bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
4970 LocTy Loc;
4971 Value *Op0, *Op1, *Op2;
4972 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4973 ParseToken(lltok::comma, "expected ',' after select condition") ||
4974 ParseTypeAndValue(Op1, PFS) ||
4975 ParseToken(lltok::comma, "expected ',' after select value") ||
4976 ParseTypeAndValue(Op2, PFS))
4977 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004978
Chris Lattnerac161bf2009-01-02 07:01:27 +00004979 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
4980 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004981
Chris Lattnerac161bf2009-01-02 07:01:27 +00004982 Inst = SelectInst::Create(Op0, Op1, Op2);
4983 return false;
4984}
4985
Chris Lattnerb55ab542009-01-05 08:18:44 +00004986/// ParseVA_Arg
4987/// ::= 'va_arg' TypeAndValue ',' Type
4988bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004989 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004990 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00004991 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004992 if (ParseTypeAndValue(Op, PFS) ||
4993 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00004994 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004995 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004996
Chris Lattnerb55ab542009-01-05 08:18:44 +00004997 if (!EltTy->isFirstClassType())
4998 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004999
5000 Inst = new VAArgInst(Op, EltTy);
5001 return false;
5002}
5003
5004/// ParseExtractElement
5005/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
5006bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
5007 LocTy Loc;
5008 Value *Op0, *Op1;
5009 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5010 ParseToken(lltok::comma, "expected ',' after extract value") ||
5011 ParseTypeAndValue(Op1, PFS))
5012 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005013
Chris Lattnerac161bf2009-01-02 07:01:27 +00005014 if (!ExtractElementInst::isValidOperands(Op0, Op1))
5015 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005016
Eric Christopherc9742252009-07-25 02:28:41 +00005017 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005018 return false;
5019}
5020
5021/// ParseInsertElement
5022/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5023bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
5024 LocTy Loc;
5025 Value *Op0, *Op1, *Op2;
5026 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5027 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
5028 ParseTypeAndValue(Op1, PFS) ||
5029 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
5030 ParseTypeAndValue(Op2, PFS))
5031 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005032
Chris Lattnerac161bf2009-01-02 07:01:27 +00005033 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00005034 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005035
Chris Lattnerac161bf2009-01-02 07:01:27 +00005036 Inst = InsertElementInst::Create(Op0, Op1, Op2);
5037 return false;
5038}
5039
5040/// ParseShuffleVector
5041/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5042bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
5043 LocTy Loc;
5044 Value *Op0, *Op1, *Op2;
5045 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5046 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
5047 ParseTypeAndValue(Op1, PFS) ||
5048 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
5049 ParseTypeAndValue(Op2, PFS))
5050 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005051
Chris Lattnerac161bf2009-01-02 07:01:27 +00005052 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00005053 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005054
Chris Lattnerac161bf2009-01-02 07:01:27 +00005055 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
5056 return false;
5057}
5058
5059/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00005060/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00005061int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005062 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005063 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005064
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005065 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005066 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
5067 ParseValue(Ty, Op0, PFS) ||
5068 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00005069 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005070 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
5071 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005072
Chris Lattnerf4f03422009-12-30 05:27:33 +00005073 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005074 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
5075 while (1) {
5076 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005077
Chris Lattner3822f632009-01-02 08:05:26 +00005078 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005079 break;
5080
Chris Lattnerf4f03422009-12-30 05:27:33 +00005081 if (Lex.getKind() == lltok::MetadataVar) {
5082 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00005083 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005084 }
Devang Patel8f842d32009-10-16 18:45:49 +00005085
Chris Lattner3822f632009-01-02 08:05:26 +00005086 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005087 ParseValue(Ty, Op0, PFS) ||
5088 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00005089 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005090 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
5091 return true;
5092 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005093
Chris Lattnerac161bf2009-01-02 07:01:27 +00005094 if (!Ty->isFirstClassType())
5095 return Error(TypeLoc, "phi node must have first class type");
5096
Jay Foad52131342011-03-30 11:28:46 +00005097 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00005098 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
5099 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
5100 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005101 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005102}
5103
Bill Wendlingfae14752011-08-12 20:24:12 +00005104/// ParseLandingPad
5105/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
5106/// Clause
5107/// ::= 'catch' TypeAndValue
5108/// ::= 'filter'
5109/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
5110bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005111 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00005112
David Majnemer7fddecc2015-06-17 20:52:32 +00005113 if (ParseType(Ty, TyLoc))
Bill Wendlingfae14752011-08-12 20:24:12 +00005114 return true;
5115
David Majnemer7fddecc2015-06-17 20:52:32 +00005116 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, 0));
Bill Wendlingfae14752011-08-12 20:24:12 +00005117 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
5118
5119 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
5120 LandingPadInst::ClauseType CT;
5121 if (EatIfPresent(lltok::kw_catch))
5122 CT = LandingPadInst::Catch;
5123 else if (EatIfPresent(lltok::kw_filter))
5124 CT = LandingPadInst::Filter;
5125 else
5126 return TokError("expected 'catch' or 'filter' clause type");
5127
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00005128 Value *V;
5129 LocTy VLoc;
Owen Andersonf8f259d2015-03-09 07:13:42 +00005130 if (ParseTypeAndValue(V, VLoc, PFS))
Bill Wendlingfae14752011-08-12 20:24:12 +00005131 return true;
Bill Wendlingfae14752011-08-12 20:24:12 +00005132
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00005133 // A 'catch' type expects a non-array constant. A filter clause expects an
5134 // array constant.
5135 if (CT == LandingPadInst::Catch) {
5136 if (isa<ArrayType>(V->getType()))
5137 Error(VLoc, "'catch' clause has an invalid type");
5138 } else {
5139 if (!isa<ArrayType>(V->getType()))
5140 Error(VLoc, "'filter' clause has an invalid type");
5141 }
5142
Owen Andersonf8f259d2015-03-09 07:13:42 +00005143 Constant *CV = dyn_cast<Constant>(V);
5144 if (!CV)
5145 return Error(VLoc, "clause argument must be a constant");
5146 LP->addClause(CV);
Bill Wendlingfae14752011-08-12 20:24:12 +00005147 }
5148
Owen Andersonf8f259d2015-03-09 07:13:42 +00005149 Inst = LP.release();
Bill Wendlingfae14752011-08-12 20:24:12 +00005150 return false;
5151}
5152
Chris Lattnerac161bf2009-01-02 07:01:27 +00005153/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00005154/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
5155/// ParameterList OptionalAttrs
5156/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
5157/// ParameterList OptionalAttrs
5158/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00005159/// ParameterList OptionalAttrs
5160bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00005161 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00005162 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00005163 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00005164 LocTy BuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00005165 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00005166 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005167 LocTy RetTypeLoc;
5168 ValID CalleeID;
5169 SmallVector<ParamInfo, 16> ArgList;
5170 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005171
Reid Kleckner5772b772014-04-24 20:14:34 +00005172 if ((TCK != CallInst::TCK_None &&
5173 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005174 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00005175 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00005176 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005177 ParseValID(CalleeID) ||
Reid Kleckner83498642014-08-26 00:33:28 +00005178 ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
5179 PFS.getFunction().isVarArg()) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00005180 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00005181 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005182 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005183
Chris Lattnerac161bf2009-01-02 07:01:27 +00005184 // If RetType is a non-function pointer type, then this is the short syntax
5185 // for the call, which means that RetType is just the return type. Infer the
5186 // rest of the function argument types from the arguments that are present.
David Blaikie23af6482015-04-16 23:24:18 +00005187 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
5188 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005189 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00005190 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00005191 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
5192 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005193
Chris Lattnerac161bf2009-01-02 07:01:27 +00005194 if (!FunctionType::isValidReturnType(RetType))
5195 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005196
Owen Anderson4056ca92009-07-29 22:17:13 +00005197 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005198 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005199
Chris Lattnerac161bf2009-01-02 07:01:27 +00005200 // Look up the callee.
5201 Value *Callee;
David Blaikie23af6482015-04-16 23:24:18 +00005202 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
5203 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005204
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005205 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00005206 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00005207 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00005208 Attrs.push_back(AttributeSet::get(RetType->getContext(),
5209 AttributeSet::ReturnIndex,
5210 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005211
Chris Lattnerac161bf2009-01-02 07:01:27 +00005212 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005213
Chris Lattnerac161bf2009-01-02 07:01:27 +00005214 // Loop through FunctionType's arguments and ensure they are specified
5215 // correctly. Also, gather any parameter attributes.
5216 FunctionType::param_iterator I = Ty->param_begin();
5217 FunctionType::param_iterator E = Ty->param_end();
5218 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005219 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005220 if (I != E) {
5221 ExpectedTy = *I++;
5222 } else if (!Ty->isVarArg()) {
5223 return Error(ArgList[i].Loc, "too many arguments specified");
5224 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005225
Chris Lattnerac161bf2009-01-02 07:01:27 +00005226 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
5227 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00005228 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005229 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00005230 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
5231 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00005232 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
5233 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005234 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005235
Chris Lattnerac161bf2009-01-02 07:01:27 +00005236 if (I != E)
5237 return Error(CallLoc, "not enough parameters specified for call");
5238
David Majnemer8d22abd2015-02-23 00:01:32 +00005239 if (FnAttrs.hasAttributes()) {
5240 if (FnAttrs.hasAlignmentAttr())
5241 return Error(CallLoc, "call instructions may not have an alignment");
5242
Bill Wendlingf5075a42013-01-27 02:24:02 +00005243 Attrs.push_back(AttributeSet::get(RetType->getContext(),
5244 AttributeSet::FunctionIndex,
5245 FnAttrs));
David Majnemer8d22abd2015-02-23 00:01:32 +00005246 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005247
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005248 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00005249 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005250
David Blaikie348de692015-04-23 21:36:23 +00005251 CallInst *CI = CallInst::Create(Ty, Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00005252 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005253 CI->setCallingConv(CC);
5254 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00005255 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005256 Inst = CI;
5257 return false;
5258}
5259
5260//===----------------------------------------------------------------------===//
5261// Memory Instructions.
5262//===----------------------------------------------------------------------===//
5263
5264/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00005265/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00005266int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005267 Value *Size = nullptr;
David Majnemera3b0eb22015-02-16 08:38:03 +00005268 LocTy SizeLoc, TyLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005269 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00005270 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00005271
5272 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
5273
David Majnemera3b0eb22015-02-16 08:38:03 +00005274 if (ParseType(Ty, TyLoc)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005275
David Majnemera3b0eb22015-02-16 08:38:03 +00005276 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
5277 return Error(TyLoc, "invalid type for alloca");
David Majnemerfad5a312015-02-11 09:13:11 +00005278
Chris Lattnerb2f39502009-12-30 05:44:30 +00005279 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00005280 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00005281 if (Lex.getKind() == lltok::kw_align) {
5282 if (ParseOptionalAlignment(Alignment)) return true;
5283 } else if (Lex.getKind() == lltok::MetadataVar) {
5284 AteExtraComma = true;
5285 } else {
5286 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
5287 ParseOptionalCommaAlign(Alignment, AteExtraComma))
5288 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005289 }
5290 }
5291
Dan Gohman2140a742010-05-28 01:14:11 +00005292 if (Size && !Size->getType()->isIntegerTy())
5293 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005294
Reid Kleckner436c42e2014-01-17 23:58:17 +00005295 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
5296 AI->setUsedWithInAlloca(IsInAlloca);
5297 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00005298 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005299}
5300
5301/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00005302/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005303/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00005304/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00005305int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005306 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00005307 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00005308 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005309 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00005310 AtomicOrdering Ordering = NotAtomic;
5311 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005312
5313 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005314 isAtomic = true;
5315 Lex.Lex();
5316 }
5317
Chris Lattnerbc639292011-11-27 06:56:53 +00005318 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005319 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005320 isVolatile = true;
5321 Lex.Lex();
5322 }
5323
David Blaikie15d9a4c2015-04-06 20:59:48 +00005324 Type *Ty;
David Blaikiea79ac142015-02-27 21:17:42 +00005325 LocTy ExplicitTypeLoc = Lex.getLoc();
5326 if (ParseType(Ty) ||
5327 ParseToken(lltok::comma, "expected comma after load's type") ||
5328 ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00005329 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005330 ParseOptionalCommaAlign(Alignment, AteExtraComma))
5331 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005332
David Blaikie15d9a4c2015-04-06 20:59:48 +00005333 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005334 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00005335 if (isAtomic && !Alignment)
5336 return Error(Loc, "atomic load must have explicit non-zero alignment");
5337 if (Ordering == Release || Ordering == AcquireRelease)
5338 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005339
David Blaikiea79ac142015-02-27 21:17:42 +00005340 if (Ty != cast<PointerType>(Val->getType())->getElementType())
5341 return Error(ExplicitTypeLoc,
5342 "explicit pointee type doesn't match operand's pointee type");
5343
David Blaikie15d9a4c2015-04-06 20:59:48 +00005344 Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00005345 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005346}
5347
5348/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00005349
5350/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
5351/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00005352/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00005353int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005354 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00005355 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00005356 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005357 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00005358 AtomicOrdering Ordering = NotAtomic;
5359 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005360
5361 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005362 isAtomic = true;
5363 Lex.Lex();
5364 }
5365
Chris Lattnerbc639292011-11-27 06:56:53 +00005366 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005367 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005368 isVolatile = true;
5369 Lex.Lex();
5370 }
5371
Chris Lattnerac161bf2009-01-02 07:01:27 +00005372 if (ParseTypeAndValue(Val, Loc, PFS) ||
5373 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005374 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00005375 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005376 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005377 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00005378
Duncan Sands19d0b472010-02-16 11:11:14 +00005379 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005380 return Error(PtrLoc, "store operand must be a pointer");
5381 if (!Val->getType()->isFirstClassType())
5382 return Error(Loc, "store operand must be a first class value");
5383 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
5384 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00005385 if (isAtomic && !Alignment)
5386 return Error(Loc, "atomic store must have explicit non-zero alignment");
5387 if (Ordering == Acquire || Ordering == AcquireRelease)
5388 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005389
Eli Friedman59b66882011-08-09 23:02:53 +00005390 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00005391 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005392}
5393
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005394/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00005395/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
5396/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00005397int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005398 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
5399 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00005400 AtomicOrdering SuccessOrdering = NotAtomic;
5401 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005402 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005403 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00005404 bool isWeak = false;
5405
5406 if (EatIfPresent(lltok::kw_weak))
5407 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00005408
5409 if (EatIfPresent(lltok::kw_volatile))
5410 isVolatile = true;
5411
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005412 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
5413 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
5414 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
5415 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
5416 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00005417 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
5418 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005419 return true;
5420
Tim Northovere94a5182014-03-11 10:48:52 +00005421 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005422 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00005423 if (SuccessOrdering < FailureOrdering)
5424 return TokError("cmpxchg must be at least as ordered on success as failure");
5425 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
5426 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005427 if (!Ptr->getType()->isPointerTy())
5428 return Error(PtrLoc, "cmpxchg operand must be a pointer");
5429 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
5430 return Error(CmpLoc, "compare value and pointer type do not match");
5431 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
5432 return Error(NewLoc, "new value and pointer type do not match");
5433 if (!New->getType()->isIntegerTy())
5434 return Error(NewLoc, "cmpxchg operand must be an integer");
5435 unsigned Size = New->getType()->getPrimitiveSizeInBits();
5436 if (Size < 8 || (Size & (Size - 1)))
5437 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
5438 " integer");
5439
Tim Northover420a2162014-06-13 14:24:07 +00005440 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
5441 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005442 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00005443 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005444 Inst = CXI;
5445 return AteExtraComma ? InstExtraComma : InstNormal;
5446}
5447
5448/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00005449/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
5450/// 'singlethread'? AtomicOrdering
5451int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005452 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
5453 bool AteExtraComma = false;
5454 AtomicOrdering Ordering = NotAtomic;
5455 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005456 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005457 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00005458
5459 if (EatIfPresent(lltok::kw_volatile))
5460 isVolatile = true;
5461
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005462 switch (Lex.getKind()) {
5463 default: return TokError("expected binary operation in atomicrmw");
5464 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
5465 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
5466 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
5467 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
5468 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
5469 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
5470 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
5471 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
5472 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
5473 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
5474 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
5475 }
5476 Lex.Lex(); // Eat the operation.
5477
5478 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
5479 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
5480 ParseTypeAndValue(Val, ValLoc, PFS) ||
5481 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
5482 return true;
5483
5484 if (Ordering == Unordered)
5485 return TokError("atomicrmw cannot be unordered");
5486 if (!Ptr->getType()->isPointerTy())
5487 return Error(PtrLoc, "atomicrmw operand must be a pointer");
5488 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
5489 return Error(ValLoc, "atomicrmw value and pointer type do not match");
5490 if (!Val->getType()->isIntegerTy())
5491 return Error(ValLoc, "atomicrmw operand must be an integer");
5492 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
5493 if (Size < 8 || (Size & (Size - 1)))
5494 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
5495 " integer");
5496
5497 AtomicRMWInst *RMWI =
5498 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
5499 RMWI->setVolatile(isVolatile);
5500 Inst = RMWI;
5501 return AteExtraComma ? InstExtraComma : InstNormal;
5502}
5503
Eli Friedmanfee02c62011-07-25 23:16:38 +00005504/// ParseFence
5505/// ::= 'fence' 'singlethread'? AtomicOrdering
5506int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
5507 AtomicOrdering Ordering = NotAtomic;
5508 SynchronizationScope Scope = CrossThread;
5509 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
5510 return true;
5511
5512 if (Ordering == Unordered)
5513 return TokError("fence cannot be unordered");
5514 if (Ordering == Monotonic)
5515 return TokError("fence cannot be monotonic");
5516
5517 Inst = new FenceInst(Context, Ordering, Scope);
5518 return InstNormal;
5519}
5520
Chris Lattnerac161bf2009-01-02 07:01:27 +00005521/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00005522/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00005523int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005524 Value *Ptr = nullptr;
5525 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00005526 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00005527
Dan Gohman16cbbe42009-07-29 15:58:36 +00005528 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00005529
David Blaikie79e6c742015-02-27 19:29:02 +00005530 Type *Ty = nullptr;
5531 LocTy ExplicitTypeLoc = Lex.getLoc();
5532 if (ParseType(Ty) ||
5533 ParseToken(lltok::comma, "expected comma after getelementptr's type") ||
5534 ParseTypeAndValue(Ptr, Loc, PFS))
5535 return true;
5536
Eli Benderskyd9806682013-04-22 17:03:42 +00005537 Type *BaseType = Ptr->getType();
5538 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
5539 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005540 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005541
David Blaikie8d757942015-03-09 23:08:44 +00005542 if (Ty != BasePointerType->getElementType())
5543 return Error(ExplicitTypeLoc,
5544 "explicit pointee type doesn't match operand's pointee type");
5545
Chris Lattnerac161bf2009-01-02 07:01:27 +00005546 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005547 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00005548 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00005549 if (Lex.getKind() == lltok::MetadataVar) {
5550 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00005551 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005552 }
Chris Lattner3822f632009-01-02 08:05:26 +00005553 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00005554 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005555 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00005556 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
5557 return Error(EltLoc, "getelementptr index type missmatch");
5558 if (Val->getType()->isVectorTy()) {
5559 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
5560 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
5561 if (ValNumEl != PtrNumEl)
5562 return Error(EltLoc,
5563 "getelementptr vector index has a wrong number of elements");
5564 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005565 Indices.push_back(Val);
5566 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005567
Owen Andersone90f9922015-03-10 06:34:57 +00005568 SmallPtrSet<const Type*, 4> Visited;
David Blaikied33bad32015-04-17 22:32:13 +00005569 if (!Indices.empty() && !Ty->isSized(&Visited))
Eli Benderskyd9806682013-04-22 17:03:42 +00005570 return Error(Loc, "base element of getelementptr must be sized");
5571
David Blaikied33bad32015-04-17 22:32:13 +00005572 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005573 return Error(Loc, "invalid getelementptr indices");
David Blaikiecd7b97e2015-03-14 21:11:24 +00005574 Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00005575 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00005576 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005577 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005578}
5579
5580/// ParseExtractValue
5581/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00005582int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005583 Value *Val; LocTy Loc;
5584 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005585 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005586 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00005587 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005588 return true;
5589
Chris Lattner392be582010-02-12 20:49:41 +00005590 if (!Val->getType()->isAggregateType())
5591 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005592
Jay Foad57aa6362011-07-13 10:26:04 +00005593 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005594 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00005595 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005596 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005597}
5598
5599/// ParseInsertValue
5600/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00005601int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005602 Value *Val0, *Val1; LocTy Loc0, Loc1;
5603 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005604 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005605 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
5606 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
5607 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00005608 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005609 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005610
Chris Lattner392be582010-02-12 20:49:41 +00005611 if (!Val0->getType()->isAggregateType())
5612 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005613
David Majnemer30074532015-02-11 07:43:58 +00005614 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
5615 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005616 return Error(Loc0, "invalid indices for insertvalue");
David Majnemer30074532015-02-11 07:43:58 +00005617 if (IndexedType != Val1->getType())
5618 return Error(Loc1, "insertvalue operand and field disagree in type: '" +
5619 getTypeString(Val1->getType()) + "' instead of '" +
5620 getTypeString(IndexedType) + "'");
Jay Foad57aa6362011-07-13 10:26:04 +00005621 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005622 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005623}
Nick Lewycky49f89192009-04-04 07:22:01 +00005624
5625//===----------------------------------------------------------------------===//
5626// Embedded metadata.
5627//===----------------------------------------------------------------------===//
5628
5629/// ParseMDNodeVector
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005630/// ::= { Element (',' Element)* }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00005631/// Element
5632/// ::= 'null' | TypeAndValue
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005633bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
David Majnemer06f960d2014-12-11 20:44:09 +00005634 if (ParseToken(lltok::lbrace, "expected '{' here"))
5635 return true;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005636
Dan Gohman1e0213a2010-07-13 19:33:27 +00005637 // Check for an empty list.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005638 if (EatIfPresent(lltok::rbrace))
Dan Gohman1e0213a2010-07-13 19:33:27 +00005639 return false;
5640
Nick Lewycky49f89192009-04-04 07:22:01 +00005641 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00005642 // Null is a special case since it is typeless.
5643 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005644 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00005645 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00005646 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005647
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005648 Metadata *MD;
5649 if (ParseMetadata(MD, nullptr))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005650 return true;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005651 Elts.push_back(MD);
Nick Lewycky49f89192009-04-04 07:22:01 +00005652 } while (EatIfPresent(lltok::comma));
5653
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005654 return ParseToken(lltok::rbrace, "expected end of metadata node");
Nick Lewycky49f89192009-04-04 07:22:01 +00005655}
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00005656
5657//===----------------------------------------------------------------------===//
5658// Use-list order directives.
5659//===----------------------------------------------------------------------===//
5660bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
5661 SMLoc Loc) {
5662 if (V->use_empty())
5663 return Error(Loc, "value has no uses");
5664
5665 unsigned NumUses = 0;
5666 SmallDenseMap<const Use *, unsigned, 16> Order;
5667 for (const Use &U : V->uses()) {
5668 if (++NumUses > Indexes.size())
5669 break;
5670 Order[&U] = Indexes[NumUses - 1];
5671 }
5672 if (NumUses < 2)
5673 return Error(Loc, "value only has one use");
5674 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
5675 return Error(Loc, "wrong number of indexes, expected " +
5676 Twine(std::distance(V->use_begin(), V->use_end())));
5677
5678 V->sortUseList([&](const Use &L, const Use &R) {
5679 return Order.lookup(&L) < Order.lookup(&R);
5680 });
5681 return false;
5682}
5683
5684/// ParseUseListOrderIndexes
5685/// ::= '{' uint32 (',' uint32)+ '}'
5686bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
5687 SMLoc Loc = Lex.getLoc();
5688 if (ParseToken(lltok::lbrace, "expected '{' here"))
5689 return true;
5690 if (Lex.getKind() == lltok::rbrace)
5691 return Lex.Error("expected non-empty list of uselistorder indexes");
5692
5693 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
5694 // indexes should be distinct numbers in the range [0, size-1], and should
5695 // not be in order.
5696 unsigned Offset = 0;
5697 unsigned Max = 0;
5698 bool IsOrdered = true;
5699 assert(Indexes.empty() && "Expected empty order vector");
5700 do {
5701 unsigned Index;
5702 if (ParseUInt32(Index))
5703 return true;
5704
5705 // Update consistency checks.
5706 Offset += Index - Indexes.size();
5707 Max = std::max(Max, Index);
5708 IsOrdered &= Index == Indexes.size();
5709
5710 Indexes.push_back(Index);
5711 } while (EatIfPresent(lltok::comma));
5712
5713 if (ParseToken(lltok::rbrace, "expected '}' here"))
5714 return true;
5715
5716 if (Indexes.size() < 2)
5717 return Error(Loc, "expected >= 2 uselistorder indexes");
5718 if (Offset != 0 || Max >= Indexes.size())
5719 return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
5720 if (IsOrdered)
5721 return Error(Loc, "expected uselistorder indexes to change the order");
5722
5723 return false;
5724}
5725
5726/// ParseUseListOrder
5727/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
5728bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
5729 SMLoc Loc = Lex.getLoc();
5730 if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
5731 return true;
5732
5733 Value *V;
5734 SmallVector<unsigned, 16> Indexes;
5735 if (ParseTypeAndValue(V, PFS) ||
5736 ParseToken(lltok::comma, "expected comma in uselistorder directive") ||
5737 ParseUseListOrderIndexes(Indexes))
5738 return true;
5739
5740 return sortUseListOrder(V, Indexes, Loc);
5741}
5742
5743/// ParseUseListOrderBB
5744/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
5745bool LLParser::ParseUseListOrderBB() {
5746 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
5747 SMLoc Loc = Lex.getLoc();
5748 Lex.Lex();
5749
5750 ValID Fn, Label;
5751 SmallVector<unsigned, 16> Indexes;
5752 if (ParseValID(Fn) ||
5753 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
5754 ParseValID(Label) ||
5755 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
5756 ParseUseListOrderIndexes(Indexes))
5757 return true;
5758
5759 // Check the function.
5760 GlobalValue *GV;
5761 if (Fn.Kind == ValID::t_GlobalName)
5762 GV = M->getNamedValue(Fn.StrVal);
5763 else if (Fn.Kind == ValID::t_GlobalID)
5764 GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
5765 else
5766 return Error(Fn.Loc, "expected function name in uselistorder_bb");
5767 if (!GV)
5768 return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
5769 auto *F = dyn_cast<Function>(GV);
5770 if (!F)
5771 return Error(Fn.Loc, "expected function name in uselistorder_bb");
5772 if (F->isDeclaration())
5773 return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
5774
5775 // Check the basic block.
5776 if (Label.Kind == ValID::t_LocalID)
5777 return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
5778 if (Label.Kind != ValID::t_LocalName)
5779 return Error(Label.Loc, "expected basic block name in uselistorder_bb");
5780 Value *V = F->getValueSymbolTable().lookup(Label.StrVal);
5781 if (!V)
5782 return Error(Label.Loc, "invalid basic block in uselistorder_bb");
5783 if (!isa<BasicBlock>(V))
5784 return Error(Label.Loc, "expected basic block in uselistorder_bb");
5785
5786 return sortUseListOrder(V, Indexes, Loc);
5787}