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