1 //===-- BitReader.cpp -----------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm-c/BitReader.h"
11 #include "llvm-c/Core.h"
12 #include "llvm/Bitcode/BitcodeReader.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/Support/MemoryBuffer.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <cstring>
18 #include <string>
19
20 using namespace llvm;
21
22 /* Builds a module from the bitcode in the specified memory buffer, returning a
23 reference to the module via the OutModule parameter. Returns 0 on success.
24 Optionally returns a human-readable error message via OutMessage. */
LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule,char ** OutMessage)25 LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
26 char **OutMessage) {
27 return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
28 OutMessage);
29 }
30
LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule)31 LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
32 LLVMModuleRef *OutModule) {
33 return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
34 }
35
LLVMParseBitcodeInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule,char ** OutMessage)36 LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
37 LLVMMemoryBufferRef MemBuf,
38 LLVMModuleRef *OutModule,
39 char **OutMessage) {
40 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
41 LLVMContext &Ctx = *unwrap(ContextRef);
42
43 Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
44 if (Error Err = ModuleOrErr.takeError()) {
45 std::string Message;
46 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
47 Message = EIB.message();
48 });
49 if (OutMessage)
50 *OutMessage = strdup(Message.c_str());
51 *OutModule = wrap((Module *)nullptr);
52 return 1;
53 }
54
55 *OutModule = wrap(ModuleOrErr.get().release());
56 return 0;
57 }
58
LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule)59 LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
60 LLVMMemoryBufferRef MemBuf,
61 LLVMModuleRef *OutModule) {
62 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
63 LLVMContext &Ctx = *unwrap(ContextRef);
64
65 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
66 expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
67 if (ModuleOrErr.getError()) {
68 *OutModule = wrap((Module *)nullptr);
69 return 1;
70 }
71
72 *OutModule = wrap(ModuleOrErr.get().release());
73 return 0;
74 }
75
76 /* Reads a module from the specified path, returning via the OutModule parameter
77 a module provider which performs lazy deserialization. Returns 0 on success.
78 Optionally returns a human-readable error message via OutMessage. */
LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)79 LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
80 LLVMMemoryBufferRef MemBuf,
81 LLVMModuleRef *OutM, char **OutMessage) {
82 LLVMContext &Ctx = *unwrap(ContextRef);
83 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
84 Expected<std::unique_ptr<Module>> ModuleOrErr =
85 getOwningLazyBitcodeModule(std::move(Owner), Ctx);
86 // Release the buffer if we didn't take ownership of it since we never owned
87 // it anyway.
88 (void)Owner.release();
89
90 if (Error Err = ModuleOrErr.takeError()) {
91 std::string Message;
92 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
93 Message = EIB.message();
94 });
95 if (OutMessage)
96 *OutMessage = strdup(Message.c_str());
97 *OutM = wrap((Module *)nullptr);
98 return 1;
99 }
100
101 *OutM = wrap(ModuleOrErr.get().release());
102
103 return 0;
104 }
105
LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM)106 LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
107 LLVMMemoryBufferRef MemBuf,
108 LLVMModuleRef *OutM) {
109 LLVMContext &Ctx = *unwrap(ContextRef);
110 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
111
112 ErrorOr<std::unique_ptr<Module>> ModuleOrErr = expectedToErrorOrAndEmitErrors(
113 Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
114 Owner.release();
115
116 if (ModuleOrErr.getError()) {
117 *OutM = wrap((Module *)nullptr);
118 return 1;
119 }
120
121 *OutM = wrap(ModuleOrErr.get().release());
122 return 0;
123 }
124
LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)125 LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
126 char **OutMessage) {
127 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
128 OutMessage);
129 }
130
LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM)131 LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
132 LLVMModuleRef *OutM) {
133 return LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), MemBuf, OutM);
134 }
135