* Add new method localLookup
[oota-llvm.git] / include / llvm / SymbolTable.h
1 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ------*- C++ -*-=//
2 //
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 
5 // distinct.
6 //
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 
9 // searched.
10 //
11 // This chaining behavior does NOT affect iterators though: only the lookup 
12 // method
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SYMBOL_TABLE_H
17 #define LLVM_SYMBOL_TABLE_H
18
19 #include "llvm/Value.h"
20 #include <map>
21
22 #ifndef NDEBUG             // Only for assertions
23 #include "llvm/Type.h"
24 #include "llvm/ConstantVals.h"
25 #endif
26
27 class Type;
28
29 class SymbolTable : public AbstractTypeUser,
30                     public std::map<const Type *, 
31                                     std::map<const std::string, Value *> > {
32 public:
33   typedef std::map<const std::string, Value *> VarMap;
34   typedef std::map<const Type *, VarMap> super;
35 private:
36
37   SymbolTable *ParentSymTab;
38
39   friend class SymTabValue;
40   inline void setParentSymTab(SymbolTable *P) { ParentSymTab = P; }
41
42 public:
43   typedef VarMap::iterator type_iterator;
44   typedef VarMap::const_iterator type_const_iterator;
45
46   inline SymbolTable(SymbolTable *P = 0) {
47     ParentSymTab = P;
48     InternallyInconsistent = false;
49   }
50   ~SymbolTable();
51
52   SymbolTable *getParentSymTab() { return ParentSymTab; }
53
54   // lookup - Returns null on failure...
55   Value *lookup(const Type *Ty, const std::string &name);
56
57   // localLookup - Look in this symbol table without falling back on parent,
58   // if non-existing.  Returns null on failure...
59   //
60   Value *localLookup(const Type *Ty, const std::string &name);
61
62   // insert - Add named definition to the symbol table...
63   inline void insert(Value *N) {
64     assert(N->hasName() && "Value must be named to go into symbol table!");
65     insertEntry(N->getName(), N->getType(), N);
66   }
67
68   // insert - Insert a constant or type into the symbol table with the specified
69   // name...  There can be a many to one mapping between names and
70   // (constant/type)s.
71   //
72   inline void insert(const std::string &Name, Value *V) {
73     assert((isa<Type>(V) || isa<Constant>(V)) &&
74            "Can only insert types and constants here!");
75     insertEntry(Name, V->getType(), V);
76   }
77
78   void remove(Value *N);
79   Value *type_remove(const type_iterator &It) {
80     return removeEntry(find(It->second->getType()), It);
81   }
82
83   // getUniqueName - Given a base name, return a string that is either equal to
84   // it (or derived from it) that does not already occur in the symbol table for
85   // the specified type.
86   //
87   std::string getUniqueName(const Type *Ty, const std::string &BaseName);
88
89   inline unsigned type_size(const Type *TypeID) const {
90     return find(TypeID)->second.size();
91   }
92
93   // Note that type_begin / type_end only work if you know that an element of 
94   // TypeID is already in the symbol table!!!
95   //
96   inline type_iterator type_begin(const Type *TypeID) { 
97     return find(TypeID)->second.begin(); 
98   }
99   inline type_const_iterator type_begin(const Type *TypeID) const {
100     return find(TypeID)->second.begin(); 
101   }
102
103   inline type_iterator type_end(const Type *TypeID) { 
104     return find(TypeID)->second.end(); 
105   }
106   inline type_const_iterator type_end(const Type *TypeID) const { 
107     return find(TypeID)->second.end(); 
108   }
109
110   void dump() const;  // Debug method, print out symbol table
111
112 private:
113   // InternallyInconsistent - There are times when the symbol table is
114   // internally inconsistent with the rest of the program.  In this one case, a
115   // value exists with a Name, and it's not in the symbol table.  When we call
116   // V->setName(""), it tries to remove itself from the symbol table and dies.
117   // We know this is happening, and so if the flag InternallyInconsistent is
118   // set, removal from the symbol table is a noop.
119   //
120   bool InternallyInconsistent;
121
122   inline super::value_type operator[](const Type *Ty) {
123     assert(0 && "Should not use this operator to access symbol table!");
124     return super::value_type();
125   }
126
127   // insertEntry - Insert a value into the symbol table with the specified
128   // name...
129   //
130   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
131
132   // removeEntry - Remove a value from the symbol table...
133   //
134   Value *removeEntry(iterator Plane, type_iterator Entry);
135
136   // This function is called when one of the types in the type plane are refined
137   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
138 };
139
140 #endif