LLVM API Documentation
00001 //===-- BitReader.cpp -----------------------------------------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "llvm-c/BitReader.h" 00011 #include "llvm/Bitcode/ReaderWriter.h" 00012 #include "llvm/IR/LLVMContext.h" 00013 #include "llvm/IR/Module.h" 00014 #include "llvm/Support/MemoryBuffer.h" 00015 #include <cstring> 00016 #include <string> 00017 00018 using namespace llvm; 00019 00020 /* Builds a module from the bitcode in the specified memory buffer, returning a 00021 reference to the module via the OutModule parameter. Returns 0 on success. 00022 Optionally returns a human-readable error message via OutMessage. */ 00023 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, 00024 LLVMModuleRef *OutModule, char **OutMessage) { 00025 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule, 00026 OutMessage); 00027 } 00028 00029 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, 00030 LLVMMemoryBufferRef MemBuf, 00031 LLVMModuleRef *OutModule, 00032 char **OutMessage) { 00033 ErrorOr<Module *> ModuleOrErr = 00034 parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef)); 00035 if (std::error_code EC = ModuleOrErr.getError()) { 00036 if (OutMessage) 00037 *OutMessage = strdup(EC.message().c_str()); 00038 *OutModule = wrap((Module*)nullptr); 00039 return 1; 00040 } 00041 00042 *OutModule = wrap(ModuleOrErr.get()); 00043 return 0; 00044 } 00045 00046 /* Reads a module from the specified path, returning via the OutModule parameter 00047 a module provider which performs lazy deserialization. Returns 0 on success. 00048 Optionally returns a human-readable error message via OutMessage. */ 00049 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, 00050 LLVMMemoryBufferRef MemBuf, 00051 LLVMModuleRef *OutM, 00052 char **OutMessage) { 00053 std::string Message; 00054 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf)); 00055 00056 ErrorOr<Module *> ModuleOrErr = 00057 getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef)); 00058 Owner.release(); 00059 00060 if (std::error_code EC = ModuleOrErr.getError()) { 00061 *OutM = wrap((Module *)nullptr); 00062 if (OutMessage) 00063 *OutMessage = strdup(EC.message().c_str()); 00064 return 1; 00065 } 00066 00067 *OutM = wrap(ModuleOrErr.get()); 00068 00069 return 0; 00070 00071 } 00072 00073 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, 00074 char **OutMessage) { 00075 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM, 00076 OutMessage); 00077 } 00078 00079 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */ 00080 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, 00081 LLVMMemoryBufferRef MemBuf, 00082 LLVMModuleProviderRef *OutMP, 00083 char **OutMessage) { 00084 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf, 00085 reinterpret_cast<LLVMModuleRef*>(OutMP), 00086 OutMessage); 00087 } 00088 00089 /* Deprecated: Use LLVMGetBitcodeModule instead. */ 00090 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, 00091 LLVMModuleProviderRef *OutMP, 00092 char **OutMessage) { 00093 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf, 00094 OutMP, OutMessage); 00095 }