BitReader.cpp revision 249423
1120586Sbms//===-- BitReader.cpp -----------------------------------------------------===// 2120586Sbms// 3120586Sbms// The LLVM Compiler Infrastructure 4120586Sbms// 5120586Sbms// This file is distributed under the University of Illinois Open Source 6120586Sbms// License. See LICENSE.TXT for details. 7120586Sbms// 8120586Sbms//===----------------------------------------------------------------------===// 9120586Sbms 10120586Sbms#include "llvm-c/BitReader.h" 11120586Sbms#include "llvm/Bitcode/ReaderWriter.h" 12120586Sbms#include "llvm/IR/LLVMContext.h" 13120586Sbms#include "llvm/Support/MemoryBuffer.h" 14120586Sbms#include <cstring> 15120586Sbms#include <string> 16120586Sbms 17120586Sbmsusing namespace llvm; 18120586Sbms 19120586Sbms/* Builds a module from the bitcode in the specified memory buffer, returning a 20120586Sbms reference to the module via the OutModule parameter. Returns 0 on success. 21120586Sbms Optionally returns a human-readable error message via OutMessage. */ 22120586SbmsLLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, 23120586Sbms LLVMModuleRef *OutModule, char **OutMessage) { 24120586Sbms return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule, 25120586Sbms OutMessage); 26120586Sbms} 27120586Sbms 28255495SjhbLLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, 29131680Sru LLVMMemoryBufferRef MemBuf, 30131681Sru LLVMModuleRef *OutModule, 31120586Sbms char **OutMessage) { 32120586Sbms std::string Message; 33120586Sbms 34120586Sbms *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef), 35120586Sbms &Message)); 36120586Sbms if (!*OutModule) { 37120586Sbms if (OutMessage) 38120586Sbms *OutMessage = strdup(Message.c_str()); 39131680Sru return 1; 40131680Sru } 41255495Sjhb 42255495Sjhb return 0; 43131680Sru} 44120586Sbms 45120586Sbms/* Reads a module from the specified path, returning via the OutModule parameter 46120586Sbms a module provider which performs lazy deserialization. Returns 0 on success. 47120586Sbms Optionally returns a human-readable error message via OutMessage. */ 48120586SbmsLLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, 49120586Sbms LLVMMemoryBufferRef MemBuf, 50255495Sjhb LLVMModuleRef *OutM, 51255495Sjhb char **OutMessage) { 52255495Sjhb std::string Message; 53255495Sjhb 54255495Sjhb *OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef), 55255495Sjhb &Message)); 56255495Sjhb if (!*OutM) { 57120586Sbms if (OutMessage) 58120586Sbms *OutMessage = strdup(Message.c_str()); 59120586Sbms return 1; 60120586Sbms } 61120586Sbms 62120586Sbms return 0; 63120586Sbms 64120586Sbms} 65120586Sbms 66120586SbmsLLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, 67120586Sbms char **OutMessage) { 68120586Sbms return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM, 69120586Sbms OutMessage); 70131680Sru} 71131680Sru 72131680Sru/* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */ 73120586SbmsLLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, 74120586Sbms LLVMMemoryBufferRef MemBuf, 75120586Sbms LLVMModuleProviderRef *OutMP, 76255495Sjhb char **OutMessage) { 77255495Sjhb return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf, 78255495Sjhb reinterpret_cast<LLVMModuleRef*>(OutMP), 79255495Sjhb OutMessage); 80255495Sjhb} 81255495Sjhb 82255495Sjhb/* Deprecated: Use LLVMGetBitcodeModule instead. */ 83255495SjhbLLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, 84120586Sbms LLVMModuleProviderRef *OutMP, 85255495Sjhb char **OutMessage) { 86255495Sjhb return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf, 87255495Sjhb OutMP, OutMessage); 88255495Sjhb} 89120586Sbms