1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This library implements the functionality defined in llvm/AsmParser/Parser.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include <system_error>
25 bool llvm::parseAssemblyInto(MemoryBufferRef F
, Module
&M
, SMDiagnostic
&Err
,
28 std::unique_ptr
<MemoryBuffer
> Buf
= MemoryBuffer::getMemBuffer(F
);
29 SM
.AddNewSourceBuffer(std::move(Buf
), SMLoc());
31 return LLParser(F
.getBuffer(), SM
, Err
, &M
, Slots
).Run();
34 std::unique_ptr
<Module
> llvm::parseAssembly(MemoryBufferRef F
,
38 std::unique_ptr
<Module
> M
=
39 make_unique
<Module
>(F
.getBufferIdentifier(), Context
);
41 if (parseAssemblyInto(F
, *M
, Err
, Slots
))
47 std::unique_ptr
<Module
> llvm::parseAssemblyFile(StringRef Filename
,
51 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> FileOrErr
=
52 MemoryBuffer::getFileOrSTDIN(Filename
);
53 if (std::error_code EC
= FileOrErr
.getError()) {
54 Err
= SMDiagnostic(Filename
, SourceMgr::DK_Error
,
55 "Could not open input file: " + EC
.message());
59 return parseAssembly(FileOrErr
.get()->getMemBufferRef(), Err
, Context
, Slots
);
62 std::unique_ptr
<Module
> llvm::parseAssemblyString(StringRef AsmString
,
66 MemoryBufferRef
F(AsmString
, "<string>");
67 return parseAssembly(F
, Err
, Context
, Slots
);
70 Constant
*llvm::parseConstantValue(StringRef Asm
, SMDiagnostic
&Err
,
71 const Module
&M
, const SlotMapping
*Slots
) {
73 std::unique_ptr
<MemoryBuffer
> Buf
= MemoryBuffer::getMemBuffer(Asm
);
74 SM
.AddNewSourceBuffer(std::move(Buf
), SMLoc());
76 if (LLParser(Asm
, SM
, Err
, const_cast<Module
*>(&M
))
77 .parseStandaloneConstantValue(C
, Slots
))
82 Type
*llvm::parseType(StringRef Asm
, SMDiagnostic
&Err
, const Module
&M
,
83 const SlotMapping
*Slots
) {
85 Type
*Ty
= parseTypeAtBeginning(Asm
, Read
, Err
, M
, Slots
);
88 if (Read
!= Asm
.size()) {
90 std::unique_ptr
<MemoryBuffer
> Buf
= MemoryBuffer::getMemBuffer(Asm
);
91 SM
.AddNewSourceBuffer(std::move(Buf
), SMLoc());
92 Err
= SM
.GetMessage(SMLoc::getFromPointer(Asm
.begin() + Read
),
93 SourceMgr::DK_Error
, "expected end of string");
98 Type
*llvm::parseTypeAtBeginning(StringRef Asm
, unsigned &Read
,
99 SMDiagnostic
&Err
, const Module
&M
,
100 const SlotMapping
*Slots
) {
102 std::unique_ptr
<MemoryBuffer
> Buf
= MemoryBuffer::getMemBuffer(Asm
);
103 SM
.AddNewSourceBuffer(std::move(Buf
), SMLoc());
105 if (LLParser(Asm
, SM
, Err
, const_cast<Module
*>(&M
))
106 .parseTypeAtBeginning(Ty
, Read
, Slots
))