LLVM API Documentation
00001 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the TypeSymbolTable class for the VMCore library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/TypeSymbolTable.h" 00015 #include "llvm/DerivedTypes.h" 00016 #include "llvm/ADT/StringExtras.h" 00017 #include "llvm/ADT/StringRef.h" 00018 #include "llvm/Support/ManagedStatic.h" 00019 #include "llvm/Support/Streams.h" 00020 #include "llvm/System/RWMutex.h" 00021 #include "llvm/System/Threading.h" 00022 #include <algorithm> 00023 using namespace llvm; 00024 00025 #define DEBUG_SYMBOL_TABLE 0 00026 #define DEBUG_ABSTYPE 0 00027 00028 static ManagedStatic<sys::SmartRWMutex<true> > TypeSymbolTableLock; 00029 00030 TypeSymbolTable::~TypeSymbolTable() { 00031 // Drop all abstract type references in the type plane... 00032 for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) { 00033 if (TI->second->isAbstract()) // If abstract, drop the reference... 00034 cast<DerivedType>(TI->second)->removeAbstractTypeUser(this); 00035 } 00036 } 00037 00038 std::string TypeSymbolTable::getUniqueName(const StringRef &BaseName) const { 00039 std::string TryName = BaseName; 00040 00041 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock); 00042 00043 const_iterator End = tmap.end(); 00044 00045 // See if the name exists 00046 while (tmap.find(TryName) != End) // Loop until we find a free 00047 TryName = BaseName.str() + utostr(++LastUnique); // name in the symbol table 00048 return TryName; 00049 } 00050 00051 // lookup a type by name - returns null on failure 00052 Type* TypeSymbolTable::lookup(const StringRef &Name) const { 00053 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock); 00054 00055 const_iterator TI = tmap.find(Name); 00056 Type* result = 0; 00057 if (TI != tmap.end()) 00058 result = const_cast<Type*>(TI->second); 00059 return result; 00060 } 00061 00062 TypeSymbolTable::iterator TypeSymbolTable::find(const StringRef &Name) { 00063 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock); 00064 return tmap.find(Name); 00065 } 00066 00067 TypeSymbolTable::const_iterator 00068 TypeSymbolTable::find(const StringRef &Name) const { 00069 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock); 00070 return tmap.find(Name); 00071 } 00072 00073 // remove - Remove a type from the symbol table... 00074 Type* TypeSymbolTable::remove(iterator Entry) { 00075 TypeSymbolTableLock->writer_acquire(); 00076 00077 assert(Entry != tmap.end() && "Invalid entry to remove!"); 00078 const Type* Result = Entry->second; 00079 00080 #if DEBUG_SYMBOL_TABLE 00081 dump(); 00082 cerr << " Removing Value: " << Result->getName() << "\n"; 00083 #endif 00084 00085 tmap.erase(Entry); 00086 00087 TypeSymbolTableLock->writer_release(); 00088 00089 // If we are removing an abstract type, remove the symbol table from it's use 00090 // list... 00091 if (Result->isAbstract()) { 00092 #if DEBUG_ABSTYPE 00093 cerr << "Removing abstract type from symtab" 00094 << Result->getDescription() 00095 << "\n"; 00096 #endif 00097 cast<DerivedType>(Result)->removeAbstractTypeUser(this); 00098 } 00099 00100 return const_cast<Type*>(Result); 00101 } 00102 00103 00104 // insert - Insert a type into the symbol table with the specified name... 00105 void TypeSymbolTable::insert(const StringRef &Name, const Type* T) { 00106 assert(T && "Can't insert null type into symbol table!"); 00107 00108 TypeSymbolTableLock->writer_acquire(); 00109 00110 if (tmap.insert(std::make_pair(Name, T)).second) { 00111 // Type inserted fine with no conflict. 00112 00113 #if DEBUG_SYMBOL_TABLE 00114 dump(); 00115 cerr << " Inserted type: " << Name << ": " << T->getDescription() << "\n"; 00116 #endif 00117 } else { 00118 // If there is a name conflict... 00119 00120 // Check to see if there is a naming conflict. If so, rename this type! 00121 std::string UniqueName = Name; 00122 if (lookup(Name)) 00123 UniqueName = getUniqueName(Name); 00124 00125 #if DEBUG_SYMBOL_TABLE 00126 dump(); 00127 cerr << " Inserting type: " << UniqueName << ": " 00128 << T->getDescription() << "\n"; 00129 #endif 00130 00131 // Insert the tmap entry 00132 tmap.insert(make_pair(UniqueName, T)); 00133 } 00134 00135 TypeSymbolTableLock->writer_release(); 00136 00137 // If we are adding an abstract type, add the symbol table to it's use list. 00138 if (T->isAbstract()) { 00139 cast<DerivedType>(T)->addAbstractTypeUser(this); 00140 #if DEBUG_ABSTYPE 00141 cerr << "Added abstract type to ST: " << T->getDescription() << "\n"; 00142 #endif 00143 } 00144 } 00145 00146 // This function is called when one of the types in the type plane are refined 00147 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType, 00148 const Type *NewType) { 00149 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock); 00150 00151 // Loop over all of the types in the symbol table, replacing any references 00152 // to OldType with references to NewType. Note that there may be multiple 00153 // occurrences, and although we only need to remove one at a time, it's 00154 // faster to remove them all in one pass. 00155 // 00156 for (iterator I = begin(), E = end(); I != E; ++I) { 00157 if (I->second == (Type*)OldType) { // FIXME when Types aren't const. 00158 #if DEBUG_ABSTYPE 00159 cerr << "Removing type " << OldType->getDescription() << "\n"; 00160 #endif 00161 OldType->removeAbstractTypeUser(this); 00162 00163 I->second = (Type*)NewType; // TODO FIXME when types aren't const 00164 if (NewType->isAbstract()) { 00165 #if DEBUG_ABSTYPE 00166 cerr << "Added type " << NewType->getDescription() << "\n"; 00167 #endif 00168 cast<DerivedType>(NewType)->addAbstractTypeUser(this); 00169 } 00170 } 00171 } 00172 } 00173 00174 00175 // Handle situation where type becomes Concreate from Abstract 00176 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) { 00177 // Loop over all of the types in the symbol table, dropping any abstract 00178 // type user entries for AbsTy which occur because there are names for the 00179 // type. 00180 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock); 00181 for (iterator TI = begin(), TE = end(); TI != TE; ++TI) 00182 if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy))) 00183 AbsTy->removeAbstractTypeUser(this); 00184 } 00185 00186 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) { 00187 cerr << " '" << T.first << "' = "; 00188 T.second->dump(); 00189 cerr << "\n"; 00190 } 00191 00192 void TypeSymbolTable::dump() const { 00193 cerr << "TypeSymbolPlane: "; 00194 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock); 00195 for_each(tmap.begin(), tmap.end(), DumpTypes); 00196 } 00197 00198 // vim: sw=2 ai