1 //===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the name/Value symbol table for LLVM.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_VALUE_SYMBOL_TABLE_H
15 #define LLVM_VALUE_SYMBOL_TABLE_H
17 #include "llvm/Value.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/System/DataTypes.h"
20 #include "llvm/ADT/ilist_node.h"
23 template<typename ValueSubClass, typename ItemParentClass>
24 class SymbolTableListTraits;
31 /// This class provides a symbol table of name/value pairs. It is essentially
32 /// a std::map<std::string,Value*> but has a controlled interface provided by
33 /// LLVM as well as ensuring uniqueness of names.
35 class ValueSymbolTable {
37 friend class SymbolTableListTraits<Argument, Function>;
38 friend class SymbolTableListTraits<BasicBlock, Function>;
39 friend class SymbolTableListTraits<Instruction, BasicBlock>;
40 friend class SymbolTableListTraits<Function, Module>;
41 friend class SymbolTableListTraits<GlobalVariable, Module>;
42 friend class SymbolTableListTraits<GlobalAlias, Module>;
46 /// @brief A mapping of names to values.
47 typedef StringMap<Value*> ValueMap;
49 /// @brief An iterator over a ValueMap.
50 typedef ValueMap::iterator iterator;
52 /// @brief A const_iterator over a ValueMap.
53 typedef ValueMap::const_iterator const_iterator;
56 /// @name Constructors
60 ValueSymbolTable() : vmap(0), LastUnique(0) {}
68 /// This method finds the value with the given \p Name in the
70 /// @returns the value associated with the \p Name
71 /// @brief Lookup a named Value.
72 Value *lookup(StringRef Name) const { return vmap.lookup(Name); }
74 /// @returns true iff the symbol table is empty
75 /// @brief Determine if the symbol table is empty
76 inline bool empty() const { return vmap.empty(); }
78 /// @brief The number of name/type pairs is returned.
79 inline unsigned size() const { return unsigned(vmap.size()); }
81 /// This function can be used from the debugger to display the
82 /// content of the symbol table while debugging.
83 /// @brief Print out symbol table on stderr
90 /// @brief Get an iterator that from the beginning of the symbol table.
91 inline iterator begin() { return vmap.begin(); }
93 /// @brief Get a const_iterator that from the beginning of the symbol table.
94 inline const_iterator begin() const { return vmap.begin(); }
96 /// @brief Get an iterator to the end of the symbol table.
97 inline iterator end() { return vmap.end(); }
99 /// @brief Get a const_iterator to the end of the symbol table.
100 inline const_iterator end() const { return vmap.end(); }
106 /// This method adds the provided value \p N to the symbol table. The Value
107 /// must have a name which is used to place the value in the symbol table.
108 /// If the inserted name conflicts, this renames the value.
109 /// @brief Add a named value to the symbol table
110 void reinsertValue(Value *V);
112 /// createValueName - This method attempts to create a value name and insert
113 /// it into the symbol table with the specified name. If it conflicts, it
114 /// auto-renames the name and returns that instead.
115 ValueName *createValueName(StringRef Name, Value *V);
117 /// This method removes a value from the symbol table. It leaves the
118 /// ValueName attached to the value, but it is no longer inserted in the
120 void removeValueName(ValueName *V);
123 /// @name Internal Data
126 ValueMap vmap; ///< The map that holds the symbol table.
127 mutable uint32_t LastUnique; ///< Counter for tracking unique names
132 /// This class provides a symbol table of name/NamedMDNode pairs. It is
133 /// essentially a StringMap wrapper.
135 class MDSymbolTable {
136 friend class SymbolTableListTraits<NamedMDNode, Module>;
140 /// @brief A mapping of names to metadata
141 typedef StringMap<NamedMDNode*> MDMap;
144 /// @brief An iterator over a ValueMap.
145 typedef MDMap::iterator iterator;
147 /// @brief A const_iterator over a ValueMap.
148 typedef MDMap::const_iterator const_iterator;
151 /// @name Constructors
155 MDSymbolTable(const MDNode &); // DO NOT IMPLEMENT
156 void operator=(const MDSymbolTable &); // DO NOT IMPLEMENT
157 MDSymbolTable() : mmap(0) {}
165 /// This method finds the value with the given \p Name in the
166 /// the symbol table.
167 /// @returns the NamedMDNode associated with the \p Name
168 /// @brief Lookup a named Value.
169 NamedMDNode *lookup(StringRef Name) const { return mmap.lookup(Name); }
171 /// @returns true iff the symbol table is empty
172 /// @brief Determine if the symbol table is empty
173 inline bool empty() const { return mmap.empty(); }
175 /// @brief The number of name/type pairs is returned.
176 inline unsigned size() const { return unsigned(mmap.size()); }
182 /// @brief Get an iterator that from the beginning of the symbol table.
183 inline iterator begin() { return mmap.begin(); }
185 /// @brief Get a const_iterator that from the beginning of the symbol table.
186 inline const_iterator begin() const { return mmap.begin(); }
188 /// @brief Get an iterator to the end of the symbol table.
189 inline iterator end() { return mmap.end(); }
191 /// @brief Get a const_iterator to the end of the symbol table.
192 inline const_iterator end() const { return mmap.end(); }
198 /// insert - The method inserts a new entry into the stringmap.
199 void insert(StringRef Name, NamedMDNode *Node) {
200 (void) mmap.GetOrCreateValue(Name, Node);
203 /// This method removes a NamedMDNode from the symbol table.
204 void remove(StringRef Name) { mmap.erase(Name); }
207 /// @name Internal Data
210 MDMap mmap; ///< The map that holds the symbol table.
214 } // End llvm namespace