Added LLVM copyright header (for lack of a better term).
[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 planed 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 class SymbolTable : public AbstractTypeUser,
30                     public std::map<const Type *, 
31                                     std::map<const std::string, Value *> > {
32 public:
33   typedef std::map<const std::string, Value *> VarMap;
34   typedef std::map<const Type *, VarMap> super;
35
36   typedef VarMap::iterator type_iterator;
37   typedef VarMap::const_iterator type_const_iterator;
38
39   inline SymbolTable() : InternallyInconsistent(false) {}
40   ~SymbolTable();
41
42   // lookup - Returns null on failure...
43   Value *lookup(const Type *Ty, const std::string &name);
44
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);
49   }
50
51   void remove(Value *N);
52   Value *type_remove(const type_iterator &It) {
53     return removeEntry(find(It->second->getType()), It);
54   }
55
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
58   // (constant/type)s.
59   //
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);
64   }
65
66   /// remove - Remove a constant or type from the symbol table with the
67   /// specified name.
68   Value *remove(const std::string &Name, Value *V) {
69     iterator TI = find(V->getType());
70     return removeEntry(TI, TI->second.find(Name));
71   }
72
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.
76   //
77   std::string getUniqueName(const Type *Ty, const std::string &BaseName);
78
79   inline unsigned type_size(const Type *TypeID) const {
80     return find(TypeID)->second.size();
81   }
82
83   // Note that type_begin / type_end only work if you know that an element of 
84   // TypeID is already in the symbol table!!!
85   //
86   inline type_iterator type_begin(const Type *TypeID) { 
87     return find(TypeID)->second.begin(); 
88   }
89   inline type_const_iterator type_begin(const Type *TypeID) const {
90     return find(TypeID)->second.begin(); 
91   }
92
93   inline type_iterator type_end(const Type *TypeID) { 
94     return find(TypeID)->second.end(); 
95   }
96   inline type_const_iterator type_end(const Type *TypeID) const { 
97     return find(TypeID)->second.end(); 
98   }
99
100   void dump() const;  // Debug method, print out symbol table
101
102 private:
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.
109   //
110   bool InternallyInconsistent;
111
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();
115   }
116
117   // insertEntry - Insert a value into the symbol table with the specified
118   // name...
119   //
120   void insertEntry(const std::string &Name, const Type *Ty, Value *V);
121
122   // removeEntry - Remove a value from the symbol table...
123   //
124   Value *removeEntry(iterator Plane, type_iterator Entry);
125
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);
129 };
130
131 #endif