blob: 28419fb27c136a6b15b10876deda6f0511974f75 [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
Chris Lattnerac161bf2009-01-02 07:01:27 +0000673 // See if this value already exists in the symbol table. If so, it is either
674 // a redefinition or a definition of a forward reference.
Chris Lattnere38317f2009-10-25 23:22:50 +0000675 if (GlobalValue *Val = M->getNamedValue(Name)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +0000676 // See if this was a redefinition. If so, there is no entry in
677 // ForwardRefVals.
678 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
679 I = ForwardRefVals.find(Name);
680 if (I == ForwardRefVals.end())
681 return Error(NameLoc, "redefinition of global named '@" + Name + "'");
682
683 // Otherwise, this was a definition of forward ref. Verify that types
684 // agree.
685 if (Val->getType() != GA->getType())
686 return Error(NameLoc,
687 "forward reference and definition of alias have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000688
Chris Lattnerac161bf2009-01-02 07:01:27 +0000689 // If they agree, just RAUW the old value with the alias and remove the
690 // forward ref info.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000691 Val->replaceAllUsesWith(GA.get());
Chris Lattnerac161bf2009-01-02 07:01:27 +0000692 Val->eraseFromParent();
693 ForwardRefVals.erase(I);
694 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000695
Chris Lattnerac161bf2009-01-02 07:01:27 +0000696 // Insert into the module, we know its name won't collide now.
Rafael Espindolaaa273822014-05-09 21:49:17 +0000697 M->getAliasList().push_back(GA.get());
Benjamin Kramer1dc34b42010-10-16 11:28:23 +0000698 assert(GA->getName() == Name && "Should not be a name conflict!");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000699
Rafael Espindolaaa273822014-05-09 21:49:17 +0000700 // The module owns this now
701 GA.release();
702
Chris Lattnerac161bf2009-01-02 07:01:27 +0000703 return false;
704}
705
706/// ParseGlobal
Nico Rieck7157bb72014-01-14 15:22:47 +0000707/// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
Eric Christopher536f0a92015-05-28 23:07:39 +0000708/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000709/// OptionalExternallyInitialized GlobalType Type Const
Nico Rieck7157bb72014-01-14 15:22:47 +0000710/// ::= 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
Chris Lattnerac161bf2009-01-02 07:01:27 +0000713///
Eric Christopher536f0a92015-05-28 23:07:39 +0000714/// Everything up to and including OptionalUnnamedAddr has been parsed
David Majnemerc4ab61c2014-03-09 06:41:58 +0000715/// already.
Chris Lattnerac161bf2009-01-02 07:01:27 +0000716///
717bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
718 unsigned Linkage, bool HasLinkage,
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000719 unsigned Visibility, unsigned DLLStorageClass,
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000720 GlobalVariable::ThreadLocalMode TLM,
721 bool UnnamedAddr) {
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +0000722 if (!isValidVisibilityForLinkage(Visibility, Linkage))
723 return Error(NameLoc,
724 "symbol with local linkage must have default visibility");
725
Chris Lattnerac161bf2009-01-02 07:01:27 +0000726 unsigned AddrSpace;
Rafael Espindola42a4c9f2014-06-06 01:20:28 +0000727 bool IsConstant, IsExternallyInitialized;
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000728 LocTy IsExternallyInitializedLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000729 LocTy TyLoc;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000730
Craig Topper2617dcc2014-04-15 06:32:26 +0000731 Type *Ty = nullptr;
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000732 if (ParseOptionalAddrSpace(AddrSpace) ||
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000733 ParseOptionalToken(lltok::kw_externally_initialized,
734 IsExternallyInitialized,
735 &IsExternallyInitializedLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +0000736 ParseGlobalType(IsConstant) ||
737 ParseType(Ty, TyLoc))
738 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000739
Chris Lattnerac161bf2009-01-02 07:01:27 +0000740 // If the linkage is specified and is external, then no initializer is
741 // present.
Craig Topper2617dcc2014-04-15 06:32:26 +0000742 Constant *Init = nullptr;
Nico Rieck7157bb72014-01-14 15:22:47 +0000743 if (!HasLinkage || (Linkage != GlobalValue::ExternalWeakLinkage &&
Chris Lattnerac161bf2009-01-02 07:01:27 +0000744 Linkage != GlobalValue::ExternalLinkage)) {
745 if (ParseGlobalValue(Ty, Init))
746 return true;
747 }
748
David Majnemer49b3d9b2015-02-16 08:41:08 +0000749 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
Chris Lattnerc9e1b482009-02-08 20:00:15 +0000750 return Error(TyLoc, "invalid type for global variable");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000751
David Majnemer598bd052014-12-09 05:56:09 +0000752 GlobalValue *GVal = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000753
754 // See if the global was forward referenced, if so, use the global.
Chris Lattner1f386b82009-02-02 07:24:28 +0000755 if (!Name.empty()) {
David Majnemer598bd052014-12-09 05:56:09 +0000756 GVal = M->getNamedValue(Name);
757 if (GVal) {
Chris Lattnere38317f2009-10-25 23:22:50 +0000758 if (!ForwardRefVals.erase(Name) || !isa<GlobalValue>(GVal))
759 return Error(NameLoc, "redefinition of global '@" + Name + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +0000760 }
Chris Lattnerac161bf2009-01-02 07:01:27 +0000761 } else {
762 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
763 I = ForwardRefValIDs.find(NumberedVals.size());
764 if (I != ForwardRefValIDs.end()) {
David Majnemer598bd052014-12-09 05:56:09 +0000765 GVal = I->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +0000766 ForwardRefValIDs.erase(I);
767 }
768 }
769
David Majnemer598bd052014-12-09 05:56:09 +0000770 GlobalVariable *GV;
771 if (!GVal) {
Craig Topper2617dcc2014-04-15 06:32:26 +0000772 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, nullptr,
773 Name, nullptr, GlobalVariable::NotThreadLocal,
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000774 AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +0000775 } else {
David Blaikie8f27ae42015-05-13 22:55:01 +0000776 if (GVal->getValueType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +0000777 return Error(TyLoc,
778 "forward reference and definition of global have different types");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000779
David Majnemer598bd052014-12-09 05:56:09 +0000780 GV = cast<GlobalVariable>(GVal);
781
Chris Lattnerac161bf2009-01-02 07:01:27 +0000782 // Move the forward-reference to the correct spot in the module.
783 M->getGlobalList().splice(M->global_end(), M->getGlobalList(), GV);
784 }
785
786 if (Name.empty())
787 NumberedVals.push_back(GV);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000788
Chris Lattnerac161bf2009-01-02 07:01:27 +0000789 // Set the parsed properties on the global.
790 if (Init)
791 GV->setInitializer(Init);
792 GV->setConstant(IsConstant);
793 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
794 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +0000795 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Michael Gottesman27e7ef32013-02-05 05:57:38 +0000796 GV->setExternallyInitialized(IsExternallyInitialized);
Hans Wennborgcbe34b42012-06-23 11:37:03 +0000797 GV->setThreadLocalMode(TLM);
Rafael Espindola45e6c192011-01-08 16:42:36 +0000798 GV->setUnnamedAddr(UnnamedAddr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000799
Chris Lattnerac161bf2009-01-02 07:01:27 +0000800 // Parse attributes on the global.
801 while (Lex.getKind() == lltok::comma) {
802 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000803
Chris Lattnerac161bf2009-01-02 07:01:27 +0000804 if (Lex.getKind() == lltok::kw_section) {
805 Lex.Lex();
806 GV->setSection(Lex.getStrVal());
807 if (ParseToken(lltok::StringConstant, "expected global section string"))
808 return true;
809 } else if (Lex.getKind() == lltok::kw_align) {
810 unsigned Alignment;
811 if (ParseOptionalAlignment(Alignment)) return true;
812 GV->setAlignment(Alignment);
813 } else {
David Majnemerdad0a642014-06-27 18:19:56 +0000814 Comdat *C;
Rafael Espindola83a362c2015-01-06 22:55:16 +0000815 if (parseOptionalComdat(Name, C))
David Majnemerdad0a642014-06-27 18:19:56 +0000816 return true;
817 if (C)
818 GV->setComdat(C);
819 else
820 return TokError("unknown global variable property!");
Chris Lattnerac161bf2009-01-02 07:01:27 +0000821 }
822 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000823
Chris Lattnerac161bf2009-01-02 07:01:27 +0000824 return false;
825}
826
Bill Wendling63b88192013-02-06 06:52:58 +0000827/// ParseUnnamedAttrGrp
Bill Wendlinga7c38772013-02-09 15:48:49 +0000828/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
Bill Wendling63b88192013-02-06 06:52:58 +0000829bool LLParser::ParseUnnamedAttrGrp() {
Bill Wendlinga7c38772013-02-09 15:48:49 +0000830 assert(Lex.getKind() == lltok::kw_attributes);
Bill Wendling63b88192013-02-06 06:52:58 +0000831 LocTy AttrGrpLoc = Lex.getLoc();
Bill Wendlinga7c38772013-02-09 15:48:49 +0000832 Lex.Lex();
833
David Majnemerb39e22b2014-12-09 18:33:57 +0000834 if (Lex.getKind() != lltok::AttrGrpID)
835 return TokError("expected attribute group id");
836
Bill Wendling63b88192013-02-06 06:52:58 +0000837 unsigned VarID = Lex.getUIntVal();
Bill Wendlingb32b0412013-02-08 06:32:06 +0000838 std::vector<unsigned> unused;
Michael Gottesman41748d72013-06-27 00:25:01 +0000839 LocTy BuiltinLoc;
Bill Wendling63b88192013-02-06 06:52:58 +0000840 Lex.Lex();
841
842 if (ParseToken(lltok::equal, "expected '=' here") ||
Bill Wendling63b88192013-02-06 06:52:58 +0000843 ParseToken(lltok::lbrace, "expected '{' here") ||
Bill Wendling09bd1f72013-02-22 00:12:35 +0000844 ParseFnAttributeValuePairs(NumberedAttrBuilders[VarID], unused, true,
Michael Gottesman41748d72013-06-27 00:25:01 +0000845 BuiltinLoc) ||
Bill Wendling63b88192013-02-06 06:52:58 +0000846 ParseToken(lltok::rbrace, "expected end of attribute group"))
847 return true;
848
Bill Wendlingb32b0412013-02-08 06:32:06 +0000849 if (!NumberedAttrBuilders[VarID].hasAttributes())
Bill Wendling63b88192013-02-06 06:52:58 +0000850 return Error(AttrGrpLoc, "attribute group has no attributes");
851
852 return false;
853}
854
Bill Wendling8b0321d2013-02-08 00:52:31 +0000855/// ParseFnAttributeValuePairs
Bill Wendling63b88192013-02-06 06:52:58 +0000856/// ::= <attr> | <attr> '=' <value>
Bill Wendlingb32b0412013-02-08 06:32:06 +0000857bool LLParser::ParseFnAttributeValuePairs(AttrBuilder &B,
858 std::vector<unsigned> &FwdRefAttrGrps,
Michael Gottesman41748d72013-06-27 00:25:01 +0000859 bool inAttrGrp, LocTy &BuiltinLoc) {
Bill Wendling8b0321d2013-02-08 00:52:31 +0000860 bool HaveError = false;
861
862 B.clear();
863
Bill Wendling63b88192013-02-06 06:52:58 +0000864 while (true) {
865 lltok::Kind Token = Lex.getKind();
Michael Gottesman41748d72013-06-27 00:25:01 +0000866 if (Token == lltok::kw_builtin)
867 BuiltinLoc = Lex.getLoc();
Bill Wendling63b88192013-02-06 06:52:58 +0000868 switch (Token) {
869 default:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000870 if (!inAttrGrp) return HaveError;
Bill Wendling63b88192013-02-06 06:52:58 +0000871 return Error(Lex.getLoc(), "unterminated attribute group");
872 case lltok::rbrace:
873 // Finished.
874 return false;
875
Bill Wendlingb32b0412013-02-08 06:32:06 +0000876 case lltok::AttrGrpID: {
877 // Allow a function to reference an attribute group:
878 //
879 // define void @foo() #1 { ... }
880 if (inAttrGrp)
881 HaveError |=
882 Error(Lex.getLoc(),
883 "cannot have an attribute group reference in an attribute group");
884
885 unsigned AttrGrpNum = Lex.getUIntVal();
886 if (inAttrGrp) break;
887
888 // Save the reference to the attribute group. We'll fill it in later.
889 FwdRefAttrGrps.push_back(AttrGrpNum);
890 break;
891 }
Bill Wendling63b88192013-02-06 06:52:58 +0000892 // Target-dependent attributes:
893 case lltok::StringConstant: {
894 std::string Attr = Lex.getStrVal();
895 Lex.Lex();
896 std::string Val;
897 if (EatIfPresent(lltok::equal) &&
898 ParseStringConstant(Val))
899 return true;
900
901 B.addAttribute(Attr, Val);
Bill Wendlingb1ea9802013-02-10 10:12:50 +0000902 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000903 }
904
905 // Target-independent attributes:
906 case lltok::kw_align: {
Bill Wendlingc62789f2013-04-18 18:30:16 +0000907 // As a hack, we allow function alignment to be initially parsed as an
908 // attribute on a function declaration/definition or added to an attribute
909 // group and later moved to the alignment field.
Bill Wendling63b88192013-02-06 06:52:58 +0000910 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000911 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000912 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000913 if (ParseToken(lltok::equal, "expected '=' here") ||
914 ParseUInt32(Alignment))
915 return true;
916 } else {
917 if (ParseOptionalAlignment(Alignment))
918 return true;
919 }
Bill Wendling63b88192013-02-06 06:52:58 +0000920 B.addAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000921 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000922 }
923 case lltok::kw_alignstack: {
924 unsigned Alignment;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000925 if (inAttrGrp) {
Bill Wendling44b08bf2013-02-10 23:15:51 +0000926 Lex.Lex();
Bill Wendling8b0321d2013-02-08 00:52:31 +0000927 if (ParseToken(lltok::equal, "expected '=' here") ||
928 ParseUInt32(Alignment))
929 return true;
930 } else {
931 if (ParseOptionalStackAlignment(Alignment))
932 return true;
933 }
Bill Wendling63b88192013-02-06 06:52:58 +0000934 B.addStackAlignmentAttr(Alignment);
Bill Wendling8b0321d2013-02-08 00:52:31 +0000935 continue;
Bill Wendling63b88192013-02-06 06:52:58 +0000936 }
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000937 case lltok::kw_alwaysinline: B.addAttribute(Attribute::AlwaysInline); break;
Michael Gottesman41748d72013-06-27 00:25:01 +0000938 case lltok::kw_builtin: B.addAttribute(Attribute::Builtin); break;
Diego Novilloc6399532013-05-24 12:26:52 +0000939 case lltok::kw_cold: B.addAttribute(Attribute::Cold); break;
Owen Anderson85fa7d52015-05-26 23:48:40 +0000940 case lltok::kw_convergent: B.addAttribute(Attribute::Convergent); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000941 case lltok::kw_inlinehint: B.addAttribute(Attribute::InlineHint); break;
Tom Roeder44cb65f2014-06-05 19:29:43 +0000942 case lltok::kw_jumptable: B.addAttribute(Attribute::JumpTable); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000943 case lltok::kw_minsize: B.addAttribute(Attribute::MinSize); break;
944 case lltok::kw_naked: B.addAttribute(Attribute::Naked); break;
945 case lltok::kw_nobuiltin: B.addAttribute(Attribute::NoBuiltin); break;
946 case lltok::kw_noduplicate: B.addAttribute(Attribute::NoDuplicate); break;
947 case lltok::kw_noimplicitfloat: B.addAttribute(Attribute::NoImplicitFloat); break;
948 case lltok::kw_noinline: B.addAttribute(Attribute::NoInline); break;
949 case lltok::kw_nonlazybind: B.addAttribute(Attribute::NonLazyBind); break;
950 case lltok::kw_noredzone: B.addAttribute(Attribute::NoRedZone); break;
951 case lltok::kw_noreturn: B.addAttribute(Attribute::NoReturn); break;
952 case lltok::kw_nounwind: B.addAttribute(Attribute::NoUnwind); break;
Andrea Di Biagio377496b2013-08-23 11:53:55 +0000953 case lltok::kw_optnone: B.addAttribute(Attribute::OptimizeNone); break;
Kostya Serebryanycf880b92013-02-26 06:58:09 +0000954 case lltok::kw_optsize: B.addAttribute(Attribute::OptimizeForSize); break;
955 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
956 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
957 case lltok::kw_returns_twice: B.addAttribute(Attribute::ReturnsTwice); break;
958 case lltok::kw_ssp: B.addAttribute(Attribute::StackProtect); break;
959 case lltok::kw_sspreq: B.addAttribute(Attribute::StackProtectReq); break;
960 case lltok::kw_sspstrong: B.addAttribute(Attribute::StackProtectStrong); break;
961 case lltok::kw_sanitize_address: B.addAttribute(Attribute::SanitizeAddress); break;
962 case lltok::kw_sanitize_thread: B.addAttribute(Attribute::SanitizeThread); break;
963 case lltok::kw_sanitize_memory: B.addAttribute(Attribute::SanitizeMemory); break;
964 case lltok::kw_uwtable: B.addAttribute(Attribute::UWTable); break;
Bill Wendling8b0321d2013-02-08 00:52:31 +0000965
966 // Error handling.
967 case lltok::kw_inreg:
968 case lltok::kw_signext:
969 case lltok::kw_zeroext:
970 HaveError |=
971 Error(Lex.getLoc(),
972 "invalid use of attribute on a function");
973 break;
974 case lltok::kw_byval:
Hal Finkelb0407ba2014-07-18 15:51:28 +0000975 case lltok::kw_dereferenceable:
Sanjoy Das31ea6d12015-04-16 20:29:50 +0000976 case lltok::kw_dereferenceable_or_null:
Reid Klecknera534a382013-12-19 02:14:12 +0000977 case lltok::kw_inalloca:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000978 case lltok::kw_nest:
979 case lltok::kw_noalias:
980 case lltok::kw_nocapture:
Nick Lewyckyd52b1522014-05-20 01:23:40 +0000981 case lltok::kw_nonnull:
Stephen Linb8bd2322013-04-20 05:14:40 +0000982 case lltok::kw_returned:
Bill Wendling8b0321d2013-02-08 00:52:31 +0000983 case lltok::kw_sret:
984 HaveError |=
985 Error(Lex.getLoc(),
986 "invalid use of parameter-only attribute on a function");
987 break;
Bill Wendling63b88192013-02-06 06:52:58 +0000988 }
989
990 Lex.Lex();
991 }
992}
Chris Lattnerac161bf2009-01-02 07:01:27 +0000993
994//===----------------------------------------------------------------------===//
995// GlobalValue Reference/Resolution Routines.
996//===----------------------------------------------------------------------===//
997
998/// GetGlobalVal - Get a value with the specified name or ID, creating a
999/// forward reference record if needed. This can return null if the value
1000/// exists but does not have the right type.
Chris Lattner229907c2011-07-18 04:54:35 +00001001GlobalValue *LLParser::GetGlobalVal(const std::string &Name, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00001002 LocTy Loc) {
Chris Lattner229907c2011-07-18 04:54:35 +00001003 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001004 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001005 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001006 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001007 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001008
Chris Lattnerac161bf2009-01-02 07:01:27 +00001009 // Look this name up in the normal function symbol table.
1010 GlobalValue *Val =
1011 cast_or_null<GlobalValue>(M->getValueSymbolTable().lookup(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001012
Chris Lattnerac161bf2009-01-02 07:01:27 +00001013 // If this is a forward reference for the value, see if we already created a
1014 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001015 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001016 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator
1017 I = ForwardRefVals.find(Name);
1018 if (I != ForwardRefVals.end())
1019 Val = I->second.first;
1020 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001021
Chris Lattnerac161bf2009-01-02 07:01:27 +00001022 // If we have the value in the symbol table or fwd-ref table, return it.
1023 if (Val) {
1024 if (Val->getType() == Ty) return Val;
1025 Error(Loc, "'@" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001026 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001027 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001028 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001029
Chris Lattnerac161bf2009-01-02 07:01:27 +00001030 // Otherwise, create a new forward reference for this value and remember it.
1031 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001032 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001033 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001034 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001035 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001036 GlobalValue::ExternalWeakLinkage, nullptr, Name,
1037 nullptr, GlobalVariable::NotThreadLocal,
Justin Holewinski898a0a02012-11-16 21:03:47 +00001038 PTy->getAddressSpace());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001039
Chris Lattnerac161bf2009-01-02 07:01:27 +00001040 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
1041 return FwdVal;
1042}
1043
Chris Lattner229907c2011-07-18 04:54:35 +00001044GlobalValue *LLParser::GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1045 PointerType *PTy = dyn_cast<PointerType>(Ty);
Craig Topper2617dcc2014-04-15 06:32:26 +00001046 if (!PTy) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001047 Error(Loc, "global variable reference must have pointer type");
Craig Topper2617dcc2014-04-15 06:32:26 +00001048 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001049 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001050
Craig Topper2617dcc2014-04-15 06:32:26 +00001051 GlobalValue *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001052
Chris Lattnerac161bf2009-01-02 07:01:27 +00001053 // If this is a forward reference for the value, see if we already created a
1054 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00001055 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001056 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator
1057 I = ForwardRefValIDs.find(ID);
1058 if (I != ForwardRefValIDs.end())
1059 Val = I->second.first;
1060 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001061
Chris Lattnerac161bf2009-01-02 07:01:27 +00001062 // If we have the value in the symbol table or fwd-ref table, return it.
1063 if (Val) {
1064 if (Val->getType() == Ty) return Val;
Benjamin Kramerc7583112010-09-27 17:42:11 +00001065 Error(Loc, "'@" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00001066 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00001067 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001068 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001069
Chris Lattnerac161bf2009-01-02 07:01:27 +00001070 // Otherwise, create a new forward reference for this value and remember it.
1071 GlobalValue *FwdVal;
Chris Lattner229907c2011-07-18 04:54:35 +00001072 if (FunctionType *FT = dyn_cast<FunctionType>(PTy->getElementType()))
Duncan Sandse2881052009-03-11 08:08:06 +00001073 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001074 else
Owen Andersonb17f3292009-07-08 19:03:57 +00001075 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
Craig Topper2617dcc2014-04-15 06:32:26 +00001076 GlobalValue::ExternalWeakLinkage, nullptr, "");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001077
Chris Lattnerac161bf2009-01-02 07:01:27 +00001078 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
1079 return FwdVal;
1080}
1081
1082
1083//===----------------------------------------------------------------------===//
David Majnemerdad0a642014-06-27 18:19:56 +00001084// Comdat Reference/Resolution Routines.
1085//===----------------------------------------------------------------------===//
1086
1087Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1088 // Look this name up in the comdat symbol table.
1089 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1090 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Name);
1091 if (I != ComdatSymTab.end())
1092 return &I->second;
1093
1094 // Otherwise, create a new forward reference for this value and remember it.
1095 Comdat *C = M->getOrInsertComdat(Name);
1096 ForwardRefComdats[Name] = Loc;
1097 return C;
1098}
1099
1100
1101//===----------------------------------------------------------------------===//
Chris Lattnerac161bf2009-01-02 07:01:27 +00001102// Helper Routines.
1103//===----------------------------------------------------------------------===//
1104
1105/// ParseToken - If the current token has the specified kind, eat it and return
1106/// success. Otherwise, emit the specified error and return failure.
1107bool LLParser::ParseToken(lltok::Kind T, const char *ErrMsg) {
1108 if (Lex.getKind() != T)
1109 return TokError(ErrMsg);
1110 Lex.Lex();
1111 return false;
1112}
1113
Chris Lattner3822f632009-01-02 08:05:26 +00001114/// ParseStringConstant
1115/// ::= StringConstant
1116bool LLParser::ParseStringConstant(std::string &Result) {
1117 if (Lex.getKind() != lltok::StringConstant)
1118 return TokError("expected string constant");
1119 Result = Lex.getStrVal();
1120 Lex.Lex();
1121 return false;
1122}
1123
1124/// ParseUInt32
1125/// ::= uint32
1126bool LLParser::ParseUInt32(unsigned &Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001127 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1128 return TokError("expected integer");
1129 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(0xFFFFFFFFULL+1);
1130 if (Val64 != unsigned(Val64))
1131 return TokError("expected 32-bit integer (too large)");
1132 Val = Val64;
1133 Lex.Lex();
1134 return false;
1135}
1136
Hal Finkelb0407ba2014-07-18 15:51:28 +00001137/// ParseUInt64
1138/// ::= uint64
1139bool LLParser::ParseUInt64(uint64_t &Val) {
1140 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1141 return TokError("expected integer");
1142 Val = Lex.getAPSIntVal().getLimitedValue();
1143 Lex.Lex();
1144 return false;
1145}
1146
Hans Wennborgcbe34b42012-06-23 11:37:03 +00001147/// ParseTLSModel
1148/// := 'localdynamic'
1149/// := 'initialexec'
1150/// := 'localexec'
1151bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1152 switch (Lex.getKind()) {
1153 default:
1154 return TokError("expected localdynamic, initialexec or localexec");
1155 case lltok::kw_localdynamic:
1156 TLM = GlobalVariable::LocalDynamicTLSModel;
1157 break;
1158 case lltok::kw_initialexec:
1159 TLM = GlobalVariable::InitialExecTLSModel;
1160 break;
1161 case lltok::kw_localexec:
1162 TLM = GlobalVariable::LocalExecTLSModel;
1163 break;
1164 }
1165
1166 Lex.Lex();
1167 return false;
1168}
1169
1170/// ParseOptionalThreadLocal
1171/// := /*empty*/
1172/// := 'thread_local'
1173/// := 'thread_local' '(' tlsmodel ')'
1174bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1175 TLM = GlobalVariable::NotThreadLocal;
1176 if (!EatIfPresent(lltok::kw_thread_local))
1177 return false;
1178
1179 TLM = GlobalVariable::GeneralDynamicTLSModel;
1180 if (Lex.getKind() == lltok::lparen) {
1181 Lex.Lex();
1182 return ParseTLSModel(TLM) ||
1183 ParseToken(lltok::rparen, "expected ')' after thread local model");
1184 }
1185 return false;
1186}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001187
1188/// ParseOptionalAddrSpace
1189/// := /*empty*/
1190/// := 'addrspace' '(' uint32 ')'
1191bool LLParser::ParseOptionalAddrSpace(unsigned &AddrSpace) {
1192 AddrSpace = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001193 if (!EatIfPresent(lltok::kw_addrspace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001194 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001195 return ParseToken(lltok::lparen, "expected '(' in address space") ||
Chris Lattner3822f632009-01-02 08:05:26 +00001196 ParseUInt32(AddrSpace) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00001197 ParseToken(lltok::rparen, "expected ')' in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001198}
Chris Lattnerac161bf2009-01-02 07:01:27 +00001199
Bill Wendling34c2eb22012-12-04 23:40:58 +00001200/// ParseOptionalParamAttrs - Parse a potentially empty list of parameter attributes.
1201bool LLParser::ParseOptionalParamAttrs(AttrBuilder &B) {
1202 bool HaveError = false;
1203
1204 B.clear();
1205
1206 while (1) {
1207 lltok::Kind Token = Lex.getKind();
1208 switch (Token) {
1209 default: // End of attributes.
1210 return HaveError;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001211 case lltok::kw_align: {
1212 unsigned Alignment;
1213 if (ParseOptionalAlignment(Alignment))
1214 return true;
Bill Wendling68d24012012-10-08 22:20:14 +00001215 B.addAlignmentAttr(Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001216 continue;
1217 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001218 case lltok::kw_byval: B.addAttribute(Attribute::ByVal); break;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001219 case lltok::kw_dereferenceable: {
1220 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001221 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001222 return true;
1223 B.addDereferenceableAttr(Bytes);
1224 continue;
1225 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001226 case lltok::kw_dereferenceable_or_null: {
1227 uint64_t Bytes;
1228 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1229 return true;
1230 B.addDereferenceableOrNullAttr(Bytes);
1231 continue;
1232 }
Reid Klecknera534a382013-12-19 02:14:12 +00001233 case lltok::kw_inalloca: B.addAttribute(Attribute::InAlloca); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001234 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1235 case lltok::kw_nest: B.addAttribute(Attribute::Nest); break;
1236 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
1237 case lltok::kw_nocapture: B.addAttribute(Attribute::NoCapture); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001238 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001239 case lltok::kw_readnone: B.addAttribute(Attribute::ReadNone); break;
1240 case lltok::kw_readonly: B.addAttribute(Attribute::ReadOnly); break;
Stephen Linb8bd2322013-04-20 05:14:40 +00001241 case lltok::kw_returned: B.addAttribute(Attribute::Returned); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001242 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1243 case lltok::kw_sret: B.addAttribute(Attribute::StructRet); break;
1244 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Charles Davisbe5557e2010-02-12 00:31:15 +00001245
Stephen Lin7577ed52013-04-20 13:16:13 +00001246 case lltok::kw_alignstack:
1247 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001248 case lltok::kw_builtin:
Stephen Lin7577ed52013-04-20 13:16:13 +00001249 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001250 case lltok::kw_jumptable:
Stephen Lin7577ed52013-04-20 13:16:13 +00001251 case lltok::kw_minsize:
1252 case lltok::kw_naked:
1253 case lltok::kw_nobuiltin:
1254 case lltok::kw_noduplicate:
1255 case lltok::kw_noimplicitfloat:
1256 case lltok::kw_noinline:
1257 case lltok::kw_nonlazybind:
1258 case lltok::kw_noredzone:
1259 case lltok::kw_noreturn:
1260 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001261 case lltok::kw_optnone:
Stephen Lin7577ed52013-04-20 13:16:13 +00001262 case lltok::kw_optsize:
Stephen Lin7577ed52013-04-20 13:16:13 +00001263 case lltok::kw_returns_twice:
1264 case lltok::kw_sanitize_address:
1265 case lltok::kw_sanitize_memory:
1266 case lltok::kw_sanitize_thread:
1267 case lltok::kw_ssp:
1268 case lltok::kw_sspreq:
1269 case lltok::kw_sspstrong:
1270 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001271 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
1272 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001273 }
Bill Wendling0be9c402012-09-28 22:30:18 +00001274
Bill Wendling34c2eb22012-12-04 23:40:58 +00001275 Lex.Lex();
1276 }
1277}
1278
1279/// ParseOptionalReturnAttrs - Parse a potentially empty list of return attributes.
1280bool LLParser::ParseOptionalReturnAttrs(AttrBuilder &B) {
1281 bool HaveError = false;
1282
1283 B.clear();
1284
1285 while (1) {
1286 lltok::Kind Token = Lex.getKind();
Bill Wendling0be9c402012-09-28 22:30:18 +00001287 switch (Token) {
Bill Wendling34c2eb22012-12-04 23:40:58 +00001288 default: // End of attributes.
1289 return HaveError;
Hal Finkelb0407ba2014-07-18 15:51:28 +00001290 case lltok::kw_dereferenceable: {
1291 uint64_t Bytes;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001292 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable, Bytes))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001293 return true;
1294 B.addDereferenceableAttr(Bytes);
1295 continue;
1296 }
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001297 case lltok::kw_dereferenceable_or_null: {
1298 uint64_t Bytes;
1299 if (ParseOptionalDerefAttrBytes(lltok::kw_dereferenceable_or_null, Bytes))
1300 return true;
1301 B.addDereferenceableOrNullAttr(Bytes);
1302 continue;
1303 }
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001304 case lltok::kw_inreg: B.addAttribute(Attribute::InReg); break;
1305 case lltok::kw_noalias: B.addAttribute(Attribute::NoAlias); break;
Nick Lewyckyd52b1522014-05-20 01:23:40 +00001306 case lltok::kw_nonnull: B.addAttribute(Attribute::NonNull); break;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001307 case lltok::kw_signext: B.addAttribute(Attribute::SExt); break;
1308 case lltok::kw_zeroext: B.addAttribute(Attribute::ZExt); break;
Bill Wendling0be9c402012-09-28 22:30:18 +00001309
Bill Wendling34c2eb22012-12-04 23:40:58 +00001310 // Error handling.
Stephen Lin7577ed52013-04-20 13:16:13 +00001311 case lltok::kw_align:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001312 case lltok::kw_byval:
Reid Klecknera534a382013-12-19 02:14:12 +00001313 case lltok::kw_inalloca:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001314 case lltok::kw_nest:
1315 case lltok::kw_nocapture:
Stephen Linb8bd2322013-04-20 05:14:40 +00001316 case lltok::kw_returned:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001317 case lltok::kw_sret:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001318 HaveError |= Error(Lex.getLoc(), "invalid use of parameter-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001319 break;
James Molloy4f6fb952012-12-20 16:04:27 +00001320
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001321 case lltok::kw_alignstack:
1322 case lltok::kw_alwaysinline:
Michael Gottesman41748d72013-06-27 00:25:01 +00001323 case lltok::kw_builtin:
Diego Novilloc6399532013-05-24 12:26:52 +00001324 case lltok::kw_cold:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001325 case lltok::kw_inlinehint:
Tom Roeder44cb65f2014-06-05 19:29:43 +00001326 case lltok::kw_jumptable:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001327 case lltok::kw_minsize:
1328 case lltok::kw_naked:
1329 case lltok::kw_nobuiltin:
1330 case lltok::kw_noduplicate:
1331 case lltok::kw_noimplicitfloat:
1332 case lltok::kw_noinline:
1333 case lltok::kw_nonlazybind:
1334 case lltok::kw_noredzone:
1335 case lltok::kw_noreturn:
1336 case lltok::kw_nounwind:
Andrea Di Biagio377496b2013-08-23 11:53:55 +00001337 case lltok::kw_optnone:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001338 case lltok::kw_optsize:
Chandler Carruth9f6b59a2013-04-09 19:46:46 +00001339 case lltok::kw_returns_twice:
1340 case lltok::kw_sanitize_address:
1341 case lltok::kw_sanitize_memory:
1342 case lltok::kw_sanitize_thread:
1343 case lltok::kw_ssp:
1344 case lltok::kw_sspreq:
1345 case lltok::kw_sspstrong:
1346 case lltok::kw_uwtable:
Bill Wendling34c2eb22012-12-04 23:40:58 +00001347 HaveError |= Error(Lex.getLoc(), "invalid use of function-only attribute");
Bill Wendling0be9c402012-09-28 22:30:18 +00001348 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001349
1350 case lltok::kw_readnone:
1351 case lltok::kw_readonly:
1352 HaveError |= Error(Lex.getLoc(), "invalid use of attribute on return type");
Bill Wendling0be9c402012-09-28 22:30:18 +00001353 }
1354
Chris Lattnerac161bf2009-01-02 07:01:27 +00001355 Lex.Lex();
1356 }
1357}
1358
1359/// ParseOptionalLinkage
1360/// ::= /*empty*/
Rafael Espindola6de96a12009-01-15 20:18:42 +00001361/// ::= 'private'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001362/// ::= 'internal'
1363/// ::= 'weak'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001364/// ::= 'weak_odr'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001365/// ::= 'linkonce'
Duncan Sands12da8ce2009-03-07 15:45:40 +00001366/// ::= 'linkonce_odr'
Bill Wendling03bcd6e2010-07-01 21:55:59 +00001367/// ::= 'available_externally'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001368/// ::= 'appending'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001369/// ::= 'common'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001370/// ::= 'extern_weak'
1371/// ::= 'external'
1372bool LLParser::ParseOptionalLinkage(unsigned &Res, bool &HasLinkage) {
1373 HasLinkage = false;
1374 switch (Lex.getKind()) {
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001375 default: Res=GlobalValue::ExternalLinkage; return false;
1376 case lltok::kw_private: Res = GlobalValue::PrivateLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001377 case lltok::kw_internal: Res = GlobalValue::InternalLinkage; break;
1378 case lltok::kw_weak: Res = GlobalValue::WeakAnyLinkage; break;
1379 case lltok::kw_weak_odr: Res = GlobalValue::WeakODRLinkage; break;
1380 case lltok::kw_linkonce: Res = GlobalValue::LinkOnceAnyLinkage; break;
1381 case lltok::kw_linkonce_odr: Res = GlobalValue::LinkOnceODRLinkage; break;
Chris Lattner184f1be2009-04-13 05:44:34 +00001382 case lltok::kw_available_externally:
1383 Res = GlobalValue::AvailableExternallyLinkage;
1384 break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001385 case lltok::kw_appending: Res = GlobalValue::AppendingLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001386 case lltok::kw_common: Res = GlobalValue::CommonLinkage; break;
Bill Wendlinga3c6f6b2009-07-20 01:03:30 +00001387 case lltok::kw_extern_weak: Res = GlobalValue::ExternalWeakLinkage; break;
1388 case lltok::kw_external: Res = GlobalValue::ExternalLinkage; break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001389 }
1390 Lex.Lex();
1391 HasLinkage = true;
1392 return false;
1393}
1394
1395/// ParseOptionalVisibility
1396/// ::= /*empty*/
1397/// ::= 'default'
1398/// ::= 'hidden'
1399/// ::= 'protected'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001400///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001401bool LLParser::ParseOptionalVisibility(unsigned &Res) {
1402 switch (Lex.getKind()) {
1403 default: Res = GlobalValue::DefaultVisibility; return false;
1404 case lltok::kw_default: Res = GlobalValue::DefaultVisibility; break;
1405 case lltok::kw_hidden: Res = GlobalValue::HiddenVisibility; break;
1406 case lltok::kw_protected: Res = GlobalValue::ProtectedVisibility; break;
1407 }
1408 Lex.Lex();
1409 return false;
1410}
1411
Nico Rieck7157bb72014-01-14 15:22:47 +00001412/// ParseOptionalDLLStorageClass
1413/// ::= /*empty*/
1414/// ::= 'dllimport'
1415/// ::= 'dllexport'
1416///
1417bool LLParser::ParseOptionalDLLStorageClass(unsigned &Res) {
1418 switch (Lex.getKind()) {
1419 default: Res = GlobalValue::DefaultStorageClass; return false;
1420 case lltok::kw_dllimport: Res = GlobalValue::DLLImportStorageClass; break;
1421 case lltok::kw_dllexport: Res = GlobalValue::DLLExportStorageClass; break;
1422 }
1423 Lex.Lex();
1424 return false;
1425}
1426
Chris Lattnerac161bf2009-01-02 07:01:27 +00001427/// ParseOptionalCallingConv
1428/// ::= /*empty*/
1429/// ::= 'ccc'
1430/// ::= 'fastcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001431/// ::= 'intel_ocl_bicc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001432/// ::= 'coldcc'
1433/// ::= 'x86_stdcallcc'
1434/// ::= 'x86_fastcallcc'
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001435/// ::= 'x86_thiscallcc'
Reid Kleckner9ccce992014-10-28 01:29:26 +00001436/// ::= 'x86_vectorcallcc'
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001437/// ::= 'arm_apcscc'
1438/// ::= 'arm_aapcscc'
1439/// ::= 'arm_aapcs_vfpcc'
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001440/// ::= 'msp430_intrcc'
Che-Liang Chiou29947902010-09-25 07:46:17 +00001441/// ::= 'ptx_kernel'
1442/// ::= 'ptx_device'
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001443/// ::= 'spir_func'
1444/// ::= 'spir_kernel'
Charles Davise8f297c2013-07-12 06:02:35 +00001445/// ::= 'x86_64_sysvcc'
1446/// ::= 'x86_64_win64cc'
Andrew Tricka3a11de2013-10-31 22:12:01 +00001447/// ::= 'webkit_jscc'
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001448/// ::= 'anyregcc'
Juergen Ributzkae6250132014-01-17 19:47:03 +00001449/// ::= 'preserve_mostcc'
1450/// ::= 'preserve_allcc'
Reid Kleckner35fc3632014-12-01 21:04:44 +00001451/// ::= 'ghccc'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001452/// ::= 'cc' UINT
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001453///
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001454bool LLParser::ParseOptionalCallingConv(unsigned &CC) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001455 switch (Lex.getKind()) {
1456 default: CC = CallingConv::C; return false;
1457 case lltok::kw_ccc: CC = CallingConv::C; break;
1458 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
1459 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
1460 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
1461 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
Anton Korobeynikov8f35fab2010-05-16 09:08:45 +00001462 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
Reid Kleckner9ccce992014-10-28 01:29:26 +00001463 case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
Anton Korobeynikova8fd40b2009-06-16 18:50:49 +00001464 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
1465 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
1466 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
Anton Korobeynikov27a0ecf2009-12-07 02:27:35 +00001467 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
Che-Liang Chiou29947902010-09-25 07:46:17 +00001468 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
1469 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
Micah Villmow48c8ddc2012-10-01 17:01:31 +00001470 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
1471 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
Elena Demikhovskyd6afb032012-10-24 14:46:16 +00001472 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
Charles Davise8f297c2013-07-12 06:02:35 +00001473 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
1474 case lltok::kw_x86_64_win64cc: CC = CallingConv::X86_64_Win64; break;
Andrew Tricka3a11de2013-10-31 22:12:01 +00001475 case lltok::kw_webkit_jscc: CC = CallingConv::WebKit_JS; break;
Juergen Ributzka9969d3e2013-11-08 23:28:16 +00001476 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
Juergen Ributzkae6250132014-01-17 19:47:03 +00001477 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
1478 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
Reid Kleckner35fc3632014-12-01 21:04:44 +00001479 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
Sandeep Patel68c5f472009-09-02 08:44:58 +00001480 case lltok::kw_cc: {
Sandeep Patel68c5f472009-09-02 08:44:58 +00001481 Lex.Lex();
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00001482 return ParseUInt32(CC);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001483 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00001484 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001485
Chris Lattnerac161bf2009-01-02 07:01:27 +00001486 Lex.Lex();
1487 return false;
1488}
1489
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001490/// ParseMetadataAttachment
1491/// ::= !dbg !42
1492bool LLParser::ParseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
1493 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
1494
1495 std::string Name = Lex.getStrVal();
1496 Kind = M->getMDKindID(Name);
1497 Lex.Lex();
1498
1499 return ParseMDNode(MD);
1500}
1501
Chris Lattner5c427632009-12-30 05:31:19 +00001502/// ParseInstructionMetadata
Chris Lattner596760d2009-12-29 21:25:40 +00001503/// ::= !dbg !42 (',' !dbg !57)*
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001504bool LLParser::ParseInstructionMetadata(Instruction &Inst) {
Chris Lattner5c427632009-12-30 05:31:19 +00001505 do {
1506 if (Lex.getKind() != lltok::MetadataVar)
1507 return TokError("expected metadata after comma");
Devang Patelba4a6fd2009-09-29 00:01:14 +00001508
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001509 unsigned MDK;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00001510 MDNode *N;
Duncan P. N. Exon Smith19717ea2015-04-24 21:21:57 +00001511 if (ParseMetadataAttachment(MDK, N))
Chris Lattner1eed2d62009-12-30 04:56:59 +00001512 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001513
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001514 Inst.setMetadata(MDK, N);
Manman Ren209b17c2013-09-28 00:22:27 +00001515 if (MDK == LLVMContext::MD_tbaa)
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00001516 InstsWithTBAATag.push_back(&Inst);
Manman Ren209b17c2013-09-28 00:22:27 +00001517
Chris Lattner596760d2009-12-29 21:25:40 +00001518 // If this is the end of the list, we're done.
Chris Lattner5c427632009-12-30 05:31:19 +00001519 } while (EatIfPresent(lltok::comma));
1520 return false;
Devang Patelea8a4b92009-09-17 23:04:48 +00001521}
1522
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +00001523/// ParseOptionalFunctionMetadata
1524/// ::= (!dbg !57)*
1525bool LLParser::ParseOptionalFunctionMetadata(Function &F) {
1526 while (Lex.getKind() == lltok::MetadataVar) {
1527 unsigned MDK;
1528 MDNode *N;
1529 if (ParseMetadataAttachment(MDK, N))
1530 return true;
1531
1532 F.setMetadata(MDK, N);
1533 }
1534 return false;
1535}
1536
Chris Lattnerac161bf2009-01-02 07:01:27 +00001537/// ParseOptionalAlignment
1538/// ::= /* empty */
1539/// ::= 'align' 4
1540bool LLParser::ParseOptionalAlignment(unsigned &Alignment) {
1541 Alignment = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001542 if (!EatIfPresent(lltok::kw_align))
1543 return false;
Chris Lattnerd11f5142009-01-05 07:46:05 +00001544 LocTy AlignLoc = Lex.getLoc();
1545 if (ParseUInt32(Alignment)) return true;
1546 if (!isPowerOf2_32(Alignment))
1547 return Error(AlignLoc, "alignment is not a power of two");
Dan Gohmand566d2c2010-07-30 21:07:05 +00001548 if (Alignment > Value::MaximumAlignment)
Dan Gohmana7e5a242010-07-28 20:12:04 +00001549 return Error(AlignLoc, "huge alignments are not supported yet");
Chris Lattnerd11f5142009-01-05 07:46:05 +00001550 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001551}
1552
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001553/// ParseOptionalDerefAttrBytes
Hal Finkelb0407ba2014-07-18 15:51:28 +00001554/// ::= /* empty */
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001555/// ::= AttrKind '(' 4 ')'
1556///
1557/// where AttrKind is either 'dereferenceable' or 'dereferenceable_or_null'.
1558bool LLParser::ParseOptionalDerefAttrBytes(lltok::Kind AttrKind,
1559 uint64_t &Bytes) {
1560 assert((AttrKind == lltok::kw_dereferenceable ||
1561 AttrKind == lltok::kw_dereferenceable_or_null) &&
1562 "contract!");
1563
Hal Finkelb0407ba2014-07-18 15:51:28 +00001564 Bytes = 0;
Sanjoy Das31ea6d12015-04-16 20:29:50 +00001565 if (!EatIfPresent(AttrKind))
Hal Finkelb0407ba2014-07-18 15:51:28 +00001566 return false;
1567 LocTy ParenLoc = Lex.getLoc();
1568 if (!EatIfPresent(lltok::lparen))
1569 return Error(ParenLoc, "expected '('");
1570 LocTy DerefLoc = Lex.getLoc();
1571 if (ParseUInt64(Bytes)) return true;
1572 ParenLoc = Lex.getLoc();
1573 if (!EatIfPresent(lltok::rparen))
1574 return Error(ParenLoc, "expected ')'");
1575 if (!Bytes)
1576 return Error(DerefLoc, "dereferenceable bytes must be non-zero");
1577 return false;
1578}
1579
Chris Lattnerb2f39502009-12-30 05:44:30 +00001580/// ParseOptionalCommaAlign
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001581/// ::=
Chris Lattnerb2f39502009-12-30 05:44:30 +00001582/// ::= ',' align 4
1583///
1584/// This returns with AteExtraComma set to true if it ate an excess comma at the
1585/// end.
1586bool LLParser::ParseOptionalCommaAlign(unsigned &Alignment,
1587 bool &AteExtraComma) {
1588 AteExtraComma = false;
1589 while (EatIfPresent(lltok::comma)) {
1590 // Metadata at the end is an early exit.
Chris Lattnereafe4de2009-12-30 05:02:06 +00001591 if (Lex.getKind() == lltok::MetadataVar) {
Chris Lattnerb2f39502009-12-30 05:44:30 +00001592 AteExtraComma = true;
1593 return false;
1594 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001595
Chris Lattner95b0ff42010-04-23 00:50:50 +00001596 if (Lex.getKind() != lltok::kw_align)
1597 return Error(Lex.getLoc(), "expected metadata or 'align'");
Duncan Sands4c5c8582010-10-21 16:07:10 +00001598
Chris Lattner95b0ff42010-04-23 00:50:50 +00001599 if (ParseOptionalAlignment(Alignment)) return true;
Chris Lattnerb2f39502009-12-30 05:44:30 +00001600 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001601
Devang Patelea8a4b92009-09-17 23:04:48 +00001602 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001603}
1604
Eli Friedmanfee02c62011-07-25 23:16:38 +00001605/// ParseScopeAndOrdering
1606/// if isAtomic: ::= 'singlethread'? AtomicOrdering
1607/// else: ::=
1608///
1609/// This sets Scope and Ordering to the parsed values.
1610bool LLParser::ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
1611 AtomicOrdering &Ordering) {
1612 if (!isAtomic)
1613 return false;
1614
1615 Scope = CrossThread;
1616 if (EatIfPresent(lltok::kw_singlethread))
1617 Scope = SingleThread;
Tim Northovere94a5182014-03-11 10:48:52 +00001618
1619 return ParseOrdering(Ordering);
1620}
1621
1622/// ParseOrdering
1623/// ::= AtomicOrdering
1624///
1625/// This sets Ordering to the parsed value.
1626bool LLParser::ParseOrdering(AtomicOrdering &Ordering) {
Eli Friedmanfee02c62011-07-25 23:16:38 +00001627 switch (Lex.getKind()) {
1628 default: return TokError("Expected ordering on atomic instruction");
1629 case lltok::kw_unordered: Ordering = Unordered; break;
1630 case lltok::kw_monotonic: Ordering = Monotonic; break;
1631 case lltok::kw_acquire: Ordering = Acquire; break;
1632 case lltok::kw_release: Ordering = Release; break;
1633 case lltok::kw_acq_rel: Ordering = AcquireRelease; break;
1634 case lltok::kw_seq_cst: Ordering = SequentiallyConsistent; break;
1635 }
1636 Lex.Lex();
1637 return false;
1638}
1639
Charles Davisbe5557e2010-02-12 00:31:15 +00001640/// ParseOptionalStackAlignment
1641/// ::= /* empty */
1642/// ::= 'alignstack' '(' 4 ')'
1643bool LLParser::ParseOptionalStackAlignment(unsigned &Alignment) {
1644 Alignment = 0;
1645 if (!EatIfPresent(lltok::kw_alignstack))
1646 return false;
1647 LocTy ParenLoc = Lex.getLoc();
1648 if (!EatIfPresent(lltok::lparen))
1649 return Error(ParenLoc, "expected '('");
1650 LocTy AlignLoc = Lex.getLoc();
1651 if (ParseUInt32(Alignment)) return true;
1652 ParenLoc = Lex.getLoc();
1653 if (!EatIfPresent(lltok::rparen))
1654 return Error(ParenLoc, "expected ')'");
1655 if (!isPowerOf2_32(Alignment))
1656 return Error(AlignLoc, "stack alignment is not a power of two");
1657 return false;
1658}
Devang Patelea8a4b92009-09-17 23:04:48 +00001659
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001660/// ParseIndexList - This parses the index list for an insert/extractvalue
1661/// instruction. This sets AteExtraComma in the case where we eat an extra
1662/// comma at the end of the line and find that it is followed by metadata.
1663/// Clients that don't allow metadata can call the version of this function that
1664/// only takes one argument.
1665///
Chris Lattnerac161bf2009-01-02 07:01:27 +00001666/// ParseIndexList
1667/// ::= (',' uint32)+
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001668///
1669bool LLParser::ParseIndexList(SmallVectorImpl<unsigned> &Indices,
1670 bool &AteExtraComma) {
1671 AteExtraComma = false;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001672
Chris Lattnerac161bf2009-01-02 07:01:27 +00001673 if (Lex.getKind() != lltok::comma)
1674 return TokError("expected ',' as start of index list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001675
Chris Lattner3822f632009-01-02 08:05:26 +00001676 while (EatIfPresent(lltok::comma)) {
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001677 if (Lex.getKind() == lltok::MetadataVar) {
David Majnemer7ccc34d2015-02-16 09:18:13 +00001678 if (Indices.empty()) return TokError("expected index");
Chris Lattner28f1eeb2009-12-30 05:14:00 +00001679 AteExtraComma = true;
1680 return false;
1681 }
Nick Lewycky83e47112010-09-29 23:32:20 +00001682 unsigned Idx = 0;
Chris Lattner3822f632009-01-02 08:05:26 +00001683 if (ParseUInt32(Idx)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001684 Indices.push_back(Idx);
1685 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001686
Chris Lattnerac161bf2009-01-02 07:01:27 +00001687 return false;
1688}
1689
1690//===----------------------------------------------------------------------===//
1691// Type Parsing.
1692//===----------------------------------------------------------------------===//
1693
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001694/// ParseType - Parse a type.
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001695bool LLParser::ParseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001696 SMLoc TypeLoc = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001697 switch (Lex.getKind()) {
1698 default:
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001699 return TokError(Msg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001700 case lltok::Type:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001701 // Type ::= 'float' | 'void' (etc)
Chris Lattnerac161bf2009-01-02 07:01:27 +00001702 Result = Lex.getTyVal();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001703 Lex.Lex();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001704 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001705 case lltok::lbrace:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001706 // Type ::= StructType
1707 if (ParseAnonStructType(Result, false))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001708 return true;
1709 break;
1710 case lltok::lsquare:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001711 // Type ::= '[' ... ']'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001712 Lex.Lex(); // eat the lsquare.
1713 if (ParseArrayVectorType(Result, false))
1714 return true;
1715 break;
1716 case lltok::less: // Either vector or packed struct.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001717 // Type ::= '<' ... '>'
Chris Lattner3822f632009-01-02 08:05:26 +00001718 Lex.Lex();
1719 if (Lex.getKind() == lltok::lbrace) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001720 if (ParseAnonStructType(Result, true) ||
Chris Lattner3822f632009-01-02 08:05:26 +00001721 ParseToken(lltok::greater, "expected '>' at end of packed struct"))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001722 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001723 } else if (ParseArrayVectorType(Result, true))
1724 return true;
1725 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001726 case lltok::LocalVar: {
1727 // Type ::= %foo
1728 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001729
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001730 // If the type hasn't been defined yet, create a forward definition and
1731 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001732 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001733 Entry.first = StructType::create(Context, Lex.getStrVal());
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001734 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001735 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001736 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001737 Lex.Lex();
1738 break;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001739 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001740
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001741 case lltok::LocalVarID: {
1742 // Type ::= %4
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001743 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001744
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001745 // If the type hasn't been defined yet, create a forward definition and
1746 // remember where that forward def'n was seen (in case it never is defined).
Craig Topper2617dcc2014-04-15 06:32:26 +00001747 if (!Entry.first) {
Chris Lattner335d3992011-08-12 18:06:37 +00001748 Entry.first = StructType::create(Context);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001749 Entry.second = Lex.getLoc();
Chris Lattnerac161bf2009-01-02 07:01:27 +00001750 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001751 Result = Entry.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001752 Lex.Lex();
1753 break;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001754 }
1755 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001756
1757 // Parse the type suffixes.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001758 while (1) {
1759 switch (Lex.getKind()) {
1760 // End of type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001761 default:
1762 if (!AllowVoid && Result->isVoidTy())
1763 return Error(TypeLoc, "void type only allowed for function results");
1764 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001765
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001766 // Type ::= Type '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001767 case lltok::star:
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001768 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001769 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001770 if (Result->isVoidTy())
1771 return TokError("pointers to void are invalid - use i8* instead");
1772 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001773 return TokError("pointer to this type is invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001774 Result = PointerType::getUnqual(Result);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001775 Lex.Lex();
1776 break;
1777
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001778 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
Chris Lattnerac161bf2009-01-02 07:01:27 +00001779 case lltok::kw_addrspace: {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001780 if (Result->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00001781 return TokError("basic block pointers are invalid");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001782 if (Result->isVoidTy())
Dan Gohman9280a682009-02-09 17:41:21 +00001783 return TokError("pointers to void are invalid; use i8* instead");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001784 if (!PointerType::isValidElementType(Result))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001785 return TokError("pointer to this type is invalid");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001786 unsigned AddrSpace;
1787 if (ParseOptionalAddrSpace(AddrSpace) ||
1788 ParseToken(lltok::star, "expected '*' in address space"))
1789 return true;
1790
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001791 Result = PointerType::get(Result, AddrSpace);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001792 break;
1793 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001794
Chris Lattnerac161bf2009-01-02 07:01:27 +00001795 /// Types '(' ArgTypeListI ')' OptFuncAttrs
1796 case lltok::lparen:
1797 if (ParseFunctionType(Result))
1798 return true;
1799 break;
1800 }
1801 }
1802}
1803
1804/// ParseParameterList
1805/// ::= '(' ')'
1806/// ::= '(' Arg (',' Arg)* ')'
1807/// Arg
1808/// ::= Type OptionalAttributes Value OptionalAttributes
1809bool LLParser::ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
Reid Kleckner83498642014-08-26 00:33:28 +00001810 PerFunctionState &PFS, bool IsMustTailCall,
1811 bool InVarArgsFunc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001812 if (ParseToken(lltok::lparen, "expected '(' in call"))
1813 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001814
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001815 unsigned AttrIndex = 1;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001816 while (Lex.getKind() != lltok::rparen) {
1817 // If this isn't the first argument, we need a comma.
1818 if (!ArgList.empty() &&
1819 ParseToken(lltok::comma, "expected ',' in argument list"))
1820 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001821
Reid Kleckner83498642014-08-26 00:33:28 +00001822 // Parse an ellipsis if this is a musttail call in a variadic function.
1823 if (Lex.getKind() == lltok::dotdotdot) {
1824 const char *Msg = "unexpected ellipsis in argument list for ";
1825 if (!IsMustTailCall)
1826 return TokError(Twine(Msg) + "non-musttail call");
1827 if (!InVarArgsFunc)
1828 return TokError(Twine(Msg) + "musttail call in non-varargs function");
1829 Lex.Lex(); // Lex the '...', it is purely for readability.
1830 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
1831 }
1832
Chris Lattnerac161bf2009-01-02 07:01:27 +00001833 // Parse the argument.
1834 LocTy ArgLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00001835 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001836 AttrBuilder ArgAttrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001837 Value *V;
Victor Hernandezfa232232009-12-03 23:40:58 +00001838 if (ParseType(ArgTy, ArgLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001839 return true;
Victor Hernandezfa232232009-12-03 23:40:58 +00001840
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00001841 if (ArgTy->isMetadataTy()) {
1842 if (ParseMetadataAsValue(V, PFS))
1843 return true;
1844 } else {
1845 // Otherwise, handle normal operands.
1846 if (ParseOptionalParamAttrs(ArgAttrs) || ParseValue(ArgTy, V, PFS))
1847 return true;
1848 }
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001849 ArgList.push_back(ParamInfo(ArgLoc, V, AttributeSet::get(V->getContext(),
1850 AttrIndex++,
1851 ArgAttrs)));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001852 }
1853
Reid Kleckner83498642014-08-26 00:33:28 +00001854 if (IsMustTailCall && InVarArgsFunc)
1855 return TokError("expected '...' at end of argument list for musttail call "
1856 "in varargs function");
1857
Chris Lattnerac161bf2009-01-02 07:01:27 +00001858 Lex.Lex(); // Lex the ')'.
1859 return false;
1860}
1861
1862
1863
Chris Lattner2ed06b42009-01-05 18:34:07 +00001864/// ParseArgumentList - Parse the argument list for a function type or function
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001865/// prototype.
Chris Lattnerac161bf2009-01-02 07:01:27 +00001866/// ::= '(' ArgTypeListI ')'
1867/// ArgTypeListI
1868/// ::= /*empty*/
1869/// ::= '...'
1870/// ::= ArgTypeList ',' '...'
1871/// ::= ArgType (',' ArgType)*
Chris Lattner2ed06b42009-01-05 18:34:07 +00001872///
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001873bool LLParser::ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
1874 bool &isVarArg){
Chris Lattnerac161bf2009-01-02 07:01:27 +00001875 isVarArg = false;
1876 assert(Lex.getKind() == lltok::lparen);
1877 Lex.Lex(); // eat the (.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001878
Chris Lattnerac161bf2009-01-02 07:01:27 +00001879 if (Lex.getKind() == lltok::rparen) {
1880 // empty
1881 } else if (Lex.getKind() == lltok::dotdotdot) {
1882 isVarArg = true;
1883 Lex.Lex();
1884 } else {
1885 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00001886 Type *ArgTy = nullptr;
Bill Wendling50d27842012-10-15 20:35:56 +00001887 AttrBuilder Attrs;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001888 std::string Name;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001889
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001890 if (ParseType(ArgTy) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00001891 ParseOptionalParamAttrs(Attrs)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001892
Chris Lattnerfdd87902009-10-05 05:54:46 +00001893 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001894 return Error(TypeLoc, "argument can not have void type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001895
Chris Lattnerdef19492011-06-17 06:36:20 +00001896 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001897 Name = Lex.getStrVal();
1898 Lex.Lex();
1899 }
Chris Lattner3822f632009-01-02 08:05:26 +00001900
Nick Lewycky0aa6a742009-06-07 07:26:46 +00001901 if (!FunctionType::isValidArgumentType(ArgTy))
Chris Lattner3822f632009-01-02 08:05:26 +00001902 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001903
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001904 unsigned AttrIndex = 1;
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001905 ArgList.emplace_back(TypeLoc, ArgTy, AttributeSet::get(ArgTy->getContext(),
1906 AttrIndex++, Attrs),
1907 std::move(Name));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001908
Chris Lattner3822f632009-01-02 08:05:26 +00001909 while (EatIfPresent(lltok::comma)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001910 // Handle ... at end of arg list.
Chris Lattner3822f632009-01-02 08:05:26 +00001911 if (EatIfPresent(lltok::dotdotdot)) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001912 isVarArg = true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001913 break;
1914 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001915
Chris Lattnerac161bf2009-01-02 07:01:27 +00001916 // Otherwise must be an argument type.
1917 TypeLoc = Lex.getLoc();
Bill Wendling34c2eb22012-12-04 23:40:58 +00001918 if (ParseType(ArgTy) || ParseOptionalParamAttrs(Attrs)) return true;
Chris Lattner3822f632009-01-02 08:05:26 +00001919
Chris Lattnerfdd87902009-10-05 05:54:46 +00001920 if (ArgTy->isVoidTy())
Chris Lattnerf880ca22009-03-09 04:49:14 +00001921 return Error(TypeLoc, "argument can not have void type");
1922
Chris Lattnerdef19492011-06-17 06:36:20 +00001923 if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001924 Name = Lex.getStrVal();
1925 Lex.Lex();
1926 } else {
1927 Name = "";
1928 }
Chris Lattner3822f632009-01-02 08:05:26 +00001929
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001930 if (!ArgTy->isFirstClassType())
Chris Lattner3822f632009-01-02 08:05:26 +00001931 return Error(TypeLoc, "invalid type for function argument");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001932
Benjamin Kramerf5e2fc42015-05-29 19:43:39 +00001933 ArgList.emplace_back(
1934 TypeLoc, ArgTy,
1935 AttributeSet::get(ArgTy->getContext(), AttrIndex++, Attrs),
1936 std::move(Name));
Chris Lattnerac161bf2009-01-02 07:01:27 +00001937 }
1938 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001939
Chris Lattner3822f632009-01-02 08:05:26 +00001940 return ParseToken(lltok::rparen, "expected ')' at end of argument list");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001941}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001942
Chris Lattnerac161bf2009-01-02 07:01:27 +00001943/// ParseFunctionType
1944/// ::= Type ArgumentList OptionalAttrs
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001945bool LLParser::ParseFunctionType(Type *&Result) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00001946 assert(Lex.getKind() == lltok::lparen);
1947
Chris Lattnerce473c72009-01-05 08:04:33 +00001948 if (!FunctionType::isValidReturnType(Result))
1949 return TokError("invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001950
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001951 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001952 bool isVarArg;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001953 if (ParseArgumentList(ArgList, isVarArg))
Chris Lattnerac161bf2009-01-02 07:01:27 +00001954 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001955
Chris Lattnerac161bf2009-01-02 07:01:27 +00001956 // Reject names on the arguments lists.
1957 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
1958 if (!ArgList[i].Name.empty())
1959 return Error(ArgList[i].Loc, "argument name invalid in function type");
Bill Wendlingfe0021a2013-01-31 00:29:54 +00001960 if (ArgList[i].Attrs.hasAttributes(i + 1))
Chris Lattner6bc5c892011-06-17 17:37:13 +00001961 return Error(ArgList[i].Loc,
1962 "argument attributes invalid in function type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00001963 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001964
Jay Foadb804a2b2011-07-12 14:06:48 +00001965 SmallVector<Type*, 16> ArgListTy;
Chris Lattnerac161bf2009-01-02 07:01:27 +00001966 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001967 ArgListTy.push_back(ArgList[i].Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00001968
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001969 Result = FunctionType::get(Result, ArgListTy, isVarArg);
Chris Lattnerac161bf2009-01-02 07:01:27 +00001970 return false;
1971}
1972
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001973/// ParseAnonStructType - Parse an anonymous struct type, which is inlined into
1974/// other structs.
1975bool LLParser::ParseAnonStructType(Type *&Result, bool Packed) {
1976 SmallVector<Type*, 8> Elts;
1977 if (ParseStructBody(Elts)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001978
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001979 Result = StructType::get(Context, Elts, Packed);
1980 return false;
1981}
1982
1983/// ParseStructDefinition - Parse a struct in a 'type' definition.
1984bool LLParser::ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
1985 std::pair<Type*, LocTy> &Entry,
1986 Type *&ResultTy) {
1987 // If the type was already defined, diagnose the redefinition.
1988 if (Entry.first && !Entry.second.isValid())
1989 return Error(TypeLoc, "redefinition of type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001990
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001991 // If we have opaque, just return without filling in the definition for the
1992 // struct. This counts as a definition as far as the .ll file goes.
1993 if (EatIfPresent(lltok::kw_opaque)) {
1994 // This type is being defined, so clear the location to indicate this.
1995 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00001996
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00001997 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00001998 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00001999 Entry.first = StructType::create(Context, Name);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002000 ResultTy = Entry.first;
2001 return false;
2002 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002003
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002004 // If the type starts with '<', then it is either a packed struct or a vector.
2005 bool isPacked = EatIfPresent(lltok::less);
2006
2007 // If we don't have a struct, then we have a random type alias, which we
2008 // accept for compatibility with old files. These types are not allowed to be
2009 // forward referenced and not allowed to be recursive.
2010 if (Lex.getKind() != lltok::lbrace) {
2011 if (Entry.first)
2012 return Error(TypeLoc, "forward references to non-struct type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002013
Craig Topper2617dcc2014-04-15 06:32:26 +00002014 ResultTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002015 if (isPacked)
2016 return ParseArrayVectorType(ResultTy, true);
2017 return ParseType(ResultTy);
2018 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002019
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002020 // This type is being defined, so clear the location to indicate this.
2021 Entry.second = SMLoc();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002022
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002023 // If this type number has never been uttered, create it.
Craig Topper2617dcc2014-04-15 06:32:26 +00002024 if (!Entry.first)
Chris Lattner335d3992011-08-12 18:06:37 +00002025 Entry.first = StructType::create(Context, Name);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002026
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002027 StructType *STy = cast<StructType>(Entry.first);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002028
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002029 SmallVector<Type*, 8> Body;
2030 if (ParseStructBody(Body) ||
2031 (isPacked && ParseToken(lltok::greater, "expected '>' in packed struct")))
2032 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002033
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002034 STy->setBody(Body, isPacked);
2035 ResultTy = STy;
2036 return false;
2037}
2038
2039
Chris Lattnerac161bf2009-01-02 07:01:27 +00002040/// ParseStructType: Handles packed and unpacked types. </> parsed elsewhere.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002041/// StructType
Chris Lattnerac161bf2009-01-02 07:01:27 +00002042/// ::= '{' '}'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002043/// ::= '{' Type (',' Type)* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00002044/// ::= '<' '{' '}' '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002045/// ::= '<' '{' Type (',' Type)* '}' '>'
2046bool LLParser::ParseStructBody(SmallVectorImpl<Type*> &Body) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002047 assert(Lex.getKind() == lltok::lbrace);
2048 Lex.Lex(); // Consume the '{'
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002049
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002050 // Handle the empty struct.
2051 if (EatIfPresent(lltok::rbrace))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002052 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002053
Chris Lattnerf880ca22009-03-09 04:49:14 +00002054 LocTy EltTyLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002055 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002056 if (ParseType(Ty)) return true;
2057 Body.push_back(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002058
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002059 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002060 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002061
Chris Lattner3822f632009-01-02 08:05:26 +00002062 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf880ca22009-03-09 04:49:14 +00002063 EltTyLoc = Lex.getLoc();
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002064 if (ParseType(Ty)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002065
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002066 if (!StructType::isValidElementType(Ty))
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002067 return Error(EltTyLoc, "invalid element type for struct");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002068
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002069 Body.push_back(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002070 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002071
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002072 return ParseToken(lltok::rbrace, "expected '}' at end of struct");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002073}
2074
2075/// ParseArrayVectorType - Parse an array or vector type, assuming the first
2076/// token has already been consumed.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002077/// Type
Chris Lattnerac161bf2009-01-02 07:01:27 +00002078/// ::= '[' APSINTVAL 'x' Types ']'
2079/// ::= '<' APSINTVAL 'x' Types '>'
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002080bool LLParser::ParseArrayVectorType(Type *&Result, bool isVector) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002081 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
2082 Lex.getAPSIntVal().getBitWidth() > 64)
2083 return TokError("expected number in address space");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002084
Chris Lattnerac161bf2009-01-02 07:01:27 +00002085 LocTy SizeLoc = Lex.getLoc();
2086 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
Chris Lattner3822f632009-01-02 08:05:26 +00002087 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002088
Chris Lattner3822f632009-01-02 08:05:26 +00002089 if (ParseToken(lltok::kw_x, "expected 'x' after element count"))
2090 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002091
2092 LocTy TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00002093 Type *EltTy = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002094 if (ParseType(EltTy)) return true;
Chris Lattnerf880ca22009-03-09 04:49:14 +00002095
Chris Lattner3822f632009-01-02 08:05:26 +00002096 if (ParseToken(isVector ? lltok::greater : lltok::rsquare,
2097 "expected end of sequential type"))
2098 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002099
Chris Lattnerac161bf2009-01-02 07:01:27 +00002100 if (isVector) {
Chris Lattnerbb1fe8a2009-02-28 18:12:41 +00002101 if (Size == 0)
2102 return Error(SizeLoc, "zero element vector is illegal");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002103 if ((unsigned)Size != Size)
2104 return Error(SizeLoc, "size too large for vector");
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002105 if (!VectorType::isValidElementType(EltTy))
Duncan Sandse6beec62012-11-13 12:59:33 +00002106 return Error(TypeLoc, "invalid vector element type");
Owen Anderson4056ca92009-07-29 22:17:13 +00002107 Result = VectorType::get(EltTy, unsigned(Size));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002108 } else {
Nick Lewycky0aa6a742009-06-07 07:26:46 +00002109 if (!ArrayType::isValidElementType(EltTy))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002110 return Error(TypeLoc, "invalid array element type");
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002111 Result = ArrayType::get(EltTy, Size);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002112 }
2113 return false;
2114}
2115
2116//===----------------------------------------------------------------------===//
2117// Function Semantic Analysis.
2118//===----------------------------------------------------------------------===//
2119
Chris Lattner3432c622009-10-28 03:39:23 +00002120LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
2121 int functionNumber)
2122 : P(p), F(f), FunctionNumber(functionNumber) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002123
2124 // Insert unnamed arguments into the NumberedVals list.
2125 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
2126 AI != E; ++AI)
2127 if (!AI->hasName())
2128 NumberedVals.push_back(AI);
2129}
2130
2131LLParser::PerFunctionState::~PerFunctionState() {
2132 // If there were any forward referenced non-basicblock values, delete them.
2133 for (std::map<std::string, std::pair<Value*, LocTy> >::iterator
2134 I = ForwardRefVals.begin(), E = ForwardRefVals.end(); I != E; ++I)
2135 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002136 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002137 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002138 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002139 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002140 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002141
Chris Lattnerac161bf2009-01-02 07:01:27 +00002142 for (std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2143 I = ForwardRefValIDs.begin(), E = ForwardRefValIDs.end(); I != E; ++I)
2144 if (!isa<BasicBlock>(I->second.first)) {
Owen Anderson09063ce2009-07-02 17:04:01 +00002145 I->second.first->replaceAllUsesWith(
Owen Andersonb292b8c2009-07-30 23:03:37 +00002146 UndefValue::get(I->second.first->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002147 delete I->second.first;
Craig Topper2617dcc2014-04-15 06:32:26 +00002148 I->second.first = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002149 }
2150}
2151
Chris Lattner3432c622009-10-28 03:39:23 +00002152bool LLParser::PerFunctionState::FinishFunction() {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002153 if (!ForwardRefVals.empty())
2154 return P.Error(ForwardRefVals.begin()->second.second,
2155 "use of undefined value '%" + ForwardRefVals.begin()->first +
2156 "'");
2157 if (!ForwardRefValIDs.empty())
2158 return P.Error(ForwardRefValIDs.begin()->second.second,
2159 "use of undefined value '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002160 Twine(ForwardRefValIDs.begin()->first) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002161 return false;
2162}
2163
2164
2165/// GetVal - Get a value with the specified name or ID, creating a
2166/// forward reference record if needed. This can return null if the value
2167/// exists but does not have the right type.
2168Value *LLParser::PerFunctionState::GetVal(const std::string &Name,
Chris Lattner229907c2011-07-18 04:54:35 +00002169 Type *Ty, LocTy Loc) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002170 // Look this name up in the normal function symbol table.
2171 Value *Val = F.getValueSymbolTable().lookup(Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002172
Chris Lattnerac161bf2009-01-02 07:01:27 +00002173 // If this is a forward reference for the value, see if we already created a
2174 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002175 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002176 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2177 I = ForwardRefVals.find(Name);
2178 if (I != ForwardRefVals.end())
2179 Val = I->second.first;
2180 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002181
Chris Lattnerac161bf2009-01-02 07:01:27 +00002182 // If we have the value in the symbol table or fwd-ref table, return it.
2183 if (Val) {
2184 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002185 if (Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002186 P.Error(Loc, "'%" + Name + "' is not a basic block");
2187 else
2188 P.Error(Loc, "'%" + Name + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002189 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002190 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002191 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002192
Chris Lattnerac161bf2009-01-02 07:01:27 +00002193 // Don't make placeholders with invalid type.
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002194 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002195 P.Error(Loc, "invalid use of a non-first-class type");
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 // Otherwise, create a new forward reference for this value and remember it.
2200 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002201 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002202 FwdVal = BasicBlock::Create(F.getContext(), Name, &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002203 else
2204 FwdVal = new Argument(Ty, Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002205
Chris Lattnerac161bf2009-01-02 07:01:27 +00002206 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
2207 return FwdVal;
2208}
2209
Chris Lattner229907c2011-07-18 04:54:35 +00002210Value *LLParser::PerFunctionState::GetVal(unsigned ID, Type *Ty,
Chris Lattnerac161bf2009-01-02 07:01:27 +00002211 LocTy Loc) {
2212 // Look this name up in the normal function symbol table.
Craig Topper2617dcc2014-04-15 06:32:26 +00002213 Value *Val = ID < NumberedVals.size() ? NumberedVals[ID] : nullptr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002214
Chris Lattnerac161bf2009-01-02 07:01:27 +00002215 // If this is a forward reference for the value, see if we already created a
2216 // forward ref record.
Craig Topper2617dcc2014-04-15 06:32:26 +00002217 if (!Val) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002218 std::map<unsigned, std::pair<Value*, LocTy> >::iterator
2219 I = ForwardRefValIDs.find(ID);
2220 if (I != ForwardRefValIDs.end())
2221 Val = I->second.first;
2222 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002223
Chris Lattnerac161bf2009-01-02 07:01:27 +00002224 // If we have the value in the symbol table or fwd-ref table, return it.
2225 if (Val) {
2226 if (Val->getType() == Ty) return Val;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002227 if (Ty->isLabelTy())
Benjamin Kramerc7583112010-09-27 17:42:11 +00002228 P.Error(Loc, "'%" + Twine(ID) + "' is not a basic block");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002229 else
Benjamin Kramerc7583112010-09-27 17:42:11 +00002230 P.Error(Loc, "'%" + Twine(ID) + "' defined with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002231 getTypeString(Val->getType()) + "'");
Craig Topper2617dcc2014-04-15 06:32:26 +00002232 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002233 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002234
Duncan P. N. Exon Smith6a6e9cb2014-08-05 18:22:58 +00002235 if (!Ty->isFirstClassType()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002236 P.Error(Loc, "invalid use of a non-first-class type");
Craig Topper2617dcc2014-04-15 06:32:26 +00002237 return nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002238 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002239
Chris Lattnerac161bf2009-01-02 07:01:27 +00002240 // Otherwise, create a new forward reference for this value and remember it.
2241 Value *FwdVal;
Chris Lattnerfdd87902009-10-05 05:54:46 +00002242 if (Ty->isLabelTy())
Owen Anderson55f1c092009-08-13 21:58:54 +00002243 FwdVal = BasicBlock::Create(F.getContext(), "", &F);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002244 else
2245 FwdVal = new Argument(Ty);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002246
Chris Lattnerac161bf2009-01-02 07:01:27 +00002247 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
2248 return FwdVal;
2249}
2250
2251/// SetInstName - After an instruction is parsed and inserted into its
2252/// basic block, this installs its name.
2253bool LLParser::PerFunctionState::SetInstName(int NameID,
2254 const std::string &NameStr,
2255 LocTy NameLoc, Instruction *Inst) {
2256 // If this instruction has void type, it cannot have a name or ID specified.
Chris Lattnerfdd87902009-10-05 05:54:46 +00002257 if (Inst->getType()->isVoidTy()) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002258 if (NameID != -1 || !NameStr.empty())
2259 return P.Error(NameLoc, "instructions returning void cannot have a name");
2260 return false;
2261 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002262
Chris Lattnerac161bf2009-01-02 07:01:27 +00002263 // If this was a numbered instruction, verify that the instruction is the
2264 // expected value and resolve any forward references.
2265 if (NameStr.empty()) {
2266 // If neither a name nor an ID was specified, just use the next ID.
2267 if (NameID == -1)
2268 NameID = NumberedVals.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002269
Chris Lattnerac161bf2009-01-02 07:01:27 +00002270 if (unsigned(NameID) != NumberedVals.size())
2271 return P.Error(NameLoc, "instruction expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00002272 Twine(NumberedVals.size()) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002273
Chris Lattnerac161bf2009-01-02 07:01:27 +00002274 std::map<unsigned, std::pair<Value*, LocTy> >::iterator FI =
2275 ForwardRefValIDs.find(NameID);
2276 if (FI != ForwardRefValIDs.end()) {
2277 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002278 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002279 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002280 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2baa6a32009-09-02 15:02:57 +00002281 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002282 ForwardRefValIDs.erase(FI);
2283 }
2284
2285 NumberedVals.push_back(Inst);
2286 return false;
2287 }
2288
2289 // Otherwise, the instruction had a name. Resolve forward refs and set it.
2290 std::map<std::string, std::pair<Value*, LocTy> >::iterator
2291 FI = ForwardRefVals.find(NameStr);
2292 if (FI != ForwardRefVals.end()) {
2293 if (FI->second.first->getType() != Inst->getType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002294 return P.Error(NameLoc, "instruction forward referenced with type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002295 getTypeString(FI->second.first->getType()) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00002296 FI->second.first->replaceAllUsesWith(Inst);
Nuno Lopes2fcee702009-09-02 14:22:03 +00002297 delete FI->second.first;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002298 ForwardRefVals.erase(FI);
2299 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002300
Chris Lattnerac161bf2009-01-02 07:01:27 +00002301 // Set the name on the instruction.
2302 Inst->setName(NameStr);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002303
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00002304 if (Inst->getName() != NameStr)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002305 return P.Error(NameLoc, "multiple definition of local value named '" +
Chris Lattnerac161bf2009-01-02 07:01:27 +00002306 NameStr + "'");
2307 return false;
2308}
2309
2310/// GetBB - Get a basic block with the specified name or ID, creating a
2311/// forward reference record if needed.
2312BasicBlock *LLParser::PerFunctionState::GetBB(const std::string &Name,
2313 LocTy Loc) {
Owen Anderson576a9a22015-03-02 05:25:09 +00002314 return dyn_cast_or_null<BasicBlock>(GetVal(Name,
2315 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002316}
2317
2318BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
Owen Anderson576a9a22015-03-02 05:25:09 +00002319 return dyn_cast_or_null<BasicBlock>(GetVal(ID,
2320 Type::getLabelTy(F.getContext()), Loc));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002321}
2322
2323/// DefineBB - Define the specified basic block, which is either named or
2324/// unnamed. If there is an error, this returns null otherwise it returns
2325/// the block being defined.
2326BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
2327 LocTy Loc) {
2328 BasicBlock *BB;
2329 if (Name.empty())
2330 BB = GetBB(NumberedVals.size(), Loc);
2331 else
2332 BB = GetBB(Name, Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00002333 if (!BB) return nullptr; // Already diagnosed error.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002334
Chris Lattnerac161bf2009-01-02 07:01:27 +00002335 // Move the block to the end of the function. Forward ref'd blocks are
2336 // inserted wherever they happen to be referenced.
2337 F.getBasicBlockList().splice(F.end(), F.getBasicBlockList(), BB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002338
Chris Lattnerac161bf2009-01-02 07:01:27 +00002339 // Remove the block from forward ref sets.
2340 if (Name.empty()) {
2341 ForwardRefValIDs.erase(NumberedVals.size());
2342 NumberedVals.push_back(BB);
2343 } else {
2344 // BB forward references are already in the function symbol table.
2345 ForwardRefVals.erase(Name);
2346 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002347
Chris Lattnerac161bf2009-01-02 07:01:27 +00002348 return BB;
2349}
2350
2351//===----------------------------------------------------------------------===//
2352// Constants.
2353//===----------------------------------------------------------------------===//
2354
2355/// ParseValID - Parse an abstract value that doesn't necessarily have a
2356/// type implied. For example, if we parse "4" we don't know what integer type
2357/// it has. The value will later be combined with its type and checked for
Victor Hernandezb8fd1522010-01-10 07:14:18 +00002358/// sanity. PFS is used to convert function-local operands of metadata (since
2359/// metadata operands are not just parsed here but also converted to values).
2360/// PFS can be null when we are not parsing metadata values inside a function.
Victor Hernandezdc6e65a2010-01-05 22:22:14 +00002361bool LLParser::ParseValID(ValID &ID, PerFunctionState *PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002362 ID.Loc = Lex.getLoc();
2363 switch (Lex.getKind()) {
2364 default: return TokError("expected value token");
2365 case lltok::GlobalID: // @42
2366 ID.UIntVal = Lex.getUIntVal();
2367 ID.Kind = ValID::t_GlobalID;
2368 break;
2369 case lltok::GlobalVar: // @foo
2370 ID.StrVal = Lex.getStrVal();
2371 ID.Kind = ValID::t_GlobalName;
2372 break;
2373 case lltok::LocalVarID: // %42
2374 ID.UIntVal = Lex.getUIntVal();
2375 ID.Kind = ValID::t_LocalID;
2376 break;
2377 case lltok::LocalVar: // %foo
Chris Lattnerac161bf2009-01-02 07:01:27 +00002378 ID.StrVal = Lex.getStrVal();
2379 ID.Kind = ValID::t_LocalName;
2380 break;
2381 case lltok::APSInt:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002382 ID.APSIntVal = Lex.getAPSIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00002383 ID.Kind = ValID::t_APSInt;
2384 break;
2385 case lltok::APFloat:
2386 ID.APFloatVal = Lex.getAPFloatVal();
2387 ID.Kind = ValID::t_APFloat;
2388 break;
2389 case lltok::kw_true:
Owen Anderson23a204d2009-07-31 17:39:07 +00002390 ID.ConstantVal = ConstantInt::getTrue(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002391 ID.Kind = ValID::t_Constant;
2392 break;
2393 case lltok::kw_false:
Owen Anderson23a204d2009-07-31 17:39:07 +00002394 ID.ConstantVal = ConstantInt::getFalse(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002395 ID.Kind = ValID::t_Constant;
2396 break;
2397 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
2398 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
2399 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002400
Chris Lattnerac161bf2009-01-02 07:01:27 +00002401 case lltok::lbrace: {
2402 // ValID ::= '{' ConstVector '}'
2403 Lex.Lex();
2404 SmallVector<Constant*, 16> Elts;
2405 if (ParseGlobalValueVector(Elts) ||
2406 ParseToken(lltok::rbrace, "expected end of struct constant"))
2407 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002408
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002409 ID.ConstantStructElts = new Constant*[Elts.size()];
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002410 ID.UIntVal = Elts.size();
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002411 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002412 ID.Kind = ValID::t_ConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002413 return false;
2414 }
2415 case lltok::less: {
2416 // ValID ::= '<' ConstVector '>' --> Vector.
2417 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
2418 Lex.Lex();
Chris Lattner3822f632009-01-02 08:05:26 +00002419 bool isPackedStruct = EatIfPresent(lltok::lbrace);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002420
Chris Lattnerac161bf2009-01-02 07:01:27 +00002421 SmallVector<Constant*, 16> Elts;
2422 LocTy FirstEltLoc = Lex.getLoc();
2423 if (ParseGlobalValueVector(Elts) ||
2424 (isPackedStruct &&
2425 ParseToken(lltok::rbrace, "expected end of packed struct")) ||
2426 ParseToken(lltok::greater, "expected end of constant"))
2427 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002428
Chris Lattnerac161bf2009-01-02 07:01:27 +00002429 if (isPackedStruct) {
Reid Kleckner2ae03e12015-03-04 18:31:10 +00002430 ID.ConstantStructElts = new Constant*[Elts.size()];
2431 memcpy(ID.ConstantStructElts, Elts.data(), Elts.size()*sizeof(Elts[0]));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002432 ID.UIntVal = Elts.size();
2433 ID.Kind = ValID::t_PackedConstantStruct;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002434 return false;
2435 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002436
Chris Lattnerac161bf2009-01-02 07:01:27 +00002437 if (Elts.empty())
2438 return Error(ID.Loc, "constant vector must not be empty");
2439
Duncan Sands9dff9be2010-02-15 16:12:20 +00002440 if (!Elts[0]->getType()->isIntegerTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002441 !Elts[0]->getType()->isFloatingPointTy() &&
2442 !Elts[0]->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002443 return Error(FirstEltLoc,
Nadav Rotem3924cb02011-12-05 06:29:09 +00002444 "vector elements must have integer, pointer or floating point type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002445
Chris Lattnerac161bf2009-01-02 07:01:27 +00002446 // Verify that all the vector elements have the same type.
2447 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
2448 if (Elts[i]->getType() != Elts[0]->getType())
2449 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002450 "vector element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002451 " is not of type '" + getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002452
Chris Lattner69229312011-02-15 00:14:00 +00002453 ID.ConstantVal = ConstantVector::get(Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002454 ID.Kind = ValID::t_Constant;
2455 return false;
2456 }
2457 case lltok::lsquare: { // Array Constant
2458 Lex.Lex();
2459 SmallVector<Constant*, 16> Elts;
2460 LocTy FirstEltLoc = Lex.getLoc();
2461 if (ParseGlobalValueVector(Elts) ||
2462 ParseToken(lltok::rsquare, "expected end of array constant"))
2463 return true;
2464
2465 // Handle empty element.
2466 if (Elts.empty()) {
2467 // Use undef instead of an array because it's inconvenient to determine
2468 // the element type at this point, there being no elements to examine.
Chris Lattner998fa0a2009-01-05 07:52:51 +00002469 ID.Kind = ValID::t_EmptyArray;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002470 return false;
2471 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002472
Chris Lattnerac161bf2009-01-02 07:01:27 +00002473 if (!Elts[0]->getType()->isFirstClassType())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002474 return Error(FirstEltLoc, "invalid array element type: " +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002475 getTypeString(Elts[0]->getType()));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002476
Owen Anderson4056ca92009-07-29 22:17:13 +00002477 ArrayType *ATy = ArrayType::get(Elts[0]->getType(), Elts.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002478
Chris Lattnerac161bf2009-01-02 07:01:27 +00002479 // Verify all elements are correct type!
Chris Lattner59d0e3b2009-01-02 08:49:06 +00002480 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002481 if (Elts[i]->getType() != Elts[0]->getType())
2482 return Error(FirstEltLoc,
Benjamin Kramerc7583112010-09-27 17:42:11 +00002483 "array element #" + Twine(i) +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002484 " is not of type '" + getTypeString(Elts[0]->getType()));
Chris Lattnerac161bf2009-01-02 07:01:27 +00002485 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002486
Jay Foad83be3612011-06-22 09:24:39 +00002487 ID.ConstantVal = ConstantArray::get(ATy, Elts);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002488 ID.Kind = ValID::t_Constant;
2489 return false;
2490 }
2491 case lltok::kw_c: // c "foo"
2492 Lex.Lex();
Chris Lattnercf9e8f62012-02-05 02:29:43 +00002493 ID.ConstantVal = ConstantDataArray::getString(Context, Lex.getStrVal(),
2494 false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002495 if (ParseToken(lltok::StringConstant, "expected string")) return true;
2496 ID.Kind = ValID::t_Constant;
2497 return false;
2498
2499 case lltok::kw_asm: {
Chad Rosier6f9c3852013-02-14 20:44:07 +00002500 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
2501 // STRINGCONSTANT
Chad Rosierd8c76102012-09-05 19:00:49 +00002502 bool HasSideEffect, AlignStack, AsmDialect;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002503 Lex.Lex();
2504 if (ParseOptionalToken(lltok::kw_sideeffect, HasSideEffect) ||
Dale Johannesen1cfb9582009-10-21 23:28:00 +00002505 ParseOptionalToken(lltok::kw_alignstack, AlignStack) ||
Chad Rosierd8c76102012-09-05 19:00:49 +00002506 ParseOptionalToken(lltok::kw_inteldialect, AsmDialect) ||
Chris Lattner3822f632009-01-02 08:05:26 +00002507 ParseStringConstant(ID.StrVal) ||
2508 ParseToken(lltok::comma, "expected comma in inline asm expression") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002509 ParseToken(lltok::StringConstant, "expected constraint string"))
2510 return true;
2511 ID.StrVal2 = Lex.getStrVal();
Chad Rosierf42fad62012-09-05 00:08:17 +00002512 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack)<<1) |
Chad Rosierd8c76102012-09-05 19:00:49 +00002513 (unsigned(AsmDialect)<<2);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002514 ID.Kind = ValID::t_InlineAsm;
2515 return false;
2516 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002517
Chris Lattner3432c622009-10-28 03:39:23 +00002518 case lltok::kw_blockaddress: {
2519 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
2520 Lex.Lex();
2521
2522 ValID Fn, Label;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002523
Chris Lattner3432c622009-10-28 03:39:23 +00002524 if (ParseToken(lltok::lparen, "expected '(' in block address expression") ||
2525 ParseValID(Fn) ||
2526 ParseToken(lltok::comma, "expected comma in block address expression")||
2527 ParseValID(Label) ||
2528 ParseToken(lltok::rparen, "expected ')' in block address expression"))
2529 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002530
Chris Lattner3432c622009-10-28 03:39:23 +00002531 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
2532 return Error(Fn.Loc, "expected function name in blockaddress");
Chris Lattneraa99c942009-11-01 01:27:45 +00002533 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
Chris Lattner3432c622009-10-28 03:39:23 +00002534 return Error(Label.Loc, "expected basic block name in blockaddress");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002535
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002536 // Try to find the function (but skip it if it's forward-referenced).
2537 GlobalValue *GV = nullptr;
2538 if (Fn.Kind == ValID::t_GlobalID) {
2539 if (Fn.UIntVal < NumberedVals.size())
2540 GV = NumberedVals[Fn.UIntVal];
2541 } else if (!ForwardRefVals.count(Fn.StrVal)) {
2542 GV = M->getNamedValue(Fn.StrVal);
2543 }
2544 Function *F = nullptr;
2545 if (GV) {
2546 // Confirm that it's actually a function with a definition.
2547 if (!isa<Function>(GV))
2548 return Error(Fn.Loc, "expected function name in blockaddress");
2549 F = cast<Function>(GV);
2550 if (F->isDeclaration())
2551 return Error(Fn.Loc, "cannot take blockaddress inside a declaration");
2552 }
2553
2554 if (!F) {
2555 // Make a global variable as a placeholder for this reference.
David Blaikieb9cc6592015-03-04 01:40:07 +00002556 GlobalValue *&FwdRef =
2557 ForwardRefBlockAddresses.insert(std::make_pair(
2558 std::move(Fn),
2559 std::map<ValID, GlobalValue *>()))
2560 .first->second.insert(std::make_pair(std::move(Label), nullptr))
2561 .first->second;
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002562 if (!FwdRef)
2563 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(Context), false,
2564 GlobalValue::InternalLinkage, nullptr, "");
2565 ID.ConstantVal = FwdRef;
2566 ID.Kind = ValID::t_Constant;
2567 return false;
2568 }
2569
2570 // We found the function; now find the basic block. Don't use PFS, since we
2571 // might be inside a constant expression.
2572 BasicBlock *BB;
2573 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
2574 if (Label.Kind == ValID::t_LocalID)
2575 BB = BlockAddressPFS->GetBB(Label.UIntVal, Label.Loc);
2576 else
2577 BB = BlockAddressPFS->GetBB(Label.StrVal, Label.Loc);
2578 if (!BB)
2579 return Error(Label.Loc, "referenced value is not a basic block");
2580 } else {
2581 if (Label.Kind == ValID::t_LocalID)
2582 return Error(Label.Loc, "cannot take address of numeric label after "
2583 "the function is defined");
2584 BB = dyn_cast_or_null<BasicBlock>(
2585 F->getValueSymbolTable().lookup(Label.StrVal));
2586 if (!BB)
2587 return Error(Label.Loc, "referenced value is not a basic block");
2588 }
2589
2590 ID.ConstantVal = BlockAddress::get(F, BB);
Chris Lattner3432c622009-10-28 03:39:23 +00002591 ID.Kind = ValID::t_Constant;
2592 return false;
2593 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00002594
Chris Lattnerac161bf2009-01-02 07:01:27 +00002595 case lltok::kw_trunc:
2596 case lltok::kw_zext:
2597 case lltok::kw_sext:
2598 case lltok::kw_fptrunc:
2599 case lltok::kw_fpext:
2600 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00002601 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002602 case lltok::kw_uitofp:
2603 case lltok::kw_sitofp:
2604 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002605 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002606 case lltok::kw_inttoptr:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002607 case lltok::kw_ptrtoint: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002608 unsigned Opc = Lex.getUIntVal();
Craig Topper2617dcc2014-04-15 06:32:26 +00002609 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002610 Constant *SrcVal;
2611 Lex.Lex();
2612 if (ParseToken(lltok::lparen, "expected '(' after constantexpr cast") ||
2613 ParseGlobalTypeAndValue(SrcVal) ||
Dan Gohman0b5d0422009-06-15 21:52:11 +00002614 ParseToken(lltok::kw_to, "expected 'to' in constantexpr cast") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002615 ParseType(DestTy) ||
2616 ParseToken(lltok::rparen, "expected ')' at end of constantexpr cast"))
2617 return true;
2618 if (!CastInst::castIsValid((Instruction::CastOps)Opc, SrcVal, DestTy))
2619 return Error(ID.Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00002620 getTypeString(SrcVal->getType()) + "' to '" +
2621 getTypeString(DestTy) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002622 ID.ConstantVal = ConstantExpr::getCast((Instruction::CastOps)Opc,
Owen Anderson02a9da32009-07-01 23:57:11 +00002623 SrcVal, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002624 ID.Kind = ValID::t_Constant;
2625 return false;
2626 }
2627 case lltok::kw_extractvalue: {
2628 Lex.Lex();
2629 Constant *Val;
2630 SmallVector<unsigned, 4> Indices;
2631 if (ParseToken(lltok::lparen, "expected '(' in extractvalue constantexpr")||
2632 ParseGlobalTypeAndValue(Val) ||
2633 ParseIndexList(Indices) ||
2634 ParseToken(lltok::rparen, "expected ')' in extractvalue constantexpr"))
2635 return true;
Devang Patel1cb51162009-11-03 19:06:07 +00002636
Chris Lattner392be582010-02-12 20:49:41 +00002637 if (!Val->getType()->isAggregateType())
2638 return Error(ID.Loc, "extractvalue operand must be aggregate type");
Jay Foad57aa6362011-07-13 10:26:04 +00002639 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00002640 return Error(ID.Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00002641 ID.ConstantVal = ConstantExpr::getExtractValue(Val, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002642 ID.Kind = ValID::t_Constant;
2643 return false;
2644 }
2645 case lltok::kw_insertvalue: {
2646 Lex.Lex();
2647 Constant *Val0, *Val1;
2648 SmallVector<unsigned, 4> Indices;
2649 if (ParseToken(lltok::lparen, "expected '(' in insertvalue constantexpr")||
2650 ParseGlobalTypeAndValue(Val0) ||
2651 ParseToken(lltok::comma, "expected comma in insertvalue constantexpr")||
2652 ParseGlobalTypeAndValue(Val1) ||
2653 ParseIndexList(Indices) ||
2654 ParseToken(lltok::rparen, "expected ')' in insertvalue constantexpr"))
2655 return true;
Chris Lattner392be582010-02-12 20:49:41 +00002656 if (!Val0->getType()->isAggregateType())
2657 return Error(ID.Loc, "insertvalue operand must be aggregate type");
David Majnemereba692d2015-02-23 07:13:52 +00002658 Type *IndexedType =
2659 ExtractValueInst::getIndexedType(Val0->getType(), Indices);
2660 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00002661 return Error(ID.Loc, "invalid indices for insertvalue");
David Majnemereba692d2015-02-23 07:13:52 +00002662 if (IndexedType != Val1->getType())
2663 return Error(ID.Loc, "insertvalue operand and field disagree in type: '" +
2664 getTypeString(Val1->getType()) +
2665 "' instead of '" + getTypeString(IndexedType) +
2666 "'");
Jay Foad57aa6362011-07-13 10:26:04 +00002667 ID.ConstantVal = ConstantExpr::getInsertValue(Val0, Val1, Indices);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002668 ID.Kind = ValID::t_Constant;
2669 return false;
2670 }
2671 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002672 case lltok::kw_fcmp: {
Chris Lattnerac161bf2009-01-02 07:01:27 +00002673 unsigned PredVal, Opc = Lex.getUIntVal();
2674 Constant *Val0, *Val1;
2675 Lex.Lex();
2676 if (ParseCmpPredicate(PredVal, Opc) ||
2677 ParseToken(lltok::lparen, "expected '(' in compare constantexpr") ||
2678 ParseGlobalTypeAndValue(Val0) ||
2679 ParseToken(lltok::comma, "expected comma in compare constantexpr") ||
2680 ParseGlobalTypeAndValue(Val1) ||
2681 ParseToken(lltok::rparen, "expected ')' in compare constantexpr"))
2682 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002683
Chris Lattnerac161bf2009-01-02 07:01:27 +00002684 if (Val0->getType() != Val1->getType())
2685 return Error(ID.Loc, "compare operands must have the same type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002686
Chris Lattnerac161bf2009-01-02 07:01:27 +00002687 CmpInst::Predicate Pred = (CmpInst::Predicate)PredVal;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002688
Chris Lattnerac161bf2009-01-02 07:01:27 +00002689 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00002690 if (!Val0->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002691 return Error(ID.Loc, "fcmp requires floating point operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002692 ID.ConstantVal = ConstantExpr::getFCmp(Pred, Val0, Val1);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00002693 } else {
2694 assert(Opc == Instruction::ICmp && "Unexpected opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002695 if (!Val0->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00002696 !Val0->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002697 return Error(ID.Loc, "icmp requires pointer or integer operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002698 ID.ConstantVal = ConstantExpr::getICmp(Pred, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002699 }
2700 ID.Kind = ValID::t_Constant;
2701 return false;
2702 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002703
Chris Lattnerac161bf2009-01-02 07:01:27 +00002704 // Binary Operators.
2705 case lltok::kw_add:
Dan Gohmana5b96452009-06-04 22:49:04 +00002706 case lltok::kw_fadd:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002707 case lltok::kw_sub:
Dan Gohmana5b96452009-06-04 22:49:04 +00002708 case lltok::kw_fsub:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002709 case lltok::kw_mul:
Dan Gohmana5b96452009-06-04 22:49:04 +00002710 case lltok::kw_fmul:
Chris Lattnerac161bf2009-01-02 07:01:27 +00002711 case lltok::kw_udiv:
2712 case lltok::kw_sdiv:
2713 case lltok::kw_fdiv:
2714 case lltok::kw_urem:
2715 case lltok::kw_srem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002716 case lltok::kw_frem:
2717 case lltok::kw_shl:
2718 case lltok::kw_lshr:
2719 case lltok::kw_ashr: {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002720 bool NUW = false;
2721 bool NSW = false;
2722 bool Exact = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002723 unsigned Opc = Lex.getUIntVal();
2724 Constant *Val0, *Val1;
2725 Lex.Lex();
Dan Gohman9c7f8082009-07-27 16:11:46 +00002726 LocTy ModifierLoc = Lex.getLoc();
Chris Lattnera676c0f2011-02-07 16:40:21 +00002727 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
2728 Opc == Instruction::Mul || Opc == Instruction::Shl) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002729 if (EatIfPresent(lltok::kw_nuw))
2730 NUW = true;
2731 if (EatIfPresent(lltok::kw_nsw)) {
2732 NSW = true;
2733 if (EatIfPresent(lltok::kw_nuw))
2734 NUW = true;
2735 }
Chris Lattnera676c0f2011-02-07 16:40:21 +00002736 } else if (Opc == Instruction::SDiv || Opc == Instruction::UDiv ||
2737 Opc == Instruction::LShr || Opc == Instruction::AShr) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002738 if (EatIfPresent(lltok::kw_exact))
2739 Exact = true;
2740 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00002741 if (ParseToken(lltok::lparen, "expected '(' in binary constantexpr") ||
2742 ParseGlobalTypeAndValue(Val0) ||
2743 ParseToken(lltok::comma, "expected comma in binary constantexpr") ||
2744 ParseGlobalTypeAndValue(Val1) ||
2745 ParseToken(lltok::rparen, "expected ')' in binary constantexpr"))
2746 return true;
2747 if (Val0->getType() != Val1->getType())
2748 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002749 if (!Val0->getType()->isIntOrIntVectorTy()) {
Dan Gohman9c7f8082009-07-27 16:11:46 +00002750 if (NUW)
2751 return Error(ModifierLoc, "nuw only applies to integer operations");
2752 if (NSW)
2753 return Error(ModifierLoc, "nsw only applies to integer operations");
2754 }
Dan Gohmana2414ea2010-05-03 22:44:19 +00002755 // Check that the type is valid for the operator.
2756 switch (Opc) {
2757 case Instruction::Add:
2758 case Instruction::Sub:
2759 case Instruction::Mul:
2760 case Instruction::UDiv:
2761 case Instruction::SDiv:
2762 case Instruction::URem:
2763 case Instruction::SRem:
Chris Lattnera676c0f2011-02-07 16:40:21 +00002764 case Instruction::Shl:
2765 case Instruction::AShr:
2766 case Instruction::LShr:
Dan Gohmana2414ea2010-05-03 22:44:19 +00002767 if (!Val0->getType()->isIntOrIntVectorTy())
2768 return Error(ID.Loc, "constexpr requires integer operands");
2769 break;
2770 case Instruction::FAdd:
2771 case Instruction::FSub:
2772 case Instruction::FMul:
2773 case Instruction::FDiv:
2774 case Instruction::FRem:
2775 if (!Val0->getType()->isFPOrFPVectorTy())
2776 return Error(ID.Loc, "constexpr requires fp operands");
2777 break;
2778 default: llvm_unreachable("Unknown binary operator!");
2779 }
Dan Gohman1b849082009-09-07 23:54:19 +00002780 unsigned Flags = 0;
2781 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
2782 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
Chris Lattner35315d02011-02-06 21:44:57 +00002783 if (Exact) Flags |= PossiblyExactOperator::IsExact;
Dan Gohman1b849082009-09-07 23:54:19 +00002784 Constant *C = ConstantExpr::get(Opc, Val0, Val1, Flags);
Dan Gohman9c7f8082009-07-27 16:11:46 +00002785 ID.ConstantVal = C;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002786 ID.Kind = ValID::t_Constant;
2787 return false;
2788 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002789
Chris Lattnerac161bf2009-01-02 07:01:27 +00002790 // Logical Operations
Chris Lattnerac161bf2009-01-02 07:01:27 +00002791 case lltok::kw_and:
2792 case lltok::kw_or:
2793 case lltok::kw_xor: {
2794 unsigned Opc = Lex.getUIntVal();
2795 Constant *Val0, *Val1;
2796 Lex.Lex();
2797 if (ParseToken(lltok::lparen, "expected '(' in logical constantexpr") ||
2798 ParseGlobalTypeAndValue(Val0) ||
2799 ParseToken(lltok::comma, "expected comma in logical constantexpr") ||
2800 ParseGlobalTypeAndValue(Val1) ||
2801 ParseToken(lltok::rparen, "expected ')' in logical constantexpr"))
2802 return true;
2803 if (Val0->getType() != Val1->getType())
2804 return Error(ID.Loc, "operands of constexpr must have same type");
Duncan Sands9dff9be2010-02-15 16:12:20 +00002805 if (!Val0->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00002806 return Error(ID.Loc,
2807 "constexpr requires integer or integer vector operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002808 ID.ConstantVal = ConstantExpr::get(Opc, Val0, Val1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002809 ID.Kind = ValID::t_Constant;
2810 return false;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002811 }
2812
Chris Lattnerac161bf2009-01-02 07:01:27 +00002813 case lltok::kw_getelementptr:
2814 case lltok::kw_shufflevector:
2815 case lltok::kw_insertelement:
2816 case lltok::kw_extractelement:
2817 case lltok::kw_select: {
2818 unsigned Opc = Lex.getUIntVal();
2819 SmallVector<Constant*, 16> Elts;
Dan Gohman1639c392009-07-27 21:53:46 +00002820 bool InBounds = false;
David Blaikief72d05b2015-03-13 18:20:45 +00002821 Type *Ty;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002822 Lex.Lex();
David Blaikief72d05b2015-03-13 18:20:45 +00002823
Dan Gohman1639c392009-07-27 21:53:46 +00002824 if (Opc == Instruction::GetElementPtr)
Dan Gohman16cbbe42009-07-29 15:58:36 +00002825 InBounds = EatIfPresent(lltok::kw_inbounds);
David Blaikief72d05b2015-03-13 18:20:45 +00002826
2827 if (ParseToken(lltok::lparen, "expected '(' in constantexpr"))
2828 return true;
2829
2830 LocTy ExplicitTypeLoc = Lex.getLoc();
2831 if (Opc == Instruction::GetElementPtr) {
2832 if (ParseType(Ty) ||
2833 ParseToken(lltok::comma, "expected comma after getelementptr's type"))
2834 return true;
2835 }
2836
2837 if (ParseGlobalValueVector(Elts) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00002838 ParseToken(lltok::rparen, "expected ')' in constantexpr"))
2839 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002840
Chris Lattnerac161bf2009-01-02 07:01:27 +00002841 if (Opc == Instruction::GetElementPtr) {
Nadav Rotem3924cb02011-12-05 06:29:09 +00002842 if (Elts.size() == 0 ||
2843 !Elts[0]->getType()->getScalarType()->isPointerTy())
David Majnemer00303b62015-02-22 23:14:52 +00002844 return Error(ID.Loc, "base of getelementptr must be a pointer");
2845
2846 Type *BaseType = Elts[0]->getType();
2847 auto *BasePointerType = cast<PointerType>(BaseType->getScalarType());
David Blaikief72d05b2015-03-13 18:20:45 +00002848 if (Ty != BasePointerType->getElementType())
2849 return Error(
2850 ExplicitTypeLoc,
2851 "explicit pointee type doesn't match operand's pointee type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002852
Jay Foaded8db7d2011-07-21 14:31:17 +00002853 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
David Majnemer00303b62015-02-22 23:14:52 +00002854 for (Constant *Val : Indices) {
2855 Type *ValTy = Val->getType();
2856 if (!ValTy->getScalarType()->isIntegerTy())
2857 return Error(ID.Loc, "getelementptr index must be an integer");
2858 if (ValTy->isVectorTy() != BaseType->isVectorTy())
2859 return Error(ID.Loc, "getelementptr index type missmatch");
2860 if (ValTy->isVectorTy()) {
2861 unsigned ValNumEl = cast<VectorType>(ValTy)->getNumElements();
2862 unsigned PtrNumEl = cast<VectorType>(BaseType)->getNumElements();
2863 if (ValNumEl != PtrNumEl)
2864 return Error(
2865 ID.Loc,
2866 "getelementptr vector index has a wrong number of elements");
2867 }
2868 }
2869
Owen Andersone90f9922015-03-10 06:34:57 +00002870 SmallPtrSet<const Type*, 4> Visited;
David Blaikiee169e822015-04-22 16:37:35 +00002871 if (!Indices.empty() && !Ty->isSized(&Visited))
David Majnemer00303b62015-02-22 23:14:52 +00002872 return Error(ID.Loc, "base element of getelementptr must be sized");
2873
David Blaikie4a2e73b2015-04-02 18:55:32 +00002874 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
David Majnemer00303b62015-02-22 23:14:52 +00002875 return Error(ID.Loc, "invalid getelementptr indices");
David Blaikie4a2e73b2015-04-02 18:55:32 +00002876 ID.ConstantVal =
2877 ConstantExpr::getGetElementPtr(Ty, Elts[0], Indices, InBounds);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002878 } else if (Opc == Instruction::Select) {
2879 if (Elts.size() != 3)
2880 return Error(ID.Loc, "expected three operands to select");
2881 if (const char *Reason = SelectInst::areInvalidOperands(Elts[0], Elts[1],
2882 Elts[2]))
2883 return Error(ID.Loc, Reason);
Owen Anderson487375e2009-07-29 18:55:55 +00002884 ID.ConstantVal = ConstantExpr::getSelect(Elts[0], Elts[1], Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002885 } else if (Opc == Instruction::ShuffleVector) {
2886 if (Elts.size() != 3)
2887 return Error(ID.Loc, "expected three operands to shufflevector");
2888 if (!ShuffleVectorInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2889 return Error(ID.Loc, "invalid operands to shufflevector");
Owen Anderson02a9da32009-07-01 23:57:11 +00002890 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002891 ConstantExpr::getShuffleVector(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002892 } else if (Opc == Instruction::ExtractElement) {
2893 if (Elts.size() != 2)
2894 return Error(ID.Loc, "expected two operands to extractelement");
2895 if (!ExtractElementInst::isValidOperands(Elts[0], Elts[1]))
2896 return Error(ID.Loc, "invalid extractelement operands");
Owen Anderson487375e2009-07-29 18:55:55 +00002897 ID.ConstantVal = ConstantExpr::getExtractElement(Elts[0], Elts[1]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002898 } else {
2899 assert(Opc == Instruction::InsertElement && "Unknown opcode");
2900 if (Elts.size() != 3)
2901 return Error(ID.Loc, "expected three operands to insertelement");
2902 if (!InsertElementInst::isValidOperands(Elts[0], Elts[1], Elts[2]))
2903 return Error(ID.Loc, "invalid insertelement operands");
Owen Anderson02a9da32009-07-01 23:57:11 +00002904 ID.ConstantVal =
Owen Anderson487375e2009-07-29 18:55:55 +00002905 ConstantExpr::getInsertElement(Elts[0], Elts[1],Elts[2]);
Chris Lattnerac161bf2009-01-02 07:01:27 +00002906 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002907
Chris Lattnerac161bf2009-01-02 07:01:27 +00002908 ID.Kind = ValID::t_Constant;
2909 return false;
2910 }
2911 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00002912
Chris Lattnerac161bf2009-01-02 07:01:27 +00002913 Lex.Lex();
2914 return false;
2915}
2916
2917/// ParseGlobalValue - Parse a global value with the specified type.
Chris Lattner229907c2011-07-18 04:54:35 +00002918bool LLParser::ParseGlobalValue(Type *Ty, Constant *&C) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002919 C = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002920 ValID ID;
Craig Topper2617dcc2014-04-15 06:32:26 +00002921 Value *V = nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00002922 bool Parsed = ParseValID(ID) ||
Craig Topper2617dcc2014-04-15 06:32:26 +00002923 ConvertValIDToValue(Ty, ID, V, nullptr);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002924 if (V && !(C = dyn_cast<Constant>(V)))
2925 return Error(ID.Loc, "global values must be constants");
2926 return Parsed;
Chris Lattnerac161bf2009-01-02 07:01:27 +00002927}
2928
Victor Hernandez9d75c962010-01-11 22:31:58 +00002929bool LLParser::ParseGlobalTypeAndValue(Constant *&V) {
Craig Topper2617dcc2014-04-15 06:32:26 +00002930 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00002931 return ParseType(Ty) ||
2932 ParseGlobalValue(Ty, V);
Victor Hernandez9d75c962010-01-11 22:31:58 +00002933}
2934
Rafael Espindola83a362c2015-01-06 22:55:16 +00002935bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
David Majnemerdad0a642014-06-27 18:19:56 +00002936 C = nullptr;
Rafael Espindola83a362c2015-01-06 22:55:16 +00002937
2938 LocTy KwLoc = Lex.getLoc();
David Majnemerdad0a642014-06-27 18:19:56 +00002939 if (!EatIfPresent(lltok::kw_comdat))
2940 return false;
Rafael Espindola83a362c2015-01-06 22:55:16 +00002941
2942 if (EatIfPresent(lltok::lparen)) {
2943 if (Lex.getKind() != lltok::ComdatVar)
2944 return TokError("expected comdat variable");
2945 C = getComdat(Lex.getStrVal(), Lex.getLoc());
2946 Lex.Lex();
2947 if (ParseToken(lltok::rparen, "expected ')' after comdat var"))
2948 return true;
2949 } else {
2950 if (GlobalName.empty())
2951 return TokError("comdat cannot be unnamed");
2952 C = getComdat(GlobalName, KwLoc);
2953 }
2954
David Majnemerdad0a642014-06-27 18:19:56 +00002955 return false;
2956}
2957
Victor Hernandez9d75c962010-01-11 22:31:58 +00002958/// ParseGlobalValueVector
2959/// ::= /*empty*/
2960/// ::= TypeAndValue (',' TypeAndValue)*
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00002961bool LLParser::ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
Victor Hernandez9d75c962010-01-11 22:31:58 +00002962 // Empty list.
2963 if (Lex.getKind() == lltok::rbrace ||
2964 Lex.getKind() == lltok::rsquare ||
2965 Lex.getKind() == lltok::greater ||
2966 Lex.getKind() == lltok::rparen)
2967 return false;
2968
2969 Constant *C;
2970 if (ParseGlobalTypeAndValue(C)) return true;
2971 Elts.push_back(C);
2972
2973 while (EatIfPresent(lltok::comma)) {
2974 if (ParseGlobalTypeAndValue(C)) return true;
2975 Elts.push_back(C);
2976 }
2977
2978 return false;
2979}
2980
Duncan P. N. Exon Smith58ef9d12015-01-12 21:23:11 +00002981bool LLParser::ParseMDTuple(MDNode *&MD, bool IsDistinct) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00002982 SmallVector<Metadata *, 16> Elts;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00002983 if (ParseMDNodeVector(Elts))
Dan Gohmanc828c542010-08-24 02:24:03 +00002984 return true;
2985
Duncan P. N. Exon Smith0b31dd12015-01-12 22:27:39 +00002986 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
Dan Gohmanc828c542010-08-24 02:24:03 +00002987 return false;
2988}
2989
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00002990/// MDNode:
2991/// ::= !{ ... }
2992/// ::= !7
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002993/// ::= !DILocation(...)
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00002994bool LLParser::ParseMDNode(MDNode *&N) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00002995 if (Lex.getKind() == lltok::MetadataVar)
2996 return ParseSpecializedMDNode(N);
2997
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00002998 return ParseToken(lltok::exclaim, "expected '!' here") ||
2999 ParseMDNodeTail(N);
3000}
3001
3002bool LLParser::ParseMDNodeTail(MDNode *&N) {
3003 // !{ ... }
3004 if (Lex.getKind() == lltok::lbrace)
3005 return ParseMDTuple(N);
3006
3007 // !42
3008 return ParseMDNodeID(N);
3009}
3010
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003011namespace {
3012
3013/// Structure to represent an optional metadata field.
3014template <class FieldTy> struct MDFieldImpl {
3015 typedef MDFieldImpl ImplTy;
3016 FieldTy Val;
3017 bool Seen;
3018
3019 void assign(FieldTy Val) {
3020 Seen = true;
3021 this->Val = std::move(Val);
3022 }
3023
3024 explicit MDFieldImpl(FieldTy Default)
3025 : Val(std::move(Default)), Seen(false) {}
3026};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003027
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003028struct MDUnsignedField : public MDFieldImpl<uint64_t> {
3029 uint64_t Max;
3030
3031 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
3032 : ImplTy(Default), Max(Max) {}
3033};
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003034struct LineField : public MDUnsignedField {
Duncan P. N. Exon Smithaf677eb2015-02-06 22:50:13 +00003035 LineField() : MDUnsignedField(0, UINT32_MAX) {}
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003036};
3037struct ColumnField : public MDUnsignedField {
3038 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
3039};
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003040struct DwarfTagField : public MDUnsignedField {
Duncan P. N. Exon Smitha81f7e12015-02-06 22:29:35 +00003041 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003042 DwarfTagField(dwarf::Tag DefaultTag)
3043 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003044};
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003045struct DwarfAttEncodingField : public MDUnsignedField {
3046 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
3047};
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003048struct DwarfVirtualityField : public MDUnsignedField {
3049 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
3050};
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003051struct DwarfLangField : public MDUnsignedField {
3052 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
3053};
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003054
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003055struct DIFlagField : public MDUnsignedField {
3056 DIFlagField() : MDUnsignedField(0, UINT32_MAX) {}
3057};
3058
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003059struct MDSignedField : public MDFieldImpl<int64_t> {
3060 int64_t Min;
3061 int64_t Max;
3062
3063 MDSignedField(int64_t Default = 0)
3064 : ImplTy(Default), Min(INT64_MIN), Max(INT64_MAX) {}
3065 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
3066 : ImplTy(Default), Min(Min), Max(Max) {}
3067};
3068
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003069struct MDBoolField : public MDFieldImpl<bool> {
3070 MDBoolField(bool Default = false) : ImplTy(Default) {}
3071};
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003072struct MDField : public MDFieldImpl<Metadata *> {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003073 bool AllowNull;
3074
3075 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003076};
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003077struct MDConstant : public MDFieldImpl<ConstantAsMetadata *> {
3078 MDConstant() : ImplTy(nullptr) {}
3079};
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003080struct MDStringField : public MDFieldImpl<MDString *> {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003081 bool AllowEmpty;
3082 MDStringField(bool AllowEmpty = true)
3083 : ImplTy(nullptr), AllowEmpty(AllowEmpty) {}
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003084};
3085struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
3086 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
3087};
3088
3089} // end namespace
3090
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00003091namespace llvm {
3092
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003093template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003094bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003095 MDUnsignedField &Result) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003096 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3097 return TokError("expected unsigned integer");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003098
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003099 auto &U = Lex.getAPSIntVal();
3100 if (U.ugt(Result.Max))
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003101 return TokError("value for '" + Name + "' too large, limit is " +
3102 Twine(Result.Max));
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003103 Result.assign(U.getZExtValue());
3104 assert(Result.Val <= Result.Max && "Expected value in range");
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003105 Lex.Lex();
3106 return false;
3107}
3108
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003109template <>
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003110bool LLParser::ParseMDField(LocTy Loc, StringRef Name, LineField &Result) {
3111 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3112}
3113template <>
3114bool LLParser::ParseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
3115 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3116}
3117
3118template <>
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003119bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
3120 if (Lex.getKind() == lltok::APSInt)
Duncan P. N. Exon Smith39b10c22015-02-04 21:57:52 +00003121 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003122
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003123 if (Lex.getKind() != lltok::DwarfTag)
3124 return TokError("expected DWARF tag");
3125
3126 unsigned Tag = dwarf::getTag(Lex.getStrVal());
3127 if (Tag == dwarf::DW_TAG_invalid)
3128 return TokError("invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
Duncan P. N. Exon Smithfad631a2015-02-04 22:02:18 +00003129 assert(Tag <= Result.Max && "Expected valid DWARF tag");
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003130
3131 Result.assign(Tag);
3132 Lex.Lex();
3133 return false;
3134}
3135
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003136template <>
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003137bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
3138 DwarfVirtualityField &Result) {
3139 if (Lex.getKind() == lltok::APSInt)
3140 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3141
3142 if (Lex.getKind() != lltok::DwarfVirtuality)
3143 return TokError("expected DWARF virtuality code");
3144
3145 unsigned Virtuality = dwarf::getVirtuality(Lex.getStrVal());
3146 if (!Virtuality)
3147 return TokError("invalid DWARF virtuality code" + Twine(" '") +
3148 Lex.getStrVal() + "'");
3149 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
3150 Result.assign(Virtuality);
3151 Lex.Lex();
3152 return false;
3153}
3154
3155template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003156bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
3157 if (Lex.getKind() == lltok::APSInt)
3158 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3159
3160 if (Lex.getKind() != lltok::DwarfLang)
3161 return TokError("expected DWARF language");
3162
3163 unsigned Lang = dwarf::getLanguage(Lex.getStrVal());
3164 if (!Lang)
3165 return TokError("invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
3166 "'");
3167 assert(Lang <= Result.Max && "Expected valid DWARF language");
3168 Result.assign(Lang);
3169 Lex.Lex();
3170 return false;
3171}
3172
3173template <>
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003174bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003175 DwarfAttEncodingField &Result) {
3176 if (Lex.getKind() == lltok::APSInt)
3177 return ParseMDField(Loc, Name, static_cast<MDUnsignedField &>(Result));
3178
3179 if (Lex.getKind() != lltok::DwarfAttEncoding)
3180 return TokError("expected DWARF type attribute encoding");
3181
3182 unsigned Encoding = dwarf::getAttributeEncoding(Lex.getStrVal());
3183 if (!Encoding)
3184 return TokError("invalid DWARF type attribute encoding" + Twine(" '") +
3185 Lex.getStrVal() + "'");
3186 assert(Encoding <= Result.Max && "Expected valid DWARF language");
3187 Result.assign(Encoding);
3188 Lex.Lex();
3189 return false;
3190}
3191
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003192/// DIFlagField
3193/// ::= uint32
3194/// ::= DIFlagVector
3195/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
3196template <>
3197bool LLParser::ParseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
3198 assert(Result.Max == UINT32_MAX && "Expected only 32-bits");
3199
3200 // Parser for a single flag.
3201 auto parseFlag = [&](unsigned &Val) {
3202 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned())
3203 return ParseUInt32(Val);
3204
3205 if (Lex.getKind() != lltok::DIFlag)
3206 return TokError("expected debug info flag");
3207
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003208 Val = DINode::getFlag(Lex.getStrVal());
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003209 if (!Val)
3210 return TokError(Twine("invalid debug info flag flag '") +
3211 Lex.getStrVal() + "'");
3212 Lex.Lex();
3213 return false;
3214 };
3215
3216 // Parse the flags and combine them together.
3217 unsigned Combined = 0;
3218 do {
3219 unsigned Val;
3220 if (parseFlag(Val))
3221 return true;
3222 Combined |= Val;
3223 } while (EatIfPresent(lltok::bar));
3224
3225 Result.assign(Combined);
3226 return false;
3227}
3228
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003229template <>
3230bool LLParser::ParseMDField(LocTy Loc, StringRef Name,
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003231 MDSignedField &Result) {
3232 if (Lex.getKind() != lltok::APSInt)
3233 return TokError("expected signed integer");
3234
3235 auto &S = Lex.getAPSIntVal();
3236 if (S < Result.Min)
3237 return TokError("value for '" + Name + "' too small, limit is " +
3238 Twine(Result.Min));
3239 if (S > Result.Max)
3240 return TokError("value for '" + Name + "' too large, limit is " +
3241 Twine(Result.Max));
3242 Result.assign(S.getExtValue());
3243 assert(Result.Val >= Result.Min && "Expected value in range");
3244 assert(Result.Val <= Result.Max && "Expected value in range");
3245 Lex.Lex();
3246 return false;
3247}
3248
3249template <>
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003250bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
3251 switch (Lex.getKind()) {
3252 default:
3253 return TokError("expected 'true' or 'false'");
3254 case lltok::kw_true:
3255 Result.assign(true);
3256 break;
3257 case lltok::kw_false:
3258 Result.assign(false);
3259 break;
3260 }
3261 Lex.Lex();
3262 return false;
3263}
3264
3265template <>
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003266bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDField &Result) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003267 if (Lex.getKind() == lltok::kw_null) {
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003268 if (!Result.AllowNull)
3269 return TokError("'" + Name + "' cannot be null");
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003270 Lex.Lex();
3271 Result.assign(nullptr);
3272 return false;
3273 }
3274
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003275 Metadata *MD;
3276 if (ParseMetadata(MD, nullptr))
3277 return true;
3278
3279 Result.assign(MD);
3280 return false;
3281}
3282
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003283template <>
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003284bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDConstant &Result) {
3285 Metadata *MD;
3286 if (ParseValueAsMetadata(MD, "expected constant", nullptr))
3287 return true;
3288
3289 Result.assign(cast<ConstantAsMetadata>(MD));
3290 return false;
3291}
3292
3293template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003294bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003295 LocTy ValueLoc = Lex.getLoc();
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003296 std::string S;
3297 if (ParseStringConstant(S))
3298 return true;
3299
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003300 if (!Result.AllowEmpty && S.empty())
3301 return Error(ValueLoc, "'" + Name + "' cannot be empty");
3302
Duncan P. N. Exon Smith3d2afaa2015-03-27 17:29:58 +00003303 Result.assign(S.empty() ? nullptr : MDString::get(Context, S));
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003304 return false;
3305}
3306
Duncan P. N. Exon Smith077c0312015-02-04 22:05:21 +00003307template <>
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003308bool LLParser::ParseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
3309 SmallVector<Metadata *, 4> MDs;
3310 if (ParseMDNodeVector(MDs))
3311 return true;
3312
3313 Result.assign(std::move(MDs));
3314 return false;
3315}
3316
Duncan P. N. Exon Smith2f09c462015-02-04 22:13:28 +00003317} // end namespace llvm
3318
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003319template <class ParserTy>
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00003320bool LLParser::ParseMDFieldsImplBody(ParserTy parseField) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003321 do {
3322 if (Lex.getKind() != lltok::LabelStr)
3323 return TokError("expected field label here");
3324
3325 if (parseField())
3326 return true;
3327 } while (EatIfPresent(lltok::comma));
3328
Duncan P. N. Exon Smith66ca92e2015-01-19 23:39:32 +00003329 return false;
3330}
3331
3332template <class ParserTy>
3333bool LLParser::ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc) {
3334 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
3335 Lex.Lex();
3336
3337 if (ParseToken(lltok::lparen, "expected '(' here"))
3338 return true;
3339 if (Lex.getKind() != lltok::rparen)
3340 if (ParseMDFieldsImplBody(parseField))
3341 return true;
3342
Duncan P. N. Exon Smith13890af2015-01-19 23:32:36 +00003343 ClosingLoc = Lex.getLoc();
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003344 return ParseToken(lltok::rparen, "expected ')' here");
3345}
3346
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00003347template <class FieldTy>
3348bool LLParser::ParseMDField(StringRef Name, FieldTy &Result) {
3349 if (Result.Seen)
3350 return TokError("field '" + Name + "' cannot be specified more than once");
3351
3352 LocTy Loc = Lex.getLoc();
3353 Lex.Lex();
3354 return ParseMDField(Loc, Name, Result);
3355}
3356
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003357bool LLParser::ParseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
3358 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003359
3360#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003361 if (Lex.getStrVal() == #CLASS) \
3362 return Parse##CLASS(N, IsDistinct);
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003363#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003364
3365 return TokError("expected metadata type");
3366}
3367
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003368#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
3369#define NOP_FIELD(NAME, TYPE, INIT)
3370#define REQUIRE_FIELD(NAME, TYPE, INIT) \
3371 if (!NAME.Seen) \
3372 return Error(ClosingLoc, "missing required field '" #NAME "'");
3373#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
Duncan P. N. Exon Smitha7477282015-01-20 02:42:29 +00003374 if (Lex.getStrVal() == #NAME) \
3375 return ParseMDField(#NAME, NAME);
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003376#define PARSE_MD_FIELDS() \
3377 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
3378 do { \
3379 LocTy ClosingLoc; \
3380 if (ParseMDFieldsImpl([&]() -> bool { \
3381 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
3382 return TokError(Twine("invalid field '") + Lex.getStrVal() + "'"); \
3383 }, ClosingLoc)) \
3384 return true; \
3385 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
3386 } while (false)
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003387#define GET_OR_DISTINCT(CLASS, ARGS) \
3388 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003389
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003390/// ParseDILocationFields:
3391/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6)
3392bool LLParser::ParseDILocation(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003393#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith9c93dc62015-02-04 22:59:18 +00003394 OPTIONAL(line, LineField, ); \
3395 OPTIONAL(column, ColumnField, ); \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003396 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003397 OPTIONAL(inlinedAt, MDField, );
3398 PARSE_MD_FIELDS();
3399#undef VISIT_MD_FIELDS
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003400
Duncan P. N. Exon Smith26489982015-03-26 22:05:04 +00003401 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003402 DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val));
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003403 return false;
3404}
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003405
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003406/// ParseGenericDINode:
3407/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
3408bool LLParser::ParseGenericDINode(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003409#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith97486072015-02-03 21:56:01 +00003410 REQUIRED(tag, DwarfTagField, ); \
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003411 OPTIONAL(header, MDStringField, ); \
3412 OPTIONAL(operands, MDFieldList, );
3413 PARSE_MD_FIELDS();
3414#undef VISIT_MD_FIELDS
3415
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003416 Result = GET_OR_DISTINCT(GenericDINode,
Duncan P. N. Exon Smith4e4aa702015-02-03 21:54:14 +00003417 (Context, tag.Val, header.Val, operands.Val));
3418 return false;
3419}
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003420
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003421/// ParseDISubrange:
3422/// ::= !DISubrange(count: 30, lowerBound: 2)
3423bool LLParser::ParseDISubrange(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003424#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith5c9a1772015-02-18 23:17:51 +00003425 REQUIRED(count, MDSignedField, (-1, -1, INT64_MAX)); \
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003426 OPTIONAL(lowerBound, MDSignedField, );
3427 PARSE_MD_FIELDS();
3428#undef VISIT_MD_FIELDS
3429
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003430 Result = GET_OR_DISTINCT(DISubrange, (Context, count.Val, lowerBound.Val));
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003431 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003432}
Duncan P. N. Exon Smithc7363f12015-02-13 01:10:38 +00003433
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003434/// ParseDIEnumerator:
3435/// ::= !DIEnumerator(value: 30, name: "SomeKind")
3436bool LLParser::ParseDIEnumerator(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003437#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smithcd8fb602015-02-18 21:16:33 +00003438 REQUIRED(name, MDStringField, ); \
3439 REQUIRED(value, MDSignedField, );
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003440 PARSE_MD_FIELDS();
3441#undef VISIT_MD_FIELDS
3442
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003443 Result = GET_OR_DISTINCT(DIEnumerator, (Context, value.Val, name.Val));
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003444 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003445}
Duncan P. N. Exon Smith87754762015-02-13 01:14:11 +00003446
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003447/// ParseDIBasicType:
3448/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32)
3449bool LLParser::ParseDIBasicType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003450#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003451 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003452 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003453 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3454 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smithcd6636c2015-02-13 01:17:35 +00003455 OPTIONAL(encoding, DwarfAttEncodingField, );
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003456 PARSE_MD_FIELDS();
3457#undef VISIT_MD_FIELDS
3458
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003459 Result = GET_OR_DISTINCT(DIBasicType, (Context, tag.Val, name.Val, size.Val,
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003460 align.Val, encoding.Val));
3461 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003462}
Duncan P. N. Exon Smith09e03f32015-02-13 01:14:58 +00003463
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003464/// ParseDIDerivedType:
3465/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003466/// line: 7, scope: !1, baseType: !2, size: 32,
3467/// align: 32, offset: 0, flags: 0, extraData: !3)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003468bool LLParser::ParseDIDerivedType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003469#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3470 REQUIRED(tag, DwarfTagField, ); \
3471 OPTIONAL(name, MDStringField, ); \
3472 OPTIONAL(file, MDField, ); \
3473 OPTIONAL(line, LineField, ); \
3474 OPTIONAL(scope, MDField, ); \
3475 REQUIRED(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003476 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3477 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
3478 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003479 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003480 OPTIONAL(extraData, MDField, );
3481 PARSE_MD_FIELDS();
3482#undef VISIT_MD_FIELDS
3483
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003484 Result = GET_OR_DISTINCT(DIDerivedType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003485 (Context, tag.Val, name.Val, file.Val, line.Val,
3486 scope.Val, baseType.Val, size.Val, align.Val,
3487 offset.Val, flags.Val, extraData.Val));
3488 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003489}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003490
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003491bool LLParser::ParseDICompositeType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003492#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3493 REQUIRED(tag, DwarfTagField, ); \
3494 OPTIONAL(name, MDStringField, ); \
3495 OPTIONAL(file, MDField, ); \
3496 OPTIONAL(line, LineField, ); \
3497 OPTIONAL(scope, MDField, ); \
3498 OPTIONAL(baseType, MDField, ); \
Duncan P. N. Exon Smithd34db172015-02-19 23:56:07 +00003499 OPTIONAL(size, MDUnsignedField, (0, UINT64_MAX)); \
3500 OPTIONAL(align, MDUnsignedField, (0, UINT64_MAX)); \
3501 OPTIONAL(offset, MDUnsignedField, (0, UINT64_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003502 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003503 OPTIONAL(elements, MDField, ); \
Duncan P. N. Exon Smithaece2dc2015-02-13 01:21:25 +00003504 OPTIONAL(runtimeLang, DwarfLangField, ); \
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003505 OPTIONAL(vtableHolder, MDField, ); \
3506 OPTIONAL(templateParams, MDField, ); \
3507 OPTIONAL(identifier, MDStringField, );
3508 PARSE_MD_FIELDS();
3509#undef VISIT_MD_FIELDS
3510
3511 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003512 DICompositeType,
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003513 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
3514 size.Val, align.Val, offset.Val, flags.Val, elements.Val,
3515 runtimeLang.Val, vtableHolder.Val, templateParams.Val, identifier.Val));
3516 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003517}
Duncan P. N. Exon Smith171d0772015-02-13 01:20:38 +00003518
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003519bool LLParser::ParseDISubroutineType(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003520#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003521 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003522 REQUIRED(types, MDField, );
3523 PARSE_MD_FIELDS();
3524#undef VISIT_MD_FIELDS
3525
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003526 Result = GET_OR_DISTINCT(DISubroutineType, (Context, flags.Val, types.Val));
Duncan P. N. Exon Smith54e2bc62015-02-13 01:22:59 +00003527 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003528}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003529
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003530/// ParseDIFileType:
3531/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir")
3532bool LLParser::ParseDIFile(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003533#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3534 REQUIRED(filename, MDStringField, ); \
3535 REQUIRED(directory, MDStringField, );
3536 PARSE_MD_FIELDS();
3537#undef VISIT_MD_FIELDS
3538
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003539 Result = GET_OR_DISTINCT(DIFile, (Context, filename.Val, directory.Val));
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003540 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003541}
Duncan P. N. Exon Smithf14b9c72015-02-13 01:19:14 +00003542
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003543/// ParseDICompileUnit:
3544/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003545/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
3546/// splitDebugFilename: "abc.debug", emissionKind: 1,
3547/// enums: !1, retainedTypes: !2, subprograms: !3,
Adrian Prantl1f599f92015-05-21 20:37:30 +00003548/// globals: !4, imports: !5, dwoId: 0x0abcd)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003549bool LLParser::ParseDICompileUnit(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003550#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3551 REQUIRED(language, DwarfLangField, ); \
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +00003552 REQUIRED(file, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003553 OPTIONAL(producer, MDStringField, ); \
3554 OPTIONAL(isOptimized, MDBoolField, ); \
3555 OPTIONAL(flags, MDStringField, ); \
3556 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
3557 OPTIONAL(splitDebugFilename, MDStringField, ); \
3558 OPTIONAL(emissionKind, MDUnsignedField, (0, UINT32_MAX)); \
3559 OPTIONAL(enums, MDField, ); \
3560 OPTIONAL(retainedTypes, MDField, ); \
3561 OPTIONAL(subprograms, MDField, ); \
3562 OPTIONAL(globals, MDField, ); \
Adrian Prantl1f599f92015-05-21 20:37:30 +00003563 OPTIONAL(imports, MDField, ); \
3564 OPTIONAL(dwoId, MDUnsignedField, );
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003565 PARSE_MD_FIELDS();
3566#undef VISIT_MD_FIELDS
3567
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003568 Result = GET_OR_DISTINCT(DICompileUnit,
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003569 (Context, language.Val, file.Val, producer.Val,
3570 isOptimized.Val, flags.Val, runtimeVersion.Val,
3571 splitDebugFilename.Val, emissionKind.Val, enums.Val,
3572 retainedTypes.Val, subprograms.Val, globals.Val,
Adrian Prantl1f599f92015-05-21 20:37:30 +00003573 imports.Val, dwoId.Val));
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003574 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003575}
Duncan P. N. Exon Smithc1f1acc2015-02-13 01:25:10 +00003576
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003577/// ParseDISubprogram:
3578/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003579/// file: !1, line: 7, type: !2, isLocal: false,
3580/// isDefinition: true, scopeLine: 8, containingType: !3,
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003581/// virtuality: DW_VIRTUALTIY_pure_virtual,
3582/// virtualIndex: 10, flags: 11,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003583/// isOptimized: false, function: void ()* @_Z3foov,
3584/// templateParams: !4, declaration: !5, variables: !6)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003585bool LLParser::ParseDISubprogram(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003586#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3587 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00003588 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003589 OPTIONAL(linkageName, MDStringField, ); \
3590 OPTIONAL(file, MDField, ); \
3591 OPTIONAL(line, LineField, ); \
3592 OPTIONAL(type, MDField, ); \
3593 OPTIONAL(isLocal, MDBoolField, ); \
3594 OPTIONAL(isDefinition, MDBoolField, (true)); \
3595 OPTIONAL(scopeLine, LineField, ); \
3596 OPTIONAL(containingType, MDField, ); \
Duncan P. N. Exon Smith890533e2015-02-13 01:28:16 +00003597 OPTIONAL(virtuality, DwarfVirtualityField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003598 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
Duncan P. N. Exon Smith70ab3d22015-02-21 01:02:18 +00003599 OPTIONAL(flags, DIFlagField, ); \
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003600 OPTIONAL(isOptimized, MDBoolField, ); \
3601 OPTIONAL(function, MDConstant, ); \
3602 OPTIONAL(templateParams, MDField, ); \
3603 OPTIONAL(declaration, MDField, ); \
3604 OPTIONAL(variables, MDField, );
3605 PARSE_MD_FIELDS();
3606#undef VISIT_MD_FIELDS
3607
3608 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003609 DISubprogram, (Context, scope.Val, name.Val, linkageName.Val, file.Val,
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003610 line.Val, type.Val, isLocal.Val, isDefinition.Val,
3611 scopeLine.Val, containingType.Val, virtuality.Val,
3612 virtualIndex.Val, flags.Val, isOptimized.Val, function.Val,
3613 templateParams.Val, declaration.Val, variables.Val));
3614 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003615}
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003616
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003617/// ParseDILexicalBlock:
3618/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
3619bool LLParser::ParseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003620#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00003621 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003622 OPTIONAL(file, MDField, ); \
3623 OPTIONAL(line, LineField, ); \
3624 OPTIONAL(column, ColumnField, );
3625 PARSE_MD_FIELDS();
3626#undef VISIT_MD_FIELDS
3627
3628 Result = GET_OR_DISTINCT(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003629 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003630 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003631}
Duncan P. N. Exon Smitha96d4092015-02-13 01:29:28 +00003632
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003633/// ParseDILexicalBlockFile:
3634/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
3635bool LLParser::ParseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003636#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith0e202b92015-03-30 16:37:48 +00003637 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003638 OPTIONAL(file, MDField, ); \
3639 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
3640 PARSE_MD_FIELDS();
3641#undef VISIT_MD_FIELDS
3642
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003643 Result = GET_OR_DISTINCT(DILexicalBlockFile,
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003644 (Context, scope.Val, file.Val, discriminator.Val));
3645 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003646}
Duncan P. N. Exon Smith06a07022015-02-13 01:30:42 +00003647
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003648/// ParseDINamespace:
3649/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
3650bool LLParser::ParseDINamespace(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003651#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3652 REQUIRED(scope, MDField, ); \
3653 OPTIONAL(file, MDField, ); \
3654 OPTIONAL(name, MDStringField, ); \
3655 OPTIONAL(line, LineField, );
3656 PARSE_MD_FIELDS();
3657#undef VISIT_MD_FIELDS
3658
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003659 Result = GET_OR_DISTINCT(DINamespace,
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003660 (Context, scope.Val, file.Val, name.Val, line.Val));
3661 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003662}
Duncan P. N. Exon Smithe1460002015-02-13 01:32:09 +00003663
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003664/// ParseDITemplateTypeParameter:
3665/// ::= !DITemplateTypeParameter(name: "Ty", type: !1)
3666bool LLParser::ParseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003667#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003668 OPTIONAL(name, MDStringField, ); \
3669 REQUIRED(type, MDField, );
3670 PARSE_MD_FIELDS();
3671#undef VISIT_MD_FIELDS
3672
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003673 Result =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003674 GET_OR_DISTINCT(DITemplateTypeParameter, (Context, name.Val, type.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003675 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003676}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003677
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003678/// ParseDITemplateValueParameter:
3679/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003680/// name: "V", type: !1, value: i32 7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003681bool LLParser::ParseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003682#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003683 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003684 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smith16d182a2015-02-28 23:21:38 +00003685 OPTIONAL(type, MDField, ); \
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003686 REQUIRED(value, MDField, );
3687 PARSE_MD_FIELDS();
3688#undef VISIT_MD_FIELDS
3689
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003690 Result = GET_OR_DISTINCT(DITemplateValueParameter,
Duncan P. N. Exon Smith3d62bba2015-02-19 00:37:21 +00003691 (Context, tag.Val, name.Val, type.Val, value.Val));
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003692 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003693}
Duncan P. N. Exon Smith2847f382015-02-13 01:34:32 +00003694
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003695/// ParseDIGlobalVariable:
3696/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003697/// file: !1, line: 7, type: !2, isLocal: false,
3698/// isDefinition: true, variable: i32* @foo,
3699/// declaration: !3)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003700bool LLParser::ParseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003701#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +00003702 REQUIRED(name, MDStringField, (/* AllowEmpty */ false)); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003703 OPTIONAL(scope, MDField, ); \
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003704 OPTIONAL(linkageName, MDStringField, ); \
3705 OPTIONAL(file, MDField, ); \
3706 OPTIONAL(line, LineField, ); \
3707 OPTIONAL(type, MDField, ); \
3708 OPTIONAL(isLocal, MDBoolField, ); \
3709 OPTIONAL(isDefinition, MDBoolField, (true)); \
3710 OPTIONAL(variable, MDConstant, ); \
3711 OPTIONAL(declaration, MDField, );
3712 PARSE_MD_FIELDS();
3713#undef VISIT_MD_FIELDS
3714
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003715 Result = GET_OR_DISTINCT(DIGlobalVariable,
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003716 (Context, scope.Val, name.Val, linkageName.Val,
3717 file.Val, line.Val, type.Val, isLocal.Val,
3718 isDefinition.Val, variable.Val, declaration.Val));
3719 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003720}
Duncan P. N. Exon Smithc8f810a2015-02-13 01:35:40 +00003721
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003722/// ParseDILocalVariable:
3723/// ::= !DILocalVariable(tag: DW_TAG_arg_variable, scope: !0, name: "foo",
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003724/// file: !1, line: 7, type: !2, arg: 2, flags: 7)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003725bool LLParser::ParseDILocalVariable(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003726#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3727 REQUIRED(tag, DwarfTagField, ); \
Duncan P. N. Exon Smithe2c61d92015-03-27 17:56:39 +00003728 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003729 OPTIONAL(name, MDStringField, ); \
3730 OPTIONAL(file, MDField, ); \
3731 OPTIONAL(line, LineField, ); \
3732 OPTIONAL(type, MDField, ); \
3733 OPTIONAL(arg, MDUnsignedField, (0, UINT8_MAX)); \
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003734 OPTIONAL(flags, DIFlagField, );
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003735 PARSE_MD_FIELDS();
3736#undef VISIT_MD_FIELDS
3737
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003738 Result = GET_OR_DISTINCT(DILocalVariable,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00003739 (Context, tag.Val, scope.Val, name.Val, file.Val,
3740 line.Val, type.Val, arg.Val, flags.Val));
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003741 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003742}
Duncan P. N. Exon Smith72fe2d02015-02-13 01:39:44 +00003743
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003744/// ParseDIExpression:
3745/// ::= !DIExpression(0, 7, -1)
3746bool LLParser::ParseDIExpression(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003747 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
3748 Lex.Lex();
3749
3750 if (ParseToken(lltok::lparen, "expected '(' here"))
3751 return true;
3752
3753 SmallVector<uint64_t, 8> Elements;
3754 if (Lex.getKind() != lltok::rparen)
3755 do {
3756 if (Lex.getKind() == lltok::DwarfOp) {
3757 if (unsigned Op = dwarf::getOperationEncoding(Lex.getStrVal())) {
3758 Lex.Lex();
3759 Elements.push_back(Op);
3760 continue;
3761 }
3762 return TokError(Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
3763 }
3764
3765 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
3766 return TokError("expected unsigned integer");
3767
3768 auto &U = Lex.getAPSIntVal();
3769 if (U.ugt(UINT64_MAX))
3770 return TokError("element too large, limit is " + Twine(UINT64_MAX));
3771 Elements.push_back(U.getZExtValue());
3772 Lex.Lex();
3773 } while (EatIfPresent(lltok::comma));
3774
3775 if (ParseToken(lltok::rparen, "expected ')' here"))
3776 return true;
3777
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003778 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003779 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003780}
Duncan P. N. Exon Smith0c5c0122015-02-13 01:42:09 +00003781
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003782/// ParseDIObjCProperty:
3783/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003784/// getter: "getFoo", attributes: 7, type: !2)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003785bool LLParser::ParseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003786#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
Duncan P. N. Exon Smith3eea1962015-03-16 19:01:54 +00003787 OPTIONAL(name, MDStringField, ); \
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003788 OPTIONAL(file, MDField, ); \
3789 OPTIONAL(line, LineField, ); \
3790 OPTIONAL(setter, MDStringField, ); \
3791 OPTIONAL(getter, MDStringField, ); \
3792 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
3793 OPTIONAL(type, MDField, );
3794 PARSE_MD_FIELDS();
3795#undef VISIT_MD_FIELDS
3796
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003797 Result = GET_OR_DISTINCT(DIObjCProperty,
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003798 (Context, name.Val, file.Val, line.Val, setter.Val,
3799 getter.Val, attributes.Val, type.Val));
3800 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003801}
Duncan P. N. Exon Smithd45ce962015-02-13 01:43:22 +00003802
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003803/// ParseDIImportedEntity:
3804/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003805/// line: 7, name: "foo")
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003806bool LLParser::ParseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003807#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
3808 REQUIRED(tag, DwarfTagField, ); \
3809 REQUIRED(scope, MDField, ); \
3810 OPTIONAL(entity, MDField, ); \
3811 OPTIONAL(line, LineField, ); \
3812 OPTIONAL(name, MDStringField, );
3813 PARSE_MD_FIELDS();
3814#undef VISIT_MD_FIELDS
3815
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003816 Result = GET_OR_DISTINCT(DIImportedEntity, (Context, tag.Val, scope.Val,
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003817 entity.Val, line.Val, name.Val));
3818 return false;
Duncan P. N. Exon Smithed458fa2015-02-10 01:08:16 +00003819}
Duncan P. N. Exon Smith1c931162015-02-13 01:46:02 +00003820
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003821#undef PARSE_MD_FIELD
Duncan P. N. Exon Smith2a6b5fc2015-01-19 23:44:41 +00003822#undef NOP_FIELD
3823#undef REQUIRE_FIELD
3824#undef DECLARE_FIELD
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003825
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003826/// ParseMetadataAsValue
3827/// ::= metadata i32 %local
3828/// ::= metadata i32 @global
3829/// ::= metadata i32 7
3830/// ::= metadata !0
3831/// ::= metadata !{...}
3832/// ::= metadata !"string"
3833bool LLParser::ParseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
3834 // Note: the type 'metadata' has already been parsed.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003835 Metadata *MD;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003836 if (ParseMetadata(MD, &PFS))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003837 return true;
3838
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003839 V = MetadataAsValue::get(Context, MD);
3840 return false;
3841}
3842
3843/// ParseValueAsMetadata
3844/// ::= i32 %local
3845/// ::= i32 @global
3846/// ::= i32 7
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003847bool LLParser::ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
3848 PerFunctionState *PFS) {
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003849 Type *Ty;
3850 LocTy Loc;
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003851 if (ParseType(Ty, TypeMsg, Loc))
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003852 return true;
3853 if (Ty->isMetadataTy())
3854 return Error(Loc, "invalid metadata-value-metadata roundtrip");
3855
3856 Value *V;
3857 if (ParseValue(Ty, V, PFS))
3858 return true;
3859
3860 MD = ValueAsMetadata::get(V);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003861 return false;
3862}
3863
3864/// ParseMetadata
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003865/// ::= i32 %local
3866/// ::= i32 @global
3867/// ::= i32 7
Dan Gohman8939ba332010-07-14 18:26:50 +00003868/// ::= !42
3869/// ::= !{...}
3870/// ::= !"string"
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00003871/// ::= !DILocation(...)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003872bool LLParser::ParseMetadata(Metadata *&MD, PerFunctionState *PFS) {
Duncan P. N. Exon Smith6a484832015-01-13 21:10:44 +00003873 if (Lex.getKind() == lltok::MetadataVar) {
3874 MDNode *N;
3875 if (ParseSpecializedMDNode(N))
3876 return true;
3877 MD = N;
3878 return false;
3879 }
3880
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003881 // ValueAsMetadata:
3882 // <type> <value>
3883 if (Lex.getKind() != lltok::exclaim)
Duncan P. N. Exon Smith19fc5ed2015-02-13 01:26:47 +00003884 return ParseValueAsMetadata(MD, "expected metadata operand", PFS);
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00003885
3886 // '!'.
3887 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
3888 Lex.Lex();
Dan Gohman8939ba332010-07-14 18:26:50 +00003889
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003890 // MDString:
3891 // ::= '!' STRINGCONSTANT
3892 if (Lex.getKind() == lltok::StringConstant) {
3893 MDString *S;
3894 if (ParseMDString(S))
3895 return true;
3896 MD = S;
3897 return false;
3898 }
3899
Dan Gohman8939ba332010-07-14 18:26:50 +00003900 // MDNode:
3901 // !{ ... }
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003902 // !7
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003903 MDNode *N;
Duncan P. N. Exon Smithf825dae2015-01-12 22:26:48 +00003904 if (ParseMDNodeTail(N))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00003905 return true;
Duncan P. N. Exon Smith62a79192015-01-12 22:24:50 +00003906 MD = N;
Dan Gohman8939ba332010-07-14 18:26:50 +00003907 return false;
3908}
3909
Victor Hernandez9d75c962010-01-11 22:31:58 +00003910
3911//===----------------------------------------------------------------------===//
3912// Function Parsing.
3913//===----------------------------------------------------------------------===//
3914
Chris Lattner229907c2011-07-18 04:54:35 +00003915bool LLParser::ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
Victor Hernandez9d75c962010-01-11 22:31:58 +00003916 PerFunctionState *PFS) {
Duncan Sands19d0b472010-02-16 11:11:14 +00003917 if (Ty->isFunctionTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003918 return Error(ID.Loc, "functions are not values, refer to them as pointers");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003919
Chris Lattnerac161bf2009-01-02 07:01:27 +00003920 switch (ID.Kind) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003921 case ValID::t_LocalID:
Victor Hernandez9d75c962010-01-11 22:31:58 +00003922 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
3923 V = PFS->GetVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003924 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003925 case ValID::t_LocalName:
Victor Hernandez9d75c962010-01-11 22:31:58 +00003926 if (!PFS) return Error(ID.Loc, "invalid use of function-local name");
3927 V = PFS->GetVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003928 return V == nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003929 case ValID::t_InlineAsm: {
Chris Lattner229907c2011-07-18 04:54:35 +00003930 PointerType *PTy = dyn_cast<PointerType>(Ty);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00003931 FunctionType *FTy =
Craig Topper2617dcc2014-04-15 06:32:26 +00003932 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : nullptr;
Victor Hernandez9d75c962010-01-11 22:31:58 +00003933 if (!FTy || !InlineAsm::Verify(FTy, ID.StrVal2))
3934 return Error(ID.Loc, "invalid type for inline asm constraint string");
Chad Rosierf42fad62012-09-05 00:08:17 +00003935 V = InlineAsm::get(FTy, ID.StrVal, ID.StrVal2, ID.UIntVal&1,
Chad Rosierd8c76102012-09-05 19:00:49 +00003936 (ID.UIntVal>>1)&1, (InlineAsm::AsmDialect(ID.UIntVal>>2)));
Victor Hernandez9d75c962010-01-11 22:31:58 +00003937 return false;
3938 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00003939 case ValID::t_GlobalName:
3940 V = GetGlobalVal(ID.StrVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003941 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003942 case ValID::t_GlobalID:
3943 V = GetGlobalVal(ID.UIntVal, Ty, ID.Loc);
Craig Topper2617dcc2014-04-15 06:32:26 +00003944 return V == nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003945 case ValID::t_APSInt:
Duncan Sands19d0b472010-02-16 11:11:14 +00003946 if (!Ty->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003947 return Error(ID.Loc, "integer constant must have integer type");
Jay Foad583abbc2010-12-07 08:25:19 +00003948 ID.APSIntVal = ID.APSIntVal.extOrTrunc(Ty->getPrimitiveSizeInBits());
Owen Andersonedb4a702009-07-24 23:12:02 +00003949 V = ConstantInt::get(Context, ID.APSIntVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003950 return false;
3951 case ValID::t_APFloat:
Duncan Sands9dff9be2010-02-15 16:12:20 +00003952 if (!Ty->isFloatingPointTy() ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00003953 !ConstantFP::isValueValidForType(Ty, ID.APFloatVal))
3954 return Error(ID.Loc, "floating point constant invalid for type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003955
Dan Gohman518cda42011-12-17 00:04:22 +00003956 // The lexer has no type info, so builds all half, float, and double FP
3957 // constants as double. Fix this here. Long double does not need this.
3958 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00003959 bool Ignored;
Dan Gohman518cda42011-12-17 00:04:22 +00003960 if (Ty->isHalfTy())
3961 ID.APFloatVal.convert(APFloat::IEEEhalf, APFloat::rmNearestTiesToEven,
3962 &Ignored);
3963 else if (Ty->isFloatTy())
3964 ID.APFloatVal.convert(APFloat::IEEEsingle, APFloat::rmNearestTiesToEven,
3965 &Ignored);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003966 }
Owen Anderson69c464d2009-07-27 20:59:43 +00003967 V = ConstantFP::get(Context, ID.APFloatVal);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003968
Chris Lattner8f57d29e2009-01-05 18:24:23 +00003969 if (V->getType() != Ty)
3970 return Error(ID.Loc, "floating point constant does not have type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00003971 getTypeString(Ty) + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00003972
Chris Lattnerac161bf2009-01-02 07:01:27 +00003973 return false;
3974 case ValID::t_Null:
Duncan Sands19d0b472010-02-16 11:11:14 +00003975 if (!Ty->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003976 return Error(ID.Loc, "null must be a pointer type");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003977 V = ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerac161bf2009-01-02 07:01:27 +00003978 return false;
3979 case ValID::t_Undef:
Chris Lattnerffa07782009-01-05 08:13:38 +00003980 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00003981 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerffa07782009-01-05 08:13:38 +00003982 return Error(ID.Loc, "invalid type for undef constant");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003983 V = UndefValue::get(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003984 return false;
Chris Lattner998fa0a2009-01-05 07:52:51 +00003985 case ValID::t_EmptyArray:
Duncan Sands19d0b472010-02-16 11:11:14 +00003986 if (!Ty->isArrayTy() || cast<ArrayType>(Ty)->getNumElements() != 0)
Chris Lattner998fa0a2009-01-05 07:52:51 +00003987 return Error(ID.Loc, "invalid empty array initializer");
Owen Andersonb292b8c2009-07-30 23:03:37 +00003988 V = UndefValue::get(Ty);
Chris Lattner998fa0a2009-01-05 07:52:51 +00003989 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00003990 case ValID::t_Zero:
Chris Lattnerffa07782009-01-05 08:13:38 +00003991 // FIXME: LabelTy should not be a first-class type.
Chris Lattnerfdd87902009-10-05 05:54:46 +00003992 if (!Ty->isFirstClassType() || Ty->isLabelTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00003993 return Error(ID.Loc, "invalid type for null constant");
Owen Anderson5a1acd92009-07-31 20:28:14 +00003994 V = Constant::getNullValue(Ty);
Chris Lattnerac161bf2009-01-02 07:01:27 +00003995 return false;
3996 case ValID::t_Constant:
Chris Lattner13ee7952010-08-28 04:09:24 +00003997 if (ID.ConstantVal->getType() != Ty)
Chris Lattnerac161bf2009-01-02 07:01:27 +00003998 return Error(ID.Loc, "constant expression type mismatch");
Chris Lattner392be582010-02-12 20:49:41 +00003999
Chris Lattnerac161bf2009-01-02 07:01:27 +00004000 V = ID.ConstantVal;
4001 return false;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004002 case ValID::t_ConstantStruct:
4003 case ValID::t_PackedConstantStruct:
Chris Lattner229907c2011-07-18 04:54:35 +00004004 if (StructType *ST = dyn_cast<StructType>(Ty)) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004005 if (ST->getNumElements() != ID.UIntVal)
4006 return Error(ID.Loc,
4007 "initializer with struct type has wrong # elements");
4008 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
4009 return Error(ID.Loc, "packed'ness of initializer and type don't match");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004010
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004011 // Verify that the elements are compatible with the structtype.
4012 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
4013 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(i))
4014 return Error(ID.Loc, "element " + Twine(i) +
4015 " of struct initializer doesn't match struct element type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004016
Reid Kleckner2ae03e12015-03-04 18:31:10 +00004017 V = ConstantStruct::get(ST, makeArrayRef(ID.ConstantStructElts,
4018 ID.UIntVal));
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004019 } else
4020 return Error(ID.Loc, "constant expression type mismatch");
4021 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004022 }
Chandler Carruthf3e85022012-01-10 18:08:01 +00004023 llvm_unreachable("Invalid ValID");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004024}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004025
Chris Lattner229907c2011-07-18 04:54:35 +00004026bool LLParser::ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004027 V = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004028 ValID ID;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004029 return ParseValID(ID, PFS) ||
4030 ConvertValIDToValue(Ty, ID, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004031}
4032
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004033bool LLParser::ParseTypeAndValue(Value *&V, PerFunctionState *PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004034 Type *Ty = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004035 return ParseType(Ty) ||
4036 ParseValue(Ty, V, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004037}
4038
Chris Lattner3ed871f2009-10-27 19:13:16 +00004039bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
4040 PerFunctionState &PFS) {
4041 Value *V;
4042 Loc = Lex.getLoc();
4043 if (ParseTypeAndValue(V, PFS)) return true;
4044 if (!isa<BasicBlock>(V))
4045 return Error(Loc, "expected a basic block");
4046 BB = cast<BasicBlock>(V);
4047 return false;
4048}
4049
4050
Chris Lattnerac161bf2009-01-02 07:01:27 +00004051/// FunctionHeader
4052/// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
Rafael Espindola45e6c192011-01-08 16:42:36 +00004053/// OptUnnamedAddr Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004054/// OptionalAlign OptGC OptionalPrefix OptionalPrologue
Chris Lattnerac161bf2009-01-02 07:01:27 +00004055bool LLParser::ParseFunctionHeader(Function *&Fn, bool isDefine) {
4056 // Parse the linkage.
4057 LocTy LinkageLoc = Lex.getLoc();
4058 unsigned Linkage;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004059
Kostya Serebryanya5054ad2012-01-20 17:56:17 +00004060 unsigned Visibility;
Nico Rieck7157bb72014-01-14 15:22:47 +00004061 unsigned DLLStorageClass;
Bill Wendling50d27842012-10-15 20:35:56 +00004062 AttrBuilder RetAttrs;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004063 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004064 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004065 LocTy RetTypeLoc = Lex.getLoc();
4066 if (ParseOptionalLinkage(Linkage) ||
4067 ParseOptionalVisibility(Visibility) ||
Nico Rieck7157bb72014-01-14 15:22:47 +00004068 ParseOptionalDLLStorageClass(DLLStorageClass) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004069 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004070 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004071 ParseType(RetType, RetTypeLoc, true /*void allowed*/))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004072 return true;
4073
4074 // Verify that the linkage is ok.
4075 switch ((GlobalValue::LinkageTypes)Linkage) {
4076 case GlobalValue::ExternalLinkage:
4077 break; // always ok.
Duncan Sandse2881052009-03-11 08:08:06 +00004078 case GlobalValue::ExternalWeakLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004079 if (isDefine)
4080 return Error(LinkageLoc, "invalid linkage for function definition");
4081 break;
Rafael Espindola6de96a12009-01-15 20:18:42 +00004082 case GlobalValue::PrivateLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004083 case GlobalValue::InternalLinkage:
Nick Lewycky8019af62009-04-13 07:02:02 +00004084 case GlobalValue::AvailableExternallyLinkage:
Duncan Sands12da8ce2009-03-07 15:45:40 +00004085 case GlobalValue::LinkOnceAnyLinkage:
4086 case GlobalValue::LinkOnceODRLinkage:
4087 case GlobalValue::WeakAnyLinkage:
4088 case GlobalValue::WeakODRLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004089 if (!isDefine)
4090 return Error(LinkageLoc, "invalid linkage for function declaration");
4091 break;
4092 case GlobalValue::AppendingLinkage:
Duncan Sands4581beb2009-03-11 20:14:15 +00004093 case GlobalValue::CommonLinkage:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004094 return Error(LinkageLoc, "invalid function linkage type");
4095 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004096
Duncan P. N. Exon Smithb80de102014-05-07 22:57:20 +00004097 if (!isValidVisibilityForLinkage(Visibility, Linkage))
4098 return Error(LinkageLoc,
4099 "symbol with local linkage must have default visibility");
4100
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004101 if (!FunctionType::isValidReturnType(RetType))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004102 return Error(RetTypeLoc, "invalid function return type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004103
Chris Lattnerac161bf2009-01-02 07:01:27 +00004104 LocTy NameLoc = Lex.getLoc();
Chris Lattner778c62c2009-02-18 21:48:13 +00004105
4106 std::string FunctionName;
4107 if (Lex.getKind() == lltok::GlobalVar) {
4108 FunctionName = Lex.getStrVal();
4109 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
4110 unsigned NameID = Lex.getUIntVal();
4111
4112 if (NameID != NumberedVals.size())
4113 return TokError("function expected to be numbered '%" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00004114 Twine(NumberedVals.size()) + "'");
Chris Lattner778c62c2009-02-18 21:48:13 +00004115 } else {
4116 return TokError("expected function name");
4117 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004118
Chris Lattner3822f632009-01-02 08:05:26 +00004119 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004120
Chris Lattner3822f632009-01-02 08:05:26 +00004121 if (Lex.getKind() != lltok::lparen)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004122 return TokError("expected '(' in function argument list");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004123
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004124 SmallVector<ArgInfo, 8> ArgList;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004125 bool isVarArg;
Bill Wendling50d27842012-10-15 20:35:56 +00004126 AttrBuilder FuncAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004127 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00004128 LocTy BuiltinLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004129 std::string Section;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004130 unsigned Alignment;
Chris Lattner3822f632009-01-02 08:05:26 +00004131 std::string GC;
Rafael Espindola563eb4b2011-01-25 19:09:56 +00004132 bool UnnamedAddr;
4133 LocTy UnnamedAddrLoc;
Craig Topper2617dcc2014-04-15 06:32:26 +00004134 Constant *Prefix = nullptr;
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004135 Constant *Prologue = nullptr;
David Majnemerdad0a642014-06-27 18:19:56 +00004136 Comdat *C;
Chris Lattner3822f632009-01-02 08:05:26 +00004137
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004138 if (ParseArgumentList(ArgList, isVarArg) ||
Rafael Espindola563eb4b2011-01-25 19:09:56 +00004139 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
4140 &UnnamedAddrLoc) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004141 ParseFnAttributeValuePairs(FuncAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00004142 BuiltinLoc) ||
Chris Lattner3822f632009-01-02 08:05:26 +00004143 (EatIfPresent(lltok::kw_section) &&
4144 ParseStringConstant(Section)) ||
Rafael Espindola83a362c2015-01-06 22:55:16 +00004145 parseOptionalComdat(FunctionName, C) ||
Chris Lattner3822f632009-01-02 08:05:26 +00004146 ParseOptionalAlignment(Alignment) ||
4147 (EatIfPresent(lltok::kw_gc) &&
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00004148 ParseStringConstant(GC)) ||
4149 (EatIfPresent(lltok::kw_prefix) &&
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004150 ParseGlobalTypeAndValue(Prefix)) ||
4151 (EatIfPresent(lltok::kw_prologue) &&
4152 ParseGlobalTypeAndValue(Prologue)))
Chris Lattner3822f632009-01-02 08:05:26 +00004153 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004154
Michael Gottesman41748d72013-06-27 00:25:01 +00004155 if (FuncAttrs.contains(Attribute::Builtin))
4156 return Error(BuiltinLoc, "'builtin' attribute not valid on function");
Bill Wendling09bd1f72013-02-22 00:12:35 +00004157
Chris Lattnerac161bf2009-01-02 07:01:27 +00004158 // If the alignment was parsed as an attribute, move to the alignment field.
Bill Wendlingc6daefa2012-10-08 23:27:46 +00004159 if (FuncAttrs.hasAlignmentAttr()) {
Bill Wendling9be77592012-09-21 15:26:31 +00004160 Alignment = FuncAttrs.getAlignment();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004161 FuncAttrs.removeAttribute(Attribute::Alignment);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004162 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004163
Chris Lattnerac161bf2009-01-02 07:01:27 +00004164 // Okay, if we got here, the function is syntactically valid. Convert types
4165 // and do semantic checks.
Jay Foadb804a2b2011-07-12 14:06:48 +00004166 std::vector<Type*> ParamTypeList;
Bill Wendlingf5075a42013-01-27 02:24:02 +00004167 SmallVector<AttributeSet, 8> Attrs;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004168
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004169 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004170 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4171 AttributeSet::ReturnIndex,
4172 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004173
Chris Lattnerac161bf2009-01-02 07:01:27 +00004174 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004175 ParamTypeList.push_back(ArgList[i].Ty);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004176 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4177 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004178 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4179 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004180 }
4181
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004182 if (FuncAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004183 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4184 AttributeSet::FunctionIndex,
4185 FuncAttrs));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004186
Bill Wendlinge94d8432012-12-07 23:16:57 +00004187 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004188
Bill Wendling749a43d2012-12-30 13:50:49 +00004189 if (PAL.hasAttribute(1, Attribute::StructRet) && !RetType->isVoidTy())
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004190 return Error(RetTypeLoc, "functions with 'sret' argument must return void");
4191
Chris Lattner229907c2011-07-18 04:54:35 +00004192 FunctionType *FT =
Owen Anderson4056ca92009-07-29 22:17:13 +00004193 FunctionType::get(RetType, ParamTypeList, isVarArg);
Chris Lattner229907c2011-07-18 04:54:35 +00004194 PointerType *PFT = PointerType::getUnqual(FT);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004195
Craig Topper2617dcc2014-04-15 06:32:26 +00004196 Fn = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004197 if (!FunctionName.empty()) {
4198 // If this was a definition of a forward reference, remove the definition
4199 // from the forward reference table and fill in the forward ref.
4200 std::map<std::string, std::pair<GlobalValue*, LocTy> >::iterator FRVI =
4201 ForwardRefVals.find(FunctionName);
4202 if (FRVI != ForwardRefVals.end()) {
4203 Fn = M->getFunction(FunctionName);
Nick Lewycky686d7cb2012-10-11 00:38:25 +00004204 if (!Fn)
4205 return Error(FRVI->second.second, "invalid forward reference to "
4206 "function as global value!");
Chris Lattnerc239eb72010-04-20 04:49:11 +00004207 if (Fn->getType() != PFT)
4208 return Error(FRVI->second.second, "invalid forward reference to "
4209 "function '" + FunctionName + "' with wrong type!");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004210
Chris Lattnerac161bf2009-01-02 07:01:27 +00004211 ForwardRefVals.erase(FRVI);
4212 } else if ((Fn = M->getFunction(FunctionName))) {
Chris Lattner5756c162011-06-17 07:06:44 +00004213 // Reject redefinitions.
4214 return Error(NameLoc, "invalid redefinition of function '" +
4215 FunctionName + "'");
Chris Lattnere38317f2009-10-25 23:22:50 +00004216 } else if (M->getNamedValue(FunctionName)) {
4217 return Error(NameLoc, "redefinition of function '@" + FunctionName + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004218 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004219
Dan Gohman399d6ae2009-08-29 23:37:49 +00004220 } else {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004221 // If this is a definition of a forward referenced function, make sure the
4222 // types agree.
4223 std::map<unsigned, std::pair<GlobalValue*, LocTy> >::iterator I
4224 = ForwardRefValIDs.find(NumberedVals.size());
4225 if (I != ForwardRefValIDs.end()) {
4226 Fn = cast<Function>(I->second.first);
4227 if (Fn->getType() != PFT)
4228 return Error(NameLoc, "type of definition and forward reference of '@" +
Benjamin Kramerc7583112010-09-27 17:42:11 +00004229 Twine(NumberedVals.size()) + "' disagree");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004230 ForwardRefValIDs.erase(I);
4231 }
4232 }
4233
Craig Topper2617dcc2014-04-15 06:32:26 +00004234 if (!Fn)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004235 Fn = Function::Create(FT, GlobalValue::ExternalLinkage, FunctionName, M);
4236 else // Move the forward-reference to the correct spot in the module.
4237 M->getFunctionList().splice(M->end(), M->getFunctionList(), Fn);
4238
4239 if (FunctionName.empty())
4240 NumberedVals.push_back(Fn);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004241
Chris Lattnerac161bf2009-01-02 07:01:27 +00004242 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
4243 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
Nico Rieck7157bb72014-01-14 15:22:47 +00004244 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004245 Fn->setCallingConv(CC);
4246 Fn->setAttributes(PAL);
Rafael Espindola45e6c192011-01-08 16:42:36 +00004247 Fn->setUnnamedAddr(UnnamedAddr);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004248 Fn->setAlignment(Alignment);
4249 Fn->setSection(Section);
David Majnemerdad0a642014-06-27 18:19:56 +00004250 Fn->setComdat(C);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004251 if (!GC.empty()) Fn->setGC(GC.c_str());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +00004252 Fn->setPrefixData(Prefix);
Peter Collingbourne51d2de72014-12-03 02:08:38 +00004253 Fn->setPrologueData(Prologue);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004254 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004255
Chris Lattnerac161bf2009-01-02 07:01:27 +00004256 // Add all of the arguments we parsed to the function.
4257 Function::arg_iterator ArgIt = Fn->arg_begin();
4258 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
4259 // If the argument has a name, insert it into the argument symbol table.
4260 if (ArgList[i].Name.empty()) continue;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004261
Chris Lattnerac161bf2009-01-02 07:01:27 +00004262 // Set the name, if it conflicted, it will be auto-renamed.
4263 ArgIt->setName(ArgList[i].Name);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004264
Benjamin Kramer1dc34b42010-10-16 11:28:23 +00004265 if (ArgIt->getName() != ArgList[i].Name)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004266 return Error(ArgList[i].Loc, "redefinition of argument '%" +
4267 ArgList[i].Name + "'");
4268 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004269
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004270 if (isDefine)
4271 return false;
4272
Robin Morisset039781e2014-08-29 21:53:01 +00004273 // Check the declaration has no block address forward references.
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004274 ValID ID;
4275 if (FunctionName.empty()) {
4276 ID.Kind = ValID::t_GlobalID;
4277 ID.UIntVal = NumberedVals.size() - 1;
4278 } else {
4279 ID.Kind = ValID::t_GlobalName;
4280 ID.StrVal = FunctionName;
4281 }
4282 auto Blocks = ForwardRefBlockAddresses.find(ID);
4283 if (Blocks != ForwardRefBlockAddresses.end())
4284 return Error(Blocks->first.Loc,
4285 "cannot take blockaddress inside a declaration");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004286 return false;
4287}
4288
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004289bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
4290 ValID ID;
4291 if (FunctionNumber == -1) {
4292 ID.Kind = ValID::t_GlobalName;
4293 ID.StrVal = F.getName();
4294 } else {
4295 ID.Kind = ValID::t_GlobalID;
4296 ID.UIntVal = FunctionNumber;
4297 }
4298
4299 auto Blocks = P.ForwardRefBlockAddresses.find(ID);
4300 if (Blocks == P.ForwardRefBlockAddresses.end())
4301 return false;
4302
4303 for (const auto &I : Blocks->second) {
4304 const ValID &BBID = I.first;
4305 GlobalValue *GV = I.second;
4306
4307 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
4308 "Expected local id or name");
4309 BasicBlock *BB;
4310 if (BBID.Kind == ValID::t_LocalName)
4311 BB = GetBB(BBID.StrVal, BBID.Loc);
4312 else
4313 BB = GetBB(BBID.UIntVal, BBID.Loc);
4314 if (!BB)
4315 return P.Error(BBID.Loc, "referenced value is not a basic block");
4316
4317 GV->replaceAllUsesWith(BlockAddress::get(&F, BB));
4318 GV->eraseFromParent();
4319 }
4320
4321 P.ForwardRefBlockAddresses.erase(Blocks);
4322 return false;
4323}
Chris Lattnerac161bf2009-01-02 07:01:27 +00004324
4325/// ParseFunctionBody
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004326/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
Chris Lattnerac161bf2009-01-02 07:01:27 +00004327bool LLParser::ParseFunctionBody(Function &Fn) {
Chris Lattner4649a732011-06-17 06:42:57 +00004328 if (Lex.getKind() != lltok::lbrace)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004329 return TokError("expected '{' in function body");
4330 Lex.Lex(); // eat the {.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004331
Chris Lattner3432c622009-10-28 03:39:23 +00004332 int FunctionNumber = -1;
4333 if (!Fn.hasName()) FunctionNumber = NumberedVals.size()-1;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004334
Chris Lattner3432c622009-10-28 03:39:23 +00004335 PerFunctionState PFS(*this, Fn, FunctionNumber);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004336
Duncan P. N. Exon Smith17169902014-08-19 00:13:19 +00004337 // Resolve block addresses and allow basic blocks to be forward-declared
4338 // within this function.
4339 if (PFS.resolveForwardRefBlockAddresses())
4340 return true;
4341 SaveAndRestore<PerFunctionState *> ScopeExit(BlockAddressPFS, &PFS);
4342
Chris Lattnerbbddd962010-01-09 19:20:07 +00004343 // We need at least one basic block.
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004344 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
Chris Lattnerbbddd962010-01-09 19:20:07 +00004345 return TokError("function body requires at least one basic block");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004346
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004347 while (Lex.getKind() != lltok::rbrace &&
4348 Lex.getKind() != lltok::kw_uselistorder)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004349 if (ParseBasicBlock(PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004350
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00004351 while (Lex.getKind() != lltok::rbrace)
4352 if (ParseUseListOrder(&PFS))
4353 return true;
4354
Chris Lattnerac161bf2009-01-02 07:01:27 +00004355 // Eat the }.
4356 Lex.Lex();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004357
Chris Lattnerac161bf2009-01-02 07:01:27 +00004358 // Verify function is ok.
Chris Lattner3432c622009-10-28 03:39:23 +00004359 return PFS.FinishFunction();
Chris Lattnerac161bf2009-01-02 07:01:27 +00004360}
4361
4362/// ParseBasicBlock
4363/// ::= LabelStr? Instruction*
4364bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
4365 // If this basic block starts out with a name, remember it.
4366 std::string Name;
4367 LocTy NameLoc = Lex.getLoc();
4368 if (Lex.getKind() == lltok::LabelStr) {
4369 Name = Lex.getStrVal();
4370 Lex.Lex();
4371 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004372
Chris Lattnerac161bf2009-01-02 07:01:27 +00004373 BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
Owen Anderson576a9a22015-03-02 05:25:09 +00004374 if (!BB)
4375 return Error(NameLoc,
4376 "unable to create block named '" + Name + "'");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004377
Chris Lattnerac161bf2009-01-02 07:01:27 +00004378 std::string NameStr;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004379
Chris Lattnerac161bf2009-01-02 07:01:27 +00004380 // Parse the instructions in this block until we get a terminator.
4381 Instruction *Inst;
4382 do {
4383 // This instruction may have three possibilities for a name: a) none
4384 // specified, b) name specified "%foo =", c) number specified: "%4 =".
4385 LocTy NameLoc = Lex.getLoc();
4386 int NameID = -1;
4387 NameStr = "";
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004388
Chris Lattnerac161bf2009-01-02 07:01:27 +00004389 if (Lex.getKind() == lltok::LocalVarID) {
4390 NameID = Lex.getUIntVal();
4391 Lex.Lex();
4392 if (ParseToken(lltok::equal, "expected '=' after instruction id"))
4393 return true;
Chris Lattnerdef19492011-06-17 06:36:20 +00004394 } else if (Lex.getKind() == lltok::LocalVar) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004395 NameStr = Lex.getStrVal();
4396 Lex.Lex();
4397 if (ParseToken(lltok::equal, "expected '=' after instruction name"))
4398 return true;
4399 }
Devang Patelea8a4b92009-09-17 23:04:48 +00004400
Chris Lattner77b89dc2009-12-30 05:23:43 +00004401 switch (ParseInstruction(Inst, BB, PFS)) {
Craig Toppera2886c22012-02-07 05:05:23 +00004402 default: llvm_unreachable("Unknown ParseInstruction result!");
Chris Lattner77b89dc2009-12-30 05:23:43 +00004403 case InstError: return true;
4404 case InstNormal:
Chris Lattner2e664bd2010-04-07 04:08:57 +00004405 BB->getInstList().push_back(Inst);
4406
Chris Lattner77b89dc2009-12-30 05:23:43 +00004407 // With a normal result, we check to see if the instruction is followed by
4408 // a comma and metadata.
4409 if (EatIfPresent(lltok::comma))
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00004410 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00004411 return true;
4412 break;
4413 case InstExtraComma:
Chris Lattner2e664bd2010-04-07 04:08:57 +00004414 BB->getInstList().push_back(Inst);
4415
Chris Lattner77b89dc2009-12-30 05:23:43 +00004416 // If the instruction parser ate an extra comma at the end of it, it
4417 // *must* be followed by metadata.
Duncan P. N. Exon Smith27d702c2015-04-24 21:29:36 +00004418 if (ParseInstructionMetadata(*Inst))
Chris Lattner77b89dc2009-12-30 05:23:43 +00004419 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004420 break;
Chris Lattner77b89dc2009-12-30 05:23:43 +00004421 }
Devang Patelea8a4b92009-09-17 23:04:48 +00004422
Chris Lattnerac161bf2009-01-02 07:01:27 +00004423 // Set the name on the instruction.
4424 if (PFS.SetInstName(NameID, NameStr, NameLoc, Inst)) return true;
4425 } while (!isa<TerminatorInst>(Inst));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004426
Chris Lattnerac161bf2009-01-02 07:01:27 +00004427 return false;
4428}
4429
4430//===----------------------------------------------------------------------===//
4431// Instruction Parsing.
4432//===----------------------------------------------------------------------===//
4433
4434/// ParseInstruction - Parse one of the many different instructions.
4435///
Chris Lattner77b89dc2009-12-30 05:23:43 +00004436int LLParser::ParseInstruction(Instruction *&Inst, BasicBlock *BB,
4437 PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004438 lltok::Kind Token = Lex.getKind();
4439 if (Token == lltok::Eof)
4440 return TokError("found end of file when expecting more instructions");
4441 LocTy Loc = Lex.getLoc();
Chris Lattner89d856e2009-03-01 00:53:13 +00004442 unsigned KeywordVal = Lex.getUIntVal();
Chris Lattnerac161bf2009-01-02 07:01:27 +00004443 Lex.Lex(); // Eat the keyword.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004444
Chris Lattnerac161bf2009-01-02 07:01:27 +00004445 switch (Token) {
4446 default: return Error(Loc, "expected instruction opcode");
4447 // Terminator Instructions.
Owen Anderson55f1c092009-08-13 21:58:54 +00004448 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004449 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
4450 case lltok::kw_br: return ParseBr(Inst, PFS);
4451 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004452 case lltok::kw_indirectbr: return ParseIndirectBr(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004453 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
Bill Wendlingf891bf82011-07-31 06:30:59 +00004454 case lltok::kw_resume: return ParseResume(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004455 // Binary Operators.
4456 case lltok::kw_add:
4457 case lltok::kw_sub:
Chris Lattnera676c0f2011-02-07 16:40:21 +00004458 case lltok::kw_mul:
4459 case lltok::kw_shl: {
Chris Lattnera676c0f2011-02-07 16:40:21 +00004460 bool NUW = EatIfPresent(lltok::kw_nuw);
4461 bool NSW = EatIfPresent(lltok::kw_nsw);
4462 if (!NUW) NUW = EatIfPresent(lltok::kw_nuw);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004463
Chris Lattnera676c0f2011-02-07 16:40:21 +00004464 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004465
Chris Lattnera676c0f2011-02-07 16:40:21 +00004466 if (NUW) cast<BinaryOperator>(Inst)->setHasNoUnsignedWrap(true);
4467 if (NSW) cast<BinaryOperator>(Inst)->setHasNoSignedWrap(true);
4468 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00004469 }
Dan Gohmana5b96452009-06-04 22:49:04 +00004470 case lltok::kw_fadd:
4471 case lltok::kw_fsub:
Michael Ilseman92053172012-11-27 00:42:44 +00004472 case lltok::kw_fmul:
4473 case lltok::kw_fdiv:
4474 case lltok::kw_frem: {
4475 FastMathFlags FMF = EatFastMathFlagsIfPresent();
4476 int Res = ParseArithmetic(Inst, PFS, KeywordVal, 2);
4477 if (Res != 0)
4478 return Res;
4479 if (FMF.any())
4480 Inst->setFastMathFlags(FMF);
4481 return 0;
4482 }
Dan Gohmana5b96452009-06-04 22:49:04 +00004483
Chris Lattner35315d02011-02-06 21:44:57 +00004484 case lltok::kw_sdiv:
Chris Lattnera676c0f2011-02-07 16:40:21 +00004485 case lltok::kw_udiv:
4486 case lltok::kw_lshr:
4487 case lltok::kw_ashr: {
4488 bool Exact = EatIfPresent(lltok::kw_exact);
4489
4490 if (ParseArithmetic(Inst, PFS, KeywordVal, 1)) return true;
4491 if (Exact) cast<BinaryOperator>(Inst)->setIsExact(true);
4492 return false;
Dan Gohman9c7f8082009-07-27 16:11:46 +00004493 }
4494
Chris Lattnerac161bf2009-01-02 07:01:27 +00004495 case lltok::kw_urem:
Chris Lattner89d856e2009-03-01 00:53:13 +00004496 case lltok::kw_srem: return ParseArithmetic(Inst, PFS, KeywordVal, 1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004497 case lltok::kw_and:
4498 case lltok::kw_or:
Chris Lattner89d856e2009-03-01 00:53:13 +00004499 case lltok::kw_xor: return ParseLogical(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004500 case lltok::kw_icmp:
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004501 case lltok::kw_fcmp: return ParseCompare(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004502 // Casts.
4503 case lltok::kw_trunc:
4504 case lltok::kw_zext:
4505 case lltok::kw_sext:
4506 case lltok::kw_fptrunc:
4507 case lltok::kw_fpext:
4508 case lltok::kw_bitcast:
Matt Arsenaultb03bd4d2013-11-15 01:34:59 +00004509 case lltok::kw_addrspacecast:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004510 case lltok::kw_uitofp:
4511 case lltok::kw_sitofp:
4512 case lltok::kw_fptoui:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004513 case lltok::kw_fptosi:
Chris Lattnerac161bf2009-01-02 07:01:27 +00004514 case lltok::kw_inttoptr:
Chris Lattner89d856e2009-03-01 00:53:13 +00004515 case lltok::kw_ptrtoint: return ParseCast(Inst, PFS, KeywordVal);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004516 // Other.
4517 case lltok::kw_select: return ParseSelect(Inst, PFS);
Chris Lattnerb55ab542009-01-05 08:18:44 +00004518 case lltok::kw_va_arg: return ParseVA_Arg(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004519 case lltok::kw_extractelement: return ParseExtractElement(Inst, PFS);
4520 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
4521 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
4522 case lltok::kw_phi: return ParsePHI(Inst, PFS);
Bill Wendlingfae14752011-08-12 20:24:12 +00004523 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
Reid Kleckner5772b772014-04-24 20:14:34 +00004524 // Call.
4525 case lltok::kw_call: return ParseCall(Inst, PFS, CallInst::TCK_None);
4526 case lltok::kw_tail: return ParseCall(Inst, PFS, CallInst::TCK_Tail);
4527 case lltok::kw_musttail: return ParseCall(Inst, PFS, CallInst::TCK_MustTail);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004528 // Memory.
Victor Hernandezc7d6a832009-10-17 00:00:19 +00004529 case lltok::kw_alloca: return ParseAlloc(Inst, PFS);
Chris Lattnerbc639292011-11-27 06:56:53 +00004530 case lltok::kw_load: return ParseLoad(Inst, PFS);
4531 case lltok::kw_store: return ParseStore(Inst, PFS);
Eli Friedman02e737b2011-08-12 22:50:01 +00004532 case lltok::kw_cmpxchg: return ParseCmpXchg(Inst, PFS);
4533 case lltok::kw_atomicrmw: return ParseAtomicRMW(Inst, PFS);
Eli Friedmanfee02c62011-07-25 23:16:38 +00004534 case lltok::kw_fence: return ParseFence(Inst, PFS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004535 case lltok::kw_getelementptr: return ParseGetElementPtr(Inst, PFS);
4536 case lltok::kw_extractvalue: return ParseExtractValue(Inst, PFS);
4537 case lltok::kw_insertvalue: return ParseInsertValue(Inst, PFS);
4538 }
4539}
4540
4541/// ParseCmpPredicate - Parse an integer or fp predicate, based on Kind.
4542bool LLParser::ParseCmpPredicate(unsigned &P, unsigned Opc) {
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004543 if (Opc == Instruction::FCmp) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004544 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00004545 default: return TokError("expected fcmp predicate (e.g. 'oeq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004546 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
4547 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
4548 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
4549 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
4550 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
4551 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
4552 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
4553 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
4554 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
4555 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
4556 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
4557 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
4558 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
4559 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
4560 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
4561 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
4562 }
4563 } else {
4564 switch (Lex.getKind()) {
David Tweeda11edf02013-01-07 13:32:38 +00004565 default: return TokError("expected icmp predicate (e.g. 'eq')");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004566 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
4567 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
4568 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
4569 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
4570 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
4571 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
4572 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
4573 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
4574 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
4575 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
4576 }
4577 }
4578 Lex.Lex();
4579 return false;
4580}
4581
4582//===----------------------------------------------------------------------===//
4583// Terminator Instructions.
4584//===----------------------------------------------------------------------===//
4585
4586/// ParseRet - Parse a return instruction.
Chris Lattner596760d2009-12-29 21:25:40 +00004587/// ::= 'ret' void (',' !dbg, !1)*
4588/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
Chris Lattner33de4272011-06-17 06:49:41 +00004589bool LLParser::ParseRet(Instruction *&Inst, BasicBlock *BB,
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004590 PerFunctionState &PFS) {
4591 SMLoc TypeLoc = Lex.getLoc();
Craig Topper2617dcc2014-04-15 06:32:26 +00004592 Type *Ty = nullptr;
Chris Lattnerf880ca22009-03-09 04:49:14 +00004593 if (ParseType(Ty, true /*void allowed*/)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004594
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004595 Type *ResType = PFS.getFunction().getReturnType();
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004596
Chris Lattnerfdd87902009-10-05 05:54:46 +00004597 if (Ty->isVoidTy()) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004598 if (!ResType->isVoidTy())
4599 return Error(TypeLoc, "value doesn't match function result type '" +
4600 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004601
Owen Anderson55f1c092009-08-13 21:58:54 +00004602 Inst = ReturnInst::Create(Context);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004603 return false;
4604 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004605
Chris Lattnerac161bf2009-01-02 07:01:27 +00004606 Value *RV;
4607 if (ParseValue(Ty, RV, PFS)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004608
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004609 if (ResType != RV->getType())
4610 return Error(TypeLoc, "value doesn't match function result type '" +
4611 getTypeString(ResType) + "'");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004612
Owen Anderson55f1c092009-08-13 21:58:54 +00004613 Inst = ReturnInst::Create(Context, RV);
Chris Lattner33de4272011-06-17 06:49:41 +00004614 return false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004615}
4616
4617
4618/// ParseBr
4619/// ::= 'br' TypeAndValue
4620/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4621bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
4622 LocTy Loc, Loc2;
Chris Lattner3ed871f2009-10-27 19:13:16 +00004623 Value *Op0;
4624 BasicBlock *Op1, *Op2;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004625 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004626
Chris Lattnerac161bf2009-01-02 07:01:27 +00004627 if (BasicBlock *BB = dyn_cast<BasicBlock>(Op0)) {
4628 Inst = BranchInst::Create(BB);
4629 return false;
4630 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004631
Owen Anderson55f1c092009-08-13 21:58:54 +00004632 if (Op0->getType() != Type::getInt1Ty(Context))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004633 return Error(Loc, "branch condition must have 'i1' type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004634
Chris Lattnerac161bf2009-01-02 07:01:27 +00004635 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004636 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004637 ParseToken(lltok::comma, "expected ',' after true destination") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004638 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004639 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004640
Chris Lattner3ed871f2009-10-27 19:13:16 +00004641 Inst = BranchInst::Create(Op1, Op2, Op0);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004642 return false;
4643}
4644
4645/// ParseSwitch
4646/// Instruction
4647/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
4648/// JumpTable
4649/// ::= (TypeAndValue ',' TypeAndValue)*
4650bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
4651 LocTy CondLoc, BBLoc;
Chris Lattner3ed871f2009-10-27 19:13:16 +00004652 Value *Cond;
4653 BasicBlock *DefaultBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004654 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
4655 ParseToken(lltok::comma, "expected ',' after switch condition") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004656 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004657 ParseToken(lltok::lsquare, "expected '[' with switch table"))
4658 return true;
4659
Duncan Sands19d0b472010-02-16 11:11:14 +00004660 if (!Cond->getType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004661 return Error(CondLoc, "switch condition must have integer type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004662
Chris Lattnerac161bf2009-01-02 07:01:27 +00004663 // Parse the jump table pairs.
4664 SmallPtrSet<Value*, 32> SeenCases;
4665 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
4666 while (Lex.getKind() != lltok::rsquare) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00004667 Value *Constant;
4668 BasicBlock *DestBB;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004669
Chris Lattnerac161bf2009-01-02 07:01:27 +00004670 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
4671 ParseToken(lltok::comma, "expected ',' after case value") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004672 ParseTypeAndBasicBlock(DestBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004673 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004674
David Blaikie70573dc2014-11-19 07:49:26 +00004675 if (!SeenCases.insert(Constant).second)
Chris Lattnerac161bf2009-01-02 07:01:27 +00004676 return Error(CondLoc, "duplicate case value in switch");
4677 if (!isa<ConstantInt>(Constant))
4678 return Error(CondLoc, "case value is not a constant integer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004679
Chris Lattner3ed871f2009-10-27 19:13:16 +00004680 Table.push_back(std::make_pair(cast<ConstantInt>(Constant), DestBB));
Chris Lattnerac161bf2009-01-02 07:01:27 +00004681 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004682
Chris Lattnerac161bf2009-01-02 07:01:27 +00004683 Lex.Lex(); // Eat the ']'.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004684
Chris Lattner3ed871f2009-10-27 19:13:16 +00004685 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00004686 for (unsigned i = 0, e = Table.size(); i != e; ++i)
4687 SI->addCase(Table[i].first, Table[i].second);
4688 Inst = SI;
4689 return false;
4690}
4691
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004692/// ParseIndirectBr
Chris Lattner3ed871f2009-10-27 19:13:16 +00004693/// Instruction
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004694/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
4695bool LLParser::ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattner3ed871f2009-10-27 19:13:16 +00004696 LocTy AddrLoc;
4697 Value *Address;
4698 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004699 ParseToken(lltok::comma, "expected ',' after indirectbr address") ||
4700 ParseToken(lltok::lsquare, "expected '[' with indirectbr"))
Chris Lattner3ed871f2009-10-27 19:13:16 +00004701 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004702
Duncan Sands19d0b472010-02-16 11:11:14 +00004703 if (!Address->getType()->isPointerTy())
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004704 return Error(AddrLoc, "indirectbr address must have pointer type");
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004705
Chris Lattner3ed871f2009-10-27 19:13:16 +00004706 // Parse the destination list.
4707 SmallVector<BasicBlock*, 16> DestList;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004708
Chris Lattner3ed871f2009-10-27 19:13:16 +00004709 if (Lex.getKind() != lltok::rsquare) {
4710 BasicBlock *DestBB;
4711 if (ParseTypeAndBasicBlock(DestBB, PFS))
4712 return true;
4713 DestList.push_back(DestBB);
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004714
Chris Lattner3ed871f2009-10-27 19:13:16 +00004715 while (EatIfPresent(lltok::comma)) {
4716 if (ParseTypeAndBasicBlock(DestBB, PFS))
4717 return true;
4718 DestList.push_back(DestBB);
4719 }
4720 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00004721
Chris Lattner3ed871f2009-10-27 19:13:16 +00004722 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
4723 return true;
4724
Chris Lattnerd04cb6d2009-10-28 00:19:10 +00004725 IndirectBrInst *IBI = IndirectBrInst::Create(Address, DestList.size());
Chris Lattner3ed871f2009-10-27 19:13:16 +00004726 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
4727 IBI->addDestination(DestList[i]);
4728 Inst = IBI;
4729 return false;
4730}
4731
4732
Chris Lattnerac161bf2009-01-02 07:01:27 +00004733/// ParseInvoke
4734/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
4735/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
4736bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
4737 LocTy CallLoc = Lex.getLoc();
Bill Wendling50d27842012-10-15 20:35:56 +00004738 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00004739 std::vector<unsigned> FwdRefAttrGrps;
Bill Wendling09bd1f72013-02-22 00:12:35 +00004740 LocTy NoBuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00004741 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00004742 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004743 LocTy RetTypeLoc;
4744 ValID CalleeID;
4745 SmallVector<ParamInfo, 16> ArgList;
4746
Chris Lattner3ed871f2009-10-27 19:13:16 +00004747 BasicBlock *NormalBB, *UnwindBB;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004748 if (ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00004749 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00004750 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004751 ParseValID(CalleeID) ||
4752 ParseParameterList(ArgList, PFS) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00004753 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
4754 NoBuiltinLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004755 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004756 ParseTypeAndBasicBlock(NormalBB, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00004757 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
Chris Lattner3ed871f2009-10-27 19:13:16 +00004758 ParseTypeAndBasicBlock(UnwindBB, PFS))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004759 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004760
Chris Lattnerac161bf2009-01-02 07:01:27 +00004761 // If RetType is a non-function pointer type, then this is the short syntax
4762 // for the call, which means that RetType is just the return type. Infer the
4763 // rest of the function argument types from the arguments that are present.
David Blaikie445e3fb2015-04-24 19:32:54 +00004764 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
4765 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004766 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00004767 std::vector<Type*> ParamTypes;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004768 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
4769 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004770
Chris Lattnerac161bf2009-01-02 07:01:27 +00004771 if (!FunctionType::isValidReturnType(RetType))
4772 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004773
Owen Anderson4056ca92009-07-29 22:17:13 +00004774 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004775 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004776
Chris Lattnerac161bf2009-01-02 07:01:27 +00004777 // Look up the callee.
4778 Value *Callee;
David Blaikie445e3fb2015-04-24 19:32:54 +00004779 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
4780 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004781
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004782 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00004783 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00004784 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00004785 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4786 AttributeSet::ReturnIndex,
4787 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004788
Chris Lattnerac161bf2009-01-02 07:01:27 +00004789 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004790
Chris Lattnerac161bf2009-01-02 07:01:27 +00004791 // Loop through FunctionType's arguments and ensure they are specified
4792 // correctly. Also, gather any parameter attributes.
4793 FunctionType::param_iterator I = Ty->param_begin();
4794 FunctionType::param_iterator E = Ty->param_end();
4795 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00004796 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004797 if (I != E) {
4798 ExpectedTy = *I++;
4799 } else if (!Ty->isVarArg()) {
4800 return Error(ArgList[i].Loc, "too many arguments specified");
4801 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004802
Chris Lattnerac161bf2009-01-02 07:01:27 +00004803 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
4804 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004805 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004806 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00004807 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
4808 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00004809 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
4810 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004811 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004812
Chris Lattnerac161bf2009-01-02 07:01:27 +00004813 if (I != E)
4814 return Error(CallLoc, "not enough parameters specified for call");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004815
David Majnemer8d22abd2015-02-23 00:01:32 +00004816 if (FnAttrs.hasAttributes()) {
4817 if (FnAttrs.hasAlignmentAttr())
4818 return Error(CallLoc, "invoke instructions may not have an alignment");
4819
Bill Wendlingf5075a42013-01-27 02:24:02 +00004820 Attrs.push_back(AttributeSet::get(RetType->getContext(),
4821 AttributeSet::FunctionIndex,
4822 FnAttrs));
David Majnemer8d22abd2015-02-23 00:01:32 +00004823 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004824
Bill Wendling3d7b0b82012-12-19 07:18:57 +00004825 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00004826 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004827
David Blaikie3e807092015-05-13 18:35:26 +00004828 InvokeInst *II = InvokeInst::Create(Ty, Callee, NormalBB, UnwindBB, Args);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004829 II->setCallingConv(CC);
4830 II->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00004831 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004832 Inst = II;
4833 return false;
4834}
4835
Bill Wendlingf891bf82011-07-31 06:30:59 +00004836/// ParseResume
4837/// ::= 'resume' TypeAndValue
4838bool LLParser::ParseResume(Instruction *&Inst, PerFunctionState &PFS) {
4839 Value *Exn; LocTy ExnLoc;
Bill Wendlingf891bf82011-07-31 06:30:59 +00004840 if (ParseTypeAndValue(Exn, ExnLoc, PFS))
4841 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004842
Bill Wendlingf891bf82011-07-31 06:30:59 +00004843 ResumeInst *RI = ResumeInst::Create(Exn);
4844 Inst = RI;
4845 return false;
4846}
Chris Lattnerac161bf2009-01-02 07:01:27 +00004847
4848//===----------------------------------------------------------------------===//
4849// Binary Operators.
4850//===----------------------------------------------------------------------===//
4851
4852/// ParseArithmetic
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004853/// ::= ArithmeticOps TypeAndValue ',' Value
4854///
4855/// If OperandType is 0, then any FP or integer operand is allowed. If it is 1,
4856/// then any integer operand is allowed, if it is 2, any fp operand is allowed.
Chris Lattnerac161bf2009-01-02 07:01:27 +00004857bool LLParser::ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004858 unsigned Opc, unsigned OperandType) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004859 LocTy Loc; Value *LHS, *RHS;
4860 if (ParseTypeAndValue(LHS, Loc, PFS) ||
4861 ParseToken(lltok::comma, "expected ',' in arithmetic operation") ||
4862 ParseValue(LHS->getType(), RHS, PFS))
4863 return true;
4864
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004865 bool Valid;
4866 switch (OperandType) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00004867 default: llvm_unreachable("Unknown operand type!");
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004868 case 0: // int or FP.
Duncan Sands9dff9be2010-02-15 16:12:20 +00004869 Valid = LHS->getType()->isIntOrIntVectorTy() ||
4870 LHS->getType()->isFPOrFPVectorTy();
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004871 break;
Duncan Sands9dff9be2010-02-15 16:12:20 +00004872 case 1: Valid = LHS->getType()->isIntOrIntVectorTy(); break;
4873 case 2: Valid = LHS->getType()->isFPOrFPVectorTy(); break;
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004874 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004875
Chris Lattnereeefa9a2009-01-05 08:24:46 +00004876 if (!Valid)
4877 return Error(Loc, "invalid operand type for instruction");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004878
Chris Lattnerac161bf2009-01-02 07:01:27 +00004879 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4880 return false;
4881}
4882
4883/// ParseLogical
4884/// ::= ArithmeticOps TypeAndValue ',' Value {
4885bool LLParser::ParseLogical(Instruction *&Inst, PerFunctionState &PFS,
4886 unsigned Opc) {
4887 LocTy Loc; Value *LHS, *RHS;
4888 if (ParseTypeAndValue(LHS, Loc, PFS) ||
4889 ParseToken(lltok::comma, "expected ',' in logical operation") ||
4890 ParseValue(LHS->getType(), RHS, PFS))
4891 return true;
4892
Duncan Sands9dff9be2010-02-15 16:12:20 +00004893 if (!LHS->getType()->isIntOrIntVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004894 return Error(Loc,"instruction requires integer or integer vector operands");
4895
4896 Inst = BinaryOperator::Create((Instruction::BinaryOps)Opc, LHS, RHS);
4897 return false;
4898}
4899
4900
4901/// ParseCompare
4902/// ::= 'icmp' IPredicates TypeAndValue ',' Value
4903/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00004904bool LLParser::ParseCompare(Instruction *&Inst, PerFunctionState &PFS,
4905 unsigned Opc) {
4906 // Parse the integer/fp comparison predicate.
4907 LocTy Loc;
4908 unsigned Pred;
4909 Value *LHS, *RHS;
4910 if (ParseCmpPredicate(Pred, Opc) ||
4911 ParseTypeAndValue(LHS, Loc, PFS) ||
4912 ParseToken(lltok::comma, "expected ',' after compare value") ||
4913 ParseValue(LHS->getType(), RHS, PFS))
4914 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004915
Chris Lattnerac161bf2009-01-02 07:01:27 +00004916 if (Opc == Instruction::FCmp) {
Duncan Sands9dff9be2010-02-15 16:12:20 +00004917 if (!LHS->getType()->isFPOrFPVectorTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004918 return Error(Loc, "fcmp requires floating point operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00004919 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Nick Lewyckya21d3da2009-07-08 03:04:38 +00004920 } else {
4921 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
Duncan Sands9dff9be2010-02-15 16:12:20 +00004922 if (!LHS->getType()->isIntOrIntVectorTy() &&
Nadav Rotem3924cb02011-12-05 06:29:09 +00004923 !LHS->getType()->getScalarType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00004924 return Error(Loc, "icmp requires integer operands");
Dan Gohmanad1f0a12009-08-25 23:17:54 +00004925 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004926 }
4927 return false;
4928}
4929
4930//===----------------------------------------------------------------------===//
4931// Other Instructions.
4932//===----------------------------------------------------------------------===//
4933
4934
4935/// ParseCast
4936/// ::= CastOpc TypeAndValue 'to' Type
4937bool LLParser::ParseCast(Instruction *&Inst, PerFunctionState &PFS,
4938 unsigned Opc) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00004939 LocTy Loc;
4940 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004941 Type *DestTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004942 if (ParseTypeAndValue(Op, Loc, PFS) ||
4943 ParseToken(lltok::kw_to, "expected 'to' after cast value") ||
4944 ParseType(DestTy))
4945 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004946
Chris Lattner89d856e2009-03-01 00:53:13 +00004947 if (!CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy)) {
4948 CastInst::castIsValid((Instruction::CastOps)Opc, Op, DestTy);
Chris Lattnerac161bf2009-01-02 07:01:27 +00004949 return Error(Loc, "invalid cast opcode for cast from '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00004950 getTypeString(Op->getType()) + "' to '" +
4951 getTypeString(DestTy) + "'");
Chris Lattner89d856e2009-03-01 00:53:13 +00004952 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00004953 Inst = CastInst::Create((Instruction::CastOps)Opc, Op, DestTy);
4954 return false;
4955}
4956
4957/// ParseSelect
4958/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
4959bool LLParser::ParseSelect(Instruction *&Inst, PerFunctionState &PFS) {
4960 LocTy Loc;
4961 Value *Op0, *Op1, *Op2;
4962 if (ParseTypeAndValue(Op0, Loc, PFS) ||
4963 ParseToken(lltok::comma, "expected ',' after select condition") ||
4964 ParseTypeAndValue(Op1, PFS) ||
4965 ParseToken(lltok::comma, "expected ',' after select value") ||
4966 ParseTypeAndValue(Op2, PFS))
4967 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004968
Chris Lattnerac161bf2009-01-02 07:01:27 +00004969 if (const char *Reason = SelectInst::areInvalidOperands(Op0, Op1, Op2))
4970 return Error(Loc, Reason);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004971
Chris Lattnerac161bf2009-01-02 07:01:27 +00004972 Inst = SelectInst::Create(Op0, Op1, Op2);
4973 return false;
4974}
4975
Chris Lattnerb55ab542009-01-05 08:18:44 +00004976/// ParseVA_Arg
4977/// ::= 'va_arg' TypeAndValue ',' Type
4978bool LLParser::ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00004979 Value *Op;
Craig Topper2617dcc2014-04-15 06:32:26 +00004980 Type *EltTy = nullptr;
Chris Lattnerb55ab542009-01-05 08:18:44 +00004981 LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00004982 if (ParseTypeAndValue(Op, PFS) ||
4983 ParseToken(lltok::comma, "expected ',' after vaarg operand") ||
Chris Lattnerb55ab542009-01-05 08:18:44 +00004984 ParseType(EltTy, TypeLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00004985 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00004986
Chris Lattnerb55ab542009-01-05 08:18:44 +00004987 if (!EltTy->isFirstClassType())
4988 return Error(TypeLoc, "va_arg requires operand with first class type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00004989
4990 Inst = new VAArgInst(Op, EltTy);
4991 return false;
4992}
4993
4994/// ParseExtractElement
4995/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
4996bool LLParser::ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
4997 LocTy Loc;
4998 Value *Op0, *Op1;
4999 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5000 ParseToken(lltok::comma, "expected ',' after extract value") ||
5001 ParseTypeAndValue(Op1, PFS))
5002 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005003
Chris Lattnerac161bf2009-01-02 07:01:27 +00005004 if (!ExtractElementInst::isValidOperands(Op0, Op1))
5005 return Error(Loc, "invalid extractelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005006
Eric Christopherc9742252009-07-25 02:28:41 +00005007 Inst = ExtractElementInst::Create(Op0, Op1);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005008 return false;
5009}
5010
5011/// ParseInsertElement
5012/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5013bool LLParser::ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
5014 LocTy Loc;
5015 Value *Op0, *Op1, *Op2;
5016 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5017 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
5018 ParseTypeAndValue(Op1, PFS) ||
5019 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
5020 ParseTypeAndValue(Op2, PFS))
5021 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005022
Chris Lattnerac161bf2009-01-02 07:01:27 +00005023 if (!InsertElementInst::isValidOperands(Op0, Op1, Op2))
Eric Christopherfef8db62009-07-23 01:01:32 +00005024 return Error(Loc, "invalid insertelement operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005025
Chris Lattnerac161bf2009-01-02 07:01:27 +00005026 Inst = InsertElementInst::Create(Op0, Op1, Op2);
5027 return false;
5028}
5029
5030/// ParseShuffleVector
5031/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
5032bool LLParser::ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
5033 LocTy Loc;
5034 Value *Op0, *Op1, *Op2;
5035 if (ParseTypeAndValue(Op0, Loc, PFS) ||
5036 ParseToken(lltok::comma, "expected ',' after shuffle mask") ||
5037 ParseTypeAndValue(Op1, PFS) ||
5038 ParseToken(lltok::comma, "expected ',' after shuffle value") ||
5039 ParseTypeAndValue(Op2, PFS))
5040 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005041
Chris Lattnerac161bf2009-01-02 07:01:27 +00005042 if (!ShuffleVectorInst::isValidOperands(Op0, Op1, Op2))
Pete Cooperc1a6f982012-02-01 23:43:12 +00005043 return Error(Loc, "invalid shufflevector operands");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005044
Chris Lattnerac161bf2009-01-02 07:01:27 +00005045 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
5046 return false;
5047}
5048
5049/// ParsePHI
Chris Lattnerc05471e2009-10-18 05:27:44 +00005050/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
Chris Lattnerf4f03422009-12-30 05:27:33 +00005051int LLParser::ParsePHI(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005052 Type *Ty = nullptr; LocTy TypeLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005053 Value *Op0, *Op1;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005054
Chris Lattnerb1ed91f2011-07-09 17:41:24 +00005055 if (ParseType(Ty, TypeLoc) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005056 ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
5057 ParseValue(Ty, Op0, PFS) ||
5058 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00005059 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005060 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
5061 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005062
Chris Lattnerf4f03422009-12-30 05:27:33 +00005063 bool AteExtraComma = false;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005064 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
5065 while (1) {
5066 PHIVals.push_back(std::make_pair(Op0, cast<BasicBlock>(Op1)));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005067
Chris Lattner3822f632009-01-02 08:05:26 +00005068 if (!EatIfPresent(lltok::comma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005069 break;
5070
Chris Lattnerf4f03422009-12-30 05:27:33 +00005071 if (Lex.getKind() == lltok::MetadataVar) {
5072 AteExtraComma = true;
Devang Patel8f842d32009-10-16 18:45:49 +00005073 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005074 }
Devang Patel8f842d32009-10-16 18:45:49 +00005075
Chris Lattner3822f632009-01-02 08:05:26 +00005076 if (ParseToken(lltok::lsquare, "expected '[' in phi value list") ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005077 ParseValue(Ty, Op0, PFS) ||
5078 ParseToken(lltok::comma, "expected ',' after insertelement value") ||
Owen Anderson55f1c092009-08-13 21:58:54 +00005079 ParseValue(Type::getLabelTy(Context), Op1, PFS) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005080 ParseToken(lltok::rsquare, "expected ']' in phi value list"))
5081 return true;
5082 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005083
Chris Lattnerac161bf2009-01-02 07:01:27 +00005084 if (!Ty->isFirstClassType())
5085 return Error(TypeLoc, "phi node must have first class type");
5086
Jay Foad52131342011-03-30 11:28:46 +00005087 PHINode *PN = PHINode::Create(Ty, PHIVals.size());
Chris Lattnerac161bf2009-01-02 07:01:27 +00005088 for (unsigned i = 0, e = PHIVals.size(); i != e; ++i)
5089 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
5090 Inst = PN;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005091 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005092}
5093
Bill Wendlingfae14752011-08-12 20:24:12 +00005094/// ParseLandingPad
5095/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
5096/// Clause
5097/// ::= 'catch' TypeAndValue
5098/// ::= 'filter'
5099/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
5100bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005101 Type *Ty = nullptr; LocTy TyLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00005102 Value *PersFn; LocTy PersFnLoc;
Bill Wendlingfae14752011-08-12 20:24:12 +00005103
5104 if (ParseType(Ty, TyLoc) ||
5105 ParseToken(lltok::kw_personality, "expected 'personality'") ||
5106 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
5107 return true;
5108
Owen Andersonf8f259d2015-03-09 07:13:42 +00005109 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(Ty, PersFn, 0));
Bill Wendlingfae14752011-08-12 20:24:12 +00005110 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
5111
5112 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
5113 LandingPadInst::ClauseType CT;
5114 if (EatIfPresent(lltok::kw_catch))
5115 CT = LandingPadInst::Catch;
5116 else if (EatIfPresent(lltok::kw_filter))
5117 CT = LandingPadInst::Filter;
5118 else
5119 return TokError("expected 'catch' or 'filter' clause type");
5120
Rafael Espindola4dc5dfc2014-06-04 18:51:31 +00005121 Value *V;
5122 LocTy VLoc;
Owen Andersonf8f259d2015-03-09 07:13:42 +00005123 if (ParseTypeAndValue(V, VLoc, PFS))
Bill Wendlingfae14752011-08-12 20:24:12 +00005124 return true;
Bill Wendlingfae14752011-08-12 20:24:12 +00005125
Bill Wendlinga52aa3c2011-08-12 20:52:25 +00005126 // A 'catch' type expects a non-array constant. A filter clause expects an
5127 // array constant.
5128 if (CT == LandingPadInst::Catch) {
5129 if (isa<ArrayType>(V->getType()))
5130 Error(VLoc, "'catch' clause has an invalid type");
5131 } else {
5132 if (!isa<ArrayType>(V->getType()))
5133 Error(VLoc, "'filter' clause has an invalid type");
5134 }
5135
Owen Andersonf8f259d2015-03-09 07:13:42 +00005136 Constant *CV = dyn_cast<Constant>(V);
5137 if (!CV)
5138 return Error(VLoc, "clause argument must be a constant");
5139 LP->addClause(CV);
Bill Wendlingfae14752011-08-12 20:24:12 +00005140 }
5141
Owen Andersonf8f259d2015-03-09 07:13:42 +00005142 Inst = LP.release();
Bill Wendlingfae14752011-08-12 20:24:12 +00005143 return false;
5144}
5145
Chris Lattnerac161bf2009-01-02 07:01:27 +00005146/// ParseCall
Reid Kleckner5772b772014-04-24 20:14:34 +00005147/// ::= 'call' OptionalCallingConv OptionalAttrs Type Value
5148/// ParameterList OptionalAttrs
5149/// ::= 'tail' 'call' OptionalCallingConv OptionalAttrs Type Value
5150/// ParameterList OptionalAttrs
5151/// ::= 'musttail' 'call' OptionalCallingConv OptionalAttrs Type Value
Chris Lattnerac161bf2009-01-02 07:01:27 +00005152/// ParameterList OptionalAttrs
5153bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
Reid Kleckner5772b772014-04-24 20:14:34 +00005154 CallInst::TailCallKind TCK) {
Bill Wendling50d27842012-10-15 20:35:56 +00005155 AttrBuilder RetAttrs, FnAttrs;
Bill Wendlingb32b0412013-02-08 06:32:06 +00005156 std::vector<unsigned> FwdRefAttrGrps;
Michael Gottesman41748d72013-06-27 00:25:01 +00005157 LocTy BuiltinLoc;
Alexey Samsonov17a9cff2014-09-10 18:00:17 +00005158 unsigned CC;
Craig Topper2617dcc2014-04-15 06:32:26 +00005159 Type *RetType = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005160 LocTy RetTypeLoc;
5161 ValID CalleeID;
5162 SmallVector<ParamInfo, 16> ArgList;
5163 LocTy CallLoc = Lex.getLoc();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005164
Reid Kleckner5772b772014-04-24 20:14:34 +00005165 if ((TCK != CallInst::TCK_None &&
5166 ParseToken(lltok::kw_call, "expected 'tail call'")) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005167 ParseOptionalCallingConv(CC) ||
Bill Wendling34c2eb22012-12-04 23:40:58 +00005168 ParseOptionalReturnAttrs(RetAttrs) ||
Chris Lattnerf880ca22009-03-09 04:49:14 +00005169 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
Chris Lattnerac161bf2009-01-02 07:01:27 +00005170 ParseValID(CalleeID) ||
Reid Kleckner83498642014-08-26 00:33:28 +00005171 ParseParameterList(ArgList, PFS, TCK == CallInst::TCK_MustTail,
5172 PFS.getFunction().isVarArg()) ||
Bill Wendling09bd1f72013-02-22 00:12:35 +00005173 ParseFnAttributeValuePairs(FnAttrs, FwdRefAttrGrps, false,
Michael Gottesman41748d72013-06-27 00:25:01 +00005174 BuiltinLoc))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005175 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005176
Chris Lattnerac161bf2009-01-02 07:01:27 +00005177 // If RetType is a non-function pointer type, then this is the short syntax
5178 // for the call, which means that RetType is just the return type. Infer the
5179 // rest of the function argument types from the arguments that are present.
David Blaikie23af6482015-04-16 23:24:18 +00005180 FunctionType *Ty = dyn_cast<FunctionType>(RetType);
5181 if (!Ty) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005182 // Pull out the types of all of the arguments...
Jay Foadb804a2b2011-07-12 14:06:48 +00005183 std::vector<Type*> ParamTypes;
Eli Friedman6cf51412010-07-24 23:06:59 +00005184 for (unsigned i = 0, e = ArgList.size(); i != e; ++i)
5185 ParamTypes.push_back(ArgList[i].V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005186
Chris Lattnerac161bf2009-01-02 07:01:27 +00005187 if (!FunctionType::isValidReturnType(RetType))
5188 return Error(RetTypeLoc, "Invalid result type for LLVM function");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005189
Owen Anderson4056ca92009-07-29 22:17:13 +00005190 Ty = FunctionType::get(RetType, ParamTypes, false);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005191 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005192
Chris Lattnerac161bf2009-01-02 07:01:27 +00005193 // Look up the callee.
5194 Value *Callee;
David Blaikie23af6482015-04-16 23:24:18 +00005195 if (ConvertValIDToValue(PointerType::getUnqual(Ty), CalleeID, Callee, &PFS))
5196 return true;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005197
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005198 // Set up the Attribute for the function.
Bill Wendlingf5075a42013-01-27 02:24:02 +00005199 SmallVector<AttributeSet, 8> Attrs;
Bill Wendling3bef2dd2012-09-19 23:54:18 +00005200 if (RetAttrs.hasAttributes())
Bill Wendlingf5075a42013-01-27 02:24:02 +00005201 Attrs.push_back(AttributeSet::get(RetType->getContext(),
5202 AttributeSet::ReturnIndex,
5203 RetAttrs));
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005204
Chris Lattnerac161bf2009-01-02 07:01:27 +00005205 SmallVector<Value*, 8> Args;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005206
Chris Lattnerac161bf2009-01-02 07:01:27 +00005207 // Loop through FunctionType's arguments and ensure they are specified
5208 // correctly. Also, gather any parameter attributes.
5209 FunctionType::param_iterator I = Ty->param_begin();
5210 FunctionType::param_iterator E = Ty->param_end();
5211 for (unsigned i = 0, e = ArgList.size(); i != e; ++i) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005212 Type *ExpectedTy = nullptr;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005213 if (I != E) {
5214 ExpectedTy = *I++;
5215 } else if (!Ty->isVarArg()) {
5216 return Error(ArgList[i].Loc, "too many arguments specified");
5217 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005218
Chris Lattnerac161bf2009-01-02 07:01:27 +00005219 if (ExpectedTy && ExpectedTy != ArgList[i].V->getType())
5220 return Error(ArgList[i].Loc, "argument is not of expected type '" +
Chris Lattner0f214eb2011-06-18 21:18:23 +00005221 getTypeString(ExpectedTy) + "'");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005222 Args.push_back(ArgList[i].V);
Bill Wendlingfe0021a2013-01-31 00:29:54 +00005223 if (ArgList[i].Attrs.hasAttributes(i + 1)) {
5224 AttrBuilder B(ArgList[i].Attrs, i + 1);
Bill Wendlingf5075a42013-01-27 02:24:02 +00005225 Attrs.push_back(AttributeSet::get(RetType->getContext(), i + 1, B));
5226 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005227 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005228
Chris Lattnerac161bf2009-01-02 07:01:27 +00005229 if (I != E)
5230 return Error(CallLoc, "not enough parameters specified for call");
5231
David Majnemer8d22abd2015-02-23 00:01:32 +00005232 if (FnAttrs.hasAttributes()) {
5233 if (FnAttrs.hasAlignmentAttr())
5234 return Error(CallLoc, "call instructions may not have an alignment");
5235
Bill Wendlingf5075a42013-01-27 02:24:02 +00005236 Attrs.push_back(AttributeSet::get(RetType->getContext(),
5237 AttributeSet::FunctionIndex,
5238 FnAttrs));
David Majnemer8d22abd2015-02-23 00:01:32 +00005239 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005240
Bill Wendling3d7b0b82012-12-19 07:18:57 +00005241 // Finish off the Attribute and check them
Bill Wendlinge94d8432012-12-07 23:16:57 +00005242 AttributeSet PAL = AttributeSet::get(Context, Attrs);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005243
David Blaikie348de692015-04-23 21:36:23 +00005244 CallInst *CI = CallInst::Create(Ty, Callee, Args);
Reid Kleckner5772b772014-04-24 20:14:34 +00005245 CI->setTailCallKind(TCK);
Chris Lattnerac161bf2009-01-02 07:01:27 +00005246 CI->setCallingConv(CC);
5247 CI->setAttributes(PAL);
Bill Wendlingb32b0412013-02-08 06:32:06 +00005248 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005249 Inst = CI;
5250 return false;
5251}
5252
5253//===----------------------------------------------------------------------===//
5254// Memory Instructions.
5255//===----------------------------------------------------------------------===//
5256
5257/// ParseAlloc
David Majnemerc4ab61c2014-03-09 06:41:58 +00005258/// ::= 'alloca' 'inalloca'? Type (',' TypeAndValue)? (',' 'align' i32)?
Chris Lattner78103722011-06-17 03:16:47 +00005259int LLParser::ParseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005260 Value *Size = nullptr;
David Majnemera3b0eb22015-02-16 08:38:03 +00005261 LocTy SizeLoc, TyLoc;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005262 unsigned Alignment = 0;
Craig Topper2617dcc2014-04-15 06:32:26 +00005263 Type *Ty = nullptr;
David Majnemerc4ab61c2014-03-09 06:41:58 +00005264
5265 bool IsInAlloca = EatIfPresent(lltok::kw_inalloca);
5266
David Majnemera3b0eb22015-02-16 08:38:03 +00005267 if (ParseType(Ty, TyLoc)) return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005268
David Majnemera3b0eb22015-02-16 08:38:03 +00005269 if (Ty->isFunctionTy() || !PointerType::isValidElementType(Ty))
5270 return Error(TyLoc, "invalid type for alloca");
David Majnemerfad5a312015-02-11 09:13:11 +00005271
Chris Lattnerb2f39502009-12-30 05:44:30 +00005272 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00005273 if (EatIfPresent(lltok::comma)) {
David Majnemerc4ab61c2014-03-09 06:41:58 +00005274 if (Lex.getKind() == lltok::kw_align) {
5275 if (ParseOptionalAlignment(Alignment)) return true;
5276 } else if (Lex.getKind() == lltok::MetadataVar) {
5277 AteExtraComma = true;
5278 } else {
5279 if (ParseTypeAndValue(Size, SizeLoc, PFS) ||
5280 ParseOptionalCommaAlign(Alignment, AteExtraComma))
5281 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005282 }
5283 }
5284
Dan Gohman2140a742010-05-28 01:14:11 +00005285 if (Size && !Size->getType()->isIntegerTy())
5286 return Error(SizeLoc, "element count must have integer type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005287
Reid Kleckner436c42e2014-01-17 23:58:17 +00005288 AllocaInst *AI = new AllocaInst(Ty, Size, Alignment);
5289 AI->setUsedWithInAlloca(IsInAlloca);
5290 Inst = AI;
Chris Lattner78103722011-06-17 03:16:47 +00005291 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005292}
5293
5294/// ParseLoad
Eli Friedman02e737b2011-08-12 22:50:01 +00005295/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005296/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
Eli Friedman02e737b2011-08-12 22:50:01 +00005297/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00005298int LLParser::ParseLoad(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005299 Value *Val; LocTy Loc;
Devang Patelea8a4b92009-09-17 23:04:48 +00005300 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00005301 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005302 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00005303 AtomicOrdering Ordering = NotAtomic;
5304 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005305
5306 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005307 isAtomic = true;
5308 Lex.Lex();
5309 }
5310
Chris Lattnerbc639292011-11-27 06:56:53 +00005311 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005312 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005313 isVolatile = true;
5314 Lex.Lex();
5315 }
5316
David Blaikie15d9a4c2015-04-06 20:59:48 +00005317 Type *Ty;
David Blaikiea79ac142015-02-27 21:17:42 +00005318 LocTy ExplicitTypeLoc = Lex.getLoc();
5319 if (ParseType(Ty) ||
5320 ParseToken(lltok::comma, "expected comma after load's type") ||
5321 ParseTypeAndValue(Val, Loc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00005322 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005323 ParseOptionalCommaAlign(Alignment, AteExtraComma))
5324 return true;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005325
David Blaikie15d9a4c2015-04-06 20:59:48 +00005326 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005327 return Error(Loc, "load operand must be a pointer to a first class type");
Eli Friedman59b66882011-08-09 23:02:53 +00005328 if (isAtomic && !Alignment)
5329 return Error(Loc, "atomic load must have explicit non-zero alignment");
5330 if (Ordering == Release || Ordering == AcquireRelease)
5331 return Error(Loc, "atomic load cannot use Release ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005332
David Blaikiea79ac142015-02-27 21:17:42 +00005333 if (Ty != cast<PointerType>(Val->getType())->getElementType())
5334 return Error(ExplicitTypeLoc,
5335 "explicit pointee type doesn't match operand's pointee type");
5336
David Blaikie15d9a4c2015-04-06 20:59:48 +00005337 Inst = new LoadInst(Ty, Val, "", isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00005338 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005339}
5340
5341/// ParseStore
Eli Friedman02e737b2011-08-12 22:50:01 +00005342
5343/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
5344/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
Eli Friedman59b66882011-08-09 23:02:53 +00005345/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
Chris Lattnerbc639292011-11-27 06:56:53 +00005346int LLParser::ParseStore(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005347 Value *Val, *Ptr; LocTy Loc, PtrLoc;
Devang Patelea8a4b92009-09-17 23:04:48 +00005348 unsigned Alignment = 0;
Chris Lattnerb2f39502009-12-30 05:44:30 +00005349 bool AteExtraComma = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005350 bool isAtomic = false;
Eli Friedman59b66882011-08-09 23:02:53 +00005351 AtomicOrdering Ordering = NotAtomic;
5352 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005353
5354 if (Lex.getKind() == lltok::kw_atomic) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005355 isAtomic = true;
5356 Lex.Lex();
5357 }
5358
Chris Lattnerbc639292011-11-27 06:56:53 +00005359 bool isVolatile = false;
Eli Friedman02e737b2011-08-12 22:50:01 +00005360 if (Lex.getKind() == lltok::kw_volatile) {
Eli Friedman02e737b2011-08-12 22:50:01 +00005361 isVolatile = true;
5362 Lex.Lex();
5363 }
5364
Chris Lattnerac161bf2009-01-02 07:01:27 +00005365 if (ParseTypeAndValue(Val, Loc, PFS) ||
5366 ParseToken(lltok::comma, "expected ',' after store operand") ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005367 ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
Eli Friedman59b66882011-08-09 23:02:53 +00005368 ParseScopeAndOrdering(isAtomic, Scope, Ordering) ||
Chris Lattnerb2f39502009-12-30 05:44:30 +00005369 ParseOptionalCommaAlign(Alignment, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005370 return true;
Devang Patelea8a4b92009-09-17 23:04:48 +00005371
Duncan Sands19d0b472010-02-16 11:11:14 +00005372 if (!Ptr->getType()->isPointerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005373 return Error(PtrLoc, "store operand must be a pointer");
5374 if (!Val->getType()->isFirstClassType())
5375 return Error(Loc, "store operand must be a first class value");
5376 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
5377 return Error(Loc, "stored value and pointer type do not match");
Eli Friedman59b66882011-08-09 23:02:53 +00005378 if (isAtomic && !Alignment)
5379 return Error(Loc, "atomic store must have explicit non-zero alignment");
5380 if (Ordering == Acquire || Ordering == AcquireRelease)
5381 return Error(Loc, "atomic store cannot use Acquire ordering");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005382
Eli Friedman59b66882011-08-09 23:02:53 +00005383 Inst = new StoreInst(Val, Ptr, isVolatile, Alignment, Ordering, Scope);
Chris Lattnerb2f39502009-12-30 05:44:30 +00005384 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005385}
5386
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005387/// ParseCmpXchg
Tim Northover420a2162014-06-13 14:24:07 +00005388/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
5389/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering
Eli Friedman02e737b2011-08-12 22:50:01 +00005390int LLParser::ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005391 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
5392 bool AteExtraComma = false;
Tim Northovere94a5182014-03-11 10:48:52 +00005393 AtomicOrdering SuccessOrdering = NotAtomic;
5394 AtomicOrdering FailureOrdering = NotAtomic;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005395 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005396 bool isVolatile = false;
Tim Northover420a2162014-06-13 14:24:07 +00005397 bool isWeak = false;
5398
5399 if (EatIfPresent(lltok::kw_weak))
5400 isWeak = true;
Eli Friedman02e737b2011-08-12 22:50:01 +00005401
5402 if (EatIfPresent(lltok::kw_volatile))
5403 isVolatile = true;
5404
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005405 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
5406 ParseToken(lltok::comma, "expected ',' after cmpxchg address") ||
5407 ParseTypeAndValue(Cmp, CmpLoc, PFS) ||
5408 ParseToken(lltok::comma, "expected ',' after cmpxchg cmp operand") ||
5409 ParseTypeAndValue(New, NewLoc, PFS) ||
Tim Northovere94a5182014-03-11 10:48:52 +00005410 ParseScopeAndOrdering(true /*Always atomic*/, Scope, SuccessOrdering) ||
5411 ParseOrdering(FailureOrdering))
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005412 return true;
5413
Tim Northovere94a5182014-03-11 10:48:52 +00005414 if (SuccessOrdering == Unordered || FailureOrdering == Unordered)
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005415 return TokError("cmpxchg cannot be unordered");
Tim Northovere94a5182014-03-11 10:48:52 +00005416 if (SuccessOrdering < FailureOrdering)
5417 return TokError("cmpxchg must be at least as ordered on success as failure");
5418 if (FailureOrdering == Release || FailureOrdering == AcquireRelease)
5419 return TokError("cmpxchg failure ordering cannot include release semantics");
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005420 if (!Ptr->getType()->isPointerTy())
5421 return Error(PtrLoc, "cmpxchg operand must be a pointer");
5422 if (cast<PointerType>(Ptr->getType())->getElementType() != Cmp->getType())
5423 return Error(CmpLoc, "compare value and pointer type do not match");
5424 if (cast<PointerType>(Ptr->getType())->getElementType() != New->getType())
5425 return Error(NewLoc, "new value and pointer type do not match");
5426 if (!New->getType()->isIntegerTy())
5427 return Error(NewLoc, "cmpxchg operand must be an integer");
5428 unsigned Size = New->getType()->getPrimitiveSizeInBits();
5429 if (Size < 8 || (Size & (Size - 1)))
5430 return Error(NewLoc, "cmpxchg operand must be power-of-two byte-sized"
5431 " integer");
5432
Tim Northover420a2162014-06-13 14:24:07 +00005433 AtomicCmpXchgInst *CXI = new AtomicCmpXchgInst(
5434 Ptr, Cmp, New, SuccessOrdering, FailureOrdering, Scope);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005435 CXI->setVolatile(isVolatile);
Tim Northover420a2162014-06-13 14:24:07 +00005436 CXI->setWeak(isWeak);
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005437 Inst = CXI;
5438 return AteExtraComma ? InstExtraComma : InstNormal;
5439}
5440
5441/// ParseAtomicRMW
Eli Friedman02e737b2011-08-12 22:50:01 +00005442/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
5443/// 'singlethread'? AtomicOrdering
5444int LLParser::ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005445 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
5446 bool AteExtraComma = false;
5447 AtomicOrdering Ordering = NotAtomic;
5448 SynchronizationScope Scope = CrossThread;
Eli Friedman02e737b2011-08-12 22:50:01 +00005449 bool isVolatile = false;
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005450 AtomicRMWInst::BinOp Operation;
Eli Friedman02e737b2011-08-12 22:50:01 +00005451
5452 if (EatIfPresent(lltok::kw_volatile))
5453 isVolatile = true;
5454
Eli Friedmanc9a551e2011-07-28 21:48:00 +00005455 switch (Lex.getKind()) {
5456 default: return TokError("expected binary operation in atomicrmw");
5457 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
5458 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
5459 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
5460 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
5461 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
5462 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
5463 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
5464 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
5465 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
5466 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
5467 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
5468 }
5469 Lex.Lex(); // Eat the operation.
5470
5471 if (ParseTypeAndValue(Ptr, PtrLoc, PFS) ||
5472 ParseToken(lltok::comma, "expected ',' after atomicrmw address") ||
5473 ParseTypeAndValue(Val, ValLoc, PFS) ||
5474 ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
5475 return true;
5476
5477 if (Ordering == Unordered)
5478 return TokError("atomicrmw cannot be unordered");
5479 if (!Ptr->getType()->isPointerTy())
5480 return Error(PtrLoc, "atomicrmw operand must be a pointer");
5481 if (cast<PointerType>(Ptr->getType())->getElementType() != Val->getType())
5482 return Error(ValLoc, "atomicrmw value and pointer type do not match");
5483 if (!Val->getType()->isIntegerTy())
5484 return Error(ValLoc, "atomicrmw operand must be an integer");
5485 unsigned Size = Val->getType()->getPrimitiveSizeInBits();
5486 if (Size < 8 || (Size & (Size - 1)))
5487 return Error(ValLoc, "atomicrmw operand must be power-of-two byte-sized"
5488 " integer");
5489
5490 AtomicRMWInst *RMWI =
5491 new AtomicRMWInst(Operation, Ptr, Val, Ordering, Scope);
5492 RMWI->setVolatile(isVolatile);
5493 Inst = RMWI;
5494 return AteExtraComma ? InstExtraComma : InstNormal;
5495}
5496
Eli Friedmanfee02c62011-07-25 23:16:38 +00005497/// ParseFence
5498/// ::= 'fence' 'singlethread'? AtomicOrdering
5499int LLParser::ParseFence(Instruction *&Inst, PerFunctionState &PFS) {
5500 AtomicOrdering Ordering = NotAtomic;
5501 SynchronizationScope Scope = CrossThread;
5502 if (ParseScopeAndOrdering(true /*Always atomic*/, Scope, Ordering))
5503 return true;
5504
5505 if (Ordering == Unordered)
5506 return TokError("fence cannot be unordered");
5507 if (Ordering == Monotonic)
5508 return TokError("fence cannot be monotonic");
5509
5510 Inst = new FenceInst(Context, Ordering, Scope);
5511 return InstNormal;
5512}
5513
Chris Lattnerac161bf2009-01-02 07:01:27 +00005514/// ParseGetElementPtr
Dan Gohman1639c392009-07-27 21:53:46 +00005515/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
Chris Lattnerf4f03422009-12-30 05:27:33 +00005516int LLParser::ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005517 Value *Ptr = nullptr;
5518 Value *Val = nullptr;
Nadav Rotem3924cb02011-12-05 06:29:09 +00005519 LocTy Loc, EltLoc;
Dan Gohman1639c392009-07-27 21:53:46 +00005520
Dan Gohman16cbbe42009-07-29 15:58:36 +00005521 bool InBounds = EatIfPresent(lltok::kw_inbounds);
Dan Gohman1639c392009-07-27 21:53:46 +00005522
David Blaikie79e6c742015-02-27 19:29:02 +00005523 Type *Ty = nullptr;
5524 LocTy ExplicitTypeLoc = Lex.getLoc();
5525 if (ParseType(Ty) ||
5526 ParseToken(lltok::comma, "expected comma after getelementptr's type") ||
5527 ParseTypeAndValue(Ptr, Loc, PFS))
5528 return true;
5529
Eli Benderskyd9806682013-04-22 17:03:42 +00005530 Type *BaseType = Ptr->getType();
5531 PointerType *BasePointerType = dyn_cast<PointerType>(BaseType->getScalarType());
5532 if (!BasePointerType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005533 return Error(Loc, "base of getelementptr must be a pointer");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005534
David Blaikie8d757942015-03-09 23:08:44 +00005535 if (Ty != BasePointerType->getElementType())
5536 return Error(ExplicitTypeLoc,
5537 "explicit pointee type doesn't match operand's pointee type");
5538
Chris Lattnerac161bf2009-01-02 07:01:27 +00005539 SmallVector<Value*, 16> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005540 bool AteExtraComma = false;
Chris Lattner3822f632009-01-02 08:05:26 +00005541 while (EatIfPresent(lltok::comma)) {
Chris Lattnerf4f03422009-12-30 05:27:33 +00005542 if (Lex.getKind() == lltok::MetadataVar) {
5543 AteExtraComma = true;
Devang Patel52b17452009-10-13 18:49:55 +00005544 break;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005545 }
Chris Lattner3822f632009-01-02 08:05:26 +00005546 if (ParseTypeAndValue(Val, EltLoc, PFS)) return true;
Nadav Rotem3924cb02011-12-05 06:29:09 +00005547 if (!Val->getType()->getScalarType()->isIntegerTy())
Chris Lattnerac161bf2009-01-02 07:01:27 +00005548 return Error(EltLoc, "getelementptr index must be an integer");
Nadav Rotem3924cb02011-12-05 06:29:09 +00005549 if (Val->getType()->isVectorTy() != Ptr->getType()->isVectorTy())
5550 return Error(EltLoc, "getelementptr index type missmatch");
5551 if (Val->getType()->isVectorTy()) {
5552 unsigned ValNumEl = cast<VectorType>(Val->getType())->getNumElements();
5553 unsigned PtrNumEl = cast<VectorType>(Ptr->getType())->getNumElements();
5554 if (ValNumEl != PtrNumEl)
5555 return Error(EltLoc,
5556 "getelementptr vector index has a wrong number of elements");
5557 }
Chris Lattnerac161bf2009-01-02 07:01:27 +00005558 Indices.push_back(Val);
5559 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005560
Owen Andersone90f9922015-03-10 06:34:57 +00005561 SmallPtrSet<const Type*, 4> Visited;
David Blaikied33bad32015-04-17 22:32:13 +00005562 if (!Indices.empty() && !Ty->isSized(&Visited))
Eli Benderskyd9806682013-04-22 17:03:42 +00005563 return Error(Loc, "base element of getelementptr must be sized");
5564
David Blaikied33bad32015-04-17 22:32:13 +00005565 if (!GetElementPtrInst::getIndexedType(Ty, Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005566 return Error(Loc, "invalid getelementptr indices");
David Blaikiecd7b97e2015-03-14 21:11:24 +00005567 Inst = GetElementPtrInst::Create(Ty, Ptr, Indices);
Dan Gohman1639c392009-07-27 21:53:46 +00005568 if (InBounds)
Dan Gohman1b849082009-09-07 23:54:19 +00005569 cast<GetElementPtrInst>(Inst)->setIsInBounds(true);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005570 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005571}
5572
5573/// ParseExtractValue
5574/// ::= 'extractvalue' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00005575int LLParser::ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005576 Value *Val; LocTy Loc;
5577 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005578 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005579 if (ParseTypeAndValue(Val, Loc, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00005580 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005581 return true;
5582
Chris Lattner392be582010-02-12 20:49:41 +00005583 if (!Val->getType()->isAggregateType())
5584 return Error(Loc, "extractvalue operand must be aggregate type");
Chris Lattnerac161bf2009-01-02 07:01:27 +00005585
Jay Foad57aa6362011-07-13 10:26:04 +00005586 if (!ExtractValueInst::getIndexedType(Val->getType(), Indices))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005587 return Error(Loc, "invalid indices for extractvalue");
Jay Foad57aa6362011-07-13 10:26:04 +00005588 Inst = ExtractValueInst::Create(Val, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005589 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005590}
5591
5592/// ParseInsertValue
5593/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
Chris Lattnerf4f03422009-12-30 05:27:33 +00005594int LLParser::ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
Chris Lattnerac161bf2009-01-02 07:01:27 +00005595 Value *Val0, *Val1; LocTy Loc0, Loc1;
5596 SmallVector<unsigned, 4> Indices;
Chris Lattnerf4f03422009-12-30 05:27:33 +00005597 bool AteExtraComma;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005598 if (ParseTypeAndValue(Val0, Loc0, PFS) ||
5599 ParseToken(lltok::comma, "expected comma after insertvalue operand") ||
5600 ParseTypeAndValue(Val1, Loc1, PFS) ||
Chris Lattnerf4f03422009-12-30 05:27:33 +00005601 ParseIndexList(Indices, AteExtraComma))
Chris Lattnerac161bf2009-01-02 07:01:27 +00005602 return true;
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005603
Chris Lattner392be582010-02-12 20:49:41 +00005604 if (!Val0->getType()->isAggregateType())
5605 return Error(Loc0, "insertvalue operand must be aggregate type");
Daniel Dunbar7d6781b2009-09-20 02:20:51 +00005606
David Majnemer30074532015-02-11 07:43:58 +00005607 Type *IndexedType = ExtractValueInst::getIndexedType(Val0->getType(), Indices);
5608 if (!IndexedType)
Chris Lattnerac161bf2009-01-02 07:01:27 +00005609 return Error(Loc0, "invalid indices for insertvalue");
David Majnemer30074532015-02-11 07:43:58 +00005610 if (IndexedType != Val1->getType())
5611 return Error(Loc1, "insertvalue operand and field disagree in type: '" +
5612 getTypeString(Val1->getType()) + "' instead of '" +
5613 getTypeString(IndexedType) + "'");
Jay Foad57aa6362011-07-13 10:26:04 +00005614 Inst = InsertValueInst::Create(Val0, Val1, Indices);
Chris Lattnerf4f03422009-12-30 05:27:33 +00005615 return AteExtraComma ? InstExtraComma : InstNormal;
Chris Lattnerac161bf2009-01-02 07:01:27 +00005616}
Nick Lewycky49f89192009-04-04 07:22:01 +00005617
5618//===----------------------------------------------------------------------===//
5619// Embedded metadata.
5620//===----------------------------------------------------------------------===//
5621
5622/// ParseMDNodeVector
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005623/// ::= { Element (',' Element)* }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00005624/// Element
5625/// ::= 'null' | TypeAndValue
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005626bool LLParser::ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
David Majnemer06f960d2014-12-11 20:44:09 +00005627 if (ParseToken(lltok::lbrace, "expected '{' here"))
5628 return true;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005629
Dan Gohman1e0213a2010-07-13 19:33:27 +00005630 // Check for an empty list.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005631 if (EatIfPresent(lltok::rbrace))
Dan Gohman1e0213a2010-07-13 19:33:27 +00005632 return false;
5633
Nick Lewycky49f89192009-04-04 07:22:01 +00005634 do {
Chris Lattnera01ddfc2009-12-30 04:42:57 +00005635 // Null is a special case since it is typeless.
5636 if (EatIfPresent(lltok::kw_null)) {
Craig Topper2617dcc2014-04-15 06:32:26 +00005637 Elts.push_back(nullptr);
Chris Lattnera01ddfc2009-12-30 04:42:57 +00005638 continue;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +00005639 }
Michael Ilseman26ee2b82012-11-15 22:34:00 +00005640
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005641 Metadata *MD;
5642 if (ParseMetadata(MD, nullptr))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005643 return true;
Duncan P. N. Exon Smithbe7ea192014-12-15 19:07:53 +00005644 Elts.push_back(MD);
Nick Lewycky49f89192009-04-04 07:22:01 +00005645 } while (EatIfPresent(lltok::comma));
5646
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00005647 return ParseToken(lltok::rbrace, "expected end of metadata node");
Nick Lewycky49f89192009-04-04 07:22:01 +00005648}
Duncan P. N. Exon Smith0a448fb2014-08-19 21:30:15 +00005649
5650//===----------------------------------------------------------------------===//
5651// Use-list order directives.
5652//===----------------------------------------------------------------------===//
5653bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
5654 SMLoc Loc) {
5655 if (V->use_empty())
5656 return Error(Loc, "value has no uses");
5657
5658 unsigned NumUses = 0;
5659 SmallDenseMap<const Use *, unsigned, 16> Order;
5660 for (const Use &U : V->uses()) {
5661 if (++NumUses > Indexes.size())
5662 break;
5663 Order[&U] = Indexes[NumUses - 1];
5664 }
5665 if (NumUses < 2)
5666 return Error(Loc, "value only has one use");
5667 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
5668 return Error(Loc, "wrong number of indexes, expected " +
5669 Twine(std::distance(V->use_begin(), V->use_end())));
5670
5671 V->sortUseList([&](const Use &L, const Use &R) {
5672 return Order.lookup(&L) < Order.lookup(&R);
5673 });
5674 return false;
5675}
5676
5677/// ParseUseListOrderIndexes
5678/// ::= '{' uint32 (',' uint32)+ '}'
5679bool LLParser::ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
5680 SMLoc Loc = Lex.getLoc();
5681 if (ParseToken(lltok::lbrace, "expected '{' here"))
5682 return true;
5683 if (Lex.getKind() == lltok::rbrace)
5684 return Lex.Error("expected non-empty list of uselistorder indexes");
5685
5686 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
5687 // indexes should be distinct numbers in the range [0, size-1], and should
5688 // not be in order.
5689 unsigned Offset = 0;
5690 unsigned Max = 0;
5691 bool IsOrdered = true;
5692 assert(Indexes.empty() && "Expected empty order vector");
5693 do {
5694 unsigned Index;
5695 if (ParseUInt32(Index))
5696 return true;
5697
5698 // Update consistency checks.
5699 Offset += Index - Indexes.size();
5700 Max = std::max(Max, Index);
5701 IsOrdered &= Index == Indexes.size();
5702
5703 Indexes.push_back(Index);
5704 } while (EatIfPresent(lltok::comma));
5705
5706 if (ParseToken(lltok::rbrace, "expected '}' here"))
5707 return true;
5708
5709 if (Indexes.size() < 2)
5710 return Error(Loc, "expected >= 2 uselistorder indexes");
5711 if (Offset != 0 || Max >= Indexes.size())
5712 return Error(Loc, "expected distinct uselistorder indexes in range [0, size)");
5713 if (IsOrdered)
5714 return Error(Loc, "expected uselistorder indexes to change the order");
5715
5716 return false;
5717}
5718
5719/// ParseUseListOrder
5720/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
5721bool LLParser::ParseUseListOrder(PerFunctionState *PFS) {
5722 SMLoc Loc = Lex.getLoc();
5723 if (ParseToken(lltok::kw_uselistorder, "expected uselistorder directive"))
5724 return true;
5725
5726 Value *V;
5727 SmallVector<unsigned, 16> Indexes;
5728 if (ParseTypeAndValue(V, PFS) ||
5729 ParseToken(lltok::comma, "expected comma in uselistorder directive") ||
5730 ParseUseListOrderIndexes(Indexes))
5731 return true;
5732
5733 return sortUseListOrder(V, Indexes, Loc);
5734}
5735
5736/// ParseUseListOrderBB
5737/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
5738bool LLParser::ParseUseListOrderBB() {
5739 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
5740 SMLoc Loc = Lex.getLoc();
5741 Lex.Lex();
5742
5743 ValID Fn, Label;
5744 SmallVector<unsigned, 16> Indexes;
5745 if (ParseValID(Fn) ||
5746 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
5747 ParseValID(Label) ||
5748 ParseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
5749 ParseUseListOrderIndexes(Indexes))
5750 return true;
5751
5752 // Check the function.
5753 GlobalValue *GV;
5754 if (Fn.Kind == ValID::t_GlobalName)
5755 GV = M->getNamedValue(Fn.StrVal);
5756 else if (Fn.Kind == ValID::t_GlobalID)
5757 GV = Fn.UIntVal < NumberedVals.size() ? NumberedVals[Fn.UIntVal] : nullptr;
5758 else
5759 return Error(Fn.Loc, "expected function name in uselistorder_bb");
5760 if (!GV)
5761 return Error(Fn.Loc, "invalid function forward reference in uselistorder_bb");
5762 auto *F = dyn_cast<Function>(GV);
5763 if (!F)
5764 return Error(Fn.Loc, "expected function name in uselistorder_bb");
5765 if (F->isDeclaration())
5766 return Error(Fn.Loc, "invalid declaration in uselistorder_bb");
5767
5768 // Check the basic block.
5769 if (Label.Kind == ValID::t_LocalID)
5770 return Error(Label.Loc, "invalid numeric label in uselistorder_bb");
5771 if (Label.Kind != ValID::t_LocalName)
5772 return Error(Label.Loc, "expected basic block name in uselistorder_bb");
5773 Value *V = F->getValueSymbolTable().lookup(Label.StrVal);
5774 if (!V)
5775 return Error(Label.Loc, "invalid basic block in uselistorder_bb");
5776 if (!isa<BasicBlock>(V))
5777 return Error(Label.Loc, "expected basic block in uselistorder_bb");
5778
5779 return sortUseListOrder(V, Indexes, Loc);
5780}