1 //===-- SymbolTable.cpp - Implement the SymbolTable class -------------------=//
3 // This file implements the SymbolTable class for the VMCore library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/SymbolTable.h"
8 #include "llvm/InstrTypes.h"
9 #include "llvm/DerivedTypes.h"
10 #include "llvm/Module.h"
11 #include "llvm/Function.h"
12 #include "Support/StringExtras.h"
22 #define DEBUG_SYMBOL_TABLE 0
23 #define DEBUG_ABSTYPE 0
25 SymbolTable::~SymbolTable() {
26 // Drop all abstract type references in the type plane...
27 iterator TyPlane = find(Type::TypeTy);
28 if (TyPlane != end()) {
29 VarMap &TyP = TyPlane->second;
30 for (VarMap::iterator I = TyP.begin(), E = TyP.end(); I != E; ++I) {
31 const Type *Ty = cast<const Type>(I->second);
32 if (Ty->isAbstract()) // If abstract, drop the reference...
33 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
37 // TODO: FIXME: BIG ONE: This doesn't unreference abstract types for the planes
38 // that could still have entries!
40 #ifndef NDEBUG // Only do this in -g mode...
41 bool LeftoverValues = true;
42 for (iterator i = begin(); i != end(); ++i) {
43 for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
44 if (!isa<Constant>(I->second) && !isa<Type>(I->second)) {
45 cerr << "Value still in symbol table! Type = '"
46 << i->first->getDescription() << "' Name = '"
48 LeftoverValues = false;
52 assert(LeftoverValues && "Values remain in symbol table!");
56 // getUniqueName - Given a base name, return a string that is either equal to
57 // it (or derived from it) that does not already occur in the symbol table for
58 // the specified type.
60 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
61 iterator I = find(Ty);
62 if (I == end()) return BaseName;
64 string TryName = BaseName;
66 type_iterator End = I->second.end();
68 while (I->second.find(TryName) != End) // Loop until we find unoccupied
69 TryName = BaseName + utostr(++Counter); // Name in the symbol table
75 // lookup - Returns null on failure...
76 Value *SymbolTable::localLookup(const Type *Ty, const string &Name) {
77 iterator I = find(Ty);
78 if (I != end()) { // We have symbols in that plane...
79 type_iterator J = I->second.find(Name);
80 if (J != I->second.end()) // and the name is in our hash table...
87 // lookup - Returns null on failure...
88 Value *SymbolTable::lookup(const Type *Ty, const string &Name) {
89 Value *LV = localLookup(Ty, Name);
91 return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
94 void SymbolTable::remove(Value *N) {
95 assert(N->hasName() && "Value doesn't have name!");
96 if (InternallyInconsistent) return;
98 iterator I = find(N->getType());
100 "Trying to remove a type that doesn't have a plane yet!");
101 removeEntry(I, I->second.find(N->getName()));
104 // removeEntry - Remove a value from the symbol table...
106 Value *SymbolTable::removeEntry(iterator Plane, type_iterator Entry) {
107 if (InternallyInconsistent) return 0;
108 assert(Plane != super::end() &&
109 Entry != Plane->second.end() && "Invalid entry to remove!");
111 Value *Result = Entry->second;
112 const Type *Ty = Result->getType();
113 #if DEBUG_SYMBOL_TABLE
114 cerr << this << " Removing Value: " << Result->getName() << endl;
117 // Remove the value from the plane...
118 Plane->second.erase(Entry);
120 // If the plane is empty, remove it now!
121 if (Plane->second.empty()) {
122 // If the plane represented an abstract type that we were interested in,
123 // unlink ourselves from this plane.
125 if (Plane->first->isAbstract()) {
127 cerr << "Plane Empty: Removing type: " << Plane->first->getDescription()
130 cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
136 // If we are removing an abstract type, remove the symbol table from it's use
138 if (Ty == Type::TypeTy) {
139 const Type *T = cast<const Type>(Result);
140 if (T->isAbstract()) {
142 cerr << "Removing abs type from symtab" << T->getDescription() << endl;
144 cast<DerivedType>(T)->removeAbstractTypeUser(this);
151 // insertEntry - Insert a value into the symbol table with the specified
154 void SymbolTable::insertEntry(const string &Name, const Type *VTy, Value *V) {
156 // Check to see if there is a naming conflict. If so, rename this value!
157 if (localLookup(VTy, Name)) {
158 string UniqueName = getUniqueName(VTy, Name);
159 assert(InternallyInconsistent == false && "Infinite loop inserting entry!");
160 InternallyInconsistent = true;
161 V->setName(UniqueName, this);
162 InternallyInconsistent = false;
166 #if DEBUG_SYMBOL_TABLE
167 cerr << this << " Inserting definition: " << Name << ": "
168 << VTy->getDescription() << endl;
171 iterator I = find(VTy);
172 if (I == end()) { // Not in collection yet... insert dummy entry
173 // Insert a new empty element. I points to the new elements.
174 I = super::insert(make_pair(VTy, VarMap())).first;
175 assert(I != end() && "How did insert fail?");
177 // Check to see if the type is abstract. If so, it might be refined in the
178 // future, which would cause the plane of the old type to get merged into
181 if (VTy->isAbstract()) {
182 cast<DerivedType>(VTy)->addAbstractTypeUser(this);
184 cerr << "Added abstract type value: " << VTy->getDescription() << endl;
189 I->second.insert(make_pair(Name, V));
191 // If we are adding an abstract type, add the symbol table to it's use list.
192 if (VTy == Type::TypeTy) {
193 const Type *T = cast<const Type>(V);
194 if (T->isAbstract()) {
195 cast<DerivedType>(T)->addAbstractTypeUser(this);
197 cerr << "Added abstract type to ST: " << T->getDescription() << endl;
203 // This function is called when one of the types in the type plane are refined
204 void SymbolTable::refineAbstractType(const DerivedType *OldType,
205 const Type *NewType) {
206 if (OldType == NewType && OldType->isAbstract())
207 return; // Noop, don't waste time dinking around
209 // Search to see if we have any values of the type oldtype. If so, we need to
210 // move them into the newtype plane...
211 iterator TPI = find(OldType);
212 if (OldType != NewType && TPI != end()) {
213 // Get a handle to the new type plane...
214 iterator NewTypeIt = find(NewType);
215 if (NewTypeIt == super::end()) { // If no plane exists, add one
216 NewTypeIt = super::insert(make_pair(NewType, VarMap())).first;
218 if (NewType->isAbstract()) {
219 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
221 cerr << "[Added] refined to abstype: "<<NewType->getDescription()<<endl;
226 VarMap &NewPlane = NewTypeIt->second;
227 VarMap &OldPlane = TPI->second;
228 while (!OldPlane.empty()) {
229 pair<const string, Value*> V = *OldPlane.begin();
231 // Check to see if there is already a value in the symbol table that this
232 // would collide with.
233 type_iterator TI = NewPlane.find(V.first);
234 if (TI != NewPlane.end() && TI->second == V.second) {
237 } else if (TI != NewPlane.end()) {
238 // The only thing we are allowing for now is two method prototypes being
241 Function *ExistM = dyn_cast<Function>(TI->second);
242 Function *NewM = dyn_cast<Function>(V.second);
244 if (ExistM && NewM && ExistM->isExternal() && NewM->isExternal()) {
245 // Ok we have two external methods. Make all uses of the new one
246 // use the old one...
248 NewM->replaceAllUsesWith(ExistM);
250 // Now we just convert it to an unnamed method... which won't get
251 // added to our symbol table. The problem is that if we call
252 // setName on the method that it will try to remove itself from
253 // the symbol table and die... because it's not in the symtab
254 // right now. To fix this, we have an internally consistent flag
255 // that turns remove into a noop. Thus the name will get null'd
256 // out, but the symbol table won't get upset.
258 assert(InternallyInconsistent == false &&
259 "Symbol table already inconsistent!");
260 InternallyInconsistent = true;
262 // Remove newM from the symtab
264 InternallyInconsistent = false;
266 // Now we can remove this method from the module entirely...
267 NewM->getParent()->getFunctionList().remove(NewM);
271 assert(0 && "Two ploanes folded together with overlapping "
275 insertEntry(V.first, NewType, V.second);
278 // Remove the item from the old type plane
279 OldPlane.erase(OldPlane.begin());
282 // Ok, now we are not referencing the type anymore... take me off your user
285 cerr << "Removing type " << OldType->getDescription() << endl;
287 OldType->removeAbstractTypeUser(this);
289 // Remove the plane that is no longer used
291 } else if (TPI != end()) {
292 assert(OldType == NewType);
294 cerr << "Removing SELF type " << OldType->getDescription() << endl;
296 OldType->removeAbstractTypeUser(this);
299 TPI = find(Type::TypeTy);
301 // Loop over all of the types in the symbol table, replacing any references to
302 // OldType with references to NewType. Note that there may be multiple
303 // occurances, and although we only need to remove one at a time, it's faster
304 // to remove them all in one pass.
306 VarMap &TyPlane = TPI->second;
307 for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
308 if (I->second == (Value*)OldType) { // FIXME when Types aren't const.
310 cerr << "Removing type " << OldType->getDescription() << endl;
312 OldType->removeAbstractTypeUser(this);
314 I->second = (Value*)NewType; // TODO FIXME when types aren't const
315 if (NewType->isAbstract()) {
317 cerr << "Added type " << NewType->getDescription() << endl;
319 cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
327 #include "llvm/Assembly/Writer.h"
330 static void DumpVal(const pair<const string, Value *> &V) {
331 cout << " '" << V.first << "' = " << V.second << "\n";
334 static void DumpPlane(const pair<const Type *, map<const string, Value *> >&P) {
335 cout << " Plane: " << P.first << "\n";
336 for_each(P.second.begin(), P.second.end(), DumpVal);
339 void SymbolTable::dump() const {
340 cout << "Symbol table dump:\n";
341 for_each(begin(), end(), DumpPlane);
345 ParentSymTab->dump();