Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- 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 | // |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 18 | template class ValueHolder<MethodArgument, Method, Method>; |
| 19 | template class ValueHolder<BasicBlock , Method, Method>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 20 | |
| 21 | Method::Method(const MethodType *Ty, const string &name) |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 22 | : Value(Ty, Value::MethodVal, name), SymTabValue(this), BasicBlocks(this), |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | ArgumentList(this, this) { |
| 24 | assert(Ty->isMethodType() && "Method signature must be of method type!"); |
| 25 | Parent = 0; |
| 26 | } |
| 27 | |
| 28 | Method::~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 Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 32 | iterator BI = begin(); |
| 33 | while ((BI = begin()) != end()) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | 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 |
Chris Lattner | 5c764a5 | 2001-09-07 16:47:18 +0000 | [diff] [blame^] | 42 | void Method::setName(const string &name, SymbolTable *ST) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 43 | Module *P; |
Chris Lattner | 5c764a5 | 2001-09-07 16:47:18 +0000 | [diff] [blame^] | 44 | assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) && |
| 45 | "Invalid symtab argument!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 46 | if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this); |
| 47 | Value::setName(name); |
| 48 | if (P && getName() != "") P->getSymbolTableSure()->insert(this); |
| 49 | } |
| 50 | |
| 51 | void Method::setParent(Module *parent) { |
| 52 | Parent = parent; |
| 53 | |
| 54 | // Relink symbol tables together... |
| 55 | setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0); |
| 56 | } |
| 57 | |
| 58 | const Type *Method::getReturnType() const { |
| 59 | return ((const MethodType *)getType())->getReturnType(); |
| 60 | } |
| 61 | |
| 62 | const MethodType *Method::getMethodType() const { |
| 63 | return (const MethodType *)getType(); |
| 64 | } |
| 65 | |
| 66 | // dropAllReferences() - This function causes all the subinstructions to "let |
| 67 | // go" of all references that they are maintaining. This allows one to |
| 68 | // 'delete' a whole class at a time, even though there may be circular |
| 69 | // references... first all references are dropped, and all use counts go to |
| 70 | // zero. Then everything is delete'd for real. Note that no operations are |
| 71 | // valid on an object that has "dropped all references", except operator |
| 72 | // delete. |
| 73 | // |
| 74 | void Method::dropAllReferences() { |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 75 | for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 76 | } |