372d0d28b91161a8781a99db51e7ed42f09ba170
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Method.cpp - Implement the Method class ------------------*- C++ -*--=//
2 //
3 // This file implements the Method & GlobalVariable classes for the VMCore
4 // library.
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"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/iOther.h"
16
17 //===----------------------------------------------------------------------===//
18 // Method Implementation
19 //===----------------------------------------------------------------------===//
20
21
22 // Instantiate Templates - This ugliness is the price we have to pay
23 // for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
24 //
25 template class ValueHolder<MethodArgument, Method, Method>;
26 template class ValueHolder<BasicBlock    , Method, Method>;
27
28 Method::Method(const MethodType *Ty, const string &name) 
29   : GlobalValue(PointerType::get(Ty), Value::MethodVal, name),
30     SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
31   assert(::isa<MethodType>(Ty) && "Method signature must be of method type!");
32   Parent = 0;
33 }
34
35 Method::~Method() {
36   dropAllReferences();    // After this it is safe to delete instructions.
37
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);
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
49 void Method::setName(const string &name, SymbolTable *ST) {
50   Module *P;
51   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
52          "Invalid symtab argument!");
53   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
54   Value::setName(name);
55   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
56 }
57
58 void Method::setParent(Module *parent) {
59   Parent = parent;
60
61   // Relink symbol tables together...
62   setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
63 }
64
65 const MethodType *Method::getMethodType() const {
66   return cast<MethodType>(cast<PointerType>(getType())->getValueType());
67 }
68
69 const Type *Method::getReturnType() const { 
70   return getMethodType()->getReturnType();
71 }
72
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 //
81 void Method::dropAllReferences() {
82   for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
83 }
84
85 //===----------------------------------------------------------------------===//
86 // GlobalVariable Implementation
87 //===----------------------------------------------------------------------===//
88
89 GlobalVariable::GlobalVariable(const Type *Ty, bool isConstant,
90                                ConstPoolVal *Initializer = 0, 
91                                const string &Name = "")
92   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Name),
93     Parent(0), Constant(isConstant) {
94   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
95
96   assert(!isConstant || hasInitializer() &&
97          "Globals Constants must have an initializer!"); 
98 }
99
100 // Specialize setName to take care of symbol table majik
101 void GlobalVariable::setName(const string &name, SymbolTable *ST) {
102   Module *P;
103   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
104          "Invalid symtab argument!");
105   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
106   Value::setName(name);
107   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
108 }