Switch ValueSymbolTable to use StringMap<Value*> instead of std::map<std::string...
[oota-llvm.git] / include / llvm / ValueSymbolTable.h
1 //===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
9 //
10 //===----------------------------------------------------------------------===//
11 //
12 // This file implements the name/Value symbol table for LLVM.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_VALUE_SYMBOL_TABLE_H
17 #define LLVM_VALUE_SYMBOL_TABLE_H
18
19 #include "llvm/Value.h"
20 #include "llvm/ADT/StringMap.h"
21
22 namespace llvm {
23   template<typename ValueSubClass, typename ItemParentClass,
24            typename SymTabClass, typename SubClass>
25         class SymbolTableListTraits;
26   template<typename NodeTy> struct ilist_traits;
27   class BasicBlock;
28   class Function;
29   class Module;
30   
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.
34 ///
35 class ValueSymbolTable {
36   friend class Value;
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> >;
47 /// @name Types
48 /// @{
49 public:
50   /// @brief A mapping of names to values.
51   typedef StringMap<Value*> ValueMap;
52
53   /// @brief An iterator over a ValueMap.
54   typedef ValueMap::iterator iterator;
55
56   /// @brief A const_iterator over a ValueMap.
57   typedef ValueMap::const_iterator const_iterator;
58
59 /// @}
60 /// @name Constructors
61 /// @{
62 public:
63
64   ValueSymbolTable() : LastUnique(0) {}
65   ~ValueSymbolTable();
66
67 /// @}
68 /// @name Accessors
69 /// @{
70 public:
71
72   /// This method finds the value with the given \p name in the
73   /// the symbol table. 
74   /// @returns the value associated with the \p name
75   /// @brief Lookup a named Value.
76   Value *lookup(const std::string &name) const;
77
78   /// @returns true iff the symbol table is empty
79   /// @brief Determine if the symbol table is empty
80   inline bool empty() const { return vmap.empty(); }
81
82   /// @brief The number of name/type pairs is returned.
83   inline unsigned size() const { return unsigned(vmap.size()); }
84
85   /// Given a base name, return a string that is either equal to it or
86   /// derived from it that does not already occur in the symbol table
87   /// for the specified type.
88   /// @brief Get a name unique to this symbol table
89   std::string getUniqueName(const std::string &BaseName) const;
90
91   /// This function can be used from the debugger to display the
92   /// content of the symbol table while debugging.
93   /// @brief Print out symbol table on stderr
94   void dump() const;
95
96 /// @}
97 /// @name Iteration
98 /// @{
99 public:
100   /// @brief Get an iterator that from the beginning of the symbol table.
101   inline iterator begin() { return vmap.begin(); }
102
103   /// @brief Get a const_iterator that from the beginning of the symbol table.
104   inline const_iterator begin() const { return vmap.begin(); }
105
106   /// @brief Get an iterator to the end of the symbol table.
107   inline iterator end() { return vmap.end(); }
108
109   /// @brief Get a const_iterator to the end of the symbol table.
110   inline const_iterator end() const { return vmap.end(); }
111   
112 /// @}
113 /// @name Mutators
114 /// @{
115 private:
116   /// This method adds the provided value \p N to the symbol table.  The Value
117   /// must have a name which is used to place the value in the symbol table. 
118   /// If the inserted name conflicts, this renames the value.
119   /// @brief Add a named value to the symbol table
120   void reinsertValue(Value *V);
121     
122   /// createValueName - This method attempts to create a value name and insert
123   /// it into the symbol table with the specified name.  If it conflicts, it
124   /// auto-renames the name and returns that instead.
125   ValueName *createValueName(const char *NameStart, unsigned NameLen, Value *V);
126   
127   /// This method removes a value from the symbol table.  It leaves the
128   /// ValueName attached to the value, but it is no longer inserted in the
129   /// symtab.
130   void removeValueName(ValueName *V);
131   
132 /// @}
133 /// @name Internal Data
134 /// @{
135 private:
136   ValueMap vmap;                    ///< The map that holds the symbol table.
137   mutable uint32_t LastUnique; ///< Counter for tracking unique names
138
139 /// @}
140 };
141
142 } // End llvm namespace
143
144 #endif