The contents of the SymTabValue class have been incorporated into the
[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     BasicBlocks(this), ArgumentList(this, this) {
49   ParentSymTab = SymTab = 0;
50 }
51
52 Function::~Function() {
53   dropAllReferences();    // After this it is safe to delete instructions.
54
55   // TODO: Should remove from the end, not the beginning of vector!
56   iterator BI = begin();
57   while ((BI = begin()) != end())
58     delete BasicBlocks.remove(BI);
59
60   // Delete all of the method arguments and unlink from symbol table...
61   ArgumentList.delete_all();
62   ArgumentList.setParent(0);
63   delete SymTab;
64 }
65
66 // Specialize setName to take care of symbol table majik
67 void Function::setName(const std::string &name, SymbolTable *ST) {
68   Module *P;
69   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
70          "Invalid symtab argument!");
71   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
72   Value::setName(name);
73   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
74 }
75
76 void Function::setParent(Module *parent) {
77   Parent = parent;
78
79   // Relink symbol tables together...
80   ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
81   if (SymTab) SymTab->setParentSymTab(ParentSymTab);
82 }
83
84 const FunctionType *Function::getFunctionType() const {
85   return cast<FunctionType>(getType()->getElementType());
86 }
87
88 const Type *Function::getReturnType() const { 
89   return getFunctionType()->getReturnType();
90 }
91
92 SymbolTable *Function::getSymbolTableSure() {
93   if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
94   return SymTab;
95 }
96
97 // hasSymbolTable() - Returns true if there is a symbol table allocated to
98 // this object AND if there is at least one name in it!
99 //
100 bool Function::hasSymbolTable() const {
101   if (!SymTab) return false;
102
103   for (SymbolTable::const_iterator I = SymTab->begin(); 
104        I != SymTab->end(); ++I) {
105     if (I->second.begin() != I->second.end())
106       return true;                                // Found nonempty type plane!
107   }
108   
109   return false;
110 }
111
112
113 // dropAllReferences() - This function causes all the subinstructions to "let
114 // go" of all references that they are maintaining.  This allows one to
115 // 'delete' a whole class at a time, even though there may be circular
116 // references... first all references are dropped, and all use counts go to
117 // zero.  Then everything is delete'd for real.  Note that no operations are
118 // valid on an object that has "dropped all references", except operator 
119 // delete.
120 //
121 void Function::dropAllReferences() {
122   for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
123 }
124
125 //===----------------------------------------------------------------------===//
126 // GlobalVariable Implementation
127 //===----------------------------------------------------------------------===//
128
129 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
130                                Constant *Initializer = 0,
131                                const std::string &Name = "")
132   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
133     isConstantGlobal(constant) {
134   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
135 }
136
137 // Specialize setName to take care of symbol table majik
138 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
139   Module *P;
140   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
141          "Invalid symtab argument!");
142   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
143   Value::setName(name);
144   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
145 }