1 //===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer based on the original SymbolTable.h
6 // written by the LLVM research group and re-written by Reid Spencer.
7 // It is distributed under the University of Illinois Open Source License.
8 // See LICENSE.TXT for details.
10 //===----------------------------------------------------------------------===//
12 // This file implements the name/Value symbol table for LLVM.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_VALUE_SYMBOL_TABLE_H
17 #define LLVM_VALUE_SYMBOL_TABLE_H
19 #include "llvm/Value.h"
23 template<typename ValueSubClass, typename ItemParentClass,
24 typename SymTabClass, typename SubClass>
25 class SymbolTableListTraits;
26 template<typename NodeTy> struct ilist_traits;
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, Function,
38 ilist_traits<Argument> >;
39 friend class SymbolTableListTraits<BasicBlock, Function, Function,
40 ilist_traits<BasicBlock> >;
41 friend class SymbolTableListTraits<Instruction, BasicBlock, Function,
42 ilist_traits<Instruction> >;
43 friend class SymbolTableListTraits<Function, Module, Module,
44 ilist_traits<Function> >;
45 friend class SymbolTableListTraits<GlobalVariable, Module, Module,
46 ilist_traits<GlobalVariable> >;
51 /// @brief A mapping of names to values.
52 typedef std::map<const std::string, Value *> ValueMap;
54 /// @brief An iterator over a ValueMap.
55 typedef ValueMap::iterator iterator;
57 /// @brief A const_iterator over a ValueMap.
58 typedef ValueMap::const_iterator const_iterator;
61 /// @name Constructors
65 ValueSymbolTable() : LastUnique(0) {}
73 /// This method finds the value with the given \p name in the
75 /// @returns the value associated with the \p name
76 /// @brief Lookup a named Value.
77 Value *lookup(const std::string &name) const;
79 /// @returns true iff the symbol table is empty
80 /// @brief Determine if the symbol table is empty
81 inline bool empty() const { return vmap.empty(); }
83 /// @brief The number of name/type pairs is returned.
84 inline unsigned size() const { return unsigned(vmap.size()); }
86 /// Given a base name, return a string that is either equal to it or
87 /// derived from it that does not already occur in the symbol table
88 /// for the specified type.
89 /// @brief Get a name unique to this symbol table
90 std::string getUniqueName(const std::string &BaseName) const;
92 /// @return 1 if the name is in the symbol table, 0 otherwise
93 /// @brief Determine if a name is in the symbol table
94 ValueMap::size_type count(const std::string &name) const {
95 return vmap.count(name);
98 /// This function can be used from the debugger to display the
99 /// content of the symbol table while debugging.
100 /// @brief Print out symbol table on stderr
108 /// @brief Get an iterator that from the beginning of the symbol table.
109 inline iterator begin() { return vmap.begin(); }
111 /// @brief Get a const_iterator that from the beginning of the symbol table.
112 inline const_iterator begin() const { return vmap.begin(); }
114 /// @brief Get an iterator to the end of the symbol table.
115 inline iterator end() { return vmap.end(); }
117 /// @brief Get a const_iterator to the end of the symbol table.
118 inline const_iterator end() const { return vmap.end(); }
125 /// This method will strip the symbol table of its names.
126 /// @brief Strip the symbol table.
130 /// This method adds the provided value \p N to the symbol table. The Value
131 /// must have a name which is used to place the value in the symbol table.
132 /// @brief Add a named value to the symbol table
133 void insert(Value *Val);
135 /// This method removes a value from the symbol table. The name of the
136 /// Value is extracted from \p Val and used to lookup the Value in the
137 /// symbol table. If the Value is not in the symbol table, this method
138 /// returns false. \p Val is not deleted, just removed from the symbol table.
139 /// @returns true if \p Val was successfully removed, false otherwise
140 /// @brief Remove a value from the symbol table.
141 bool remove(Value* Val);
143 /// Given a value with a non-empty name, remove its existing
144 /// entry from the symbol table and insert a new one for Name. This is
145 /// equivalent to doing "remove(V), V->Name = Name, insert(V)".
146 /// @brief Rename a value in the symbol table
147 bool rename(Value *V, const std::string &Name);
150 /// @name Internal Data
153 ValueMap vmap; ///< The map that holds the symbol table.
154 mutable uint32_t LastUnique; ///< Counter for tracking unique names
160 } // End llvm namespace