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