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/Module.h"
12 #include "llvm/Method.h"
14 #define DEBUG_SYMBOL_TABLE 0
15 #define DEBUG_ABSTYPE 0
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);
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;
39 assert(LeftoverValues && "Values remain in symbol table!");
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.
47 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
48 iterator I = find(Ty);
49 if (I == end()) return BaseName;
51 string TryName = BaseName;
53 type_iterator End = I->second.end();
55 while (I->second.find(TryName) != End) // Loop until we find unoccupied
56 TryName = BaseName + utostr(++Counter); // Name in the symbol table
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...
71 return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
74 void SymbolTable::remove(Value *N) {
75 assert(N->hasName() && "Value doesn't have name!");
77 iterator I = find(N->getType());
78 removeEntry(I, I->second.find(N->getName()));
81 // removeEntry - Remove a value from the symbol table...
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!");
88 Value *Result = Entry->second;
89 const Type *Ty = Result->getType();
90 #if DEBUG_SYMBOL_TABLE
91 cerr << this << " Removing Value: " << Result->getName() << endl;
94 // Remove the value from the plane...
95 Plane->second.erase(Entry);
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.
102 if (Plane->first->isAbstract()) {
104 cerr << "Plane Empty: Removing type: " << Plane->first->getDescription()
107 cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
113 // If we are removing an abstract type, remove the symbol table from it's use
115 if (Ty == Type::TypeTy) {
116 const Type *T = cast<const Type>(Result);
117 if (T->isAbstract()) {
119 cerr << "Removing abs type from symtab" << T->getDescription() << endl;
121 cast<DerivedType>(T)->removeAbstractTypeUser(this);
128 // insertEntry - Insert a value into the symbol table with the specified
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!");
136 #if DEBUG_SYMBOL_TABLE
137 cerr << this << " Inserting definition: " << Name << ": "
138 << VTy->getDescription() << endl;
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?");
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
151 if (VTy->isAbstract()) {
152 cast<DerivedType>(VTy)->addAbstractTypeUser(this);
154 cerr << "Added abstract type value: " << VTy->getDescription() << endl;
159 I->second.insert(make_pair(Name, V));
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);
167 cerr << "Added abstract type to ST: " << T->getDescription() << endl;
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
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;
183 if (NewType->isAbstract()) {
184 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
186 cerr << "refined to abstype: " << NewType->getDescription() <<endl;
191 VarMap &NewPlane = NewTypeIt->second;
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);
197 VarMap &OldPlane = TPI->second;
198 while (!OldPlane.empty()) {
199 pair<const string, Value*> V = *OldPlane.begin();
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) {
207 } else if (TI != NewPlane.end()) {
208 // The only thing we are allowing for now is two method prototypes being
211 Method *ExistM = dyn_cast<Method>(TI->second);
212 Method *NewM = dyn_cast<Method>(V.second);
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...
218 NewM->replaceAllUsesWith(ExistM);
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.
228 InternallyInconsistent = true;
230 // Remove newM from the symtab
232 InternallyInconsistent = false;
234 // Now we can remove this method from the module entirely...
235 NewM->getParent()->getMethodList().remove(NewM);
239 assert(0 && "Two ploanes folded together with overlapping "
243 insertEntry(V.first, NewType, V.second);
246 // Remove the item from the old type plane
247 OldPlane.erase(OldPlane.begin());
250 // Ok, now we are not referencing the type anymore... take me off your user
253 cerr << "Removing type " << OldType->getDescription() << endl;
255 OldType->removeAbstractTypeUser(this);
257 // Remove the plane that is no longer used
261 TPI = find(Type::TypeTy);
262 assert(TPI != end() &&"Type plane not in symbol table but we contain types!");
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.
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.
273 cerr << "Removing type " << OldType->getDescription() << endl;
275 OldType->removeAbstractTypeUser(this);
277 I->second = (Value*)NewType; // TODO FIXME when types aren't const
278 if (NewType->isAbstract()) {
280 cerr << "Added type " << NewType->getDescription() << endl;
282 cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
289 #include "llvm/Assembly/Writer.h"
292 static void DumpVal(const pair<const string, Value *> &V) {
293 cout << " '" << V.first << "' = " << V.second << endl;
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);
301 void SymbolTable::dump() const {
302 cout << "Symbol table dump:\n";
303 for_each(begin(), end(), DumpPlane);
307 ParentSymTab->dump();