blob: 286ef7fbe19de01b05908b255feff746f9030e98 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Method.cpp - Implement the Method class ------------------*- C++ -*--=//
2//
Chris Lattnerda975502001-09-10 07:58:01 +00003// This file implements the Method & GlobalVariable classes for the VMCore
4// 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"
12#include "llvm/Method.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//===----------------------------------------------------------------------===//
18// Method Implementation
19//===----------------------------------------------------------------------===//
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 Lattnerf2a738c2001-07-14 06:13:19 +000025template class ValueHolder<MethodArgument, Method, Method>;
26template class ValueHolder<BasicBlock , Method, Method>;
Chris Lattner2f7c9632001-06-06 20:29:01 +000027
Chris Lattner4e8c4872002-03-23 22:51:58 +000028Function::Function(const MethodType *Ty, bool isInternal,
29 const std::string &name)
Chris Lattner7a787222001-11-26 19:14:56 +000030 : GlobalValue(PointerType::get(Ty), Value::MethodVal, isInternal, name),
Vikram S. Adveb375b892001-11-08 04:38:58 +000031 SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
Chris Lattner38569342001-10-01 20:11:19 +000032 assert(::isa<MethodType>(Ty) && "Method signature must be of method type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000033}
34
Chris Lattner4e8c4872002-03-23 22:51:58 +000035Function::~Function() {
Chris Lattner2f7c9632001-06-06 20:29:01 +000036 dropAllReferences(); // After this it is safe to delete instructions.
37
38 // TODO: Should remove from the end, not the beginning of vector!
Chris Lattner4cee8d82001-06-27 23:41:11 +000039 iterator BI = begin();
40 while ((BI = begin()) != end())
Chris Lattner2f7c9632001-06-06 20:29:01 +000041 delete BasicBlocks.remove(BI);
42
43 // Delete all of the method arguments and unlink from symbol table...
44 ArgumentList.delete_all();
45 ArgumentList.setParent(0);
46}
47
48// Specialize setName to take care of symbol table majik
Chris Lattner4e8c4872002-03-23 22:51:58 +000049void Function::setName(const std::string &name, SymbolTable *ST) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000050 Module *P;
Chris Lattner5c764a52001-09-07 16:47:18 +000051 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
52 "Invalid symtab argument!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000053 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
54 Value::setName(name);
55 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
56}
57
Chris Lattner4e8c4872002-03-23 22:51:58 +000058void Function::setParent(Module *parent) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000059 Parent = parent;
60
61 // Relink symbol tables together...
62 setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
63}
64
Chris Lattner4e8c4872002-03-23 22:51:58 +000065const MethodType *Function::getMethodType() const {
Chris Lattner2413b162001-12-04 00:03:30 +000066 return cast<MethodType>(cast<PointerType>(getType())->getElementType());
Chris Lattner7fac0702001-10-03 14:53:21 +000067}
68
Chris Lattner4e8c4872002-03-23 22:51:58 +000069const Type *Function::getReturnType() const {
Chris Lattner7fac0702001-10-03 14:53:21 +000070 return getMethodType()->getReturnType();
Chris Lattner2f7c9632001-06-06 20:29:01 +000071}
72
Chris Lattner2f7c9632001-06-06 20:29:01 +000073// dropAllReferences() - This function causes all the subinstructions to "let
74// go" of all references that they are maintaining. This allows one to
75// 'delete' a whole class at a time, even though there may be circular
76// references... first all references are dropped, and all use counts go to
77// zero. Then everything is delete'd for real. Note that no operations are
78// valid on an object that has "dropped all references", except operator
79// delete.
80//
Chris Lattner4e8c4872002-03-23 22:51:58 +000081void Function::dropAllReferences() {
Chris Lattner4cee8d82001-06-27 23:41:11 +000082 for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
Chris Lattner2f7c9632001-06-06 20:29:01 +000083}
Chris Lattnerda975502001-09-10 07:58:01 +000084
85//===----------------------------------------------------------------------===//
86// GlobalVariable Implementation
87//===----------------------------------------------------------------------===//
88
Chris Lattner3462ae32001-12-03 22:26:30 +000089GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
90 Constant *Initializer = 0,
Chris Lattner7f74a562002-01-20 22:54:45 +000091 const std::string &Name = "")
Chris Lattner7a787222001-11-26 19:14:56 +000092 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
Chris Lattner3462ae32001-12-03 22:26:30 +000093 isConstantGlobal(constant) {
Chris Lattner37798642001-09-18 04:01:05 +000094 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
Chris Lattnerda975502001-09-10 07:58:01 +000095}
96
97// Specialize setName to take care of symbol table majik
Chris Lattner7f74a562002-01-20 22:54:45 +000098void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
Chris Lattnerda975502001-09-10 07:58:01 +000099 Module *P;
100 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
101 "Invalid symtab argument!");
102 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
103 Value::setName(name);
104 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
105}