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"
22 #define DEBUG_SYMBOL_TABLE 0
23 #define DEBUG_ABSTYPE 0
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);
33 std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const {
34 std::string TryName = BaseName;
35 const_iterator End = tmap.end();
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
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);
47 return const_cast<Type*>(TI->second);
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) {
62 // remove - Remove a type from the symbol table...
63 Type* TypeSymbolTable::erase(iterator Entry) {
64 assert(Entry != tmap.end() && "Invalid entry to remove!");
66 const Type* Result = Entry->second;
68 #if DEBUG_SYMBOL_TABLE
70 std::cerr << " Removing Value: " << Result->getName() << "\n";
75 // If we are removing an abstract type, remove the symbol table from it's use
77 if (Result->isAbstract()) {
79 std::cerr << "Removing abstract type from symtab" << Result->getDescription()<<"\n";
81 cast<DerivedType>(Result)->removeAbstractTypeUser(this);
84 return const_cast<Type*>(Result);
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!");
92 // Check to see if there is a naming conflict. If so, rename this type!
93 std::string UniqueName = Name;
95 UniqueName = getUniqueName(Name);
97 #if DEBUG_SYMBOL_TABLE
99 std::cerr << " Inserting type: " << UniqueName << ": "
100 << T->getDescription() << "\n";
103 // Insert the tmap entry
104 tmap.insert(make_pair(UniqueName, T));
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);
110 std::cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
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(); ) {
120 RemovedSymbol = true;
123 return RemovedSymbol;
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.
136 // Add the new entry.
137 this->insert(name,T);
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) {
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.
153 for (iterator I = begin(), E = end(); I != E; ++I) {
154 if (I->second == (Type*)OldType) { // FIXME when Types aren't const.
156 std::cerr << "Removing type " << OldType->getDescription() << "\n";
158 OldType->removeAbstractTypeUser(this);
160 I->second = (Type*)NewType; // TODO FIXME when types aren't const
161 if (NewType->isAbstract()) {
163 std::cerr << "Added type " << NewType->getDescription() << "\n";
165 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
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
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);
182 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
183 std::cerr << " '" << T.first << "' = ";
188 void TypeSymbolTable::dump() const {
189 std::cerr << "TypeSymbolPlane: ";
190 for_each(tmap.begin(), tmap.end(), DumpTypes);