1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
3 // This file implements the Function & GlobalVariable classes for the VMCore
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Module.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/iOther.h"
11 #include "SymbolTableListTraitsImpl.h"
13 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
14 return F->getBasicBlockList();
17 Argument *ilist_traits<Argument>::createNode() {
18 return new Argument(Type::IntTy);
21 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
22 return F->getArgumentList();
25 // Explicit instantiations of SymbolTableListTraits since some of the methods
26 // are not in the public header file...
27 template SymbolTableListTraits<Argument, Function, Function>;
28 template SymbolTableListTraits<BasicBlock, Function, Function>;
30 //===----------------------------------------------------------------------===//
31 // Argument Implementation
32 //===----------------------------------------------------------------------===//
34 // Specialize setName to take care of symbol table majik
35 void Argument::setName(const std::string &name, SymbolTable *ST) {
37 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
38 "Invalid symtab argument!");
39 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
41 if (P && hasName()) P->getSymbolTable()->insert(this);
44 //===----------------------------------------------------------------------===//
45 // Function Implementation
46 //===----------------------------------------------------------------------===//
49 Function::Function(const FunctionType *Ty, bool isInternal,
50 const std::string &name, Module *ParentModule)
51 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
52 BasicBlocks.setItemParent(this);
53 BasicBlocks.setParent(this);
54 ArgumentList.setItemParent(this);
55 ArgumentList.setParent(this);
56 ParentSymTab = SymTab = 0;
59 ParentModule->getFunctionList().push_back(this);
62 Function::~Function() {
63 dropAllReferences(); // After this it is safe to delete instructions.
65 BasicBlocks.clear(); // Delete all basic blocks...
67 // Delete all of the method arguments and unlink from symbol table...
69 ArgumentList.setParent(0);
73 // Specialize setName to take care of symbol table majik
74 void Function::setName(const std::string &name, SymbolTable *ST) {
76 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
77 "Invalid symtab argument!");
78 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
80 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
83 void Function::setParent(Module *parent) {
86 // Relink symbol tables together...
87 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
88 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
91 const FunctionType *Function::getFunctionType() const {
92 return cast<FunctionType>(getType()->getElementType());
95 const Type *Function::getReturnType() const {
96 return getFunctionType()->getReturnType();
99 SymbolTable *Function::getSymbolTableSure() {
100 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
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!
107 bool Function::hasSymbolTable() const {
108 if (!SymTab) return false;
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!
120 // 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
128 void Function::dropAllReferences() {
129 for (iterator I = begin(), E = end(); I != E; ++I)
130 I->dropAllReferences();
133 //===----------------------------------------------------------------------===//
134 // GlobalVariable Implementation
135 //===----------------------------------------------------------------------===//
137 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
138 Constant *Initializer,
139 const std::string &Name)
140 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
141 isConstantGlobal(constant) {
142 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
145 // Specialize setName to take care of symbol table majik
146 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
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);