BitReader.cpp revision 195340
1185380Ssam//===-- BitReader.cpp -----------------------------------------------------===//
2187831Ssam//
3185380Ssam//                     The LLVM Compiler Infrastructure
4185380Ssam//
5185380Ssam// This file is distributed under the University of Illinois Open Source
6185380Ssam// License. See LICENSE.TXT for details.
7185380Ssam//
8185380Ssam//===----------------------------------------------------------------------===//
9185380Ssam
10185380Ssam#include "llvm-c/BitReader.h"
11185380Ssam#include "llvm/Bitcode/ReaderWriter.h"
12185380Ssam#include "llvm/LLVMContext.h"
13185380Ssam#include "llvm/Support/MemoryBuffer.h"
14185380Ssam#include <string>
15185380Ssam#include <cstring>
16185380Ssam
17187831Ssamusing namespace llvm;
18185380Ssam
19185380Ssam/* Builds a module from the bitcode in the specified memory buffer, returning a
20185380Ssam   reference to the module via the OutModule parameter. Returns 0 on success.
21185380Ssam   Optionally returns a human-readable error message via OutMessage. */
22185380Ssamint LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
23185380Ssam                     LLVMModuleRef *OutModule, char **OutMessage) {
24185380Ssam  std::string Message;
25185380Ssam
26185380Ssam  *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), getGlobalContext(),
27185380Ssam                                     &Message));
28185380Ssam  if (!*OutModule) {
29185380Ssam    if (OutMessage)
30185380Ssam      *OutMessage = strdup(Message.c_str());
31185380Ssam    return 1;
32185380Ssam  }
33185380Ssam
34185380Ssam  return 0;
35185380Ssam}
36185380Ssam
37185380Ssamint LLVMParseBitcodeInContext(LLVMMemoryBufferRef MemBuf,
38185380Ssam                              LLVMContextRef ContextRef,
39185380Ssam                              LLVMModuleRef *OutModule, char **OutMessage) {
40185380Ssam  std::string Message;
41185380Ssam
42185380Ssam  *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), *unwrap(ContextRef),
43185380Ssam                                     &Message));
44185380Ssam  if (!*OutModule) {
45185380Ssam    if (OutMessage)
46185380Ssam      *OutMessage = strdup(Message.c_str());
47185380Ssam    return 1;
48185380Ssam  }
49185380Ssam
50185380Ssam  return 0;
51185380Ssam}
52185380Ssam
53185380Ssam/* Reads a module from the specified path, returning via the OutModule parameter
54185380Ssam   a module provider which performs lazy deserialization. Returns 0 on success.
55185380Ssam   Optionally returns a human-readable error message via OutMessage. */
56185380Ssamint LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
57185380Ssam                                 LLVMModuleProviderRef *OutMP,
58185380Ssam                                  char **OutMessage) {
59185380Ssam  std::string Message;
60185380Ssam
61185380Ssam  *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), getGlobalContext(),
62185380Ssam                                         &Message));
63185380Ssam
64185380Ssam  if (!*OutMP) {
65185380Ssam    if (OutMessage)
66185380Ssam      *OutMessage = strdup(Message.c_str());
67185380Ssam      return 1;
68185380Ssam  }
69185380Ssam
70185380Ssam  return 0;
71185380Ssam}
72185380Ssam
73185380Ssamint LLVMGetBitcodeModuleProviderInContext(LLVMMemoryBufferRef MemBuf,
74185380Ssam                                          LLVMContextRef ContextRef,
75185380Ssam                                          LLVMModuleProviderRef *OutMP,
76185380Ssam                                          char **OutMessage) {
77185380Ssam  std::string Message;
78185380Ssam
79185380Ssam  *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), *unwrap(ContextRef),
80185380Ssam                                         &Message));
81185380Ssam  if (!*OutMP) {
82185380Ssam    if (OutMessage)
83185380Ssam      *OutMessage = strdup(Message.c_str());
84185380Ssam    return 1;
85185380Ssam  }
86185380Ssam
87185380Ssam  return 0;
88185380Ssam}
89185380Ssam