Add a method to reserve space for operands
[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 class SymbolTable : public AbstractTypeUser,
23                     public std::map<const Type *, 
24                                     std::map<const std::string, Value *> > {
25 public:
26   typedef std::map<const std::string, Value *> VarMap;
27   typedef std::map<const Type *, VarMap> super;
28
29   typedef VarMap::iterator type_iterator;
30   typedef VarMap::const_iterator type_const_iterator;
31
32   inline SymbolTable() : InternallyInconsistent(false) {}
33   ~SymbolTable();
34
35   // lookup - Returns null on failure...
36   Value *lookup(const Type *Ty, const std::string &name);
37
38   // insert - Add named definition to the symbol table...
39   inline void insert(Value *N) {
40     assert(N->hasName() && "Value must be named to go into symbol table!");
41     insertEntry(N->getName(), N->getType(), N);
42   }
43
44   void remove(Value *N);
45   Value *type_remove(const type_iterator &It) {
46     return removeEntry(find(It->second->getType()), It);
47   }
48
49   // insert - Insert a constant or type into the symbol table with the specified
50   // name...  There can be a many to one mapping between names and
51   // (constant/type)s.
52   //
53   inline void insert(const std::string &Name, Value *V) {
54     assert((isa<Type>(V) || isa<Constant>(V)) &&
55            "Can only insert types and constants here!");
56     insertEntry(Name, V->getType(), V);
57   }
58
59   /// remove - Remove a constant or type from the symbol table with the
60   /// specified name.
61   Value *remove(const std::string &Name, Value *V) {
62     iterator TI = find(V->getType());
63     return removeEntry(TI, TI->second.find(Name));
64   }
65
66   // getUniqueName - Given a base name, return a string that is either equal to
67   // it (or derived from it) that does not already occur in the symbol table for
68   // the specified type.
69   //
70   std::string getUniqueName(const Type *Ty, const std::string &BaseName);
71
72   inline unsigned type_size(const Type *TypeID) const {
73     return find(TypeID)->second.size();
74   }
75
76   // Note that type_begin / type_end only work if you know that an element of 
77   // TypeID is already in the symbol table!!!
78   //
79   inline type_iterator type_begin(const Type *TypeID) { 
80     return find(TypeID)->second.begin(); 
81   }
82   inline type_const_iterator type_begin(const Type *TypeID) const {
83     return find(TypeID)->second.begin(); 
84   }
85
86   inline type_iterator type_end(const Type *TypeID) { 
87     return find(TypeID)->second.end(); 
88   }
89   inline type_const_iterator type_end(const Type *TypeID) const { 
90     return find(TypeID)->second.end(); 
91   }
92
93   void dump() const;  // Debug method, print out symbol table
94
95 private:
96   // InternallyInconsistent - There are times when the symbol table is
97   // internally inconsistent with the rest of the program.  In this one case, a
98   // value exists with a Name, and it's not in the symbol table.  When we call
99   // V->setName(""), it tries to remove itself from the symbol table and dies.
100   // We know this is happening, and so if the flag InternallyInconsistent is
101   // set, removal from the symbol table is a noop.
102   //
103   bool InternallyInconsistent;
104
105   inline super::value_type operator[](const Type *Ty) {
106     assert(0 && "Should not use this operator to access symbol table!");
107     return super::value_type();
108   }
109
110   // insertEntry - Insert a value into the symbol table with the specified
111   // name...
112   //
113   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
114
115   // removeEntry - Remove a value from the symbol table...
116   //
117   Value *removeEntry(iterator Plane, type_iterator Entry);
118
119   // This function is called when one of the types in the type plane are refined
120   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
121   virtual void typeBecameConcrete(const DerivedType *AbsTy);
122 };
123
124 #endif