The contents of the SymTabValue class have been incorporated into the
[oota-llvm.git] / lib / VMCore / ValueHolderImpl.h
1 //===-- llvm/ValueHolderImpl.h - Implement ValueHolder template --*- C++ -*--=//
2 //
3 // This file implements the ValueHolder class.  This is kept out of line because
4 // it tends to pull in a lot of dependencies on other headers and most files
5 // don't need all that crud.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_VALUEHOLDER_IMPL_H
10 #define LLVM_VALUEHOLDER_IMPL_H
11
12 #include "llvm/ValueHolder.h"
13 #include "llvm/SymbolTable.h"
14 #include <algorithm>
15
16 template<class ValueSubclass, class ItemParentType, class SymTabType>
17 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
18 ::setParent(SymTabType *P) { 
19   if (Parent) {     // Remove all of the items from the old symbol table..
20     SymbolTable *SymTab = Parent->getSymbolTable();
21     for (iterator I = begin(); I != end(); ++I)
22       if ((*I)->hasName()) SymTab->remove(*I);
23   }
24
25   Parent = P; 
26
27   if (Parent) {     // Remove all of the items from the old symbol table..
28     SymbolTable *SymTab = Parent->getSymbolTableSure();
29     for (iterator I = begin(); I != end(); ++I)
30       if ((*I)->hasName()) SymTab->insert(*I);
31   }
32 }
33
34
35 template<class ValueSubclass, class ItemParentType, class SymTabType>
36 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
37 ::remove(ValueSubclass *D) {
38   iterator I(find(begin(), end(), D));
39   assert(I != end() && "Value not in ValueHolder!!");
40   remove(I);
41 }
42
43 // ValueHolder::remove(iterator &) this removes the element at the location
44 // specified by the iterator, and leaves the iterator pointing to the element
45 // that used to follow the element deleted.
46 //
47 template<class ValueSubclass, class ItemParentType, class SymTabType>
48 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType,SymTabType>
49 ::remove(iterator &DI) {
50   assert(DI != ValueList.end() && 
51          "Trying to remove the end of the value list!!!");
52   
53   ValueSubclass *i = *DI;
54   DI = ValueList.erase(DI);
55
56   i->setParent(0);  // I don't own you anymore... byebye...
57   
58   // You don't get to be in the symbol table anymore... byebye
59   if (i->hasName() && Parent)
60     Parent->getSymbolTable()->remove(i);
61   
62   return i;
63 }
64
65 template<class ValueSubclass, class ItemParentType, class SymTabType>
66 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType,SymTabType>
67 ::pop_back() {
68   assert(!ValueList.empty() && "Can't pop_back an empty valuelist!");
69   ValueSubclass *i = ValueList.back();
70   ValueList.pop_back();
71   i->setParent(0);  // I don't own you anymore... byebye...
72   
73   // You don't get to be in the symbol table anymore... byebye
74   if (i->hasName() && Parent)
75     Parent->getSymbolTable()->remove(i);
76   
77   return i;
78 }
79
80
81 template<class ValueSubclass, class ItemParentType, class SymTabType>
82 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType,SymTabType>
83 ::remove(const iterator &DI) {
84   assert(DI != ValueList.end() && 
85          "Trying to remove the end of the value holder list!!!");
86   
87   ValueSubclass *i = *DI;
88   ValueList.erase(DI);
89
90   i->setParent(0);  // I don't own you anymore... byebye...
91   
92   // You don't get to be in the symbol table anymore... byebye
93   if (i->hasName() && Parent)
94     Parent->getSymbolTable()->remove(i);
95   
96   return i;
97 }
98
99 template<class ValueSubclass, class ItemParentType, class SymTabType>
100 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType,SymTabType>
101 ::replaceWith(iterator &DI, ValueSubclass *NewVal) {
102   assert(DI != ValueList.end() && 
103          "Trying to replace the end of the value holder list!!!");
104
105   // Remove the value from the current container...
106   ValueSubclass *Ret = *DI;
107   Ret->setParent(0);  // I don't own you anymore... byebye...
108   
109   // You don't get to be in the symbol table anymore... byebye
110   if (Ret->hasName() && Parent)
111     Parent->getSymbolTable()->remove(Ret);
112
113   // Insert the new value into the container...
114   assert(NewVal->getParent() == 0 && "Value already has parent!");
115   NewVal->setParent(ItemParent);
116  
117   *DI = NewVal;
118   if (NewVal->hasName() && Parent)
119     Parent->getSymbolTableSure()->insert(NewVal);
120  
121   return Ret;
122 }
123
124
125 template<class ValueSubclass, class ItemParentType, class SymTabType>
126 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
127 ::push_front(ValueSubclass *Inst) {
128   assert(Inst->getParent() == 0 && "Value already has parent!");
129   Inst->setParent(ItemParent);
130
131   //ValueList.push_front(Inst);
132   ValueList.insert(ValueList.begin(), Inst);
133  
134   if (Inst->hasName() && Parent)
135     Parent->getSymbolTableSure()->insert(Inst);
136 }
137
138 template<class ValueSubclass, class ItemParentType, class SymTabType>
139 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
140 ::push_back(ValueSubclass *Inst) {
141   assert(Inst->getParent() == 0 && "Value already has parent!");
142   Inst->setParent(ItemParent);
143
144   ValueList.push_back(Inst);
145   
146   if (Inst->hasName() && Parent)
147     Parent->getSymbolTableSure()->insert(Inst);
148 }
149
150 // ValueHolder::insert - This method inserts the specified value *BEFORE* the 
151 // indicated iterator position, and returns an interator to the newly inserted
152 // value.
153 //
154 template<class ValueSubclass, class ItemParentType, class SymTabType>
155 ValueHolder<ValueSubclass,ItemParentType,SymTabType>::iterator
156 ValueHolder<ValueSubclass,ItemParentType,SymTabType>
157 ::insert(iterator Pos, ValueSubclass *Inst) {
158   assert(Inst->getParent() == 0 && "Value already has parent!");
159   Inst->setParent(ItemParent);
160
161   iterator I = ValueList.insert(Pos, Inst);
162   if (Inst->hasName() && Parent)
163     Parent->getSymbolTableSure()->insert(Inst);
164   return I;
165 }
166
167 // ValueHolder::insert - This method inserts the specified _range_ of values
168 // before the 'Pos' iterator, and returns an iterator to the first newly
169 // inserted element.  This currently only works for vector iterators...
170 //
171 // FIXME: This is not generic so that the code does not have to be around
172 // to be used... is this ok?
173 //
174 template<class ValueSubclass, class ItemParentType, class SymTabType>
175 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
176 ::insert(iterator Pos,                     // Where to insert
177          iterator First, iterator Last) {   // Vector to read insts from
178
179   // Check to make sure that the values are not already in some valueholder...
180   
181   for (iterator X = First; X != Last; ++X) {
182     assert((*X)->getParent() == 0 &&
183            "Cannot insert into valueholder, value already has a parent!");
184     (*X)->setParent(ItemParent);
185   }
186
187   // Add all of the values to the value holder...
188   ValueList.insert(Pos, First, Last);
189
190   // Insert all of the instructions in the symbol table...
191   if (Parent)
192     for (;First != Last; ++First)
193       if ((*First)->hasName())
194         Parent->getSymbolTableSure()->insert(*First);
195 }
196
197 #endif