blob: c5e9e2181928ffc11ab20d171109ce7e72d573c7 [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
8#include "llvm/ValueHolderImpl.h"
9#include "llvm/DerivedTypes.h"
10#include "llvm/SymbolTable.h"
11#include "llvm/Module.h"
Chris Lattner57698e22002-03-26 18:01:55 +000012#include "llvm/Function.h"
Chris Lattnerda975502001-09-10 07:58:01 +000013#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "llvm/BasicBlock.h"
15#include "llvm/iOther.h"
16
Chris Lattnerda975502001-09-10 07:58:01 +000017//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +000018// Function Implementation
Chris Lattnerda975502001-09-10 07:58:01 +000019//===----------------------------------------------------------------------===//
20
21
Chris Lattner2f7c9632001-06-06 20:29:01 +000022// Instantiate Templates - This ugliness is the price we have to pay
23// for having a ValueHolderImpl.h file seperate from ValueHolder.h! :(
24//
Chris Lattner57698e22002-03-26 18:01:55 +000025template class ValueHolder<FunctionArgument, Function, Function>;
26template class ValueHolder<BasicBlock , Function, Function>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000027
Chris Lattner91db5822002-03-29 03:44:36 +000028Function::Function(const FunctionType *Ty, bool isInternal,
Chris Lattner4e8c4872002-03-23 22:51:58 +000029 const std::string &name)
Chris Lattner57698e22002-03-26 18:01:55 +000030 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name),
Vikram S. Adveb375b892001-11-08 04:38:58 +000031 SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000032}
33
Chris Lattner4e8c4872002-03-23 22:51:58 +000034Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000035 dropAllReferences(); // After this it is safe to delete instructions.
36
37 // TODO: Should remove from the end, not the beginning of vector!
Chris Lattner4cee8d82001-06-27 23:41:11 +000038 iterator BI = begin();
39 while ((BI = begin()) != end())
Chris Lattner2f7c9632001-06-06 20:29:01 +000040 delete BasicBlocks.remove(BI);
41
42 // Delete all of the method arguments and unlink from symbol table...
43 ArgumentList.delete_all();
44 ArgumentList.setParent(0);
45}
46
47// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +000048void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000049 Module *P;
Chris Lattner5c764a52001-09-07 16:47:18 +000050 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
51 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000052 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
53 Value::setName(name);
54 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
55}
56
Chris Lattner4e8c4872002-03-23 22:51:58 +000057void Function::setParent(Module *parent) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000058 Parent = parent;
59
60 // Relink symbol tables together...
61 setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
62}
63
Chris Lattner91db5822002-03-29 03:44:36 +000064const FunctionType *Function::getFunctionType() const {
65 return cast<FunctionType>(getType()->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +000066}
67
Chris Lattner4e8c4872002-03-23 22:51:58 +000068const Type *Function::getReturnType() const {
Chris Lattner91db5822002-03-29 03:44:36 +000069 return getFunctionType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +000070}
71
Chris Lattner2f7c9632001-06-06 20:29:01 +000072// dropAllReferences() - This function causes all the subinstructions to "let
73// go" of all references that they are maintaining. This allows one to
74// 'delete' a whole class at a time, even though there may be circular
75// references... first all references are dropped, and all use counts go to
76// zero. Then everything is delete'd for real. Note that no operations are
77// valid on an object that has "dropped all references", except operator
78// delete.
79//
Chris Lattner4e8c4872002-03-23 22:51:58 +000080void Function::dropAllReferences() {
Chris Lattner4cee8d82001-06-27 23:41:11 +000081 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
Chris Lattner2f7c9632001-06-06 20:29:01 +000082}
Chris Lattnerda975502001-09-10 07:58:01 +000083
84//===----------------------------------------------------------------------===//
85// GlobalVariable Implementation
86//===----------------------------------------------------------------------===//
87
Chris Lattner3462ae32001-12-03 22:26:30 +000088GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
89 Constant *Initializer = 0,
Chris Lattner7f74a562002-01-20 22:54:45 +000090 const std::string &Name = "")
Chris Lattner7a787222001-11-26 19:14:56 +000091 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +000092 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +000093 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattnerda975502001-09-10 07:58:01 +000094}
95
96// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000097void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +000098 Module *P;
99 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
100 "Invalid symtab argument!");
101 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
102 Value::setName(name);
103 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
104}