blob: 9b3acb5ca0a9042d7bb224351214ec9cc3752c15 [file] [log] [blame]
Gordon Henriksen2b0eed22007-12-11 00:20:48 +00001//===-- BitReader.cpp -----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen2b0eed22007-12-11 00:20:48 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm-c/BitReader.h"
11#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/LLVMContext.h"
Filip Pizlodec20e42013-05-01 20:59:00 +000013#include "llvm/IR/Module.h"
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000014#include "llvm/Support/MemoryBuffer.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000015#include <cstring>
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include <string>
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000017
18using namespace llvm;
19
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000020/* 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.
Chris Lattner25963c62010-01-09 22:27:07 +000022 Optionally returns a human-readable error message via OutMessage. */
23LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
24 LLVMModuleRef *OutModule, char **OutMessage) {
Daniel Dunbar754946c2010-02-15 21:08:22 +000025 return LLVMParseBitcodeInContext(wrap(&getGlobalContext()), MemBuf, OutModule,
26 OutMessage);
Owen Anderson31d44e42009-07-02 07:17:57 +000027}
28
Chris Lattner25963c62010-01-09 22:27:07 +000029LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
30 LLVMMemoryBufferRef MemBuf,
31 LLVMModuleRef *OutModule,
32 char **OutMessage) {
Rafael Espindola8f31e212014-01-15 01:08:23 +000033 ErrorOr<Module *> ModuleOrErr =
Rafael Espindolad96d5532014-08-26 21:49:01 +000034 parseBitcodeFile(unwrap(MemBuf)->getMemBufferRef(), *unwrap(ContextRef));
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000035 if (std::error_code EC = ModuleOrErr.getError()) {
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000036 if (OutMessage)
Rafael Espindola8f31e212014-01-15 01:08:23 +000037 *OutMessage = strdup(EC.message().c_str());
Craig Topper2617dcc2014-04-15 06:32:26 +000038 *OutModule = wrap((Module*)nullptr);
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000039 return 1;
40 }
Joe Abbey2ad8df22012-11-25 15:23:39 +000041
Rafael Espindola8f31e212014-01-15 01:08:23 +000042 *OutModule = wrap(ModuleOrErr.get());
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000043 return 0;
44}
45
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000046/* Reads a module from the specified path, returning via the OutModule parameter
47 a module provider which performs lazy deserialization. Returns 0 on success.
Joe Abbey2ad8df22012-11-25 15:23:39 +000048 Optionally returns a human-readable error message via OutMessage. */
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +000049LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
50 LLVMMemoryBufferRef MemBuf,
51 LLVMModuleRef *OutM,
52 char **OutMessage) {
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000053 std::string Message;
Rafael Espindolae2c1d772014-08-26 22:00:09 +000054 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
55
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000056 ErrorOr<Module *> ModuleOrErr =
Rafael Espindola68812152014-09-03 17:31:46 +000057 getLazyBitcodeModule(std::move(Owner), *unwrap(ContextRef));
Rafael Espindolae2c1d772014-08-26 22:00:09 +000058 Owner.release();
Joe Abbey2ad8df22012-11-25 15:23:39 +000059
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000060 if (std::error_code EC = ModuleOrErr.getError()) {
Craig Topper2617dcc2014-04-15 06:32:26 +000061 *OutM = wrap((Module *)nullptr);
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000062 if (OutMessage)
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000063 *OutMessage = strdup(EC.message().c_str());
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000064 return 1;
65 }
Joe Abbey2ad8df22012-11-25 15:23:39 +000066
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000067 *OutM = wrap(ModuleOrErr.get());
68
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000069 return 0;
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +000070
71}
72
73LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
74 char **OutMessage) {
75 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
76 OutMessage);
77}
78
79/* Deprecated: Use LLVMGetBitcodeModuleInContext instead. */
80LLVMBool LLVMGetBitcodeModuleProviderInContext(LLVMContextRef ContextRef,
81 LLVMMemoryBufferRef MemBuf,
82 LLVMModuleProviderRef *OutMP,
83 char **OutMessage) {
84 return LLVMGetBitcodeModuleInContext(ContextRef, MemBuf,
85 reinterpret_cast<LLVMModuleRef*>(OutMP),
86 OutMessage);
87}
88
89/* Deprecated: Use LLVMGetBitcodeModule instead. */
90LLVMBool LLVMGetBitcodeModuleProvider(LLVMMemoryBufferRef MemBuf,
91 LLVMModuleProviderRef *OutMP,
92 char **OutMessage) {
93 return LLVMGetBitcodeModuleProviderInContext(LLVMGetGlobalContext(), MemBuf,
94 OutMP, OutMessage);
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000095}