blob: c7c38690c5a050939060ce19ed2a35b874f73c98 [file] [log] [blame]
Gordon Henriksenbbc65972007-12-11 00:20:48 +00001//===-- BitReader.cpp -----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Gordon Henriksen and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm-c/BitReader.h"
11#include "llvm/Bitcode/ReaderWriter.h"
12#include "llvm/Support/MemoryBuffer.h"
13#include <string>
14
15using namespace llvm;
16
Gordon Henriksenda1435f2007-12-19 22:30:40 +000017/* Builds a module from the bitcode in the specified memory buffer, returning a
18 reference to the module via the OutModule parameter. Returns 0 on success.
19 Optionally returns a human-readable error message via OutMessage. */
20int LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
21 LLVMModuleRef *OutModule, char **OutMessage) {
Gordon Henriksenbbc65972007-12-11 00:20:48 +000022 std::string Message;
23
Gordon Henriksenda1435f2007-12-19 22:30:40 +000024 *OutModule = wrap(ParseBitcodeFile(unwrap(MemBuf), &Message));
Gordon Henriksenbbc65972007-12-11 00:20:48 +000025 if (!*OutModule) {
26 if (OutMessage)
27 *OutMessage = strdup(Message.c_str());
28 return 1;
29 }
30
31 return 0;
32}
33
Gordon Henriksenda1435f2007-12-19 22:30:40 +000034/* Reads a module from the specified path, returning via the OutModule parameter
35 a module provider which performs lazy deserialization. Returns 0 on success.
36 Optionally returns a human-readable error message via OutMessage. */
37int LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
38 LLVMModuleProviderRef *OutMP,
39 char **OutMessage) {
40 std::string Message;
41
42 *OutMP = wrap(getBitcodeModuleProvider(unwrap(MemBuf), &Message));
43 if (!*OutMP) {
44 if (OutMessage)
45 *OutMessage = strdup(Message.c_str());
46 return 1;
47 }
48
49 return 0;
Gordon Henriksenbbc65972007-12-11 00:20:48 +000050}