1 //===-- BitReader.cpp -----------------------------------------------------===// 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 #include "llvm-c/BitReader.h" 11 #include "llvm/Bitcode/ReaderWriter.h" 12 #include "llvm/IR/LLVMContext.h" 13 #include "llvm/IR/Module.h" 14 #include "llvm/Support/MemoryBuffer.h" 15 #include <cstring> 16 #include <string> 17 18 using namespace llvm; 19 20 /* Builds a module from the bitcode in the specified memory buffer, returning a 21 reference to the module via the OutModule parameter. Returns 0 on success. 22 Optionally returns a human-readable error message via OutMessage. */ 23 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, 24 LLVMModuleRef *OutModule, char **OutMessage) { 25 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule, 26 OutMessage); 27 } 28 29 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, 30 LLVMMemoryBufferRef MemBuf, 31 LLVMModuleRef *OutModule, 32 char **OutMessage) { 33 ErrorOr<Module *> ModuleOrErr = 34 parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef)); 35 if (std::error_code EC = ModuleOrErr.getError()) { 36 if (OutMessage) 37 *OutMessage = strdup(EC.message().c_str()); 38 *OutModule = wrap((Module*)nullptr); 39 return 1; 40 } 41 42 *OutModule = wrap(ModuleOrErr.get()); 43 return 0; 44 } 45 46 /* Reads a module from the specified path, returning via the OutModule parameter 47 a module provider which performs lazy deserialization. Returns 0 on success. 48 Optionally returns a human-readable error message via OutMessage. */ 49 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, 50 LLVMMemoryBufferRef MemBuf, 51 LLVMModuleRef *OutM, 52 char **OutMessage) { 53 std::string Message; 54 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf)); 55 56 ErrorOr<Module *> ModuleOrErr = 57 getLazyBitcodeModule(Owner, *unwrap(ContextRef)); 58 Owner.release(); 59 60 if (std::error_code EC = ModuleOrErr.getError()) { 61 *OutM = wrap((Module *)nullptr); 62 if (OutMessage) 63 *OutMessage = strdup(EC.message().c_str()); 64 return 1; 65 } 66 67 *OutM = wrap(ModuleOrErr.get()); 68 69 return 0; 70 71 } 72 73 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, 74 char **OutMessage) { 75 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM, 76 OutMessage); 77 } 78 79 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */ 80 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, 81 LLVMMemoryBufferRef MemBuf, 82 LLVMModuleProviderRef *OutMP, 83 char **OutMessage) { 84 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf, 85 reinterpret_cast<LLVMModuleRef*>(OutMP), 86 OutMessage); 87 } 88 89 /* Deprecated: Use LLVMGetBitcodeModule instead. */ 90 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, 91 LLVMModuleProviderRef *OutMP, 92 char **OutMessage) { 93 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf, 94 OutMP, OutMessage); 95 } 96