6d8a9b30ae1fd8bfee9f0c9b7f6bdceb765a2743
[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/ADT/OwningPtr.h"
23 #include "llvm/Assembly/Parser.h"
24 #include "llvm/Bitcode/ReaderWriter.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/system_error.h"
28
29 namespace llvm {
30
31   /// If the given MemoryBuffer holds a bitcode image, return a Module for it
32   /// which does lazy deserialization of function bodies.  Otherwise, attempt to
33   /// parse it as LLVM Assembly and return a fully populated Module. This
34   /// function *always* takes ownership of the given MemoryBuffer.
35   inline Module *getLazyIRModule(MemoryBuffer *Buffer,
36                                  SMDiagnostic &Err,
37                                  LLVMContext &Context) {
38     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
39                   (const unsigned char *)Buffer->getBufferEnd())) {
40       std::string ErrMsg;
41       Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
42       if (M == 0) {
43         Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
44                            ErrMsg);
45         // ParseBitcodeFile does not take ownership of the Buffer in the
46         // case of an error.
47         delete Buffer;
48       }
49       return M;
50     }
51
52     return ParseAssembly(Buffer, 0, Err, Context);
53   }
54
55   /// If the given file holds a bitcode image, return a Module
56   /// for it which does lazy deserialization of function bodies.  Otherwise,
57   /// attempt to parse it as LLVM Assembly and return a fully populated
58   /// Module.
59   inline Module *getLazyIRFileModule(const std::string &Filename,
60                                      SMDiagnostic &Err,
61                                      LLVMContext &Context) {
62     OwningPtr<MemoryBuffer> File;
63     if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
64       Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
65                          "Could not open input file: " + ec.message());
66       return 0;
67     }
68
69     return getLazyIRModule(File.take(), Err, Context);
70   }
71
72   /// If the given MemoryBuffer holds a bitcode image, return a Module
73   /// for it.  Otherwise, attempt to parse it as LLVM Assembly and return
74   /// a Module for it. This function *always* takes ownership of the given
75   /// MemoryBuffer.
76   inline Module *ParseIR(MemoryBuffer *Buffer,
77                          SMDiagnostic &Err,
78                          LLVMContext &Context) {
79     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
80                   (const unsigned char *)Buffer->getBufferEnd())) {
81       std::string ErrMsg;
82       Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
83       if (M == 0)
84         Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
85                            ErrMsg);
86       // ParseBitcodeFile does not take ownership of the Buffer.
87       delete Buffer;
88       return M;
89     }
90
91     return ParseAssembly(Buffer, 0, Err, Context);
92   }
93
94   /// If the given file holds a bitcode image, return a Module for it.
95   /// Otherwise, attempt to parse it as LLVM Assembly and return a Module
96   /// for it.
97   inline Module *ParseIRFile(const std::string &Filename,
98                              SMDiagnostic &Err,
99                              LLVMContext &Context) {
100     OwningPtr<MemoryBuffer> File;
101     if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
102       Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
103                          "Could not open input file: " + ec.message());
104       return 0;
105     }
106
107     return ParseIR(File.take(), Err, Context);
108   }
109
110 }
111
112 #endif