1 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declarations for the Module class that is used to
11 // maintain all the information related to a VM module.
13 // A module also maintains a GlobalValRefMap object that is used to hold all
14 // constant references to global variables in the module. When a global
15 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
17 //===----------------------------------------------------------------------===//
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/ADT/SetVector.h"
29 class GlobalValueRefMap; // Used by ConstantVals.cpp
30 class ConstantPointerRef;
34 template<> struct ilist_traits<Function>
35 : public SymbolTableListTraits<Function, Module, Module> {
36 // createNode is used to create a node that marks the end of the list...
37 static Function *createNode();
38 static iplist<Function> &getList(Module *M);
40 template<> struct ilist_traits<GlobalVariable>
41 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
42 // createNode is used to create a node that marks the end of the list...
43 static GlobalVariable *createNode();
44 static iplist<GlobalVariable> &getList(Module *M);
49 typedef iplist<GlobalVariable> GlobalListType;
50 typedef iplist<Function> FunctionListType;
51 typedef SetVector<std::string> LibraryListType;
53 // Global Variable iterators...
54 typedef GlobalListType::iterator giterator;
55 typedef GlobalListType::const_iterator const_giterator;
56 typedef std::reverse_iterator<giterator> reverse_giterator;
57 typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
59 // Function iterators...
60 typedef FunctionListType::iterator iterator;
61 typedef FunctionListType::const_iterator const_iterator;
62 typedef std::reverse_iterator<iterator> reverse_iterator;
63 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
65 // Library list iterators
66 typedef LibraryListType::const_iterator lib_iterator;
68 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
69 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
72 GlobalListType GlobalList; // The Global Variables in the module
73 FunctionListType FunctionList; // The Functions in the module
74 LibraryListType LibraryList; // The Libraries needed by the module
75 SymbolTable *SymTab; // Symbol Table for the module
76 std::string ModuleID; // Human readable identifier for the module
77 std::string TargetTriple; // Platform target triple Module compiled on
79 // These flags are probably not the right long-term way to handle this kind of
80 // target information, but it is sufficient for now.
81 Endianness Endian; // True if target is little endian
82 PointerSize PtrSize; // True if target has 32-bit pointers (false = 64-bit)
84 // Accessor for the underlying GVRefMap... only through the Constant class...
85 friend class Constant;
86 friend class ConstantPointerRef;
87 ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
88 void destroyConstantPointerRef(ConstantPointerRef *CPR);
91 Module(const std::string &ModuleID);
94 const std::string& getModuleIdentifier() const { return ModuleID; }
95 const std::string& getTargetTriple() const { return TargetTriple; }
96 void setTargetTriple(const std::string& T) { TargetTriple = T; }
98 /// Target endian information...
99 Endianness getEndianness() const { return Endian; }
100 void setEndianness(Endianness E) { Endian = E; }
102 /// Target Pointer Size information...
103 PointerSize getPointerSize() const { return PtrSize; }
104 void setPointerSize(PointerSize PS) { PtrSize = PS; }
106 //===--------------------------------------------------------------------===//
107 // Methods for easy access to the functions in the module.
110 /// getOrInsertFunction - Look up the specified function in the module symbol
111 /// table. If it does not exist, add a prototype for the function and return
113 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
115 /// getOrInsertFunction - Look up the specified function in the module symbol
116 /// table. If it does not exist, add a prototype for the function and return
117 /// it. This version of the method takes a null terminated list of function
118 /// arguments, which makes it easier for clients to use.
119 Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
121 /// getFunction - Look up the specified function in the module symbol table.
122 /// If it does not exist, return null.
124 Function *getFunction(const std::string &Name, const FunctionType *Ty);
126 /// getMainFunction - This function looks up main efficiently. This is such a
127 /// common case, that it is a method in Module. If main cannot be found, a
128 /// null pointer is returned.
130 Function *getMainFunction();
132 /// getNamedFunction - Return the first function in the module with the
133 /// specified name, of arbitrary type. This method returns null if a function
134 /// with the specified name is not found.
136 Function *getNamedFunction(const std::string &Name);
138 //===--------------------------------------------------------------------===//
139 // Methods for easy access to the global variables in the module.
142 /// getGlobalVariable - Look up the specified global variable in the module
143 /// symbol table. If it does not exist, return null. Note that this only
144 /// returns a global variable if it does not have internal linkage. The type
145 /// argument should be the underlying type of the global, i.e., it should not
146 /// have the top-level PointerType, which represents the address of the
149 GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
152 //===--------------------------------------------------------------------===//
153 // Methods for easy access to the types in the module.
156 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
157 /// there is already an entry for this name, true is returned and the symbol
158 /// table is not modified.
160 bool addTypeName(const std::string &Name, const Type *Ty);
162 /// getTypeName - If there is at least one entry in the symbol table for the
163 /// specified type, return it.
165 std::string getTypeName(const Type *Ty) const;
167 /// getTypeByName - Return the type with the specified name in this module, or
168 /// null if there is none by that name.
169 const Type *getTypeByName(const std::string &Name) const;
172 //===--------------------------------------------------------------------===//
173 // Methods for direct access to the globals list, functions list, and symbol
177 /// Get the underlying elements of the Module...
178 inline const GlobalListType &getGlobalList() const { return GlobalList; }
179 inline GlobalListType &getGlobalList() { return GlobalList; }
180 inline const FunctionListType &getFunctionList() const { return FunctionList;}
181 inline FunctionListType &getFunctionList() { return FunctionList;}
183 /// getSymbolTable() - Get access to the symbol table for the module, where
184 /// global variables and functions are identified.
186 inline SymbolTable &getSymbolTable() { return *SymTab; }
187 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
190 //===--------------------------------------------------------------------===//
191 // Module iterator forwarding functions
193 // Globals list interface
194 inline giterator gbegin() { return GlobalList.begin(); }
195 inline const_giterator gbegin() const { return GlobalList.begin(); }
196 inline giterator gend () { return GlobalList.end(); }
197 inline const_giterator gend () const { return GlobalList.end(); }
199 inline reverse_giterator grbegin() { return GlobalList.rbegin(); }
200 inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
201 inline reverse_giterator grend () { return GlobalList.rend(); }
202 inline const_reverse_giterator grend () const { return GlobalList.rend(); }
204 inline unsigned gsize() const { return GlobalList.size(); }
205 inline bool gempty() const { return GlobalList.empty(); }
206 inline const GlobalVariable &gfront() const { return GlobalList.front(); }
207 inline GlobalVariable &gfront() { return GlobalList.front(); }
208 inline const GlobalVariable &gback() const { return GlobalList.back(); }
209 inline GlobalVariable &gback() { return GlobalList.back(); }
211 // FunctionList interface
212 inline iterator begin() { return FunctionList.begin(); }
213 inline const_iterator begin() const { return FunctionList.begin(); }
214 inline iterator end () { return FunctionList.end(); }
215 inline const_iterator end () const { return FunctionList.end(); }
217 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
218 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
219 inline reverse_iterator rend () { return FunctionList.rend(); }
220 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
222 inline unsigned size() const { return FunctionList.size(); }
223 inline bool empty() const { return FunctionList.empty(); }
224 inline const Function &front() const { return FunctionList.front(); }
225 inline Function &front() { return FunctionList.front(); }
226 inline const Function &back() const { return FunctionList.back(); }
227 inline Function &back() { return FunctionList.back(); }
229 //===--------------------------------------------------------------------===//
230 // List of dependent library access functions
232 /// @brief Get a constant iterator to beginning of dependent library list.
233 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
235 /// @brief Get a constant iterator to end of dependent library list.
236 inline lib_iterator lib_end() const { return LibraryList.end(); }
238 /// @brief Returns the number of items in the list of libraries.
239 inline unsigned lib_size() const { return LibraryList.size(); }
241 /// @brief Add a library to the list of dependent libraries
242 inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
244 /// @brief Remove a library from the list of dependent libraries
245 inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
247 /// @brief Get all the libraries
248 inline const LibraryListType& getLibraries() const { return LibraryList; }
250 //===--------------------------------------------------------------------===//
251 // Utility functions for printing and dumping Module objects
253 void print(std::ostream &OS) const { print(OS, 0); }
254 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
258 /// dropAllReferences() - This function causes all the subinstructions to "let
259 /// go" of all references that they are maintaining. This allows one to
260 /// 'delete' a whole class at a time, even though there may be circular
261 /// references... first all references are dropped, and all use counts go to
262 /// zero. Then everything is delete'd for real. Note that no operations are
263 /// valid on an object that has "dropped all references", except operator
266 void dropAllReferences();
269 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
274 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
279 } // End llvm namespace