2003-10-13 03:32:08 +00:00
|
|
|
//===-- SymbolTable.cpp - Implement the SymbolTable class -----------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2004-05-25 08:52:42 +00:00
|
|
|
// This file was developed by the LLVM research group and revised by Reid
|
|
|
|
// Spencer. It is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file implements the SymbolTable class for the VMCore library.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/SymbolTable.h"
|
2001-09-07 16:48:17 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-10-23 15:30:18 +00:00
|
|
|
#include "llvm/Module.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2002-05-10 18:54:35 +00:00
|
|
|
#include <algorithm>
|
2004-07-04 11:55:08 +00:00
|
|
|
#include <iostream>
|
2004-05-25 08:52:42 +00:00
|
|
|
|
2003-11-21 20:23:48 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2001-10-23 01:53:22 +00:00
|
|
|
#define DEBUG_SYMBOL_TABLE 0
|
|
|
|
#define DEBUG_ABSTYPE 0
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
SymbolTable::~SymbolTable() {
|
2001-09-07 16:48:17 +00:00
|
|
|
// Drop all abstract type references in the type plane...
|
2004-05-25 08:52:42 +00:00
|
|
|
for (type_iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
|
|
|
|
if (TI->second->isAbstract()) // If abstract, drop the reference...
|
|
|
|
cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
|
2001-09-07 16:48:17 +00:00
|
|
|
}
|
2001-11-03 03:27:53 +00:00
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
// TODO: FIXME: BIG ONE: This doesn't unreference abstract types for the
|
|
|
|
// planes that could still have entries!
|
2001-11-03 03:27:53 +00:00
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#ifndef NDEBUG // Only do this in -g mode...
|
2001-09-07 16:48:17 +00:00
|
|
|
bool LeftoverValues = true;
|
2004-05-25 08:52:42 +00:00
|
|
|
for (plane_iterator PI = pmap.begin(); PI != pmap.end(); ++PI) {
|
|
|
|
for (value_iterator VI = PI->second.begin(); VI != PI->second.end(); ++VI)
|
|
|
|
if (!isa<Constant>(VI->second) ) {
|
2004-07-17 23:57:36 +00:00
|
|
|
std::cerr << "Value still in symbol table! Type = '"
|
2004-05-25 08:52:42 +00:00
|
|
|
<< PI->first->getDescription() << "' Name = '"
|
|
|
|
<< VI->first << "'\n";
|
2004-07-17 23:57:36 +00:00
|
|
|
LeftoverValues = false;
|
2001-09-07 16:48:17 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-09-07 16:48:17 +00:00
|
|
|
|
|
|
|
assert(LeftoverValues && "Values remain in symbol table!");
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-06-25 07:33:13 +00:00
|
|
|
// getUniqueName - Given a base name, return a string that is either equal to
|
|
|
|
// it (or derived from it) that does not already occur in the symbol table for
|
|
|
|
// the specified type.
|
|
|
|
//
|
2003-05-22 21:47:17 +00:00
|
|
|
std::string SymbolTable::getUniqueName(const Type *Ty,
|
2004-05-25 08:52:42 +00:00
|
|
|
const std::string &BaseName) const {
|
|
|
|
// Find the plane
|
|
|
|
plane_const_iterator PI = pmap.find(Ty);
|
|
|
|
if (PI == pmap.end()) return BaseName;
|
2001-06-25 07:33:13 +00:00
|
|
|
|
2003-05-22 21:47:17 +00:00
|
|
|
std::string TryName = BaseName;
|
2004-05-25 08:52:42 +00:00
|
|
|
const ValueMap& vmap = PI->second;
|
|
|
|
value_const_iterator End = vmap.end();
|
2001-06-25 07:33:13 +00:00
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
// See if the name exists
|
|
|
|
while (vmap.find(TryName) != End) // Loop until we find a free
|
2003-11-09 19:39:48 +00:00
|
|
|
TryName = BaseName + utostr(++LastUnique); // name in the symbol table
|
2001-06-25 07:33:13 +00:00
|
|
|
return TryName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
// lookup a value - Returns null on failure...
|
2003-12-31 07:08:19 +00:00
|
|
|
Value *SymbolTable::lookup(const Type *Ty, const std::string &Name) const {
|
2004-05-25 08:52:42 +00:00
|
|
|
plane_const_iterator PI = pmap.find(Ty);
|
2004-08-04 00:37:31 +00:00
|
|
|
if (PI != pmap.end()) { // We have symbols in that plane.
|
2004-05-25 08:52:42 +00:00
|
|
|
value_const_iterator VI = PI->second.find(Name);
|
2004-08-04 00:37:31 +00:00
|
|
|
if (VI != PI->second.end()) // and the name is in our hash table.
|
2004-05-25 08:52:42 +00:00
|
|
|
return VI->second;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2004-05-25 08:52:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
// lookup a type by name - returns null on failure
|
|
|
|
Type* SymbolTable::lookupType( const std::string& Name ) const {
|
|
|
|
type_const_iterator TI = tmap.find( Name );
|
|
|
|
if ( TI != tmap.end() )
|
2004-07-04 11:55:08 +00:00
|
|
|
return const_cast<Type*>(TI->second);
|
2002-03-08 20:26:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
// Remove a value
|
2001-06-06 20:29:01 +00:00
|
|
|
void SymbolTable::remove(Value *N) {
|
|
|
|
assert(N->hasName() && "Value doesn't have name!");
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
plane_iterator PI = pmap.find(N->getType());
|
|
|
|
assert(PI != pmap.end() &&
|
|
|
|
"Trying to remove a value that doesn't have a type plane yet!");
|
|
|
|
removeEntry(PI, PI->second.find(N->getName()));
|
2001-10-23 01:53:22 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2005-03-06 02:14:28 +00:00
|
|
|
/// changeName - Given a value with a non-empty name, remove its existing entry
|
|
|
|
/// from the symbol table and insert a new one for Name. This is equivalent to
|
|
|
|
/// doing "remove(V), V->Name = Name, insert(V)", but is faster, and will not
|
|
|
|
/// temporarily remove the symbol table plane if V is the last value in the
|
|
|
|
/// symtab with that name (which could invalidate iterators to that plane).
|
|
|
|
void SymbolTable::changeName(Value *V, const std::string &name) {
|
|
|
|
assert(!V->getName().empty() && !name.empty() && V->getName() != name &&
|
|
|
|
"Illegal use of this method!");
|
|
|
|
|
|
|
|
plane_iterator PI = pmap.find(V->getType());
|
|
|
|
assert(PI != pmap.end() && "Value doesn't have an entry in this table?");
|
|
|
|
ValueMap &VM = PI->second;
|
|
|
|
|
2005-03-06 05:21:40 +00:00
|
|
|
value_iterator VI = VM.find(V->getName());
|
|
|
|
assert(VI != VM.end() && "Value does have an entry in this table?");
|
2005-03-06 02:14:28 +00:00
|
|
|
|
2005-03-06 05:21:40 +00:00
|
|
|
// Remove the old entry.
|
|
|
|
VM.erase(VI);
|
2005-03-06 02:14:28 +00:00
|
|
|
|
|
|
|
// See if we can insert the new name.
|
|
|
|
VI = VM.lower_bound(name);
|
|
|
|
|
|
|
|
// Is there a naming conflict?
|
|
|
|
if (VI != VM.end() && VI->first == name) {
|
|
|
|
V->Name = getUniqueName(V->getType(), name);
|
|
|
|
VM.insert(make_pair(V->Name, V));
|
|
|
|
} else {
|
|
|
|
V->Name = name;
|
|
|
|
VM.insert(VI, make_pair(name, V));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
|
2001-10-23 01:53:22 +00:00
|
|
|
// removeEntry - Remove a value from the symbol table...
|
2004-05-25 08:52:42 +00:00
|
|
|
Value *SymbolTable::removeEntry(plane_iterator Plane, value_iterator Entry) {
|
|
|
|
assert(Plane != pmap.end() &&
|
2001-10-23 01:53:22 +00:00
|
|
|
Entry != Plane->second.end() && "Invalid entry to remove!");
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-10-23 01:53:22 +00:00
|
|
|
Value *Result = Entry->second;
|
2001-06-06 20:29:01 +00:00
|
|
|
#if DEBUG_SYMBOL_TABLE
|
2002-04-07 22:33:13 +00:00
|
|
|
dump();
|
|
|
|
std::cerr << " Removing Value: " << Result->getName() << "\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|
|
|
|
|
2001-09-07 16:48:17 +00:00
|
|
|
// Remove the value from the plane...
|
2001-10-23 01:53:22 +00:00
|
|
|
Plane->second.erase(Entry);
|
|
|
|
|
|
|
|
// If the plane is empty, remove it now!
|
|
|
|
if (Plane->second.empty()) {
|
|
|
|
// If the plane represented an abstract type that we were interested in,
|
|
|
|
// unlink ourselves from this plane.
|
|
|
|
//
|
|
|
|
if (Plane->first->isAbstract()) {
|
|
|
|
#if DEBUG_ABSTYPE
|
2003-05-22 21:47:17 +00:00
|
|
|
std::cerr << "Plane Empty: Removing type: "
|
|
|
|
<< Plane->first->getDescription() << "\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
|
|
|
cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
|
|
|
|
}
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
pmap.erase(Plane);
|
2001-10-23 01:53:22 +00:00
|
|
|
}
|
2004-05-25 08:52:42 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// removeEntry - Remove a type from the symbol table...
|
2005-03-06 05:46:41 +00:00
|
|
|
Type* SymbolTable::remove(type_iterator Entry) {
|
2004-05-25 08:52:42 +00:00
|
|
|
assert( Entry != tmap.end() && "Invalid entry to remove!");
|
|
|
|
|
2004-07-04 11:55:08 +00:00
|
|
|
const Type* Result = Entry->second;
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
#if DEBUG_SYMBOL_TABLE
|
|
|
|
dump();
|
|
|
|
std::cerr << " Removing Value: " << Result->getName() << "\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tmap.erase(Entry);
|
2001-09-07 16:48:17 +00:00
|
|
|
|
|
|
|
// If we are removing an abstract type, remove the symbol table from it's use
|
|
|
|
// list...
|
2004-05-25 08:52:42 +00:00
|
|
|
if (Result->isAbstract()) {
|
2001-10-23 01:53:22 +00:00
|
|
|
#if DEBUG_ABSTYPE
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << "Removing abstract type from symtab" << Result->getDescription()<<"\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
2004-05-25 08:52:42 +00:00
|
|
|
cast<DerivedType>(Result)->removeAbstractTypeUser(this);
|
2001-09-07 16:48:17 +00:00
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-07-04 11:55:08 +00:00
|
|
|
return const_cast<Type*>(Result);
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
// insertEntry - Insert a value into the symbol table with the specified name.
|
2003-05-22 21:47:17 +00:00
|
|
|
void SymbolTable::insertEntry(const std::string &Name, const Type *VTy,
|
|
|
|
Value *V) {
|
2004-08-04 00:37:31 +00:00
|
|
|
plane_iterator PI = pmap.find(VTy); // Plane iterator
|
|
|
|
value_iterator VI; // Actual value iterator
|
|
|
|
ValueMap *VM; // The plane we care about.
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
#if DEBUG_SYMBOL_TABLE
|
2002-04-07 22:33:13 +00:00
|
|
|
dump();
|
2003-05-22 21:47:17 +00:00
|
|
|
std::cerr << " Inserting definition: " << Name << ": "
|
|
|
|
<< VTy->getDescription() << "\n";
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
if (PI == pmap.end()) { // Not in collection yet... insert dummy entry
|
2001-10-22 04:55:44 +00:00
|
|
|
// Insert a new empty element. I points to the new elements.
|
2004-08-04 00:37:31 +00:00
|
|
|
VM = &pmap.insert(make_pair(VTy, ValueMap())).first->second;
|
|
|
|
VI = VM->end();
|
2001-10-22 04:55:44 +00:00
|
|
|
|
|
|
|
// Check to see if the type is abstract. If so, it might be refined in the
|
|
|
|
// future, which would cause the plane of the old type to get merged into
|
|
|
|
// a new type plane.
|
|
|
|
//
|
2001-10-23 01:53:22 +00:00
|
|
|
if (VTy->isAbstract()) {
|
2001-10-22 04:55:44 +00:00
|
|
|
cast<DerivedType>(VTy)->addAbstractTypeUser(this);
|
2001-10-23 01:53:22 +00:00
|
|
|
#if DEBUG_ABSTYPE
|
2003-05-22 21:47:17 +00:00
|
|
|
std::cerr << "Added abstract type value: " << VTy->getDescription()
|
|
|
|
<< "\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
2004-08-04 00:37:31 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// Check to see if there is a naming conflict. If so, rename this value!
|
|
|
|
VM = &PI->second;
|
|
|
|
VI = VM->lower_bound(Name);
|
|
|
|
if (VI != VM->end() && VI->first == Name) {
|
2005-03-06 05:21:40 +00:00
|
|
|
V->Name = getUniqueName(VTy, Name);
|
|
|
|
VM->insert(make_pair(V->Name, V));
|
2004-08-04 00:37:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-08-04 00:37:31 +00:00
|
|
|
VM->insert(VI, make_pair(Name, V));
|
2004-05-25 08:52:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// insertEntry - Insert a value into the symbol table with the specified
|
|
|
|
// name...
|
|
|
|
//
|
2004-07-04 11:55:08 +00:00
|
|
|
void SymbolTable::insertEntry(const std::string& Name, const Type* T) {
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
// Check to see if there is a naming conflict. If so, rename this type!
|
|
|
|
std::string UniqueName = Name;
|
|
|
|
if (lookupType(Name))
|
|
|
|
UniqueName = getUniqueName(T, Name);
|
|
|
|
|
|
|
|
#if DEBUG_SYMBOL_TABLE
|
|
|
|
dump();
|
|
|
|
std::cerr << " Inserting type: " << UniqueName << ": "
|
|
|
|
<< T->getDescription() << "\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Insert the tmap entry
|
|
|
|
tmap.insert(make_pair(UniqueName, T));
|
2001-09-07 16:48:17 +00:00
|
|
|
|
|
|
|
// If we are adding an abstract type, add the symbol table to it's use list.
|
2004-05-25 08:52:42 +00:00
|
|
|
if (T->isAbstract()) {
|
|
|
|
cast<DerivedType>(T)->addAbstractTypeUser(this);
|
2001-10-23 01:53:22 +00:00
|
|
|
#if DEBUG_ABSTYPE
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
2004-05-25 08:52:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the symbol table of its names.
|
2005-03-05 19:02:15 +00:00
|
|
|
bool SymbolTable::strip() {
|
2004-05-25 08:52:42 +00:00
|
|
|
bool RemovedSymbol = false;
|
|
|
|
for (plane_iterator I = pmap.begin(); I != pmap.end();) {
|
|
|
|
// Removing items from the plane can cause the plane itself to get deleted.
|
|
|
|
// If this happens, make sure we incremented our plane iterator already!
|
|
|
|
ValueMap &Plane = (I++)->second;
|
|
|
|
value_iterator B = Plane.begin(), Bend = Plane.end();
|
|
|
|
while (B != Bend) { // Found nonempty type plane!
|
|
|
|
Value *V = B->second;
|
2005-03-05 19:02:15 +00:00
|
|
|
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
|
2004-07-17 23:57:36 +00:00
|
|
|
// Set name to "", removing from symbol table!
|
2005-03-05 19:02:15 +00:00
|
|
|
V->setName("");
|
2004-05-25 08:52:42 +00:00
|
|
|
RemovedSymbol = true;
|
|
|
|
}
|
|
|
|
++B;
|
2001-10-23 01:53:22 +00:00
|
|
|
}
|
2001-09-07 16:48:17 +00:00
|
|
|
}
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
for (type_iterator TI = tmap.begin(); TI != tmap.end(); ) {
|
2005-03-06 05:46:41 +00:00
|
|
|
remove(TI++);
|
2004-05-25 08:52:42 +00:00
|
|
|
RemovedSymbol = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RemovedSymbol;
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
|
2001-09-07 16:48:17 +00:00
|
|
|
// This function is called when one of the types in the type plane are refined
|
|
|
|
void SymbolTable::refineAbstractType(const DerivedType *OldType,
|
2004-07-17 23:57:36 +00:00
|
|
|
const Type *NewType) {
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
// Search to see if we have any values of the type Oldtype. If so, we need to
|
2001-11-03 03:27:53 +00:00
|
|
|
// move them into the newtype plane...
|
2004-05-25 08:52:42 +00:00
|
|
|
plane_iterator PI = pmap.find(OldType);
|
|
|
|
if (PI != pmap.end()) {
|
2001-11-03 03:27:53 +00:00
|
|
|
// Get a handle to the new type plane...
|
2004-05-25 08:52:42 +00:00
|
|
|
plane_iterator NewTypeIt = pmap.find(NewType);
|
|
|
|
if (NewTypeIt == pmap.end()) { // If no plane exists, add one
|
|
|
|
NewTypeIt = pmap.insert(make_pair(NewType, ValueMap())).first;
|
2001-11-03 03:27:53 +00:00
|
|
|
|
|
|
|
if (NewType->isAbstract()) {
|
|
|
|
cast<DerivedType>(NewType)->addAbstractTypeUser(this);
|
2001-10-23 01:53:22 +00:00
|
|
|
#if DEBUG_ABSTYPE
|
2003-05-22 21:47:17 +00:00
|
|
|
std::cerr << "[Added] refined to abstype: " << NewType->getDescription()
|
|
|
|
<< "\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
2002-01-25 03:56:34 +00:00
|
|
|
}
|
2001-10-23 01:53:22 +00:00
|
|
|
}
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
ValueMap &NewPlane = NewTypeIt->second;
|
|
|
|
ValueMap &OldPlane = PI->second;
|
2001-10-22 04:55:44 +00:00
|
|
|
while (!OldPlane.empty()) {
|
2003-05-22 21:47:17 +00:00
|
|
|
std::pair<const std::string, Value*> V = *OldPlane.begin();
|
2001-10-22 21:59:46 +00:00
|
|
|
|
|
|
|
// Check to see if there is already a value in the symbol table that this
|
|
|
|
// would collide with.
|
2004-05-25 08:52:42 +00:00
|
|
|
value_iterator VI = NewPlane.find(V.first);
|
|
|
|
if (VI != NewPlane.end() && VI->second == V.second) {
|
2001-10-22 21:59:46 +00:00
|
|
|
// No action
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
} else if (VI != NewPlane.end()) {
|
2002-12-15 16:41:52 +00:00
|
|
|
// The only thing we are allowing for now is two external global values
|
2001-10-22 21:59:46 +00:00
|
|
|
// folded into one.
|
|
|
|
//
|
2004-05-25 08:52:42 +00:00
|
|
|
GlobalValue *ExistGV = dyn_cast<GlobalValue>(VI->second);
|
2002-12-15 16:41:52 +00:00
|
|
|
GlobalValue *NewGV = dyn_cast<GlobalValue>(V.second);
|
2001-10-23 02:32:45 +00:00
|
|
|
|
2003-08-29 04:49:54 +00:00
|
|
|
if (ExistGV && NewGV) {
|
|
|
|
assert((ExistGV->isExternal() || NewGV->isExternal()) &&
|
|
|
|
"Two planes folded together with overlapping value names!");
|
|
|
|
|
|
|
|
// Make sure that ExistGV is the one we want to keep!
|
2003-08-29 05:11:34 +00:00
|
|
|
if (!NewGV->isExternal())
|
2003-08-29 04:49:54 +00:00
|
|
|
std::swap(NewGV, ExistGV);
|
|
|
|
|
2002-12-15 16:41:52 +00:00
|
|
|
// Ok we have two external global values. Make all uses of the new
|
|
|
|
// one use the old one...
|
2003-08-29 05:11:34 +00:00
|
|
|
NewGV->uncheckedReplaceAllUsesWith(ExistGV);
|
2001-10-23 02:32:45 +00:00
|
|
|
|
2005-03-06 05:13:42 +00:00
|
|
|
// Update NewGV's name, we're about the remove it from the symbol
|
|
|
|
// table.
|
|
|
|
NewGV->Name = "";
|
2001-10-23 15:30:18 +00:00
|
|
|
|
2002-12-15 16:41:52 +00:00
|
|
|
// Now we can remove this global from the module entirely...
|
|
|
|
Module *M = NewGV->getParent();
|
|
|
|
if (Function *F = dyn_cast<Function>(NewGV))
|
|
|
|
M->getFunctionList().remove(F);
|
|
|
|
else
|
|
|
|
M->getGlobalList().remove(cast<GlobalVariable>(NewGV));
|
|
|
|
delete NewGV;
|
2003-11-19 01:02:52 +00:00
|
|
|
} else {
|
|
|
|
// If they are not global values, they must be just random values who
|
|
|
|
// happen to conflict now that types have been resolved. If this is
|
|
|
|
// the case, reinsert the value into the new plane, allowing it to get
|
|
|
|
// renamed.
|
|
|
|
assert(V.second->getType() == NewType &&"Type resolution is broken!");
|
|
|
|
insert(V.second);
|
2001-10-23 02:32:45 +00:00
|
|
|
}
|
2001-10-22 21:59:46 +00:00
|
|
|
} else {
|
|
|
|
insertEntry(V.first, NewType, V.second);
|
|
|
|
}
|
|
|
|
// Remove the item from the old type plane
|
2001-10-22 04:55:44 +00:00
|
|
|
OldPlane.erase(OldPlane.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ok, now we are not referencing the type anymore... take me off your user
|
|
|
|
// list please!
|
2001-10-23 01:53:22 +00:00
|
|
|
#if DEBUG_ABSTYPE
|
2003-05-22 21:47:17 +00:00
|
|
|
std::cerr << "Removing type " << OldType->getDescription() << "\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
2001-10-22 04:55:44 +00:00
|
|
|
OldType->removeAbstractTypeUser(this);
|
2001-10-23 01:53:22 +00:00
|
|
|
|
|
|
|
// Remove the plane that is no longer used
|
2004-05-25 08:52:42 +00:00
|
|
|
pmap.erase(PI);
|
2001-10-22 04:55:44 +00:00
|
|
|
}
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
// 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
|
|
|
|
// occurrences, and although we only need to remove one at a time, it's
|
|
|
|
// faster to remove them all in one pass.
|
|
|
|
//
|
|
|
|
for (type_iterator I = type_begin(), E = type_end(); I != E; ++I) {
|
|
|
|
if (I->second == (Type*)OldType) { // FIXME when Types aren't const.
|
2001-10-23 01:53:22 +00:00
|
|
|
#if DEBUG_ABSTYPE
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << "Removing type " << OldType->getDescription() << "\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
2004-05-25 08:52:42 +00:00
|
|
|
OldType->removeAbstractTypeUser(this);
|
2002-01-25 03:56:34 +00:00
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
I->second = (Type*)NewType; // TODO FIXME when types aren't const
|
|
|
|
if (NewType->isAbstract()) {
|
2001-10-23 01:53:22 +00:00
|
|
|
#if DEBUG_ABSTYPE
|
2004-07-17 23:57:36 +00:00
|
|
|
std::cerr << "Added type " << NewType->getDescription() << "\n";
|
2001-10-23 01:53:22 +00:00
|
|
|
#endif
|
2004-07-17 23:57:36 +00:00
|
|
|
cast<DerivedType>(NewType)->addAbstractTypeUser(this);
|
2001-10-23 01:53:22 +00:00
|
|
|
}
|
2004-05-25 08:52:42 +00:00
|
|
|
}
|
2002-01-25 03:56:34 +00:00
|
|
|
}
|
2001-09-07 16:48:17 +00:00
|
|
|
}
|
2001-10-03 19:28:15 +00:00
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
// Handle situation where type becomes Concreate from Abstract
|
2003-10-03 18:46:24 +00:00
|
|
|
void SymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
|
2004-05-25 08:52:42 +00:00
|
|
|
plane_iterator PI = pmap.find(AbsTy);
|
2003-10-03 18:46:24 +00:00
|
|
|
|
|
|
|
// If there are any values in the symbol table of this type, then the type
|
2004-05-25 08:52:42 +00:00
|
|
|
// plane is a use of the abstract type which must be dropped.
|
|
|
|
if (PI != pmap.end())
|
2003-10-03 18:46:24 +00:00
|
|
|
AbsTy->removeAbstractTypeUser(this);
|
|
|
|
|
2004-05-25 08:52:42 +00:00
|
|
|
// 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.
|
|
|
|
for (type_iterator TI = type_begin(), TE = type_end(); TI != TE; ++TI)
|
|
|
|
if (TI->second == (Type*)AbsTy) // FIXME when Types aren't const.
|
|
|
|
AbsTy->removeAbstractTypeUser(this);
|
2003-10-03 18:46:24 +00:00
|
|
|
}
|
|
|
|
|
2003-05-22 21:47:17 +00:00
|
|
|
static void DumpVal(const std::pair<const std::string, Value *> &V) {
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << " '" << V.first << "' = ";
|
2002-04-07 22:33:13 +00:00
|
|
|
V.second->dump();
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << "\n";
|
2001-10-03 19:28:15 +00:00
|
|
|
}
|
|
|
|
|
2003-05-22 21:47:17 +00:00
|
|
|
static void DumpPlane(const std::pair<const Type *,
|
|
|
|
std::map<const std::string, Value *> >&P){
|
2002-04-07 22:33:13 +00:00
|
|
|
P.first->dump();
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << "\n";
|
2001-10-03 19:28:15 +00:00
|
|
|
for_each(P.second.begin(), P.second.end(), DumpVal);
|
|
|
|
}
|
|
|
|
|
2004-07-04 11:55:08 +00:00
|
|
|
static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << " '" << T.first << "' = ";
|
|
|
|
T.second->dump();
|
|
|
|
std::cerr << "\n";
|
|
|
|
}
|
|
|
|
|
2001-10-03 19:28:15 +00:00
|
|
|
void SymbolTable::dump() const {
|
2004-05-25 08:52:42 +00:00
|
|
|
std::cerr << "Symbol table dump:\n Plane:";
|
|
|
|
for_each(pmap.begin(), pmap.end(), DumpPlane);
|
|
|
|
std::cerr << " Types: ";
|
|
|
|
for_each(tmap.begin(), tmap.end(), DumpTypes);
|
2001-10-03 19:28:15 +00:00
|
|
|
}
|
2004-05-25 08:52:42 +00:00
|
|
|
|
|
|
|
// vim: sw=2 ai
|