LLVM  9.0.0svn
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 
13 #include "llvm/AsmParser/Parser.h"
14 #include "LLParser.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Module.h"
19 #include "llvm/Support/SourceMgr.h"
21 #include <cstring>
22 #include <system_error>
23 using namespace llvm;
24 
27  SlotMapping *Slots, bool UpgradeDebugInfo,
28  StringRef DataLayoutString) {
29  SourceMgr SM;
30  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
31  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
32 
34  return LLParser(F.getBuffer(), SM, Err, M, Index,
35  M ? M->getContext() : Context, Slots, UpgradeDebugInfo,
36  DataLayoutString)
37  .Run();
38 }
39 
40 std::unique_ptr<Module>
42  SlotMapping *Slots, bool UpgradeDebugInfo,
43  StringRef DataLayoutString) {
44  std::unique_ptr<Module> M =
45  make_unique<Module>(F.getBufferIdentifier(), Context);
46 
47  if (parseAssemblyInto(F, M.get(), nullptr, Err, Slots, UpgradeDebugInfo,
48  DataLayoutString))
49  return nullptr;
50 
51  return M;
52 }
53 
54 std::unique_ptr<Module>
57  bool UpgradeDebugInfo, StringRef DataLayoutString) {
60  if (std::error_code EC = FileOrErr.getError()) {
61  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
62  "Could not open input file: " + EC.message());
63  return nullptr;
64  }
65 
66  return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
67  UpgradeDebugInfo, DataLayoutString);
68 }
69 
72  SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
73  std::unique_ptr<Module> M =
74  make_unique<Module>(F.getBufferIdentifier(), Context);
75  std::unique_ptr<ModuleSummaryIndex> Index =
76  make_unique<ModuleSummaryIndex>(/*HaveGVs=*/true);
77 
78  if (parseAssemblyInto(F, M.get(), Index.get(), Err, Slots, UpgradeDebugInfo,
79  DataLayoutString))
80  return {nullptr, nullptr};
81 
82  return {std::move(M), std::move(Index)};
83 }
84 
86  StringRef Filename, SMDiagnostic &Err, LLVMContext &Context,
87  SlotMapping *Slots, bool UpgradeDebugInfo, StringRef DataLayoutString) {
90  if (std::error_code EC = FileOrErr.getError()) {
91  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
92  "Could not open input file: " + EC.message());
93  return {nullptr, nullptr};
94  }
95 
96  return parseAssemblyWithIndex(FileOrErr.get()->getMemBufferRef(), Err,
97  Context, Slots, UpgradeDebugInfo,
98  DataLayoutString);
99 }
100 
101 std::unique_ptr<Module>
104  bool UpgradeDebugInfo, StringRef DataLayoutString) {
105  MemoryBufferRef F(AsmString, "<string>");
106  return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
107  DataLayoutString);
108 }
109 
112  SMDiagnostic &Err) {
113  SourceMgr SM;
114  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
115  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
116 
117  // The parser holds a reference to a context that is unused when parsing the
118  // index, but we need to initialize it.
119  LLVMContext unusedContext;
120  return LLParser(F.getBuffer(), SM, Err, nullptr, &Index, unusedContext).Run();
121 }
122 
123 std::unique_ptr<ModuleSummaryIndex>
125  std::unique_ptr<ModuleSummaryIndex> Index =
126  make_unique<ModuleSummaryIndex>(/*HaveGVs=*/false);
127 
128  if (parseSummaryIndexAssemblyInto(F, *Index, Err))
129  return nullptr;
130 
131  return Index;
132 }
133 
134 std::unique_ptr<ModuleSummaryIndex>
138  if (std::error_code EC = FileOrErr.getError()) {
139  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
140  "Could not open input file: " + EC.message());
141  return nullptr;
142  }
143 
144  return parseSummaryIndexAssembly(FileOrErr.get()->getMemBufferRef(), Err);
145 }
146 
148  const Module &M, const SlotMapping *Slots) {
149  SourceMgr SM;
150  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
151  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
152  Constant *C;
153  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
154  .parseStandaloneConstantValue(C, Slots))
155  return nullptr;
156  return C;
157 }
158 
160  const SlotMapping *Slots) {
161  unsigned Read;
162  Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
163  if (!Ty)
164  return nullptr;
165  if (Read != Asm.size()) {
166  SourceMgr SM;
167  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
168  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
169  Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
170  SourceMgr::DK_Error, "expected end of string");
171  return nullptr;
172  }
173  return Ty;
174 }
176  SMDiagnostic &Err, const Module &M,
177  const SlotMapping *Slots) {
178  SourceMgr SM;
179  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
180  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
181  Type *Ty;
182  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M), nullptr, M.getContext())
183  .parseTypeAtBeginning(Ty, Read, Slots))
184  return nullptr;
185  return Ty;
186 }
uint64_t CallInst * C
Represents either an error or a value T.
Definition: ErrorOr.h:56
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:147
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
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:102
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
std::unique_ptr< ModuleSummaryIndex > parseSummaryIndexAssemblyFile(StringRef Filename, SMDiagnostic &Err)
This function is a main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:135
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:151
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
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:175
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:124
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:41
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:25
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
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:55
This is an important base class in LLVM.
Definition: Constant.h:41
std::error_code getError() const
Definition: ErrorOr.h:159
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:41
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:168
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:70
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:85
Module.h This file contains the declarations for the Module class.
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:274
This struct contains the mappings from the slot numbers to unnamed metadata nodes, global values and types.
Definition: SlotMapping.h:32
iterator begin() const
Definition: StringRef.h:101
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
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:110
Holds the Module and ModuleSummaryIndex returned by the interfaces that parse both.
Definition: Parser.h:72
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:48
Type * parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type in the given string.
Definition: Parser.cpp:159
Represents a location in source code.
Definition: SMLoc.h:23
reference get()
Definition: ErrorOr.h:156
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:259