Add convenience functions for reading in LLVM IR that autodetect
[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 #include "llvm/ModuleProvider.h"
27
28 namespace llvm {
29
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
34   /// MemoryBuffer.
35   inline ModuleProvider *getIRModuleProvider(MemoryBuffer *Buffer,
36                                              const std::string &Filename,
37                                              SMDiagnostic &Err,
38                                              LLVMContext &Context) {
39     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
40                   (const unsigned char *)Buffer->getBufferEnd())) {
41       std::string ErrMsg;
42       ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, &ErrMsg);
43       if (MP == 0) {
44         Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
45         // ParseBitcodeFile does not take ownership of the Buffer in the
46         // case of an error.
47         delete Buffer;
48       }
49       return MP;
50     }
51
52     Module *M = ParseAssembly(Buffer, Filename, 0, Err, Context);
53     if (M == 0)
54       return 0;
55     return new ExistingModuleProvider(M);
56   }
57
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
61   /// ModuleProvider.
62   inline ModuleProvider *getIRFileModuleProvider(const std::string &Filename,
63                                                  SMDiagnostic &Err,
64                                                  LLVMContext &Context) {
65     std::string ErrMsg;
66     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
67     if (F == 0) {
68       Err = SMDiagnostic(Filename, -1, -1,
69                          "Could not open input file '" + Filename + "'", "");
70       return 0;
71     }
72
73     return getIRModuleProvider(F, Filename, Err, Context);
74   }
75
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
79   /// MemoryBuffer.
80   inline Module *ParseIR(MemoryBuffer *Buffer,
81                          const std::string &Filename,
82                          SMDiagnostic &Err,
83                          LLVMContext &Context) {
84     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
85                   (const unsigned char *)Buffer->getBufferEnd())) {
86       std::string ErrMsg;
87       Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
88       // ParseBitcodeFile does not take ownership of the Buffer.
89       delete Buffer;
90       if (M == 0)
91         Err = SMDiagnostic(Filename, -1, -1, ErrMsg, "");
92       return M;
93     }
94
95     return ParseAssembly(Buffer, Filename, 0, Err, Context);
96   }
97
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
100   /// for it.
101   inline Module *ParseIRFile(const std::string &Filename,
102                              SMDiagnostic &Err,
103                              LLVMContext &Context) {
104     std::string ErrMsg;
105     MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
106     if (F == 0) {
107       Err = SMDiagnostic(Filename, -1, -1,
108                          "Could not open input file '" + Filename + "'", "");
109       return 0;
110     }
111
112     return ParseIR(F, Filename, Err, Context);
113   }
114
115 }
116
117 #endif