LLVM API Documentation

ValueSymbolTable.cpp

Go to the documentation of this file.
00001 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group.  It is distributed under 
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the ValueSymbolTable class for the VMCore library.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/GlobalValue.h"
00015 #include "llvm/Type.h"
00016 #include "llvm/ValueSymbolTable.h"
00017 #include "llvm/ADT/StringExtras.h"
00018 #include <algorithm>
00019 #include <iostream>
00020 
00021 using namespace llvm;
00022 
00023 #define DEBUG_SYMBOL_TABLE 0
00024 #define DEBUG_ABSTYPE 0
00025 
00026 // Class destructor
00027 ValueSymbolTable::~ValueSymbolTable() {
00028 #ifndef NDEBUG   // Only do this in -g mode...
00029   bool LeftoverValues = true;
00030   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
00031     if (!isa<Constant>(VI->second) ) {
00032       std::cerr << "Value still in symbol table! Type = '"
00033                 << VI->second->getType()->getDescription() << "' Name = '"
00034                 << VI->first << "'\n";
00035       LeftoverValues = false;
00036     }
00037   assert(LeftoverValues && "Values remain in symbol table!");
00038 #endif
00039 }
00040 
00041 // getUniqueName - Given a base name, return a string that is either equal to
00042 // it (or derived from it) that does not already occur in the symbol table for
00043 // the specified type.
00044 //
00045 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
00046   std::string TryName = BaseName;
00047   const_iterator End = vmap.end();
00048 
00049   // See if the name exists
00050   while (vmap.find(TryName) != End)            // Loop until we find a free
00051     TryName = BaseName + utostr(++LastUnique); // name in the symbol table
00052   return TryName;
00053 }
00054 
00055 
00056 // lookup a value - Returns null on failure...
00057 //
00058 Value *ValueSymbolTable::lookup(const std::string &Name) const {
00059   const_iterator VI = vmap.find(Name);
00060   if (VI != vmap.end())                   // We found the symbol
00061     return const_cast<Value*>(VI->second);
00062   return 0;
00063 }
00064 
00065 // Strip the symbol table of its names.
00066 //
00067 bool ValueSymbolTable::strip() {
00068   bool RemovedSymbol = false;
00069   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) {
00070     Value *V = VI->second;
00071     ++VI;
00072     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
00073       // Set name to "", removing from symbol table!
00074       V->setName("");
00075       RemovedSymbol = true;
00076     }
00077   }
00078   return RemovedSymbol;
00079 }
00080 
00081 // Insert a value into the symbol table with the specified name...
00082 //
00083 void ValueSymbolTable::insert(Value* V) {
00084   assert(V && "Can't insert null Value into symbol table!");
00085   assert(V->hasName() && "Can't insert nameless Value into symbol table");
00086 
00087   // Check to see if there is a naming conflict.  If so, rename this type!
00088   std::string UniqueName = getUniqueName(V->getName());
00089 
00090 #if DEBUG_SYMBOL_TABLE
00091   dump();
00092   std::cerr << " Inserting value: " << UniqueName << ": " << V->dump() << "\n";
00093 #endif
00094 
00095   // Insert the vmap entry
00096   vmap.insert(make_pair(UniqueName, V));
00097 }
00098 
00099 // Remove a value
00100 bool ValueSymbolTable::erase(Value *V) {
00101   assert(V->hasName() && "Value doesn't have name!");
00102   iterator Entry = vmap.find(V->getName());
00103   if (Entry == vmap.end())
00104     return false;
00105 
00106 #if DEBUG_SYMBOL_TABLE
00107   dump();
00108   std::cerr << " Removing Value: " << Entry->second->getName() << "\n";
00109 #endif
00110 
00111   // Remove the value from the plane...
00112   vmap.erase(Entry);
00113   return true;
00114 }
00115 
00116 
00117 // rename - Given a value with a non-empty name, remove its existing entry
00118 // from the symbol table and insert a new one for Name.  This is equivalent to
00119 // doing "remove(V), V->Name = Name, insert(V)", 
00120 //
00121 bool ValueSymbolTable::rename(Value *V, const std::string &name) {
00122   assert(V && "Can't rename a null Value");
00123   assert(V->hasName() && "Can't rename a nameless Value");
00124   assert(!V->getName().empty() && "Can't rename an Value with null name");
00125   assert(V->getName() != name && "Can't rename a Value with same name");
00126   assert(!name.empty() && "Can't rename a named Value with a null name");
00127 
00128   // Find the name
00129   iterator VI = vmap.find(V->getName());
00130 
00131   // If we didn't find it, we're done
00132   if (VI == vmap.end())
00133     return false;
00134 
00135   // Remove the old entry.
00136   vmap.erase(VI);
00137 
00138   // See if we can insert the new name.
00139   VI = vmap.lower_bound(name);
00140 
00141   // Is there a naming conflict?
00142   if (VI != vmap.end() && VI->first == name) {
00143     V->Name = getUniqueName( name);
00144     vmap.insert(make_pair(V->Name, V));
00145   } else {
00146     V->Name = name;
00147     vmap.insert(VI, make_pair(name, V));
00148   }
00149 
00150   return true;
00151 }
00152 
00153 // DumpVal - a std::for_each function for dumping a value
00154 //
00155 static void DumpVal(const std::pair<const std::string, Value *> &V) {
00156   std::cerr << "  '" << V.first << "' = ";
00157   V.second->dump();
00158   std::cerr << "\n";
00159 }
00160 
00161 // dump - print out the symbol table
00162 //
00163 void ValueSymbolTable::dump() const {
00164   std::cerr << "ValueSymbolTable:\n";
00165   for_each(vmap.begin(), vmap.end(), DumpVal);
00166 }