1 //===-- llvm/SymbolTable.h - Implement a type planned symtab ------*- C++ -*-=//
3 // This file implements a symbol table that has planed broken up by type.
4 // Identical types may have overlapping symbol names as long as they are
7 // Note that this implements a chained symbol table. If a name being 'lookup'd
8 // isn't found in the current symbol table, then the parent symbol table is
11 // This chaining behavior does NOT affect iterators though: only the lookup
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SYMBOL_TABLE_H
17 #define LLVM_SYMBOL_TABLE_H
19 #include "llvm/Value.h"
22 #ifndef NDEBUG // Only for assertions
23 #include "llvm/Type.h"
24 #include "llvm/ConstPoolVals.h"
30 // TODO: Change this back to vector<map<const string, Value *> >
31 // Make the vector be a data member, and base it on UniqueID's
32 // That should be much more efficient!
34 class SymbolTable : public AbstractTypeUser,
35 public map<const Type *, map<const string, Value *> > {
37 typedef map<const string, Value *> VarMap;
38 typedef map<const Type *, VarMap> super;
41 SymbolTable *ParentSymTab;
43 friend class SymTabValue;
44 inline void setParentSymTab(SymbolTable *P) { ParentSymTab = P; }
47 typedef VarMap::iterator type_iterator;
48 typedef VarMap::const_iterator type_const_iterator;
50 inline SymbolTable(SymbolTable *P = 0) { ParentSymTab = P; }
53 SymbolTable *getParentSymTab() { return ParentSymTab; }
55 // lookup - Returns null on failure...
56 Value *lookup(const Type *Ty, const string &name);
58 // insert - Add named definition to the symbol table...
59 inline void insert(Value *N) {
60 assert(N->hasName() && "Value must be named to go into symbol table!");
61 insertEntry(N->getName(), N->getType(), N);
64 // insert - Insert a constant or type into the symbol table with the specified
65 // name... There can be a many to one mapping between names and
68 inline void insert(const string &Name, Value *V) {
69 assert((isa<Type>(V) || isa<ConstPoolVal>(V)) &&
70 "Can only insert types and constants here!");
71 insertEntry(Name, V->getType(), V);
74 void remove(Value *N);
75 Value *type_remove(const type_iterator &It) {
76 return removeEntry(find(It->second->getType()), It);
79 // getUniqueName - Given a base name, return a string that is either equal to
80 // it (or derived from it) that does not already occur in the symbol table for
81 // the specified type.
83 string getUniqueName(const Type *Ty, const string &BaseName);
85 inline unsigned type_size(const Type *TypeID) const {
86 return find(TypeID)->second.size();
89 // Note that type_begin / type_end only work if you know that an element of
90 // TypeID is already in the symbol table!!!
92 inline type_iterator type_begin(const Type *TypeID) {
93 return find(TypeID)->second.begin();
95 inline type_const_iterator type_begin(const Type *TypeID) const {
96 return find(TypeID)->second.begin();
99 inline type_iterator type_end(const Type *TypeID) {
100 return find(TypeID)->second.end();
102 inline type_const_iterator type_end(const Type *TypeID) const {
103 return find(TypeID)->second.end();
106 void dump() const; // Debug method, print out symbol table
109 inline super::value_type operator[](const Type *Ty) {
110 assert(0 && "Should not use this operator to access symbol table!");
111 return super::value_type();
114 // insertEntry - Insert a value into the symbol table with the specified
117 void insertEntry(const string &Name, const Type *Ty, Value *V);
119 // removeEntry - Remove a value from the symbol table...
121 Value *removeEntry(iterator Plane, type_iterator Entry);
123 // This function is called when one of the types in the type plane are refined
124 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);