LLVM 22.0.0git
Parser.h
Go to the documentation of this file.
1//===-- Parser.h - Parser for LLVM IR text assembly files -------*- 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// These classes are implemented by the lib/AsmParser library.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ASMPARSER_PARSER_H
14#define LLVM_ASMPARSER_PARSER_H
15
17#include "llvm/ADT/StringRef.h"
19#include <memory>
20#include <optional>
21
22namespace llvm {
23
24class Constant;
25class DIExpression;
26class LLVMContext;
27class MemoryBufferRef;
28class Module;
29class ModuleSummaryIndex;
30struct SlotMapping;
31class SMDiagnostic;
32class Type;
33
34typedef llvm::function_ref<std::optional<std::string>(StringRef, StringRef)>
36
37/// This function is a main interface to the LLVM Assembly Parser. It parses
38/// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
39/// Module (intermediate representation) with the corresponding features. Note
40/// that this does not verify that the generated Module is valid, so you should
41/// run the verifier after parsing the file to check that it is okay.
42/// Parse LLVM Assembly from a file
43/// \param Filename The name of the file to parse
44/// \param Err Error result info.
45/// \param Context Context in which to allocate globals info.
46/// \param Slots The optional slot mapping that will be initialized during
47/// parsing.
48LLVM_ABI std::unique_ptr<Module>
50 SlotMapping *Slots = nullptr);
51
52/// The function is a secondary interface to the LLVM Assembly Parser. It parses
53/// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
54/// Module (intermediate representation) with the corresponding features. Note
55/// that this does not verify that the generated Module is valid, so you should
56/// run the verifier after parsing the file to check that it is okay.
57/// Parse LLVM Assembly from a string
58/// \param AsmString The string containing assembly
59/// \param Err Error result info.
60/// \param Context Context in which to allocate globals info.
61/// \param Slots The optional slot mapping that will be initialized during
62/// parsing.
63LLVM_ABI std::unique_ptr<Module>
65 LLVMContext &Context, SlotMapping *Slots = nullptr);
66
67/// Holds the Module and ModuleSummaryIndex returned by the interfaces
68/// that parse both.
70 std::unique_ptr<Module> Mod;
71 std::unique_ptr<ModuleSummaryIndex> Index;
72};
73
74/// This function is a main interface to the LLVM Assembly Parser. It parses
75/// an ASCII file that (presumably) contains LLVM Assembly code, including
76/// a module summary. It returns a Module (intermediate representation) and
77/// a ModuleSummaryIndex with the corresponding features. Note that this does
78/// not verify that the generated Module or Index are valid, so you should
79/// run the verifier after parsing the file to check that they are okay.
80/// Parse LLVM Assembly from a file
81/// \param Filename The name of the file to parse
82/// \param Err Error result info.
83/// \param Context Context in which to allocate globals info.
84/// \param Slots The optional slot mapping that will be initialized during
85/// parsing.
86/// \param DataLayoutCallback Override datalayout in the llvm assembly.
88 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
89 SlotMapping *Slots = nullptr,
90 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
91 return std::nullopt;
92 });
93
94/// Only for use in llvm-as for testing; this does not produce a valid module.
96 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
97 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback);
98
99/// This function is a main interface to the LLVM Assembly Parser. It parses
100/// an ASCII file that (presumably) contains LLVM Assembly code for a module
101/// summary. It returns a ModuleSummaryIndex with the corresponding features.
102/// Note that this does not verify that the generated Index is valid, so you
103/// should run the verifier after parsing the file to check that it is okay.
104/// Parse LLVM Assembly Index from a file
105/// \param Filename The name of the file to parse
106/// \param Err Error result info.
107LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
108parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err);
109
110/// The function is a secondary interface to the LLVM Assembly Parser. It parses
111/// an ASCII string that (presumably) contains LLVM Assembly code for a module
112/// summary. It returns a a ModuleSummaryIndex with the corresponding features.
113/// Note that this does not verify that the generated Index is valid, so you
114/// should run the verifier after parsing the file to check that it is okay.
115/// Parse LLVM Assembly from a string
116/// \param AsmString The string containing assembly
117/// \param Err Error result info.
118LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
119parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err);
120
121/// parseAssemblyFile and parseAssemblyString are wrappers around this function.
122/// Parse LLVM Assembly from a MemoryBuffer.
123/// \param F The MemoryBuffer containing assembly
124/// \param Err Error result info.
125/// \param Slots The optional slot mapping that will be initialized during
126/// parsing.
127/// \param DataLayoutCallback Override datalayout in the llvm assembly.
128LLVM_ABI std::unique_ptr<Module> parseAssembly(
129 MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
130 SlotMapping *Slots = nullptr,
131 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
132 return std::nullopt;
133 });
134
135/// Parse LLVM Assembly including the summary index from a MemoryBuffer.
136///
137/// \param F The MemoryBuffer containing assembly with summary
138/// \param Err Error result info.
139/// \param Slots The optional slot mapping that will be initialized during
140/// parsing.
141///
142/// parseAssemblyFileWithIndex is a wrapper around this function.
143LLVM_ABI ParsedModuleAndIndex
144parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err,
145 LLVMContext &Context, SlotMapping *Slots = nullptr);
146
147/// Parse LLVM Assembly for summary index from a MemoryBuffer.
148///
149/// \param F The MemoryBuffer containing assembly with summary
150/// \param Err Error result info.
151///
152/// parseSummaryIndexAssemblyFile is a wrapper around this function.
153LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
154parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err);
155
156/// This function is the low-level interface to the LLVM Assembly Parser.
157/// This is kept as an independent function instead of being inlined into
158/// parseAssembly for the convenience of interactive users that want to add
159/// recently parsed bits to an existing module.
160///
161/// \param F The MemoryBuffer containing assembly
162/// \param M The module to add data to.
163/// \param Index The index to add data to.
164/// \param Err Error result info.
165/// \param Slots The optional slot mapping that will be initialized during
166/// parsing.
167/// \return true on error.
168/// \param DataLayoutCallback Override datalayout in the llvm assembly.
170 MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err,
171 SlotMapping *Slots = nullptr,
172 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
173 return std::nullopt;
174 });
175
176/// Parse a type and a constant value in the given string.
177///
178/// The constant value can be any LLVM constant, including a constant
179/// expression.
180///
181/// \param Slots The optional slot mapping that will restore the parsing state
182/// of the module.
183/// \return null on error.
184LLVM_ABI Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err,
185 const Module &M,
186 const SlotMapping *Slots = nullptr);
187
188/// Parse a type in the given string.
189///
190/// \param Slots The optional slot mapping that will restore the parsing state
191/// of the module.
192/// \return null on error.
193LLVM_ABI Type *parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
194 const SlotMapping *Slots = nullptr);
195
196/// Parse a string \p Asm that starts with a type.
197/// \p Read[out] gives the number of characters that have been read to parse
198/// the type in \p Asm.
199///
200/// \param Slots The optional slot mapping that will restore the parsing state
201/// of the module.
202/// \return null on error.
203LLVM_ABI Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read,
204 SMDiagnostic &Err, const Module &M,
205 const SlotMapping *Slots = nullptr);
206
207LLVM_ABI DIExpression *
208parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read,
209 SMDiagnostic &Err, const Module &M,
210 const SlotMapping *Slots);
211
212} // End llvm namespace
213
214#endif
RelocType Type
Definition: COFFYAML.cpp:410
#define LLVM_ABI
Definition: Compiler.h:213
uint32_t Index
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:282
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
An efficient, type-erasing, non-owning reference to a callable.
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:54
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err)
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:183
@ Read
Definition: CodeGenData.h:108
LLVM_ABI std::unique_ptr< Module > parseAssemblyString(StringRef AsmString, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:136
LLVM_ABI Type * parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a string Asm that starts with a type.
Definition: Parser.cpp:216
LLVM_ABI std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:170
LLVM_ABI DIExpression * parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots)
Definition: Parser.cpp:229
LLVM_ABI ParsedModuleAndIndex parseAssemblyFileWithIndexNoUpgradeDebugInfo(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback)
Only for use in llvm-as for testing; this does not produce a valid module.
Definition: Parser.cpp:128
llvm::function_ref< std::optional< std::string >(StringRef, StringRef)> DataLayoutCallbackTy
Definition: Parser.h:35
LLVM_ABI std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:47
LLVM_ABI ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
Parse LLVM Assembly including the summary index from a MemoryBuffer.
Definition: Parser.cpp:91
LLVM_ABI bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
This function is the low-level interface to the LLVM Assembly Parser.
Definition: Parser.cpp:38
LLVM_ABI std::unique_ptr< Module > parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:59
LLVM_ABI Constant * parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type and a constant value in the given string.
Definition: Parser.cpp:188
LLVM_ABI Type * parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type in the given string.
Definition: Parser.cpp:200
LLVM_ABI ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:120
LLVM_ABI std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition: Parser.cpp:159
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition: Parser.h:69
std::unique_ptr< Module > Mod
Definition: Parser.h:70
std::unique_ptr< ModuleSummaryIndex > Index
Definition: Parser.h:71
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition: SlotMapping.h:33