Give llvm::SourceMgr the ability to have a client-specified
[oota-llvm.git] / include / llvm / Support / IRReader.h
1 //===---- llvm/Support/IRReader.h - Reader for LLVM IR files ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines functions for reading LLVM IR. They support both
11 // Bitcode and Assembly, automatically detecting the input format.
12 //
13 // These functions must be defined in a header file in order to avoid
14 // library dependencies, since they reference both Bitcode and Assembly
15 // functions.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_SUPPORT_IRREADER_H
20 #define LLVM_SUPPORT_IRREADER_H
21
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
27 namespace llvm {
28
29   /// If the given MemoryBuffer holds a bitcode image, return a Module for it
30   /// which does lazy deserialization of function bodies.  Otherwise, attempt to
31   /// parse it as LLVM Assembly and return a fully populated Module. This
32   /// function *always* takes ownership of the given MemoryBuffer.
33   inline Module *getLazyIRModule(MemoryBuffer *Buffer,
34                                  SMDiagnostic &Err,
35                                  LLVMContext &Context) {
36     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
37                   (const unsigned char *)Buffer->getBufferEnd())) {
38       std::string ErrMsg;
39       Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
40       if (M == 0) {
41         Err = SMDiagnostic(SMLoc(), Buffer->getBufferIdentifier(), -1, -1,
42                            ErrMsg, "");
43         // ParseBitcodeFile does not take ownership of the Buffer in the
44         // case of an error.
45         delete Buffer;
46       }
47       return M;
48     }
49
50     return ParseAssembly(Buffer, 0, Err, Context);
51   }
52
53   /// If the given file holds a bitcode image, return a Module
54   /// for it which does lazy deserialization of function bodies.  Otherwise,
55   /// attempt to parse it as LLVM Assembly and return a fully populated
56   /// Module.
57   inline Module *getLazyIRFileModule(const std::string &Filename,
58                                      SMDiagnostic &Err,
59                                      LLVMContext &Context) {
60     std::string ErrMsg;
61     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
62     if (F == 0) {
63       Err = SMDiagnostic(SMLoc(), Filename, -1, -1,
64                          "Could not open input file '" + Filename + "'", "");
65       return 0;
66     }
67
68     return getLazyIRModule(F, Err, Context);
69   }
70
71   /// If the given MemoryBuffer holds a bitcode image, return a Module
72   /// for it.  Otherwise, attempt to parse it as LLVM Assembly and return
73   /// a Module for it. This function *always* takes ownership of the given
74   /// MemoryBuffer.
75   inline Module *ParseIR(MemoryBuffer *Buffer,
76                          SMDiagnostic &Err,
77                          LLVMContext &Context) {
78     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
79                   (const unsigned char *)Buffer->getBufferEnd())) {
80       std::string ErrMsg;
81       Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
82       // ParseBitcodeFile does not take ownership of the Buffer.
83       delete Buffer;
84       if (M == 0)
85         Err = SMDiagnostic(SMLoc(), Buffer->getBufferIdentifier(),
86                            -1, -1, ErrMsg, "");
87       return M;
88     }
89
90     return ParseAssembly(Buffer, 0, Err, Context);
91   }
92
93   /// If the given file holds a bitcode image, return a Module for it.
94   /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
95   /// for it.
96   inline Module *ParseIRFile(const std::string &Filename,
97                              SMDiagnostic &Err,
98                              LLVMContext &Context) {
99     std::string ErrMsg;
100     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
101     if (F == 0) {
102       Err = SMDiagnostic(SMLoc(), Filename, -1, -1,
103                          "Could not open input file '" + Filename + "'", "");
104       return 0;
105     }
106
107     return ParseIR(F, Err, Context);
108   }
109
110 }
111
112 #endif