2002-03-26 18:01:55 +00:00
|
|
|
//===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2002-03-26 18:01:55 +00:00
|
|
|
// This file implements the Function & GlobalVariable classes for the VMCore
|
2001-09-10 07:58:01 +00:00
|
|
|
// library.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Module.h"
|
2002-09-06 20:46:32 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/iOther.h"
|
2002-09-08 18:59:35 +00:00
|
|
|
#include "Support/LeakDetector.h"
|
2002-06-25 16:13:24 +00:00
|
|
|
#include "SymbolTableListTraitsImpl.h"
|
|
|
|
|
2002-09-06 21:33:15 +00:00
|
|
|
BasicBlock *ilist_traits<BasicBlock>::createNode() {
|
2002-09-08 18:59:35 +00:00
|
|
|
BasicBlock *Ret = new BasicBlock();
|
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-09-06 21:33:15 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
|
|
|
|
return F->getBasicBlockList();
|
|
|
|
}
|
|
|
|
|
|
|
|
Argument *ilist_traits<Argument>::createNode() {
|
2002-09-08 18:59:35 +00:00
|
|
|
Argument *Ret = new Argument(Type::IntTy);
|
|
|
|
// This should not be garbage monitored.
|
|
|
|
LeakDetector::removeGarbageObject(Ret);
|
|
|
|
return Ret;
|
2002-06-25 16:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
|
|
|
|
return F->getArgumentList();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit instantiations of SymbolTableListTraits since some of the methods
|
|
|
|
// are not in the public header file...
|
|
|
|
template SymbolTableListTraits<Argument, Function, Function>;
|
|
|
|
template SymbolTableListTraits<BasicBlock, Function, Function>;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-04-09 19:39:35 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Argument Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-09-06 21:33:15 +00:00
|
|
|
Argument::Argument(const Type *Ty, const std::string &Name = "", Function *Par)
|
|
|
|
: Value(Ty, Value::ArgumentVal, Name) {
|
|
|
|
Parent = 0;
|
2002-09-08 18:59:35 +00:00
|
|
|
|
|
|
|
// Make sure that we get added to a function
|
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-06 21:33:15 +00:00
|
|
|
if (Par)
|
|
|
|
Par->getArgumentList().push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-09 19:39:35 +00:00
|
|
|
// Specialize setName to take care of symbol table majik
|
|
|
|
void Argument::setName(const std::string &name, SymbolTable *ST) {
|
|
|
|
Function *P;
|
|
|
|
assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
|
|
|
|
"Invalid symtab argument!");
|
|
|
|
if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
|
|
|
|
Value::setName(name);
|
|
|
|
if (P && hasName()) P->getSymbolTable()->insert(this);
|
|
|
|
}
|
|
|
|
|
2002-09-06 21:33:15 +00:00
|
|
|
void Argument::setParent(Function *parent) {
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2002-09-06 21:33:15 +00:00
|
|
|
Parent = parent;
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2002-09-06 21:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-10 07:58:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-03-26 18:01:55 +00:00
|
|
|
// Function Implementation
|
2001-09-10 07:58:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-03-29 03:44:36 +00:00
|
|
|
Function::Function(const FunctionType *Ty, bool isInternal,
|
2002-09-06 20:46:32 +00:00
|
|
|
const std::string &name, Module *ParentModule)
|
2002-06-25 16:13:24 +00:00
|
|
|
: GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
|
|
|
|
BasicBlocks.setItemParent(this);
|
|
|
|
BasicBlocks.setParent(this);
|
|
|
|
ArgumentList.setItemParent(this);
|
|
|
|
ArgumentList.setParent(this);
|
2002-04-28 04:51:51 +00:00
|
|
|
ParentSymTab = SymTab = 0;
|
2002-09-06 20:46:32 +00:00
|
|
|
|
2002-09-08 18:59:35 +00:00
|
|
|
// Make sure that we get added to a function
|
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-06 20:46:32 +00:00
|
|
|
if (ParentModule)
|
|
|
|
ParentModule->getFunctionList().push_back(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 22:51:58 +00:00
|
|
|
Function::~Function() {
|
2001-06-06 20:29:01 +00:00
|
|
|
dropAllReferences(); // After this it is safe to delete instructions.
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlocks.clear(); // Delete all basic blocks...
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Delete all of the method arguments and unlink from symbol table...
|
2002-06-25 16:13:24 +00:00
|
|
|
ArgumentList.clear();
|
2001-06-06 20:29:01 +00:00
|
|
|
ArgumentList.setParent(0);
|
2002-04-28 04:51:51 +00:00
|
|
|
delete SymTab;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
2002-03-23 22:51:58 +00:00
|
|
|
void Function::setName(const std::string &name, SymbolTable *ST) {
|
2001-06-06 20:29:01 +00:00
|
|
|
Module *P;
|
2001-09-07 16:47:18 +00:00
|
|
|
assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
|
|
|
|
"Invalid symtab argument!");
|
2001-06-06 20:29:01 +00:00
|
|
|
if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
|
|
|
|
Value::setName(name);
|
|
|
|
if (P && getName() != "") P->getSymbolTableSure()->insert(this);
|
|
|
|
}
|
|
|
|
|
2002-03-23 22:51:58 +00:00
|
|
|
void Function::setParent(Module *parent) {
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
Parent = parent;
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
// Relink symbol tables together...
|
2002-04-28 04:51:51 +00:00
|
|
|
ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
|
|
|
|
if (SymTab) SymTab->setParentSymTab(ParentSymTab);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-03-29 03:44:36 +00:00
|
|
|
const FunctionType *Function::getFunctionType() const {
|
|
|
|
return cast<FunctionType>(getType()->getElementType());
|
2001-10-03 14:53:21 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 22:51:58 +00:00
|
|
|
const Type *Function::getReturnType() const {
|
2002-03-29 03:44:36 +00:00
|
|
|
return getFunctionType()->getReturnType();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-04-28 04:51:51 +00:00
|
|
|
SymbolTable *Function::getSymbolTableSure() {
|
|
|
|
if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
|
|
|
|
return SymTab;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasSymbolTable() - Returns true if there is a symbol table allocated to
|
|
|
|
// this object AND if there is at least one name in it!
|
|
|
|
//
|
|
|
|
bool Function::hasSymbolTable() const {
|
|
|
|
if (!SymTab) return false;
|
|
|
|
|
|
|
|
for (SymbolTable::const_iterator I = SymTab->begin();
|
|
|
|
I != SymTab->end(); ++I) {
|
|
|
|
if (I->second.begin() != I->second.end())
|
|
|
|
return true; // Found nonempty type plane!
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// dropAllReferences() - This function causes all the subinstructions to "let
|
|
|
|
// go" of all references that they are maintaining. This allows one to
|
|
|
|
// 'delete' a whole class at a time, even though there may be circular
|
|
|
|
// references... first all references are dropped, and all use counts go to
|
|
|
|
// zero. Then everything is delete'd for real. Note that no operations are
|
|
|
|
// valid on an object that has "dropped all references", except operator
|
|
|
|
// delete.
|
|
|
|
//
|
2002-03-23 22:51:58 +00:00
|
|
|
void Function::dropAllReferences() {
|
2002-06-25 16:13:24 +00:00
|
|
|
for (iterator I = begin(), E = end(); I != E; ++I)
|
|
|
|
I->dropAllReferences();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-09-10 07:58:01 +00:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// GlobalVariable Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
|
2002-07-24 22:08:53 +00:00
|
|
|
Constant *Initializer,
|
2002-09-06 21:33:15 +00:00
|
|
|
const std::string &Name, Module *ParentModule)
|
2001-11-26 19:14:56 +00:00
|
|
|
: GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
|
2001-12-03 22:26:30 +00:00
|
|
|
isConstantGlobal(constant) {
|
2001-09-18 04:01:05 +00:00
|
|
|
if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
|
2002-09-06 21:33:15 +00:00
|
|
|
|
2002-09-08 18:59:35 +00:00
|
|
|
LeakDetector::addGarbageObject(this);
|
|
|
|
|
2002-09-06 21:33:15 +00:00
|
|
|
if (ParentModule)
|
|
|
|
ParentModule->getGlobalList().push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalVariable::setParent(Module *parent) {
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::addGarbageObject(this);
|
2002-09-06 21:33:15 +00:00
|
|
|
Parent = parent;
|
2002-09-08 18:59:35 +00:00
|
|
|
if (getParent())
|
|
|
|
LeakDetector::removeGarbageObject(this);
|
2001-09-10 07:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
2002-01-20 22:54:45 +00:00
|
|
|
void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
|
2001-09-10 07:58:01 +00:00
|
|
|
Module *P;
|
|
|
|
assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
|
|
|
|
"Invalid symtab argument!");
|
|
|
|
if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
|
|
|
|
Value::setName(name);
|
|
|
|
if (P && getName() != "") P->getSymbolTableSure()->insert(this);
|
|
|
|
}
|