515e054d5c53dacfcec2d3cd4a3f1d866334d137
[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   /// This function can be used from the debugger to display the
76   /// content of the symbol table while debugging.
77   /// @brief Print out symbol table on stderr
78   void dump() const;
79
80 /// @}
81 /// @name Iteration
82 /// @{
83 public:
84
85   /// @brief Get an iterator that from the beginning of the symbol table.
86   inline iterator begin() { return vmap.begin(); }
87
88   /// @brief Get a const_iterator that from the beginning of the symbol table.
89   inline const_iterator begin() const { return vmap.begin(); }
90
91   /// @brief Get an iterator to the end of the symbol table.
92   inline iterator end() { return vmap.end(); }
93
94   /// @brief Get a const_iterator to the end of the symbol table.
95   inline const_iterator end() const { return vmap.end(); }
96
97 /// @}
98 /// @name Mutators
99 /// @{
100 public:
101
102   /// This method will strip the symbol table of its names.
103   /// @brief Strip the symbol table.
104   bool strip();
105
106   /// This method adds the provided value \p N to the symbol table.  The Value
107   /// must have a name which is used to place the value in the symbol table. 
108   /// @brief Add a named value to the symbol table
109   void insert(Value *Val);
110
111   /// This method removes a value from the symbol table. The name of the
112   /// Value is extracted from \p Val and used to lookup the Value in the
113   /// symbol table. If the Value is not in the symbol table, this method
114   /// returns false.
115   /// @returns true if \p Val was successfully erased, false otherwise
116   /// @brief Remove a value from the symbol table.
117   bool erase(Value* Val);
118
119   /// Given a value with a non-empty name, remove its existing
120   /// entry from the symbol table and insert a new one for Name.  This is
121   /// equivalent to doing "remove(V), V->Name = Name, insert(V)".
122   /// @brief Rename a value in the symbol table
123   bool rename(Value *V, const std::string &Name);
124
125 /// @}
126 /// @name Internal Data
127 /// @{
128 private:
129   ValueMap vmap;                    ///< The map that holds the symbol table.
130   mutable uint32_t LastUnique; ///< Counter for tracking unique names
131
132 /// @}
133
134 };
135
136 } // End llvm namespace
137
138 #endif