LLVM API Documentation
00001 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and re-written by Reid 00006 // Spencer. It is distributed under the University of Illinois Open Source 00007 // License. See LICENSE.TXT for details. 00008 // 00009 //===----------------------------------------------------------------------===// 00010 // 00011 // This file implements the main symbol table for LLVM. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_SYMBOL_TABLE_H 00016 #define LLVM_SYMBOL_TABLE_H 00017 00018 #include "llvm/Value.h" 00019 #include <map> 00020 00021 namespace llvm { 00022 00023 /// This class provides a symbol table of name/value pairs that is broken 00024 /// up by type. For each Type* there is a "plane" of name/value pairs in 00025 /// the symbol table. Identical types may have overlapping symbol names as 00026 /// long as they are distinct. The SymbolTable also tracks, separately, a 00027 /// map of name/type pairs. This allows types to be named. Types are treated 00028 /// distinctly from Values. 00029 /// 00030 /// The SymbolTable provides several utility functions for answering common 00031 /// questions about its contents as well as an iterator interface for 00032 /// directly iterating over the contents. To reduce confusion, the terms 00033 /// "type", "value", and "plane" are used consistently. For example, 00034 /// There is a TypeMap typedef that is the mapping of names to Types. 00035 /// Similarly there is a ValueMap typedef that is the mapping of 00036 /// names to Values. Finally, there is a PlaneMap typedef that is the 00037 /// mapping of types to planes of ValueMap. This is the basic structure 00038 /// of the symbol table. When you call type_begin() you're asking 00039 /// for an iterator at the start of the TypeMap. When you call 00040 /// plane_begin(), you're asking for an iterator at the start of 00041 /// the PlaneMap. Finally, when you call value_begin(), you're asking 00042 /// for an iterator at the start of a ValueMap for a specific type 00043 /// plane. 00044 class SymbolTable : public AbstractTypeUser { 00045 00046 /// @name Types 00047 /// @{ 00048 public: 00049 00050 /// @brief A mapping of names to types. 00051 typedef std::map<const std::string, const Type*> TypeMap; 00052 00053 /// @brief An iterator over the TypeMap. 00054 typedef TypeMap::iterator type_iterator; 00055 00056 /// @brief A const_iterator over the TypeMap. 00057 typedef TypeMap::const_iterator type_const_iterator; 00058 00059 /// @brief A mapping of names to values. 00060 typedef std::map<const std::string, Value *> ValueMap; 00061 00062 /// @brief An iterator over a ValueMap. 00063 typedef ValueMap::iterator value_iterator; 00064 00065 /// @brief A const_iterator over a ValueMap. 00066 typedef ValueMap::const_iterator value_const_iterator; 00067 00068 /// @brief A mapping of types to names to values (type planes). 00069 typedef std::map<const Type *, ValueMap> PlaneMap; 00070 00071 /// @brief An iterator over the type planes. 00072 typedef PlaneMap::iterator plane_iterator; 00073 00074 /// @brief A const_iterator over the type planes 00075 typedef PlaneMap::const_iterator plane_const_iterator; 00076 00077 /// @} 00078 /// @name Constructors 00079 /// @{ 00080 public: 00081 00082 SymbolTable() : LastUnique(0) {} 00083 ~SymbolTable(); 00084 00085 /// @} 00086 /// @name Accessors 00087 /// @{ 00088 public: 00089 00090 /// This method finds the value with the given \p name in the 00091 /// type plane \p Ty and returns it. This method will not find any 00092 /// Types, only Values. Use lookupType to find Types by name. 00093 /// @returns null on failure, otherwise the Value associated with 00094 /// the \p name in type plane \p Ty. 00095 /// @brief Lookup a named, typed value. 00096 Value *lookup(const Type *Ty, const std::string &name) const; 00097 00098 /// This method finds the type with the given \p name in the 00099 /// type map and returns it. 00100 /// @returns null if the name is not found, otherwise the Type 00101 /// associated with the \p name. 00102 /// @brief Lookup a type by name. 00103 Type* lookupType(const std::string& name) const; 00104 00105 /// @returns true iff the type map and the type plane are both not 00106 /// empty. 00107 /// @brief Determine if the symbol table is empty 00108 inline bool isEmpty() const { return pmap.empty() && tmap.empty(); } 00109 00110 /// @brief The number of name/type pairs is returned. 00111 inline unsigned num_types() const { return unsigned(tmap.size()); } 00112 00113 /// Given a base name, return a string that is either equal to it or 00114 /// derived from it that does not already occur in the symbol table 00115 /// for the specified type. 00116 /// @brief Get a name unique to this symbol table 00117 std::string getUniqueName(const Type *Ty, 00118 const std::string &BaseName) const; 00119 00120 /// This function can be used from the debugger to display the 00121 /// content of the symbol table while debugging. 00122 /// @brief Print out symbol table on stderr 00123 void dump() const; 00124 00125 /// @} 00126 /// @name Iteration 00127 /// @{ 00128 public: 00129 00130 /// Get an iterator that starts at the beginning of the type planes. 00131 /// The iterator will iterate over the Type/ValueMap pairs in the 00132 /// type planes. 00133 inline plane_iterator plane_begin() { return pmap.begin(); } 00134 00135 /// Get a const_iterator that starts at the beginning of the type 00136 /// planes. The iterator will iterate over the Type/ValueMap pairs 00137 /// in the type planes. 00138 inline plane_const_iterator plane_begin() const { return pmap.begin(); } 00139 00140 /// Get an iterator at the end of the type planes. This serves as 00141 /// the marker for end of iteration over the type planes. 00142 inline plane_iterator plane_end() { return pmap.end(); } 00143 00144 /// Get a const_iterator at the end of the type planes. This serves as 00145 /// the marker for end of iteration over the type planes. 00146 inline plane_const_iterator plane_end() const { return pmap.end(); } 00147 00148 /// Get an iterator that starts at the beginning of a type plane. 00149 /// The iterator will iterate over the name/value pairs in the type plane. 00150 /// @note The type plane must already exist before using this. 00151 inline value_iterator value_begin(const Type *Typ) { 00152 assert(Typ && "Can't get value iterator with null type!"); 00153 return pmap.find(Typ)->second.begin(); 00154 } 00155 00156 /// Get a const_iterator that starts at the beginning of a type plane. 00157 /// The iterator will iterate over the name/value pairs in the type plane. 00158 /// @note The type plane must already exist before using this. 00159 inline value_const_iterator value_begin(const Type *Typ) const { 00160 assert(Typ && "Can't get value iterator with null type!"); 00161 return pmap.find(Typ)->second.begin(); 00162 } 00163 00164 /// Get an iterator to the end of a type plane. This serves as the marker 00165 /// for end of iteration of the type plane. 00166 /// @note The type plane must already exist before using this. 00167 inline value_iterator value_end(const Type *Typ) { 00168 assert(Typ && "Can't get value iterator with null type!"); 00169 return pmap.find(Typ)->second.end(); 00170 } 00171 00172 /// Get a const_iterator to the end of a type plane. This serves as the 00173 /// marker for end of iteration of the type plane. 00174 /// @note The type plane must already exist before using this. 00175 inline value_const_iterator value_end(const Type *Typ) const { 00176 assert(Typ && "Can't get value iterator with null type!"); 00177 return pmap.find(Typ)->second.end(); 00178 } 00179 00180 /// Get an iterator to the start of the name/Type map. 00181 inline type_iterator type_begin() { return tmap.begin(); } 00182 00183 /// @brief Get a const_iterator to the start of the name/Type map. 00184 inline type_const_iterator type_begin() const { return tmap.begin(); } 00185 00186 /// Get an iterator to the end of the name/Type map. This serves as the 00187 /// marker for end of iteration of the types. 00188 inline type_iterator type_end() { return tmap.end(); } 00189 00190 /// Get a const-iterator to the end of the name/Type map. This serves 00191 /// as the marker for end of iteration of the types. 00192 inline type_const_iterator type_end() const { return tmap.end(); } 00193 00194 /// This method returns a plane_const_iterator for iteration over 00195 /// the type planes starting at a specific plane, given by \p Ty. 00196 /// @brief Find a type plane. 00197 inline plane_const_iterator find(const Type* Typ) const { 00198 assert(Typ && "Can't find type plane with null type!"); 00199 return pmap.find(Typ); 00200 } 00201 00202 /// This method returns a plane_iterator for iteration over the 00203 /// type planes starting at a specific plane, given by \p Ty. 00204 /// @brief Find a type plane. 00205 inline plane_iterator find(const Type* Typ) { 00206 assert(Typ && "Can't find type plane with null type!"); 00207 return pmap.find(Typ); 00208 } 00209 00210 00211 /// @} 00212 /// @name Mutators 00213 /// @{ 00214 public: 00215 00216 /// This method will strip the symbol table of its names leaving the type and 00217 /// values. 00218 /// @brief Strip the symbol table. 00219 bool strip(); 00220 00221 /// Inserts a type into the symbol table with the specified name. There can be 00222 /// a many-to-one mapping between names and types. This method allows a type 00223 /// with an existing entry in the symbol table to get a new name. 00224 /// @brief Insert a type under a new name. 00225 void insert(const std::string &Name, const Type *Typ); 00226 00227 /// Remove a type at the specified position in the symbol table. 00228 /// @returns the removed Type. 00229 Type* remove(type_iterator TI); 00230 00231 /// @} 00232 /// @name Mutators used by Value::setName and other LLVM internals. 00233 /// @{ 00234 public: 00235 00236 /// This method adds the provided value \p N to the symbol table. The Value 00237 /// must have both a name and a type which are extracted and used to place the 00238 /// value in the correct type plane under the value's name. 00239 /// @brief Add a named value to the symbol table 00240 inline void insert(Value *Val) { 00241 assert(Val && "Can't insert null type into symbol table!"); 00242 assert(Val->hasName() && "Value must be named to go into symbol table!"); 00243 insertEntry(Val->getName(), Val->getType(), Val); 00244 } 00245 00246 /// This method removes a named value from the symbol table. The type and name 00247 /// of the Value are extracted from \p N and used to lookup the Value in the 00248 /// correct type plane. If the Value is not in the symbol table, this method 00249 /// silently ignores the request. 00250 /// @brief Remove a named value from the symbol table. 00251 void remove(Value* Val); 00252 00253 /// changeName - Given a value with a non-empty name, remove its existing 00254 /// entry from the symbol table and insert a new one for Name. This is 00255 /// equivalent to doing "remove(V), V->Name = Name, insert(V)", but is faster, 00256 /// and will not temporarily remove the symbol table plane if V is the last 00257 /// value in the symtab with that name (which could invalidate iterators to 00258 /// that plane). 00259 void changeName(Value *V, const std::string &Name); 00260 00261 /// @} 00262 /// @name Internal Methods 00263 /// @{ 00264 private: 00265 /// @brief Insert a value into the symbol table with the specified name. 00266 void insertEntry(const std::string &Name, const Type *Ty, Value *V); 00267 00268 /// This function is called when one of the types in the type plane 00269 /// is refined. 00270 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy); 00271 00272 /// This function markes a type as being concrete (defined). 00273 virtual void typeBecameConcrete(const DerivedType *AbsTy); 00274 00275 /// @} 00276 /// @name Internal Data 00277 /// @{ 00278 private: 00279 00280 /// This is the main content of the symbol table. It provides 00281 /// separate type planes for named values. That is, each named 00282 /// value is organized into a separate dictionary based on 00283 /// Type. This means that the same name can be used for different 00284 /// types without conflict. 00285 /// @brief The mapping of types to names to values. 00286 PlaneMap pmap; 00287 00288 /// This is the type plane. It is separated from the pmap 00289 /// because the elements of the map are name/Type pairs not 00290 /// name/Value pairs and Type is not a Value. 00291 TypeMap tmap; 00292 00293 /// This value is used to retain the last unique value used 00294 /// by getUniqueName to generate unique names. 00295 mutable unsigned long LastUnique; 00296 00297 /// @} 00298 00299 }; 00300 00301 } // End llvm namespace 00302 00303 // vim: sw=2 00304 00305 #endif 00306