Simple is good
[oota-llvm.git] / lib / VMCore / TypeSymbolTable.cpp
1 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer. It is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TypeSymbolTable class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/TypeSymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include <iostream>
18
19 using namespace llvm;
20
21 #define DEBUG_SYMBOL_TABLE 0
22 #define DEBUG_ABSTYPE 0
23
24 TypeSymbolTable::~TypeSymbolTable() {
25   // Drop all abstract type references in the type plane...
26   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
27     if (TI->second->isAbstract())   // If abstract, drop the reference...
28       cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
29   }
30 }
31
32 std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const {
33   std::string TryName = BaseName;
34   const_iterator End = tmap.end();
35
36   // See if the name exists
37   while (tmap.find(TryName) != End)            // Loop until we find a free
38     TryName = BaseName + utostr(++LastUnique); // name in the symbol table
39   return TryName;
40 }
41
42 // lookup a type by name - returns null on failure
43 Type* TypeSymbolTable::lookup(const std::string& Name) const {
44   const_iterator TI = tmap.find(Name);
45   if (TI != tmap.end())
46     return const_cast<Type*>(TI->second);
47   return 0;
48 }
49
50 // Erase a specific type from the symbol table
51 bool TypeSymbolTable::erase(Type *N) {
52   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
53     if (TI->second == N) {
54       this->erase(TI);
55       return true;
56     }
57   }
58   return false;
59 }
60
61 // remove - Remove a type from the symbol table...
62 Type* TypeSymbolTable::erase(iterator Entry) {
63   assert(Entry != tmap.end() && "Invalid entry to remove!");
64
65   const Type* Result = Entry->second;
66
67 #if DEBUG_SYMBOL_TABLE
68   dump();
69   std::cerr << " Removing Value: " << Result->getName() << "\n";
70 #endif
71
72   tmap.erase(Entry);
73
74   // If we are removing an abstract type, remove the symbol table from it's use
75   // list...
76   if (Result->isAbstract()) {
77 #if DEBUG_ABSTYPE
78     std::cerr << "Removing abstract type from symtab" << Result->getDescription()<<"\n";
79 #endif
80     cast<DerivedType>(Result)->removeAbstractTypeUser(this);
81   }
82
83   return const_cast<Type*>(Result);
84 }
85
86
87 // insert - Insert a type into the symbol table with the specified name...
88 void TypeSymbolTable::insert(const std::string& Name, const Type* T) {
89   assert(T && "Can't insert null type into symbol table!");
90
91   // Check to see if there is a naming conflict.  If so, rename this type!
92   std::string UniqueName = Name;
93   if (lookup(Name))
94     UniqueName = getUniqueName(Name);
95
96 #if DEBUG_SYMBOL_TABLE
97   dump();
98   std::cerr << " Inserting type: " << UniqueName << ": "
99             << T->getDescription() << "\n";
100 #endif
101
102   // Insert the tmap entry
103   tmap.insert(make_pair(UniqueName, T));
104
105   // If we are adding an abstract type, add the symbol table to it's use list.
106   if (T->isAbstract()) {
107     cast<DerivedType>(T)->addAbstractTypeUser(this);
108 #if DEBUG_ABSTYPE
109     std::cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
110 #endif
111   }
112 }
113
114 // Strip the symbol table of its names.
115 bool TypeSymbolTable::strip() {
116   bool RemovedSymbol = false;
117   for (iterator TI = tmap.begin(); TI != tmap.end(); ) {
118     erase(TI++);
119     RemovedSymbol = true;
120   }
121
122   return RemovedSymbol;
123 }
124
125 /// rename - Given a value with a non-empty name, remove its existing entry
126 /// from the symbol table and insert a new one for Name.  This is equivalent to
127 /// doing "remove(V), V->Name = Name, insert(V)", but is faster, and will not
128 /// temporarily remove the symbol table plane if V is the last value in the
129 /// symtab with that name (which could invalidate iterators to that plane).
130 bool TypeSymbolTable::rename(Type *T, const std::string &name) {
131   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
132     if (TI->second == T) {
133       // Remove the old entry.
134       tmap.erase(TI);
135       // Add the new entry.
136       this->insert(name,T);
137       return true;
138     }
139   }
140   return false;
141 }
142
143 // This function is called when one of the types in the type plane are refined
144 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
145                                          const Type *NewType) {
146
147   // Loop over all of the types in the symbol table, replacing any references
148   // to OldType with references to NewType.  Note that there may be multiple
149   // occurrences, and although we only need to remove one at a time, it's
150   // faster to remove them all in one pass.
151   //
152   for (iterator I = begin(), E = end(); I != E; ++I) {
153     if (I->second == (Type*)OldType) {  // FIXME when Types aren't const.
154 #if DEBUG_ABSTYPE
155       std::cerr << "Removing type " << OldType->getDescription() << "\n";
156 #endif
157       OldType->removeAbstractTypeUser(this);
158
159       I->second = (Type*)NewType;  // TODO FIXME when types aren't const
160       if (NewType->isAbstract()) {
161 #if DEBUG_ABSTYPE
162         std::cerr << "Added type " << NewType->getDescription() << "\n";
163 #endif
164         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
165       }
166     }
167   }
168 }
169
170
171 // Handle situation where type becomes Concreate from Abstract
172 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
173   // Loop over all of the types in the symbol table, dropping any abstract
174   // type user entries for AbsTy which occur because there are names for the
175   // type.
176   for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
177     if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
178       AbsTy->removeAbstractTypeUser(this);
179 }
180
181 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
182   std::cerr << "  '" << T.first << "' = ";
183   T.second->dump();
184   std::cerr << "\n";
185 }
186
187 void TypeSymbolTable::dump() const {
188   std::cerr << "TypeSymbolPlane: ";
189   for_each(tmap.begin(), tmap.end(), DumpTypes);
190 }
191
192 // vim: sw=2 ai