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/DerivedTypes.h"
9 #include "llvm/Module.h"
10 #include "Support/StringExtras.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<Type>(I->second);
23 if (Ty->isAbstract()) // If abstract, drop the reference...
24 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
28 // TODO: FIXME: BIG ONE: This doesn't unreference abstract types for the planes
29 // that could still have entries!
31 #ifndef NDEBUG // Only do this in -g mode...
32 bool LeftoverValues = true;
33 for (iterator i = begin(); i != end(); ++i) {
34 for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
35 if (!isa<Constant>(I->second) && !isa<Type>(I->second)) {
36 std::cerr << "Value still in symbol table! Type = '"
37 << i->first->getDescription() << "' Name = '"
39 LeftoverValues = false;
43 assert(LeftoverValues && "Values remain in symbol table!");
47 // getUniqueName - Given a base name, return a string that is either equal to
48 // it (or derived from it) that does not already occur in the symbol table for
49 // the specified type.
51 std::string SymbolTable::getUniqueName(const Type *Ty,
52 const std::string &BaseName) {
53 iterator I = find(Ty);
54 if (I == end()) return BaseName;
56 std::string TryName = BaseName;
58 type_iterator End = I->second.end();
60 while (I->second.find(TryName) != End) // Loop until we find unoccupied
61 TryName = BaseName + utostr(++Counter); // Name in the symbol table
67 // lookup - Returns null on failure...
68 Value *SymbolTable::lookup(const Type *Ty, const std::string &Name) {
69 iterator I = find(Ty);
70 if (I != end()) { // We have symbols in that plane...
71 type_iterator J = I->second.find(Name);
72 if (J != I->second.end()) // and the name is in our hash table...
79 void SymbolTable::remove(Value *N) {
80 assert(N->hasName() && "Value doesn't have name!");
81 if (InternallyInconsistent) return;
83 iterator I = find(N->getType());
85 "Trying to remove a type that doesn't have a plane yet!");
86 removeEntry(I, I->second.find(N->getName()));
89 // removeEntry - Remove a value from the symbol table...
91 Value *SymbolTable::removeEntry(iterator Plane, type_iterator Entry) {
92 if (InternallyInconsistent) return 0;
93 assert(Plane != super::end() &&
94 Entry != Plane->second.end() && "Invalid entry to remove!");
96 Value *Result = Entry->second;
97 const Type *Ty = Result->getType();
98 #if DEBUG_SYMBOL_TABLE
100 std::cerr << " Removing Value: " << Result->getName() << "\n";
103 // Remove the value from the plane...
104 Plane->second.erase(Entry);
106 // If the plane is empty, remove it now!
107 if (Plane->second.empty()) {
108 // If the plane represented an abstract type that we were interested in,
109 // unlink ourselves from this plane.
111 if (Plane->first->isAbstract()) {
113 std::cerr << "Plane Empty: Removing type: "
114 << Plane->first->getDescription() << "\n";
116 cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
122 // If we are removing an abstract type, remove the symbol table from it's use
124 if (Ty == Type::TypeTy) {
125 const Type *T = cast<Type>(Result);
126 if (T->isAbstract()) {
128 std::cerr << "Removing abs type from symtab" << T->getDescription()<<"\n";
130 cast<DerivedType>(T)->removeAbstractTypeUser(this);
137 // insertEntry - Insert a value into the symbol table with the specified
140 void SymbolTable::insertEntry(const std::string &Name, const Type *VTy,
143 // Check to see if there is a naming conflict. If so, rename this value!
144 if (lookup(VTy, Name)) {
145 std::string UniqueName = getUniqueName(VTy, Name);
146 assert(InternallyInconsistent == false && "Infinite loop inserting entry!");
147 InternallyInconsistent = true;
148 V->setName(UniqueName, this);
149 InternallyInconsistent = false;
153 #if DEBUG_SYMBOL_TABLE
155 std::cerr << " Inserting definition: " << Name << ": "
156 << VTy->getDescription() << "\n";
159 iterator I = find(VTy);
160 if (I == end()) { // Not in collection yet... insert dummy entry
161 // Insert a new empty element. I points to the new elements.
162 I = super::insert(make_pair(VTy, VarMap())).first;
163 assert(I != end() && "How did insert fail?");
165 // Check to see if the type is abstract. If so, it might be refined in the
166 // future, which would cause the plane of the old type to get merged into
169 if (VTy->isAbstract()) {
170 cast<DerivedType>(VTy)->addAbstractTypeUser(this);
172 std::cerr << "Added abstract type value: " << VTy->getDescription()
178 I->second.insert(make_pair(Name, V));
180 // If we are adding an abstract type, add the symbol table to it's use list.
181 if (VTy == Type::TypeTy) {
182 const Type *T = cast<Type>(V);
183 if (T->isAbstract()) {
184 cast<DerivedType>(T)->addAbstractTypeUser(this);
186 std::cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
192 // This function is called when one of the types in the type plane are refined
193 void SymbolTable::refineAbstractType(const DerivedType *OldType,
194 const Type *NewType) {
195 if (OldType == NewType && OldType->isAbstract())
196 return; // Noop, don't waste time dinking around
198 // Search to see if we have any values of the type oldtype. If so, we need to
199 // move them into the newtype plane...
200 iterator TPI = find(OldType);
201 if (OldType != NewType && TPI != end()) {
202 // Get a handle to the new type plane...
203 iterator NewTypeIt = find(NewType);
204 if (NewTypeIt == super::end()) { // If no plane exists, add one
205 NewTypeIt = super::insert(make_pair(NewType, VarMap())).first;
207 if (NewType->isAbstract()) {
208 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
210 std::cerr << "[Added] refined to abstype: " << NewType->getDescription()
216 VarMap &NewPlane = NewTypeIt->second;
217 VarMap &OldPlane = TPI->second;
218 while (!OldPlane.empty()) {
219 std::pair<const std::string, Value*> V = *OldPlane.begin();
221 // Check to see if there is already a value in the symbol table that this
222 // would collide with.
223 type_iterator TI = NewPlane.find(V.first);
224 if (TI != NewPlane.end() && TI->second == V.second) {
227 } else if (TI != NewPlane.end()) {
228 // The only thing we are allowing for now is two external global values
231 GlobalValue *ExistGV = dyn_cast<GlobalValue>(TI->second);
232 GlobalValue *NewGV = dyn_cast<GlobalValue>(V.second);
234 if (ExistGV && NewGV) {
235 assert((ExistGV->isExternal() || NewGV->isExternal()) &&
236 "Two planes folded together with overlapping value names!");
238 // Make sure that ExistGV is the one we want to keep!
239 if (!NewGV->isExternal() || !NewGV->use_empty()) {
240 std::swap(NewGV, ExistGV);
243 // Ok we have two external global values. Make all uses of the new
244 // one use the old one...
246 assert(NewGV->use_empty() && "No uses allowed on untyped value!");
248 // We cannot replaceAllUsesWith, because they have different types!
249 //NewGV->replaceAllUsesWith(ExistGV);
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 global from the module entirely...
268 Module *M = NewGV->getParent();
269 if (Function *F = dyn_cast<Function>(NewGV))
270 M->getFunctionList().remove(F);
272 M->getGlobalList().remove(cast<GlobalVariable>(NewGV));
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 std::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 std::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 std::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 std::cerr << "Added type " << NewType->getDescription() << "\n";
320 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
326 static void DumpVal(const std::pair<const std::string, Value *> &V) {
327 std::cout << " '" << V.first << "' = ";
332 static void DumpPlane(const std::pair<const Type *,
333 std::map<const std::string, Value *> >&P){
334 std::cout << " Plane: ";
337 for_each(P.second.begin(), P.second.end(), DumpVal);
340 void SymbolTable::dump() const {
341 std::cout << "Symbol table dump:\n";
342 for_each(begin(), end(), DumpPlane);