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 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 28 | //===----------------------------------------------------------------------===// |
Misha Brukman | 3bcead7 | 2004-04-21 18:27:56 +0000 | [diff] [blame] | 29 | // Methods to implement the globals and functions lists. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 30 | // |
| 31 | |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 32 | Function *ilist_traits<Function>::createSentinel() { |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 33 | FunctionType *FTy = |
Reid Spencer | e76289b | 2007-04-09 06:12:07 +0000 | [diff] [blame] | 34 | FunctionType::get(Type::VoidTy, std::vector<const Type*>(), false); |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 35 | Function *Ret = Function::Create(FTy, GlobalValue::ExternalLinkage); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 36 | // This should not be garbage monitored. |
| 37 | LeakDetector::removeGarbageObject(Ret); |
| 38 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 39 | } |
Chris Lattner | f6c93e3 | 2005-01-30 00:09:23 +0000 | [diff] [blame] | 40 | GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 41 | GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false, |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 42 | GlobalValue::ExternalLinkage); |
Chris Lattner | 184b298 | 2002-09-08 18:59:35 +0000 | [diff] [blame] | 43 | // This should not be garbage monitored. |
| 44 | LeakDetector::removeGarbageObject(Ret); |
| 45 | return Ret; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 46 | } |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 47 | GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() { |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 48 | GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, |
| 49 | GlobalValue::ExternalLinkage); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 50 | // This should not be garbage monitored. |
| 51 | LeakDetector::removeGarbageObject(Ret); |
| 52 | return Ret; |
| 53 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 54 | |
| 55 | iplist<Function> &ilist_traits<Function>::getList(Module *M) { |
| 56 | return M->getFunctionList(); |
| 57 | } |
| 58 | iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) { |
| 59 | return M->getGlobalList(); |
| 60 | } |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 61 | iplist<GlobalAlias> &ilist_traits<GlobalAlias>::getList(Module *M) { |
| 62 | return M->getAliasList(); |
| 63 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 64 | |
| 65 | // Explicit instantiations of SymbolTableListTraits since some of the methods |
Chris Lattner | eef2fe7 | 2006-01-24 04:13:11 +0000 | [diff] [blame] | 66 | // are not in the public header file. |
Chris Lattner | b47aa54 | 2007-04-17 03:26:42 +0000 | [diff] [blame] | 67 | template class SymbolTableListTraits<GlobalVariable, Module>; |
| 68 | template class SymbolTableListTraits<Function, Module>; |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 69 | template class SymbolTableListTraits<GlobalAlias, Module>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 71 | //===----------------------------------------------------------------------===// |
| 72 | // Primitive Module methods. |
| 73 | // |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 74 | |
Chris Lattner | c3f6e00 | 2003-04-22 18:02:04 +0000 | [diff] [blame] | 75 | Module::Module(const std::string &MID) |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 76 | : ModuleID(MID), DataLayout("") { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 77 | ValSymTab = new ValueSymbolTable(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 78 | TypeSymTab = new TypeSymbolTable(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | Module::~Module() { |
| 82 | dropAllReferences(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 83 | GlobalList.clear(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 84 | FunctionList.clear(); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 85 | AliasList.clear(); |
Reid Spencer | a0b05b3 | 2004-07-25 18:08:57 +0000 | [diff] [blame] | 86 | LibraryList.clear(); |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 87 | delete ValSymTab; |
| 88 | delete TypeSymTab; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 91 | /// Target endian information... |
| 92 | Module::Endianness Module::getEndianness() const { |
| 93 | std::string temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 94 | Module::Endianness ret = AnyEndianness; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 95 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 96 | while (!temp.empty()) { |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 97 | std::string token = getToken(temp, "-"); |
| 98 | |
| 99 | if (token[0] == 'e') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 100 | ret = LittleEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 101 | } else if (token[0] == 'E') { |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 102 | ret = BigEndian; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 106 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 109 | /// Target Pointer Size information... |
| 110 | Module::PointerSize Module::getPointerSize() const { |
| 111 | std::string temp = DataLayout; |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 112 | Module::PointerSize ret = AnyPointerSize; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 113 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 114 | while (!temp.empty()) { |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 115 | std::string token = getToken(temp, "-"); |
| 116 | char signal = getToken(token, ":")[0]; |
| 117 | |
| 118 | if (signal == 'p') { |
| 119 | int size = atoi(getToken(token, ":").c_str()); |
| 120 | if (size == 32) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 121 | ret = Pointer32; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 122 | else if (size == 64) |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 123 | ret = Pointer64; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
Owen Anderson | 08aecf5 | 2006-05-18 05:46:08 +0000 | [diff] [blame] | 127 | return ret; |
Owen Anderson | 9eb1a26 | 2006-05-18 02:10:31 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
| 131 | // Methods for easy access to the functions in the module. |
| 132 | // |
| 133 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 134 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 135 | // table. If it does not exist, add a prototype for the function and return |
| 136 | // it. This is nice because it allows most passes to get away with not handling |
| 137 | // the symbol table directly for this common task. |
| 138 | // |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 139 | Constant *Module::getOrInsertFunction(const std::string &Name, |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 140 | const FunctionType *Ty) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 141 | ValueSymbolTable &SymTab = getValueSymbolTable(); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 142 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 143 | // See if we have a definition for the specified function already. |
Reid Spencer | 1241d6d | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 144 | GlobalValue *F = dyn_cast_or_null<GlobalValue>(SymTab.lookup(Name)); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 145 | if (F == 0) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 146 | // Nope, add it |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 147 | Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name); |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 148 | FunctionList.push_back(New); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 149 | return New; // Return the new prototype. |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 150 | } |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 151 | |
| 152 | // Okay, the function exists. Does it have externally visible linkage? |
| 153 | if (F->hasInternalLinkage()) { |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 154 | // Clear the function's name. |
| 155 | F->setName(""); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 156 | // Retry, now there won't be a conflict. |
Chris Lattner | 42e983e | 2008-06-27 21:25:24 +0000 | [diff] [blame] | 157 | Constant *NewF = getOrInsertFunction(Name, Ty); |
| 158 | F->setName(&Name[0], Name.size()); |
| 159 | return NewF; |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // If the function exists but has the wrong type, return a bitcast to the |
| 163 | // right type. |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 164 | if (F->getType() != PointerType::getUnqual(Ty)) |
| 165 | return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty)); |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 166 | |
| 167 | // Otherwise, we just found the existing function or a prototype. |
| 168 | return F; |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 171 | // getOrInsertFunction - Look up the specified function in the module symbol |
| 172 | // table. If it does not exist, add a prototype for the function and return it. |
| 173 | // This version of the method takes a null terminated list of function |
| 174 | // arguments, which makes it easier for clients to use. |
| 175 | // |
Chris Lattner | 505c06b | 2007-01-07 08:09:25 +0000 | [diff] [blame] | 176 | Constant *Module::getOrInsertFunction(const std::string &Name, |
Chris Lattner | bd717d8 | 2003-08-31 00:19:28 +0000 | [diff] [blame] | 177 | const Type *RetTy, ...) { |
| 178 | va_list Args; |
| 179 | va_start(Args, RetTy); |
| 180 | |
| 181 | // Build the list of argument types... |
| 182 | std::vector<const Type*> ArgTys; |
| 183 | while (const Type *ArgTy = va_arg(Args, const Type*)) |
| 184 | ArgTys.push_back(ArgTy); |
| 185 | |
| 186 | va_end(Args); |
| 187 | |
| 188 | // Build the function type and chain to the other getOrInsertFunction... |
| 189 | return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false)); |
| 190 | } |
| 191 | |
| 192 | |
Chris Lattner | a483b06 | 2002-03-29 03:44:18 +0000 | [diff] [blame] | 193 | // getFunction - Look up the specified function in the module symbol table. |
| 194 | // If it does not exist, return null. |
| 195 | // |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 196 | Function *Module::getFunction(const std::string &Name) const { |
| 197 | const ValueSymbolTable &SymTab = getValueSymbolTable(); |
| 198 | return dyn_cast_or_null<Function>(SymTab.lookup(Name)); |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | e43649f | 2008-06-27 21:09:10 +0000 | [diff] [blame] | 201 | Function *Module::getFunction(const char *Name) const { |
| 202 | const ValueSymbolTable &SymTab = getValueSymbolTable(); |
| 203 | return dyn_cast_or_null<Function>(SymTab.lookup(Name, Name+strlen(Name))); |
| 204 | } |
| 205 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | // Methods for easy access to the global variables in the module. |
| 208 | // |
| 209 | |
| 210 | /// getGlobalVariable - Look up the specified global variable in the module |
Chris Lattner | 7d4d93c | 2005-12-05 05:30:21 +0000 | [diff] [blame] | 211 | /// symbol table. If it does not exist, return null. The type argument |
| 212 | /// should be the underlying type of the global, i.e., it should not have |
| 213 | /// the top-level PointerType, which represents the address of the global. |
| 214 | /// If AllowInternal is set to true, this function will return types that |
| 215 | /// have InternalLinkage. By default, these types are not returned. |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 216 | /// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 217 | GlobalVariable *Module::getGlobalVariable(const std::string &Name, |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 218 | bool AllowInternal) const { |
| 219 | if (Value *V = ValSymTab->lookup(Name)) { |
| 220 | GlobalVariable *Result = dyn_cast<GlobalVariable>(V); |
| 221 | if (Result && (AllowInternal || !Result->hasInternalLinkage())) |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 222 | return Result; |
| 223 | } |
| 224 | return 0; |
| 225 | } |
| 226 | |
Bill Wendling | f5f6f74 | 2008-11-05 23:42:27 +0000 | [diff] [blame^] | 227 | /// getOrInsertGlobal - Look up the specified global in the module symbol table. |
| 228 | /// 1. If it does not exist, add a declaration of the global and return it. |
| 229 | /// 2. Else, the global exists but has the wrong type: return the function |
| 230 | /// with a constantexpr cast to the right type. |
| 231 | /// 3. Finally, if the existing global is the correct delclaration, return the |
| 232 | /// existing global. |
Bill Wendling | 2f40956 | 2008-11-04 22:51:24 +0000 | [diff] [blame] | 233 | Constant *Module::getOrInsertGlobal(const std::string &Name, const Type *Ty) { |
| 234 | ValueSymbolTable &SymTab = getValueSymbolTable(); |
| 235 | |
| 236 | // See if we have a definition for the specified global already. |
| 237 | GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(SymTab.lookup(Name)); |
| 238 | if (GV == 0) { |
| 239 | // Nope, add it |
| 240 | GlobalVariable *New = |
| 241 | new GlobalVariable(Ty, false, GlobalVariable::ExternalLinkage, 0, Name); |
| 242 | GlobalList.push_back(New); |
| 243 | return New; // Return the new declaration. |
| 244 | } |
| 245 | |
| 246 | // If the variable exists but has the wrong type, return a bitcast to the |
| 247 | // right type. |
| 248 | if (GV->getType() != PointerType::getUnqual(Ty)) |
| 249 | return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty)); |
| 250 | |
| 251 | // Otherwise, we just found the existing function or a prototype. |
| 252 | return GV; |
| 253 | } |
| 254 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 255 | //===----------------------------------------------------------------------===// |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 256 | // Methods for easy access to the global variables in the module. |
| 257 | // |
| 258 | |
| 259 | // getNamedAlias - Look up the specified global in the module symbol table. |
| 260 | // If it does not exist, return null. |
| 261 | // |
| 262 | GlobalAlias *Module::getNamedAlias(const std::string &Name) const { |
| 263 | const ValueSymbolTable &SymTab = getValueSymbolTable(); |
| 264 | return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name)); |
| 265 | } |
| 266 | |
| 267 | //===----------------------------------------------------------------------===// |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 268 | // Methods for easy access to the types in the module. |
| 269 | // |
| 270 | |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 271 | |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 272 | // addTypeName - Insert an entry in the symbol table mapping Str to Type. If |
| 273 | // there is already an entry for this name, true is returned and the symbol |
| 274 | // table is not modified. |
| 275 | // |
| 276 | bool Module::addTypeName(const std::string &Name, const Type *Ty) { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 277 | TypeSymbolTable &ST = getTypeSymbolTable(); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 278 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 279 | if (ST.lookup(Name)) return true; // Already in symtab... |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 280 | |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 281 | // Not in symbol table? Set the name with the Symtab as an argument so the |
| 282 | // type knows what to update... |
Reid Spencer | f9776c3 | 2004-07-10 16:37:42 +0000 | [diff] [blame] | 283 | ST.insert(Name, Ty); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 284 | |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | /// getTypeByName - Return the type with the specified name in this module, or |
| 289 | /// null if there is none by that name. |
| 290 | const Type *Module::getTypeByName(const std::string &Name) const { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 291 | const TypeSymbolTable &ST = getTypeSymbolTable(); |
| 292 | return cast_or_null<Type>(ST.lookup(Name)); |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 293 | } |
Chris Lattner | 1f985e0 | 2002-11-08 20:34:02 +0000 | [diff] [blame] | 294 | |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 295 | // getTypeName - If there is at least one entry in the symbol table for the |
| 296 | // specified type, return it. |
| 297 | // |
Chris Lattner | be3596c | 2003-12-31 07:09:33 +0000 | [diff] [blame] | 298 | std::string Module::getTypeName(const Type *Ty) const { |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 299 | const TypeSymbolTable &ST = getTypeSymbolTable(); |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 300 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 301 | TypeSymbolTable::const_iterator TI = ST.begin(); |
| 302 | TypeSymbolTable::const_iterator TE = ST.end(); |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 303 | if ( TI == TE ) return ""; // No names for types |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 304 | |
Reid Spencer | abb6f00 | 2004-05-25 08:52:20 +0000 | [diff] [blame] | 305 | while (TI != TE && TI->second != Ty) |
Chris Lattner | 10b7cb5 | 2002-04-13 18:58:33 +0000 | [diff] [blame] | 306 | ++TI; |
| 307 | |
| 308 | if (TI != TE) // Must have found an entry! |
| 309 | return TI->first; |
| 310 | return ""; // Must not have found anything... |
| 311 | } |
| 312 | |
Chris Lattner | 09bd1a0 | 2003-12-31 08:43:01 +0000 | [diff] [blame] | 313 | //===----------------------------------------------------------------------===// |
| 314 | // Other module related stuff. |
| 315 | // |
| 316 | |
| 317 | |
Chris Lattner | e0f6af9b | 2002-08-17 23:32:47 +0000 | [diff] [blame] | 318 | // dropAllReferences() - This function causes all the subelementss to "let go" |
| 319 | // of all references that they are maintaining. This allows one to 'delete' a |
| 320 | // whole module at a time, even though there may be circular references... first |
| 321 | // 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] | 322 | // 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] | 323 | // has "dropped all references", except operator delete. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 324 | // |
| 325 | void Module::dropAllReferences() { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 326 | for(Module::iterator I = begin(), E = end(); I != E; ++I) |
| 327 | I->dropAllReferences(); |
Chris Lattner | 446ad50 | 2001-10-13 06:58:40 +0000 | [diff] [blame] | 328 | |
Chris Lattner | 531f9e9 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 329 | 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] | 330 | I->dropAllReferences(); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 331 | |
| 332 | for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I) |
| 333 | I->dropAllReferences(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | 31cf984 | 2001-06-30 04:35:40 +0000 | [diff] [blame] | 335 | |
Reid Spencer | 3f4e6e8 | 2007-02-04 00:40:42 +0000 | [diff] [blame] | 336 | void Module::addLibrary(const std::string& Lib) { |
| 337 | for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I) |
| 338 | if (*I == Lib) |
| 339 | return; |
| 340 | LibraryList.push_back(Lib); |
| 341 | } |
| 342 | |
| 343 | void Module::removeLibrary(const std::string& Lib) { |
| 344 | LibraryListType::iterator I = LibraryList.begin(); |
| 345 | LibraryListType::iterator E = LibraryList.end(); |
| 346 | for (;I != E; ++I) |
| 347 | if (*I == Lib) { |
| 348 | LibraryList.erase(I); |
| 349 | return; |
| 350 | } |
| 351 | } |
| 352 | |