Start using the new function cloning header
[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->getSymbolTableSure()->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   SymTab = 0;
88
89   // Create the arguments vector, all arguments start out unnamed.
90   for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
91     assert(Ty->getParamType(i) != Type::VoidTy &&
92            "Cannot have void typed arguments!");
93     ArgumentList.push_back(new Argument(Ty->getParamType(i)));
94   }
95
96   // Make sure that we get added to a function
97   LeakDetector::addGarbageObject(this);
98
99   if (ParentModule)
100     ParentModule->getFunctionList().push_back(this);
101 }
102
103 Function::~Function() {
104   dropAllReferences();    // After this it is safe to delete instructions.
105
106   BasicBlocks.clear();    // Delete all basic blocks...
107
108   // Delete all of the method arguments and unlink from symbol table...
109   ArgumentList.clear();
110   ArgumentList.setParent(0);
111   delete SymTab;
112 }
113
114 // Specialize setName to take care of symbol table majik
115 void Function::setName(const std::string &name, SymbolTable *ST) {
116   Module *P;
117   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
118          "Invalid symtab argument!");
119   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
120   Value::setName(name);
121   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
122 }
123
124 void Function::setParent(Module *parent) {
125   if (getParent())
126     LeakDetector::addGarbageObject(this);
127   Parent = parent;
128   if (getParent())
129     LeakDetector::removeGarbageObject(this);
130 }
131
132 const FunctionType *Function::getFunctionType() const {
133   return cast<FunctionType>(getType()->getElementType());
134 }
135
136 const Type *Function::getReturnType() const { 
137   return getFunctionType()->getReturnType();
138 }
139
140 SymbolTable *Function::getSymbolTableSure() {
141   if (!SymTab) SymTab = new SymbolTable();
142   return SymTab;
143 }
144
145 // hasSymbolTable() - Returns true if there is a symbol table allocated to
146 // this object AND if there is at least one name in it!
147 //
148 bool Function::hasSymbolTable() const {
149   if (!SymTab) return false;
150
151   for (SymbolTable::const_iterator I = SymTab->begin(); 
152        I != SymTab->end(); ++I) {
153     if (I->second.begin() != I->second.end())
154       return true;                                // Found nonempty type plane!
155   }
156   
157   return false;
158 }
159
160
161 // dropAllReferences() - This function causes all the subinstructions to "let
162 // go" of all references that they are maintaining.  This allows one to
163 // 'delete' a whole class at a time, even though there may be circular
164 // references... first all references are dropped, and all use counts go to
165 // zero.  Then everything is delete'd for real.  Note that no operations are
166 // valid on an object that has "dropped all references", except operator 
167 // delete.
168 //
169 void Function::dropAllReferences() {
170   for (iterator I = begin(), E = end(); I != E; ++I)
171     I->dropAllReferences();
172 }
173
174 //===----------------------------------------------------------------------===//
175 // GlobalVariable Implementation
176 //===----------------------------------------------------------------------===//
177
178 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
179                                Constant *Initializer,
180                                const std::string &Name, Module *ParentModule)
181   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
182     isConstantGlobal(constant) {
183   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
184
185   LeakDetector::addGarbageObject(this);
186
187   if (ParentModule)
188     ParentModule->getGlobalList().push_back(this);
189 }
190
191 void GlobalVariable::setParent(Module *parent) {
192   if (getParent())
193     LeakDetector::addGarbageObject(this);
194   Parent = parent;
195   if (getParent())
196     LeakDetector::removeGarbageObject(this);
197 }
198
199 // Specialize setName to take care of symbol table majik
200 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
201   Module *P;
202   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
203          "Invalid symtab argument!");
204   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
205   Value::setName(name);
206   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
207 }