1 //===-- Module.cpp - Implement the Module class ---------------------------===//
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 implements the Module class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Module.h"
15 #include "llvm/InstrTypes.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "Support/STLExtras.h"
19 #include "Support/LeakDetector.h"
20 #include "SymbolTableListTraitsImpl.h"
27 //===----------------------------------------------------------------------===//
28 // Methods to implement the globals and functions lists.
31 Function *ilist_traits<Function>::createNode() {
33 FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false);
34 Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage);
35 // This should not be garbage monitored.
36 LeakDetector::removeGarbageObject(Ret);
39 GlobalVariable *ilist_traits<GlobalVariable>::createNode() {
40 GlobalVariable *Ret = new GlobalVariable(Type::IntTy, false,
41 GlobalValue::ExternalLinkage);
42 // This should not be garbage monitored.
43 LeakDetector::removeGarbageObject(Ret);
47 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
48 return M->getFunctionList();
50 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
51 return M->getGlobalList();
54 // Explicit instantiations of SymbolTableListTraits since some of the methods
55 // are not in the public header file...
56 template class SymbolTableListTraits<GlobalVariable, Module, Module>;
57 template class SymbolTableListTraits<Function, Module, Module>;
59 //===----------------------------------------------------------------------===//
60 // Primitive Module methods.
63 Module::Module(const std::string &MID)
64 : ModuleID(MID), Endian(AnyEndianness), PtrSize(AnyPointerSize) {
65 FunctionList.setItemParent(this);
66 FunctionList.setParent(this);
67 GlobalList.setItemParent(this);
68 GlobalList.setParent(this);
69 SymTab = new SymbolTable();
75 GlobalList.setParent(0);
77 FunctionList.setParent(0);
81 // Module::dump() - Allow printing from debugger
82 void Module::dump() const {
86 //===----------------------------------------------------------------------===//
87 // Methods for easy access to the functions in the module.
90 // getOrInsertFunction - Look up the specified function in the module symbol
91 // table. If it does not exist, add a prototype for the function and return
92 // it. This is nice because it allows most passes to get away with not handling
93 // the symbol table directly for this common task.
95 Function *Module::getOrInsertFunction(const std::string &Name,
96 const FunctionType *Ty) {
97 SymbolTable &SymTab = getSymbolTable();
99 // See if we have a definitions for the specified function already...
100 if (Value *V = SymTab.lookup(PointerType::get(Ty), Name)) {
101 return cast<Function>(V); // Yup, got it
102 } else { // Nope, add one
103 Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name);
104 FunctionList.push_back(New);
105 return New; // Return the new prototype...
109 // getOrInsertFunction - Look up the specified function in the module symbol
110 // table. If it does not exist, add a prototype for the function and return it.
111 // This version of the method takes a null terminated list of function
112 // arguments, which makes it easier for clients to use.
114 Function *Module::getOrInsertFunction(const std::string &Name,
115 const Type *RetTy, ...) {
117 va_start(Args, RetTy);
119 // Build the list of argument types...
120 std::vector<const Type*> ArgTys;
121 while (const Type *ArgTy = va_arg(Args, const Type*))
122 ArgTys.push_back(ArgTy);
126 // Build the function type and chain to the other getOrInsertFunction...
127 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false));
131 // getFunction - Look up the specified function in the module symbol table.
132 // If it does not exist, return null.
134 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
135 SymbolTable &SymTab = getSymbolTable();
136 return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name));
140 /// getMainFunction - This function looks up main efficiently. This is such a
141 /// common case, that it is a method in Module. If main cannot be found, a
142 /// null pointer is returned.
144 Function *Module::getMainFunction() {
145 std::vector<const Type*> Params;
148 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
152 // void main(void)...
153 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
157 Params.push_back(Type::IntTy);
159 // int main(int argc)...
160 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
164 // void main(int argc)...
165 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
169 for (unsigned i = 0; i != 2; ++i) { // Check argv and envp
170 Params.push_back(PointerType::get(PointerType::get(Type::SByteTy)));
172 // int main(int argc, char **argv)...
173 if (Function *F = getFunction("main", FunctionType::get(Type::IntTy,
177 // void main(int argc, char **argv)...
178 if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy,
183 // Ok, try to find main the hard way...
184 return getNamedFunction("main");
187 /// getNamedFunction - Return the first function in the module with the
188 /// specified name, of arbitrary type. This method returns null if a function
189 /// with the specified name is not found.
191 Function *Module::getNamedFunction(const std::string &Name) {
192 // Loop over all of the functions, looking for the function desired
194 for (iterator I = begin(), E = end(); I != E; ++I)
195 if (I->getName() == Name)
200 return Found; // Non-external function not found...
203 //===----------------------------------------------------------------------===//
204 // Methods for easy access to the global variables in the module.
207 /// getGlobalVariable - Look up the specified global variable in the module
208 /// symbol table. If it does not exist, return null. Note that this only
209 /// returns a global variable if it does not have internal linkage. The type
210 /// argument should be the underlying type of the global, ie, it should not
211 /// have the top-level PointerType, which represents the address of the
214 GlobalVariable *Module::getGlobalVariable(const std::string &Name,
216 if (Value *V = getSymbolTable().lookup(PointerType::get(Ty), Name)) {
217 GlobalVariable *Result = cast<GlobalVariable>(V);
218 if (!Result->hasInternalLinkage())
226 //===----------------------------------------------------------------------===//
227 // Methods for easy access to the types in the module.
231 // addTypeName - Insert an entry in the symbol table mapping Str to Type. If
232 // there is already an entry for this name, true is returned and the symbol
233 // table is not modified.
235 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
236 SymbolTable &ST = getSymbolTable();
238 if (ST.lookupType(Name)) return true; // Already in symtab...
240 // Not in symbol table? Set the name with the Symtab as an argument so the
241 // type knows what to update...
247 /// getTypeByName - Return the type with the specified name in this module, or
248 /// null if there is none by that name.
249 const Type *Module::getTypeByName(const std::string &Name) const {
250 const SymbolTable &ST = getSymbolTable();
251 return cast_or_null<Type>(ST.lookupType(Name));
254 // getTypeName - If there is at least one entry in the symbol table for the
255 // specified type, return it.
257 std::string Module::getTypeName(const Type *Ty) const {
258 const SymbolTable &ST = getSymbolTable();
260 SymbolTable::type_const_iterator TI = ST.type_begin();
261 SymbolTable::type_const_iterator TE = ST.type_end();
262 if ( TI == TE ) return ""; // No names for types
264 while (TI != TE && TI->second != Ty)
267 if (TI != TE) // Must have found an entry!
269 return ""; // Must not have found anything...
273 //===----------------------------------------------------------------------===//
274 // Other module related stuff.
278 // dropAllReferences() - This function causes all the subelementss to "let go"
279 // of all references that they are maintaining. This allows one to 'delete' a
280 // whole module at a time, even though there may be circular references... first
281 // all references are dropped, and all use counts go to zero. Then everything
282 // is deleted for real. Note that no operations are valid on an object that
283 // has "dropped all references", except operator delete.
285 void Module::dropAllReferences() {
286 for(Module::iterator I = begin(), E = end(); I != E; ++I)
287 I->dropAllReferences();
289 for(Module::giterator I = gbegin(), E = gend(); I != E; ++I)
290 I->dropAllReferences();