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/InlineAsm.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/Support/DataTypes.h"
31 class GlobalValueRefMap; // Used by ConstantVals.cpp
35 template<> struct ilist_traits<Function>
36 : public SymbolTableListTraits<Function, Module, Module> {
37 // createSentinel is used to create a node that marks the end of the list.
38 static Function *createSentinel();
39 static void destroySentinel(Function *F) { delete F; }
40 static iplist<Function> &getList(Module *M);
42 template<> struct ilist_traits<GlobalVariable>
43 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
44 // createSentinel is used to create a node that marks the end of the list.
45 static GlobalVariable *createSentinel();
46 static void destroySentinel(GlobalVariable *GV) { delete GV; }
47 static iplist<GlobalVariable> &getList(Module *M);
49 template<> struct ilist_traits<InlineAsm>
50 : public SymbolTableListTraits<InlineAsm, Module, Module> {
51 // createSentinel is used to create a node that marks the end of the list.
52 static InlineAsm *createSentinel();
53 static void destroySentinel(InlineAsm *GV) { delete GV; }
54 static iplist<InlineAsm> &getList(Module *M);
59 typedef iplist<GlobalVariable> GlobalListType;
60 typedef iplist<Function> FunctionListType;
61 typedef iplist<InlineAsm> InlineAsmListType;
62 typedef SetVector<std::string> LibraryListType;
64 // Global Variable iterators.
65 typedef GlobalListType::iterator global_iterator;
66 typedef GlobalListType::const_iterator const_global_iterator;
68 // Function iterators.
69 typedef FunctionListType::iterator iterator;
70 typedef FunctionListType::const_iterator const_iterator;
72 // Inline Asm iterators.
73 typedef InlineAsmListType::iterator inlineasm_iterator;
74 typedef InlineAsmListType::const_iterator const_inlineasm_iterator;
76 // Library list iterators.
77 typedef LibraryListType::const_iterator lib_iterator;
79 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
80 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
83 GlobalListType GlobalList; // The Global Variables in the module
84 FunctionListType FunctionList; // The Functions in the module
85 InlineAsmListType InlineAsmList; // The inline asm objects in the module.
86 LibraryListType LibraryList; // The Libraries needed by the module
87 std::string GlobalScopeAsm; // Inline Asm at global scope.
88 SymbolTable *SymTab; // Symbol Table for the module
89 std::string ModuleID; // Human readable identifier for the module
90 std::string TargetTriple; // Platform target triple Module compiled on
91 Endianness Endian; // Endianness assumed in the module
92 PointerSize PtrSize; // Pointer size assumed in the module
94 friend class Constant;
97 Module(const std::string &ModuleID);
100 const std::string &getModuleIdentifier() const { return ModuleID; }
101 void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
103 const std::string &getTargetTriple() const { return TargetTriple; }
104 void setTargetTriple(const std::string &T) { TargetTriple = T; }
106 /// Target endian information...
107 Endianness getEndianness() const { return Endian; }
108 void setEndianness(Endianness E) { Endian = E; }
110 /// Target Pointer Size information...
111 PointerSize getPointerSize() const { return PtrSize; }
112 void setPointerSize(PointerSize PS) { PtrSize = PS; }
114 // Access to any module-scope inline asm blocks.
115 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
116 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
118 //===--------------------------------------------------------------------===//
119 // Methods for easy access to the functions in the module.
122 /// getOrInsertFunction - Look up the specified function in the module symbol
123 /// table. If it does not exist, add a prototype for the function and return
125 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
127 /// getOrInsertFunction - Look up the specified function in the module symbol
128 /// table. If it does not exist, add a prototype for the function and return
129 /// it. This version of the method takes a null terminated list of function
130 /// arguments, which makes it easier for clients to use.
131 Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
134 /// getFunction - Look up the specified function in the module symbol table.
135 /// If it does not exist, return null.
137 Function *getFunction(const std::string &Name, const FunctionType *Ty);
139 /// getMainFunction - This function looks up main efficiently. This is such a
140 /// common case, that it is a method in Module. If main cannot be found, a
141 /// null pointer is returned.
143 Function *getMainFunction();
145 /// getNamedFunction - Return the first function in the module with the
146 /// specified name, of arbitrary type. This method returns null if a function
147 /// with the specified name is not found.
149 Function *getNamedFunction(const std::string &Name);
151 //===--------------------------------------------------------------------===//
152 // Methods for easy access to the global variables in the module.
155 /// getGlobalVariable - Look up the specified global variable in the module
156 /// symbol table. If it does not exist, return null. The type argument
157 /// should be the underlying type of the global, i.e., it should not have
158 /// the top-level PointerType, which represents the address of the global.
159 /// If AllowInternal is set to true, this function will return types that
160 /// have InternalLinkage. By default, these types are not returned.
162 GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty,
163 bool AllowInternal = false);
166 //===--------------------------------------------------------------------===//
167 // Methods for easy access to the types in the module.
170 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
171 /// there is already an entry for this name, true is returned and the symbol
172 /// table is not modified.
174 bool addTypeName(const std::string &Name, const Type *Ty);
176 /// getTypeName - If there is at least one entry in the symbol table for the
177 /// specified type, return it.
179 std::string getTypeName(const Type *Ty) const;
181 /// getTypeByName - Return the type with the specified name in this module, or
182 /// null if there is none by that name.
183 const Type *getTypeByName(const std::string &Name) const;
186 //===--------------------------------------------------------------------===//
187 // Methods for direct access to the globals list, functions list, and symbol
191 // Get the underlying elements of the Module.
192 const GlobalListType &getGlobalList() const { return GlobalList; }
193 GlobalListType &getGlobalList() { return GlobalList; }
194 const FunctionListType &getFunctionList() const { return FunctionList; }
195 FunctionListType &getFunctionList() { return FunctionList; }
196 const InlineAsmListType &getInlineAsmList() const { return InlineAsmList; }
197 InlineAsmListType &getInlineAsmList() { return InlineAsmList; }
199 /// getSymbolTable() - Get access to the symbol table for the module, where
200 /// global variables and functions are identified.
202 SymbolTable &getSymbolTable() { return *SymTab; }
203 const SymbolTable &getSymbolTable() const { return *SymTab; }
206 //===--------------------------------------------------------------------===//
207 // Module iterator forwarding functions
209 // Globals list interface
210 global_iterator global_begin() { return GlobalList.begin(); }
211 const_global_iterator global_begin() const { return GlobalList.begin(); }
212 global_iterator global_end () { return GlobalList.end(); }
213 const_global_iterator global_end () const { return GlobalList.end(); }
214 bool global_empty() const { return GlobalList.empty(); }
216 // FunctionList interface
217 iterator begin() { return FunctionList.begin(); }
218 const_iterator begin() const { return FunctionList.begin(); }
219 iterator end () { return FunctionList.end(); }
220 const_iterator end () const { return FunctionList.end(); }
222 size_t size() const { return FunctionList.size(); }
223 bool empty() const { return FunctionList.empty(); }
225 // Inline Asm list interface
226 inlineasm_iterator inlineasm_begin() {
227 return InlineAsmList.begin();
229 const_inlineasm_iterator inlineasm_begin() const {
230 return InlineAsmList.begin();
232 inlineasm_iterator inlineasm_end() {
233 return InlineAsmList.end();
235 const_inlineasm_iterator inlineasm_end() const {
236 return InlineAsmList.end();
238 bool inlineasm_empty() const { return InlineAsmList.empty(); }
240 //===--------------------------------------------------------------------===//
241 // List of dependent library access functions
243 /// @brief Get a constant iterator to beginning of dependent library list.
244 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
246 /// @brief Get a constant iterator to end of dependent library list.
247 inline lib_iterator lib_end() const { return LibraryList.end(); }
249 /// @brief Returns the number of items in the list of libraries.
250 inline size_t lib_size() const { return LibraryList.size(); }
252 /// @brief Add a library to the list of dependent libraries
253 inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
255 /// @brief Remove a library from the list of dependent libraries
256 inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
258 /// @brief Get all the libraries
259 inline const LibraryListType& getLibraries() const { return LibraryList; }
261 //===--------------------------------------------------------------------===//
262 // Utility functions for printing and dumping Module objects
264 void print(std::ostream &OS) const { print(OS, 0); }
265 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
269 /// dropAllReferences() - This function causes all the subinstructions to "let
270 /// go" of all references that they are maintaining. This allows one to
271 /// 'delete' a whole class at a time, even though there may be circular
272 /// references... first all references are dropped, and all use counts go to
273 /// zero. Then everything is delete'd for real. Note that no operations are
274 /// valid on an object that has "dropped all references", except operator
277 void dropAllReferences();
280 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
285 } // End llvm namespace