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