Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===-- Module.cpp - Implement the Module class ---------------------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the Module class for the VMCore library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 15 | #include "llvm/InstrTypes.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/Support/LeakDetector.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 21 | #include "SymbolTableListTraitsImpl.h" |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 22 | #include "llvm/TypeSymbolTable.h" |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 24 | #include <cstdarg> |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 25 | #include <cstdlib> |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 26 | #include <map> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 27 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
Misha Brukman | 3bcead7 | 2004-04-21 18:27:56 +0000 | [diff] [blame] | 30 | // Methods to implement the globals and functions lists. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 31 | // |
| 32 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 33 | Function *ilist_traits<Function>::createSentinel() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 34 | FunctionType *FTy = |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 35 | FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false, |
| 36 | std::vector<FunctionType::ParameterAttributes>() ); |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 37 | Function *Ret = new Function(FTy, GlobalValue::ExternalLinkage); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 38 | // This should not be garbage monitored. |
| 39 | LeakDetector::removeGarbageObject(Ret); |
| 40 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 41 | } |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 42 | GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 43 | GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false, |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 44 | GlobalValue::ExternalLinkage); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 45 | // This should not be garbage monitored. |
| 46 | LeakDetector::removeGarbageObject(Ret); |
| 47 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | iplist<Function> &ilist_traits<Function>::getList(Module *M) { |
| 51 | return M->getFunctionList(); |
| 52 | } |
| 53 | iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { |
| 54 | return M->getGlobalList(); |
| 55 | } |
| 56 | |
| 57 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 58 | // are not in the public header file. |
Chris Lattner | 41baa98 | 2003-11-05 05:15:42 +0000 | [diff] [blame] | 59 | template class SymbolTableListTraits<GlobalVariable, Module, Module>; |
| 60 | template class SymbolTableListTraits<Function, Module, Module>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 62 | //===----------------------------------------------------------------------===// |
| 63 | // Primitive Module methods. |
| 64 | // |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 65 | |
Chris Lattner | c3f6e00 | 2003-04-22 18:02:04 +0000 | [diff] [blame] | 66 | Module::Module(const std::string &MID) |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 67 | : ModuleID(MID), DataLayout("") { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 68 | FunctionList.setItemParent(this); |
| 69 | FunctionList.setParent(this); |
| 70 | GlobalList.setItemParent(this); |
| 71 | GlobalList.setParent(this); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 72 | ValSymTab = new SymbolTable(); |
| 73 | TypeSymTab = new TypeSymbolTable(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | Module::~Module() { |
| 77 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 78 | GlobalList.clear(); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 79 | GlobalList.setParent(0); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 80 | FunctionList.clear(); |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 81 | FunctionList.setParent(0); |
Reid Spencer | a0b05b3 | 2004-07-25 18:08:57 +0000 | [diff] [blame] | 82 | LibraryList.clear(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 83 | delete ValSymTab; |
| 84 | delete TypeSymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 87 | // Module::dump() - Allow printing from debugger |
| 88 | void Module::dump() const { |
Bill Wendling | 22e978a | 2006-12-07 20:04:42 +0000 | [diff] [blame] | 89 | print(*cerr.stream()); |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 92 | /// Target endian information... |
| 93 | Module::Endianness Module::getEndianness() const { |
| 94 | std::string temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 95 | Module::Endianness ret = AnyEndianness; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 96 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 97 | while (!temp.empty()) { |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 98 | std::string token = getToken(temp, "-"); |
| 99 | |
| 100 | if (token[0] == 'e') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 101 | ret = LittleEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 102 | } else if (token[0] == 'E') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 103 | ret = BigEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 107 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 110 | /// Target Pointer Size information... |
| 111 | Module::PointerSize Module::getPointerSize() const { |
| 112 | std::string temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 113 | Module::PointerSize ret = AnyPointerSize; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 114 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 115 | while (!temp.empty()) { |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 116 | std::string token = getToken(temp, "-"); |
| 117 | char signal = getToken(token, ":")[0]; |
| 118 | |
| 119 | if (signal == 'p') { |
| 120 | int size = atoi(getToken(token, ":").c_str()); |
| 121 | if (size == 32) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 122 | ret = Pointer32; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 123 | else if (size == 64) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 124 | ret = Pointer64; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 128 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 131 | //===----------------------------------------------------------------------===// |
| 132 | // Methods for easy access to the functions in the module. |
| 133 | // |
| 134 | |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 135 | Constant *Module::getOrInsertFunction(const std::string &Name, |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 136 | const FunctionType *Ty) { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 137 | SymbolTable &SymTab = getValueSymbolTable(); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 139 | // See if we have a definitions for the specified function already. |
| 140 | Function *F = |
| 141 | dyn_cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name)); |
| 142 | if (F == 0) { |
| 143 | // Nope, add it. |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 144 | Function *New = new Function(Ty, GlobalVariable::ExternalLinkage, Name); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 145 | FunctionList.push_back(New); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 146 | return New; // Return the new prototype. |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 148 | |
| 149 | // Okay, the function exists. Does it have externally visible linkage? |
| 150 | if (F->hasInternalLinkage()) { |
| 151 | // Rename the function. |
| 152 | F->setName(SymTab.getUniqueName(F->getType(), F->getName())); |
| 153 | // Retry, now there won't be a conflict. |
| 154 | return getOrInsertFunction(Name, Ty); |
| 155 | } |
| 156 | |
| 157 | // If the function exists but has the wrong type, return a bitcast to the |
| 158 | // right type. |
| 159 | if (F->getFunctionType() != Ty) |
| 160 | return ConstantExpr::getBitCast(F, PointerType::get(Ty)); |
| 161 | |
| 162 | // Otherwise, we just found the existing function or a prototype. |
| 163 | return F; |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 166 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 167 | // table. If it does not exist, add a prototype for the function and return it. |
| 168 | // This version of the method takes a null terminated list of function |
| 169 | // arguments, which makes it easier for clients to use. |
| 170 | // |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 171 | Constant *Module::getOrInsertFunction(const std::string &Name, |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 172 | const Type *RetTy, ...) { |
| 173 | va_list Args; |
| 174 | va_start(Args, RetTy); |
| 175 | |
| 176 | // Build the list of argument types... |
| 177 | std::vector<const Type*> ArgTys; |
| 178 | while (const Type *ArgTy = va_arg(Args, const Type*)) |
| 179 | ArgTys.push_back(ArgTy); |
| 180 | |
| 181 | va_end(Args); |
| 182 | |
| 183 | // Build the function type and chain to the other getOrInsertFunction... |
| 184 | return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false)); |
| 185 | } |
| 186 | |
| 187 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 188 | // getFunction - Look up the specified function in the module symbol table. |
| 189 | // If it does not exist, return null. |
| 190 | // |
| 191 | Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 192 | SymbolTable &SymTab = getValueSymbolTable(); |
Chris Lattner | 98cf1f5 | 2002-11-20 18:36:02 +0000 | [diff] [blame] | 193 | return cast_or_null<Function>(SymTab.lookup(PointerType::get(Ty), Name)); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 197 | /// getMainFunction - This function looks up main efficiently. This is such a |
| 198 | /// common case, that it is a method in Module. If main cannot be found, a |
| 199 | /// null pointer is returned. |
| 200 | /// |
| 201 | Function *Module::getMainFunction() { |
| 202 | std::vector<const Type*> Params; |
| 203 | |
| 204 | // int main(void)... |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 205 | if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty, |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 206 | Params, false))) |
| 207 | return F; |
| 208 | |
| 209 | // void main(void)... |
| 210 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 211 | Params, false))) |
| 212 | return F; |
| 213 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 214 | Params.push_back(Type::Int32Ty); |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 215 | |
| 216 | // int main(int argc)... |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 217 | if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty, |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 218 | Params, false))) |
| 219 | return F; |
| 220 | |
| 221 | // void main(int argc)... |
| 222 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 223 | Params, false))) |
| 224 | return F; |
| 225 | |
| 226 | for (unsigned i = 0; i != 2; ++i) { // Check argv and envp |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 227 | Params.push_back(PointerType::get(PointerType::get(Type::Int8Ty))); |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 228 | |
| 229 | // int main(int argc, char **argv)... |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 230 | if (Function *F = getFunction("main", FunctionType::get(Type::Int32Ty, |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 231 | Params, false))) |
| 232 | return F; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 234 | // void main(int argc, char **argv)... |
| 235 | if (Function *F = getFunction("main", FunctionType::get(Type::VoidTy, |
| 236 | Params, false))) |
| 237 | return F; |
| 238 | } |
| 239 | |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 240 | // Ok, try to find main the hard way... |
| 241 | return getNamedFunction("main"); |
| 242 | } |
| 243 | |
| 244 | /// getNamedFunction - Return the first function in the module with the |
| 245 | /// specified name, of arbitrary type. This method returns null if a function |
| 246 | /// with the specified name is not found. |
| 247 | /// |
Reid Spencer | 9fef163 | 2006-05-31 16:40:28 +0000 | [diff] [blame] | 248 | Function *Module::getNamedFunction(const std::string &Name) const { |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 249 | // Loop over all of the functions, looking for the function desired |
Reid Spencer | 9fef163 | 2006-05-31 16:40:28 +0000 | [diff] [blame] | 250 | const Function *Found = 0; |
| 251 | for (const_iterator I = begin(), E = end(); I != E; ++I) |
Chris Lattner | b2e46c0 | 2002-11-19 18:41:44 +0000 | [diff] [blame] | 252 | if (I->getName() == Name) |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 253 | if (I->isDeclaration()) |
Chris Lattner | 4b4dacd | 2003-07-23 20:21:30 +0000 | [diff] [blame] | 254 | Found = I; |
| 255 | else |
Reid Spencer | 9fef163 | 2006-05-31 16:40:28 +0000 | [diff] [blame] | 256 | return const_cast<Function*>(&(*I)); |
| 257 | return const_cast<Function*>(Found); // Non-external function not found... |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 260 | //===----------------------------------------------------------------------===// |
| 261 | // Methods for easy access to the global variables in the module. |
| 262 | // |
| 263 | |
| 264 | /// getGlobalVariable - Look up the specified global variable in the module |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 265 | /// symbol table. If it does not exist, return null. The type argument |
| 266 | /// should be the underlying type of the global, i.e., it should not have |
| 267 | /// the top-level PointerType, which represents the address of the global. |
| 268 | /// If AllowInternal is set to true, this function will return types that |
| 269 | /// have InternalLinkage. By default, these types are not returned. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 270 | /// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 271 | GlobalVariable *Module::getGlobalVariable(const std::string &Name, |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 272 | const Type *Ty, bool AllowInternal) { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 273 | if (Value *V = getValueSymbolTable().lookup(PointerType::get(Ty), Name)) { |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 274 | GlobalVariable *Result = cast<GlobalVariable>(V); |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 275 | if (AllowInternal || !Result->hasInternalLinkage()) |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 276 | return Result; |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | |
Chris Lattner | 48a8e09 | 2006-03-08 18:39:13 +0000 | [diff] [blame] | 281 | /// getNamedGlobal - Return the first global variable in the module with the |
| 282 | /// specified name, of arbitrary type. This method returns null if a global |
| 283 | /// with the specified name is not found. |
| 284 | /// |
Reid Spencer | 9fef163 | 2006-05-31 16:40:28 +0000 | [diff] [blame] | 285 | GlobalVariable *Module::getNamedGlobal(const std::string &Name) const { |
Chris Lattner | 48a8e09 | 2006-03-08 18:39:13 +0000 | [diff] [blame] | 286 | // FIXME: This would be much faster with a symbol table that doesn't |
| 287 | // discriminate based on type! |
Reid Spencer | 9fef163 | 2006-05-31 16:40:28 +0000 | [diff] [blame] | 288 | for (const_global_iterator I = global_begin(), E = global_end(); |
Chris Lattner | 48a8e09 | 2006-03-08 18:39:13 +0000 | [diff] [blame] | 289 | I != E; ++I) |
| 290 | if (I->getName() == Name) |
Reid Spencer | 9fef163 | 2006-05-31 16:40:28 +0000 | [diff] [blame] | 291 | return const_cast<GlobalVariable*>(&(*I)); |
Chris Lattner | 48a8e09 | 2006-03-08 18:39:13 +0000 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 295 | |
| 296 | |
| 297 | //===----------------------------------------------------------------------===// |
| 298 | // Methods for easy access to the types in the module. |
| 299 | // |
| 300 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 301 | |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 302 | // addTypeName - Insert an entry in the symbol table mapping Str to Type. If |
| 303 | // there is already an entry for this name, true is returned and the symbol |
| 304 | // table is not modified. |
| 305 | // |
| 306 | bool Module::addTypeName(const std::string &Name, const Type *Ty) { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 307 | TypeSymbolTable &ST = getTypeSymbolTable(); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 308 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 309 | if (ST.lookup(Name)) return true; // Already in symtab... |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 310 | |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 311 | // Not in symbol table? Set the name with the Symtab as an argument so the |
| 312 | // type knows what to update... |
Reid Spencer | f9776c3 | 2004-07-10 16:37:42 +0000 | [diff] [blame] | 313 | ST.insert(Name, Ty); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 314 | |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | /// getTypeByName - Return the type with the specified name in this module, or |
| 319 | /// null if there is none by that name. |
| 320 | const Type *Module::getTypeByName(const std::string &Name) const { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 321 | const TypeSymbolTable &ST = getTypeSymbolTable(); |
| 322 | return cast_or_null<Type>(ST.lookup(Name)); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 323 | } |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 325 | // getTypeName - If there is at least one entry in the symbol table for the |
| 326 | // specified type, return it. |
| 327 | // |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 328 | std::string Module::getTypeName(const Type *Ty) const { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 329 | const TypeSymbolTable &ST = getTypeSymbolTable(); |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 330 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 331 | TypeSymbolTable::const_iterator TI = ST.begin(); |
| 332 | TypeSymbolTable::const_iterator TE = ST.end(); |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 333 | if ( TI == TE ) return ""; // No names for types |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 334 | |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 335 | while (TI != TE && TI->second != Ty) |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 336 | ++TI; |
| 337 | |
| 338 | if (TI != TE) // Must have found an entry! |
| 339 | return TI->first; |
| 340 | return ""; // Must not have found anything... |
| 341 | } |
| 342 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 343 | //===----------------------------------------------------------------------===// |
| 344 | // Other module related stuff. |
| 345 | // |
| 346 | |
| 347 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 348 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 349 | // of all references that they are maintaining. This allows one to 'delete' a |
| 350 | // whole module at a time, even though there may be circular references... first |
| 351 | // all references are dropped, and all use counts go to zero. Then everything |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 352 | // is deleted for real. Note that no operations are valid on an object that |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 353 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 354 | // |
| 355 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 356 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 357 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 359 | for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 360 | I->dropAllReferences(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 361 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 362 | |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 363 | void Module::addLibrary(const std::string& Lib) { |
| 364 | for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I) |
| 365 | if (*I == Lib) |
| 366 | return; |
| 367 | LibraryList.push_back(Lib); |
| 368 | } |
| 369 | |
| 370 | void Module::removeLibrary(const std::string& Lib) { |
| 371 | LibraryListType::iterator I = LibraryList.begin(); |
| 372 | LibraryListType::iterator E = LibraryList.end(); |
| 373 | for (;I != E; ++I) |
| 374 | if (*I == Lib) { |
| 375 | LibraryList.erase(I); |
| 376 | return; |
| 377 | } |
| 378 | } |
| 379 | |