1 //===---- llvm/Support/IRReader.h - Reader for LLVM IR files ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines functions for reading LLVM IR. They support both
11 // Bitcode and Assembly, automatically detecting the input format.
13 // These functions must be defined in a header file in order to avoid
14 // library dependencies, since they reference both Bitcode and Assembly
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_SUPPORT_IRREADER_H
20 #define LLVM_SUPPORT_IRREADER_H
22 #include "llvm/Assembly/Parser.h"
23 #include "llvm/Bitcode/ReaderWriter.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/ModuleProvider.h"
30 /// If the given MemoryBuffer holds a bitcode image, return a ModuleProvider
31 /// for it which does lazy deserialization of function bodies. Otherwise,
32 /// attempt to parse it as LLVM Assembly and return a fully populated
33 /// ModuleProvider. This function *always* takes ownership of the given
35 inline ModuleProvider *getIRModuleProvider(MemoryBuffer *Buffer,
37 LLVMContext &Context) {
38 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
39 (const unsigned char *)Buffer->getBufferEnd())) {
41 ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, &ErrMsg);
43 Err = SMDiagnostic(Buffer->getBufferIdentifier(), -1, -1, ErrMsg, "");
44 // ParseBitcodeFile does not take ownership of the Buffer in the
51 Module *M = ParseAssembly(Buffer, 0, Err, Context);
54 return new ExistingModuleProvider(M);
57 /// If the given file holds a bitcode image, return a ModuleProvider
58 /// for it which does lazy deserialization of function bodies. Otherwise,
59 /// attempt to parse it as LLVM Assembly and return a fully populated
61 inline ModuleProvider *getIRFileModuleProvider(const std::string &Filename,
63 LLVMContext &Context) {
65 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
67 Err = SMDiagnostic(Filename, -1, -1,
68 "Could not open input file '" + Filename + "'", "");
72 return getIRModuleProvider(F, Err, Context);
75 /// If the given MemoryBuffer holds a bitcode image, return a Module
76 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
77 /// a Module for it. This function *always* takes ownership of the given
79 inline Module *ParseIR(MemoryBuffer *Buffer,
81 LLVMContext &Context) {
82 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
83 (const unsigned char *)Buffer->getBufferEnd())) {
85 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
86 // ParseBitcodeFile does not take ownership of the Buffer.
89 Err = SMDiagnostic(Buffer->getBufferIdentifier(), -1, -1, ErrMsg, "");
93 return ParseAssembly(Buffer, 0, Err, Context);
96 /// If the given file holds a bitcode image, return a Module for it.
97 /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
99 inline Module *ParseIRFile(const std::string &Filename,
101 LLVMContext &Context) {
103 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
105 Err = SMDiagnostic(Filename, -1, -1,
106 "Could not open input file '" + Filename + "'", "");
110 return ParseIR(F, Err, Context);