LLVM  12.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/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include <memory>
19 
20 namespace llvm {
21 
22 class Constant;
23 class LLVMContext;
24 class MemoryBufferRef;
25 class Module;
26 class ModuleSummaryIndex;
27 struct SlotMapping;
28 class SMDiagnostic;
29 class Type;
30 
33 
34 /// This function is a main interface to the LLVM Assembly Parser. It parses
35 /// 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.
39 /// Parse LLVM Assembly from a file
40 /// \param Filename The name of the file to parse
41 /// \param Err Error result info.
42 /// \param Context Context in which to allocate globals info.
43 /// \param Slots The optional slot mapping that will be initialized during
44 /// parsing.
45 std::unique_ptr<Module> parseAssemblyFile(StringRef Filename, SMDiagnostic &Err,
47  SlotMapping *Slots = nullptr);
48 
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.
54 /// Parse LLVM Assembly from a string
55 /// \param AsmString The string containing assembly
56 /// \param Err Error result info.
57 /// \param Context Context in which to allocate globals info.
58 /// \param Slots The optional slot mapping that will be initialized during
59 /// parsing.
60 std::unique_ptr<Module> parseAssemblyString(StringRef AsmString,
61  SMDiagnostic &Err,
63  SlotMapping *Slots = nullptr);
64 
65 /// Holds the Module and ModuleSummaryIndex returned by the interfaces
66 /// that parse both.
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.
84 /// \param DataLayoutCallback Override datalayout in the llvm assembly.
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.
92  StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
93  SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback);
94 
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.
103 std::unique_ptr<ModuleSummaryIndex>
105 
106 /// parseAssemblyFile and parseAssemblyString are wrappers around this function.
107 /// Parse LLVM Assembly from a MemoryBuffer.
108 /// \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.
112 /// \param DataLayoutCallback Override datalayout in the llvm assembly.
113 std::unique_ptr<Module> parseAssembly(
114  MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context,
115  SlotMapping *Slots = nullptr,
116  DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
117 
118 /// 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.
124 ///
125 /// parseAssemblyFileWithIndex is a wrapper around this function.
127  SMDiagnostic &Err,
128  LLVMContext &Context,
129  SlotMapping *Slots = nullptr);
130 
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.
137 std::unique_ptr<ModuleSummaryIndex>
139 
140 /// 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.
147 /// \param Index The index to add data to.
148 /// \param Err Error result info.
149 /// \param Slots The optional slot mapping that will be initialized during
150 /// parsing.
151 /// \return true on error.
152 /// \param DataLayoutCallback Override datalayout in the llvm assembly.
153 bool parseAssemblyInto(
155  SlotMapping *Slots = nullptr,
156  DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
157 
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.
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.
174 Type *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.
184 Type *parseTypeAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err,
185  const Module &M, const SlotMapping *Slots = nullptr);
186 
187 } // End llvm namespace
188 
189 #endif
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
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:182
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
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
Machine Debugify Module
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:176
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:60
F(f)
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:170
std::unique_ptr< Module > Mod
Definition: Parser.h:68
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:210
Class to hold module path string table and global value map, and encapsulate methods for operating on...
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition: Parser.cpp:159
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
This is an important base class in LLVM.
Definition: Constant.h:41
std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:48
bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
This function is the low-level interface to the LLVM Assembly Parser.
Definition: Parser.cpp:39
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
std::unique_ptr< ModuleSummaryIndex > Index
Definition: Parser.h:69
This struct contains the mappings from the slot numbers to unnamed metadata nodes, global values and types.
Definition: SlotMapping.h:32
ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
Parse LLVM Assembly including the summary index from a MemoryBuffer.
Definition: Parser.cpp:92
ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:120
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition: Parser.h:67
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
Type * parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type in the given string.
Definition: Parser.cpp:194
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:246