TypeSymbolTable.cpp [plain text]
#include "llvm/TypeSymbolTable.h"
#include "llvm/DerivedTypes.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
using namespace llvm;
#define DEBUG_SYMBOL_TABLE 0
#define DEBUG_ABSTYPE 0
TypeSymbolTable::~TypeSymbolTable() {
for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
if (TI->second->isAbstract()) cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
}
}
std::string TypeSymbolTable::getUniqueName(StringRef BaseName) const {
std::string TryName = BaseName;
const_iterator End = tmap.end();
while (tmap.find(TryName) != End) TryName = BaseName.str() + utostr(++LastUnique); return TryName;
}
Type* TypeSymbolTable::lookup(StringRef Name) const {
const_iterator TI = tmap.find(Name);
Type* result = 0;
if (TI != tmap.end())
result = const_cast<Type*>(TI->second);
return result;
}
Type* TypeSymbolTable::remove(iterator Entry) {
assert(Entry != tmap.end() && "Invalid entry to remove!");
const Type* Result = Entry->second;
#if DEBUG_SYMBOL_TABLE
dump();
dbgs() << " Removing Value: " << *Result << "\n";
#endif
tmap.erase(Entry);
if (Result->isAbstract()) {
#if DEBUG_ABSTYPE
dbgs() << "Removing abstract type from symtab"
<< *Result << "\n";
#endif
cast<DerivedType>(Result)->removeAbstractTypeUser(this);
}
return const_cast<Type*>(Result);
}
void TypeSymbolTable::insert(StringRef Name, const Type* T) {
assert(T && "Can't insert null type into symbol table!");
if (tmap.insert(std::make_pair(Name, T)).second) {
#if DEBUG_SYMBOL_TABLE
dump();
dbgs() << " Inserted type: " << Name << ": " << *T << "\n";
#endif
} else {
std::string UniqueName = Name;
if (lookup(Name))
UniqueName = getUniqueName(Name);
#if DEBUG_SYMBOL_TABLE
dump();
dbgs() << " Inserting type: " << UniqueName << ": "
<< *T << "\n";
#endif
tmap.insert(make_pair(UniqueName, T));
}
if (T->isAbstract()) {
cast<DerivedType>(T)->addAbstractTypeUser(this);
#if DEBUG_ABSTYPE
dbgs() << "Added abstract type to ST: " << *T << "\n";
#endif
}
}
void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
const Type *NewType) {
for (iterator I = begin(), E = end(); I != E; ++I) {
if (I->second == const_cast<DerivedType *>(OldType)) {
#if DEBUG_ABSTYPE
dbgs() << "Removing type " << *OldType << "\n";
#endif
OldType->removeAbstractTypeUser(this);
I->second = const_cast<Type *>(NewType);
if (NewType->isAbstract()) {
#if DEBUG_ABSTYPE
dbgs() << "Added type " << *NewType << "\n";
#endif
cast<DerivedType>(NewType)->addAbstractTypeUser(this);
}
}
}
}
void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
AbsTy->removeAbstractTypeUser(this);
}
static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
dbgs() << " '" << T.first << "' = ";
T.second->dump();
dbgs() << "\n";
}
void TypeSymbolTable::dump() const {
dbgs() << "TypeSymbolPlane: ";
for_each(tmap.begin(), tmap.end(), DumpTypes);
}