Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Method.cpp - Implement the Method class ------------------*- C++ -*--=// |
| 2 | // |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 3 | // This file implements the Method & GlobalVariable classes for the VMCore |
| 4 | // library. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 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" |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 13 | #include "llvm/GlobalVariable.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 14 | #include "llvm/BasicBlock.h" |
| 15 | #include "llvm/iOther.h" |
| 16 | |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | // Method Implementation |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 22 | // Instantiate Templates - This ugliness is the price we have to pay |
| 23 | // for having a ValueHolderImpl.h file seperate from ValueHolder.h! :( |
| 24 | // |
Chris Lattner | f2a738c | 2001-07-14 06:13:19 +0000 | [diff] [blame] | 25 | template class ValueHolder<MethodArgument, Method, Method>; |
| 26 | template class ValueHolder<BasicBlock , Method, Method>; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame^] | 28 | Function::Function(const MethodType *Ty, bool isInternal, |
| 29 | const std::string &name) |
Chris Lattner | 7a78722 | 2001-11-26 19:14:56 +0000 | [diff] [blame] | 30 | : GlobalValue(PointerType::get(Ty), Value::MethodVal, isInternal, name), |
Vikram S. Adve | b375b89 | 2001-11-08 04:38:58 +0000 | [diff] [blame] | 31 | SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) { |
Chris Lattner | 3856934 | 2001-10-01 20:11:19 +0000 | [diff] [blame] | 32 | assert(::isa<MethodType>(Ty) && "Method signature must be of method type!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame^] | 35 | Function::~Function() { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 36 | dropAllReferences(); // After this it is safe to delete instructions. |
| 37 | |
| 38 | // TODO: Should remove from the end, not the beginning of vector! |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 39 | iterator BI = begin(); |
| 40 | while ((BI = begin()) != end()) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 41 | delete BasicBlocks.remove(BI); |
| 42 | |
| 43 | // Delete all of the method arguments and unlink from symbol table... |
| 44 | ArgumentList.delete_all(); |
| 45 | ArgumentList.setParent(0); |
| 46 | } |
| 47 | |
| 48 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame^] | 49 | void Function::setName(const std::string &name, SymbolTable *ST) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 50 | Module *P; |
Chris Lattner | 5c764a5 | 2001-09-07 16:47:18 +0000 | [diff] [blame] | 51 | assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) && |
| 52 | "Invalid symtab argument!"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this); |
| 54 | Value::setName(name); |
| 55 | if (P && getName() != "") P->getSymbolTableSure()->insert(this); |
| 56 | } |
| 57 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame^] | 58 | void Function::setParent(Module *parent) { |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 59 | Parent = parent; |
| 60 | |
| 61 | // Relink symbol tables together... |
| 62 | setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0); |
| 63 | } |
| 64 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame^] | 65 | const MethodType *Function::getMethodType() const { |
Chris Lattner | 2413b16 | 2001-12-04 00:03:30 +0000 | [diff] [blame] | 66 | return cast<MethodType>(cast<PointerType>(getType())->getElementType()); |
Chris Lattner | 7fac070 | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame^] | 69 | const Type *Function::getReturnType() const { |
Chris Lattner | 7fac070 | 2001-10-03 14:53:21 +0000 | [diff] [blame] | 70 | return getMethodType()->getReturnType(); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 |
| 79 | // delete. |
| 80 | // |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame^] | 81 | void Function::dropAllReferences() { |
Chris Lattner | 4cee8d8 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 82 | for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences)); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 83 | } |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 84 | |
| 85 | //===----------------------------------------------------------------------===// |
| 86 | // GlobalVariable Implementation |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 89 | GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern, |
| 90 | Constant *Initializer = 0, |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 91 | const std::string &Name = "") |
Chris Lattner | 7a78722 | 2001-11-26 19:14:56 +0000 | [diff] [blame] | 92 | : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name), |
Chris Lattner | 3462ae3 | 2001-12-03 22:26:30 +0000 | [diff] [blame] | 93 | isConstantGlobal(constant) { |
Chris Lattner | 3779864 | 2001-09-18 04:01:05 +0000 | [diff] [blame] | 94 | if (Initializer) Operands.push_back(Use((Value*)Initializer, this)); |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // Specialize setName to take care of symbol table majik |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 98 | void GlobalVariable::setName(const std::string &name, SymbolTable *ST) { |
Chris Lattner | da97550 | 2001-09-10 07:58:01 +0000 | [diff] [blame] | 99 | Module *P; |
| 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); |
| 105 | } |