1 //===-- SymbolTable.cpp - Implement the SymbolTable class -------------------=//
3 // This file implements the SymbolTable class for the VMCore library.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/SymbolTable.h"
8 #include "llvm/InstrTypes.h"
9 #include "llvm/Tools/StringExtras.h"
11 #include "llvm/BasicBlock.h" // Required for assertions to work.
12 #include "llvm/Type.h"
15 SymbolTable::~SymbolTable() {
16 #ifndef NDEBUG // Only do this in -g mode...
18 for (iterator i = begin(); i != end(); ++i) {
19 if (i->second.begin() != i->second.end()) {
20 for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
21 cerr << "Value still in symbol table! Type = " << i->first->getName()
22 << " Name = " << I->first << endl;
26 assert(Good && "Values remain in symbol table!");
30 SymbolTable::type_iterator SymbolTable::type_find(const Value *D) {
31 assert(D->hasName() && "type_find(Value*) only works on named nodes!");
32 return type_find(D->getType(), D->getName());
36 // find - returns end(Ty->getIDNumber()) on failure...
37 SymbolTable::type_iterator SymbolTable::type_find(const Type *Ty,
39 iterator I = find(Ty);
40 if (I == end()) { // Not in collection yet... insert dummy entry
41 (*this)[Ty] = VarMap();
43 assert(I != end() && "How did insert fail?");
46 return I->second.find(Name);
49 // getUniqueName - Given a base name, return a string that is either equal to
50 // it (or derived from it) that does not already occur in the symbol table for
51 // the specified type.
53 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
54 iterator I = find(Ty);
55 if (I == end()) return BaseName;
57 string TryName = BaseName;
59 type_iterator End = I->second.end();
61 while (I->second.find(TryName) != End) // Loop until we find unoccupied
62 TryName = BaseName + utostr(++Counter); // Name in the symbol table
68 // lookup - Returns null on failure...
69 Value *SymbolTable::lookup(const Type *Ty, const string &Name) {
70 iterator I = find(Ty);
71 if (I != end()) { // We have symbols in that plane...
72 type_iterator J = I->second.find(Name);
73 if (J != I->second.end()) // and the name is in our hash table...
77 return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
80 void SymbolTable::remove(Value *N) {
81 assert(N->hasName() && "Value doesn't have name!");
82 assert(type_find(N) != type_end(N->getType()) &&
83 "Value not in symbol table!");
84 type_remove(type_find(N));
88 #define DEBUG_SYMBOL_TABLE 0
90 Value *SymbolTable::type_remove(const type_iterator &It) {
91 Value *Result = It->second;
92 #if DEBUG_SYMBOL_TABLE
93 cerr << this << " Removing Value: " << Result->getName() << endl;
96 find(Result->getType())->second.erase(It);
101 void SymbolTable::insert(Value *N) {
102 assert(N->hasName() && "Value must be named to go into symbol table!");
104 // TODO: The typeverifier should catch this when its implemented
105 if (lookup(N->getType(), N->getName())) {
106 cerr << "SymbolTable WARNING: Name already in symbol table: '"
107 << N->getName() << "'\n";
108 abort(); // TODO: REMOVE THIS
111 #if DEBUG_SYMBOL_TABLE
112 cerr << this << " Inserting definition: " << N->getName() << ": "
113 << N->getType()->getName() << endl;
116 iterator I = find(N->getType());
117 if (I == end()) { // Not in collection yet... insert dummy entry
118 (*this)[N->getType()] = VarMap();
119 I = find(N->getType());
120 assert(I != end() && "How did insert fail?");
123 I->second.insert(make_pair(N->getName(), N));