blob: c3d8e87f8c1441b404da4c936242423216244ed1 [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 Lattner7a787222001-11-26 19:14:56 +000028Method::Method(const MethodType *Ty, bool isInternal, const string &name)
29 : GlobalValue(PointerType::get(Ty), Value::MethodVal, isInternal, name),
Vikram S. Adveb375b892001-11-08 04:38:58 +000030 SymTabValue(this), BasicBlocks(this), ArgumentList(this, this) {
Chris Lattner38569342001-10-01 20:11:19 +000031 assert(::isa<MethodType>(Ty) && "Method signature must be of method type!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000032}
33
34Method::~Method() {
35 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 Lattner5c764a52001-09-07 16:47:18 +000048void Method::setName(const 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
57void Method::setParent(Module *parent) {
58 Parent = parent;
59
60 // Relink symbol tables together...
61 setParentSymTab(Parent ? Parent->getSymbolTableSure() : 0);
62}
63
Chris Lattner7fac0702001-10-03 14:53:21 +000064const MethodType *Method::getMethodType() const {
65 return cast<MethodType>(cast<PointerType>(getType())->getValueType());
66}
67
Chris Lattner2f7c9632001-06-06 20:29:01 +000068const Type *Method::getReturnType() const {
Chris Lattner7fac0702001-10-03 14:53:21 +000069 return getMethodType()->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//
80void Method::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 Lattner37798642001-09-18 04:01:05 +000090 const 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
97void GlobalVariable::setName(const string &name, SymbolTable *ST) {
98 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}