blob: aa0d08b0a2db35c18b59d7e8b1f5c36e350588f3 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Method.cpp - Implement the Method class ------------------*- C++ -*--=//
2//
3// This file implements the Method class for the VMCore library.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/ValueHolderImpl.h"
8#include "llvm/DerivedTypes.h"
9#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
11#include "llvm/Method.h"
12#include "llvm/BasicBlock.h"
13#include "llvm/iOther.h"
14
15// Instantiate Templates - This ugliness is the price we have to pay
16// for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
17//
18template class ValueHolder<MethodArgument, Method>;
19template class ValueHolder<BasicBlock , Method>;
20
21Method::Method(const MethodType *Ty, const string &name)
22 : SymTabValue(Ty, Value::MethodVal, name), BasicBlocks(this),
23 ArgumentList(this, this) {
24 assert(Ty->isMethodType() && "Method signature must be of method type!");
25 Parent = 0;
26}
27
28Method::~Method() {
29 dropAllReferences(); // After this it is safe to delete instructions.
30
31 // TODO: Should remove from the end, not the beginning of vector!
Chris Lattner4cee8d82001-06-27 23:41:11 +000032 iterator BI = begin();
33 while ((BI = begin()) != end())
Chris Lattner2f7c9632001-06-06 20:29:01 +000034 delete BasicBlocks.remove(BI);
35
36 // Delete all of the method arguments and unlink from symbol table...
37 ArgumentList.delete_all();
38 ArgumentList.setParent(0);
39}
40
41// Specialize setName to take care of symbol table majik
42void Method::setName(const string &name) {
43 Module *P;
44 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
45 Value::setName(name);
46 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
47}
48
49void Method::setParent(Module *parent) {
50 Parent = parent;
51
52 // Relink symbol tables together...
53 setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
54}
55
56const Type *Method::getReturnType() const {
57 return ((const MethodType *)getType())->getReturnType();
58}
59
60const MethodType *Method::getMethodType() const {
61 return (const MethodType *)getType();
62}
63
64// dropAllReferences() - This function causes all the subinstructions to "let
65// go" of all references that they are maintaining. This allows one to
66// 'delete' a whole class at a time, even though there may be circular
67// references... first all references are dropped, and all use counts go to
68// zero. Then everything is delete'd for real. Note that no operations are
69// valid on an object that has "dropped all references", except operator
70// delete.
71//
72void Method::dropAllReferences() {
Chris Lattner4cee8d82001-06-27 23:41:11 +000073 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
Chris Lattner2f7c9632001-06-06 20:29:01 +000074}