1 //===-- llvm/TypeSymbolTable.h - Implement a Type Symtab --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer based on the original SymbolTable
6 // implemented by the LLVM Research Group and re-written by Reid Spencer.
7 // It is distributed under the University of Illinois Open Source License.
8 // See LICENSE.TXT for details.
10 //===----------------------------------------------------------------------===//
12 // This file implements the name/type symbol table for LLVM.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_TYPE_SYMBOL_TABLE_H
17 #define LLVM_TYPE_SYMBOL_TABLE_H
19 #include "llvm/Type.h"
24 /// This class provides a symbol table of name/type pairs with operations to
25 /// support constructing, searching and iterating over the symbol table. The
26 /// class derives from AbstractTypeUser so that the contents of the symbol
27 /// table can be updated when abstract types become concrete.
28 class TypeSymbolTable : public AbstractTypeUser {
34 /// @brief A mapping of names to types.
35 typedef std::map<const std::string, const Type*> TypeMap;
37 /// @brief An iterator over the TypeMap.
38 typedef TypeMap::iterator iterator;
40 /// @brief A const_iterator over the TypeMap.
41 typedef TypeMap::const_iterator const_iterator;
44 /// @name Constructors
56 /// Generates a unique name for a type based on the \p BaseName by
57 /// incrementing an integer and appending it to the name, if necessary
58 /// @returns the unique name
59 /// @brief Get a unique name for a type
60 std::string getUniqueName(const std::string &BaseName) const;
62 /// This method finds the type with the given \p name in the type map
64 /// @returns null if the name is not found, otherwise the Type
65 /// associated with the \p name.
66 /// @brief Lookup a type by name.
67 Type* lookup(const std::string& name) const;
69 /// @returns true iff the symbol table is empty.
70 /// @brief Determine if the symbol table is empty
71 inline bool empty() const { return tmap.empty(); }
73 /// @returns the size of the symbol table
74 /// @brief The number of name/type pairs is returned.
75 inline unsigned size() const { return unsigned(tmap.size()); }
77 /// This function can be used from the debugger to display the
78 /// content of the symbol table while debugging.
79 /// @brief Print out symbol table on stderr
86 /// Get an iterator to the start of the symbol table
87 inline iterator begin() { return tmap.begin(); }
89 /// @brief Get a const_iterator to the start of the symbol table
90 inline const_iterator begin() const { return tmap.begin(); }
92 /// Get an iterator to the end of the symbol talbe.
93 inline iterator end() { return tmap.end(); }
95 /// Get a const_iterator to the end of the symbol table.
96 inline const_iterator end() const { return tmap.end(); }
103 /// This method will strip the symbol table of its names
104 /// @brief Strip the symbol table.
107 /// Inserts a type into the symbol table with the specified name. There can be
108 /// a many-to-one mapping between names and types. This method allows a type
109 /// with an existing entry in the symbol table to get a new name.
110 /// @brief Insert a type under a new name.
111 void insert(const std::string &Name, const Type *Typ);
113 /// Remove a type at the specified position in the symbol table.
114 /// @returns the removed Type.
115 /// @returns the Type that was erased from the symbol table.
116 Type* erase(iterator TI);
118 /// Remove a specific Type from the symbol table. This isn't fast, linear
119 /// search, O(n), algorithm.
120 /// @returns true if the erase was successful (TI was found)
121 bool erase(Type* TI);
123 /// Rename a type. This ain't fast, we have to linearly search for it first.
124 /// @returns true if the rename was successful (type was found)
125 bool rename(Type* T, const std::string& new_name);
128 /// @name AbstractTypeUser Methods
131 /// This function is called when one of the types in the type plane
133 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
135 /// This function markes a type as being concrete (defined).
136 virtual void typeBecameConcrete(const DerivedType *AbsTy);
139 /// @name Internal Data
142 TypeMap tmap; ///< This is the mapping of names to types.
143 mutable unsigned long LastUnique; ///< Counter for tracking unique names
149 } // End llvm namespace