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), *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 ErrorOr<Module *> ModuleOrErr =
55 getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef));
57 if (std::error_code EC = ModuleOrErr.getError()) {
58 *OutM = wrap((Module *)nullptr);
60 *OutMessage = strdup(EC.message().c_str());
64 *OutM = wrap(ModuleOrErr.get());
70 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
72 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
76 /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
77 LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
78 LLVMMemoryBufferRef MemBuf,
79 LLVMModuleProviderRef *OutMP,
81 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
82 reinterpret_cast<LLVMModuleRef*>(OutMP),
86 /* Deprecated: Use LLVMGetBitcodeModule instead. */
87 LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
88 LLVMModuleProviderRef *OutMP,
90 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,