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"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <cstring>
22 #include <system_error>
23 using namespace llvm;
24
parseAssemblyInto(MemoryBufferRef F,Module & M,SMDiagnostic & Err)25 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
26 SourceMgr SM;
27 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F, false);
28 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
29
30 return LLParser(F.getBuffer(), SM, Err, &M).Run();
31 }
32
parseAssembly(MemoryBufferRef F,SMDiagnostic & Err,LLVMContext & Context)33 std::unique_ptr<Module> llvm::parseAssembly(MemoryBufferRef F,
34 SMDiagnostic &Err,
35 LLVMContext &Context) {
36 std::unique_ptr<Module> M =
37 make_unique<Module>(F.getBufferIdentifier(), Context);
38
39 if (parseAssemblyInto(F, *M, Err))
40 return nullptr;
41
42 return M;
43 }
44
parseAssemblyFile(StringRef Filename,SMDiagnostic & Err,LLVMContext & Context)45 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
46 SMDiagnostic &Err,
47 LLVMContext &Context) {
48 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
49 MemoryBuffer::getFileOrSTDIN(Filename);
50 if (std::error_code EC = FileOrErr.getError()) {
51 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
52 "Could not open input file: " + EC.message());
53 return nullptr;
54 }
55
56 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context);
57 }
58
parseAssemblyString(StringRef AsmString,SMDiagnostic & Err,LLVMContext & Context)59 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
60 SMDiagnostic &Err,
61 LLVMContext &Context) {
62 MemoryBufferRef F(AsmString, "<string>");
63 return parseAssembly(F, Err, Context);
64 }
65