Type safety for TypeSymbolTable!

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73614 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Owen Anderson 2009-06-17 16:56:27 +00:00
parent da06288aeb
commit ea808c98d8

View File

@ -14,13 +14,18 @@
#include "llvm/TypeSymbolTable.h" #include "llvm/TypeSymbolTable.h"
#include "llvm/DerivedTypes.h" #include "llvm/DerivedTypes.h"
#include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringExtras.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Streams.h" #include "llvm/Support/Streams.h"
#include "llvm/Support/Threading.h"
#include "llvm/System/RWMutex.h"
#include <algorithm> #include <algorithm>
using namespace llvm; using namespace llvm;
#define DEBUG_SYMBOL_TABLE 0 #define DEBUG_SYMBOL_TABLE 0
#define DEBUG_ABSTYPE 0 #define DEBUG_ABSTYPE 0
static ManagedStatic<sys::RWMutex> TypeSymbolTableLock;
TypeSymbolTable::~TypeSymbolTable() { TypeSymbolTable::~TypeSymbolTable() {
// Drop all abstract type references in the type plane... // Drop all abstract type references in the type plane...
for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) { for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
@ -31,26 +36,39 @@ TypeSymbolTable::~TypeSymbolTable() {
std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const { std::string TypeSymbolTable::getUniqueName(const std::string &BaseName) const {
std::string TryName = BaseName; std::string TryName = BaseName;
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
const_iterator End = tmap.end(); const_iterator End = tmap.end();
// See if the name exists // See if the name exists
while (tmap.find(TryName) != End) // Loop until we find a free while (tmap.find(TryName) != End) // Loop until we find a free
TryName = BaseName + utostr(++LastUnique); // name in the symbol table TryName = BaseName + utostr(++LastUnique); // name in the symbol table
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
return TryName; return TryName;
} }
// lookup a type by name - returns null on failure // lookup a type by name - returns null on failure
Type* TypeSymbolTable::lookup(const std::string& Name) const { Type* TypeSymbolTable::lookup(const std::string& Name) const {
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
const_iterator TI = tmap.find(Name); const_iterator TI = tmap.find(Name);
Type* result = 0;
if (TI != tmap.end()) if (TI != tmap.end())
return const_cast<Type*>(TI->second); result = const_cast<Type*>(TI->second);
return 0;
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
return result;
} }
// remove - Remove a type from the symbol table... // remove - Remove a type from the symbol table...
Type* TypeSymbolTable::remove(iterator Entry) { Type* TypeSymbolTable::remove(iterator Entry) {
if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_acquire();
assert(Entry != tmap.end() && "Invalid entry to remove!"); assert(Entry != tmap.end() && "Invalid entry to remove!");
const Type* Result = Entry->second; const Type* Result = Entry->second;
#if DEBUG_SYMBOL_TABLE #if DEBUG_SYMBOL_TABLE
@ -59,6 +77,8 @@ Type* TypeSymbolTable::remove(iterator Entry) {
#endif #endif
tmap.erase(Entry); tmap.erase(Entry);
if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_release();
// If we are removing an abstract type, remove the symbol table from it's use // If we are removing an abstract type, remove the symbol table from it's use
// list... // list...
@ -79,6 +99,8 @@ Type* TypeSymbolTable::remove(iterator Entry) {
void TypeSymbolTable::insert(const std::string& Name, const Type* T) { void TypeSymbolTable::insert(const std::string& Name, const Type* T) {
assert(T && "Can't insert null type into symbol table!"); assert(T && "Can't insert null type into symbol table!");
if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_acquire();
if (tmap.insert(make_pair(Name, T)).second) { if (tmap.insert(make_pair(Name, T)).second) {
// Type inserted fine with no conflict. // Type inserted fine with no conflict.
@ -103,6 +125,8 @@ void TypeSymbolTable::insert(const std::string& Name, const Type* T) {
// Insert the tmap entry // Insert the tmap entry
tmap.insert(make_pair(UniqueName, T)); tmap.insert(make_pair(UniqueName, T));
} }
if (llvm_is_multithreaded()) TypeSymbolTableLock->writer_release();
// If we are adding an abstract type, add the symbol table to it's use list. // If we are adding an abstract type, add the symbol table to it's use list.
if (T->isAbstract()) { if (T->isAbstract()) {
@ -116,6 +140,7 @@ void TypeSymbolTable::insert(const std::string& Name, const Type* T) {
// This function is called when one of the types in the type plane are refined // This function is called when one of the types in the type plane are refined
void TypeSymbolTable::refineAbstractType(const DerivedType *OldType, void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
const Type *NewType) { const Type *NewType) {
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
// Loop over all of the types in the symbol table, replacing any references // Loop over all of the types in the symbol table, replacing any references
// to OldType with references to NewType. Note that there may be multiple // to OldType with references to NewType. Note that there may be multiple
@ -138,6 +163,8 @@ void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
} }
} }
} }
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
} }
@ -146,9 +173,11 @@ void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
// Loop over all of the types in the symbol table, dropping any abstract // Loop over all of the types in the symbol table, dropping any abstract
// type user entries for AbsTy which occur because there are names for the // type user entries for AbsTy which occur because there are names for the
// type. // type.
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
for (iterator TI = begin(), TE = end(); TI != TE; ++TI) for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy))) if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
AbsTy->removeAbstractTypeUser(this); AbsTy->removeAbstractTypeUser(this);
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
} }
static void DumpTypes(const std::pair<const std::string, const Type*>& T ) { static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
@ -159,7 +188,9 @@ static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
void TypeSymbolTable::dump() const { void TypeSymbolTable::dump() const {
cerr << "TypeSymbolPlane: "; cerr << "TypeSymbolPlane: ";
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_acquire();
for_each(tmap.begin(), tmap.end(), DumpTypes); for_each(tmap.begin(), tmap.end(), DumpTypes);
if (llvm_is_multithreaded()) TypeSymbolTableLock->reader_release();
} }
// vim: sw=2 ai // vim: sw=2 ai