LLVM API Documentation

ModuleProvider.h

Go to the documentation of this file.
00001 //===-- llvm/ModuleProvider.h - Interface for module providers --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provides an abstract interface for loading a module from some
00011 // place.  This interface allows incremental or random access loading of
00012 // functions from the file.  This is useful for applications like JIT compilers
00013 // or interprocedural optimizers that do not need the entire program in memory
00014 // at the same time.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #ifndef MODULEPROVIDER_H
00019 #define MODULEPROVIDER_H
00020 
00021 #include <string>
00022 
00023 namespace llvm {
00024 
00025 class Function;
00026 class Module;
00027 
00028 class ModuleProvider {
00029 protected:
00030   Module *TheModule;
00031   ModuleProvider();
00032 
00033 public:
00034   virtual ~ModuleProvider();
00035 
00036   /// getModule - returns the module this provider is encapsulating.
00037   ///
00038   Module* getModule() { return TheModule; }
00039 
00040   /// materializeFunction - make sure the given function is fully read.  If the
00041   /// module is corrupt, this returns true and fills in the optional string
00042   /// with information about the problem.  If successful, this returns false.
00043   ///
00044   virtual bool materializeFunction(Function *F, std::string *ErrInfo = 0) = 0;
00045 
00046   /// dematerializeFunction - If the given function is read in, and if the
00047   /// module provider supports it, release the memory for the function, and set
00048   /// it up to be materialized lazily.  If the provider doesn't support this
00049   /// capability, this method is a noop.
00050   ///
00051   virtual void dematerializeFunction(Function *) {}
00052   
00053   /// materializeModule - make sure the entire Module has been completely read.
00054   /// On error, return null and fill in the error string if specified.
00055   ///
00056   virtual Module* materializeModule(std::string *ErrInfo = 0) = 0;
00057 
00058   /// releaseModule - no longer delete the Module* when provider is destroyed.
00059   /// On error, return null and fill in the error string if specified.
00060   ///
00061   virtual Module* releaseModule(std::string *ErrInfo = 0) {
00062     // Since we're losing control of this Module, we must hand it back complete
00063     if (!materializeModule(ErrInfo))
00064       return 0;
00065     Module *tempM = TheModule;
00066     TheModule = 0;
00067     return tempM;
00068   }
00069 };
00070 
00071 
00072 /// ExistingModuleProvider - Allow conversion from a fully materialized Module
00073 /// into a ModuleProvider, allowing code that expects a ModuleProvider to work
00074 /// if we just have a Module.  Note that the ModuleProvider takes ownership of
00075 /// the Module specified.
00076 struct ExistingModuleProvider : public ModuleProvider {
00077   explicit ExistingModuleProvider(Module *M) {
00078     TheModule = M;
00079   }
00080   bool materializeFunction(Function *, std::string * = 0) {
00081     return false;
00082   }
00083   Module* materializeModule(std::string * = 0) { return TheModule; }
00084 };
00085 
00086 } // End llvm namespace
00087 
00088 #endif