Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 1 | //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 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" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Streams.h" |
Jeff Cohen | df46b26 | 2006-01-11 16:21:23 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 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 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 50 | // remove - Remove a type from the symbol table... |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 51 | Type* TypeSymbolTable::remove(iterator Entry) { |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 52 | assert(Entry != tmap.end() && "Invalid entry to remove!"); |
| 53 | |
| 54 | const Type* Result = Entry->second; |
| 55 | |
| 56 | #if DEBUG_SYMBOL_TABLE |
| 57 | dump(); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 58 | cerr << " Removing Value: " << Result->getName() << "\n"; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 59 | #endif |
| 60 | |
| 61 | tmap.erase(Entry); |
| 62 | |
| 63 | // If we are removing an abstract type, remove the symbol table from it's use |
| 64 | // list... |
| 65 | if (Result->isAbstract()) { |
| 66 | #if DEBUG_ABSTYPE |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 67 | cerr << "Removing abstract type from symtab" << Result->getDescription()<<"\n"; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 68 | #endif |
| 69 | cast<DerivedType>(Result)->removeAbstractTypeUser(this); |
| 70 | } |
| 71 | |
| 72 | return const_cast<Type*>(Result); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | // insert - Insert a type into the symbol table with the specified name... |
| 77 | void TypeSymbolTable::insert(const std::string& Name, const Type* T) { |
| 78 | assert(T && "Can't insert null type into symbol table!"); |
| 79 | |
Chris Lattner | 58f84d4 | 2007-02-07 05:35:58 +0000 | [diff] [blame] | 80 | if (tmap.insert(make_pair(Name, T)).second) { |
| 81 | // Type inserted fine with no conflict. |
| 82 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 83 | #if DEBUG_SYMBOL_TABLE |
Chris Lattner | 58f84d4 | 2007-02-07 05:35:58 +0000 | [diff] [blame] | 84 | dump(); |
| 85 | cerr << " Inserted type: " << Name << ": " << T->getDescription() << "\n"; |
| 86 | #endif |
| 87 | } else { |
| 88 | // If there is a name conflict... |
| 89 | |
| 90 | // Check to see if there is a naming conflict. If so, rename this type! |
| 91 | std::string UniqueName = Name; |
| 92 | if (lookup(Name)) |
| 93 | UniqueName = getUniqueName(Name); |
| 94 | |
| 95 | #if DEBUG_SYMBOL_TABLE |
| 96 | dump(); |
| 97 | cerr << " Inserting type: " << UniqueName << ": " |
| 98 | << T->getDescription() << "\n"; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
Chris Lattner | 58f84d4 | 2007-02-07 05:35:58 +0000 | [diff] [blame] | 101 | // Insert the tmap entry |
| 102 | tmap.insert(make_pair(UniqueName, T)); |
| 103 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 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 |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 109 | cerr << "Added abstract type to ST: " << T->getDescription() << "\n"; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 110 | #endif |
| 111 | } |
| 112 | } |
| 113 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 114 | // This function is called when one of the types in the type plane are refined |
| 115 | void TypeSymbolTable::refineAbstractType(const DerivedType *OldType, |
| 116 | const Type *NewType) { |
| 117 | |
| 118 | // Loop over all of the types in the symbol table, replacing any references |
| 119 | // to OldType with references to NewType. Note that there may be multiple |
| 120 | // occurrences, and although we only need to remove one at a time, it's |
| 121 | // faster to remove them all in one pass. |
| 122 | // |
| 123 | for (iterator I = begin(), E = end(); I != E; ++I) { |
| 124 | if (I->second == (Type*)OldType) { // FIXME when Types aren't const. |
| 125 | #if DEBUG_ABSTYPE |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 126 | cerr << "Removing type " << OldType->getDescription() << "\n"; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 127 | #endif |
| 128 | OldType->removeAbstractTypeUser(this); |
| 129 | |
| 130 | I->second = (Type*)NewType; // TODO FIXME when types aren't const |
| 131 | if (NewType->isAbstract()) { |
| 132 | #if DEBUG_ABSTYPE |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 133 | cerr << "Added type " << NewType->getDescription() << "\n"; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 134 | #endif |
| 135 | cast<DerivedType>(NewType)->addAbstractTypeUser(this); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | // Handle situation where type becomes Concreate from Abstract |
| 143 | void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) { |
| 144 | // Loop over all of the types in the symbol table, dropping any abstract |
| 145 | // type user entries for AbsTy which occur because there are names for the |
| 146 | // type. |
| 147 | for (iterator TI = begin(), TE = end(); TI != TE; ++TI) |
| 148 | if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy))) |
| 149 | AbsTy->removeAbstractTypeUser(this); |
| 150 | } |
| 151 | |
| 152 | static void DumpTypes(const std::pair<const std::string, const Type*>& T ) { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 153 | cerr << " '" << T.first << "' = "; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 154 | T.second->dump(); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 155 | cerr << "\n"; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | void TypeSymbolTable::dump() const { |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 159 | cerr << "TypeSymbolPlane: "; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 160 | for_each(tmap.begin(), tmap.end(), DumpTypes); |
| 161 | } |
| 162 | |
| 163 | // vim: sw=2 ai |