blob: e1c7f746a3355ff8a271b23cc2a4be4ed79ffb33 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- Parser.h - Parser for LLVM IR text assembly files -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020016#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringRef.h"
18#include <memory>
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010019
20namespace llvm {
21
22class Constant;
23class LLVMContext;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020024class MemoryBufferRef;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010025class Module;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010026class ModuleSummaryIndex;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027struct SlotMapping;
28class SMDiagnostic;
29class Type;
30
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020031typedef llvm::function_ref<Optional<std::string>(StringRef)>
32 DataLayoutCallbackTy;
33
Andrew Scullcdfcccc2018-10-05 20:58:37 +010034/// This function is a main interface to the LLVM Assembly Parser. It parses
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010035/// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
36/// Module (intermediate representation) with the corresponding features. Note
37/// that this does not verify that the generated Module is valid, so you should
38/// run the verifier after parsing the file to check that it is okay.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010039/// Parse LLVM Assembly from a file
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010040/// \param Filename The name of the file to parse
Andrew Scullcdfcccc2018-10-05 20:58:37 +010041/// \param Err Error result info.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010042/// \param Context Context in which to allocate globals info.
43/// \param Slots The optional slot mapping that will be initialized during
44/// parsing.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020045std::unique_ptr<Module> parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
46 LLVMContext &Context,
47 SlotMapping *Slots = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048
49/// The function is a secondary interface to the LLVM Assembly Parser. It parses
50/// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
51/// Module (intermediate representation) with the corresponding features. Note
52/// that this does not verify that the generated Module is valid, so you should
53/// run the verifier after parsing the file to check that it is okay.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010054/// Parse LLVM Assembly from a string
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010055/// \param AsmString The string containing assembly
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056/// \param Err Error result info.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057/// \param Context Context in which to allocate globals info.
58/// \param Slots The optional slot mapping that will be initialized during
59/// parsing.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
Andrew Scullcdfcccc2018-10-05 20:58:37 +010061 SMDiagnostic &Err,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010062 LLVMContext &Context,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020063 SlotMapping *Slots = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010064
Andrew Scullcdfcccc2018-10-05 20:58:37 +010065/// Holds the Module and ModuleSummaryIndex returned by the interfaces
66/// that parse both.
67struct ParsedModuleAndIndex {
68 std::unique_ptr<Module> Mod;
69 std::unique_ptr<ModuleSummaryIndex> Index;
70};
71
72/// This function is a main interface to the LLVM Assembly Parser. It parses
73/// an ASCII file that (presumably) contains LLVM Assembly code, including
74/// a module summary. It returns a Module (intermediate representation) and
75/// a ModuleSummaryIndex with the corresponding features. Note that this does
76/// not verify that the generated Module or Index are valid, so you should
77/// run the verifier after parsing the file to check that they are okay.
78/// Parse LLVM Assembly from a file
79/// \param Filename The name of the file to parse
80/// \param Err Error result info.
81/// \param Context Context in which to allocate globals info.
82/// \param Slots The optional slot mapping that will be initialized during
83/// parsing.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020084/// \param DataLayoutCallback Override datalayout in the llvm assembly.
85ParsedModuleAndIndex parseAssemblyFileWithIndex(
86 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
87 SlotMapping *Slots = nullptr,
88 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
89
90/// Only for use in llvm-as for testing; this does not produce a valid module.
91ParsedModuleAndIndex parseAssemblyFileWithIndexNoUpgradeDebugInfo(
92 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
93 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback);
Andrew Scullcdfcccc2018-10-05 20:58:37 +010094
95/// This function is a main interface to the LLVM Assembly Parser. It parses
96/// an ASCII file that (presumably) contains LLVM Assembly code for a module
97/// summary. It returns a a ModuleSummaryIndex with the corresponding features.
98/// Note that this does not verify that the generated Index is valid, so you
99/// should run the verifier after parsing the file to check that it is okay.
100/// Parse LLVM Assembly Index from a file
101/// \param Filename The name of the file to parse
102/// \param Err Error result info.
103std::unique_ptr<ModuleSummaryIndex>
104parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err);
105
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100106/// parseAssemblyFile and parseAssemblyString are wrappers around this function.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100107/// Parse LLVM Assembly from a MemoryBuffer.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108/// \param F The MemoryBuffer containing assembly
109/// \param Err Error result info.
110/// \param Slots The optional slot mapping that will be initialized during
111/// parsing.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200112/// \param DataLayoutCallback Override datalayout in the llvm assembly.
113std::unique_ptr<Module> parseAssembly(
114 MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
115 SlotMapping *Slots = nullptr,
116 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100117
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100118/// Parse LLVM Assembly including the summary index from a MemoryBuffer.
119///
120/// \param F The MemoryBuffer containing assembly with summary
121/// \param Err Error result info.
122/// \param Slots The optional slot mapping that will be initialized during
123/// parsing.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100124///
125/// parseAssemblyFileWithIndex is a wrapper around this function.
126ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F,
127 SMDiagnostic &Err,
128 LLVMContext &Context,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200129 SlotMapping *Slots = nullptr);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100130
131/// Parse LLVM Assembly for summary index from a MemoryBuffer.
132///
133/// \param F The MemoryBuffer containing assembly with summary
134/// \param Err Error result info.
135///
136/// parseSummaryIndexAssemblyFile is a wrapper around this function.
137std::unique_ptr<ModuleSummaryIndex>
138parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err);
139
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140/// This function is the low-level interface to the LLVM Assembly Parser.
141/// This is kept as an independent function instead of being inlined into
142/// parseAssembly for the convenience of interactive users that want to add
143/// recently parsed bits to an existing module.
144///
145/// \param F The MemoryBuffer containing assembly
146/// \param M The module to add data to.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100147/// \param Index The index to add data to.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100148/// \param Err Error result info.
149/// \param Slots The optional slot mapping that will be initialized during
150/// parsing.
151/// \return true on error.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200152/// \param DataLayoutCallback Override datalayout in the llvm assembly.
153bool parseAssemblyInto(
154 MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err,
155 SlotMapping *Slots = nullptr,
156 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100157
158/// Parse a type and a constant value in the given string.
159///
160/// The constant value can be any LLVM constant, including a constant
161/// expression.
162///
163/// \param Slots The optional slot mapping that will restore the parsing state
164/// of the module.
165/// \return null on error.
166Constant *parseConstantValue(StringRef Asm, SMDiagnostic &Err, const Module &M,
167 const SlotMapping *Slots = nullptr);
168
169/// Parse a type in the given string.
170///
171/// \param Slots The optional slot mapping that will restore the parsing state
172/// of the module.
173/// \return null on error.
174Type *parseType(StringRef Asm, SMDiagnostic &Err, const Module &M,
175 const SlotMapping *Slots = nullptr);
176
177/// Parse a string \p Asm that starts with a type.
178/// \p Read[out] gives the number of characters that have been read to parse
179/// the type in \p Asm.
180///
181/// \param Slots The optional slot mapping that will restore the parsing state
182/// of the module.
183/// \return null on error.
184Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err,
185 const Module &M, const SlotMapping *Slots = nullptr);
186
187} // End llvm namespace
188
189#endif