501e04f59258704246e011122ffdf0f1b16f2c11
[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 <map>
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
51   /// @brief A mapping of names to values.
52   typedef std::map<const std::string, Value *> ValueMap;
53
54   /// @brief An iterator over a ValueMap.
55   typedef ValueMap::iterator iterator;
56
57   /// @brief A const_iterator over a ValueMap.
58   typedef ValueMap::const_iterator const_iterator;
59
60 /// @}
61 /// @name Constructors
62 /// @{
63 public:
64
65   ValueSymbolTable() : LastUnique(0) {}
66   ~ValueSymbolTable();
67
68 /// @}
69 /// @name Accessors
70 /// @{
71 public:
72
73   /// This method finds the value with the given \p name in the
74   /// the symbol table. 
75   /// @returns the value associated with the \p name
76   /// @brief Lookup a named Value.
77   Value *lookup(const std::string &name) const;
78
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(); }
82
83   /// @brief The number of name/type pairs is returned.
84   inline unsigned size() const { return unsigned(vmap.size()); }
85
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;
91
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);
96   }
97
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
101   void dump() const;
102
103 /// @}
104 /// @name Iteration
105 /// @{
106 public:
107
108   /// @brief Get an iterator that from the beginning of the symbol table.
109   inline iterator begin() { return vmap.begin(); }
110
111   /// @brief Get a const_iterator that from the beginning of the symbol table.
112   inline const_iterator begin() const { return vmap.begin(); }
113
114   /// @brief Get an iterator to the end of the symbol table.
115   inline iterator end() { return vmap.end(); }
116
117   /// @brief Get a const_iterator to the end of the symbol table.
118   inline const_iterator end() const { return vmap.end(); }
119
120 /// @}
121 /// @name Mutators
122 /// @{
123 public:
124
125   /// This method will strip the symbol table of its names.
126   /// @brief Strip the symbol table.
127   bool strip();
128   
129 private:
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);
134
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);
142
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);
148
149 /// @}
150 /// @name Internal Data
151 /// @{
152 private:
153   ValueMap vmap;                    ///< The map that holds the symbol table.
154   mutable uint32_t LastUnique; ///< Counter for tracking unique names
155
156 /// @}
157
158 };
159
160 } // End llvm namespace
161
162 #endif