1 //===-- llvm/TypeSymbolTable.h - Implement a Type Symtab --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer. It is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the name/type symbol table for LLVM.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TYPE_SYMBOL_TABLE_H
15 #define LLVM_TYPE_SYMBOL_TABLE_H
17 #include "llvm/Type.h"
22 /// This class provides a symbol table of name/type pairs with operations to
23 /// support constructing, searching and iterating over the symbol table. The
24 /// class derives from AbstractTypeUser so that the contents of the symbol
25 /// table can be updated when abstract types become concrete.
26 class TypeSymbolTable : public AbstractTypeUser {
32 /// @brief A mapping of names to types.
33 typedef std::map<const std::string, const Type*> TypeMap;
35 /// @brief An iterator over the TypeMap.
36 typedef TypeMap::iterator iterator;
38 /// @brief A const_iterator over the TypeMap.
39 typedef TypeMap::const_iterator const_iterator;
42 /// @name Constructors
54 /// Generates a unique name for a type based on the \p BaseName by
55 /// incrementing an integer and appending it to the name, if necessary
56 /// @returns the unique name
57 /// @brief Get a unique name for a type
58 std::string getUniqueName(const std::string &BaseName) const;
60 /// This method finds the type with the given \p name in the type map
62 /// @returns null if the name is not found, otherwise the Type
63 /// associated with the \p name.
64 /// @brief Lookup a type by name.
65 Type* lookup(const std::string& name) const;
67 /// @returns true iff the symbol table is empty.
68 /// @brief Determine if the symbol table is empty
69 inline bool empty() const { return tmap.empty(); }
71 /// @returns the size of the symbol table
72 /// @brief The number of name/type pairs is returned.
73 inline unsigned size() const { return unsigned(tmap.size()); }
75 /// This function can be used from the debugger to display the
76 /// content of the symbol table while debugging.
77 /// @brief Print out symbol table on stderr
84 /// Get an iterator to the start of the symbol table
85 inline iterator begin() { return tmap.begin(); }
87 /// @brief Get a const_iterator to the start of the symbol table
88 inline const_iterator begin() const { return tmap.begin(); }
90 /// Get an iterator to the end of the symbol talbe.
91 inline iterator end() { return tmap.end(); }
93 /// Get a const_iterator to the end of the symbol table.
94 inline const_iterator end() const { return tmap.end(); }
101 /// This method will strip the symbol table of its names
102 /// @brief Strip the symbol table.
105 /// Inserts a type into the symbol table with the specified name. There can be
106 /// a many-to-one mapping between names and types. This method allows a type
107 /// with an existing entry in the symbol table to get a new name.
108 /// @brief Insert a type under a new name.
109 void insert(const std::string &Name, const Type *Typ);
111 /// Remove a type at the specified position in the symbol table.
112 /// @returns the removed Type.
113 /// @returns the Type that was erased from the symbol table.
114 Type* remove(iterator TI);
116 /// Remove a specific Type from the symbol table. This isn't fast, linear
117 /// search, O(n), algorithm.
118 /// @returns true if the erase was successful (TI was found)
119 bool remove(Type* TI);
121 /// Rename a type. This ain't fast, we have to linearly search for it first.
122 /// @returns true if the rename was successful (type was found)
123 bool rename(Type* T, const std::string& new_name);
126 /// @name AbstractTypeUser Methods
129 /// This function is called when one of the types in the type plane
131 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
133 /// This function markes a type as being concrete (defined).
134 virtual void typeBecameConcrete(const DerivedType *AbsTy);
137 /// @name Internal Data
140 TypeMap tmap; ///< This is the mapping of names to types.
141 mutable uint32_t LastUnique; ///< Counter for tracking unique names
147 } // End llvm namespace