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))