1 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/ADT/StringRef.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/Streams.h"
20 #include "llvm/System/RWMutex.h"
21 #include "llvm/System/Threading.h"
25 #define DEBUG_SYMBOL_TABLE 0
26 #define DEBUG_ABSTYPE 0
28 static ManagedStatic<sys::SmartRWMutex<true> > TypeSymbolTableLock;
30 TypeSymbolTable::~TypeSymbolTable() {
31 // Drop all abstract type references in the type plane...
32 for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
33 if (TI->second->isAbstract()) // If abstract, drop the reference...
34 cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
38 std::string TypeSymbolTable::getUniqueName(const StringRef &BaseName) const {
39 std::string TryName = BaseName;
41 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
43 const_iterator End = tmap.end();
45 // See if the name exists
46 while (tmap.find(TryName) != End) // Loop until we find a free
47 TryName = BaseName.str() + utostr(++LastUnique); // name in the symbol table
51 // lookup a type by name - returns null on failure
52 Type* TypeSymbolTable::lookup(const StringRef &Name) const {
53 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
55 const_iterator TI = tmap.find(Name);
58 result = const_cast<Type*>(TI->second);
62 // remove - Remove a type from the symbol table...
63 Type* TypeSymbolTable::remove(iterator Entry) {
64 TypeSymbolTableLock->writer_acquire();
66 assert(Entry != tmap.end() && "Invalid entry to remove!");
67 const Type* Result = Entry->second;
69 #if DEBUG_SYMBOL_TABLE
71 cerr << " Removing Value: " << Result->getName() << "\n";
76 TypeSymbolTableLock->writer_release();
78 // If we are removing an abstract type, remove the symbol table from it's use
80 if (Result->isAbstract()) {
82 cerr << "Removing abstract type from symtab"
83 << Result->getDescription()
86 cast<DerivedType>(Result)->removeAbstractTypeUser(this);
89 return const_cast<Type*>(Result);
93 // insert - Insert a type into the symbol table with the specified name...
94 void TypeSymbolTable::insert(const StringRef &Name, const Type* T) {
95 assert(T && "Can't insert null type into symbol table!");
97 TypeSymbolTableLock->writer_acquire();
99 if (tmap.insert(std::make_pair(Name, T)).second) {
100 // Type inserted fine with no conflict.
102 #if DEBUG_SYMBOL_TABLE
104 cerr << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
107 // If there is a name conflict...
109 // Check to see if there is a naming conflict. If so, rename this type!
110 std::string UniqueName = Name;
112 UniqueName = getUniqueName(Name);
114 #if DEBUG_SYMBOL_TABLE
116 cerr << " Inserting type: " << UniqueName << ": "
117 << T->getDescription() << "\n";
120 // Insert the tmap entry
121 tmap.insert(make_pair(UniqueName, T));
124 TypeSymbolTableLock->writer_release();
126 // If we are adding an abstract type, add the symbol table to it's use list.
127 if (T->isAbstract()) {
128 cast<DerivedType>(T)->addAbstractTypeUser(this);
130 cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
135 // This function is called when one of the types in the type plane are refined
136 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
137 const Type *NewType) {
138 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
140 // Loop over all of the types in the symbol table, replacing any references
141 // to OldType with references to NewType. Note that there may be multiple
142 // occurrences, and although we only need to remove one at a time, it's
143 // faster to remove them all in one pass.
145 for (iterator I = begin(), E = end(); I != E; ++I) {
146 if (I->second == (Type*)OldType) { // FIXME when Types aren't const.
148 cerr << "Removing type " << OldType->getDescription() << "\n";
150 OldType->removeAbstractTypeUser(this);
152 I->second = (Type*)NewType; // TODO FIXME when types aren't const
153 if (NewType->isAbstract()) {
155 cerr << "Added type " << NewType->getDescription() << "\n";
157 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
164 // Handle situation where type becomes Concreate from Abstract
165 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
166 // Loop over all of the types in the symbol table, dropping any abstract
167 // type user entries for AbsTy which occur because there are names for the
169 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
170 for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
171 if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
172 AbsTy->removeAbstractTypeUser(this);
175 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
176 cerr << " '" << T.first << "' = ";
181 void TypeSymbolTable::dump() const {
182 cerr << "TypeSymbolPlane: ";
183 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
184 for_each(tmap.begin(), tmap.end(), DumpTypes);