[llvm-commits] [bug_122] CVS: llvm/include/llvm/SymbolTable.h

LLVM llvm at cs.uiuc.edu
Sun May 16 18:24:01 PDT 2004


Changes in directory llvm/include/llvm:

SymbolTable.h updated: 1.30.6.1 -> 1.30.6.2

---
Log message:

Revisions to support a separate map of name/type pairs and clarification of
the interface of the SymbolTable. Three sets of iterators are now possible:
(1) Over the type planes of values (plane_*), (2) Over the values in a type 
plane (value_*), and (3) Over the named types (type_*).


---
Diffs of the changes:  (+165 -178)

Index: llvm/include/llvm/SymbolTable.h
diff -u llvm/include/llvm/SymbolTable.h:1.30.6.1 llvm/include/llvm/SymbolTable.h:1.30.6.2
--- llvm/include/llvm/SymbolTable.h:1.30.6.1	Wed May 12 11:33:43 2004
+++ llvm/include/llvm/SymbolTable.h	Sun May 16 18:24:36 2004
@@ -20,12 +20,16 @@
 namespace llvm {
 
 /// This class provides a symbol table of name/value pairs that is broken
-/// up by type. Each Type* represents a "type plane" in the symbol table.
-/// Identical types may have overlapping symbol names as long as they are
-/// distinct. Note that this implements a chained symbol table. If a name
+/// 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 since Types are not Values.
+/// 
+/// Note that the value map implements a chained symbol table. If a name
 /// being looked up (with lookup()) 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.
+/// does NOT affect the iterators though: only the lookup method.
 ///
 /// The SymbolTable provides several utility functions for answering common
 /// questions about its contents as well as an iterator interface for
@@ -37,11 +41,8 @@
 /// @{
 public:
 
-  /// @brief A mapping of names to values
-  typedef std::map<const std::string, Value *> ValueMap;
-
-  /// @brief A mapping of types to names to values
-  typedef std::map<const Type *, ValueMap> TypeMap;
+  /// @brief A mapping of names to types
+  typedef std::map<const std::string, Type*> TypeMap;
 
   /// @brief An iterator over the TypeMap
   typedef TypeMap::iterator type_iterator;
@@ -49,12 +50,24 @@
   /// @brief A const_iterator over the TypeMap
   typedef TypeMap::const_iterator type_const_iterator;
 
-  /// @brief A iterator over a ValueMap
+  /// @brief A mapping of names to values
+  typedef std::map<const std::string, Value *> ValueMap;
+
+  /// @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> TypePlaneMap;
+
+  /// @brief An iterator over the type planes
+  typedef TypePlaneMap::iterator plane_iterator;
+
+  /// @brief A const_iterator over the type planes
+  typedef TypePlaneMap::const_iterator plane_const_iterator;
+
 /// @}
 /// @name Constructors
 /// @{
@@ -69,31 +82,47 @@
 public:
 
   /// This method finds the value with the given \p name in the
-  /// type plane \p Ty and returns it.
+  /// 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;
 
+  /// This method finds the type with the given \p name in the
+  /// type  map and returns it.
+  /// @returns null if the name is not found, otherwise the Type
+  /// associated with the \p name.
+  /// @brief Lookup a type by name.
+  Type* lookupType( const std::string& name ) const;
+
+  /// @returns true iff the type map is not empty.
   /// @brief Determine if there are types in the symbol table
-  bool hasTypes( void ) const; 
+  inline bool hasTypes() const { return ! tmap.empty(); }
 
+  /// @returns true iff the type map and the type plane are both not 
+  /// empty.
   /// @brief Determine if the symbol table is empty
-  bool isEmpty( void ) const;
+  inline bool isEmpty() const { return pmap.empty() && tmap.empty(); }
 
-  /// The type plane associated with the \p TypeID parameter is
-  /// found and the number of entries in the plane is returned.
-  /// @returns Number of entries in the specified type plane.
+  /// The plane associated with the \p TypeID parameter is found
+  /// and the number of entries in the plane is returned.
+  /// @returns Number of entries in the specified type plane or 0.
   /// @brief Get the size of a type plane.
-  inline unsigned type_size(const Type *TypeID) const {
-    return map.find(TypeID)->second.size();
-  }
+  inline unsigned type_size(const Type *TypeID) const; 
+
+  /// The number of name/type pairs is returned.
+  inline unsigned num_types() const { return tmap.size(); }
 
   /// Finds the value \p val in the symbol table and returns its
   /// name. Only the type plane associated with the type of \p val
   /// is searched.
   /// @brief Return the name of a value
-  std::string get_name( const Value* val ) const;
+  std::string get_name( const Value* Val ) const;
+
+  /// Finds the type \p Ty in the symbol table and returns its name.
+  /// @brief Return the name of a type
+  std::string get_name( const Type* Ty ) const;
 
   /// 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 
@@ -102,7 +131,9 @@
   std::string getUniqueName(const Type *Ty, 
     const std::string &BaseName) const;
 
-  /// @brief Debug method, print out symbol table
+  /// 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;  
 
 /// @}
@@ -126,140 +157,165 @@
   /// @brief Insert a constant or type.
   inline void insert(const std::string &Name, Value *V) {
     assert((isa<Type>(V) || isa<Constant>(V)) &&
-	   "Can only insert types and constants into a symbol table!");
+           "Can only insert types and constants into a symbol table!");
     insertEntry(Name, V->getType(), V);
   }
 
+  /// Inserts a type into the symbol table with the specified name. There
+  /// can be a many-to-one mapping between names and types. This method
+  /// allows a type with an existing entry in the symbol table to get
+  /// a new name.
+  /// @brief Insert a type under a new name.
+  inline void insert(const std::string &Name, Type *T) {
+    insertEntry(Name, T );
+  }
+
   /// This method removes a named value from the symbol table. The
-  /// type and name of the Value are extracted from \N and used to
+  /// 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 *N);
+  void remove(Value* N);
+
+  /// This method removes a named type from the symbol table. The
+  /// name of the type is extracted from \P T and used to look up
+  /// the Type in the type map. If the Type is not in the symbol
+  /// table, this method silently ignores the request.
+  /// @brief Remove a named type from the symbol table.
+  void remove(Type* T );
 
   /// Remove a constant or type with the specified name from the 
   /// symbol table.
   /// @returns the removed Value.
-  /// @breif Remove a constant or type from the symbol table.
-  Value *remove(const std::string &Name, Value *V) {
-      TypeMap::iterator TI = map.find(V->getType());
-    return removeEntry(TI, TI->second.find(Name));
+  /// @brief Remove a constant or type from the symbol table.
+  inline Value* remove(const std::string &Name, Value *V) {
+    plane_iterator PI = pmap.find(V->getType());
+    return removeEntry(PI, PI->second.find(Name));
   }
 
-  /// @brief Strip the symbol table. This leaves the values without names.
-  bool strip( void );
+  /// Remove a type with the specified name from the symbol table.
+  /// @returns the removed Type.
+  /// @brief Remove a named tyep from the symbol table.
+  inline Type* remove(const std::string& Name, Type* T ) {
+    return removeEntry( tmap.find(Name) );
+  }
 
-  /// @brief Empty the symbol table completely
-  void clear( void ) { map.clear(); }
+  /// Removes a specific value from the symbol table. 
+  /// @returns the removed value.
+  /// @brief Remove a specific value given by an iterator
+  inline Value *value_remove(const value_iterator &It) {
+    return this->removeEntry(pmap.find(It->second->getType()), It);
+  }
+
+  /// This method will strip the symbol table of its names leaving
+  /// the type and values. 
+  /// @brief Strip the symbol table. 
+  bool strip();
+
+  /// @brief Empty the symbol table completely.
+  inline void clear() { pmap.clear(); tmap.clear(); }
 
 /// @}
 /// @name Iteration
 /// @{
 public:
 
-  inline type_iterator begin() { return map.begin(); }
-
-  inline type_const_iterator begin() const { return map.begin(); }
-
-  inline type_iterator end() { return map.end(); }
-
-  inline type_const_iterator end() const { return map.end(); }
+  /// 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 type_begin(const Type *TypeID) { 
-    return map.find(TypeID)->second.begin(); 
+  inline value_iterator value_begin(const Type *TypeID) { 
+    return pmap.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 type_begin(const Type *TypeID) const {
-    return map.find(TypeID)->second.begin(); 
+  inline value_const_iterator value_begin(const Type *TypeID) const {
+    return pmap.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 type_end(const Type *TypeID) { 
-    return map.find(TypeID)->second.end(); 
+  inline value_iterator value_end(const Type *TypeID) { 
+    return pmap.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 type_end(const Type *TypeID) const { 
-    return map.find(TypeID)->second.end(); 
+  inline value_const_iterator value_end(const Type *TypeID) const { 
+    return pmap.find(TypeID)->second.end(); 
   }
 
-  /// This method returns a type_const_iterator for iteration over
+  /// Get an iterator to the start of the name/Type map.
+  inline type_iterator type_begin() { return tmap.begin(); }
+
+  /// Get a const_iterator to the start of the name/Type map.
+  inline type_const_iterator type_begin() const { return tmap.begin(); }
+
+  /// Get an iterator to the end of the name/Type map. This serves as the
+  /// marker for end of iteration of the types.
+  inline type_iterator type_end() { return tmap.end(); }
+
+  /// Get a const-iterator to the end of the name/Type map. This serves 
+  /// as the marker for end of iteration of the types.
+  inline type_const_iterator type_end() const { return tmap.end(); }
+
+  /// This method returns a plane_const_iterator for iteration over
   /// the type planes.
   /// @brief Find a type plane.
-  type_const_iterator find(const Type* Ty ) const {
-    return map.find( Ty );
+  inline plane_const_iterator find(const Type* Ty ) const {
+    return pmap.find( Ty );
   }
 
-  type_iterator find( const Type* Ty ) { return map.find(Ty); }
-
-  /// @brief Find a Type Plane
-  const ValueMap* findPlane( const Type* Ty ) const {
-    type_const_iterator I = map.find( Ty );
-    if ( I == map.end() ) return 0;
+  /// This method returns a plane_iterator for iteration over the
+  /// type planes.
+  /// @breif Find a type plane.
+  inline plane_iterator find( const Type* Ty ) { return pmap.find(Ty); }
+
+  /// This method returns a ValueMap* for a specific type plane.
+  /// @brief Find a type plane
+  inline const ValueMap* findPlane( const Type* Ty ) const {
+    plane_const_iterator I = pmap.find( Ty );
+    if ( I == pmap.end() ) return 0;
     return &I->second;
   }
 
-  /// @returns the removed value.
-  /// @brief Remove a specific value given by an iterator
-  Value *type_remove(const value_iterator &It) {
-    return this->removeEntry(map.find(It->second->getType()), It);
-  }
-
-/// @}
-/// @name Applicators
-/// @{
-public:
-  /// This function calls the functor doIt for each type in the Symbol
-  /// Table. The arguments to the functor are the name and type.
-  /// @brief Apply a functor to the types in the Symbol Table.
-  // template<typename FUNCTOR>
-  // void applyToTypes(FUNCTOR& doIt) const;
-
-  /// This function calls the functor doIt for each type in the Symbol
-  /// Table that has the specified name. If the functor returns true,
-  /// the search stops.
-  //template<typename FUNCTOR>
-  //void findTypeNamed( FUNCTOR& doIt, const std::string& Name) const;
-
-  /// This function calls the functor doIt for each value in the Symbol
-  /// Table. The arguments to the functor are the type, name and value.
-  //template<typename FUNCTOR>
-  //void apply( FUNCTOR& doIt ) const;
-
-  /// This function removes any Types for which the functor \p decide
-  /// returns true.
-  /// @returns the number of types removed
-  //template<typename FUNCTOR>
-  //unsigned removeMatchingTypes( FUNCTOR& decide );
-
-  /// This function replaces the named value of each entry in the
-  /// Type plane with value provided by the replace functor. The functor
-  /// should return the new Value* or null if no replacement should be
-  /// made.
-  /// @returns the number of replacements made.
-  //template<typename FUNCTOR>
-  //unsigned replaceTypes( FUNCTOR& replace );
-
 /// @}
 /// @name Internal Methods
 /// @{
 private:
   /// Insert a value into the symbol table with the specified name.
   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
+  void insertEntry(const std::string &Name, Type *T);
+
+  /// Remove a specific value from a specific plane in the SymbolTable.
+  /// @returns the removed Value.
+  Value* removeEntry(plane_iterator Plane, value_iterator Entry);
 
-  /// Remove a value from the symbol table...
-  Value *removeEntry(TypeMap::iterator Plane, value_iterator Entry);
+  /// Remove a specific type from the SymbolTable.
+  /// @returns the removed Type.
+  Type*  removeEntry(type_iterator Entry);
 
   /// This function is called when one of the types in the type plane 
   /// is refined.
@@ -277,9 +333,15 @@
   /// 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.
+  /// types without conflict. Note that the Type::TypeTy plane is
+  /// not stored in this map but is in tmap.
   /// @brief The mapping of types to names to values.
-  TypeMap map;
+  TypePlaneMap pmap;
+
+  /// This is the Type::TypeTy plane. It is separated from the pmap
+  /// because the elements of the map are name/Type pairs not 
+  /// name/Value pairs and Type is not a Value.
+  TypeMap tmap;
 
   /// 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, 
@@ -298,84 +360,9 @@
 
 };
 
-/*
-template<typename FUNCTOR>
-void SymbolTable::applyToTypes( FUNCTOR& doIt ) const {
-  type_const_iterator TI = map.find(Type::TypeTy);
-  if (TI != map.end()) {
-    value_const_iterator VI = TI->second.begin();
-    for (; VI != TI->second.end(); ++VI) {
-      doIt( Type::TypeTy, VI->first, VI->second );
-    }
-  }
-}
-
-template<typename FUNCTOR>
-void SymbolTable::findTypeNamed( FUNCTOR& decide, 
-  const std::string& Name ) const {
-  type_const_iterator TI = map.begin();
-  type_const_iterator TE = map.end();
-  for ( ; TI != TE; ++TI ) {
-    if (TI->first != Type::TypeTy) {
-      const ValueMap& VM = TI->second;
-      // Does this type plane contain an entry with the specified name?
-      value_const_iterator VI = VM.find(Name);
-      if (VI != VM.end()) {
-	if ( decide( TI->first, VI->first, VI->second ) ) break;
-      }
-    }
-  }
-}
-  
-template<typename FUNCTOR>
-void SymbolTable::apply( FUNCTOR& doIt ) const {
-  for (type_const_iterator TI = map.begin(), TE = map.end(); 
-    TI != TE; ++TI) {
-    for (value_const_iterator VI = TI->second.begin(), 
-	   VE = TI->second.end(); VI != VE; ++VI) {
-      doIt( TI->first, VI->first, VI->second );
-    }
-  }
-}
-
-template<typename FUNCTOR>
-unsigned SymbolTable::removeMatchingTypes( FUNCTOR& decide ) {
-  unsigned result = 0;
-  type_iterator PI = map.find(Type::TypeTy);
-  if (PI != map.end()) {
-    ValueMap& Plane = PI->second;
-    for (value_iterator I=Plane.begin(); I != Plane.end(); ++I) {
-      if ( decide( PI->first, I->first, I->second ) )
-      {
-	Plane.erase( I++ );
-	result++;
-      }
-    }
-  }
-  return result;
-}
-
-template<typename FUNCTOR>
-unsigned SymbolTable::replaceTypes( FUNCTOR& replace ) {
-  unsigned result = 0;
-  type_iterator PI = map.find(Type::TypeTy);
-  if (PI != map.end()) {
-    ValueMap& Plane = PI->second;
-    for (value_iterator I=Plane.begin(), PE=Plane.end(); I != PE; ++I) {
-      Value* v = replace( PI->first, I->first, I->second );
-      if ( v ) {
-	I->second = v;
-	result++;
-	result++;
-      }
-    }
-  }
-  return result;
-}
-*/
-
 } // End llvm namespace
 
+// vim: sw=2
+
 #endif
 
-// vim: sw=2





More information about the llvm-commits mailing list