1 //===-- Function.cpp - Implement the Global object classes -------*- C++ -*--=//
3 // This file implements the Function & GlobalVariable classes for the VMCore
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Function.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Module.h"
11 #include "llvm/GlobalVariable.h"
12 #include "llvm/BasicBlock.h"
13 #include "llvm/iOther.h"
14 #include "llvm/Argument.h"
15 #include "SymbolTableListTraitsImpl.h"
17 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
18 return F->getBasicBlockList();
21 Argument *ilist_traits<Argument>::createNode() {
22 return new Argument(Type::IntTy);
25 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
26 return F->getArgumentList();
29 // Explicit instantiations of SymbolTableListTraits since some of the methods
30 // are not in the public header file...
31 template SymbolTableListTraits<Argument, Function, Function>;
32 template SymbolTableListTraits<BasicBlock, Function, Function>;
34 //===----------------------------------------------------------------------===//
35 // Argument Implementation
36 //===----------------------------------------------------------------------===//
38 // Specialize setName to take care of symbol table majik
39 void Argument::setName(const std::string &name, SymbolTable *ST) {
41 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
42 "Invalid symtab argument!");
43 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
45 if (P && hasName()) P->getSymbolTable()->insert(this);
48 //===----------------------------------------------------------------------===//
49 // Function Implementation
50 //===----------------------------------------------------------------------===//
53 Function::Function(const FunctionType *Ty, bool isInternal,
54 const std::string &name)
55 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, isInternal, name) {
56 BasicBlocks.setItemParent(this);
57 BasicBlocks.setParent(this);
58 ArgumentList.setItemParent(this);
59 ArgumentList.setParent(this);
60 ParentSymTab = SymTab = 0;
63 Function::~Function() {
64 dropAllReferences(); // After this it is safe to delete instructions.
66 BasicBlocks.clear(); // Delete all basic blocks...
68 // Delete all of the method arguments and unlink from symbol table...
70 ArgumentList.setParent(0);
74 // Specialize setName to take care of symbol table majik
75 void Function::setName(const std::string &name, SymbolTable *ST) {
77 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
78 "Invalid symtab argument!");
79 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
81 if (P && getName() != "") P->getSymbolTableSure()->insert(this);
84 void Function::setParent(Module *parent) {
87 // Relink symbol tables together...
88 ParentSymTab = Parent ? Parent->getSymbolTableSure() : 0;
89 if (SymTab) SymTab->setParentSymTab(ParentSymTab);
92 const FunctionType *Function::getFunctionType() const {
93 return cast<FunctionType>(getType()->getElementType());
96 const Type *Function::getReturnType() const {
97 return getFunctionType()->getReturnType();
100 SymbolTable *Function::getSymbolTableSure() {
101 if (!SymTab) SymTab = new SymbolTable(ParentSymTab);
105 // hasSymbolTable() - Returns true if there is a symbol table allocated to
106 // this object AND if there is at least one name in it!
108 bool Function::hasSymbolTable() const {
109 if (!SymTab) return false;
111 for (SymbolTable::const_iterator I = SymTab->begin();
112 I != SymTab->end(); ++I) {
113 if (I->second.begin() != I->second.end())
114 return true; // Found nonempty type plane!
121 // dropAllReferences() - This function causes all the subinstructions to "let
122 // go" of all references that they are maintaining. This allows one to
123 // 'delete' a whole class at a time, even though there may be circular
124 // references... first all references are dropped, and all use counts go to
125 // zero. Then everything is delete'd for real. Note that no operations are
126 // valid on an object that has "dropped all references", except operator
129 void Function::dropAllReferences() {
130 for (iterator I = begin(), E = end(); I != E; ++I)
131 I->dropAllReferences();
134 //===----------------------------------------------------------------------===//
135 // GlobalVariable Implementation
136 //===----------------------------------------------------------------------===//
138 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, bool isIntern,
139 Constant *Initializer,
140 const std::string &Name)
141 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, isIntern, Name),
142 isConstantGlobal(constant) {
143 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
146 // Specialize setName to take care of symbol table majik
147 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
149 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
150 "Invalid symtab argument!");
151 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
152 Value::setName(name);
153 if (P && getName() != "") P->getSymbolTableSure()->insert(this);