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
33 template<> struct ilist_traits<Function>
34 : public SymbolTableListTraits<Function, Module, Module> {
35 // createSentinal is used to create a node that marks the end of the list.
36 static Function *createSentinal();
37 static void destroySentinal(Function *F) { delete F; }
38 static iplist<Function> &getList(Module *M);
40 template<> struct ilist_traits<GlobalVariable>
41 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
42 // createSentinal is used to create a node that marks the end of the list.
43 static GlobalVariable *createSentinal();
44 static void destroySentinal(GlobalVariable *GV) { delete GV; }
45 static iplist<GlobalVariable> &getList(Module *M);
50 typedef iplist<GlobalVariable> GlobalListType;
51 typedef iplist<Function> FunctionListType;
52 typedef SetVector<std::string> LibraryListType;
54 // Global Variable iterators...
55 typedef GlobalListType::iterator giterator;
56 typedef GlobalListType::const_iterator const_giterator;
57 typedef std::reverse_iterator<giterator> reverse_giterator;
58 typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
60 // Function iterators...
61 typedef FunctionListType::iterator iterator;
62 typedef FunctionListType::const_iterator const_iterator;
63 typedef std::reverse_iterator<iterator> reverse_iterator;
64 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
66 // Library list iterators
67 typedef LibraryListType::const_iterator lib_iterator;
69 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
70 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
73 GlobalListType GlobalList; // The Global Variables in the module
74 FunctionListType FunctionList; // The Functions in the module
75 LibraryListType LibraryList; // The Libraries needed by the module
76 SymbolTable *SymTab; // Symbol Table for the module
77 std::string ModuleID; // Human readable identifier for the module
78 std::string TargetTriple; // Platform target triple Module compiled on
80 // These flags are probably not the right long-term way to handle this kind of
81 // target information, but it is sufficient for now.
82 Endianness Endian; // True if target is little endian
83 PointerSize PtrSize; // True if target has 32-bit pointers (false = 64-bit)
85 friend class Constant;
88 Module(const std::string &ModuleID);
91 const std::string& getModuleIdentifier() const { return ModuleID; }
92 const std::string& getTargetTriple() const { return TargetTriple; }
93 void setTargetTriple(const std::string& T) { TargetTriple = T; }
95 /// Target endian information...
96 Endianness getEndianness() const { return Endian; }
97 void setEndianness(Endianness E) { Endian = E; }
99 /// Target Pointer Size information...
100 PointerSize getPointerSize() const { return PtrSize; }
101 void setPointerSize(PointerSize PS) { PtrSize = PS; }
103 //===--------------------------------------------------------------------===//
104 // Methods for easy access to the functions in the module.
107 /// getOrInsertFunction - Look up the specified function in the module symbol
108 /// table. If it does not exist, add a prototype for the function and return
110 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
112 /// getOrInsertFunction - Look up the specified function in the module symbol
113 /// table. If it does not exist, add a prototype for the function and return
114 /// it. This version of the method takes a null terminated list of function
115 /// arguments, which makes it easier for clients to use.
116 Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
118 /// getFunction - Look up the specified function in the module symbol table.
119 /// If it does not exist, return null.
121 Function *getFunction(const std::string &Name, const FunctionType *Ty);
123 /// getMainFunction - This function looks up main efficiently. This is such a
124 /// common case, that it is a method in Module. If main cannot be found, a
125 /// null pointer is returned.
127 Function *getMainFunction();
129 /// getNamedFunction - Return the first function in the module with the
130 /// specified name, of arbitrary type. This method returns null if a function
131 /// with the specified name is not found.
133 Function *getNamedFunction(const std::string &Name);
135 //===--------------------------------------------------------------------===//
136 // Methods for easy access to the global variables in the module.
139 /// getGlobalVariable - Look up the specified global variable in the module
140 /// symbol table. If it does not exist, return null. Note that this only
141 /// returns a global variable if it does not have internal linkage. The type
142 /// argument should be the underlying type of the global, i.e., it should not
143 /// have the top-level PointerType, which represents the address of the
146 GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty);
149 //===--------------------------------------------------------------------===//
150 // Methods for easy access to the types in the module.
153 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
154 /// there is already an entry for this name, true is returned and the symbol
155 /// table is not modified.
157 bool addTypeName(const std::string &Name, const Type *Ty);
159 /// getTypeName - If there is at least one entry in the symbol table for the
160 /// specified type, return it.
162 std::string getTypeName(const Type *Ty) const;
164 /// getTypeByName - Return the type with the specified name in this module, or
165 /// null if there is none by that name.
166 const Type *getTypeByName(const std::string &Name) const;
169 //===--------------------------------------------------------------------===//
170 // Methods for direct access to the globals list, functions list, and symbol
174 /// Get the underlying elements of the Module...
175 inline const GlobalListType &getGlobalList() const { return GlobalList; }
176 inline GlobalListType &getGlobalList() { return GlobalList; }
177 inline const FunctionListType &getFunctionList() const { return FunctionList;}
178 inline FunctionListType &getFunctionList() { return FunctionList;}
180 /// getSymbolTable() - Get access to the symbol table for the module, where
181 /// global variables and functions are identified.
183 inline SymbolTable &getSymbolTable() { return *SymTab; }
184 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
187 //===--------------------------------------------------------------------===//
188 // Module iterator forwarding functions
190 // Globals list interface
191 inline giterator gbegin() { return GlobalList.begin(); }
192 inline const_giterator gbegin() const { return GlobalList.begin(); }
193 inline giterator gend () { return GlobalList.end(); }
194 inline const_giterator gend () const { return GlobalList.end(); }
196 inline reverse_giterator grbegin() { return GlobalList.rbegin(); }
197 inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
198 inline reverse_giterator grend () { return GlobalList.rend(); }
199 inline const_reverse_giterator grend () const { return GlobalList.rend(); }
201 inline size_t gsize() const { return GlobalList.size(); }
202 inline bool gempty() const { return GlobalList.empty(); }
203 inline const GlobalVariable &gfront() const { return GlobalList.front(); }
204 inline GlobalVariable &gfront() { return GlobalList.front(); }
205 inline const GlobalVariable &gback() const { return GlobalList.back(); }
206 inline GlobalVariable &gback() { return GlobalList.back(); }
208 // FunctionList interface
209 inline iterator begin() { return FunctionList.begin(); }
210 inline const_iterator begin() const { return FunctionList.begin(); }
211 inline iterator end () { return FunctionList.end(); }
212 inline const_iterator end () const { return FunctionList.end(); }
214 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
215 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
216 inline reverse_iterator rend () { return FunctionList.rend(); }
217 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
219 inline size_t size() const { return FunctionList.size(); }
220 inline bool empty() const { return FunctionList.empty(); }
221 inline const Function &front() const { return FunctionList.front(); }
222 inline Function &front() { return FunctionList.front(); }
223 inline const Function &back() const { return FunctionList.back(); }
224 inline Function &back() { return FunctionList.back(); }
226 //===--------------------------------------------------------------------===//
227 // List of dependent library access functions
229 /// @brief Get a constant iterator to beginning of dependent library list.
230 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
232 /// @brief Get a constant iterator to end of dependent library list.
233 inline lib_iterator lib_end() const { return LibraryList.end(); }
235 /// @brief Returns the number of items in the list of libraries.
236 inline size_t lib_size() const { return LibraryList.size(); }
238 /// @brief Add a library to the list of dependent libraries
239 inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
241 /// @brief Remove a library from the list of dependent libraries
242 inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
244 /// @brief Get all the libraries
245 inline const LibraryListType& getLibraries() const { return LibraryList; }
247 //===--------------------------------------------------------------------===//
248 // Utility functions for printing and dumping Module objects
250 void print(std::ostream &OS) const { print(OS, 0); }
251 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
255 /// dropAllReferences() - This function causes all the subinstructions to "let
256 /// go" of all references that they are maintaining. This allows one to
257 /// 'delete' a whole class at a time, even though there may be circular
258 /// references... first all references are dropped, and all use counts go to
259 /// zero. Then everything is delete'd for real. Note that no operations are
260 /// valid on an object that has "dropped all references", except operator
263 void dropAllReferences();
266 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
271 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
276 } // End llvm namespace