1 //===-- Method.cpp - Implement the Method class ------------------*- C++ -*--=//
3 // This file implements the Method & GlobalVariable classes for the VMCore
6 //===----------------------------------------------------------------------===//
8 #include "llvm/ValueHolderImpl.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/Module.h"
12 #include "llvm/Method.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/iOther.h"
17 //===----------------------------------------------------------------------===//
18 // Method Implementation
19 //===----------------------------------------------------------------------===//
22 // Instantiate Templates - This ugliness is the price we have to pay
23 // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
25 template class ValueHolder<MethodArgument, Method, Method>;
26 template class ValueHolder<BasicBlock , Method, Method>;
28 Function::Function(const MethodType *Ty, bool isInternal,
29 const std::string &name)
30 : GlobalValue(PointerType::get(Ty), Value::MethodVal, isInternal, name),
31 SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
32 assert(::isa<MethodType>(Ty) && "Method signature must be of method type!");
35 Function::~Function() {
36 dropAllReferences(); // After this it is safe to delete instructions.
38 // TODO: Should remove from the end, not the beginning of vector!
39 iterator BI = begin();
40 while ((BI = begin()) != end())
41 delete BasicBlocks.remove(BI);
43 // Delete all of the method arguments and unlink from symbol table...
44 ArgumentList.delete_all();
45 ArgumentList.setParent(0);
48 // Specialize setName to take care of symbol table majik
49 void Function::setName(const std::string &name, SymbolTable *ST) {
51 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
52 "Invalid symtab argument!");
53 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
55 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
58 void Function::setParent(Module *parent) {
61 // Relink symbol tables together...
62 setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
65 const MethodType *Function::getMethodType() const {
66 return cast<MethodType>(cast<PointerType>(getType())->getElementType());
69 const Type *Function::getReturnType() const {
70 return getMethodType()->getReturnType();
73 // dropAllReferences() - This function causes all the subinstructions to "let
74 // go" of all references that they are maintaining. This allows one to
75 // 'delete' a whole class at a time, even though there may be circular
76 // references... first all references are dropped, and all use counts go to
77 // zero. Then everything is delete'd for real. Note that no operations are
78 // valid on an object that has "dropped all references", except operator
81 void Function::dropAllReferences() {
82 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
85 //===----------------------------------------------------------------------===//
86 // GlobalVariable Implementation
87 //===----------------------------------------------------------------------===//
89 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
90 Constant *Initializer = 0,
91 const std::string &Name = "")
92 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
93 isConstantGlobal(constant) {
94 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
97 // Specialize setName to take care of symbol table majik
98 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
100 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
101 "Invalid symtab argument!");
102 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
103 Value::setName(name);
104 if (P && getName() != "") P->getSymbolTableSure()->insert(this);