xref: /freebsd/contrib/llvm-project/llvm/include/llvm/AsmParser/Parser.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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/STLFunctionalExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include <memory>
19 #include <optional>
20 
21 namespace llvm {
22 
23 class Constant;
24 class DIExpression;
25 class LLVMContext;
26 class MemoryBufferRef;
27 class Module;
28 class ModuleSummaryIndex;
29 struct SlotMapping;
30 class SMDiagnostic;
31 class Type;
32 
33 typedef llvm::function_ref<std::optional<std::string>(StringRef, StringRef)>
34     DataLayoutCallbackTy;
35 
36 /// This function is a main interface to the LLVM Assembly Parser. It parses
37 /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
38 /// Module (intermediate representation) with the corresponding features. Note
39 /// that this does not verify that the generated Module is valid, so you should
40 /// run the verifier after parsing the file to check that it is okay.
41 /// Parse LLVM Assembly from a file
42 /// \param Filename The name of the file to parse
43 /// \param Err Error result info.
44 /// \param Context Context in which to allocate globals info.
45 /// \param Slots The optional slot mapping that will be initialized during
46 ///              parsing.
47 std::unique_ptr<Module> parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
48                                           LLVMContext &Context,
49                                           SlotMapping *Slots = nullptr);
50 
51 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
52 /// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
53 /// Module (intermediate representation) with the corresponding features. Note
54 /// that this does not verify that the generated Module is valid, so you should
55 /// run the verifier after parsing the file to check that it is okay.
56 /// Parse LLVM Assembly from a string
57 /// \param AsmString The string containing assembly
58 /// \param Err Error result info.
59 /// \param Context Context in which to allocate globals info.
60 /// \param Slots The optional slot mapping that will be initialized during
61 ///              parsing.
62 std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
63                                             SMDiagnostic &Err,
64                                             LLVMContext &Context,
65                                             SlotMapping *Slots = nullptr);
66 
67 /// Holds the Module and ModuleSummaryIndex returned by the interfaces
68 /// that parse both.
69 struct ParsedModuleAndIndex {
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.
87 ParsedModuleAndIndex parseAssemblyFileWithIndex(
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.
95 ParsedModuleAndIndex parseAssemblyFileWithIndexNoUpgradeDebugInfo(
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.
107 std::unique_ptr<ModuleSummaryIndex>
108 parseSummaryIndexAssemblyFile(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.
118 std::unique_ptr<ModuleSummaryIndex>
119 parseSummaryIndexAssemblyString(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.
128 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.
143 ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F,
144                                             SMDiagnostic &Err,
145                                             LLVMContext &Context,
146                                             SlotMapping *Slots = nullptr);
147 
148 /// Parse LLVM Assembly for summary index from a MemoryBuffer.
149 ///
150 /// \param F The MemoryBuffer containing assembly with summary
151 /// \param Err Error result info.
152 ///
153 /// parseSummaryIndexAssemblyFile is a wrapper around this function.
154 std::unique_ptr<ModuleSummaryIndex>
155 parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err);
156 
157 /// This function is the low-level interface to the LLVM Assembly Parser.
158 /// This is kept as an independent function instead of being inlined into
159 /// parseAssembly for the convenience of interactive users that want to add
160 /// recently parsed bits to an existing module.
161 ///
162 /// \param F The MemoryBuffer containing assembly
163 /// \param M The module to add data to.
164 /// \param Index The index to add data to.
165 /// \param Err Error result info.
166 /// \param Slots The optional slot mapping that will be initialized during
167 ///              parsing.
168 /// \return true on error.
169 /// \param DataLayoutCallback Override datalayout in the llvm assembly.
170 bool parseAssemblyInto(
171     MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err,
172     SlotMapping *Slots = nullptr,
173     DataLayoutCallbackTy DataLayoutCallback = [](StringRef, StringRef) {
174       return std::nullopt;
175     });
176 
177 /// Parse a type and a constant value in the given string.
178 ///
179 /// The constant value can be any LLVM constant, including a constant
180 /// expression.
181 ///
182 /// \param Slots The optional slot mapping that will restore the parsing state
183 /// of the module.
184 /// \return null on error.
185 Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, 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.
193 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.
203 Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err,
204                            const Module &M, const SlotMapping *Slots = nullptr);
205 
206 DIExpression *parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read,
207                                                SMDiagnostic &Err,
208                                                const Module &M,
209                                                const SlotMapping *Slots);
210 
211 } // End llvm namespace
212 
213 #endif
214