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"
25 class GlobalValueRefMap; // Used by ConstantVals.cpp
26 class ConstantPointerRef;
30 template<> struct ilist_traits<Function>
31 : public SymbolTableListTraits<Function, Module, Module> {
32 // createNode is used to create a node that marks the end of the list...
33 static Function *createNode();
34 static iplist<Function> &getList(Module *M);
36 template<> struct ilist_traits<GlobalVariable>
37 : public SymbolTableListTraits<GlobalVariable, Module, Module> {
38 // createNode is used to create a node that marks the end of the list...
39 static GlobalVariable *createNode();
40 static iplist<GlobalVariable> &getList(Module *M);
43 struct Module : public Annotable {
44 typedef iplist<GlobalVariable> GlobalListType;
45 typedef iplist<Function> FunctionListType;
47 // Global Variable iterators...
48 typedef GlobalListType::iterator giterator;
49 typedef GlobalListType::const_iterator const_giterator;
50 typedef std::reverse_iterator<giterator> reverse_giterator;
51 typedef std::reverse_iterator<const_giterator> const_reverse_giterator;
53 // Function iterators...
54 typedef FunctionListType::iterator iterator;
55 typedef FunctionListType::const_iterator const_iterator;
56 typedef std::reverse_iterator<iterator> reverse_iterator;
57 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
59 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
60 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
63 GlobalListType GlobalList; // The Global Variables in the module
64 FunctionListType FunctionList; // The Functions in the module
65 GlobalValueRefMap *GVRefMap; // Keep track of GlobalValueRef's
66 SymbolTable *SymTab; // Symbol Table for the module
67 std::string ModuleID; // Human readable identifier for the module
69 // These flags are probably not the right long-term way to handle this kind of
70 // target information, but it is sufficient for now.
71 Endianness Endian; // True if target is little endian
72 PointerSize PtrSize; // True if target has 32-bit pointers (false = 64-bit)
74 // Accessor for the underlying GVRefMap... only through the Constant class...
75 friend class Constant;
76 friend class ConstantPointerRef;
77 void mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV);
78 ConstantPointerRef *getConstantPointerRef(GlobalValue *GV);
79 void destroyConstantPointerRef(ConstantPointerRef *CPR);
82 Module(const std::string &ModuleID);
85 const std::string &getModuleIdentifier() const { return ModuleID; }
87 /// Target endian information...
88 Endianness getEndianness() const { return Endian; }
89 void setEndianness(Endianness E) { Endian = E; }
91 /// Target Pointer Size information...
92 PointerSize getPointerSize() const { return PtrSize; }
93 void setPointerSize(PointerSize PS) { PtrSize = PS; }
95 /// getOrInsertFunction - Look up the specified function in the module symbol
96 /// table. If it does not exist, add a prototype for the function and return
98 Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
100 /// getOrInsertFunction - Look up the specified function in the module symbol
101 /// table. If it does not exist, add a prototype for the function and return
102 /// it. This version of the method takes a null terminated list of function
103 /// arguments, which makes it easier for clients to use.
104 Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...);
106 /// getFunction - Look up the specified function in the module symbol table.
107 /// If it does not exist, return null.
109 Function *getFunction(const std::string &Name, const FunctionType *Ty);
111 /// getMainFunction - This function looks up main efficiently. This is such a
112 /// common case, that it is a method in Module. If main cannot be found, a
113 /// null pointer is returned.
115 Function *getMainFunction();
117 /// getNamedFunction - Return the first function in the module with the
118 /// specified name, of arbitrary type. This method returns null if a function
119 /// with the specified name is not found.
121 Function *getNamedFunction(const std::string &Name);
123 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
124 /// there is already an entry for this name, true is returned and the symbol
125 /// table is not modified.
127 bool addTypeName(const std::string &Name, const Type *Ty);
129 /// getTypeName - If there is at least one entry in the symbol table for the
130 /// specified type, return it.
132 std::string getTypeName(const Type *Ty);
134 /// Get the underlying elements of the Module...
135 inline const GlobalListType &getGlobalList() const { return GlobalList; }
136 inline GlobalListType &getGlobalList() { return GlobalList; }
137 inline const FunctionListType &getFunctionList() const { return FunctionList;}
138 inline FunctionListType &getFunctionList() { return FunctionList;}
141 //===--------------------------------------------------------------------===//
142 // Symbol table support functions...
144 /// getSymbolTable() - Get access to the symbol table for the module, where
145 /// global variables and functions are identified.
147 inline SymbolTable &getSymbolTable() { return *SymTab; }
148 inline const SymbolTable &getSymbolTable() const { return *SymTab; }
151 //===--------------------------------------------------------------------===//
152 // Module iterator forwarding functions
154 inline giterator gbegin() { return GlobalList.begin(); }
155 inline const_giterator gbegin() const { return GlobalList.begin(); }
156 inline giterator gend () { return GlobalList.end(); }
157 inline const_giterator gend () const { return GlobalList.end(); }
159 inline reverse_giterator grbegin() { return GlobalList.rbegin(); }
160 inline const_reverse_giterator grbegin() const { return GlobalList.rbegin(); }
161 inline reverse_giterator grend () { return GlobalList.rend(); }
162 inline const_reverse_giterator grend () const { return GlobalList.rend(); }
164 inline unsigned gsize() const { return GlobalList.size(); }
165 inline bool gempty() const { return GlobalList.empty(); }
166 inline const GlobalVariable &gfront() const { return GlobalList.front(); }
167 inline GlobalVariable &gfront() { return GlobalList.front(); }
168 inline const GlobalVariable &gback() const { return GlobalList.back(); }
169 inline GlobalVariable &gback() { return GlobalList.back(); }
173 inline iterator begin() { return FunctionList.begin(); }
174 inline const_iterator begin() const { return FunctionList.begin(); }
175 inline iterator end () { return FunctionList.end(); }
176 inline const_iterator end () const { return FunctionList.end(); }
178 inline reverse_iterator rbegin() { return FunctionList.rbegin(); }
179 inline const_reverse_iterator rbegin() const { return FunctionList.rbegin(); }
180 inline reverse_iterator rend () { return FunctionList.rend(); }
181 inline const_reverse_iterator rend () const { return FunctionList.rend(); }
183 inline unsigned size() const { return FunctionList.size(); }
184 inline bool empty() const { return FunctionList.empty(); }
185 inline const Function &front() const { return FunctionList.front(); }
186 inline Function &front() { return FunctionList.front(); }
187 inline const Function &back() const { return FunctionList.back(); }
188 inline Function &back() { return FunctionList.back(); }
190 void print(std::ostream &OS) const;
193 /// dropAllReferences() - This function causes all the subinstructions to "let
194 /// go" of all references that they are maintaining. This allows one to
195 /// 'delete' a whole class at a time, even though there may be circular
196 /// references... first all references are dropped, and all use counts go to
197 /// zero. Then everything is delete'd for real. Note that no operations are
198 /// valid on an object that has "dropped all references", except operator
201 void dropAllReferences();
204 inline std::ostream &operator<<(std::ostream &O, const Module *M) {
209 inline std::ostream &operator<<(std::ostream &O, const Module &M) {