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