blob: 510f3d5bd77c23aef8feb37ee00b5caa55fbf652 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- Module.cpp - Implement the Module class ---------------------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements the Module class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/Module.h"
Chris Lattner31cf9842001-06-30 04:35:40 +000015#include "llvm/InstrTypes.h"
Chris Lattnerca142372002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattnera483b062002-03-29 03:44:18 +000017#include "llvm/DerivedTypes.h"
Owen Anderson6773d382009-07-01 16:58:40 +000018#include "llvm/LLVMContext.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/ADT/STLExtras.h"
Owen Anderson9eb1a262006-05-18 02:10:31 +000020#include "llvm/ADT/StringExtras.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000022#include "SymbolTableListTraitsImpl.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000023#include "llvm/TypeSymbolTable.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000024#include <algorithm>
Chris Lattnerbd717d82003-08-31 00:19:28 +000025#include <cstdarg>
Owen Anderson9eb1a262006-05-18 02:10:31 +000026#include <cstdlib>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner09bd1a02003-12-31 08:43:01 +000029//===----------------------------------------------------------------------===//
Misha Brukman3bcead72004-04-21 18:27:56 +000030// Methods to implement the globals and functions lists.
Chris Lattner09bd1a02003-12-31 08:43:01 +000031//
32
Chris Lattnerf6c93e32005-01-30 00:09:23 +000033GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() {
Chris Lattner46b5c642009-11-06 04:27:31 +000034 GlobalVariable *Ret = new GlobalVariable(Type::getInt32Ty(getGlobalContext()),
Owen Andersonb17f3292009-07-08 19:03:57 +000035 false, GlobalValue::ExternalLinkage);
Chris Lattner184b2982002-09-08 18:59:35 +000036 // This should not be garbage monitored.
37 LeakDetector::removeGarbageObject(Ret);
38 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000039}
Anton Korobeynikova97b6942007-04-25 14:27:10 +000040GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
Owen Anderson55f1c092009-08-13 21:58:54 +000041 GlobalAlias *Ret = new GlobalAlias(Type::getInt32Ty(getGlobalContext()),
Dan Gohman929391a2008-01-29 12:09:55 +000042 GlobalValue::ExternalLinkage);
Anton Korobeynikova97b6942007-04-25 14:27:10 +000043 // This should not be garbage monitored.
44 LeakDetector::removeGarbageObject(Ret);
45 return Ret;
46}
Chris Lattner113f4f42002-06-25 16:13:24 +000047
Chris Lattner113f4f42002-06-25 16:13:24 +000048// Explicit instantiations of SymbolTableListTraits since some of the methods
Chris Lattnereef2fe72006-01-24 04:13:11 +000049// are not in the public header file.
John McCall086bb4e2009-12-19 00:55:12 +000050template class llvm::SymbolTableListTraits<GlobalVariable, Module>;
51template class llvm::SymbolTableListTraits<Function, Module>;
52template class llvm::SymbolTableListTraits<GlobalAlias, Module>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000053
Chris Lattner09bd1a02003-12-31 08:43:01 +000054//===----------------------------------------------------------------------===//
55// Primitive Module methods.
56//
Chris Lattner446ad502001-10-13 06:58:40 +000057
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000058Module::Module(StringRef MID, LLVMContext& C)
Owen Anderson6773d382009-07-01 16:58:40 +000059 : Context(C), ModuleID(MID), DataLayout("") {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000060 ValSymTab = new ValueSymbolTable();
Reid Spencer32af9e82007-01-06 07:24:44 +000061 TypeSymTab = new TypeSymbolTable();
Devang Patelfcfee0f2010-01-07 19:39:36 +000062 NamedMDSymTab = new MDSymbolTable();
Chris Lattner2f7c9632001-06-06 20:29:01 +000063}
64
65Module::~Module() {
66 dropAllReferences();
Chris Lattner113f4f42002-06-25 16:13:24 +000067 GlobalList.clear();
Chris Lattner113f4f42002-06-25 16:13:24 +000068 FunctionList.clear();
Anton Korobeynikova97b6942007-04-25 14:27:10 +000069 AliasList.clear();
Reid Spencera0b05b32004-07-25 18:08:57 +000070 LibraryList.clear();
Devang Patel18dfdc92009-07-29 17:16:17 +000071 NamedMDList.clear();
Reid Spencer32af9e82007-01-06 07:24:44 +000072 delete ValSymTab;
73 delete TypeSymTab;
Devang Patel71ff5472010-01-09 01:44:59 +000074 delete NamedMDSymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000075}
76
Owen Anderson9eb1a262006-05-18 02:10:31 +000077/// Target endian information...
78Module::Endianness Module::getEndianness() const {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000079 StringRef temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000080 Module::Endianness ret = AnyEndianness;
Owen Anderson9eb1a262006-05-18 02:10:31 +000081
Owen Anderson08aecf52006-05-18 05:46:08 +000082 while (!temp.empty()) {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000083 StringRef token = DataLayout;
84 tie(token, temp) = getToken(DataLayout, "-");
Owen Anderson9eb1a262006-05-18 02:10:31 +000085
86 if (token[0] == 'e') {
Owen Anderson08aecf52006-05-18 05:46:08 +000087 ret = LittleEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000088 } else if (token[0] == 'E') {
Owen Anderson08aecf52006-05-18 05:46:08 +000089 ret = BigEndian;
Owen Anderson9eb1a262006-05-18 02:10:31 +000090 }
91 }
92
Owen Anderson08aecf52006-05-18 05:46:08 +000093 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +000094}
95
Owen Anderson9eb1a262006-05-18 02:10:31 +000096/// Target Pointer Size information...
97Module::PointerSize Module::getPointerSize() const {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +000098 StringRef temp = DataLayout;
Owen Anderson08aecf52006-05-18 05:46:08 +000099 Module::PointerSize ret = AnyPointerSize;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000100
Owen Anderson08aecf52006-05-18 05:46:08 +0000101 while (!temp.empty()) {
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000102 StringRef token, signalToken;
103 tie(token, temp) = getToken(temp, "-");
104 tie(signalToken, token) = getToken(token, ":");
Owen Anderson9eb1a262006-05-18 02:10:31 +0000105
Benjamin Kramerc6fe3c32010-01-11 18:03:24 +0000106 if (signalToken[0] == 'p') {
107 int size = 0;
108 getToken(token, ":").first.getAsInteger(10, size);
Owen Anderson9eb1a262006-05-18 02:10:31 +0000109 if (size == 32)
Owen Anderson08aecf52006-05-18 05:46:08 +0000110 ret = Pointer32;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000111 else if (size == 64)
Owen Anderson08aecf52006-05-18 05:46:08 +0000112 ret = Pointer64;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000113 }
114 }
115
Owen Anderson08aecf52006-05-18 05:46:08 +0000116 return ret;
Owen Anderson9eb1a262006-05-18 02:10:31 +0000117}
118
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000119/// getNamedValue - Return the first global value in the module with
120/// the specified name, of arbitrary type. This method returns null
121/// if a global with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000122GlobalValue *Module::getNamedValue(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000123 return cast_or_null<GlobalValue>(getValueSymbolTable().lookup(Name));
124}
125
Chris Lattnera0566972009-12-29 09:01:33 +0000126/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
127/// This ID is uniqued across modules in the current LLVMContext.
128unsigned Module::getMDKindID(StringRef Name) const {
129 return Context.getMDKindID(Name);
130}
131
132/// getMDKindNames - Populate client supplied SmallVector with the name for
133/// custom metadata IDs registered in this LLVMContext. ID #0 is not used,
134/// so it is filled in as an empty string.
135void Module::getMDKindNames(SmallVectorImpl<StringRef> &Result) const {
136 return Context.getMDKindNames(Result);
137}
138
139
Chris Lattner09bd1a02003-12-31 08:43:01 +0000140//===----------------------------------------------------------------------===//
141// Methods for easy access to the functions in the module.
142//
143
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000144// getOrInsertFunction - Look up the specified function in the module symbol
145// table. If it does not exist, add a prototype for the function and return
146// it. This is nice because it allows most passes to get away with not handling
147// the symbol table directly for this common task.
148//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000149Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000150 const FunctionType *Ty,
151 AttrListPtr AttributeList) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000152 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000153 GlobalValue *F = getNamedValue(Name);
Chris Lattner505c06b2007-01-07 08:09:25 +0000154 if (F == 0) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000155 // Nope, add it
Gabor Greife9ecc682008-04-06 20:25:17 +0000156 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000157 if (!New->isIntrinsic()) // Intrinsics get attrs set on construction
158 New->setAttributes(AttributeList);
Chris Lattnera483b062002-03-29 03:44:18 +0000159 FunctionList.push_back(New);
Chris Lattner505c06b2007-01-07 08:09:25 +0000160 return New; // Return the new prototype.
Chris Lattnera483b062002-03-29 03:44:18 +0000161 }
Chris Lattner505c06b2007-01-07 08:09:25 +0000162
163 // Okay, the function exists. Does it have externally visible linkage?
Rafael Espindola6de96a12009-01-15 20:18:42 +0000164 if (F->hasLocalLinkage()) {
Chris Lattner42e983e2008-06-27 21:25:24 +0000165 // Clear the function's name.
166 F->setName("");
Chris Lattner505c06b2007-01-07 08:09:25 +0000167 // Retry, now there won't be a conflict.
Chris Lattner42e983e2008-06-27 21:25:24 +0000168 Constant *NewF = getOrInsertFunction(Name, Ty);
Daniel Dunbard43b86d2009-07-25 06:02:13 +0000169 F->setName(Name);
Chris Lattner42e983e2008-06-27 21:25:24 +0000170 return NewF;
Chris Lattner505c06b2007-01-07 08:09:25 +0000171 }
172
173 // If the function exists but has the wrong type, return a bitcast to the
174 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000175 if (F->getType() != PointerType::getUnqual(Ty))
176 return ConstantExpr::getBitCast(F, PointerType::getUnqual(Ty));
Chris Lattner505c06b2007-01-07 08:09:25 +0000177
178 // Otherwise, we just found the existing function or a prototype.
179 return F;
Chris Lattnera483b062002-03-29 03:44:18 +0000180}
181
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000182Constant *Module::getOrInsertTargetIntrinsic(StringRef Name,
Dale Johannesenb842d522009-02-05 01:49:45 +0000183 const FunctionType *Ty,
184 AttrListPtr AttributeList) {
Dale Johannesenb842d522009-02-05 01:49:45 +0000185 // See if we have a definition for the specified function already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000186 GlobalValue *F = getNamedValue(Name);
Dale Johannesenb842d522009-02-05 01:49:45 +0000187 if (F == 0) {
188 // Nope, add it
189 Function *New = Function::Create(Ty, GlobalVariable::ExternalLinkage, Name);
190 New->setAttributes(AttributeList);
191 FunctionList.push_back(New);
192 return New; // Return the new prototype.
193 }
194
195 // Otherwise, we just found the existing function or a prototype.
196 return F;
197}
198
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000199Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000200 const FunctionType *Ty) {
201 AttrListPtr AttributeList = AttrListPtr::get((AttributeWithIndex *)0, 0);
202 return getOrInsertFunction(Name, Ty, AttributeList);
203}
204
Chris Lattnerbd717d82003-08-31 00:19:28 +0000205// getOrInsertFunction - Look up the specified function in the module symbol
206// table. If it does not exist, add a prototype for the function and return it.
207// This version of the method takes a null terminated list of function
208// arguments, which makes it easier for clients to use.
209//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000210Constant *Module::getOrInsertFunction(StringRef Name,
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000211 AttrListPtr AttributeList,
212 const Type *RetTy, ...) {
213 va_list Args;
214 va_start(Args, RetTy);
215
216 // Build the list of argument types...
217 std::vector<const Type*> ArgTys;
218 while (const Type *ArgTy = va_arg(Args, const Type*))
219 ArgTys.push_back(ArgTy);
220
221 va_end(Args);
222
223 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000224 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000225 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000226 AttributeList);
227}
228
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000229Constant *Module::getOrInsertFunction(StringRef Name,
Chris Lattnerbd717d82003-08-31 00:19:28 +0000230 const Type *RetTy, ...) {
231 va_list Args;
232 va_start(Args, RetTy);
233
234 // Build the list of argument types...
235 std::vector<const Type*> ArgTys;
236 while (const Type *ArgTy = va_arg(Args, const Type*))
237 ArgTys.push_back(ArgTy);
238
239 va_end(Args);
240
241 // Build the function type and chain to the other getOrInsertFunction...
Owen Anderson785c56c2009-07-08 23:50:31 +0000242 return getOrInsertFunction(Name,
Owen Anderson4056ca92009-07-29 22:17:13 +0000243 FunctionType::get(RetTy, ArgTys, false),
Nick Lewycky3a0c1062009-01-04 22:54:40 +0000244 AttrListPtr::get((AttributeWithIndex *)0, 0));
Chris Lattnerbd717d82003-08-31 00:19:28 +0000245}
246
Chris Lattnera483b062002-03-29 03:44:18 +0000247// getFunction - Look up the specified function in the module symbol table.
248// If it does not exist, return null.
249//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000250Function *Module::getFunction(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000251 return dyn_cast_or_null<Function>(getNamedValue(Name));
Chris Lattnere43649f2008-06-27 21:09:10 +0000252}
253
Chris Lattner09bd1a02003-12-31 08:43:01 +0000254//===----------------------------------------------------------------------===//
255// Methods for easy access to the global variables in the module.
256//
257
258/// getGlobalVariable - Look up the specified global variable in the module
Chris Lattner7d4d93c2005-12-05 05:30:21 +0000259/// symbol table. If it does not exist, return null. The type argument
260/// should be the underlying type of the global, i.e., it should not have
261/// the top-level PointerType, which represents the address of the global.
Rafael Espindola6de96a12009-01-15 20:18:42 +0000262/// If AllowLocal is set to true, this function will return types that
263/// have an local. By default, these types are not returned.
Chris Lattner09bd1a02003-12-31 08:43:01 +0000264///
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000265GlobalVariable *Module::getGlobalVariable(StringRef Name,
Rafael Espindola6de96a12009-01-15 20:18:42 +0000266 bool AllowLocal) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000267 if (GlobalVariable *Result =
268 dyn_cast_or_null<GlobalVariable>(getNamedValue(Name)))
269 if (AllowLocal || !Result->hasLocalLinkage())
Chris Lattner09bd1a02003-12-31 08:43:01 +0000270 return Result;
Chris Lattner09bd1a02003-12-31 08:43:01 +0000271 return 0;
272}
273
Bill Wendlingf5f6f742008-11-05 23:42:27 +0000274/// getOrInsertGlobal - Look up the specified global in the module symbol table.
275/// 1. If it does not exist, add a declaration of the global and return it.
276/// 2. Else, the global exists but has the wrong type: return the function
277/// with a constantexpr cast to the right type.
278/// 3. Finally, if the existing global is the correct delclaration, return the
279/// existing global.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000280Constant *Module::getOrInsertGlobal(StringRef Name, const Type *Ty) {
Bill Wendling2f409562008-11-04 22:51:24 +0000281 // See if we have a definition for the specified global already.
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000282 GlobalVariable *GV = dyn_cast_or_null<GlobalVariable>(getNamedValue(Name));
Bill Wendling2f409562008-11-04 22:51:24 +0000283 if (GV == 0) {
284 // Nope, add it
285 GlobalVariable *New =
Owen Andersonb17f3292009-07-08 19:03:57 +0000286 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
287 0, Name);
288 return New; // Return the new declaration.
Bill Wendling2f409562008-11-04 22:51:24 +0000289 }
290
291 // If the variable exists but has the wrong type, return a bitcast to the
292 // right type.
Owen Anderson4056ca92009-07-29 22:17:13 +0000293 if (GV->getType() != PointerType::getUnqual(Ty))
294 return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
Bill Wendling2f409562008-11-04 22:51:24 +0000295
296 // Otherwise, we just found the existing function or a prototype.
297 return GV;
298}
299
Chris Lattner09bd1a02003-12-31 08:43:01 +0000300//===----------------------------------------------------------------------===//
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000301// Methods for easy access to the global variables in the module.
302//
303
304// getNamedAlias - Look up the specified global in the module symbol table.
305// If it does not exist, return null.
306//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000307GlobalAlias *Module::getNamedAlias(StringRef Name) const {
Daniel Dunbardcf8d3c2009-03-06 22:04:43 +0000308 return dyn_cast_or_null<GlobalAlias>(getNamedValue(Name));
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000309}
310
Devang Patel98250792009-07-30 23:59:04 +0000311/// getNamedMetadata - Return the first NamedMDNode in the module with the
312/// specified name. This method returns null if a NamedMDNode with the
313//// specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000314NamedMDNode *Module::getNamedMetadata(StringRef Name) const {
Devang Patelfcfee0f2010-01-07 19:39:36 +0000315 return NamedMDSymTab->lookup(Name);
Devang Patel18dfdc92009-07-29 17:16:17 +0000316}
317
Devang Patel98250792009-07-30 23:59:04 +0000318/// getOrInsertNamedMetadata - Return the first named MDNode in the module
319/// with the specified name. This method returns a new NamedMDNode if a
320/// NamedMDNode with the specified name is not found.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000321NamedMDNode *Module::getOrInsertNamedMetadata(StringRef Name) {
Devang Patelfcfee0f2010-01-07 19:39:36 +0000322 NamedMDNode *NMD = NamedMDSymTab->lookup(Name);
Devang Patel98250792009-07-30 23:59:04 +0000323 if (!NMD)
Owen Anderson55f1c092009-08-13 21:58:54 +0000324 NMD = NamedMDNode::Create(getContext(), Name, NULL, 0, this);
Devang Patel98250792009-07-30 23:59:04 +0000325 return NMD;
326}
327
Devang Patelfcfee0f2010-01-07 19:39:36 +0000328/// addMDNodeName - Insert an entry in the NamedMDNode symbol table mapping
329/// Name to NMD.
330void Module::addMDNodeName(StringRef Name, NamedMDNode *NMD) {
331 NamedMDSymTab->insert(Name, NMD);
332}
333
Anton Korobeynikova97b6942007-04-25 14:27:10 +0000334//===----------------------------------------------------------------------===//
Chris Lattner09bd1a02003-12-31 08:43:01 +0000335// Methods for easy access to the types in the module.
336//
337
Chris Lattner1f985e02002-11-08 20:34:02 +0000338
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000339// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
340// there is already an entry for this name, true is returned and the symbol
341// table is not modified.
342//
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000343bool Module::addTypeName(StringRef Name, const Type *Ty) {
Reid Spencer32af9e82007-01-06 07:24:44 +0000344 TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000345
Reid Spencer32af9e82007-01-06 07:24:44 +0000346 if (ST.lookup(Name)) return true; // Already in symtab...
Misha Brukmanb1c93172005-04-21 23:48:37 +0000347
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000348 // Not in symbol table? Set the name with the Symtab as an argument so the
349 // type knows what to update...
Reid Spencerf9776c32004-07-10 16:37:42 +0000350 ST.insert(Name, Ty);
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000351
352 return false;
353}
354
355/// getTypeByName - Return the type with the specified name in this module, or
356/// null if there is none by that name.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000357const Type *Module::getTypeByName(StringRef Name) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000358 const TypeSymbolTable &ST = getTypeSymbolTable();
359 return cast_or_null<Type>(ST.lookup(Name));
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000360}
Chris Lattner1f985e02002-11-08 20:34:02 +0000361
Chris Lattner10b7cb52002-04-13 18:58:33 +0000362// getTypeName - If there is at least one entry in the symbol table for the
363// specified type, return it.
364//
Chris Lattnerbe3596c2003-12-31 07:09:33 +0000365std::string Module::getTypeName(const Type *Ty) const {
Reid Spencer32af9e82007-01-06 07:24:44 +0000366 const TypeSymbolTable &ST = getTypeSymbolTable();
Chris Lattner10b7cb52002-04-13 18:58:33 +0000367
Reid Spencer32af9e82007-01-06 07:24:44 +0000368 TypeSymbolTable::const_iterator TI = ST.begin();
369 TypeSymbolTable::const_iterator TE = ST.end();
Reid Spencerabb6f002004-05-25 08:52:20 +0000370 if ( TI == TE ) return ""; // No names for types
Chris Lattner10b7cb52002-04-13 18:58:33 +0000371
Reid Spencerabb6f002004-05-25 08:52:20 +0000372 while (TI != TE && TI->second != Ty)
Chris Lattner10b7cb52002-04-13 18:58:33 +0000373 ++TI;
374
375 if (TI != TE) // Must have found an entry!
376 return TI->first;
377 return ""; // Must not have found anything...
378}
379
Chris Lattner09bd1a02003-12-31 08:43:01 +0000380//===----------------------------------------------------------------------===//
381// Other module related stuff.
382//
383
384
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000385// dropAllReferences() - This function causes all the subelementss to "let go"
386// of all references that they are maintaining. This allows one to 'delete' a
387// whole module at a time, even though there may be circular references... first
388// all references are dropped, and all use counts go to zero. Then everything
Misha Brukmanfa100532003-10-10 17:54:14 +0000389// is deleted for real. Note that no operations are valid on an object that
Chris Lattnere0f6af9b2002-08-17 23:32:47 +0000390// has "dropped all references", except operator delete.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000391//
392void Module::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000393 for(Module::iterator I = begin(), E = end(); I != E; ++I)
394 I->dropAllReferences();
Chris Lattner446ad502001-10-13 06:58:40 +0000395
Chris Lattner531f9e92005-03-15 04:54:21 +0000396 for(Module::global_iterator I = global_begin(), E = global_end(); I != E; ++I)
Chris Lattner113f4f42002-06-25 16:13:24 +0000397 I->dropAllReferences();
Anton Korobeynikovb18f8f82007-04-28 13:45:00 +0000398
399 for(Module::alias_iterator I = alias_begin(), E = alias_end(); I != E; ++I)
400 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000401}
Chris Lattner31cf9842001-06-30 04:35:40 +0000402
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000403void Module::addLibrary(StringRef Lib) {
Reid Spencer3f4e6e82007-02-04 00:40:42 +0000404 for (Module::lib_iterator I = lib_begin(), E = lib_end(); I != E; ++I)
405 if (*I == Lib)
406 return;
407 LibraryList.push_back(Lib);
408}
409
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000410void Module::removeLibrary(StringRef Lib) {
Reid Spencer3f4e6e82007-02-04 00:40:42 +0000411 LibraryListType::iterator I = LibraryList.begin();
412 LibraryListType::iterator E = LibraryList.end();
413 for (;I != E; ++I)
414 if (*I == Lib) {
415 LibraryList.erase(I);
416 return;
417 }
418}