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"
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. */
23 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
24 LLVMModuleRef *OutModule, char **OutMessage) {
25 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
29 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
30 LLVMMemoryBufferRef MemBuf,
31 LLVMModuleRef *OutModule,
33 ErrorOr<Module *> ModuleOrErr =
34 parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef));
35 if (std::error_code EC = ModuleOrErr.getError()) {
37 *OutMessage = strdup(EC.message().c_str());
38 *OutModule = wrap((Module*)nullptr);
42 *OutModule = wrap(ModuleOrErr.get());
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. */
49 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
50 LLVMMemoryBufferRef MemBuf,
54 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
56 ErrorOr<Module *> ModuleOrErr =
57 getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
60 if (std::error_code EC = ModuleOrErr.getError()) {
61 *OutM = wrap((Module *)nullptr);
63 *OutMessage = strdup(EC.message().c_str());
67 *OutM = wrap(ModuleOrErr.get());
73 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
75 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
79 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
80 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
81 LLVMMemoryBufferRef MemBuf,
82 LLVMModuleProviderRef *OutMP,
84 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
85 reinterpret_cast<LLVMModuleRef*>(OutMP),
89 /* Deprecated: Use LLVMGetBitcodeModule instead. */
90 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
91 LLVMModuleProviderRef *OutMP,
93 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,