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 Method::Method(const MethodType *Ty, bool isInternal, const string &name)
29 : GlobalValue(PointerType::get(Ty), Value::MethodVal, isInternal, name),
30 SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
31 assert(::isa<MethodType>(Ty) && "Method signature must be of method type!");
35 dropAllReferences(); // After this it is safe to delete instructions.
37 // TODO: Should remove from the end, not the beginning of vector!
38 iterator BI = begin();
39 while ((BI = begin()) != end())
40 delete BasicBlocks.remove(BI);
42 // Delete all of the method arguments and unlink from symbol table...
43 ArgumentList.delete_all();
44 ArgumentList.setParent(0);
47 // Specialize setName to take care of symbol table majik
48 void Method::setName(const string &name, SymbolTable *ST) {
50 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
51 "Invalid symtab argument!");
52 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
54 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
57 void Method::setParent(Module *parent) {
60 // Relink symbol tables together...
61 setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
64 const MethodType *Method::getMethodType() const {
65 return cast<MethodType>(cast<PointerType>(getType())->getValueType());
68 const Type *Method::getReturnType() const {
69 return getMethodType()->getReturnType();
72 // dropAllReferences() - This function causes all the subinstructions to "let
73 // go" of all references that they are maintaining. This allows one to
74 // 'delete' a whole class at a time, even though there may be circular
75 // references... first all references are dropped, and all use counts go to
76 // zero. Then everything is delete'd for real. Note that no operations are
77 // valid on an object that has "dropped all references", except operator
80 void Method::dropAllReferences() {
81 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
84 //===----------------------------------------------------------------------===//
85 // GlobalVariable Implementation
86 //===----------------------------------------------------------------------===//
88 GlobalVariable::GlobalVariable(const Type *Ty, bool isConstant, bool isIntern,
89 ConstPoolVal *Initializer = 0,
90 const string &Name = "")
91 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
92 Constant(isConstant) {
93 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
96 // Specialize setName to take care of symbol table majik
97 void GlobalVariable::setName(const string &name, SymbolTable *ST) {
99 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
100 "Invalid symtab argument!");
101 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
102 Value::setName(name);
103 if (P && getName() != "") P->getSymbolTableSure()->insert(this);