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/Support/StringExtras.h"
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/Method.h"
13 #define DEBUG_SYMBOL_TABLE 0
14 #define DEBUG_ABSTYPE 0
16 SymbolTable::~SymbolTable() {
17 // Drop all abstract type references in the type plane...
18 iterator TyPlane = find(Type::TypeTy);
19 if (TyPlane != end()) {
20 VarMap &TyP = TyPlane->second;
21 for (VarMap::iterator I = TyP.begin(), E = TyP.end(); I != E; ++I) {
22 const Type *Ty = cast<const Type>(I->second);
23 if (Ty->isAbstract()) // If abstract, drop the reference...
24 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
27 #ifndef NDEBUG // Only do this in -g mode...
28 bool LeftoverValues = true;
29 for (iterator i = begin(); i != end(); ++i) {
30 for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
31 if (!isa<ConstPoolVal>(I->second) && !isa<Type>(I->second)) {
32 cerr << "Value still in symbol table! Type = '"
33 << i->first->getDescription() << "' Name = '" << I->first << "'\n";
34 LeftoverValues = false;
38 assert(LeftoverValues && "Values remain in symbol table!");
42 // getUniqueName - Given a base name, return a string that is either equal to
43 // it (or derived from it) that does not already occur in the symbol table for
44 // the specified type.
46 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
47 iterator I = find(Ty);
48 if (I == end()) return BaseName;
50 string TryName = BaseName;
52 type_iterator End = I->second.end();
54 while (I->second.find(TryName) != End) // Loop until we find unoccupied
55 TryName = BaseName + utostr(++Counter); // Name in the symbol table
61 // lookup - Returns null on failure...
62 Value *SymbolTable::lookup(const Type *Ty, const string &Name) {
63 iterator I = find(Ty);
64 if (I != end()) { // We have symbols in that plane...
65 type_iterator J = I->second.find(Name);
66 if (J != I->second.end()) // and the name is in our hash table...
70 return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
73 void SymbolTable::remove(Value *N) {
74 assert(N->hasName() && "Value doesn't have name!");
76 iterator I = find(N->getType());
77 removeEntry(I, I->second.find(N->getName()));
80 // removeEntry - Remove a value from the symbol table...
82 Value *SymbolTable::removeEntry(iterator Plane, type_iterator Entry) {
83 if (InternallyInconsistent) return 0;
84 assert(Plane != super::end() &&
85 Entry != Plane->second.end() && "Invalid entry to remove!");
87 Value *Result = Entry->second;
88 const Type *Ty = Result->getType();
89 #if DEBUG_SYMBOL_TABLE
90 cerr << this << " Removing Value: " << Result->getName() << endl;
93 // Remove the value from the plane...
94 Plane->second.erase(Entry);
96 // If the plane is empty, remove it now!
97 if (Plane->second.empty()) {
98 // If the plane represented an abstract type that we were interested in,
99 // unlink ourselves from this plane.
101 if (Plane->first->isAbstract()) {
103 cerr << "Plane Empty: Removing type: " << Plane->first->getDescription()
106 cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
112 // If we are removing an abstract type, remove the symbol table from it's use
114 if (Ty == Type::TypeTy) {
115 const Type *T = cast<const Type>(Result);
116 if (T->isAbstract()) {
118 cerr << "Removing abs type from symtab" << T->getDescription() << endl;
120 cast<DerivedType>(T)->removeAbstractTypeUser(this);
127 // insertEntry - Insert a value into the symbol table with the specified
130 void SymbolTable::insertEntry(const string &Name, const Type *VTy, Value *V) {
131 // TODO: The typeverifier should catch this when its implemented
132 assert(lookup(VTy, Name) == 0 &&
133 "SymbolTable::insertEntry - Name already in symbol table!");
135 #if DEBUG_SYMBOL_TABLE
136 cerr << this << " Inserting definition: " << Name << ": "
137 << VTy->getDescription() << endl;
140 iterator I = find(VTy);
141 if (I == end()) { // Not in collection yet... insert dummy entry
142 // Insert a new empty element. I points to the new elements.
143 I = super::insert(make_pair(VTy, VarMap())).first;
144 assert(I != end() && "How did insert fail?");
146 // Check to see if the type is abstract. If so, it might be refined in the
147 // future, which would cause the plane of the old type to get merged into
150 if (VTy->isAbstract()) {
151 cast<DerivedType>(VTy)->addAbstractTypeUser(this);
153 cerr << "Added abstract type value: " << VTy->getDescription() << endl;
158 I->second.insert(make_pair(Name, V));
160 // If we are adding an abstract type, add the symbol table to it's use list.
161 if (VTy == Type::TypeTy) {
162 const Type *T = cast<const Type>(V);
163 if (T->isAbstract()) {
164 cast<DerivedType>(T)->addAbstractTypeUser(this);
166 cerr << "Added abstract type to ST: " << T->getDescription() << endl;
172 // This function is called when one of the types in the type plane are refined
173 void SymbolTable::refineAbstractType(const DerivedType *OldType,
174 const Type *NewType) {
175 if (OldType == NewType) return; // Noop, don't waste time dinking around
177 // Get a handle to the new type plane...
178 iterator NewTypeIt = find(NewType);
179 if (NewTypeIt == super::end()) { // If no plane exists, add one
180 NewTypeIt = super::insert(make_pair(NewType, VarMap())).first;
182 if (NewType->isAbstract()) {
183 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
185 cerr << "refined to abstype: " << NewType->getDescription() <<endl;
190 VarMap &NewPlane = NewTypeIt->second;
192 // Search to see if we have any values of the type oldtype. If so, we need to
193 // move them into the newtype plane...
194 iterator TPI = find(OldType);
196 VarMap &OldPlane = TPI->second;
197 while (!OldPlane.empty()) {
198 pair<const string, Value*> V = *OldPlane.begin();
200 // Check to see if there is already a value in the symbol table that this
201 // would collide with.
202 type_iterator TI = NewPlane.find(V.first);
203 if (TI != NewPlane.end() && TI->second == V.second) {
206 } else if (TI != NewPlane.end()) {
207 // The only thing we are allowing for now is two method prototypes being
210 Method *ExistM = dyn_cast<Method>(TI->second);
211 Method *NewM = dyn_cast<Method>(V.second);
213 if (ExistM && NewM && ExistM->isExternal() && NewM->isExternal()) {
214 // Ok we have two external methods. Make all uses of the new one
215 // use the old one...
217 NewM->replaceAllUsesWith(ExistM);
219 // Now we just convert it to an unnamed method... which won't get
220 // added to our symbol table. The problem is that if we call
221 // setName on the method that it will try to remove itself from
222 // the symbol table and die... because it's not in the symtab
223 // right now. To fix this, we have an internally consistent flag
224 // that turns remove into a noop. Thus the name will get null'd
225 // out, but the symbol table won't get upset.
227 InternallyInconsistent = true;
229 // Remove newM from the symtab
231 InternallyInconsistent = false;
233 assert(0 && "Two ploanes folded together with overlapping "
237 insertEntry(V.first, NewType, V.second);
240 // Remove the item from the old type plane
241 OldPlane.erase(OldPlane.begin());
244 // Ok, now we are not referencing the type anymore... take me off your user
247 cerr << "Removing type " << OldType->getDescription() << endl;
249 OldType->removeAbstractTypeUser(this);
251 // Remove the plane that is no longer used
255 TPI = find(Type::TypeTy);
256 assert(TPI != end() &&"Type plane not in symbol table but we contain types!");
258 // Loop over all of the types in the symbol table, replacing any references to
259 // OldType with references to NewType. Note that there may be multiple
260 // occurances, and although we only need to remove one at a time, it's faster
261 // to remove them all in one pass.
263 VarMap &TyPlane = TPI->second;
264 for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
265 if (I->second == (Value*)OldType) { // FIXME when Types aren't const.
267 cerr << "Removing type " << OldType->getDescription() << endl;
269 OldType->removeAbstractTypeUser(this);
271 I->second = (Value*)NewType; // TODO FIXME when types aren't const
272 if (NewType->isAbstract()) {
274 cerr << "Added type " << NewType->getDescription() << endl;
276 cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
283 #include "llvm/Assembly/Writer.h"
286 static void DumpVal(const pair<const string, Value *> &V) {
287 cout << " '" << V.first << "' = " << V.second << endl;
290 static void DumpPlane(const pair<const Type *, map<const string, Value *> >&P) {
291 cout << " Plane: " << P.first << endl;
292 for_each(P.second.begin(), P.second.end(), DumpVal);
295 void SymbolTable::dump() const {
296 cout << "Symbol table dump:\n";
297 for_each(begin(), end(), DumpPlane);
301 ParentSymTab->dump();