1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
3 // This file implements the Function & GlobalVariable classes for the VMCore
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Function.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Module.h"
11 #include "llvm/GlobalVariable.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/iOther.h"
14 #include "llvm/Argument.h"
15 #include "ValueHolderImpl.h"
17 //===----------------------------------------------------------------------===//
18 // Argument Implementation
19 //===----------------------------------------------------------------------===//
21 // Specialize setName to take care of symbol table majik
22 void Argument::setName(const std::string &name, SymbolTable *ST) {
24 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
25 "Invalid symtab argument!");
26 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
28 if (P && hasName()) P->getSymbolTable()->insert(this);
33 //===----------------------------------------------------------------------===//
34 // Function Implementation
35 //===----------------------------------------------------------------------===//
38 // Instantiate Templates - This ugliness is the price we have to pay
39 // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
41 template class ValueHolder<Argument , Function, Function>;
42 template class ValueHolder<BasicBlock, Function, Function>;
44 Function::Function(const FunctionType *Ty, bool isInternal,
45 const std::string &name)
46 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name),
47 BasicBlocks(this), ArgumentList(this, this) {
48 ParentSymTab = SymTab = 0;
51 Function::~Function() {
52 dropAllReferences(); // After this it is safe to delete instructions.
54 // TODO: Should remove from the end, not the beginning of vector!
55 iterator BI = begin();
56 while ((BI = begin()) != end())
57 delete BasicBlocks.remove(BI);
59 // Delete all of the method arguments and unlink from symbol table...
60 ArgumentList.delete_all();
61 ArgumentList.setParent(0);
65 // Specialize setName to take care of symbol table majik
66 void Function::setName(const std::string &name, SymbolTable *ST) {
68 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
69 "Invalid symtab argument!");
70 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
72 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
75 void Function::setParent(Module *parent) {
78 // Relink symbol tables together...
79 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
80 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
83 const FunctionType *Function::getFunctionType() const {
84 return cast<FunctionType>(getType()->getElementType());
87 const Type *Function::getReturnType() const {
88 return getFunctionType()->getReturnType();
91 SymbolTable *Function::getSymbolTableSure() {
92 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
96 // hasSymbolTable() - Returns true if there is a symbol table allocated to
97 // this object AND if there is at least one name in it!
99 bool Function::hasSymbolTable() const {
100 if (!SymTab) return false;
102 for (SymbolTable::const_iterator I = SymTab->begin();
103 I != SymTab->end(); ++I) {
104 if (I->second.begin() != I->second.end())
105 return true; // Found nonempty type plane!
112 // dropAllReferences() - This function causes all the subinstructions to "let
113 // go" of all references that they are maintaining. This allows one to
114 // 'delete' a whole class at a time, even though there may be circular
115 // references... first all references are dropped, and all use counts go to
116 // zero. Then everything is delete'd for real. Note that no operations are
117 // valid on an object that has "dropped all references", except operator
120 void Function::dropAllReferences() {
121 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
124 //===----------------------------------------------------------------------===//
125 // GlobalVariable Implementation
126 //===----------------------------------------------------------------------===//
128 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
129 Constant *Initializer = 0,
130 const std::string &Name = "")
131 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
132 isConstantGlobal(constant) {
133 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
136 // Specialize setName to take care of symbol table majik
137 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
139 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
140 "Invalid symtab argument!");
141 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
142 Value::setName(name);
143 if (P && getName() != "") P->getSymbolTableSure()->insert(this);