Convert a runtime check into an assertion
[oota-llvm.git] / lib / VMCore / SymbolTable.cpp
1 //===-- SymbolTable.cpp - Implement the SymbolTable class -------------------=//
2 //
3 // This file implements the SymbolTable class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/SymbolTable.h"
8 #include "llvm/InstrTypes.h"
9 #include "llvm/Support/StringExtras.h"
10 #include "llvm/DerivedTypes.h"
11
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);
21     }
22   }
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;
31       }
32   }
33   
34   assert(LeftoverValues && "Values remain in symbol table!");
35 #endif
36 }
37
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());
41 }
42
43
44 // find - returns end(Ty->getIDNumber()) on failure...
45 SymbolTable::type_iterator SymbolTable::type_find(const Type *Ty, 
46                                                   const string &Name) {
47   iterator I = find(Ty);
48   if (I == end()) {      // Not in collection yet... insert dummy entry
49     (*this)[Ty] = VarMap();
50     I = find(Ty);
51     assert(I != end() && "How did insert fail?");
52   }
53
54   return I->second.find(Name);
55 }
56
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.
60 //
61 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
62   iterator I = find(Ty);
63   if (I == end()) return BaseName;
64
65   string TryName = BaseName;
66   unsigned Counter = 0;
67   type_iterator End = I->second.end();
68
69   while (I->second.find(TryName) != End)     // Loop until we find unoccupied
70     TryName = BaseName + utostr(++Counter);  // Name in the symbol table
71   return TryName;
72 }
73
74
75
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...
82       return J->second;
83   }
84
85   return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
86 }
87
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));
93 }
94
95
96 #define DEBUG_SYMBOL_TABLE 0
97
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;
103 #endif
104
105   // Remove the value from the plane...
106   find(Ty)->second.erase(It);
107
108   // If we are removing an abstract type, remove the symbol table from it's use
109   // list...
110   if (Ty == Type::TypeTy) {
111     const Type *T = cast<const Type>(Result);
112     if (T->isAbstract())
113       cast<DerivedType>(T)->removeAbstractTypeUser(this);
114   }
115
116   return Result;
117 }
118
119 // insertEntry - Insert a value into the symbol table with the specified
120 // name...
121 //
122 void SymbolTable::insertEntry(const string &Name, Value *V) {
123   const Type *VTy = V->getType();
124
125   // TODO: The typeverifier should catch this when its implemented
126   assert(lookup(VTy, Name) == 0 && 
127          "SymbolTable::insertEntry - Name already in symbol table!");
128
129 #if DEBUG_SYMBOL_TABLE
130   cerr << this << " Inserting definition: " << Name << ": " 
131        << VTy->getDescription() << endl;
132 #endif
133
134   iterator I = find(VTy);
135   if (I == end()) {      // Not in collection yet... insert dummy entry
136     (*this)[VTy] = VarMap();
137     I = find(VTy);
138     assert(I != end() && "How did insert fail?");
139   }
140
141   I->second.insert(make_pair(Name, V));
142
143   // If we are adding an abstract type, add the symbol table to it's use list.
144   if (VTy == Type::TypeTy) {
145     const Type *T = cast<const Type>(V);
146     if (T->isAbstract())
147       cast<DerivedType>(T)->addAbstractTypeUser(this);
148   }
149 }
150
151 // This function is called when one of the types in the type plane are refined
152 void SymbolTable::refineAbstractType(const DerivedType *OldType,
153                                      const Type *NewType) {
154   if (OldType == NewType) return;  // Noop, don't waste time dinking around
155
156   iterator TPI = find(Type::TypeTy);
157   assert(TPI != end() &&"Type plane not in symbol table but we contain types!");
158
159   // Loop over all of the types in the symbol table, replacing any references to
160   // OldType with references to NewType.  Note that there may be multiple
161   // occurances, and although we only need to remove one at a time, it's faster
162   // to remove them all in one pass.
163   //
164   VarMap &TyPlane = TPI->second;
165   for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
166     if (I->second == (Value*)OldType) {  // FIXME when Types aren't const.
167       OldType->removeAbstractTypeUser(this);
168       I->second = (Value*)NewType;  // TODO FIXME when types aren't const
169       if (NewType->isAbstract())
170         cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
171     }
172 }
173
174
175 #ifndef NDEBUG
176 #include "llvm/Assembly/Writer.h"
177 #include <algorithm>
178
179 static void DumpVal(const pair<const string, Value *> &V) {
180   cout << "  '%" << V.first << "' = " << V.second << endl;
181 }
182
183 static void DumpPlane(const pair<const Type *, map<const string, Value *> >&P) {
184   cout << "  Plane: " << P.first << endl;
185   for_each(P.second.begin(), P.second.end(), DumpVal);
186 }
187
188 void SymbolTable::dump() const {
189   cout << "Symbol table dump:\n";
190   for_each(begin(), end(), DumpPlane);
191
192   if (ParentSymTab) {
193     cout << "Parent ";
194     ParentSymTab->dump();
195   }
196 }
197
198 #endif