Included assert.h so that the code compiles under newer versions of GCC.
[oota-llvm.git] / include / llvm / SymbolTable.h
1 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ------*- C++ -*-=//
2 //
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 
5 // distinct.
6 //
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 
9 // searched.
10 //
11 // This chaining behavior does NOT affect iterators though: only the lookup 
12 // method
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_SYMBOL_TABLE_H
17 #define LLVM_SYMBOL_TABLE_H
18
19 #include <assert.h>
20
21 #include "llvm/Value.h"
22 #include <map>
23
24 class SymbolTable : public AbstractTypeUser,
25                     public std::map<const Type *, 
26                                     std::map<const std::string, Value *> > {
27 public:
28   typedef std::map<const std::string, Value *> VarMap;
29   typedef std::map<const Type *, VarMap> super;
30
31   typedef VarMap::iterator type_iterator;
32   typedef VarMap::const_iterator type_const_iterator;
33
34   inline SymbolTable() : InternallyInconsistent(false) {}
35   ~SymbolTable();
36
37   // lookup - Returns null on failure...
38   Value *lookup(const Type *Ty, const std::string &name);
39
40   // insert - Add named definition to the symbol table...
41   inline void insert(Value *N) {
42     assert(N->hasName() && "Value must be named to go into symbol table!");
43     insertEntry(N->getName(), N->getType(), N);
44   }
45
46   void remove(Value *N);
47   Value *type_remove(const type_iterator &It) {
48     return removeEntry(find(It->second->getType()), It);
49   }
50
51   // insert - Insert a constant or type into the symbol table with the specified
52   // name...  There can be a many to one mapping between names and
53   // (constant/type)s.
54   //
55   inline void insert(const std::string &Name, Value *V) {
56     assert((isa<Type>(V) || isa<Constant>(V)) &&
57            "Can only insert types and constants here!");
58     insertEntry(Name, V->getType(), V);
59   }
60
61   /// remove - Remove a constant or type from the symbol table with the
62   /// specified name.
63   Value *remove(const std::string &Name, Value *V) {
64     iterator TI = find(V->getType());
65     return removeEntry(TI, TI->second.find(Name));
66   }
67
68   // getUniqueName - Given a base name, return a string that is either equal to
69   // it (or derived from it) that does not already occur in the symbol table for
70   // the specified type.
71   //
72   std::string getUniqueName(const Type *Ty, const std::string &BaseName);
73
74   inline unsigned type_size(const Type *TypeID) const {
75     return find(TypeID)->second.size();
76   }
77
78   // Note that type_begin / type_end only work if you know that an element of 
79   // TypeID is already in the symbol table!!!
80   //
81   inline type_iterator type_begin(const Type *TypeID) { 
82     return find(TypeID)->second.begin(); 
83   }
84   inline type_const_iterator type_begin(const Type *TypeID) const {
85     return find(TypeID)->second.begin(); 
86   }
87
88   inline type_iterator type_end(const Type *TypeID) { 
89     return find(TypeID)->second.end(); 
90   }
91   inline type_const_iterator type_end(const Type *TypeID) const { 
92     return find(TypeID)->second.end(); 
93   }
94
95   void dump() const;  // Debug method, print out symbol table
96
97 private:
98   // InternallyInconsistent - There are times when the symbol table is
99   // internally inconsistent with the rest of the program.  In this one case, a
100   // value exists with a Name, and it's not in the symbol table.  When we call
101   // V->setName(""), it tries to remove itself from the symbol table and dies.
102   // We know this is happening, and so if the flag InternallyInconsistent is
103   // set, removal from the symbol table is a noop.
104   //
105   bool InternallyInconsistent;
106
107   inline super::value_type operator[](const Type *Ty) {
108     assert(0 && "Should not use this operator to access symbol table!");
109     return super::value_type();
110   }
111
112   // insertEntry - Insert a value into the symbol table with the specified
113   // name...
114   //
115   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
116
117   // removeEntry - Remove a value from the symbol table...
118   //
119   Value *removeEntry(iterator Plane, type_iterator Entry);
120
121   // This function is called when one of the types in the type plane are refined
122   virtual void refineAbstractType(const DerivedType *OldTy, const Type *NewTy);
123 };
124
125 #endif