Add a method
[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   inline SymbolTable() 
83     : pmap(), tmap(), InternallyInconsistent(false), LastUnique(0) {}
84   ~SymbolTable();
85
86 /// @}
87 /// @name Accessors
88 /// @{
89 public:
90
91   /// This method finds the value with the given \p name in the
92   /// type plane \p Ty and returns it. This method will not find any
93   /// Types, only Values. Use lookupType to find Types by name.
94   /// @returns null on failure, otherwise the Value associated with
95   /// the \p name in type plane \p Ty.
96   /// @brief Lookup a named, typed value.
97   Value *lookup(const Type *Ty, const std::string &name) const;
98
99   /// This method finds the type with the given \p name in the
100   /// type  map and returns it.
101   /// @returns null if the name is not found, otherwise the Type
102   /// associated with the \p name.
103   /// @brief Lookup a type by name.
104   Type* lookupType( const std::string& name ) const;
105
106   /// @returns true iff the type map is not empty.
107   /// @brief Determine if there are types in the symbol table
108   inline bool hasTypes() const { return ! tmap.empty(); }
109
110   /// @returns true iff the type map and the type plane are both not 
111   /// empty.
112   /// @brief Determine if the symbol table is empty
113   inline bool isEmpty() const { return pmap.empty() && tmap.empty(); }
114
115   /// The plane associated with the \p TypeID parameter is found
116   /// and the number of entries in the plane is returned.
117   /// @returns Number of entries in the specified type plane or 0.
118   /// @brief Get the size of a type plane.
119   unsigned type_size(const Type *TypeID) const;
120
121   /// @brief The number of name/type pairs is returned.
122   inline unsigned num_types() const { return (unsigned)tmap.size(); }
123
124   /// Finds the value \p val in the symbol table and returns its
125   /// name. Only the type plane associated with the type of \p val
126   /// is searched.
127   /// @brief Return the name of a value
128   std::string get_name( const Value* Val ) const;
129
130   /// Finds the type \p Ty in the symbol table and returns its name.
131   /// @brief Return the name of a type
132   std::string get_name( const Type* Ty ) const;
133
134   /// Given a base name, return a string that is either equal to it or 
135   /// derived from it that does not already occur in the symbol table 
136   /// for the specified type.
137   /// @brief Get a name unique to this symbol table
138   std::string getUniqueName(const Type *Ty, 
139     const std::string &BaseName) const;
140
141   /// This function can be used from the debugger to display the
142   /// content of the symbol table while debugging.
143   /// @brief Print out symbol table on stderr
144   void dump() const;  
145
146 /// @}
147 /// @name Mutators
148 /// @{
149 public:
150
151   /// This method adds the provided value \p N to the symbol table. 
152   /// The Value must have both a name and a type which are extracted 
153   /// and used to place the value in the correct type plane under 
154   /// the value's name.
155   /// @brief Add a named value to the symbol table
156   inline void insert(Value *Val) {
157     assert(Val && "Can't insert null type into symbol table!");
158     assert(Val->hasName() && "Value must be named to go into symbol table!");
159     insertEntry(Val->getName(), Val->getType(), Val);
160   }
161
162   /// Inserts a constant into the symbol table with the specified
163   /// name. There can be a many to one mapping between names and constants.
164   /// @brief Insert a constant or type.
165   inline void insert(const std::string &Name, Value *Val) {
166     assert(Val && "Can't insert null type into symbol table!");
167     assert(isa<Constant>(Val) &&
168            "Can only insert constants into a symbol table!");
169     insertEntry(Name, Val->getType(), Val);
170   }
171
172   /// Inserts a type into the symbol table with the specified name. There
173   /// can be a many-to-one mapping between names and types. This method
174   /// allows a type with an existing entry in the symbol table to get
175   /// a new name.
176   /// @brief Insert a type under a new name.
177   inline void insert(const std::string &Name, const Type *Typ) {
178     assert(Typ && "Can't insert null type into symbol table!");
179     insertEntry(Name, Typ );
180   }
181
182   /// This method removes a named value from the symbol table. The
183   /// type and name of the Value are extracted from \p N and used to
184   /// lookup the Value in the correct type plane. If the Value is
185   /// not in the symbol table, this method silently ignores the
186   /// request.
187   /// @brief Remove a named value from the symbol table.
188   void remove(Value* Val);
189
190   /// This method removes a named type from the symbol table. The
191   /// name of the type is extracted from \p T and used to look up
192   /// the Type in the type map. If the Type is not in the symbol
193   /// table, this method silently ignores the request.
194   /// @brief Remove a named type from the symbol table.
195   void remove(const Type* Typ );
196
197   /// Remove a constant or type with the specified name from the 
198   /// symbol table.
199   /// @returns the removed Value.
200   /// @brief Remove a constant or type from the symbol table.
201   inline Value* remove(const std::string &Name, Value *Val) {
202     assert(Val && "Can't remove null value from symbol table!");
203     plane_iterator PI = pmap.find(Val->getType());
204     return removeEntry(PI, PI->second.find(Name));
205   }
206
207   /// Remove a type at the specified position in the symbol table.
208   /// @returns the removed Type.
209   inline Type* remove(type_iterator TI) {
210     return removeEntry(TI);
211   }
212
213   /// Removes a specific value from the symbol table. 
214   /// @returns the removed value.
215   /// @brief Remove a specific value given by an iterator
216   inline Value *value_remove(const value_iterator &It) {
217     return this->removeEntry(pmap.find(It->second->getType()), It);
218   }
219
220   /// This method will strip the symbol table of its names leaving
221   /// the type and values. 
222   /// @brief Strip the symbol table. 
223   bool strip();
224
225   /// @brief Empty the symbol table completely.
226   inline void clear() { pmap.clear(); tmap.clear(); }
227
228 /// @}
229 /// @name Iteration
230 /// @{
231 public:
232
233   /// Get an iterator that starts at the beginning of the type planes.
234   /// The iterator will iterate over the Type/ValueMap pairs in the
235   /// type planes. 
236   inline plane_iterator plane_begin() { return pmap.begin(); }
237
238   /// Get a const_iterator that starts at the beginning of the type 
239   /// planes.  The iterator will iterate over the Type/ValueMap pairs 
240   /// in the type planes. 
241   inline plane_const_iterator plane_begin() const { return pmap.begin(); }
242
243   /// Get an iterator at the end of the type planes. This serves as
244   /// the marker for end of iteration over the type planes.
245   inline plane_iterator plane_end() { return pmap.end(); }
246
247   /// Get a const_iterator at the end of the type planes. This serves as
248   /// the marker for end of iteration over the type planes.
249   inline plane_const_iterator plane_end() const { return pmap.end(); }
250
251   /// Get an iterator that starts at the beginning of a type plane.
252   /// The iterator will iterate over the name/value pairs in the type plane.
253   /// @note The type plane must already exist before using this.
254   inline value_iterator value_begin(const Type *Typ) { 
255     assert(Typ && "Can't get value iterator with null type!");
256     return pmap.find(Typ)->second.begin(); 
257   }
258
259   /// Get a const_iterator that starts at the beginning of a type plane.
260   /// The iterator will iterate over the name/value pairs in the type plane.
261   /// @note The type plane must already exist before using this.
262   inline value_const_iterator value_begin(const Type *Typ) const {
263     assert(Typ && "Can't get value iterator with null type!");
264     return pmap.find(Typ)->second.begin(); 
265   }
266
267   /// Get an iterator to the end of a type plane. This serves as the marker
268   /// for end of iteration of the type plane.
269   /// @note The type plane must already exist before using this.
270   inline value_iterator value_end(const Type *Typ) { 
271     assert(Typ && "Can't get value iterator with null type!");
272     return pmap.find(Typ)->second.end(); 
273   }
274
275   /// Get a const_iterator to the end of a type plane. This serves as the
276   /// marker for end of iteration of the type plane.
277   /// @note The type plane must already exist before using this.
278   inline value_const_iterator value_end(const Type *Typ) const { 
279     assert(Typ && "Can't get value iterator with null type!");
280     return pmap.find(Typ)->second.end(); 
281   }
282
283   /// Get an iterator to the start of the name/Type map.
284   inline type_iterator type_begin() { return tmap.begin(); }
285
286   /// @brief Get a const_iterator to the start of the name/Type map.
287   inline type_const_iterator type_begin() const { return tmap.begin(); }
288
289   /// Get an iterator to the end of the name/Type map. This serves as the
290   /// marker for end of iteration of the types.
291   inline type_iterator type_end() { return tmap.end(); }
292
293   /// Get a const-iterator to the end of the name/Type map. This serves 
294   /// as the marker for end of iteration of the types.
295   inline type_const_iterator type_end() const { return tmap.end(); }
296
297   /// This method returns a plane_const_iterator for iteration over
298   /// the type planes starting at a specific plane, given by \p Ty.
299   /// @brief Find a type plane.
300   inline plane_const_iterator find(const Type* Typ ) const {
301     assert(Typ && "Can't find type plane with null type!");
302     return pmap.find( Typ );
303   }
304
305   /// This method returns a plane_iterator for iteration over the
306   /// type planes starting at a specific plane, given by \p Ty.
307   /// @brief Find a type plane.
308   inline plane_iterator find( const Type* Typ ) { 
309     assert(Typ && "Can't find type plane with null type!");
310     return pmap.find(Typ); 
311   }
312
313   /// This method returns a ValueMap* for a specific type plane. This
314   /// interface is deprecated and may go away in the future.
315   /// @deprecated
316   /// @brief Find a type plane
317   inline const ValueMap* findPlane( const Type* Typ ) const {
318     assert(Typ && "Can't find type plane with null type!");
319     plane_const_iterator I = pmap.find( Typ );
320     if ( I == pmap.end() ) return 0;
321     return &I->second;
322   }
323
324 /// @}
325 /// @name Internal Methods
326 /// @{
327 private:
328   /// @brief Insert a value into the symbol table with the specified name.
329   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
330
331   /// @brief Insert a type into the symbol table with the specified name.
332   void insertEntry(const std::string &Name, const Type *T);
333
334   /// Remove a specific value from a specific plane in the SymbolTable.
335   /// @returns the removed Value.
336   Value* removeEntry(plane_iterator Plane, value_iterator Entry);
337
338   /// Remove a specific type from the SymbolTable.
339   /// @returns the removed Type.
340   Type*  removeEntry(type_iterator Entry);
341
342   /// This function is called when one of the types in the type plane 
343   /// is refined.
344   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
345
346   /// This function markes a type as being concrete (defined).
347   virtual void typeBecameConcrete(const DerivedType *AbsTy);
348
349 /// @}
350 /// @name Internal Data 
351 /// @{
352 private:
353
354   /// This is the main content of the symbol table. It provides
355   /// separate type planes for named values. That is, each named
356   /// value is organized into a separate dictionary based on 
357   /// Type. This means that the same name can be used for different
358   /// types without conflict. 
359   /// @brief The mapping of types to names to values.
360   PlaneMap pmap;
361
362   /// This is the type plane. It is separated from the pmap
363   /// because the elements of the map are name/Type pairs not 
364   /// name/Value pairs and Type is not a Value.
365   TypeMap tmap;
366
367   /// There are times when the symbol table is internally inconsistent with 
368   /// the rest of the program.  In this one case, a value exists with a Name, 
369   /// and it's not in the symbol table.  When we call V->setName(""), it 
370   /// tries to remove itself from the symbol table and dies.  We know this 
371   /// is happening, and so if the flag InternallyInconsistent is set, 
372   /// removal from the symbol table is a noop.
373   /// @brief Indicator of symbol table internal inconsistency.
374   bool InternallyInconsistent;
375
376   /// This value is used to retain the last unique value used
377   /// by getUniqueName to generate unique names.
378   mutable unsigned long LastUnique;
379
380 /// @}
381
382 };
383
384 } // End llvm namespace
385
386 // vim: sw=2
387
388 #endif
389