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 18using 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. */ 23LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, 24 LLVMModuleRef *OutModule, char **OutMessage) { 25 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule, 26 OutMessage); 27} 28 29LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, 30 LLVMMemoryBufferRef MemBuf, 31 LLVMModuleRef *OutModule, 32 char **OutMessage) { 33 std::string Message; 34 35 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef), 36 &Message)); 37 if (!*OutModule) { 38 if (OutMessage) 39 *OutMessage = strdup(Message.c_str()); 40 return 1; 41 } 42 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. */ 49LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, 50 LLVMMemoryBufferRef MemBuf, 51 LLVMModuleRef *OutM, 52 char **OutMessage) { 53 std::string Message; 54 55 *OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef), 56 &Message)); 57 if (!*OutM) { 58 if (OutMessage) 59 *OutMessage = strdup(Message.c_str()); 60 return 1; 61 } 62 63 return 0; 64 65} 66 67LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, 68 char **OutMessage) { 69 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM, 70 OutMessage); 71} 72 73/* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */ 74LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, 75 LLVMMemoryBufferRef MemBuf, 76 LLVMModuleProviderRef *OutMP, 77 char **OutMessage) { 78 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf, 79 reinterpret_cast<LLVMModuleRef*>(OutMP), 80 OutMessage); 81} 82 83/* Deprecated: Use LLVMGetBitcodeModule instead. */ 84LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, 85 LLVMModuleProviderRef *OutMP, 86 char **OutMessage) { 87 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf, 88 OutMP, OutMessage); 89} 90