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, Value *V) {
123 const Type *VTy = V->getType();
125 // TODO: The typeverifier should catch this when its implemented
126 if (lookup(VTy, Name)) {
127 cerr << "SymbolTable ERROR: Name already in symbol table: '"
128 << Name << "' for type '" << VTy->getDescription() << "'\n";
129 abort(); // TODO: REMOVE THIS
132 #if DEBUG_SYMBOL_TABLE
133 cerr << this << " Inserting definition: " << Name << ": "
134 << VTy->getDescription() << endl;
137 iterator I = find(VTy);
138 if (I == end()) { // Not in collection yet... insert dummy entry
139 (*this)[VTy] = VarMap();
141 assert(I != end() && "How did insert fail?");
144 I->second.insert(make_pair(Name, V));
146 // If we are adding an abstract type, add the symbol table to it's use list.
147 if (VTy == Type::TypeTy) {
148 const Type *T = cast<const Type>(V);
150 cast<DerivedType>(T)->addAbstractTypeUser(this);
154 // This function is called when one of the types in the type plane are refined
155 void SymbolTable::refineAbstractType(const DerivedType *OldType,
156 const Type *NewType) {
157 if (OldType == NewType) return; // Noop, don't waste time dinking around
159 iterator TPI = find(Type::TypeTy);
160 assert(TPI != end() &&"Type plane not in symbol table but we contain types!");
162 // Loop over all of the types in the symbol table, replacing any references to
163 // OldType with references to NewType. Note that there may be multiple
164 // occurances, and although we only need to remove one at a time, it's faster
165 // to remove them all in one pass.
167 VarMap &TyPlane = TPI->second;
168 for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
169 if (I->second == (Value*)OldType) { // FIXME when Types aren't const.
170 OldType->removeAbstractTypeUser(this);
171 I->second = (Value*)NewType; // TODO FIXME when types aren't const
172 if (NewType->isAbstract())
173 cast<const DerivedType>(NewType)->addAbstractTypeUser(this);