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