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 "llvm/Intrinsics.h"
12 #include "Support/LeakDetector.h"
13 #include "SymbolTableListTraitsImpl.h"
15 BasicBlock *ilist_traits<BasicBlock>::createNode() {
16 BasicBlock *Ret = new BasicBlock();
17 // This should not be garbage monitored.
18 LeakDetector::removeGarbageObject(Ret);
22 iplist<BasicBlock> &ilist_traits<BasicBlock>::getList(Function *F) {
23 return F->getBasicBlockList();
26 Argument *ilist_traits<Argument>::createNode() {
27 Argument *Ret = new Argument(Type::IntTy);
28 // This should not be garbage monitored.
29 LeakDetector::removeGarbageObject(Ret);
33 iplist<Argument> &ilist_traits<Argument>::getList(Function *F) {
34 return F->getArgumentList();
37 // Explicit instantiations of SymbolTableListTraits since some of the methods
38 // are not in the public header file...
39 template SymbolTableListTraits<Argument, Function, Function>;
40 template SymbolTableListTraits<BasicBlock, Function, Function>;
42 //===----------------------------------------------------------------------===//
43 // Argument Implementation
44 //===----------------------------------------------------------------------===//
46 Argument::Argument(const Type *Ty, const std::string &Name, Function *Par)
47 : Value(Ty, Value::ArgumentVal, Name) {
50 // Make sure that we get added to a function
51 LeakDetector::addGarbageObject(this);
54 Par->getArgumentList().push_back(this);
58 // Specialize setName to take care of symbol table majik
59 void Argument::setName(const std::string &name, SymbolTable *ST) {
61 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
62 "Invalid symtab argument!");
63 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
65 if (P && hasName()) P->getSymbolTable().insert(this);
68 void Argument::setParent(Function *parent) {
70 LeakDetector::addGarbageObject(this);
73 LeakDetector::removeGarbageObject(this);
77 //===----------------------------------------------------------------------===//
78 // Function Implementation
79 //===----------------------------------------------------------------------===//
81 Function::Function(const FunctionType *Ty, LinkageTypes Linkage,
82 const std::string &name, Module *ParentModule)
83 : GlobalValue(PointerType::get(Ty), Value::FunctionVal, Linkage, name) {
84 BasicBlocks.setItemParent(this);
85 BasicBlocks.setParent(this);
86 ArgumentList.setItemParent(this);
87 ArgumentList.setParent(this);
88 SymTab = new SymbolTable();
90 // Create the arguments vector, all arguments start out unnamed.
91 for (unsigned i = 0, e = Ty->getNumParams(); i != e; ++i) {
92 assert(Ty->getParamType(i) != Type::VoidTy &&
93 "Cannot have void typed arguments!");
94 ArgumentList.push_back(new Argument(Ty->getParamType(i)));
97 // Make sure that we get added to a function
98 LeakDetector::addGarbageObject(this);
101 ParentModule->getFunctionList().push_back(this);
104 Function::~Function() {
105 dropAllReferences(); // After this it is safe to delete instructions.
107 BasicBlocks.clear(); // Delete all basic blocks...
109 // Delete all of the method arguments and unlink from symbol table...
110 ArgumentList.clear();
111 ArgumentList.setParent(0);
115 // Specialize setName to take care of symbol table majik
116 void Function::setName(const std::string &name, SymbolTable *ST) {
118 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
119 "Invalid symtab argument!");
120 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
121 Value::setName(name);
122 if (P && getName() != "") P->getSymbolTable().insert(this);
125 void Function::setParent(Module *parent) {
127 LeakDetector::addGarbageObject(this);
130 LeakDetector::removeGarbageObject(this);
133 const FunctionType *Function::getFunctionType() const {
134 return cast<FunctionType>(getType()->getElementType());
137 const Type *Function::getReturnType() const {
138 return getFunctionType()->getReturnType();
141 // dropAllReferences() - This function causes all the subinstructions to "let
142 // go" of all references that they are maintaining. This allows one to
143 // 'delete' a whole class at a time, even though there may be circular
144 // references... first all references are dropped, and all use counts go to
145 // zero. Then everything is delete'd for real. Note that no operations are
146 // valid on an object that has "dropped all references", except operator
149 void Function::dropAllReferences() {
150 for (iterator I = begin(), E = end(); I != E; ++I)
151 I->dropAllReferences();
154 /// getIntrinsicID - This method returns the ID number of the specified
155 /// function, or LLVMIntrinsic::not_intrinsic if the function is not an
156 /// instrinsic, or if the pointer is null. This value is always defined to be
157 /// zero to allow easy checking for whether a function is intrinsic or not. The
158 /// particular intrinsic functions which correspond to this value are defined in
159 /// llvm/Intrinsics.h.
161 unsigned Function::getIntrinsicID() const {
162 if (getName().size() <= 5 || getName()[4] != '.' || getName()[0] != 'l' ||
163 getName()[1] != 'l' || getName()[2] != 'v' || getName()[3] != 'm')
164 return 0; // All intrinsics start with 'llvm.'
166 switch (getName()[5]) {
168 if (getName() == "llvm.longjmp") return LLVMIntrinsic::longjmp;
171 if (getName() == "llvm.setjmp") return LLVMIntrinsic::setjmp;
174 if (getName() == "llvm.va_copy") return LLVMIntrinsic::va_copy;
175 if (getName() == "llvm.va_end") return LLVMIntrinsic::va_end;
176 if (getName() == "llvm.va_start") return LLVMIntrinsic::va_start;
179 // The "llvm." namespace is reserved!
180 assert(0 && "Unknown LLVM intrinsic function!");
185 //===----------------------------------------------------------------------===//
186 // GlobalVariable Implementation
187 //===----------------------------------------------------------------------===//
189 GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link,
190 Constant *Initializer,
191 const std::string &Name, Module *ParentModule)
192 : GlobalValue(PointerType::get(Ty), Value::GlobalVariableVal, Link, Name),
193 isConstantGlobal(constant) {
194 if (Initializer) Operands.push_back(Use((Value*)Initializer, this));
196 LeakDetector::addGarbageObject(this);
199 ParentModule->getGlobalList().push_back(this);
202 void GlobalVariable::setParent(Module *parent) {
204 LeakDetector::addGarbageObject(this);
207 LeakDetector::removeGarbageObject(this);
210 // Specialize setName to take care of symbol table majik
211 void GlobalVariable::setName(const std::string &name, SymbolTable *ST) {
213 assert((ST == 0 || (!getParent() || ST == &getParent()->getSymbolTable())) &&
214 "Invalid symtab argument!");
215 if ((P = getParent()) && hasName()) P->getSymbolTable().remove(this);
216 Value::setName(name);
217 if (P && getName() != "") P->getSymbolTable().insert(this);