ensure that every error return sets a message (and goes through Error, for
[oota-llvm.git] / lib / Bitcode / Reader / ReaderWrappers.cpp
1 //===- ReaderWrappers.cpp - Parse bitcode from file or buffer -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements loading and parsing a bitcode file and parsing a
11 // module from a memory buffer.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "BitcodeReader.h"
17 #include "llvm/System/MappedFile.h"
18 using namespace llvm;
19
20 //===----------------------------------------------------------------------===//
21 // BitcodeFileReader - Read from an mmap'able file descriptor.
22
23 namespace {
24   /// BitcodeFileReader - parses bitcode from a file.
25   ///
26   class BitcodeFileReader : public BitcodeReader {
27   private:
28     std::string Filename;
29     sys::MappedFile File;
30     
31     BitcodeFileReader(const BitcodeFileReader&); // DO NOT IMPLEMENT
32     void operator=(const BitcodeFileReader&); // DO NOT IMPLEMENT
33   public:
34     BitcodeFileReader(const std::string &FN) : Filename(FN) {}
35     bool Read(std::string *ErrMsg);
36     
37     void FreeState() {
38       BitcodeReader::FreeState();
39       File.close();
40     }
41   };
42 }
43
44 bool BitcodeFileReader::Read(std::string *ErrMsg) {
45   if (File.open(sys::Path(Filename), sys::MappedFile::READ_ACCESS, ErrMsg))
46     return true;
47   if (!File.map(ErrMsg)) {
48     File.close();
49     return true;
50   }
51   unsigned char *Buffer = reinterpret_cast<unsigned char*>(File.base());
52   if (!ParseBitcode(Buffer, File.size(), Filename))
53     return false;
54   assert(getErrorString() && "Didn't set an error string?");
55   if (ErrMsg) *ErrMsg = getErrorString();
56   return true;
57 }
58
59
60
61 //===----------------------------------------------------------------------===//
62 // External interface
63 //===----------------------------------------------------------------------===//
64
65 /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file.
66 ///
67 ModuleProvider *llvm::getBitcodeModuleProvider(const std::string &Filename,
68                                                std::string *ErrMsg) {
69   if (Filename != std::string("-")) {
70     BitcodeFileReader *R = new BitcodeFileReader(Filename);
71     if (R->Read(ErrMsg)) {
72       delete R;
73       return 0;
74     }
75     return R;
76   }
77   
78   assert(0 && "FIXME: stdin reading unimp!");
79 #if 0
80   // Read from stdin
81   BytecodeStdinReader *R = new BytecodeStdinReader();
82   if (R->Read(ErrMsg)) {
83     delete R;
84     return 0;
85   }
86   return R;
87 #endif
88 }
89
90 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
91 /// If an error occurs, return null and fill in *ErrMsg if non-null.
92 Module *llvm::ParseBitcodeFile(const std::string &Filename,std::string *ErrMsg){
93   ModuleProvider *MP = getBitcodeModuleProvider(Filename, ErrMsg);
94   if (!MP) return 0;
95   Module *M = MP->releaseModule(ErrMsg);
96   delete MP;
97   return M;
98 }