142b9f9f085ef98efee52ac3a6fbafe128f9975b
[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 #define DEBUG_TYPE "valuesymtab"
15 #include "llvm/GlobalValue.h"
16 #include "llvm/Type.h"
17 #include "llvm/ValueSymbolTable.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/Debug.h"
20 #include <algorithm>
21 using namespace llvm;
22
23 // Class destructor
24 ValueSymbolTable::~ValueSymbolTable() {
25 #ifndef NDEBUG   // Only do this in -g mode...
26   bool LeftoverValues = true;
27   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
28     if (!isa<Constant>(VI->second) ) {
29       DEBUG(DOUT << "Value still in symbol table! Type = '"
30            << VI->second->getType()->getDescription() << "' Name = '"
31            << VI->first << "'\n");
32       LeftoverValues = false;
33     }
34   assert(LeftoverValues && "Values remain in symbol table!");
35 #endif
36 }
37
38 // getUniqueName - Given a base name, return a string that is either equal to
39 // it (or derived from it) that does not already occur in the symbol table for
40 // the specified type.
41 //
42 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
43   std::string TryName = BaseName;
44   const_iterator End = vmap.end();
45
46   // See if the name exists
47   while (vmap.find(TryName) != End)            // Loop until we find a free
48     TryName = BaseName + utostr(++LastUnique); // name in the symbol table
49   return TryName;
50 }
51
52
53 // lookup a value - Returns null on failure...
54 //
55 Value *ValueSymbolTable::lookup(const std::string &Name) const {
56   const_iterator VI = vmap.find(Name);
57   if (VI != vmap.end())                   // We found the symbol
58     return const_cast<Value*>(VI->second);
59   return 0;
60 }
61
62 // Insert a value into the symbol table with the specified name...
63 //
64 void ValueSymbolTable::insert(Value* V) {
65   assert(V && "Can't insert null Value into symbol table!");
66   assert(V->hasName() && "Can't insert nameless Value into symbol table");
67
68   // Try inserting the name, assuming it won't conflict.
69   if (vmap.insert(make_pair(V->Name, V)).second) {
70     DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
71     return;
72   }
73   
74   // Otherwise, there is a naming conflict.  Rename this value.
75   std::string UniqueName = V->getName();
76   unsigned BaseSize = UniqueName.size();
77   do {
78     // Trim any suffix off.
79     UniqueName.resize(BaseSize);
80     UniqueName += utostr(++LastUnique);
81   } while (!vmap.insert(make_pair(UniqueName, V)).second);
82
83   DEBUG(DOUT << " Inserting value: " << UniqueName << ": " << *V << "\n");
84
85   // Insert the vmap entry
86   V->Name = UniqueName;
87 }
88
89 // Remove a value
90 void ValueSymbolTable::remove(Value *V) {
91   assert(V->hasName() && "Value doesn't have name!");
92   iterator Entry = vmap.find(V->getName());
93   assert(Entry != vmap.end() && "Entry was not in the symtab!");
94
95   DEBUG(DOUT << " Removing Value: " << Entry->second->getName() << "\n");
96
97   // Remove the value from the plane...
98   vmap.erase(Entry);
99 }
100
101 // DumpVal - a std::for_each function for dumping a value
102 //
103 static void DumpVal(const std::pair<const std::string, Value *> &V) {
104   DOUT << "  '" << V.first << "' = ";
105   V.second->dump();
106   DOUT << "\n";
107 }
108
109 // dump - print out the symbol table
110 //
111 void ValueSymbolTable::dump() const {
112   DOUT << "ValueSymbolTable:\n";
113   for_each(vmap.begin(), vmap.end(), DumpVal);
114 }