Add Module::getTypeName
[oota-llvm.git] / lib / VMCore / Module.cpp
1 //===-- Module.cpp - Implement the Module class ------------------*- C++ -*--=//
2 //
3 // This file implements the Module class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Module.h"
8 #include "llvm/Function.h"
9 #include "llvm/GlobalVariable.h"
10 #include "llvm/InstrTypes.h"
11 #include "llvm/Type.h"
12 #include "llvm/ConstantVals.h"
13 #include "llvm/DerivedTypes.h"
14 #include "Support/STLExtras.h"
15 #include "ValueHolderImpl.h"
16 #include <map>
17
18 // Instantiate Templates - This ugliness is the price we have to pay
19 // for having a DefHolderImpl.h file seperate from DefHolder.h!  :(
20 //
21 template class ValueHolder<GlobalVariable, Module, Module>;
22 template class ValueHolder<Function, Module, Module>;
23
24 // Define the GlobalValueRefMap as a struct that wraps a map so that we don't
25 // have Module.h depend on <map>
26 //
27 struct GlobalValueRefMap : public std::map<GlobalValue*, ConstantPointerRef*>{
28 };
29
30
31 Module::Module()
32   : Value(Type::VoidTy, Value::ModuleVal, ""), SymTabValue(this),
33     GlobalList(this, this), FunctionList(this, this), GVRefMap(0) {
34 }
35
36 Module::~Module() {
37   dropAllReferences();
38   GlobalList.delete_all();
39   GlobalList.setParent(0);
40   FunctionList.delete_all();
41   FunctionList.setParent(0);
42 }
43
44 // getOrInsertFunction - Look up the specified function in the module symbol
45 // table.  If it does not exist, add a prototype for the function and return
46 // it.  This is nice because it allows most passes to get away with not handling
47 // the symbol table directly for this common task.
48 //
49 Function *Module::getOrInsertFunction(const std::string &Name,
50                                       const FunctionType *Ty) {
51   SymbolTable *SymTab = getSymbolTableSure();
52
53   // See if we have a definitions for the specified function already...
54   if (Value *V = SymTab->lookup(PointerType::get(Ty), Name)) {
55     return cast<Function>(V);      // Yup, got it
56   } else {                         // Nope, add one
57     Function *New = new Function(Ty, false, Name);
58     FunctionList.push_back(New);
59     return New;                    // Return the new prototype...
60   }
61 }
62
63 // getFunction - Look up the specified function in the module symbol table.
64 // If it does not exist, return null.
65 //
66 Function *Module::getFunction(const std::string &Name, const FunctionType *Ty) {
67   SymbolTable *SymTab = getSymbolTable();
68   if (SymTab == 0) return 0;  // No symtab, no symbols...
69
70   return cast_or_null<Function>(SymTab->lookup(PointerType::get(Ty), Name));
71 }
72
73 // addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
74 // there is already an entry for this name, true is returned and the symbol
75 // table is not modified.
76 //
77 bool Module::addTypeName(const std::string &Name, const Type *Ty) {
78   SymbolTable *ST = getSymbolTableSure();
79
80   if (ST->lookup(Type::TypeTy, Name)) return true;  // Already in symtab...
81   
82   // Not in symbol table?  Set the name with the Symtab as an argument so the
83   // type knows what to update...
84   ((Value*)Ty)->setName(Name, ST);
85
86   return false;
87 }
88
89 // getTypeName - If there is at least one entry in the symbol table for the
90 // specified type, return it.
91 //
92 std::string Module::getTypeName(const Type *Ty) {
93   const SymbolTable *ST = getSymbolTable();
94   if (ST == 0) return "";  // No symbol table, must not have an entry...
95   if (ST->find(Type::TypeTy) == ST->end())
96     return ""; // No names for types...
97
98   SymbolTable::type_const_iterator TI = ST->type_begin(Type::TypeTy);
99   SymbolTable::type_const_iterator TE = ST->type_end(Type::TypeTy);
100
101   while (TI != TE && TI->second != (const Value*)Ty)
102     ++TI;
103
104   if (TI != TE)  // Must have found an entry!
105     return TI->first;
106   return "";     // Must not have found anything...
107 }
108
109
110 // dropAllReferences() - This function causes all the subinstructions to "let
111 // go" of all references that they are maintaining.  This allows one to
112 // 'delete' a whole class at a time, even though there may be circular
113 // references... first all references are dropped, and all use counts go to
114 // zero.  Then everything is delete'd for real.  Note that no operations are
115 // valid on an object that has "dropped all references", except operator 
116 // delete.
117 //
118 void Module::dropAllReferences() {
119   for_each(FunctionList.begin(), FunctionList.end(),
120            std::mem_fun(&Function::dropAllReferences));
121
122   for_each(GlobalList.begin(), GlobalList.end(),
123            std::mem_fun(&GlobalVariable::dropAllReferences));
124
125   // If there are any GlobalVariable references still out there, nuke them now.
126   // Since all references are hereby dropped, nothing could possibly reference
127   // them still.
128   if (GVRefMap) {
129     for (GlobalValueRefMap::iterator I = GVRefMap->begin(), E = GVRefMap->end();
130          I != E; ++I) {
131       // Delete the ConstantPointerRef node...
132       I->second->destroyConstant();
133     }
134
135     // Since the table is empty, we can now delete it...
136     delete GVRefMap;
137   }
138 }
139
140 // Accessor for the underlying GlobalValRefMap...
141 ConstantPointerRef *Module::getConstantPointerRef(GlobalValue *V){
142   // Create ref map lazily on demand...
143   if (GVRefMap == 0) GVRefMap = new GlobalValueRefMap();
144
145   GlobalValueRefMap::iterator I = GVRefMap->find(V);
146   if (I != GVRefMap->end()) return I->second;
147
148   ConstantPointerRef *Ref = new ConstantPointerRef(V);
149   GVRefMap->insert(std::make_pair(V, Ref));
150
151   return Ref;
152 }
153
154 void Module::mutateConstantPointerRef(GlobalValue *OldGV, GlobalValue *NewGV) {
155   GlobalValueRefMap::iterator I = GVRefMap->find(OldGV);
156   assert(I != GVRefMap->end() && 
157          "mutateConstantPointerRef; OldGV not in table!");
158   ConstantPointerRef *Ref = I->second;
159
160   // Remove the old entry...
161   GVRefMap->erase(I);
162
163   // Insert the new entry...
164   GVRefMap->insert(std::make_pair(NewGV, Ref));
165 }