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/Debug.h"
19
#include "llvm/Support/ManagedStatic.h"
20
#include "llvm/Support/raw_ostream.h"
24
#define DEBUG_SYMBOL_TABLE 0
25
#define DEBUG_ABSTYPE 0
27
TypeSymbolTable::~TypeSymbolTable() {
28
// Drop all abstract type references in the type plane...
29
for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
30
if (TI->second->isAbstract()) // If abstract, drop the reference...
31
cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
35
std::string TypeSymbolTable::getUniqueName(StringRef BaseName) const {
36
std::string TryName = BaseName;
38
const_iterator End = tmap.end();
40
// See if the name exists
41
while (tmap.find(TryName) != End) // Loop until we find a free
42
TryName = BaseName.str() + utostr(++LastUnique); // name in the symbol table
46
// lookup a type by name - returns null on failure
47
Type* TypeSymbolTable::lookup(StringRef Name) const {
48
const_iterator TI = tmap.find(Name);
51
result = const_cast<Type*>(TI->second);
55
// remove - Remove a type from the symbol table...
56
Type* TypeSymbolTable::remove(iterator Entry) {
57
assert(Entry != tmap.end() && "Invalid entry to remove!");
58
const Type* Result = Entry->second;
60
#if DEBUG_SYMBOL_TABLE
62
dbgs() << " Removing Value: " << Result->getDescription() << "\n";
67
// If we are removing an abstract type, remove the symbol table from it's use
69
if (Result->isAbstract()) {
71
dbgs() << "Removing abstract type from symtab"
72
<< Result->getDescription()
75
cast<DerivedType>(Result)->removeAbstractTypeUser(this);
78
return const_cast<Type*>(Result);
82
// insert - Insert a type into the symbol table with the specified name...
83
void TypeSymbolTable::insert(StringRef Name, const Type* T) {
84
assert(T && "Can't insert null type into symbol table!");
86
if (tmap.insert(std::make_pair(Name, T)).second) {
87
// Type inserted fine with no conflict.
89
#if DEBUG_SYMBOL_TABLE
91
dbgs() << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
94
// If there is a name conflict...
96
// Check to see if there is a naming conflict. If so, rename this type!
97
std::string UniqueName = Name;
99
UniqueName = getUniqueName(Name);
101
#if DEBUG_SYMBOL_TABLE
103
dbgs() << " Inserting type: " << UniqueName << ": "
104
<< T->getDescription() << "\n";
107
// Insert the tmap entry
108
tmap.insert(make_pair(UniqueName, T));
111
// If we are adding an abstract type, add the symbol table to it's use list.
112
if (T->isAbstract()) {
113
cast<DerivedType>(T)->addAbstractTypeUser(this);
115
dbgs() << "Added abstract type to ST: " << T->getDescription() << "\n";
120
// This function is called when one of the types in the type plane are refined
121
void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
122
const Type *NewType) {
123
// Loop over all of the types in the symbol table, replacing any references
124
// to OldType with references to NewType. Note that there may be multiple
125
// occurrences, and although we only need to remove one at a time, it's
126
// faster to remove them all in one pass.
128
for (iterator I = begin(), E = end(); I != E; ++I) {
129
if (I->second == (Type*)OldType) { // FIXME when Types aren't const.
131
dbgs() << "Removing type " << OldType->getDescription() << "\n";
133
OldType->removeAbstractTypeUser(this);
135
I->second = (Type*)NewType; // TODO FIXME when types aren't const
136
if (NewType->isAbstract()) {
138
dbgs() << "Added type " << NewType->getDescription() << "\n";
140
cast<DerivedType>(NewType)->addAbstractTypeUser(this);
147
// Handle situation where type becomes Concreate from Abstract
148
void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
149
// Loop over all of the types in the symbol table, dropping any abstract
150
// type user entries for AbsTy which occur because there are names for the
152
for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
153
if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
154
AbsTy->removeAbstractTypeUser(this);
157
static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
158
dbgs() << " '" << T.first << "' = ";
163
void TypeSymbolTable::dump() const {
164
dbgs() << "TypeSymbolPlane: ";
165
for_each(tmap.begin(), tmap.end(), DumpTypes);