Delete the really inefficient method: void remove(const Type* Typ);
[oota-llvm.git] / include / llvm / SymbolTable.h
1 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and re-written by Reid
6 // Spencer. It is distributed under the University of Illinois Open Source 
7 // License. See LICENSE.TXT for details.
8 // 
9 //===----------------------------------------------------------------------===//
10 //
11 // This file implements the main symbol table for LLVM.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SYMBOL_TABLE_H
16 #define LLVM_SYMBOL_TABLE_H
17
18 #include "llvm/Value.h"
19 #include <map>
20
21 namespace llvm {
22
23 /// This class provides a symbol table of name/value pairs that is broken
24 /// up by type. For each Type* there is a "plane" of name/value pairs in 
25 /// the symbol table.  Identical types may have overlapping symbol names as 
26 /// long as they are distinct. The SymbolTable also tracks,  separately, a 
27 /// map of name/type pairs. This allows types to be named. Types are treated 
28 /// distinctly from Values.
29 /// 
30 /// The SymbolTable provides several utility functions for answering common
31 /// questions about its contents as well as an iterator interface for
32 /// directly iterating over the contents. To reduce confusion, the terms 
33 /// "type", "value", and "plane" are used consistently. For example,
34 /// There is a TypeMap typedef that is the mapping of names to Types. 
35 /// Similarly there is a ValueMap typedef that is the mapping of 
36 /// names to Values. Finally, there is a PlaneMap typedef that is the
37 /// mapping of types to planes of ValueMap. This is the basic structure
38 /// of the symbol table. When you call type_begin() you're asking
39 /// for an iterator at the start of the TypeMap. When you call
40 /// plane_begin(), you're asking for an iterator at the start of 
41 /// the PlaneMap. Finally, when you call value_begin(), you're asking
42 /// for an iterator at the start of a ValueMap for a specific type
43 /// plane.
44 class SymbolTable : public AbstractTypeUser {
45
46 /// @name Types
47 /// @{
48 public:
49
50   /// @brief A mapping of names to types.
51   typedef std::map<const std::string, const Type*> TypeMap;
52
53   /// @brief An iterator over the TypeMap.
54   typedef TypeMap::iterator type_iterator;
55
56   /// @brief A const_iterator over the TypeMap.
57   typedef TypeMap::const_iterator type_const_iterator;
58
59   /// @brief A mapping of names to values.
60   typedef std::map<const std::string, Value *> ValueMap;
61
62   /// @brief An iterator over a ValueMap.
63   typedef ValueMap::iterator value_iterator;
64
65   /// @brief A const_iterator over a ValueMap.
66   typedef ValueMap::const_iterator value_const_iterator;
67
68   /// @brief A mapping of types to names to values (type planes).
69   typedef std::map<const Type *, ValueMap> PlaneMap;
70
71   /// @brief An iterator over the type planes.
72   typedef PlaneMap::iterator plane_iterator;
73
74   /// @brief A const_iterator over the type planes
75   typedef PlaneMap::const_iterator plane_const_iterator;
76
77 /// @}
78 /// @name Constructors
79 /// @{
80 public:
81
82   SymbolTable() : LastUnique(0) {}
83   ~SymbolTable();
84
85 /// @}
86 /// @name Accessors
87 /// @{
88 public:
89
90   /// This method finds the value with the given \p name in the
91   /// type plane \p Ty and returns it. This method will not find any
92   /// Types, only Values. Use lookupType to find Types by name.
93   /// @returns null on failure, otherwise the Value associated with
94   /// the \p name in type plane \p Ty.
95   /// @brief Lookup a named, typed value.
96   Value *lookup(const Type *Ty, const std::string &name) const;
97
98   /// This method finds the type with the given \p name in the
99   /// type  map and returns it.
100   /// @returns null if the name is not found, otherwise the Type
101   /// associated with the \p name.
102   /// @brief Lookup a type by name.
103   Type* lookupType(const std::string& name) const;
104
105   /// @returns true iff the type map and the type plane are both not 
106   /// empty.
107   /// @brief Determine if the symbol table is empty
108   inline bool isEmpty() const { return pmap.empty() && tmap.empty(); }
109
110   /// @brief The number of name/type pairs is returned.
111   inline unsigned num_types() const { return (unsigned)tmap.size(); }
112
113   /// Given a base name, return a string that is either equal to it or 
114   /// derived from it that does not already occur in the symbol table 
115   /// for the specified type.
116   /// @brief Get a name unique to this symbol table
117   std::string getUniqueName(const Type *Ty, 
118     const std::string &BaseName) const;
119
120   /// This function can be used from the debugger to display the
121   /// content of the symbol table while debugging.
122   /// @brief Print out symbol table on stderr
123   void dump() const;  
124
125 /// @}
126 /// @name Mutators
127 /// @{
128 public:
129
130   /// This method adds the provided value \p N to the symbol table. 
131   /// The Value must have both a name and a type which are extracted 
132   /// and used to place the value in the correct type plane under 
133   /// the value's name.
134   /// @brief Add a named value to the symbol table
135   inline void insert(Value *Val) {
136     assert(Val && "Can't insert null type into symbol table!");
137     assert(Val->hasName() && "Value must be named to go into symbol table!");
138     insertEntry(Val->getName(), Val->getType(), Val);
139   }
140
141   /// Inserts a type into the symbol table with the specified name. There
142   /// can be a many-to-one mapping between names and types. This method
143   /// allows a type with an existing entry in the symbol table to get
144   /// a new name.
145   /// @brief Insert a type under a new name.
146   inline void insert(const std::string &Name, const Type *Typ) {
147     assert(Typ && "Can't insert null type into symbol table!");
148     insertEntry(Name, Typ);
149   }
150
151   /// This method removes a named value from the symbol table. The
152   /// type and name of the Value are extracted from \p N and used to
153   /// lookup the Value in the correct type plane. If the Value is
154   /// not in the symbol table, this method silently ignores the
155   /// request.
156   /// @brief Remove a named value from the symbol table.
157   void remove(Value* Val);
158
159   /// Remove a type at the specified position in the symbol table.
160   /// @returns the removed Type.
161   Type* remove(type_iterator TI);
162
163   /// changeName - Given a value with a non-empty name, remove its existing
164   /// entry from the symbol table and insert a new one for Name.  This is
165   /// equivalent to doing "remove(V), V->Name = Name, insert(V)", but is faster,
166   /// and will not temporarily remove the symbol table plane if V is the last
167   /// value in the symtab with that name (which could invalidate iterators to
168   /// that plane).
169   void changeName(Value *V, const std::string &Name);
170
171   /// This method will strip the symbol table of its names leaving
172   /// the type and values. 
173   /// @brief Strip the symbol table. 
174   bool strip();
175
176 /// @}
177 /// @name Iteration
178 /// @{
179 public:
180
181   /// Get an iterator that starts at the beginning of the type planes.
182   /// The iterator will iterate over the Type/ValueMap pairs in the
183   /// type planes. 
184   inline plane_iterator plane_begin() { return pmap.begin(); }
185
186   /// Get a const_iterator that starts at the beginning of the type 
187   /// planes.  The iterator will iterate over the Type/ValueMap pairs 
188   /// in the type planes. 
189   inline plane_const_iterator plane_begin() const { return pmap.begin(); }
190
191   /// Get an iterator at the end of the type planes. This serves as
192   /// the marker for end of iteration over the type planes.
193   inline plane_iterator plane_end() { return pmap.end(); }
194
195   /// Get a const_iterator at the end of the type planes. This serves as
196   /// the marker for end of iteration over the type planes.
197   inline plane_const_iterator plane_end() const { return pmap.end(); }
198
199   /// Get an iterator that starts at the beginning of a type plane.
200   /// The iterator will iterate over the name/value pairs in the type plane.
201   /// @note The type plane must already exist before using this.
202   inline value_iterator value_begin(const Type *Typ) { 
203     assert(Typ && "Can't get value iterator with null type!");
204     return pmap.find(Typ)->second.begin(); 
205   }
206
207   /// Get a const_iterator that starts at the beginning of a type plane.
208   /// The iterator will iterate over the name/value pairs in the type plane.
209   /// @note The type plane must already exist before using this.
210   inline value_const_iterator value_begin(const Type *Typ) const {
211     assert(Typ && "Can't get value iterator with null type!");
212     return pmap.find(Typ)->second.begin(); 
213   }
214
215   /// Get an iterator to the end of a type plane. This serves as the marker
216   /// for end of iteration of the type plane.
217   /// @note The type plane must already exist before using this.
218   inline value_iterator value_end(const Type *Typ) { 
219     assert(Typ && "Can't get value iterator with null type!");
220     return pmap.find(Typ)->second.end(); 
221   }
222
223   /// Get a const_iterator to the end of a type plane. This serves as the
224   /// marker for end of iteration of the type plane.
225   /// @note The type plane must already exist before using this.
226   inline value_const_iterator value_end(const Type *Typ) const { 
227     assert(Typ && "Can't get value iterator with null type!");
228     return pmap.find(Typ)->second.end(); 
229   }
230
231   /// Get an iterator to the start of the name/Type map.
232   inline type_iterator type_begin() { return tmap.begin(); }
233
234   /// @brief Get a const_iterator to the start of the name/Type map.
235   inline type_const_iterator type_begin() const { return tmap.begin(); }
236
237   /// Get an iterator to the end of the name/Type map. This serves as the
238   /// marker for end of iteration of the types.
239   inline type_iterator type_end() { return tmap.end(); }
240
241   /// Get a const-iterator to the end of the name/Type map. This serves 
242   /// as the marker for end of iteration of the types.
243   inline type_const_iterator type_end() const { return tmap.end(); }
244
245   /// This method returns a plane_const_iterator for iteration over
246   /// the type planes starting at a specific plane, given by \p Ty.
247   /// @brief Find a type plane.
248   inline plane_const_iterator find(const Type* Typ) const {
249     assert(Typ && "Can't find type plane with null type!");
250     return pmap.find(Typ);
251   }
252
253   /// This method returns a plane_iterator for iteration over the
254   /// type planes starting at a specific plane, given by \p Ty.
255   /// @brief Find a type plane.
256   inline plane_iterator find(const Type* Typ) { 
257     assert(Typ && "Can't find type plane with null type!");
258     return pmap.find(Typ); 
259   }
260
261   /// This method returns a ValueMap* for a specific type plane. This
262   /// interface is deprecated and may go away in the future.
263   /// @deprecated
264   /// @brief Find a type plane
265   inline const ValueMap* findPlane(const Type* Typ) const {
266     assert(Typ && "Can't find type plane with null type!");
267     plane_const_iterator I = pmap.find(Typ);
268     if (I == pmap.end()) return 0;
269     return &I->second;
270   }
271
272 /// @}
273 /// @name Internal Methods
274 /// @{
275 private:
276   /// @brief Insert a value into the symbol table with the specified name.
277   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
278
279   /// @brief Insert a type into the symbol table with the specified name.
280   void insertEntry(const std::string &Name, const Type *T);
281
282   /// Remove a specific value from a specific plane in the SymbolTable.
283   /// @returns the removed Value.
284   Value* removeEntry(plane_iterator Plane, value_iterator Entry);
285
286   /// This function is called when one of the types in the type plane 
287   /// is refined.
288   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
289
290   /// This function markes a type as being concrete (defined).
291   virtual void typeBecameConcrete(const DerivedType *AbsTy);
292
293 /// @}
294 /// @name Internal Data 
295 /// @{
296 private:
297
298   /// This is the main content of the symbol table. It provides
299   /// separate type planes for named values. That is, each named
300   /// value is organized into a separate dictionary based on 
301   /// Type. This means that the same name can be used for different
302   /// types without conflict. 
303   /// @brief The mapping of types to names to values.
304   PlaneMap pmap;
305
306   /// This is the type plane. It is separated from the pmap
307   /// because the elements of the map are name/Type pairs not 
308   /// name/Value pairs and Type is not a Value.
309   TypeMap tmap;
310
311   /// This value is used to retain the last unique value used
312   /// by getUniqueName to generate unique names.
313   mutable unsigned long LastUnique;
314
315 /// @}
316
317 };
318
319 } // End llvm namespace
320
321 // vim: sw=2
322
323 #endif
324