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