LLVM  7.0.0svn
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 //===----------------------------------------------------------------------===//
14 #include "llvm/AsmParser/Parser.h"
15 #include "LLParser.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/IR/Module.h"
19 #include "llvm/Support/SourceMgr.h"
21 #include <cstring>
22 #include <system_error>
23 using namespace llvm;
26  SlotMapping *Slots, bool UpgradeDebugInfo,
27  StringRef DataLayoutString) {
28  SourceMgr SM;
29  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
30  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
32  return LLParser(F.getBuffer(), SM, Err, &M, Slots, UpgradeDebugInfo,
33  DataLayoutString)
34  .Run();
35 }
37 std::unique_ptr<Module>
39  SlotMapping *Slots, bool UpgradeDebugInfo,
40  StringRef DataLayoutString) {
41  std::unique_ptr<Module> M =
42  make_unique<Module>(F.getBufferIdentifier(), Context);
44  if (parseAssemblyInto(F, *M, Err, Slots, UpgradeDebugInfo, DataLayoutString))
45  return nullptr;
47  return M;
48 }
50 std::unique_ptr<Module>
53  bool UpgradeDebugInfo, StringRef DataLayoutString) {
56  if (std::error_code EC = FileOrErr.getError()) {
57  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
58  "Could not open input file: " + EC.message());
59  return nullptr;
60  }
62  return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots,
63  UpgradeDebugInfo, DataLayoutString);
64 }
66 std::unique_ptr<Module>
69  bool UpgradeDebugInfo, StringRef DataLayoutString) {
70  MemoryBufferRef F(AsmString, "<string>");
71  return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo,
72  DataLayoutString);
73 }
76  const Module &M, const SlotMapping *Slots) {
77  SourceMgr SM;
78  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
79  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
80  Constant *C;
81  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M))
82  .parseStandaloneConstantValue(C, Slots))
83  return nullptr;
84  return C;
85 }
88  const SlotMapping *Slots) {
89  unsigned Read;
90  Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots);
91  if (!Ty)
92  return nullptr;
93  if (Read != Asm.size()) {
94  SourceMgr SM;
95  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
96  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
97  Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read),
98  SourceMgr::DK_Error, "expected end of string");
99  return nullptr;
100  }
101  return Ty;
102 }
104  SMDiagnostic &Err, const Module &M,
105  const SlotMapping *Slots) {
106  SourceMgr SM;
107  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm);
108  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
109  Type *Ty;
110  if (LLParser(Asm, SM, Err, const_cast<Module *>(&M))
111  .parseTypeAtBeginning(Ty, Read, Slots))
112  return nullptr;
113  return Ty;
114 }
uint64_t CallInst * C
Represents either an error or a value T.
Definition: ErrorOr.h:69
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
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:75
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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.
std::unique_ptr< Module > parseAssemblyString(StringRef AsmString, SMDiagnostic &Error, LLVMContext &Context, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:67
bool parseAssemblyInto(MemoryBufferRef F, Module &M, 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
StringRef getBuffer() const
Definition: MemoryBuffer.h:277
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:103
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:38
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
This is an important base class in LLVM.
Definition: Constant.h:42
std::error_code getError() const
Definition: ErrorOr.h:172
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
Module.h This file contains the declarations for the Module class.
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:279
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 "-".
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::unique_ptr< Module > parseAssemblyFile(StringRef Filename, SMDiagnostic &Error, LLVMContext &Context, SlotMapping *Slots=nullptr, bool UpgradeDebugInfo=true, StringRef DataLayoutString="")
This function is the main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:51
Type * parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, const SlotMapping *Slots=nullptr)
Parse a type in the given string.
Definition: Parser.cpp:87
Represents a location in source code.
Definition: SMLoc.h:24
reference get()
Definition: ErrorOr.h:169
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:260