blob: 70569c0f67ce7539f0e878921347b0fd5260ee95 [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 Lattner184b2982002-09-08 18:59:35 +000011#include "Support/LeakDetector.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000012#include "SymbolTableListTraitsImpl.h"
13
Chris Lattner9ed7aef2002-09-06 21:33:15 +000014BasicBlock *ilist_traits<BasicBlock>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000015 BasicBlock *Ret = new BasicBlock();
16 // This should not be garbage monitored.
17 LeakDetector::removeGarbageObject(Ret);
18 return Ret;
Chris Lattner9ed7aef2002-09-06 21:33:15 +000019}
20
Chris Lattner113f4f42002-06-25 16:13:24 +000021iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
22 return F->getBasicBlockList();
23}
24
25Argument *ilist_traits<Argument>::createNode() {
Chris Lattner184b2982002-09-08 18:59:35 +000026 Argument *Ret = new Argument(Type::IntTy);
27 // This should not be garbage monitored.
28 LeakDetector::removeGarbageObject(Ret);
29 return Ret;
Chris Lattner113f4f42002-06-25 16:13:24 +000030}
31
32iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
33 return F->getArgumentList();
34}
35
36// Explicit instantiations of SymbolTableListTraits since some of the methods
37// are not in the public header file...
38template SymbolTableListTraits<Argument, Function, Function>;
39template SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000040
Chris Lattnerda975502001-09-10 07:58:01 +000041//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000042// Argument Implementation
43//===----------------------------------------------------------------------===//
44
Vikram S. Adve0267d0c2002-09-17 01:17:57 +000045Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
Chris Lattner9ed7aef2002-09-06 21:33:15 +000046 : Value(Ty, Value::ArgumentVal, Name) {
47 Parent = 0;
Chris Lattner184b2982002-09-08 18:59:35 +000048
49 // Make sure that we get added to a function
50 LeakDetector::addGarbageObject(this);
51
Chris Lattner9ed7aef2002-09-06 21:33:15 +000052 if (Par)
53 Par->getArgumentList().push_back(this);
54}
55
56
Chris Lattnerd255ae22002-04-09 19:39:35 +000057// Specialize setName to take care of symbol table majik
58void Argument::setName(const std::string &name, SymbolTable *ST) {
59 Function *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +000060 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerd255ae22002-04-09 19:39:35 +000061 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +000062 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000063 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +000064 if (P && hasName()) P->getSymbolTable().insert(this);
Chris Lattnerd255ae22002-04-09 19:39:35 +000065}
66
Chris Lattner9ed7aef2002-09-06 21:33:15 +000067void Argument::setParent(Function *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +000068 if (getParent())
69 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000070 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +000071 if (getParent())
72 LeakDetector::removeGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +000073}
74
75
Chris Lattnerd255ae22002-04-09 19:39:35 +000076//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000077// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000078//===----------------------------------------------------------------------===//
79
Chris Lattner91db5822002-03-29 03:44:36 +000080Function::Function(const FunctionType *Ty, bool isInternal,
Chris Lattner6213ae02002-09-06 20:46:32 +000081 const std::string &name, Module *ParentModule)
Chris Lattner113f4f42002-06-25 16:13:24 +000082 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
83 BasicBlocks.setItemParent(this);
84 BasicBlocks.setParent(this);
85 ArgumentList.setItemParent(this);
86 ArgumentList.setParent(this);
Chris Lattnerb251c3d2002-11-20 18:07:48 +000087 SymTab = new SymbolTable();
Chris Lattner6213ae02002-09-06 20:46:32 +000088
Chris Lattner149376d2002-10-13 20:57:00 +000089 // Create the arguments vector, all arguments start out unnamed.
90 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
91 assert(Ty->getParamType(i) != Type::VoidTy &&
92 "Cannot have void typed arguments!");
93 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
94 }
95
Chris Lattner184b2982002-09-08 18:59:35 +000096 // Make sure that we get added to a function
97 LeakDetector::addGarbageObject(this);
98
Chris Lattner6213ae02002-09-06 20:46:32 +000099 if (ParentModule)
100 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000101}
102
Chris Lattner4e8c4872002-03-23 22:51:58 +0000103Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000104 dropAllReferences(); // After this it is safe to delete instructions.
105
Chris Lattner113f4f42002-06-25 16:13:24 +0000106 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000107
108 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +0000109 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000110 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +0000111 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000112}
113
114// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +0000115void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000116 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000117 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattner5c764a52001-09-07 16:47:18 +0000118 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000119 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000121 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000122}
123
Chris Lattner4e8c4872002-03-23 22:51:58 +0000124void Function::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000125 if (getParent())
126 LeakDetector::addGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000127 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000128 if (getParent())
129 LeakDetector::removeGarbageObject(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000130}
131
Chris Lattner91db5822002-03-29 03:44:36 +0000132const FunctionType *Function::getFunctionType() const {
133 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +0000134}
135
Chris Lattner4e8c4872002-03-23 22:51:58 +0000136const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +0000137 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138}
139
Chris Lattner2f7c9632001-06-06 20:29:01 +0000140// dropAllReferences() - This function causes all the subinstructions to "let
141// go" of all references that they are maintaining. This allows one to
142// 'delete' a whole class at a time, even though there may be circular
143// references... first all references are dropped, and all use counts go to
144// zero. Then everything is delete'd for real. Note that no operations are
145// valid on an object that has "dropped all references", except operator
146// delete.
147//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000148void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000149 for (iterator I = begin(), E = end(); I != E; ++I)
150 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000151}
Chris Lattnerda975502001-09-10 07:58:01 +0000152
153//===----------------------------------------------------------------------===//
154// GlobalVariable Implementation
155//===----------------------------------------------------------------------===//
156
Chris Lattner3462ae32001-12-03 22:26:30 +0000157GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000158 Constant *Initializer,
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000159 const std::string &Name, Module *ParentModule)
Chris Lattner7a787222001-11-26 19:14:56 +0000160 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000161 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000162 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000163
Chris Lattner184b2982002-09-08 18:59:35 +0000164 LeakDetector::addGarbageObject(this);
165
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000166 if (ParentModule)
167 ParentModule->getGlobalList().push_back(this);
168}
169
170void GlobalVariable::setParent(Module *parent) {
Chris Lattner184b2982002-09-08 18:59:35 +0000171 if (getParent())
172 LeakDetector::addGarbageObject(this);
Chris Lattner9ed7aef2002-09-06 21:33:15 +0000173 Parent = parent;
Chris Lattner184b2982002-09-08 18:59:35 +0000174 if (getParent())
175 LeakDetector::removeGarbageObject(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000176}
177
178// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000179void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000180 Module *P;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000181 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
Chris Lattnerda975502001-09-10 07:58:01 +0000182 "Invalid symtab argument!");
Chris Lattner98cf1f52002-11-20 18:36:02 +0000183 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000184 Value::setName(name);
Chris Lattner98cf1f52002-11-20 18:36:02 +0000185 if (P && getName() != "") P->getSymbolTable().insert(this);
Chris Lattnerda975502001-09-10 07:58:01 +0000186}