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"
20#include <memory>
21#include <optional>
22
23namespace llvm {
24
25class Constant;
26class DIExpression;
27class LLVMContext;
28class MemoryBufferRef;
29class Module;
31struct SlotMapping;
32class SMDiagnostic;
33class Type;
34
37
38/// This function is a main interface to the LLVM Assembly Parser. It parses
39/// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
40/// Module (intermediate representation) with the corresponding features. Note
41/// that this does not verify that the generated Module is valid, so you should
42/// run the verifier after parsing the file to check that it is okay.
43/// Parse LLVM Assembly from a file
44/// \param Filename The name of the file to parse
45/// \param Err Error result info.
46/// \param Context Context in which to allocate globals info.
47/// \param Slots The optional slot mapping that will be initialized during
48/// parsing.
49LLVM_ABI std::unique_ptr<Module>
51 SlotMapping *Slots = nullptr);
52
53/// The function is a secondary interface to the LLVM Assembly Parser. It parses
54/// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
55/// Module (intermediate representation) with the corresponding features. Note
56/// that this does not verify that the generated Module is valid, so you should
57/// run the verifier after parsing the file to check that it is okay.
58/// Parse LLVM Assembly from a string
59/// \param AsmString The string containing assembly
60/// \param Err Error result info.
61/// \param Context Context in which to allocate globals info.
62/// \param Slots The optional slot mapping that will be initialized during
63/// parsing.
64LLVM_ABI std::unique_ptr<Module>
66 LLVMContext &Context, SlotMapping *Slots = nullptr,
67 AsmParserContext *ParserContext = nullptr);
68
69/// Holds the Module and ModuleSummaryIndex returned by the interfaces
70/// that parse both.
72 std::unique_ptr<Module> Mod;
73 std::unique_ptr<ModuleSummaryIndex> Index;
74};
75
76/// This function is a main interface to the LLVM Assembly Parser. It parses
77/// an ASCII file that (presumably) contains LLVM Assembly code, including
78/// a module summary. It returns a Module (intermediate representation) and
79/// a ModuleSummaryIndex with the corresponding features. Note that this does
80/// not verify that the generated Module or Index are valid, so you should
81/// run the verifier after parsing the file to check that they are okay.
82/// Parse LLVM Assembly from a file
83/// \param Filename The name of the file to parse
84/// \param Err Error result info.
85/// \param Context Context in which to allocate globals info.
86/// \param Slots The optional slot mapping that will be initialized during
87/// parsing.
88/// \param DataLayoutCallback Override datalayout in the llvm assembly.
90 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
91 SlotMapping *Slots = nullptr,
92 DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
93 return std::nullopt;
94 });
95
96/// Only for use in llvm-as for testing; this does not produce a valid module.
98 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
99 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback);
100
101/// This function is a main interface to the LLVM Assembly Parser. It parses
102/// an ASCII file that (presumably) contains LLVM Assembly code for a module
103/// summary. It returns a ModuleSummaryIndex with the corresponding features.
104/// Note that this does not verify that the generated Index is valid, so you
105/// should run the verifier after parsing the file to check that it is okay.
106/// Parse LLVM Assembly Index from a file
107/// \param Filename The name of the file to parse
108/// \param Err Error result info.
109LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
110parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err);
111
112/// The function is a secondary interface to the LLVM Assembly Parser. It parses
113/// an ASCII string that (presumably) contains LLVM Assembly code for a module
114/// summary. It returns a a ModuleSummaryIndex with the corresponding features.
115/// Note that this does not verify that the generated Index is valid, so you
116/// should run the verifier after parsing the file to check that it is okay.
117/// Parse LLVM Assembly from a string
118/// \param AsmString The string containing assembly
119/// \param Err Error result info.
120LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
121parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err);
122
123/// parseAssemblyFile and parseAssemblyString are wrappers around this function.
124/// Parse LLVM Assembly from a MemoryBuffer.
125/// \param F The MemoryBuffer containing assembly
126/// \param Err Error result info.
127/// \param Slots The optional slot mapping that will be initialized during
128/// parsing.
129/// \param DataLayoutCallback Override datalayout in the llvm assembly.
130LLVM_ABI std::unique_ptr<Module> parseAssembly(
131 MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
132 SlotMapping *Slots = nullptr,
133 DataLayoutCallbackTy DataLayoutCallback =
134 [](StringRef, StringRef) { return std::nullopt; },
135 AsmParserContext *ParserContext = nullptr);
136
137/// Parse LLVM Assembly including the summary index from a MemoryBuffer.
138///
139/// \param F The MemoryBuffer containing assembly with summary
140/// \param Err Error result info.
141/// \param Slots The optional slot mapping that will be initialized during
142/// parsing.
143///
144/// parseAssemblyFileWithIndex is a wrapper around this function.
147 LLVMContext &Context, SlotMapping *Slots = nullptr);
148
149/// Parse LLVM Assembly for summary index from a MemoryBuffer.
150///
151/// \param F The MemoryBuffer containing assembly with summary
152/// \param Err Error result info.
153///
154/// parseSummaryIndexAssemblyFile is a wrapper around this function.
155LLVM_ABI std::unique_ptr<ModuleSummaryIndex>
157
158/// This function is the low-level interface to the LLVM Assembly Parser.
159/// This is kept as an independent function instead of being inlined into
160/// parseAssembly for the convenience of interactive users that want to add
161/// recently parsed bits to an existing module.
162///
163/// \param F The MemoryBuffer containing assembly
164/// \param M The module to add data to.
165/// \param Index The index to add data to.
166/// \param Err Error result info.
167/// \param Slots The optional slot mapping that will be initialized during
168/// parsing.
169/// \return true on error.
170/// \param DataLayoutCallback Override datalayout in the llvm assembly.
173 SlotMapping *Slots = nullptr,
174 DataLayoutCallbackTy DataLayoutCallback =
175 [](StringRef, StringRef) { return std::nullopt; },
176 AsmParserContext *ParserContext = nullptr);
177
178/// Parse a type and a constant value in the given string.
179///
180/// The constant value can be any LLVM constant, including a constant
181/// expression.
182///
183/// \param Slots The optional slot mapping that will restore the parsing state
184/// of the module.
185/// \return null on error.
187 const Module &M,
188 const SlotMapping *Slots = nullptr);
189
190/// Parse a type in the given string.
191///
192/// \param Slots The optional slot mapping that will restore the parsing state
193/// of the module.
194/// \return null on error.
196 const SlotMapping *Slots = nullptr);
197
198/// Parse a string \p Asm that starts with a type.
199/// \p Read[out] gives the number of characters that have been read to parse
200/// the type in \p Asm.
201///
202/// \param Slots The optional slot mapping that will restore the parsing state
203/// of the module.
204/// \return null on error.
206 SMDiagnostic &Err, const Module &M,
207 const SlotMapping *Slots = nullptr);
208
211 SMDiagnostic &Err, const Module &M,
212 const SlotMapping *Slots);
213
214} // End llvm namespace
215
216#endif
#define LLVM_ABI
Definition Compiler.h:213
#define F(x, y, z)
Definition MD5.cpp:55
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:297
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
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:142
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:190
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:223
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:51
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:177
LLVM_ABI DIExpression * parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots)
Definition Parser.cpp:236
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:40
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:133
llvm::function_ref< std::optional< std::string >(StringRef, StringRef)> DataLayoutCallbackTy
Definition Parser.h:36
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:96
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:64
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:195
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:207
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:125
LLVM_ABI std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition Parser.cpp:166
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition Parser.h:71
std::unique_ptr< Module > Mod
Definition Parser.h:72
std::unique_ptr< ModuleSummaryIndex > Index
Definition Parser.h:73
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition SlotMapping.h:33