blob: d7e8bea9ddcec20f1325b8b2cb1219cd93a87abf [file] [log] [blame]
Chris Lattner57698e22002-03-26 18:01:55 +00001//===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner57698e22002-03-26 18:01:55 +00003// This file implements the Function & GlobalVariable classes for the VMCore
Chris Lattnerda975502001-09-10 07:58:01 +00004// library.
Chris Lattner2f7c9632001-06-06 20:29:01 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattner2f7c9632001-06-06 20:29:01 +00008#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +00009#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/iOther.h"
Chris Lattner184b2982002-09-08 18:59:35 +000011#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000012#include "SymbolTableListTraitsImpl.h"
13
Chris Lattner9ed7aef2002-09-06 21:33:15 +000014BasicBlock *ilist_traits<BasicBlock>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000015 BasicBlock *Ret = new BasicBlock();
16 // This should not be garbage monitored.
17 LeakDetector::removeGarbageObject(Ret);
18 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000019}
20
Chris Lattner113f4f42002-06-25 16:13:24 +000021iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
22 return F->getBasicBlockList();
23}
24
25Argument *ilist_traits<Argument>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000026 Argument *Ret = new Argument(Type::IntTy);
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
29 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000030}
31
32iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
33 return F->getArgumentList();
34}
35
36// Explicit instantiations of SymbolTableListTraits since some of the methods
37// are not in the public header file...
38template SymbolTableListTraits<Argument, Function, Function>;
39template SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000040
Chris Lattnerda975502001-09-10 07:58:01 +000041//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000042// Argument Implementation
43//===----------------------------------------------------------------------===//
44
Vikram S. Adve0267d0c2002-09-17 01:17:57 +000045Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner9ed7aef2002-09-06 21:33:15 +000046 : Value(Ty, Value::ArgumentVal, Name) {
47 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000048
49 // Make sure that we get added to a function
50 LeakDetector::addGarbageObject(this);
51
Chris Lattner9ed7aef2002-09-06 21:33:15 +000052 if (Par)
53 Par->getArgumentList().push_back(this);
54}
55
56
Chris Lattnerd255ae22002-04-09 19:39:35 +000057// Specialize setName to take care of symbol table majik
58void Argument::setName(const std::string &name, SymbolTable *ST) {
59 Function *P;
60 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
61 "Invalid symtab argument!");
62 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
63 Value::setName(name);
64 if (P && hasName()) P->getSymbolTable()->insert(this);
65}
66
Chris Lattner9ed7aef2002-09-06 21:33:15 +000067void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000068 if (getParent())
69 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000070 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000071 if (getParent())
72 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000073}
74
75
Chris Lattnerd255ae22002-04-09 19:39:35 +000076//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000077// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000078//===----------------------------------------------------------------------===//
79
Chris Lattner91db5822002-03-29 03:44:36 +000080Function::Function(const FunctionType *Ty, bool isInternal,
Chris Lattner6213ae02002-09-06 20:46:32 +000081 const std::string &name, Module *ParentModule)
Chris Lattner113f4f42002-06-25 16:13:24 +000082 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
83 BasicBlocks.setItemParent(this);
84 BasicBlocks.setParent(this);
85 ArgumentList.setItemParent(this);
86 ArgumentList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000087 ParentSymTab = SymTab = 0;
Chris Lattner6213ae02002-09-06 20:46:32 +000088
Chris Lattner184b2982002-09-08 18:59:35 +000089 // Make sure that we get added to a function
90 LeakDetector::addGarbageObject(this);
91
Chris Lattner6213ae02002-09-06 20:46:32 +000092 if (ParentModule)
93 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000094}
95
Chris Lattner4e8c4872002-03-23 22:51:58 +000096Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000097 dropAllReferences(); // After this it is safe to delete instructions.
98
Chris Lattner113f4f42002-06-25 16:13:24 +000099 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100
101 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000103 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000104 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105}
106
107// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000108void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000109 Module *P;
Chris Lattner5c764a52001-09-07 16:47:18 +0000110 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
111 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
113 Value::setName(name);
114 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
115}
116
Chris Lattner4e8c4872002-03-23 22:51:58 +0000117void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000118 if (getParent())
119 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000121 if (getParent())
122 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000123
124 // Relink symbol tables together...
Chris Lattner2c8ff632002-04-28 04:51:51 +0000125 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
126 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127}
128
Chris Lattner91db5822002-03-29 03:44:36 +0000129const FunctionType *Function::getFunctionType() const {
130 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000131}
132
Chris Lattner4e8c4872002-03-23 22:51:58 +0000133const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000134 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000135}
136
Chris Lattner2c8ff632002-04-28 04:51:51 +0000137SymbolTable *Function::getSymbolTableSure() {
138 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
139 return SymTab;
140}
141
142// hasSymbolTable() - Returns true if there is a symbol table allocated to
143// this object AND if there is at least one name in it!
144//
145bool Function::hasSymbolTable() const {
146 if (!SymTab) return false;
147
148 for (SymbolTable::const_iterator I = SymTab->begin();
149 I != SymTab->end(); ++I) {
150 if (I->second.begin() != I->second.end())
151 return true; // Found nonempty type plane!
152 }
153
154 return false;
155}
156
157
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158// dropAllReferences() - This function causes all the subinstructions to "let
159// go" of all references that they are maintaining. This allows one to
160// 'delete' a whole class at a time, even though there may be circular
161// references... first all references are dropped, and all use counts go to
162// zero. Then everything is delete'd for real. Note that no operations are
163// valid on an object that has "dropped all references", except operator
164// delete.
165//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000166void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000167 for (iterator I = begin(), E = end(); I != E; ++I)
168 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000169}
Chris Lattnerda975502001-09-10 07:58:01 +0000170
171//===----------------------------------------------------------------------===//
172// GlobalVariable Implementation
173//===----------------------------------------------------------------------===//
174
Chris Lattner3462ae32001-12-03 22:26:30 +0000175GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000176 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000177 const std::string &Name, Module *ParentModule)
Chris Lattner7a787222001-11-26 19:14:56 +0000178 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000179 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000180 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000181
Chris Lattner184b2982002-09-08 18:59:35 +0000182 LeakDetector::addGarbageObject(this);
183
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000184 if (ParentModule)
185 ParentModule->getGlobalList().push_back(this);
186}
187
188void GlobalVariable::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000189 if (getParent())
190 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000191 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000192 if (getParent())
193 LeakDetector::removeGarbageObject(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000194}
195
196// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000197void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000198 Module *P;
199 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
200 "Invalid symtab argument!");
201 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
202 Value::setName(name);
203 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
204}