1*f22ef01cSRoman Divacky //===-- BitReader.cpp -----------------------------------------------------===//
2*f22ef01cSRoman Divacky //
3*f22ef01cSRoman Divacky //                     The LLVM Compiler Infrastructure
4*f22ef01cSRoman Divacky //
5*f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6*f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7*f22ef01cSRoman Divacky //
8*f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9*f22ef01cSRoman Divacky 
10*f22ef01cSRoman Divacky #include "llvm-c/BitReader.h"
11*f22ef01cSRoman Divacky #include "llvm/Bitcode/ReaderWriter.h"
12*f22ef01cSRoman Divacky #include "llvm/LLVMContext.h"
13*f22ef01cSRoman Divacky #include "llvm/Support/MemoryBuffer.h"
14*f22ef01cSRoman Divacky #include <string>
15*f22ef01cSRoman Divacky #include <cstring>
16*f22ef01cSRoman Divacky 
17*f22ef01cSRoman Divacky using namespace llvm;
18*f22ef01cSRoman Divacky 
19*f22ef01cSRoman Divacky /* Builds a module from the bitcode in the specified memory buffer, returning a
20*f22ef01cSRoman Divacky    reference to the module via the OutModule parameter. Returns 0 on success.
21*f22ef01cSRoman Divacky    Optionally returns a human-readable error message via OutMessage. */
22*f22ef01cSRoman Divacky LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
23*f22ef01cSRoman Divacky                           LLVMModuleRef *OutModule, char **OutMessage) {
24*f22ef01cSRoman Divacky   return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
25*f22ef01cSRoman Divacky                                    OutMessage);
26*f22ef01cSRoman Divacky }
27*f22ef01cSRoman Divacky 
28*f22ef01cSRoman Divacky LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
29*f22ef01cSRoman Divacky                                    LLVMMemoryBufferRef MemBuf,
30*f22ef01cSRoman Divacky                                    LLVMModuleRef *OutModule,
31*f22ef01cSRoman Divacky                                    char **OutMessage) {
32*f22ef01cSRoman Divacky   std::string Message;
33*f22ef01cSRoman Divacky 
34*f22ef01cSRoman Divacky   *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef),
35*f22ef01cSRoman Divacky                                      &Message));
36*f22ef01cSRoman Divacky   if (!*OutModule) {
37*f22ef01cSRoman Divacky     if (OutMessage)
38*f22ef01cSRoman Divacky       *OutMessage = strdup(Message.c_str());
39*f22ef01cSRoman Divacky     return 1;
40*f22ef01cSRoman Divacky   }
41*f22ef01cSRoman Divacky 
42*f22ef01cSRoman Divacky   return 0;
43*f22ef01cSRoman Divacky }
44*f22ef01cSRoman Divacky 
45*f22ef01cSRoman Divacky /* Reads a module from the specified path, returning via the OutModule parameter
46*f22ef01cSRoman Divacky    a module provider which performs lazy deserialization. Returns 0 on success.
47*f22ef01cSRoman Divacky    Optionally returns a human-readable error message via OutMessage. */
48*f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
49*f22ef01cSRoman Divacky                                        LLVMMemoryBufferRef MemBuf,
50*f22ef01cSRoman Divacky                                        LLVMModuleRef *OutM,
51*f22ef01cSRoman Divacky                                        char **OutMessage) {
52*f22ef01cSRoman Divacky   std::string Message;
53*f22ef01cSRoman Divacky 
54*f22ef01cSRoman Divacky   *OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef),
55*f22ef01cSRoman Divacky                                     &Message));
56*f22ef01cSRoman Divacky   if (!*OutM) {
57*f22ef01cSRoman Divacky     if (OutMessage)
58*f22ef01cSRoman Divacky       *OutMessage = strdup(Message.c_str());
59*f22ef01cSRoman Divacky     return 1;
60*f22ef01cSRoman Divacky   }
61*f22ef01cSRoman Divacky 
62*f22ef01cSRoman Divacky   return 0;
63*f22ef01cSRoman Divacky 
64*f22ef01cSRoman Divacky }
65*f22ef01cSRoman Divacky 
66*f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
67*f22ef01cSRoman Divacky                               char **OutMessage) {
68*f22ef01cSRoman Divacky   return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
69*f22ef01cSRoman Divacky                                        OutMessage);
70*f22ef01cSRoman Divacky }
71*f22ef01cSRoman Divacky 
72*f22ef01cSRoman Divacky /* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
73*f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
74*f22ef01cSRoman Divacky                                                LLVMMemoryBufferRef MemBuf,
75*f22ef01cSRoman Divacky                                                LLVMModuleProviderRef *OutMP,
76*f22ef01cSRoman Divacky                                                char **OutMessage) {
77*f22ef01cSRoman Divacky   return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
78*f22ef01cSRoman Divacky                                        reinterpret_cast<LLVMModuleRef*>(OutMP),
79*f22ef01cSRoman Divacky                                        OutMessage);
80*f22ef01cSRoman Divacky }
81*f22ef01cSRoman Divacky 
82*f22ef01cSRoman Divacky /* Deprecated: Use LLVMGetBitcodeModule instead. */
83*f22ef01cSRoman Divacky LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
84*f22ef01cSRoman Divacky                                       LLVMModuleProviderRef *OutMP,
85*f22ef01cSRoman Divacky                                       char **OutMessage) {
86*f22ef01cSRoman Divacky   return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
87*f22ef01cSRoman Divacky                                                OutMP, OutMessage);
88*f22ef01cSRoman Divacky }
89