Add more support for new style casts
[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   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
130   }
131
132 #if DEBUG_SYMBOL_TABLE
133   cerr << this << " Inserting definition: " << Name << ": " 
134        << VTy->getDescription() << endl;
135 #endif
136
137   iterator I = find(VTy);
138   if (I == end()) {      // Not in collection yet... insert dummy entry
139     (*this)[VTy] = VarMap();
140     I = find(VTy);
141     assert(I != end() && "How did insert fail?");
142   }
143
144   I->second.insert(make_pair(Name, V));
145
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);
149     if (T->isAbstract())
150       cast<DerivedType>(T)->addAbstractTypeUser(this);
151   }
152 }
153
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
158
159   iterator TPI = find(Type::TypeTy);
160   assert(TPI != end() &&"Type plane not in symbol table but we contain types!");
161
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.
166   //
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);
174     }
175 }