Fix spelling of doxygen directive.
[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, 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 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 or type into the symbol table with the specified
163   /// name. There can be a many to one mapping between names and constants
164   /// or types.
165   /// @brief Insert a constant or type.
166   inline void insert(const std::string &Name, Value *Val) {
167     assert(Val && "Can't insert null type into symbol table!");
168     assert((isa<Type>(Val) || isa<Constant>(Val)) &&
169            "Can only insert types and constants into a symbol table!");
170     insertEntry(Name, Val->getType(), Val);
171   }
172
173   /// Inserts a type into the symbol table with the specified name. There
174   /// can be a many-to-one mapping between names and types. This method
175   /// allows a type with an existing entry in the symbol table to get
176   /// a new name.
177   /// @brief Insert a type under a new name.
178   inline void insert(const std::string &Name, Type *Typ) {
179     assert(Typ && "Can't insert null type into symbol table!");
180     insertEntry(Name, Typ );
181   }
182
183   /// This method removes a named value from the symbol table. The
184   /// type and name of the Value are extracted from \p N and used to
185   /// lookup the Value in the correct type plane. If the Value is
186   /// not in the symbol table, this method silently ignores the
187   /// request.
188   /// @brief Remove a named value from the symbol table.
189   void remove(Value* Val);
190
191   /// This method removes a named type from the symbol table. The
192   /// name of the type is extracted from \P T and used to look up
193   /// the Type in the type map. If the Type is not in the symbol
194   /// table, this method silently ignores the request.
195   /// @brief Remove a named type from the symbol table.
196   void remove(Type* Typ );
197
198   /// Remove a constant or type with the specified name from the 
199   /// symbol table.
200   /// @returns the removed Value.
201   /// @brief Remove a constant or type from the symbol table.
202   inline Value* remove(const std::string &Name, Value *Val) {
203     assert(Val && "Can't remove null value from symbol table!");
204     plane_iterator PI = pmap.find(Val->getType());
205     return removeEntry(PI, PI->second.find(Name));
206   }
207
208   /// Remove a type with the specified name from the symbol table.
209   /// @returns the removed Type.
210   /// @brief Remove a named tyep from the symbol table.
211   inline Type* remove(const std::string& Name, Type* T ) {
212     return removeEntry( tmap.find(Name) );
213   }
214
215   /// Removes a specific value from the symbol table. 
216   /// @returns the removed value.
217   /// @brief Remove a specific value given by an iterator
218   inline Value *value_remove(const value_iterator &It) {
219     return this->removeEntry(pmap.find(It->second->getType()), It);
220   }
221
222   /// This method will strip the symbol table of its names leaving
223   /// the type and values. 
224   /// @brief Strip the symbol table. 
225   bool strip();
226
227   /// @brief Empty the symbol table completely.
228   inline void clear() { pmap.clear(); tmap.clear(); }
229
230 /// @}
231 /// @name Iteration
232 /// @{
233 public:
234
235   /// Get an iterator that starts at the beginning of the type planes.
236   /// The iterator will iterate over the Type/ValueMap pairs in the
237   /// type planes. 
238   inline plane_iterator plane_begin() { return pmap.begin(); }
239
240   /// Get a const_iterator that starts at the beginning of the type 
241   /// planes.  The iterator will iterate over the Type/ValueMap pairs 
242   /// in the type planes. 
243   inline plane_const_iterator plane_begin() const { return pmap.begin(); }
244
245   /// Get an iterator at the end of the type planes. This serves as
246   /// the marker for end of iteration over the type planes.
247   inline plane_iterator plane_end() { return pmap.end(); }
248
249   /// Get a const_iterator at the end of the type planes. This serves as
250   /// the marker for end of iteration over the type planes.
251   inline plane_const_iterator plane_end() const { return pmap.end(); }
252
253   /// Get an iterator that starts at the beginning of a type plane.
254   /// The iterator will iterate over the name/value pairs in the type plane.
255   /// @note The type plane must already exist before using this.
256   inline value_iterator value_begin(const Type *Typ) { 
257     assert(Typ && "Can't get value iterator with null type!");
258     return pmap.find(Typ)->second.begin(); 
259   }
260
261   /// Get a const_iterator that starts at the beginning of a type plane.
262   /// The iterator will iterate over the name/value pairs in the type plane.
263   /// @note The type plane must already exist before using this.
264   inline value_const_iterator value_begin(const Type *Typ) const {
265     assert(Typ && "Can't get value iterator with null type!");
266     return pmap.find(Typ)->second.begin(); 
267   }
268
269   /// Get an iterator to the end of a type plane. This serves as the marker
270   /// for end of iteration of the type plane.
271   /// @note The type plane must already exist before using this.
272   inline value_iterator value_end(const Type *Typ) { 
273     assert(Typ && "Can't get value iterator with null type!");
274     return pmap.find(Typ)->second.end(); 
275   }
276
277   /// Get a const_iterator to the end of a type plane. This serves as the
278   /// marker for end of iteration of the type plane.
279   /// @note The type plane must already exist before using this.
280   inline value_const_iterator value_end(const Type *Typ) const { 
281     assert(Typ && "Can't get value iterator with null type!");
282     return pmap.find(Typ)->second.end(); 
283   }
284
285   /// Get an iterator to the start of the name/Type map.
286   inline type_iterator type_begin() { return tmap.begin(); }
287
288   /// @brief Get a const_iterator to the start of the name/Type map.
289   inline type_const_iterator type_begin() const { return tmap.begin(); }
290
291   /// Get an iterator to the end of the name/Type map. This serves as the
292   /// marker for end of iteration of the types.
293   inline type_iterator type_end() { return tmap.end(); }
294
295   /// Get a const-iterator to the end of the name/Type map. This serves 
296   /// as the marker for end of iteration of the types.
297   inline type_const_iterator type_end() const { return tmap.end(); }
298
299   /// This method returns a plane_const_iterator for iteration over
300   /// the type planes starting at a specific plane, given by \p Ty.
301   /// @brief Find a type plane.
302   inline plane_const_iterator find(const Type* Typ ) const {
303     assert(Typ && "Can't find type plane with null type!");
304     return pmap.find( Typ );
305   }
306
307   /// This method returns a plane_iterator for iteration over the
308   /// type planes starting at a specific plane, given by \p Ty.
309   /// @brief Find a type plane.
310   inline plane_iterator find( const Type* Typ ) { 
311     assert(Typ && "Can't find type plane with null type!");
312     return pmap.find(Typ); 
313   }
314
315   /// This method returns a ValueMap* for a specific type plane. This
316   /// interface is deprecated and may go away in the future.
317   /// @deprecated
318   /// @brief Find a type plane
319   inline const ValueMap* findPlane( const Type* Typ ) const {
320     assert(Typ && "Can't find type plane with null type!");
321     plane_const_iterator I = pmap.find( Typ );
322     if ( I == pmap.end() ) return 0;
323     return &I->second;
324   }
325
326 /// @}
327 /// @name Internal Methods
328 /// @{
329 private:
330   /// @brief Insert a value into the symbol table with the specified name.
331   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
332
333   /// @brief Insert a type into the symbol table with the specified name.
334   void insertEntry(const std::string &Name, Type *T);
335
336   /// Remove a specific value from a specific plane in the SymbolTable.
337   /// @returns the removed Value.
338   Value* removeEntry(plane_iterator Plane, value_iterator Entry);
339
340   /// Remove a specific type from the SymbolTable.
341   /// @returns the removed Type.
342   Type*  removeEntry(type_iterator Entry);
343
344   /// This function is called when one of the types in the type plane 
345   /// is refined.
346   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
347
348   /// This function markes a type as being concrete (defined).
349   virtual void typeBecameConcrete(const DerivedType *AbsTy);
350
351 /// @}
352 /// @name Internal Data 
353 /// @{
354 private:
355
356   /// This is the main content of the symbol table. It provides
357   /// separate type planes for named values. That is, each named
358   /// value is organized into a separate dictionary based on 
359   /// Type. This means that the same name can be used for different
360   /// types without conflict. Note that the Type::TypeTy plane is
361   /// not stored in this map but is in tmap.
362   /// @brief The mapping of types to names to values.
363   PlaneMap pmap;
364
365   /// This is the Type::TypeTy plane. It is separated from the pmap
366   /// because the elements of the map are name/Type pairs not 
367   /// name/Value pairs and Type is not a Value.
368   TypeMap tmap;
369
370   /// There are times when the symbol table is internally inconsistent with 
371   /// the rest of the program.  In this one case, a value exists with a Name, 
372   /// and it's not in the symbol table.  When we call V->setName(""), it 
373   /// tries to remove itself from the symbol table and dies.  We know this 
374   /// is happening, and so if the flag InternallyInconsistent is set, 
375   /// removal from the symbol table is a noop.
376   /// @brief Indicator of symbol table internal inconsistency.
377   bool InternallyInconsistent;
378
379   /// This value is used to retain the last unique value used
380   /// by getUniqueName to generate unique names.
381   mutable unsigned long LastUnique;
382
383 /// @}
384
385 };
386
387 } // End llvm namespace
388
389 // vim: sw=2
390
391 #endif
392