1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group. It is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ValueSymbolTable class for the VMCore library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/GlobalValue.h"
15 #include "llvm/Type.h"
16 #include "llvm/ValueSymbolTable.h"
17 #include "llvm/ADT/StringExtras.h"
18 #include "llvm/Support/Debug.h"
22 #define DEBUG_SYMBOL_TABLE 0
23 #define DEBUG_ABSTYPE 0
26 ValueSymbolTable::~ValueSymbolTable() {
27 #ifndef NDEBUG // Only do this in -g mode...
28 bool LeftoverValues = true;
29 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
30 if (!isa<Constant>(VI->second) ) {
31 DOUT << "Value still in symbol table! Type = '"
32 << VI->second->getType()->getDescription() << "' Name = '"
33 << VI->first << "'\n";
34 LeftoverValues = false;
36 assert(LeftoverValues && "Values remain in symbol table!");
40 // getUniqueName - Given a base name, return a string that is either equal to
41 // it (or derived from it) that does not already occur in the symbol table for
42 // the specified type.
44 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
45 std::string TryName = BaseName;
46 const_iterator End = vmap.end();
48 // See if the name exists
49 while (vmap.find(TryName) != End) // Loop until we find a free
50 TryName = BaseName + utostr(++LastUnique); // name in the symbol table
55 // lookup a value - Returns null on failure...
57 Value *ValueSymbolTable::lookup(const std::string &Name) const {
58 const_iterator VI = vmap.find(Name);
59 if (VI != vmap.end()) // We found the symbol
60 return const_cast<Value*>(VI->second);
64 // Strip the symbol table of its names.
66 bool ValueSymbolTable::strip() {
67 bool RemovedSymbol = false;
68 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) {
69 Value *V = VI->second;
71 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
72 // Set name to "", removing from symbol table!
80 // Insert a value into the symbol table with the specified name...
82 void ValueSymbolTable::insert(Value* V) {
83 assert(V && "Can't insert null Value into symbol table!");
84 assert(V->hasName() && "Can't insert nameless Value into symbol table");
86 // Check to see if there is a naming conflict. If so, rename this type!
87 std::string UniqueName = getUniqueName(V->getName());
89 #if DEBUG_SYMBOL_TABLE
91 DOUT << " Inserting value: " << UniqueName << ": " << V->dump() << "\n";
94 // Insert the vmap entry
95 vmap.insert(make_pair(UniqueName, V));
99 bool ValueSymbolTable::erase(Value *V) {
100 assert(V->hasName() && "Value doesn't have name!");
101 iterator Entry = vmap.find(V->getName());
102 if (Entry == vmap.end())
105 #if DEBUG_SYMBOL_TABLE
107 DOUT << " Removing Value: " << Entry->second->getName() << "\n";
110 // Remove the value from the plane...
116 // rename - Given a value with a non-empty name, remove its existing entry
117 // from the symbol table and insert a new one for Name. This is equivalent to
118 // doing "remove(V), V->Name = Name, insert(V)",
120 bool ValueSymbolTable::rename(Value *V, const std::string &name) {
121 assert(V && "Can't rename a null Value");
122 assert(V->hasName() && "Can't rename a nameless Value");
123 assert(!V->getName().empty() && "Can't rename an Value with null name");
124 assert(V->getName() != name && "Can't rename a Value with same name");
125 assert(!name.empty() && "Can't rename a named Value with a null name");
128 iterator VI = vmap.find(V->getName());
130 // If we didn't find it, we're done
131 if (VI == vmap.end())
134 // Remove the old entry.
137 // See if we can insert the new name.
138 VI = vmap.lower_bound(name);
140 // Is there a naming conflict?
141 if (VI != vmap.end() && VI->first == name) {
142 V->Name = getUniqueName( name);
143 vmap.insert(make_pair(V->Name, V));
146 vmap.insert(VI, make_pair(name, V));
152 // DumpVal - a std::for_each function for dumping a value
154 static void DumpVal(const std::pair<const std::string, Value *> &V) {
155 DOUT << " '" << V.first << "' = ";
160 // dump - print out the symbol table
162 void ValueSymbolTable::dump() const {
163 DOUT << "ValueSymbolTable:\n";
164 for_each(vmap.begin(), vmap.end(), DumpVal);