1 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ------*- C++ -*-=//
3 // This file implements a symbol table that has planed broken up by type.
4 // Identical types may have overlapping symbol names as long as they are
7 // Note that this implements a chained symbol table. If a name being 'lookup'd
8 // isn't found in the current symbol table, then the parent symbol table is
11 // This chaining behavior does NOT affect iterators though: only the lookup
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SYMBOL_TABLE_H
17 #define LLVM_SYMBOL_TABLE_H
19 #include "llvm/Value.h"
22 class SymbolTable : public AbstractTypeUser,
23 public std::map<const Type *,
24 std::map<const std::string, Value *> > {
26 typedef std::map<const std::string, Value *> VarMap;
27 typedef std::map<const Type *, VarMap> super;
30 SymbolTable *ParentSymTab;
32 friend class Function;
33 inline void setParentSymTab(SymbolTable *P) { ParentSymTab = P; }
36 typedef VarMap::iterator type_iterator;
37 typedef VarMap::const_iterator type_const_iterator;
39 inline SymbolTable(SymbolTable *P = 0) {
41 InternallyInconsistent = false;
45 SymbolTable *getParentSymTab() { return ParentSymTab; }
47 // lookup - Returns null on failure...
48 Value *lookup(const Type *Ty, const std::string &name);
50 // localLookup - Look in this symbol table without falling back on parent,
51 // if non-existing. Returns null on failure...
53 Value *localLookup(const Type *Ty, const std::string &name);
55 // insert - Add named definition to the symbol table...
56 inline void insert(Value *N) {
57 assert(N->hasName() && "Value must be named to go into symbol table!");
58 insertEntry(N->getName(), N->getType(), N);
61 // insert - Insert a constant or type into the symbol table with the specified
62 // name... There can be a many to one mapping between names and
65 inline void insert(const std::string &Name, Value *V) {
66 assert((isa<Type>(V) || isa<Constant>(V)) &&
67 "Can only insert types and constants here!");
68 insertEntry(Name, V->getType(), V);
71 void remove(Value *N);
72 Value *type_remove(const type_iterator &It) {
73 return removeEntry(find(It->second->getType()), It);
76 // getUniqueName - Given a base name, return a string that is either equal to
77 // it (or derived from it) that does not already occur in the symbol table for
78 // the specified type.
80 std::string getUniqueName(const Type *Ty, const std::string &BaseName);
82 inline unsigned type_size(const Type *TypeID) const {
83 return find(TypeID)->second.size();
86 // Note that type_begin / type_end only work if you know that an element of
87 // TypeID is already in the symbol table!!!
89 inline type_iterator type_begin(const Type *TypeID) {
90 return find(TypeID)->second.begin();
92 inline type_const_iterator type_begin(const Type *TypeID) const {
93 return find(TypeID)->second.begin();
96 inline type_iterator type_end(const Type *TypeID) {
97 return find(TypeID)->second.end();
99 inline type_const_iterator type_end(const Type *TypeID) const {
100 return find(TypeID)->second.end();
103 void dump() const; // Debug method, print out symbol table
106 // InternallyInconsistent - There are times when the symbol table is
107 // internally inconsistent with the rest of the program. In this one case, a
108 // value exists with a Name, and it's not in the symbol table. When we call
109 // V->setName(""), it tries to remove itself from the symbol table and dies.
110 // We know this is happening, and so if the flag InternallyInconsistent is
111 // set, removal from the symbol table is a noop.
113 bool InternallyInconsistent;
115 inline super::value_type operator[](const Type *Ty) {
116 assert(0 && "Should not use this operator to access symbol table!");
117 return super::value_type();
120 // insertEntry - Insert a value into the symbol table with the specified
123 void insertEntry(const std::string &Name, const Type *Ty, Value *V);
125 // removeEntry - Remove a value from the symbol table...
127 Value *removeEntry(iterator Plane, type_iterator Entry);
129 // This function is called when one of the types in the type plane are refined
130 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);