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