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/IR/Module.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "llvm/Support/raw_ostream.h"
21 #include <system_error>
24 Module *llvm::ParseAssembly(std::unique_ptr<MemoryBuffer> F, Module *M,
25 SMDiagnostic &Err, LLVMContext &Context) {
27 MemoryBuffer *Buf = F.get();
28 SM.AddNewSourceBuffer(F.release(), SMLoc());
30 // If we are parsing into an existing module, do it.
32 return LLParser(Buf->getBuffer(), SM, Err, M).Run() ? nullptr : M;
34 // Otherwise create a new module.
35 std::unique_ptr<Module> M2(new Module(Buf->getBufferIdentifier(), Context));
36 if (LLParser(Buf->getBuffer(), SM, Err, M2.get()).Run())
41 Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
42 LLVMContext &Context) {
43 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
44 MemoryBuffer::getFileOrSTDIN(Filename);
45 if (std::error_code EC = FileOrErr.getError()) {
46 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
47 "Could not open input file: " + EC.message());
51 return ParseAssembly(std::move(FileOrErr.get()), nullptr, Err, Context);
54 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
55 SMDiagnostic &Err, LLVMContext &Context) {
57 MemoryBuffer::getMemBuffer(StringRef(AsmString), "<string>");
59 return ParseAssembly(std::unique_ptr<MemoryBuffer>(F), M, Err, Context);