598f9fd0c24946565c9e53cf7c97502ee5106f06
[oota-llvm.git] / lib / VMCore / Function.cpp
1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
2 //
3 // This file implements the Function & GlobalVariable classes for the VMCore
4 // library.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "llvm/Function.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/Module.h"
12 #include "llvm/GlobalVariable.h"
13 #include "llvm/BasicBlock.h"
14 #include "llvm/iOther.h"
15 #include "llvm/Argument.h"
16 #include "ValueHolderImpl.h"
17
18 //===----------------------------------------------------------------------===//
19 // Argument Implementation
20 //===----------------------------------------------------------------------===//
21
22 // Specialize setName to take care of symbol table majik
23 void Argument::setName(const std::string &name, SymbolTable *ST) {
24   Function *P;
25   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
26          "Invalid symtab argument!");
27   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
28   Value::setName(name);
29   if (P && hasName()) P->getSymbolTable()->insert(this);
30 }
31
32
33
34 //===----------------------------------------------------------------------===//
35 // Function Implementation
36 //===----------------------------------------------------------------------===//
37
38
39 // Instantiate Templates - This ugliness is the price we have to pay
40 // for having a ValueHolderImpl.h file seperate from ValueHolder.h!  :(
41 //
42 template class ValueHolder<Argument  , Function, Function>;
43 template class ValueHolder<BasicBlock, Function, Function>;
44
45 Function::Function(const FunctionType *Ty, bool isInternal,
46                    const std::string &name)
47   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name),
48     SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
49 }
50
51 Function::~Function() {
52   dropAllReferences();    // After this it is safe to delete instructions.
53
54   // TODO: Should remove from the end, not the beginning of vector!
55   iterator BI = begin();
56   while ((BI = begin()) != end())
57     delete BasicBlocks.remove(BI);
58
59   // Delete all of the method arguments and unlink from symbol table...
60   ArgumentList.delete_all();
61   ArgumentList.setParent(0);
62 }
63
64 // Specialize setName to take care of symbol table majik
65 void Function::setName(const std::string &name, SymbolTable *ST) {
66   Module *P;
67   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
68          "Invalid symtab argument!");
69   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
70   Value::setName(name);
71   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
72 }
73
74 void Function::setParent(Module *parent) {
75   Parent = parent;
76
77   // Relink symbol tables together...
78   setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
79 }
80
81 const FunctionType *Function::getFunctionType() const {
82   return cast<FunctionType>(getType()->getElementType());
83 }
84
85 const Type *Function::getReturnType() const { 
86   return getFunctionType()->getReturnType();
87 }
88
89 // dropAllReferences() - This function causes all the subinstructions to "let
90 // go" of all references that they are maintaining.  This allows one to
91 // 'delete' a whole class at a time, even though there may be circular
92 // references... first all references are dropped, and all use counts go to
93 // zero.  Then everything is delete'd for real.  Note that no operations are
94 // valid on an object that has "dropped all references", except operator 
95 // delete.
96 //
97 void Function::dropAllReferences() {
98   for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
99 }
100
101 //===----------------------------------------------------------------------===//
102 // GlobalVariable Implementation
103 //===----------------------------------------------------------------------===//
104
105 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
106                                Constant *Initializer = 0,
107                                const std::string &Name = "")
108   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
109     isConstantGlobal(constant) {
110   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
111 }
112
113 // Specialize setName to take care of symbol table majik
114 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
115   Module *P;
116   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
117          "Invalid symtab argument!");
118   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
119   Value::setName(name);
120   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
121 }