Simplify condition, this does not change the predicate at all though
[oota-llvm.git] / include / llvm / SymbolTable.h
1 //===-- llvm/SymbolTable.h - Implement a type plane'd symtab ----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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 
12 // distinct.
13 //
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 
16 // searched.
17 //
18 // This chaining behavior does NOT affect iterators though: only the lookup 
19 // method.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_SYMBOL_TABLE_H
24 #define LLVM_SYMBOL_TABLE_H
25
26 #include "llvm/Value.h"
27 #include <map>
28
29 namespace llvm {
30
31 class SymbolTable : public AbstractTypeUser,
32                     public std::map<const Type *, 
33                                     std::map<const std::string, Value *> > {
34 public:
35   typedef std::map<const std::string, Value *> VarMap;
36   typedef std::map<const Type *, VarMap> super;
37
38   typedef VarMap::iterator type_iterator;
39   typedef VarMap::const_iterator type_const_iterator;
40
41   inline SymbolTable() : InternallyInconsistent(false), LastUnique(0) {}
42   ~SymbolTable();
43
44   // lookup - Returns null on failure...
45   Value *lookup(const Type *Ty, const std::string &name) const;
46
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);
51   }
52
53   void remove(Value *N);
54   Value *type_remove(const type_iterator &It) {
55     return removeEntry(find(It->second->getType()), It);
56   }
57
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
60   // (constant/type)s.
61   //
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);
66   }
67
68   /// remove - Remove a constant or type from the symbol table with the
69   /// specified name.
70   Value *remove(const std::string &Name, Value *V) {
71     iterator TI = find(V->getType());
72     return removeEntry(TI, TI->second.find(Name));
73   }
74
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.
78   //
79   std::string getUniqueName(const Type *Ty, const std::string &BaseName);
80
81   inline unsigned type_size(const Type *TypeID) const {
82     return find(TypeID)->second.size();
83   }
84
85   // Note that type_begin / type_end only work if you know that an element of 
86   // TypeID is already in the symbol table!!!
87   //
88   inline type_iterator type_begin(const Type *TypeID) { 
89     return find(TypeID)->second.begin(); 
90   }
91   inline type_const_iterator type_begin(const Type *TypeID) const {
92     return find(TypeID)->second.begin(); 
93   }
94
95   inline type_iterator type_end(const Type *TypeID) { 
96     return find(TypeID)->second.end(); 
97   }
98   inline type_const_iterator type_end(const Type *TypeID) const { 
99     return find(TypeID)->second.end(); 
100   }
101
102   void dump() const;  // Debug method, print out symbol table
103
104 private:
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.
111   //
112   bool InternallyInconsistent;
113
114   // LastUnique - This value is used to retain the last unique value used
115   // by getUniqueName to generate unique names.
116   unsigned long LastUnique;
117
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();
121   }
122
123   // insertEntry - Insert a value into the symbol table with the specified
124   // name...
125   //
126   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
127
128   // removeEntry - Remove a value from the symbol table...
129   //
130   Value *removeEntry(iterator Plane, type_iterator Entry);
131
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);
135 };
136
137 } // End llvm namespace
138
139 #endif