For PR411:
[oota-llvm.git] / include / llvm / SymbolTable.h
index 1ea5739a2197a3fdd83085474f3b0e0bf10a2038..6451f9c8e22ec0c7bf603b11c2fd4cb9941b4140 100644 (file)
@@ -1,22 +1,14 @@
 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ----*- C++ -*-===//
-// 
-//                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
-//===----------------------------------------------------------------------===//
+//                     The LLVM Compiler Infrastructure
 //
-// This file implements a symbol table that has planed broken up by type.  
-// Identical types may have overlapping symbol names as long as they are 
-// distinct.
+// This file was developed by the LLVM research group and re-written by Reid
+// Spencer. It is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
-// Note that this implements a chained symbol table.  If a name being 'lookup'd
-// isn't found in the current symbol table, then the parent symbol table is 
-// searched.
+//===----------------------------------------------------------------------===//
 //
-// This chaining behavior does NOT affect iterators though: only the lookup 
-// method.
+// This file implements the main symbol table for LLVM.
 //
 //===----------------------------------------------------------------------===//
 
 #define LLVM_SYMBOL_TABLE_H
 
 #include "llvm/Value.h"
+#include "llvm/Support/DataTypes.h"
 #include <map>
 
-class SymbolTable : public AbstractTypeUser,
-                   public std::map<const Type *, 
-                                    std::map<const std::string, Value *> > {
+namespace llvm {
+
+/// This class provides a symbol table of name/value pairs that is broken
+/// up by type. For each Type* there is a "plane" of name/value pairs in
+/// the symbol table.  Identical types may have overlapping symbol names as
+/// long as they are distinct. The SymbolTable also tracks,  separately, a
+/// map of name/type pairs. This allows types to be named. Types are treated
+/// distinctly from Values.
+///
+/// The SymbolTable provides several utility functions for answering common
+/// questions about its contents as well as an iterator interface for
+/// directly iterating over the contents. To reduce confusion, the terms
+/// "type", "value", and "plane" are used consistently. For example,
+/// There is a TypeMap typedef that is the mapping of names to Types.
+/// Similarly there is a ValueMap typedef that is the mapping of
+/// names to Values. Finally, there is a PlaneMap typedef that is the
+/// mapping of types to planes of ValueMap. This is the basic structure
+/// of the symbol table. When you call type_begin() you're asking
+/// for an iterator at the start of the TypeMap. When you call
+/// plane_begin(), you're asking for an iterator at the start of
+/// the PlaneMap. Finally, when you call value_begin(), you're asking
+/// for an iterator at the start of a ValueMap for a specific type
+/// plane.
+class SymbolTable : public AbstractTypeUser {
+
+/// @name Types
+/// @{
 public:
-  typedef std::map<const std::string, Value *> VarMap;
-  typedef std::map<const Type *, VarMap> super;
+  /// @brief A mapping of names to values.
+  typedef std::map<const std::string, Value *> ValueMap;
 
-  typedef VarMap::iterator type_iterator;
-  typedef VarMap::const_iterator type_const_iterator;
+  /// @brief An iterator over a ValueMap.
+  typedef ValueMap::iterator value_iterator;
+
+  /// @brief A const_iterator over a ValueMap.
+  typedef ValueMap::const_iterator value_const_iterator;
+
+  /// @brief A mapping of types to names to values (type planes).
+  typedef std::map<const Type *, ValueMap> PlaneMap;
+
+  /// @brief An iterator over the type planes.
+  typedef PlaneMap::iterator plane_iterator;
+
+  /// @brief A const_iterator over the type planes
+  typedef PlaneMap::const_iterator plane_const_iterator;
+
+/// @}
+/// @name Constructors
+/// @{
+public:
 
-  inline SymbolTable() : InternallyInconsistent(false) {}
+  SymbolTable() : LastUnique(0) {}
   ~SymbolTable();
 
-  // lookup - Returns null on failure...
-  Value *lookup(const Type *Ty, const std::string &name);
+/// @}
+/// @name Accessors
+/// @{
+public:
 
-  // insert - Add named definition to the symbol table...
-  inline void insert(Value *N) {
-    assert(N->hasName() && "Value must be named to go into symbol table!");
-    insertEntry(N->getName(), N->getType(), N);
-  }
+  /// This method finds the value with the given \p name in the
+  /// type plane \p Ty and returns it. This method will not find any
+  /// Types, only Values. Use lookupType to find Types by name.
+  /// @returns null on failure, otherwise the Value associated with
+  /// the \p name in type plane \p Ty.
+  /// @brief Lookup a named, typed value.
+  Value *lookup(const Type *Ty, const std::string &name) const;
 
-  void remove(Value *N);
-  Value *type_remove(const type_iterator &It) {
-    return removeEntry(find(It->second->getType()), It);
-  }
+  /// @returns true iff the type map and the type plane are both not
+  /// empty.
+  /// @brief Determine if the symbol table is empty
+  inline bool isEmpty() const { return pmap.empty(); }
 
-  // insert - Insert a constant or type into the symbol table with the specified
-  // name...  There can be a many to one mapping between names and
-  // (constant/type)s.
-  //
-  inline void insert(const std::string &Name, Value *V) {
-    assert((isa<Type>(V) || isa<Constant>(V)) &&
-          "Can only insert types and constants here!");
-    insertEntry(Name, V->getType(), V);
-  }
+  /// Given a base name, return a string that is either equal to it or
+  /// derived from it that does not already occur in the symbol table
+  /// for the specified type.
+  /// @brief Get a name unique to this symbol table
+  std::string getUniqueName(const Type *Ty,
+                            const std::string &BaseName) const;
 
-  /// remove - Remove a constant or type from the symbol table with the
-  /// specified name.
-  Value *remove(const std::string &Name, Value *V) {
-    iterator TI = find(V->getType());
-    return removeEntry(TI, TI->second.find(Name));
-  }
+  /// This function can be used from the debugger to display the
+  /// content of the symbol table while debugging.
+  /// @brief Print out symbol table on stderr
+  void dump() const;
 
-  // getUniqueName - Given a base name, return a string that is either equal to
-  // it (or derived from it) that does not already occur in the symbol table for
-  // the specified type.
-  //
-  std::string getUniqueName(const Type *Ty, const std::string &BaseName);
+/// @}
+/// @name Iteration
+/// @{
+public:
 
-  inline unsigned type_size(const Type *TypeID) const {
-    return find(TypeID)->second.size();
+  /// Get an iterator that starts at the beginning of the type planes.
+  /// The iterator will iterate over the Type/ValueMap pairs in the
+  /// type planes.
+  inline plane_iterator plane_begin() { return pmap.begin(); }
+
+  /// Get a const_iterator that starts at the beginning of the type
+  /// planes.  The iterator will iterate over the Type/ValueMap pairs
+  /// in the type planes.
+  inline plane_const_iterator plane_begin() const { return pmap.begin(); }
+
+  /// Get an iterator at the end of the type planes. This serves as
+  /// the marker for end of iteration over the type planes.
+  inline plane_iterator plane_end() { return pmap.end(); }
+
+  /// Get a const_iterator at the end of the type planes. This serves as
+  /// the marker for end of iteration over the type planes.
+  inline plane_const_iterator plane_end() const { return pmap.end(); }
+
+  /// Get an iterator that starts at the beginning of a type plane.
+  /// The iterator will iterate over the name/value pairs in the type plane.
+  /// @note The type plane must already exist before using this.
+  inline value_iterator value_begin(const Type *Typ) {
+    assert(Typ && "Can't get value iterator with null type!");
+    return pmap.find(Typ)->second.begin();
   }
 
-  // Note that type_begin / type_end only work if you know that an element of 
-  // TypeID is already in the symbol table!!!
-  //
-  inline type_iterator type_begin(const Type *TypeID) { 
-    return find(TypeID)->second.begin(); 
+  /// Get a const_iterator that starts at the beginning of a type plane.
+  /// The iterator will iterate over the name/value pairs in the type plane.
+  /// @note The type plane must already exist before using this.
+  inline value_const_iterator value_begin(const Type *Typ) const {
+    assert(Typ && "Can't get value iterator with null type!");
+    return pmap.find(Typ)->second.begin();
   }
-  inline type_const_iterator type_begin(const Type *TypeID) const {
-    return find(TypeID)->second.begin(); 
+
+  /// Get an iterator to the end of a type plane. This serves as the marker
+  /// for end of iteration of the type plane.
+  /// @note The type plane must already exist before using this.
+  inline value_iterator value_end(const Type *Typ) {
+    assert(Typ && "Can't get value iterator with null type!");
+    return pmap.find(Typ)->second.end();
   }
 
-  inline type_iterator type_end(const Type *TypeID) { 
-    return find(TypeID)->second.end(); 
+  /// Get a const_iterator to the end of a type plane. This serves as the
+  /// marker for end of iteration of the type plane.
+  /// @note The type plane must already exist before using this.
+  inline value_const_iterator value_end(const Type *Typ) const {
+    assert(Typ && "Can't get value iterator with null type!");
+    return pmap.find(Typ)->second.end();
   }
-  inline type_const_iterator type_end(const Type *TypeID) const { 
-    return find(TypeID)->second.end(); 
+
+  /// This method returns a plane_const_iterator for iteration over
+  /// the type planes starting at a specific plane, given by \p Ty.
+  /// @brief Find a type plane.
+  inline plane_const_iterator find(const Type* Typ) const {
+    assert(Typ && "Can't find type plane with null type!");
+    return pmap.find(Typ);
   }
 
-  void dump() const;  // Debug method, print out symbol table
+  /// This method returns a plane_iterator for iteration over the
+  /// type planes starting at a specific plane, given by \p Ty.
+  /// @brief Find a type plane.
+  inline plane_iterator find(const Type* Typ) {
+    assert(Typ && "Can't find type plane with null type!");
+    return pmap.find(Typ);
+  }
 
-private:
-  // InternallyInconsistent - There are times when the symbol table is
-  // internally inconsistent with the rest of the program.  In this one case, a
-  // value exists with a Name, and it's not in the symbol table.  When we call
-  // V->setName(""), it tries to remove itself from the symbol table and dies.
-  // We know this is happening, and so if the flag InternallyInconsistent is
-  // set, removal from the symbol table is a noop.
-  //
-  bool InternallyInconsistent;
-
-  inline super::value_type operator[](const Type *Ty) {
-    assert(0 && "Should not use this operator to access symbol table!");
-    return super::value_type();
+
+/// @}
+/// @name Mutators
+/// @{
+public:
+
+  /// This method will strip the symbol table of its names leaving the type and
+  /// values.
+  /// @brief Strip the symbol table.
+  bool strip();
+
+/// @}
+/// @name Mutators used by Value::setName and other LLVM internals.
+/// @{
+public:
+
+  /// This method adds the provided value \p N to the symbol table.  The Value
+  /// must have both a name and a type which are extracted and used to place the
+  /// value in the correct type plane under the value's name.
+  /// @brief Add a named value to the symbol table
+  inline void insert(Value *Val) {
+    assert(Val && "Can't insert null type into symbol table!");
+    assert(Val->hasName() && "Value must be named to go into symbol table!");
+    insertEntry(Val->getName(), Val->getType(), Val);
   }
 
-  // insertEntry - Insert a value into the symbol table with the specified
-  // name...
-  //
-  void insertEntry(const std::string &Name, const Type *Ty, Value *V);
+  /// This method removes a named value from the symbol table. The type and name
+  /// of the Value are extracted from \p N and used to lookup the Value in the
+  /// correct type plane. If the Value is not in the symbol table, this method
+  /// silently ignores the request.
+  /// @brief Remove a named value from the symbol table.
+  void remove(Value* Val);
+
+  /// changeName - Given a value with a non-empty name, remove its existing
+  /// entry from the symbol table and insert a new one for Name.  This is
+  /// equivalent to doing "remove(V), V->Name = Name, insert(V)", but is faster,
+  /// and will not temporarily remove the symbol table plane if V is the last
+  /// value in the symtab with that name (which could invalidate iterators to
+  /// that plane).
+  void changeName(Value *V, const std::string &Name);
 
-  // removeEntry - Remove a value from the symbol table...
-  //
-  Value *removeEntry(iterator Plane, type_iterator Entry);
+/// @}
+/// @name Internal Methods
+/// @{
+private:
+  /// @brief Insert a value into the symbol table with the specified name.
+  void insertEntry(const std::string &Name, const Type *Ty, Value *V);
 
-  // This function is called when one of the types in the type plane are refined
+  /// This function is called when one of the types in the type plane
+  /// is refined.
   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
+
+  /// This function markes a type as being concrete (defined).
   virtual void typeBecameConcrete(const DerivedType *AbsTy);
+
+/// @}
+/// @name Internal Data
+/// @{
+private:
+
+  /// This is the main content of the symbol table. It provides
+  /// separate type planes for named values. That is, each named
+  /// value is organized into a separate dictionary based on
+  /// Type. This means that the same name can be used for different
+  /// types without conflict.
+  /// @brief The mapping of types to names to values.
+  PlaneMap pmap;
+
+  /// This value is used to retain the last unique value used
+  /// by getUniqueName to generate unique names.
+  mutable uint32_t LastUnique;
+/// @}
+
 };
 
+} // End llvm namespace
+
+// vim: sw=2
+
 #endif
+