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"
31 class SymbolTable : public AbstractTypeUser,
32 public std::map<const Type *,
33 std::map<const std::string, Value *> > {
35 typedef std::map<const std::string, Value *> VarMap;
36 typedef std::map<const Type *, VarMap> super;
38 typedef VarMap::iterator type_iterator;
39 typedef VarMap::const_iterator type_const_iterator;
41 inline SymbolTable() : InternallyInconsistent(false), LastUnique(0) {}
44 // lookup - Returns null on failure...
45 Value *lookup(const Type *Ty, const std::string &name) const;
47 // insert - Add named definition to the symbol table...
48 inline void insert(Value *N) {
49 assert(N->hasName() && "Value must be named to go into symbol table!");
50 insertEntry(N->getName(), N->getType(), N);
53 void remove(Value *N);
54 Value *type_remove(const type_iterator &It) {
55 return removeEntry(find(It->second->getType()), It);
58 // insert - Insert a constant or type into the symbol table with the specified
59 // name... There can be a many to one mapping between names and
62 inline void insert(const std::string &Name, Value *V) {
63 assert((isa<Type>(V) || isa<Constant>(V)) &&
64 "Can only insert types and constants here!");
65 insertEntry(Name, V->getType(), V);
68 /// remove - Remove a constant or type from the symbol table with the
70 Value *remove(const std::string &Name, Value *V) {
71 iterator TI = find(V->getType());
72 return removeEntry(TI, TI->second.find(Name));
75 // getUniqueName - Given a base name, return a string that is either equal to
76 // it (or derived from it) that does not already occur in the symbol table for
77 // the specified type.
79 std::string getUniqueName(const Type *Ty, const std::string &BaseName);
81 inline unsigned type_size(const Type *TypeID) const {
82 return find(TypeID)->second.size();
85 // Note that type_begin / type_end only work if you know that an element of
86 // TypeID is already in the symbol table!!!
88 inline type_iterator type_begin(const Type *TypeID) {
89 return find(TypeID)->second.begin();
91 inline type_const_iterator type_begin(const Type *TypeID) const {
92 return find(TypeID)->second.begin();
95 inline type_iterator type_end(const Type *TypeID) {
96 return find(TypeID)->second.end();
98 inline type_const_iterator type_end(const Type *TypeID) const {
99 return find(TypeID)->second.end();
102 void dump() const; // Debug method, print out symbol table
105 // InternallyInconsistent - There are times when the symbol table is
106 // internally inconsistent with the rest of the program. In this one case, a
107 // value exists with a Name, and it's not in the symbol table. When we call
108 // V->setName(""), it tries to remove itself from the symbol table and dies.
109 // We know this is happening, and so if the flag InternallyInconsistent is
110 // set, removal from the symbol table is a noop.
112 bool InternallyInconsistent;
114 // LastUnique - This value is used to retain the last unique value used
115 // by getUniqueName to generate unique names.
116 unsigned long LastUnique;
118 inline super::value_type operator[](const Type *Ty) {
119 assert(0 && "Should not use this operator to access symbol table!");
120 return super::value_type();
123 // insertEntry - Insert a value into the symbol table with the specified
126 void insertEntry(const std::string &Name, const Type *Ty, Value *V);
128 // removeEntry - Remove a value from the symbol table...
130 Value *removeEntry(iterator Plane, type_iterator Entry);
132 // This function is called when one of the types in the type plane are refined
133 virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
134 virtual void typeBecameConcrete(const DerivedType *AbsTy);
137 } // End llvm namespace