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/raw_ostream.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 TypeSymbolTable::iterator TypeSymbolTable::find(const StringRef &Name) {
63 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
64 return tmap.find(Name);
67 TypeSymbolTable::const_iterator
68 TypeSymbolTable::find(const StringRef &Name) const {
69 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
70 return tmap.find(Name);
73 // remove - Remove a type from the symbol table...
74 Type* TypeSymbolTable::remove(iterator Entry) {
75 TypeSymbolTableLock->writer_acquire();
77 assert(Entry != tmap.end() && "Invalid entry to remove!");
78 const Type* Result = Entry->second;
80 #if DEBUG_SYMBOL_TABLE
82 errs() << " Removing Value: " << Result->getDescription() << "\n";
87 TypeSymbolTableLock->writer_release();
89 // If we are removing an abstract type, remove the symbol table from it's use
91 if (Result->isAbstract()) {
93 errs() << "Removing abstract type from symtab"
94 << Result->getDescription()
97 cast<DerivedType>(Result)->removeAbstractTypeUser(this);
100 return const_cast<Type*>(Result);
104 // insert - Insert a type into the symbol table with the specified name...
105 void TypeSymbolTable::insert(const StringRef &Name, const Type* T) {
106 assert(T && "Can't insert null type into symbol table!");
108 TypeSymbolTableLock->writer_acquire();
110 if (tmap.insert(std::make_pair(Name, T)).second) {
111 // Type inserted fine with no conflict.
113 #if DEBUG_SYMBOL_TABLE
115 errs() << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
118 // If there is a name conflict...
120 // Check to see if there is a naming conflict. If so, rename this type!
121 std::string UniqueName = Name;
123 UniqueName = getUniqueName(Name);
125 #if DEBUG_SYMBOL_TABLE
127 errs() << " Inserting type: " << UniqueName << ": "
128 << T->getDescription() << "\n";
131 // Insert the tmap entry
132 tmap.insert(make_pair(UniqueName, T));
135 TypeSymbolTableLock->writer_release();
137 // If we are adding an abstract type, add the symbol table to it's use list.
138 if (T->isAbstract()) {
139 cast<DerivedType>(T)->addAbstractTypeUser(this);
141 errs() << "Added abstract type to ST: " << T->getDescription() << "\n";
146 // This function is called when one of the types in the type plane are refined
147 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
148 const Type *NewType) {
149 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
151 // Loop over all of the types in the symbol table, replacing any references
152 // to OldType with references to NewType. Note that there may be multiple
153 // occurrences, and although we only need to remove one at a time, it's
154 // faster to remove them all in one pass.
156 for (iterator I = begin(), E = end(); I != E; ++I) {
157 if (I->second == (Type*)OldType) { // FIXME when Types aren't const.
159 errs() << "Removing type " << OldType->getDescription() << "\n";
161 OldType->removeAbstractTypeUser(this);
163 I->second = (Type*)NewType; // TODO FIXME when types aren't const
164 if (NewType->isAbstract()) {
166 errs() << "Added type " << NewType->getDescription() << "\n";
168 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
175 // Handle situation where type becomes Concreate from Abstract
176 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
177 // Loop over all of the types in the symbol table, dropping any abstract
178 // type user entries for AbsTy which occur because there are names for the
180 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
181 for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
182 if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
183 AbsTy->removeAbstractTypeUser(this);
186 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
187 errs() << " '" << T.first << "' = ";
192 void TypeSymbolTable::dump() const {
193 errs() << "TypeSymbolPlane: ";
194 sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
195 for_each(tmap.begin(), tmap.end(), DumpTypes);