-// DumpVal - a std::for_each function for dumping a value
-//
-static void DumpVal(const std::pair<const std::string, Value *> &V) {
- DOUT << " '" << V.first << "' = ";
- V.second->dump();
- DOUT << "\n";
+/// createValueName - This method attempts to create a value name and insert
+/// it into the symbol table with the specified name. If it conflicts, it
+/// auto-renames the name and returns that instead.
+ValueName *ValueSymbolTable::createValueName(const char *NameStart,
+ unsigned NameLen, Value *V) {
+ ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
+ if (Entry.getValue() == 0) {
+ Entry.setValue(V);
+ //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
+ // << *V << "\n");
+ return &Entry;
+ }
+
+ // FIXME: this could be much more efficient.
+
+ // Otherwise, there is a naming conflict. Rename this value.
+ std::string UniqueName(NameStart, NameStart+NameLen);
+ while (1) {
+ // Trim any suffix off.
+ UniqueName.resize(NameLen);
+ UniqueName += utostr(++LastUnique);
+ // Try insert the vmap entry with this suffix.
+ ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
+ &UniqueName[UniqueName.size()]);
+ if (NewName.getValue() == 0) {
+ // Newly inserted name. Success!
+ NewName.setValue(V);
+ //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
+ return &NewName;
+ }
+ }