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(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
26 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F, false);
27 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
29 return LLParser(F.getBuffer(), SM, Err, &M).Run();
32 std::unique_ptr<Module> llvm::parseAssembly(MemoryBufferRef F,
34 LLVMContext &Context) {
35 std::unique_ptr<Module> M =
36 make_unique<Module>(F.getBufferIdentifier(), Context);
38 if (parseAssemblyInto(F, *M, Err))
44 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
46 LLVMContext &Context) {
47 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
48 MemoryBuffer::getFileOrSTDIN(Filename);
49 if (std::error_code EC = FileOrErr.getError()) {
50 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
51 "Could not open input file: " + EC.message());
55 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context);
58 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
60 LLVMContext &Context) {
61 MemoryBufferRef F(AsmString, "<string>");
62 return parseAssembly(F, Err, Context);