blob: 591bd9009f34b8bdb2ed9793fc5e941df6502a15 [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 Lattner6e6f5be2002-04-08 00:15:29 +00008#include "llvm/Function.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/DerivedTypes.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000010#include "llvm/Module.h"
Chris Lattnerda975502001-09-10 07:58:01 +000011#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000012#include "llvm/BasicBlock.h"
13#include "llvm/iOther.h"
Chris Lattnerd255ae22002-04-09 19:39:35 +000014#include "llvm/Argument.h"
Chris Lattner113f4f42002-06-25 16:13:24 +000015#include "SymbolTableListTraitsImpl.h"
16
17iplist<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
38// Specialize setName to take care of symbol table majik
39void 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
Chris Lattnerd255ae22002-04-09 19:39:35 +000048//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000049// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000050//===----------------------------------------------------------------------===//
51
52
Chris Lattner91db5822002-03-29 03:44:36 +000053Function::Function(const FunctionType *Ty, bool isInternal,
Chris Lattner4e8c4872002-03-23 22:51:58 +000054 const std::string &name)
Chris Lattner113f4f42002-06-25 16:13:24 +000055 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
56 BasicBlocks.setItemParent(this);
57 BasicBlocks.setParent(this);
58 ArgumentList.setItemParent(this);
59 ArgumentList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000060 ParentSymTab = SymTab = 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +000061}
62
Chris Lattner4e8c4872002-03-23 22:51:58 +000063Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000064 dropAllReferences(); // After this it is safe to delete instructions.
65
Chris Lattner113f4f42002-06-25 16:13:24 +000066 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +000067
68 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +000069 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000070 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000071 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000072}
73
74// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +000075void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000076 Module *P;
Chris Lattner5c764a52001-09-07 16:47:18 +000077 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
78 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000079 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
80 Value::setName(name);
81 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
82}
83
Chris Lattner4e8c4872002-03-23 22:51:58 +000084void Function::setParent(Module *parent) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000085 Parent = parent;
86
87 // Relink symbol tables together...
Chris Lattner2c8ff632002-04-28 04:51:51 +000088 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
89 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
Chris Lattner2f7c9632001-06-06 20:29:01 +000090}
91
Chris Lattner91db5822002-03-29 03:44:36 +000092const FunctionType *Function::getFunctionType() const {
93 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +000094}
95
Chris Lattner4e8c4872002-03-23 22:51:58 +000096const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +000097 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +000098}
99
Chris Lattner2c8ff632002-04-28 04:51:51 +0000100SymbolTable *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//
108bool 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
Chris Lattner2f7c9632001-06-06 20:29:01 +0000121// 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//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000129void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000130 for (iterator I = begin(), E = end(); I != E; ++I)
131 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132}
Chris Lattnerda975502001-09-10 07:58:01 +0000133
134//===----------------------------------------------------------------------===//
135// GlobalVariable Implementation
136//===----------------------------------------------------------------------===//
137
Chris Lattner3462ae32001-12-03 22:26:30 +0000138GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000139 Constant *Initializer,
140 const std::string &Name)
Chris Lattner7a787222001-11-26 19:14:56 +0000141 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000142 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000143 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattnerda975502001-09-10 07:58:01 +0000144}
145
146// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000147void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000148 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}