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/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" 21 #include <cstring> 22 #include <system_error> 23 using namespace llvm; 24 25 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err, 26 SlotMapping *Slots, bool UpgradeDebugInfo, 27 StringRef DataLayoutString) { 28 SourceMgr SM; 29 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F); 30 SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); 31 32 return LLParser(F.getBuffer(), SM, Err, &M, Slots, UpgradeDebugInfo, 33 DataLayoutString) 34 .Run(); 35 } 36 37 std::unique_ptr<Module> 38 llvm::parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, 39 SlotMapping *Slots, bool UpgradeDebugInfo, 40 StringRef DataLayoutString) { 41 std::unique_ptr<Module> M = 42 make_unique<Module>(F.getBufferIdentifier(), Context); 43 44 if (parseAssemblyInto(F, *M, Err, Slots, UpgradeDebugInfo, DataLayoutString)) 45 return nullptr; 46 47 return M; 48 } 49 50 std::unique_ptr<Module> 51 llvm::parseAssemblyFile(StringRef Filename, SMDiagnostic &Err, 52 LLVMContext &Context, SlotMapping *Slots, 53 bool UpgradeDebugInfo, StringRef DataLayoutString) { 54 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 55 MemoryBuffer::getFileOrSTDIN(Filename); 56 if (std::error_code EC = FileOrErr.getError()) { 57 Err = SMDiagnostic(Filename, SourceMgr::DK_Error, 58 "Could not open input file: " + EC.message()); 59 return nullptr; 60 } 61 62 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots, 63 UpgradeDebugInfo, DataLayoutString); 64 } 65 66 std::unique_ptr<Module> 67 llvm::parseAssemblyString(StringRef AsmString, SMDiagnostic &Err, 68 LLVMContext &Context, SlotMapping *Slots, 69 bool UpgradeDebugInfo, StringRef DataLayoutString) { 70 MemoryBufferRef F(AsmString, "<string>"); 71 return parseAssembly(F, Err, Context, Slots, UpgradeDebugInfo, 72 DataLayoutString); 73 } 74 75 Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err, 76 const Module &M, const SlotMapping *Slots) { 77 SourceMgr SM; 78 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); 79 SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); 80 Constant *C; 81 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M)) 82 .parseStandaloneConstantValue(C, Slots)) 83 return nullptr; 84 return C; 85 } 86 87 Type *llvm::parseType(StringRef Asm, SMDiagnostic &Err, const Module &M, 88 const SlotMapping *Slots) { 89 unsigned Read; 90 Type *Ty = parseTypeAtBeginning(Asm, Read, Err, M, Slots); 91 if (!Ty) 92 return nullptr; 93 if (Read != Asm.size()) { 94 SourceMgr SM; 95 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); 96 SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); 97 Err = SM.GetMessage(SMLoc::getFromPointer(Asm.begin() + Read), 98 SourceMgr::DK_Error, "expected end of string"); 99 return nullptr; 100 } 101 return Ty; 102 } 103 Type *llvm::parseTypeAtBeginning(StringRef Asm, unsigned &Read, 104 SMDiagnostic &Err, const Module &M, 105 const SlotMapping *Slots) { 106 SourceMgr SM; 107 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Asm); 108 SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); 109 Type *Ty; 110 if (LLParser(Asm, SM, Err, const_cast<Module *>(&M)) 111 .parseTypeAtBeginning(Ty, Read, Slots)) 112 return nullptr; 113 return Ty; 114 } 115