Expose typedefs
[oota-llvm.git] / include / llvm / SymbolTable.h
1 //===-- llvm/SymbolTable.h - Implement a type planned 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/ConstPoolVals.h"
25 #endif
26
27 class Value;
28 class Type;
29
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!
33 //
34 class SymbolTable : public AbstractTypeUser,
35                     public map<const Type *, map<const string, Value *> > {
36 public:
37   typedef map<const string, Value *> VarMap;
38   typedef map<const Type *, VarMap> super;
39 private:
40
41   SymbolTable *ParentSymTab;
42
43   friend class SymTabValue;
44   inline void setParentSymTab(SymbolTable *P) { ParentSymTab = P; }
45
46 public:
47   typedef VarMap::iterator type_iterator;
48   typedef VarMap::const_iterator type_const_iterator;
49
50   inline SymbolTable(SymbolTable *P = 0) { ParentSymTab = P; }
51   ~SymbolTable();
52
53   SymbolTable *getParentSymTab() { return ParentSymTab; }
54
55   // lookup - Returns null on failure...
56   Value *lookup(const Type *Ty, const string &name);
57
58   // find - returns end(Ty->getIDNumber()) on failure...
59   type_iterator type_find(const Type *Ty, const string &name);
60   type_iterator type_find(const Value *D);
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);
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 string &Name, Value *V) {
73     assert((isa<Type>(V) || isa<ConstPoolVal>(V)) &&
74            "Can only insert types and constants here!");
75     insertEntry(Name, V);
76   }
77
78   void remove(Value *N);
79   Value *type_remove(const type_iterator &It);
80
81   // getUniqueName - Given a base name, return a string that is either equal to
82   // it (or derived from it) that does not already occur in the symbol table for
83   // the specified type.
84   //
85   string getUniqueName(const Type *Ty, const string &BaseName);
86
87   inline unsigned type_size(const Type *TypeID) const {
88     return find(TypeID)->second.size();
89   }
90
91   // Note that type_begin / type_end only work if you know that an element of 
92   // TypeID is already in the symbol table!!!
93   //
94   inline type_iterator type_begin(const Type *TypeID) { 
95     return find(TypeID)->second.begin(); 
96   }
97   inline type_const_iterator type_begin(const Type *TypeID) const {
98     return find(TypeID)->second.begin(); 
99   }
100
101   inline type_iterator type_end(const Type *TypeID) { 
102     return find(TypeID)->second.end(); 
103   }
104   inline type_const_iterator type_end(const Type *TypeID) const { 
105     return find(TypeID)->second.end(); 
106   }
107
108   void dump() const;  // Debug method, print out symbol table
109
110 private:
111   // insertEntry - Insert a value into the symbol table with the specified
112   // name...
113   //
114   void insertEntry(const string &Name, Value *V);
115
116   // This function is called when one of the types in the type plane are refined
117   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
118 };
119
120 #endif