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/LLVMContext.h" 00013 #include "llvm/Support/MemoryBuffer.h" 00014 #include <string> 00015 #include <cstring> 00016 00017 using namespace llvm; 00018 00019 /* Builds a module from the bitcode in the specified memory buffer, returning a 00020 reference to the module via the OutModule parameter. Returns 0 on success. 00021 Optionally returns a human-readable error message via OutMessage. */ 00022 int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, 00023 LLVMModuleRef *OutModule, char **OutMessage) { 00024 std::string Message; 00025 00026 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), getGlobalContext(), 00027 &Message)); 00028 if (!*OutModule) { 00029 if (OutMessage) 00030 *OutMessage = strdup(Message.c_str()); 00031 return 1; 00032 } 00033 00034 return 0; 00035 } 00036 00037 int LLVMParseBitcodeInContext(LLVMContextRef ContextRef, 00038 LLVMMemoryBufferRef MemBuf, 00039 LLVMModuleRef *OutModule, char **OutMessage) { 00040 std::string Message; 00041 00042 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef), 00043 &Message)); 00044 if (!*OutModule) { 00045 if (OutMessage) 00046 *OutMessage = strdup(Message.c_str()); 00047 return 1; 00048 } 00049 00050 return 0; 00051 } 00052 00053 /* Reads a module from the specified path, returning via the OutModule parameter 00054 a module provider which performs lazy deserialization. Returns 0 on success. 00055 Optionally returns a human-readable error message via OutMessage. */ 00056 int LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf, 00057 LLVMModuleProviderRef *OutMP, 00058 char **OutMessage) { 00059 std::string Message; 00060 00061 *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), getGlobalContext(), 00062 &Message)); 00063 00064 if (!*OutMP) { 00065 if (OutMessage) 00066 *OutMessage = strdup(Message.c_str()); 00067 return 1; 00068 } 00069 00070 return 0; 00071 } 00072 00073 int LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef, 00074 LLVMMemoryBufferRef MemBuf, 00075 LLVMModuleProviderRef *OutMP, 00076 char **OutMessage) { 00077 std::string Message; 00078 00079 *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), *unwrap(ContextRef), 00080 &Message)); 00081 if (!*OutMP) { 00082 if (OutMessage) 00083 *OutMessage = strdup(Message.c_str()); 00084 return 1; 00085 } 00086 00087 return 0; 00088 }