Add another required #include for freestanding .h files.
[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 is distributed under the University of Illinois Open Source
6 // 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 #include "llvm/Support/DataTypes.h"
20
21 namespace llvm {
22   template<typename ValueSubClass, typename ItemParentClass>
23         class SymbolTableListTraits;
24   class BasicBlock;
25   class Function;
26   class NamedMDNode;
27   class Module;
28   class StringRef;
29   
30 /// This class provides a symbol table of name/value pairs. It is essentially
31 /// a std::map<std::string,Value*> but has a controlled interface provided by
32 /// LLVM as well as ensuring uniqueness of names.
33 ///
34 class ValueSymbolTable {
35   friend class Value;
36   friend class SymbolTableListTraits<Argument, Function>;
37   friend class SymbolTableListTraits<BasicBlock, Function>;
38   friend class SymbolTableListTraits<Instruction, BasicBlock>;
39   friend class SymbolTableListTraits<Function, Module>;
40   friend class SymbolTableListTraits<GlobalVariable, Module>;
41   friend class SymbolTableListTraits<GlobalAlias, Module>;
42   friend class SymbolTableListTraits<NamedMDNode, Module>;
43 /// @name Types
44 /// @{
45 public:
46   /// @brief A mapping of names to values.
47   typedef StringMap<Value*> ValueMap;
48
49   /// @brief An iterator over a ValueMap.
50   typedef ValueMap::iterator iterator;
51
52   /// @brief A const_iterator over a ValueMap.
53   typedef ValueMap::const_iterator const_iterator;
54
55 /// @}
56 /// @name Constructors
57 /// @{
58 public:
59
60   ValueSymbolTable() : vmap(0), LastUnique(0) {}
61   ~ValueSymbolTable();
62
63 /// @}
64 /// @name Accessors
65 /// @{
66 public:
67
68   /// This method finds the value with the given \p Name in the
69   /// the symbol table. 
70   /// @returns the value associated with the \p Name
71   /// @brief Lookup a named Value.
72   Value *lookup(const StringRef &Name) const { return vmap.lookup(Name); }
73
74   /// @returns true iff the symbol table is empty
75   /// @brief Determine if the symbol table is empty
76   inline bool empty() const { return vmap.empty(); }
77
78   /// @brief The number of name/type pairs is returned.
79   inline unsigned size() const { return unsigned(vmap.size()); }
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   /// @brief Get an iterator that from the beginning of the symbol table.
91   inline iterator begin() { return vmap.begin(); }
92
93   /// @brief Get a const_iterator that from the beginning of the symbol table.
94   inline const_iterator begin() const { return vmap.begin(); }
95
96   /// @brief Get an iterator to the end of the symbol table.
97   inline iterator end() { return vmap.end(); }
98
99   /// @brief Get a const_iterator to the end of the symbol table.
100   inline const_iterator end() const { return vmap.end(); }
101   
102 /// @}
103 /// @name Mutators
104 /// @{
105 private:
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   /// If the inserted name conflicts, this renames the value.
109   /// @brief Add a named value to the symbol table
110   void reinsertValue(Value *V);
111     
112   /// createValueName - This method attempts to create a value name and insert
113   /// it into the symbol table with the specified name.  If it conflicts, it
114   /// auto-renames the name and returns that instead.
115   ValueName *createValueName(const StringRef &Name, Value *V);
116   
117   /// This method removes a value from the symbol table.  It leaves the
118   /// ValueName attached to the value, but it is no longer inserted in the
119   /// symtab.
120   void removeValueName(ValueName *V);
121   
122 /// @}
123 /// @name Internal Data
124 /// @{
125 private:
126   ValueMap vmap;                    ///< The map that holds the symbol table.
127   mutable uint32_t LastUnique; ///< Counter for tracking unique names
128
129 /// @}
130 };
131
132 } // End llvm namespace
133
134 #endif