1 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TypeSymbolTable class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/TypeSymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/Support/Streams.h"
21 #define DEBUG_SYMBOL_TABLE 0
22 #define DEBUG_ABSTYPE 0
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);
32 std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const {
33 std::string TryName = BaseName;
34 const_iterator End = tmap.end();
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
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);
46 return const_cast<Type*>(TI->second);
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) {
61 // remove - Remove a type from the symbol table...
62 Type* TypeSymbolTable::erase(iterator Entry) {
63 assert(Entry != tmap.end() && "Invalid entry to remove!");
65 const Type* Result = Entry->second;
67 #if DEBUG_SYMBOL_TABLE
69 cerr << " Removing Value: " << Result->getName() << "\n";
74 // If we are removing an abstract type, remove the symbol table from it's use
76 if (Result->isAbstract()) {
78 cerr << "Removing abstract type from symtab" << Result->getDescription()<<"\n";
80 cast<DerivedType>(Result)->removeAbstractTypeUser(this);
83 return const_cast<Type*>(Result);
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!");
91 // Check to see if there is a naming conflict. If so, rename this type!
92 std::string UniqueName = Name;
94 UniqueName = getUniqueName(Name);
96 #if DEBUG_SYMBOL_TABLE
98 cerr << " Inserting type: " << UniqueName << ": "
99 << T->getDescription() << "\n";
102 // Insert the tmap entry
103 tmap.insert(make_pair(UniqueName, T));
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);
109 cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
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(); ) {
119 RemovedSymbol = true;
122 return RemovedSymbol;
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.
135 // Add the new entry.
136 this->insert(name,T);
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) {
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.
152 for (iterator I = begin(), E = end(); I != E; ++I) {
153 if (I->second == (Type*)OldType) { // FIXME when Types aren't const.
155 cerr << "Removing type " << OldType->getDescription() << "\n";
157 OldType->removeAbstractTypeUser(this);
159 I->second = (Type*)NewType; // TODO FIXME when types aren't const
160 if (NewType->isAbstract()) {
162 cerr << "Added type " << NewType->getDescription() << "\n";
164 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
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
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);
181 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
182 cerr << " '" << T.first << "' = ";
187 void TypeSymbolTable::dump() const {
188 cerr << "TypeSymbolPlane: ";
189 for_each(tmap.begin(), tmap.end(), DumpTypes);