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"
12 SymbolTable::~SymbolTable() {
13 // Drop all abstract type references in the type plane...
14 iterator TyPlane = find(Type::TypeTy);
15 if (TyPlane != end()) {
16 VarMap &TyP = TyPlane->second;
17 for (VarMap::iterator I = TyP.begin(), E = TyP.end(); I != E; ++I) {
18 const Type *Ty = cast<const Type>(I->second);
19 if (Ty->isAbstract()) // If abstract, drop the reference...
20 cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
23 #ifndef NDEBUG // Only do this in -g mode...
24 bool LeftoverValues = true;
25 for (iterator i = begin(); i != end(); ++i) {
26 for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
27 if (!isa<ConstPoolVal>(I->second) && !isa<Type>(I->second)) {
28 cerr << "Value still in symbol table! Type = '"
29 << i->first->getDescription() << "' Name = '" << I->first << "'\n";
30 LeftoverValues = false;
34 assert(LeftoverValues && "Values remain in symbol table!");
38 SymbolTable::type_iterator SymbolTable::type_find(const Value *D) {
39 assert(D->hasName() && "type_find(Value*) only works on named nodes!");
40 return type_find(D->getType(), D->getName());
44 // find - returns end(Ty->getIDNumber()) on failure...
45 SymbolTable::type_iterator SymbolTable::type_find(const Type *Ty,
47 iterator I = find(Ty);
48 if (I == end()) { // Not in collection yet... insert dummy entry
49 (*this)[Ty] = VarMap();
51 assert(I != end() && "How did insert fail?");
54 return I->second.find(Name);
57 // getUniqueName - Given a base name, return a string that is either equal to
58 // it (or derived from it) that does not already occur in the symbol table for
59 // the specified type.
61 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
62 iterator I = find(Ty);
63 if (I == end()) return BaseName;
65 string TryName = BaseName;
67 type_iterator End = I->second.end();
69 while (I->second.find(TryName) != End) // Loop until we find unoccupied
70 TryName = BaseName + utostr(++Counter); // Name in the symbol table
76 // lookup - Returns null on failure...
77 Value *SymbolTable::lookup(const Type *Ty, const string &Name) {
78 iterator I = find(Ty);
79 if (I != end()) { // We have symbols in that plane...
80 type_iterator J = I->second.find(Name);
81 if (J != I->second.end()) // and the name is in our hash table...
85 return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
88 void SymbolTable::remove(Value *N) {
89 assert(N->hasName() && "Value doesn't have name!");
90 assert(type_find(N) != type_end(N->getType()) &&
91 "Value not in symbol table!");
92 type_remove(type_find(N));
96 #define DEBUG_SYMBOL_TABLE 0
98 Value *SymbolTable::type_remove(const type_iterator &It) {
99 Value *Result = It->second;
100 const Type *Ty = Result->getType();
101 #if DEBUG_SYMBOL_TABLE
102 cerr << this << " Removing Value: " << Result->getName() << endl;
105 // Remove the value from the plane...
106 find(Ty)->second.erase(It);
108 // If we are removing an abstract type, remove the symbol table from it's use
110 if (Ty == Type::TypeTy) {
111 const Type *T = cast<const Type>(Result);
113 cast<DerivedType>(T)->removeAbstractTypeUser(this);
119 // insertEntry - Insert a value into the symbol table with the specified
122 void SymbolTable::insertEntry(const string &Name, const Type *VTy, Value *V) {
123 // TODO: The typeverifier should catch this when its implemented
124 assert(lookup(VTy, Name) == 0 &&
125 "SymbolTable::insertEntry - Name already in symbol table!");
127 #if DEBUG_SYMBOL_TABLE
128 cerr << this << " Inserting definition: " << Name << ": "
129 << VTy->getDescription() << endl;
132 iterator I = find(VTy);
133 if (I == end()) { // Not in collection yet... insert dummy entry
134 // Insert a new empty element. I points to the new elements.
135 I = super::insert(make_pair(VTy, VarMap())).first;
136 assert(I != end() && "How did insert fail?");
138 // Check to see if the type is abstract. If so, it might be refined in the
139 // future, which would cause the plane of the old type to get merged into
142 if (VTy->isAbstract())
143 cast<DerivedType>(VTy)->addAbstractTypeUser(this);
146 I->second.insert(make_pair(Name, V));
148 // If we are adding an abstract type, add the symbol table to it's use list.
149 if (VTy == Type::TypeTy) {
150 const Type *T = cast<const Type>(V);
152 cast<DerivedType>(T)->addAbstractTypeUser(this);
156 // This function is called when one of the types in the type plane are refined
157 void SymbolTable::refineAbstractType(const DerivedType *OldType,
158 const Type *NewType) {
159 if (OldType == NewType) return; // Noop, don't waste time dinking around
161 // Search to see if we have any values of the type oldtype. If so, we need to
162 // move them into the newtype plane...
163 iterator TPI = find(OldType);
165 VarMap &OldPlane = TPI->second;
166 while (!OldPlane.empty()) {
167 pair<const string, Value*> V = *OldPlane.begin();
168 OldPlane.erase(OldPlane.begin());
169 insertEntry(V.first, NewType, V.second);
172 // Ok, now we are not referencing the type anymore... take me off your user
174 OldType->removeAbstractTypeUser(this);
177 TPI = find(Type::TypeTy);
178 assert(TPI != end() &&"Type plane not in symbol table but we contain types!");
180 // Loop over all of the types in the symbol table, replacing any references to
181 // OldType with references to NewType. Note that there may be multiple
182 // occurances, and although we only need to remove one at a time, it's faster
183 // to remove them all in one pass.
185 VarMap &TyPlane = TPI->second;
186 for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
187 if (I->second == (Value*)OldType) { // FIXME when Types aren't const.
188 OldType->removeAbstractTypeUser(this);
189 I->second = (Value*)NewType; // TODO FIXME when types aren't const
190 if (NewType->isAbstract())
191 cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
197 #include "llvm/Assembly/Writer.h"
200 static void DumpVal(const pair<const string, Value *> &V) {
201 cout << " '%" << V.first << "' = " << V.second << endl;
204 static void DumpPlane(const pair<const Type *, map<const string, Value *> >&P) {
205 cout << " Plane: " << P.first << endl;
206 for_each(P.second.begin(), P.second.end(), DumpVal);
209 void SymbolTable::dump() const {
210 cout << "Symbol table dump:\n";
211 for_each(begin(), end(), DumpPlane);
215 ParentSymTab->dump();