LLVM API Documentation

TypeSymbolTable.h

Go to the documentation of this file.
00001 //===-- llvm/TypeSymbolTable.h - Implement a Type Symtab --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by Reid Spencer.  It is distributed under the 
00006 // University of Illinois Open Source License.  See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the name/type symbol table for LLVM.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_TYPE_SYMBOL_TABLE_H
00015 #define LLVM_TYPE_SYMBOL_TABLE_H
00016 
00017 #include "llvm/Type.h"
00018 #include <map>
00019 
00020 namespace llvm {
00021 
00022 /// This class provides a symbol table of name/type pairs with operations to
00023 /// support constructing, searching and iterating over the symbol table. The
00024 /// class derives from AbstractTypeUser so that the contents of the symbol
00025 /// table can be updated when abstract types become concrete.
00026 class TypeSymbolTable : public AbstractTypeUser {
00027 
00028 /// @name Types
00029 /// @{
00030 public:
00031 
00032   /// @brief A mapping of names to types.
00033   typedef std::map<const std::string, const Type*> TypeMap;
00034 
00035   /// @brief An iterator over the TypeMap.
00036   typedef TypeMap::iterator iterator;
00037 
00038   /// @brief A const_iterator over the TypeMap.
00039   typedef TypeMap::const_iterator const_iterator;
00040 
00041 /// @}
00042 /// @name Constructors
00043 /// @{
00044 public:
00045 
00046   TypeSymbolTable() {}
00047   ~TypeSymbolTable();
00048 
00049 /// @}
00050 /// @name Accessors
00051 /// @{
00052 public:
00053 
00054   /// Generates a unique name for a type based on the \p BaseName by
00055   /// incrementing an integer and appending it to the name, if necessary
00056   /// @returns the unique name
00057   /// @brief Get a unique name for a type
00058   std::string getUniqueName(const std::string &BaseName) const;
00059 
00060   /// This method finds the type with the given \p name in the type map
00061   /// and returns it.
00062   /// @returns null if the name is not found, otherwise the Type
00063   /// associated with the \p name.
00064   /// @brief Lookup a type by name.
00065   Type* lookup(const std::string& name) const;
00066 
00067   /// @returns true iff the symbol table is empty.
00068   /// @brief Determine if the symbol table is empty
00069   inline bool empty() const { return tmap.empty(); }
00070 
00071   /// @returns the size of the symbol table
00072   /// @brief The number of name/type pairs is returned.
00073   inline unsigned size() const { return unsigned(tmap.size()); }
00074 
00075   /// This function can be used from the debugger to display the
00076   /// content of the symbol table while debugging.
00077   /// @brief Print out symbol table on stderr
00078   void dump() const;
00079 
00080 /// @}
00081 /// @name Iteration
00082 /// @{
00083 public:
00084   /// Get an iterator to the start of the symbol table
00085   inline iterator begin() { return tmap.begin(); }
00086 
00087   /// @brief Get a const_iterator to the start of the symbol table
00088   inline const_iterator begin() const { return tmap.begin(); }
00089 
00090   /// Get an iterator to the end of the symbol talbe. 
00091   inline iterator end() { return tmap.end(); }
00092 
00093   /// Get a const_iterator to the end of the symbol table.
00094   inline const_iterator end() const { return tmap.end(); }
00095 
00096 /// @}
00097 /// @name Mutators
00098 /// @{
00099 public:
00100 
00101   /// This method will strip the symbol table of its names 
00102   /// @brief Strip the symbol table.
00103   bool strip();
00104 
00105   /// Inserts a type into the symbol table with the specified name. There can be
00106   /// a many-to-one mapping between names and types. This method allows a type
00107   /// with an existing entry in the symbol table to get a new name.
00108   /// @brief Insert a type under a new name.
00109   void insert(const std::string &Name, const Type *Typ);
00110 
00111   /// Remove a type at the specified position in the symbol table.
00112   /// @returns the removed Type.
00113   /// @returns the Type that was erased from the symbol table.
00114   Type* erase(iterator TI);
00115 
00116   /// Remove a specific Type from the symbol table. This isn't fast, linear
00117   /// search, O(n), algorithm.
00118   /// @returns true if the erase was successful (TI was found)
00119   bool erase(Type* TI);
00120 
00121   /// Rename a type. This ain't fast, we have to linearly search for it first.
00122   /// @returns true if the rename was successful (type was found)
00123   bool rename(Type* T, const std::string& new_name);
00124 
00125 /// @}
00126 /// @name AbstractTypeUser Methods
00127 /// @{
00128 private:
00129   /// This function is called when one of the types in the type plane
00130   /// is refined.
00131   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
00132 
00133   /// This function markes a type as being concrete (defined).
00134   virtual void typeBecameConcrete(const DerivedType *AbsTy);
00135 
00136 /// @}
00137 /// @name Internal Data
00138 /// @{
00139 private:
00140   TypeMap tmap; ///< This is the mapping of names to types.
00141   mutable unsigned long LastUnique; ///< Counter for tracking unique names
00142 
00143 /// @}
00144 
00145 };
00146 
00147 } // End llvm namespace
00148 
00149 #endif
00150