d7e8bea9ddcec20f1325b8b2cb1219cd93a87abf
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
2 //
3 // This file implements the Function & GlobalVariable classes for the VMCore
4 // library.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Module.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/iOther.h"
11 #include "Support/LeakDetector.h"
12 #include "SymbolTableListTraitsImpl.h"
13
14 BasicBlock *ilist_traits<BasicBlock>::createNode() {
15   BasicBlock *Ret = new BasicBlock();
16   // This should not be garbage monitored.
17   LeakDetector::removeGarbageObject(Ret);
18   return Ret;
19 }
20
21 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
22   return F->getBasicBlockList();
23 }
24
25 Argument *ilist_traits<Argument>::createNode() {
26   Argument *Ret = new Argument(Type::IntTy);
27   // This should not be garbage monitored.
28   LeakDetector::removeGarbageObject(Ret);
29   return Ret;
30 }
31
32 iplist<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...
38 template SymbolTableListTraits<Argument, Function, Function>;
39 template SymbolTableListTraits<BasicBlock, Function, Function>;
40
41 //===----------------------------------------------------------------------===//
42 // Argument Implementation
43 //===----------------------------------------------------------------------===//
44
45 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par) 
46   : Value(Ty, Value::ArgumentVal, Name) {
47   Parent = 0;
48
49   // Make sure that we get added to a function
50   LeakDetector::addGarbageObject(this);
51
52   if (Par)
53     Par->getArgumentList().push_back(this);
54 }
55
56
57 // Specialize setName to take care of symbol table majik
58 void 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
67 void Argument::setParent(Function *parent) {
68   if (getParent())
69     LeakDetector::addGarbageObject(this);
70   Parent = parent;
71   if (getParent())
72     LeakDetector::removeGarbageObject(this);
73 }
74
75
76 //===----------------------------------------------------------------------===//
77 // Function Implementation
78 //===----------------------------------------------------------------------===//
79
80 Function::Function(const FunctionType *Ty, bool isInternal,
81                    const std::string &name, Module *ParentModule)
82   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
83   BasicBlocks.setItemParent(this);
84   BasicBlocks.setParent(this);
85   ArgumentList.setItemParent(this);
86   ArgumentList.setParent(this);
87   ParentSymTab = SymTab = 0;
88
89   // Make sure that we get added to a function
90   LeakDetector::addGarbageObject(this);
91
92   if (ParentModule)
93     ParentModule->getFunctionList().push_back(this);
94 }
95
96 Function::~Function() {
97   dropAllReferences();    // After this it is safe to delete instructions.
98
99   BasicBlocks.clear();    // Delete all basic blocks...
100
101   // Delete all of the method arguments and unlink from symbol table...
102   ArgumentList.clear();
103   ArgumentList.setParent(0);
104   delete SymTab;
105 }
106
107 // Specialize setName to take care of symbol table majik
108 void Function::setName(const std::string &name, SymbolTable *ST) {
109   Module *P;
110   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
111          "Invalid symtab argument!");
112   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
113   Value::setName(name);
114   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
115 }
116
117 void Function::setParent(Module *parent) {
118   if (getParent())
119     LeakDetector::addGarbageObject(this);
120   Parent = parent;
121   if (getParent())
122     LeakDetector::removeGarbageObject(this);
123
124   // Relink symbol tables together...
125   ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
126   if (SymTab) SymTab->setParentSymTab(ParentSymTab);
127 }
128
129 const FunctionType *Function::getFunctionType() const {
130   return cast<FunctionType>(getType()->getElementType());
131 }
132
133 const Type *Function::getReturnType() const { 
134   return getFunctionType()->getReturnType();
135 }
136
137 SymbolTable *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 //
145 bool 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
158 // 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 //
166 void Function::dropAllReferences() {
167   for (iterator I = begin(), E = end(); I != E; ++I)
168     I->dropAllReferences();
169 }
170
171 //===----------------------------------------------------------------------===//
172 // GlobalVariable Implementation
173 //===----------------------------------------------------------------------===//
174
175 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
176                                Constant *Initializer,
177                                const std::string &Name, Module *ParentModule)
178   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
179     isConstantGlobal(constant) {
180   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
181
182   LeakDetector::addGarbageObject(this);
183
184   if (ParentModule)
185     ParentModule->getGlobalList().push_back(this);
186 }
187
188 void GlobalVariable::setParent(Module *parent) {
189   if (getParent())
190     LeakDetector::addGarbageObject(this);
191   Parent = parent;
192   if (getParent())
193     LeakDetector::removeGarbageObject(this);
194 }
195
196 // Specialize setName to take care of symbol table majik
197 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
198   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 }