Eliminate the concept of a deferred symbol table. The optimization really isn't,
[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 = new SymbolTable();
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 // dropAllReferences() - This function causes all the subinstructions to "let
141 // go" of all references that they are maintaining.  This allows one to
142 // 'delete' a whole class at a time, even though there may be circular
143 // references... first all references are dropped, and all use counts go to
144 // zero.  Then everything is delete'd for real.  Note that no operations are
145 // valid on an object that has "dropped all references", except operator 
146 // delete.
147 //
148 void Function::dropAllReferences() {
149   for (iterator I = begin(), E = end(); I != E; ++I)
150     I->dropAllReferences();
151 }
152
153 //===----------------------------------------------------------------------===//
154 // GlobalVariable Implementation
155 //===----------------------------------------------------------------------===//
156
157 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
158                                Constant *Initializer,
159                                const std::string &Name, Module *ParentModule)
160   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
161     isConstantGlobal(constant) {
162   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
163
164   LeakDetector::addGarbageObject(this);
165
166   if (ParentModule)
167     ParentModule->getGlobalList().push_back(this);
168 }
169
170 void GlobalVariable::setParent(Module *parent) {
171   if (getParent())
172     LeakDetector::addGarbageObject(this);
173   Parent = parent;
174   if (getParent())
175     LeakDetector::removeGarbageObject(this);
176 }
177
178 // Specialize setName to take care of symbol table majik
179 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
180   Module *P;
181   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
182          "Invalid symtab argument!");
183   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
184   Value::setName(name);
185   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
186 }