LLVM  8.0.1
Parser.cpp
Go to the documentation of this file.
1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This library implements the functionality defined in llvm/AsmParser/Parser.h
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/AsmParser/Parser.h"
15 #include "LLParser.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/IR/Module.h"
20 #include "llvm/Support/SourceMgr.h"
22 #include <cstring>
23 #include <system_error>
24 using namespace llvm;
25 
28  SlotMapping *Slots, bool UpgradeDebugInfo,
29  StringRef DataLayoutString) {
30  SourceMgr SM;
31  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
32  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
33 
35  return LLParser(F.getBuffer(), SM, Err, M, Index,
36  M ? M->getContext() : Context, Slots, UpgradeDebugInfo,
37  DataLayoutString)
38  .Run();
39 }
40 
41 std::unique_ptr<Module>
43  SlotMapping *Slots, bool UpgradeDebugInfo,
44  StringRef DataLayoutString) {
45  std::unique_ptr<Module> M =
46  make_unique<Module>(F.getBufferIdentifier(), Context);
47 
48  if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo,
49  DataLayoutString))
50  return nullptr;
51 
52  return M;
53 }
54 
55 std::unique_ptr<Module>
58  bool UpgradeDebugInfo, StringRef DataLayoutString) {
61  if (std::error_code EC = FileOrErr.getError()) {
62  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
63  "Could not open input file: " + EC.message());
64  return nullptr;
65  }
66 
67  return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
68  UpgradeDebugInfo, DataLayoutString);
69 }
70 
73  SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
74  std::unique_ptr<Module> M =
75  make_unique<Module>(F.getBufferIdentifier(), Context);
76  std::unique_ptr<ModuleSummaryIndex> Index =
77  make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
78 
79  if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
80  DataLayoutString))
81  return {nullptr, nullptr};
82 
83  return {std::move(M), std::move(Index)};
84 }
85 
87  StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
88  SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
91  if (std::error_code EC = FileOrErr.getError()) {
92  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
93  "Could not open input file: " + EC.message());
94  return {nullptr, nullptr};
95  }
96 
97  return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
98  Context, Slots, UpgradeDebugInfo,
99  DataLayoutString);
100 }
101 
102 std::unique_ptr<Module>
105  bool UpgradeDebugInfo, StringRef DataLayoutString) {
106  MemoryBufferRef F(AsmString, "<string>");
107  return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
108  DataLayoutString);
109 }
110 
113  SMDiagnostic &Err) {
114  SourceMgr SM;
115  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
116  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
117 
118  // The parser holds a reference to a context that is unused when parsing the
119  // index, but we need to initialize it.
120  LLVMContext unusedContext;
121  return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run();
122 }
123 
124 std::unique_ptr<ModuleSummaryIndex>
126  std::unique_ptr<ModuleSummaryIndex> Index =
127  make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
128 
129  if (parseSummaryIndexAssemblyInto(F, *Index, Err))
130  return nullptr;
131 
132  return Index;
133 }
134 
135 std::unique_ptr<ModuleSummaryIndex>
139  if (std::error_code EC = FileOrErr.getError()) {
140  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
141  "Could not open input file: " + EC.message());
142  return nullptr;
143  }
144 
145  return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
146 }
147 
149  const Module &M, const SlotMapping *Slots) {
150  SourceMgr SM;
151  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
152  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
153  Constant *C;
154  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
155  .parseStandaloneConstantValue(C, Slots))
156  return nullptr;
157  return C;
158 }
159 
161  const SlotMapping *Slots) {
162  unsigned Read;
163  Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
164  if (!Ty)
165  return nullptr;
166  if (Read != Asm.size()) {
167  SourceMgr SM;
168  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
169  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
170  Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
171  SourceMgr::DK_Error, "expected end of string");
172  return nullptr;
173  }
174  return Ty;
175 }
177  SMDiagnostic &Err, const Module &M,
178  const SlotMapping *Slots) {
179  SourceMgr SM;
180  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
181  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
182  Type *Ty;
183  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
184  .parseTypeAtBeginning(Ty, Read, Slots))
185  return nullptr;
186  return Ty;
187 }
uint64_t CallInst * C
Represents either an error or a value T.
Definition: ErrorOr.h:57
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:148
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
F(f)
std::unique_ptr< Module > parseAssemblyString(StringRef AsmString, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:103
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
StringRef getBuffer() const
Definition: MemoryBuffer.h:273
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:136
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:152
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:176
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:125
std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:42
bool parseAssemblyInto(MemoryBufferRef F, Module *M, ModuleSummaryIndex *Index, SMDiagnostic &Err, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
This function is the low-level interface to the LLVM Assembly Parser.
Definition: Parser.cpp:26
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:69
std::unique_ptr< Module > parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:56
This is an important base class in LLVM.
Definition: Constant.h:42
std::error_code getError() const
Definition: ErrorOr.h:160
bool UpgradeDebugInfo(Module &M)
Check the debug info version number, if it is out-dated, drop the debug info.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:42
SMDiagnostic GetMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges=None, ArrayRef< SMFixIt > FixIts=None) const
Return an SMDiagnostic at the specified location with the specified string.
Definition: SourceMgr.cpp:169
ParsedModuleAndIndex parseAssemblyWithIndex(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
Parse LLVM Assembly including the summary index from a MemoryBuffer.
Definition: Parser.cpp:71
ParsedModuleAndIndex parseAssemblyFileWithIndex(StringRef Filename, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:86
Module.h This file contains the declarations for the Module class.
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:275
This struct contains the mappings from the slot numbers to unnamed metadata nodes, global values and types.
Definition: SlotMapping.h:33
iterator begin() const
Definition: StringRef.h:106
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:37
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
static bool parseSummaryIndexAssemblyInto(MemoryBufferRef F, ModuleSummaryIndex &Index, SMDiagnostic &Err)
Definition: Parser.cpp:111
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition: Parser.h:73
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Type * parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type in the given string.
Definition: Parser.cpp:160
Represents a location in source code.
Definition: SMLoc.h:24
reference get()
Definition: ErrorOr.h:157
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:260