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"
23 #define DEBUG_SYMBOL_TABLE 0
24 #define DEBUG_ABSTYPE 0
27 ValueSymbolTable::~ValueSymbolTable() {
28 #ifndef NDEBUG // Only do this in -g mode...
29 bool LeftoverValues
= true;
30 for (iterator VI
= vmap
.begin(), VE
= vmap
.end(); VI
!= VE
; ++VI
)
31 if (!isa
<Constant
>(VI
->second
) ) {
32 std::cerr
<< "Value still in symbol table! Type = '"
33 << VI
->second
->getType()->getDescription() << "' Name = '"
34 << VI
->first
<< "'\n";
35 LeftoverValues
= false;
37 assert(LeftoverValues
&& "Values remain in symbol table!");
41 // getUniqueName - Given a base name, return a string that is either equal to
42 // it (or derived from it) that does not already occur in the symbol table for
43 // the specified type.
45 std::string
ValueSymbolTable::getUniqueName(const std::string
&BaseName
) const {
46 std::string TryName
= BaseName
;
47 const_iterator End
= vmap
.end();
49 // See if the name exists
50 while (vmap
.find(TryName
) != End
) // Loop until we find a free
51 TryName
= BaseName
+ utostr(++LastUnique
); // name in the symbol table
56 // lookup a value - Returns null on failure...
58 Value
*ValueSymbolTable::lookup(const std::string
&Name
) const {
59 const_iterator VI
= vmap
.find(Name
);
60 if (VI
!= vmap
.end()) // We found the symbol
61 return const_cast<Value
*>(VI
->second
);
65 // Strip the symbol table of its names.
67 bool ValueSymbolTable::strip() {
68 bool RemovedSymbol
= false;
69 for (iterator VI
= vmap
.begin(), VE
= vmap
.end(); VI
!= VE
; ) {
70 Value
*V
= VI
->second
;
72 if (!isa
<GlobalValue
>(V
) || cast
<GlobalValue
>(V
)->hasInternalLinkage()) {
73 // Set name to "", removing from symbol table!
81 // Insert a value into the symbol table with the specified name...
83 void ValueSymbolTable::insert(Value
* V
) {
84 assert(V
&& "Can't insert null Value into symbol table!");
85 assert(V
->hasName() && "Can't insert nameless Value into symbol table");
87 // Check to see if there is a naming conflict. If so, rename this type!
88 std::string UniqueName
= getUniqueName(V
->getName());
90 #if DEBUG_SYMBOL_TABLE
92 std::cerr
<< " Inserting value: " << UniqueName
<< ": " << V
->dump() << "\n";
95 // Insert the vmap entry
96 vmap
.insert(make_pair(UniqueName
, V
));
100 bool ValueSymbolTable::erase(Value
*V
) {
101 assert(V
->hasName() && "Value doesn't have name!");
102 iterator Entry
= vmap
.find(V
->getName());
103 if (Entry
== vmap
.end())
106 #if DEBUG_SYMBOL_TABLE
108 std::cerr
<< " Removing Value: " << Entry
->second
->getName() << "\n";
111 // Remove the value from the plane...
117 // rename - Given a value with a non-empty name, remove its existing entry
118 // from the symbol table and insert a new one for Name. This is equivalent to
119 // doing "remove(V), V->Name = Name, insert(V)",
121 bool ValueSymbolTable::rename(Value
*V
, const std::string
&name
) {
122 assert(V
&& "Can't rename a null Value");
123 assert(V
->hasName() && "Can't rename a nameless Value");
124 assert(!V
->getName().empty() && "Can't rename an Value with null name");
125 assert(V
->getName() != name
&& "Can't rename a Value with same name");
126 assert(!name
.empty() && "Can't rename a named Value with a null name");
129 iterator VI
= vmap
.find(V
->getName());
131 // If we didn't find it, we're done
132 if (VI
== vmap
.end())
135 // Remove the old entry.
138 // See if we can insert the new name.
139 VI
= vmap
.lower_bound(name
);
141 // Is there a naming conflict?
142 if (VI
!= vmap
.end() && VI
->first
== name
) {
143 V
->Name
= getUniqueName( name
);
144 vmap
.insert(make_pair(V
->Name
, V
));
147 vmap
.insert(VI
, make_pair(name
, V
));
153 // DumpVal - a std::for_each function for dumping a value
155 static void DumpVal(const std::pair
<const std::string
, Value
*> &V
) {
156 std::cerr
<< " '" << V
.first
<< "' = ";
161 // dump - print out the symbol table
163 void ValueSymbolTable::dump() const {
164 std::cerr
<< "ValueSymbolTable:\n";
165 for_each(vmap
.begin(), vmap
.end(), DumpVal
);