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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-04-08 00:15:29 +00:00
|
|
|
#include "llvm/Function.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
|
|
|
#include "llvm/Module.h"
|
2001-09-10 07:58:01 +00:00
|
|
|
#include "llvm/GlobalVariable.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/iOther.h"
|
2002-04-08 00:15:29 +00:00
|
|
|
#include "ValueHolderImpl.h"
|
2001-06-06 20:29:01 +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
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
// Instantiate Templates - This ugliness is the price we have to pay
|
|
|
|
// for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
|
|
|
|
//
|
2002-03-26 18:01:55 +00:00
|
|
|
template class ValueHolder<FunctionArgument, Function, Function>;
|
|
|
|
template class ValueHolder<BasicBlock , Function, Function>;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2002-03-29 03:44:36 +00:00
|
|
|
Function::Function(const FunctionType *Ty, bool isInternal,
|
2002-03-23 22:51:58 +00:00
|
|
|
const std::string &name)
|
2002-03-26 18:01:55 +00:00
|
|
|
: GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name),
|
2001-11-08 04:38:58 +00:00
|
|
|
SymTabValue(this), BasicBlocks(this), ArgumentList(this, 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.
|
|
|
|
|
|
|
|
// TODO: Should remove from the end, not the beginning of vector!
|
2001-06-27 23:41:11 +00:00
|
|
|
iterator BI = begin();
|
|
|
|
while ((BI = begin()) != end())
|
2001-06-06 20:29:01 +00:00
|
|
|
delete BasicBlocks.remove(BI);
|
|
|
|
|
|
|
|
// Delete all of the method arguments and unlink from symbol table...
|
|
|
|
ArgumentList.delete_all();
|
|
|
|
ArgumentList.setParent(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2001-06-06 20:29:01 +00:00
|
|
|
Parent = parent;
|
|
|
|
|
|
|
|
// Relink symbol tables together...
|
|
|
|
setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
2001-06-27 23:41:11 +00:00
|
|
|
for_each(begin(), end(), std::mem_fun(&BasicBlock::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,
|
|
|
|
Constant *Initializer = 0,
|
2002-01-20 22:54:45 +00:00
|
|
|
const std::string &Name = "")
|
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));
|
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);
|
|
|
|
}
|