3ad7a66c5ced003702e47709113fafa7dd1f1b67
[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/Module.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/iOther.h"
11 #include "SymbolTableListTraitsImpl.h"
12
13 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
14   return F->getBasicBlockList();
15 }
16
17 Argument *ilist_traits<Argument>::createNode() {
18   return new Argument(Type::IntTy);
19 }
20
21 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
22   return F->getArgumentList();
23 }
24
25 // Explicit instantiations of SymbolTableListTraits since some of the methods
26 // are not in the public header file...
27 template SymbolTableListTraits<Argument, Function, Function>;
28 template SymbolTableListTraits<BasicBlock, Function, Function>;
29
30 //===----------------------------------------------------------------------===//
31 // Argument Implementation
32 //===----------------------------------------------------------------------===//
33
34 // Specialize setName to take care of symbol table majik
35 void Argument::setName(const std::string &name, SymbolTable *ST) {
36   Function *P;
37   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
38          "Invalid symtab argument!");
39   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
40   Value::setName(name);
41   if (P && hasName()) P->getSymbolTable()->insert(this);
42 }
43
44 //===----------------------------------------------------------------------===//
45 // Function Implementation
46 //===----------------------------------------------------------------------===//
47
48
49 Function::Function(const FunctionType *Ty, bool isInternal,
50                    const std::string &name, Module *ParentModule)
51   : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
52   BasicBlocks.setItemParent(this);
53   BasicBlocks.setParent(this);
54   ArgumentList.setItemParent(this);
55   ArgumentList.setParent(this);
56   ParentSymTab = SymTab = 0;
57
58   if (ParentModule)
59     ParentModule->getFunctionList().push_back(this);
60 }
61
62 Function::~Function() {
63   dropAllReferences();    // After this it is safe to delete instructions.
64
65   BasicBlocks.clear();    // Delete all basic blocks...
66
67   // Delete all of the method arguments and unlink from symbol table...
68   ArgumentList.clear();
69   ArgumentList.setParent(0);
70   delete SymTab;
71 }
72
73 // Specialize setName to take care of symbol table majik
74 void Function::setName(const std::string &name, SymbolTable *ST) {
75   Module *P;
76   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
77          "Invalid symtab argument!");
78   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
79   Value::setName(name);
80   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
81 }
82
83 void Function::setParent(Module *parent) {
84   Parent = parent;
85
86   // Relink symbol tables together...
87   ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
88   if (SymTab) SymTab->setParentSymTab(ParentSymTab);
89 }
90
91 const FunctionType *Function::getFunctionType() const {
92   return cast<FunctionType>(getType()->getElementType());
93 }
94
95 const Type *Function::getReturnType() const { 
96   return getFunctionType()->getReturnType();
97 }
98
99 SymbolTable *Function::getSymbolTableSure() {
100   if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
101   return SymTab;
102 }
103
104 // hasSymbolTable() - Returns true if there is a symbol table allocated to
105 // this object AND if there is at least one name in it!
106 //
107 bool Function::hasSymbolTable() const {
108   if (!SymTab) return false;
109
110   for (SymbolTable::const_iterator I = SymTab->begin(); 
111        I != SymTab->end(); ++I) {
112     if (I->second.begin() != I->second.end())
113       return true;                                // Found nonempty type plane!
114   }
115   
116   return false;
117 }
118
119
120 // dropAllReferences() - This function causes all the subinstructions to "let
121 // go" of all references that they are maintaining.  This allows one to
122 // 'delete' a whole class at a time, even though there may be circular
123 // references... first all references are dropped, and all use counts go to
124 // zero.  Then everything is delete'd for real.  Note that no operations are
125 // valid on an object that has "dropped all references", except operator 
126 // delete.
127 //
128 void Function::dropAllReferences() {
129   for (iterator I = begin(), E = end(); I != E; ++I)
130     I->dropAllReferences();
131 }
132
133 //===----------------------------------------------------------------------===//
134 // GlobalVariable Implementation
135 //===----------------------------------------------------------------------===//
136
137 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
138                                Constant *Initializer,
139                                const std::string &Name)
140   : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
141     isConstantGlobal(constant) {
142   if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
143 }
144
145 // Specialize setName to take care of symbol table majik
146 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
147   Module *P;
148   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
149          "Invalid symtab argument!");
150   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
151   Value::setName(name);
152   if (P && getName() != "") P->getSymbolTableSure()->insert(this);
153 }