LLVM 24.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
16#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/StringRef.h"
21#include <memory>
22#include <optional>
23
24namespace llvm {
25
26class Constant;
27class DIExpression;
28class LLVMContext;
29class MemoryBufferRef;
30class Module;
32struct SlotMapping;
33class SMDiagnostic;
34class Type;
35
38
39/// This function is a main interface to the LLVM Assembly Parser. It parses
40/// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
41/// Module (intermediate representation) with the corresponding features. Note
42/// that this does not verify that the generated Module is valid, so you should
43/// run the verifier after parsing the file to check that it is okay.
44/// Parse LLVM Assembly from a file
45/// \param Filename The name of the file to parse
46/// \param Err Error result info.
47/// \param Context Context in which to allocate globals info.
48/// \param Slots The optional slot mapping that will be initialized during
49/// parsing.
50LLVM_ABI std::unique_ptr<Module>
52 SlotMapping *Slots = nullptr);
53
54/// The function is a secondary interface to the LLVM Assembly Parser. It parses
55/// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
56/// Module (intermediate representation) with the corresponding features. Note
57/// that this does not verify that the generated Module is valid, so you should
58/// run the verifier after parsing the file to check that it is okay.
59/// Parse LLVM Assembly from a string
60/// \param AsmString The string containing assembly
61/// \param Err Error result info.
62/// \param Context Context in which to allocate globals info.
63/// \param Slots The optional slot mapping that will be initialized during
64/// parsing.
65LLVM_ABI std::unique_ptr<Module>
67 LLVMContext &Context, SlotMapping *Slots = nullptr,
68 AsmParserContext *ParserContext = nullptr);
69
70/// Holds the Module and ModuleSummaryIndex returned by the interfaces
71/// that parse both.
73 std::unique_ptr<Module> Mod;
74 std::unique_ptr<ModuleSummaryIndex> Index;
75};
76
77/// This function is a main interface to the LLVM Assembly Parser. It parses
78/// an ASCII file that (presumably) contains LLVM Assembly code, including
79/// a module summary. It returns a Module (intermediate representation) and
80/// a ModuleSummaryIndex with the corresponding features. Note that this does
81/// not verify that the generated Module or Index are valid, so you should
82/// run the verifier after parsing the file to check that they are okay.
83/// Parse LLVM Assembly from a file
84/// \param Filename The name of the file to parse
85/// \param Err Error result info.
86/// \param Context Context in which to allocate globals info.
87/// \param Slots The optional slot mapping that will be initialized during
88/// parsing.
89/// \param DataLayoutCallback Override datalayout in the llvm assembly.
92 SlotMapping *Slots = nullptr,
93 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
94 return std::nullopt;
95 });
96
97/// Only for use in llvm-as for testing; this does not produce a valid module.
99 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
100 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback);
101
102/// This function is a main interface to the LLVM Assembly Parser. It parses
103/// an ASCII file that (presumably) contains LLVM Assembly code for a module
104/// summary. It returns a ModuleSummaryIndex with the corresponding features.
105/// Note that this does not verify that the generated Index is valid, so you
106/// should run the verifier after parsing the file to check that it is okay.
107/// Parse LLVM Assembly Index from a file
108/// \param Filename The name of the file to parse
109/// \param Err Error result info.
110LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
111parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err);
112
113/// The function is a secondary interface to the LLVM Assembly Parser. It parses
114/// an ASCII string that (presumably) contains LLVM Assembly code for a module
115/// summary. It returns a a ModuleSummaryIndex with the corresponding features.
116/// Note that this does not verify that the generated Index is valid, so you
117/// should run the verifier after parsing the file to check that it is okay.
118/// Parse LLVM Assembly from a string
119/// \param AsmString The string containing assembly
120/// \param Err Error result info.
121LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
122parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err);
123
124/// parseAssemblyFile and parseAssemblyString are wrappers around this function.
125/// Parse LLVM Assembly from a MemoryBuffer.
126/// \param F The MemoryBuffer containing assembly
127/// \param Err Error result info.
128/// \param Slots The optional slot mapping that will be initialized during
129/// parsing.
130/// \param DataLayoutCallback Override datalayout in the llvm assembly.
131LLVM_ABI std::unique_ptr<Module> parseAssembly(
132 MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
133 SlotMapping *Slots = nullptr,
134 DataLayoutCallbackTy DataLayoutCallback =
135 [](StringRef, StringRef) { return std::nullopt; },
136 AsmParserContext *ParserContext = nullptr);
137
138/// Parse LLVM Assembly including the summary index from a MemoryBuffer.
139///
140/// \param F The MemoryBuffer containing assembly with summary
141/// \param Err Error result info.
142/// \param Slots The optional slot mapping that will be initialized during
143/// parsing.
144///
145/// parseAssemblyFileWithIndex is a wrapper around this function.
148 LLVMContext &Context, SlotMapping *Slots = nullptr);
149
150/// Parse LLVM Assembly for summary index from a MemoryBuffer.
151///
152/// \param F The MemoryBuffer containing assembly with summary
153/// \param Err Error result info.
154///
155/// parseSummaryIndexAssemblyFile is a wrapper around this function.
156LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
158
159/// This function is the low-level interface to the LLVM Assembly Parser.
160/// This is kept as an independent function instead of being inlined into
161/// parseAssembly for the convenience of interactive users that want to add
162/// recently parsed bits to an existing module.
163///
164/// \param F The MemoryBuffer containing assembly
165/// \param M The module to add data to.
166/// \param Index The index to add data to.
167/// \param Err Error result info.
168/// \param Slots The optional slot mapping that will be initialized during
169/// parsing.
170/// \return true on error.
171/// \param DataLayoutCallback Override datalayout in the llvm assembly.
174 SlotMapping *Slots = nullptr,
175 DataLayoutCallbackTy DataLayoutCallback =
176 [](StringRef, StringRef) { return std::nullopt; },
177 AsmParserContext *ParserContext = nullptr);
178
179/// Parse a type and a constant value in the given string.
180///
181/// The constant value can be any LLVM constant, including a constant
182/// expression.
183///
184/// \param Slots The optional slot mapping that will restore the parsing state
185/// of the module.
186/// \return null on error.
188 const Module &M,
189 const SlotMapping *Slots = nullptr);
190
191/// Parse a type in the given string.
192///
193/// \param Slots The optional slot mapping that will restore the parsing state
194/// of the module.
195/// \return null on error.
197 const SlotMapping *Slots = nullptr);
198
199/// Parse a string \p Asm that starts with a type.
200/// \p Read[out] gives the number of characters that have been read to parse
201/// the type in \p Asm.
202///
203/// \param Slots The optional slot mapping that will restore the parsing state
204/// of the module.
205/// \return null on error.
207 SMDiagnostic &Err, const Module &M,
208 const SlotMapping *Slots = nullptr);
209
212 SMDiagnostic &Err, const Module &M,
213 const SlotMapping *Slots);
214
215/// Parse standalone metadata definitions using and updating the supplied slot
216/// mapping. Each string must contain exactly one complete definition.
217/// \param ErrorDefinitionIndex The index of the definition containing an error.
218/// \return true on error.
220 SMDiagnostic &Err, const Module &M,
221 SlotMapping &Slots,
222 unsigned &ErrorDefinitionIndex);
223
224} // End llvm namespace
225
226#endif
#define LLVM_ABI
Definition Compiler.h:215
#define F(x, y, z)
Definition MD5.cpp:54
static constexpr StringLiteral Filename
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Registry of file location information for LLVM IR constructs.
This is an important base class in LLVM.
Definition Constant.h:43
DWARF expression.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Class to hold module path string table and global value map, and encapsulate methods for operating on...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:308
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::unique_ptr< Module > parseAssemblyString(StringRef AsmString, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, AsmParserContext *ParserContext=nullptr)
The function is a secondary interface to the LLVM Assembly Parser.
Definition Parser.cpp:144
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:192
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:225
LLVM_ABI std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;}, AsmParserContext *ParserContext=nullptr)
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition Parser.cpp:53
LLVM_ABI bool parseMetadataDefinitions(ArrayRef< StringRef > Definitions, SMDiagnostic &Err, const Module &M, SlotMapping &Slots, unsigned &ErrorDefinitionIndex)
Parse standalone metadata definitions using and updating the supplied slot mapping.
Definition Parser.cpp:253
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:179
LLVM_ABI DIExpression * parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots)
Definition Parser.cpp:238
LLVM_ABI bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;}, AsmParserContext *ParserContext=nullptr)
This function is the low-level interface to the LLVM Assembly Parser.
Definition Parser.cpp:42
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:135
llvm::function_ref< std::optional< std::string >(StringRef, StringRef)> DataLayoutCallbackTy
Definition Parser.h:37
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:98
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:66
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:197
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:209
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:127
LLVM_ABI std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition Parser.cpp:168
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition Parser.h:72
std::unique_ptr< Module > Mod
Definition Parser.h:73
std::unique_ptr< ModuleSummaryIndex > Index
Definition Parser.h:74
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition SlotMapping.h:32