1249259Sdim//===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===//
2249259Sdim//
3249259Sdim//                     The LLVM Compiler Infrastructure
4249259Sdim//
5249259Sdim// This file is distributed under the University of Illinois Open Source
6249259Sdim// License. See LICENSE.TXT for details.
7249259Sdim//
8249259Sdim//===----------------------------------------------------------------------===//
9249259Sdim//
10249259Sdim// This file implements the name/Value symbol table for LLVM.
11249259Sdim//
12249259Sdim//===----------------------------------------------------------------------===//
13249259Sdim
14249259Sdim#ifndef LLVM_IR_VALUESYMBOLTABLE_H
15249259Sdim#define LLVM_IR_VALUESYMBOLTABLE_H
16249259Sdim
17249259Sdim#include "llvm/ADT/StringMap.h"
18249259Sdim#include "llvm/IR/Value.h"
19249259Sdim#include "llvm/Support/DataTypes.h"
20249259Sdim
21249259Sdimnamespace llvm {
22249259Sdim  template<typename ValueSubClass, typename ItemParentClass>
23249259Sdim        class SymbolTableListTraits;
24249259Sdim  class BasicBlock;
25249259Sdim  class Function;
26249259Sdim  class NamedMDNode;
27249259Sdim  class Module;
28249259Sdim  class StringRef;
29249259Sdim
30249259Sdim/// This class provides a symbol table of name/value pairs. It is essentially
31249259Sdim/// a std::map<std::string,Value*> but has a controlled interface provided by
32249259Sdim/// LLVM as well as ensuring uniqueness of names.
33249259Sdim///
34249259Sdimclass ValueSymbolTable {
35249259Sdim  friend class Value;
36249259Sdim  friend class SymbolTableListTraits<Argument, Function>;
37249259Sdim  friend class SymbolTableListTraits<BasicBlock, Function>;
38249259Sdim  friend class SymbolTableListTraits<Instruction, BasicBlock>;
39249259Sdim  friend class SymbolTableListTraits<Function, Module>;
40249259Sdim  friend class SymbolTableListTraits<GlobalVariable, Module>;
41249259Sdim  friend class SymbolTableListTraits<GlobalAlias, Module>;
42249259Sdim/// @name Types
43249259Sdim/// @{
44249259Sdimpublic:
45249259Sdim  /// @brief A mapping of names to values.
46249259Sdim  typedef StringMap<Value*> ValueMap;
47249259Sdim
48249259Sdim  /// @brief An iterator over a ValueMap.
49249259Sdim  typedef ValueMap::iterator iterator;
50249259Sdim
51249259Sdim  /// @brief A const_iterator over a ValueMap.
52249259Sdim  typedef ValueMap::const_iterator const_iterator;
53249259Sdim
54249259Sdim/// @}
55249259Sdim/// @name Constructors
56249259Sdim/// @{
57249259Sdimpublic:
58249259Sdim
59249259Sdim  ValueSymbolTable() : vmap(0), LastUnique(0) {}
60249259Sdim  ~ValueSymbolTable();
61249259Sdim
62249259Sdim/// @}
63249259Sdim/// @name Accessors
64249259Sdim/// @{
65249259Sdimpublic:
66249259Sdim
67249259Sdim  /// This method finds the value with the given \p Name in the
68249259Sdim  /// the symbol table.
69249259Sdim  /// @returns the value associated with the \p Name
70249259Sdim  /// @brief Lookup a named Value.
71249259Sdim  Value *lookup(StringRef Name) const { return vmap.lookup(Name); }
72249259Sdim
73249259Sdim  /// @returns true iff the symbol table is empty
74249259Sdim  /// @brief Determine if the symbol table is empty
75249259Sdim  inline bool empty() const { return vmap.empty(); }
76249259Sdim
77249259Sdim  /// @brief The number of name/type pairs is returned.
78249259Sdim  inline unsigned size() const { return unsigned(vmap.size()); }
79249259Sdim
80249259Sdim  /// This function can be used from the debugger to display the
81249259Sdim  /// content of the symbol table while debugging.
82249259Sdim  /// @brief Print out symbol table on stderr
83249259Sdim  void dump() const;
84249259Sdim
85249259Sdim/// @}
86249259Sdim/// @name Iteration
87249259Sdim/// @{
88249259Sdimpublic:
89249259Sdim  /// @brief Get an iterator that from the beginning of the symbol table.
90249259Sdim  inline iterator begin() { return vmap.begin(); }
91249259Sdim
92249259Sdim  /// @brief Get a const_iterator that from the beginning of the symbol table.
93249259Sdim  inline const_iterator begin() const { return vmap.begin(); }
94249259Sdim
95249259Sdim  /// @brief Get an iterator to the end of the symbol table.
96249259Sdim  inline iterator end() { return vmap.end(); }
97249259Sdim
98249259Sdim  /// @brief Get a const_iterator to the end of the symbol table.
99249259Sdim  inline const_iterator end() const { return vmap.end(); }
100249259Sdim
101249259Sdim/// @}
102249259Sdim/// @name Mutators
103249259Sdim/// @{
104249259Sdimprivate:
105249259Sdim  /// This method adds the provided value \p N to the symbol table.  The Value
106249259Sdim  /// must have a name which is used to place the value in the symbol table.
107249259Sdim  /// If the inserted name conflicts, this renames the value.
108249259Sdim  /// @brief Add a named value to the symbol table
109249259Sdim  void reinsertValue(Value *V);
110249259Sdim
111249259Sdim  /// createValueName - This method attempts to create a value name and insert
112249259Sdim  /// it into the symbol table with the specified name.  If it conflicts, it
113249259Sdim  /// auto-renames the name and returns that instead.
114249259Sdim  ValueName *createValueName(StringRef Name, Value *V);
115249259Sdim
116249259Sdim  /// This method removes a value from the symbol table.  It leaves the
117249259Sdim  /// ValueName attached to the value, but it is no longer inserted in the
118249259Sdim  /// symtab.
119249259Sdim  void removeValueName(ValueName *V);
120249259Sdim
121249259Sdim/// @}
122249259Sdim/// @name Internal Data
123249259Sdim/// @{
124249259Sdimprivate:
125249259Sdim  ValueMap vmap;                    ///< The map that holds the symbol table.
126249259Sdim  mutable uint32_t LastUnique; ///< Counter for tracking unique names
127249259Sdim
128249259Sdim/// @}
129249259Sdim};
130249259Sdim
131249259Sdim} // End llvm namespace
132249259Sdim
133249259Sdim#endif
134