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 SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
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);
64 // Specialize setName to take care of symbol table majik
65 void Function::setName(const std::string &name, SymbolTable *ST) {
67 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
68 "Invalid symtab argument!");
69 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
71 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
74 void Function::setParent(Module *parent) {
77 // Relink symbol tables together...
78 setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
81 const FunctionType *Function::getFunctionType() const {
82 return cast<FunctionType>(getType()->getElementType());
85 const Type *Function::getReturnType() const {
86 return getFunctionType()->getReturnType();
89 // dropAllReferences() - This function causes all the subinstructions to "let
90 // go" of all references that they are maintaining. This allows one to
91 // 'delete' a whole class at a time, even though there may be circular
92 // references... first all references are dropped, and all use counts go to
93 // zero. Then everything is delete'd for real. Note that no operations are
94 // valid on an object that has "dropped all references", except operator
97 void Function::dropAllReferences() {
98 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
101 //===----------------------------------------------------------------------===//
102 // GlobalVariable Implementation
103 //===----------------------------------------------------------------------===//
105 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
106 Constant *Initializer = 0,
107 const std::string &Name = "")
108 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
109 isConstantGlobal(constant) {
110 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
113 // Specialize setName to take care of symbol table majik
114 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
116 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
117 "Invalid symtab argument!");
118 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
119 Value::setName(name);
120 if (P && getName() != "") P->getSymbolTableSure()->insert(this);