Add a new version of Module::getFunction that takes a const char* instead
[oota-llvm.git] / lib / VMCore / ValueSymbolTable.cpp
1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 using namespace llvm;
21
22 // Class destructor
23 ValueSymbolTable::~ValueSymbolTable() {
24 #ifndef NDEBUG   // Only do this in -g mode...
25   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
26     cerr << "Value still in symbol table! Type = '"
27          << VI->getValue()->getType()->getDescription() << "' Name = '"
28          << VI->getKeyData() << "'\n";
29   assert(vmap.empty() && "Values remain in symbol table!");
30 #endif
31 }
32
33 // getUniqueName - Given a base name, return a string that is either equal to
34 // it (or derived from it) that does not already occur in the symbol table for
35 // the specified type.
36 //
37 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
38   std::string TryName = BaseName;
39
40   // See if the name exists
41   while (vmap.find(&TryName[0], &TryName[TryName.size()]) != vmap.end())
42     // Loop until we find a free name in the symbol table.
43     TryName = BaseName + utostr(++LastUnique);
44   return TryName;
45 }
46
47
48 // lookup a value - Returns null on failure...
49 //
50 Value *ValueSymbolTable::lookup(const std::string &Name) const {
51   const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
52   if (VI != vmap.end())                   // We found the symbol
53     return VI->getValue();
54   return 0;
55 }
56
57 Value *ValueSymbolTable::lookup(const char *NameBegin,
58                                 const char *NameEnd) const {
59   const_iterator VI = vmap.find(NameBegin, NameEnd);
60   if (VI != vmap.end())                   // We found the symbol
61     return VI->getValue();
62   return 0;
63 }
64
65 // Insert a value into the symbol table with the specified name...
66 //
67 void ValueSymbolTable::reinsertValue(Value* V) {
68   assert(V->hasName() && "Can't insert nameless Value into symbol table");
69
70   // Try inserting the name, assuming it won't conflict.
71   if (vmap.insert(V->Name)) {
72     //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
73     return;
74   }
75   
76   // FIXME: this could be much more efficient.
77   
78   // Otherwise, there is a naming conflict.  Rename this value.
79   std::string UniqueName = V->getName();
80   
81   V->Name->Destroy();
82   
83   unsigned BaseSize = UniqueName.size();
84   while (1) {
85     // Trim any suffix off.
86     UniqueName.resize(BaseSize);
87     UniqueName += utostr(++LastUnique);
88     // Try insert the vmap entry with this suffix.
89     ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
90                                                &UniqueName[UniqueName.size()]);
91     if (NewName.getValue() == 0) {
92       // Newly inserted name.  Success!
93       NewName.setValue(V);
94       V->Name = &NewName;
95       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
96       return;
97     }
98   }
99 }
100
101 void ValueSymbolTable::removeValueName(ValueName *V) {
102   //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
103   // Remove the value from the plane.
104   vmap.remove(V);
105 }
106
107 /// createValueName - This method attempts to create a value name and insert
108 /// it into the symbol table with the specified name.  If it conflicts, it
109 /// auto-renames the name and returns that instead.
110 ValueName *ValueSymbolTable::createValueName(const char *NameStart,
111                                              unsigned NameLen, Value *V) {
112   ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
113   if (Entry.getValue() == 0) {
114     Entry.setValue(V);
115     //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
116     //           << *V << "\n");
117     return &Entry;
118   }
119   
120   // FIXME: this could be much more efficient.
121   
122   // Otherwise, there is a naming conflict.  Rename this value.
123   std::string UniqueName(NameStart, NameStart+NameLen);
124   while (1) {
125     // Trim any suffix off.
126     UniqueName.resize(NameLen);
127     UniqueName += utostr(++LastUnique);
128     // Try insert the vmap entry with this suffix.
129     ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
130                                                &UniqueName[UniqueName.size()]);
131     if (NewName.getValue() == 0) {
132       // Newly inserted name.  Success!
133       NewName.setValue(V);
134       //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
135       return &NewName;
136     }
137   }
138 }
139
140
141 // dump - print out the symbol table
142 //
143 void ValueSymbolTable::dump() const {
144   //DOUT << "ValueSymbolTable:\n";
145   for (const_iterator I = begin(), E = end(); I != E; ++I) {
146     //DOUT << "  '" << I->getKeyData() << "' = ";
147     I->getValue()->dump();
148     //DOUT << "\n";
149   }
150 }