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
82 SymbolTable() : LastUnique(0) {}
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;
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;
105 /// @returns true iff the type map and the type plane are both not
107 /// @brief Determine if the symbol table is empty
108 inline bool isEmpty() const { return pmap.empty() && tmap.empty(); }
110 /// @brief The number of name/type pairs is returned.
111 inline unsigned num_types() const { return (unsigned)tmap.size(); }
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;
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
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);
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
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);
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
156 /// @brief Remove a named value from the symbol table.
157 void remove(Value* Val);
159 /// This method removes a named type from the symbol table. The
160 /// name of the type is extracted from \p T and used to look up
161 /// the Type in the type map. If the Type is not in the symbol
162 /// table, this method silently ignores the request.
163 /// @brief Remove a named type from the symbol table.
164 void remove(const Type* Typ);
166 /// Remove a type at the specified position in the symbol table.
167 /// @returns the removed Type.
168 inline Type* remove(type_iterator TI) {
169 return removeEntry(TI);
172 /// changeName - Given a value with a non-empty name, remove its existing
173 /// entry from the symbol table and insert a new one for Name. This is
174 /// equivalent to doing "remove(V), V->Name = Name, insert(V)", but is faster,
175 /// and will not temporarily remove the symbol table plane if V is the last
176 /// value in the symtab with that name (which could invalidate iterators to
178 void changeName(Value *V, const std::string &Name);
180 /// This method will strip the symbol table of its names leaving
181 /// the type and values.
182 /// @brief Strip the symbol table.
190 /// Get an iterator that starts at the beginning of the type planes.
191 /// The iterator will iterate over the Type/ValueMap pairs in the
193 inline plane_iterator plane_begin() { return pmap.begin(); }
195 /// Get a const_iterator that starts at the beginning of the type
196 /// planes. The iterator will iterate over the Type/ValueMap pairs
197 /// in the type planes.
198 inline plane_const_iterator plane_begin() const { return pmap.begin(); }
200 /// Get an iterator at the end of the type planes. This serves as
201 /// the marker for end of iteration over the type planes.
202 inline plane_iterator plane_end() { return pmap.end(); }
204 /// Get a const_iterator at the end of the type planes. This serves as
205 /// the marker for end of iteration over the type planes.
206 inline plane_const_iterator plane_end() const { return pmap.end(); }
208 /// Get an iterator that starts at the beginning of a type plane.
209 /// The iterator will iterate over the name/value pairs in the type plane.
210 /// @note The type plane must already exist before using this.
211 inline value_iterator value_begin(const Type *Typ) {
212 assert(Typ && "Can't get value iterator with null type!");
213 return pmap.find(Typ)->second.begin();
216 /// Get a const_iterator that starts at the beginning of a type plane.
217 /// The iterator will iterate over the name/value pairs in the type plane.
218 /// @note The type plane must already exist before using this.
219 inline value_const_iterator value_begin(const Type *Typ) const {
220 assert(Typ && "Can't get value iterator with null type!");
221 return pmap.find(Typ)->second.begin();
224 /// Get an iterator to the end of a type plane. This serves as the marker
225 /// for end of iteration of the type plane.
226 /// @note The type plane must already exist before using this.
227 inline value_iterator value_end(const Type *Typ) {
228 assert(Typ && "Can't get value iterator with null type!");
229 return pmap.find(Typ)->second.end();
232 /// Get a const_iterator to the end of a type plane. This serves as the
233 /// marker for end of iteration of the type plane.
234 /// @note The type plane must already exist before using this.
235 inline value_const_iterator value_end(const Type *Typ) const {
236 assert(Typ && "Can't get value iterator with null type!");
237 return pmap.find(Typ)->second.end();
240 /// Get an iterator to the start of the name/Type map.
241 inline type_iterator type_begin() { return tmap.begin(); }
243 /// @brief Get a const_iterator to the start of the name/Type map.
244 inline type_const_iterator type_begin() const { return tmap.begin(); }
246 /// Get an iterator to the end of the name/Type map. This serves as the
247 /// marker for end of iteration of the types.
248 inline type_iterator type_end() { return tmap.end(); }
250 /// Get a const-iterator to the end of the name/Type map. This serves
251 /// as the marker for end of iteration of the types.
252 inline type_const_iterator type_end() const { return tmap.end(); }
254 /// This method returns a plane_const_iterator for iteration over
255 /// the type planes starting at a specific plane, given by \p Ty.
256 /// @brief Find a type plane.
257 inline plane_const_iterator find(const Type* Typ) const {
258 assert(Typ && "Can't find type plane with null type!");
259 return pmap.find(Typ);
262 /// This method returns a plane_iterator for iteration over the
263 /// type planes starting at a specific plane, given by \p Ty.
264 /// @brief Find a type plane.
265 inline plane_iterator find(const Type* Typ) {
266 assert(Typ && "Can't find type plane with null type!");
267 return pmap.find(Typ);
270 /// This method returns a ValueMap* for a specific type plane. This
271 /// interface is deprecated and may go away in the future.
273 /// @brief Find a type plane
274 inline const ValueMap* findPlane(const Type* Typ) const {
275 assert(Typ && "Can't find type plane with null type!");
276 plane_const_iterator I = pmap.find(Typ);
277 if (I == pmap.end()) return 0;
282 /// @name Internal Methods
285 /// @brief Insert a value into the symbol table with the specified name.
286 void insertEntry(const std::string &Name, const Type *Ty, Value *V);
288 /// @brief Insert a type into the symbol table with the specified name.
289 void insertEntry(const std::string &Name, const Type *T);
291 /// Remove a specific value from a specific plane in the SymbolTable.
292 /// @returns the removed Value.
293 Value* removeEntry(plane_iterator Plane, value_iterator Entry);
295 /// Remove a specific type from the SymbolTable.
296 /// @returns the removed Type.
297 Type* removeEntry(type_iterator Entry);
299 /// This function is called when one of the types in the type plane
301 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
303 /// This function markes a type as being concrete (defined).
304 virtual void typeBecameConcrete(const DerivedType *AbsTy);
307 /// @name Internal Data
311 /// This is the main content of the symbol table. It provides
312 /// separate type planes for named values. That is, each named
313 /// value is organized into a separate dictionary based on
314 /// Type. This means that the same name can be used for different
315 /// types without conflict.
316 /// @brief The mapping of types to names to values.
319 /// This is the type plane. It is separated from the pmap
320 /// because the elements of the map are name/Type pairs not
321 /// name/Value pairs and Type is not a Value.
324 /// This value is used to retain the last unique value used
325 /// by getUniqueName to generate unique names.
326 mutable unsigned long LastUnique;
332 } // End llvm namespace