Add extra code for debugging linker problems
[oota-llvm.git] / lib / VMCore / SymbolTable.cpp
1 //===-- SymbolTable.cpp - Implement the SymbolTable class -------------------=//
2 //
3 // This file implements the SymbolTable class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/SymbolTable.h"
8 #include "llvm/InstrTypes.h"
9 #include "llvm/Support/StringExtras.h"
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/Module.h"
12 #include "llvm/Method.h"
13
14 #define DEBUG_SYMBOL_TABLE 0
15 #define DEBUG_ABSTYPE 0
16
17 SymbolTable::~SymbolTable() {
18   // Drop all abstract type references in the type plane...
19   iterator TyPlane = find(Type::TypeTy);
20   if (TyPlane != end()) {
21     VarMap &TyP = TyPlane->second;
22     for (VarMap::iterator I = TyP.begin(), E = TyP.end(); I != E; ++I) {
23       const Type *Ty = cast<const Type>(I->second);
24       if (Ty->isAbstract())   // If abstract, drop the reference...
25         cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
26     }
27   }
28 #ifndef NDEBUG   // Only do this in -g mode...
29   bool LeftoverValues = true;
30   for (iterator i = begin(); i != end(); ++i) {
31     for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
32       if (!isa<ConstPoolVal>(I->second) && !isa<Type>(I->second)) {
33         cerr << "Value still in symbol table! Type = '"
34              << i->first->getDescription() << "' Name = '" << I->first << "'\n";
35         LeftoverValues = false;
36       }
37   }
38   
39   assert(LeftoverValues && "Values remain in symbol table!");
40 #endif
41 }
42
43 // getUniqueName - Given a base name, return a string that is either equal to
44 // it (or derived from it) that does not already occur in the symbol table for
45 // the specified type.
46 //
47 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
48   iterator I = find(Ty);
49   if (I == end()) return BaseName;
50
51   string TryName = BaseName;
52   unsigned Counter = 0;
53   type_iterator End = I->second.end();
54
55   while (I->second.find(TryName) != End)     // Loop until we find unoccupied
56     TryName = BaseName + utostr(++Counter);  // Name in the symbol table
57   return TryName;
58 }
59
60
61
62 // lookup - Returns null on failure...
63 Value *SymbolTable::lookup(const Type *Ty, const string &Name) {
64   iterator I = find(Ty);
65   if (I != end()) {                      // We have symbols in that plane...
66     type_iterator J = I->second.find(Name);
67     if (J != I->second.end())            // and the name is in our hash table...
68       return J->second;
69   }
70
71   return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
72 }
73
74 void SymbolTable::remove(Value *N) {
75   assert(N->hasName() && "Value doesn't have name!");
76
77   iterator I = find(N->getType());
78   removeEntry(I, I->second.find(N->getName()));
79 }
80
81 // removeEntry - Remove a value from the symbol table...
82 //
83 Value *SymbolTable::removeEntry(iterator Plane, type_iterator Entry) {
84   if (InternallyInconsistent) return 0;
85   assert(Plane != super::end() &&
86          Entry != Plane->second.end() && "Invalid entry to remove!");
87
88   Value *Result = Entry->second;
89   const Type *Ty = Result->getType();
90 #if DEBUG_SYMBOL_TABLE
91   cerr << this << " Removing Value: " << Result->getName() << endl;
92 #endif
93
94   // Remove the value from the plane...
95   Plane->second.erase(Entry);
96
97   // If the plane is empty, remove it now!
98   if (Plane->second.empty()) {
99     // If the plane represented an abstract type that we were interested in,
100     // unlink ourselves from this plane.
101     //
102     if (Plane->first->isAbstract()) {
103 #if DEBUG_ABSTYPE
104       cerr << "Plane Empty: Removing type: " << Plane->first->getDescription()
105            << endl;
106 #endif
107       cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
108     }
109
110     erase(Plane);
111   }
112
113   // If we are removing an abstract type, remove the symbol table from it's use
114   // list...
115   if (Ty == Type::TypeTy) {
116     const Type *T = cast<const Type>(Result);
117     if (T->isAbstract()) {
118 #if DEBUG_ABSTYPE
119       cerr << "Removing abs type from symtab" << T->getDescription() << endl;
120 #endif
121       cast<DerivedType>(T)->removeAbstractTypeUser(this);
122     }
123   }
124
125   return Result;
126 }
127
128 // insertEntry - Insert a value into the symbol table with the specified
129 // name...
130 //
131 void SymbolTable::insertEntry(const string &Name, const Type *VTy, Value *V) {
132   // TODO: The typeverifier should catch this when its implemented
133   assert(lookup(VTy, Name) == 0 && 
134          "SymbolTable::insertEntry - Name already in symbol table!");
135
136 #if DEBUG_SYMBOL_TABLE
137   cerr << this << " Inserting definition: " << Name << ": " 
138        << VTy->getDescription() << endl;
139 #endif
140
141   iterator I = find(VTy);
142   if (I == end()) {      // Not in collection yet... insert dummy entry
143     // Insert a new empty element.  I points to the new elements.
144     I = super::insert(make_pair(VTy, VarMap())).first;
145     assert(I != end() && "How did insert fail?");
146
147     // Check to see if the type is abstract.  If so, it might be refined in the
148     // future, which would cause the plane of the old type to get merged into
149     // a new type plane.
150     //
151     if (VTy->isAbstract()) {
152       cast<DerivedType>(VTy)->addAbstractTypeUser(this);
153 #if DEBUG_ABSTYPE
154       cerr << "Added abstract type value: " << VTy->getDescription() << endl;
155 #endif
156     }
157   }
158
159   I->second.insert(make_pair(Name, V));
160
161   // If we are adding an abstract type, add the symbol table to it's use list.
162   if (VTy == Type::TypeTy) {
163     const Type *T = cast<const Type>(V);
164     if (T->isAbstract()) {
165       cast<DerivedType>(T)->addAbstractTypeUser(this);
166 #if DEBUG_ABSTYPE
167       cerr << "Added abstract type to ST: " << T->getDescription() << endl;
168 #endif
169     }
170   }
171 }
172
173 // This function is called when one of the types in the type plane are refined
174 void SymbolTable::refineAbstractType(const DerivedType *OldType,
175                                      const Type *NewType) {
176   if (OldType == NewType) return;  // Noop, don't waste time dinking around
177
178   // Get a handle to the new type plane...
179   iterator NewTypeIt = find(NewType);
180   if (NewTypeIt == super::end()) {      // If no plane exists, add one
181     NewTypeIt = super::insert(make_pair(NewType, VarMap())).first;
182
183     if (NewType->isAbstract()) {
184       cast<DerivedType>(NewType)->addAbstractTypeUser(this);
185 #if DEBUG_ABSTYPE
186       cerr << "refined to abstype: " << NewType->getDescription() <<endl;
187 #endif
188     }
189   }
190
191   VarMap &NewPlane = NewTypeIt->second;
192
193   // Search to see if we have any values of the type oldtype.  If so, we need to
194   // move them into the newtype plane...
195   iterator TPI = find(OldType);
196   if (TPI != end()) {
197     VarMap &OldPlane = TPI->second;
198     while (!OldPlane.empty()) {
199       pair<const string, Value*> V = *OldPlane.begin();
200
201       // Check to see if there is already a value in the symbol table that this
202       // would collide with.
203       type_iterator TI = NewPlane.find(V.first);
204       if (TI != NewPlane.end() && TI->second == V.second) {
205         // No action
206
207       } else if (TI != NewPlane.end()) {
208         // The only thing we are allowing for now is two method prototypes being
209         // folded into one.
210         //
211         Method *ExistM = dyn_cast<Method>(TI->second);
212         Method *NewM = dyn_cast<Method>(V.second);
213
214         if (ExistM && NewM && ExistM->isExternal() && NewM->isExternal()) {
215           // Ok we have two external methods.  Make all uses of the new one
216           // use the old one...
217           //
218           NewM->replaceAllUsesWith(ExistM);
219           
220           // Now we just convert it to an unnamed method... which won't get
221           // added to our symbol table.  The problem is that if we call
222           // setName on the method that it will try to remove itself from
223           // the symbol table and die... because it's not in the symtab
224           // right now.  To fix this, we have an internally consistent flag
225           // that turns remove into a noop.  Thus the name will get null'd
226           // out, but the symbol table won't get upset.
227           //
228           InternallyInconsistent = true;
229
230           // Remove newM from the symtab
231           NewM->setName("");
232           InternallyInconsistent = false;
233
234           // Now we can remove this method from the module entirely...
235           NewM->getParent()->getMethodList().remove(NewM);
236           delete NewM;
237
238         } else {
239           assert(0 && "Two ploanes folded together with overlapping "
240                  "value names!");
241         }
242       } else {
243         insertEntry(V.first, NewType, V.second);
244
245       }
246       // Remove the item from the old type plane
247       OldPlane.erase(OldPlane.begin());
248     }
249
250     // Ok, now we are not referencing the type anymore... take me off your user
251     // list please!
252 #if DEBUG_ABSTYPE
253     cerr << "Removing type " << OldType->getDescription() << endl;
254 #endif
255     OldType->removeAbstractTypeUser(this);
256
257     // Remove the plane that is no longer used
258     erase(TPI);
259   }
260
261   TPI = find(Type::TypeTy);
262   assert(TPI != end() &&"Type plane not in symbol table but we contain types!");
263
264   // Loop over all of the types in the symbol table, replacing any references to
265   // OldType with references to NewType.  Note that there may be multiple
266   // occurances, and although we only need to remove one at a time, it's faster
267   // to remove them all in one pass.
268   //
269   VarMap &TyPlane = TPI->second;
270   for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
271     if (I->second == (Value*)OldType) {  // FIXME when Types aren't const.
272 #if DEBUG_ABSTYPE
273       cerr << "Removing type " << OldType->getDescription() << endl;
274 #endif
275       OldType->removeAbstractTypeUser(this);
276
277       I->second = (Value*)NewType;  // TODO FIXME when types aren't const
278       if (NewType->isAbstract()) {
279 #if DEBUG_ABSTYPE
280         cerr << "Added type " << NewType->getDescription() << endl;
281 #endif
282         cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
283       }
284     }
285 }
286
287
288 #ifndef NDEBUG
289 #include "llvm/Assembly/Writer.h"
290 #include <algorithm>
291
292 static void DumpVal(const pair<const string, Value *> &V) {
293   cout << "  '" << V.first << "' = " << V.second << endl;
294 }
295
296 static void DumpPlane(const pair<const Type *, map<const string, Value *> >&P) {
297   cout << "  Plane: " << P.first << endl;
298   for_each(P.second.begin(), P.second.end(), DumpVal);
299 }
300
301 void SymbolTable::dump() const {
302   cout << "Symbol table dump:\n";
303   for_each(begin(), end(), DumpPlane);
304
305   if (ParentSymTab) {
306     cout << "Parent ";
307     ParentSymTab->dump();
308   }
309 }
310
311 #endif