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