For PR950:
[oota-llvm.git] / include / llvm / ModuleProvider.h
1 //===-- llvm/ModuleProvider.h - Interface for module providers --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides an abstract interface for loading a module from some
11 // place.  This interface allows incremental or random access loading of
12 // functions from the file.  This is useful for applications like JIT compilers
13 // or interprocedural optimizers that do not need the entire program in memory
14 // at the same time.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef MODULEPROVIDER_H
19 #define MODULEPROVIDER_H
20
21 #include <string>
22
23 namespace llvm {
24
25 class Function;
26 class Module;
27
28 class ModuleProvider {
29 protected:
30   Module *TheModule;
31   ModuleProvider();
32
33 public:
34   virtual ~ModuleProvider();
35
36   /// getModule - returns the module this provider is encapsulating.
37   ///
38   Module* getModule() { return TheModule; }
39
40   /// materializeFunction - make sure the given function is fully read.  If the
41   /// module is corrupt, this returns true and fills in the optional string
42   /// with information about the problem.  If successful, this returns false.
43   ///
44   virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) = 0;
45
46   /// materializeModule - make sure the entire Module has been completely read.
47   /// On error, return null and fill in the error string if specified.
48   ///
49   virtual Module* materializeModule(std::string *ErrInfo = 0) = 0;
50
51   /// releaseModule - no longer delete the Module* when provider is destroyed.
52   /// On error, return null and fill in the error string if specified.
53   ///
54   virtual Module* releaseModule(std::string *ErrInfo = 0) {
55     // Since we're losing control of this Module, we must hand it back complete
56     if (!materializeModule(ErrInfo))
57       return 0;
58     Module *tempM = TheModule;
59     TheModule = 0;
60     return tempM;
61   }
62 };
63
64
65 /// ExistingModuleProvider - Allow conversion from a fully materialized Module
66 /// into a ModuleProvider, allowing code that expects a ModuleProvider to work
67 /// if we just have a Module.  Note that the ModuleProvider takes ownership of
68 /// the Module specified.
69 struct ExistingModuleProvider : public ModuleProvider {
70   ExistingModuleProvider(Module *M) {
71     TheModule = M;
72   }
73   bool materializeFunction(Function *F, std::string *ErrInfo = 0) {
74     return false;
75   }
76   Module* materializeModule(std::string *ErrInfo = 0) { return TheModule; }
77 };
78
79 } // End llvm namespace
80
81 #endif