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 bool llvm::parseAssemblyInto(std::unique_ptr<MemoryBuffer> F, Module &M,
27 StringRef Buf = F->getBuffer();
28 SM.AddNewSourceBuffer(F.release(), SMLoc());
30 return LLParser(Buf, SM, Err, &M).Run();
33 std::unique_ptr<Module> llvm::parseAssembly(std::unique_ptr<MemoryBuffer> F,
35 LLVMContext &Context) {
36 std::unique_ptr<Module> M =
37 make_unique<Module>(F->getBufferIdentifier(), Context);
39 if (parseAssemblyInto(std::move(F), *M, Err))
45 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
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());
56 return parseAssembly(std::move(FileOrErr.get()), Err, Context);
59 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
61 LLVMContext &Context) {
62 std::unique_ptr<MemoryBuffer> F(
63 MemoryBuffer::getMemBuffer(AsmString, "<string>"));
65 return parseAssembly(std::move(F), Err, Context);