LLVM 19.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
15#include "llvm/IR/Module.h"
19#include <system_error>
20
21using namespace llvm;
22
25 SlotMapping *Slots, bool UpgradeDebugInfo,
26 DataLayoutCallbackTy DataLayoutCallback) {
27 SourceMgr SM;
28 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
29 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
30
31 std::optional<LLVMContext> OptContext;
32 return LLParser(F.getBuffer(), SM, Err, M, Index,
33 M ? M->getContext() : OptContext.emplace(), Slots)
34 .Run(UpgradeDebugInfo, DataLayoutCallback);
35}
36
39 SlotMapping *Slots,
40 DataLayoutCallbackTy DataLayoutCallback) {
41 return ::parseAssemblyInto(F, M, Index, Err, Slots,
42 /*UpgradeDebugInfo*/ true, DataLayoutCallback);
43}
44
45std::unique_ptr<Module>
47 SlotMapping *Slots,
48 DataLayoutCallbackTy DataLayoutCallback) {
49 std::unique_ptr<Module> M =
50 std::make_unique<Module>(F.getBufferIdentifier(), Context);
51
52 if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, DataLayoutCallback))
53 return nullptr;
54
55 return M;
56}
57
58std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
59 SMDiagnostic &Err,
60 LLVMContext &Context,
61 SlotMapping *Slots) {
64 if (std::error_code EC = FileOrErr.getError()) {
65 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
66 "Could not open input file: " + EC.message());
67 return nullptr;
68 }
69
70 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
71}
72
75 LLVMContext &Context, SlotMapping *Slots,
77 DataLayoutCallbackTy DataLayoutCallback) {
78 std::unique_ptr<Module> M =
79 std::make_unique<Module>(F.getBufferIdentifier(), Context);
80 std::unique_ptr<ModuleSummaryIndex> Index =
81 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
82
83 if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
84 DataLayoutCallback))
85 return {nullptr, nullptr};
86
87 return {std::move(M), std::move(Index)};
88}
89
91 SMDiagnostic &Err,
92 LLVMContext &Context,
93 SlotMapping *Slots) {
94 return ::parseAssemblyWithIndex(
95 F, Err, Context, Slots,
96 /*UpgradeDebugInfo*/ true,
97 [](StringRef, StringRef) { return std::nullopt; });
98}
99
102 LLVMContext &Context, SlotMapping *Slots,
103 bool UpgradeDebugInfo,
104 DataLayoutCallbackTy DataLayoutCallback) {
106 MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
107 if (std::error_code EC = FileOrErr.getError()) {
108 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
109 "Could not open input file: " + EC.message());
110 return {nullptr, nullptr};
111 }
112
113 return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
115 DataLayoutCallback);
116}
117
120 LLVMContext &Context, SlotMapping *Slots,
121 DataLayoutCallbackTy DataLayoutCallback) {
122 return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
123 /*UpgradeDebugInfo*/ true,
124 DataLayoutCallback);
125}
126
128 StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
129 SlotMapping *Slots, DataLayoutCallbackTy DataLayoutCallback) {
130 return ::parseAssemblyFileWithIndex(Filename, Err, Context, Slots,
131 /*UpgradeDebugInfo*/ false,
132 DataLayoutCallback);
133}
134
135std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
136 SMDiagnostic &Err,
137 LLVMContext &Context,
138 SlotMapping *Slots) {
139 MemoryBufferRef F(AsmString, "<string>");
140 return parseAssembly(F, Err, Context, Slots);
141}
142
145 SMDiagnostic &Err) {
146 SourceMgr SM;
147 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
148 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
149
150 // The parser holds a reference to a context that is unused when parsing the
151 // index, but we need to initialize it.
152 LLVMContext unusedContext;
153 return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext)
154 .Run(true, [](StringRef, StringRef) { return std::nullopt; });
155}
156
157std::unique_ptr<ModuleSummaryIndex>
159 std::unique_ptr<ModuleSummaryIndex> Index =
160 std::make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
161
163 return nullptr;
164
165 return Index;
166}
167
168std::unique_ptr<ModuleSummaryIndex>
172 if (std::error_code EC = FileOrErr.getError()) {
173 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
174 "Could not open input file: " + EC.message());
175 return nullptr;
176 }
177
178 return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
179}
180
181std::unique_ptr<ModuleSummaryIndex>
183 MemoryBufferRef F(AsmString, "<string>");
184 return parseSummaryIndexAssembly(F, Err);
185}
186
188 const Module &M, const SlotMapping *Slots) {
189 SourceMgr SM;
190 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
191 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
192 Constant *C;
193 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
195 return nullptr;
196 return C;
197}
198
200 const SlotMapping *Slots) {
201 unsigned Read;
202 Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
203 if (!Ty)
204 return nullptr;
205 if (Read != Asm.size()) {
206 SourceMgr SM;
207 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
208 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
209 Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
210 SourceMgr::DK_Error, "expected end of string");
211 return nullptr;
212 }
213 return Ty;
214}
216 SMDiagnostic &Err, const Module &M,
217 const SlotMapping *Slots) {
218 SourceMgr SM;
219 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
220 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
221 Type *Ty;
222 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
223 .parseTypeAtBeginning(Ty, Read, Slots))
224 return nullptr;
225 return Ty;
226}
static ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:74
static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, ModuleSummaryIndex &Index, SMDiagnostic &Err)
Definition: Parser.cpp:143
static bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:23
static ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots, bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback)
Definition: Parser.cpp:101
#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.
LLVMContext & Context
This is an important base class in LLVM.
Definition: Constant.h:41
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 parseTypeAtBeginning(Type *&Ty, unsigned &Read, const SlotMapping *Slots)
Definition: LLParser.cpp:127
bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots)
Definition: LLParser.cpp:114
bool Run(bool UpgradeDebugInfo, DataLayoutCallbackTy DataLayoutCallback=[](StringRef, StringRef) { return std::nullopt;})
Run: module ::= toplevelentity*.
Definition: LLParser.cpp:95
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
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:182
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:135
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:215
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:169
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:127
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:46
ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
Parse LLVM Assembly including the summary index from a MemoryBuffer.
Definition: Parser.cpp:90
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:37
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:58
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:187
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:199
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:119
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssembly(MemoryBufferRef F, SMDiagnostic &Err)
Parse LLVM Assembly for summary index from a MemoryBuffer.
Definition: Parser.cpp:158
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition: Parser.h:68
This struct contains the mappings from the slot numbers to unnamed metadata nodes,...
Definition: SlotMapping.h:33