dfb78eee822ad1767b27e1a692af20e9ff147e4a
[oota-llvm.git] / include / llvm / SymbolTable.h
1 //===-- llvm/SymbolTable.h - Implement a type planed 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 <vector>
20 #include <map>
21 #include <string>
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 map<const Type *, map<const string, Value *> > {
31   typedef map<const string, Value *> VarMap;
32   typedef map<const Type *, VarMap> super;
33
34   SymbolTable *ParentSymTab;
35
36   friend class SymTabValue;
37   inline void setParentSymTab(SymbolTable *P) { ParentSymTab = P; }
38
39 public:
40   typedef VarMap::iterator type_iterator;
41   typedef VarMap::const_iterator type_const_iterator;
42
43   inline SymbolTable(SymbolTable *P = 0) { ParentSymTab = P; }
44   ~SymbolTable();
45
46   SymbolTable *getParentSymTab() { return ParentSymTab; }
47
48   // lookup - Returns null on failure...
49   Value *lookup(const Type *Ty, const string &name);
50
51   // find - returns end(Ty->getIDNumber()) on failure...
52   type_iterator type_find(const Type *Ty, const string &name);
53   type_iterator type_find(const Value *D);
54
55   // insert - Add named definition to the symbol table...
56   void insert(Value *N);
57
58   void remove(Value *N);
59   Value *type_remove(const type_iterator &It);
60
61   inline unsigned type_size(const Type *TypeID) const {
62     return find(TypeID)->second.size();
63   }
64
65   // Note that type_begin / type_end only work if you know that an element of 
66   // TypeID is already in the symbol table!!!
67   //
68   inline type_iterator type_begin(const Type *TypeID) { 
69     return find(TypeID)->second.begin(); 
70   }
71   inline type_const_iterator type_begin(const Type *TypeID) const {
72     return find(TypeID)->second.begin(); 
73   }
74
75   inline type_iterator type_end(const Type *TypeID) { 
76     return find(TypeID)->second.end(); 
77   }
78   inline type_const_iterator type_end(const Type *TypeID) const { 
79     return find(TypeID)->second.end(); 
80   }
81 };
82
83 #endif