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