LLVM 22.0.0git
Parser.cpp
Go to the documentation of this file.
1//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
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// This library implements the functionality defined in llvm/AsmParser/Parser.h
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/IR/Module.h"
20#include <system_error>
21
22using namespace llvm;
23
26 SlotMapping *Slots, bool UpgradeDebugInfo,
27 DataLayoutCallbackTy DataLayoutCallback,
28 AsmParserContext *ParserContext = nullptr) {
29 SourceMgr SM;
30 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
31 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
32
33 std::optional<LLVMContext> OptContext;
34 return LLParser(F.getBuffer(), SM, Err, M, Index,
35 M ? M->getContext() : OptContext.emplace(), Slots,
36 ParserContext)
37 .Run(UpgradeDebugInfo, DataLayoutCallback);
38}
39
42 SlotMapping *Slots,
43 DataLayoutCallbackTy DataLayoutCallback,
44 AsmParserContext *ParserContext) {
45 return ::parseAssemblyInto(F, M, Index, Err, Slots,
46 /*UpgradeDebugInfo*/ true, DataLayoutCallback,
47 ParserContext);
48}
49
50std::unique_ptr<Module>
52 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback,
53 AsmParserContext *ParserContext) {
54 std::unique_ptr<Module> M =
55 std::make_unique<Module>(F.getBufferIdentifier(), Context);
56
57 if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, DataLayoutCallback,
58 ParserContext))
59 return nullptr;
60
61 return M;
62}
63
64std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
65 SMDiagnostic &Err,
66 LLVMContext &Context,
67 SlotMapping *Slots) {
70 if (std::error_code EC = FileOrErr.getError()) {
71 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
72 "Could not open input file: " + EC.message());
73 return nullptr;
74 }
75
76 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
77}
78
81 LLVMContext &Context, SlotMapping *Slots,
83 DataLayoutCallbackTy DataLayoutCallback) {
84 std::unique_ptr<Module> M =
85 std::make_unique<Module>(F.getBufferIdentifier(), Context);
86 std::unique_ptr<ModuleSummaryIndex> Index =
87 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
88
89 if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
90 DataLayoutCallback))
91 return {nullptr, nullptr};
92
93 return {std::move(M), std::move(Index)};
94}
95
97 SMDiagnostic &Err,
98 LLVMContext &Context,
99 SlotMapping *Slots) {
100 return ::parseAssemblyWithIndex(
101 F, Err, Context, Slots,
102 /*UpgradeDebugInfo*/ true,
103 [](StringRef, StringRef) { return std::nullopt; });
104}
105
108 LLVMContext &Context, SlotMapping *Slots,
109 bool UpgradeDebugInfo,
110 DataLayoutCallbackTy DataLayoutCallback) {
112 MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
113 if (std::error_code EC = FileOrErr.getError()) {
114 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
115 "Could not open input file: " + EC.message());
116 return {nullptr, nullptr};
117 }
118
119 return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
120 Context, Slots, UpgradeDebugInfo,
121 DataLayoutCallback);
122}
123
126 LLVMContext &Context, SlotMapping *Slots,
127 DataLayoutCallbackTy DataLayoutCallback) {
128 return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
129 /*UpgradeDebugInfo*/ true,
130 DataLayoutCallback);
131}
132
134 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
135 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback) {
136 return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
137 /*UpgradeDebugInfo*/ false,
138 DataLayoutCallback);
139}
140
141std::unique_ptr<Module>
143 LLVMContext &Context, SlotMapping *Slots,
144 AsmParserContext *ParserContext) {
145 MemoryBufferRef F(AsmString, "<string>");
146 return parseAssembly(
147 F, Err, Context, Slots, [](StringRef, StringRef) { return std::nullopt; },
148 ParserContext);
149}
150
152 ModuleSummaryIndex &Index,
153 SMDiagnostic &Err) {
154 SourceMgr SM;
155 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
156 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
157
158 // The parser holds a reference to a context that is unused when parsing the
159 // index, but we need to initialize it.
160 LLVMContext unusedContext;
161 return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext)
162 .Run(true, [](StringRef, StringRef) { return std::nullopt; });
163}
164
165std::unique_ptr<ModuleSummaryIndex>
167 std::unique_ptr<ModuleSummaryIndex> Index =
168 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
169
170 if (parseSummaryIndexAssemblyInto(F, *Index, Err))
171 return nullptr;
172
173 return Index;
174}
175
176std::unique_ptr<ModuleSummaryIndex>
180 if (std::error_code EC = FileOrErr.getError()) {
181 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
182 "Could not open input file: " + EC.message());
183 return nullptr;
184 }
185
186 return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
187}
188
189std::unique_ptr<ModuleSummaryIndex>
191 MemoryBufferRef F(AsmString, "<string>");
192 return parseSummaryIndexAssembly(F, Err);
193}
194
196 const Module &M, const SlotMapping *Slots) {
197 SourceMgr SM;
198 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
199 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
200 Constant *C;
201 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
203 return nullptr;
204 return C;
205}
206
208 const SlotMapping *Slots) {
209 unsigned Read;
210 Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
211 if (!Ty)
212 return nullptr;
213 if (Read != Asm.size()) {
214 SourceMgr SM;
215 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
216 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
217 Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
218 SourceMgr::DK_Error, "expected end of string");
219 return nullptr;
220 }
221 return Ty;
222}
224 SMDiagnostic &Err, const Module &M,
225 const SlotMapping *Slots) {
226 SourceMgr SM;
227 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
228 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
229 Type *Ty;
230 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
231 .parseTypeAtBeginning(Ty, Read, Slots))
232 return nullptr;
233 return Ty;
234}
235
237 unsigned &Read,
238 SMDiagnostic &Err,
239 const Module &M,
240 const SlotMapping *Slots) {
241 SourceMgr SM;
242 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
243 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
244 MDNode *MD;
245 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
247 return nullptr;
248 return dyn_cast<DIExpression>(MD);
249}
static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, ModuleSummaryIndex &Index, SMDiagnostic &Err)
Definition Parser.cpp:151
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:55
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
Registry of file location information for LLVM IR constructs.
This is an important base class in LLVM.
Definition Constant.h:43
DWARF expression.
Represents either an error or a value T.
Definition ErrorOr.h:56
reference get()
Definition ErrorOr.h:149
std::error_code getError() const
Definition ErrorOr.h:152
bool parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read, const SlotMapping *Slots)
Definition LLParser.cpp:123
bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition LLParser.cpp:107
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition LLParser.cpp:94
bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
Run: module ::= toplevelentity*.
Definition LLParser.cpp:75
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Metadata node.
Definition Metadata.h:1078
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
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
LLVMContext & getContext() const
Get the global data context.
Definition Module.h:285
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition SourceMgr.h:297
Represents a location in source code.
Definition SMLoc.h:23
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:36
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:37
LLVM_ABI SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}) const
Return an SMDiagnostic at the specified location with the specified string.
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition SourceMgr.h:160
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
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
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
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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 bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
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
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition SlotMapping.h:33