LLVM API Documentation
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 #include "llvm/Support/DataTypes.h" 00026 00027 namespace llvm { 00028 00029 class GlobalVariable; 00030 class GlobalValueRefMap; // Used by ConstantVals.cpp 00031 class FunctionType; 00032 class SymbolTable; 00033 00034 template<> struct ilist_traits<Function> 00035 : public SymbolTableListTraits<Function, Module, Module> { 00036 // createSentinel is used to create a node that marks the end of the list. 00037 static Function *createSentinel(); 00038 static void destroySentinel(Function *F) { delete F; } 00039 static iplist<Function> &getList(Module *M); 00040 }; 00041 template<> struct ilist_traits<GlobalVariable> 00042 : public SymbolTableListTraits<GlobalVariable, Module, Module> { 00043 // createSentinel is used to create a node that marks the end of the list. 00044 static GlobalVariable *createSentinel(); 00045 static void destroySentinel(GlobalVariable *GV) { delete GV; } 00046 static iplist<GlobalVariable> &getList(Module *M); 00047 }; 00048 00049 class Module { 00050 public: 00051 typedef iplist<GlobalVariable> GlobalListType; 00052 typedef iplist<Function> FunctionListType; 00053 typedef SetVector<std::string> LibraryListType; 00054 00055 // Global Variable iterators. 00056 typedef GlobalListType::iterator global_iterator; 00057 typedef GlobalListType::const_iterator const_global_iterator; 00058 00059 // Function iterators. 00060 typedef FunctionListType::iterator iterator; 00061 typedef FunctionListType::const_iterator const_iterator; 00062 00063 // Library list iterators. 00064 typedef LibraryListType::const_iterator lib_iterator; 00065 00066 enum Endianness { AnyEndianness, LittleEndian, BigEndian }; 00067 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 }; 00068 00069 private: 00070 GlobalListType GlobalList; // The Global Variables in the module 00071 FunctionListType FunctionList; // The Functions in the module 00072 LibraryListType LibraryList; // The Libraries needed by the module 00073 std::string GlobalScopeAsm; // Inline Asm at global scope. 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 Endianness Endian; // Endianness assumed in the module 00078 PointerSize PtrSize; // Pointer size assumed in the module 00079 00080 friend class Constant; 00081 00082 public: 00083 Module(const std::string &ModuleID); 00084 ~Module(); 00085 00086 const std::string &getModuleIdentifier() const { return ModuleID; } 00087 void setModuleIdentifier(const std::string &ID) { ModuleID = ID; } 00088 00089 const std::string &getTargetTriple() const { return TargetTriple; } 00090 void setTargetTriple(const std::string &T) { TargetTriple = T; } 00091 00092 /// Target endian information... 00093 Endianness getEndianness() const { return Endian; } 00094 void setEndianness(Endianness E) { Endian = E; } 00095 00096 /// Target Pointer Size information... 00097 PointerSize getPointerSize() const { return PtrSize; } 00098 void setPointerSize(PointerSize PS) { PtrSize = PS; } 00099 00100 // Access to any module-scope inline asm blocks. 00101 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; } 00102 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; } 00103 00104 //===--------------------------------------------------------------------===// 00105 // Methods for easy access to the functions in the module. 00106 // 00107 00108 /// getOrInsertFunction - Look up the specified function in the module symbol 00109 /// table. If it does not exist, add a prototype for the function and return 00110 /// it. 00111 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T); 00112 00113 /// getOrInsertFunction - Look up the specified function in the module symbol 00114 /// table. If it does not exist, add a prototype for the function and return 00115 /// it. This version of the method takes a null terminated list of function 00116 /// arguments, which makes it easier for clients to use. 00117 Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...) 00118 END_WITH_NULL; 00119 00120 /// getFunction - Look up the specified function in the module symbol table. 00121 /// If it does not exist, return null. 00122 /// 00123 Function *getFunction(const std::string &Name, const FunctionType *Ty); 00124 00125 /// getMainFunction - This function looks up main efficiently. This is such a 00126 /// common case, that it is a method in Module. If main cannot be found, a 00127 /// null pointer is returned. 00128 /// 00129 Function *getMainFunction(); 00130 00131 /// getNamedFunction - Return the first function in the module with the 00132 /// specified name, of arbitrary type. This method returns null if a function 00133 /// with the specified name is not found. 00134 /// 00135 Function *getNamedFunction(const std::string &Name); 00136 00137 //===--------------------------------------------------------------------===// 00138 // Methods for easy access to the global variables in the module. 00139 // 00140 00141 /// getGlobalVariable - Look up the specified global variable in the module 00142 /// symbol table. If it does not exist, return null. The type argument 00143 /// should be the underlying type of the global, i.e., it should not have 00144 /// the top-level PointerType, which represents the address of the global. 00145 /// If AllowInternal is set to true, this function will return types that 00146 /// have InternalLinkage. By default, these types are not returned. 00147 /// 00148 GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty, 00149 bool AllowInternal = false); 00150 00151 /// getNamedGlobal - Return the first global variable in the module with the 00152 /// specified name, of arbitrary type. This method returns null if a global 00153 /// with the specified name is not found. 00154 /// 00155 GlobalVariable *getNamedGlobal(const std::string &Name); 00156 00157 00158 //===--------------------------------------------------------------------===// 00159 // Methods for easy access to the types in the module. 00160 // 00161 00162 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If 00163 /// there is already an entry for this name, true is returned and the symbol 00164 /// table is not modified. 00165 /// 00166 bool addTypeName(const std::string &Name, const Type *Ty); 00167 00168 /// getTypeName - If there is at least one entry in the symbol table for the 00169 /// specified type, return it. 00170 /// 00171 std::string getTypeName(const Type *Ty) const; 00172 00173 /// getTypeByName - Return the type with the specified name in this module, or 00174 /// null if there is none by that name. 00175 const Type *getTypeByName(const std::string &Name) const; 00176 00177 00178 //===--------------------------------------------------------------------===// 00179 // Methods for direct access to the globals list, functions list, and symbol 00180 // table. 00181 // 00182 00183 // Get the underlying elements of the Module. 00184 const GlobalListType &getGlobalList() const { return GlobalList; } 00185 GlobalListType &getGlobalList() { return GlobalList; } 00186 const FunctionListType &getFunctionList() const { return FunctionList; } 00187 FunctionListType &getFunctionList() { return FunctionList; } 00188 00189 /// getSymbolTable() - Get access to the symbol table for the module, where 00190 /// global variables and functions are identified. 00191 /// 00192 SymbolTable &getSymbolTable() { return *SymTab; } 00193 const SymbolTable &getSymbolTable() const { return *SymTab; } 00194 00195 00196 //===--------------------------------------------------------------------===// 00197 // Module iterator forwarding functions 00198 // 00199 // Globals list interface 00200 global_iterator global_begin() { return GlobalList.begin(); } 00201 const_global_iterator global_begin() const { return GlobalList.begin(); } 00202 global_iterator global_end () { return GlobalList.end(); } 00203 const_global_iterator global_end () const { return GlobalList.end(); } 00204 bool global_empty() const { return GlobalList.empty(); } 00205 00206 // FunctionList interface 00207 iterator begin() { return FunctionList.begin(); } 00208 const_iterator begin() const { return FunctionList.begin(); } 00209 iterator end () { return FunctionList.end(); } 00210 const_iterator end () const { return FunctionList.end(); } 00211 00212 size_t size() const { return FunctionList.size(); } 00213 bool empty() const { return FunctionList.empty(); } 00214 00215 //===--------------------------------------------------------------------===// 00216 // List of dependent library access functions 00217 00218 /// @brief Get a constant iterator to beginning of dependent library list. 00219 inline lib_iterator lib_begin() const { return LibraryList.begin(); } 00220 00221 /// @brief Get a constant iterator to end of dependent library list. 00222 inline lib_iterator lib_end() const { return LibraryList.end(); } 00223 00224 /// @brief Returns the number of items in the list of libraries. 00225 inline size_t lib_size() const { return LibraryList.size(); } 00226 00227 /// @brief Add a library to the list of dependent libraries 00228 inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); } 00229 00230 /// @brief Remove a library from the list of dependent libraries 00231 inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); } 00232 00233 /// @brief Get all the libraries 00234 inline const LibraryListType& getLibraries() const { return LibraryList; } 00235 00236 //===--------------------------------------------------------------------===// 00237 // Utility functions for printing and dumping Module objects 00238 00239 void print(std::ostream &OS) const { print(OS, 0); } 00240 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 00241 00242 void dump() const; 00243 00244 /// dropAllReferences() - This function causes all the subinstructions to "let 00245 /// go" of all references that they are maintaining. This allows one to 00246 /// 'delete' a whole class at a time, even though there may be circular 00247 /// references... first all references are dropped, and all use counts go to 00248 /// zero. Then everything is delete'd for real. Note that no operations are 00249 /// valid on an object that has "dropped all references", except operator 00250 /// delete. 00251 /// 00252 void dropAllReferences(); 00253 }; 00254 00255 inline std::ostream &operator<<(std::ostream &O, const Module &M) { 00256 M.print(O); 00257 return O; 00258 } 00259 00260 } // End llvm namespace 00261 00262 #endif