For PR411:
[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
24 /// This class provides a symbol table of name/value pairs. It is essentially
25 /// a std::map<std::string,Value*> but has a controlled interface provided by
26 /// LLVM as well as ensuring uniqueness of names.
27 ///
28 class ValueSymbolTable {
29
30 /// @name Types
31 /// @{
32 public:
33
34   /// @brief A mapping of names to values.
35   typedef std::map<const std::string, Value *> ValueMap;
36
37   /// @brief An iterator over a ValueMap.
38   typedef ValueMap::iterator iterator;
39
40   /// @brief A const_iterator over a ValueMap.
41   typedef ValueMap::const_iterator const_iterator;
42
43 /// @}
44 /// @name Constructors
45 /// @{
46 public:
47
48   ValueSymbolTable() : LastUnique(0) {}
49   ~ValueSymbolTable();
50
51 /// @}
52 /// @name Accessors
53 /// @{
54 public:
55
56   /// This method finds the value with the given \p name in the
57   /// the symbol table. 
58   /// @returns the value associated with the \p name
59   /// @brief Lookup a named Value.
60   Value *lookup(const std::string &name) const;
61
62   /// @returns true iff the symbol table is empty
63   /// @brief Determine if the symbol table is empty
64   inline bool empty() const { return vmap.empty(); }
65
66   /// @brief The number of name/type pairs is returned.
67   inline unsigned size() const { return unsigned(vmap.size()); }
68
69   /// Given a base name, return a string that is either equal to it or
70   /// derived from it that does not already occur in the symbol table
71   /// for the specified type.
72   /// @brief Get a name unique to this symbol table
73   std::string getUniqueName(const std::string &BaseName) const;
74
75   /// @return 1 if the name is in the symbol table, 0 otherwise
76   /// @brief Determine if a name is in the symbol table
77   ValueMap::size_type count(const std::string &name) const { 
78     return vmap.count(name);
79   }
80
81   /// This function can be used from the debugger to display the
82   /// content of the symbol table while debugging.
83   /// @brief Print out symbol table on stderr
84   void dump() const;
85
86 /// @}
87 /// @name Iteration
88 /// @{
89 public:
90
91   /// @brief Get an iterator that from the beginning of the symbol table.
92   inline iterator begin() { return vmap.begin(); }
93
94   /// @brief Get a const_iterator that from the beginning of the symbol table.
95   inline const_iterator begin() const { return vmap.begin(); }
96
97   /// @brief Get an iterator to the end of the symbol table.
98   inline iterator end() { return vmap.end(); }
99
100   /// @brief Get a const_iterator to the end of the symbol table.
101   inline const_iterator end() const { return vmap.end(); }
102
103 /// @}
104 /// @name Mutators
105 /// @{
106 public:
107
108   /// This method will strip the symbol table of its names.
109   /// @brief Strip the symbol table.
110   bool strip();
111
112   /// This method adds the provided value \p N to the symbol table.  The Value
113   /// must have a name which is used to place the value in the symbol table. 
114   /// @brief Add a named value to the symbol table
115   void insert(Value *Val);
116
117   /// This method removes a value from the symbol table. The name of the
118   /// Value is extracted from \p Val and used to lookup the Value in the
119   /// symbol table. If the Value is not in the symbol table, this method
120   /// returns false. \p Val is not deleted, just removed from the symbol table.
121   /// @returns true if \p Val was successfully removed, false otherwise
122   /// @brief Remove a value from the symbol table.
123   bool remove(Value* Val);
124
125   /// Given a value with a non-empty name, remove its existing
126   /// entry from the symbol table and insert a new one for Name.  This is
127   /// equivalent to doing "remove(V), V->Name = Name, insert(V)".
128   /// @brief Rename a value in the symbol table
129   bool rename(Value *V, const std::string &Name);
130
131 /// @}
132 /// @name Internal Data
133 /// @{
134 private:
135   ValueMap vmap;                    ///< The map that holds the symbol table.
136   mutable uint32_t LastUnique; ///< Counter for tracking unique names
137
138 /// @}
139
140 };
141
142 } // End llvm namespace
143
144 #endif