1 //===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under
6 // the University of Illinois Open Source 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"
21 template<typename ValueSubClass, typename ItemParentClass>
22 class SymbolTableListTraits;
27 /// This class provides a symbol table of name/value pairs. It is essentially
28 /// a std::map<std::string,Value*> but has a controlled interface provided by
29 /// LLVM as well as ensuring uniqueness of names.
31 class ValueSymbolTable {
33 friend class SymbolTableListTraits<Argument, Function>;
34 friend class SymbolTableListTraits<BasicBlock, Function>;
35 friend class SymbolTableListTraits<Instruction, BasicBlock>;
36 friend class SymbolTableListTraits<Function, Module>;
37 friend class SymbolTableListTraits<GlobalVariable, Module>;
41 /// @brief A mapping of names to values.
42 typedef StringMap<Value*> ValueMap;
44 /// @brief An iterator over a ValueMap.
45 typedef ValueMap::iterator iterator;
47 /// @brief A const_iterator over a ValueMap.
48 typedef ValueMap::const_iterator const_iterator;
51 /// @name Constructors
55 ValueSymbolTable() : vmap(0), LastUnique(0) {}
63 /// This method finds the value with the given \p name in the
65 /// @returns the value associated with the \p name
66 /// @brief Lookup a named Value.
67 Value *lookup(const std::string &name) const;
69 /// @returns true iff the symbol table is empty
70 /// @brief Determine if the symbol table is empty
71 inline bool empty() const { return vmap.empty(); }
73 /// @brief The number of name/type pairs is returned.
74 inline unsigned size() const { return unsigned(vmap.size()); }
76 /// Given a base name, return a string that is either equal to it or
77 /// derived from it that does not already occur in the symbol table
78 /// for the specified type.
79 /// @brief Get a name unique to this symbol table
80 std::string getUniqueName(const std::string &BaseName) const;
82 /// This function can be used from the debugger to display the
83 /// content of the symbol table while debugging.
84 /// @brief Print out symbol table on stderr
91 /// @brief Get an iterator that from the beginning of the symbol table.
92 inline iterator begin() { return vmap.begin(); }
94 /// @brief Get a const_iterator that from the beginning of the symbol table.
95 inline const_iterator begin() const { return vmap.begin(); }
97 /// @brief Get an iterator to the end of the symbol table.
98 inline iterator end() { return vmap.end(); }
100 /// @brief Get a const_iterator to the end of the symbol table.
101 inline const_iterator end() const { return vmap.end(); }
107 /// This method adds the provided value \p N to the symbol table. The Value
108 /// must have a name which is used to place the value in the symbol table.
109 /// If the inserted name conflicts, this renames the value.
110 /// @brief Add a named value to the symbol table
111 void reinsertValue(Value *V);
113 /// createValueName - This method attempts to create a value name and insert
114 /// it into the symbol table with the specified name. If it conflicts, it
115 /// auto-renames the name and returns that instead.
116 ValueName *createValueName(const char *NameStart, unsigned NameLen, Value *V);
118 /// This method removes a value from the symbol table. It leaves the
119 /// ValueName attached to the value, but it is no longer inserted in the
121 void removeValueName(ValueName *V);
124 /// @name Internal Data
127 ValueMap vmap; ///< The map that holds the symbol table.
128 mutable uint32_t LastUnique; ///< Counter for tracking unique names
133 } // End llvm namespace