LLVM  mainline
ValueSymbolTable.h
Go to the documentation of this file.
00001 //===-- llvm/ValueSymbolTable.h - Implement a Value Symtab ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the name/Value symbol table for LLVM.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_IR_VALUESYMBOLTABLE_H
00015 #define LLVM_IR_VALUESYMBOLTABLE_H
00016 
00017 #include "llvm/ADT/SmallString.h"
00018 #include "llvm/ADT/StringMap.h"
00019 #include "llvm/IR/Value.h"
00020 #include "llvm/Support/DataTypes.h"
00021 
00022 namespace llvm {
00023   template <typename ValueSubClass> class SymbolTableListTraits;
00024   class BasicBlock;
00025   class Function;
00026   class NamedMDNode;
00027   class Module;
00028   class StringRef;
00029 
00030 /// This class provides a symbol table of name/value pairs. It is essentially
00031 /// a std::map<std::string,Value*> but has a controlled interface provided by
00032 /// LLVM as well as ensuring uniqueness of names.
00033 ///
00034 class ValueSymbolTable {
00035   friend class Value;
00036   friend class SymbolTableListTraits<Argument>;
00037   friend class SymbolTableListTraits<BasicBlock>;
00038   friend class SymbolTableListTraits<Instruction>;
00039   friend class SymbolTableListTraits<Function>;
00040   friend class SymbolTableListTraits<GlobalVariable>;
00041   friend class SymbolTableListTraits<GlobalAlias>;
00042 /// @name Types
00043 /// @{
00044 public:
00045   /// @brief A mapping of names to values.
00046   typedef StringMap<Value*> ValueMap;
00047 
00048   /// @brief An iterator over a ValueMap.
00049   typedef ValueMap::iterator iterator;
00050 
00051   /// @brief A const_iterator over a ValueMap.
00052   typedef ValueMap::const_iterator const_iterator;
00053 
00054 /// @}
00055 /// @name Constructors
00056 /// @{
00057 public:
00058   ValueSymbolTable() : vmap(0), LastUnique(0) {}
00059   ~ValueSymbolTable();
00060 
00061 /// @}
00062 /// @name Accessors
00063 /// @{
00064 public:
00065   /// This method finds the value with the given \p Name in the
00066   /// the symbol table.
00067   /// @returns the value associated with the \p Name
00068   /// @brief Lookup a named Value.
00069   Value *lookup(StringRef Name) const { return vmap.lookup(Name); }
00070 
00071   /// @returns true iff the symbol table is empty
00072   /// @brief Determine if the symbol table is empty
00073   inline bool empty() const { return vmap.empty(); }
00074 
00075   /// @brief The number of name/type pairs is returned.
00076   inline unsigned size() const { return unsigned(vmap.size()); }
00077 
00078   /// This function can be used from the debugger to display the
00079   /// content of the symbol table while debugging.
00080   /// @brief Print out symbol table on stderr
00081   void dump() const;
00082 
00083 /// @}
00084 /// @name Iteration
00085 /// @{
00086 public:
00087   /// @brief Get an iterator that from the beginning of the symbol table.
00088   inline iterator begin() { return vmap.begin(); }
00089 
00090   /// @brief Get a const_iterator that from the beginning of the symbol table.
00091   inline const_iterator begin() const { return vmap.begin(); }
00092 
00093   /// @brief Get an iterator to the end of the symbol table.
00094   inline iterator end() { return vmap.end(); }
00095 
00096   /// @brief Get a const_iterator to the end of the symbol table.
00097   inline const_iterator end() const { return vmap.end(); }
00098 
00099   /// @}
00100   /// @name Mutators
00101   /// @{
00102 private:
00103   ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
00104 
00105   /// This method adds the provided value \p N to the symbol table.  The Value
00106   /// must have a name which is used to place the value in the symbol table.
00107   /// If the inserted name conflicts, this renames the value.
00108   /// @brief Add a named value to the symbol table
00109   void reinsertValue(Value *V);
00110 
00111   /// createValueName - This method attempts to create a value name and insert
00112   /// it into the symbol table with the specified name.  If it conflicts, it
00113   /// auto-renames the name and returns that instead.
00114   ValueName *createValueName(StringRef Name, Value *V);
00115 
00116   /// This method removes a value from the symbol table.  It leaves the
00117   /// ValueName attached to the value, but it is no longer inserted in the
00118   /// symtab.
00119   void removeValueName(ValueName *V);
00120 
00121   /// @}
00122   /// @name Internal Data
00123   /// @{
00124 private:
00125   ValueMap vmap;                    ///< The map that holds the symbol table.
00126   mutable uint32_t LastUnique; ///< Counter for tracking unique names
00127 
00128 /// @}
00129 };
00130 
00131 } // End llvm namespace
00132 
00133 #endif