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,
36 const std::string &Filename,
38 LLVMContext &Context) {
39 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
40 (const unsigned char *)Buffer->getBufferEnd())) {
42 ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, &ErrMsg);
44 Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
45 // ParseBitcodeFile does not take ownership of the Buffer in the
52 Module *M = ParseAssembly(Buffer, Filename, 0, Err, Context);
55 return new ExistingModuleProvider(M);
58 /// If the given file holds a bitcode image, return a ModuleProvider
59 /// for it which does lazy deserialization of function bodies. Otherwise,
60 /// attempt to parse it as LLVM Assembly and return a fully populated
62 inline ModuleProvider *getIRFileModuleProvider(const std::string &Filename,
64 LLVMContext &Context) {
66 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
68 Err = SMDiagnostic(Filename, -1, -1,
69 "Could not open input file '" + Filename + "'", "");
73 return getIRModuleProvider(F, Filename, Err, Context);
76 /// If the given MemoryBuffer holds a bitcode image, return a Module
77 /// for it. Otherwise, attempt to parse it as LLVM Assembly and return
78 /// a Module for it. This function *always* takes ownership of the given
80 inline Module *ParseIR(MemoryBuffer *Buffer,
81 const std::string &Filename,
83 LLVMContext &Context) {
84 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
85 (const unsigned char *)Buffer->getBufferEnd())) {
87 Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
88 // ParseBitcodeFile does not take ownership of the Buffer.
91 Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
95 return ParseAssembly(Buffer, Filename, 0, Err, Context);
98 /// If the given file holds a bitcode image, return a Module for it.
99 /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
101 inline Module *ParseIRFile(const std::string &Filename,
103 LLVMContext &Context) {
105 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
107 Err = SMDiagnostic(Filename, -1, -1,
108 "Could not open input file '" + Filename + "'", "");
112 return ParseIR(F, Filename, Err, Context);