2003-09-30 18:37:50 +00:00
|
|
|
//===-- llvm/SymbolTable.h - Implement a type plane'd symtab ----*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2004-05-25 08:45:53 +00:00
|
|
|
// This file was developed by the LLVM research group and re-written by Reid
|
2005-04-21 20:19:05 +00:00
|
|
|
// Spencer. It is distributed under the University of Illinois Open Source
|
2004-05-25 08:45:53 +00:00
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2003-10-20 20:19:47 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2004-05-25 08:45:53 +00:00
|
|
|
// This file implements the main symbol table for LLVM.
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_SYMBOL_TABLE_H
|
|
|
|
#define LLVM_SYMBOL_TABLE_H
|
|
|
|
|
2001-09-07 16:21:36 +00:00
|
|
|
#include "llvm/Value.h"
|
2006-05-31 20:40:36 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include <map>
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2004-05-25 08:45:53 +00:00
|
|
|
/// This class provides a symbol table of name/value pairs that is broken
|
2005-04-21 20:19:05 +00:00
|
|
|
/// 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
|
2004-05-25 08:45:53 +00:00
|
|
|
/// distinctly from Values.
|
2005-04-21 20:19:05 +00:00
|
|
|
///
|
2004-05-25 08:45:53 +00:00
|
|
|
/// The SymbolTable provides several utility functions for answering common
|
|
|
|
/// questions about its contents as well as an iterator interface for
|
2005-04-21 20:19:05 +00:00
|
|
|
/// directly iterating over the contents. To reduce confusion, the terms
|
2004-05-25 08:45:53 +00:00
|
|
|
/// "type", "value", and "plane" are used consistently. For example,
|
2005-04-21 20:19:05 +00:00
|
|
|
/// There is a TypeMap typedef that is the mapping of names to Types.
|
|
|
|
/// Similarly there is a ValueMap typedef that is the mapping of
|
2004-05-25 08:45:53 +00:00
|
|
|
/// names to Values. Finally, there is a PlaneMap typedef that is the
|
2004-12-05 06:33:15 +00:00
|
|
|
/// mapping of types to planes of ValueMap. This is the basic structure
|
2004-05-25 08:45:53 +00:00
|
|
|
/// of the symbol table. When you call type_begin() you're asking
|
|
|
|
/// for an iterator at the start of the TypeMap. When you call
|
2005-04-21 20:19:05 +00:00
|
|
|
/// plane_begin(), you're asking for an iterator at the start of
|
2004-05-25 08:45:53 +00:00
|
|
|
/// the PlaneMap. Finally, when you call value_begin(), you're asking
|
|
|
|
/// for an iterator at the start of a ValueMap for a specific type
|
|
|
|
/// plane.
|
|
|
|
class SymbolTable : public AbstractTypeUser {
|
|
|
|
|
|
|
|
/// @name Types
|
|
|
|
/// @{
|
2001-10-13 06:17:50 +00:00
|
|
|
public:
|
2004-05-25 08:45:53 +00:00
|
|
|
/// @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;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2004-05-25 08:45:53 +00:00
|
|
|
/// @brief A mapping of types to names to values (type planes).
|
|
|
|
typedef std::map<const Type *, ValueMap> PlaneMap;
|
|
|
|
|
|
|
|
/// @brief An iterator over the type planes.
|
|
|
|
typedef PlaneMap::iterator plane_iterator;
|
|
|
|
|
|
|
|
/// @brief A const_iterator over the type planes
|
|
|
|
typedef PlaneMap::const_iterator plane_const_iterator;
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Constructors
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
|
2005-03-06 05:22:05 +00:00
|
|
|
SymbolTable() : LastUnique(0) {}
|
2001-06-06 20:29:01 +00:00
|
|
|
~SymbolTable();
|
|
|
|
|
2004-05-25 08:45:53 +00:00
|
|
|
/// @}
|
|
|
|
/// @name Accessors
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// This method finds the value with the given \p name in the
|
|
|
|
/// 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.
|
2003-12-31 07:08:19 +00:00
|
|
|
Value *lookup(const Type *Ty, const std::string &name) const;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// @returns true iff the type map and the type plane are both not
|
2004-05-25 08:45:53 +00:00
|
|
|
/// empty.
|
|
|
|
/// @brief Determine if the symbol table is empty
|
2007-01-06 07:24:44 +00:00
|
|
|
inline bool isEmpty() const { return pmap.empty(); }
|
2004-05-25 08:45:53 +00:00
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// 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
|
2004-05-25 08:45:53 +00:00
|
|
|
/// for the specified type.
|
|
|
|
/// @brief Get a name unique to this symbol table
|
2005-04-21 20:19:05 +00:00
|
|
|
std::string getUniqueName(const Type *Ty,
|
2005-03-06 06:03:44 +00:00
|
|
|
const std::string &BaseName) const;
|
2004-05-25 08:45:53 +00:00
|
|
|
|
|
|
|
/// 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
|
2005-04-21 20:19:05 +00:00
|
|
|
void dump() const;
|
2004-05-25 08:45:53 +00:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @name Iteration
|
|
|
|
/// @{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// Get an iterator that starts at the beginning of the type planes.
|
|
|
|
/// The iterator will iterate over the Type/ValueMap pairs in the
|
2005-04-21 20:19:05 +00:00
|
|
|
/// type planes.
|
2004-05-25 08:45:53 +00:00
|
|
|
inline plane_iterator plane_begin() { return pmap.begin(); }
|
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// 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.
|
2004-05-25 08:45:53 +00:00
|
|
|
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.
|
2005-04-21 20:19:05 +00:00
|
|
|
inline value_iterator value_begin(const Type *Typ) {
|
2004-05-25 08:45:53 +00:00
|
|
|
assert(Typ && "Can't get value iterator with null type!");
|
2005-04-21 20:19:05 +00:00
|
|
|
return pmap.find(Typ)->second.begin();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2004-05-25 08:45:53 +00:00
|
|
|
|
|
|
|
/// 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 value_begin(const Type *Typ) const {
|
|
|
|
assert(Typ && "Can't get value iterator with null type!");
|
2005-04-21 20:19:05 +00:00
|
|
|
return pmap.find(Typ)->second.begin();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-05-25 08:45:53 +00:00
|
|
|
/// 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.
|
2005-04-21 20:19:05 +00:00
|
|
|
inline value_iterator value_end(const Type *Typ) {
|
2004-05-25 08:45:53 +00:00
|
|
|
assert(Typ && "Can't get value iterator with null type!");
|
2005-04-21 20:19:05 +00:00
|
|
|
return pmap.find(Typ)->second.end();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2004-05-25 08:45:53 +00:00
|
|
|
|
|
|
|
/// 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.
|
2005-04-21 20:19:05 +00:00
|
|
|
inline value_const_iterator value_end(const Type *Typ) const {
|
2004-05-25 08:45:53 +00:00
|
|
|
assert(Typ && "Can't get value iterator with null type!");
|
2005-04-21 20:19:05 +00:00
|
|
|
return pmap.find(Typ)->second.end();
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2001-09-07 16:21:36 +00:00
|
|
|
|
2004-05-25 08:45:53 +00:00
|
|
|
/// 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.
|
2005-03-05 19:58:40 +00:00
|
|
|
inline plane_const_iterator find(const Type* Typ) const {
|
2004-05-25 08:45:53 +00:00
|
|
|
assert(Typ && "Can't find type plane with null type!");
|
2005-03-05 19:58:40 +00:00
|
|
|
return pmap.find(Typ);
|
2004-05-25 08:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This method returns a plane_iterator for iteration over the
|
|
|
|
/// type planes starting at a specific plane, given by \p Ty.
|
2004-05-26 17:42:51 +00:00
|
|
|
/// @brief Find a type plane.
|
2005-04-21 20:19:05 +00:00
|
|
|
inline plane_iterator find(const Type* Typ) {
|
2004-05-25 08:45:53 +00:00
|
|
|
assert(Typ && "Can't find type plane with null type!");
|
2005-04-21 20:19:05 +00:00
|
|
|
return pmap.find(Typ);
|
2004-05-25 08:45:53 +00:00
|
|
|
}
|
2003-11-09 19:39:48 +00:00
|
|
|
|
2005-03-06 06:03:44 +00:00
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @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();
|
|
|
|
|
|
|
|
/// @}
|
|
|
|
/// @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);
|
|
|
|
|
2004-05-25 08:45:53 +00:00
|
|
|
/// @}
|
|
|
|
/// @name Internal Methods
|
|
|
|
/// @{
|
|
|
|
private:
|
|
|
|
/// @brief Insert a value into the symbol table with the specified name.
|
2002-01-20 22:54:45 +00:00
|
|
|
void insertEntry(const std::string &Name, const Type *Ty, Value *V);
|
2001-09-07 16:21:36 +00:00
|
|
|
|
2005-04-21 20:19:05 +00:00
|
|
|
/// This function is called when one of the types in the type plane
|
2004-05-25 08:45:53 +00:00
|
|
|
/// is refined.
|
2001-09-07 16:21:36 +00:00
|
|
|
virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
|
2004-05-25 08:45:53 +00:00
|
|
|
|
|
|
|
/// This function markes a type as being concrete (defined).
|
2003-10-03 18:46:24 +00:00
|
|
|
virtual void typeBecameConcrete(const DerivedType *AbsTy);
|
2004-05-25 08:45:53 +00:00
|
|
|
|
|
|
|
/// @}
|
2005-04-21 20:19:05 +00:00
|
|
|
/// @name Internal Data
|
2004-05-25 08:45:53 +00:00
|
|
|
/// @{
|
|
|
|
private:
|
|
|
|
|
|
|
|
/// This is the main content of the symbol table. It provides
|
|
|
|
/// separate type planes for named values. That is, each named
|
2005-04-21 20:19:05 +00:00
|
|
|
/// value is organized into a separate dictionary based on
|
2004-05-25 08:45:53 +00:00
|
|
|
/// Type. This means that the same name can be used for different
|
2005-04-21 20:19:05 +00:00
|
|
|
/// types without conflict.
|
2004-05-25 08:45:53 +00:00
|
|
|
/// @brief The mapping of types to names to values.
|
|
|
|
PlaneMap pmap;
|
|
|
|
|
|
|
|
/// This value is used to retain the last unique value used
|
|
|
|
/// by getUniqueName to generate unique names.
|
2006-05-31 20:40:36 +00:00
|
|
|
mutable uint32_t LastUnique;
|
2004-05-25 08:45:53 +00:00
|
|
|
/// @}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
};
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2004-05-25 08:45:53 +00:00
|
|
|
// vim: sw=2
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
#endif
|
2004-05-25 08:45:53 +00:00
|
|
|
|