1f22ef01cSRoman Divacky //===-- BitReader.cpp -----------------------------------------------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky 
10f22ef01cSRoman Divacky #include "llvm-c/BitReader.h"
11f22ef01cSRoman Divacky #include "llvm/Bitcode/ReaderWriter.h"
12139f7f9bSDimitry Andric #include "llvm/IR/LLVMContext.h"
13284c1978SDimitry Andric #include "llvm/IR/Module.h"
14f22ef01cSRoman Divacky #include "llvm/Support/MemoryBuffer.h"
15f22ef01cSRoman Divacky #include <cstring>
16139f7f9bSDimitry Andric #include <string>
17f22ef01cSRoman Divacky 
18f22ef01cSRoman Divacky using namespace llvm;
19f22ef01cSRoman Divacky 
20f22ef01cSRoman Divacky /* Builds a module from the bitcode in the specified memory buffer, returning a
21f22ef01cSRoman Divacky    reference to the module via the OutModule parameter. Returns 0 on success.
22f22ef01cSRoman Divacky    Optionally returns a human-readable error message via OutMessage. */
23f22ef01cSRoman Divacky LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
24f22ef01cSRoman Divacky                           LLVMModuleRef *OutModule, char **OutMessage) {
25f22ef01cSRoman Divacky   return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
26f22ef01cSRoman Divacky                                    OutMessage);
27f22ef01cSRoman Divacky }
28f22ef01cSRoman Divacky 
29f22ef01cSRoman Divacky LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
30f22ef01cSRoman Divacky                                    LLVMMemoryBufferRef MemBuf,
31f22ef01cSRoman Divacky                                    LLVMModuleRef *OutModule,
32f22ef01cSRoman Divacky                                    char **OutMessage) {
3391bc56edSDimitry Andric   ErrorOr<Module *> ModuleOrErr =
34*39d628a0SDimitry Andric       parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef));
3591bc56edSDimitry Andric   if (std::error_code EC = ModuleOrErr.getError()) {
36f22ef01cSRoman Divacky     if (OutMessage)
3791bc56edSDimitry Andric       *OutMessage = strdup(EC.message().c_str());
3891bc56edSDimitry Andric     *OutModule = wrap((Module*)nullptr);
39f22ef01cSRoman Divacky     return 1;
40f22ef01cSRoman Divacky   }
41f22ef01cSRoman Divacky 
4291bc56edSDimitry Andric   *OutModule = wrap(ModuleOrErr.get());
43f22ef01cSRoman Divacky   return 0;
44f22ef01cSRoman Divacky }
45f22ef01cSRoman Divacky 
46f22ef01cSRoman Divacky /* Reads a module from the specified path, returning via the OutModule parameter
47f22ef01cSRoman Divacky    a module provider which performs lazy deserialization. Returns 0 on success.
48f22ef01cSRoman Divacky    Optionally returns a human-readable error message via OutMessage. */
49f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
50f22ef01cSRoman Divacky                                        LLVMMemoryBufferRef MemBuf,
51f22ef01cSRoman Divacky                                        LLVMModuleRef *OutM,
52f22ef01cSRoman Divacky                                        char **OutMessage) {
53f22ef01cSRoman Divacky   std::string Message;
54*39d628a0SDimitry Andric   std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
55*39d628a0SDimitry Andric 
5691bc56edSDimitry Andric   ErrorOr<Module *> ModuleOrErr =
57*39d628a0SDimitry Andric       getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
58*39d628a0SDimitry Andric   Owner.release();
59f22ef01cSRoman Divacky 
6091bc56edSDimitry Andric   if (std::error_code EC = ModuleOrErr.getError()) {
6191bc56edSDimitry Andric     *OutM = wrap((Module *)nullptr);
62f22ef01cSRoman Divacky     if (OutMessage)
6391bc56edSDimitry Andric       *OutMessage = strdup(EC.message().c_str());
64f22ef01cSRoman Divacky     return 1;
65f22ef01cSRoman Divacky   }
66f22ef01cSRoman Divacky 
6791bc56edSDimitry Andric   *OutM = wrap(ModuleOrErr.get());
6891bc56edSDimitry Andric 
69f22ef01cSRoman Divacky   return 0;
70f22ef01cSRoman Divacky 
71f22ef01cSRoman Divacky }
72f22ef01cSRoman Divacky 
73f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
74f22ef01cSRoman Divacky                               char **OutMessage) {
75f22ef01cSRoman Divacky   return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
76f22ef01cSRoman Divacky                                        OutMessage);
77f22ef01cSRoman Divacky }
78f22ef01cSRoman Divacky 
79f22ef01cSRoman Divacky /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
80f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
81f22ef01cSRoman Divacky                                                LLVMMemoryBufferRef MemBuf,
82f22ef01cSRoman Divacky                                                LLVMModuleProviderRef *OutMP,
83f22ef01cSRoman Divacky                                                char **OutMessage) {
84f22ef01cSRoman Divacky   return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
85f22ef01cSRoman Divacky                                        reinterpret_cast<LLVMModuleRef*>(OutMP),
86f22ef01cSRoman Divacky                                        OutMessage);
87f22ef01cSRoman Divacky }
88f22ef01cSRoman Divacky 
89f22ef01cSRoman Divacky /* Deprecated: Use LLVMGetBitcodeModule instead. */
90f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
91f22ef01cSRoman Divacky                                       LLVMModuleProviderRef *OutMP,
92f22ef01cSRoman Divacky                                       char **OutMessage) {
93f22ef01cSRoman Divacky   return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
94f22ef01cSRoman Divacky                                                OutMP, OutMessage);
95f22ef01cSRoman Divacky }
96