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, LinkageTypes Linkage,
81 const std::string &name, Module *ParentModule)
82 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
83 BasicBlocks.setItemParent(this);
84 BasicBlocks.setParent(this);
85 ArgumentList.setItemParent(this);
86 ArgumentList.setParent(this);
87 SymTab = new SymbolTable();
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)));
96 // Make sure that we get added to a function
97 LeakDetector::addGarbageObject(this);
100 ParentModule->getFunctionList().push_back(this);
103 Function::~Function() {
104 dropAllReferences(); // After this it is safe to delete instructions.
106 BasicBlocks.clear(); // Delete all basic blocks...
108 // Delete all of the method arguments and unlink from symbol table...
109 ArgumentList.clear();
110 ArgumentList.setParent(0);
114 // Specialize setName to take care of symbol table majik
115 void Function::setName(const std::string &name, SymbolTable *ST) {
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->getSymbolTable().insert(this);
124 void Function::setParent(Module *parent) {
126 LeakDetector::addGarbageObject(this);
129 LeakDetector::removeGarbageObject(this);
132 const FunctionType *Function::getFunctionType() const {
133 return cast<FunctionType>(getType()->getElementType());
136 const Type *Function::getReturnType() const {
137 return getFunctionType()->getReturnType();
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
148 void Function::dropAllReferences() {
149 for (iterator I = begin(), E = end(); I != E; ++I)
150 I->dropAllReferences();
153 //===----------------------------------------------------------------------===//
154 // GlobalVariable Implementation
155 //===----------------------------------------------------------------------===//
157 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
158 Constant *Initializer,
159 const std::string &Name, Module *ParentModule)
160 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
161 isConstantGlobal(constant) {
162 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
164 LeakDetector::addGarbageObject(this);
167 ParentModule->getGlobalList().push_back(this);
170 void GlobalVariable::setParent(Module *parent) {
172 LeakDetector::addGarbageObject(this);
175 LeakDetector::removeGarbageObject(this);
178 // Specialize setName to take care of symbol table majik
179 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
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->getSymbolTable().insert(this);