1193323Sed//===-- BitReader.cpp -----------------------------------------------------===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed
10193323Sed#include "llvm-c/BitReader.h"
11193323Sed#include "llvm/Bitcode/ReaderWriter.h"
12249423Sdim#include "llvm/IR/LLVMContext.h"
13251662Sdim#include "llvm/IR/Module.h"
14193323Sed#include "llvm/Support/MemoryBuffer.h"
15249423Sdim#include <cstring>
16193323Sed#include <string>
17193323Sed
18193323Sedusing namespace llvm;
19193323Sed
20193323Sed/* Builds a module from the bitcode in the specified memory buffer, returning a
21193323Sed   reference to the module via the OutModule parameter. Returns 0 on success.
22202375Srdivacky   Optionally returns a human-readable error message via OutMessage. */
23202375SrdivackyLLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
24202375Srdivacky                          LLVMModuleRef *OutModule, char **OutMessage) {
25203954Srdivacky  return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
26203954Srdivacky                                   OutMessage);
27193323Sed}
28193323Sed
29202375SrdivackyLLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
30202375Srdivacky                                   LLVMMemoryBufferRef MemBuf,
31202375Srdivacky                                   LLVMModuleRef *OutModule,
32202375Srdivacky                                   char **OutMessage) {
33195340Sed  std::string Message;
34249423Sdim
35198090Srdivacky  *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef),
36195340Sed                                     &Message));
37195340Sed  if (!*OutModule) {
38195340Sed    if (OutMessage)
39195340Sed      *OutMessage = strdup(Message.c_str());
40195340Sed    return 1;
41195340Sed  }
42249423Sdim
43195340Sed  return 0;
44195340Sed}
45195340Sed
46193323Sed/* Reads a module from the specified path, returning via the OutModule parameter
47193323Sed   a module provider which performs lazy deserialization. Returns 0 on success.
48249423Sdim   Optionally returns a human-readable error message via OutMessage. */
49204642SrdivackyLLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
50204642Srdivacky                                       LLVMMemoryBufferRef MemBuf,
51204642Srdivacky                                       LLVMModuleRef *OutM,
52204642Srdivacky                                       char **OutMessage) {
53195340Sed  std::string Message;
54249423Sdim
55204642Srdivacky  *OutM = wrap(getLazyBitcodeModule(unwrap(MemBuf), *unwrap(ContextRef),
56204642Srdivacky                                    &Message));
57204642Srdivacky  if (!*OutM) {
58193323Sed    if (OutMessage)
59193323Sed      *OutMessage = strdup(Message.c_str());
60193323Sed    return 1;
61193323Sed  }
62249423Sdim
63193323Sed  return 0;
64204642Srdivacky
65193323Sed}
66204642Srdivacky
67204642SrdivackyLLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
68204642Srdivacky                              char **OutMessage) {
69204642Srdivacky  return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
70204642Srdivacky                                       OutMessage);
71204642Srdivacky}
72204642Srdivacky
73204642Srdivacky/* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
74204642SrdivackyLLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
75204642Srdivacky                                               LLVMMemoryBufferRef MemBuf,
76204642Srdivacky                                               LLVMModuleProviderRef *OutMP,
77204642Srdivacky                                               char **OutMessage) {
78204642Srdivacky  return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
79204642Srdivacky                                       reinterpret_cast<LLVMModuleRef*>(OutMP),
80204642Srdivacky                                       OutMessage);
81204642Srdivacky}
82204642Srdivacky
83204642Srdivacky/* Deprecated: Use LLVMGetBitcodeModule instead. */
84204642SrdivackyLLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
85204642Srdivacky                                      LLVMModuleProviderRef *OutMP,
86204642Srdivacky                                      char **OutMessage) {
87204642Srdivacky  return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
88204642Srdivacky                                               OutMP, OutMessage);
89204642Srdivacky}
90