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