1//===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
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#ifndef LLVM_ASMPARSER_LLPARSER_H
14#define LLVM_ASMPARSER_LLPARSER_H
15
16#include "LLLexer.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/AsmParser/NumberedValues.h"
19#include "llvm/AsmParser/Parser.h"
20#include "llvm/IR/Attributes.h"
21#include "llvm/IR/FMF.h"
22#include "llvm/IR/Instructions.h"
23#include "llvm/IR/ModuleSummaryIndex.h"
24#include "llvm/Support/ModRef.h"
25#include <map>
26#include <optional>
27
28namespace llvm {
29 class Module;
30 class ConstantRange;
31 class FunctionType;
32 class GlobalObject;
33 class SMDiagnostic;
34 class SMLoc;
35 class SourceMgr;
36 class Type;
37 struct MaybeAlign;
38 class Function;
39 class Value;
40 class BasicBlock;
41 class Instruction;
42 class Constant;
43 class GlobalValue;
44 class Comdat;
45 class MDString;
46 class MDNode;
47 struct SlotMapping;
48
49 /// ValID - Represents a reference of a definition of some sort with no type.
50 /// There are several cases where we have to parse the value but where the
51 /// type can depend on later context. This may either be a numeric reference
52 /// or a symbolic (%var) reference. This is just a discriminated union.
53 struct ValID {
54 enum {
55 t_LocalID, // ID in UIntVal.
56 t_GlobalID, // ID in UIntVal.
57 t_LocalName, // Name in StrVal.
58 t_GlobalName, // Name in StrVal.
59 t_APSInt, // Value in APSIntVal.
60 t_APFloat, // Value in APFloatVal.
61 t_Null, // No value.
62 t_Undef, // No value.
63 t_Zero, // No value.
64 t_None, // No value.
65 t_Poison, // No value.
66 t_EmptyArray, // No value: []
67 t_Constant, // Value in ConstantVal.
68 t_ConstantSplat, // Value in ConstantVal.
69 t_InlineAsm, // Value in FTy/StrVal/StrVal2/UIntVal.
70 t_ConstantStruct, // Value in ConstantStructElts.
71 t_PackedConstantStruct // Value in ConstantStructElts.
72 } Kind = t_LocalID;
73
74 LLLexer::LocTy Loc;
75 unsigned UIntVal;
76 FunctionType *FTy = nullptr;
77 std::string StrVal, StrVal2;
78 APSInt APSIntVal;
79 APFloat APFloatVal{0.0};
80 Constant *ConstantVal;
81 std::unique_ptr<Constant *[]> ConstantStructElts;
82 bool NoCFI = false;
83
84 ValID() = default;
85 ValID(const ValID &RHS)
86 : Kind(RHS.Kind), Loc(RHS.Loc), UIntVal(RHS.UIntVal), FTy(RHS.FTy),
87 StrVal(RHS.StrVal), StrVal2(RHS.StrVal2), APSIntVal(RHS.APSIntVal),
88 APFloatVal(RHS.APFloatVal), ConstantVal(RHS.ConstantVal),
89 NoCFI(RHS.NoCFI) {
90 assert(!RHS.ConstantStructElts);
91 }
92
93 bool operator<(const ValID &RHS) const {
94 assert(Kind == RHS.Kind && "Comparing ValIDs of different kinds");
95 if (Kind == t_LocalID || Kind == t_GlobalID)
96 return UIntVal < RHS.UIntVal;
97 assert((Kind == t_LocalName || Kind == t_GlobalName ||
98 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
99 "Ordering not defined for this ValID kind yet");
100 return StrVal < RHS.StrVal;
101 }
102 };
103
104 class LLParser {
105 public:
106 typedef LLLexer::LocTy LocTy;
107 private:
108 LLVMContext &Context;
109 // Lexer to determine whether to use opaque pointers or not.
110 LLLexer OPLex;
111 LLLexer Lex;
112 // Module being parsed, null if we are only parsing summary index.
113 Module *M;
114 // Summary index being parsed, null if we are only parsing Module.
115 ModuleSummaryIndex *Index;
116 SlotMapping *Slots;
117
118 SmallVector<Instruction*, 64> InstsWithTBAATag;
119
120 /// DIAssignID metadata does not support temporary RAUW so we cannot use
121 /// the normal metadata forward reference resolution method. Instead,
122 /// non-temporary DIAssignID are attached to instructions (recorded here)
123 /// then replaced later.
124 DenseMap<MDNode *, SmallVector<Instruction *, 2>> TempDIAssignIDAttachments;
125
126 // Type resolution handling data structures. The location is set when we
127 // have processed a use of the type but not a definition yet.
128 StringMap<std::pair<Type*, LocTy> > NamedTypes;
129 std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes;
130
131 std::map<unsigned, TrackingMDNodeRef> NumberedMetadata;
132 std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes;
133
134 // Global Value reference information.
135 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
136 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
137 NumberedValues<GlobalValue *> NumberedVals;
138
139 // Comdat forward reference information.
140 std::map<std::string, LocTy> ForwardRefComdats;
141
142 // References to blockaddress. The key is the function ValID, the value is
143 // a list of references to blocks in that function.
144 std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses;
145 class PerFunctionState;
146 /// Reference to per-function state to allow basic blocks to be
147 /// forward-referenced by blockaddress instructions within the same
148 /// function.
149 PerFunctionState *BlockAddressPFS;
150
151 // References to dso_local_equivalent. The key is the global's ValID, the
152 // value is a placeholder value that will be replaced. Note there are two
153 // maps for tracking ValIDs that are GlobalNames and ValIDs that are
154 // GlobalIDs. These are needed because "operator<" doesn't discriminate
155 // between the two.
156 std::map<ValID, GlobalValue *> ForwardRefDSOLocalEquivalentNames;
157 std::map<ValID, GlobalValue *> ForwardRefDSOLocalEquivalentIDs;
158
159 // Attribute builder reference information.
160 std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
161 std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
162
163 // Summary global value reference information.
164 std::map<unsigned, std::vector<std::pair<ValueInfo *, LocTy>>>
165 ForwardRefValueInfos;
166 std::map<unsigned, std::vector<std::pair<AliasSummary *, LocTy>>>
167 ForwardRefAliasees;
168 std::vector<ValueInfo> NumberedValueInfos;
169
170 // Summary type id reference information.
171 std::map<unsigned, std::vector<std::pair<GlobalValue::GUID *, LocTy>>>
172 ForwardRefTypeIds;
173
174 // Map of module ID to path.
175 std::map<unsigned, StringRef> ModuleIdMap;
176
177 /// Only the llvm-as tool may set this to false to bypass
178 /// UpgradeDebuginfo so it can generate broken bitcode.
179 bool UpgradeDebugInfo;
180
181 std::string SourceFileName;
182
183 public:
184 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
185 ModuleSummaryIndex *Index, LLVMContext &Context,
186 SlotMapping *Slots = nullptr)
187 : Context(Context), OPLex(F, SM, Err, Context),
188 Lex(F, SM, Err, Context), M(M), Index(Index), Slots(Slots),
189 BlockAddressPFS(nullptr) {}
190 bool Run(
191 bool UpgradeDebugInfo,
192 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
193 return std::nullopt;
194 });
195
196 bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots);
197
198 bool parseTypeAtBeginning(Type *&Ty, unsigned &Read,
199 const SlotMapping *Slots);
200
201 LLVMContext &getContext() { return Context; }
202
203 private:
204 bool error(LocTy L, const Twine &Msg) const { return Lex.Error(ErrorLoc: L, Msg); }
205 bool tokError(const Twine &Msg) const { return error(L: Lex.getLoc(), Msg); }
206
207 bool checkValueID(LocTy L, StringRef Kind, StringRef Prefix,
208 unsigned NextID, unsigned ID) const;
209
210 /// Restore the internal name and slot mappings using the mappings that
211 /// were created at an earlier parsing stage.
212 void restoreParsingState(const SlotMapping *Slots);
213
214 /// getGlobalVal - Get a value with the specified name or ID, creating a
215 /// forward reference record if needed. This can return null if the value
216 /// exists but does not have the right type.
217 GlobalValue *getGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
218 GlobalValue *getGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
219
220 /// Get a Comdat with the specified name, creating a forward reference
221 /// record if needed.
222 Comdat *getComdat(const std::string &Name, LocTy Loc);
223
224 // Helper Routines.
225 bool parseToken(lltok::Kind T, const char *ErrMsg);
226 bool EatIfPresent(lltok::Kind T) {
227 if (Lex.getKind() != T) return false;
228 Lex.Lex();
229 return true;
230 }
231
232 FastMathFlags EatFastMathFlagsIfPresent() {
233 FastMathFlags FMF;
234 while (true)
235 switch (Lex.getKind()) {
236 case lltok::kw_fast: FMF.setFast(); Lex.Lex(); continue;
237 case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue;
238 case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue;
239 case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue;
240 case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
241 case lltok::kw_contract:
242 FMF.setAllowContract(true);
243 Lex.Lex();
244 continue;
245 case lltok::kw_reassoc: FMF.setAllowReassoc(); Lex.Lex(); continue;
246 case lltok::kw_afn: FMF.setApproxFunc(); Lex.Lex(); continue;
247 default: return FMF;
248 }
249 return FMF;
250 }
251
252 bool parseOptionalToken(lltok::Kind T, bool &Present,
253 LocTy *Loc = nullptr) {
254 if (Lex.getKind() != T) {
255 Present = false;
256 } else {
257 if (Loc)
258 *Loc = Lex.getLoc();
259 Lex.Lex();
260 Present = true;
261 }
262 return false;
263 }
264 bool parseStringConstant(std::string &Result);
265 bool parseUInt32(unsigned &Val);
266 bool parseUInt32(unsigned &Val, LocTy &Loc) {
267 Loc = Lex.getLoc();
268 return parseUInt32(Val);
269 }
270 bool parseUInt64(uint64_t &Val);
271 bool parseUInt64(uint64_t &Val, LocTy &Loc) {
272 Loc = Lex.getLoc();
273 return parseUInt64(Val);
274 }
275 bool parseFlag(unsigned &Val);
276
277 bool parseStringAttribute(AttrBuilder &B);
278
279 bool parseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
280 bool parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
281 bool parseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr &UnnamedAddr);
282 bool parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS = 0);
283 bool parseOptionalProgramAddrSpace(unsigned &AddrSpace) {
284 return parseOptionalAddrSpace(
285 AddrSpace, DefaultAS: M->getDataLayout().getProgramAddressSpace());
286 };
287 bool parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
288 bool InAttrGroup);
289 bool parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam);
290 bool parseOptionalParamAttrs(AttrBuilder &B) {
291 return parseOptionalParamOrReturnAttrs(B, IsParam: true);
292 }
293 bool parseOptionalReturnAttrs(AttrBuilder &B) {
294 return parseOptionalParamOrReturnAttrs(B, IsParam: false);
295 }
296 bool parseOptionalLinkage(unsigned &Res, bool &HasLinkage,
297 unsigned &Visibility, unsigned &DLLStorageClass,
298 bool &DSOLocal);
299 void parseOptionalDSOLocal(bool &DSOLocal);
300 void parseOptionalVisibility(unsigned &Res);
301 void parseOptionalDLLStorageClass(unsigned &Res);
302 bool parseOptionalCallingConv(unsigned &CC);
303 bool parseOptionalAlignment(MaybeAlign &Alignment,
304 bool AllowParens = false);
305 bool parseOptionalCodeModel(CodeModel::Model &model);
306 bool parseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);
307 bool parseOptionalUWTableKind(UWTableKind &Kind);
308 bool parseAllocKind(AllocFnKind &Kind);
309 std::optional<MemoryEffects> parseMemoryAttr();
310 unsigned parseNoFPClassAttr();
311 bool parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID,
312 AtomicOrdering &Ordering);
313 bool parseScope(SyncScope::ID &SSID);
314 bool parseOrdering(AtomicOrdering &Ordering);
315 bool parseOptionalStackAlignment(unsigned &Alignment);
316 bool parseOptionalCommaAlign(MaybeAlign &Alignment, bool &AteExtraComma);
317 bool parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc,
318 bool &AteExtraComma);
319 bool parseAllocSizeArguments(unsigned &BaseSizeArg,
320 std::optional<unsigned> &HowManyArg);
321 bool parseVScaleRangeArguments(unsigned &MinValue, unsigned &MaxValue);
322 bool parseIndexList(SmallVectorImpl<unsigned> &Indices,
323 bool &AteExtraComma);
324 bool parseIndexList(SmallVectorImpl<unsigned> &Indices) {
325 bool AteExtraComma;
326 if (parseIndexList(Indices, AteExtraComma))
327 return true;
328 if (AteExtraComma)
329 return tokError(Msg: "expected index");
330 return false;
331 }
332
333 // Top-Level Entities
334 bool parseTopLevelEntities();
335 void dropUnknownMetadataReferences();
336 bool validateEndOfModule(bool UpgradeDebugInfo);
337 bool validateEndOfIndex();
338 bool parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback);
339 bool parseTargetDefinition(std::string &TentativeDLStr, LocTy &DLStrLoc);
340 bool parseModuleAsm();
341 bool parseSourceFileName();
342 bool parseUnnamedType();
343 bool parseNamedType();
344 bool parseDeclare();
345 bool parseDefine();
346
347 bool parseGlobalType(bool &IsConstant);
348 bool parseUnnamedGlobal();
349 bool parseNamedGlobal();
350 bool parseGlobal(const std::string &Name, unsigned NameID, LocTy NameLoc,
351 unsigned Linkage, bool HasLinkage, unsigned Visibility,
352 unsigned DLLStorageClass, bool DSOLocal,
353 GlobalVariable::ThreadLocalMode TLM,
354 GlobalVariable::UnnamedAddr UnnamedAddr);
355 bool parseAliasOrIFunc(const std::string &Name, unsigned NameID,
356 LocTy NameLoc, unsigned L, unsigned Visibility,
357 unsigned DLLStorageClass, bool DSOLocal,
358 GlobalVariable::ThreadLocalMode TLM,
359 GlobalVariable::UnnamedAddr UnnamedAddr);
360 bool parseComdat();
361 bool parseStandaloneMetadata();
362 bool parseNamedMetadata();
363 bool parseMDString(MDString *&Result);
364 bool parseMDNodeID(MDNode *&Result);
365 bool parseUnnamedAttrGrp();
366 bool parseFnAttributeValuePairs(AttrBuilder &B,
367 std::vector<unsigned> &FwdRefAttrGrps,
368 bool inAttrGrp, LocTy &BuiltinLoc);
369 bool parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken,
370 Attribute::AttrKind AttrKind);
371
372 // Module Summary Index Parsing.
373 bool skipModuleSummaryEntry();
374 bool parseSummaryEntry();
375 bool parseModuleEntry(unsigned ID);
376 bool parseModuleReference(StringRef &ModulePath);
377 bool parseGVReference(ValueInfo &VI, unsigned &GVId);
378 bool parseSummaryIndexFlags();
379 bool parseBlockCount();
380 bool parseGVEntry(unsigned ID);
381 bool parseFunctionSummary(std::string Name, GlobalValue::GUID, unsigned ID);
382 bool parseVariableSummary(std::string Name, GlobalValue::GUID, unsigned ID);
383 bool parseAliasSummary(std::string Name, GlobalValue::GUID, unsigned ID);
384 bool parseGVFlags(GlobalValueSummary::GVFlags &GVFlags);
385 bool parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags);
386 bool parseOptionalFFlags(FunctionSummary::FFlags &FFlags);
387 bool parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls);
388 bool parseHotness(CalleeInfo::HotnessType &Hotness);
389 bool parseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo &TypeIdInfo);
390 bool parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests);
391 bool parseVFuncIdList(lltok::Kind Kind,
392 std::vector<FunctionSummary::VFuncId> &VFuncIdList);
393 bool parseConstVCallList(
394 lltok::Kind Kind,
395 std::vector<FunctionSummary::ConstVCall> &ConstVCallList);
396 using IdToIndexMapType =
397 std::map<unsigned, std::vector<std::pair<unsigned, LocTy>>>;
398 bool parseConstVCall(FunctionSummary::ConstVCall &ConstVCall,
399 IdToIndexMapType &IdToIndexMap, unsigned Index);
400 bool parseVFuncId(FunctionSummary::VFuncId &VFuncId,
401 IdToIndexMapType &IdToIndexMap, unsigned Index);
402 bool parseOptionalVTableFuncs(VTableFuncList &VTableFuncs);
403 bool parseOptionalParamAccesses(
404 std::vector<FunctionSummary::ParamAccess> &Params);
405 bool parseParamNo(uint64_t &ParamNo);
406 using IdLocListType = std::vector<std::pair<unsigned, LocTy>>;
407 bool parseParamAccess(FunctionSummary::ParamAccess &Param,
408 IdLocListType &IdLocList);
409 bool parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call,
410 IdLocListType &IdLocList);
411 bool parseParamAccessOffset(ConstantRange &Range);
412 bool parseOptionalRefs(std::vector<ValueInfo> &Refs);
413 bool parseTypeIdEntry(unsigned ID);
414 bool parseTypeIdSummary(TypeIdSummary &TIS);
415 bool parseTypeIdCompatibleVtableEntry(unsigned ID);
416 bool parseTypeTestResolution(TypeTestResolution &TTRes);
417 bool parseOptionalWpdResolutions(
418 std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap);
419 bool parseWpdRes(WholeProgramDevirtResolution &WPDRes);
420 bool parseOptionalResByArg(
421 std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg>
422 &ResByArg);
423 bool parseArgs(std::vector<uint64_t> &Args);
424 bool addGlobalValueToIndex(std::string Name, GlobalValue::GUID,
425 GlobalValue::LinkageTypes Linkage, unsigned ID,
426 std::unique_ptr<GlobalValueSummary> Summary,
427 LocTy Loc);
428 bool parseOptionalAllocs(std::vector<AllocInfo> &Allocs);
429 bool parseMemProfs(std::vector<MIBInfo> &MIBs);
430 bool parseAllocType(uint8_t &AllocType);
431 bool parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites);
432
433 // Type Parsing.
434 bool parseType(Type *&Result, const Twine &Msg, bool AllowVoid = false);
435 bool parseType(Type *&Result, bool AllowVoid = false) {
436 return parseType(Result, Msg: "expected type", AllowVoid);
437 }
438 bool parseType(Type *&Result, const Twine &Msg, LocTy &Loc,
439 bool AllowVoid = false) {
440 Loc = Lex.getLoc();
441 return parseType(Result, Msg, AllowVoid);
442 }
443 bool parseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
444 Loc = Lex.getLoc();
445 return parseType(Result, AllowVoid);
446 }
447 bool parseAnonStructType(Type *&Result, bool Packed);
448 bool parseStructBody(SmallVectorImpl<Type *> &Body);
449 bool parseStructDefinition(SMLoc TypeLoc, StringRef Name,
450 std::pair<Type *, LocTy> &Entry,
451 Type *&ResultTy);
452
453 bool parseArrayVectorType(Type *&Result, bool IsVector);
454 bool parseFunctionType(Type *&Result);
455 bool parseTargetExtType(Type *&Result);
456
457 // Function Semantic Analysis.
458 class PerFunctionState {
459 LLParser &P;
460 Function &F;
461 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
462 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
463 NumberedValues<Value *> NumberedVals;
464
465 /// FunctionNumber - If this is an unnamed function, this is the slot
466 /// number of it, otherwise it is -1.
467 int FunctionNumber;
468
469 public:
470 PerFunctionState(LLParser &p, Function &f, int functionNumber,
471 ArrayRef<unsigned> UnnamedArgNums);
472 ~PerFunctionState();
473
474 Function &getFunction() const { return F; }
475
476 bool finishFunction();
477
478 /// GetVal - Get a value with the specified name or ID, creating a
479 /// forward reference record if needed. This can return null if the value
480 /// exists but does not have the right type.
481 Value *getVal(const std::string &Name, Type *Ty, LocTy Loc);
482 Value *getVal(unsigned ID, Type *Ty, LocTy Loc);
483
484 /// setInstName - After an instruction is parsed and inserted into its
485 /// basic block, this installs its name.
486 bool setInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
487 Instruction *Inst);
488
489 /// GetBB - Get a basic block with the specified name or ID, creating a
490 /// forward reference record if needed. This can return null if the value
491 /// is not a BasicBlock.
492 BasicBlock *getBB(const std::string &Name, LocTy Loc);
493 BasicBlock *getBB(unsigned ID, LocTy Loc);
494
495 /// DefineBB - Define the specified basic block, which is either named or
496 /// unnamed. If there is an error, this returns null otherwise it returns
497 /// the block being defined.
498 BasicBlock *defineBB(const std::string &Name, int NameID, LocTy Loc);
499
500 bool resolveForwardRefBlockAddresses();
501 };
502
503 bool convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
504 PerFunctionState *PFS);
505
506 Value *checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty,
507 Value *Val);
508
509 bool parseConstantValue(Type *Ty, Constant *&C);
510 bool parseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
511 bool parseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
512 return parseValue(Ty, V, PFS: &PFS);
513 }
514
515 bool parseValue(Type *Ty, Value *&V, LocTy &Loc, PerFunctionState &PFS) {
516 Loc = Lex.getLoc();
517 return parseValue(Ty, V, PFS: &PFS);
518 }
519
520 bool parseTypeAndValue(Value *&V, PerFunctionState *PFS);
521 bool parseTypeAndValue(Value *&V, PerFunctionState &PFS) {
522 return parseTypeAndValue(V, PFS: &PFS);
523 }
524 bool parseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
525 Loc = Lex.getLoc();
526 return parseTypeAndValue(V, PFS);
527 }
528 bool parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
529 PerFunctionState &PFS);
530 bool parseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
531 LocTy Loc;
532 return parseTypeAndBasicBlock(BB, Loc, PFS);
533 }
534
535 struct ParamInfo {
536 LocTy Loc;
537 Value *V;
538 AttributeSet Attrs;
539 ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
540 : Loc(loc), V(v), Attrs(attrs) {}
541 };
542 bool parseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
543 PerFunctionState &PFS, bool IsMustTailCall = false,
544 bool InVarArgsFunc = false);
545
546 bool
547 parseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> &BundleList,
548 PerFunctionState &PFS);
549
550 bool parseExceptionArgs(SmallVectorImpl<Value *> &Args,
551 PerFunctionState &PFS);
552
553 bool resolveFunctionType(Type *RetType,
554 const SmallVector<ParamInfo, 16> &ArgList,
555 FunctionType *&FuncTy);
556
557 // Constant Parsing.
558 bool parseValID(ValID &ID, PerFunctionState *PFS,
559 Type *ExpectedTy = nullptr);
560 bool parseGlobalValue(Type *Ty, Constant *&C);
561 bool parseGlobalTypeAndValue(Constant *&V);
562 bool parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts,
563 std::optional<unsigned> *InRangeOp = nullptr);
564 bool parseOptionalComdat(StringRef GlobalName, Comdat *&C);
565 bool parseSanitizer(GlobalVariable *GV);
566 bool parseMetadataAsValue(Value *&V, PerFunctionState &PFS);
567 bool parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg,
568 PerFunctionState *PFS);
569 bool parseDIArgList(Metadata *&MD, PerFunctionState *PFS);
570 bool parseMetadata(Metadata *&MD, PerFunctionState *PFS);
571 bool parseMDTuple(MDNode *&MD, bool IsDistinct = false);
572 bool parseMDNode(MDNode *&N);
573 bool parseMDNodeTail(MDNode *&N);
574 bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts);
575 bool parseMetadataAttachment(unsigned &Kind, MDNode *&MD);
576 bool parseInstructionMetadata(Instruction &Inst);
577 bool parseGlobalObjectMetadataAttachment(GlobalObject &GO);
578 bool parseOptionalFunctionMetadata(Function &F);
579
580 template <class FieldTy>
581 bool parseMDField(LocTy Loc, StringRef Name, FieldTy &Result);
582 template <class FieldTy> bool parseMDField(StringRef Name, FieldTy &Result);
583 template <class ParserTy> bool parseMDFieldsImplBody(ParserTy ParseField);
584 template <class ParserTy>
585 bool parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc);
586 bool parseSpecializedMDNode(MDNode *&N, bool IsDistinct = false);
587
588#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \
589 bool parse##CLASS(MDNode *&Result, bool IsDistinct);
590#include "llvm/IR/Metadata.def"
591
592 // Function Parsing.
593 struct ArgInfo {
594 LocTy Loc;
595 Type *Ty;
596 AttributeSet Attrs;
597 std::string Name;
598 ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
599 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
600 };
601 bool parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList,
602 SmallVectorImpl<unsigned> &UnnamedArgNums,
603 bool &IsVarArg);
604 bool parseFunctionHeader(Function *&Fn, bool IsDefine,
605 unsigned &FunctionNumber,
606 SmallVectorImpl<unsigned> &UnnamedArgNums);
607 bool parseFunctionBody(Function &Fn, unsigned FunctionNumber,
608 ArrayRef<unsigned> UnnamedArgNums);
609 bool parseBasicBlock(PerFunctionState &PFS);
610
611 enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
612
613 // Instruction Parsing. Each instruction parsing routine can return with a
614 // normal result, an error result, or return having eaten an extra comma.
615 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
616 int parseInstruction(Instruction *&Inst, BasicBlock *BB,
617 PerFunctionState &PFS);
618 bool parseCmpPredicate(unsigned &P, unsigned Opc);
619
620 bool parseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
621 bool parseBr(Instruction *&Inst, PerFunctionState &PFS);
622 bool parseSwitch(Instruction *&Inst, PerFunctionState &PFS);
623 bool parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
624 bool parseInvoke(Instruction *&Inst, PerFunctionState &PFS);
625 bool parseResume(Instruction *&Inst, PerFunctionState &PFS);
626 bool parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS);
627 bool parseCatchRet(Instruction *&Inst, PerFunctionState &PFS);
628 bool parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS);
629 bool parseCatchPad(Instruction *&Inst, PerFunctionState &PFS);
630 bool parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
631 bool parseCallBr(Instruction *&Inst, PerFunctionState &PFS);
632
633 bool parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc,
634 bool IsFP);
635 bool parseArithmetic(Instruction *&Inst, PerFunctionState &PFS,
636 unsigned Opc, bool IsFP);
637 bool parseLogical(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
638 bool parseCompare(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
639 bool parseCast(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc);
640 bool parseSelect(Instruction *&Inst, PerFunctionState &PFS);
641 bool parseVAArg(Instruction *&Inst, PerFunctionState &PFS);
642 bool parseExtractElement(Instruction *&Inst, PerFunctionState &PFS);
643 bool parseInsertElement(Instruction *&Inst, PerFunctionState &PFS);
644 bool parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS);
645 int parsePHI(Instruction *&Inst, PerFunctionState &PFS);
646 bool parseLandingPad(Instruction *&Inst, PerFunctionState &PFS);
647 bool parseCall(Instruction *&Inst, PerFunctionState &PFS,
648 CallInst::TailCallKind TCK);
649 int parseAlloc(Instruction *&Inst, PerFunctionState &PFS);
650 int parseLoad(Instruction *&Inst, PerFunctionState &PFS);
651 int parseStore(Instruction *&Inst, PerFunctionState &PFS);
652 int parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS);
653 int parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS);
654 int parseFence(Instruction *&Inst, PerFunctionState &PFS);
655 int parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS);
656 int parseExtractValue(Instruction *&Inst, PerFunctionState &PFS);
657 int parseInsertValue(Instruction *&Inst, PerFunctionState &PFS);
658 bool parseFreeze(Instruction *&I, PerFunctionState &PFS);
659
660 // Use-list order directives.
661 bool parseUseListOrder(PerFunctionState *PFS = nullptr);
662 bool parseUseListOrderBB();
663 bool parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
664 bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
665 };
666} // End llvm namespace
667
668#endif
669

source code of llvm/include/llvm/AsmParser/LLParser.h