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/IR/Module.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "llvm/Support/SourceMgr.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <cstring> 21 #include <system_error> 22 using namespace llvm; 23 24 bool llvm::parseAssemblyInto(std::unique_ptr<MemoryBuffer> F, Module &M, 25 SMDiagnostic &Err) { 26 SourceMgr SM; 27 StringRef Buf = F->getBuffer(); 28 SM.AddNewSourceBuffer(std::move(F), SMLoc()); 29 30 return LLParser(Buf, SM, Err, &M).Run(); 31 } 32 33 std::unique_ptr<Module> llvm::parseAssembly(std::unique_ptr<MemoryBuffer> F, 34 SMDiagnostic &Err, 35 LLVMContext &Context) { 36 std::unique_ptr<Module> M = 37 make_unique<Module>(F->getBufferIdentifier(), Context); 38 39 if (parseAssemblyInto(std::move(F), *M, Err)) 40 return nullptr; 41 42 return std::move(M); 43 } 44 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(std::move(FileOrErr.get()), Err, Context); 57 } 58 59 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString, 60 SMDiagnostic &Err, 61 LLVMContext &Context) { 62 std::unique_ptr<MemoryBuffer> F( 63 MemoryBuffer::getMemBuffer(AsmString, "<string>")); 64 65 return parseAssembly(std::move(F), Err, Context); 66 } 67