1 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
9 //===----------------------------------------------------------------------===//
11 // This file implements the main symbol table for LLVM.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SYMBOL_TABLE_H
16 #define LLVM_SYMBOL_TABLE_H
18 #include "llvm/Value.h"
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.
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
44 class SymbolTable : public AbstractTypeUser {
50 /// @brief A mapping of names to types.
51 typedef std::map<const std::string, const Type*> TypeMap;
53 /// @brief An iterator over the TypeMap.
54 typedef TypeMap::iterator type_iterator;
56 /// @brief A const_iterator over the TypeMap.
57 typedef TypeMap::const_iterator type_const_iterator;
59 /// @brief A mapping of names to values.
60 typedef std::map<const std::string, Value *> ValueMap;
62 /// @brief An iterator over a ValueMap.
63 typedef ValueMap::iterator value_iterator;
65 /// @brief A const_iterator over a ValueMap.
66 typedef ValueMap::const_iterator value_const_iterator;
68 /// @brief A mapping of types to names to values (type planes).
69 typedef std::map<const Type *, ValueMap> PlaneMap;
71 /// @brief An iterator over the type planes.
72 typedef PlaneMap::iterator plane_iterator;
74 /// @brief A const_iterator over the type planes
75 typedef PlaneMap::const_iterator plane_const_iterator;
78 /// @name Constructors
83 : pmap(), tmap(), InternallyInconsistent(false), LastUnique(0) {}
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;
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;
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(); }
110 /// @returns true iff the type map and the type plane are both not
112 /// @brief Determine if the symbol table is empty
113 inline bool isEmpty() const { return pmap.empty() && tmap.empty(); }
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;
121 /// @brief The number of name/type pairs is returned.
122 inline unsigned num_types() const { return tmap.size(); }
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
127 /// @brief Return the name of a value
128 std::string get_name( const Value* Val ) const;
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;
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;
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
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);
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
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<Constant>(Val) &&
169 "Can only insert constants into a symbol table!");
170 insertEntry(Name, Val->getType(), Val);
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
177 /// @brief Insert a type under a new name.
178 inline void insert(const std::string &Name, const Type *Typ) {
179 assert(Typ && "Can't insert null type into symbol table!");
180 insertEntry(Name, Typ );
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
188 /// @brief Remove a named value from the symbol table.
189 void remove(Value* Val);
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(const Type* Typ );
198 /// Remove a constant or type with the specified name from the
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));
208 /// Remove a type at the specified position in the symbol table.
209 /// @returns the removed Type.
210 inline Type* remove(type_iterator TI) {
211 return removeEntry(TI);
214 /// Removes a specific value from the symbol table.
215 /// @returns the removed value.
216 /// @brief Remove a specific value given by an iterator
217 inline Value *value_remove(const value_iterator &It) {
218 return this->removeEntry(pmap.find(It->second->getType()), It);
221 /// This method will strip the symbol table of its names leaving
222 /// the type and values.
223 /// @brief Strip the symbol table.
226 /// @brief Empty the symbol table completely.
227 inline void clear() { pmap.clear(); tmap.clear(); }
234 /// Get an iterator that starts at the beginning of the type planes.
235 /// The iterator will iterate over the Type/ValueMap pairs in the
237 inline plane_iterator plane_begin() { return pmap.begin(); }
239 /// Get a const_iterator that starts at the beginning of the type
240 /// planes. The iterator will iterate over the Type/ValueMap pairs
241 /// in the type planes.
242 inline plane_const_iterator plane_begin() const { return pmap.begin(); }
244 /// Get an iterator at the end of the type planes. This serves as
245 /// the marker for end of iteration over the type planes.
246 inline plane_iterator plane_end() { return pmap.end(); }
248 /// Get a const_iterator at the end of the type planes. This serves as
249 /// the marker for end of iteration over the type planes.
250 inline plane_const_iterator plane_end() const { return pmap.end(); }
252 /// Get an iterator that starts at the beginning of a type plane.
253 /// The iterator will iterate over the name/value pairs in the type plane.
254 /// @note The type plane must already exist before using this.
255 inline value_iterator value_begin(const Type *Typ) {
256 assert(Typ && "Can't get value iterator with null type!");
257 return pmap.find(Typ)->second.begin();
260 /// Get a const_iterator that starts at the beginning of a type plane.
261 /// The iterator will iterate over the name/value pairs in the type plane.
262 /// @note The type plane must already exist before using this.
263 inline value_const_iterator value_begin(const Type *Typ) const {
264 assert(Typ && "Can't get value iterator with null type!");
265 return pmap.find(Typ)->second.begin();
268 /// Get an iterator to the end of a type plane. This serves as the marker
269 /// for end of iteration of the type plane.
270 /// @note The type plane must already exist before using this.
271 inline value_iterator value_end(const Type *Typ) {
272 assert(Typ && "Can't get value iterator with null type!");
273 return pmap.find(Typ)->second.end();
276 /// Get a const_iterator to the end of a type plane. This serves as the
277 /// marker for end of iteration of the type plane.
278 /// @note The type plane must already exist before using this.
279 inline value_const_iterator value_end(const Type *Typ) const {
280 assert(Typ && "Can't get value iterator with null type!");
281 return pmap.find(Typ)->second.end();
284 /// Get an iterator to the start of the name/Type map.
285 inline type_iterator type_begin() { return tmap.begin(); }
287 /// @brief Get a const_iterator to the start of the name/Type map.
288 inline type_const_iterator type_begin() const { return tmap.begin(); }
290 /// Get an iterator to the end of the name/Type map. This serves as the
291 /// marker for end of iteration of the types.
292 inline type_iterator type_end() { return tmap.end(); }
294 /// Get a const-iterator to the end of the name/Type map. This serves
295 /// as the marker for end of iteration of the types.
296 inline type_const_iterator type_end() const { return tmap.end(); }
298 /// This method returns a plane_const_iterator for iteration over
299 /// the type planes starting at a specific plane, given by \p Ty.
300 /// @brief Find a type plane.
301 inline plane_const_iterator find(const Type* Typ ) const {
302 assert(Typ && "Can't find type plane with null type!");
303 return pmap.find( Typ );
306 /// This method returns a plane_iterator for iteration over the
307 /// type planes starting at a specific plane, given by \p Ty.
308 /// @brief Find a type plane.
309 inline plane_iterator find( const Type* Typ ) {
310 assert(Typ && "Can't find type plane with null type!");
311 return pmap.find(Typ);
314 /// This method returns a ValueMap* for a specific type plane. This
315 /// interface is deprecated and may go away in the future.
317 /// @brief Find a type plane
318 inline const ValueMap* findPlane( const Type* Typ ) const {
319 assert(Typ && "Can't find type plane with null type!");
320 plane_const_iterator I = pmap.find( Typ );
321 if ( I == pmap.end() ) return 0;
326 /// @name Internal Methods
329 /// @brief Insert a value into the symbol table with the specified name.
330 void insertEntry(const std::string &Name, const Type *Ty, Value *V);
332 /// @brief Insert a type into the symbol table with the specified name.
333 void insertEntry(const std::string &Name, const Type *T);
335 /// Remove a specific value from a specific plane in the SymbolTable.
336 /// @returns the removed Value.
337 Value* removeEntry(plane_iterator Plane, value_iterator Entry);
339 /// Remove a specific type from the SymbolTable.
340 /// @returns the removed Type.
341 Type* removeEntry(type_iterator Entry);
343 /// This function is called when one of the types in the type plane
345 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
347 /// This function markes a type as being concrete (defined).
348 virtual void typeBecameConcrete(const DerivedType *AbsTy);
351 /// @name Internal Data
355 /// This is the main content of the symbol table. It provides
356 /// separate type planes for named values. That is, each named
357 /// value is organized into a separate dictionary based on
358 /// Type. This means that the same name can be used for different
359 /// types without conflict.
360 /// @brief The mapping of types to names to values.
363 /// This is the type plane. It is separated from the pmap
364 /// because the elements of the map are name/Type pairs not
365 /// name/Value pairs and Type is not a Value.
368 /// There are times when the symbol table is internally inconsistent with
369 /// the rest of the program. In this one case, a value exists with a Name,
370 /// and it's not in the symbol table. When we call V->setName(""), it
371 /// tries to remove itself from the symbol table and dies. We know this
372 /// is happening, and so if the flag InternallyInconsistent is set,
373 /// removal from the symbol table is a noop.
374 /// @brief Indicator of symbol table internal inconsistency.
375 bool InternallyInconsistent;
377 /// This value is used to retain the last unique value used
378 /// by getUniqueName to generate unique names.
379 mutable unsigned long LastUnique;
385 } // End llvm namespace