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"
21 #define DEBUG_SYMBOL_TABLE 0
22 #define DEBUG_ABSTYPE 0
24 SymbolTable::~SymbolTable() {
25 // Drop all abstract type references in the type plane...
26 iterator TyPlane = find(Type::TypeTy);
27 if (TyPlane != end()) {
28 VarMap &TyP = TyPlane->second;
29 for (VarMap::iterator I = TyP.begin(), E = TyP.end(); I != E; ++I) {
30 const Type *Ty = cast<const Type>(I->second);
31 if (Ty->isAbstract()) // If abstract, drop the reference...
32 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
36 // TODO: FIXME: BIG ONE: This doesn't unreference abstract types for the planes
37 // that could still have entries!
39 #ifndef NDEBUG // Only do this in -g mode...
40 bool LeftoverValues = true;
41 for (iterator i = begin(); i != end(); ++i) {
42 for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
43 if (!isa<Constant>(I->second) && !isa<Type>(I->second)) {
44 cerr << "Value still in symbol table! Type = '"
45 << i->first->getDescription() << "' Name = '"
47 LeftoverValues = false;
51 assert(LeftoverValues && "Values remain in symbol table!");
55 // getUniqueName - Given a base name, return a string that is either equal to
56 // it (or derived from it) that does not already occur in the symbol table for
57 // the specified type.
59 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
60 iterator I = find(Ty);
61 if (I == end()) return BaseName;
63 string TryName = BaseName;
65 type_iterator End = I->second.end();
67 while (I->second.find(TryName) != End) // Loop until we find unoccupied
68 TryName = BaseName + utostr(++Counter); // Name in the symbol table
74 // lookup - Returns null on failure...
75 Value *SymbolTable::localLookup(const Type *Ty, const string &Name) {
76 iterator I = find(Ty);
77 if (I != end()) { // We have symbols in that plane...
78 type_iterator J = I->second.find(Name);
79 if (J != I->second.end()) // and the name is in our hash table...
86 // lookup - Returns null on failure...
87 Value *SymbolTable::lookup(const Type *Ty, const string &Name) {
88 Value *LV = localLookup(Ty, Name);
90 return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
93 void SymbolTable::remove(Value *N) {
94 assert(N->hasName() && "Value doesn't have name!");
95 if (InternallyInconsistent) return;
97 iterator I = find(N->getType());
99 "Trying to remove a type that doesn't have a plane yet!");
100 removeEntry(I, I->second.find(N->getName()));
103 // removeEntry - Remove a value from the symbol table...
105 Value *SymbolTable::removeEntry(iterator Plane, type_iterator Entry) {
106 if (InternallyInconsistent) return 0;
107 assert(Plane != super::end() &&
108 Entry != Plane->second.end() && "Invalid entry to remove!");
110 Value *Result = Entry->second;
111 const Type *Ty = Result->getType();
112 #if DEBUG_SYMBOL_TABLE
114 std::cerr << " Removing Value: " << Result->getName() << "\n";
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() << "\n";
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
168 cerr << " Inserting definition: " << Name << ": "
169 << VTy->getDescription() << "\n";
172 iterator I = find(VTy);
173 if (I == end()) { // Not in collection yet... insert dummy entry
174 // Insert a new empty element. I points to the new elements.
175 I = super::insert(make_pair(VTy, VarMap())).first;
176 assert(I != end() && "How did insert fail?");
178 // Check to see if the type is abstract. If so, it might be refined in the
179 // future, which would cause the plane of the old type to get merged into
182 if (VTy->isAbstract()) {
183 cast<DerivedType>(VTy)->addAbstractTypeUser(this);
185 cerr << "Added abstract type value: " << VTy->getDescription() << "\n";
190 I->second.insert(make_pair(Name, V));
192 // If we are adding an abstract type, add the symbol table to it's use list.
193 if (VTy == Type::TypeTy) {
194 const Type *T = cast<const Type>(V);
195 if (T->isAbstract()) {
196 cast<DerivedType>(T)->addAbstractTypeUser(this);
198 cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
204 // This function is called when one of the types in the type plane are refined
205 void SymbolTable::refineAbstractType(const DerivedType *OldType,
206 const Type *NewType) {
207 if (OldType == NewType && OldType->isAbstract())
208 return; // Noop, don't waste time dinking around
210 // Search to see if we have any values of the type oldtype. If so, we need to
211 // move them into the newtype plane...
212 iterator TPI = find(OldType);
213 if (OldType != NewType && TPI != end()) {
214 // Get a handle to the new type plane...
215 iterator NewTypeIt = find(NewType);
216 if (NewTypeIt == super::end()) { // If no plane exists, add one
217 NewTypeIt = super::insert(make_pair(NewType, VarMap())).first;
219 if (NewType->isAbstract()) {
220 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
222 cerr << "[Added] refined to abstype: "<<NewType->getDescription()<<"\n";
227 VarMap &NewPlane = NewTypeIt->second;
228 VarMap &OldPlane = TPI->second;
229 while (!OldPlane.empty()) {
230 pair<const string, Value*> V = *OldPlane.begin();
232 // Check to see if there is already a value in the symbol table that this
233 // would collide with.
234 type_iterator TI = NewPlane.find(V.first);
235 if (TI != NewPlane.end() && TI->second == V.second) {
238 } else if (TI != NewPlane.end()) {
239 // The only thing we are allowing for now is two method prototypes being
242 Function *ExistM = dyn_cast<Function>(TI->second);
243 Function *NewM = dyn_cast<Function>(V.second);
245 if (ExistM && NewM && ExistM->isExternal() && NewM->isExternal()) {
246 // Ok we have two external methods. Make all uses of the new one
247 // use the old one...
249 NewM->replaceAllUsesWith(ExistM);
251 // Now we just convert it to an unnamed method... which won't get
252 // added to our symbol table. The problem is that if we call
253 // setName on the method that it will try to remove itself from
254 // the symbol table and die... because it's not in the symtab
255 // right now. To fix this, we have an internally consistent flag
256 // that turns remove into a noop. Thus the name will get null'd
257 // out, but the symbol table won't get upset.
259 assert(InternallyInconsistent == false &&
260 "Symbol table already inconsistent!");
261 InternallyInconsistent = true;
263 // Remove newM from the symtab
265 InternallyInconsistent = false;
267 // Now we can remove this method from the module entirely...
268 NewM->getParent()->getFunctionList().remove(NewM);
272 assert(0 && "Two ploanes folded together with overlapping "
276 insertEntry(V.first, NewType, V.second);
279 // Remove the item from the old type plane
280 OldPlane.erase(OldPlane.begin());
283 // Ok, now we are not referencing the type anymore... take me off your user
286 cerr << "Removing type " << OldType->getDescription() << "\n";
288 OldType->removeAbstractTypeUser(this);
290 // Remove the plane that is no longer used
292 } else if (TPI != end()) {
293 assert(OldType == NewType);
295 cerr << "Removing SELF type " << OldType->getDescription() << "\n";
297 OldType->removeAbstractTypeUser(this);
300 TPI = find(Type::TypeTy);
302 // Loop over all of the types in the symbol table, replacing any references
303 // to OldType with references to NewType. Note that there may be multiple
304 // occurances, and although we only need to remove one at a time, it's
305 // faster to remove them all in one pass.
307 VarMap &TyPlane = TPI->second;
308 for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
309 if (I->second == (Value*)OldType) { // FIXME when Types aren't const.
311 cerr << "Removing type " << OldType->getDescription() << "\n";
313 OldType->removeAbstractTypeUser(this);
315 I->second = (Value*)NewType; // TODO FIXME when types aren't const
316 if (NewType->isAbstract()) {
318 cerr << "Added type " << NewType->getDescription() << "\n";
320 cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
330 static void DumpVal(const pair<const string, Value *> &V) {
331 std::cout << " '" << V.first << "' = ";
336 static void DumpPlane(const pair<const Type *, map<const string, Value *> >&P) {
337 std::cout << " Plane: ";
340 for_each(P.second.begin(), P.second.end(), DumpVal);
343 void SymbolTable::dump() const {
344 std::cout << "Symbol table dump:\n";
345 for_each(begin(), end(), DumpPlane);
348 std::cout << "Parent ";
349 ParentSymTab->dump();