Use archive libraries instead of object files for VMCore, BCReader,
[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 <algorithm>
19 #include <iostream>
20
21 using namespace llvm;
22
23 #define DEBUG_SYMBOL_TABLE 0
24 #define DEBUG_ABSTYPE 0
25
26 // Class destructor
27 ValueSymbolTable::~ValueSymbolTable() {
28 #ifndef NDEBUG   // Only do this in -g mode...
29   bool LeftoverValues = true;
30   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
31     if (!isa<Constant>(VI->second) ) {
32       std::cerr << "Value still in symbol table! Type = '"
33                 << VI->second->getType()->getDescription() << "' Name = '"
34                 << VI->first << "'\n";
35       LeftoverValues = false;
36     }
37   assert(LeftoverValues && "Values remain in symbol table!");
38 #endif
39 }
40
41 // getUniqueName - Given a base name, return a string that is either equal to
42 // it (or derived from it) that does not already occur in the symbol table for
43 // the specified type.
44 //
45 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
46   std::string TryName = BaseName;
47   const_iterator End = vmap.end();
48
49   // See if the name exists
50   while (vmap.find(TryName) != End)            // Loop until we find a free
51     TryName = BaseName + utostr(++LastUnique); // name in the symbol table
52   return TryName;
53 }
54
55
56 // lookup a value - Returns null on failure...
57 //
58 Value *ValueSymbolTable::lookup(const std::string &Name) const {
59   const_iterator VI = vmap.find(Name);
60   if (VI != vmap.end())                   // We found the symbol
61     return const_cast<Value*>(VI->second);
62   return 0;
63 }
64
65 // Strip the symbol table of its names.
66 //
67 bool ValueSymbolTable::strip() {
68   bool RemovedSymbol = false;
69   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) {
70     Value *V = VI->second;
71     ++VI;
72     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
73       // Set name to "", removing from symbol table!
74       V->setName("");
75       RemovedSymbol = true;
76     }
77   }
78   return RemovedSymbol;
79 }
80
81 // Insert a value into the symbol table with the specified name...
82 //
83 void ValueSymbolTable::insert(Value* V) {
84   assert(V && "Can't insert null Value into symbol table!");
85   assert(V->hasName() && "Can't insert nameless Value into symbol table");
86
87   // Check to see if there is a naming conflict.  If so, rename this type!
88   std::string UniqueName = getUniqueName(V->getName());
89
90 #if DEBUG_SYMBOL_TABLE
91   dump();
92   std::cerr << " Inserting value: " << UniqueName << ": " << V->dump() << "\n";
93 #endif
94
95   // Insert the vmap entry
96   vmap.insert(make_pair(UniqueName, V));
97 }
98
99 // Remove a value
100 bool ValueSymbolTable::erase(Value *V) {
101   assert(V->hasName() && "Value doesn't have name!");
102   iterator Entry = vmap.find(V->getName());
103   if (Entry == vmap.end())
104     return false;
105
106 #if DEBUG_SYMBOL_TABLE
107   dump();
108   std::cerr << " Removing Value: " << Entry->second->getName() << "\n";
109 #endif
110
111   // Remove the value from the plane...
112   vmap.erase(Entry);
113   return true;
114 }
115
116
117 // rename - Given a value with a non-empty name, remove its existing entry
118 // from the symbol table and insert a new one for Name.  This is equivalent to
119 // doing "remove(V), V->Name = Name, insert(V)", 
120 //
121 bool ValueSymbolTable::rename(Value *V, const std::string &name) {
122   assert(V && "Can't rename a null Value");
123   assert(V->hasName() && "Can't rename a nameless Value");
124   assert(!V->getName().empty() && "Can't rename an Value with null name");
125   assert(V->getName() != name && "Can't rename a Value with same name");
126   assert(!name.empty() && "Can't rename a named Value with a null name");
127
128   // Find the name
129   iterator VI = vmap.find(V->getName());
130
131   // If we didn't find it, we're done
132   if (VI == vmap.end())
133     return false;
134
135   // Remove the old entry.
136   vmap.erase(VI);
137
138   // See if we can insert the new name.
139   VI = vmap.lower_bound(name);
140
141   // Is there a naming conflict?
142   if (VI != vmap.end() && VI->first == name) {
143     V->Name = getUniqueName( name);
144     vmap.insert(make_pair(V->Name, V));
145   } else {
146     V->Name = name;
147     vmap.insert(VI, make_pair(name, V));
148   }
149
150   return true;
151 }
152
153 // DumpVal - a std::for_each function for dumping a value
154 //
155 static void DumpVal(const std::pair<const std::string, Value *> &V) {
156   std::cerr << "  '" << V.first << "' = ";
157   V.second->dump();
158   std::cerr << "\n";
159 }
160
161 // dump - print out the symbol table
162 //
163 void ValueSymbolTable::dump() const {
164   std::cerr << "ValueSymbolTable:\n";
165   for_each(vmap.begin(), vmap.end(), DumpVal);
166 }