1 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a symbol table that has planes broken up by type.
11 // Identical types may have overlapping symbol names as long as they are
14 // Note that this implements a chained symbol table. If a name being 'lookup'd
15 // isn't found in the current symbol table, then the parent symbol table is
18 // This chaining behavior does NOT affect iterators though: only the lookup
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_SYMBOL_TABLE_H
24 #define LLVM_SYMBOL_TABLE_H
26 #include "llvm/Value.h"
29 class SymbolTable : public AbstractTypeUser,
30 public std::map<const Type *,
31 std::map<const std::string, Value *> > {
33 typedef std::map<const std::string, Value *> VarMap;
34 typedef std::map<const Type *, VarMap> super;
36 typedef VarMap::iterator type_iterator;
37 typedef VarMap::const_iterator type_const_iterator;
39 inline SymbolTable() : InternallyInconsistent(false) {}
42 // lookup - Returns null on failure...
43 Value *lookup(const Type *Ty, const std::string &name);
45 // insert - Add named definition to the symbol table...
46 inline void insert(Value *N) {
47 assert(N->hasName() && "Value must be named to go into symbol table!");
48 insertEntry(N->getName(), N->getType(), N);
51 void remove(Value *N);
52 Value *type_remove(const type_iterator &It) {
53 return removeEntry(find(It->second->getType()), It);
56 // insert - Insert a constant or type into the symbol table with the specified
57 // name... There can be a many to one mapping between names and
60 inline void insert(const std::string &Name, Value *V) {
61 assert((isa<Type>(V) || isa<Constant>(V)) &&
62 "Can only insert types and constants here!");
63 insertEntry(Name, V->getType(), V);
66 /// remove - Remove a constant or type from the symbol table with the
68 Value *remove(const std::string &Name, Value *V) {
69 iterator TI = find(V->getType());
70 return removeEntry(TI, TI->second.find(Name));
73 // getUniqueName - Given a base name, return a string that is either equal to
74 // it (or derived from it) that does not already occur in the symbol table for
75 // the specified type.
77 std::string getUniqueName(const Type *Ty, const std::string &BaseName);
79 inline unsigned type_size(const Type *TypeID) const {
80 return find(TypeID)->second.size();
83 // Note that type_begin / type_end only work if you know that an element of
84 // TypeID is already in the symbol table!!!
86 inline type_iterator type_begin(const Type *TypeID) {
87 return find(TypeID)->second.begin();
89 inline type_const_iterator type_begin(const Type *TypeID) const {
90 return find(TypeID)->second.begin();
93 inline type_iterator type_end(const Type *TypeID) {
94 return find(TypeID)->second.end();
96 inline type_const_iterator type_end(const Type *TypeID) const {
97 return find(TypeID)->second.end();
100 void dump() const; // Debug method, print out symbol table
103 // InternallyInconsistent - There are times when the symbol table is
104 // internally inconsistent with the rest of the program. In this one case, a
105 // value exists with a Name, and it's not in the symbol table. When we call
106 // V->setName(""), it tries to remove itself from the symbol table and dies.
107 // We know this is happening, and so if the flag InternallyInconsistent is
108 // set, removal from the symbol table is a noop.
110 bool InternallyInconsistent;
112 inline super::value_type operator[](const Type *Ty) {
113 assert(0 && "Should not use this operator to access symbol table!");
114 return super::value_type();
117 // insertEntry - Insert a value into the symbol table with the specified
120 void insertEntry(const std::string &Name, const Type *Ty, Value *V);
122 // removeEntry - Remove a value from the symbol table...
124 Value *removeEntry(iterator Plane, type_iterator Entry);
126 // This function is called when one of the types in the type plane are refined
127 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
128 virtual void typeBecameConcrete(const DerivedType *AbsTy);