Support abstract types by keeping on the use list of the abstract type.
[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 <vector>
21 #include <map>
22
23 class Value;
24 class Type;
25
26 // TODO: Change this back to vector<map<const string, Value *> >
27 // Make the vector be a data member, and base it on UniqueID's
28 // That should be much more efficient!
29 //
30 class SymbolTable : public AbstractTypeUser,
31                     public map<const Type *, map<const string, Value *> > {
32   typedef map<const string, Value *> VarMap;
33   typedef map<const Type *, VarMap> super;
34
35   SymbolTable *ParentSymTab;
36
37   friend class SymTabValue;
38   inline void setParentSymTab(SymbolTable *P) { ParentSymTab = P; }
39
40 public:
41   typedef VarMap::iterator type_iterator;
42   typedef VarMap::const_iterator type_const_iterator;
43
44   inline SymbolTable(SymbolTable *P = 0) { ParentSymTab = P; }
45   ~SymbolTable();
46
47   SymbolTable *getParentSymTab() { return ParentSymTab; }
48
49   // lookup - Returns null on failure...
50   Value *lookup(const Type *Ty, const string &name);
51
52   // find - returns end(Ty->getIDNumber()) on failure...
53   type_iterator type_find(const Type *Ty, const string &name);
54   type_iterator type_find(const Value *D);
55
56   // insert - Add named definition to the symbol table...
57   inline void insert(Value *N) {
58     assert(N->hasName() && "Value must be named to go into symbol table!");
59     insertEntry(N->getName(), N);
60   }
61
62   // insert - Insert a constant or type into the symbol table with the specified
63   // name...  There can be a many to one mapping between names and
64   // (constant/type)s.
65   //
66   inline void insert(const string &Name, Value *V) {
67     assert((V->isType() || V->isConstant()) &&
68            "Can only insert types and constants here!");
69     insertEntry(Name, V);
70   }
71
72   void remove(Value *N);
73   Value *type_remove(const type_iterator &It);
74
75   // getUniqueName - Given a base name, return a string that is either equal to
76   // it (or derived from it) that does not already occur in the symbol table for
77   // the specified type.
78   //
79   string getUniqueName(const Type *Ty, const string &BaseName);
80
81   inline unsigned type_size(const Type *TypeID) const {
82     return find(TypeID)->second.size();
83   }
84
85   // Note that type_begin / type_end only work if you know that an element of 
86   // TypeID is already in the symbol table!!!
87   //
88   inline type_iterator type_begin(const Type *TypeID) { 
89     return find(TypeID)->second.begin(); 
90   }
91   inline type_const_iterator type_begin(const Type *TypeID) const {
92     return find(TypeID)->second.begin(); 
93   }
94
95   inline type_iterator type_end(const Type *TypeID) { 
96     return find(TypeID)->second.end(); 
97   }
98   inline type_const_iterator type_end(const Type *TypeID) const { 
99     return find(TypeID)->second.end(); 
100   }
101
102 private:
103   // insertEntry - Insert a value into the symbol table with the specified
104   // name...
105   //
106   void insertEntry(const string &Name, Value *V);
107
108   // This function is called when one of the types in the type plane are refined
109   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
110 };
111
112 #endif