6efb9983874ef087f91f5ae2dc262bd36968556c
[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.  It is distributed under 
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ValueSymbolTable class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
19 #include <algorithm>
20 using namespace llvm;
21
22 #define DEBUG_SYMBOL_TABLE 0
23 #define DEBUG_ABSTYPE 0
24
25 // Class destructor
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;
35     }
36   assert(LeftoverValues && "Values remain in symbol table!");
37 #endif
38 }
39
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.
43 //
44 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
45   std::string TryName = BaseName;
46   const_iterator End = vmap.end();
47
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
51   return TryName;
52 }
53
54
55 // lookup a value - Returns null on failure...
56 //
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);
61   return 0;
62 }
63
64 // Strip the symbol table of its names.
65 //
66 bool ValueSymbolTable::strip() {
67   bool RemovedSymbol = false;
68   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) {
69     Value *V = VI->second;
70     ++VI;
71     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
72       // Set name to "", removing from symbol table!
73       V->setName("");
74       RemovedSymbol = true;
75     }
76   }
77   return RemovedSymbol;
78 }
79
80 // Insert a value into the symbol table with the specified name...
81 //
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");
85
86   // Check to see if there is a naming conflict.  If so, rename this type!
87   std::string UniqueName = getUniqueName(V->getName());
88
89 #if DEBUG_SYMBOL_TABLE
90   dump();
91   DOUT << " Inserting value: " << UniqueName << ": " << V->dump() << "\n";
92 #endif
93
94   // Insert the vmap entry
95   vmap.insert(make_pair(UniqueName, V));
96 }
97
98 // Remove a value
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())
103     return false;
104
105 #if DEBUG_SYMBOL_TABLE
106   dump();
107   DOUT << " Removing Value: " << Entry->second->getName() << "\n";
108 #endif
109
110   // Remove the value from the plane...
111   vmap.erase(Entry);
112   return true;
113 }
114
115
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)", 
119 //
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");
126
127   // Find the name
128   iterator VI = vmap.find(V->getName());
129
130   // If we didn't find it, we're done
131   if (VI == vmap.end())
132     return false;
133
134   // Remove the old entry.
135   vmap.erase(VI);
136
137   // See if we can insert the new name.
138   VI = vmap.lower_bound(name);
139
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));
144   } else {
145     V->Name = name;
146     vmap.insert(VI, make_pair(name, V));
147   }
148
149   return true;
150 }
151
152 // DumpVal - a std::for_each function for dumping a value
153 //
154 static void DumpVal(const std::pair<const std::string, Value *> &V) {
155   DOUT << "  '" << V.first << "' = ";
156   V.second->dump();
157   DOUT << "\n";
158 }
159
160 // dump - print out the symbol table
161 //
162 void ValueSymbolTable::dump() const {
163   DOUT << "ValueSymbolTable:\n";
164   for_each(vmap.begin(), vmap.end(), DumpVal);
165 }