1 //===-- BitReader.cpp -----------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
19 using std::error_code;
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,
30 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
31 LLVMMemoryBufferRef MemBuf,
32 LLVMModuleRef *OutModule,
34 ErrorOr<Module *> ModuleOrErr =
35 parseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef));
36 if (error_code EC = ModuleOrErr.getError()) {
38 *OutMessage = strdup(EC.message().c_str());
39 *OutModule = wrap((Module*)nullptr);
43 *OutModule = wrap(ModuleOrErr.get());
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,
55 ErrorOr<Module *> ModuleOrErr =
56 getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef));
58 if (error_code EC = ModuleOrErr.getError()) {
59 *OutM = wrap((Module *)nullptr);
61 *OutMessage = strdup(EC.message().c_str());
65 *OutM = wrap(ModuleOrErr.get());
71 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
73 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
77 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
78 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
79 LLVMMemoryBufferRef MemBuf,
80 LLVMModuleProviderRef *OutMP,
82 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
83 reinterpret_cast<LLVMModuleRef*>(OutMP),
87 /* Deprecated: Use LLVMGetBitcodeModule instead. */
88 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
89 LLVMModuleProviderRef *OutMP,
91 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,