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