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/SymbolTable.h"
11 #include "llvm/Module.h"
12 #include "llvm/GlobalVariable.h"
13 #include "llvm/BasicBlock.h"
14 #include "llvm/iOther.h"
15 #include "llvm/Argument.h"
16 #include "ValueHolderImpl.h"
18 //===----------------------------------------------------------------------===//
19 // Argument Implementation
20 //===----------------------------------------------------------------------===//
22 // Specialize setName to take care of symbol table majik
23 void Argument::setName(const std::string &name, SymbolTable *ST) {
25 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
26 "Invalid symtab argument!");
27 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
29 if (P && hasName()) P->getSymbolTable()->insert(this);
34 //===----------------------------------------------------------------------===//
35 // Function Implementation
36 //===----------------------------------------------------------------------===//
39 // Instantiate Templates - This ugliness is the price we have to pay
40 // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
42 template class ValueHolder<Argument , Function, Function>;
43 template class ValueHolder<BasicBlock, Function, Function>;
45 Function::Function(const FunctionType *Ty, bool isInternal,
46 const std::string &name)
47 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name),
48 BasicBlocks(this), ArgumentList(this, this) {
49 ParentSymTab = SymTab = 0;
52 Function::~Function() {
53 dropAllReferences(); // After this it is safe to delete instructions.
55 // TODO: Should remove from the end, not the beginning of vector!
56 iterator BI = begin();
57 while ((BI = begin()) != end())
58 delete BasicBlocks.remove(BI);
60 // Delete all of the method arguments and unlink from symbol table...
61 ArgumentList.delete_all();
62 ArgumentList.setParent(0);
66 // Specialize setName to take care of symbol table majik
67 void Function::setName(const std::string &name, SymbolTable *ST) {
69 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
70 "Invalid symtab argument!");
71 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
73 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
76 void Function::setParent(Module *parent) {
79 // Relink symbol tables together...
80 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
81 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
84 const FunctionType *Function::getFunctionType() const {
85 return cast<FunctionType>(getType()->getElementType());
88 const Type *Function::getReturnType() const {
89 return getFunctionType()->getReturnType();
92 SymbolTable *Function::getSymbolTableSure() {
93 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
97 // hasSymbolTable() - Returns true if there is a symbol table allocated to
98 // this object AND if there is at least one name in it!
100 bool Function::hasSymbolTable() const {
101 if (!SymTab) return false;
103 for (SymbolTable::const_iterator I = SymTab->begin();
104 I != SymTab->end(); ++I) {
105 if (I->second.begin() != I->second.end())
106 return true; // Found nonempty type plane!
113 // dropAllReferences() - This function causes all the subinstructions to "let
114 // go" of all references that they are maintaining. This allows one to
115 // 'delete' a whole class at a time, even though there may be circular
116 // references... first all references are dropped, and all use counts go to
117 // zero. Then everything is delete'd for real. Note that no operations are
118 // valid on an object that has "dropped all references", except operator
121 void Function::dropAllReferences() {
122 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
125 //===----------------------------------------------------------------------===//
126 // GlobalVariable Implementation
127 //===----------------------------------------------------------------------===//
129 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
130 Constant *Initializer = 0,
131 const std::string &Name = "")
132 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
133 isConstantGlobal(constant) {
134 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
137 // Specialize setName to take care of symbol table majik
138 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
140 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
141 "Invalid symtab argument!");
142 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
143 Value::setName(name);
144 if (P && getName() != "") P->getSymbolTableSure()->insert(this);