1//===-- LLParser.cpp - Parser Class ---------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the parser class for .ll files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/AsmParser/LLParser.h"
14#include "llvm/ADT/APSInt.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/ScopeExit.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/AsmParser/LLToken.h"
20#include "llvm/AsmParser/SlotMapping.h"
21#include "llvm/BinaryFormat/Dwarf.h"
22#include "llvm/IR/Argument.h"
23#include "llvm/IR/Attributes.h"
24#include "llvm/IR/AutoUpgrade.h"
25#include "llvm/IR/BasicBlock.h"
26#include "llvm/IR/CallingConv.h"
27#include "llvm/IR/Comdat.h"
28#include "llvm/IR/ConstantRange.h"
29#include "llvm/IR/ConstantRangeList.h"
30#include "llvm/IR/Constants.h"
31#include "llvm/IR/DebugInfoMetadata.h"
32#include "llvm/IR/DerivedTypes.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/GlobalIFunc.h"
35#include "llvm/IR/GlobalObject.h"
36#include "llvm/IR/InlineAsm.h"
37#include "llvm/IR/InstIterator.h"
38#include "llvm/IR/Instructions.h"
39#include "llvm/IR/IntrinsicInst.h"
40#include "llvm/IR/Intrinsics.h"
41#include "llvm/IR/LLVMContext.h"
42#include "llvm/IR/Metadata.h"
43#include "llvm/IR/Module.h"
44#include "llvm/IR/Operator.h"
45#include "llvm/IR/Value.h"
46#include "llvm/IR/ValueSymbolTable.h"
47#include "llvm/Support/Casting.h"
48#include "llvm/Support/Compiler.h"
49#include "llvm/Support/ErrorHandling.h"
50#include "llvm/Support/MathExtras.h"
51#include "llvm/Support/ModRef.h"
52#include "llvm/Support/SaveAndRestore.h"
53#include "llvm/Support/raw_ostream.h"
54#include <algorithm>
55#include <cassert>
56#include <cstring>
57#include <optional>
58#include <vector>
59
60using namespace llvm;
61
62static cl::opt<bool> AllowIncompleteIR(
63 "allow-incomplete-ir", cl::init(Val: false), cl::Hidden,
64 cl::desc(
65 "Allow incomplete IR on a best effort basis (references to unknown "
66 "metadata will be dropped)"));
67
68static std::string getTypeString(Type *T) {
69 std::string Result;
70 raw_string_ostream Tmp(Result);
71 Tmp << *T;
72 return Tmp.str();
73}
74
75/// Run: module ::= toplevelentity*
76bool LLParser::Run(bool UpgradeDebugInfo,
77 DataLayoutCallbackTy DataLayoutCallback) {
78 // Prime the lexer.
79 Lex.Lex();
80
81 if (Context.shouldDiscardValueNames())
82 return error(
83 L: Lex.getLoc(),
84 Msg: "Can't read textual IR with a Context that discards named Values");
85
86 if (M) {
87 if (parseTargetDefinitions(DataLayoutCallback))
88 return true;
89 }
90
91 return parseTopLevelEntities() || validateEndOfModule(UpgradeDebugInfo) ||
92 validateEndOfIndex();
93}
94
95bool LLParser::parseStandaloneConstantValue(Constant *&C,
96 const SlotMapping *Slots) {
97 restoreParsingState(Slots);
98 Lex.Lex();
99
100 Type *Ty = nullptr;
101 if (parseType(Result&: Ty) || parseConstantValue(Ty, C))
102 return true;
103 if (Lex.getKind() != lltok::Eof)
104 return error(L: Lex.getLoc(), Msg: "expected end of string");
105 return false;
106}
107
108bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read,
109 const SlotMapping *Slots) {
110 restoreParsingState(Slots);
111 Lex.Lex();
112
113 Read = 0;
114 SMLoc Start = Lex.getLoc();
115 Ty = nullptr;
116 if (parseType(Result&: Ty))
117 return true;
118 SMLoc End = Lex.getLoc();
119 Read = End.getPointer() - Start.getPointer();
120
121 return false;
122}
123
124bool LLParser::parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read,
125 const SlotMapping *Slots) {
126 restoreParsingState(Slots);
127 Lex.Lex();
128
129 Read = 0;
130 SMLoc Start = Lex.getLoc();
131 Result = nullptr;
132 bool Status = parseDIExpressionBody(Result, /*IsDistinct=*/false);
133 SMLoc End = Lex.getLoc();
134 Read = End.getPointer() - Start.getPointer();
135
136 return Status;
137}
138
139void LLParser::restoreParsingState(const SlotMapping *Slots) {
140 if (!Slots)
141 return;
142 NumberedVals = Slots->GlobalValues;
143 NumberedMetadata = Slots->MetadataNodes;
144 for (const auto &I : Slots->NamedTypes)
145 NamedTypes.insert(
146 KV: std::make_pair(x: I.getKey(), y: std::make_pair(x: I.second, y: LocTy())));
147 for (const auto &I : Slots->Types)
148 NumberedTypes.insert(
149 x: std::make_pair(x: I.first, y: std::make_pair(x: I.second, y: LocTy())));
150}
151
152static void dropIntrinsicWithUnknownMetadataArgument(IntrinsicInst *II) {
153 // White-list intrinsics that are safe to drop.
154 if (!isa<DbgInfoIntrinsic>(Val: II) &&
155 II->getIntrinsicID() != Intrinsic::experimental_noalias_scope_decl)
156 return;
157
158 SmallVector<MetadataAsValue *> MVs;
159 for (Value *V : II->args())
160 if (auto *MV = dyn_cast<MetadataAsValue>(Val: V))
161 if (auto *MD = dyn_cast<MDNode>(Val: MV->getMetadata()))
162 if (MD->isTemporary())
163 MVs.push_back(Elt: MV);
164
165 if (!MVs.empty()) {
166 assert(II->use_empty() && "Cannot have uses");
167 II->eraseFromParent();
168
169 // Also remove no longer used MetadataAsValue wrappers.
170 for (MetadataAsValue *MV : MVs)
171 if (MV->use_empty())
172 delete MV;
173 }
174}
175
176void LLParser::dropUnknownMetadataReferences() {
177 auto Pred = [](unsigned MDKind, MDNode *Node) { return Node->isTemporary(); };
178 for (Function &F : *M) {
179 F.eraseMetadataIf(Pred);
180 for (Instruction &I : make_early_inc_range(Range: instructions(F))) {
181 I.eraseMetadataIf(Pred);
182
183 if (auto *II = dyn_cast<IntrinsicInst>(Val: &I))
184 dropIntrinsicWithUnknownMetadataArgument(II);
185 }
186 }
187
188 for (GlobalVariable &GV : M->globals())
189 GV.eraseMetadataIf(Pred);
190
191 for (const auto &[ID, Info] : make_early_inc_range(Range&: ForwardRefMDNodes)) {
192 // Check whether there is only a single use left, which would be in our
193 // own NumberedMetadata.
194 if (Info.first->getNumTemporaryUses() == 1) {
195 NumberedMetadata.erase(x: ID);
196 ForwardRefMDNodes.erase(x: ID);
197 }
198 }
199}
200
201/// validateEndOfModule - Do final validity and basic correctness checks at the
202/// end of the module.
203bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) {
204 if (!M)
205 return false;
206
207 // We should have already returned an error if we observed both intrinsics and
208 // records in this IR.
209 assert(!(SeenNewDbgInfoFormat && SeenOldDbgInfoFormat) &&
210 "Mixed debug intrinsics/records seen without a parsing error?");
211
212 // Handle any function attribute group forward references.
213 for (const auto &RAG : ForwardRefAttrGroups) {
214 Value *V = RAG.first;
215 const std::vector<unsigned> &Attrs = RAG.second;
216 AttrBuilder B(Context);
217
218 for (const auto &Attr : Attrs) {
219 auto R = NumberedAttrBuilders.find(x: Attr);
220 if (R != NumberedAttrBuilders.end())
221 B.merge(B: R->second);
222 }
223
224 if (Function *Fn = dyn_cast<Function>(Val: V)) {
225 AttributeList AS = Fn->getAttributes();
226 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
227 AS = AS.removeFnAttributes(C&: Context);
228
229 FnAttrs.merge(B);
230
231 // If the alignment was parsed as an attribute, move to the alignment
232 // field.
233 if (MaybeAlign A = FnAttrs.getAlignment()) {
234 Fn->setAlignment(*A);
235 FnAttrs.removeAttribute(Val: Attribute::Alignment);
236 }
237
238 AS = AS.addFnAttributes(C&: Context, B: FnAttrs);
239 Fn->setAttributes(AS);
240 } else if (CallInst *CI = dyn_cast<CallInst>(Val: V)) {
241 AttributeList AS = CI->getAttributes();
242 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
243 AS = AS.removeFnAttributes(C&: Context);
244 FnAttrs.merge(B);
245 AS = AS.addFnAttributes(C&: Context, B: FnAttrs);
246 CI->setAttributes(AS);
247 } else if (InvokeInst *II = dyn_cast<InvokeInst>(Val: V)) {
248 AttributeList AS = II->getAttributes();
249 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
250 AS = AS.removeFnAttributes(C&: Context);
251 FnAttrs.merge(B);
252 AS = AS.addFnAttributes(C&: Context, B: FnAttrs);
253 II->setAttributes(AS);
254 } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(Val: V)) {
255 AttributeList AS = CBI->getAttributes();
256 AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs());
257 AS = AS.removeFnAttributes(C&: Context);
258 FnAttrs.merge(B);
259 AS = AS.addFnAttributes(C&: Context, B: FnAttrs);
260 CBI->setAttributes(AS);
261 } else if (auto *GV = dyn_cast<GlobalVariable>(Val: V)) {
262 AttrBuilder Attrs(M->getContext(), GV->getAttributes());
263 Attrs.merge(B);
264 GV->setAttributes(AttributeSet::get(C&: Context,B: Attrs));
265 } else {
266 llvm_unreachable("invalid object with forward attribute group reference");
267 }
268 }
269
270 // If there are entries in ForwardRefBlockAddresses at this point, the
271 // function was never defined.
272 if (!ForwardRefBlockAddresses.empty())
273 return error(L: ForwardRefBlockAddresses.begin()->first.Loc,
274 Msg: "expected function name in blockaddress");
275
276 auto ResolveForwardRefDSOLocalEquivalents = [&](const ValID &GVRef,
277 GlobalValue *FwdRef) {
278 GlobalValue *GV = nullptr;
279 if (GVRef.Kind == ValID::t_GlobalName) {
280 GV = M->getNamedValue(Name: GVRef.StrVal);
281 } else {
282 GV = NumberedVals.get(ID: GVRef.UIntVal);
283 }
284
285 if (!GV)
286 return error(L: GVRef.Loc, Msg: "unknown function '" + GVRef.StrVal +
287 "' referenced by dso_local_equivalent");
288
289 if (!GV->getValueType()->isFunctionTy())
290 return error(L: GVRef.Loc,
291 Msg: "expected a function, alias to function, or ifunc "
292 "in dso_local_equivalent");
293
294 auto *Equiv = DSOLocalEquivalent::get(GV);
295 FwdRef->replaceAllUsesWith(V: Equiv);
296 FwdRef->eraseFromParent();
297 return false;
298 };
299
300 // If there are entries in ForwardRefDSOLocalEquivalentIDs/Names at this
301 // point, they are references after the function was defined. Resolve those
302 // now.
303 for (auto &Iter : ForwardRefDSOLocalEquivalentIDs) {
304 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
305 return true;
306 }
307 for (auto &Iter : ForwardRefDSOLocalEquivalentNames) {
308 if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second))
309 return true;
310 }
311 ForwardRefDSOLocalEquivalentIDs.clear();
312 ForwardRefDSOLocalEquivalentNames.clear();
313
314 for (const auto &NT : NumberedTypes)
315 if (NT.second.second.isValid())
316 return error(L: NT.second.second,
317 Msg: "use of undefined type '%" + Twine(NT.first) + "'");
318
319 for (const auto &[Name, TypeInfo] : NamedTypes)
320 if (TypeInfo.second.isValid())
321 return error(L: TypeInfo.second,
322 Msg: "use of undefined type named '" + Name + "'");
323
324 if (!ForwardRefComdats.empty())
325 return error(L: ForwardRefComdats.begin()->second,
326 Msg: "use of undefined comdat '$" +
327 ForwardRefComdats.begin()->first + "'");
328
329 for (const auto &[Name, Info] : make_early_inc_range(Range&: ForwardRefVals)) {
330 if (StringRef(Name).starts_with(Prefix: "llvm.")) {
331 Intrinsic::ID IID = Intrinsic::lookupIntrinsicID(Name);
332 // Automatically create declarations for intrinsics. Intrinsics can only
333 // be called directly, so the call function type directly determines the
334 // declaration function type.
335 //
336 // Additionally, automatically add the required mangling suffix to the
337 // intrinsic name. This means that we may replace a single forward
338 // declaration with multiple functions here.
339 for (Use &U : make_early_inc_range(Range: Info.first->uses())) {
340 auto *CB = dyn_cast<CallBase>(Val: U.getUser());
341 if (!CB || !CB->isCallee(U: &U))
342 return error(L: Info.second, Msg: "intrinsic can only be used as callee");
343
344 SmallVector<Type *> OverloadTys;
345 if (IID != Intrinsic::not_intrinsic &&
346 Intrinsic::getIntrinsicSignature(IID, FT: CB->getFunctionType(),
347 ArgTys&: OverloadTys)) {
348 U.set(Intrinsic::getOrInsertDeclaration(M, id: IID, Tys: OverloadTys));
349 } else {
350 // Try to upgrade the intrinsic.
351 Function *TmpF = Function::Create(Ty: CB->getFunctionType(),
352 Linkage: Function::ExternalLinkage, N: Name, M);
353 Function *NewF = nullptr;
354 if (!UpgradeIntrinsicFunction(F: TmpF, NewFn&: NewF)) {
355 if (IID == Intrinsic::not_intrinsic)
356 return error(L: Info.second, Msg: "unknown intrinsic '" + Name + "'");
357 return error(L: Info.second, Msg: "invalid intrinsic signature");
358 }
359
360 U.set(TmpF);
361 UpgradeIntrinsicCall(CB, NewFn: NewF);
362 if (TmpF->use_empty())
363 TmpF->eraseFromParent();
364 }
365 }
366
367 Info.first->eraseFromParent();
368 ForwardRefVals.erase(x: Name);
369 continue;
370 }
371
372 // If incomplete IR is allowed, also add declarations for
373 // non-intrinsics.
374 if (!AllowIncompleteIR)
375 continue;
376
377 auto GetCommonFunctionType = [](Value *V) -> FunctionType * {
378 FunctionType *FTy = nullptr;
379 for (Use &U : V->uses()) {
380 auto *CB = dyn_cast<CallBase>(Val: U.getUser());
381 if (!CB || !CB->isCallee(U: &U) || (FTy && FTy != CB->getFunctionType()))
382 return nullptr;
383 FTy = CB->getFunctionType();
384 }
385 return FTy;
386 };
387
388 // First check whether this global is only used in calls with the same
389 // type, in which case we'll insert a function. Otherwise, fall back to
390 // using a dummy i8 type.
391 Type *Ty = GetCommonFunctionType(Info.first);
392 if (!Ty)
393 Ty = Type::getInt8Ty(C&: Context);
394
395 GlobalValue *GV;
396 if (auto *FTy = dyn_cast<FunctionType>(Val: Ty))
397 GV = Function::Create(Ty: FTy, Linkage: GlobalValue::ExternalLinkage, N: Name, M);
398 else
399 GV = new GlobalVariable(*M, Ty, /*isConstant*/ false,
400 GlobalValue::ExternalLinkage,
401 /*Initializer*/ nullptr, Name);
402 Info.first->replaceAllUsesWith(V: GV);
403 Info.first->eraseFromParent();
404 ForwardRefVals.erase(x: Name);
405 }
406
407 if (!ForwardRefVals.empty())
408 return error(L: ForwardRefVals.begin()->second.second,
409 Msg: "use of undefined value '@" + ForwardRefVals.begin()->first +
410 "'");
411
412 if (!ForwardRefValIDs.empty())
413 return error(L: ForwardRefValIDs.begin()->second.second,
414 Msg: "use of undefined value '@" +
415 Twine(ForwardRefValIDs.begin()->first) + "'");
416
417 if (AllowIncompleteIR && !ForwardRefMDNodes.empty())
418 dropUnknownMetadataReferences();
419
420 if (!ForwardRefMDNodes.empty())
421 return error(L: ForwardRefMDNodes.begin()->second.second,
422 Msg: "use of undefined metadata '!" +
423 Twine(ForwardRefMDNodes.begin()->first) + "'");
424
425 // Resolve metadata cycles.
426 for (auto &N : NumberedMetadata) {
427 if (N.second && !N.second->isResolved())
428 N.second->resolveCycles();
429 }
430
431 DISubprogram::cleanupRetainedNodes(NewDistinctSPs);
432 NewDistinctSPs.clear();
433
434 for (auto *Inst : InstsWithTBAATag) {
435 MDNode *MD = Inst->getMetadata(KindID: LLVMContext::MD_tbaa);
436 // With incomplete IR, the tbaa metadata may have been dropped.
437 if (!AllowIncompleteIR)
438 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
439 if (MD) {
440 auto *UpgradedMD = UpgradeTBAANode(TBAANode&: *MD);
441 if (MD != UpgradedMD)
442 Inst->setMetadata(KindID: LLVMContext::MD_tbaa, Node: UpgradedMD);
443 }
444 }
445
446 // Look for intrinsic functions and CallInst that need to be upgraded. We use
447 // make_early_inc_range here because we may remove some functions.
448 for (Function &F : llvm::make_early_inc_range(Range&: *M))
449 UpgradeCallsToIntrinsic(F: &F);
450
451 if (UpgradeDebugInfo)
452 llvm::UpgradeDebugInfo(M&: *M);
453
454 UpgradeModuleFlags(M&: *M);
455 UpgradeNVVMAnnotations(M&: *M);
456 UpgradeSectionAttributes(M&: *M);
457 copyModuleAttrToFunctions(M&: *M);
458
459 if (!Slots)
460 return false;
461 // Initialize the slot mapping.
462 // Because by this point we've parsed and validated everything, we can "steal"
463 // the mapping from LLParser as it doesn't need it anymore.
464 Slots->GlobalValues = std::move(NumberedVals);
465 Slots->MetadataNodes = std::move(NumberedMetadata);
466 for (const auto &I : NamedTypes)
467 Slots->NamedTypes.insert(KV: std::make_pair(x: I.getKey(), y: I.second.first));
468 for (const auto &I : NumberedTypes)
469 Slots->Types.insert(x: std::make_pair(x: I.first, y: I.second.first));
470
471 return false;
472}
473
474/// Do final validity and basic correctness checks at the end of the index.
475bool LLParser::validateEndOfIndex() {
476 if (!Index)
477 return false;
478
479 if (!ForwardRefValueInfos.empty())
480 return error(L: ForwardRefValueInfos.begin()->second.front().second,
481 Msg: "use of undefined summary '^" +
482 Twine(ForwardRefValueInfos.begin()->first) + "'");
483
484 if (!ForwardRefAliasees.empty())
485 return error(L: ForwardRefAliasees.begin()->second.front().second,
486 Msg: "use of undefined summary '^" +
487 Twine(ForwardRefAliasees.begin()->first) + "'");
488
489 if (!ForwardRefTypeIds.empty())
490 return error(L: ForwardRefTypeIds.begin()->second.front().second,
491 Msg: "use of undefined type id summary '^" +
492 Twine(ForwardRefTypeIds.begin()->first) + "'");
493
494 return false;
495}
496
497//===----------------------------------------------------------------------===//
498// Top-Level Entities
499//===----------------------------------------------------------------------===//
500
501bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) {
502 // Delay parsing of the data layout string until the target triple is known.
503 // Then, pass both the the target triple and the tentative data layout string
504 // to DataLayoutCallback, allowing to override the DL string.
505 // This enables importing modules with invalid DL strings.
506 std::string TentativeDLStr = M->getDataLayoutStr();
507 LocTy DLStrLoc;
508
509 bool Done = false;
510 while (!Done) {
511 switch (Lex.getKind()) {
512 case lltok::kw_target:
513 if (parseTargetDefinition(TentativeDLStr, DLStrLoc))
514 return true;
515 break;
516 case lltok::kw_source_filename:
517 if (parseSourceFileName())
518 return true;
519 break;
520 default:
521 Done = true;
522 }
523 }
524 // Run the override callback to potentially change the data layout string, and
525 // parse the data layout string.
526 if (auto LayoutOverride =
527 DataLayoutCallback(M->getTargetTriple().str(), TentativeDLStr)) {
528 TentativeDLStr = *LayoutOverride;
529 DLStrLoc = {};
530 }
531 Expected<DataLayout> MaybeDL = DataLayout::parse(LayoutString: TentativeDLStr);
532 if (!MaybeDL)
533 return error(L: DLStrLoc, Msg: toString(E: MaybeDL.takeError()));
534 M->setDataLayout(MaybeDL.get());
535 return false;
536}
537
538bool LLParser::parseTopLevelEntities() {
539 // If there is no Module, then parse just the summary index entries.
540 if (!M) {
541 while (true) {
542 switch (Lex.getKind()) {
543 case lltok::Eof:
544 return false;
545 case lltok::SummaryID:
546 if (parseSummaryEntry())
547 return true;
548 break;
549 case lltok::kw_source_filename:
550 if (parseSourceFileName())
551 return true;
552 break;
553 default:
554 // Skip everything else
555 Lex.Lex();
556 }
557 }
558 }
559 while (true) {
560 switch (Lex.getKind()) {
561 default:
562 return tokError(Msg: "expected top-level entity");
563 case lltok::Eof: return false;
564 case lltok::kw_declare:
565 if (parseDeclare())
566 return true;
567 break;
568 case lltok::kw_define:
569 if (parseDefine())
570 return true;
571 break;
572 case lltok::kw_module:
573 if (parseModuleAsm())
574 return true;
575 break;
576 case lltok::LocalVarID:
577 if (parseUnnamedType())
578 return true;
579 break;
580 case lltok::LocalVar:
581 if (parseNamedType())
582 return true;
583 break;
584 case lltok::GlobalID:
585 if (parseUnnamedGlobal())
586 return true;
587 break;
588 case lltok::GlobalVar:
589 if (parseNamedGlobal())
590 return true;
591 break;
592 case lltok::ComdatVar: if (parseComdat()) return true; break;
593 case lltok::exclaim:
594 if (parseStandaloneMetadata())
595 return true;
596 break;
597 case lltok::SummaryID:
598 if (parseSummaryEntry())
599 return true;
600 break;
601 case lltok::MetadataVar:
602 if (parseNamedMetadata())
603 return true;
604 break;
605 case lltok::kw_attributes:
606 if (parseUnnamedAttrGrp())
607 return true;
608 break;
609 case lltok::kw_uselistorder:
610 if (parseUseListOrder())
611 return true;
612 break;
613 case lltok::kw_uselistorder_bb:
614 if (parseUseListOrderBB())
615 return true;
616 break;
617 }
618 }
619}
620
621/// toplevelentity
622/// ::= 'module' 'asm' STRINGCONSTANT
623bool LLParser::parseModuleAsm() {
624 assert(Lex.getKind() == lltok::kw_module);
625 Lex.Lex();
626
627 std::string AsmStr;
628 if (parseToken(T: lltok::kw_asm, ErrMsg: "expected 'module asm'") ||
629 parseStringConstant(Result&: AsmStr))
630 return true;
631
632 M->appendModuleInlineAsm(Asm: AsmStr);
633 return false;
634}
635
636/// toplevelentity
637/// ::= 'target' 'triple' '=' STRINGCONSTANT
638/// ::= 'target' 'datalayout' '=' STRINGCONSTANT
639bool LLParser::parseTargetDefinition(std::string &TentativeDLStr,
640 LocTy &DLStrLoc) {
641 assert(Lex.getKind() == lltok::kw_target);
642 std::string Str;
643 switch (Lex.Lex()) {
644 default:
645 return tokError(Msg: "unknown target property");
646 case lltok::kw_triple:
647 Lex.Lex();
648 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after target triple") ||
649 parseStringConstant(Result&: Str))
650 return true;
651 M->setTargetTriple(Triple(std::move(Str)));
652 return false;
653 case lltok::kw_datalayout:
654 Lex.Lex();
655 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after target datalayout"))
656 return true;
657 DLStrLoc = Lex.getLoc();
658 if (parseStringConstant(Result&: TentativeDLStr))
659 return true;
660 return false;
661 }
662}
663
664/// toplevelentity
665/// ::= 'source_filename' '=' STRINGCONSTANT
666bool LLParser::parseSourceFileName() {
667 assert(Lex.getKind() == lltok::kw_source_filename);
668 Lex.Lex();
669 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after source_filename") ||
670 parseStringConstant(Result&: SourceFileName))
671 return true;
672 if (M)
673 M->setSourceFileName(SourceFileName);
674 return false;
675}
676
677/// parseUnnamedType:
678/// ::= LocalVarID '=' 'type' type
679bool LLParser::parseUnnamedType() {
680 LocTy TypeLoc = Lex.getLoc();
681 unsigned TypeID = Lex.getUIntVal();
682 Lex.Lex(); // eat LocalVarID;
683
684 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name") ||
685 parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' after '='"))
686 return true;
687
688 Type *Result = nullptr;
689 if (parseStructDefinition(TypeLoc, Name: "", Entry&: NumberedTypes[TypeID], ResultTy&: Result))
690 return true;
691
692 if (!isa<StructType>(Val: Result)) {
693 std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID];
694 if (Entry.first)
695 return error(L: TypeLoc, Msg: "non-struct types may not be recursive");
696 Entry.first = Result;
697 Entry.second = SMLoc();
698 }
699
700 return false;
701}
702
703/// toplevelentity
704/// ::= LocalVar '=' 'type' type
705bool LLParser::parseNamedType() {
706 std::string Name = Lex.getStrVal();
707 LocTy NameLoc = Lex.getLoc();
708 Lex.Lex(); // eat LocalVar.
709
710 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name") ||
711 parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' after name"))
712 return true;
713
714 Type *Result = nullptr;
715 if (parseStructDefinition(TypeLoc: NameLoc, Name, Entry&: NamedTypes[Name], ResultTy&: Result))
716 return true;
717
718 if (!isa<StructType>(Val: Result)) {
719 std::pair<Type*, LocTy> &Entry = NamedTypes[Name];
720 if (Entry.first)
721 return error(L: NameLoc, Msg: "non-struct types may not be recursive");
722 Entry.first = Result;
723 Entry.second = SMLoc();
724 }
725
726 return false;
727}
728
729/// toplevelentity
730/// ::= 'declare' FunctionHeader
731bool LLParser::parseDeclare() {
732 assert(Lex.getKind() == lltok::kw_declare);
733 Lex.Lex();
734
735 std::vector<std::pair<unsigned, MDNode *>> MDs;
736 while (Lex.getKind() == lltok::MetadataVar) {
737 unsigned MDK;
738 MDNode *N;
739 if (parseMetadataAttachment(Kind&: MDK, MD&: N))
740 return true;
741 MDs.push_back(x: {MDK, N});
742 }
743
744 Function *F;
745 unsigned FunctionNumber = -1;
746 SmallVector<unsigned> UnnamedArgNums;
747 if (parseFunctionHeader(Fn&: F, IsDefine: false, FunctionNumber, UnnamedArgNums))
748 return true;
749 for (auto &MD : MDs)
750 F->addMetadata(KindID: MD.first, MD&: *MD.second);
751 return false;
752}
753
754/// toplevelentity
755/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
756bool LLParser::parseDefine() {
757 assert(Lex.getKind() == lltok::kw_define);
758
759 FileLoc FunctionStart = getTokLineColumnPos();
760 Lex.Lex();
761
762 Function *F;
763 unsigned FunctionNumber = -1;
764 SmallVector<unsigned> UnnamedArgNums;
765 bool RetValue =
766 parseFunctionHeader(Fn&: F, IsDefine: true, FunctionNumber, UnnamedArgNums) ||
767 parseOptionalFunctionMetadata(F&: *F) ||
768 parseFunctionBody(Fn&: *F, FunctionNumber, UnnamedArgNums);
769 if (ParserContext)
770 ParserContext->addFunctionLocation(
771 F, FileLocRange(FunctionStart, getPrevTokEndLineColumnPos()));
772
773 return RetValue;
774}
775
776/// parseGlobalType
777/// ::= 'constant'
778/// ::= 'global'
779bool LLParser::parseGlobalType(bool &IsConstant) {
780 if (Lex.getKind() == lltok::kw_constant)
781 IsConstant = true;
782 else if (Lex.getKind() == lltok::kw_global)
783 IsConstant = false;
784 else {
785 IsConstant = false;
786 return tokError(Msg: "expected 'global' or 'constant'");
787 }
788 Lex.Lex();
789 return false;
790}
791
792bool LLParser::parseOptionalUnnamedAddr(
793 GlobalVariable::UnnamedAddr &UnnamedAddr) {
794 if (EatIfPresent(T: lltok::kw_unnamed_addr))
795 UnnamedAddr = GlobalValue::UnnamedAddr::Global;
796 else if (EatIfPresent(T: lltok::kw_local_unnamed_addr))
797 UnnamedAddr = GlobalValue::UnnamedAddr::Local;
798 else
799 UnnamedAddr = GlobalValue::UnnamedAddr::None;
800 return false;
801}
802
803/// parseUnnamedGlobal:
804/// OptionalVisibility (ALIAS | IFUNC) ...
805/// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
806/// OptionalDLLStorageClass
807/// ... -> global variable
808/// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ...
809/// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier
810/// OptionalVisibility
811/// OptionalDLLStorageClass
812/// ... -> global variable
813bool LLParser::parseUnnamedGlobal() {
814 unsigned VarID;
815 std::string Name;
816 LocTy NameLoc = Lex.getLoc();
817
818 // Handle the GlobalID form.
819 if (Lex.getKind() == lltok::GlobalID) {
820 VarID = Lex.getUIntVal();
821 if (checkValueID(L: NameLoc, Kind: "global", Prefix: "@", NextID: NumberedVals.getNext(), ID: VarID))
822 return true;
823
824 Lex.Lex(); // eat GlobalID;
825 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name"))
826 return true;
827 } else {
828 VarID = NumberedVals.getNext();
829 }
830
831 bool HasLinkage;
832 unsigned Linkage, Visibility, DLLStorageClass;
833 bool DSOLocal;
834 GlobalVariable::ThreadLocalMode TLM;
835 GlobalVariable::UnnamedAddr UnnamedAddr;
836 if (parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass,
837 DSOLocal) ||
838 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
839 return true;
840
841 switch (Lex.getKind()) {
842 default:
843 return parseGlobal(Name, NameID: VarID, NameLoc, Linkage, HasLinkage, Visibility,
844 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
845 case lltok::kw_alias:
846 case lltok::kw_ifunc:
847 return parseAliasOrIFunc(Name, NameID: VarID, NameLoc, L: Linkage, Visibility,
848 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
849 }
850}
851
852/// parseNamedGlobal:
853/// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ...
854/// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
855/// OptionalVisibility OptionalDLLStorageClass
856/// ... -> global variable
857bool LLParser::parseNamedGlobal() {
858 assert(Lex.getKind() == lltok::GlobalVar);
859 LocTy NameLoc = Lex.getLoc();
860 std::string Name = Lex.getStrVal();
861 Lex.Lex();
862
863 bool HasLinkage;
864 unsigned Linkage, Visibility, DLLStorageClass;
865 bool DSOLocal;
866 GlobalVariable::ThreadLocalMode TLM;
867 GlobalVariable::UnnamedAddr UnnamedAddr;
868 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' in global variable") ||
869 parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass,
870 DSOLocal) ||
871 parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr))
872 return true;
873
874 switch (Lex.getKind()) {
875 default:
876 return parseGlobal(Name, NameID: -1, NameLoc, Linkage, HasLinkage, Visibility,
877 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
878 case lltok::kw_alias:
879 case lltok::kw_ifunc:
880 return parseAliasOrIFunc(Name, NameID: -1, NameLoc, L: Linkage, Visibility,
881 DLLStorageClass, DSOLocal, TLM, UnnamedAddr);
882 }
883}
884
885bool LLParser::parseComdat() {
886 assert(Lex.getKind() == lltok::ComdatVar);
887 std::string Name = Lex.getStrVal();
888 LocTy NameLoc = Lex.getLoc();
889 Lex.Lex();
890
891 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here"))
892 return true;
893
894 if (parseToken(T: lltok::kw_comdat, ErrMsg: "expected comdat keyword"))
895 return tokError(Msg: "expected comdat type");
896
897 Comdat::SelectionKind SK;
898 switch (Lex.getKind()) {
899 default:
900 return tokError(Msg: "unknown selection kind");
901 case lltok::kw_any:
902 SK = Comdat::Any;
903 break;
904 case lltok::kw_exactmatch:
905 SK = Comdat::ExactMatch;
906 break;
907 case lltok::kw_largest:
908 SK = Comdat::Largest;
909 break;
910 case lltok::kw_nodeduplicate:
911 SK = Comdat::NoDeduplicate;
912 break;
913 case lltok::kw_samesize:
914 SK = Comdat::SameSize;
915 break;
916 }
917 Lex.Lex();
918
919 // See if the comdat was forward referenced, if so, use the comdat.
920 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
921 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Key: Name);
922 if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(x: Name))
923 return error(L: NameLoc, Msg: "redefinition of comdat '$" + Name + "'");
924
925 Comdat *C;
926 if (I != ComdatSymTab.end())
927 C = &I->second;
928 else
929 C = M->getOrInsertComdat(Name);
930 C->setSelectionKind(SK);
931
932 return false;
933}
934
935// MDString:
936// ::= '!' STRINGCONSTANT
937bool LLParser::parseMDString(MDString *&Result) {
938 std::string Str;
939 if (parseStringConstant(Result&: Str))
940 return true;
941 Result = MDString::get(Context, Str);
942 return false;
943}
944
945// MDNode:
946// ::= '!' MDNodeNumber
947bool LLParser::parseMDNodeID(MDNode *&Result) {
948 // !{ ..., !42, ... }
949 LocTy IDLoc = Lex.getLoc();
950 unsigned MID = 0;
951 if (parseUInt32(Val&: MID))
952 return true;
953
954 // If not a forward reference, just return it now.
955 auto [It, Inserted] = NumberedMetadata.try_emplace(k: MID);
956 if (!Inserted) {
957 Result = It->second;
958 return false;
959 }
960
961 // Otherwise, create MDNode forward reference.
962 auto &FwdRef = ForwardRefMDNodes[MID];
963 FwdRef = std::make_pair(x: MDTuple::getTemporary(Context, MDs: {}), y&: IDLoc);
964
965 Result = FwdRef.first.get();
966 It->second.reset(MD: Result);
967 return false;
968}
969
970/// parseNamedMetadata:
971/// !foo = !{ !1, !2 }
972bool LLParser::parseNamedMetadata() {
973 assert(Lex.getKind() == lltok::MetadataVar);
974 std::string Name = Lex.getStrVal();
975 Lex.Lex();
976
977 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here") ||
978 parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here") ||
979 parseToken(T: lltok::lbrace, ErrMsg: "Expected '{' here"))
980 return true;
981
982 NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name);
983 if (Lex.getKind() != lltok::rbrace)
984 do {
985 MDNode *N = nullptr;
986 // parse DIExpressions inline as a special case. They are still MDNodes,
987 // so they can still appear in named metadata. Remove this logic if they
988 // become plain Metadata.
989 if (Lex.getKind() == lltok::MetadataVar &&
990 Lex.getStrVal() == "DIExpression") {
991 if (parseDIExpression(Result&: N, /*IsDistinct=*/false))
992 return true;
993 // DIArgLists should only appear inline in a function, as they may
994 // contain LocalAsMetadata arguments which require a function context.
995 } else if (Lex.getKind() == lltok::MetadataVar &&
996 Lex.getStrVal() == "DIArgList") {
997 return tokError(Msg: "found DIArgList outside of function");
998 } else if (parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here") ||
999 parseMDNodeID(Result&: N)) {
1000 return true;
1001 }
1002 NMD->addOperand(M: N);
1003 } while (EatIfPresent(T: lltok::comma));
1004
1005 return parseToken(T: lltok::rbrace, ErrMsg: "expected end of metadata node");
1006}
1007
1008/// parseStandaloneMetadata:
1009/// !42 = !{...}
1010bool LLParser::parseStandaloneMetadata() {
1011 assert(Lex.getKind() == lltok::exclaim);
1012 Lex.Lex();
1013 unsigned MetadataID = 0;
1014
1015 MDNode *Init;
1016 if (parseUInt32(Val&: MetadataID) || parseToken(T: lltok::equal, ErrMsg: "expected '=' here"))
1017 return true;
1018
1019 // Detect common error, from old metadata syntax.
1020 if (Lex.getKind() == lltok::Type)
1021 return tokError(Msg: "unexpected type in metadata definition");
1022
1023 bool IsDistinct = EatIfPresent(T: lltok::kw_distinct);
1024 if (Lex.getKind() == lltok::MetadataVar) {
1025 if (parseSpecializedMDNode(N&: Init, IsDistinct))
1026 return true;
1027 } else if (parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here") ||
1028 parseMDTuple(MD&: Init, IsDistinct))
1029 return true;
1030
1031 // See if this was forward referenced, if so, handle it.
1032 auto FI = ForwardRefMDNodes.find(x: MetadataID);
1033 if (FI != ForwardRefMDNodes.end()) {
1034 auto *ToReplace = FI->second.first.get();
1035 // DIAssignID has its own special forward-reference "replacement" for
1036 // attachments (the temporary attachments are never actually attached).
1037 if (isa<DIAssignID>(Val: Init)) {
1038 for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) {
1039 assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) &&
1040 "Inst unexpectedly already has DIAssignID attachment");
1041 Inst->setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: Init);
1042 }
1043 }
1044
1045 ToReplace->replaceAllUsesWith(MD: Init);
1046 ForwardRefMDNodes.erase(position: FI);
1047
1048 assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work");
1049 } else {
1050 auto [It, Inserted] = NumberedMetadata.try_emplace(k: MetadataID);
1051 if (!Inserted)
1052 return tokError(Msg: "Metadata id is already used");
1053 It->second.reset(MD: Init);
1054 }
1055
1056 return false;
1057}
1058
1059// Skips a single module summary entry.
1060bool LLParser::skipModuleSummaryEntry() {
1061 // Each module summary entry consists of a tag for the entry
1062 // type, followed by a colon, then the fields which may be surrounded by
1063 // nested sets of parentheses. The "tag:" looks like a Label. Once parsing
1064 // support is in place we will look for the tokens corresponding to the
1065 // expected tags.
1066 if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module &&
1067 Lex.getKind() != lltok::kw_typeid && Lex.getKind() != lltok::kw_flags &&
1068 Lex.getKind() != lltok::kw_blockcount)
1069 return tokError(
1070 Msg: "Expected 'gv', 'module', 'typeid', 'flags' or 'blockcount' at the "
1071 "start of summary entry");
1072 if (Lex.getKind() == lltok::kw_flags)
1073 return parseSummaryIndexFlags();
1074 if (Lex.getKind() == lltok::kw_blockcount)
1075 return parseBlockCount();
1076 Lex.Lex();
1077 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' at start of summary entry") ||
1078 parseToken(T: lltok::lparen, ErrMsg: "expected '(' at start of summary entry"))
1079 return true;
1080 // Now walk through the parenthesized entry, until the number of open
1081 // parentheses goes back down to 0 (the first '(' was parsed above).
1082 unsigned NumOpenParen = 1;
1083 do {
1084 switch (Lex.getKind()) {
1085 case lltok::lparen:
1086 NumOpenParen++;
1087 break;
1088 case lltok::rparen:
1089 NumOpenParen--;
1090 break;
1091 case lltok::Eof:
1092 return tokError(Msg: "found end of file while parsing summary entry");
1093 default:
1094 // Skip everything in between parentheses.
1095 break;
1096 }
1097 Lex.Lex();
1098 } while (NumOpenParen > 0);
1099 return false;
1100}
1101
1102/// SummaryEntry
1103/// ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry
1104bool LLParser::parseSummaryEntry() {
1105 assert(Lex.getKind() == lltok::SummaryID);
1106 unsigned SummaryID = Lex.getUIntVal();
1107
1108 // For summary entries, colons should be treated as distinct tokens,
1109 // not an indication of the end of a label token.
1110 Lex.setIgnoreColonInIdentifiers(true);
1111
1112 Lex.Lex();
1113 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here"))
1114 return true;
1115
1116 // If we don't have an index object, skip the summary entry.
1117 if (!Index)
1118 return skipModuleSummaryEntry();
1119
1120 bool result = false;
1121 switch (Lex.getKind()) {
1122 case lltok::kw_gv:
1123 result = parseGVEntry(ID: SummaryID);
1124 break;
1125 case lltok::kw_module:
1126 result = parseModuleEntry(ID: SummaryID);
1127 break;
1128 case lltok::kw_typeid:
1129 result = parseTypeIdEntry(ID: SummaryID);
1130 break;
1131 case lltok::kw_typeidCompatibleVTable:
1132 result = parseTypeIdCompatibleVtableEntry(ID: SummaryID);
1133 break;
1134 case lltok::kw_flags:
1135 result = parseSummaryIndexFlags();
1136 break;
1137 case lltok::kw_blockcount:
1138 result = parseBlockCount();
1139 break;
1140 default:
1141 result = error(L: Lex.getLoc(), Msg: "unexpected summary kind");
1142 break;
1143 }
1144 Lex.setIgnoreColonInIdentifiers(false);
1145 return result;
1146}
1147
1148static bool isValidVisibilityForLinkage(unsigned V, unsigned L) {
1149 return !GlobalValue::isLocalLinkage(Linkage: (GlobalValue::LinkageTypes)L) ||
1150 (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility;
1151}
1152static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L) {
1153 return !GlobalValue::isLocalLinkage(Linkage: (GlobalValue::LinkageTypes)L) ||
1154 (GlobalValue::DLLStorageClassTypes)S == GlobalValue::DefaultStorageClass;
1155}
1156
1157// If there was an explicit dso_local, update GV. In the absence of an explicit
1158// dso_local we keep the default value.
1159static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) {
1160 if (DSOLocal)
1161 GV.setDSOLocal(true);
1162}
1163
1164/// parseAliasOrIFunc:
1165/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1166/// OptionalVisibility OptionalDLLStorageClass
1167/// OptionalThreadLocal OptionalUnnamedAddr
1168/// 'alias|ifunc' AliaseeOrResolver SymbolAttrs*
1169///
1170/// AliaseeOrResolver
1171/// ::= TypeAndValue
1172///
1173/// SymbolAttrs
1174/// ::= ',' 'partition' StringConstant
1175///
1176/// Everything through OptionalUnnamedAddr has already been parsed.
1177///
1178bool LLParser::parseAliasOrIFunc(const std::string &Name, unsigned NameID,
1179 LocTy NameLoc, unsigned L, unsigned Visibility,
1180 unsigned DLLStorageClass, bool DSOLocal,
1181 GlobalVariable::ThreadLocalMode TLM,
1182 GlobalVariable::UnnamedAddr UnnamedAddr) {
1183 bool IsAlias;
1184 if (Lex.getKind() == lltok::kw_alias)
1185 IsAlias = true;
1186 else if (Lex.getKind() == lltok::kw_ifunc)
1187 IsAlias = false;
1188 else
1189 llvm_unreachable("Not an alias or ifunc!");
1190 Lex.Lex();
1191
1192 GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L;
1193
1194 if(IsAlias && !GlobalAlias::isValidLinkage(L: Linkage))
1195 return error(L: NameLoc, Msg: "invalid linkage type for alias");
1196
1197 if (!isValidVisibilityForLinkage(V: Visibility, L))
1198 return error(L: NameLoc,
1199 Msg: "symbol with local linkage must have default visibility");
1200
1201 if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L))
1202 return error(L: NameLoc,
1203 Msg: "symbol with local linkage cannot have a DLL storage class");
1204
1205 Type *Ty;
1206 LocTy ExplicitTypeLoc = Lex.getLoc();
1207 if (parseType(Result&: Ty) ||
1208 parseToken(T: lltok::comma, ErrMsg: "expected comma after alias or ifunc's type"))
1209 return true;
1210
1211 Constant *Aliasee;
1212 LocTy AliaseeLoc = Lex.getLoc();
1213 if (Lex.getKind() != lltok::kw_bitcast &&
1214 Lex.getKind() != lltok::kw_getelementptr &&
1215 Lex.getKind() != lltok::kw_addrspacecast &&
1216 Lex.getKind() != lltok::kw_inttoptr) {
1217 if (parseGlobalTypeAndValue(V&: Aliasee))
1218 return true;
1219 } else {
1220 // The bitcast dest type is not present, it is implied by the dest type.
1221 ValID ID;
1222 if (parseValID(ID, /*PFS=*/nullptr))
1223 return true;
1224 if (ID.Kind != ValID::t_Constant)
1225 return error(L: AliaseeLoc, Msg: "invalid aliasee");
1226 Aliasee = ID.ConstantVal;
1227 }
1228
1229 Type *AliaseeType = Aliasee->getType();
1230 auto *PTy = dyn_cast<PointerType>(Val: AliaseeType);
1231 if (!PTy)
1232 return error(L: AliaseeLoc, Msg: "An alias or ifunc must have pointer type");
1233 unsigned AddrSpace = PTy->getAddressSpace();
1234
1235 GlobalValue *GVal = nullptr;
1236
1237 // See if the alias was forward referenced, if so, prepare to replace the
1238 // forward reference.
1239 if (!Name.empty()) {
1240 auto I = ForwardRefVals.find(x: Name);
1241 if (I != ForwardRefVals.end()) {
1242 GVal = I->second.first;
1243 ForwardRefVals.erase(x: Name);
1244 } else if (M->getNamedValue(Name)) {
1245 return error(L: NameLoc, Msg: "redefinition of global '@" + Name + "'");
1246 }
1247 } else {
1248 auto I = ForwardRefValIDs.find(x: NameID);
1249 if (I != ForwardRefValIDs.end()) {
1250 GVal = I->second.first;
1251 ForwardRefValIDs.erase(position: I);
1252 }
1253 }
1254
1255 // Okay, create the alias/ifunc but do not insert it into the module yet.
1256 std::unique_ptr<GlobalAlias> GA;
1257 std::unique_ptr<GlobalIFunc> GI;
1258 GlobalValue *GV;
1259 if (IsAlias) {
1260 GA.reset(p: GlobalAlias::create(Ty, AddressSpace: AddrSpace, Linkage, Name, Aliasee,
1261 /*Parent=*/nullptr));
1262 GV = GA.get();
1263 } else {
1264 GI.reset(p: GlobalIFunc::create(Ty, AddressSpace: AddrSpace, Linkage, Name, Resolver: Aliasee,
1265 /*Parent=*/nullptr));
1266 GV = GI.get();
1267 }
1268 GV->setThreadLocalMode(TLM);
1269 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
1270 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
1271 GV->setUnnamedAddr(UnnamedAddr);
1272 maybeSetDSOLocal(DSOLocal, GV&: *GV);
1273
1274 // At this point we've parsed everything except for the IndirectSymbolAttrs.
1275 // Now parse them if there are any.
1276 while (Lex.getKind() == lltok::comma) {
1277 Lex.Lex();
1278
1279 if (Lex.getKind() == lltok::kw_partition) {
1280 Lex.Lex();
1281 GV->setPartition(Lex.getStrVal());
1282 if (parseToken(T: lltok::StringConstant, ErrMsg: "expected partition string"))
1283 return true;
1284 } else if (!IsAlias && Lex.getKind() == lltok::MetadataVar) {
1285 if (parseGlobalObjectMetadataAttachment(GO&: *GI))
1286 return true;
1287 } else {
1288 return tokError(Msg: "unknown alias or ifunc property!");
1289 }
1290 }
1291
1292 if (Name.empty())
1293 NumberedVals.add(ID: NameID, V: GV);
1294
1295 if (GVal) {
1296 // Verify that types agree.
1297 if (GVal->getType() != GV->getType())
1298 return error(
1299 L: ExplicitTypeLoc,
1300 Msg: "forward reference and definition of alias have different types");
1301
1302 // If they agree, just RAUW the old value with the alias and remove the
1303 // forward ref info.
1304 GVal->replaceAllUsesWith(V: GV);
1305 GVal->eraseFromParent();
1306 }
1307
1308 // Insert into the module, we know its name won't collide now.
1309 if (IsAlias)
1310 M->insertAlias(Alias: GA.release());
1311 else
1312 M->insertIFunc(IFunc: GI.release());
1313 assert(GV->getName() == Name && "Should not be a name conflict!");
1314
1315 return false;
1316}
1317
1318static bool isSanitizer(lltok::Kind Kind) {
1319 switch (Kind) {
1320 case lltok::kw_no_sanitize_address:
1321 case lltok::kw_no_sanitize_hwaddress:
1322 case lltok::kw_sanitize_memtag:
1323 case lltok::kw_sanitize_address_dyninit:
1324 return true;
1325 default:
1326 return false;
1327 }
1328}
1329
1330bool LLParser::parseSanitizer(GlobalVariable *GV) {
1331 using SanitizerMetadata = GlobalValue::SanitizerMetadata;
1332 SanitizerMetadata Meta;
1333 if (GV->hasSanitizerMetadata())
1334 Meta = GV->getSanitizerMetadata();
1335
1336 switch (Lex.getKind()) {
1337 case lltok::kw_no_sanitize_address:
1338 Meta.NoAddress = true;
1339 break;
1340 case lltok::kw_no_sanitize_hwaddress:
1341 Meta.NoHWAddress = true;
1342 break;
1343 case lltok::kw_sanitize_memtag:
1344 Meta.Memtag = true;
1345 break;
1346 case lltok::kw_sanitize_address_dyninit:
1347 Meta.IsDynInit = true;
1348 break;
1349 default:
1350 return tokError(Msg: "non-sanitizer token passed to LLParser::parseSanitizer()");
1351 }
1352 GV->setSanitizerMetadata(Meta);
1353 Lex.Lex();
1354 return false;
1355}
1356
1357/// parseGlobal
1358/// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier
1359/// OptionalVisibility OptionalDLLStorageClass
1360/// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace
1361/// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs
1362/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
1363/// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr
1364/// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type
1365/// Const OptionalAttrs
1366///
1367/// Everything up to and including OptionalUnnamedAddr has been parsed
1368/// already.
1369///
1370bool LLParser::parseGlobal(const std::string &Name, unsigned NameID,
1371 LocTy NameLoc, unsigned Linkage, bool HasLinkage,
1372 unsigned Visibility, unsigned DLLStorageClass,
1373 bool DSOLocal, GlobalVariable::ThreadLocalMode TLM,
1374 GlobalVariable::UnnamedAddr UnnamedAddr) {
1375 if (!isValidVisibilityForLinkage(V: Visibility, L: Linkage))
1376 return error(L: NameLoc,
1377 Msg: "symbol with local linkage must have default visibility");
1378
1379 if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L: Linkage))
1380 return error(L: NameLoc,
1381 Msg: "symbol with local linkage cannot have a DLL storage class");
1382
1383 unsigned AddrSpace;
1384 bool IsConstant, IsExternallyInitialized;
1385 LocTy IsExternallyInitializedLoc;
1386 LocTy TyLoc;
1387
1388 Type *Ty = nullptr;
1389 if (parseOptionalAddrSpace(AddrSpace) ||
1390 parseOptionalToken(T: lltok::kw_externally_initialized,
1391 Present&: IsExternallyInitialized,
1392 Loc: &IsExternallyInitializedLoc) ||
1393 parseGlobalType(IsConstant) || parseType(Result&: Ty, Loc&: TyLoc))
1394 return true;
1395
1396 // If the linkage is specified and is external, then no initializer is
1397 // present.
1398 Constant *Init = nullptr;
1399 if (!HasLinkage ||
1400 !GlobalValue::isValidDeclarationLinkage(
1401 Linkage: (GlobalValue::LinkageTypes)Linkage)) {
1402 if (parseGlobalValue(Ty, C&: Init))
1403 return true;
1404 }
1405
1406 if (Ty->isFunctionTy() || !PointerType::isValidElementType(ElemTy: Ty))
1407 return error(L: TyLoc, Msg: "invalid type for global variable");
1408
1409 GlobalValue *GVal = nullptr;
1410
1411 // See if the global was forward referenced, if so, use the global.
1412 if (!Name.empty()) {
1413 auto I = ForwardRefVals.find(x: Name);
1414 if (I != ForwardRefVals.end()) {
1415 GVal = I->second.first;
1416 ForwardRefVals.erase(position: I);
1417 } else if (M->getNamedValue(Name)) {
1418 return error(L: NameLoc, Msg: "redefinition of global '@" + Name + "'");
1419 }
1420 } else {
1421 // Handle @"", where a name is syntactically specified, but semantically
1422 // missing.
1423 if (NameID == (unsigned)-1)
1424 NameID = NumberedVals.getNext();
1425
1426 auto I = ForwardRefValIDs.find(x: NameID);
1427 if (I != ForwardRefValIDs.end()) {
1428 GVal = I->second.first;
1429 ForwardRefValIDs.erase(position: I);
1430 }
1431 }
1432
1433 GlobalVariable *GV = new GlobalVariable(
1434 *M, Ty, false, GlobalValue::ExternalLinkage, nullptr, Name, nullptr,
1435 GlobalVariable::NotThreadLocal, AddrSpace);
1436
1437 if (Name.empty())
1438 NumberedVals.add(ID: NameID, V: GV);
1439
1440 // Set the parsed properties on the global.
1441 if (Init)
1442 GV->setInitializer(Init);
1443 GV->setConstant(IsConstant);
1444 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
1445 maybeSetDSOLocal(DSOLocal, GV&: *GV);
1446 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
1447 GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
1448 GV->setExternallyInitialized(IsExternallyInitialized);
1449 GV->setThreadLocalMode(TLM);
1450 GV->setUnnamedAddr(UnnamedAddr);
1451
1452 if (GVal) {
1453 if (GVal->getAddressSpace() != AddrSpace)
1454 return error(
1455 L: TyLoc,
1456 Msg: "forward reference and definition of global have different types");
1457
1458 GVal->replaceAllUsesWith(V: GV);
1459 GVal->eraseFromParent();
1460 }
1461
1462 // parse attributes on the global.
1463 while (Lex.getKind() == lltok::comma) {
1464 Lex.Lex();
1465
1466 if (Lex.getKind() == lltok::kw_section) {
1467 Lex.Lex();
1468 GV->setSection(Lex.getStrVal());
1469 if (parseToken(T: lltok::StringConstant, ErrMsg: "expected global section string"))
1470 return true;
1471 } else if (Lex.getKind() == lltok::kw_partition) {
1472 Lex.Lex();
1473 GV->setPartition(Lex.getStrVal());
1474 if (parseToken(T: lltok::StringConstant, ErrMsg: "expected partition string"))
1475 return true;
1476 } else if (Lex.getKind() == lltok::kw_align) {
1477 MaybeAlign Alignment;
1478 if (parseOptionalAlignment(Alignment))
1479 return true;
1480 if (Alignment)
1481 GV->setAlignment(*Alignment);
1482 } else if (Lex.getKind() == lltok::kw_code_model) {
1483 CodeModel::Model CodeModel;
1484 if (parseOptionalCodeModel(model&: CodeModel))
1485 return true;
1486 GV->setCodeModel(CodeModel);
1487 } else if (Lex.getKind() == lltok::MetadataVar) {
1488 if (parseGlobalObjectMetadataAttachment(GO&: *GV))
1489 return true;
1490 } else if (isSanitizer(Kind: Lex.getKind())) {
1491 if (parseSanitizer(GV))
1492 return true;
1493 } else {
1494 Comdat *C;
1495 if (parseOptionalComdat(GlobalName: Name, C))
1496 return true;
1497 if (C)
1498 GV->setComdat(C);
1499 else
1500 return tokError(Msg: "unknown global variable property!");
1501 }
1502 }
1503
1504 AttrBuilder Attrs(M->getContext());
1505 LocTy BuiltinLoc;
1506 std::vector<unsigned> FwdRefAttrGrps;
1507 if (parseFnAttributeValuePairs(B&: Attrs, FwdRefAttrGrps, inAttrGrp: false, BuiltinLoc))
1508 return true;
1509 if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) {
1510 GV->setAttributes(AttributeSet::get(C&: Context, B: Attrs));
1511 ForwardRefAttrGroups[GV] = FwdRefAttrGrps;
1512 }
1513
1514 return false;
1515}
1516
1517/// parseUnnamedAttrGrp
1518/// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}'
1519bool LLParser::parseUnnamedAttrGrp() {
1520 assert(Lex.getKind() == lltok::kw_attributes);
1521 LocTy AttrGrpLoc = Lex.getLoc();
1522 Lex.Lex();
1523
1524 if (Lex.getKind() != lltok::AttrGrpID)
1525 return tokError(Msg: "expected attribute group id");
1526
1527 unsigned VarID = Lex.getUIntVal();
1528 std::vector<unsigned> unused;
1529 LocTy BuiltinLoc;
1530 Lex.Lex();
1531
1532 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here") ||
1533 parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here"))
1534 return true;
1535
1536 auto R = NumberedAttrBuilders.find(x: VarID);
1537 if (R == NumberedAttrBuilders.end())
1538 R = NumberedAttrBuilders.emplace(args&: VarID, args: AttrBuilder(M->getContext())).first;
1539
1540 if (parseFnAttributeValuePairs(B&: R->second, FwdRefAttrGrps&: unused, inAttrGrp: true, BuiltinLoc) ||
1541 parseToken(T: lltok::rbrace, ErrMsg: "expected end of attribute group"))
1542 return true;
1543
1544 if (!R->second.hasAttributes())
1545 return error(L: AttrGrpLoc, Msg: "attribute group has no attributes");
1546
1547 return false;
1548}
1549
1550static Attribute::AttrKind tokenToAttribute(lltok::Kind Kind) {
1551 switch (Kind) {
1552#define GET_ATTR_NAMES
1553#define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \
1554 case lltok::kw_##DISPLAY_NAME: \
1555 return Attribute::ENUM_NAME;
1556#include "llvm/IR/Attributes.inc"
1557 default:
1558 return Attribute::None;
1559 }
1560}
1561
1562bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
1563 bool InAttrGroup) {
1564 if (Attribute::isTypeAttrKind(Kind: Attr))
1565 return parseRequiredTypeAttr(B, AttrToken: Lex.getKind(), AttrKind: Attr);
1566
1567 switch (Attr) {
1568 case Attribute::Alignment: {
1569 MaybeAlign Alignment;
1570 if (InAttrGroup) {
1571 uint32_t Value = 0;
1572 Lex.Lex();
1573 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here") || parseUInt32(Val&: Value))
1574 return true;
1575 Alignment = Align(Value);
1576 } else {
1577 if (parseOptionalAlignment(Alignment, AllowParens: true))
1578 return true;
1579 }
1580 B.addAlignmentAttr(Align: Alignment);
1581 return false;
1582 }
1583 case Attribute::StackAlignment: {
1584 unsigned Alignment;
1585 if (InAttrGroup) {
1586 Lex.Lex();
1587 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here") ||
1588 parseUInt32(Val&: Alignment))
1589 return true;
1590 } else {
1591 if (parseOptionalStackAlignment(Alignment))
1592 return true;
1593 }
1594 B.addStackAlignmentAttr(Align: Alignment);
1595 return false;
1596 }
1597 case Attribute::AllocSize: {
1598 unsigned ElemSizeArg;
1599 std::optional<unsigned> NumElemsArg;
1600 if (parseAllocSizeArguments(BaseSizeArg&: ElemSizeArg, HowManyArg&: NumElemsArg))
1601 return true;
1602 B.addAllocSizeAttr(ElemSizeArg, NumElemsArg);
1603 return false;
1604 }
1605 case Attribute::VScaleRange: {
1606 unsigned MinValue, MaxValue;
1607 if (parseVScaleRangeArguments(MinValue, MaxValue))
1608 return true;
1609 B.addVScaleRangeAttr(MinValue,
1610 MaxValue: MaxValue > 0 ? MaxValue : std::optional<unsigned>());
1611 return false;
1612 }
1613 case Attribute::Dereferenceable: {
1614 std::optional<uint64_t> Bytes;
1615 if (parseOptionalAttrBytes(AttrKind: lltok::kw_dereferenceable, Bytes))
1616 return true;
1617 assert(Bytes.has_value());
1618 B.addDereferenceableAttr(Bytes: Bytes.value());
1619 return false;
1620 }
1621 case Attribute::DeadOnReturn: {
1622 std::optional<uint64_t> Bytes;
1623 if (parseOptionalAttrBytes(AttrKind: lltok::kw_dead_on_return, Bytes,
1624 /*ErrorNoBytes=*/false))
1625 return true;
1626 if (Bytes.has_value()) {
1627 B.addDeadOnReturnAttr(Info: DeadOnReturnInfo(Bytes.value()));
1628 } else {
1629 B.addDeadOnReturnAttr(Info: DeadOnReturnInfo());
1630 }
1631 return false;
1632 }
1633 case Attribute::DereferenceableOrNull: {
1634 std::optional<uint64_t> Bytes;
1635 if (parseOptionalAttrBytes(AttrKind: lltok::kw_dereferenceable_or_null, Bytes))
1636 return true;
1637 assert(Bytes.has_value());
1638 B.addDereferenceableOrNullAttr(Bytes: Bytes.value());
1639 return false;
1640 }
1641 case Attribute::UWTable: {
1642 UWTableKind Kind;
1643 if (parseOptionalUWTableKind(Kind))
1644 return true;
1645 B.addUWTableAttr(Kind);
1646 return false;
1647 }
1648 case Attribute::AllocKind: {
1649 AllocFnKind Kind = AllocFnKind::Unknown;
1650 if (parseAllocKind(Kind))
1651 return true;
1652 B.addAllocKindAttr(Kind);
1653 return false;
1654 }
1655 case Attribute::Memory: {
1656 std::optional<MemoryEffects> ME = parseMemoryAttr();
1657 if (!ME)
1658 return true;
1659 B.addMemoryAttr(ME: *ME);
1660 return false;
1661 }
1662 case Attribute::DenormalFPEnv: {
1663 std::optional<DenormalFPEnv> Mode = parseDenormalFPEnvAttr();
1664 if (!Mode)
1665 return true;
1666
1667 B.addDenormalFPEnvAttr(Mode: *Mode);
1668 return false;
1669 }
1670 case Attribute::NoFPClass: {
1671 if (FPClassTest NoFPClass =
1672 static_cast<FPClassTest>(parseNoFPClassAttr())) {
1673 B.addNoFPClassAttr(NoFPClassMask: NoFPClass);
1674 return false;
1675 }
1676
1677 return true;
1678 }
1679 case Attribute::Range:
1680 return parseRangeAttr(B);
1681 case Attribute::Initializes:
1682 return parseInitializesAttr(B);
1683 case Attribute::Captures:
1684 return parseCapturesAttr(B);
1685 default:
1686 B.addAttribute(Val: Attr);
1687 Lex.Lex();
1688 return false;
1689 }
1690}
1691
1692static bool upgradeMemoryAttr(MemoryEffects &ME, lltok::Kind Kind) {
1693 switch (Kind) {
1694 case lltok::kw_readnone:
1695 ME &= MemoryEffects::none();
1696 return true;
1697 case lltok::kw_readonly:
1698 ME &= MemoryEffects::readOnly();
1699 return true;
1700 case lltok::kw_writeonly:
1701 ME &= MemoryEffects::writeOnly();
1702 return true;
1703 case lltok::kw_argmemonly:
1704 ME &= MemoryEffects::argMemOnly();
1705 return true;
1706 case lltok::kw_inaccessiblememonly:
1707 ME &= MemoryEffects::inaccessibleMemOnly();
1708 return true;
1709 case lltok::kw_inaccessiblemem_or_argmemonly:
1710 ME &= MemoryEffects::inaccessibleOrArgMemOnly();
1711 return true;
1712 default:
1713 return false;
1714 }
1715}
1716
1717/// parseFnAttributeValuePairs
1718/// ::= <attr> | <attr> '=' <value>
1719bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B,
1720 std::vector<unsigned> &FwdRefAttrGrps,
1721 bool InAttrGrp, LocTy &BuiltinLoc) {
1722 bool HaveError = false;
1723
1724 B.clear();
1725
1726 MemoryEffects ME = MemoryEffects::unknown();
1727 while (true) {
1728 lltok::Kind Token = Lex.getKind();
1729 if (Token == lltok::rbrace)
1730 break; // Finished.
1731
1732 if (Token == lltok::StringConstant) {
1733 if (parseStringAttribute(B))
1734 return true;
1735 continue;
1736 }
1737
1738 if (Token == lltok::AttrGrpID) {
1739 // Allow a function to reference an attribute group:
1740 //
1741 // define void @foo() #1 { ... }
1742 if (InAttrGrp) {
1743 HaveError |= error(
1744 L: Lex.getLoc(),
1745 Msg: "cannot have an attribute group reference in an attribute group");
1746 } else {
1747 // Save the reference to the attribute group. We'll fill it in later.
1748 FwdRefAttrGrps.push_back(x: Lex.getUIntVal());
1749 }
1750 Lex.Lex();
1751 continue;
1752 }
1753
1754 SMLoc Loc = Lex.getLoc();
1755 if (Token == lltok::kw_builtin)
1756 BuiltinLoc = Loc;
1757
1758 if (upgradeMemoryAttr(ME, Kind: Token)) {
1759 Lex.Lex();
1760 continue;
1761 }
1762
1763 Attribute::AttrKind Attr = tokenToAttribute(Kind: Token);
1764 if (Attr == Attribute::None) {
1765 if (!InAttrGrp)
1766 break;
1767 return error(L: Lex.getLoc(), Msg: "unterminated attribute group");
1768 }
1769
1770 if (parseEnumAttribute(Attr, B, InAttrGroup: InAttrGrp))
1771 return true;
1772
1773 // As a hack, we allow function alignment to be initially parsed as an
1774 // attribute on a function declaration/definition or added to an attribute
1775 // group and later moved to the alignment field.
1776 if (!Attribute::canUseAsFnAttr(Kind: Attr) && Attr != Attribute::Alignment)
1777 HaveError |= error(L: Loc, Msg: "this attribute does not apply to functions");
1778 }
1779
1780 if (ME != MemoryEffects::unknown())
1781 B.addMemoryAttr(ME);
1782 return HaveError;
1783}
1784
1785//===----------------------------------------------------------------------===//
1786// GlobalValue Reference/Resolution Routines.
1787//===----------------------------------------------------------------------===//
1788
1789static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy) {
1790 // The used global type does not matter. We will later RAUW it with a
1791 // global/function of the correct type.
1792 return new GlobalVariable(*M, Type::getInt8Ty(C&: M->getContext()), false,
1793 GlobalValue::ExternalWeakLinkage, nullptr, "",
1794 nullptr, GlobalVariable::NotThreadLocal,
1795 PTy->getAddressSpace());
1796}
1797
1798Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
1799 Value *Val) {
1800 Type *ValTy = Val->getType();
1801 if (ValTy == Ty)
1802 return Val;
1803 if (Ty->isLabelTy())
1804 error(L: Loc, Msg: "'" + Name + "' is not a basic block");
1805 else
1806 error(L: Loc, Msg: "'" + Name + "' defined with type '" +
1807 getTypeString(T: Val->getType()) + "' but expected '" +
1808 getTypeString(T: Ty) + "'");
1809 return nullptr;
1810}
1811
1812/// getGlobalVal - Get a value with the specified name or ID, creating a
1813/// forward reference record if needed. This can return null if the value
1814/// exists but does not have the right type.
1815GlobalValue *LLParser::getGlobalVal(const std::string &Name, Type *Ty,
1816 LocTy Loc) {
1817 PointerType *PTy = dyn_cast<PointerType>(Val: Ty);
1818 if (!PTy) {
1819 error(L: Loc, Msg: "global variable reference must have pointer type");
1820 return nullptr;
1821 }
1822
1823 // Look this name up in the normal function symbol table.
1824 GlobalValue *Val =
1825 cast_or_null<GlobalValue>(Val: M->getValueSymbolTable().lookup(Name));
1826
1827 // If this is a forward reference for the value, see if we already created a
1828 // forward ref record.
1829 if (!Val) {
1830 auto I = ForwardRefVals.find(x: Name);
1831 if (I != ForwardRefVals.end())
1832 Val = I->second.first;
1833 }
1834
1835 // If we have the value in the symbol table or fwd-ref table, return it.
1836 if (Val)
1837 return cast_or_null<GlobalValue>(
1838 Val: checkValidVariableType(Loc, Name: "@" + Name, Ty, Val));
1839
1840 // Otherwise, create a new forward reference for this value and remember it.
1841 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1842 ForwardRefVals[Name] = std::make_pair(x&: FwdVal, y&: Loc);
1843 return FwdVal;
1844}
1845
1846GlobalValue *LLParser::getGlobalVal(unsigned ID, Type *Ty, LocTy Loc) {
1847 PointerType *PTy = dyn_cast<PointerType>(Val: Ty);
1848 if (!PTy) {
1849 error(L: Loc, Msg: "global variable reference must have pointer type");
1850 return nullptr;
1851 }
1852
1853 GlobalValue *Val = NumberedVals.get(ID);
1854
1855 // If this is a forward reference for the value, see if we already created a
1856 // forward ref record.
1857 if (!Val) {
1858 auto I = ForwardRefValIDs.find(x: ID);
1859 if (I != ForwardRefValIDs.end())
1860 Val = I->second.first;
1861 }
1862
1863 // If we have the value in the symbol table or fwd-ref table, return it.
1864 if (Val)
1865 return cast_or_null<GlobalValue>(
1866 Val: checkValidVariableType(Loc, Name: "@" + Twine(ID), Ty, Val));
1867
1868 // Otherwise, create a new forward reference for this value and remember it.
1869 GlobalValue *FwdVal = createGlobalFwdRef(M, PTy);
1870 ForwardRefValIDs[ID] = std::make_pair(x&: FwdVal, y&: Loc);
1871 return FwdVal;
1872}
1873
1874//===----------------------------------------------------------------------===//
1875// Comdat Reference/Resolution Routines.
1876//===----------------------------------------------------------------------===//
1877
1878Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) {
1879 // Look this name up in the comdat symbol table.
1880 Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable();
1881 Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Key: Name);
1882 if (I != ComdatSymTab.end())
1883 return &I->second;
1884
1885 // Otherwise, create a new forward reference for this value and remember it.
1886 Comdat *C = M->getOrInsertComdat(Name);
1887 ForwardRefComdats[Name] = Loc;
1888 return C;
1889}
1890
1891//===----------------------------------------------------------------------===//
1892// Helper Routines.
1893//===----------------------------------------------------------------------===//
1894
1895/// parseToken - If the current token has the specified kind, eat it and return
1896/// success. Otherwise, emit the specified error and return failure.
1897bool LLParser::parseToken(lltok::Kind T, const char *ErrMsg) {
1898 if (Lex.getKind() != T)
1899 return tokError(Msg: ErrMsg);
1900 Lex.Lex();
1901 return false;
1902}
1903
1904/// parseStringConstant
1905/// ::= StringConstant
1906bool LLParser::parseStringConstant(std::string &Result) {
1907 if (Lex.getKind() != lltok::StringConstant)
1908 return tokError(Msg: "expected string constant");
1909 Result = Lex.getStrVal();
1910 Lex.Lex();
1911 return false;
1912}
1913
1914/// parseUInt32
1915/// ::= uint32
1916bool LLParser::parseUInt32(uint32_t &Val) {
1917 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1918 return tokError(Msg: "expected integer");
1919 uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(Limit: 0xFFFFFFFFULL+1);
1920 if (Val64 != unsigned(Val64))
1921 return tokError(Msg: "expected 32-bit integer (too large)");
1922 Val = Val64;
1923 Lex.Lex();
1924 return false;
1925}
1926
1927/// parseUInt64
1928/// ::= uint64
1929bool LLParser::parseUInt64(uint64_t &Val) {
1930 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
1931 return tokError(Msg: "expected integer");
1932 Val = Lex.getAPSIntVal().getLimitedValue();
1933 Lex.Lex();
1934 return false;
1935}
1936
1937/// parseTLSModel
1938/// := 'localdynamic'
1939/// := 'initialexec'
1940/// := 'localexec'
1941bool LLParser::parseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
1942 switch (Lex.getKind()) {
1943 default:
1944 return tokError(Msg: "expected localdynamic, initialexec or localexec");
1945 case lltok::kw_localdynamic:
1946 TLM = GlobalVariable::LocalDynamicTLSModel;
1947 break;
1948 case lltok::kw_initialexec:
1949 TLM = GlobalVariable::InitialExecTLSModel;
1950 break;
1951 case lltok::kw_localexec:
1952 TLM = GlobalVariable::LocalExecTLSModel;
1953 break;
1954 }
1955
1956 Lex.Lex();
1957 return false;
1958}
1959
1960/// parseOptionalThreadLocal
1961/// := /*empty*/
1962/// := 'thread_local'
1963/// := 'thread_local' '(' tlsmodel ')'
1964bool LLParser::parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
1965 TLM = GlobalVariable::NotThreadLocal;
1966 if (!EatIfPresent(T: lltok::kw_thread_local))
1967 return false;
1968
1969 TLM = GlobalVariable::GeneralDynamicTLSModel;
1970 if (Lex.getKind() == lltok::lparen) {
1971 Lex.Lex();
1972 return parseTLSModel(TLM) ||
1973 parseToken(T: lltok::rparen, ErrMsg: "expected ')' after thread local model");
1974 }
1975 return false;
1976}
1977
1978/// parseOptionalAddrSpace
1979/// := /*empty*/
1980/// := 'addrspace' '(' uint32 ')'
1981bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) {
1982 AddrSpace = DefaultAS;
1983 if (!EatIfPresent(T: lltok::kw_addrspace))
1984 return false;
1985
1986 auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool {
1987 if (Lex.getKind() == lltok::StringConstant) {
1988 const std::string &AddrSpaceStr = Lex.getStrVal();
1989 if (AddrSpaceStr == "A") {
1990 AddrSpace = M->getDataLayout().getAllocaAddrSpace();
1991 } else if (AddrSpaceStr == "G") {
1992 AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace();
1993 } else if (AddrSpaceStr == "P") {
1994 AddrSpace = M->getDataLayout().getProgramAddressSpace();
1995 } else if (std::optional<unsigned> AS =
1996 M->getDataLayout().getNamedAddressSpace(Name: AddrSpaceStr)) {
1997 AddrSpace = *AS;
1998 } else {
1999 return tokError(Msg: "invalid symbolic addrspace '" + AddrSpaceStr + "'");
2000 }
2001 Lex.Lex();
2002 return false;
2003 }
2004 if (Lex.getKind() != lltok::APSInt)
2005 return tokError(Msg: "expected integer or string constant");
2006 SMLoc Loc = Lex.getLoc();
2007 if (parseUInt32(Val&: AddrSpace))
2008 return true;
2009 if (!isUInt<24>(x: AddrSpace))
2010 return error(L: Loc, Msg: "invalid address space, must be a 24-bit integer");
2011 return false;
2012 };
2013
2014 return parseToken(T: lltok::lparen, ErrMsg: "expected '(' in address space") ||
2015 ParseAddrspaceValue(AddrSpace) ||
2016 parseToken(T: lltok::rparen, ErrMsg: "expected ')' in address space");
2017}
2018
2019/// parseStringAttribute
2020/// := StringConstant
2021/// := StringConstant '=' StringConstant
2022bool LLParser::parseStringAttribute(AttrBuilder &B) {
2023 std::string Attr = Lex.getStrVal();
2024 Lex.Lex();
2025 std::string Val;
2026 if (EatIfPresent(T: lltok::equal) && parseStringConstant(Result&: Val))
2027 return true;
2028 B.addAttribute(A: Attr, V: Val);
2029 return false;
2030}
2031
2032/// Parse a potentially empty list of parameter or return attributes.
2033bool LLParser::parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam) {
2034 bool HaveError = false;
2035
2036 B.clear();
2037
2038 while (true) {
2039 lltok::Kind Token = Lex.getKind();
2040 if (Token == lltok::StringConstant) {
2041 if (parseStringAttribute(B))
2042 return true;
2043 continue;
2044 }
2045
2046 if (Token == lltok::kw_nocapture) {
2047 Lex.Lex();
2048 B.addCapturesAttr(CI: CaptureInfo::none());
2049 continue;
2050 }
2051
2052 SMLoc Loc = Lex.getLoc();
2053 Attribute::AttrKind Attr = tokenToAttribute(Kind: Token);
2054 if (Attr == Attribute::None)
2055 return HaveError;
2056
2057 if (parseEnumAttribute(Attr, B, /* InAttrGroup */ false))
2058 return true;
2059
2060 if (IsParam && !Attribute::canUseAsParamAttr(Kind: Attr))
2061 HaveError |= error(L: Loc, Msg: "this attribute does not apply to parameters");
2062 if (!IsParam && !Attribute::canUseAsRetAttr(Kind: Attr))
2063 HaveError |= error(L: Loc, Msg: "this attribute does not apply to return values");
2064 }
2065}
2066
2067static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) {
2068 HasLinkage = true;
2069 switch (Kind) {
2070 default:
2071 HasLinkage = false;
2072 return GlobalValue::ExternalLinkage;
2073 case lltok::kw_private:
2074 return GlobalValue::PrivateLinkage;
2075 case lltok::kw_internal:
2076 return GlobalValue::InternalLinkage;
2077 case lltok::kw_weak:
2078 return GlobalValue::WeakAnyLinkage;
2079 case lltok::kw_weak_odr:
2080 return GlobalValue::WeakODRLinkage;
2081 case lltok::kw_linkonce:
2082 return GlobalValue::LinkOnceAnyLinkage;
2083 case lltok::kw_linkonce_odr:
2084 return GlobalValue::LinkOnceODRLinkage;
2085 case lltok::kw_available_externally:
2086 return GlobalValue::AvailableExternallyLinkage;
2087 case lltok::kw_appending:
2088 return GlobalValue::AppendingLinkage;
2089 case lltok::kw_common:
2090 return GlobalValue::CommonLinkage;
2091 case lltok::kw_extern_weak:
2092 return GlobalValue::ExternalWeakLinkage;
2093 case lltok::kw_external:
2094 return GlobalValue::ExternalLinkage;
2095 }
2096}
2097
2098/// parseOptionalLinkage
2099/// ::= /*empty*/
2100/// ::= 'private'
2101/// ::= 'internal'
2102/// ::= 'weak'
2103/// ::= 'weak_odr'
2104/// ::= 'linkonce'
2105/// ::= 'linkonce_odr'
2106/// ::= 'available_externally'
2107/// ::= 'appending'
2108/// ::= 'common'
2109/// ::= 'extern_weak'
2110/// ::= 'external'
2111bool LLParser::parseOptionalLinkage(unsigned &Res, bool &HasLinkage,
2112 unsigned &Visibility,
2113 unsigned &DLLStorageClass, bool &DSOLocal) {
2114 Res = parseOptionalLinkageAux(Kind: Lex.getKind(), HasLinkage);
2115 if (HasLinkage)
2116 Lex.Lex();
2117 parseOptionalDSOLocal(DSOLocal);
2118 parseOptionalVisibility(Res&: Visibility);
2119 parseOptionalDLLStorageClass(Res&: DLLStorageClass);
2120
2121 if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) {
2122 return error(L: Lex.getLoc(), Msg: "dso_location and DLL-StorageClass mismatch");
2123 }
2124
2125 return false;
2126}
2127
2128void LLParser::parseOptionalDSOLocal(bool &DSOLocal) {
2129 switch (Lex.getKind()) {
2130 default:
2131 DSOLocal = false;
2132 break;
2133 case lltok::kw_dso_local:
2134 DSOLocal = true;
2135 Lex.Lex();
2136 break;
2137 case lltok::kw_dso_preemptable:
2138 DSOLocal = false;
2139 Lex.Lex();
2140 break;
2141 }
2142}
2143
2144/// parseOptionalVisibility
2145/// ::= /*empty*/
2146/// ::= 'default'
2147/// ::= 'hidden'
2148/// ::= 'protected'
2149///
2150void LLParser::parseOptionalVisibility(unsigned &Res) {
2151 switch (Lex.getKind()) {
2152 default:
2153 Res = GlobalValue::DefaultVisibility;
2154 return;
2155 case lltok::kw_default:
2156 Res = GlobalValue::DefaultVisibility;
2157 break;
2158 case lltok::kw_hidden:
2159 Res = GlobalValue::HiddenVisibility;
2160 break;
2161 case lltok::kw_protected:
2162 Res = GlobalValue::ProtectedVisibility;
2163 break;
2164 }
2165 Lex.Lex();
2166}
2167
2168bool LLParser::parseOptionalImportType(lltok::Kind Kind,
2169 GlobalValueSummary::ImportKind &Res) {
2170 switch (Kind) {
2171 default:
2172 return tokError(Msg: "unknown import kind. Expect definition or declaration.");
2173 case lltok::kw_definition:
2174 Res = GlobalValueSummary::Definition;
2175 return false;
2176 case lltok::kw_declaration:
2177 Res = GlobalValueSummary::Declaration;
2178 return false;
2179 }
2180}
2181
2182/// parseOptionalDLLStorageClass
2183/// ::= /*empty*/
2184/// ::= 'dllimport'
2185/// ::= 'dllexport'
2186///
2187void LLParser::parseOptionalDLLStorageClass(unsigned &Res) {
2188 switch (Lex.getKind()) {
2189 default:
2190 Res = GlobalValue::DefaultStorageClass;
2191 return;
2192 case lltok::kw_dllimport:
2193 Res = GlobalValue::DLLImportStorageClass;
2194 break;
2195 case lltok::kw_dllexport:
2196 Res = GlobalValue::DLLExportStorageClass;
2197 break;
2198 }
2199 Lex.Lex();
2200}
2201
2202/// parseOptionalCallingConv
2203/// ::= /*empty*/
2204/// ::= 'ccc'
2205/// ::= 'fastcc'
2206/// ::= 'intel_ocl_bicc'
2207/// ::= 'coldcc'
2208/// ::= 'cfguard_checkcc'
2209/// ::= 'x86_stdcallcc'
2210/// ::= 'x86_fastcallcc'
2211/// ::= 'x86_thiscallcc'
2212/// ::= 'x86_vectorcallcc'
2213/// ::= 'arm_apcscc'
2214/// ::= 'arm_aapcscc'
2215/// ::= 'arm_aapcs_vfpcc'
2216/// ::= 'aarch64_vector_pcs'
2217/// ::= 'aarch64_sve_vector_pcs'
2218/// ::= 'aarch64_sme_preservemost_from_x0'
2219/// ::= 'aarch64_sme_preservemost_from_x1'
2220/// ::= 'aarch64_sme_preservemost_from_x2'
2221/// ::= 'msp430_intrcc'
2222/// ::= 'avr_intrcc'
2223/// ::= 'avr_signalcc'
2224/// ::= 'ptx_kernel'
2225/// ::= 'ptx_device'
2226/// ::= 'spir_func'
2227/// ::= 'spir_kernel'
2228/// ::= 'x86_64_sysvcc'
2229/// ::= 'win64cc'
2230/// ::= 'anyregcc'
2231/// ::= 'preserve_mostcc'
2232/// ::= 'preserve_allcc'
2233/// ::= 'preserve_nonecc'
2234/// ::= 'ghccc'
2235/// ::= 'swiftcc'
2236/// ::= 'swifttailcc'
2237/// ::= 'x86_intrcc'
2238/// ::= 'hhvmcc'
2239/// ::= 'hhvm_ccc'
2240/// ::= 'cxx_fast_tlscc'
2241/// ::= 'amdgpu_vs'
2242/// ::= 'amdgpu_ls'
2243/// ::= 'amdgpu_hs'
2244/// ::= 'amdgpu_es'
2245/// ::= 'amdgpu_gs'
2246/// ::= 'amdgpu_ps'
2247/// ::= 'amdgpu_cs'
2248/// ::= 'amdgpu_cs_chain'
2249/// ::= 'amdgpu_cs_chain_preserve'
2250/// ::= 'amdgpu_kernel'
2251/// ::= 'tailcc'
2252/// ::= 'm68k_rtdcc'
2253/// ::= 'graalcc'
2254/// ::= 'riscv_vector_cc'
2255/// ::= 'riscv_vls_cc'
2256/// ::= 'cc' UINT
2257///
2258bool LLParser::parseOptionalCallingConv(unsigned &CC) {
2259 switch (Lex.getKind()) {
2260 default: CC = CallingConv::C; return false;
2261 case lltok::kw_ccc: CC = CallingConv::C; break;
2262 case lltok::kw_fastcc: CC = CallingConv::Fast; break;
2263 case lltok::kw_coldcc: CC = CallingConv::Cold; break;
2264 case lltok::kw_cfguard_checkcc: CC = CallingConv::CFGuard_Check; break;
2265 case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break;
2266 case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break;
2267 case lltok::kw_x86_regcallcc: CC = CallingConv::X86_RegCall; break;
2268 case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break;
2269 case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break;
2270 case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break;
2271 case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break;
2272 case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break;
2273 case lltok::kw_aarch64_vector_pcs:CC = CallingConv::AArch64_VectorCall; break;
2274 case lltok::kw_aarch64_sve_vector_pcs:
2275 CC = CallingConv::AArch64_SVE_VectorCall;
2276 break;
2277 case lltok::kw_aarch64_sme_preservemost_from_x0:
2278 CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0;
2279 break;
2280 case lltok::kw_aarch64_sme_preservemost_from_x1:
2281 CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1;
2282 break;
2283 case lltok::kw_aarch64_sme_preservemost_from_x2:
2284 CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2;
2285 break;
2286 case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break;
2287 case lltok::kw_avr_intrcc: CC = CallingConv::AVR_INTR; break;
2288 case lltok::kw_avr_signalcc: CC = CallingConv::AVR_SIGNAL; break;
2289 case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break;
2290 case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break;
2291 case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break;
2292 case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break;
2293 case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break;
2294 case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break;
2295 case lltok::kw_win64cc: CC = CallingConv::Win64; break;
2296 case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break;
2297 case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break;
2298 case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break;
2299 case lltok::kw_preserve_nonecc:CC = CallingConv::PreserveNone; break;
2300 case lltok::kw_ghccc: CC = CallingConv::GHC; break;
2301 case lltok::kw_swiftcc: CC = CallingConv::Swift; break;
2302 case lltok::kw_swifttailcc: CC = CallingConv::SwiftTail; break;
2303 case lltok::kw_x86_intrcc: CC = CallingConv::X86_INTR; break;
2304 case lltok::kw_hhvmcc:
2305 CC = CallingConv::DUMMY_HHVM;
2306 break;
2307 case lltok::kw_hhvm_ccc:
2308 CC = CallingConv::DUMMY_HHVM_C;
2309 break;
2310 case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break;
2311 case lltok::kw_amdgpu_vs: CC = CallingConv::AMDGPU_VS; break;
2312 case lltok::kw_amdgpu_gfx: CC = CallingConv::AMDGPU_Gfx; break;
2313 case lltok::kw_amdgpu_ls: CC = CallingConv::AMDGPU_LS; break;
2314 case lltok::kw_amdgpu_hs: CC = CallingConv::AMDGPU_HS; break;
2315 case lltok::kw_amdgpu_es: CC = CallingConv::AMDGPU_ES; break;
2316 case lltok::kw_amdgpu_gs: CC = CallingConv::AMDGPU_GS; break;
2317 case lltok::kw_amdgpu_ps: CC = CallingConv::AMDGPU_PS; break;
2318 case lltok::kw_amdgpu_cs: CC = CallingConv::AMDGPU_CS; break;
2319 case lltok::kw_amdgpu_cs_chain:
2320 CC = CallingConv::AMDGPU_CS_Chain;
2321 break;
2322 case lltok::kw_amdgpu_cs_chain_preserve:
2323 CC = CallingConv::AMDGPU_CS_ChainPreserve;
2324 break;
2325 case lltok::kw_amdgpu_kernel: CC = CallingConv::AMDGPU_KERNEL; break;
2326 case lltok::kw_amdgpu_gfx_whole_wave:
2327 CC = CallingConv::AMDGPU_Gfx_WholeWave;
2328 break;
2329 case lltok::kw_tailcc: CC = CallingConv::Tail; break;
2330 case lltok::kw_m68k_rtdcc: CC = CallingConv::M68k_RTD; break;
2331 case lltok::kw_graalcc: CC = CallingConv::GRAAL; break;
2332 case lltok::kw_riscv_vector_cc:
2333 CC = CallingConv::RISCV_VectorCall;
2334 break;
2335 case lltok::kw_riscv_vls_cc:
2336 // Default ABI_VLEN
2337 CC = CallingConv::RISCV_VLSCall_128;
2338 Lex.Lex();
2339 if (!EatIfPresent(T: lltok::lparen))
2340 break;
2341 uint32_t ABIVlen;
2342 if (parseUInt32(Val&: ABIVlen) || !EatIfPresent(T: lltok::rparen))
2343 return true;
2344 switch (ABIVlen) {
2345 default:
2346 return tokError(Msg: "unknown RISC-V ABI VLEN");
2347#define CC_VLS_CASE(ABIVlen) \
2348 case ABIVlen: \
2349 CC = CallingConv::RISCV_VLSCall_##ABIVlen; \
2350 break;
2351 CC_VLS_CASE(32)
2352 CC_VLS_CASE(64)
2353 CC_VLS_CASE(128)
2354 CC_VLS_CASE(256)
2355 CC_VLS_CASE(512)
2356 CC_VLS_CASE(1024)
2357 CC_VLS_CASE(2048)
2358 CC_VLS_CASE(4096)
2359 CC_VLS_CASE(8192)
2360 CC_VLS_CASE(16384)
2361 CC_VLS_CASE(32768)
2362 CC_VLS_CASE(65536)
2363#undef CC_VLS_CASE
2364 }
2365 return false;
2366 case lltok::kw_cheriot_compartmentcallcc:
2367 CC = CallingConv::CHERIoT_CompartmentCall;
2368 break;
2369 case lltok::kw_cheriot_compartmentcalleecc:
2370 CC = CallingConv::CHERIoT_CompartmentCallee;
2371 break;
2372 case lltok::kw_cheriot_librarycallcc:
2373 CC = CallingConv::CHERIoT_LibraryCall;
2374 break;
2375 case lltok::kw_cc: {
2376 Lex.Lex();
2377 return parseUInt32(Val&: CC);
2378 }
2379 }
2380
2381 Lex.Lex();
2382 return false;
2383}
2384
2385/// parseMetadataAttachment
2386/// ::= !dbg !42
2387bool LLParser::parseMetadataAttachment(unsigned &Kind, MDNode *&MD) {
2388 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment");
2389
2390 std::string Name = Lex.getStrVal();
2391 Kind = M->getMDKindID(Name);
2392 Lex.Lex();
2393
2394 return parseMDNode(N&: MD);
2395}
2396
2397/// parseInstructionMetadata
2398/// ::= !dbg !42 (',' !dbg !57)*
2399bool LLParser::parseInstructionMetadata(Instruction &Inst) {
2400 do {
2401 if (Lex.getKind() != lltok::MetadataVar)
2402 return tokError(Msg: "expected metadata after comma");
2403
2404 unsigned MDK;
2405 MDNode *N;
2406 if (parseMetadataAttachment(Kind&: MDK, MD&: N))
2407 return true;
2408
2409 if (MDK == LLVMContext::MD_DIAssignID)
2410 TempDIAssignIDAttachments[N].push_back(Elt: &Inst);
2411 else
2412 Inst.setMetadata(KindID: MDK, Node: N);
2413
2414 if (MDK == LLVMContext::MD_tbaa)
2415 InstsWithTBAATag.push_back(Elt: &Inst);
2416
2417 // If this is the end of the list, we're done.
2418 } while (EatIfPresent(T: lltok::comma));
2419 return false;
2420}
2421
2422/// parseGlobalObjectMetadataAttachment
2423/// ::= !dbg !57
2424bool LLParser::parseGlobalObjectMetadataAttachment(GlobalObject &GO) {
2425 unsigned MDK;
2426 MDNode *N;
2427 if (parseMetadataAttachment(Kind&: MDK, MD&: N))
2428 return true;
2429
2430 GO.addMetadata(KindID: MDK, MD&: *N);
2431 return false;
2432}
2433
2434/// parseOptionalFunctionMetadata
2435/// ::= (!dbg !57)*
2436bool LLParser::parseOptionalFunctionMetadata(Function &F) {
2437 while (Lex.getKind() == lltok::MetadataVar)
2438 if (parseGlobalObjectMetadataAttachment(GO&: F))
2439 return true;
2440 return false;
2441}
2442
2443/// parseOptionalAlignment
2444/// ::= /* empty */
2445/// ::= 'align' 4
2446bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
2447 Alignment = std::nullopt;
2448 if (!EatIfPresent(T: lltok::kw_align))
2449 return false;
2450 LocTy AlignLoc = Lex.getLoc();
2451 uint64_t Value = 0;
2452
2453 LocTy ParenLoc = Lex.getLoc();
2454 bool HaveParens = false;
2455 if (AllowParens) {
2456 if (EatIfPresent(T: lltok::lparen))
2457 HaveParens = true;
2458 }
2459
2460 if (parseUInt64(Val&: Value))
2461 return true;
2462
2463 if (HaveParens && !EatIfPresent(T: lltok::rparen))
2464 return error(L: ParenLoc, Msg: "expected ')'");
2465
2466 if (!isPowerOf2_64(Value))
2467 return error(L: AlignLoc, Msg: "alignment is not a power of two");
2468 if (Value > Value::MaximumAlignment)
2469 return error(L: AlignLoc, Msg: "huge alignments are not supported yet");
2470 Alignment = Align(Value);
2471 return false;
2472}
2473
2474/// parseOptionalPrefAlignment
2475/// ::= /* empty */
2476/// ::= 'prefalign' '(' 4 ')'
2477bool LLParser::parseOptionalPrefAlignment(MaybeAlign &Alignment) {
2478 Alignment = std::nullopt;
2479 if (!EatIfPresent(T: lltok::kw_prefalign))
2480 return false;
2481 LocTy AlignLoc = Lex.getLoc();
2482 uint64_t Value = 0;
2483
2484 LocTy ParenLoc = Lex.getLoc();
2485 if (!EatIfPresent(T: lltok::lparen))
2486 return error(L: ParenLoc, Msg: "expected '('");
2487
2488 if (parseUInt64(Val&: Value))
2489 return true;
2490
2491 ParenLoc = Lex.getLoc();
2492 if (!EatIfPresent(T: lltok::rparen))
2493 return error(L: ParenLoc, Msg: "expected ')'");
2494
2495 if (!isPowerOf2_64(Value))
2496 return error(L: AlignLoc, Msg: "alignment is not a power of two");
2497 if (Value > Value::MaximumAlignment)
2498 return error(L: AlignLoc, Msg: "huge alignments are not supported yet");
2499 Alignment = Align(Value);
2500 return false;
2501}
2502
2503/// parseOptionalCodeModel
2504/// ::= /* empty */
2505/// ::= 'code_model' "large"
2506bool LLParser::parseOptionalCodeModel(CodeModel::Model &model) {
2507 Lex.Lex();
2508 auto StrVal = Lex.getStrVal();
2509 auto ErrMsg = "expected global code model string";
2510 if (StrVal == "tiny")
2511 model = CodeModel::Tiny;
2512 else if (StrVal == "small")
2513 model = CodeModel::Small;
2514 else if (StrVal == "kernel")
2515 model = CodeModel::Kernel;
2516 else if (StrVal == "medium")
2517 model = CodeModel::Medium;
2518 else if (StrVal == "large")
2519 model = CodeModel::Large;
2520 else
2521 return tokError(Msg: ErrMsg);
2522 if (parseToken(T: lltok::StringConstant, ErrMsg))
2523 return true;
2524 return false;
2525}
2526
2527/// parseOptionalAttrBytes
2528/// ::= /* empty */
2529/// ::= AttrKind '(' 4 ')'
2530///
2531/// where AttrKind is either 'dereferenceable', 'dereferenceable_or_null', or
2532/// 'dead_on_return'
2533bool LLParser::parseOptionalAttrBytes(lltok::Kind AttrKind,
2534 std::optional<uint64_t> &Bytes,
2535 bool ErrorNoBytes) {
2536 assert((AttrKind == lltok::kw_dereferenceable ||
2537 AttrKind == lltok::kw_dereferenceable_or_null ||
2538 AttrKind == lltok::kw_dead_on_return) &&
2539 "contract!");
2540
2541 Bytes = 0;
2542 if (!EatIfPresent(T: AttrKind))
2543 return false;
2544 LocTy ParenLoc = Lex.getLoc();
2545 if (!EatIfPresent(T: lltok::lparen)) {
2546 if (ErrorNoBytes)
2547 return error(L: ParenLoc, Msg: "expected '('");
2548 Bytes = std::nullopt;
2549 return false;
2550 }
2551 LocTy DerefLoc = Lex.getLoc();
2552 if (parseUInt64(Val&: Bytes.value()))
2553 return true;
2554 ParenLoc = Lex.getLoc();
2555 if (!EatIfPresent(T: lltok::rparen))
2556 return error(L: ParenLoc, Msg: "expected ')'");
2557 if (!Bytes.value())
2558 return error(L: DerefLoc, Msg: "byte count specified must be non-zero");
2559 return false;
2560}
2561
2562bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) {
2563 Lex.Lex();
2564 Kind = UWTableKind::Default;
2565 if (!EatIfPresent(T: lltok::lparen))
2566 return false;
2567 LocTy KindLoc = Lex.getLoc();
2568 if (Lex.getKind() == lltok::kw_sync)
2569 Kind = UWTableKind::Sync;
2570 else if (Lex.getKind() == lltok::kw_async)
2571 Kind = UWTableKind::Async;
2572 else
2573 return error(L: KindLoc, Msg: "expected unwind table kind");
2574 Lex.Lex();
2575 return parseToken(T: lltok::rparen, ErrMsg: "expected ')'");
2576}
2577
2578bool LLParser::parseAllocKind(AllocFnKind &Kind) {
2579 Lex.Lex();
2580 LocTy ParenLoc = Lex.getLoc();
2581 if (!EatIfPresent(T: lltok::lparen))
2582 return error(L: ParenLoc, Msg: "expected '('");
2583 LocTy KindLoc = Lex.getLoc();
2584 std::string Arg;
2585 if (parseStringConstant(Result&: Arg))
2586 return error(L: KindLoc, Msg: "expected allockind value");
2587 for (StringRef A : llvm::split(Str: Arg, Separator: ",")) {
2588 if (A == "alloc") {
2589 Kind |= AllocFnKind::Alloc;
2590 } else if (A == "realloc") {
2591 Kind |= AllocFnKind::Realloc;
2592 } else if (A == "free") {
2593 Kind |= AllocFnKind::Free;
2594 } else if (A == "uninitialized") {
2595 Kind |= AllocFnKind::Uninitialized;
2596 } else if (A == "zeroed") {
2597 Kind |= AllocFnKind::Zeroed;
2598 } else if (A == "aligned") {
2599 Kind |= AllocFnKind::Aligned;
2600 } else {
2601 return error(L: KindLoc, Msg: Twine("unknown allockind ") + A);
2602 }
2603 }
2604 ParenLoc = Lex.getLoc();
2605 if (!EatIfPresent(T: lltok::rparen))
2606 return error(L: ParenLoc, Msg: "expected ')'");
2607 if (Kind == AllocFnKind::Unknown)
2608 return error(L: KindLoc, Msg: "expected allockind value");
2609 return false;
2610}
2611
2612static std::optional<MemoryEffects::Location> keywordToLoc(lltok::Kind Tok) {
2613 switch (Tok) {
2614 case lltok::kw_argmem:
2615 return IRMemLocation::ArgMem;
2616 case lltok::kw_inaccessiblemem:
2617 return IRMemLocation::InaccessibleMem;
2618 case lltok::kw_errnomem:
2619 return IRMemLocation::ErrnoMem;
2620 case lltok::kw_target_mem0:
2621 return IRMemLocation::TargetMem0;
2622 case lltok::kw_target_mem1:
2623 return IRMemLocation::TargetMem1;
2624 default:
2625 return std::nullopt;
2626 }
2627}
2628
2629static std::optional<ModRefInfo> keywordToModRef(lltok::Kind Tok) {
2630 switch (Tok) {
2631 case lltok::kw_none:
2632 return ModRefInfo::NoModRef;
2633 case lltok::kw_read:
2634 return ModRefInfo::Ref;
2635 case lltok::kw_write:
2636 return ModRefInfo::Mod;
2637 case lltok::kw_readwrite:
2638 return ModRefInfo::ModRef;
2639 default:
2640 return std::nullopt;
2641 }
2642}
2643
2644static std::optional<DenormalMode::DenormalModeKind>
2645keywordToDenormalModeKind(lltok::Kind Tok) {
2646 switch (Tok) {
2647 case lltok::kw_ieee:
2648 return DenormalMode::IEEE;
2649 case lltok::kw_preservesign:
2650 return DenormalMode::PreserveSign;
2651 case lltok::kw_positivezero:
2652 return DenormalMode::PositiveZero;
2653 case lltok::kw_dynamic:
2654 return DenormalMode::Dynamic;
2655 default:
2656 return std::nullopt;
2657 }
2658}
2659
2660std::optional<MemoryEffects> LLParser::parseMemoryAttr() {
2661 MemoryEffects ME = MemoryEffects::none();
2662
2663 // We use syntax like memory(argmem: read), so the colon should not be
2664 // interpreted as a label terminator.
2665 Lex.setIgnoreColonInIdentifiers(true);
2666 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
2667
2668 Lex.Lex();
2669 if (!EatIfPresent(T: lltok::lparen)) {
2670 tokError(Msg: "expected '('");
2671 return std::nullopt;
2672 }
2673
2674 bool SeenLoc = false;
2675 do {
2676 std::optional<IRMemLocation> Loc = keywordToLoc(Tok: Lex.getKind());
2677 if (Loc) {
2678 Lex.Lex();
2679 if (!EatIfPresent(T: lltok::colon)) {
2680 tokError(Msg: "expected ':' after location");
2681 return std::nullopt;
2682 }
2683 }
2684
2685 std::optional<ModRefInfo> MR = keywordToModRef(Tok: Lex.getKind());
2686 if (!MR) {
2687 if (!Loc)
2688 tokError(Msg: "expected memory location (argmem, inaccessiblemem, errnomem) "
2689 "or access kind (none, read, write, readwrite)");
2690 else
2691 tokError(Msg: "expected access kind (none, read, write, readwrite)");
2692 return std::nullopt;
2693 }
2694
2695 Lex.Lex();
2696 if (Loc) {
2697 SeenLoc = true;
2698 ME = ME.getWithModRef(Loc: *Loc, MR: *MR);
2699 } else {
2700 if (SeenLoc) {
2701 tokError(Msg: "default access kind must be specified first");
2702 return std::nullopt;
2703 }
2704 ME = MemoryEffects(*MR);
2705 }
2706
2707 if (EatIfPresent(T: lltok::rparen))
2708 return ME;
2709 } while (EatIfPresent(T: lltok::comma));
2710
2711 tokError(Msg: "unterminated memory attribute");
2712 return std::nullopt;
2713}
2714
2715std::optional<DenormalMode> LLParser::parseDenormalFPEnvEntry() {
2716 std::optional<DenormalMode::DenormalModeKind> OutputMode =
2717 keywordToDenormalModeKind(Tok: Lex.getKind());
2718 if (!OutputMode) {
2719 tokError(Msg: "expected denormal behavior kind (ieee, preservesign, "
2720 "positivezero, dynamic)");
2721 return {};
2722 }
2723
2724 Lex.Lex();
2725
2726 std::optional<DenormalMode::DenormalModeKind> InputMode;
2727 if (EatIfPresent(T: lltok::bar)) {
2728 InputMode = keywordToDenormalModeKind(Tok: Lex.getKind());
2729 if (!InputMode) {
2730 tokError(Msg: "expected denormal behavior kind (ieee, preservesign, "
2731 "positivezero, dynamic)");
2732 return {};
2733 }
2734
2735 Lex.Lex();
2736 } else {
2737 // Single item, input == output mode
2738 InputMode = OutputMode;
2739 }
2740
2741 return DenormalMode(*OutputMode, *InputMode);
2742}
2743
2744std::optional<DenormalFPEnv> LLParser::parseDenormalFPEnvAttr() {
2745 // We use syntax like denormal_fpenv(float: preservesign), so the colon should
2746 // not be interpreted as a label terminator.
2747 Lex.setIgnoreColonInIdentifiers(true);
2748 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
2749
2750 Lex.Lex();
2751
2752 if (parseToken(T: lltok::lparen, ErrMsg: "expected '('"))
2753 return {};
2754
2755 DenormalMode DefaultMode = DenormalMode::getIEEE();
2756 DenormalMode F32Mode = DenormalMode::getInvalid();
2757
2758 bool HasDefaultSection = false;
2759 if (Lex.getKind() != lltok::Type) {
2760 std::optional<DenormalMode> ParsedDefaultMode = parseDenormalFPEnvEntry();
2761 if (!ParsedDefaultMode)
2762 return {};
2763 DefaultMode = *ParsedDefaultMode;
2764 HasDefaultSection = true;
2765 }
2766
2767 bool HasComma = EatIfPresent(T: lltok::comma);
2768 if (Lex.getKind() == lltok::Type) {
2769 if (HasDefaultSection && !HasComma) {
2770 tokError(Msg: "expected ',' before float:");
2771 return {};
2772 }
2773
2774 Type *Ty = nullptr;
2775 if (parseType(Result&: Ty) || !Ty->isFloatTy()) {
2776 tokError(Msg: "expected float:");
2777 return {};
2778 }
2779
2780 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' before float denormal_fpenv"))
2781 return {};
2782
2783 std::optional<DenormalMode> ParsedF32Mode = parseDenormalFPEnvEntry();
2784 if (!ParsedF32Mode)
2785 return {};
2786
2787 F32Mode = *ParsedF32Mode;
2788 }
2789
2790 if (parseToken(T: lltok::rparen, ErrMsg: "unterminated denormal_fpenv"))
2791 return {};
2792
2793 return DenormalFPEnv(DefaultMode, F32Mode);
2794}
2795
2796static unsigned keywordToFPClassTest(lltok::Kind Tok) {
2797 switch (Tok) {
2798 case lltok::kw_all:
2799 return fcAllFlags;
2800 case lltok::kw_nan:
2801 return fcNan;
2802 case lltok::kw_snan:
2803 return fcSNan;
2804 case lltok::kw_qnan:
2805 return fcQNan;
2806 case lltok::kw_inf:
2807 return fcInf;
2808 case lltok::kw_ninf:
2809 return fcNegInf;
2810 case lltok::kw_pinf:
2811 return fcPosInf;
2812 case lltok::kw_norm:
2813 return fcNormal;
2814 case lltok::kw_nnorm:
2815 return fcNegNormal;
2816 case lltok::kw_pnorm:
2817 return fcPosNormal;
2818 case lltok::kw_sub:
2819 return fcSubnormal;
2820 case lltok::kw_nsub:
2821 return fcNegSubnormal;
2822 case lltok::kw_psub:
2823 return fcPosSubnormal;
2824 case lltok::kw_zero:
2825 return fcZero;
2826 case lltok::kw_nzero:
2827 return fcNegZero;
2828 case lltok::kw_pzero:
2829 return fcPosZero;
2830 default:
2831 return 0;
2832 }
2833}
2834
2835unsigned LLParser::parseNoFPClassAttr() {
2836 unsigned Mask = fcNone;
2837
2838 Lex.Lex();
2839 if (!EatIfPresent(T: lltok::lparen)) {
2840 tokError(Msg: "expected '('");
2841 return 0;
2842 }
2843
2844 do {
2845 uint64_t Value = 0;
2846 unsigned TestMask = keywordToFPClassTest(Tok: Lex.getKind());
2847 if (TestMask != 0) {
2848 Mask |= TestMask;
2849 // TODO: Disallow overlapping masks to avoid copy paste errors
2850 } else if (Mask == 0 && Lex.getKind() == lltok::APSInt &&
2851 !parseUInt64(Val&: Value)) {
2852 if (Value == 0 || (Value & ~static_cast<unsigned>(fcAllFlags)) != 0) {
2853 error(L: Lex.getLoc(), Msg: "invalid mask value for 'nofpclass'");
2854 return 0;
2855 }
2856
2857 if (!EatIfPresent(T: lltok::rparen)) {
2858 error(L: Lex.getLoc(), Msg: "expected ')'");
2859 return 0;
2860 }
2861
2862 return Value;
2863 } else {
2864 error(L: Lex.getLoc(), Msg: "expected nofpclass test mask");
2865 return 0;
2866 }
2867
2868 Lex.Lex();
2869 if (EatIfPresent(T: lltok::rparen))
2870 return Mask;
2871 } while (1);
2872
2873 llvm_unreachable("unterminated nofpclass attribute");
2874}
2875
2876/// parseOptionalCommaAlign
2877/// ::=
2878/// ::= ',' align 4
2879///
2880/// This returns with AteExtraComma set to true if it ate an excess comma at the
2881/// end.
2882bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment,
2883 bool &AteExtraComma) {
2884 AteExtraComma = false;
2885 while (EatIfPresent(T: lltok::comma)) {
2886 // Metadata at the end is an early exit.
2887 if (Lex.getKind() == lltok::MetadataVar) {
2888 AteExtraComma = true;
2889 return false;
2890 }
2891
2892 if (Lex.getKind() != lltok::kw_align)
2893 return error(L: Lex.getLoc(), Msg: "expected metadata or 'align'");
2894
2895 if (parseOptionalAlignment(Alignment))
2896 return true;
2897 }
2898
2899 return false;
2900}
2901
2902/// parseOptionalCommaAddrSpace
2903/// ::=
2904/// ::= ',' addrspace(1)
2905///
2906/// This returns with AteExtraComma set to true if it ate an excess comma at the
2907/// end.
2908bool LLParser::parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
2909 bool &AteExtraComma) {
2910 AteExtraComma = false;
2911 while (EatIfPresent(T: lltok::comma)) {
2912 // Metadata at the end is an early exit.
2913 if (Lex.getKind() == lltok::MetadataVar) {
2914 AteExtraComma = true;
2915 return false;
2916 }
2917
2918 Loc = Lex.getLoc();
2919 if (Lex.getKind() != lltok::kw_addrspace)
2920 return error(L: Lex.getLoc(), Msg: "expected metadata or 'addrspace'");
2921
2922 if (parseOptionalAddrSpace(AddrSpace))
2923 return true;
2924 }
2925
2926 return false;
2927}
2928
2929bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg,
2930 std::optional<unsigned> &HowManyArg) {
2931 Lex.Lex();
2932
2933 auto StartParen = Lex.getLoc();
2934 if (!EatIfPresent(T: lltok::lparen))
2935 return error(L: StartParen, Msg: "expected '('");
2936
2937 if (parseUInt32(Val&: BaseSizeArg))
2938 return true;
2939
2940 if (EatIfPresent(T: lltok::comma)) {
2941 auto HowManyAt = Lex.getLoc();
2942 unsigned HowMany;
2943 if (parseUInt32(Val&: HowMany))
2944 return true;
2945 if (HowMany == BaseSizeArg)
2946 return error(L: HowManyAt,
2947 Msg: "'allocsize' indices can't refer to the same parameter");
2948 HowManyArg = HowMany;
2949 } else
2950 HowManyArg = std::nullopt;
2951
2952 auto EndParen = Lex.getLoc();
2953 if (!EatIfPresent(T: lltok::rparen))
2954 return error(L: EndParen, Msg: "expected ')'");
2955 return false;
2956}
2957
2958bool LLParser::parseVScaleRangeArguments(unsigned &MinValue,
2959 unsigned &MaxValue) {
2960 Lex.Lex();
2961
2962 auto StartParen = Lex.getLoc();
2963 if (!EatIfPresent(T: lltok::lparen))
2964 return error(L: StartParen, Msg: "expected '('");
2965
2966 if (parseUInt32(Val&: MinValue))
2967 return true;
2968
2969 if (EatIfPresent(T: lltok::comma)) {
2970 if (parseUInt32(Val&: MaxValue))
2971 return true;
2972 } else
2973 MaxValue = MinValue;
2974
2975 auto EndParen = Lex.getLoc();
2976 if (!EatIfPresent(T: lltok::rparen))
2977 return error(L: EndParen, Msg: "expected ')'");
2978 return false;
2979}
2980
2981/// parseScopeAndOrdering
2982/// if isAtomic: ::= SyncScope? AtomicOrdering
2983/// else: ::=
2984///
2985/// This sets Scope and Ordering to the parsed values.
2986bool LLParser::parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID,
2987 AtomicOrdering &Ordering) {
2988 if (!IsAtomic)
2989 return false;
2990
2991 return parseScope(SSID) || parseOrdering(Ordering);
2992}
2993
2994/// parseScope
2995/// ::= syncscope("singlethread" | "<target scope>")?
2996///
2997/// This sets synchronization scope ID to the ID of the parsed value.
2998bool LLParser::parseScope(SyncScope::ID &SSID) {
2999 SSID = SyncScope::System;
3000 if (EatIfPresent(T: lltok::kw_syncscope)) {
3001 auto StartParenAt = Lex.getLoc();
3002 if (!EatIfPresent(T: lltok::lparen))
3003 return error(L: StartParenAt, Msg: "Expected '(' in syncscope");
3004
3005 std::string SSN;
3006 auto SSNAt = Lex.getLoc();
3007 if (parseStringConstant(Result&: SSN))
3008 return error(L: SSNAt, Msg: "Expected synchronization scope name");
3009
3010 auto EndParenAt = Lex.getLoc();
3011 if (!EatIfPresent(T: lltok::rparen))
3012 return error(L: EndParenAt, Msg: "Expected ')' in syncscope");
3013
3014 SSID = Context.getOrInsertSyncScopeID(SSN);
3015 }
3016
3017 return false;
3018}
3019
3020/// parseOrdering
3021/// ::= AtomicOrdering
3022///
3023/// This sets Ordering to the parsed value.
3024bool LLParser::parseOrdering(AtomicOrdering &Ordering) {
3025 switch (Lex.getKind()) {
3026 default:
3027 return tokError(Msg: "Expected ordering on atomic instruction");
3028 case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break;
3029 case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break;
3030 // Not specified yet:
3031 // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break;
3032 case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break;
3033 case lltok::kw_release: Ordering = AtomicOrdering::Release; break;
3034 case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break;
3035 case lltok::kw_seq_cst:
3036 Ordering = AtomicOrdering::SequentiallyConsistent;
3037 break;
3038 }
3039 Lex.Lex();
3040 return false;
3041}
3042
3043/// parseOptionalStackAlignment
3044/// ::= /* empty */
3045/// ::= 'alignstack' '(' 4 ')'
3046bool LLParser::parseOptionalStackAlignment(unsigned &Alignment) {
3047 Alignment = 0;
3048 if (!EatIfPresent(T: lltok::kw_alignstack))
3049 return false;
3050 LocTy ParenLoc = Lex.getLoc();
3051 if (!EatIfPresent(T: lltok::lparen))
3052 return error(L: ParenLoc, Msg: "expected '('");
3053 LocTy AlignLoc = Lex.getLoc();
3054 if (parseUInt32(Val&: Alignment))
3055 return true;
3056 ParenLoc = Lex.getLoc();
3057 if (!EatIfPresent(T: lltok::rparen))
3058 return error(L: ParenLoc, Msg: "expected ')'");
3059 if (!isPowerOf2_32(Value: Alignment))
3060 return error(L: AlignLoc, Msg: "stack alignment is not a power of two");
3061 return false;
3062}
3063
3064/// parseIndexList - This parses the index list for an insert/extractvalue
3065/// instruction. This sets AteExtraComma in the case where we eat an extra
3066/// comma at the end of the line and find that it is followed by metadata.
3067/// Clients that don't allow metadata can call the version of this function that
3068/// only takes one argument.
3069///
3070/// parseIndexList
3071/// ::= (',' uint32)+
3072///
3073bool LLParser::parseIndexList(SmallVectorImpl<unsigned> &Indices,
3074 bool &AteExtraComma) {
3075 AteExtraComma = false;
3076
3077 if (Lex.getKind() != lltok::comma)
3078 return tokError(Msg: "expected ',' as start of index list");
3079
3080 while (EatIfPresent(T: lltok::comma)) {
3081 if (Lex.getKind() == lltok::MetadataVar) {
3082 if (Indices.empty())
3083 return tokError(Msg: "expected index");
3084 AteExtraComma = true;
3085 return false;
3086 }
3087 unsigned Idx = 0;
3088 if (parseUInt32(Val&: Idx))
3089 return true;
3090 Indices.push_back(Elt: Idx);
3091 }
3092
3093 return false;
3094}
3095
3096//===----------------------------------------------------------------------===//
3097// Type Parsing.
3098//===----------------------------------------------------------------------===//
3099
3100/// parseType - parse a type.
3101bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) {
3102 SMLoc TypeLoc = Lex.getLoc();
3103 switch (Lex.getKind()) {
3104 default:
3105 return tokError(Msg);
3106 case lltok::Type:
3107 // Type ::= 'float' | 'void' (etc)
3108 Result = Lex.getTyVal();
3109 Lex.Lex();
3110
3111 // Handle "ptr" opaque pointer type.
3112 //
3113 // Type ::= ptr ('addrspace' '(' uint32 ')')?
3114 if (Result->isPointerTy()) {
3115 unsigned AddrSpace;
3116 if (parseOptionalAddrSpace(AddrSpace))
3117 return true;
3118 Result = PointerType::get(C&: getContext(), AddressSpace: AddrSpace);
3119
3120 // Give a nice error for 'ptr*'.
3121 if (Lex.getKind() == lltok::star)
3122 return tokError(Msg: "ptr* is invalid - use ptr instead");
3123
3124 // Fall through to parsing the type suffixes only if this 'ptr' is a
3125 // function return. Otherwise, return success, implicitly rejecting other
3126 // suffixes.
3127 if (Lex.getKind() != lltok::lparen)
3128 return false;
3129 }
3130 break;
3131 case lltok::kw_target: {
3132 // Type ::= TargetExtType
3133 if (parseTargetExtType(Result))
3134 return true;
3135 break;
3136 }
3137 case lltok::lbrace:
3138 // Type ::= StructType
3139 if (parseAnonStructType(Result, Packed: false))
3140 return true;
3141 break;
3142 case lltok::lsquare:
3143 // Type ::= '[' ... ']'
3144 Lex.Lex(); // eat the lsquare.
3145 if (parseArrayVectorType(Result, IsVector: false))
3146 return true;
3147 break;
3148 case lltok::less: // Either vector or packed struct.
3149 // Type ::= '<' ... '>'
3150 Lex.Lex();
3151 if (Lex.getKind() == lltok::lbrace) {
3152 if (parseAnonStructType(Result, Packed: true) ||
3153 parseToken(T: lltok::greater, ErrMsg: "expected '>' at end of packed struct"))
3154 return true;
3155 } else if (parseArrayVectorType(Result, IsVector: true))
3156 return true;
3157 break;
3158 case lltok::LocalVar: {
3159 // Type ::= %foo
3160 std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()];
3161
3162 // If the type hasn't been defined yet, create a forward definition and
3163 // remember where that forward def'n was seen (in case it never is defined).
3164 if (!Entry.first) {
3165 Entry.first = StructType::create(Context, Name: Lex.getStrVal());
3166 Entry.second = Lex.getLoc();
3167 }
3168 Result = Entry.first;
3169 Lex.Lex();
3170 break;
3171 }
3172
3173 case lltok::LocalVarID: {
3174 // Type ::= %4
3175 std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()];
3176
3177 // If the type hasn't been defined yet, create a forward definition and
3178 // remember where that forward def'n was seen (in case it never is defined).
3179 if (!Entry.first) {
3180 Entry.first = StructType::create(Context);
3181 Entry.second = Lex.getLoc();
3182 }
3183 Result = Entry.first;
3184 Lex.Lex();
3185 break;
3186 }
3187 }
3188
3189 // parse the type suffixes.
3190 while (true) {
3191 switch (Lex.getKind()) {
3192 // End of type.
3193 default:
3194 if (!AllowVoid && Result->isVoidTy())
3195 return error(L: TypeLoc, Msg: "void type only allowed for function results");
3196 return false;
3197
3198 // Type ::= Type '*'
3199 case lltok::star:
3200 if (Result->isLabelTy())
3201 return tokError(Msg: "basic block pointers are invalid");
3202 if (Result->isVoidTy())
3203 return tokError(Msg: "pointers to void are invalid - use i8* instead");
3204 if (!PointerType::isValidElementType(ElemTy: Result))
3205 return tokError(Msg: "pointer to this type is invalid");
3206 Result = PointerType::getUnqual(C&: Context);
3207 Lex.Lex();
3208 break;
3209
3210 // Type ::= Type 'addrspace' '(' uint32 ')' '*'
3211 case lltok::kw_addrspace: {
3212 if (Result->isLabelTy())
3213 return tokError(Msg: "basic block pointers are invalid");
3214 if (Result->isVoidTy())
3215 return tokError(Msg: "pointers to void are invalid; use i8* instead");
3216 if (!PointerType::isValidElementType(ElemTy: Result))
3217 return tokError(Msg: "pointer to this type is invalid");
3218 unsigned AddrSpace;
3219 if (parseOptionalAddrSpace(AddrSpace) ||
3220 parseToken(T: lltok::star, ErrMsg: "expected '*' in address space"))
3221 return true;
3222
3223 Result = PointerType::get(C&: Context, AddressSpace: AddrSpace);
3224 break;
3225 }
3226
3227 /// Types '(' ArgTypeListI ')' OptFuncAttrs
3228 case lltok::lparen:
3229 if (parseFunctionType(Result))
3230 return true;
3231 break;
3232 }
3233 }
3234}
3235
3236/// parseParameterList
3237/// ::= '(' ')'
3238/// ::= '(' Arg (',' Arg)* ')'
3239/// Arg
3240/// ::= Type OptionalAttributes Value OptionalAttributes
3241bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
3242 PerFunctionState &PFS, bool IsMustTailCall,
3243 bool InVarArgsFunc) {
3244 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in call"))
3245 return true;
3246
3247 while (Lex.getKind() != lltok::rparen) {
3248 // If this isn't the first argument, we need a comma.
3249 if (!ArgList.empty() &&
3250 parseToken(T: lltok::comma, ErrMsg: "expected ',' in argument list"))
3251 return true;
3252
3253 // parse an ellipsis if this is a musttail call in a variadic function.
3254 if (Lex.getKind() == lltok::dotdotdot) {
3255 const char *Msg = "unexpected ellipsis in argument list for ";
3256 if (!IsMustTailCall)
3257 return tokError(Msg: Twine(Msg) + "non-musttail call");
3258 if (!InVarArgsFunc)
3259 return tokError(Msg: Twine(Msg) + "musttail call in non-varargs function");
3260 Lex.Lex(); // Lex the '...', it is purely for readability.
3261 return parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of argument list");
3262 }
3263
3264 // parse the argument.
3265 LocTy ArgLoc;
3266 Type *ArgTy = nullptr;
3267 Value *V;
3268 if (parseType(Result&: ArgTy, Loc&: ArgLoc))
3269 return true;
3270 if (!FunctionType::isValidArgumentType(ArgTy))
3271 return error(L: ArgLoc, Msg: "invalid type for function argument");
3272
3273 AttrBuilder ArgAttrs(M->getContext());
3274
3275 if (ArgTy->isMetadataTy()) {
3276 if (parseMetadataAsValue(V, PFS))
3277 return true;
3278 } else {
3279 // Otherwise, handle normal operands.
3280 if (parseOptionalParamAttrs(B&: ArgAttrs) || parseValue(Ty: ArgTy, V, PFS))
3281 return true;
3282 }
3283 ArgList.push_back(Elt: ParamInfo(
3284 ArgLoc, V, AttributeSet::get(C&: V->getContext(), B: ArgAttrs)));
3285 }
3286
3287 if (IsMustTailCall && InVarArgsFunc)
3288 return tokError(Msg: "expected '...' at end of argument list for musttail call "
3289 "in varargs function");
3290
3291 Lex.Lex(); // Lex the ')'.
3292 return false;
3293}
3294
3295/// parseRequiredTypeAttr
3296/// ::= attrname(<ty>)
3297bool LLParser::parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken,
3298 Attribute::AttrKind AttrKind) {
3299 Type *Ty = nullptr;
3300 if (!EatIfPresent(T: AttrToken))
3301 return true;
3302 if (!EatIfPresent(T: lltok::lparen))
3303 return error(L: Lex.getLoc(), Msg: "expected '('");
3304 if (parseType(Result&: Ty))
3305 return true;
3306 if (!EatIfPresent(T: lltok::rparen))
3307 return error(L: Lex.getLoc(), Msg: "expected ')'");
3308
3309 B.addTypeAttr(Kind: AttrKind, Ty);
3310 return false;
3311}
3312
3313/// parseRangeAttr
3314/// ::= range(<ty> <n>,<n>)
3315bool LLParser::parseRangeAttr(AttrBuilder &B) {
3316 Lex.Lex();
3317
3318 APInt Lower;
3319 APInt Upper;
3320 Type *Ty = nullptr;
3321 LocTy TyLoc;
3322
3323 auto ParseAPSInt = [&](unsigned BitWidth, APInt &Val) {
3324 if (Lex.getKind() != lltok::APSInt)
3325 return tokError(Msg: "expected integer");
3326 if (Lex.getAPSIntVal().getBitWidth() > BitWidth)
3327 return tokError(
3328 Msg: "integer is too large for the bit width of specified type");
3329 Val = Lex.getAPSIntVal().extend(width: BitWidth);
3330 Lex.Lex();
3331 return false;
3332 };
3333
3334 if (parseToken(T: lltok::lparen, ErrMsg: "expected '('") || parseType(Result&: Ty, Loc&: TyLoc))
3335 return true;
3336 if (!Ty->isIntegerTy())
3337 return error(L: TyLoc, Msg: "the range must have integer type!");
3338
3339 unsigned BitWidth = Ty->getPrimitiveSizeInBits();
3340
3341 if (ParseAPSInt(BitWidth, Lower) ||
3342 parseToken(T: lltok::comma, ErrMsg: "expected ','") || ParseAPSInt(BitWidth, Upper))
3343 return true;
3344 if (Lower == Upper && !Lower.isZero())
3345 return tokError(Msg: "the range represent the empty set but limits aren't 0!");
3346
3347 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'"))
3348 return true;
3349
3350 B.addRangeAttr(CR: ConstantRange(Lower, Upper));
3351 return false;
3352}
3353
3354/// parseInitializesAttr
3355/// ::= initializes((Lo1,Hi1),(Lo2,Hi2),...)
3356bool LLParser::parseInitializesAttr(AttrBuilder &B) {
3357 Lex.Lex();
3358
3359 auto ParseAPSInt = [&](APInt &Val) {
3360 if (Lex.getKind() != lltok::APSInt)
3361 return tokError(Msg: "expected integer");
3362 Val = Lex.getAPSIntVal().extend(width: 64);
3363 Lex.Lex();
3364 return false;
3365 };
3366
3367 if (parseToken(T: lltok::lparen, ErrMsg: "expected '('"))
3368 return true;
3369
3370 SmallVector<ConstantRange, 2> RangeList;
3371 // Parse each constant range.
3372 do {
3373 APInt Lower, Upper;
3374 if (parseToken(T: lltok::lparen, ErrMsg: "expected '('"))
3375 return true;
3376
3377 if (ParseAPSInt(Lower) || parseToken(T: lltok::comma, ErrMsg: "expected ','") ||
3378 ParseAPSInt(Upper))
3379 return true;
3380
3381 if (Lower == Upper)
3382 return tokError(Msg: "the range should not represent the full or empty set!");
3383
3384 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'"))
3385 return true;
3386
3387 RangeList.push_back(Elt: ConstantRange(Lower, Upper));
3388 } while (EatIfPresent(T: lltok::comma));
3389
3390 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'"))
3391 return true;
3392
3393 auto CRLOrNull = ConstantRangeList::getConstantRangeList(RangesRef: RangeList);
3394 if (!CRLOrNull.has_value())
3395 return tokError(Msg: "Invalid (unordered or overlapping) range list");
3396 B.addInitializesAttr(CRL: *CRLOrNull);
3397 return false;
3398}
3399
3400bool LLParser::parseCapturesAttr(AttrBuilder &B) {
3401 CaptureComponents Other = CaptureComponents::None;
3402 std::optional<CaptureComponents> Ret;
3403
3404 // We use syntax like captures(ret: address, provenance), so the colon
3405 // should not be interpreted as a label terminator.
3406 Lex.setIgnoreColonInIdentifiers(true);
3407 llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); });
3408
3409 Lex.Lex();
3410 if (parseToken(T: lltok::lparen, ErrMsg: "expected '('"))
3411 return true;
3412
3413 CaptureComponents *Current = &Other;
3414 bool SeenComponent = false;
3415 while (true) {
3416 if (EatIfPresent(T: lltok::kw_ret)) {
3417 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
3418 return true;
3419 if (Ret)
3420 return tokError(Msg: "duplicate 'ret' location");
3421 Ret = CaptureComponents::None;
3422 Current = &*Ret;
3423 SeenComponent = false;
3424 }
3425
3426 if (EatIfPresent(T: lltok::kw_none)) {
3427 if (SeenComponent)
3428 return tokError(Msg: "cannot use 'none' with other component");
3429 *Current = CaptureComponents::None;
3430 } else {
3431 if (SeenComponent && capturesNothing(CC: *Current))
3432 return tokError(Msg: "cannot use 'none' with other component");
3433
3434 if (EatIfPresent(T: lltok::kw_address_is_null))
3435 *Current |= CaptureComponents::AddressIsNull;
3436 else if (EatIfPresent(T: lltok::kw_address))
3437 *Current |= CaptureComponents::Address;
3438 else if (EatIfPresent(T: lltok::kw_provenance))
3439 *Current |= CaptureComponents::Provenance;
3440 else if (EatIfPresent(T: lltok::kw_read_provenance))
3441 *Current |= CaptureComponents::ReadProvenance;
3442 else
3443 return tokError(Msg: "expected one of 'none', 'address', 'address_is_null', "
3444 "'provenance' or 'read_provenance'");
3445 }
3446
3447 SeenComponent = true;
3448 if (EatIfPresent(T: lltok::rparen))
3449 break;
3450
3451 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' or ')'"))
3452 return true;
3453 }
3454
3455 B.addCapturesAttr(CI: CaptureInfo(Other, Ret.value_or(u&: Other)));
3456 return false;
3457}
3458
3459/// parseOptionalOperandBundles
3460/// ::= /*empty*/
3461/// ::= '[' OperandBundle [, OperandBundle ]* ']'
3462///
3463/// OperandBundle
3464/// ::= bundle-tag '(' ')'
3465/// ::= bundle-tag '(' Type Value [, Type Value ]* ')'
3466///
3467/// bundle-tag ::= String Constant
3468bool LLParser::parseOptionalOperandBundles(
3469 SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) {
3470 LocTy BeginLoc = Lex.getLoc();
3471 if (!EatIfPresent(T: lltok::lsquare))
3472 return false;
3473
3474 while (Lex.getKind() != lltok::rsquare) {
3475 // If this isn't the first operand bundle, we need a comma.
3476 if (!BundleList.empty() &&
3477 parseToken(T: lltok::comma, ErrMsg: "expected ',' in input list"))
3478 return true;
3479
3480 std::string Tag;
3481 if (parseStringConstant(Result&: Tag))
3482 return true;
3483
3484 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in operand bundle"))
3485 return true;
3486
3487 std::vector<Value *> Inputs;
3488 while (Lex.getKind() != lltok::rparen) {
3489 // If this isn't the first input, we need a comma.
3490 if (!Inputs.empty() &&
3491 parseToken(T: lltok::comma, ErrMsg: "expected ',' in input list"))
3492 return true;
3493
3494 Type *Ty = nullptr;
3495 Value *Input = nullptr;
3496 if (parseType(Result&: Ty))
3497 return true;
3498 if (Ty->isMetadataTy()) {
3499 if (parseMetadataAsValue(V&: Input, PFS))
3500 return true;
3501 } else if (parseValue(Ty, V&: Input, PFS)) {
3502 return true;
3503 }
3504 Inputs.push_back(x: Input);
3505 }
3506
3507 BundleList.emplace_back(Args: std::move(Tag), Args: std::move(Inputs));
3508
3509 Lex.Lex(); // Lex the ')'.
3510 }
3511
3512 if (BundleList.empty())
3513 return error(L: BeginLoc, Msg: "operand bundle set must not be empty");
3514
3515 Lex.Lex(); // Lex the ']'.
3516 return false;
3517}
3518
3519bool LLParser::checkValueID(LocTy Loc, StringRef Kind, StringRef Prefix,
3520 unsigned NextID, unsigned ID) {
3521 if (ID < NextID)
3522 return error(L: Loc, Msg: Kind + " expected to be numbered '" + Prefix +
3523 Twine(NextID) + "' or greater");
3524
3525 return false;
3526}
3527
3528/// parseArgumentList - parse the argument list for a function type or function
3529/// prototype.
3530/// ::= '(' ArgTypeListI ')'
3531/// ArgTypeListI
3532/// ::= /*empty*/
3533/// ::= '...'
3534/// ::= ArgTypeList ',' '...'
3535/// ::= ArgType (',' ArgType)*
3536///
3537bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
3538 SmallVectorImpl<unsigned> &UnnamedArgNums,
3539 bool &IsVarArg) {
3540 unsigned CurValID = 0;
3541 IsVarArg = false;
3542 assert(Lex.getKind() == lltok::lparen);
3543 Lex.Lex(); // eat the (.
3544
3545 if (Lex.getKind() != lltok::rparen) {
3546 do {
3547 // Handle ... at end of arg list.
3548 if (EatIfPresent(T: lltok::dotdotdot)) {
3549 IsVarArg = true;
3550 break;
3551 }
3552
3553 // Otherwise must be an argument type.
3554 LocTy TypeLoc = Lex.getLoc();
3555 Type *ArgTy = nullptr;
3556 AttrBuilder Attrs(M->getContext());
3557 if (parseType(Result&: ArgTy) || parseOptionalParamAttrs(B&: Attrs))
3558 return true;
3559
3560 if (ArgTy->isVoidTy())
3561 return error(L: TypeLoc, Msg: "argument can not have void type");
3562
3563 std::string Name;
3564 FileLoc IdentStart;
3565 FileLoc IdentEnd;
3566 bool Unnamed = false;
3567 if (Lex.getKind() == lltok::LocalVar) {
3568 Name = Lex.getStrVal();
3569 IdentStart = getTokLineColumnPos();
3570 Lex.Lex();
3571 IdentEnd = getPrevTokEndLineColumnPos();
3572 } else {
3573 unsigned ArgID;
3574 if (Lex.getKind() == lltok::LocalVarID) {
3575 ArgID = Lex.getUIntVal();
3576 IdentStart = getTokLineColumnPos();
3577 if (checkValueID(Loc: TypeLoc, Kind: "argument", Prefix: "%", NextID: CurValID, ID: ArgID))
3578 return true;
3579 Lex.Lex();
3580 IdentEnd = getPrevTokEndLineColumnPos();
3581 } else {
3582 ArgID = CurValID;
3583 Unnamed = true;
3584 }
3585 UnnamedArgNums.push_back(Elt: ArgID);
3586 CurValID = ArgID + 1;
3587 }
3588
3589 if (!FunctionType::isValidArgumentType(ArgTy))
3590 return error(L: TypeLoc, Msg: "invalid type for function argument");
3591
3592 ArgList.emplace_back(
3593 Args&: TypeLoc, Args&: ArgTy,
3594 Args: Unnamed ? std::nullopt
3595 : std::make_optional(t: FileLocRange(IdentStart, IdentEnd)),
3596 Args: AttributeSet::get(C&: ArgTy->getContext(), B: Attrs), Args: std::move(Name));
3597 } while (EatIfPresent(T: lltok::comma));
3598 }
3599
3600 return parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of argument list");
3601}
3602
3603/// parseFunctionType
3604/// ::= Type ArgumentList OptionalAttrs
3605bool LLParser::parseFunctionType(Type *&Result) {
3606 assert(Lex.getKind() == lltok::lparen);
3607
3608 if (!FunctionType::isValidReturnType(RetTy: Result))
3609 return tokError(Msg: "invalid function return type");
3610
3611 SmallVector<ArgInfo, 8> ArgList;
3612 bool IsVarArg;
3613 SmallVector<unsigned> UnnamedArgNums;
3614 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg))
3615 return true;
3616
3617 // Reject names on the arguments lists.
3618 for (const ArgInfo &Arg : ArgList) {
3619 if (!Arg.Name.empty())
3620 return error(L: Arg.Loc, Msg: "argument name invalid in function type");
3621 if (Arg.Attrs.hasAttributes())
3622 return error(L: Arg.Loc, Msg: "argument attributes invalid in function type");
3623 }
3624
3625 SmallVector<Type*, 16> ArgListTy;
3626 for (const ArgInfo &Arg : ArgList)
3627 ArgListTy.push_back(Elt: Arg.Ty);
3628
3629 Result = FunctionType::get(Result, Params: ArgListTy, isVarArg: IsVarArg);
3630 return false;
3631}
3632
3633/// parseAnonStructType - parse an anonymous struct type, which is inlined into
3634/// other structs.
3635bool LLParser::parseAnonStructType(Type *&Result, bool Packed) {
3636 SmallVector<Type*, 8> Elts;
3637 if (parseStructBody(Body&: Elts))
3638 return true;
3639
3640 Result = StructType::get(Context, Elements: Elts, isPacked: Packed);
3641 return false;
3642}
3643
3644/// parseStructDefinition - parse a struct in a 'type' definition.
3645bool LLParser::parseStructDefinition(SMLoc TypeLoc, StringRef Name,
3646 std::pair<Type *, LocTy> &Entry,
3647 Type *&ResultTy) {
3648 // If the type was already defined, diagnose the redefinition.
3649 if (Entry.first && !Entry.second.isValid())
3650 return error(L: TypeLoc, Msg: "redefinition of type");
3651
3652 // If we have opaque, just return without filling in the definition for the
3653 // struct. This counts as a definition as far as the .ll file goes.
3654 if (EatIfPresent(T: lltok::kw_opaque)) {
3655 // This type is being defined, so clear the location to indicate this.
3656 Entry.second = SMLoc();
3657
3658 // If this type number has never been uttered, create it.
3659 if (!Entry.first)
3660 Entry.first = StructType::create(Context, Name);
3661 ResultTy = Entry.first;
3662 return false;
3663 }
3664
3665 // If the type starts with '<', then it is either a packed struct or a vector.
3666 bool isPacked = EatIfPresent(T: lltok::less);
3667
3668 // If we don't have a struct, then we have a random type alias, which we
3669 // accept for compatibility with old files. These types are not allowed to be
3670 // forward referenced and not allowed to be recursive.
3671 if (Lex.getKind() != lltok::lbrace) {
3672 if (Entry.first)
3673 return error(L: TypeLoc, Msg: "forward references to non-struct type");
3674
3675 ResultTy = nullptr;
3676 if (isPacked)
3677 return parseArrayVectorType(Result&: ResultTy, IsVector: true);
3678 return parseType(Result&: ResultTy);
3679 }
3680
3681 // This type is being defined, so clear the location to indicate this.
3682 Entry.second = SMLoc();
3683
3684 // If this type number has never been uttered, create it.
3685 if (!Entry.first)
3686 Entry.first = StructType::create(Context, Name);
3687
3688 StructType *STy = cast<StructType>(Val: Entry.first);
3689
3690 SmallVector<Type*, 8> Body;
3691 if (parseStructBody(Body) ||
3692 (isPacked && parseToken(T: lltok::greater, ErrMsg: "expected '>' in packed struct")))
3693 return true;
3694
3695 if (auto E = STy->setBodyOrError(Elements: Body, isPacked))
3696 return tokError(Msg: toString(E: std::move(E)));
3697
3698 ResultTy = STy;
3699 return false;
3700}
3701
3702/// parseStructType: Handles packed and unpacked types. </> parsed elsewhere.
3703/// StructType
3704/// ::= '{' '}'
3705/// ::= '{' Type (',' Type)* '}'
3706/// ::= '<' '{' '}' '>'
3707/// ::= '<' '{' Type (',' Type)* '}' '>'
3708bool LLParser::parseStructBody(SmallVectorImpl<Type *> &Body) {
3709 assert(Lex.getKind() == lltok::lbrace);
3710 Lex.Lex(); // Consume the '{'
3711
3712 // Handle the empty struct.
3713 if (EatIfPresent(T: lltok::rbrace))
3714 return false;
3715
3716 LocTy EltTyLoc = Lex.getLoc();
3717 Type *Ty = nullptr;
3718 if (parseType(Result&: Ty))
3719 return true;
3720 Body.push_back(Elt: Ty);
3721
3722 if (!StructType::isValidElementType(ElemTy: Ty))
3723 return error(L: EltTyLoc, Msg: "invalid element type for struct");
3724
3725 while (EatIfPresent(T: lltok::comma)) {
3726 EltTyLoc = Lex.getLoc();
3727 if (parseType(Result&: Ty))
3728 return true;
3729
3730 if (!StructType::isValidElementType(ElemTy: Ty))
3731 return error(L: EltTyLoc, Msg: "invalid element type for struct");
3732
3733 Body.push_back(Elt: Ty);
3734 }
3735
3736 return parseToken(T: lltok::rbrace, ErrMsg: "expected '}' at end of struct");
3737}
3738
3739/// parseArrayVectorType - parse an array or vector type, assuming the first
3740/// token has already been consumed.
3741/// Type
3742/// ::= '[' APSINTVAL 'x' Types ']'
3743/// ::= '<' APSINTVAL 'x' Types '>'
3744/// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>'
3745bool LLParser::parseArrayVectorType(Type *&Result, bool IsVector) {
3746 bool Scalable = false;
3747
3748 if (IsVector && Lex.getKind() == lltok::kw_vscale) {
3749 Lex.Lex(); // consume the 'vscale'
3750 if (parseToken(T: lltok::kw_x, ErrMsg: "expected 'x' after vscale"))
3751 return true;
3752
3753 Scalable = true;
3754 }
3755
3756 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() ||
3757 Lex.getAPSIntVal().getBitWidth() > 64)
3758 return tokError(Msg: "expected number in address space");
3759
3760 LocTy SizeLoc = Lex.getLoc();
3761 uint64_t Size = Lex.getAPSIntVal().getZExtValue();
3762 Lex.Lex();
3763
3764 if (parseToken(T: lltok::kw_x, ErrMsg: "expected 'x' after element count"))
3765 return true;
3766
3767 LocTy TypeLoc = Lex.getLoc();
3768 Type *EltTy = nullptr;
3769 if (parseType(Result&: EltTy))
3770 return true;
3771
3772 if (parseToken(T: IsVector ? lltok::greater : lltok::rsquare,
3773 ErrMsg: "expected end of sequential type"))
3774 return true;
3775
3776 if (IsVector) {
3777 if (Size == 0)
3778 return error(L: SizeLoc, Msg: "zero element vector is illegal");
3779 if ((unsigned)Size != Size)
3780 return error(L: SizeLoc, Msg: "size too large for vector");
3781 if (!VectorType::isValidElementType(ElemTy: EltTy))
3782 return error(L: TypeLoc, Msg: "invalid vector element type");
3783 Result = VectorType::get(ElementType: EltTy, NumElements: unsigned(Size), Scalable);
3784 } else {
3785 if (!ArrayType::isValidElementType(ElemTy: EltTy))
3786 return error(L: TypeLoc, Msg: "invalid array element type");
3787 Result = ArrayType::get(ElementType: EltTy, NumElements: Size);
3788 }
3789 return false;
3790}
3791
3792/// parseTargetExtType - handle target extension type syntax
3793/// TargetExtType
3794/// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')'
3795///
3796/// TargetExtTypeParams
3797/// ::= /*empty*/
3798/// ::= ',' Type TargetExtTypeParams
3799///
3800/// TargetExtIntParams
3801/// ::= /*empty*/
3802/// ::= ',' uint32 TargetExtIntParams
3803bool LLParser::parseTargetExtType(Type *&Result) {
3804 Lex.Lex(); // Eat the 'target' keyword.
3805
3806 // Get the mandatory type name.
3807 std::string TypeName;
3808 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in target extension type") ||
3809 parseStringConstant(Result&: TypeName))
3810 return true;
3811
3812 // Parse all of the integer and type parameters at the same time; the use of
3813 // SeenInt will allow us to catch cases where type parameters follow integer
3814 // parameters.
3815 SmallVector<Type *> TypeParams;
3816 SmallVector<unsigned> IntParams;
3817 bool SeenInt = false;
3818 while (Lex.getKind() == lltok::comma) {
3819 Lex.Lex(); // Eat the comma.
3820
3821 if (Lex.getKind() == lltok::APSInt) {
3822 SeenInt = true;
3823 unsigned IntVal;
3824 if (parseUInt32(Val&: IntVal))
3825 return true;
3826 IntParams.push_back(Elt: IntVal);
3827 } else if (SeenInt) {
3828 // The only other kind of parameter we support is type parameters, which
3829 // must precede the integer parameters. This is therefore an error.
3830 return tokError(Msg: "expected uint32 param");
3831 } else {
3832 Type *TypeParam;
3833 if (parseType(Result&: TypeParam, /*AllowVoid=*/true))
3834 return true;
3835 TypeParams.push_back(Elt: TypeParam);
3836 }
3837 }
3838
3839 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in target extension type"))
3840 return true;
3841
3842 auto TTy =
3843 TargetExtType::getOrError(Context, Name: TypeName, Types: TypeParams, Ints: IntParams);
3844 if (auto E = TTy.takeError())
3845 return tokError(Msg: toString(E: std::move(E)));
3846
3847 Result = *TTy;
3848 return false;
3849}
3850
3851//===----------------------------------------------------------------------===//
3852// Function Semantic Analysis.
3853//===----------------------------------------------------------------------===//
3854
3855LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f,
3856 int functionNumber,
3857 ArrayRef<unsigned> UnnamedArgNums)
3858 : P(p), F(f), FunctionNumber(functionNumber) {
3859
3860 // Insert unnamed arguments into the NumberedVals list.
3861 auto It = UnnamedArgNums.begin();
3862 for (Argument &A : F.args()) {
3863 if (!A.hasName()) {
3864 unsigned ArgNum = *It++;
3865 NumberedVals.add(ID: ArgNum, V: &A);
3866 }
3867 }
3868}
3869
3870LLParser::PerFunctionState::~PerFunctionState() {
3871 // If there were any forward referenced non-basicblock values, delete them.
3872
3873 for (const auto &P : ForwardRefVals) {
3874 if (isa<BasicBlock>(Val: P.second.first))
3875 continue;
3876 P.second.first->replaceAllUsesWith(
3877 V: PoisonValue::get(T: P.second.first->getType()));
3878 P.second.first->deleteValue();
3879 }
3880
3881 for (const auto &P : ForwardRefValIDs) {
3882 if (isa<BasicBlock>(Val: P.second.first))
3883 continue;
3884 P.second.first->replaceAllUsesWith(
3885 V: PoisonValue::get(T: P.second.first->getType()));
3886 P.second.first->deleteValue();
3887 }
3888}
3889
3890bool LLParser::PerFunctionState::finishFunction() {
3891 if (!ForwardRefVals.empty())
3892 return P.error(L: ForwardRefVals.begin()->second.second,
3893 Msg: "use of undefined value '%" + ForwardRefVals.begin()->first +
3894 "'");
3895 if (!ForwardRefValIDs.empty())
3896 return P.error(L: ForwardRefValIDs.begin()->second.second,
3897 Msg: "use of undefined value '%" +
3898 Twine(ForwardRefValIDs.begin()->first) + "'");
3899 return false;
3900}
3901
3902/// getVal - Get a value with the specified name or ID, creating a
3903/// forward reference record if needed. This can return null if the value
3904/// exists but does not have the right type.
3905Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty,
3906 LocTy Loc) {
3907 // Look this name up in the normal function symbol table.
3908 Value *Val = F.getValueSymbolTable()->lookup(Name);
3909
3910 // If this is a forward reference for the value, see if we already created a
3911 // forward ref record.
3912 if (!Val) {
3913 auto I = ForwardRefVals.find(x: Name);
3914 if (I != ForwardRefVals.end())
3915 Val = I->second.first;
3916 }
3917
3918 // If we have the value in the symbol table or fwd-ref table, return it.
3919 if (Val)
3920 return P.checkValidVariableType(Loc, Name: "%" + Name, Ty, Val);
3921
3922 // Don't make placeholders with invalid type.
3923 if (!Ty->isFirstClassType()) {
3924 P.error(L: Loc, Msg: "invalid use of a non-first-class type");
3925 return nullptr;
3926 }
3927
3928 // Otherwise, create a new forward reference for this value and remember it.
3929 Value *FwdVal;
3930 if (Ty->isLabelTy()) {
3931 FwdVal = BasicBlock::Create(Context&: F.getContext(), Name, Parent: &F);
3932 } else {
3933 FwdVal = new Argument(Ty, Name);
3934 }
3935 if (FwdVal->getName() != Name) {
3936 P.error(L: Loc, Msg: "name is too long which can result in name collisions, "
3937 "consider making the name shorter or "
3938 "increasing -non-global-value-max-name-size");
3939 return nullptr;
3940 }
3941
3942 ForwardRefVals[Name] = std::make_pair(x&: FwdVal, y&: Loc);
3943 return FwdVal;
3944}
3945
3946Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) {
3947 // Look this name up in the normal function symbol table.
3948 Value *Val = NumberedVals.get(ID);
3949
3950 // If this is a forward reference for the value, see if we already created a
3951 // forward ref record.
3952 if (!Val) {
3953 auto I = ForwardRefValIDs.find(x: ID);
3954 if (I != ForwardRefValIDs.end())
3955 Val = I->second.first;
3956 }
3957
3958 // If we have the value in the symbol table or fwd-ref table, return it.
3959 if (Val)
3960 return P.checkValidVariableType(Loc, Name: "%" + Twine(ID), Ty, Val);
3961
3962 if (!Ty->isFirstClassType()) {
3963 P.error(L: Loc, Msg: "invalid use of a non-first-class type");
3964 return nullptr;
3965 }
3966
3967 // Otherwise, create a new forward reference for this value and remember it.
3968 Value *FwdVal;
3969 if (Ty->isLabelTy()) {
3970 FwdVal = BasicBlock::Create(Context&: F.getContext(), Name: "", Parent: &F);
3971 } else {
3972 FwdVal = new Argument(Ty);
3973 }
3974
3975 ForwardRefValIDs[ID] = std::make_pair(x&: FwdVal, y&: Loc);
3976 return FwdVal;
3977}
3978
3979/// setInstName - After an instruction is parsed and inserted into its
3980/// basic block, this installs its name.
3981bool LLParser::PerFunctionState::setInstName(int NameID,
3982 const std::string &NameStr,
3983 LocTy NameLoc, Instruction *Inst) {
3984 // If this instruction has void type, it cannot have a name or ID specified.
3985 if (Inst->getType()->isVoidTy()) {
3986 if (NameID != -1 || !NameStr.empty())
3987 return P.error(L: NameLoc, Msg: "instructions returning void cannot have a name");
3988 return false;
3989 }
3990
3991 // If this was a numbered instruction, verify that the instruction is the
3992 // expected value and resolve any forward references.
3993 if (NameStr.empty()) {
3994 // If neither a name nor an ID was specified, just use the next ID.
3995 if (NameID == -1)
3996 NameID = NumberedVals.getNext();
3997
3998 if (P.checkValueID(Loc: NameLoc, Kind: "instruction", Prefix: "%", NextID: NumberedVals.getNext(),
3999 ID: NameID))
4000 return true;
4001
4002 auto FI = ForwardRefValIDs.find(x: NameID);
4003 if (FI != ForwardRefValIDs.end()) {
4004 Value *Sentinel = FI->second.first;
4005 if (Sentinel->getType() != Inst->getType())
4006 return P.error(L: NameLoc, Msg: "instruction forward referenced with type '" +
4007 getTypeString(T: FI->second.first->getType()) +
4008 "'");
4009
4010 Sentinel->replaceAllUsesWith(V: Inst);
4011 Sentinel->deleteValue();
4012 ForwardRefValIDs.erase(position: FI);
4013 }
4014
4015 NumberedVals.add(ID: NameID, V: Inst);
4016 return false;
4017 }
4018
4019 // Otherwise, the instruction had a name. Resolve forward refs and set it.
4020 auto FI = ForwardRefVals.find(x: NameStr);
4021 if (FI != ForwardRefVals.end()) {
4022 Value *Sentinel = FI->second.first;
4023 if (Sentinel->getType() != Inst->getType())
4024 return P.error(L: NameLoc, Msg: "instruction forward referenced with type '" +
4025 getTypeString(T: FI->second.first->getType()) +
4026 "'");
4027
4028 Sentinel->replaceAllUsesWith(V: Inst);
4029 Sentinel->deleteValue();
4030 ForwardRefVals.erase(position: FI);
4031 }
4032
4033 // Set the name on the instruction.
4034 Inst->setName(NameStr);
4035
4036 if (Inst->getName() != NameStr)
4037 return P.error(L: NameLoc, Msg: "multiple definition of local value named '" +
4038 NameStr + "'");
4039 return false;
4040}
4041
4042/// getBB - Get a basic block with the specified name or ID, creating a
4043/// forward reference record if needed.
4044BasicBlock *LLParser::PerFunctionState::getBB(const std::string &Name,
4045 LocTy Loc) {
4046 return dyn_cast_or_null<BasicBlock>(
4047 Val: getVal(Name, Ty: Type::getLabelTy(C&: F.getContext()), Loc));
4048}
4049
4050BasicBlock *LLParser::PerFunctionState::getBB(unsigned ID, LocTy Loc) {
4051 return dyn_cast_or_null<BasicBlock>(
4052 Val: getVal(ID, Ty: Type::getLabelTy(C&: F.getContext()), Loc));
4053}
4054
4055/// defineBB - Define the specified basic block, which is either named or
4056/// unnamed. If there is an error, this returns null otherwise it returns
4057/// the block being defined.
4058BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
4059 int NameID, LocTy Loc) {
4060 BasicBlock *BB;
4061 if (Name.empty()) {
4062 if (NameID != -1) {
4063 if (P.checkValueID(Loc, Kind: "label", Prefix: "", NextID: NumberedVals.getNext(), ID: NameID))
4064 return nullptr;
4065 } else {
4066 NameID = NumberedVals.getNext();
4067 }
4068 BB = getBB(ID: NameID, Loc);
4069 if (!BB) {
4070 P.error(L: Loc, Msg: "unable to create block numbered '" + Twine(NameID) + "'");
4071 return nullptr;
4072 }
4073 } else {
4074 BB = getBB(Name, Loc);
4075 if (!BB) {
4076 P.error(L: Loc, Msg: "unable to create block named '" + Name + "'");
4077 return nullptr;
4078 }
4079 }
4080
4081 // Move the block to the end of the function. Forward ref'd blocks are
4082 // inserted wherever they happen to be referenced.
4083 F.splice(ToIt: F.end(), FromF: &F, FromIt: BB->getIterator());
4084
4085 // Remove the block from forward ref sets.
4086 if (Name.empty()) {
4087 ForwardRefValIDs.erase(x: NameID);
4088 NumberedVals.add(ID: NameID, V: BB);
4089 } else {
4090 // BB forward references are already in the function symbol table.
4091 ForwardRefVals.erase(x: Name);
4092 }
4093
4094 return BB;
4095}
4096
4097//===----------------------------------------------------------------------===//
4098// Constants.
4099//===----------------------------------------------------------------------===//
4100
4101/// parseValID - parse an abstract value that doesn't necessarily have a
4102/// type implied. For example, if we parse "4" we don't know what integer type
4103/// it has. The value will later be combined with its type and checked for
4104/// basic correctness. PFS is used to convert function-local operands of
4105/// metadata (since metadata operands are not just parsed here but also
4106/// converted to values). PFS can be null when we are not parsing metadata
4107/// values inside a function.
4108bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
4109 ID.Loc = Lex.getLoc();
4110 switch (Lex.getKind()) {
4111 default:
4112 return tokError(Msg: "expected value token");
4113 case lltok::GlobalID: // @42
4114 ID.UIntVal = Lex.getUIntVal();
4115 ID.Kind = ValID::t_GlobalID;
4116 break;
4117 case lltok::GlobalVar: // @foo
4118 ID.StrVal = Lex.getStrVal();
4119 ID.Kind = ValID::t_GlobalName;
4120 break;
4121 case lltok::LocalVarID: // %42
4122 ID.UIntVal = Lex.getUIntVal();
4123 ID.Kind = ValID::t_LocalID;
4124 break;
4125 case lltok::LocalVar: // %foo
4126 ID.StrVal = Lex.getStrVal();
4127 ID.Kind = ValID::t_LocalName;
4128 break;
4129 case lltok::APSInt:
4130 ID.APSIntVal = Lex.getAPSIntVal();
4131 ID.Kind = ValID::t_APSInt;
4132 break;
4133 case lltok::APFloat:
4134 ID.APFloatVal = Lex.getAPFloatVal();
4135 ID.Kind = ValID::t_APFloat;
4136 break;
4137 case lltok::kw_true:
4138 ID.ConstantVal = ConstantInt::getTrue(Context);
4139 ID.Kind = ValID::t_Constant;
4140 break;
4141 case lltok::kw_false:
4142 ID.ConstantVal = ConstantInt::getFalse(Context);
4143 ID.Kind = ValID::t_Constant;
4144 break;
4145 case lltok::kw_null: ID.Kind = ValID::t_Null; break;
4146 case lltok::kw_undef: ID.Kind = ValID::t_Undef; break;
4147 case lltok::kw_poison: ID.Kind = ValID::t_Poison; break;
4148 case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break;
4149 case lltok::kw_none: ID.Kind = ValID::t_None; break;
4150
4151 case lltok::lbrace: {
4152 // ValID ::= '{' ConstVector '}'
4153 Lex.Lex();
4154 SmallVector<Constant*, 16> Elts;
4155 if (parseGlobalValueVector(Elts) ||
4156 parseToken(T: lltok::rbrace, ErrMsg: "expected end of struct constant"))
4157 return true;
4158
4159 ID.ConstantStructElts = std::make_unique<Constant *[]>(num: Elts.size());
4160 ID.UIntVal = Elts.size();
4161 memcpy(dest: ID.ConstantStructElts.get(), src: Elts.data(),
4162 n: Elts.size() * sizeof(Elts[0]));
4163 ID.Kind = ValID::t_ConstantStruct;
4164 return false;
4165 }
4166 case lltok::less: {
4167 // ValID ::= '<' ConstVector '>' --> Vector.
4168 // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct.
4169 Lex.Lex();
4170 bool isPackedStruct = EatIfPresent(T: lltok::lbrace);
4171
4172 SmallVector<Constant*, 16> Elts;
4173 LocTy FirstEltLoc = Lex.getLoc();
4174 if (parseGlobalValueVector(Elts) ||
4175 (isPackedStruct &&
4176 parseToken(T: lltok::rbrace, ErrMsg: "expected end of packed struct")) ||
4177 parseToken(T: lltok::greater, ErrMsg: "expected end of constant"))
4178 return true;
4179
4180 if (isPackedStruct) {
4181 ID.ConstantStructElts = std::make_unique<Constant *[]>(num: Elts.size());
4182 memcpy(dest: ID.ConstantStructElts.get(), src: Elts.data(),
4183 n: Elts.size() * sizeof(Elts[0]));
4184 ID.UIntVal = Elts.size();
4185 ID.Kind = ValID::t_PackedConstantStruct;
4186 return false;
4187 }
4188
4189 if (Elts.empty())
4190 return error(L: ID.Loc, Msg: "constant vector must not be empty");
4191
4192 if (!Elts[0]->getType()->isIntegerTy() &&
4193 !Elts[0]->getType()->isFloatingPointTy() &&
4194 !Elts[0]->getType()->isPointerTy())
4195 return error(
4196 L: FirstEltLoc,
4197 Msg: "vector elements must have integer, pointer or floating point type");
4198
4199 // Verify that all the vector elements have the same type.
4200 for (unsigned i = 1, e = Elts.size(); i != e; ++i)
4201 if (Elts[i]->getType() != Elts[0]->getType())
4202 return error(L: FirstEltLoc, Msg: "vector element #" + Twine(i) +
4203 " is not of type '" +
4204 getTypeString(T: Elts[0]->getType()));
4205
4206 ID.ConstantVal = ConstantVector::get(V: Elts);
4207 ID.Kind = ValID::t_Constant;
4208 return false;
4209 }
4210 case lltok::lsquare: { // Array Constant
4211 Lex.Lex();
4212 SmallVector<Constant*, 16> Elts;
4213 LocTy FirstEltLoc = Lex.getLoc();
4214 if (parseGlobalValueVector(Elts) ||
4215 parseToken(T: lltok::rsquare, ErrMsg: "expected end of array constant"))
4216 return true;
4217
4218 // Handle empty element.
4219 if (Elts.empty()) {
4220 // Use undef instead of an array because it's inconvenient to determine
4221 // the element type at this point, there being no elements to examine.
4222 ID.Kind = ValID::t_EmptyArray;
4223 return false;
4224 }
4225
4226 if (!Elts[0]->getType()->isFirstClassType())
4227 return error(L: FirstEltLoc, Msg: "invalid array element type: " +
4228 getTypeString(T: Elts[0]->getType()));
4229
4230 ArrayType *ATy = ArrayType::get(ElementType: Elts[0]->getType(), NumElements: Elts.size());
4231
4232 // Verify all elements are correct type!
4233 for (unsigned i = 0, e = Elts.size(); i != e; ++i) {
4234 if (Elts[i]->getType() != Elts[0]->getType())
4235 return error(L: FirstEltLoc, Msg: "array element #" + Twine(i) +
4236 " is not of type '" +
4237 getTypeString(T: Elts[0]->getType()));
4238 }
4239
4240 ID.ConstantVal = ConstantArray::get(T: ATy, V: Elts);
4241 ID.Kind = ValID::t_Constant;
4242 return false;
4243 }
4244 case lltok::kw_c: // c "foo"
4245 Lex.Lex();
4246 ID.ConstantVal = ConstantDataArray::getString(Context, Initializer: Lex.getStrVal(),
4247 AddNull: false);
4248 if (parseToken(T: lltok::StringConstant, ErrMsg: "expected string"))
4249 return true;
4250 ID.Kind = ValID::t_Constant;
4251 return false;
4252
4253 case lltok::kw_asm: {
4254 // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ','
4255 // STRINGCONSTANT
4256 bool HasSideEffect, AlignStack, AsmDialect, CanThrow;
4257 Lex.Lex();
4258 if (parseOptionalToken(T: lltok::kw_sideeffect, Present&: HasSideEffect) ||
4259 parseOptionalToken(T: lltok::kw_alignstack, Present&: AlignStack) ||
4260 parseOptionalToken(T: lltok::kw_inteldialect, Present&: AsmDialect) ||
4261 parseOptionalToken(T: lltok::kw_unwind, Present&: CanThrow) ||
4262 parseStringConstant(Result&: ID.StrVal) ||
4263 parseToken(T: lltok::comma, ErrMsg: "expected comma in inline asm expression") ||
4264 parseToken(T: lltok::StringConstant, ErrMsg: "expected constraint string"))
4265 return true;
4266 ID.StrVal2 = Lex.getStrVal();
4267 ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack) << 1) |
4268 (unsigned(AsmDialect) << 2) | (unsigned(CanThrow) << 3);
4269 ID.Kind = ValID::t_InlineAsm;
4270 return false;
4271 }
4272
4273 case lltok::kw_blockaddress: {
4274 // ValID ::= 'blockaddress' '(' @foo ',' %bar ')'
4275 Lex.Lex();
4276
4277 ValID Fn, Label;
4278
4279 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in block address expression") ||
4280 parseValID(ID&: Fn, PFS) ||
4281 parseToken(T: lltok::comma,
4282 ErrMsg: "expected comma in block address expression") ||
4283 parseValID(ID&: Label, PFS) ||
4284 parseToken(T: lltok::rparen, ErrMsg: "expected ')' in block address expression"))
4285 return true;
4286
4287 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
4288 return error(L: Fn.Loc, Msg: "expected function name in blockaddress");
4289 if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
4290 return error(L: Label.Loc, Msg: "expected basic block name in blockaddress");
4291
4292 // Try to find the function (but skip it if it's forward-referenced).
4293 GlobalValue *GV = nullptr;
4294 if (Fn.Kind == ValID::t_GlobalID) {
4295 GV = NumberedVals.get(ID: Fn.UIntVal);
4296 } else if (!ForwardRefVals.count(x: Fn.StrVal)) {
4297 GV = M->getNamedValue(Name: Fn.StrVal);
4298 }
4299 Function *F = nullptr;
4300 if (GV) {
4301 // Confirm that it's actually a function with a definition.
4302 if (!isa<Function>(Val: GV))
4303 return error(L: Fn.Loc, Msg: "expected function name in blockaddress");
4304 F = cast<Function>(Val: GV);
4305 if (F->isDeclaration())
4306 return error(L: Fn.Loc, Msg: "cannot take blockaddress inside a declaration");
4307 }
4308
4309 if (!F) {
4310 // Make a global variable as a placeholder for this reference.
4311 GlobalValue *&FwdRef =
4312 ForwardRefBlockAddresses[std::move(Fn)][std::move(Label)];
4313 if (!FwdRef) {
4314 unsigned FwdDeclAS;
4315 if (ExpectedTy) {
4316 // If we know the type that the blockaddress is being assigned to,
4317 // we can use the address space of that type.
4318 if (!ExpectedTy->isPointerTy())
4319 return error(L: ID.Loc,
4320 Msg: "type of blockaddress must be a pointer and not '" +
4321 getTypeString(T: ExpectedTy) + "'");
4322 FwdDeclAS = ExpectedTy->getPointerAddressSpace();
4323 } else if (PFS) {
4324 // Otherwise, we default the address space of the current function.
4325 FwdDeclAS = PFS->getFunction().getAddressSpace();
4326 } else {
4327 llvm_unreachable("Unknown address space for blockaddress");
4328 }
4329 FwdRef = new GlobalVariable(
4330 *M, Type::getInt8Ty(C&: Context), false, GlobalValue::InternalLinkage,
4331 nullptr, "", nullptr, GlobalValue::NotThreadLocal, FwdDeclAS);
4332 }
4333
4334 ID.ConstantVal = FwdRef;
4335 ID.Kind = ValID::t_Constant;
4336 return false;
4337 }
4338
4339 // We found the function; now find the basic block. Don't use PFS, since we
4340 // might be inside a constant expression.
4341 BasicBlock *BB;
4342 if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) {
4343 if (Label.Kind == ValID::t_LocalID)
4344 BB = BlockAddressPFS->getBB(ID: Label.UIntVal, Loc: Label.Loc);
4345 else
4346 BB = BlockAddressPFS->getBB(Name: Label.StrVal, Loc: Label.Loc);
4347 if (!BB)
4348 return error(L: Label.Loc, Msg: "referenced value is not a basic block");
4349 } else {
4350 if (Label.Kind == ValID::t_LocalID)
4351 return error(L: Label.Loc, Msg: "cannot take address of numeric label after "
4352 "the function is defined");
4353 BB = dyn_cast_or_null<BasicBlock>(
4354 Val: F->getValueSymbolTable()->lookup(Name: Label.StrVal));
4355 if (!BB)
4356 return error(L: Label.Loc, Msg: "referenced value is not a basic block");
4357 }
4358
4359 ID.ConstantVal = BlockAddress::get(F, BB);
4360 ID.Kind = ValID::t_Constant;
4361 return false;
4362 }
4363
4364 case lltok::kw_dso_local_equivalent: {
4365 // ValID ::= 'dso_local_equivalent' @foo
4366 Lex.Lex();
4367
4368 ValID Fn;
4369
4370 if (parseValID(ID&: Fn, PFS))
4371 return true;
4372
4373 if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
4374 return error(L: Fn.Loc,
4375 Msg: "expected global value name in dso_local_equivalent");
4376
4377 // Try to find the function (but skip it if it's forward-referenced).
4378 GlobalValue *GV = nullptr;
4379 if (Fn.Kind == ValID::t_GlobalID) {
4380 GV = NumberedVals.get(ID: Fn.UIntVal);
4381 } else if (!ForwardRefVals.count(x: Fn.StrVal)) {
4382 GV = M->getNamedValue(Name: Fn.StrVal);
4383 }
4384
4385 if (!GV) {
4386 // Make a placeholder global variable as a placeholder for this reference.
4387 auto &FwdRefMap = (Fn.Kind == ValID::t_GlobalID)
4388 ? ForwardRefDSOLocalEquivalentIDs
4389 : ForwardRefDSOLocalEquivalentNames;
4390 GlobalValue *&FwdRef = FwdRefMap[Fn];
4391 if (!FwdRef) {
4392 FwdRef = new GlobalVariable(*M, Type::getInt8Ty(C&: Context), false,
4393 GlobalValue::InternalLinkage, nullptr, "",
4394 nullptr, GlobalValue::NotThreadLocal);
4395 }
4396
4397 ID.ConstantVal = FwdRef;
4398 ID.Kind = ValID::t_Constant;
4399 return false;
4400 }
4401
4402 if (!GV->getValueType()->isFunctionTy())
4403 return error(L: Fn.Loc, Msg: "expected a function, alias to function, or ifunc "
4404 "in dso_local_equivalent");
4405
4406 ID.ConstantVal = DSOLocalEquivalent::get(GV);
4407 ID.Kind = ValID::t_Constant;
4408 return false;
4409 }
4410
4411 case lltok::kw_no_cfi: {
4412 // ValID ::= 'no_cfi' @foo
4413 Lex.Lex();
4414
4415 if (parseValID(ID, PFS))
4416 return true;
4417
4418 if (ID.Kind != ValID::t_GlobalID && ID.Kind != ValID::t_GlobalName)
4419 return error(L: ID.Loc, Msg: "expected global value name in no_cfi");
4420
4421 ID.NoCFI = true;
4422 return false;
4423 }
4424 case lltok::kw_ptrauth: {
4425 // ValID ::= 'ptrauth' '(' ptr @foo ',' i32 <key>
4426 // (',' i64 <disc> (',' ptr addrdisc (',' ptr ds)?
4427 // )? )? ')'
4428 Lex.Lex();
4429
4430 Constant *Ptr, *Key;
4431 Constant *Disc = nullptr, *AddrDisc = nullptr,
4432 *DeactivationSymbol = nullptr;
4433
4434 if (parseToken(T: lltok::lparen,
4435 ErrMsg: "expected '(' in constant ptrauth expression") ||
4436 parseGlobalTypeAndValue(V&: Ptr) ||
4437 parseToken(T: lltok::comma,
4438 ErrMsg: "expected comma in constant ptrauth expression") ||
4439 parseGlobalTypeAndValue(V&: Key))
4440 return true;
4441 // If present, parse the optional disc/addrdisc/ds.
4442 if (EatIfPresent(T: lltok::comma) && parseGlobalTypeAndValue(V&: Disc))
4443 return true;
4444 if (EatIfPresent(T: lltok::comma) && parseGlobalTypeAndValue(V&: AddrDisc))
4445 return true;
4446 if (EatIfPresent(T: lltok::comma) &&
4447 parseGlobalTypeAndValue(V&: DeactivationSymbol))
4448 return true;
4449 if (parseToken(T: lltok::rparen,
4450 ErrMsg: "expected ')' in constant ptrauth expression"))
4451 return true;
4452
4453 if (!Ptr->getType()->isPointerTy())
4454 return error(L: ID.Loc, Msg: "constant ptrauth base pointer must be a pointer");
4455
4456 auto *KeyC = dyn_cast<ConstantInt>(Val: Key);
4457 if (!KeyC || KeyC->getBitWidth() != 32)
4458 return error(L: ID.Loc, Msg: "constant ptrauth key must be i32 constant");
4459
4460 ConstantInt *DiscC = nullptr;
4461 if (Disc) {
4462 DiscC = dyn_cast<ConstantInt>(Val: Disc);
4463 if (!DiscC || DiscC->getBitWidth() != 64)
4464 return error(
4465 L: ID.Loc,
4466 Msg: "constant ptrauth integer discriminator must be i64 constant");
4467 } else {
4468 DiscC = ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: 0);
4469 }
4470
4471 if (AddrDisc) {
4472 if (!AddrDisc->getType()->isPointerTy())
4473 return error(
4474 L: ID.Loc, Msg: "constant ptrauth address discriminator must be a pointer");
4475 } else {
4476 AddrDisc = ConstantPointerNull::get(T: PointerType::get(C&: Context, AddressSpace: 0));
4477 }
4478
4479 if (!DeactivationSymbol)
4480 DeactivationSymbol =
4481 ConstantPointerNull::get(T: PointerType::get(C&: Context, AddressSpace: 0));
4482 if (!DeactivationSymbol->getType()->isPointerTy())
4483 return error(L: ID.Loc,
4484 Msg: "constant ptrauth deactivation symbol must be a pointer");
4485
4486 ID.ConstantVal =
4487 ConstantPtrAuth::get(Ptr, Key: KeyC, Disc: DiscC, AddrDisc, DeactivationSymbol);
4488 ID.Kind = ValID::t_Constant;
4489 return false;
4490 }
4491
4492 case lltok::kw_trunc:
4493 case lltok::kw_bitcast:
4494 case lltok::kw_addrspacecast:
4495 case lltok::kw_inttoptr:
4496 case lltok::kw_ptrtoaddr:
4497 case lltok::kw_ptrtoint: {
4498 unsigned Opc = Lex.getUIntVal();
4499 Type *DestTy = nullptr;
4500 Constant *SrcVal;
4501 Lex.Lex();
4502 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' after constantexpr cast") ||
4503 parseGlobalTypeAndValue(V&: SrcVal) ||
4504 parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in constantexpr cast") ||
4505 parseType(Result&: DestTy) ||
4506 parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of constantexpr cast"))
4507 return true;
4508 if (!CastInst::castIsValid(op: (Instruction::CastOps)Opc, S: SrcVal, DstTy: DestTy))
4509 return error(L: ID.Loc, Msg: "invalid cast opcode for cast from '" +
4510 getTypeString(T: SrcVal->getType()) + "' to '" +
4511 getTypeString(T: DestTy) + "'");
4512 ID.ConstantVal = ConstantExpr::getCast(ops: (Instruction::CastOps)Opc,
4513 C: SrcVal, Ty: DestTy);
4514 ID.Kind = ValID::t_Constant;
4515 return false;
4516 }
4517 case lltok::kw_extractvalue:
4518 return error(L: ID.Loc, Msg: "extractvalue constexprs are no longer supported");
4519 case lltok::kw_insertvalue:
4520 return error(L: ID.Loc, Msg: "insertvalue constexprs are no longer supported");
4521 case lltok::kw_udiv:
4522 return error(L: ID.Loc, Msg: "udiv constexprs are no longer supported");
4523 case lltok::kw_sdiv:
4524 return error(L: ID.Loc, Msg: "sdiv constexprs are no longer supported");
4525 case lltok::kw_urem:
4526 return error(L: ID.Loc, Msg: "urem constexprs are no longer supported");
4527 case lltok::kw_srem:
4528 return error(L: ID.Loc, Msg: "srem constexprs are no longer supported");
4529 case lltok::kw_fadd:
4530 return error(L: ID.Loc, Msg: "fadd constexprs are no longer supported");
4531 case lltok::kw_fsub:
4532 return error(L: ID.Loc, Msg: "fsub constexprs are no longer supported");
4533 case lltok::kw_fmul:
4534 return error(L: ID.Loc, Msg: "fmul constexprs are no longer supported");
4535 case lltok::kw_fdiv:
4536 return error(L: ID.Loc, Msg: "fdiv constexprs are no longer supported");
4537 case lltok::kw_frem:
4538 return error(L: ID.Loc, Msg: "frem constexprs are no longer supported");
4539 case lltok::kw_and:
4540 return error(L: ID.Loc, Msg: "and constexprs are no longer supported");
4541 case lltok::kw_or:
4542 return error(L: ID.Loc, Msg: "or constexprs are no longer supported");
4543 case lltok::kw_lshr:
4544 return error(L: ID.Loc, Msg: "lshr constexprs are no longer supported");
4545 case lltok::kw_ashr:
4546 return error(L: ID.Loc, Msg: "ashr constexprs are no longer supported");
4547 case lltok::kw_shl:
4548 return error(L: ID.Loc, Msg: "shl constexprs are no longer supported");
4549 case lltok::kw_mul:
4550 return error(L: ID.Loc, Msg: "mul constexprs are no longer supported");
4551 case lltok::kw_fneg:
4552 return error(L: ID.Loc, Msg: "fneg constexprs are no longer supported");
4553 case lltok::kw_select:
4554 return error(L: ID.Loc, Msg: "select constexprs are no longer supported");
4555 case lltok::kw_zext:
4556 return error(L: ID.Loc, Msg: "zext constexprs are no longer supported");
4557 case lltok::kw_sext:
4558 return error(L: ID.Loc, Msg: "sext constexprs are no longer supported");
4559 case lltok::kw_fptrunc:
4560 return error(L: ID.Loc, Msg: "fptrunc constexprs are no longer supported");
4561 case lltok::kw_fpext:
4562 return error(L: ID.Loc, Msg: "fpext constexprs are no longer supported");
4563 case lltok::kw_uitofp:
4564 return error(L: ID.Loc, Msg: "uitofp constexprs are no longer supported");
4565 case lltok::kw_sitofp:
4566 return error(L: ID.Loc, Msg: "sitofp constexprs are no longer supported");
4567 case lltok::kw_fptoui:
4568 return error(L: ID.Loc, Msg: "fptoui constexprs are no longer supported");
4569 case lltok::kw_fptosi:
4570 return error(L: ID.Loc, Msg: "fptosi constexprs are no longer supported");
4571 case lltok::kw_icmp:
4572 return error(L: ID.Loc, Msg: "icmp constexprs are no longer supported");
4573 case lltok::kw_fcmp:
4574 return error(L: ID.Loc, Msg: "fcmp constexprs are no longer supported");
4575
4576 // Binary Operators.
4577 case lltok::kw_add:
4578 case lltok::kw_sub:
4579 case lltok::kw_xor: {
4580 bool NUW = false;
4581 bool NSW = false;
4582 unsigned Opc = Lex.getUIntVal();
4583 Constant *Val0, *Val1;
4584 Lex.Lex();
4585 if (Opc == Instruction::Add || Opc == Instruction::Sub ||
4586 Opc == Instruction::Mul) {
4587 if (EatIfPresent(T: lltok::kw_nuw))
4588 NUW = true;
4589 if (EatIfPresent(T: lltok::kw_nsw)) {
4590 NSW = true;
4591 if (EatIfPresent(T: lltok::kw_nuw))
4592 NUW = true;
4593 }
4594 }
4595 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in binary constantexpr") ||
4596 parseGlobalTypeAndValue(V&: Val0) ||
4597 parseToken(T: lltok::comma, ErrMsg: "expected comma in binary constantexpr") ||
4598 parseGlobalTypeAndValue(V&: Val1) ||
4599 parseToken(T: lltok::rparen, ErrMsg: "expected ')' in binary constantexpr"))
4600 return true;
4601 if (Val0->getType() != Val1->getType())
4602 return error(L: ID.Loc, Msg: "operands of constexpr must have same type");
4603 // Check that the type is valid for the operator.
4604 if (!Val0->getType()->isIntOrIntVectorTy())
4605 return error(L: ID.Loc,
4606 Msg: "constexpr requires integer or integer vector operands");
4607 unsigned Flags = 0;
4608 if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap;
4609 if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap;
4610 ID.ConstantVal = ConstantExpr::get(Opcode: Opc, C1: Val0, C2: Val1, Flags);
4611 ID.Kind = ValID::t_Constant;
4612 return false;
4613 }
4614
4615 case lltok::kw_splat: {
4616 Lex.Lex();
4617 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' after vector splat"))
4618 return true;
4619 Constant *C;
4620 if (parseGlobalTypeAndValue(V&: C))
4621 return true;
4622 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of vector splat"))
4623 return true;
4624
4625 ID.ConstantVal = C;
4626 ID.Kind = ValID::t_ConstantSplat;
4627 return false;
4628 }
4629
4630 case lltok::kw_getelementptr:
4631 case lltok::kw_shufflevector:
4632 case lltok::kw_insertelement:
4633 case lltok::kw_extractelement: {
4634 unsigned Opc = Lex.getUIntVal();
4635 SmallVector<Constant*, 16> Elts;
4636 GEPNoWrapFlags NW;
4637 bool HasInRange = false;
4638 APSInt InRangeStart;
4639 APSInt InRangeEnd;
4640 Type *Ty;
4641 Lex.Lex();
4642
4643 if (Opc == Instruction::GetElementPtr) {
4644 while (true) {
4645 if (EatIfPresent(T: lltok::kw_inbounds))
4646 NW |= GEPNoWrapFlags::inBounds();
4647 else if (EatIfPresent(T: lltok::kw_nusw))
4648 NW |= GEPNoWrapFlags::noUnsignedSignedWrap();
4649 else if (EatIfPresent(T: lltok::kw_nuw))
4650 NW |= GEPNoWrapFlags::noUnsignedWrap();
4651 else
4652 break;
4653 }
4654
4655 if (EatIfPresent(T: lltok::kw_inrange)) {
4656 if (parseToken(T: lltok::lparen, ErrMsg: "expected '('"))
4657 return true;
4658 if (Lex.getKind() != lltok::APSInt)
4659 return tokError(Msg: "expected integer");
4660 InRangeStart = Lex.getAPSIntVal();
4661 Lex.Lex();
4662 if (parseToken(T: lltok::comma, ErrMsg: "expected ','"))
4663 return true;
4664 if (Lex.getKind() != lltok::APSInt)
4665 return tokError(Msg: "expected integer");
4666 InRangeEnd = Lex.getAPSIntVal();
4667 Lex.Lex();
4668 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'"))
4669 return true;
4670 HasInRange = true;
4671 }
4672 }
4673
4674 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in constantexpr"))
4675 return true;
4676
4677 if (Opc == Instruction::GetElementPtr) {
4678 if (parseType(Result&: Ty) ||
4679 parseToken(T: lltok::comma, ErrMsg: "expected comma after getelementptr's type"))
4680 return true;
4681 }
4682
4683 if (parseGlobalValueVector(Elts) ||
4684 parseToken(T: lltok::rparen, ErrMsg: "expected ')' in constantexpr"))
4685 return true;
4686
4687 if (Opc == Instruction::GetElementPtr) {
4688 if (Elts.size() == 0 ||
4689 !Elts[0]->getType()->isPtrOrPtrVectorTy())
4690 return error(L: ID.Loc, Msg: "base of getelementptr must be a pointer");
4691
4692 Type *BaseType = Elts[0]->getType();
4693 std::optional<ConstantRange> InRange;
4694 if (HasInRange) {
4695 unsigned IndexWidth =
4696 M->getDataLayout().getIndexTypeSizeInBits(Ty: BaseType);
4697 InRangeStart = InRangeStart.extOrTrunc(width: IndexWidth);
4698 InRangeEnd = InRangeEnd.extOrTrunc(width: IndexWidth);
4699 if (InRangeStart.sge(RHS: InRangeEnd))
4700 return error(L: ID.Loc, Msg: "expected end to be larger than start");
4701 InRange = ConstantRange::getNonEmpty(Lower: InRangeStart, Upper: InRangeEnd);
4702 }
4703
4704 unsigned GEPWidth =
4705 BaseType->isVectorTy()
4706 ? cast<FixedVectorType>(Val: BaseType)->getNumElements()
4707 : 0;
4708
4709 ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end());
4710 for (Constant *Val : Indices) {
4711 Type *ValTy = Val->getType();
4712 if (!ValTy->isIntOrIntVectorTy())
4713 return error(L: ID.Loc, Msg: "getelementptr index must be an integer");
4714 if (auto *ValVTy = dyn_cast<VectorType>(Val: ValTy)) {
4715 unsigned ValNumEl = cast<FixedVectorType>(Val: ValVTy)->getNumElements();
4716 if (GEPWidth && (ValNumEl != GEPWidth))
4717 return error(
4718 L: ID.Loc,
4719 Msg: "getelementptr vector index has a wrong number of elements");
4720 // GEPWidth may have been unknown because the base is a scalar,
4721 // but it is known now.
4722 GEPWidth = ValNumEl;
4723 }
4724 }
4725
4726 SmallPtrSet<Type*, 4> Visited;
4727 if (!Indices.empty() && !Ty->isSized(Visited: &Visited))
4728 return error(L: ID.Loc, Msg: "base element of getelementptr must be sized");
4729
4730 if (!ConstantExpr::isSupportedGetElementPtr(SrcElemTy: Ty))
4731 return error(L: ID.Loc, Msg: "invalid base element for constant getelementptr");
4732
4733 if (!GetElementPtrInst::getIndexedType(Ty, IdxList: Indices))
4734 return error(L: ID.Loc, Msg: "invalid getelementptr indices");
4735
4736 ID.ConstantVal =
4737 ConstantExpr::getGetElementPtr(Ty, C: Elts[0], IdxList: Indices, NW, InRange);
4738 } else if (Opc == Instruction::ShuffleVector) {
4739 if (Elts.size() != 3)
4740 return error(L: ID.Loc, Msg: "expected three operands to shufflevector");
4741 if (!ShuffleVectorInst::isValidOperands(V1: Elts[0], V2: Elts[1], Mask: Elts[2]))
4742 return error(L: ID.Loc, Msg: "invalid operands to shufflevector");
4743 SmallVector<int, 16> Mask;
4744 ShuffleVectorInst::getShuffleMask(Mask: cast<Constant>(Val: Elts[2]), Result&: Mask);
4745 ID.ConstantVal = ConstantExpr::getShuffleVector(V1: Elts[0], V2: Elts[1], Mask);
4746 } else if (Opc == Instruction::ExtractElement) {
4747 if (Elts.size() != 2)
4748 return error(L: ID.Loc, Msg: "expected two operands to extractelement");
4749 if (!ExtractElementInst::isValidOperands(Vec: Elts[0], Idx: Elts[1]))
4750 return error(L: ID.Loc, Msg: "invalid extractelement operands");
4751 ID.ConstantVal = ConstantExpr::getExtractElement(Vec: Elts[0], Idx: Elts[1]);
4752 } else {
4753 assert(Opc == Instruction::InsertElement && "Unknown opcode");
4754 if (Elts.size() != 3)
4755 return error(L: ID.Loc, Msg: "expected three operands to insertelement");
4756 if (!InsertElementInst::isValidOperands(Vec: Elts[0], NewElt: Elts[1], Idx: Elts[2]))
4757 return error(L: ID.Loc, Msg: "invalid insertelement operands");
4758 ID.ConstantVal =
4759 ConstantExpr::getInsertElement(Vec: Elts[0], Elt: Elts[1],Idx: Elts[2]);
4760 }
4761
4762 ID.Kind = ValID::t_Constant;
4763 return false;
4764 }
4765 }
4766
4767 Lex.Lex();
4768 return false;
4769}
4770
4771/// parseGlobalValue - parse a global value with the specified type.
4772bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) {
4773 C = nullptr;
4774 ValID ID;
4775 Value *V = nullptr;
4776 bool Parsed = parseValID(ID, /*PFS=*/nullptr, ExpectedTy: Ty) ||
4777 convertValIDToValue(Ty, ID, V, PFS: nullptr);
4778 if (V && !(C = dyn_cast<Constant>(Val: V)))
4779 return error(L: ID.Loc, Msg: "global values must be constants");
4780 return Parsed;
4781}
4782
4783bool LLParser::parseGlobalTypeAndValue(Constant *&V) {
4784 Type *Ty = nullptr;
4785 return parseType(Result&: Ty) || parseGlobalValue(Ty, C&: V);
4786}
4787
4788bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) {
4789 C = nullptr;
4790
4791 LocTy KwLoc = Lex.getLoc();
4792 if (!EatIfPresent(T: lltok::kw_comdat))
4793 return false;
4794
4795 if (EatIfPresent(T: lltok::lparen)) {
4796 if (Lex.getKind() != lltok::ComdatVar)
4797 return tokError(Msg: "expected comdat variable");
4798 C = getComdat(Name: Lex.getStrVal(), Loc: Lex.getLoc());
4799 Lex.Lex();
4800 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' after comdat var"))
4801 return true;
4802 } else {
4803 if (GlobalName.empty())
4804 return tokError(Msg: "comdat cannot be unnamed");
4805 C = getComdat(Name: std::string(GlobalName), Loc: KwLoc);
4806 }
4807
4808 return false;
4809}
4810
4811/// parseGlobalValueVector
4812/// ::= /*empty*/
4813/// ::= TypeAndValue (',' TypeAndValue)*
4814bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) {
4815 // Empty list.
4816 if (Lex.getKind() == lltok::rbrace ||
4817 Lex.getKind() == lltok::rsquare ||
4818 Lex.getKind() == lltok::greater ||
4819 Lex.getKind() == lltok::rparen)
4820 return false;
4821
4822 do {
4823 // Let the caller deal with inrange.
4824 if (Lex.getKind() == lltok::kw_inrange)
4825 return false;
4826
4827 Constant *C;
4828 if (parseGlobalTypeAndValue(V&: C))
4829 return true;
4830 Elts.push_back(Elt: C);
4831 } while (EatIfPresent(T: lltok::comma));
4832
4833 return false;
4834}
4835
4836bool LLParser::parseMDTuple(MDNode *&MD, bool IsDistinct) {
4837 SmallVector<Metadata *, 16> Elts;
4838 if (parseMDNodeVector(Elts))
4839 return true;
4840
4841 MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts);
4842 return false;
4843}
4844
4845/// MDNode:
4846/// ::= !{ ... }
4847/// ::= !7
4848/// ::= !DILocation(...)
4849bool LLParser::parseMDNode(MDNode *&N) {
4850 if (Lex.getKind() == lltok::MetadataVar)
4851 return parseSpecializedMDNode(N);
4852
4853 return parseToken(T: lltok::exclaim, ErrMsg: "expected '!' here") || parseMDNodeTail(N);
4854}
4855
4856bool LLParser::parseMDNodeTail(MDNode *&N) {
4857 // !{ ... }
4858 if (Lex.getKind() == lltok::lbrace)
4859 return parseMDTuple(MD&: N);
4860
4861 // !42
4862 return parseMDNodeID(Result&: N);
4863}
4864
4865namespace {
4866
4867/// Structure to represent an optional metadata field.
4868template <class FieldTy> struct MDFieldImpl {
4869 typedef MDFieldImpl ImplTy;
4870 FieldTy Val;
4871 bool Seen;
4872
4873 void assign(FieldTy Val) {
4874 Seen = true;
4875 this->Val = std::move(Val);
4876 }
4877
4878 explicit MDFieldImpl(FieldTy Default)
4879 : Val(std::move(Default)), Seen(false) {}
4880};
4881
4882/// Structure to represent an optional metadata field that
4883/// can be of either type (A or B) and encapsulates the
4884/// MD<typeofA>Field and MD<typeofB>Field structs, so not
4885/// to reimplement the specifics for representing each Field.
4886template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl {
4887 typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy;
4888 FieldTypeA A;
4889 FieldTypeB B;
4890 bool Seen;
4891
4892 enum {
4893 IsInvalid = 0,
4894 IsTypeA = 1,
4895 IsTypeB = 2
4896 } WhatIs;
4897
4898 void assign(FieldTypeA A) {
4899 Seen = true;
4900 this->A = std::move(A);
4901 WhatIs = IsTypeA;
4902 }
4903
4904 void assign(FieldTypeB B) {
4905 Seen = true;
4906 this->B = std::move(B);
4907 WhatIs = IsTypeB;
4908 }
4909
4910 explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB)
4911 : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false),
4912 WhatIs(IsInvalid) {}
4913};
4914
4915struct MDUnsignedField : public MDFieldImpl<uint64_t> {
4916 uint64_t Max;
4917
4918 MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX)
4919 : ImplTy(Default), Max(Max) {}
4920};
4921
4922struct LineField : public MDUnsignedField {
4923 LineField() : MDUnsignedField(0, UINT32_MAX) {}
4924};
4925
4926struct ColumnField : public MDUnsignedField {
4927 ColumnField() : MDUnsignedField(0, UINT16_MAX) {}
4928};
4929
4930struct DwarfTagField : public MDUnsignedField {
4931 DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {}
4932 DwarfTagField(dwarf::Tag DefaultTag)
4933 : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {}
4934};
4935
4936struct DwarfMacinfoTypeField : public MDUnsignedField {
4937 DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {}
4938 DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType)
4939 : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {}
4940};
4941
4942struct DwarfAttEncodingField : public MDUnsignedField {
4943 DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {}
4944};
4945
4946struct DwarfVirtualityField : public MDUnsignedField {
4947 DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {}
4948};
4949
4950struct DwarfLangField : public MDUnsignedField {
4951 DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {}
4952};
4953
4954struct DwarfSourceLangNameField : public MDUnsignedField {
4955 DwarfSourceLangNameField() : MDUnsignedField(0, UINT32_MAX) {}
4956};
4957
4958struct DwarfCCField : public MDUnsignedField {
4959 DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {}
4960};
4961
4962struct DwarfEnumKindField : public MDUnsignedField {
4963 DwarfEnumKindField()
4964 : MDUnsignedField(dwarf::DW_APPLE_ENUM_KIND_invalid,
4965 dwarf::DW_APPLE_ENUM_KIND_max) {}
4966};
4967
4968struct EmissionKindField : public MDUnsignedField {
4969 EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {}
4970};
4971
4972struct FixedPointKindField : public MDUnsignedField {
4973 FixedPointKindField()
4974 : MDUnsignedField(0, DIFixedPointType::LastFixedPointKind) {}
4975};
4976
4977struct NameTableKindField : public MDUnsignedField {
4978 NameTableKindField()
4979 : MDUnsignedField(
4980 0, (unsigned)
4981 DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {}
4982};
4983
4984struct DIFlagField : public MDFieldImpl<DINode::DIFlags> {
4985 DIFlagField() : MDFieldImpl(DINode::FlagZero) {}
4986};
4987
4988struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> {
4989 DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {}
4990};
4991
4992struct MDAPSIntField : public MDFieldImpl<APSInt> {
4993 MDAPSIntField() : ImplTy(APSInt()) {}
4994};
4995
4996struct MDSignedField : public MDFieldImpl<int64_t> {
4997 int64_t Min = INT64_MIN;
4998 int64_t Max = INT64_MAX;
4999
5000 MDSignedField(int64_t Default = 0)
5001 : ImplTy(Default) {}
5002 MDSignedField(int64_t Default, int64_t Min, int64_t Max)
5003 : ImplTy(Default), Min(Min), Max(Max) {}
5004};
5005
5006struct MDBoolField : public MDFieldImpl<bool> {
5007 MDBoolField(bool Default = false) : ImplTy(Default) {}
5008};
5009
5010struct MDField : public MDFieldImpl<Metadata *> {
5011 bool AllowNull;
5012
5013 MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {}
5014};
5015
5016struct MDStringField : public MDFieldImpl<MDString *> {
5017 enum class EmptyIs {
5018 Null, //< Allow empty input string, map to nullptr
5019 Empty, //< Allow empty input string, map to an empty MDString
5020 Error, //< Disallow empty string, map to an error
5021 } EmptyIs;
5022 MDStringField(enum EmptyIs EmptyIs = EmptyIs::Null)
5023 : ImplTy(nullptr), EmptyIs(EmptyIs) {}
5024};
5025
5026struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> {
5027 MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {}
5028};
5029
5030struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> {
5031 ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {}
5032};
5033
5034struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> {
5035 MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true)
5036 : ImplTy(MDSignedField(Default), MDField(AllowNull)) {}
5037
5038 MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max,
5039 bool AllowNull = true)
5040 : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {}
5041
5042 bool isMDSignedField() const { return WhatIs == IsTypeA; }
5043 bool isMDField() const { return WhatIs == IsTypeB; }
5044 int64_t getMDSignedValue() const {
5045 assert(isMDSignedField() && "Wrong field type");
5046 return A.Val;
5047 }
5048 Metadata *getMDFieldValue() const {
5049 assert(isMDField() && "Wrong field type");
5050 return B.Val;
5051 }
5052};
5053
5054struct MDUnsignedOrMDField : MDEitherFieldImpl<MDUnsignedField, MDField> {
5055 MDUnsignedOrMDField(uint64_t Default = 0, bool AllowNull = true)
5056 : ImplTy(MDUnsignedField(Default), MDField(AllowNull)) {}
5057
5058 MDUnsignedOrMDField(uint64_t Default, uint64_t Max, bool AllowNull = true)
5059 : ImplTy(MDUnsignedField(Default, Max), MDField(AllowNull)) {}
5060
5061 bool isMDUnsignedField() const { return WhatIs == IsTypeA; }
5062 bool isMDField() const { return WhatIs == IsTypeB; }
5063 uint64_t getMDUnsignedValue() const {
5064 assert(isMDUnsignedField() && "Wrong field type");
5065 return A.Val;
5066 }
5067 Metadata *getMDFieldValue() const {
5068 assert(isMDField() && "Wrong field type");
5069 return B.Val;
5070 }
5071
5072 Metadata *getValueAsMetadata(LLVMContext &Context) const {
5073 if (isMDUnsignedField())
5074 return ConstantAsMetadata::get(
5075 C: ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: getMDUnsignedValue()));
5076 if (isMDField())
5077 return getMDFieldValue();
5078 return nullptr;
5079 }
5080};
5081
5082} // end anonymous namespace
5083
5084namespace llvm {
5085
5086template <>
5087bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) {
5088 if (Lex.getKind() != lltok::APSInt)
5089 return tokError(Msg: "expected integer");
5090
5091 Result.assign(Val: Lex.getAPSIntVal());
5092 Lex.Lex();
5093 return false;
5094}
5095
5096template <>
5097bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5098 MDUnsignedField &Result) {
5099 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
5100 return tokError(Msg: "expected unsigned integer");
5101
5102 auto &U = Lex.getAPSIntVal();
5103 if (U.ugt(RHS: Result.Max))
5104 return tokError(Msg: "value for '" + Name + "' too large, limit is " +
5105 Twine(Result.Max));
5106 Result.assign(Val: U.getZExtValue());
5107 assert(Result.Val <= Result.Max && "Expected value in range");
5108 Lex.Lex();
5109 return false;
5110}
5111
5112template <>
5113bool LLParser::parseMDField(LocTy Loc, StringRef Name, LineField &Result) {
5114 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5115}
5116template <>
5117bool LLParser::parseMDField(LocTy Loc, StringRef Name, ColumnField &Result) {
5118 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5119}
5120
5121template <>
5122bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) {
5123 if (Lex.getKind() == lltok::APSInt)
5124 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5125
5126 if (Lex.getKind() != lltok::DwarfTag)
5127 return tokError(Msg: "expected DWARF tag");
5128
5129 unsigned Tag = dwarf::getTag(TagString: Lex.getStrVal());
5130 if (Tag == dwarf::DW_TAG_invalid)
5131 return tokError(Msg: "invalid DWARF tag" + Twine(" '") + Lex.getStrVal() + "'");
5132 assert(Tag <= Result.Max && "Expected valid DWARF tag");
5133
5134 Result.assign(Val: Tag);
5135 Lex.Lex();
5136 return false;
5137}
5138
5139template <>
5140bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5141 DwarfMacinfoTypeField &Result) {
5142 if (Lex.getKind() == lltok::APSInt)
5143 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5144
5145 if (Lex.getKind() != lltok::DwarfMacinfo)
5146 return tokError(Msg: "expected DWARF macinfo type");
5147
5148 unsigned Macinfo = dwarf::getMacinfo(MacinfoString: Lex.getStrVal());
5149 if (Macinfo == dwarf::DW_MACINFO_invalid)
5150 return tokError(Msg: "invalid DWARF macinfo type" + Twine(" '") +
5151 Lex.getStrVal() + "'");
5152 assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type");
5153
5154 Result.assign(Val: Macinfo);
5155 Lex.Lex();
5156 return false;
5157}
5158
5159template <>
5160bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5161 DwarfVirtualityField &Result) {
5162 if (Lex.getKind() == lltok::APSInt)
5163 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5164
5165 if (Lex.getKind() != lltok::DwarfVirtuality)
5166 return tokError(Msg: "expected DWARF virtuality code");
5167
5168 unsigned Virtuality = dwarf::getVirtuality(VirtualityString: Lex.getStrVal());
5169 if (Virtuality == dwarf::DW_VIRTUALITY_invalid)
5170 return tokError(Msg: "invalid DWARF virtuality code" + Twine(" '") +
5171 Lex.getStrVal() + "'");
5172 assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code");
5173 Result.assign(Val: Virtuality);
5174 Lex.Lex();
5175 return false;
5176}
5177
5178template <>
5179bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5180 DwarfEnumKindField &Result) {
5181 if (Lex.getKind() == lltok::APSInt)
5182 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5183
5184 if (Lex.getKind() != lltok::DwarfEnumKind)
5185 return tokError(Msg: "expected DWARF enum kind code");
5186
5187 unsigned EnumKind = dwarf::getEnumKind(EnumKindString: Lex.getStrVal());
5188 if (EnumKind == dwarf::DW_APPLE_ENUM_KIND_invalid)
5189 return tokError(Msg: "invalid DWARF enum kind code" + Twine(" '") +
5190 Lex.getStrVal() + "'");
5191 assert(EnumKind <= Result.Max && "Expected valid DWARF enum kind code");
5192 Result.assign(Val: EnumKind);
5193 Lex.Lex();
5194 return false;
5195}
5196
5197template <>
5198bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) {
5199 if (Lex.getKind() == lltok::APSInt)
5200 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5201
5202 if (Lex.getKind() != lltok::DwarfLang)
5203 return tokError(Msg: "expected DWARF language");
5204
5205 unsigned Lang = dwarf::getLanguage(LanguageString: Lex.getStrVal());
5206 if (!Lang)
5207 return tokError(Msg: "invalid DWARF language" + Twine(" '") + Lex.getStrVal() +
5208 "'");
5209 assert(Lang <= Result.Max && "Expected valid DWARF language");
5210 Result.assign(Val: Lang);
5211 Lex.Lex();
5212 return false;
5213}
5214
5215template <>
5216bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5217 DwarfSourceLangNameField &Result) {
5218 if (Lex.getKind() == lltok::APSInt)
5219 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5220
5221 if (Lex.getKind() != lltok::DwarfSourceLangName)
5222 return tokError(Msg: "expected DWARF source language name");
5223
5224 unsigned Lang = dwarf::getSourceLanguageName(SourceLanguageNameString: Lex.getStrVal());
5225 if (!Lang)
5226 return tokError(Msg: "invalid DWARF source language name" + Twine(" '") +
5227 Lex.getStrVal() + "'");
5228 assert(Lang <= Result.Max && "Expected valid DWARF source language name");
5229 Result.assign(Val: Lang);
5230 Lex.Lex();
5231 return false;
5232}
5233
5234template <>
5235bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) {
5236 if (Lex.getKind() == lltok::APSInt)
5237 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5238
5239 if (Lex.getKind() != lltok::DwarfCC)
5240 return tokError(Msg: "expected DWARF calling convention");
5241
5242 unsigned CC = dwarf::getCallingConvention(LanguageString: Lex.getStrVal());
5243 if (!CC)
5244 return tokError(Msg: "invalid DWARF calling convention" + Twine(" '") +
5245 Lex.getStrVal() + "'");
5246 assert(CC <= Result.Max && "Expected valid DWARF calling convention");
5247 Result.assign(Val: CC);
5248 Lex.Lex();
5249 return false;
5250}
5251
5252template <>
5253bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5254 EmissionKindField &Result) {
5255 if (Lex.getKind() == lltok::APSInt)
5256 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5257
5258 if (Lex.getKind() != lltok::EmissionKind)
5259 return tokError(Msg: "expected emission kind");
5260
5261 auto Kind = DICompileUnit::getEmissionKind(Str: Lex.getStrVal());
5262 if (!Kind)
5263 return tokError(Msg: "invalid emission kind" + Twine(" '") + Lex.getStrVal() +
5264 "'");
5265 assert(*Kind <= Result.Max && "Expected valid emission kind");
5266 Result.assign(Val: *Kind);
5267 Lex.Lex();
5268 return false;
5269}
5270
5271template <>
5272bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5273 FixedPointKindField &Result) {
5274 if (Lex.getKind() == lltok::APSInt)
5275 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5276
5277 if (Lex.getKind() != lltok::FixedPointKind)
5278 return tokError(Msg: "expected fixed-point kind");
5279
5280 auto Kind = DIFixedPointType::getFixedPointKind(Str: Lex.getStrVal());
5281 if (!Kind)
5282 return tokError(Msg: "invalid fixed-point kind" + Twine(" '") + Lex.getStrVal() +
5283 "'");
5284 assert(*Kind <= Result.Max && "Expected valid fixed-point kind");
5285 Result.assign(Val: *Kind);
5286 Lex.Lex();
5287 return false;
5288}
5289
5290template <>
5291bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5292 NameTableKindField &Result) {
5293 if (Lex.getKind() == lltok::APSInt)
5294 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5295
5296 if (Lex.getKind() != lltok::NameTableKind)
5297 return tokError(Msg: "expected nameTable kind");
5298
5299 auto Kind = DICompileUnit::getNameTableKind(Str: Lex.getStrVal());
5300 if (!Kind)
5301 return tokError(Msg: "invalid nameTable kind" + Twine(" '") + Lex.getStrVal() +
5302 "'");
5303 assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind");
5304 Result.assign(Val: (unsigned)*Kind);
5305 Lex.Lex();
5306 return false;
5307}
5308
5309template <>
5310bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5311 DwarfAttEncodingField &Result) {
5312 if (Lex.getKind() == lltok::APSInt)
5313 return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result));
5314
5315 if (Lex.getKind() != lltok::DwarfAttEncoding)
5316 return tokError(Msg: "expected DWARF type attribute encoding");
5317
5318 unsigned Encoding = dwarf::getAttributeEncoding(EncodingString: Lex.getStrVal());
5319 if (!Encoding)
5320 return tokError(Msg: "invalid DWARF type attribute encoding" + Twine(" '") +
5321 Lex.getStrVal() + "'");
5322 assert(Encoding <= Result.Max && "Expected valid DWARF language");
5323 Result.assign(Val: Encoding);
5324 Lex.Lex();
5325 return false;
5326}
5327
5328/// DIFlagField
5329/// ::= uint32
5330/// ::= DIFlagVector
5331/// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic
5332template <>
5333bool LLParser::parseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) {
5334
5335 // parser for a single flag.
5336 auto parseFlag = [&](DINode::DIFlags &Val) {
5337 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
5338 uint32_t TempVal = static_cast<uint32_t>(Val);
5339 bool Res = parseUInt32(Val&: TempVal);
5340 Val = static_cast<DINode::DIFlags>(TempVal);
5341 return Res;
5342 }
5343
5344 if (Lex.getKind() != lltok::DIFlag)
5345 return tokError(Msg: "expected debug info flag");
5346
5347 Val = DINode::getFlag(Flag: Lex.getStrVal());
5348 if (!Val)
5349 return tokError(Msg: Twine("invalid debug info flag '") + Lex.getStrVal() +
5350 "'");
5351 Lex.Lex();
5352 return false;
5353 };
5354
5355 // parse the flags and combine them together.
5356 DINode::DIFlags Combined = DINode::FlagZero;
5357 do {
5358 DINode::DIFlags Val;
5359 if (parseFlag(Val))
5360 return true;
5361 Combined |= Val;
5362 } while (EatIfPresent(T: lltok::bar));
5363
5364 Result.assign(Val: Combined);
5365 return false;
5366}
5367
5368/// DISPFlagField
5369/// ::= uint32
5370/// ::= DISPFlagVector
5371/// ::= DISPFlagVector '|' DISPFlag* '|' uint32
5372template <>
5373bool LLParser::parseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) {
5374
5375 // parser for a single flag.
5376 auto parseFlag = [&](DISubprogram::DISPFlags &Val) {
5377 if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) {
5378 uint32_t TempVal = static_cast<uint32_t>(Val);
5379 bool Res = parseUInt32(Val&: TempVal);
5380 Val = static_cast<DISubprogram::DISPFlags>(TempVal);
5381 return Res;
5382 }
5383
5384 if (Lex.getKind() != lltok::DISPFlag)
5385 return tokError(Msg: "expected debug info flag");
5386
5387 Val = DISubprogram::getFlag(Flag: Lex.getStrVal());
5388 if (!Val)
5389 return tokError(Msg: Twine("invalid subprogram debug info flag '") +
5390 Lex.getStrVal() + "'");
5391 Lex.Lex();
5392 return false;
5393 };
5394
5395 // parse the flags and combine them together.
5396 DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero;
5397 do {
5398 DISubprogram::DISPFlags Val;
5399 if (parseFlag(Val))
5400 return true;
5401 Combined |= Val;
5402 } while (EatIfPresent(T: lltok::bar));
5403
5404 Result.assign(Val: Combined);
5405 return false;
5406}
5407
5408template <>
5409bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDSignedField &Result) {
5410 if (Lex.getKind() != lltok::APSInt)
5411 return tokError(Msg: "expected signed integer");
5412
5413 auto &S = Lex.getAPSIntVal();
5414 if (S < Result.Min)
5415 return tokError(Msg: "value for '" + Name + "' too small, limit is " +
5416 Twine(Result.Min));
5417 if (S > Result.Max)
5418 return tokError(Msg: "value for '" + Name + "' too large, limit is " +
5419 Twine(Result.Max));
5420 Result.assign(Val: S.getExtValue());
5421 assert(Result.Val >= Result.Min && "Expected value in range");
5422 assert(Result.Val <= Result.Max && "Expected value in range");
5423 Lex.Lex();
5424 return false;
5425}
5426
5427template <>
5428bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) {
5429 switch (Lex.getKind()) {
5430 default:
5431 return tokError(Msg: "expected 'true' or 'false'");
5432 case lltok::kw_true:
5433 Result.assign(Val: true);
5434 break;
5435 case lltok::kw_false:
5436 Result.assign(Val: false);
5437 break;
5438 }
5439 Lex.Lex();
5440 return false;
5441}
5442
5443template <>
5444bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDField &Result) {
5445 if (Lex.getKind() == lltok::kw_null) {
5446 if (!Result.AllowNull)
5447 return tokError(Msg: "'" + Name + "' cannot be null");
5448 Lex.Lex();
5449 Result.assign(Val: nullptr);
5450 return false;
5451 }
5452
5453 Metadata *MD;
5454 if (parseMetadata(MD, PFS: nullptr))
5455 return true;
5456
5457 Result.assign(Val: MD);
5458 return false;
5459}
5460
5461template <>
5462bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5463 MDSignedOrMDField &Result) {
5464 // Try to parse a signed int.
5465 if (Lex.getKind() == lltok::APSInt) {
5466 MDSignedField Res = Result.A;
5467 if (!parseMDField(Loc, Name, Result&: Res)) {
5468 Result.assign(A: Res);
5469 return false;
5470 }
5471 return true;
5472 }
5473
5474 // Otherwise, try to parse as an MDField.
5475 MDField Res = Result.B;
5476 if (!parseMDField(Loc, Name, Result&: Res)) {
5477 Result.assign(B: Res);
5478 return false;
5479 }
5480
5481 return true;
5482}
5483
5484template <>
5485bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5486 MDUnsignedOrMDField &Result) {
5487 // Try to parse an unsigned int.
5488 if (Lex.getKind() == lltok::APSInt) {
5489 MDUnsignedField Res = Result.A;
5490 if (!parseMDField(Loc, Name, Result&: Res)) {
5491 Result.assign(A: Res);
5492 return false;
5493 }
5494 return true;
5495 }
5496
5497 // Otherwise, try to parse as an MDField.
5498 MDField Res = Result.B;
5499 if (!parseMDField(Loc, Name, Result&: Res)) {
5500 Result.assign(B: Res);
5501 return false;
5502 }
5503
5504 return true;
5505}
5506
5507template <>
5508bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDStringField &Result) {
5509 LocTy ValueLoc = Lex.getLoc();
5510 std::string S;
5511 if (parseStringConstant(Result&: S))
5512 return true;
5513
5514 if (S.empty()) {
5515 switch (Result.EmptyIs) {
5516 case MDStringField::EmptyIs::Null:
5517 Result.assign(Val: nullptr);
5518 return false;
5519 case MDStringField::EmptyIs::Empty:
5520 break;
5521 case MDStringField::EmptyIs::Error:
5522 return error(L: ValueLoc, Msg: "'" + Name + "' cannot be empty");
5523 }
5524 }
5525
5526 Result.assign(Val: MDString::get(Context, Str: S));
5527 return false;
5528}
5529
5530template <>
5531bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) {
5532 SmallVector<Metadata *, 4> MDs;
5533 if (parseMDNodeVector(Elts&: MDs))
5534 return true;
5535
5536 Result.assign(Val: std::move(MDs));
5537 return false;
5538}
5539
5540template <>
5541bool LLParser::parseMDField(LocTy Loc, StringRef Name,
5542 ChecksumKindField &Result) {
5543 std::optional<DIFile::ChecksumKind> CSKind =
5544 DIFile::getChecksumKind(CSKindStr: Lex.getStrVal());
5545
5546 if (Lex.getKind() != lltok::ChecksumKind || !CSKind)
5547 return tokError(Msg: "invalid checksum kind" + Twine(" '") + Lex.getStrVal() +
5548 "'");
5549
5550 Result.assign(Val: *CSKind);
5551 Lex.Lex();
5552 return false;
5553}
5554
5555} // end namespace llvm
5556
5557template <class ParserTy>
5558bool LLParser::parseMDFieldsImplBody(ParserTy ParseField) {
5559 do {
5560 if (Lex.getKind() != lltok::LabelStr)
5561 return tokError(Msg: "expected field label here");
5562
5563 if (ParseField())
5564 return true;
5565 } while (EatIfPresent(T: lltok::comma));
5566
5567 return false;
5568}
5569
5570template <class ParserTy>
5571bool LLParser::parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc) {
5572 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5573 Lex.Lex();
5574
5575 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
5576 return true;
5577 if (Lex.getKind() != lltok::rparen)
5578 if (parseMDFieldsImplBody(ParseField))
5579 return true;
5580
5581 ClosingLoc = Lex.getLoc();
5582 return parseToken(T: lltok::rparen, ErrMsg: "expected ')' here");
5583}
5584
5585template <class FieldTy>
5586bool LLParser::parseMDField(StringRef Name, FieldTy &Result) {
5587 if (Result.Seen)
5588 return tokError(Msg: "field '" + Name + "' cannot be specified more than once");
5589
5590 LocTy Loc = Lex.getLoc();
5591 Lex.Lex();
5592 return parseMDField(Loc, Name, Result);
5593}
5594
5595bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) {
5596 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
5597
5598#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
5599 if (Lex.getStrVal() == #CLASS) \
5600 return parse##CLASS(N, IsDistinct);
5601#include "llvm/IR/Metadata.def"
5602
5603 return tokError(Msg: "expected metadata type");
5604}
5605
5606#define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT
5607#define NOP_FIELD(NAME, TYPE, INIT)
5608#define REQUIRE_FIELD(NAME, TYPE, INIT) \
5609 if (!NAME.Seen) \
5610 return error(ClosingLoc, "missing required field '" #NAME "'");
5611#define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \
5612 if (Lex.getStrVal() == #NAME) \
5613 return parseMDField(#NAME, NAME);
5614#define PARSE_MD_FIELDS() \
5615 VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \
5616 do { \
5617 LocTy ClosingLoc; \
5618 if (parseMDFieldsImpl( \
5619 [&]() -> bool { \
5620 VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \
5621 return tokError(Twine("invalid field '") + Lex.getStrVal() + \
5622 "'"); \
5623 }, \
5624 ClosingLoc)) \
5625 return true; \
5626 VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \
5627 } while (false)
5628#define GET_OR_DISTINCT(CLASS, ARGS) \
5629 (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS)
5630
5631/// parseDILocationFields:
5632/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
5633/// isImplicitCode: true, atomGroup: 1, atomRank: 1)
5634bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) {
5635#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5636 OPTIONAL(line, LineField, ); \
5637 OPTIONAL(column, ColumnField, ); \
5638 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
5639 OPTIONAL(inlinedAt, MDField, ); \
5640 OPTIONAL(isImplicitCode, MDBoolField, (false)); \
5641 OPTIONAL(atomGroup, MDUnsignedField, (0, UINT64_MAX)); \
5642 OPTIONAL(atomRank, MDUnsignedField, (0, UINT8_MAX));
5643 PARSE_MD_FIELDS();
5644#undef VISIT_MD_FIELDS
5645
5646 Result = GET_OR_DISTINCT(
5647 DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val,
5648 isImplicitCode.Val, atomGroup.Val, atomRank.Val));
5649 return false;
5650}
5651
5652/// parseDIAssignID:
5653/// ::= distinct !DIAssignID()
5654bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) {
5655 if (!IsDistinct)
5656 return tokError(Msg: "missing 'distinct', required for !DIAssignID()");
5657
5658 Lex.Lex();
5659
5660 // Now eat the parens.
5661 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
5662 return true;
5663 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
5664 return true;
5665
5666 Result = DIAssignID::getDistinct(Context);
5667 return false;
5668}
5669
5670/// parseGenericDINode:
5671/// ::= !GenericDINode(tag: 15, header: "...", operands: {...})
5672bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) {
5673#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5674 REQUIRED(tag, DwarfTagField, ); \
5675 OPTIONAL(header, MDStringField, ); \
5676 OPTIONAL(operands, MDFieldList, );
5677 PARSE_MD_FIELDS();
5678#undef VISIT_MD_FIELDS
5679
5680 Result = GET_OR_DISTINCT(GenericDINode,
5681 (Context, tag.Val, header.Val, operands.Val));
5682 return false;
5683}
5684
5685/// parseDISubrangeType:
5686/// ::= !DISubrangeType(name: "whatever", file: !0,
5687/// line: 7, scope: !1, baseType: !2, size: 32,
5688/// align: 32, flags: 0, lowerBound: !3
5689/// upperBound: !4, stride: !5, bias: !6)
5690bool LLParser::parseDISubrangeType(MDNode *&Result, bool IsDistinct) {
5691#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5692 OPTIONAL(name, MDStringField, ); \
5693 OPTIONAL(file, MDField, ); \
5694 OPTIONAL(line, LineField, ); \
5695 OPTIONAL(scope, MDField, ); \
5696 OPTIONAL(baseType, MDField, ); \
5697 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5698 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5699 OPTIONAL(flags, DIFlagField, ); \
5700 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5701 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5702 OPTIONAL(stride, MDSignedOrMDField, ); \
5703 OPTIONAL(bias, MDSignedOrMDField, );
5704 PARSE_MD_FIELDS();
5705#undef VISIT_MD_FIELDS
5706
5707 auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * {
5708 if (Bound.isMDSignedField())
5709 return ConstantAsMetadata::get(C: ConstantInt::getSigned(
5710 Ty: Type::getInt64Ty(C&: Context), V: Bound.getMDSignedValue()));
5711 if (Bound.isMDField())
5712 return Bound.getMDFieldValue();
5713 return nullptr;
5714 };
5715
5716 Metadata *LowerBound = convToMetadata(lowerBound);
5717 Metadata *UpperBound = convToMetadata(upperBound);
5718 Metadata *Stride = convToMetadata(stride);
5719 Metadata *Bias = convToMetadata(bias);
5720
5721 Result = GET_OR_DISTINCT(
5722 DISubrangeType, (Context, name.Val, file.Val, line.Val, scope.Val,
5723 size.getValueAsMetadata(Context), align.Val, flags.Val,
5724 baseType.Val, LowerBound, UpperBound, Stride, Bias));
5725
5726 return false;
5727}
5728
5729/// parseDISubrange:
5730/// ::= !DISubrange(count: 30, lowerBound: 2)
5731/// ::= !DISubrange(count: !node, lowerBound: 2)
5732/// ::= !DISubrange(lowerBound: !node1, upperBound: !node2, stride: !node3)
5733bool LLParser::parseDISubrange(MDNode *&Result, bool IsDistinct) {
5734#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5735 OPTIONAL(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \
5736 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5737 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5738 OPTIONAL(stride, MDSignedOrMDField, );
5739 PARSE_MD_FIELDS();
5740#undef VISIT_MD_FIELDS
5741
5742 Metadata *Count = nullptr;
5743 Metadata *LowerBound = nullptr;
5744 Metadata *UpperBound = nullptr;
5745 Metadata *Stride = nullptr;
5746
5747 auto convToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * {
5748 if (Bound.isMDSignedField())
5749 return ConstantAsMetadata::get(C: ConstantInt::getSigned(
5750 Ty: Type::getInt64Ty(C&: Context), V: Bound.getMDSignedValue()));
5751 if (Bound.isMDField())
5752 return Bound.getMDFieldValue();
5753 return nullptr;
5754 };
5755
5756 Count = convToMetadata(count);
5757 LowerBound = convToMetadata(lowerBound);
5758 UpperBound = convToMetadata(upperBound);
5759 Stride = convToMetadata(stride);
5760
5761 Result = GET_OR_DISTINCT(DISubrange,
5762 (Context, Count, LowerBound, UpperBound, Stride));
5763
5764 return false;
5765}
5766
5767/// parseDIGenericSubrange:
5768/// ::= !DIGenericSubrange(lowerBound: !node1, upperBound: !node2, stride:
5769/// !node3)
5770bool LLParser::parseDIGenericSubrange(MDNode *&Result, bool IsDistinct) {
5771#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5772 OPTIONAL(count, MDSignedOrMDField, ); \
5773 OPTIONAL(lowerBound, MDSignedOrMDField, ); \
5774 OPTIONAL(upperBound, MDSignedOrMDField, ); \
5775 OPTIONAL(stride, MDSignedOrMDField, );
5776 PARSE_MD_FIELDS();
5777#undef VISIT_MD_FIELDS
5778
5779 auto ConvToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * {
5780 if (Bound.isMDSignedField())
5781 return DIExpression::get(
5782 Context, Elements: {dwarf::DW_OP_consts,
5783 static_cast<uint64_t>(Bound.getMDSignedValue())});
5784 if (Bound.isMDField())
5785 return Bound.getMDFieldValue();
5786 return nullptr;
5787 };
5788
5789 Metadata *Count = ConvToMetadata(count);
5790 Metadata *LowerBound = ConvToMetadata(lowerBound);
5791 Metadata *UpperBound = ConvToMetadata(upperBound);
5792 Metadata *Stride = ConvToMetadata(stride);
5793
5794 Result = GET_OR_DISTINCT(DIGenericSubrange,
5795 (Context, Count, LowerBound, UpperBound, Stride));
5796
5797 return false;
5798}
5799
5800/// parseDIEnumerator:
5801/// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind")
5802bool LLParser::parseDIEnumerator(MDNode *&Result, bool IsDistinct) {
5803#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5804 REQUIRED(name, MDStringField, ); \
5805 REQUIRED(value, MDAPSIntField, ); \
5806 OPTIONAL(isUnsigned, MDBoolField, (false));
5807 PARSE_MD_FIELDS();
5808#undef VISIT_MD_FIELDS
5809
5810 if (isUnsigned.Val && value.Val.isNegative())
5811 return tokError(Msg: "unsigned enumerator with negative value");
5812
5813 APSInt Value(value.Val);
5814 // Add a leading zero so that unsigned values with the msb set are not
5815 // mistaken for negative values when used for signed enumerators.
5816 if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet())
5817 Value = Value.zext(width: Value.getBitWidth() + 1);
5818
5819 Result =
5820 GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val));
5821
5822 return false;
5823}
5824
5825/// parseDIBasicType:
5826/// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32,
5827/// encoding: DW_ATE_encoding, flags: 0)
5828bool LLParser::parseDIBasicType(MDNode *&Result, bool IsDistinct) {
5829#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5830 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
5831 OPTIONAL(name, MDStringField, ); \
5832 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5833 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5834 OPTIONAL(dataSize, MDUnsignedField, (0, UINT32_MAX)); \
5835 OPTIONAL(encoding, DwarfAttEncodingField, ); \
5836 OPTIONAL(num_extra_inhabitants, MDUnsignedField, (0, UINT32_MAX)); \
5837 OPTIONAL(flags, DIFlagField, );
5838 PARSE_MD_FIELDS();
5839#undef VISIT_MD_FIELDS
5840
5841 Result = GET_OR_DISTINCT(
5842 DIBasicType,
5843 (Context, tag.Val, name.Val, size.getValueAsMetadata(Context), align.Val,
5844 encoding.Val, num_extra_inhabitants.Val, dataSize.Val, flags.Val));
5845 return false;
5846}
5847
5848/// parseDIFixedPointType:
5849/// ::= !DIFixedPointType(tag: DW_TAG_base_type, name: "xyz", size: 32,
5850/// align: 32, encoding: DW_ATE_signed_fixed,
5851/// flags: 0, kind: Rational, factor: 3, numerator: 1,
5852/// denominator: 8)
5853bool LLParser::parseDIFixedPointType(MDNode *&Result, bool IsDistinct) {
5854#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5855 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \
5856 OPTIONAL(name, MDStringField, ); \
5857 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5858 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5859 OPTIONAL(encoding, DwarfAttEncodingField, ); \
5860 OPTIONAL(flags, DIFlagField, ); \
5861 OPTIONAL(kind, FixedPointKindField, ); \
5862 OPTIONAL(factor, MDSignedField, ); \
5863 OPTIONAL(numerator, MDAPSIntField, ); \
5864 OPTIONAL(denominator, MDAPSIntField, );
5865 PARSE_MD_FIELDS();
5866#undef VISIT_MD_FIELDS
5867
5868 Result = GET_OR_DISTINCT(DIFixedPointType,
5869 (Context, tag.Val, name.Val,
5870 size.getValueAsMetadata(Context), align.Val,
5871 encoding.Val, flags.Val, kind.Val, factor.Val,
5872 numerator.Val, denominator.Val));
5873 return false;
5874}
5875
5876/// parseDIStringType:
5877/// ::= !DIStringType(name: "character(4)", size: 32, align: 32)
5878bool LLParser::parseDIStringType(MDNode *&Result, bool IsDistinct) {
5879#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5880 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_string_type)); \
5881 OPTIONAL(name, MDStringField, ); \
5882 OPTIONAL(stringLength, MDField, ); \
5883 OPTIONAL(stringLengthExpression, MDField, ); \
5884 OPTIONAL(stringLocationExpression, MDField, ); \
5885 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5886 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5887 OPTIONAL(encoding, DwarfAttEncodingField, );
5888 PARSE_MD_FIELDS();
5889#undef VISIT_MD_FIELDS
5890
5891 Result = GET_OR_DISTINCT(
5892 DIStringType,
5893 (Context, tag.Val, name.Val, stringLength.Val, stringLengthExpression.Val,
5894 stringLocationExpression.Val, size.getValueAsMetadata(Context),
5895 align.Val, encoding.Val));
5896 return false;
5897}
5898
5899/// parseDIDerivedType:
5900/// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0,
5901/// line: 7, scope: !1, baseType: !2, size: 32,
5902/// align: 32, offset: 0, flags: 0, extraData: !3,
5903/// dwarfAddressSpace: 3, ptrAuthKey: 1,
5904/// ptrAuthIsAddressDiscriminated: true,
5905/// ptrAuthExtraDiscriminator: 0x1234,
5906/// ptrAuthIsaPointer: 1, ptrAuthAuthenticatesNullValues:1
5907/// )
5908bool LLParser::parseDIDerivedType(MDNode *&Result, bool IsDistinct) {
5909#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5910 REQUIRED(tag, DwarfTagField, ); \
5911 OPTIONAL(name, MDStringField, ); \
5912 OPTIONAL(file, MDField, ); \
5913 OPTIONAL(line, LineField, ); \
5914 OPTIONAL(scope, MDField, ); \
5915 REQUIRED(baseType, MDField, ); \
5916 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5917 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5918 OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5919 OPTIONAL(flags, DIFlagField, ); \
5920 OPTIONAL(extraData, MDField, ); \
5921 OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX)); \
5922 OPTIONAL(annotations, MDField, ); \
5923 OPTIONAL(ptrAuthKey, MDUnsignedField, (0, 7)); \
5924 OPTIONAL(ptrAuthIsAddressDiscriminated, MDBoolField, ); \
5925 OPTIONAL(ptrAuthExtraDiscriminator, MDUnsignedField, (0, 0xffff)); \
5926 OPTIONAL(ptrAuthIsaPointer, MDBoolField, ); \
5927 OPTIONAL(ptrAuthAuthenticatesNullValues, MDBoolField, );
5928 PARSE_MD_FIELDS();
5929#undef VISIT_MD_FIELDS
5930
5931 std::optional<unsigned> DWARFAddressSpace;
5932 if (dwarfAddressSpace.Val != UINT32_MAX)
5933 DWARFAddressSpace = dwarfAddressSpace.Val;
5934 std::optional<DIDerivedType::PtrAuthData> PtrAuthData;
5935 if (ptrAuthKey.Val)
5936 PtrAuthData.emplace(
5937 args: (unsigned)ptrAuthKey.Val, args&: ptrAuthIsAddressDiscriminated.Val,
5938 args: (unsigned)ptrAuthExtraDiscriminator.Val, args&: ptrAuthIsaPointer.Val,
5939 args&: ptrAuthAuthenticatesNullValues.Val);
5940
5941 Result = GET_OR_DISTINCT(
5942 DIDerivedType, (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val,
5943 baseType.Val, size.getValueAsMetadata(Context), align.Val,
5944 offset.getValueAsMetadata(Context), DWARFAddressSpace,
5945 PtrAuthData, flags.Val, extraData.Val, annotations.Val));
5946 return false;
5947}
5948
5949bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) {
5950#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
5951 REQUIRED(tag, DwarfTagField, ); \
5952 OPTIONAL(name, MDStringField, ); \
5953 OPTIONAL(file, MDField, ); \
5954 OPTIONAL(line, LineField, ); \
5955 OPTIONAL(scope, MDField, ); \
5956 OPTIONAL(baseType, MDField, ); \
5957 OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5958 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
5959 OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \
5960 OPTIONAL(flags, DIFlagField, ); \
5961 OPTIONAL(elements, MDField, ); \
5962 OPTIONAL(runtimeLang, DwarfLangField, ); \
5963 OPTIONAL(enumKind, DwarfEnumKindField, ); \
5964 OPTIONAL(vtableHolder, MDField, ); \
5965 OPTIONAL(templateParams, MDField, ); \
5966 OPTIONAL(identifier, MDStringField, ); \
5967 OPTIONAL(discriminator, MDField, ); \
5968 OPTIONAL(dataLocation, MDField, ); \
5969 OPTIONAL(associated, MDField, ); \
5970 OPTIONAL(allocated, MDField, ); \
5971 OPTIONAL(rank, MDSignedOrMDField, ); \
5972 OPTIONAL(annotations, MDField, ); \
5973 OPTIONAL(num_extra_inhabitants, MDUnsignedField, (0, UINT32_MAX)); \
5974 OPTIONAL(specification, MDField, ); \
5975 OPTIONAL(bitStride, MDField, );
5976 PARSE_MD_FIELDS();
5977#undef VISIT_MD_FIELDS
5978
5979 Metadata *Rank = nullptr;
5980 if (rank.isMDSignedField())
5981 Rank = ConstantAsMetadata::get(C: ConstantInt::getSigned(
5982 Ty: Type::getInt64Ty(C&: Context), V: rank.getMDSignedValue()));
5983 else if (rank.isMDField())
5984 Rank = rank.getMDFieldValue();
5985
5986 std::optional<unsigned> EnumKind;
5987 if (enumKind.Val != dwarf::DW_APPLE_ENUM_KIND_invalid)
5988 EnumKind = enumKind.Val;
5989
5990 // If this has an identifier try to build an ODR type.
5991 if (identifier.Val)
5992 if (auto *CT = DICompositeType::buildODRType(
5993 Context, Identifier&: *identifier.Val, Tag: tag.Val, Name: name.Val, File: file.Val, Line: line.Val,
5994 Scope: scope.Val, BaseType: baseType.Val, SizeInBits: size.getValueAsMetadata(Context),
5995 AlignInBits: align.Val, OffsetInBits: offset.getValueAsMetadata(Context), Specification: specification.Val,
5996 NumExtraInhabitants: num_extra_inhabitants.Val, Flags: flags.Val, Elements: elements.Val, RuntimeLang: runtimeLang.Val,
5997 EnumKind, VTableHolder: vtableHolder.Val, TemplateParams: templateParams.Val, Discriminator: discriminator.Val,
5998 DataLocation: dataLocation.Val, Associated: associated.Val, Allocated: allocated.Val, Rank,
5999 Annotations: annotations.Val, BitStride: bitStride.Val)) {
6000 Result = CT;
6001 return false;
6002 }
6003
6004 // Create a new node, and save it in the context if it belongs in the type
6005 // map.
6006 Result = GET_OR_DISTINCT(
6007 DICompositeType,
6008 (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val,
6009 size.getValueAsMetadata(Context), align.Val,
6010 offset.getValueAsMetadata(Context), flags.Val, elements.Val,
6011 runtimeLang.Val, EnumKind, vtableHolder.Val, templateParams.Val,
6012 identifier.Val, discriminator.Val, dataLocation.Val, associated.Val,
6013 allocated.Val, Rank, annotations.Val, specification.Val,
6014 num_extra_inhabitants.Val, bitStride.Val));
6015 return false;
6016}
6017
6018bool LLParser::parseDISubroutineType(MDNode *&Result, bool IsDistinct) {
6019#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6020 OPTIONAL(flags, DIFlagField, ); \
6021 OPTIONAL(cc, DwarfCCField, ); \
6022 REQUIRED(types, MDField, );
6023 PARSE_MD_FIELDS();
6024#undef VISIT_MD_FIELDS
6025
6026 Result = GET_OR_DISTINCT(DISubroutineType,
6027 (Context, flags.Val, cc.Val, types.Val));
6028 return false;
6029}
6030
6031/// parseDIFileType:
6032/// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir",
6033/// checksumkind: CSK_MD5,
6034/// checksum: "000102030405060708090a0b0c0d0e0f",
6035/// source: "source file contents")
6036bool LLParser::parseDIFile(MDNode *&Result, bool IsDistinct) {
6037 // The default constructed value for checksumkind is required, but will never
6038 // be used, as the parser checks if the field was actually Seen before using
6039 // the Val.
6040#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6041 REQUIRED(filename, MDStringField, ); \
6042 REQUIRED(directory, MDStringField, ); \
6043 OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \
6044 OPTIONAL(checksum, MDStringField, ); \
6045 OPTIONAL(source, MDStringField, (MDStringField::EmptyIs::Empty));
6046 PARSE_MD_FIELDS();
6047#undef VISIT_MD_FIELDS
6048
6049 std::optional<DIFile::ChecksumInfo<MDString *>> OptChecksum;
6050 if (checksumkind.Seen && checksum.Seen)
6051 OptChecksum.emplace(args&: checksumkind.Val, args&: checksum.Val);
6052 else if (checksumkind.Seen || checksum.Seen)
6053 return tokError(Msg: "'checksumkind' and 'checksum' must be provided together");
6054
6055 MDString *Source = nullptr;
6056 if (source.Seen)
6057 Source = source.Val;
6058 Result = GET_OR_DISTINCT(
6059 DIFile, (Context, filename.Val, directory.Val, OptChecksum, Source));
6060 return false;
6061}
6062
6063/// parseDICompileUnit:
6064/// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang",
6065/// isOptimized: true, flags: "-O2", runtimeVersion: 1,
6066/// splitDebugFilename: "abc.debug",
6067/// emissionKind: FullDebug, enums: !1, retainedTypes: !2,
6068/// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd,
6069/// sysroot: "/", sdk: "MacOSX.sdk")
6070bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) {
6071 if (!IsDistinct)
6072 return tokError(Msg: "missing 'distinct', required for !DICompileUnit");
6073
6074 LocTy Loc = Lex.getLoc();
6075
6076#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6077 REQUIRED(file, MDField, (/* AllowNull */ false)); \
6078 OPTIONAL(language, DwarfLangField, ); \
6079 OPTIONAL(sourceLanguageName, DwarfSourceLangNameField, ); \
6080 OPTIONAL(sourceLanguageVersion, MDUnsignedField, (0, UINT32_MAX)); \
6081 OPTIONAL(producer, MDStringField, ); \
6082 OPTIONAL(isOptimized, MDBoolField, ); \
6083 OPTIONAL(flags, MDStringField, ); \
6084 OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \
6085 OPTIONAL(splitDebugFilename, MDStringField, ); \
6086 OPTIONAL(emissionKind, EmissionKindField, ); \
6087 OPTIONAL(enums, MDField, ); \
6088 OPTIONAL(retainedTypes, MDField, ); \
6089 OPTIONAL(globals, MDField, ); \
6090 OPTIONAL(imports, MDField, ); \
6091 OPTIONAL(macros, MDField, ); \
6092 OPTIONAL(dwoId, MDUnsignedField, ); \
6093 OPTIONAL(splitDebugInlining, MDBoolField, = true); \
6094 OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \
6095 OPTIONAL(nameTableKind, NameTableKindField, ); \
6096 OPTIONAL(rangesBaseAddress, MDBoolField, = false); \
6097 OPTIONAL(sysroot, MDStringField, ); \
6098 OPTIONAL(sdk, MDStringField, );
6099 PARSE_MD_FIELDS();
6100#undef VISIT_MD_FIELDS
6101
6102 if (!language.Seen && !sourceLanguageName.Seen)
6103 return error(L: Loc, Msg: "missing one of 'language' or 'sourceLanguageName', "
6104 "required for !DICompileUnit");
6105
6106 if (language.Seen && sourceLanguageName.Seen)
6107 return error(L: Loc, Msg: "can only specify one of 'language' and "
6108 "'sourceLanguageName' on !DICompileUnit");
6109
6110 if (sourceLanguageVersion.Seen && !sourceLanguageName.Seen)
6111 return error(L: Loc, Msg: "'sourceLanguageVersion' requires an associated "
6112 "'sourceLanguageName' on !DICompileUnit");
6113
6114 Result = DICompileUnit::getDistinct(
6115 Context,
6116 SourceLanguage: language.Seen ? DISourceLanguageName(language.Val)
6117 : DISourceLanguageName(sourceLanguageName.Val,
6118 sourceLanguageVersion.Val),
6119 File: file.Val, Producer: producer.Val, IsOptimized: isOptimized.Val, Flags: flags.Val, RuntimeVersion: runtimeVersion.Val,
6120 SplitDebugFilename: splitDebugFilename.Val, EmissionKind: emissionKind.Val, EnumTypes: enums.Val, RetainedTypes: retainedTypes.Val,
6121 GlobalVariables: globals.Val, ImportedEntities: imports.Val, Macros: macros.Val, DWOId: dwoId.Val, SplitDebugInlining: splitDebugInlining.Val,
6122 DebugInfoForProfiling: debugInfoForProfiling.Val, NameTableKind: nameTableKind.Val, RangesBaseAddress: rangesBaseAddress.Val,
6123 SysRoot: sysroot.Val, SDK: sdk.Val);
6124 return false;
6125}
6126
6127/// parseDISubprogram:
6128/// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo",
6129/// file: !1, line: 7, type: !2, isLocal: false,
6130/// isDefinition: true, scopeLine: 8, containingType: !3,
6131/// virtuality: DW_VIRTUALTIY_pure_virtual,
6132/// virtualIndex: 10, thisAdjustment: 4, flags: 11,
6133/// spFlags: 10, isOptimized: false, templateParams: !4,
6134/// declaration: !5, retainedNodes: !6, thrownTypes: !7,
6135/// annotations: !8)
6136bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) {
6137 auto Loc = Lex.getLoc();
6138#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6139 OPTIONAL(scope, MDField, ); \
6140 OPTIONAL(name, MDStringField, ); \
6141 OPTIONAL(linkageName, MDStringField, ); \
6142 OPTIONAL(file, MDField, ); \
6143 OPTIONAL(line, LineField, ); \
6144 OPTIONAL(type, MDField, ); \
6145 OPTIONAL(isLocal, MDBoolField, ); \
6146 OPTIONAL(isDefinition, MDBoolField, (true)); \
6147 OPTIONAL(scopeLine, LineField, ); \
6148 OPTIONAL(containingType, MDField, ); \
6149 OPTIONAL(virtuality, DwarfVirtualityField, ); \
6150 OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \
6151 OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \
6152 OPTIONAL(flags, DIFlagField, ); \
6153 OPTIONAL(spFlags, DISPFlagField, ); \
6154 OPTIONAL(isOptimized, MDBoolField, ); \
6155 OPTIONAL(unit, MDField, ); \
6156 OPTIONAL(templateParams, MDField, ); \
6157 OPTIONAL(declaration, MDField, ); \
6158 OPTIONAL(retainedNodes, MDField, ); \
6159 OPTIONAL(thrownTypes, MDField, ); \
6160 OPTIONAL(annotations, MDField, ); \
6161 OPTIONAL(targetFuncName, MDStringField, ); \
6162 OPTIONAL(keyInstructions, MDBoolField, );
6163 PARSE_MD_FIELDS();
6164#undef VISIT_MD_FIELDS
6165
6166 // An explicit spFlags field takes precedence over individual fields in
6167 // older IR versions.
6168 DISubprogram::DISPFlags SPFlags =
6169 spFlags.Seen ? spFlags.Val
6170 : DISubprogram::toSPFlags(IsLocalToUnit: isLocal.Val, IsDefinition: isDefinition.Val,
6171 IsOptimized: isOptimized.Val, Virtuality: virtuality.Val);
6172 if ((SPFlags & DISubprogram::SPFlagDefinition) && !IsDistinct)
6173 return error(
6174 L: Loc,
6175 Msg: "missing 'distinct', required for !DISubprogram that is a Definition");
6176 Result = GET_OR_DISTINCT(
6177 DISubprogram,
6178 (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val,
6179 type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val,
6180 thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val,
6181 declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val,
6182 targetFuncName.Val, keyInstructions.Val));
6183
6184 if (IsDistinct)
6185 NewDistinctSPs.push_back(Elt: cast<DISubprogram>(Val: Result));
6186
6187 return false;
6188}
6189
6190/// parseDILexicalBlock:
6191/// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9)
6192bool LLParser::parseDILexicalBlock(MDNode *&Result, bool IsDistinct) {
6193#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6194 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6195 OPTIONAL(file, MDField, ); \
6196 OPTIONAL(line, LineField, ); \
6197 OPTIONAL(column, ColumnField, );
6198 PARSE_MD_FIELDS();
6199#undef VISIT_MD_FIELDS
6200
6201 Result = GET_OR_DISTINCT(
6202 DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val));
6203 return false;
6204}
6205
6206/// parseDILexicalBlockFile:
6207/// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9)
6208bool LLParser::parseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) {
6209#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6210 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6211 OPTIONAL(file, MDField, ); \
6212 REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX));
6213 PARSE_MD_FIELDS();
6214#undef VISIT_MD_FIELDS
6215
6216 Result = GET_OR_DISTINCT(DILexicalBlockFile,
6217 (Context, scope.Val, file.Val, discriminator.Val));
6218 return false;
6219}
6220
6221/// parseDICommonBlock:
6222/// ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9)
6223bool LLParser::parseDICommonBlock(MDNode *&Result, bool IsDistinct) {
6224#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6225 REQUIRED(scope, MDField, ); \
6226 OPTIONAL(declaration, MDField, ); \
6227 OPTIONAL(name, MDStringField, ); \
6228 OPTIONAL(file, MDField, ); \
6229 OPTIONAL(line, LineField, );
6230 PARSE_MD_FIELDS();
6231#undef VISIT_MD_FIELDS
6232
6233 Result = GET_OR_DISTINCT(DICommonBlock,
6234 (Context, scope.Val, declaration.Val, name.Val,
6235 file.Val, line.Val));
6236 return false;
6237}
6238
6239/// parseDINamespace:
6240/// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9)
6241bool LLParser::parseDINamespace(MDNode *&Result, bool IsDistinct) {
6242#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6243 REQUIRED(scope, MDField, ); \
6244 OPTIONAL(name, MDStringField, ); \
6245 OPTIONAL(exportSymbols, MDBoolField, );
6246 PARSE_MD_FIELDS();
6247#undef VISIT_MD_FIELDS
6248
6249 Result = GET_OR_DISTINCT(DINamespace,
6250 (Context, scope.Val, name.Val, exportSymbols.Val));
6251 return false;
6252}
6253
6254/// parseDIMacro:
6255/// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value:
6256/// "SomeValue")
6257bool LLParser::parseDIMacro(MDNode *&Result, bool IsDistinct) {
6258#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6259 REQUIRED(type, DwarfMacinfoTypeField, ); \
6260 OPTIONAL(line, LineField, ); \
6261 REQUIRED(name, MDStringField, ); \
6262 OPTIONAL(value, MDStringField, );
6263 PARSE_MD_FIELDS();
6264#undef VISIT_MD_FIELDS
6265
6266 Result = GET_OR_DISTINCT(DIMacro,
6267 (Context, type.Val, line.Val, name.Val, value.Val));
6268 return false;
6269}
6270
6271/// parseDIMacroFile:
6272/// ::= !DIMacroFile(line: 9, file: !2, nodes: !3)
6273bool LLParser::parseDIMacroFile(MDNode *&Result, bool IsDistinct) {
6274#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6275 OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \
6276 OPTIONAL(line, LineField, ); \
6277 REQUIRED(file, MDField, ); \
6278 OPTIONAL(nodes, MDField, );
6279 PARSE_MD_FIELDS();
6280#undef VISIT_MD_FIELDS
6281
6282 Result = GET_OR_DISTINCT(DIMacroFile,
6283 (Context, type.Val, line.Val, file.Val, nodes.Val));
6284 return false;
6285}
6286
6287/// parseDIModule:
6288/// ::= !DIModule(scope: !0, name: "SomeModule", configMacros:
6289/// "-DNDEBUG", includePath: "/usr/include", apinotes: "module.apinotes",
6290/// file: !1, line: 4, isDecl: false)
6291bool LLParser::parseDIModule(MDNode *&Result, bool IsDistinct) {
6292#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6293 REQUIRED(scope, MDField, ); \
6294 REQUIRED(name, MDStringField, ); \
6295 OPTIONAL(configMacros, MDStringField, ); \
6296 OPTIONAL(includePath, MDStringField, ); \
6297 OPTIONAL(apinotes, MDStringField, ); \
6298 OPTIONAL(file, MDField, ); \
6299 OPTIONAL(line, LineField, ); \
6300 OPTIONAL(isDecl, MDBoolField, );
6301 PARSE_MD_FIELDS();
6302#undef VISIT_MD_FIELDS
6303
6304 Result = GET_OR_DISTINCT(DIModule, (Context, file.Val, scope.Val, name.Val,
6305 configMacros.Val, includePath.Val,
6306 apinotes.Val, line.Val, isDecl.Val));
6307 return false;
6308}
6309
6310/// parseDITemplateTypeParameter:
6311/// ::= !DITemplateTypeParameter(name: "Ty", type: !1, defaulted: false)
6312bool LLParser::parseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) {
6313#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6314 OPTIONAL(name, MDStringField, ); \
6315 REQUIRED(type, MDField, ); \
6316 OPTIONAL(defaulted, MDBoolField, );
6317 PARSE_MD_FIELDS();
6318#undef VISIT_MD_FIELDS
6319
6320 Result = GET_OR_DISTINCT(DITemplateTypeParameter,
6321 (Context, name.Val, type.Val, defaulted.Val));
6322 return false;
6323}
6324
6325/// parseDITemplateValueParameter:
6326/// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter,
6327/// name: "V", type: !1, defaulted: false,
6328/// value: i32 7)
6329bool LLParser::parseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) {
6330#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6331 OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \
6332 OPTIONAL(name, MDStringField, ); \
6333 OPTIONAL(type, MDField, ); \
6334 OPTIONAL(defaulted, MDBoolField, ); \
6335 REQUIRED(value, MDField, );
6336
6337 PARSE_MD_FIELDS();
6338#undef VISIT_MD_FIELDS
6339
6340 Result = GET_OR_DISTINCT(
6341 DITemplateValueParameter,
6342 (Context, tag.Val, name.Val, type.Val, defaulted.Val, value.Val));
6343 return false;
6344}
6345
6346/// parseDIGlobalVariable:
6347/// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo",
6348/// file: !1, line: 7, type: !2, isLocal: false,
6349/// isDefinition: true, templateParams: !3,
6350/// declaration: !4, align: 8)
6351bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) {
6352#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6353 OPTIONAL(name, MDStringField, (MDStringField::EmptyIs::Error)); \
6354 OPTIONAL(scope, MDField, ); \
6355 OPTIONAL(linkageName, MDStringField, ); \
6356 OPTIONAL(file, MDField, ); \
6357 OPTIONAL(line, LineField, ); \
6358 OPTIONAL(type, MDField, ); \
6359 OPTIONAL(isLocal, MDBoolField, ); \
6360 OPTIONAL(isDefinition, MDBoolField, (true)); \
6361 OPTIONAL(templateParams, MDField, ); \
6362 OPTIONAL(declaration, MDField, ); \
6363 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6364 OPTIONAL(annotations, MDField, );
6365 PARSE_MD_FIELDS();
6366#undef VISIT_MD_FIELDS
6367
6368 Result =
6369 GET_OR_DISTINCT(DIGlobalVariable,
6370 (Context, scope.Val, name.Val, linkageName.Val, file.Val,
6371 line.Val, type.Val, isLocal.Val, isDefinition.Val,
6372 declaration.Val, templateParams.Val, align.Val,
6373 annotations.Val));
6374 return false;
6375}
6376
6377/// parseDILocalVariable:
6378/// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo",
6379/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
6380/// align: 8)
6381/// ::= !DILocalVariable(scope: !0, name: "foo",
6382/// file: !1, line: 7, type: !2, arg: 2, flags: 7,
6383/// align: 8)
6384bool LLParser::parseDILocalVariable(MDNode *&Result, bool IsDistinct) {
6385#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6386 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6387 OPTIONAL(name, MDStringField, ); \
6388 OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \
6389 OPTIONAL(file, MDField, ); \
6390 OPTIONAL(line, LineField, ); \
6391 OPTIONAL(type, MDField, ); \
6392 OPTIONAL(flags, DIFlagField, ); \
6393 OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \
6394 OPTIONAL(annotations, MDField, );
6395 PARSE_MD_FIELDS();
6396#undef VISIT_MD_FIELDS
6397
6398 Result = GET_OR_DISTINCT(DILocalVariable,
6399 (Context, scope.Val, name.Val, file.Val, line.Val,
6400 type.Val, arg.Val, flags.Val, align.Val,
6401 annotations.Val));
6402 return false;
6403}
6404
6405/// parseDILabel:
6406/// ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7, column: 4)
6407bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) {
6408#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6409 REQUIRED(scope, MDField, (/* AllowNull */ false)); \
6410 REQUIRED(name, MDStringField, ); \
6411 REQUIRED(file, MDField, ); \
6412 REQUIRED(line, LineField, ); \
6413 OPTIONAL(column, ColumnField, ); \
6414 OPTIONAL(isArtificial, MDBoolField, ); \
6415 OPTIONAL(coroSuspendIdx, MDUnsignedField, );
6416 PARSE_MD_FIELDS();
6417#undef VISIT_MD_FIELDS
6418
6419 std::optional<unsigned> CoroSuspendIdx =
6420 coroSuspendIdx.Seen ? std::optional<unsigned>(coroSuspendIdx.Val)
6421 : std::nullopt;
6422
6423 Result = GET_OR_DISTINCT(DILabel,
6424 (Context, scope.Val, name.Val, file.Val, line.Val,
6425 column.Val, isArtificial.Val, CoroSuspendIdx));
6426 return false;
6427}
6428
6429/// parseDIExpressionBody:
6430/// ::= (0, 7, -1)
6431bool LLParser::parseDIExpressionBody(MDNode *&Result, bool IsDistinct) {
6432 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
6433 return true;
6434
6435 SmallVector<uint64_t, 8> Elements;
6436 if (Lex.getKind() != lltok::rparen)
6437 do {
6438 if (Lex.getKind() == lltok::DwarfOp) {
6439 if (unsigned Op = dwarf::getOperationEncoding(OperationEncodingString: Lex.getStrVal())) {
6440 Lex.Lex();
6441 Elements.push_back(Elt: Op);
6442 continue;
6443 }
6444 return tokError(Msg: Twine("invalid DWARF op '") + Lex.getStrVal() + "'");
6445 }
6446
6447 if (Lex.getKind() == lltok::DwarfAttEncoding) {
6448 if (unsigned Op = dwarf::getAttributeEncoding(EncodingString: Lex.getStrVal())) {
6449 Lex.Lex();
6450 Elements.push_back(Elt: Op);
6451 continue;
6452 }
6453 return tokError(Msg: Twine("invalid DWARF attribute encoding '") +
6454 Lex.getStrVal() + "'");
6455 }
6456
6457 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
6458 return tokError(Msg: "expected unsigned integer");
6459
6460 auto &U = Lex.getAPSIntVal();
6461 if (U.ugt(UINT64_MAX))
6462 return tokError(Msg: "element too large, limit is " + Twine(UINT64_MAX));
6463 Elements.push_back(Elt: U.getZExtValue());
6464 Lex.Lex();
6465 } while (EatIfPresent(T: lltok::comma));
6466
6467 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
6468 return true;
6469
6470 Result = GET_OR_DISTINCT(DIExpression, (Context, Elements));
6471 return false;
6472}
6473
6474/// parseDIExpression:
6475/// ::= !DIExpression(0, 7, -1)
6476bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) {
6477 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
6478 assert(Lex.getStrVal() == "DIExpression" && "Expected '!DIExpression'");
6479 Lex.Lex();
6480
6481 return parseDIExpressionBody(Result, IsDistinct);
6482}
6483
6484/// ParseDIArgList:
6485/// ::= !DIArgList(i32 7, i64 %0)
6486bool LLParser::parseDIArgList(Metadata *&MD, PerFunctionState *PFS) {
6487 assert(PFS && "Expected valid function state");
6488 assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name");
6489 Lex.Lex();
6490
6491 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
6492 return true;
6493
6494 SmallVector<ValueAsMetadata *, 4> Args;
6495 if (Lex.getKind() != lltok::rparen)
6496 do {
6497 Metadata *MD;
6498 if (parseValueAsMetadata(MD, TypeMsg: "expected value-as-metadata operand", PFS))
6499 return true;
6500 Args.push_back(Elt: dyn_cast<ValueAsMetadata>(Val: MD));
6501 } while (EatIfPresent(T: lltok::comma));
6502
6503 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
6504 return true;
6505
6506 MD = DIArgList::get(Context, Args);
6507 return false;
6508}
6509
6510/// parseDIGlobalVariableExpression:
6511/// ::= !DIGlobalVariableExpression(var: !0, expr: !1)
6512bool LLParser::parseDIGlobalVariableExpression(MDNode *&Result,
6513 bool IsDistinct) {
6514#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6515 REQUIRED(var, MDField, ); \
6516 REQUIRED(expr, MDField, );
6517 PARSE_MD_FIELDS();
6518#undef VISIT_MD_FIELDS
6519
6520 Result =
6521 GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val));
6522 return false;
6523}
6524
6525/// parseDIObjCProperty:
6526/// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo",
6527/// getter: "getFoo", attributes: 7, type: !2)
6528bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) {
6529#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6530 OPTIONAL(name, MDStringField, ); \
6531 OPTIONAL(file, MDField, ); \
6532 OPTIONAL(line, LineField, ); \
6533 OPTIONAL(setter, MDStringField, ); \
6534 OPTIONAL(getter, MDStringField, ); \
6535 OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \
6536 OPTIONAL(type, MDField, );
6537 PARSE_MD_FIELDS();
6538#undef VISIT_MD_FIELDS
6539
6540 Result = GET_OR_DISTINCT(DIObjCProperty,
6541 (Context, name.Val, file.Val, line.Val, getter.Val,
6542 setter.Val, attributes.Val, type.Val));
6543 return false;
6544}
6545
6546/// parseDIImportedEntity:
6547/// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1,
6548/// line: 7, name: "foo", elements: !2)
6549bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) {
6550#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
6551 REQUIRED(tag, DwarfTagField, ); \
6552 REQUIRED(scope, MDField, ); \
6553 OPTIONAL(entity, MDField, ); \
6554 OPTIONAL(file, MDField, ); \
6555 OPTIONAL(line, LineField, ); \
6556 OPTIONAL(name, MDStringField, ); \
6557 OPTIONAL(elements, MDField, );
6558 PARSE_MD_FIELDS();
6559#undef VISIT_MD_FIELDS
6560
6561 Result = GET_OR_DISTINCT(DIImportedEntity,
6562 (Context, tag.Val, scope.Val, entity.Val, file.Val,
6563 line.Val, name.Val, elements.Val));
6564 return false;
6565}
6566
6567#undef PARSE_MD_FIELD
6568#undef NOP_FIELD
6569#undef REQUIRE_FIELD
6570#undef DECLARE_FIELD
6571
6572/// parseMetadataAsValue
6573/// ::= metadata i32 %local
6574/// ::= metadata i32 @global
6575/// ::= metadata i32 7
6576/// ::= metadata !0
6577/// ::= metadata !{...}
6578/// ::= metadata !"string"
6579bool LLParser::parseMetadataAsValue(Value *&V, PerFunctionState &PFS) {
6580 // Note: the type 'metadata' has already been parsed.
6581 Metadata *MD;
6582 if (parseMetadata(MD, PFS: &PFS))
6583 return true;
6584
6585 V = MetadataAsValue::get(Context, MD);
6586 return false;
6587}
6588
6589/// parseValueAsMetadata
6590/// ::= i32 %local
6591/// ::= i32 @global
6592/// ::= i32 7
6593bool LLParser::parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
6594 PerFunctionState *PFS) {
6595 Type *Ty;
6596 LocTy Loc;
6597 if (parseType(Result&: Ty, Msg: TypeMsg, Loc))
6598 return true;
6599 if (Ty->isMetadataTy())
6600 return error(L: Loc, Msg: "invalid metadata-value-metadata roundtrip");
6601
6602 Value *V;
6603 if (parseValue(Ty, V, PFS))
6604 return true;
6605
6606 MD = ValueAsMetadata::get(V);
6607 return false;
6608}
6609
6610/// parseMetadata
6611/// ::= i32 %local
6612/// ::= i32 @global
6613/// ::= i32 7
6614/// ::= !42
6615/// ::= !{...}
6616/// ::= !"string"
6617/// ::= !DILocation(...)
6618bool LLParser::parseMetadata(Metadata *&MD, PerFunctionState *PFS) {
6619 if (Lex.getKind() == lltok::MetadataVar) {
6620 // DIArgLists are a special case, as they are a list of ValueAsMetadata and
6621 // so parsing this requires a Function State.
6622 if (Lex.getStrVal() == "DIArgList") {
6623 Metadata *AL;
6624 if (parseDIArgList(MD&: AL, PFS))
6625 return true;
6626 MD = AL;
6627 return false;
6628 }
6629 MDNode *N;
6630 if (parseSpecializedMDNode(N)) {
6631 return true;
6632 }
6633 MD = N;
6634 return false;
6635 }
6636
6637 // ValueAsMetadata:
6638 // <type> <value>
6639 if (Lex.getKind() != lltok::exclaim)
6640 return parseValueAsMetadata(MD, TypeMsg: "expected metadata operand", PFS);
6641
6642 // '!'.
6643 assert(Lex.getKind() == lltok::exclaim && "Expected '!' here");
6644 Lex.Lex();
6645
6646 // MDString:
6647 // ::= '!' STRINGCONSTANT
6648 if (Lex.getKind() == lltok::StringConstant) {
6649 MDString *S;
6650 if (parseMDString(Result&: S))
6651 return true;
6652 MD = S;
6653 return false;
6654 }
6655
6656 // MDNode:
6657 // !{ ... }
6658 // !7
6659 MDNode *N;
6660 if (parseMDNodeTail(N))
6661 return true;
6662 MD = N;
6663 return false;
6664}
6665
6666//===----------------------------------------------------------------------===//
6667// Function Parsing.
6668//===----------------------------------------------------------------------===//
6669
6670bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
6671 PerFunctionState *PFS) {
6672 if (Ty->isFunctionTy())
6673 return error(L: ID.Loc, Msg: "functions are not values, refer to them as pointers");
6674
6675 switch (ID.Kind) {
6676 case ValID::t_LocalID:
6677 if (!PFS)
6678 return error(L: ID.Loc, Msg: "invalid use of function-local name");
6679 V = PFS->getVal(ID: ID.UIntVal, Ty, Loc: ID.Loc);
6680 return V == nullptr;
6681 case ValID::t_LocalName:
6682 if (!PFS)
6683 return error(L: ID.Loc, Msg: "invalid use of function-local name");
6684 V = PFS->getVal(Name: ID.StrVal, Ty, Loc: ID.Loc);
6685 return V == nullptr;
6686 case ValID::t_InlineAsm: {
6687 if (!ID.FTy)
6688 return error(L: ID.Loc, Msg: "invalid type for inline asm constraint string");
6689 if (Error Err = InlineAsm::verify(Ty: ID.FTy, Constraints: ID.StrVal2))
6690 return error(L: ID.Loc, Msg: toString(E: std::move(Err)));
6691 V = InlineAsm::get(
6692 Ty: ID.FTy, AsmString: ID.StrVal, Constraints: ID.StrVal2, hasSideEffects: ID.UIntVal & 1, isAlignStack: (ID.UIntVal >> 1) & 1,
6693 asmDialect: InlineAsm::AsmDialect((ID.UIntVal >> 2) & 1), canThrow: (ID.UIntVal >> 3) & 1);
6694 return false;
6695 }
6696 case ValID::t_GlobalName:
6697 V = getGlobalVal(Name: ID.StrVal, Ty, Loc: ID.Loc);
6698 if (V && ID.NoCFI)
6699 V = NoCFIValue::get(GV: cast<GlobalValue>(Val: V));
6700 return V == nullptr;
6701 case ValID::t_GlobalID:
6702 V = getGlobalVal(ID: ID.UIntVal, Ty, Loc: ID.Loc);
6703 if (V && ID.NoCFI)
6704 V = NoCFIValue::get(GV: cast<GlobalValue>(Val: V));
6705 return V == nullptr;
6706 case ValID::t_APSInt:
6707 if (!Ty->isIntegerTy())
6708 return error(L: ID.Loc, Msg: "integer constant must have integer type");
6709 ID.APSIntVal = ID.APSIntVal.extOrTrunc(width: Ty->getPrimitiveSizeInBits());
6710 V = ConstantInt::get(Context, V: ID.APSIntVal);
6711 return false;
6712 case ValID::t_APFloat:
6713 if (!Ty->isFloatingPointTy() ||
6714 !ConstantFP::isValueValidForType(Ty, V: ID.APFloatVal))
6715 return error(L: ID.Loc, Msg: "floating point constant invalid for type");
6716
6717 // The lexer has no type info, so builds all half, bfloat, float, and double
6718 // FP constants as double. Fix this here. Long double does not need this.
6719 if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) {
6720 // Check for signaling before potentially converting and losing that info.
6721 bool IsSNAN = ID.APFloatVal.isSignaling();
6722 bool Ignored;
6723 if (Ty->isHalfTy())
6724 ID.APFloatVal.convert(ToSemantics: APFloat::IEEEhalf(), RM: APFloat::rmNearestTiesToEven,
6725 losesInfo: &Ignored);
6726 else if (Ty->isBFloatTy())
6727 ID.APFloatVal.convert(ToSemantics: APFloat::BFloat(), RM: APFloat::rmNearestTiesToEven,
6728 losesInfo: &Ignored);
6729 else if (Ty->isFloatTy())
6730 ID.APFloatVal.convert(ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven,
6731 losesInfo: &Ignored);
6732 if (IsSNAN) {
6733 // The convert call above may quiet an SNaN, so manufacture another
6734 // SNaN. The bitcast works because the payload (significand) parameter
6735 // is truncated to fit.
6736 APInt Payload = ID.APFloatVal.bitcastToAPInt();
6737 ID.APFloatVal = APFloat::getSNaN(Sem: ID.APFloatVal.getSemantics(),
6738 Negative: ID.APFloatVal.isNegative(), payload: &Payload);
6739 }
6740 }
6741 V = ConstantFP::get(Context, V: ID.APFloatVal);
6742
6743 if (V->getType() != Ty)
6744 return error(L: ID.Loc, Msg: "floating point constant does not have type '" +
6745 getTypeString(T: Ty) + "'");
6746
6747 return false;
6748 case ValID::t_Null:
6749 if (!Ty->isPointerTy())
6750 return error(L: ID.Loc, Msg: "null must be a pointer type");
6751 V = ConstantPointerNull::get(T: cast<PointerType>(Val: Ty));
6752 return false;
6753 case ValID::t_Undef:
6754 // FIXME: LabelTy should not be a first-class type.
6755 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6756 return error(L: ID.Loc, Msg: "invalid type for undef constant");
6757 V = UndefValue::get(T: Ty);
6758 return false;
6759 case ValID::t_EmptyArray:
6760 if (!Ty->isArrayTy() || cast<ArrayType>(Val: Ty)->getNumElements() != 0)
6761 return error(L: ID.Loc, Msg: "invalid empty array initializer");
6762 V = PoisonValue::get(T: Ty);
6763 return false;
6764 case ValID::t_Zero:
6765 // FIXME: LabelTy should not be a first-class type.
6766 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6767 return error(L: ID.Loc, Msg: "invalid type for null constant");
6768 if (auto *TETy = dyn_cast<TargetExtType>(Val: Ty))
6769 if (!TETy->hasProperty(Prop: TargetExtType::HasZeroInit))
6770 return error(L: ID.Loc, Msg: "invalid type for null constant");
6771 V = Constant::getNullValue(Ty);
6772 return false;
6773 case ValID::t_None:
6774 if (!Ty->isTokenTy())
6775 return error(L: ID.Loc, Msg: "invalid type for none constant");
6776 V = Constant::getNullValue(Ty);
6777 return false;
6778 case ValID::t_Poison:
6779 // FIXME: LabelTy should not be a first-class type.
6780 if (!Ty->isFirstClassType() || Ty->isLabelTy())
6781 return error(L: ID.Loc, Msg: "invalid type for poison constant");
6782 V = PoisonValue::get(T: Ty);
6783 return false;
6784 case ValID::t_Constant:
6785 if (ID.ConstantVal->getType() != Ty)
6786 return error(L: ID.Loc, Msg: "constant expression type mismatch: got type '" +
6787 getTypeString(T: ID.ConstantVal->getType()) +
6788 "' but expected '" + getTypeString(T: Ty) + "'");
6789 V = ID.ConstantVal;
6790 return false;
6791 case ValID::t_ConstantSplat:
6792 if (!Ty->isVectorTy())
6793 return error(L: ID.Loc, Msg: "vector constant must have vector type");
6794 if (ID.ConstantVal->getType() != Ty->getScalarType())
6795 return error(L: ID.Loc, Msg: "constant expression type mismatch: got type '" +
6796 getTypeString(T: ID.ConstantVal->getType()) +
6797 "' but expected '" +
6798 getTypeString(T: Ty->getScalarType()) + "'");
6799 V = ConstantVector::getSplat(EC: cast<VectorType>(Val: Ty)->getElementCount(),
6800 Elt: ID.ConstantVal);
6801 return false;
6802 case ValID::t_ConstantStruct:
6803 case ValID::t_PackedConstantStruct:
6804 if (StructType *ST = dyn_cast<StructType>(Val: Ty)) {
6805 if (ST->getNumElements() != ID.UIntVal)
6806 return error(L: ID.Loc,
6807 Msg: "initializer with struct type has wrong # elements");
6808 if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct))
6809 return error(L: ID.Loc, Msg: "packed'ness of initializer and type don't match");
6810
6811 // Verify that the elements are compatible with the structtype.
6812 for (unsigned i = 0, e = ID.UIntVal; i != e; ++i)
6813 if (ID.ConstantStructElts[i]->getType() != ST->getElementType(N: i))
6814 return error(
6815 L: ID.Loc,
6816 Msg: "element " + Twine(i) +
6817 " of struct initializer doesn't match struct element type");
6818
6819 V = ConstantStruct::get(
6820 T: ST, V: ArrayRef(ID.ConstantStructElts.get(), ID.UIntVal));
6821 } else
6822 return error(L: ID.Loc, Msg: "constant expression type mismatch");
6823 return false;
6824 }
6825 llvm_unreachable("Invalid ValID");
6826}
6827
6828bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
6829 C = nullptr;
6830 ValID ID;
6831 auto Loc = Lex.getLoc();
6832 if (parseValID(ID, /*PFS=*/nullptr))
6833 return true;
6834 switch (ID.Kind) {
6835 case ValID::t_APSInt:
6836 case ValID::t_APFloat:
6837 case ValID::t_Undef:
6838 case ValID::t_Poison:
6839 case ValID::t_Zero:
6840 case ValID::t_Constant:
6841 case ValID::t_ConstantSplat:
6842 case ValID::t_ConstantStruct:
6843 case ValID::t_PackedConstantStruct: {
6844 Value *V;
6845 if (convertValIDToValue(Ty, ID, V, /*PFS=*/nullptr))
6846 return true;
6847 assert(isa<Constant>(V) && "Expected a constant value");
6848 C = cast<Constant>(Val: V);
6849 return false;
6850 }
6851 case ValID::t_Null:
6852 C = Constant::getNullValue(Ty);
6853 return false;
6854 default:
6855 return error(L: Loc, Msg: "expected a constant value");
6856 }
6857}
6858
6859bool LLParser::parseValue(Type *Ty, Value *&V, PerFunctionState *PFS) {
6860 V = nullptr;
6861 ValID ID;
6862
6863 FileLoc Start = getTokLineColumnPos();
6864 bool Ret = parseValID(ID, PFS, ExpectedTy: Ty) || convertValIDToValue(Ty, ID, V, PFS);
6865 if (!Ret && ParserContext) {
6866 FileLoc End = getPrevTokEndLineColumnPos();
6867 ParserContext->addValueReferenceAtLocation(V, FileLocRange(Start, End));
6868 }
6869 return Ret;
6870}
6871
6872bool LLParser::parseTypeAndValue(Value *&V, PerFunctionState *PFS) {
6873 Type *Ty = nullptr;
6874 return parseType(Result&: Ty) || parseValue(Ty, V, PFS);
6875}
6876
6877bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
6878 PerFunctionState &PFS) {
6879 Value *V;
6880 Loc = Lex.getLoc();
6881 if (parseTypeAndValue(V, PFS))
6882 return true;
6883 if (!isa<BasicBlock>(Val: V))
6884 return error(L: Loc, Msg: "expected a basic block");
6885 BB = cast<BasicBlock>(Val: V);
6886 return false;
6887}
6888
6889bool isOldDbgFormatIntrinsic(StringRef Name) {
6890 // Exit early for the common (non-debug-intrinsic) case.
6891 // We can make this the only check when we begin supporting all "llvm.dbg"
6892 // intrinsics in the new debug info format.
6893 if (!Name.starts_with(Prefix: "llvm.dbg."))
6894 return false;
6895 Intrinsic::ID FnID = Intrinsic::lookupIntrinsicID(Name);
6896 return FnID == Intrinsic::dbg_declare || FnID == Intrinsic::dbg_value ||
6897 FnID == Intrinsic::dbg_assign;
6898}
6899
6900/// FunctionHeader
6901/// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility
6902/// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName
6903/// '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign
6904/// OptGC OptionalPrefix OptionalPrologue OptPersonalityFn
6905bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
6906 unsigned &FunctionNumber,
6907 SmallVectorImpl<unsigned> &UnnamedArgNums) {
6908 // parse the linkage.
6909 LocTy LinkageLoc = Lex.getLoc();
6910 unsigned Linkage;
6911 unsigned Visibility;
6912 unsigned DLLStorageClass;
6913 bool DSOLocal;
6914 AttrBuilder RetAttrs(M->getContext());
6915 unsigned CC;
6916 bool HasLinkage;
6917 Type *RetType = nullptr;
6918 LocTy RetTypeLoc = Lex.getLoc();
6919 if (parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass,
6920 DSOLocal) ||
6921 parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) ||
6922 parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/))
6923 return true;
6924
6925 // Verify that the linkage is ok.
6926 switch ((GlobalValue::LinkageTypes)Linkage) {
6927 case GlobalValue::ExternalLinkage:
6928 break; // always ok.
6929 case GlobalValue::ExternalWeakLinkage:
6930 if (IsDefine)
6931 return error(L: LinkageLoc, Msg: "invalid linkage for function definition");
6932 break;
6933 case GlobalValue::PrivateLinkage:
6934 case GlobalValue::InternalLinkage:
6935 case GlobalValue::AvailableExternallyLinkage:
6936 case GlobalValue::LinkOnceAnyLinkage:
6937 case GlobalValue::LinkOnceODRLinkage:
6938 case GlobalValue::WeakAnyLinkage:
6939 case GlobalValue::WeakODRLinkage:
6940 if (!IsDefine)
6941 return error(L: LinkageLoc, Msg: "invalid linkage for function declaration");
6942 break;
6943 case GlobalValue::AppendingLinkage:
6944 case GlobalValue::CommonLinkage:
6945 return error(L: LinkageLoc, Msg: "invalid function linkage type");
6946 }
6947
6948 if (!isValidVisibilityForLinkage(V: Visibility, L: Linkage))
6949 return error(L: LinkageLoc,
6950 Msg: "symbol with local linkage must have default visibility");
6951
6952 if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L: Linkage))
6953 return error(L: LinkageLoc,
6954 Msg: "symbol with local linkage cannot have a DLL storage class");
6955
6956 if (!FunctionType::isValidReturnType(RetTy: RetType))
6957 return error(L: RetTypeLoc, Msg: "invalid function return type");
6958
6959 LocTy NameLoc = Lex.getLoc();
6960
6961 std::string FunctionName;
6962 if (Lex.getKind() == lltok::GlobalVar) {
6963 FunctionName = Lex.getStrVal();
6964 } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok.
6965 FunctionNumber = Lex.getUIntVal();
6966 if (checkValueID(Loc: NameLoc, Kind: "function", Prefix: "@", NextID: NumberedVals.getNext(),
6967 ID: FunctionNumber))
6968 return true;
6969 } else {
6970 return tokError(Msg: "expected function name");
6971 }
6972
6973 Lex.Lex();
6974
6975 if (Lex.getKind() != lltok::lparen)
6976 return tokError(Msg: "expected '(' in function argument list");
6977
6978 SmallVector<ArgInfo, 8> ArgList;
6979 bool IsVarArg;
6980 AttrBuilder FuncAttrs(M->getContext());
6981 std::vector<unsigned> FwdRefAttrGrps;
6982 LocTy BuiltinLoc;
6983 std::string Section;
6984 std::string Partition;
6985 MaybeAlign Alignment, PrefAlignment;
6986 std::string GC;
6987 GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
6988 unsigned AddrSpace = 0;
6989 Constant *Prefix = nullptr;
6990 Constant *Prologue = nullptr;
6991 Constant *PersonalityFn = nullptr;
6992 Comdat *C;
6993
6994 if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg) ||
6995 parseOptionalUnnamedAddr(UnnamedAddr) ||
6996 parseOptionalProgramAddrSpace(AddrSpace) ||
6997 parseFnAttributeValuePairs(B&: FuncAttrs, FwdRefAttrGrps, InAttrGrp: false,
6998 BuiltinLoc) ||
6999 (EatIfPresent(T: lltok::kw_section) && parseStringConstant(Result&: Section)) ||
7000 (EatIfPresent(T: lltok::kw_partition) && parseStringConstant(Result&: Partition)) ||
7001 parseOptionalComdat(GlobalName: FunctionName, C) ||
7002 parseOptionalAlignment(Alignment) ||
7003 parseOptionalPrefAlignment(Alignment&: PrefAlignment) ||
7004 (EatIfPresent(T: lltok::kw_gc) && parseStringConstant(Result&: GC)) ||
7005 (EatIfPresent(T: lltok::kw_prefix) && parseGlobalTypeAndValue(V&: Prefix)) ||
7006 (EatIfPresent(T: lltok::kw_prologue) && parseGlobalTypeAndValue(V&: Prologue)) ||
7007 (EatIfPresent(T: lltok::kw_personality) &&
7008 parseGlobalTypeAndValue(V&: PersonalityFn)))
7009 return true;
7010
7011 if (FuncAttrs.contains(A: Attribute::Builtin))
7012 return error(L: BuiltinLoc, Msg: "'builtin' attribute not valid on function");
7013
7014 // If the alignment was parsed as an attribute, move to the alignment field.
7015 if (MaybeAlign A = FuncAttrs.getAlignment()) {
7016 Alignment = A;
7017 FuncAttrs.removeAttribute(Val: Attribute::Alignment);
7018 }
7019
7020 // Okay, if we got here, the function is syntactically valid. Convert types
7021 // and do semantic checks.
7022 std::vector<Type*> ParamTypeList;
7023 SmallVector<AttributeSet, 8> Attrs;
7024
7025 for (const ArgInfo &Arg : ArgList) {
7026 ParamTypeList.push_back(x: Arg.Ty);
7027 Attrs.push_back(Elt: Arg.Attrs);
7028 }
7029
7030 AttributeList PAL =
7031 AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FuncAttrs),
7032 RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs: Attrs);
7033
7034 if (PAL.hasParamAttr(ArgNo: 0, Kind: Attribute::StructRet) && !RetType->isVoidTy())
7035 return error(L: RetTypeLoc, Msg: "functions with 'sret' argument must return void");
7036
7037 FunctionType *FT = FunctionType::get(Result: RetType, Params: ParamTypeList, isVarArg: IsVarArg);
7038 PointerType *PFT = PointerType::get(C&: Context, AddressSpace: AddrSpace);
7039
7040 Fn = nullptr;
7041 GlobalValue *FwdFn = nullptr;
7042 if (!FunctionName.empty()) {
7043 // If this was a definition of a forward reference, remove the definition
7044 // from the forward reference table and fill in the forward ref.
7045 auto FRVI = ForwardRefVals.find(x: FunctionName);
7046 if (FRVI != ForwardRefVals.end()) {
7047 FwdFn = FRVI->second.first;
7048 if (FwdFn->getType() != PFT)
7049 return error(L: FRVI->second.second,
7050 Msg: "invalid forward reference to "
7051 "function '" +
7052 FunctionName +
7053 "' with wrong type: "
7054 "expected '" +
7055 getTypeString(T: PFT) + "' but was '" +
7056 getTypeString(T: FwdFn->getType()) + "'");
7057 ForwardRefVals.erase(position: FRVI);
7058 } else if ((Fn = M->getFunction(Name: FunctionName))) {
7059 // Reject redefinitions.
7060 return error(L: NameLoc,
7061 Msg: "invalid redefinition of function '" + FunctionName + "'");
7062 } else if (M->getNamedValue(Name: FunctionName)) {
7063 return error(L: NameLoc, Msg: "redefinition of function '@" + FunctionName + "'");
7064 }
7065
7066 } else {
7067 // Handle @"", where a name is syntactically specified, but semantically
7068 // missing.
7069 if (FunctionNumber == (unsigned)-1)
7070 FunctionNumber = NumberedVals.getNext();
7071
7072 // If this is a definition of a forward referenced function, make sure the
7073 // types agree.
7074 auto I = ForwardRefValIDs.find(x: FunctionNumber);
7075 if (I != ForwardRefValIDs.end()) {
7076 FwdFn = I->second.first;
7077 if (FwdFn->getType() != PFT)
7078 return error(L: NameLoc, Msg: "type of definition and forward reference of '@" +
7079 Twine(FunctionNumber) +
7080 "' disagree: "
7081 "expected '" +
7082 getTypeString(T: PFT) + "' but was '" +
7083 getTypeString(T: FwdFn->getType()) + "'");
7084 ForwardRefValIDs.erase(position: I);
7085 }
7086 }
7087
7088 Fn = Function::Create(Ty: FT, Linkage: GlobalValue::ExternalLinkage, AddrSpace,
7089 N: FunctionName, M);
7090
7091 assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS");
7092
7093 if (FunctionName.empty())
7094 NumberedVals.add(ID: FunctionNumber, V: Fn);
7095
7096 Fn->setLinkage((GlobalValue::LinkageTypes)Linkage);
7097 maybeSetDSOLocal(DSOLocal, GV&: *Fn);
7098 Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility);
7099 Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
7100 Fn->setCallingConv(CC);
7101 Fn->setAttributes(PAL);
7102 Fn->setUnnamedAddr(UnnamedAddr);
7103 if (Alignment)
7104 Fn->setAlignment(*Alignment);
7105 Fn->setPreferredAlignment(PrefAlignment);
7106 Fn->setSection(Section);
7107 Fn->setPartition(Partition);
7108 Fn->setComdat(C);
7109 Fn->setPersonalityFn(PersonalityFn);
7110 if (!GC.empty()) Fn->setGC(GC);
7111 Fn->setPrefixData(Prefix);
7112 Fn->setPrologueData(Prologue);
7113 ForwardRefAttrGroups[Fn] = FwdRefAttrGrps;
7114
7115 // Add all of the arguments we parsed to the function.
7116 Function::arg_iterator ArgIt = Fn->arg_begin();
7117 for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) {
7118 if (ParserContext && ArgList[i].IdentLoc)
7119 ParserContext->addInstructionOrArgumentLocation(
7120 &*ArgIt, ArgList[i].IdentLoc.value());
7121 // If the argument has a name, insert it into the argument symbol table.
7122 if (ArgList[i].Name.empty()) continue;
7123
7124 // Set the name, if it conflicted, it will be auto-renamed.
7125 ArgIt->setName(ArgList[i].Name);
7126
7127 if (ArgIt->getName() != ArgList[i].Name)
7128 return error(L: ArgList[i].Loc,
7129 Msg: "redefinition of argument '%" + ArgList[i].Name + "'");
7130 }
7131
7132 if (FwdFn) {
7133 FwdFn->replaceAllUsesWith(V: Fn);
7134 FwdFn->eraseFromParent();
7135 }
7136
7137 if (IsDefine)
7138 return false;
7139
7140 // Check the declaration has no block address forward references.
7141 ValID ID;
7142 if (FunctionName.empty()) {
7143 ID.Kind = ValID::t_GlobalID;
7144 ID.UIntVal = FunctionNumber;
7145 } else {
7146 ID.Kind = ValID::t_GlobalName;
7147 ID.StrVal = FunctionName;
7148 }
7149 auto Blocks = ForwardRefBlockAddresses.find(x: ID);
7150 if (Blocks != ForwardRefBlockAddresses.end())
7151 return error(L: Blocks->first.Loc,
7152 Msg: "cannot take blockaddress inside a declaration");
7153 return false;
7154}
7155
7156bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
7157 ValID ID;
7158 if (FunctionNumber == -1) {
7159 ID.Kind = ValID::t_GlobalName;
7160 ID.StrVal = std::string(F.getName());
7161 } else {
7162 ID.Kind = ValID::t_GlobalID;
7163 ID.UIntVal = FunctionNumber;
7164 }
7165
7166 auto Blocks = P.ForwardRefBlockAddresses.find(x: ID);
7167 if (Blocks == P.ForwardRefBlockAddresses.end())
7168 return false;
7169
7170 for (const auto &I : Blocks->second) {
7171 const ValID &BBID = I.first;
7172 GlobalValue *GV = I.second;
7173
7174 assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) &&
7175 "Expected local id or name");
7176 BasicBlock *BB;
7177 if (BBID.Kind == ValID::t_LocalName)
7178 BB = getBB(Name: BBID.StrVal, Loc: BBID.Loc);
7179 else
7180 BB = getBB(ID: BBID.UIntVal, Loc: BBID.Loc);
7181 if (!BB)
7182 return P.error(L: BBID.Loc, Msg: "referenced value is not a basic block");
7183
7184 Value *ResolvedVal = BlockAddress::get(F: &F, BB);
7185 ResolvedVal = P.checkValidVariableType(Loc: BBID.Loc, Name: BBID.StrVal, Ty: GV->getType(),
7186 Val: ResolvedVal);
7187 if (!ResolvedVal)
7188 return true;
7189 GV->replaceAllUsesWith(V: ResolvedVal);
7190 GV->eraseFromParent();
7191 }
7192
7193 P.ForwardRefBlockAddresses.erase(position: Blocks);
7194 return false;
7195}
7196
7197/// parseFunctionBody
7198/// ::= '{' BasicBlock+ UseListOrderDirective* '}'
7199bool LLParser::parseFunctionBody(Function &Fn, unsigned FunctionNumber,
7200 ArrayRef<unsigned> UnnamedArgNums) {
7201 if (Lex.getKind() != lltok::lbrace)
7202 return tokError(Msg: "expected '{' in function body");
7203 Lex.Lex(); // eat the {.
7204
7205 PerFunctionState PFS(*this, Fn, FunctionNumber, UnnamedArgNums);
7206
7207 // Resolve block addresses and allow basic blocks to be forward-declared
7208 // within this function.
7209 if (PFS.resolveForwardRefBlockAddresses())
7210 return true;
7211 SaveAndRestore ScopeExit(BlockAddressPFS, &PFS);
7212
7213 // We need at least one basic block.
7214 if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder)
7215 return tokError(Msg: "function body requires at least one basic block");
7216
7217 while (Lex.getKind() != lltok::rbrace &&
7218 Lex.getKind() != lltok::kw_uselistorder)
7219 if (parseBasicBlock(PFS))
7220 return true;
7221
7222 while (Lex.getKind() != lltok::rbrace)
7223 if (parseUseListOrder(PFS: &PFS))
7224 return true;
7225
7226 // Eat the }.
7227 Lex.Lex();
7228
7229 // Verify function is ok.
7230 return PFS.finishFunction();
7231}
7232
7233/// parseBasicBlock
7234/// ::= (LabelStr|LabelID)? Instruction*
7235bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
7236 FileLoc BBStart = getTokLineColumnPos();
7237
7238 // If this basic block starts out with a name, remember it.
7239 std::string Name;
7240 int NameID = -1;
7241 LocTy NameLoc = Lex.getLoc();
7242 if (Lex.getKind() == lltok::LabelStr) {
7243 Name = Lex.getStrVal();
7244 Lex.Lex();
7245 } else if (Lex.getKind() == lltok::LabelID) {
7246 NameID = Lex.getUIntVal();
7247 Lex.Lex();
7248 }
7249
7250 BasicBlock *BB = PFS.defineBB(Name, NameID, Loc: NameLoc);
7251 if (!BB)
7252 return true;
7253
7254 std::string NameStr;
7255
7256 // Parse the instructions and debug values in this block until we get a
7257 // terminator.
7258 Instruction *Inst;
7259 auto DeleteDbgRecord = [](DbgRecord *DR) { DR->deleteRecord(); };
7260 using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype(DeleteDbgRecord)>;
7261 SmallVector<DbgRecordPtr> TrailingDbgRecord;
7262 do {
7263 // Handle debug records first - there should always be an instruction
7264 // following the debug records, i.e. they cannot appear after the block
7265 // terminator.
7266 while (Lex.getKind() == lltok::hash) {
7267 if (SeenOldDbgInfoFormat)
7268 return error(L: Lex.getLoc(), Msg: "debug record should not appear in a module "
7269 "containing debug info intrinsics");
7270 SeenNewDbgInfoFormat = true;
7271 Lex.Lex();
7272
7273 DbgRecord *DR;
7274 if (parseDebugRecord(DR, PFS))
7275 return true;
7276 TrailingDbgRecord.emplace_back(Args&: DR, Args&: DeleteDbgRecord);
7277 }
7278
7279 FileLoc InstStart = getTokLineColumnPos();
7280 // This instruction may have three possibilities for a name: a) none
7281 // specified, b) name specified "%foo =", c) number specified: "%4 =".
7282 LocTy NameLoc = Lex.getLoc();
7283 int NameID = -1;
7284 NameStr = "";
7285
7286 if (Lex.getKind() == lltok::LocalVarID) {
7287 NameID = Lex.getUIntVal();
7288 Lex.Lex();
7289 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after instruction id"))
7290 return true;
7291 } else if (Lex.getKind() == lltok::LocalVar) {
7292 NameStr = Lex.getStrVal();
7293 Lex.Lex();
7294 if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after instruction name"))
7295 return true;
7296 }
7297
7298 switch (parseInstruction(Inst, BB, PFS)) {
7299 default:
7300 llvm_unreachable("Unknown parseInstruction result!");
7301 case InstError: return true;
7302 case InstNormal:
7303 Inst->insertInto(ParentBB: BB, It: BB->end());
7304
7305 // With a normal result, we check to see if the instruction is followed by
7306 // a comma and metadata.
7307 if (EatIfPresent(T: lltok::comma))
7308 if (parseInstructionMetadata(Inst&: *Inst))
7309 return true;
7310 break;
7311 case InstExtraComma:
7312 Inst->insertInto(ParentBB: BB, It: BB->end());
7313
7314 // If the instruction parser ate an extra comma at the end of it, it
7315 // *must* be followed by metadata.
7316 if (parseInstructionMetadata(Inst&: *Inst))
7317 return true;
7318 break;
7319 }
7320
7321 // Set the name on the instruction.
7322 if (PFS.setInstName(NameID, NameStr, NameLoc, Inst))
7323 return true;
7324
7325 // Attach any preceding debug values to this instruction.
7326 for (DbgRecordPtr &DR : TrailingDbgRecord)
7327 BB->insertDbgRecordBefore(DR: DR.release(), Here: Inst->getIterator());
7328 TrailingDbgRecord.clear();
7329 if (ParserContext) {
7330 ParserContext->addInstructionOrArgumentLocation(
7331 Inst, FileLocRange(InstStart, getPrevTokEndLineColumnPos()));
7332 }
7333 } while (!Inst->isTerminator());
7334
7335 if (ParserContext)
7336 ParserContext->addBlockLocation(
7337 BB, FileLocRange(BBStart, getPrevTokEndLineColumnPos()));
7338
7339 assert(TrailingDbgRecord.empty() &&
7340 "All debug values should have been attached to an instruction.");
7341
7342 return false;
7343}
7344
7345/// parseDebugRecord
7346/// ::= #dbg_label '(' MDNode ')'
7347/// ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ','
7348/// (MDNode ',' Metadata ',' Metadata ',')? MDNode ')'
7349bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) {
7350 using RecordKind = DbgRecord::Kind;
7351 using LocType = DbgVariableRecord::LocationType;
7352 LocTy DVRLoc = Lex.getLoc();
7353 if (Lex.getKind() != lltok::DbgRecordType)
7354 return error(L: DVRLoc, Msg: "expected debug record type here");
7355 RecordKind RecordType = StringSwitch<RecordKind>(Lex.getStrVal())
7356 .Case(S: "declare", Value: RecordKind::ValueKind)
7357 .Case(S: "value", Value: RecordKind::ValueKind)
7358 .Case(S: "assign", Value: RecordKind::ValueKind)
7359 .Case(S: "label", Value: RecordKind::LabelKind)
7360 .Case(S: "declare_value", Value: RecordKind::ValueKind);
7361
7362 // Parsing labels is trivial; parse here and early exit, otherwise go into the
7363 // full DbgVariableRecord processing stage.
7364 if (RecordType == RecordKind::LabelKind) {
7365 Lex.Lex();
7366 if (parseToken(T: lltok::lparen, ErrMsg: "Expected '(' here"))
7367 return true;
7368 MDNode *Label;
7369 if (parseMDNode(N&: Label))
7370 return true;
7371 if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here"))
7372 return true;
7373 MDNode *DbgLoc;
7374 if (parseMDNode(N&: DbgLoc))
7375 return true;
7376 if (parseToken(T: lltok::rparen, ErrMsg: "Expected ')' here"))
7377 return true;
7378 DR = DbgLabelRecord::createUnresolvedDbgLabelRecord(Label, DL: DbgLoc);
7379 return false;
7380 }
7381
7382 LocType ValueType = StringSwitch<LocType>(Lex.getStrVal())
7383 .Case(S: "declare", Value: LocType::Declare)
7384 .Case(S: "value", Value: LocType::Value)
7385 .Case(S: "assign", Value: LocType::Assign)
7386 .Case(S: "declare_value", Value: LocType::DeclareValue);
7387
7388 Lex.Lex();
7389 if (parseToken(T: lltok::lparen, ErrMsg: "Expected '(' here"))
7390 return true;
7391
7392 // Parse Value field.
7393 Metadata *ValLocMD;
7394 if (parseMetadata(MD&: ValLocMD, PFS: &PFS))
7395 return true;
7396 if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here"))
7397 return true;
7398
7399 // Parse Variable field.
7400 MDNode *Variable;
7401 if (parseMDNode(N&: Variable))
7402 return true;
7403 if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here"))
7404 return true;
7405
7406 // Parse Expression field.
7407 MDNode *Expression;
7408 if (parseMDNode(N&: Expression))
7409 return true;
7410 if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here"))
7411 return true;
7412
7413 // Parse additional fields for #dbg_assign.
7414 MDNode *AssignID = nullptr;
7415 Metadata *AddressLocation = nullptr;
7416 MDNode *AddressExpression = nullptr;
7417 if (ValueType == LocType::Assign) {
7418 // Parse DIAssignID.
7419 if (parseMDNode(N&: AssignID))
7420 return true;
7421 if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here"))
7422 return true;
7423
7424 // Parse address ValueAsMetadata.
7425 if (parseMetadata(MD&: AddressLocation, PFS: &PFS))
7426 return true;
7427 if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here"))
7428 return true;
7429
7430 // Parse address DIExpression.
7431 if (parseMDNode(N&: AddressExpression))
7432 return true;
7433 if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here"))
7434 return true;
7435 }
7436
7437 /// Parse DILocation.
7438 MDNode *DebugLoc;
7439 if (parseMDNode(N&: DebugLoc))
7440 return true;
7441
7442 if (parseToken(T: lltok::rparen, ErrMsg: "Expected ')' here"))
7443 return true;
7444 DR = DbgVariableRecord::createUnresolvedDbgVariableRecord(
7445 Type: ValueType, Val: ValLocMD, Variable, Expression, AssignID, Address: AddressLocation,
7446 AddressExpression, DI: DebugLoc);
7447 return false;
7448}
7449//===----------------------------------------------------------------------===//
7450// Instruction Parsing.
7451//===----------------------------------------------------------------------===//
7452
7453/// parseInstruction - parse one of the many different instructions.
7454///
7455int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB,
7456 PerFunctionState &PFS) {
7457 lltok::Kind Token = Lex.getKind();
7458 if (Token == lltok::Eof)
7459 return tokError(Msg: "found end of file when expecting more instructions");
7460 LocTy Loc = Lex.getLoc();
7461 unsigned KeywordVal = Lex.getUIntVal();
7462 Lex.Lex(); // Eat the keyword.
7463
7464 switch (Token) {
7465 default:
7466 return error(L: Loc, Msg: "expected instruction opcode");
7467 // Terminator Instructions.
7468 case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false;
7469 case lltok::kw_ret:
7470 return parseRet(Inst, BB, PFS);
7471 case lltok::kw_br:
7472 return parseBr(Inst, PFS);
7473 case lltok::kw_switch:
7474 return parseSwitch(Inst, PFS);
7475 case lltok::kw_indirectbr:
7476 return parseIndirectBr(Inst, PFS);
7477 case lltok::kw_invoke:
7478 return parseInvoke(Inst, PFS);
7479 case lltok::kw_resume:
7480 return parseResume(Inst, PFS);
7481 case lltok::kw_cleanupret:
7482 return parseCleanupRet(Inst, PFS);
7483 case lltok::kw_catchret:
7484 return parseCatchRet(Inst, PFS);
7485 case lltok::kw_catchswitch:
7486 return parseCatchSwitch(Inst, PFS);
7487 case lltok::kw_catchpad:
7488 return parseCatchPad(Inst, PFS);
7489 case lltok::kw_cleanuppad:
7490 return parseCleanupPad(Inst, PFS);
7491 case lltok::kw_callbr:
7492 return parseCallBr(Inst, PFS);
7493 // Unary Operators.
7494 case lltok::kw_fneg: {
7495 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7496 int Res = parseUnaryOp(Inst, PFS, Opc: KeywordVal, /*IsFP*/ true);
7497 if (Res != 0)
7498 return Res;
7499 if (FMF.any())
7500 Inst->setFastMathFlags(FMF);
7501 return false;
7502 }
7503 // Binary Operators.
7504 case lltok::kw_add:
7505 case lltok::kw_sub:
7506 case lltok::kw_mul:
7507 case lltok::kw_shl: {
7508 bool NUW = EatIfPresent(T: lltok::kw_nuw);
7509 bool NSW = EatIfPresent(T: lltok::kw_nsw);
7510 if (!NUW) NUW = EatIfPresent(T: lltok::kw_nuw);
7511
7512 if (parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ false))
7513 return true;
7514
7515 if (NUW) cast<BinaryOperator>(Val: Inst)->setHasNoUnsignedWrap(true);
7516 if (NSW) cast<BinaryOperator>(Val: Inst)->setHasNoSignedWrap(true);
7517 return false;
7518 }
7519 case lltok::kw_fadd:
7520 case lltok::kw_fsub:
7521 case lltok::kw_fmul:
7522 case lltok::kw_fdiv:
7523 case lltok::kw_frem: {
7524 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7525 int Res = parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ true);
7526 if (Res != 0)
7527 return Res;
7528 if (FMF.any())
7529 Inst->setFastMathFlags(FMF);
7530 return 0;
7531 }
7532
7533 case lltok::kw_sdiv:
7534 case lltok::kw_udiv:
7535 case lltok::kw_lshr:
7536 case lltok::kw_ashr: {
7537 bool Exact = EatIfPresent(T: lltok::kw_exact);
7538
7539 if (parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ false))
7540 return true;
7541 if (Exact) cast<BinaryOperator>(Val: Inst)->setIsExact(true);
7542 return false;
7543 }
7544
7545 case lltok::kw_urem:
7546 case lltok::kw_srem:
7547 return parseArithmetic(Inst, PFS, Opc: KeywordVal,
7548 /*IsFP*/ false);
7549 case lltok::kw_or: {
7550 bool Disjoint = EatIfPresent(T: lltok::kw_disjoint);
7551 if (parseLogical(Inst, PFS, Opc: KeywordVal))
7552 return true;
7553 if (Disjoint)
7554 cast<PossiblyDisjointInst>(Val: Inst)->setIsDisjoint(true);
7555 return false;
7556 }
7557 case lltok::kw_and:
7558 case lltok::kw_xor:
7559 return parseLogical(Inst, PFS, Opc: KeywordVal);
7560 case lltok::kw_icmp: {
7561 bool SameSign = EatIfPresent(T: lltok::kw_samesign);
7562 if (parseCompare(Inst, PFS, Opc: KeywordVal))
7563 return true;
7564 if (SameSign)
7565 cast<ICmpInst>(Val: Inst)->setSameSign();
7566 return false;
7567 }
7568 case lltok::kw_fcmp: {
7569 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7570 int Res = parseCompare(Inst, PFS, Opc: KeywordVal);
7571 if (Res != 0)
7572 return Res;
7573 if (FMF.any())
7574 Inst->setFastMathFlags(FMF);
7575 return 0;
7576 }
7577
7578 // Casts.
7579 case lltok::kw_uitofp:
7580 case lltok::kw_zext: {
7581 bool NonNeg = EatIfPresent(T: lltok::kw_nneg);
7582 bool Res = parseCast(Inst, PFS, Opc: KeywordVal);
7583 if (Res != 0)
7584 return Res;
7585 if (NonNeg)
7586 Inst->setNonNeg();
7587 return 0;
7588 }
7589 case lltok::kw_trunc: {
7590 bool NUW = EatIfPresent(T: lltok::kw_nuw);
7591 bool NSW = EatIfPresent(T: lltok::kw_nsw);
7592 if (!NUW)
7593 NUW = EatIfPresent(T: lltok::kw_nuw);
7594 if (parseCast(Inst, PFS, Opc: KeywordVal))
7595 return true;
7596 if (NUW)
7597 cast<TruncInst>(Val: Inst)->setHasNoUnsignedWrap(true);
7598 if (NSW)
7599 cast<TruncInst>(Val: Inst)->setHasNoSignedWrap(true);
7600 return false;
7601 }
7602 case lltok::kw_sext:
7603 case lltok::kw_bitcast:
7604 case lltok::kw_addrspacecast:
7605 case lltok::kw_sitofp:
7606 case lltok::kw_fptoui:
7607 case lltok::kw_fptosi:
7608 case lltok::kw_inttoptr:
7609 case lltok::kw_ptrtoaddr:
7610 case lltok::kw_ptrtoint:
7611 return parseCast(Inst, PFS, Opc: KeywordVal);
7612 case lltok::kw_fptrunc:
7613 case lltok::kw_fpext: {
7614 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7615 if (parseCast(Inst, PFS, Opc: KeywordVal))
7616 return true;
7617 if (FMF.any())
7618 Inst->setFastMathFlags(FMF);
7619 return false;
7620 }
7621
7622 // Other.
7623 case lltok::kw_select: {
7624 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7625 int Res = parseSelect(Inst, PFS);
7626 if (Res != 0)
7627 return Res;
7628 if (FMF.any()) {
7629 if (!isa<FPMathOperator>(Val: Inst))
7630 return error(L: Loc, Msg: "fast-math-flags specified for select without "
7631 "floating-point scalar or vector return type");
7632 Inst->setFastMathFlags(FMF);
7633 }
7634 return 0;
7635 }
7636 case lltok::kw_va_arg:
7637 return parseVAArg(Inst, PFS);
7638 case lltok::kw_extractelement:
7639 return parseExtractElement(Inst, PFS);
7640 case lltok::kw_insertelement:
7641 return parseInsertElement(Inst, PFS);
7642 case lltok::kw_shufflevector:
7643 return parseShuffleVector(Inst, PFS);
7644 case lltok::kw_phi: {
7645 FastMathFlags FMF = EatFastMathFlagsIfPresent();
7646 int Res = parsePHI(Inst, PFS);
7647 if (Res != 0)
7648 return Res;
7649 if (FMF.any()) {
7650 if (!isa<FPMathOperator>(Val: Inst))
7651 return error(L: Loc, Msg: "fast-math-flags specified for phi without "
7652 "floating-point scalar or vector return type");
7653 Inst->setFastMathFlags(FMF);
7654 }
7655 return 0;
7656 }
7657 case lltok::kw_landingpad:
7658 return parseLandingPad(Inst, PFS);
7659 case lltok::kw_freeze:
7660 return parseFreeze(I&: Inst, PFS);
7661 // Call.
7662 case lltok::kw_call:
7663 return parseCall(Inst, PFS, TCK: CallInst::TCK_None);
7664 case lltok::kw_tail:
7665 return parseCall(Inst, PFS, TCK: CallInst::TCK_Tail);
7666 case lltok::kw_musttail:
7667 return parseCall(Inst, PFS, TCK: CallInst::TCK_MustTail);
7668 case lltok::kw_notail:
7669 return parseCall(Inst, PFS, TCK: CallInst::TCK_NoTail);
7670 // Memory.
7671 case lltok::kw_alloca:
7672 return parseAlloc(Inst, PFS);
7673 case lltok::kw_load:
7674 return parseLoad(Inst, PFS);
7675 case lltok::kw_store:
7676 return parseStore(Inst, PFS);
7677 case lltok::kw_cmpxchg:
7678 return parseCmpXchg(Inst, PFS);
7679 case lltok::kw_atomicrmw:
7680 return parseAtomicRMW(Inst, PFS);
7681 case lltok::kw_fence:
7682 return parseFence(Inst, PFS);
7683 case lltok::kw_getelementptr:
7684 return parseGetElementPtr(Inst, PFS);
7685 case lltok::kw_extractvalue:
7686 return parseExtractValue(Inst, PFS);
7687 case lltok::kw_insertvalue:
7688 return parseInsertValue(Inst, PFS);
7689 }
7690}
7691
7692/// parseCmpPredicate - parse an integer or fp predicate, based on Kind.
7693bool LLParser::parseCmpPredicate(unsigned &P, unsigned Opc) {
7694 if (Opc == Instruction::FCmp) {
7695 switch (Lex.getKind()) {
7696 default:
7697 return tokError(Msg: "expected fcmp predicate (e.g. 'oeq')");
7698 case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break;
7699 case lltok::kw_one: P = CmpInst::FCMP_ONE; break;
7700 case lltok::kw_olt: P = CmpInst::FCMP_OLT; break;
7701 case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break;
7702 case lltok::kw_ole: P = CmpInst::FCMP_OLE; break;
7703 case lltok::kw_oge: P = CmpInst::FCMP_OGE; break;
7704 case lltok::kw_ord: P = CmpInst::FCMP_ORD; break;
7705 case lltok::kw_uno: P = CmpInst::FCMP_UNO; break;
7706 case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break;
7707 case lltok::kw_une: P = CmpInst::FCMP_UNE; break;
7708 case lltok::kw_ult: P = CmpInst::FCMP_ULT; break;
7709 case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break;
7710 case lltok::kw_ule: P = CmpInst::FCMP_ULE; break;
7711 case lltok::kw_uge: P = CmpInst::FCMP_UGE; break;
7712 case lltok::kw_true: P = CmpInst::FCMP_TRUE; break;
7713 case lltok::kw_false: P = CmpInst::FCMP_FALSE; break;
7714 }
7715 } else {
7716 switch (Lex.getKind()) {
7717 default:
7718 return tokError(Msg: "expected icmp predicate (e.g. 'eq')");
7719 case lltok::kw_eq: P = CmpInst::ICMP_EQ; break;
7720 case lltok::kw_ne: P = CmpInst::ICMP_NE; break;
7721 case lltok::kw_slt: P = CmpInst::ICMP_SLT; break;
7722 case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break;
7723 case lltok::kw_sle: P = CmpInst::ICMP_SLE; break;
7724 case lltok::kw_sge: P = CmpInst::ICMP_SGE; break;
7725 case lltok::kw_ult: P = CmpInst::ICMP_ULT; break;
7726 case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break;
7727 case lltok::kw_ule: P = CmpInst::ICMP_ULE; break;
7728 case lltok::kw_uge: P = CmpInst::ICMP_UGE; break;
7729 }
7730 }
7731 Lex.Lex();
7732 return false;
7733}
7734
7735//===----------------------------------------------------------------------===//
7736// Terminator Instructions.
7737//===----------------------------------------------------------------------===//
7738
7739/// parseRet - parse a return instruction.
7740/// ::= 'ret' void (',' !dbg, !1)*
7741/// ::= 'ret' TypeAndValue (',' !dbg, !1)*
7742bool LLParser::parseRet(Instruction *&Inst, BasicBlock *BB,
7743 PerFunctionState &PFS) {
7744 SMLoc TypeLoc = Lex.getLoc();
7745 Type *Ty = nullptr;
7746 if (parseType(Result&: Ty, AllowVoid: true /*void allowed*/))
7747 return true;
7748
7749 Type *ResType = PFS.getFunction().getReturnType();
7750
7751 if (Ty->isVoidTy()) {
7752 if (!ResType->isVoidTy())
7753 return error(L: TypeLoc, Msg: "value doesn't match function result type '" +
7754 getTypeString(T: ResType) + "'");
7755
7756 Inst = ReturnInst::Create(C&: Context);
7757 return false;
7758 }
7759
7760 Value *RV;
7761 if (parseValue(Ty, V&: RV, PFS))
7762 return true;
7763
7764 if (ResType != RV->getType())
7765 return error(L: TypeLoc, Msg: "value doesn't match function result type '" +
7766 getTypeString(T: ResType) + "'");
7767
7768 Inst = ReturnInst::Create(C&: Context, retVal: RV);
7769 return false;
7770}
7771
7772/// parseBr
7773/// ::= 'br' TypeAndValue
7774/// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
7775bool LLParser::parseBr(Instruction *&Inst, PerFunctionState &PFS) {
7776 LocTy Loc, Loc2;
7777 Value *Op0;
7778 BasicBlock *Op1, *Op2;
7779 if (parseTypeAndValue(V&: Op0, Loc, PFS))
7780 return true;
7781
7782 if (BasicBlock *BB = dyn_cast<BasicBlock>(Val: Op0)) {
7783 Inst = BranchInst::Create(IfTrue: BB);
7784 return false;
7785 }
7786
7787 if (Op0->getType() != Type::getInt1Ty(C&: Context))
7788 return error(L: Loc, Msg: "branch condition must have 'i1' type");
7789
7790 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' after branch condition") ||
7791 parseTypeAndBasicBlock(BB&: Op1, Loc, PFS) ||
7792 parseToken(T: lltok::comma, ErrMsg: "expected ',' after true destination") ||
7793 parseTypeAndBasicBlock(BB&: Op2, Loc&: Loc2, PFS))
7794 return true;
7795
7796 Inst = BranchInst::Create(IfTrue: Op1, IfFalse: Op2, Cond: Op0);
7797 return false;
7798}
7799
7800/// parseSwitch
7801/// Instruction
7802/// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']'
7803/// JumpTable
7804/// ::= (TypeAndValue ',' TypeAndValue)*
7805bool LLParser::parseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
7806 LocTy CondLoc, BBLoc;
7807 Value *Cond;
7808 BasicBlock *DefaultBB;
7809 if (parseTypeAndValue(V&: Cond, Loc&: CondLoc, PFS) ||
7810 parseToken(T: lltok::comma, ErrMsg: "expected ',' after switch condition") ||
7811 parseTypeAndBasicBlock(BB&: DefaultBB, Loc&: BBLoc, PFS) ||
7812 parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with switch table"))
7813 return true;
7814
7815 if (!Cond->getType()->isIntegerTy())
7816 return error(L: CondLoc, Msg: "switch condition must have integer type");
7817
7818 // parse the jump table pairs.
7819 SmallPtrSet<Value*, 32> SeenCases;
7820 SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table;
7821 while (Lex.getKind() != lltok::rsquare) {
7822 Value *Constant;
7823 BasicBlock *DestBB;
7824
7825 if (parseTypeAndValue(V&: Constant, Loc&: CondLoc, PFS) ||
7826 parseToken(T: lltok::comma, ErrMsg: "expected ',' after case value") ||
7827 parseTypeAndBasicBlock(BB&: DestBB, PFS))
7828 return true;
7829
7830 if (!SeenCases.insert(Ptr: Constant).second)
7831 return error(L: CondLoc, Msg: "duplicate case value in switch");
7832 if (!isa<ConstantInt>(Val: Constant))
7833 return error(L: CondLoc, Msg: "case value is not a constant integer");
7834
7835 Table.push_back(Elt: std::make_pair(x: cast<ConstantInt>(Val: Constant), y&: DestBB));
7836 }
7837
7838 Lex.Lex(); // Eat the ']'.
7839
7840 SwitchInst *SI = SwitchInst::Create(Value: Cond, Default: DefaultBB, NumCases: Table.size());
7841 for (const auto &[OnVal, Dest] : Table)
7842 SI->addCase(OnVal, Dest);
7843 Inst = SI;
7844 return false;
7845}
7846
7847/// parseIndirectBr
7848/// Instruction
7849/// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']'
7850bool LLParser::parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) {
7851 LocTy AddrLoc;
7852 Value *Address;
7853 if (parseTypeAndValue(V&: Address, Loc&: AddrLoc, PFS) ||
7854 parseToken(T: lltok::comma, ErrMsg: "expected ',' after indirectbr address") ||
7855 parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with indirectbr"))
7856 return true;
7857
7858 if (!Address->getType()->isPointerTy())
7859 return error(L: AddrLoc, Msg: "indirectbr address must have pointer type");
7860
7861 // parse the destination list.
7862 SmallVector<BasicBlock*, 16> DestList;
7863
7864 if (Lex.getKind() != lltok::rsquare) {
7865 BasicBlock *DestBB;
7866 if (parseTypeAndBasicBlock(BB&: DestBB, PFS))
7867 return true;
7868 DestList.push_back(Elt: DestBB);
7869
7870 while (EatIfPresent(T: lltok::comma)) {
7871 if (parseTypeAndBasicBlock(BB&: DestBB, PFS))
7872 return true;
7873 DestList.push_back(Elt: DestBB);
7874 }
7875 }
7876
7877 if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' at end of block list"))
7878 return true;
7879
7880 IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests: DestList.size());
7881 for (BasicBlock *Dest : DestList)
7882 IBI->addDestination(Dest);
7883 Inst = IBI;
7884 return false;
7885}
7886
7887// If RetType is a non-function pointer type, then this is the short syntax
7888// for the call, which means that RetType is just the return type. Infer the
7889// rest of the function argument types from the arguments that are present.
7890bool LLParser::resolveFunctionType(Type *RetType, ArrayRef<ParamInfo> ArgList,
7891 FunctionType *&FuncTy) {
7892 FuncTy = dyn_cast<FunctionType>(Val: RetType);
7893 if (!FuncTy) {
7894 // Pull out the types of all of the arguments...
7895 SmallVector<Type *, 8> ParamTypes;
7896 ParamTypes.reserve(N: ArgList.size());
7897 for (const ParamInfo &Arg : ArgList)
7898 ParamTypes.push_back(Elt: Arg.V->getType());
7899
7900 if (!FunctionType::isValidReturnType(RetTy: RetType))
7901 return true;
7902
7903 FuncTy = FunctionType::get(Result: RetType, Params: ParamTypes, isVarArg: false);
7904 }
7905 return false;
7906}
7907
7908/// parseInvoke
7909/// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
7910/// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
7911bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
7912 LocTy CallLoc = Lex.getLoc();
7913 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
7914 std::vector<unsigned> FwdRefAttrGrps;
7915 LocTy NoBuiltinLoc;
7916 unsigned CC;
7917 unsigned InvokeAddrSpace;
7918 Type *RetType = nullptr;
7919 LocTy RetTypeLoc;
7920 ValID CalleeID;
7921 SmallVector<ParamInfo, 16> ArgList;
7922 SmallVector<OperandBundleDef, 2> BundleList;
7923
7924 BasicBlock *NormalBB, *UnwindBB;
7925 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) ||
7926 parseOptionalProgramAddrSpace(AddrSpace&: InvokeAddrSpace) ||
7927 parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) ||
7928 parseValID(ID&: CalleeID, PFS: &PFS) || parseParameterList(ArgList, PFS) ||
7929 parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false,
7930 BuiltinLoc&: NoBuiltinLoc) ||
7931 parseOptionalOperandBundles(BundleList, PFS) ||
7932 parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in invoke") ||
7933 parseTypeAndBasicBlock(BB&: NormalBB, PFS) ||
7934 parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' in invoke") ||
7935 parseTypeAndBasicBlock(BB&: UnwindBB, PFS))
7936 return true;
7937
7938 // If RetType is a non-function pointer type, then this is the short syntax
7939 // for the call, which means that RetType is just the return type. Infer the
7940 // rest of the function argument types from the arguments that are present.
7941 FunctionType *Ty;
7942 if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty))
7943 return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function");
7944
7945 CalleeID.FTy = Ty;
7946
7947 // Look up the callee.
7948 Value *Callee;
7949 if (convertValIDToValue(Ty: PointerType::get(C&: Context, AddressSpace: InvokeAddrSpace), ID&: CalleeID,
7950 V&: Callee, PFS: &PFS))
7951 return true;
7952
7953 // Set up the Attribute for the function.
7954 SmallVector<Value *, 8> Args;
7955 SmallVector<AttributeSet, 8> ArgAttrs;
7956
7957 // Loop through FunctionType's arguments and ensure they are specified
7958 // correctly. Also, gather any parameter attributes.
7959 FunctionType::param_iterator I = Ty->param_begin();
7960 FunctionType::param_iterator E = Ty->param_end();
7961 for (const ParamInfo &Arg : ArgList) {
7962 Type *ExpectedTy = nullptr;
7963 if (I != E) {
7964 ExpectedTy = *I++;
7965 } else if (!Ty->isVarArg()) {
7966 return error(L: Arg.Loc, Msg: "too many arguments specified");
7967 }
7968
7969 if (ExpectedTy && ExpectedTy != Arg.V->getType())
7970 return error(L: Arg.Loc, Msg: "argument is not of expected type '" +
7971 getTypeString(T: ExpectedTy) + "'");
7972 Args.push_back(Elt: Arg.V);
7973 ArgAttrs.push_back(Elt: Arg.Attrs);
7974 }
7975
7976 if (I != E)
7977 return error(L: CallLoc, Msg: "not enough parameters specified for call");
7978
7979 // Finish off the Attribute and check them
7980 AttributeList PAL =
7981 AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs),
7982 RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs);
7983
7984 InvokeInst *II =
7985 InvokeInst::Create(Ty, Func: Callee, IfNormal: NormalBB, IfException: UnwindBB, Args, Bundles: BundleList);
7986 II->setCallingConv(CC);
7987 II->setAttributes(PAL);
7988 ForwardRefAttrGroups[II] = FwdRefAttrGrps;
7989 Inst = II;
7990 return false;
7991}
7992
7993/// parseResume
7994/// ::= 'resume' TypeAndValue
7995bool LLParser::parseResume(Instruction *&Inst, PerFunctionState &PFS) {
7996 Value *Exn; LocTy ExnLoc;
7997 if (parseTypeAndValue(V&: Exn, Loc&: ExnLoc, PFS))
7998 return true;
7999
8000 ResumeInst *RI = ResumeInst::Create(Exn);
8001 Inst = RI;
8002 return false;
8003}
8004
8005bool LLParser::parseExceptionArgs(SmallVectorImpl<Value *> &Args,
8006 PerFunctionState &PFS) {
8007 if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in catchpad/cleanuppad"))
8008 return true;
8009
8010 while (Lex.getKind() != lltok::rsquare) {
8011 // If this isn't the first argument, we need a comma.
8012 if (!Args.empty() &&
8013 parseToken(T: lltok::comma, ErrMsg: "expected ',' in argument list"))
8014 return true;
8015
8016 // parse the argument.
8017 LocTy ArgLoc;
8018 Type *ArgTy = nullptr;
8019 if (parseType(Result&: ArgTy, Loc&: ArgLoc))
8020 return true;
8021
8022 Value *V;
8023 if (ArgTy->isMetadataTy()) {
8024 if (parseMetadataAsValue(V, PFS))
8025 return true;
8026 } else {
8027 if (parseValue(Ty: ArgTy, V, PFS))
8028 return true;
8029 }
8030 Args.push_back(Elt: V);
8031 }
8032
8033 Lex.Lex(); // Lex the ']'.
8034 return false;
8035}
8036
8037/// parseCleanupRet
8038/// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue)
8039bool LLParser::parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) {
8040 Value *CleanupPad = nullptr;
8041
8042 if (parseToken(T: lltok::kw_from, ErrMsg: "expected 'from' after cleanupret"))
8043 return true;
8044
8045 if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CleanupPad, PFS))
8046 return true;
8047
8048 if (parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' in cleanupret"))
8049 return true;
8050
8051 BasicBlock *UnwindBB = nullptr;
8052 if (Lex.getKind() == lltok::kw_to) {
8053 Lex.Lex();
8054 if (parseToken(T: lltok::kw_caller, ErrMsg: "expected 'caller' in cleanupret"))
8055 return true;
8056 } else {
8057 if (parseTypeAndBasicBlock(BB&: UnwindBB, PFS)) {
8058 return true;
8059 }
8060 }
8061
8062 Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB);
8063 return false;
8064}
8065
8066/// parseCatchRet
8067/// ::= 'catchret' from Parent Value 'to' TypeAndValue
8068bool LLParser::parseCatchRet(Instruction *&Inst, PerFunctionState &PFS) {
8069 Value *CatchPad = nullptr;
8070
8071 if (parseToken(T: lltok::kw_from, ErrMsg: "expected 'from' after catchret"))
8072 return true;
8073
8074 if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CatchPad, PFS))
8075 return true;
8076
8077 BasicBlock *BB;
8078 if (parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in catchret") ||
8079 parseTypeAndBasicBlock(BB, PFS))
8080 return true;
8081
8082 Inst = CatchReturnInst::Create(CatchPad, BB);
8083 return false;
8084}
8085
8086/// parseCatchSwitch
8087/// ::= 'catchswitch' within Parent
8088bool LLParser::parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) {
8089 Value *ParentPad;
8090
8091 if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after catchswitch"))
8092 return true;
8093
8094 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
8095 Lex.getKind() != lltok::LocalVarID)
8096 return tokError(Msg: "expected scope value for catchswitch");
8097
8098 if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: ParentPad, PFS))
8099 return true;
8100
8101 if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with catchswitch labels"))
8102 return true;
8103
8104 SmallVector<BasicBlock *, 32> Table;
8105 do {
8106 BasicBlock *DestBB;
8107 if (parseTypeAndBasicBlock(BB&: DestBB, PFS))
8108 return true;
8109 Table.push_back(Elt: DestBB);
8110 } while (EatIfPresent(T: lltok::comma));
8111
8112 if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' after catchswitch labels"))
8113 return true;
8114
8115 if (parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' after catchswitch scope"))
8116 return true;
8117
8118 BasicBlock *UnwindBB = nullptr;
8119 if (EatIfPresent(T: lltok::kw_to)) {
8120 if (parseToken(T: lltok::kw_caller, ErrMsg: "expected 'caller' in catchswitch"))
8121 return true;
8122 } else {
8123 if (parseTypeAndBasicBlock(BB&: UnwindBB, PFS))
8124 return true;
8125 }
8126
8127 auto *CatchSwitch =
8128 CatchSwitchInst::Create(ParentPad, UnwindDest: UnwindBB, NumHandlers: Table.size());
8129 for (BasicBlock *DestBB : Table)
8130 CatchSwitch->addHandler(Dest: DestBB);
8131 Inst = CatchSwitch;
8132 return false;
8133}
8134
8135/// parseCatchPad
8136/// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue
8137bool LLParser::parseCatchPad(Instruction *&Inst, PerFunctionState &PFS) {
8138 Value *CatchSwitch = nullptr;
8139
8140 if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after catchpad"))
8141 return true;
8142
8143 if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID)
8144 return tokError(Msg: "expected scope value for catchpad");
8145
8146 if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CatchSwitch, PFS))
8147 return true;
8148
8149 SmallVector<Value *, 8> Args;
8150 if (parseExceptionArgs(Args, PFS))
8151 return true;
8152
8153 Inst = CatchPadInst::Create(CatchSwitch, Args);
8154 return false;
8155}
8156
8157/// parseCleanupPad
8158/// ::= 'cleanuppad' within Parent ParamList
8159bool LLParser::parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
8160 Value *ParentPad = nullptr;
8161
8162 if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after cleanuppad"))
8163 return true;
8164
8165 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
8166 Lex.getKind() != lltok::LocalVarID)
8167 return tokError(Msg: "expected scope value for cleanuppad");
8168
8169 if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: ParentPad, PFS))
8170 return true;
8171
8172 SmallVector<Value *, 8> Args;
8173 if (parseExceptionArgs(Args, PFS))
8174 return true;
8175
8176 Inst = CleanupPadInst::Create(ParentPad, Args);
8177 return false;
8178}
8179
8180//===----------------------------------------------------------------------===//
8181// Unary Operators.
8182//===----------------------------------------------------------------------===//
8183
8184/// parseUnaryOp
8185/// ::= UnaryOp TypeAndValue ',' Value
8186///
8187/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
8188/// operand is allowed.
8189bool LLParser::parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS,
8190 unsigned Opc, bool IsFP) {
8191 LocTy Loc; Value *LHS;
8192 if (parseTypeAndValue(V&: LHS, Loc, PFS))
8193 return true;
8194
8195 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
8196 : LHS->getType()->isIntOrIntVectorTy();
8197
8198 if (!Valid)
8199 return error(L: Loc, Msg: "invalid operand type for instruction");
8200
8201 Inst = UnaryOperator::Create(Op: (Instruction::UnaryOps)Opc, S: LHS);
8202 return false;
8203}
8204
8205/// parseCallBr
8206/// ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList
8207/// OptionalAttrs OptionalOperandBundles 'to' TypeAndValue
8208/// '[' LabelList ']'
8209bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) {
8210 LocTy CallLoc = Lex.getLoc();
8211 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
8212 std::vector<unsigned> FwdRefAttrGrps;
8213 LocTy NoBuiltinLoc;
8214 unsigned CC;
8215 Type *RetType = nullptr;
8216 LocTy RetTypeLoc;
8217 ValID CalleeID;
8218 SmallVector<ParamInfo, 16> ArgList;
8219 SmallVector<OperandBundleDef, 2> BundleList;
8220
8221 BasicBlock *DefaultDest;
8222 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) ||
8223 parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) ||
8224 parseValID(ID&: CalleeID, PFS: &PFS) || parseParameterList(ArgList, PFS) ||
8225 parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false,
8226 BuiltinLoc&: NoBuiltinLoc) ||
8227 parseOptionalOperandBundles(BundleList, PFS) ||
8228 parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in callbr") ||
8229 parseTypeAndBasicBlock(BB&: DefaultDest, PFS) ||
8230 parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in callbr"))
8231 return true;
8232
8233 // parse the destination list.
8234 SmallVector<BasicBlock *, 16> IndirectDests;
8235
8236 if (Lex.getKind() != lltok::rsquare) {
8237 BasicBlock *DestBB;
8238 if (parseTypeAndBasicBlock(BB&: DestBB, PFS))
8239 return true;
8240 IndirectDests.push_back(Elt: DestBB);
8241
8242 while (EatIfPresent(T: lltok::comma)) {
8243 if (parseTypeAndBasicBlock(BB&: DestBB, PFS))
8244 return true;
8245 IndirectDests.push_back(Elt: DestBB);
8246 }
8247 }
8248
8249 if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' at end of block list"))
8250 return true;
8251
8252 // If RetType is a non-function pointer type, then this is the short syntax
8253 // for the call, which means that RetType is just the return type. Infer the
8254 // rest of the function argument types from the arguments that are present.
8255 FunctionType *Ty;
8256 if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty))
8257 return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function");
8258
8259 CalleeID.FTy = Ty;
8260
8261 // Look up the callee.
8262 Value *Callee;
8263 if (convertValIDToValue(Ty: PointerType::getUnqual(C&: Context), ID&: CalleeID, V&: Callee,
8264 PFS: &PFS))
8265 return true;
8266
8267 // Set up the Attribute for the function.
8268 SmallVector<Value *, 8> Args;
8269 SmallVector<AttributeSet, 8> ArgAttrs;
8270
8271 // Loop through FunctionType's arguments and ensure they are specified
8272 // correctly. Also, gather any parameter attributes.
8273 FunctionType::param_iterator I = Ty->param_begin();
8274 FunctionType::param_iterator E = Ty->param_end();
8275 for (const ParamInfo &Arg : ArgList) {
8276 Type *ExpectedTy = nullptr;
8277 if (I != E) {
8278 ExpectedTy = *I++;
8279 } else if (!Ty->isVarArg()) {
8280 return error(L: Arg.Loc, Msg: "too many arguments specified");
8281 }
8282
8283 if (ExpectedTy && ExpectedTy != Arg.V->getType())
8284 return error(L: Arg.Loc, Msg: "argument is not of expected type '" +
8285 getTypeString(T: ExpectedTy) + "'");
8286 Args.push_back(Elt: Arg.V);
8287 ArgAttrs.push_back(Elt: Arg.Attrs);
8288 }
8289
8290 if (I != E)
8291 return error(L: CallLoc, Msg: "not enough parameters specified for call");
8292
8293 // Finish off the Attribute and check them
8294 AttributeList PAL =
8295 AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs),
8296 RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs);
8297
8298 CallBrInst *CBI =
8299 CallBrInst::Create(Ty, Func: Callee, DefaultDest, IndirectDests, Args,
8300 Bundles: BundleList);
8301 CBI->setCallingConv(CC);
8302 CBI->setAttributes(PAL);
8303 ForwardRefAttrGroups[CBI] = FwdRefAttrGrps;
8304 Inst = CBI;
8305 return false;
8306}
8307
8308//===----------------------------------------------------------------------===//
8309// Binary Operators.
8310//===----------------------------------------------------------------------===//
8311
8312/// parseArithmetic
8313/// ::= ArithmeticOps TypeAndValue ',' Value
8314///
8315/// If IsFP is false, then any integer operand is allowed, if it is true, any fp
8316/// operand is allowed.
8317bool LLParser::parseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
8318 unsigned Opc, bool IsFP) {
8319 LocTy Loc; Value *LHS, *RHS;
8320 if (parseTypeAndValue(V&: LHS, Loc, PFS) ||
8321 parseToken(T: lltok::comma, ErrMsg: "expected ',' in arithmetic operation") ||
8322 parseValue(Ty: LHS->getType(), V&: RHS, PFS))
8323 return true;
8324
8325 bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy()
8326 : LHS->getType()->isIntOrIntVectorTy();
8327
8328 if (!Valid)
8329 return error(L: Loc, Msg: "invalid operand type for instruction");
8330
8331 Inst = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS);
8332 return false;
8333}
8334
8335/// parseLogical
8336/// ::= ArithmeticOps TypeAndValue ',' Value {
8337bool LLParser::parseLogical(Instruction *&Inst, PerFunctionState &PFS,
8338 unsigned Opc) {
8339 LocTy Loc; Value *LHS, *RHS;
8340 if (parseTypeAndValue(V&: LHS, Loc, PFS) ||
8341 parseToken(T: lltok::comma, ErrMsg: "expected ',' in logical operation") ||
8342 parseValue(Ty: LHS->getType(), V&: RHS, PFS))
8343 return true;
8344
8345 if (!LHS->getType()->isIntOrIntVectorTy())
8346 return error(L: Loc,
8347 Msg: "instruction requires integer or integer vector operands");
8348
8349 Inst = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS);
8350 return false;
8351}
8352
8353/// parseCompare
8354/// ::= 'icmp' IPredicates TypeAndValue ',' Value
8355/// ::= 'fcmp' FPredicates TypeAndValue ',' Value
8356bool LLParser::parseCompare(Instruction *&Inst, PerFunctionState &PFS,
8357 unsigned Opc) {
8358 // parse the integer/fp comparison predicate.
8359 LocTy Loc;
8360 unsigned Pred;
8361 Value *LHS, *RHS;
8362 if (parseCmpPredicate(P&: Pred, Opc) || parseTypeAndValue(V&: LHS, Loc, PFS) ||
8363 parseToken(T: lltok::comma, ErrMsg: "expected ',' after compare value") ||
8364 parseValue(Ty: LHS->getType(), V&: RHS, PFS))
8365 return true;
8366
8367 if (Opc == Instruction::FCmp) {
8368 if (!LHS->getType()->isFPOrFPVectorTy())
8369 return error(L: Loc, Msg: "fcmp requires floating point operands");
8370 Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS);
8371 } else {
8372 assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!");
8373 if (!LHS->getType()->isIntOrIntVectorTy() &&
8374 !LHS->getType()->isPtrOrPtrVectorTy())
8375 return error(L: Loc, Msg: "icmp requires integer operands");
8376 Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS);
8377 }
8378 return false;
8379}
8380
8381//===----------------------------------------------------------------------===//
8382// Other Instructions.
8383//===----------------------------------------------------------------------===//
8384
8385/// parseCast
8386/// ::= CastOpc TypeAndValue 'to' Type
8387bool LLParser::parseCast(Instruction *&Inst, PerFunctionState &PFS,
8388 unsigned Opc) {
8389 LocTy Loc;
8390 Value *Op;
8391 Type *DestTy = nullptr;
8392 if (parseTypeAndValue(V&: Op, Loc, PFS) ||
8393 parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' after cast value") ||
8394 parseType(Result&: DestTy))
8395 return true;
8396
8397 if (!CastInst::castIsValid(op: (Instruction::CastOps)Opc, S: Op, DstTy: DestTy))
8398 return error(L: Loc, Msg: "invalid cast opcode for cast from '" +
8399 getTypeString(T: Op->getType()) + "' to '" +
8400 getTypeString(T: DestTy) + "'");
8401 Inst = CastInst::Create((Instruction::CastOps)Opc, S: Op, Ty: DestTy);
8402 return false;
8403}
8404
8405/// parseSelect
8406/// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8407bool LLParser::parseSelect(Instruction *&Inst, PerFunctionState &PFS) {
8408 LocTy Loc;
8409 Value *Op0, *Op1, *Op2;
8410 if (parseTypeAndValue(V&: Op0, Loc, PFS) ||
8411 parseToken(T: lltok::comma, ErrMsg: "expected ',' after select condition") ||
8412 parseTypeAndValue(V&: Op1, PFS) ||
8413 parseToken(T: lltok::comma, ErrMsg: "expected ',' after select value") ||
8414 parseTypeAndValue(V&: Op2, PFS))
8415 return true;
8416
8417 if (const char *Reason = SelectInst::areInvalidOperands(Cond: Op0, True: Op1, False: Op2))
8418 return error(L: Loc, Msg: Reason);
8419
8420 Inst = SelectInst::Create(C: Op0, S1: Op1, S2: Op2);
8421 return false;
8422}
8423
8424/// parseVAArg
8425/// ::= 'va_arg' TypeAndValue ',' Type
8426bool LLParser::parseVAArg(Instruction *&Inst, PerFunctionState &PFS) {
8427 Value *Op;
8428 Type *EltTy = nullptr;
8429 LocTy TypeLoc;
8430 if (parseTypeAndValue(V&: Op, PFS) ||
8431 parseToken(T: lltok::comma, ErrMsg: "expected ',' after vaarg operand") ||
8432 parseType(Result&: EltTy, Loc&: TypeLoc))
8433 return true;
8434
8435 if (!EltTy->isFirstClassType())
8436 return error(L: TypeLoc, Msg: "va_arg requires operand with first class type");
8437
8438 Inst = new VAArgInst(Op, EltTy);
8439 return false;
8440}
8441
8442/// parseExtractElement
8443/// ::= 'extractelement' TypeAndValue ',' TypeAndValue
8444bool LLParser::parseExtractElement(Instruction *&Inst, PerFunctionState &PFS) {
8445 LocTy Loc;
8446 Value *Op0, *Op1;
8447 if (parseTypeAndValue(V&: Op0, Loc, PFS) ||
8448 parseToken(T: lltok::comma, ErrMsg: "expected ',' after extract value") ||
8449 parseTypeAndValue(V&: Op1, PFS))
8450 return true;
8451
8452 if (!ExtractElementInst::isValidOperands(Vec: Op0, Idx: Op1))
8453 return error(L: Loc, Msg: "invalid extractelement operands");
8454
8455 Inst = ExtractElementInst::Create(Vec: Op0, Idx: Op1);
8456 return false;
8457}
8458
8459/// parseInsertElement
8460/// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8461bool LLParser::parseInsertElement(Instruction *&Inst, PerFunctionState &PFS) {
8462 LocTy Loc;
8463 Value *Op0, *Op1, *Op2;
8464 if (parseTypeAndValue(V&: Op0, Loc, PFS) ||
8465 parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value") ||
8466 parseTypeAndValue(V&: Op1, PFS) ||
8467 parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value") ||
8468 parseTypeAndValue(V&: Op2, PFS))
8469 return true;
8470
8471 if (!InsertElementInst::isValidOperands(Vec: Op0, NewElt: Op1, Idx: Op2))
8472 return error(L: Loc, Msg: "invalid insertelement operands");
8473
8474 Inst = InsertElementInst::Create(Vec: Op0, NewElt: Op1, Idx: Op2);
8475 return false;
8476}
8477
8478/// parseShuffleVector
8479/// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue
8480bool LLParser::parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) {
8481 LocTy Loc;
8482 Value *Op0, *Op1, *Op2;
8483 if (parseTypeAndValue(V&: Op0, Loc, PFS) ||
8484 parseToken(T: lltok::comma, ErrMsg: "expected ',' after shuffle mask") ||
8485 parseTypeAndValue(V&: Op1, PFS) ||
8486 parseToken(T: lltok::comma, ErrMsg: "expected ',' after shuffle value") ||
8487 parseTypeAndValue(V&: Op2, PFS))
8488 return true;
8489
8490 if (!ShuffleVectorInst::isValidOperands(V1: Op0, V2: Op1, Mask: Op2))
8491 return error(L: Loc, Msg: "invalid shufflevector operands");
8492
8493 Inst = new ShuffleVectorInst(Op0, Op1, Op2);
8494 return false;
8495}
8496
8497/// parsePHI
8498/// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')*
8499int LLParser::parsePHI(Instruction *&Inst, PerFunctionState &PFS) {
8500 Type *Ty = nullptr; LocTy TypeLoc;
8501 Value *Op0, *Op1;
8502
8503 if (parseType(Result&: Ty, Loc&: TypeLoc))
8504 return true;
8505
8506 if (!Ty->isFirstClassType())
8507 return error(L: TypeLoc, Msg: "phi node must have first class type");
8508
8509 bool First = true;
8510 bool AteExtraComma = false;
8511 SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals;
8512
8513 while (true) {
8514 if (First) {
8515 if (Lex.getKind() != lltok::lsquare)
8516 break;
8517 First = false;
8518 } else if (!EatIfPresent(T: lltok::comma))
8519 break;
8520
8521 if (Lex.getKind() == lltok::MetadataVar) {
8522 AteExtraComma = true;
8523 break;
8524 }
8525
8526 if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in phi value list") ||
8527 parseValue(Ty, V&: Op0, PFS) ||
8528 parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value") ||
8529 parseValue(Ty: Type::getLabelTy(C&: Context), V&: Op1, PFS) ||
8530 parseToken(T: lltok::rsquare, ErrMsg: "expected ']' in phi value list"))
8531 return true;
8532
8533 PHIVals.push_back(Elt: std::make_pair(x&: Op0, y: cast<BasicBlock>(Val: Op1)));
8534 }
8535
8536 PHINode *PN = PHINode::Create(Ty, NumReservedValues: PHIVals.size());
8537 for (const auto &[Val, BB] : PHIVals)
8538 PN->addIncoming(V: Val, BB);
8539 Inst = PN;
8540 return AteExtraComma ? InstExtraComma : InstNormal;
8541}
8542
8543/// parseLandingPad
8544/// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
8545/// Clause
8546/// ::= 'catch' TypeAndValue
8547/// ::= 'filter'
8548/// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
8549bool LLParser::parseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
8550 Type *Ty = nullptr; LocTy TyLoc;
8551
8552 if (parseType(Result&: Ty, Loc&: TyLoc))
8553 return true;
8554
8555 std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(RetTy: Ty, NumReservedClauses: 0));
8556 LP->setCleanup(EatIfPresent(T: lltok::kw_cleanup));
8557
8558 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
8559 LandingPadInst::ClauseType CT;
8560 if (EatIfPresent(T: lltok::kw_catch))
8561 CT = LandingPadInst::Catch;
8562 else if (EatIfPresent(T: lltok::kw_filter))
8563 CT = LandingPadInst::Filter;
8564 else
8565 return tokError(Msg: "expected 'catch' or 'filter' clause type");
8566
8567 Value *V;
8568 LocTy VLoc;
8569 if (parseTypeAndValue(V, Loc&: VLoc, PFS))
8570 return true;
8571
8572 // A 'catch' type expects a non-array constant. A filter clause expects an
8573 // array constant.
8574 if (CT == LandingPadInst::Catch) {
8575 if (isa<ArrayType>(Val: V->getType()))
8576 return error(L: VLoc, Msg: "'catch' clause has an invalid type");
8577 } else {
8578 if (!isa<ArrayType>(Val: V->getType()))
8579 return error(L: VLoc, Msg: "'filter' clause has an invalid type");
8580 }
8581
8582 Constant *CV = dyn_cast<Constant>(Val: V);
8583 if (!CV)
8584 return error(L: VLoc, Msg: "clause argument must be a constant");
8585 LP->addClause(ClauseVal: CV);
8586 }
8587
8588 Inst = LP.release();
8589 return false;
8590}
8591
8592/// parseFreeze
8593/// ::= 'freeze' Type Value
8594bool LLParser::parseFreeze(Instruction *&Inst, PerFunctionState &PFS) {
8595 LocTy Loc;
8596 Value *Op;
8597 if (parseTypeAndValue(V&: Op, Loc, PFS))
8598 return true;
8599
8600 Inst = new FreezeInst(Op);
8601 return false;
8602}
8603
8604/// parseCall
8605/// ::= 'call' OptionalFastMathFlags OptionalCallingConv
8606/// OptionalAttrs Type Value ParameterList OptionalAttrs
8607/// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv
8608/// OptionalAttrs Type Value ParameterList OptionalAttrs
8609/// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv
8610/// OptionalAttrs Type Value ParameterList OptionalAttrs
8611/// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv
8612/// OptionalAttrs Type Value ParameterList OptionalAttrs
8613bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
8614 CallInst::TailCallKind TCK) {
8615 AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext());
8616 std::vector<unsigned> FwdRefAttrGrps;
8617 LocTy BuiltinLoc;
8618 unsigned CallAddrSpace;
8619 unsigned CC;
8620 Type *RetType = nullptr;
8621 LocTy RetTypeLoc;
8622 ValID CalleeID;
8623 SmallVector<ParamInfo, 16> ArgList;
8624 SmallVector<OperandBundleDef, 2> BundleList;
8625 LocTy CallLoc = Lex.getLoc();
8626
8627 if (TCK != CallInst::TCK_None &&
8628 parseToken(T: lltok::kw_call,
8629 ErrMsg: "expected 'tail call', 'musttail call', or 'notail call'"))
8630 return true;
8631
8632 FastMathFlags FMF = EatFastMathFlagsIfPresent();
8633
8634 if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) ||
8635 parseOptionalProgramAddrSpace(AddrSpace&: CallAddrSpace) ||
8636 parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) ||
8637 parseValID(ID&: CalleeID, PFS: &PFS) ||
8638 parseParameterList(ArgList, PFS, IsMustTailCall: TCK == CallInst::TCK_MustTail,
8639 InVarArgsFunc: PFS.getFunction().isVarArg()) ||
8640 parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false, BuiltinLoc) ||
8641 parseOptionalOperandBundles(BundleList, PFS))
8642 return true;
8643
8644 // If RetType is a non-function pointer type, then this is the short syntax
8645 // for the call, which means that RetType is just the return type. Infer the
8646 // rest of the function argument types from the arguments that are present.
8647 FunctionType *Ty;
8648 if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty))
8649 return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function");
8650
8651 CalleeID.FTy = Ty;
8652
8653 // Look up the callee.
8654 Value *Callee;
8655 if (convertValIDToValue(Ty: PointerType::get(C&: Context, AddressSpace: CallAddrSpace), ID&: CalleeID,
8656 V&: Callee, PFS: &PFS))
8657 return true;
8658
8659 // Set up the Attribute for the function.
8660 SmallVector<AttributeSet, 8> Attrs;
8661
8662 SmallVector<Value*, 8> Args;
8663
8664 // Loop through FunctionType's arguments and ensure they are specified
8665 // correctly. Also, gather any parameter attributes.
8666 FunctionType::param_iterator I = Ty->param_begin();
8667 FunctionType::param_iterator E = Ty->param_end();
8668 for (const ParamInfo &Arg : ArgList) {
8669 Type *ExpectedTy = nullptr;
8670 if (I != E) {
8671 ExpectedTy = *I++;
8672 } else if (!Ty->isVarArg()) {
8673 return error(L: Arg.Loc, Msg: "too many arguments specified");
8674 }
8675
8676 if (ExpectedTy && ExpectedTy != Arg.V->getType())
8677 return error(L: Arg.Loc, Msg: "argument is not of expected type '" +
8678 getTypeString(T: ExpectedTy) + "'");
8679 Args.push_back(Elt: Arg.V);
8680 Attrs.push_back(Elt: Arg.Attrs);
8681 }
8682
8683 if (I != E)
8684 return error(L: CallLoc, Msg: "not enough parameters specified for call");
8685
8686 // Finish off the Attribute and check them
8687 AttributeList PAL =
8688 AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs),
8689 RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs: Attrs);
8690
8691 CallInst *CI = CallInst::Create(Ty, Func: Callee, Args, Bundles: BundleList);
8692 CI->setTailCallKind(TCK);
8693 CI->setCallingConv(CC);
8694 if (FMF.any()) {
8695 if (!isa<FPMathOperator>(Val: CI)) {
8696 CI->deleteValue();
8697 return error(L: CallLoc, Msg: "fast-math-flags specified for call without "
8698 "floating-point scalar or vector return type");
8699 }
8700 CI->setFastMathFlags(FMF);
8701 }
8702
8703 if (CalleeID.Kind == ValID::t_GlobalName &&
8704 isOldDbgFormatIntrinsic(Name: CalleeID.StrVal)) {
8705 if (SeenNewDbgInfoFormat) {
8706 CI->deleteValue();
8707 return error(L: CallLoc, Msg: "llvm.dbg intrinsic should not appear in a module "
8708 "using non-intrinsic debug info");
8709 }
8710 SeenOldDbgInfoFormat = true;
8711 }
8712 CI->setAttributes(PAL);
8713 ForwardRefAttrGroups[CI] = FwdRefAttrGrps;
8714 Inst = CI;
8715 return false;
8716}
8717
8718//===----------------------------------------------------------------------===//
8719// Memory Instructions.
8720//===----------------------------------------------------------------------===//
8721
8722/// parseAlloc
8723/// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)?
8724/// (',' 'align' i32)? (',', 'addrspace(n))?
8725int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
8726 Value *Size = nullptr;
8727 LocTy SizeLoc, TyLoc, ASLoc;
8728 MaybeAlign Alignment;
8729 unsigned AddrSpace = 0;
8730 Type *Ty = nullptr;
8731
8732 bool IsInAlloca = EatIfPresent(T: lltok::kw_inalloca);
8733 bool IsSwiftError = EatIfPresent(T: lltok::kw_swifterror);
8734
8735 if (parseType(Result&: Ty, Loc&: TyLoc))
8736 return true;
8737
8738 if (Ty->isFunctionTy() || !PointerType::isValidElementType(ElemTy: Ty))
8739 return error(L: TyLoc, Msg: "invalid type for alloca");
8740
8741 bool AteExtraComma = false;
8742 if (EatIfPresent(T: lltok::comma)) {
8743 if (Lex.getKind() == lltok::kw_align) {
8744 if (parseOptionalAlignment(Alignment))
8745 return true;
8746 if (parseOptionalCommaAddrSpace(AddrSpace, Loc&: ASLoc, AteExtraComma))
8747 return true;
8748 } else if (Lex.getKind() == lltok::kw_addrspace) {
8749 ASLoc = Lex.getLoc();
8750 if (parseOptionalAddrSpace(AddrSpace))
8751 return true;
8752 } else if (Lex.getKind() == lltok::MetadataVar) {
8753 AteExtraComma = true;
8754 } else {
8755 if (parseTypeAndValue(V&: Size, Loc&: SizeLoc, PFS))
8756 return true;
8757 if (EatIfPresent(T: lltok::comma)) {
8758 if (Lex.getKind() == lltok::kw_align) {
8759 if (parseOptionalAlignment(Alignment))
8760 return true;
8761 if (parseOptionalCommaAddrSpace(AddrSpace, Loc&: ASLoc, AteExtraComma))
8762 return true;
8763 } else if (Lex.getKind() == lltok::kw_addrspace) {
8764 ASLoc = Lex.getLoc();
8765 if (parseOptionalAddrSpace(AddrSpace))
8766 return true;
8767 } else if (Lex.getKind() == lltok::MetadataVar) {
8768 AteExtraComma = true;
8769 }
8770 }
8771 }
8772 }
8773
8774 if (Size && !Size->getType()->isIntegerTy())
8775 return error(L: SizeLoc, Msg: "element count must have integer type");
8776
8777 SmallPtrSet<Type *, 4> Visited;
8778 if (!Alignment && !Ty->isSized(Visited: &Visited))
8779 return error(L: TyLoc, Msg: "Cannot allocate unsized type");
8780 if (!Alignment)
8781 Alignment = M->getDataLayout().getPrefTypeAlign(Ty);
8782 AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, *Alignment);
8783 AI->setUsedWithInAlloca(IsInAlloca);
8784 AI->setSwiftError(IsSwiftError);
8785 Inst = AI;
8786 return AteExtraComma ? InstExtraComma : InstNormal;
8787}
8788
8789/// parseLoad
8790/// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)?
8791/// ::= 'load' 'atomic' 'volatile'? TypeAndValue
8792/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
8793int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) {
8794 Value *Val; LocTy Loc;
8795 MaybeAlign Alignment;
8796 bool AteExtraComma = false;
8797 bool isAtomic = false;
8798 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
8799 SyncScope::ID SSID = SyncScope::System;
8800
8801 if (Lex.getKind() == lltok::kw_atomic) {
8802 isAtomic = true;
8803 Lex.Lex();
8804 }
8805
8806 bool isVolatile = false;
8807 if (Lex.getKind() == lltok::kw_volatile) {
8808 isVolatile = true;
8809 Lex.Lex();
8810 }
8811
8812 Type *Ty;
8813 LocTy ExplicitTypeLoc = Lex.getLoc();
8814 if (parseType(Result&: Ty) ||
8815 parseToken(T: lltok::comma, ErrMsg: "expected comma after load's type") ||
8816 parseTypeAndValue(V&: Val, Loc, PFS) ||
8817 parseScopeAndOrdering(IsAtomic: isAtomic, SSID, Ordering) ||
8818 parseOptionalCommaAlign(Alignment, AteExtraComma))
8819 return true;
8820
8821 if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType())
8822 return error(L: Loc, Msg: "load operand must be a pointer to a first class type");
8823 if (isAtomic && !Alignment)
8824 return error(L: Loc, Msg: "atomic load must have explicit non-zero alignment");
8825 if (Ordering == AtomicOrdering::Release ||
8826 Ordering == AtomicOrdering::AcquireRelease)
8827 return error(L: Loc, Msg: "atomic load cannot use Release ordering");
8828
8829 SmallPtrSet<Type *, 4> Visited;
8830 if (!Alignment && !Ty->isSized(Visited: &Visited))
8831 return error(L: ExplicitTypeLoc, Msg: "loading unsized types is not allowed");
8832 if (!Alignment)
8833 Alignment = M->getDataLayout().getABITypeAlign(Ty);
8834 Inst = new LoadInst(Ty, Val, "", isVolatile, *Alignment, Ordering, SSID);
8835 return AteExtraComma ? InstExtraComma : InstNormal;
8836}
8837
8838/// parseStore
8839
8840/// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)?
8841/// ::= 'store' 'atomic' 'volatile'? TypeAndValue ',' TypeAndValue
8842/// 'singlethread'? AtomicOrdering (',' 'align' i32)?
8843int LLParser::parseStore(Instruction *&Inst, PerFunctionState &PFS) {
8844 Value *Val, *Ptr; LocTy Loc, PtrLoc;
8845 MaybeAlign Alignment;
8846 bool AteExtraComma = false;
8847 bool isAtomic = false;
8848 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
8849 SyncScope::ID SSID = SyncScope::System;
8850
8851 if (Lex.getKind() == lltok::kw_atomic) {
8852 isAtomic = true;
8853 Lex.Lex();
8854 }
8855
8856 bool isVolatile = false;
8857 if (Lex.getKind() == lltok::kw_volatile) {
8858 isVolatile = true;
8859 Lex.Lex();
8860 }
8861
8862 if (parseTypeAndValue(V&: Val, Loc, PFS) ||
8863 parseToken(T: lltok::comma, ErrMsg: "expected ',' after store operand") ||
8864 parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) ||
8865 parseScopeAndOrdering(IsAtomic: isAtomic, SSID, Ordering) ||
8866 parseOptionalCommaAlign(Alignment, AteExtraComma))
8867 return true;
8868
8869 if (!Ptr->getType()->isPointerTy())
8870 return error(L: PtrLoc, Msg: "store operand must be a pointer");
8871 if (!Val->getType()->isFirstClassType())
8872 return error(L: Loc, Msg: "store operand must be a first class value");
8873 if (isAtomic && !Alignment)
8874 return error(L: Loc, Msg: "atomic store must have explicit non-zero alignment");
8875 if (Ordering == AtomicOrdering::Acquire ||
8876 Ordering == AtomicOrdering::AcquireRelease)
8877 return error(L: Loc, Msg: "atomic store cannot use Acquire ordering");
8878 SmallPtrSet<Type *, 4> Visited;
8879 if (!Alignment && !Val->getType()->isSized(Visited: &Visited))
8880 return error(L: Loc, Msg: "storing unsized types is not allowed");
8881 if (!Alignment)
8882 Alignment = M->getDataLayout().getABITypeAlign(Ty: Val->getType());
8883
8884 Inst = new StoreInst(Val, Ptr, isVolatile, *Alignment, Ordering, SSID);
8885 return AteExtraComma ? InstExtraComma : InstNormal;
8886}
8887
8888/// parseCmpXchg
8889/// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ','
8890/// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering ','
8891/// 'Align'?
8892int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) {
8893 Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc;
8894 bool AteExtraComma = false;
8895 AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic;
8896 AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic;
8897 SyncScope::ID SSID = SyncScope::System;
8898 bool isVolatile = false;
8899 bool isWeak = false;
8900 MaybeAlign Alignment;
8901
8902 if (EatIfPresent(T: lltok::kw_weak))
8903 isWeak = true;
8904
8905 if (EatIfPresent(T: lltok::kw_volatile))
8906 isVolatile = true;
8907
8908 if (parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) ||
8909 parseToken(T: lltok::comma, ErrMsg: "expected ',' after cmpxchg address") ||
8910 parseTypeAndValue(V&: Cmp, Loc&: CmpLoc, PFS) ||
8911 parseToken(T: lltok::comma, ErrMsg: "expected ',' after cmpxchg cmp operand") ||
8912 parseTypeAndValue(V&: New, Loc&: NewLoc, PFS) ||
8913 parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering&: SuccessOrdering) ||
8914 parseOrdering(Ordering&: FailureOrdering) ||
8915 parseOptionalCommaAlign(Alignment, AteExtraComma))
8916 return true;
8917
8918 if (!AtomicCmpXchgInst::isValidSuccessOrdering(Ordering: SuccessOrdering))
8919 return tokError(Msg: "invalid cmpxchg success ordering");
8920 if (!AtomicCmpXchgInst::isValidFailureOrdering(Ordering: FailureOrdering))
8921 return tokError(Msg: "invalid cmpxchg failure ordering");
8922 if (!Ptr->getType()->isPointerTy())
8923 return error(L: PtrLoc, Msg: "cmpxchg operand must be a pointer");
8924 if (Cmp->getType() != New->getType())
8925 return error(L: NewLoc, Msg: "compare value and new value type do not match");
8926 if (!New->getType()->isFirstClassType())
8927 return error(L: NewLoc, Msg: "cmpxchg operand must be a first class value");
8928
8929 const Align DefaultAlignment(
8930 PFS.getFunction().getDataLayout().getTypeStoreSize(
8931 Ty: Cmp->getType()));
8932
8933 AtomicCmpXchgInst *CXI =
8934 new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment.value_or(u: DefaultAlignment),
8935 SuccessOrdering, FailureOrdering, SSID);
8936 CXI->setVolatile(isVolatile);
8937 CXI->setWeak(isWeak);
8938
8939 Inst = CXI;
8940 return AteExtraComma ? InstExtraComma : InstNormal;
8941}
8942
8943/// parseAtomicRMW
8944/// ::= 'atomicrmw' 'volatile'? BinOp TypeAndValue ',' TypeAndValue
8945/// 'singlethread'? AtomicOrdering
8946int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) {
8947 Value *Ptr, *Val; LocTy PtrLoc, ValLoc;
8948 bool AteExtraComma = false;
8949 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
8950 SyncScope::ID SSID = SyncScope::System;
8951 bool isVolatile = false;
8952 bool IsFP = false;
8953 AtomicRMWInst::BinOp Operation;
8954 MaybeAlign Alignment;
8955
8956 if (EatIfPresent(T: lltok::kw_volatile))
8957 isVolatile = true;
8958
8959 switch (Lex.getKind()) {
8960 default:
8961 return tokError(Msg: "expected binary operation in atomicrmw");
8962 case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break;
8963 case lltok::kw_add: Operation = AtomicRMWInst::Add; break;
8964 case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break;
8965 case lltok::kw_and: Operation = AtomicRMWInst::And; break;
8966 case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break;
8967 case lltok::kw_or: Operation = AtomicRMWInst::Or; break;
8968 case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break;
8969 case lltok::kw_max: Operation = AtomicRMWInst::Max; break;
8970 case lltok::kw_min: Operation = AtomicRMWInst::Min; break;
8971 case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break;
8972 case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break;
8973 case lltok::kw_uinc_wrap:
8974 Operation = AtomicRMWInst::UIncWrap;
8975 break;
8976 case lltok::kw_udec_wrap:
8977 Operation = AtomicRMWInst::UDecWrap;
8978 break;
8979 case lltok::kw_usub_cond:
8980 Operation = AtomicRMWInst::USubCond;
8981 break;
8982 case lltok::kw_usub_sat:
8983 Operation = AtomicRMWInst::USubSat;
8984 break;
8985 case lltok::kw_fadd:
8986 Operation = AtomicRMWInst::FAdd;
8987 IsFP = true;
8988 break;
8989 case lltok::kw_fsub:
8990 Operation = AtomicRMWInst::FSub;
8991 IsFP = true;
8992 break;
8993 case lltok::kw_fmax:
8994 Operation = AtomicRMWInst::FMax;
8995 IsFP = true;
8996 break;
8997 case lltok::kw_fmin:
8998 Operation = AtomicRMWInst::FMin;
8999 IsFP = true;
9000 break;
9001 case lltok::kw_fmaximum:
9002 Operation = AtomicRMWInst::FMaximum;
9003 IsFP = true;
9004 break;
9005 case lltok::kw_fminimum:
9006 Operation = AtomicRMWInst::FMinimum;
9007 IsFP = true;
9008 break;
9009 }
9010 Lex.Lex(); // Eat the operation.
9011
9012 if (parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) ||
9013 parseToken(T: lltok::comma, ErrMsg: "expected ',' after atomicrmw address") ||
9014 parseTypeAndValue(V&: Val, Loc&: ValLoc, PFS) ||
9015 parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering) ||
9016 parseOptionalCommaAlign(Alignment, AteExtraComma))
9017 return true;
9018
9019 if (Ordering == AtomicOrdering::Unordered)
9020 return tokError(Msg: "atomicrmw cannot be unordered");
9021 if (!Ptr->getType()->isPointerTy())
9022 return error(L: PtrLoc, Msg: "atomicrmw operand must be a pointer");
9023 if (Val->getType()->isScalableTy())
9024 return error(L: ValLoc, Msg: "atomicrmw operand may not be scalable");
9025
9026 if (Operation == AtomicRMWInst::Xchg) {
9027 if (!Val->getType()->isIntegerTy() &&
9028 !Val->getType()->isFloatingPointTy() &&
9029 !Val->getType()->isPointerTy()) {
9030 return error(
9031 L: ValLoc,
9032 Msg: "atomicrmw " + AtomicRMWInst::getOperationName(Op: Operation) +
9033 " operand must be an integer, floating point, or pointer type");
9034 }
9035 } else if (IsFP) {
9036 if (!Val->getType()->isFPOrFPVectorTy()) {
9037 return error(L: ValLoc, Msg: "atomicrmw " +
9038 AtomicRMWInst::getOperationName(Op: Operation) +
9039 " operand must be a floating point type");
9040 }
9041 } else {
9042 if (!Val->getType()->isIntegerTy()) {
9043 return error(L: ValLoc, Msg: "atomicrmw " +
9044 AtomicRMWInst::getOperationName(Op: Operation) +
9045 " operand must be an integer");
9046 }
9047 }
9048
9049 unsigned Size =
9050 PFS.getFunction().getDataLayout().getTypeStoreSizeInBits(
9051 Ty: Val->getType());
9052 if (Size < 8 || (Size & (Size - 1)))
9053 return error(L: ValLoc, Msg: "atomicrmw operand must be power-of-two byte-sized"
9054 " integer");
9055 const Align DefaultAlignment(
9056 PFS.getFunction().getDataLayout().getTypeStoreSize(
9057 Ty: Val->getType()));
9058 AtomicRMWInst *RMWI =
9059 new AtomicRMWInst(Operation, Ptr, Val,
9060 Alignment.value_or(u: DefaultAlignment), Ordering, SSID);
9061 RMWI->setVolatile(isVolatile);
9062 Inst = RMWI;
9063 return AteExtraComma ? InstExtraComma : InstNormal;
9064}
9065
9066/// parseFence
9067/// ::= 'fence' 'singlethread'? AtomicOrdering
9068int LLParser::parseFence(Instruction *&Inst, PerFunctionState &PFS) {
9069 AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
9070 SyncScope::ID SSID = SyncScope::System;
9071 if (parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering))
9072 return true;
9073
9074 if (Ordering == AtomicOrdering::Unordered)
9075 return tokError(Msg: "fence cannot be unordered");
9076 if (Ordering == AtomicOrdering::Monotonic)
9077 return tokError(Msg: "fence cannot be monotonic");
9078
9079 Inst = new FenceInst(Context, Ordering, SSID);
9080 return InstNormal;
9081}
9082
9083/// parseGetElementPtr
9084/// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)*
9085int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) {
9086 Value *Ptr = nullptr;
9087 Value *Val = nullptr;
9088 LocTy Loc, EltLoc;
9089 GEPNoWrapFlags NW;
9090
9091 while (true) {
9092 if (EatIfPresent(T: lltok::kw_inbounds))
9093 NW |= GEPNoWrapFlags::inBounds();
9094 else if (EatIfPresent(T: lltok::kw_nusw))
9095 NW |= GEPNoWrapFlags::noUnsignedSignedWrap();
9096 else if (EatIfPresent(T: lltok::kw_nuw))
9097 NW |= GEPNoWrapFlags::noUnsignedWrap();
9098 else
9099 break;
9100 }
9101
9102 Type *Ty = nullptr;
9103 if (parseType(Result&: Ty) ||
9104 parseToken(T: lltok::comma, ErrMsg: "expected comma after getelementptr's type") ||
9105 parseTypeAndValue(V&: Ptr, Loc, PFS))
9106 return true;
9107
9108 Type *BaseType = Ptr->getType();
9109 PointerType *BasePointerType = dyn_cast<PointerType>(Val: BaseType->getScalarType());
9110 if (!BasePointerType)
9111 return error(L: Loc, Msg: "base of getelementptr must be a pointer");
9112
9113 SmallVector<Value*, 16> Indices;
9114 bool AteExtraComma = false;
9115 // GEP returns a vector of pointers if at least one of parameters is a vector.
9116 // All vector parameters should have the same vector width.
9117 ElementCount GEPWidth = BaseType->isVectorTy()
9118 ? cast<VectorType>(Val: BaseType)->getElementCount()
9119 : ElementCount::getFixed(MinVal: 0);
9120
9121 while (EatIfPresent(T: lltok::comma)) {
9122 if (Lex.getKind() == lltok::MetadataVar) {
9123 AteExtraComma = true;
9124 break;
9125 }
9126 if (parseTypeAndValue(V&: Val, Loc&: EltLoc, PFS))
9127 return true;
9128 if (!Val->getType()->isIntOrIntVectorTy())
9129 return error(L: EltLoc, Msg: "getelementptr index must be an integer");
9130
9131 if (auto *ValVTy = dyn_cast<VectorType>(Val: Val->getType())) {
9132 ElementCount ValNumEl = ValVTy->getElementCount();
9133 if (GEPWidth != ElementCount::getFixed(MinVal: 0) && GEPWidth != ValNumEl)
9134 return error(
9135 L: EltLoc,
9136 Msg: "getelementptr vector index has a wrong number of elements");
9137 GEPWidth = ValNumEl;
9138 }
9139 Indices.push_back(Elt: Val);
9140 }
9141
9142 SmallPtrSet<Type*, 4> Visited;
9143 if (!Indices.empty() && !Ty->isSized(Visited: &Visited))
9144 return error(L: Loc, Msg: "base element of getelementptr must be sized");
9145
9146 auto *STy = dyn_cast<StructType>(Val: Ty);
9147 if (STy && STy->isScalableTy())
9148 return error(L: Loc, Msg: "getelementptr cannot target structure that contains "
9149 "scalable vector type");
9150
9151 if (!GetElementPtrInst::getIndexedType(Ty, IdxList: Indices))
9152 return error(L: Loc, Msg: "invalid getelementptr indices");
9153 GetElementPtrInst *GEP = GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList: Indices);
9154 Inst = GEP;
9155 GEP->setNoWrapFlags(NW);
9156 return AteExtraComma ? InstExtraComma : InstNormal;
9157}
9158
9159/// parseExtractValue
9160/// ::= 'extractvalue' TypeAndValue (',' uint32)+
9161int LLParser::parseExtractValue(Instruction *&Inst, PerFunctionState &PFS) {
9162 Value *Val; LocTy Loc;
9163 SmallVector<unsigned, 4> Indices;
9164 bool AteExtraComma;
9165 if (parseTypeAndValue(V&: Val, Loc, PFS) ||
9166 parseIndexList(Indices, AteExtraComma))
9167 return true;
9168
9169 if (!Val->getType()->isAggregateType())
9170 return error(L: Loc, Msg: "extractvalue operand must be aggregate type");
9171
9172 if (!ExtractValueInst::getIndexedType(Agg: Val->getType(), Idxs: Indices))
9173 return error(L: Loc, Msg: "invalid indices for extractvalue");
9174 Inst = ExtractValueInst::Create(Agg: Val, Idxs: Indices);
9175 return AteExtraComma ? InstExtraComma : InstNormal;
9176}
9177
9178/// parseInsertValue
9179/// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+
9180int LLParser::parseInsertValue(Instruction *&Inst, PerFunctionState &PFS) {
9181 Value *Val0, *Val1; LocTy Loc0, Loc1;
9182 SmallVector<unsigned, 4> Indices;
9183 bool AteExtraComma;
9184 if (parseTypeAndValue(V&: Val0, Loc&: Loc0, PFS) ||
9185 parseToken(T: lltok::comma, ErrMsg: "expected comma after insertvalue operand") ||
9186 parseTypeAndValue(V&: Val1, Loc&: Loc1, PFS) ||
9187 parseIndexList(Indices, AteExtraComma))
9188 return true;
9189
9190 if (!Val0->getType()->isAggregateType())
9191 return error(L: Loc0, Msg: "insertvalue operand must be aggregate type");
9192
9193 Type *IndexedType = ExtractValueInst::getIndexedType(Agg: Val0->getType(), Idxs: Indices);
9194 if (!IndexedType)
9195 return error(L: Loc0, Msg: "invalid indices for insertvalue");
9196 if (IndexedType != Val1->getType())
9197 return error(L: Loc1, Msg: "insertvalue operand and field disagree in type: '" +
9198 getTypeString(T: Val1->getType()) + "' instead of '" +
9199 getTypeString(T: IndexedType) + "'");
9200 Inst = InsertValueInst::Create(Agg: Val0, Val: Val1, Idxs: Indices);
9201 return AteExtraComma ? InstExtraComma : InstNormal;
9202}
9203
9204//===----------------------------------------------------------------------===//
9205// Embedded metadata.
9206//===----------------------------------------------------------------------===//
9207
9208/// parseMDNodeVector
9209/// ::= { Element (',' Element)* }
9210/// Element
9211/// ::= 'null' | Metadata
9212bool LLParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) {
9213 if (parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here"))
9214 return true;
9215
9216 // Check for an empty list.
9217 if (EatIfPresent(T: lltok::rbrace))
9218 return false;
9219
9220 do {
9221 if (EatIfPresent(T: lltok::kw_null)) {
9222 Elts.push_back(Elt: nullptr);
9223 continue;
9224 }
9225
9226 Metadata *MD;
9227 if (parseMetadata(MD, PFS: nullptr))
9228 return true;
9229 Elts.push_back(Elt: MD);
9230 } while (EatIfPresent(T: lltok::comma));
9231
9232 return parseToken(T: lltok::rbrace, ErrMsg: "expected end of metadata node");
9233}
9234
9235//===----------------------------------------------------------------------===//
9236// Use-list order directives.
9237//===----------------------------------------------------------------------===//
9238bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes,
9239 SMLoc Loc) {
9240 if (!V->hasUseList())
9241 return false;
9242 if (V->use_empty())
9243 return error(L: Loc, Msg: "value has no uses");
9244
9245 unsigned NumUses = 0;
9246 SmallDenseMap<const Use *, unsigned, 16> Order;
9247 for (const Use &U : V->uses()) {
9248 if (++NumUses > Indexes.size())
9249 break;
9250 Order[&U] = Indexes[NumUses - 1];
9251 }
9252 if (NumUses < 2)
9253 return error(L: Loc, Msg: "value only has one use");
9254 if (Order.size() != Indexes.size() || NumUses > Indexes.size())
9255 return error(L: Loc,
9256 Msg: "wrong number of indexes, expected " + Twine(V->getNumUses()));
9257
9258 V->sortUseList(Cmp: [&](const Use &L, const Use &R) {
9259 return Order.lookup(Val: &L) < Order.lookup(Val: &R);
9260 });
9261 return false;
9262}
9263
9264/// parseUseListOrderIndexes
9265/// ::= '{' uint32 (',' uint32)+ '}'
9266bool LLParser::parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) {
9267 SMLoc Loc = Lex.getLoc();
9268 if (parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here"))
9269 return true;
9270 if (Lex.getKind() == lltok::rbrace)
9271 return tokError(Msg: "expected non-empty list of uselistorder indexes");
9272
9273 // Use Offset, Max, and IsOrdered to check consistency of indexes. The
9274 // indexes should be distinct numbers in the range [0, size-1], and should
9275 // not be in order.
9276 unsigned Offset = 0;
9277 unsigned Max = 0;
9278 bool IsOrdered = true;
9279 assert(Indexes.empty() && "Expected empty order vector");
9280 do {
9281 unsigned Index;
9282 if (parseUInt32(Val&: Index))
9283 return true;
9284
9285 // Update consistency checks.
9286 Offset += Index - Indexes.size();
9287 Max = std::max(a: Max, b: Index);
9288 IsOrdered &= Index == Indexes.size();
9289
9290 Indexes.push_back(Elt: Index);
9291 } while (EatIfPresent(T: lltok::comma));
9292
9293 if (parseToken(T: lltok::rbrace, ErrMsg: "expected '}' here"))
9294 return true;
9295
9296 if (Indexes.size() < 2)
9297 return error(L: Loc, Msg: "expected >= 2 uselistorder indexes");
9298 if (Offset != 0 || Max >= Indexes.size())
9299 return error(L: Loc,
9300 Msg: "expected distinct uselistorder indexes in range [0, size)");
9301 if (IsOrdered)
9302 return error(L: Loc, Msg: "expected uselistorder indexes to change the order");
9303
9304 return false;
9305}
9306
9307/// parseUseListOrder
9308/// ::= 'uselistorder' Type Value ',' UseListOrderIndexes
9309bool LLParser::parseUseListOrder(PerFunctionState *PFS) {
9310 SMLoc Loc = Lex.getLoc();
9311 if (parseToken(T: lltok::kw_uselistorder, ErrMsg: "expected uselistorder directive"))
9312 return true;
9313
9314 Value *V;
9315 SmallVector<unsigned, 16> Indexes;
9316 if (parseTypeAndValue(V, PFS) ||
9317 parseToken(T: lltok::comma, ErrMsg: "expected comma in uselistorder directive") ||
9318 parseUseListOrderIndexes(Indexes))
9319 return true;
9320
9321 return sortUseListOrder(V, Indexes, Loc);
9322}
9323
9324/// parseUseListOrderBB
9325/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
9326bool LLParser::parseUseListOrderBB() {
9327 assert(Lex.getKind() == lltok::kw_uselistorder_bb);
9328 SMLoc Loc = Lex.getLoc();
9329 Lex.Lex();
9330
9331 ValID Fn, Label;
9332 SmallVector<unsigned, 16> Indexes;
9333 if (parseValID(ID&: Fn, /*PFS=*/nullptr) ||
9334 parseToken(T: lltok::comma, ErrMsg: "expected comma in uselistorder_bb directive") ||
9335 parseValID(ID&: Label, /*PFS=*/nullptr) ||
9336 parseToken(T: lltok::comma, ErrMsg: "expected comma in uselistorder_bb directive") ||
9337 parseUseListOrderIndexes(Indexes))
9338 return true;
9339
9340 // Check the function.
9341 GlobalValue *GV;
9342 if (Fn.Kind == ValID::t_GlobalName)
9343 GV = M->getNamedValue(Name: Fn.StrVal);
9344 else if (Fn.Kind == ValID::t_GlobalID)
9345 GV = NumberedVals.get(ID: Fn.UIntVal);
9346 else
9347 return error(L: Fn.Loc, Msg: "expected function name in uselistorder_bb");
9348 if (!GV)
9349 return error(L: Fn.Loc,
9350 Msg: "invalid function forward reference in uselistorder_bb");
9351 auto *F = dyn_cast<Function>(Val: GV);
9352 if (!F)
9353 return error(L: Fn.Loc, Msg: "expected function name in uselistorder_bb");
9354 if (F->isDeclaration())
9355 return error(L: Fn.Loc, Msg: "invalid declaration in uselistorder_bb");
9356
9357 // Check the basic block.
9358 if (Label.Kind == ValID::t_LocalID)
9359 return error(L: Label.Loc, Msg: "invalid numeric label in uselistorder_bb");
9360 if (Label.Kind != ValID::t_LocalName)
9361 return error(L: Label.Loc, Msg: "expected basic block name in uselistorder_bb");
9362 Value *V = F->getValueSymbolTable()->lookup(Name: Label.StrVal);
9363 if (!V)
9364 return error(L: Label.Loc, Msg: "invalid basic block in uselistorder_bb");
9365 if (!isa<BasicBlock>(Val: V))
9366 return error(L: Label.Loc, Msg: "expected basic block in uselistorder_bb");
9367
9368 return sortUseListOrder(V, Indexes, Loc);
9369}
9370
9371/// ModuleEntry
9372/// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
9373/// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'
9374bool LLParser::parseModuleEntry(unsigned ID) {
9375 assert(Lex.getKind() == lltok::kw_module);
9376 Lex.Lex();
9377
9378 std::string Path;
9379 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9380 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9381 parseToken(T: lltok::kw_path, ErrMsg: "expected 'path' here") ||
9382 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9383 parseStringConstant(Result&: Path) ||
9384 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9385 parseToken(T: lltok::kw_hash, ErrMsg: "expected 'hash' here") ||
9386 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9387 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
9388 return true;
9389
9390 ModuleHash Hash;
9391 if (parseUInt32(Val&: Hash[0]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9392 parseUInt32(Val&: Hash[1]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9393 parseUInt32(Val&: Hash[2]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9394 parseUInt32(Val&: Hash[3]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9395 parseUInt32(Val&: Hash[4]))
9396 return true;
9397
9398 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here") ||
9399 parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9400 return true;
9401
9402 auto ModuleEntry = Index->addModule(ModPath: Path, Hash);
9403 ModuleIdMap[ID] = ModuleEntry->first();
9404
9405 return false;
9406}
9407
9408/// TypeIdEntry
9409/// ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')'
9410bool LLParser::parseTypeIdEntry(unsigned ID) {
9411 assert(Lex.getKind() == lltok::kw_typeid);
9412 Lex.Lex();
9413
9414 std::string Name;
9415 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9416 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9417 parseToken(T: lltok::kw_name, ErrMsg: "expected 'name' here") ||
9418 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9419 parseStringConstant(Result&: Name))
9420 return true;
9421
9422 TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(TypeId: Name);
9423 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9424 parseTypeIdSummary(TIS) || parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9425 return true;
9426
9427 // Check if this ID was forward referenced, and if so, update the
9428 // corresponding GUIDs.
9429 auto FwdRefTIDs = ForwardRefTypeIds.find(x: ID);
9430 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
9431 for (auto TIDRef : FwdRefTIDs->second) {
9432 assert(!*TIDRef.first &&
9433 "Forward referenced type id GUID expected to be 0");
9434 *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(GlobalName: Name);
9435 }
9436 ForwardRefTypeIds.erase(position: FwdRefTIDs);
9437 }
9438
9439 return false;
9440}
9441
9442/// TypeIdSummary
9443/// ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')'
9444bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) {
9445 if (parseToken(T: lltok::kw_summary, ErrMsg: "expected 'summary' here") ||
9446 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9447 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9448 parseTypeTestResolution(TTRes&: TIS.TTRes))
9449 return true;
9450
9451 if (EatIfPresent(T: lltok::comma)) {
9452 // Expect optional wpdResolutions field
9453 if (parseOptionalWpdResolutions(WPDResMap&: TIS.WPDRes))
9454 return true;
9455 }
9456
9457 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9458 return true;
9459
9460 return false;
9461}
9462
9463static ValueInfo EmptyVI =
9464 ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8);
9465
9466/// TypeIdCompatibleVtableEntry
9467/// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ','
9468/// TypeIdCompatibleVtableInfo
9469/// ')'
9470bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) {
9471 assert(Lex.getKind() == lltok::kw_typeidCompatibleVTable);
9472 Lex.Lex();
9473
9474 std::string Name;
9475 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9476 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9477 parseToken(T: lltok::kw_name, ErrMsg: "expected 'name' here") ||
9478 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9479 parseStringConstant(Result&: Name))
9480 return true;
9481
9482 TypeIdCompatibleVtableInfo &TI =
9483 Index->getOrInsertTypeIdCompatibleVtableSummary(TypeId: Name);
9484 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9485 parseToken(T: lltok::kw_summary, ErrMsg: "expected 'summary' here") ||
9486 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9487 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
9488 return true;
9489
9490 IdToIndexMapType IdToIndexMap;
9491 // parse each call edge
9492 do {
9493 uint64_t Offset;
9494 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9495 parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here") ||
9496 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") || parseUInt64(Val&: Offset) ||
9497 parseToken(T: lltok::comma, ErrMsg: "expected ',' here"))
9498 return true;
9499
9500 LocTy Loc = Lex.getLoc();
9501 unsigned GVId;
9502 ValueInfo VI;
9503 if (parseGVReference(VI, GVId))
9504 return true;
9505
9506 // Keep track of the TypeIdCompatibleVtableInfo array index needing a
9507 // forward reference. We will save the location of the ValueInfo needing an
9508 // update, but can only do so once the std::vector is finalized.
9509 if (VI == EmptyVI)
9510 IdToIndexMap[GVId].push_back(x: std::make_pair(x: TI.size(), y&: Loc));
9511 TI.push_back(x: {Offset, VI});
9512
9513 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in call"))
9514 return true;
9515 } while (EatIfPresent(T: lltok::comma));
9516
9517 // Now that the TI vector is finalized, it is safe to save the locations
9518 // of any forward GV references that need updating later.
9519 for (auto I : IdToIndexMap) {
9520 auto &Infos = ForwardRefValueInfos[I.first];
9521 for (auto P : I.second) {
9522 assert(TI[P.first].VTableVI == EmptyVI &&
9523 "Forward referenced ValueInfo expected to be empty");
9524 Infos.emplace_back(args: &TI[P.first].VTableVI, args&: P.second);
9525 }
9526 }
9527
9528 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here") ||
9529 parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9530 return true;
9531
9532 // Check if this ID was forward referenced, and if so, update the
9533 // corresponding GUIDs.
9534 auto FwdRefTIDs = ForwardRefTypeIds.find(x: ID);
9535 if (FwdRefTIDs != ForwardRefTypeIds.end()) {
9536 for (auto TIDRef : FwdRefTIDs->second) {
9537 assert(!*TIDRef.first &&
9538 "Forward referenced type id GUID expected to be 0");
9539 *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(GlobalName: Name);
9540 }
9541 ForwardRefTypeIds.erase(position: FwdRefTIDs);
9542 }
9543
9544 return false;
9545}
9546
9547/// TypeTestResolution
9548/// ::= 'typeTestRes' ':' '(' 'kind' ':'
9549/// ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ','
9550/// 'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]?
9551/// [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]?
9552/// [',' 'inlinesBits' ':' UInt64]? ')'
9553bool LLParser::parseTypeTestResolution(TypeTestResolution &TTRes) {
9554 if (parseToken(T: lltok::kw_typeTestRes, ErrMsg: "expected 'typeTestRes' here") ||
9555 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9556 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9557 parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here") ||
9558 parseToken(T: lltok::colon, ErrMsg: "expected ':' here"))
9559 return true;
9560
9561 switch (Lex.getKind()) {
9562 case lltok::kw_unknown:
9563 TTRes.TheKind = TypeTestResolution::Unknown;
9564 break;
9565 case lltok::kw_unsat:
9566 TTRes.TheKind = TypeTestResolution::Unsat;
9567 break;
9568 case lltok::kw_byteArray:
9569 TTRes.TheKind = TypeTestResolution::ByteArray;
9570 break;
9571 case lltok::kw_inline:
9572 TTRes.TheKind = TypeTestResolution::Inline;
9573 break;
9574 case lltok::kw_single:
9575 TTRes.TheKind = TypeTestResolution::Single;
9576 break;
9577 case lltok::kw_allOnes:
9578 TTRes.TheKind = TypeTestResolution::AllOnes;
9579 break;
9580 default:
9581 return error(L: Lex.getLoc(), Msg: "unexpected TypeTestResolution kind");
9582 }
9583 Lex.Lex();
9584
9585 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9586 parseToken(T: lltok::kw_sizeM1BitWidth, ErrMsg: "expected 'sizeM1BitWidth' here") ||
9587 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9588 parseUInt32(Val&: TTRes.SizeM1BitWidth))
9589 return true;
9590
9591 // parse optional fields
9592 while (EatIfPresent(T: lltok::comma)) {
9593 switch (Lex.getKind()) {
9594 case lltok::kw_alignLog2:
9595 Lex.Lex();
9596 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") ||
9597 parseUInt64(Val&: TTRes.AlignLog2))
9598 return true;
9599 break;
9600 case lltok::kw_sizeM1:
9601 Lex.Lex();
9602 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseUInt64(Val&: TTRes.SizeM1))
9603 return true;
9604 break;
9605 case lltok::kw_bitMask: {
9606 unsigned Val;
9607 Lex.Lex();
9608 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseUInt32(Val))
9609 return true;
9610 assert(Val <= 0xff);
9611 TTRes.BitMask = (uint8_t)Val;
9612 break;
9613 }
9614 case lltok::kw_inlineBits:
9615 Lex.Lex();
9616 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") ||
9617 parseUInt64(Val&: TTRes.InlineBits))
9618 return true;
9619 break;
9620 default:
9621 return error(L: Lex.getLoc(), Msg: "expected optional TypeTestResolution field");
9622 }
9623 }
9624
9625 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9626 return true;
9627
9628 return false;
9629}
9630
9631/// OptionalWpdResolutions
9632/// ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')'
9633/// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')'
9634bool LLParser::parseOptionalWpdResolutions(
9635 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) {
9636 if (parseToken(T: lltok::kw_wpdResolutions, ErrMsg: "expected 'wpdResolutions' here") ||
9637 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9638 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
9639 return true;
9640
9641 do {
9642 uint64_t Offset;
9643 WholeProgramDevirtResolution WPDRes;
9644 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9645 parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here") ||
9646 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") || parseUInt64(Val&: Offset) ||
9647 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") || parseWpdRes(WPDRes) ||
9648 parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9649 return true;
9650 WPDResMap[Offset] = WPDRes;
9651 } while (EatIfPresent(T: lltok::comma));
9652
9653 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9654 return true;
9655
9656 return false;
9657}
9658
9659/// WpdRes
9660/// ::= 'wpdRes' ':' '(' 'kind' ':' 'indir'
9661/// [',' OptionalResByArg]? ')'
9662/// ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl'
9663/// ',' 'singleImplName' ':' STRINGCONSTANT ','
9664/// [',' OptionalResByArg]? ')'
9665/// ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel'
9666/// [',' OptionalResByArg]? ')'
9667bool LLParser::parseWpdRes(WholeProgramDevirtResolution &WPDRes) {
9668 if (parseToken(T: lltok::kw_wpdRes, ErrMsg: "expected 'wpdRes' here") ||
9669 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9670 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9671 parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here") ||
9672 parseToken(T: lltok::colon, ErrMsg: "expected ':' here"))
9673 return true;
9674
9675 switch (Lex.getKind()) {
9676 case lltok::kw_indir:
9677 WPDRes.TheKind = WholeProgramDevirtResolution::Indir;
9678 break;
9679 case lltok::kw_singleImpl:
9680 WPDRes.TheKind = WholeProgramDevirtResolution::SingleImpl;
9681 break;
9682 case lltok::kw_branchFunnel:
9683 WPDRes.TheKind = WholeProgramDevirtResolution::BranchFunnel;
9684 break;
9685 default:
9686 return error(L: Lex.getLoc(), Msg: "unexpected WholeProgramDevirtResolution kind");
9687 }
9688 Lex.Lex();
9689
9690 // parse optional fields
9691 while (EatIfPresent(T: lltok::comma)) {
9692 switch (Lex.getKind()) {
9693 case lltok::kw_singleImplName:
9694 Lex.Lex();
9695 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9696 parseStringConstant(Result&: WPDRes.SingleImplName))
9697 return true;
9698 break;
9699 case lltok::kw_resByArg:
9700 if (parseOptionalResByArg(ResByArg&: WPDRes.ResByArg))
9701 return true;
9702 break;
9703 default:
9704 return error(L: Lex.getLoc(),
9705 Msg: "expected optional WholeProgramDevirtResolution field");
9706 }
9707 }
9708
9709 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9710 return true;
9711
9712 return false;
9713}
9714
9715/// OptionalResByArg
9716/// ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')'
9717/// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':'
9718/// ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' |
9719/// 'virtualConstProp' )
9720/// [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]?
9721/// [',' 'bit' ':' UInt32]? ')'
9722bool LLParser::parseOptionalResByArg(
9723 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
9724 &ResByArg) {
9725 if (parseToken(T: lltok::kw_resByArg, ErrMsg: "expected 'resByArg' here") ||
9726 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9727 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
9728 return true;
9729
9730 do {
9731 std::vector<uint64_t> Args;
9732 if (parseArgs(Args) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
9733 parseToken(T: lltok::kw_byArg, ErrMsg: "expected 'byArg here") ||
9734 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9735 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
9736 parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here") ||
9737 parseToken(T: lltok::colon, ErrMsg: "expected ':' here"))
9738 return true;
9739
9740 WholeProgramDevirtResolution::ByArg ByArg;
9741 switch (Lex.getKind()) {
9742 case lltok::kw_indir:
9743 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::Indir;
9744 break;
9745 case lltok::kw_uniformRetVal:
9746 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal;
9747 break;
9748 case lltok::kw_uniqueRetVal:
9749 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal;
9750 break;
9751 case lltok::kw_virtualConstProp:
9752 ByArg.TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp;
9753 break;
9754 default:
9755 return error(L: Lex.getLoc(),
9756 Msg: "unexpected WholeProgramDevirtResolution::ByArg kind");
9757 }
9758 Lex.Lex();
9759
9760 // parse optional fields
9761 while (EatIfPresent(T: lltok::comma)) {
9762 switch (Lex.getKind()) {
9763 case lltok::kw_info:
9764 Lex.Lex();
9765 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9766 parseUInt64(Val&: ByArg.Info))
9767 return true;
9768 break;
9769 case lltok::kw_byte:
9770 Lex.Lex();
9771 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9772 parseUInt32(Val&: ByArg.Byte))
9773 return true;
9774 break;
9775 case lltok::kw_bit:
9776 Lex.Lex();
9777 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9778 parseUInt32(Val&: ByArg.Bit))
9779 return true;
9780 break;
9781 default:
9782 return error(L: Lex.getLoc(),
9783 Msg: "expected optional whole program devirt field");
9784 }
9785 }
9786
9787 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9788 return true;
9789
9790 ResByArg[Args] = ByArg;
9791 } while (EatIfPresent(T: lltok::comma));
9792
9793 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9794 return true;
9795
9796 return false;
9797}
9798
9799/// OptionalResByArg
9800/// ::= 'args' ':' '(' UInt64[, UInt64]* ')'
9801bool LLParser::parseArgs(std::vector<uint64_t> &Args) {
9802 if (parseToken(T: lltok::kw_args, ErrMsg: "expected 'args' here") ||
9803 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9804 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
9805 return true;
9806
9807 do {
9808 uint64_t Val;
9809 if (parseUInt64(Val))
9810 return true;
9811 Args.push_back(x: Val);
9812 } while (EatIfPresent(T: lltok::comma));
9813
9814 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9815 return true;
9816
9817 return false;
9818}
9819
9820static const auto FwdVIRef = (GlobalValueSummaryMapTy::value_type *)-8;
9821
9822static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) {
9823 bool ReadOnly = Fwd->isReadOnly();
9824 bool WriteOnly = Fwd->isWriteOnly();
9825 assert(!(ReadOnly && WriteOnly));
9826 *Fwd = Resolved;
9827 if (ReadOnly)
9828 Fwd->setReadOnly();
9829 if (WriteOnly)
9830 Fwd->setWriteOnly();
9831}
9832
9833/// Stores the given Name/GUID and associated summary into the Index.
9834/// Also updates any forward references to the associated entry ID.
9835bool LLParser::addGlobalValueToIndex(
9836 std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage,
9837 unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) {
9838 // First create the ValueInfo utilizing the Name or GUID.
9839 ValueInfo VI;
9840 if (GUID != 0) {
9841 assert(Name.empty());
9842 VI = Index->getOrInsertValueInfo(GUID);
9843 } else {
9844 assert(!Name.empty());
9845 if (M) {
9846 auto *GV = M->getNamedValue(Name);
9847 if (!GV)
9848 return error(L: Loc, Msg: "Reference to undefined global \"" + Name + "\"");
9849
9850 VI = Index->getOrInsertValueInfo(GV);
9851 } else {
9852 assert(
9853 (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) &&
9854 "Need a source_filename to compute GUID for local");
9855 GUID = GlobalValue::getGUIDAssumingExternalLinkage(
9856 GlobalName: GlobalValue::getGlobalIdentifier(Name, Linkage, FileName: SourceFileName));
9857 VI = Index->getOrInsertValueInfo(GUID, Name: Index->saveString(String: Name));
9858 }
9859 }
9860
9861 // Resolve forward references from calls/refs
9862 auto FwdRefVIs = ForwardRefValueInfos.find(x: ID);
9863 if (FwdRefVIs != ForwardRefValueInfos.end()) {
9864 for (auto VIRef : FwdRefVIs->second) {
9865 assert(VIRef.first->getRef() == FwdVIRef &&
9866 "Forward referenced ValueInfo expected to be empty");
9867 resolveFwdRef(Fwd: VIRef.first, Resolved&: VI);
9868 }
9869 ForwardRefValueInfos.erase(position: FwdRefVIs);
9870 }
9871
9872 // Resolve forward references from aliases
9873 auto FwdRefAliasees = ForwardRefAliasees.find(x: ID);
9874 if (FwdRefAliasees != ForwardRefAliasees.end()) {
9875 for (auto AliaseeRef : FwdRefAliasees->second) {
9876 assert(!AliaseeRef.first->hasAliasee() &&
9877 "Forward referencing alias already has aliasee");
9878 assert(Summary && "Aliasee must be a definition");
9879 AliaseeRef.first->setAliasee(AliaseeVI&: VI, Aliasee: Summary.get());
9880 }
9881 ForwardRefAliasees.erase(position: FwdRefAliasees);
9882 }
9883
9884 // Add the summary if one was provided.
9885 if (Summary)
9886 Index->addGlobalValueSummary(VI, Summary: std::move(Summary));
9887
9888 // Save the associated ValueInfo for use in later references by ID.
9889 if (ID == NumberedValueInfos.size())
9890 NumberedValueInfos.push_back(x: VI);
9891 else {
9892 // Handle non-continuous numbers (to make test simplification easier).
9893 if (ID > NumberedValueInfos.size())
9894 NumberedValueInfos.resize(new_size: ID + 1);
9895 NumberedValueInfos[ID] = VI;
9896 }
9897
9898 return false;
9899}
9900
9901/// parseSummaryIndexFlags
9902/// ::= 'flags' ':' UInt64
9903bool LLParser::parseSummaryIndexFlags() {
9904 assert(Lex.getKind() == lltok::kw_flags);
9905 Lex.Lex();
9906
9907 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here"))
9908 return true;
9909 uint64_t Flags;
9910 if (parseUInt64(Val&: Flags))
9911 return true;
9912 if (Index)
9913 Index->setFlags(Flags);
9914 return false;
9915}
9916
9917/// parseBlockCount
9918/// ::= 'blockcount' ':' UInt64
9919bool LLParser::parseBlockCount() {
9920 assert(Lex.getKind() == lltok::kw_blockcount);
9921 Lex.Lex();
9922
9923 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here"))
9924 return true;
9925 uint64_t BlockCount;
9926 if (parseUInt64(Val&: BlockCount))
9927 return true;
9928 if (Index)
9929 Index->setBlockCount(BlockCount);
9930 return false;
9931}
9932
9933/// parseGVEntry
9934/// ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64)
9935/// [',' 'summaries' ':' Summary[',' Summary]* ]? ')'
9936/// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')'
9937bool LLParser::parseGVEntry(unsigned ID) {
9938 assert(Lex.getKind() == lltok::kw_gv);
9939 Lex.Lex();
9940
9941 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9942 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
9943 return true;
9944
9945 LocTy Loc = Lex.getLoc();
9946 std::string Name;
9947 GlobalValue::GUID GUID = 0;
9948 switch (Lex.getKind()) {
9949 case lltok::kw_name:
9950 Lex.Lex();
9951 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9952 parseStringConstant(Result&: Name))
9953 return true;
9954 // Can't create GUID/ValueInfo until we have the linkage.
9955 break;
9956 case lltok::kw_guid:
9957 Lex.Lex();
9958 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") || parseUInt64(Val&: GUID))
9959 return true;
9960 break;
9961 default:
9962 return error(L: Lex.getLoc(), Msg: "expected name or guid tag");
9963 }
9964
9965 if (!EatIfPresent(T: lltok::comma)) {
9966 // No summaries. Wrap up.
9967 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
9968 return true;
9969 // This was created for a call to an external or indirect target.
9970 // A GUID with no summary came from a VALUE_GUID record, dummy GUID
9971 // created for indirect calls with VP. A Name with no GUID came from
9972 // an external definition. We pass ExternalLinkage since that is only
9973 // used when the GUID must be computed from Name, and in that case
9974 // the symbol must have external linkage.
9975 return addGlobalValueToIndex(Name, GUID, Linkage: GlobalValue::ExternalLinkage, ID,
9976 Summary: nullptr, Loc);
9977 }
9978
9979 // Have a list of summaries
9980 if (parseToken(T: lltok::kw_summaries, ErrMsg: "expected 'summaries' here") ||
9981 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
9982 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
9983 return true;
9984 do {
9985 switch (Lex.getKind()) {
9986 case lltok::kw_function:
9987 if (parseFunctionSummary(Name, GUID, ID))
9988 return true;
9989 break;
9990 case lltok::kw_variable:
9991 if (parseVariableSummary(Name, GUID, ID))
9992 return true;
9993 break;
9994 case lltok::kw_alias:
9995 if (parseAliasSummary(Name, GUID, ID))
9996 return true;
9997 break;
9998 default:
9999 return error(L: Lex.getLoc(), Msg: "expected summary type");
10000 }
10001 } while (EatIfPresent(T: lltok::comma));
10002
10003 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here") ||
10004 parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10005 return true;
10006
10007 return false;
10008}
10009
10010/// FunctionSummary
10011/// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags
10012/// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]?
10013/// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]?
10014/// [',' OptionalRefs]? ')'
10015bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
10016 unsigned ID) {
10017 LocTy Loc = Lex.getLoc();
10018 assert(Lex.getKind() == lltok::kw_function);
10019 Lex.Lex();
10020
10021 StringRef ModulePath;
10022 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10023 GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
10024 /*NotEligibleToImport=*/false,
10025 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10026 GlobalValueSummary::Definition);
10027 unsigned InstCount;
10028 SmallVector<FunctionSummary::EdgeTy, 0> Calls;
10029 FunctionSummary::TypeIdInfo TypeIdInfo;
10030 std::vector<FunctionSummary::ParamAccess> ParamAccesses;
10031 SmallVector<ValueInfo, 0> Refs;
10032 std::vector<CallsiteInfo> Callsites;
10033 std::vector<AllocInfo> Allocs;
10034 // Default is all-zeros (conservative values).
10035 FunctionSummary::FFlags FFlags = {};
10036 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10037 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
10038 parseModuleReference(ModulePath) ||
10039 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") || parseGVFlags(GVFlags) ||
10040 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
10041 parseToken(T: lltok::kw_insts, ErrMsg: "expected 'insts' here") ||
10042 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") || parseUInt32(Val&: InstCount))
10043 return true;
10044
10045 // parse optional fields
10046 while (EatIfPresent(T: lltok::comma)) {
10047 switch (Lex.getKind()) {
10048 case lltok::kw_funcFlags:
10049 if (parseOptionalFFlags(FFlags))
10050 return true;
10051 break;
10052 case lltok::kw_calls:
10053 if (parseOptionalCalls(Calls))
10054 return true;
10055 break;
10056 case lltok::kw_typeIdInfo:
10057 if (parseOptionalTypeIdInfo(TypeIdInfo))
10058 return true;
10059 break;
10060 case lltok::kw_refs:
10061 if (parseOptionalRefs(Refs))
10062 return true;
10063 break;
10064 case lltok::kw_params:
10065 if (parseOptionalParamAccesses(Params&: ParamAccesses))
10066 return true;
10067 break;
10068 case lltok::kw_allocs:
10069 if (parseOptionalAllocs(Allocs))
10070 return true;
10071 break;
10072 case lltok::kw_callsites:
10073 if (parseOptionalCallsites(Callsites))
10074 return true;
10075 break;
10076 default:
10077 return error(L: Lex.getLoc(), Msg: "expected optional function summary field");
10078 }
10079 }
10080
10081 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10082 return true;
10083
10084 auto FS = std::make_unique<FunctionSummary>(
10085 args&: GVFlags, args&: InstCount, args&: FFlags, args: std::move(Refs), args: std::move(Calls),
10086 args: std::move(TypeIdInfo.TypeTests),
10087 args: std::move(TypeIdInfo.TypeTestAssumeVCalls),
10088 args: std::move(TypeIdInfo.TypeCheckedLoadVCalls),
10089 args: std::move(TypeIdInfo.TypeTestAssumeConstVCalls),
10090 args: std::move(TypeIdInfo.TypeCheckedLoadConstVCalls),
10091 args: std::move(ParamAccesses), args: std::move(Callsites), args: std::move(Allocs));
10092
10093 FS->setModulePath(ModulePath);
10094
10095 return addGlobalValueToIndex(Name, GUID,
10096 Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
10097 Summary: std::move(FS), Loc);
10098}
10099
10100/// VariableSummary
10101/// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags
10102/// [',' OptionalRefs]? ')'
10103bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID,
10104 unsigned ID) {
10105 LocTy Loc = Lex.getLoc();
10106 assert(Lex.getKind() == lltok::kw_variable);
10107 Lex.Lex();
10108
10109 StringRef ModulePath;
10110 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10111 GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
10112 /*NotEligibleToImport=*/false,
10113 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10114 GlobalValueSummary::Definition);
10115 GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false,
10116 /* WriteOnly */ false,
10117 /* Constant */ false,
10118 GlobalObject::VCallVisibilityPublic);
10119 SmallVector<ValueInfo, 0> Refs;
10120 VTableFuncList VTableFuncs;
10121 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10122 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
10123 parseModuleReference(ModulePath) ||
10124 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") || parseGVFlags(GVFlags) ||
10125 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
10126 parseGVarFlags(GVarFlags))
10127 return true;
10128
10129 // parse optional fields
10130 while (EatIfPresent(T: lltok::comma)) {
10131 switch (Lex.getKind()) {
10132 case lltok::kw_vTableFuncs:
10133 if (parseOptionalVTableFuncs(VTableFuncs))
10134 return true;
10135 break;
10136 case lltok::kw_refs:
10137 if (parseOptionalRefs(Refs))
10138 return true;
10139 break;
10140 default:
10141 return error(L: Lex.getLoc(), Msg: "expected optional variable summary field");
10142 }
10143 }
10144
10145 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10146 return true;
10147
10148 auto GS =
10149 std::make_unique<GlobalVarSummary>(args&: GVFlags, args&: GVarFlags, args: std::move(Refs));
10150
10151 GS->setModulePath(ModulePath);
10152 GS->setVTableFuncs(std::move(VTableFuncs));
10153
10154 return addGlobalValueToIndex(Name, GUID,
10155 Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
10156 Summary: std::move(GS), Loc);
10157}
10158
10159/// AliasSummary
10160/// ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ','
10161/// 'aliasee' ':' GVReference ')'
10162bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID,
10163 unsigned ID) {
10164 assert(Lex.getKind() == lltok::kw_alias);
10165 LocTy Loc = Lex.getLoc();
10166 Lex.Lex();
10167
10168 StringRef ModulePath;
10169 GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags(
10170 GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility,
10171 /*NotEligibleToImport=*/false,
10172 /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
10173 GlobalValueSummary::Definition);
10174 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10175 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
10176 parseModuleReference(ModulePath) ||
10177 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") || parseGVFlags(GVFlags) ||
10178 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
10179 parseToken(T: lltok::kw_aliasee, ErrMsg: "expected 'aliasee' here") ||
10180 parseToken(T: lltok::colon, ErrMsg: "expected ':' here"))
10181 return true;
10182
10183 ValueInfo AliaseeVI;
10184 unsigned GVId;
10185 auto AS = std::make_unique<AliasSummary>(args&: GVFlags);
10186 AS->setModulePath(ModulePath);
10187
10188 if (!EatIfPresent(T: lltok::kw_null)) {
10189 if (parseGVReference(VI&: AliaseeVI, GVId))
10190 return true;
10191
10192 // Record forward reference if the aliasee is not parsed yet.
10193 if (AliaseeVI.getRef() == FwdVIRef) {
10194 ForwardRefAliasees[GVId].emplace_back(args: AS.get(), args&: Loc);
10195 } else {
10196 auto Summary = Index->findSummaryInModule(VI: AliaseeVI, ModuleId: ModulePath);
10197 assert(Summary && "Aliasee must be a definition");
10198 AS->setAliasee(AliaseeVI, Aliasee: Summary);
10199 }
10200 }
10201
10202 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10203 return true;
10204
10205 return addGlobalValueToIndex(Name, GUID,
10206 Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID,
10207 Summary: std::move(AS), Loc);
10208}
10209
10210/// Flag
10211/// ::= [0|1]
10212bool LLParser::parseFlag(unsigned &Val) {
10213 if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned())
10214 return tokError(Msg: "expected integer");
10215 Val = (unsigned)Lex.getAPSIntVal().getBoolValue();
10216 Lex.Lex();
10217 return false;
10218}
10219
10220/// OptionalFFlags
10221/// := 'funcFlags' ':' '(' ['readNone' ':' Flag]?
10222/// [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]?
10223/// [',' 'returnDoesNotAlias' ':' Flag]? ')'
10224/// [',' 'noInline' ':' Flag]? ')'
10225/// [',' 'alwaysInline' ':' Flag]? ')'
10226/// [',' 'noUnwind' ':' Flag]? ')'
10227/// [',' 'mayThrow' ':' Flag]? ')'
10228/// [',' 'hasUnknownCall' ':' Flag]? ')'
10229/// [',' 'mustBeUnreachable' ':' Flag]? ')'
10230
10231bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
10232 assert(Lex.getKind() == lltok::kw_funcFlags);
10233 Lex.Lex();
10234
10235 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in funcFlags") ||
10236 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in funcFlags"))
10237 return true;
10238
10239 do {
10240 unsigned Val = 0;
10241 switch (Lex.getKind()) {
10242 case lltok::kw_readNone:
10243 Lex.Lex();
10244 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10245 return true;
10246 FFlags.ReadNone = Val;
10247 break;
10248 case lltok::kw_readOnly:
10249 Lex.Lex();
10250 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10251 return true;
10252 FFlags.ReadOnly = Val;
10253 break;
10254 case lltok::kw_noRecurse:
10255 Lex.Lex();
10256 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10257 return true;
10258 FFlags.NoRecurse = Val;
10259 break;
10260 case lltok::kw_returnDoesNotAlias:
10261 Lex.Lex();
10262 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10263 return true;
10264 FFlags.ReturnDoesNotAlias = Val;
10265 break;
10266 case lltok::kw_noInline:
10267 Lex.Lex();
10268 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10269 return true;
10270 FFlags.NoInline = Val;
10271 break;
10272 case lltok::kw_alwaysInline:
10273 Lex.Lex();
10274 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10275 return true;
10276 FFlags.AlwaysInline = Val;
10277 break;
10278 case lltok::kw_noUnwind:
10279 Lex.Lex();
10280 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10281 return true;
10282 FFlags.NoUnwind = Val;
10283 break;
10284 case lltok::kw_mayThrow:
10285 Lex.Lex();
10286 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10287 return true;
10288 FFlags.MayThrow = Val;
10289 break;
10290 case lltok::kw_hasUnknownCall:
10291 Lex.Lex();
10292 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10293 return true;
10294 FFlags.HasUnknownCall = Val;
10295 break;
10296 case lltok::kw_mustBeUnreachable:
10297 Lex.Lex();
10298 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val))
10299 return true;
10300 FFlags.MustBeUnreachable = Val;
10301 break;
10302 default:
10303 return error(L: Lex.getLoc(), Msg: "expected function flag type");
10304 }
10305 } while (EatIfPresent(T: lltok::comma));
10306
10307 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in funcFlags"))
10308 return true;
10309
10310 return false;
10311}
10312
10313/// OptionalCalls
10314/// := 'calls' ':' '(' Call [',' Call]* ')'
10315/// Call ::= '(' 'callee' ':' GVReference
10316/// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]?
10317/// [ ',' 'tail' ]? ')'
10318bool LLParser::parseOptionalCalls(
10319 SmallVectorImpl<FunctionSummary::EdgeTy> &Calls) {
10320 assert(Lex.getKind() == lltok::kw_calls);
10321 Lex.Lex();
10322
10323 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in calls") ||
10324 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in calls"))
10325 return true;
10326
10327 IdToIndexMapType IdToIndexMap;
10328 // parse each call edge
10329 do {
10330 ValueInfo VI;
10331 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in call") ||
10332 parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' in call") ||
10333 parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
10334 return true;
10335
10336 LocTy Loc = Lex.getLoc();
10337 unsigned GVId;
10338 if (parseGVReference(VI, GVId))
10339 return true;
10340
10341 CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown;
10342 unsigned RelBF = 0;
10343 unsigned HasTailCall = false;
10344
10345 // parse optional fields
10346 while (EatIfPresent(T: lltok::comma)) {
10347 switch (Lex.getKind()) {
10348 case lltok::kw_hotness:
10349 Lex.Lex();
10350 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseHotness(Hotness))
10351 return true;
10352 break;
10353 // Deprecated, keep in order to support old files.
10354 case lltok::kw_relbf:
10355 Lex.Lex();
10356 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseUInt32(Val&: RelBF))
10357 return true;
10358 break;
10359 case lltok::kw_tail:
10360 Lex.Lex();
10361 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val&: HasTailCall))
10362 return true;
10363 break;
10364 default:
10365 return error(L: Lex.getLoc(), Msg: "expected hotness, relbf, or tail");
10366 }
10367 }
10368 // Keep track of the Call array index needing a forward reference.
10369 // We will save the location of the ValueInfo needing an update, but
10370 // can only do so once the std::vector is finalized.
10371 if (VI.getRef() == FwdVIRef)
10372 IdToIndexMap[GVId].push_back(x: std::make_pair(x: Calls.size(), y&: Loc));
10373 Calls.push_back(
10374 Elt: FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, HasTailCall)});
10375
10376 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in call"))
10377 return true;
10378 } while (EatIfPresent(T: lltok::comma));
10379
10380 // Now that the Calls vector is finalized, it is safe to save the locations
10381 // of any forward GV references that need updating later.
10382 for (auto I : IdToIndexMap) {
10383 auto &Infos = ForwardRefValueInfos[I.first];
10384 for (auto P : I.second) {
10385 assert(Calls[P.first].first.getRef() == FwdVIRef &&
10386 "Forward referenced ValueInfo expected to be empty");
10387 Infos.emplace_back(args: &Calls[P.first].first, args&: P.second);
10388 }
10389 }
10390
10391 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in calls"))
10392 return true;
10393
10394 return false;
10395}
10396
10397/// Hotness
10398/// := ('unknown'|'cold'|'none'|'hot'|'critical')
10399bool LLParser::parseHotness(CalleeInfo::HotnessType &Hotness) {
10400 switch (Lex.getKind()) {
10401 case lltok::kw_unknown:
10402 Hotness = CalleeInfo::HotnessType::Unknown;
10403 break;
10404 case lltok::kw_cold:
10405 Hotness = CalleeInfo::HotnessType::Cold;
10406 break;
10407 case lltok::kw_none:
10408 Hotness = CalleeInfo::HotnessType::None;
10409 break;
10410 case lltok::kw_hot:
10411 Hotness = CalleeInfo::HotnessType::Hot;
10412 break;
10413 case lltok::kw_critical:
10414 Hotness = CalleeInfo::HotnessType::Critical;
10415 break;
10416 default:
10417 return error(L: Lex.getLoc(), Msg: "invalid call edge hotness");
10418 }
10419 Lex.Lex();
10420 return false;
10421}
10422
10423/// OptionalVTableFuncs
10424/// := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')'
10425/// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')'
10426bool LLParser::parseOptionalVTableFuncs(VTableFuncList &VTableFuncs) {
10427 assert(Lex.getKind() == lltok::kw_vTableFuncs);
10428 Lex.Lex();
10429
10430 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in vTableFuncs") ||
10431 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in vTableFuncs"))
10432 return true;
10433
10434 IdToIndexMapType IdToIndexMap;
10435 // parse each virtual function pair
10436 do {
10437 ValueInfo VI;
10438 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in vTableFunc") ||
10439 parseToken(T: lltok::kw_virtFunc, ErrMsg: "expected 'callee' in vTableFunc") ||
10440 parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
10441 return true;
10442
10443 LocTy Loc = Lex.getLoc();
10444 unsigned GVId;
10445 if (parseGVReference(VI, GVId))
10446 return true;
10447
10448 uint64_t Offset;
10449 if (parseToken(T: lltok::comma, ErrMsg: "expected comma") ||
10450 parseToken(T: lltok::kw_offset, ErrMsg: "expected offset") ||
10451 parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseUInt64(Val&: Offset))
10452 return true;
10453
10454 // Keep track of the VTableFuncs array index needing a forward reference.
10455 // We will save the location of the ValueInfo needing an update, but
10456 // can only do so once the std::vector is finalized.
10457 if (VI == EmptyVI)
10458 IdToIndexMap[GVId].push_back(x: std::make_pair(x: VTableFuncs.size(), y&: Loc));
10459 VTableFuncs.push_back(x: {VI, Offset});
10460
10461 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in vTableFunc"))
10462 return true;
10463 } while (EatIfPresent(T: lltok::comma));
10464
10465 // Now that the VTableFuncs vector is finalized, it is safe to save the
10466 // locations of any forward GV references that need updating later.
10467 for (auto I : IdToIndexMap) {
10468 auto &Infos = ForwardRefValueInfos[I.first];
10469 for (auto P : I.second) {
10470 assert(VTableFuncs[P.first].FuncVI == EmptyVI &&
10471 "Forward referenced ValueInfo expected to be empty");
10472 Infos.emplace_back(args: &VTableFuncs[P.first].FuncVI, args&: P.second);
10473 }
10474 }
10475
10476 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in vTableFuncs"))
10477 return true;
10478
10479 return false;
10480}
10481
10482/// ParamNo := 'param' ':' UInt64
10483bool LLParser::parseParamNo(uint64_t &ParamNo) {
10484 if (parseToken(T: lltok::kw_param, ErrMsg: "expected 'param' here") ||
10485 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") || parseUInt64(Val&: ParamNo))
10486 return true;
10487 return false;
10488}
10489
10490/// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']'
10491bool LLParser::parseParamAccessOffset(ConstantRange &Range) {
10492 APSInt Lower;
10493 APSInt Upper;
10494 auto ParseAPSInt = [&](APSInt &Val) {
10495 if (Lex.getKind() != lltok::APSInt)
10496 return tokError(Msg: "expected integer");
10497 Val = Lex.getAPSIntVal();
10498 Val = Val.extOrTrunc(width: FunctionSummary::ParamAccess::RangeWidth);
10499 Val.setIsSigned(true);
10500 Lex.Lex();
10501 return false;
10502 };
10503 if (parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here") ||
10504 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10505 parseToken(T: lltok::lsquare, ErrMsg: "expected '[' here") || ParseAPSInt(Lower) ||
10506 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") || ParseAPSInt(Upper) ||
10507 parseToken(T: lltok::rsquare, ErrMsg: "expected ']' here"))
10508 return true;
10509
10510 ++Upper;
10511 Range =
10512 (Lower == Upper && !Lower.isMaxValue())
10513 ? ConstantRange::getEmpty(BitWidth: FunctionSummary::ParamAccess::RangeWidth)
10514 : ConstantRange(Lower, Upper);
10515
10516 return false;
10517}
10518
10519/// ParamAccessCall
10520/// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')'
10521bool LLParser::parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call,
10522 IdLocListType &IdLocList) {
10523 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
10524 parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' here") ||
10525 parseToken(T: lltok::colon, ErrMsg: "expected ':' here"))
10526 return true;
10527
10528 unsigned GVId;
10529 ValueInfo VI;
10530 LocTy Loc = Lex.getLoc();
10531 if (parseGVReference(VI, GVId))
10532 return true;
10533
10534 Call.Callee = VI;
10535 IdLocList.emplace_back(args&: GVId, args&: Loc);
10536
10537 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
10538 parseParamNo(ParamNo&: Call.ParamNo) ||
10539 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
10540 parseParamAccessOffset(Range&: Call.Offsets))
10541 return true;
10542
10543 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10544 return true;
10545
10546 return false;
10547}
10548
10549/// ParamAccess
10550/// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')'
10551/// OptionalParamAccessCalls := '(' Call [',' Call]* ')'
10552bool LLParser::parseParamAccess(FunctionSummary::ParamAccess &Param,
10553 IdLocListType &IdLocList) {
10554 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
10555 parseParamNo(ParamNo&: Param.ParamNo) ||
10556 parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
10557 parseParamAccessOffset(Range&: Param.Use))
10558 return true;
10559
10560 if (EatIfPresent(T: lltok::comma)) {
10561 if (parseToken(T: lltok::kw_calls, ErrMsg: "expected 'calls' here") ||
10562 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10563 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
10564 return true;
10565 do {
10566 FunctionSummary::ParamAccess::Call Call;
10567 if (parseParamAccessCall(Call, IdLocList))
10568 return true;
10569 Param.Calls.push_back(x: Call);
10570 } while (EatIfPresent(T: lltok::comma));
10571
10572 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10573 return true;
10574 }
10575
10576 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10577 return true;
10578
10579 return false;
10580}
10581
10582/// OptionalParamAccesses
10583/// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')'
10584bool LLParser::parseOptionalParamAccesses(
10585 std::vector<FunctionSummary::ParamAccess> &Params) {
10586 assert(Lex.getKind() == lltok::kw_params);
10587 Lex.Lex();
10588
10589 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10590 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
10591 return true;
10592
10593 IdLocListType VContexts;
10594 size_t CallsNum = 0;
10595 do {
10596 FunctionSummary::ParamAccess ParamAccess;
10597 if (parseParamAccess(Param&: ParamAccess, IdLocList&: VContexts))
10598 return true;
10599 CallsNum += ParamAccess.Calls.size();
10600 assert(VContexts.size() == CallsNum);
10601 (void)CallsNum;
10602 Params.emplace_back(args: std::move(ParamAccess));
10603 } while (EatIfPresent(T: lltok::comma));
10604
10605 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10606 return true;
10607
10608 // Now that the Params is finalized, it is safe to save the locations
10609 // of any forward GV references that need updating later.
10610 IdLocListType::const_iterator ItContext = VContexts.begin();
10611 for (auto &PA : Params) {
10612 for (auto &C : PA.Calls) {
10613 if (C.Callee.getRef() == FwdVIRef)
10614 ForwardRefValueInfos[ItContext->first].emplace_back(args: &C.Callee,
10615 args: ItContext->second);
10616 ++ItContext;
10617 }
10618 }
10619 assert(ItContext == VContexts.end());
10620
10621 return false;
10622}
10623
10624/// OptionalRefs
10625/// := 'refs' ':' '(' GVReference [',' GVReference]* ')'
10626bool LLParser::parseOptionalRefs(SmallVectorImpl<ValueInfo> &Refs) {
10627 assert(Lex.getKind() == lltok::kw_refs);
10628 Lex.Lex();
10629
10630 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in refs") ||
10631 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in refs"))
10632 return true;
10633
10634 struct ValueContext {
10635 ValueInfo VI;
10636 unsigned GVId;
10637 LocTy Loc;
10638 };
10639 std::vector<ValueContext> VContexts;
10640 // parse each ref edge
10641 do {
10642 ValueContext VC;
10643 VC.Loc = Lex.getLoc();
10644 if (parseGVReference(VI&: VC.VI, GVId&: VC.GVId))
10645 return true;
10646 VContexts.push_back(x: VC);
10647 } while (EatIfPresent(T: lltok::comma));
10648
10649 // Sort value contexts so that ones with writeonly
10650 // and readonly ValueInfo are at the end of VContexts vector.
10651 // See FunctionSummary::specialRefCounts()
10652 llvm::sort(C&: VContexts, Comp: [](const ValueContext &VC1, const ValueContext &VC2) {
10653 return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier();
10654 });
10655
10656 IdToIndexMapType IdToIndexMap;
10657 for (auto &VC : VContexts) {
10658 // Keep track of the Refs array index needing a forward reference.
10659 // We will save the location of the ValueInfo needing an update, but
10660 // can only do so once the std::vector is finalized.
10661 if (VC.VI.getRef() == FwdVIRef)
10662 IdToIndexMap[VC.GVId].push_back(x: std::make_pair(x: Refs.size(), y&: VC.Loc));
10663 Refs.push_back(Elt: VC.VI);
10664 }
10665
10666 // Now that the Refs vector is finalized, it is safe to save the locations
10667 // of any forward GV references that need updating later.
10668 for (auto I : IdToIndexMap) {
10669 auto &Infos = ForwardRefValueInfos[I.first];
10670 for (auto P : I.second) {
10671 assert(Refs[P.first].getRef() == FwdVIRef &&
10672 "Forward referenced ValueInfo expected to be empty");
10673 Infos.emplace_back(args: &Refs[P.first], args&: P.second);
10674 }
10675 }
10676
10677 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in refs"))
10678 return true;
10679
10680 return false;
10681}
10682
10683/// OptionalTypeIdInfo
10684/// := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]?
10685/// [',' TypeCheckedLoadVCalls]? [',' TypeTestAssumeConstVCalls]?
10686/// [',' TypeCheckedLoadConstVCalls]? ')'
10687bool LLParser::parseOptionalTypeIdInfo(
10688 FunctionSummary::TypeIdInfo &TypeIdInfo) {
10689 assert(Lex.getKind() == lltok::kw_typeIdInfo);
10690 Lex.Lex();
10691
10692 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10693 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in typeIdInfo"))
10694 return true;
10695
10696 do {
10697 switch (Lex.getKind()) {
10698 case lltok::kw_typeTests:
10699 if (parseTypeTests(TypeTests&: TypeIdInfo.TypeTests))
10700 return true;
10701 break;
10702 case lltok::kw_typeTestAssumeVCalls:
10703 if (parseVFuncIdList(Kind: lltok::kw_typeTestAssumeVCalls,
10704 VFuncIdList&: TypeIdInfo.TypeTestAssumeVCalls))
10705 return true;
10706 break;
10707 case lltok::kw_typeCheckedLoadVCalls:
10708 if (parseVFuncIdList(Kind: lltok::kw_typeCheckedLoadVCalls,
10709 VFuncIdList&: TypeIdInfo.TypeCheckedLoadVCalls))
10710 return true;
10711 break;
10712 case lltok::kw_typeTestAssumeConstVCalls:
10713 if (parseConstVCallList(Kind: lltok::kw_typeTestAssumeConstVCalls,
10714 ConstVCallList&: TypeIdInfo.TypeTestAssumeConstVCalls))
10715 return true;
10716 break;
10717 case lltok::kw_typeCheckedLoadConstVCalls:
10718 if (parseConstVCallList(Kind: lltok::kw_typeCheckedLoadConstVCalls,
10719 ConstVCallList&: TypeIdInfo.TypeCheckedLoadConstVCalls))
10720 return true;
10721 break;
10722 default:
10723 return error(L: Lex.getLoc(), Msg: "invalid typeIdInfo list type");
10724 }
10725 } while (EatIfPresent(T: lltok::comma));
10726
10727 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in typeIdInfo"))
10728 return true;
10729
10730 return false;
10731}
10732
10733/// TypeTests
10734/// ::= 'typeTests' ':' '(' (SummaryID | UInt64)
10735/// [',' (SummaryID | UInt64)]* ')'
10736bool LLParser::parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) {
10737 assert(Lex.getKind() == lltok::kw_typeTests);
10738 Lex.Lex();
10739
10740 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10741 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in typeIdInfo"))
10742 return true;
10743
10744 IdToIndexMapType IdToIndexMap;
10745 do {
10746 GlobalValue::GUID GUID = 0;
10747 if (Lex.getKind() == lltok::SummaryID) {
10748 unsigned ID = Lex.getUIntVal();
10749 LocTy Loc = Lex.getLoc();
10750 // Keep track of the TypeTests array index needing a forward reference.
10751 // We will save the location of the GUID needing an update, but
10752 // can only do so once the std::vector is finalized.
10753 IdToIndexMap[ID].push_back(x: std::make_pair(x: TypeTests.size(), y&: Loc));
10754 Lex.Lex();
10755 } else if (parseUInt64(Val&: GUID))
10756 return true;
10757 TypeTests.push_back(x: GUID);
10758 } while (EatIfPresent(T: lltok::comma));
10759
10760 // Now that the TypeTests vector is finalized, it is safe to save the
10761 // locations of any forward GV references that need updating later.
10762 for (auto I : IdToIndexMap) {
10763 auto &Ids = ForwardRefTypeIds[I.first];
10764 for (auto P : I.second) {
10765 assert(TypeTests[P.first] == 0 &&
10766 "Forward referenced type id GUID expected to be 0");
10767 Ids.emplace_back(args: &TypeTests[P.first], args&: P.second);
10768 }
10769 }
10770
10771 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in typeIdInfo"))
10772 return true;
10773
10774 return false;
10775}
10776
10777/// VFuncIdList
10778/// ::= Kind ':' '(' VFuncId [',' VFuncId]* ')'
10779bool LLParser::parseVFuncIdList(
10780 lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) {
10781 assert(Lex.getKind() == Kind);
10782 Lex.Lex();
10783
10784 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10785 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
10786 return true;
10787
10788 IdToIndexMapType IdToIndexMap;
10789 do {
10790 FunctionSummary::VFuncId VFuncId;
10791 if (parseVFuncId(VFuncId, IdToIndexMap, Index: VFuncIdList.size()))
10792 return true;
10793 VFuncIdList.push_back(x: VFuncId);
10794 } while (EatIfPresent(T: lltok::comma));
10795
10796 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10797 return true;
10798
10799 // Now that the VFuncIdList vector is finalized, it is safe to save the
10800 // locations of any forward GV references that need updating later.
10801 for (auto I : IdToIndexMap) {
10802 auto &Ids = ForwardRefTypeIds[I.first];
10803 for (auto P : I.second) {
10804 assert(VFuncIdList[P.first].GUID == 0 &&
10805 "Forward referenced type id GUID expected to be 0");
10806 Ids.emplace_back(args: &VFuncIdList[P.first].GUID, args&: P.second);
10807 }
10808 }
10809
10810 return false;
10811}
10812
10813/// ConstVCallList
10814/// ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')'
10815bool LLParser::parseConstVCallList(
10816 lltok::Kind Kind,
10817 std::vector<FunctionSummary::ConstVCall> &ConstVCallList) {
10818 assert(Lex.getKind() == Kind);
10819 Lex.Lex();
10820
10821 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10822 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
10823 return true;
10824
10825 IdToIndexMapType IdToIndexMap;
10826 do {
10827 FunctionSummary::ConstVCall ConstVCall;
10828 if (parseConstVCall(ConstVCall, IdToIndexMap, Index: ConstVCallList.size()))
10829 return true;
10830 ConstVCallList.push_back(x: ConstVCall);
10831 } while (EatIfPresent(T: lltok::comma));
10832
10833 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10834 return true;
10835
10836 // Now that the ConstVCallList vector is finalized, it is safe to save the
10837 // locations of any forward GV references that need updating later.
10838 for (auto I : IdToIndexMap) {
10839 auto &Ids = ForwardRefTypeIds[I.first];
10840 for (auto P : I.second) {
10841 assert(ConstVCallList[P.first].VFunc.GUID == 0 &&
10842 "Forward referenced type id GUID expected to be 0");
10843 Ids.emplace_back(args: &ConstVCallList[P.first].VFunc.GUID, args&: P.second);
10844 }
10845 }
10846
10847 return false;
10848}
10849
10850/// ConstVCall
10851/// ::= '(' VFuncId ',' Args ')'
10852bool LLParser::parseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
10853 IdToIndexMapType &IdToIndexMap, unsigned Index) {
10854 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here") ||
10855 parseVFuncId(VFuncId&: ConstVCall.VFunc, IdToIndexMap, Index))
10856 return true;
10857
10858 if (EatIfPresent(T: lltok::comma))
10859 if (parseArgs(Args&: ConstVCall.Args))
10860 return true;
10861
10862 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10863 return true;
10864
10865 return false;
10866}
10867
10868/// VFuncId
10869/// ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ','
10870/// 'offset' ':' UInt64 ')'
10871bool LLParser::parseVFuncId(FunctionSummary::VFuncId &VFuncId,
10872 IdToIndexMapType &IdToIndexMap, unsigned Index) {
10873 assert(Lex.getKind() == lltok::kw_vFuncId);
10874 Lex.Lex();
10875
10876 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10877 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
10878 return true;
10879
10880 if (Lex.getKind() == lltok::SummaryID) {
10881 VFuncId.GUID = 0;
10882 unsigned ID = Lex.getUIntVal();
10883 LocTy Loc = Lex.getLoc();
10884 // Keep track of the array index needing a forward reference.
10885 // We will save the location of the GUID needing an update, but
10886 // can only do so once the caller's std::vector is finalized.
10887 IdToIndexMap[ID].push_back(x: std::make_pair(x&: Index, y&: Loc));
10888 Lex.Lex();
10889 } else if (parseToken(T: lltok::kw_guid, ErrMsg: "expected 'guid' here") ||
10890 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10891 parseUInt64(Val&: VFuncId.GUID))
10892 return true;
10893
10894 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here") ||
10895 parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here") ||
10896 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10897 parseUInt64(Val&: VFuncId.Offset) ||
10898 parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10899 return true;
10900
10901 return false;
10902}
10903
10904/// GVFlags
10905/// ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ','
10906/// 'visibility' ':' Flag 'notEligibleToImport' ':' Flag ','
10907/// 'live' ':' Flag ',' 'dsoLocal' ':' Flag ','
10908/// 'canAutoHide' ':' Flag ',' ')'
10909bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) {
10910 assert(Lex.getKind() == lltok::kw_flags);
10911 Lex.Lex();
10912
10913 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10914 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
10915 return true;
10916
10917 do {
10918 unsigned Flag = 0;
10919 switch (Lex.getKind()) {
10920 case lltok::kw_linkage:
10921 Lex.Lex();
10922 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
10923 return true;
10924 bool HasLinkage;
10925 GVFlags.Linkage = parseOptionalLinkageAux(Kind: Lex.getKind(), HasLinkage);
10926 assert(HasLinkage && "Linkage not optional in summary entry");
10927 Lex.Lex();
10928 break;
10929 case lltok::kw_visibility:
10930 Lex.Lex();
10931 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
10932 return true;
10933 parseOptionalVisibility(Res&: Flag);
10934 GVFlags.Visibility = Flag;
10935 break;
10936 case lltok::kw_notEligibleToImport:
10937 Lex.Lex();
10938 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val&: Flag))
10939 return true;
10940 GVFlags.NotEligibleToImport = Flag;
10941 break;
10942 case lltok::kw_live:
10943 Lex.Lex();
10944 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val&: Flag))
10945 return true;
10946 GVFlags.Live = Flag;
10947 break;
10948 case lltok::kw_dsoLocal:
10949 Lex.Lex();
10950 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val&: Flag))
10951 return true;
10952 GVFlags.DSOLocal = Flag;
10953 break;
10954 case lltok::kw_canAutoHide:
10955 Lex.Lex();
10956 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'") || parseFlag(Val&: Flag))
10957 return true;
10958 GVFlags.CanAutoHide = Flag;
10959 break;
10960 case lltok::kw_importType:
10961 Lex.Lex();
10962 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
10963 return true;
10964 GlobalValueSummary::ImportKind IK;
10965 if (parseOptionalImportType(Kind: Lex.getKind(), Res&: IK))
10966 return true;
10967 GVFlags.ImportType = static_cast<unsigned>(IK);
10968 Lex.Lex();
10969 break;
10970 default:
10971 return error(L: Lex.getLoc(), Msg: "expected gv flag type");
10972 }
10973 } while (EatIfPresent(T: lltok::comma));
10974
10975 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here"))
10976 return true;
10977
10978 return false;
10979}
10980
10981/// GVarFlags
10982/// ::= 'varFlags' ':' '(' 'readonly' ':' Flag
10983/// ',' 'writeonly' ':' Flag
10984/// ',' 'constant' ':' Flag ')'
10985bool LLParser::parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) {
10986 assert(Lex.getKind() == lltok::kw_varFlags);
10987 Lex.Lex();
10988
10989 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
10990 parseToken(T: lltok::lparen, ErrMsg: "expected '(' here"))
10991 return true;
10992
10993 auto ParseRest = [this](unsigned int &Val) {
10994 Lex.Lex();
10995 if (parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
10996 return true;
10997 return parseFlag(Val);
10998 };
10999
11000 do {
11001 unsigned Flag = 0;
11002 switch (Lex.getKind()) {
11003 case lltok::kw_readonly:
11004 if (ParseRest(Flag))
11005 return true;
11006 GVarFlags.MaybeReadOnly = Flag;
11007 break;
11008 case lltok::kw_writeonly:
11009 if (ParseRest(Flag))
11010 return true;
11011 GVarFlags.MaybeWriteOnly = Flag;
11012 break;
11013 case lltok::kw_constant:
11014 if (ParseRest(Flag))
11015 return true;
11016 GVarFlags.Constant = Flag;
11017 break;
11018 case lltok::kw_vcall_visibility:
11019 if (ParseRest(Flag))
11020 return true;
11021 GVarFlags.VCallVisibility = Flag;
11022 break;
11023 default:
11024 return error(L: Lex.getLoc(), Msg: "expected gvar flag type");
11025 }
11026 } while (EatIfPresent(T: lltok::comma));
11027 return parseToken(T: lltok::rparen, ErrMsg: "expected ')' here");
11028}
11029
11030/// ModuleReference
11031/// ::= 'module' ':' UInt
11032bool LLParser::parseModuleReference(StringRef &ModulePath) {
11033 // parse module id.
11034 if (parseToken(T: lltok::kw_module, ErrMsg: "expected 'module' here") ||
11035 parseToken(T: lltok::colon, ErrMsg: "expected ':' here") ||
11036 parseToken(T: lltok::SummaryID, ErrMsg: "expected module ID"))
11037 return true;
11038
11039 unsigned ModuleID = Lex.getUIntVal();
11040 auto I = ModuleIdMap.find(x: ModuleID);
11041 // We should have already parsed all module IDs
11042 assert(I != ModuleIdMap.end());
11043 ModulePath = I->second;
11044 return false;
11045}
11046
11047/// GVReference
11048/// ::= SummaryID
11049bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) {
11050 bool WriteOnly = false, ReadOnly = EatIfPresent(T: lltok::kw_readonly);
11051 if (!ReadOnly)
11052 WriteOnly = EatIfPresent(T: lltok::kw_writeonly);
11053 if (parseToken(T: lltok::SummaryID, ErrMsg: "expected GV ID"))
11054 return true;
11055
11056 GVId = Lex.getUIntVal();
11057 // Check if we already have a VI for this GV
11058 if (GVId < NumberedValueInfos.size() && NumberedValueInfos[GVId]) {
11059 assert(NumberedValueInfos[GVId].getRef() != FwdVIRef);
11060 VI = NumberedValueInfos[GVId];
11061 } else
11062 // We will create a forward reference to the stored location.
11063 VI = ValueInfo(false, FwdVIRef);
11064
11065 if (ReadOnly)
11066 VI.setReadOnly();
11067 if (WriteOnly)
11068 VI.setWriteOnly();
11069 return false;
11070}
11071
11072/// OptionalAllocs
11073/// := 'allocs' ':' '(' Alloc [',' Alloc]* ')'
11074/// Alloc ::= '(' 'versions' ':' '(' Version [',' Version]* ')'
11075/// ',' MemProfs ')'
11076/// Version ::= UInt32
11077bool LLParser::parseOptionalAllocs(std::vector<AllocInfo> &Allocs) {
11078 assert(Lex.getKind() == lltok::kw_allocs);
11079 Lex.Lex();
11080
11081 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in allocs") ||
11082 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in allocs"))
11083 return true;
11084
11085 // parse each alloc
11086 do {
11087 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in alloc") ||
11088 parseToken(T: lltok::kw_versions, ErrMsg: "expected 'versions' in alloc") ||
11089 parseToken(T: lltok::colon, ErrMsg: "expected ':'") ||
11090 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in versions"))
11091 return true;
11092
11093 SmallVector<uint8_t> Versions;
11094 do {
11095 uint8_t V = 0;
11096 if (parseAllocType(AllocType&: V))
11097 return true;
11098 Versions.push_back(Elt: V);
11099 } while (EatIfPresent(T: lltok::comma));
11100
11101 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in versions") ||
11102 parseToken(T: lltok::comma, ErrMsg: "expected ',' in alloc"))
11103 return true;
11104
11105 std::vector<MIBInfo> MIBs;
11106 if (parseMemProfs(MIBs))
11107 return true;
11108
11109 Allocs.push_back(x: {Versions, MIBs});
11110
11111 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in alloc"))
11112 return true;
11113 } while (EatIfPresent(T: lltok::comma));
11114
11115 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in allocs"))
11116 return true;
11117
11118 return false;
11119}
11120
11121/// MemProfs
11122/// := 'memProf' ':' '(' MemProf [',' MemProf]* ')'
11123/// MemProf ::= '(' 'type' ':' AllocType
11124/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
11125/// StackId ::= UInt64
11126bool LLParser::parseMemProfs(std::vector<MIBInfo> &MIBs) {
11127 assert(Lex.getKind() == lltok::kw_memProf);
11128 Lex.Lex();
11129
11130 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in memprof") ||
11131 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in memprof"))
11132 return true;
11133
11134 // parse each MIB
11135 do {
11136 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in memprof") ||
11137 parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' in memprof") ||
11138 parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
11139 return true;
11140
11141 uint8_t AllocType;
11142 if (parseAllocType(AllocType))
11143 return true;
11144
11145 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' in memprof") ||
11146 parseToken(T: lltok::kw_stackIds, ErrMsg: "expected 'stackIds' in memprof") ||
11147 parseToken(T: lltok::colon, ErrMsg: "expected ':'") ||
11148 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in stackIds"))
11149 return true;
11150
11151 SmallVector<unsigned> StackIdIndices;
11152 // Combined index alloc records may not have a stack id list.
11153 if (Lex.getKind() != lltok::rparen) {
11154 do {
11155 uint64_t StackId = 0;
11156 if (parseUInt64(Val&: StackId))
11157 return true;
11158 StackIdIndices.push_back(Elt: Index->addOrGetStackIdIndex(StackId));
11159 } while (EatIfPresent(T: lltok::comma));
11160 }
11161
11162 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in stackIds"))
11163 return true;
11164
11165 MIBs.push_back(x: {(AllocationType)AllocType, StackIdIndices});
11166
11167 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in memprof"))
11168 return true;
11169 } while (EatIfPresent(T: lltok::comma));
11170
11171 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in memprof"))
11172 return true;
11173
11174 return false;
11175}
11176
11177/// AllocType
11178/// := ('none'|'notcold'|'cold'|'hot')
11179bool LLParser::parseAllocType(uint8_t &AllocType) {
11180 switch (Lex.getKind()) {
11181 case lltok::kw_none:
11182 AllocType = (uint8_t)AllocationType::None;
11183 break;
11184 case lltok::kw_notcold:
11185 AllocType = (uint8_t)AllocationType::NotCold;
11186 break;
11187 case lltok::kw_cold:
11188 AllocType = (uint8_t)AllocationType::Cold;
11189 break;
11190 case lltok::kw_hot:
11191 AllocType = (uint8_t)AllocationType::Hot;
11192 break;
11193 default:
11194 return error(L: Lex.getLoc(), Msg: "invalid alloc type");
11195 }
11196 Lex.Lex();
11197 return false;
11198}
11199
11200/// OptionalCallsites
11201/// := 'callsites' ':' '(' Callsite [',' Callsite]* ')'
11202/// Callsite ::= '(' 'callee' ':' GVReference
11203/// ',' 'clones' ':' '(' Version [',' Version]* ')'
11204/// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')'
11205/// Version ::= UInt32
11206/// StackId ::= UInt64
11207bool LLParser::parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites) {
11208 assert(Lex.getKind() == lltok::kw_callsites);
11209 Lex.Lex();
11210
11211 if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in callsites") ||
11212 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in callsites"))
11213 return true;
11214
11215 IdToIndexMapType IdToIndexMap;
11216 // parse each callsite
11217 do {
11218 if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in callsite") ||
11219 parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' in callsite") ||
11220 parseToken(T: lltok::colon, ErrMsg: "expected ':'"))
11221 return true;
11222
11223 ValueInfo VI;
11224 unsigned GVId = 0;
11225 LocTy Loc = Lex.getLoc();
11226 if (!EatIfPresent(T: lltok::kw_null)) {
11227 if (parseGVReference(VI, GVId))
11228 return true;
11229 }
11230
11231 if (parseToken(T: lltok::comma, ErrMsg: "expected ',' in callsite") ||
11232 parseToken(T: lltok::kw_clones, ErrMsg: "expected 'clones' in callsite") ||
11233 parseToken(T: lltok::colon, ErrMsg: "expected ':'") ||
11234 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in clones"))
11235 return true;
11236
11237 SmallVector<unsigned> Clones;
11238 do {
11239 unsigned V = 0;
11240 if (parseUInt32(Val&: V))
11241 return true;
11242 Clones.push_back(Elt: V);
11243 } while (EatIfPresent(T: lltok::comma));
11244
11245 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in clones") ||
11246 parseToken(T: lltok::comma, ErrMsg: "expected ',' in callsite") ||
11247 parseToken(T: lltok::kw_stackIds, ErrMsg: "expected 'stackIds' in callsite") ||
11248 parseToken(T: lltok::colon, ErrMsg: "expected ':'") ||
11249 parseToken(T: lltok::lparen, ErrMsg: "expected '(' in stackIds"))
11250 return true;
11251
11252 SmallVector<unsigned> StackIdIndices;
11253 // Synthesized callsite records will not have a stack id list.
11254 if (Lex.getKind() != lltok::rparen) {
11255 do {
11256 uint64_t StackId = 0;
11257 if (parseUInt64(Val&: StackId))
11258 return true;
11259 StackIdIndices.push_back(Elt: Index->addOrGetStackIdIndex(StackId));
11260 } while (EatIfPresent(T: lltok::comma));
11261 }
11262
11263 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in stackIds"))
11264 return true;
11265
11266 // Keep track of the Callsites array index needing a forward reference.
11267 // We will save the location of the ValueInfo needing an update, but
11268 // can only do so once the SmallVector is finalized.
11269 if (VI.getRef() == FwdVIRef)
11270 IdToIndexMap[GVId].push_back(x: std::make_pair(x: Callsites.size(), y&: Loc));
11271 Callsites.push_back(x: {VI, Clones, StackIdIndices});
11272
11273 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in callsite"))
11274 return true;
11275 } while (EatIfPresent(T: lltok::comma));
11276
11277 // Now that the Callsites vector is finalized, it is safe to save the
11278 // locations of any forward GV references that need updating later.
11279 for (auto I : IdToIndexMap) {
11280 auto &Infos = ForwardRefValueInfos[I.first];
11281 for (auto P : I.second) {
11282 assert(Callsites[P.first].Callee.getRef() == FwdVIRef &&
11283 "Forward referenced ValueInfo expected to be empty");
11284 Infos.emplace_back(args: &Callsites[P.first].Callee, args&: P.second);
11285 }
11286 }
11287
11288 if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in callsites"))
11289 return true;
11290
11291 return false;
11292}
11293