LLVM 20.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 SourceMgr SM;
29 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
30 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
31
32 std::optional<LLVMContext> OptContext;
33 return LLParser(F.getBuffer(), SM, Err, M, Index,
34 M ? M->getContext() : OptContext.emplace(), Slots)
35 .Run(UpgradeDebugInfo, DataLayoutCallback);
36}
37
40 SlotMapping *Slots,
41 DataLayoutCallbackTy DataLayoutCallback) {
42 return ::parseAssemblyInto(F, M, Index, Err, Slots,
43 /*UpgradeDebugInfo*/ true, DataLayoutCallback);
44}
45
46std::unique_ptr<Module>
48 SlotMapping *Slots,
49 DataLayoutCallbackTy DataLayoutCallback) {
50 std::unique_ptr<Module> M =
51 std::make_unique<Module>(F.getBufferIdentifier(), Context);
52
53 if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, DataLayoutCallback))
54 return nullptr;
55
56 return M;
57}
58
59std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
60 SMDiagnostic &Err,
61 LLVMContext &Context,
62 SlotMapping *Slots) {
65 if (std::error_code EC = FileOrErr.getError()) {
66 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
67 "Could not open input file: " + EC.message());
68 return nullptr;
69 }
70
71 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
72}
73
76 LLVMContext &Context, SlotMapping *Slots,
78 DataLayoutCallbackTy DataLayoutCallback) {
79 std::unique_ptr<Module> M =
80 std::make_unique<Module>(F.getBufferIdentifier(), Context);
81 std::unique_ptr<ModuleSummaryIndex> Index =
82 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
83
84 if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
85 DataLayoutCallback))
86 return {nullptr, nullptr};
87
88 return {std::move(M), std::move(Index)};
89}
90
92 SMDiagnostic &Err,
93 LLVMContext &Context,
94 SlotMapping *Slots) {
95 return ::parseAssemblyWithIndex(
96 F, Err, Context, Slots,
97 /*UpgradeDebugInfo*/ true,
98 [](StringRef, StringRef) { return std::nullopt; });
99}
100
103 LLVMContext &Context, SlotMapping *Slots,
104 bool UpgradeDebugInfo,
105 DataLayoutCallbackTy DataLayoutCallback) {
107 MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
108 if (std::error_code EC = FileOrErr.getError()) {
109 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
110 "Could not open input file: " + EC.message());
111 return {nullptr, nullptr};
112 }
113
114 return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
115 Context, Slots, UpgradeDebugInfo,
116 DataLayoutCallback);
117}
118
121 LLVMContext &Context, SlotMapping *Slots,
122 DataLayoutCallbackTy DataLayoutCallback) {
123 return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
124 /*UpgradeDebugInfo*/ true,
125 DataLayoutCallback);
126}
127
129 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
130 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback) {
131 return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
132 /*UpgradeDebugInfo*/ false,
133 DataLayoutCallback);
134}
135
136std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
137 SMDiagnostic &Err,
138 LLVMContext &Context,
139 SlotMapping *Slots) {
140 MemoryBufferRef F(AsmString, "<string>");
141 return parseAssembly(F, Err, Context, Slots);
142}
143
146 SMDiagnostic &Err) {
147 SourceMgr SM;
148 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
149 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
150
151 // The parser holds a reference to a context that is unused when parsing the
152 // index, but we need to initialize it.
153 LLVMContext unusedContext;
154 return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext)
155 .Run(true, [](StringRef, StringRef) { return std::nullopt; });
156}
157
158std::unique_ptr<ModuleSummaryIndex>
160 std::unique_ptr<ModuleSummaryIndex> Index =
161 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
162
164 return nullptr;
165
166 return Index;
167}
168
169std::unique_ptr<ModuleSummaryIndex>
173 if (std::error_code EC = FileOrErr.getError()) {
174 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
175 "Could not open input file: " + EC.message());
176 return nullptr;
177 }
178
179 return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
180}
181
182std::unique_ptr<ModuleSummaryIndex>
184 MemoryBufferRef F(AsmString, "<string>");
185 return parseSummaryIndexAssembly(F, Err);
186}
187
189 const Module &M, const SlotMapping *Slots) {
190 SourceMgr SM;
191 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
192 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
193 Constant *C;
194 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
196 return nullptr;
197 return C;
198}
199
201 const SlotMapping *Slots) {
202 unsigned Read;
203 Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
204 if (!Ty)
205 return nullptr;
206 if (Read != Asm.size()) {
207 SourceMgr SM;
208 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
209 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
210 Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
211 SourceMgr::DK_Error, "expected end of string");
212 return nullptr;
213 }
214 return Ty;
215}
217 SMDiagnostic &Err, const Module &M,
218 const SlotMapping *Slots) {
219 SourceMgr SM;
220 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
221 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
222 Type *Ty;
223 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
224 .parseTypeAtBeginning(Ty, Read, Slots))
225 return nullptr;
226 return Ty;
227}
228
230 unsigned &Read,
231 SMDiagnostic &Err,
232 const Module &M,
233 const SlotMapping *Slots) {
234 SourceMgr SM;
235 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
236 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
237 MDNode *MD;
238 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
240 return nullptr;
241 return dyn_cast<DIExpression>(MD);
242}
static ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:75
static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, ModuleSummaryIndex &Index, SMDiagnostic &Err)
Definition: Parser.cpp:144
static bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:24
static ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:102
#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...
Module.h This file contains the declarations for the Module class.
This is an important base class in LLVM.
Definition: Constant.h:42
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:127
bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition: LLParser.cpp:111
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition: LLParser.cpp:98
bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
Run: module ::= toplevelentity*.
Definition: LLParser.cpp:79
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:1069
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:65
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:281
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:31
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.
Definition: SourceMgr.cpp:274
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:144
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyString(StringRef AsmString, SMDiagnostic &Err)
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:183
@ Read
Definition: CodeGenData.h:102
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
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:216
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:170
DIExpression * parseDIExpressionBodyAtBeginning(StringRef Asm, unsigned &Read, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots)
Definition: Parser.cpp:229
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
std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:47
ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
Parse LLVM Assembly including the summary index from a MemoryBuffer.
Definition: Parser.cpp:91
bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots=nullptr, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
This function is the low-level interface to the LLVM Assembly Parser.
Definition: Parser.cpp:38
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:59
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:188
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
Type * parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type in the given string.
Definition: Parser.cpp:200
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:120
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition: Parser.cpp:159
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition: Parser.h:69
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition: SlotMapping.h:33