blob: 3ad7a66c5ced003702e47709113fafa7dd1f1b67 [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
13iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
14 return F->getBasicBlockList();
15}
16
17Argument *ilist_traits<Argument>::createNode() {
18 return new Argument(Type::IntTy);
19}
20
21iplist<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...
27template SymbolTableListTraits<Argument, Function, Function>;
28template SymbolTableListTraits<BasicBlock, Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000029
Chris Lattnerda975502001-09-10 07:58:01 +000030//===----------------------------------------------------------------------===//
Chris Lattnerd255ae22002-04-09 19:39:35 +000031// Argument Implementation
32//===----------------------------------------------------------------------===//
33
34// Specialize setName to take care of symbol table majik
35void 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
Chris Lattnerd255ae22002-04-09 19:39:35 +000044//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000045// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000046//===----------------------------------------------------------------------===//
47
48
Chris Lattner91db5822002-03-29 03:44:36 +000049Function::Function(const FunctionType *Ty, bool isInternal,
Chris Lattner6213ae02002-09-06 20:46:32 +000050 const std::string &name, Module *ParentModule)
Chris Lattner113f4f42002-06-25 16:13:24 +000051 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
52 BasicBlocks.setItemParent(this);
53 BasicBlocks.setParent(this);
54 ArgumentList.setItemParent(this);
55 ArgumentList.setParent(this);
Chris Lattner2c8ff632002-04-28 04:51:51 +000056 ParentSymTab = SymTab = 0;
Chris Lattner6213ae02002-09-06 20:46:32 +000057
58 if (ParentModule)
59 ParentModule->getFunctionList().push_back(this);
Chris Lattner2f7c9632001-06-06 20:29:01 +000060}
61
Chris Lattner4e8c4872002-03-23 22:51:58 +000062Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000063 dropAllReferences(); // After this it is safe to delete instructions.
64
Chris Lattner113f4f42002-06-25 16:13:24 +000065 BasicBlocks.clear(); // Delete all basic blocks...
Chris Lattner2f7c9632001-06-06 20:29:01 +000066
67 // Delete all of the method arguments and unlink from symbol table...
Chris Lattner113f4f42002-06-25 16:13:24 +000068 ArgumentList.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000069 ArgumentList.setParent(0);
Chris Lattner2c8ff632002-04-28 04:51:51 +000070 delete SymTab;
Chris Lattner2f7c9632001-06-06 20:29:01 +000071}
72
73// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +000074void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000075 Module *P;
Chris Lattner5c764a52001-09-07 16:47:18 +000076 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
77 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000078 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
79 Value::setName(name);
80 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
81}
82
Chris Lattner4e8c4872002-03-23 22:51:58 +000083void Function::setParent(Module *parent) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000084 Parent = parent;
85
86 // Relink symbol tables together...
Chris Lattner2c8ff632002-04-28 04:51:51 +000087 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
88 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
Chris Lattner2f7c9632001-06-06 20:29:01 +000089}
90
Chris Lattner91db5822002-03-29 03:44:36 +000091const FunctionType *Function::getFunctionType() const {
92 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +000093}
94
Chris Lattner4e8c4872002-03-23 22:51:58 +000095const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +000096 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +000097}
98
Chris Lattner2c8ff632002-04-28 04:51:51 +000099SymbolTable *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//
107bool 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
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120// 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//
Chris Lattner4e8c4872002-03-23 22:51:58 +0000128void Function::dropAllReferences() {
Chris Lattner113f4f42002-06-25 16:13:24 +0000129 for (iterator I = begin(), E = end(); I != E; ++I)
130 I->dropAllReferences();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000131}
Chris Lattnerda975502001-09-10 07:58:01 +0000132
133//===----------------------------------------------------------------------===//
134// GlobalVariable Implementation
135//===----------------------------------------------------------------------===//
136
Chris Lattner3462ae32001-12-03 22:26:30 +0000137GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
Chris Lattnera82ee2d2002-07-24 22:08:53 +0000138 Constant *Initializer,
139 const std::string &Name)
Chris Lattner7a787222001-11-26 19:14:56 +0000140 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +0000141 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +0000142 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattnerda975502001-09-10 07:58:01 +0000143}
144
145// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +0000146void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +0000147 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}