blob: 59017178f68abbd2b4ce920509d315d471cd6a88 [file] [log] [blame]
Chris Lattner57698e22002-03-26 18:01:55 +00001//===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
Chris Lattner2f7c9632001-06-06 20:29:01 +00002//
Chris Lattner57698e22002-03-26 18:01:55 +00003// This file implements the Function & GlobalVariable classes for the VMCore
Chris Lattnerda975502001-09-10 07:58:01 +00004// library.
Chris Lattner2f7c9632001-06-06 20:29:01 +00005//
6//===----------------------------------------------------------------------===//
7
Chris Lattner2f7c9632001-06-06 20:29:01 +00008#include "llvm/Module.h"
Chris Lattner6213ae02002-09-06 20:46:32 +00009#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/iOther.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000011#include "SymbolTableListTraitsImpl.h"
12
Chris Lattner9ed7aef2002-09-06 21:33:15 +000013BasicBlock *ilist_traits<BasicBlock>::createNode() {
14 return new BasicBlock();
15}
16
Chris Lattner113f4f42002-06-25 16:13:24 +000017iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
18 return F->getBasicBlockList();
19}
20
21Argument *ilist_traits<Argument>::createNode() {
22 return new Argument(Type::IntTy);
23}
24
25iplist<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...
31template SymbolTableListTraits<Argument, Function, Function>;
32template SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000033
Chris Lattnerda975502001-09-10 07:58:01 +000034//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000035// Argument Implementation
36//===----------------------------------------------------------------------===//
37
Chris Lattner9ed7aef2002-09-06 21:33:15 +000038Argument::Argument(const Type *Ty, const std::string &Name = "", Function *Par)
39 : Value(Ty, Value::ArgumentVal, Name) {
40 Parent = 0;
41 if (Par)
42 Par->getArgumentList().push_back(this);
43}
44
45
Chris Lattnerd255ae22002-04-09 19:39:35 +000046// Specialize setName to take care of symbol table majik
47void Argument::setName(const std::string &name, SymbolTable *ST) {
48 Function *P;
49 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
50 "Invalid symtab argument!");
51 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
52 Value::setName(name);
53 if (P && hasName()) P->getSymbolTable()->insert(this);
54}
55
Chris Lattner9ed7aef2002-09-06 21:33:15 +000056void Argument::setParent(Function *parent) {
57 Parent = parent;
58}
59
60
Chris Lattnerd255ae22002-04-09 19:39:35 +000061//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000062// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000063//===----------------------------------------------------------------------===//
64
Chris Lattner91db5822002-03-29 03:44:36 +000065Function::Function(const FunctionType *Ty, bool isInternal,
Chris Lattner6213ae02002-09-06 20:46:32 +000066 const std::string &name, Module *ParentModule)
Chris Lattner113f4f42002-06-25 16:13:24 +000067 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
68 BasicBlocks.setItemParent(this);
69 BasicBlocks.setParent(this);
70 ArgumentList.setItemParent(this);
71 ArgumentList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000072 ParentSymTab = SymTab = 0;
Chris Lattner6213ae02002-09-06 20:46:32 +000073
74 if (ParentModule)
75 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000076}
77
Chris Lattner4e8c4872002-03-23 22:51:58 +000078Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000079 dropAllReferences(); // After this it is safe to delete instructions.
80
Chris Lattner113f4f42002-06-25 16:13:24 +000081 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +000082
83 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +000084 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000085 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000086 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000087}
88
89// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +000090void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000091 Module *P;
Chris Lattner5c764a52001-09-07 16:47:18 +000092 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
93 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000094 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
95 Value::setName(name);
96 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
97}
98
Chris Lattner4e8c4872002-03-23 22:51:58 +000099void Function::setParent(Module *parent) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000100 Parent = parent;
101
102 // Relink symbol tables together...
Chris Lattner2c8ff632002-04-28 04:51:51 +0000103 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
104 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000105}
106
Chris Lattner91db5822002-03-29 03:44:36 +0000107const FunctionType *Function::getFunctionType() const {
108 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000109}
110
Chris Lattner4e8c4872002-03-23 22:51:58 +0000111const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000112 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000113}
114
Chris Lattner2c8ff632002-04-28 04:51:51 +0000115SymbolTable *Function::getSymbolTableSure() {
116 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
117 return SymTab;
118}
119
120// hasSymbolTable() - Returns true if there is a symbol table allocated to
121// this object AND if there is at least one name in it!
122//
123bool Function::hasSymbolTable() const {
124 if (!SymTab) return false;
125
126 for (SymbolTable::const_iterator I = SymTab->begin();
127 I != SymTab->end(); ++I) {
128 if (I->second.begin() != I->second.end())
129 return true; // Found nonempty type plane!
130 }
131
132 return false;
133}
134
135
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136// dropAllReferences() - This function causes all the subinstructions to "let
137// go" of all references that they are maintaining. This allows one to
138// 'delete' a whole class at a time, even though there may be circular
139// references... first all references are dropped, and all use counts go to
140// zero. Then everything is delete'd for real. Note that no operations are
141// valid on an object that has "dropped all references", except operator
142// delete.
143//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000144void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000145 for (iterator I = begin(), E = end(); I != E; ++I)
146 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000147}
Chris Lattnerda975502001-09-10 07:58:01 +0000148
149//===----------------------------------------------------------------------===//
150// GlobalVariable Implementation
151//===----------------------------------------------------------------------===//
152
Chris Lattner3462ae32001-12-03 22:26:30 +0000153GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000154 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000155 const std::string &Name, Module *ParentModule)
Chris Lattner7a787222001-11-26 19:14:56 +0000156 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000157 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000158 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000159
160 if (ParentModule)
161 ParentModule->getGlobalList().push_back(this);
162}
163
164void GlobalVariable::setParent(Module *parent) {
165 Parent = parent;
Chris Lattnerda975502001-09-10 07:58:01 +0000166}
167
168// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000169void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000170 Module *P;
171 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
172 "Invalid symtab argument!");
173 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
174 Value::setName(name);
175 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
176}