For PR411:
[oota-llvm.git] / lib / VMCore / ValueSymbolTable.cpp
1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 // This file implements the ValueSymbolTable class for the VMCore library.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/GlobalValue.h"
16 #include "llvm/Type.h"
17 #include "llvm/ValueSymbolTable.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include <algorithm>
20 #include <iostream>
21
22 using namespace llvm;
23
24 #define DEBUG_SYMBOL_TABLE 0
25 #define DEBUG_ABSTYPE 0
26
27 // Class destructor
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;
37     }
38   assert(LeftoverValues && "Values remain in symbol table!");
39 #endif
40 }
41
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.
45 //
46 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
47   std::string TryName = BaseName;
48   const_iterator End = vmap.end();
49
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
53   return TryName;
54 }
55
56
57 // lookup a value - Returns null on failure...
58 //
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);
63   return 0;
64 }
65
66 // Strip the symbol table of its names.
67 //
68 bool ValueSymbolTable::strip() {
69   bool RemovedSymbol = false;
70   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) {
71     Value *V = VI->second;
72     ++VI;
73     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
74       // Set name to "", removing from symbol table!
75       V->setName("");
76       RemovedSymbol = true;
77     }
78   }
79   return RemovedSymbol;
80 }
81
82 // Insert a value into the symbol table with the specified name...
83 //
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");
87
88   // Check to see if there is a naming conflict.  If so, rename this type!
89   std::string UniqueName = getUniqueName(V->getName());
90
91 #if DEBUG_SYMBOL_TABLE
92   dump();
93   std::cerr << " Inserting value: " << UniqueName << ": " << V->dump() << "\n";
94 #endif
95
96   // Insert the vmap entry
97   vmap.insert(make_pair(UniqueName, V));
98 }
99
100 // Remove a value
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())
105     return false;
106
107 #if DEBUG_SYMBOL_TABLE
108   dump();
109   std::cerr << " Removing Value: " << Entry->second->getName() << "\n";
110 #endif
111
112   // Remove the value from the plane...
113   vmap.erase(Entry);
114   return true;
115 }
116
117
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)", 
121 //
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");
128
129   // Find the name
130   iterator VI = vmap.find(V->getName());
131
132   // If we didn't find it, we're done
133   if (VI == vmap.end())
134     return false;
135
136   // Remove the old entry.
137   vmap.erase(VI);
138
139   // See if we can insert the new name.
140   VI = vmap.lower_bound(name);
141
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));
146   } else {
147     V->Name = name;
148     vmap.insert(VI, make_pair(name, V));
149   }
150
151   return true;
152 }
153
154 // DumpVal - a std::for_each function for dumping a value
155 //
156 static void DumpVal(const std::pair<const std::string, Value *> &V) {
157   std::cerr << "  '" << V.first << "' = ";
158   V.second->dump();
159   std::cerr << "\n";
160 }
161
162 // dump - print out the symbol table
163 //
164 void ValueSymbolTable::dump() const {
165   std::cerr << "ValueSymbolTable:\n";
166   for_each(vmap.begin(), vmap.end(), DumpVal);
167 }