1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and revised by Reid
6 // Spencer. It is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file implements the ValueSymbolTable class for the VMCore library.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/GlobalValue.h"
16 #include "llvm/Type.h"
17 #include "llvm/ValueSymbolTable.h"
18 #include "llvm/ADT/StringExtras.h"
24 #define DEBUG_SYMBOL_TABLE 0
25 #define DEBUG_ABSTYPE 0
28 ValueSymbolTable::~ValueSymbolTable() {
29 #ifndef NDEBUG // Only do this in -g mode...
30 bool LeftoverValues = true;
31 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
32 if (!isa<Constant>(VI->second) ) {
33 std::cerr << "Value still in symbol table! Type = '"
34 << VI->second->getType()->getDescription() << "' Name = '"
35 << VI->first << "'\n";
36 LeftoverValues = false;
38 assert(LeftoverValues && "Values remain in symbol table!");
42 // getUniqueName - Given a base name, return a string that is either equal to
43 // it (or derived from it) that does not already occur in the symbol table for
44 // the specified type.
46 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
47 std::string TryName = BaseName;
48 const_iterator End = vmap.end();
50 // See if the name exists
51 while (vmap.find(TryName) != End) // Loop until we find a free
52 TryName = BaseName + utostr(++LastUnique); // name in the symbol table
57 // lookup a value - Returns null on failure...
59 Value *ValueSymbolTable::lookup(const std::string &Name) const {
60 const_iterator VI = vmap.find(Name);
61 if (VI != vmap.end()) // We found the symbol
62 return const_cast<Value*>(VI->second);
66 // Strip the symbol table of its names.
68 bool ValueSymbolTable::strip() {
69 bool RemovedSymbol = false;
70 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) {
71 Value *V = VI->second;
73 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
74 // Set name to "", removing from symbol table!
82 // Insert a value into the symbol table with the specified name...
84 void ValueSymbolTable::insert(Value* V) {
85 assert(V && "Can't insert null Value into symbol table!");
86 assert(V->hasName() && "Can't insert nameless Value into symbol table");
88 // Check to see if there is a naming conflict. If so, rename this type!
89 std::string UniqueName = getUniqueName(V->getName());
91 #if DEBUG_SYMBOL_TABLE
93 std::cerr << " Inserting value: " << UniqueName << ": " << V->dump() << "\n";
96 // Insert the vmap entry
97 vmap.insert(make_pair(UniqueName, V));
101 bool ValueSymbolTable::erase(Value *V) {
102 assert(V->hasName() && "Value doesn't have name!");
103 iterator Entry = vmap.find(V->getName());
104 if (Entry == vmap.end())
107 #if DEBUG_SYMBOL_TABLE
109 std::cerr << " Removing Value: " << Entry->second->getName() << "\n";
112 // Remove the value from the plane...
118 // rename - Given a value with a non-empty name, remove its existing entry
119 // from the symbol table and insert a new one for Name. This is equivalent to
120 // doing "remove(V), V->Name = Name, insert(V)",
122 bool ValueSymbolTable::rename(Value *V, const std::string &name) {
123 assert(V && "Can't rename a null Value");
124 assert(V->hasName() && "Can't rename a nameless Value");
125 assert(!V->getName().empty() && "Can't rename an Value with null name");
126 assert(V->getName() != name && "Can't rename a Value with same name");
127 assert(!name.empty() && "Can't rename a named Value with a null name");
130 iterator VI = vmap.find(V->getName());
132 // If we didn't find it, we're done
133 if (VI == vmap.end())
136 // Remove the old entry.
139 // See if we can insert the new name.
140 VI = vmap.lower_bound(name);
142 // Is there a naming conflict?
143 if (VI != vmap.end() && VI->first == name) {
144 V->Name = getUniqueName( name);
145 vmap.insert(make_pair(V->Name, V));
148 vmap.insert(VI, make_pair(name, V));
154 // DumpVal - a std::for_each function for dumping a value
156 static void DumpVal(const std::pair<const std::string, Value *> &V) {
157 std::cerr << " '" << V.first << "' = ";
162 // dump - print out the symbol table
164 void ValueSymbolTable::dump() const {
165 std::cerr << "ValueSymbolTable:\n";
166 for_each(vmap.begin(), vmap.end(), DumpVal);