LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Module.h

Go to the documentation of this file.
00001 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declarations for the Module class that is used to 
00011 // maintain all the information related to a VM module.
00012 //
00013 // A module also maintains a GlobalValRefMap object that is used to hold all
00014 // constant references to global variables in the module.  When a global
00015 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_MODULE_H
00020 #define LLVM_MODULE_H
00021 
00022 #include "llvm/Function.h"
00023 #include "llvm/GlobalVariable.h"
00024 #include "llvm/ADT/SetVector.h"
00025 
00026 namespace llvm {
00027 
00028 class GlobalVariable;
00029 class GlobalValueRefMap;   // Used by ConstantVals.cpp
00030 class FunctionType;
00031 class SymbolTable;
00032 
00033 template<> struct ilist_traits<Function>
00034   : public SymbolTableListTraits<Function, Module, Module> {
00035   // createNode is used to create a node that marks the end of the list...
00036   static Function *createNode();
00037   static iplist<Function> &getList(Module *M);
00038 };
00039 template<> struct ilist_traits<GlobalVariable>
00040   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
00041   // createNode is used to create a node that marks the end of the list...
00042   static GlobalVariable *createNode();
00043   static iplist<GlobalVariable> &getList(Module *M);
00044 };
00045 
00046 class Module {
00047 public:
00048   typedef iplist<GlobalVariable> GlobalListType;
00049   typedef iplist<Function> FunctionListType;
00050   typedef SetVector<std::string> LibraryListType;
00051 
00052   // Global Variable iterators...
00053   typedef GlobalListType::iterator                             giterator;
00054   typedef GlobalListType::const_iterator                 const_giterator;
00055   typedef std::reverse_iterator<giterator>             reverse_giterator;
00056   typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
00057 
00058   // Function iterators...
00059   typedef FunctionListType::iterator                          iterator;
00060   typedef FunctionListType::const_iterator              const_iterator;
00061   typedef std::reverse_iterator<iterator>             reverse_iterator;
00062   typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00063 
00064   // Library list iterators
00065   typedef LibraryListType::const_iterator lib_iterator;
00066 
00067   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
00068   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
00069 
00070 private:
00071   GlobalListType GlobalList;     // The Global Variables in the module
00072   FunctionListType FunctionList; // The Functions in the module
00073   LibraryListType LibraryList;   // The Libraries needed by the module
00074   SymbolTable *SymTab;           // Symbol Table for the module
00075   std::string ModuleID;          // Human readable identifier for the module
00076   std::string TargetTriple;      // Platform target triple Module compiled on
00077 
00078   // These flags are probably not the right long-term way to handle this kind of
00079   // target information, but it is sufficient for now.
00080   Endianness  Endian;     // True if target is little endian
00081   PointerSize PtrSize;    // True if target has 32-bit pointers (false = 64-bit)
00082 
00083   friend class Constant;
00084 
00085 public:
00086   Module(const std::string &ModuleID);
00087   ~Module();
00088 
00089   const std::string& getModuleIdentifier() const { return ModuleID; }
00090   const std::string& getTargetTriple() const { return TargetTriple; }
00091   void setTargetTriple(const std::string& T) { TargetTriple = T; }
00092 
00093   /// Target endian information...
00094   Endianness getEndianness() const { return Endian; }
00095   void setEndianness(Endianness E) { Endian = E; }
00096 
00097   /// Target Pointer Size information...
00098   PointerSize getPointerSize() const { return PtrSize; }
00099   void setPointerSize(PointerSize PS) { PtrSize = PS; }
00100 
00101   //===--------------------------------------------------------------------===//
00102   // Methods for easy access to the functions in the module.
00103   //
00104 
00105   /// getOrInsertFunction - Look up the specified function in the module symbol
00106   /// table.  If it does not exist, add a prototype for the function and return
00107   /// it.
00108   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
00109 
00110   /// getOrInsertFunction - Look up the specified function in the module symbol
00111   /// table.  If it does not exist, add a prototype for the function and return
00112   /// it.  This version of the method takes a null terminated list of function
00113   /// arguments, which makes it easier for clients to use.
00114   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
00115 
00116   /// getFunction - Look up the specified function in the module symbol table.
00117   /// If it does not exist, return null.
00118   ///
00119   Function *getFunction(const std::string &Name, const FunctionType *Ty);
00120 
00121   /// getMainFunction - This function looks up main efficiently.  This is such a
00122   /// common case, that it is a method in Module.  If main cannot be found, a
00123   /// null pointer is returned.
00124   ///
00125   Function *getMainFunction();
00126 
00127   /// getNamedFunction - Return the first function in the module with the
00128   /// specified name, of arbitrary type.  This method returns null if a function
00129   /// with the specified name is not found.
00130   ///
00131   Function *getNamedFunction(const std::string &Name);
00132 
00133   //===--------------------------------------------------------------------===//
00134   // Methods for easy access to the global variables in the module.
00135   //
00136 
00137   /// getGlobalVariable - Look up the specified global variable in the module
00138   /// symbol table.  If it does not exist, return null.  Note that this only
00139   /// returns a global variable if it does not have internal linkage.  The type
00140   /// argument should be the underlying type of the global, i.e., it should not
00141   /// have the top-level PointerType, which represents the address of the
00142   /// global.
00143   ///
00144   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
00145 
00146 
00147   //===--------------------------------------------------------------------===//
00148   // Methods for easy access to the types in the module.
00149   //
00150 
00151   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
00152   /// there is already an entry for this name, true is returned and the symbol
00153   /// table is not modified.
00154   ///
00155   bool addTypeName(const std::string &Name, const Type *Ty);
00156 
00157   /// getTypeName - If there is at least one entry in the symbol table for the
00158   /// specified type, return it.
00159   ///
00160   std::string getTypeName(const Type *Ty) const;
00161 
00162   /// getTypeByName - Return the type with the specified name in this module, or
00163   /// null if there is none by that name.
00164   const Type *getTypeByName(const std::string &Name) const;
00165 
00166 
00167   //===--------------------------------------------------------------------===//
00168   // Methods for direct access to the globals list, functions list, and symbol
00169   // table.
00170   //
00171 
00172   /// Get the underlying elements of the Module...
00173   inline const GlobalListType &getGlobalList() const  { return GlobalList; }
00174   inline       GlobalListType &getGlobalList()        { return GlobalList; }
00175   inline const FunctionListType &getFunctionList() const { return FunctionList;}
00176   inline       FunctionListType &getFunctionList()       { return FunctionList;}
00177 
00178   /// getSymbolTable() - Get access to the symbol table for the module, where
00179   /// global variables and functions are identified.
00180   ///
00181   inline       SymbolTable &getSymbolTable()       { return *SymTab; }
00182   inline const SymbolTable &getSymbolTable() const { return *SymTab; }
00183 
00184 
00185   //===--------------------------------------------------------------------===//
00186   // Module iterator forwarding functions
00187   //
00188   // Globals list interface
00189   inline giterator                gbegin()       { return GlobalList.begin(); }
00190   inline const_giterator          gbegin() const { return GlobalList.begin(); }
00191   inline giterator                gend  ()       { return GlobalList.end();   }
00192   inline const_giterator          gend  () const { return GlobalList.end();   }
00193 
00194   inline reverse_giterator       grbegin()       { return GlobalList.rbegin(); }
00195   inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
00196   inline reverse_giterator       grend  ()       { return GlobalList.rend();   }
00197   inline const_reverse_giterator grend  () const { return GlobalList.rend();   }
00198 
00199   inline size_t                    gsize() const { return GlobalList.size(); }
00200   inline bool                     gempty() const { return GlobalList.empty(); }
00201   inline const GlobalVariable    &gfront() const { return GlobalList.front(); }
00202   inline       GlobalVariable    &gfront()       { return GlobalList.front(); }
00203   inline const GlobalVariable     &gback() const { return GlobalList.back(); }
00204   inline       GlobalVariable     &gback()       { return GlobalList.back(); }
00205 
00206   // FunctionList interface
00207   inline iterator                begin()       { return FunctionList.begin(); }
00208   inline const_iterator          begin() const { return FunctionList.begin(); }
00209   inline iterator                end  ()       { return FunctionList.end();   }
00210   inline const_iterator          end  () const { return FunctionList.end();   }
00211 
00212   inline reverse_iterator       rbegin()       { return FunctionList.rbegin(); }
00213   inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
00214   inline reverse_iterator       rend  ()       { return FunctionList.rend();   }
00215   inline const_reverse_iterator rend  () const { return FunctionList.rend();   }
00216 
00217   inline size_t                   size() const { return FunctionList.size(); }
00218   inline bool                    empty() const { return FunctionList.empty(); }
00219   inline const Function         &front() const { return FunctionList.front(); }
00220   inline       Function         &front()       { return FunctionList.front(); }
00221   inline const Function          &back() const { return FunctionList.back(); }
00222   inline       Function          &back()       { return FunctionList.back(); }
00223 
00224   //===--------------------------------------------------------------------===//
00225   // List of dependent library access functions
00226 
00227   /// @brief Get a constant iterator to beginning of dependent library list.
00228   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
00229 
00230   /// @brief Get a constant iterator to end of dependent library list.
00231   inline lib_iterator lib_end() const { return LibraryList.end(); }
00232 
00233   /// @brief Returns the number of items in the list of libraries.
00234   inline size_t lib_size() const { return LibraryList.size(); }
00235 
00236   /// @brief Add a library to the list of dependent libraries
00237   inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
00238 
00239   /// @brief Remove a library from the list of dependent libraries
00240   inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
00241 
00242   /// @brief Get all the libraries 
00243   inline const LibraryListType& getLibraries() const { return LibraryList; }
00244 
00245   //===--------------------------------------------------------------------===//
00246   // Utility functions for printing and dumping Module objects
00247 
00248   void print(std::ostream &OS) const { print(OS, 0); }
00249   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
00250 
00251   void dump() const;
00252 
00253   /// dropAllReferences() - This function causes all the subinstructions to "let
00254   /// go" of all references that they are maintaining.  This allows one to
00255   /// 'delete' a whole class at a time, even though there may be circular
00256   /// references... first all references are dropped, and all use counts go to
00257   /// zero.  Then everything is delete'd for real.  Note that no operations are
00258   /// valid on an object that has "dropped all references", except operator 
00259   /// delete.
00260   ///
00261   void dropAllReferences();
00262 };
00263 
00264 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
00265   M->print(O);
00266   return O;
00267 }
00268 
00269 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
00270   M.print(O);
00271   return O;
00272 }
00273 
00274 } // End llvm namespace
00275 
00276 #endif