1 //===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the name/Value symbol table for LLVM. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_IR_VALUESYMBOLTABLE_H 15 #define LLVM_IR_VALUESYMBOLTABLE_H 16 17 #include "llvm/ADT/SmallString.h" 18 #include "llvm/ADT/StringMap.h" 19 #include "llvm/IR/Value.h" 20 #include "llvm/Support/DataTypes.h" 21 22 namespace llvm { 23 template <typename ValueSubClass> class SymbolTableListTraits; 24 class BasicBlock; 25 class Function; 26 class NamedMDNode; 27 class Module; 28 class StringRef; 29 30 /// This class provides a symbol table of name/value pairs. It is essentially 31 /// a std::map<std::string,Value*> but has a controlled interface provided by 32 /// LLVM as well as ensuring uniqueness of names. 33 /// 34 class ValueSymbolTable { 35 friend class Value; 36 friend class SymbolTableListTraits<Argument>; 37 friend class SymbolTableListTraits<BasicBlock>; 38 friend class SymbolTableListTraits<Instruction>; 39 friend class SymbolTableListTraits<Function>; 40 friend class SymbolTableListTraits<GlobalVariable>; 41 friend class SymbolTableListTraits<GlobalAlias>; 42 /// @name Types 43 /// @{ 44 public: 45 /// @brief A mapping of names to values. 46 typedef StringMap<Value*> ValueMap; 47 48 /// @brief An iterator over a ValueMap. 49 typedef ValueMap::iterator iterator; 50 51 /// @brief A const_iterator over a ValueMap. 52 typedef ValueMap::const_iterator const_iterator; 53 54 /// @} 55 /// @name Constructors 56 /// @{ 57 public: ValueSymbolTable()58 ValueSymbolTable() : vmap(0), LastUnique(0) {} 59 ~ValueSymbolTable(); 60 61 /// @} 62 /// @name Accessors 63 /// @{ 64 public: 65 /// This method finds the value with the given \p Name in the 66 /// the symbol table. 67 /// @returns the value associated with the \p Name 68 /// @brief Lookup a named Value. lookup(StringRef Name)69 Value *lookup(StringRef Name) const { return vmap.lookup(Name); } 70 71 /// @returns true iff the symbol table is empty 72 /// @brief Determine if the symbol table is empty empty()73 inline bool empty() const { return vmap.empty(); } 74 75 /// @brief The number of name/type pairs is returned. size()76 inline unsigned size() const { return unsigned(vmap.size()); } 77 78 /// This function can be used from the debugger to display the 79 /// content of the symbol table while debugging. 80 /// @brief Print out symbol table on stderr 81 void dump() const; 82 83 /// @} 84 /// @name Iteration 85 /// @{ 86 public: 87 /// @brief Get an iterator that from the beginning of the symbol table. begin()88 inline iterator begin() { return vmap.begin(); } 89 90 /// @brief Get a const_iterator that from the beginning of the symbol table. begin()91 inline const_iterator begin() const { return vmap.begin(); } 92 93 /// @brief Get an iterator to the end of the symbol table. end()94 inline iterator end() { return vmap.end(); } 95 96 /// @brief Get a const_iterator to the end of the symbol table. end()97 inline const_iterator end() const { return vmap.end(); } 98 99 /// @} 100 /// @name Mutators 101 /// @{ 102 private: 103 ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName); 104 105 /// This method adds the provided value \p N to the symbol table. The Value 106 /// must have a name which is used to place the value in the symbol table. 107 /// If the inserted name conflicts, this renames the value. 108 /// @brief Add a named value to the symbol table 109 void reinsertValue(Value *V); 110 111 /// createValueName - This method attempts to create a value name and insert 112 /// it into the symbol table with the specified name. If it conflicts, it 113 /// auto-renames the name and returns that instead. 114 ValueName *createValueName(StringRef Name, Value *V); 115 116 /// This method removes a value from the symbol table. It leaves the 117 /// ValueName attached to the value, but it is no longer inserted in the 118 /// symtab. 119 void removeValueName(ValueName *V); 120 121 /// @} 122 /// @name Internal Data 123 /// @{ 124 private: 125 ValueMap vmap; ///< The map that holds the symbol table. 126 mutable uint32_t LastUnique; ///< Counter for tracking unique names 127 128 /// @} 129 }; 130 131 } // End llvm namespace 132 133 #endif 134