The TargetData is not used for the isPowerOfTwo determination. It has never
[oota-llvm.git] / include / llvm / Support / IRReader.h
index e7780b05d534fec520a4462158672e034a568934..6d8a9b30ae1fd8bfee9f0c9b7f6bdceb765a2743 100644 (file)
 #ifndef LLVM_SUPPORT_IRREADER_H
 #define LLVM_SUPPORT_IRREADER_H
 
+#include "llvm/ADT/OwningPtr.h"
 #include "llvm/Assembly/Parser.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
-#include "llvm/ModuleProvider.h"
+#include "llvm/Support/system_error.h"
 
 namespace llvm {
 
-  /// If the given MemoryBuffer holds a bitcode image, return a ModuleProvider
-  /// for it which does lazy deserialization of function bodies.  Otherwise,
-  /// attempt to parse it as LLVM Assembly and return a fully populated
-  /// ModuleProvider. This function *always* takes ownership of the given
-  /// MemoryBuffer.
-  inline ModuleProvider *getIRModuleProvider(MemoryBuffer *Buffer,
-                                             SMDiagnostic &Err,
-                                             LLVMContext &Context) {
+  /// If the given MemoryBuffer holds a bitcode image, return a Module for it
+  /// which does lazy deserialization of function bodies.  Otherwise, attempt to
+  /// parse it as LLVM Assembly and return a fully populated Module. This
+  /// function *always* takes ownership of the given MemoryBuffer.
+  inline Module *getLazyIRModule(MemoryBuffer *Buffer,
+                                 SMDiagnostic &Err,
+                                 LLVMContext &Context) {
     if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
                   (const unsigned char *)Buffer->getBufferEnd())) {
       std::string ErrMsg;
-      ModuleProvider *MP = getBitcodeModuleProvider(Buffer, Context, &ErrMsg);
-      if (MP == 0) {
-        Err = SMDiagnostic(Buffer->getBufferIdentifier(), -1, -1, ErrMsg, "");
+      Module *M = getLazyBitcodeModule(Buffer, Context, &ErrMsg);
+      if (M == 0) {
+        Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
+                           ErrMsg);
         // ParseBitcodeFile does not take ownership of the Buffer in the
         // case of an error.
         delete Buffer;
       }
-      return MP;
+      return M;
     }
 
-    Module *M = ParseAssembly(Buffer, 0, Err, Context);
-    if (M == 0)
-      return 0;
-    return new ExistingModuleProvider(M);
+    return ParseAssembly(Buffer, 0, Err, Context);
   }
 
-  /// If the given file holds a bitcode image, return a ModuleProvider
+  /// If the given file holds a bitcode image, return a Module
   /// for it which does lazy deserialization of function bodies.  Otherwise,
   /// attempt to parse it as LLVM Assembly and return a fully populated
-  /// ModuleProvider.
-  inline ModuleProvider *getIRFileModuleProvider(const std::string &Filename,
-                                                 SMDiagnostic &Err,
-                                                 LLVMContext &Context) {
-    std::string ErrMsg;
-    MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
-    if (F == 0) {
-      Err = SMDiagnostic(Filename, -1, -1,
-                         "Could not open input file '" + Filename + "'", "");
+  /// Module.
+  inline Module *getLazyIRFileModule(const std::string &Filename,
+                                     SMDiagnostic &Err,
+                                     LLVMContext &Context) {
+    OwningPtr<MemoryBuffer> File;
+    if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
+      Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
+                         "Could not open input file: " + ec.message());
       return 0;
     }
 
-    return getIRModuleProvider(F, Err, Context);
+    return getLazyIRModule(File.take(), Err, Context);
   }
 
   /// If the given MemoryBuffer holds a bitcode image, return a Module
@@ -83,10 +80,11 @@ namespace llvm {
                   (const unsigned char *)Buffer->getBufferEnd())) {
       std::string ErrMsg;
       Module *M = ParseBitcodeFile(Buffer, Context, &ErrMsg);
+      if (M == 0)
+        Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
+                           ErrMsg);
       // ParseBitcodeFile does not take ownership of the Buffer.
       delete Buffer;
-      if (M == 0)
-        Err = SMDiagnostic(Buffer->getBufferIdentifier(), -1, -1, ErrMsg, "");
       return M;
     }
 
@@ -99,15 +97,14 @@ namespace llvm {
   inline Module *ParseIRFile(const std::string &Filename,
                              SMDiagnostic &Err,
                              LLVMContext &Context) {
-    std::string ErrMsg;
-    MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrMsg);
-    if (F == 0) {
-      Err = SMDiagnostic(Filename, -1, -1,
-                         "Could not open input file '" + Filename + "'", "");
+    OwningPtr<MemoryBuffer> File;
+    if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), File)) {
+      Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
+                         "Could not open input file: " + ec.message());
       return 0;
     }
 
-    return ParseIR(F, Err, Context);
+    return ParseIR(File.take(), Err, Context);
   }
 
 }