/// 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
+/// 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
public:
/// @brief A mapping of names to types.
- typedef std::map<const std::string, Type*> TypeMap;
+ typedef std::map<const std::string, const Type*> TypeMap;
/// @brief An iterator over the TypeMap.
typedef TypeMap::iterator type_iterator;
/// @{
public:
- inline SymbolTable()
- : pmap(), tmap(), InternallyInconsistent(false), LastUnique(0) {}
+ SymbolTable() : LastUnique(0) {}
~SymbolTable();
/// @}
/// @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
- inline bool hasTypes() const { return ! tmap.empty(); }
+ Type* lookupType(const std::string& name) const;
/// @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() && tmap.empty(); }
- /// 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.
- unsigned type_size(const Type *TypeID) const;
-
/// @brief 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;
-
- /// 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;
+ inline unsigned num_types() const { return (unsigned)tmap.size(); }
/// 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;
+ const std::string &BaseName) const;
/// 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;
-/// @}
-/// @name Mutators
-/// @{
-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);
- }
-
- /// Inserts a constant or type into the symbol table with the specified
- /// name. There can be a many to one mapping between names and constants
- /// or types.
- /// @brief Insert a constant or type.
- inline void insert(const std::string &Name, Value *Val) {
- assert(Val && "Can't insert null type into symbol table!");
- assert(!isa<Type>(Val) && "Cannot insert types with this interface!");
- assert(isa<Constant>(Val) &&
- "Can only insert constants into a symbol table!");
- insertEntry(Name, Val->getType(), Val);
- }
-
- /// 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 *Typ) {
- assert(Typ && "Can't insert null type into symbol table!");
- insertEntry(Name, Typ );
- }
-
- /// 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);
-
- /// 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* Typ );
-
- /// Remove a constant or type with the specified name from the
- /// symbol table.
- /// @returns the removed Value.
- /// @brief Remove a constant or type from the symbol table.
- inline Value* remove(const std::string &Name, Value *Val) {
- assert(Val && "Can't remove null value from symbol table!");
- assert(!isa<Type>(Val) && "Can't remove types with this interface!");
- plane_iterator PI = pmap.find(Val->getType());
- return removeEntry(PI, PI->second.find(Name));
- }
-
- /// 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) );
- }
-
- /// 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
/// @{
/// 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 {
+ inline plane_const_iterator find(const Type* Typ) const {
assert(Typ && "Can't find type plane with null type!");
- return pmap.find( Typ );
+ return pmap.find(Typ);
}
/// 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 ) {
+ inline plane_iterator find(const Type* Typ) {
assert(Typ && "Can't find type plane with null type!");
return pmap.find(Typ);
}
- /// This method returns a ValueMap* for a specific type plane. This
- /// interface is deprecated and may go away in the future.
- /// @deprecated
- /// @brief Find a type plane
- inline const ValueMap* findPlane( const Type* Typ ) const {
- assert(Typ && "Can't find type plane with null type!");
- plane_const_iterator I = pmap.find( Typ );
- if ( I == pmap.end() ) return 0;
- return &I->second;
+
+/// @}
+/// @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();
+
+ /// 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.
+ void insert(const std::string &Name, const Type *Typ);
+
+ /// Remove a type at the specified position in the symbol table.
+ /// @returns the removed Type.
+ Type* remove(type_iterator TI);
+
+/// @}
+/// @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);
}
+ /// 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);
+
/// @}
/// @name Internal Methods
/// @{
/// @brief Insert a value into the symbol table with the specified name.
void insertEntry(const std::string &Name, const Type *Ty, Value *V);
- /// @brief Insert a type into the symbol table with the specified name.
- 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 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.
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
/// 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. Note that the Type::TypeTy plane is
- /// not stored in this map but is in tmap.
+ /// types without conflict.
/// @brief The mapping of types to names to values.
PlaneMap pmap;
- /// This is the Type::TypeTy plane. It is separated from the pmap
+ /// This is the type 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,
- /// 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.
- /// @brief Indicator of symbol table internal inconsistency.
- bool InternallyInconsistent;
-
/// This value is used to retain the last unique value used
/// by getUniqueName to generate unique names.
mutable unsigned long LastUnique;