1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
3 // This file implements the Function & GlobalVariable classes for the VMCore
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Module.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/iOther.h"
11 #include "Support/LeakDetector.h"
12 #include "SymbolTableListTraitsImpl.h"
14 BasicBlock *ilist_traits<BasicBlock>::createNode() {
15 BasicBlock *Ret = new BasicBlock();
16 // This should not be garbage monitored.
17 LeakDetector::removeGarbageObject(Ret);
21 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
22 return F->getBasicBlockList();
25 Argument *ilist_traits<Argument>::createNode() {
26 Argument *Ret = new Argument(Type::IntTy);
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
32 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
33 return F->getArgumentList();
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>;
41 //===----------------------------------------------------------------------===//
42 // Argument Implementation
43 //===----------------------------------------------------------------------===//
45 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
46 : Value(Ty, Value::ArgumentVal, Name) {
49 // Make sure that we get added to a function
50 LeakDetector::addGarbageObject(this);
53 Par->getArgumentList().push_back(this);
57 // Specialize setName to take care of symbol table majik
58 void Argument::setName(const std::string &name, SymbolTable *ST) {
60 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
61 "Invalid symtab argument!");
62 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
64 if (P && hasName()) P->getSymbolTable()->insert(this);
67 void Argument::setParent(Function *parent) {
69 LeakDetector::addGarbageObject(this);
72 LeakDetector::removeGarbageObject(this);
76 //===----------------------------------------------------------------------===//
77 // Function Implementation
78 //===----------------------------------------------------------------------===//
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;
89 // Make sure that we get added to a function
90 LeakDetector::addGarbageObject(this);
93 ParentModule->getFunctionList().push_back(this);
96 Function::~Function() {
97 dropAllReferences(); // After this it is safe to delete instructions.
99 BasicBlocks.clear(); // Delete all basic blocks...
101 // Delete all of the method arguments and unlink from symbol table...
102 ArgumentList.clear();
103 ArgumentList.setParent(0);
107 // Specialize setName to take care of symbol table majik
108 void Function::setName(const std::string &name, SymbolTable *ST) {
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);
117 void Function::setParent(Module *parent) {
119 LeakDetector::addGarbageObject(this);
122 LeakDetector::removeGarbageObject(this);
124 // Relink symbol tables together...
125 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
126 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
129 const FunctionType *Function::getFunctionType() const {
130 return cast<FunctionType>(getType()->getElementType());
133 const Type *Function::getReturnType() const {
134 return getFunctionType()->getReturnType();
137 SymbolTable *Function::getSymbolTableSure() {
138 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
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!
145 bool Function::hasSymbolTable() const {
146 if (!SymTab) return false;
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!
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
166 void Function::dropAllReferences() {
167 for (iterator I = begin(), E = end(); I != E; ++I)
168 I->dropAllReferences();
171 //===----------------------------------------------------------------------===//
172 // GlobalVariable Implementation
173 //===----------------------------------------------------------------------===//
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));
182 LeakDetector::addGarbageObject(this);
185 ParentModule->getGlobalList().push_back(this);
188 void GlobalVariable::setParent(Module *parent) {
190 LeakDetector::addGarbageObject(this);
193 LeakDetector::removeGarbageObject(this);
196 // Specialize setName to take care of symbol table majik
197 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
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);