3f20a173e91b48b56149c6ae0ee77e57b5f5a32d
[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
100 template<class ValueSubclass, class ItemParentType, class SymTabType>
101 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
102 ::remove(iterator S, iterator E) {
103   for (iterator I = S; I != E; ++I) {
104     ValueSubclass *i = *I;
105     i->setParent(0);  // I don't own you anymore... byebye...
106   
107     // You don't get to be in the symbol table anymore... byebye
108     if (i->hasName() && Parent)
109       Parent->getSymbolTable()->remove(i);
110   }
111
112   ValueList.erase(S, E);
113 }
114
115
116 template<class ValueSubclass, class ItemParentType, class SymTabType>
117 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType,SymTabType>
118 ::replaceWith(iterator &DI, ValueSubclass *NewVal) {
119   assert(DI != ValueList.end() && 
120          "Trying to replace the end of the value holder list!!!");
121
122   // Remove the value from the current container...
123   ValueSubclass *Ret = *DI;
124   Ret->setParent(0);  // I don't own you anymore... byebye...
125   
126   // You don't get to be in the symbol table anymore... byebye
127   if (Ret->hasName() && Parent)
128     Parent->getSymbolTable()->remove(Ret);
129
130   // Insert the new value into the container...
131   assert(NewVal->getParent() == 0 && "Value already has parent!");
132   NewVal->setParent(ItemParent);
133  
134   *DI = NewVal;
135   if (NewVal->hasName() && Parent)
136     Parent->getSymbolTableSure()->insert(NewVal);
137  
138   return Ret;
139 }
140
141
142 template<class ValueSubclass, class ItemParentType, class SymTabType>
143 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
144 ::push_front(ValueSubclass *Inst) {
145   assert(Inst->getParent() == 0 && "Value already has parent!");
146   Inst->setParent(ItemParent);
147
148   //ValueList.push_front(Inst);
149   ValueList.insert(ValueList.begin(), Inst);
150  
151   if (Inst->hasName() && Parent)
152     Parent->getSymbolTableSure()->insert(Inst);
153 }
154
155 template<class ValueSubclass, class ItemParentType, class SymTabType>
156 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
157 ::push_back(ValueSubclass *Inst) {
158   assert(Inst->getParent() == 0 && "Value already has parent!");
159   Inst->setParent(ItemParent);
160
161   ValueList.push_back(Inst);
162   
163   if (Inst->hasName() && Parent)
164     Parent->getSymbolTableSure()->insert(Inst);
165 }
166
167 // ValueHolder::insert - This method inserts the specified value *BEFORE* the 
168 // indicated iterator position, and returns an interator to the newly inserted
169 // value.
170 //
171 template<class ValueSubclass, class ItemParentType, class SymTabType>
172 ValueHolder<ValueSubclass,ItemParentType,SymTabType>::iterator
173 ValueHolder<ValueSubclass,ItemParentType,SymTabType>
174 ::insert(iterator Pos, ValueSubclass *Inst) {
175   assert(Inst->getParent() == 0 && "Value already has parent!");
176   Inst->setParent(ItemParent);
177
178   iterator I = ValueList.insert(Pos, Inst);
179   if (Inst->hasName() && Parent)
180     Parent->getSymbolTableSure()->insert(Inst);
181   return I;
182 }
183
184 // ValueHolder::insert - This method inserts the specified _range_ of values
185 // before the 'Pos' iterator, and returns an iterator to the first newly
186 // inserted element.  This currently only works for vector iterators...
187 //
188 // FIXME: This is not generic so that the code does not have to be around
189 // to be used... is this ok?
190 //
191 template<class ValueSubclass, class ItemParentType, class SymTabType>
192 ValueHolder<ValueSubclass,ItemParentType,SymTabType>::iterator
193 ValueHolder<ValueSubclass,ItemParentType,SymTabType>
194 ::insert(iterator Pos,                      // Where to insert
195          iterator First, iterator Last) {   // Vector to read insts from
196
197   // Since the vector range insert operation doesn't return an updated iterator,
198   // we have to convert the iterator to and index and back to assure that it
199   // cannot get invalidated.  Gross hack, but effective.
200   //
201   unsigned Offset = Pos-begin();
202
203   // Check to make sure that the values are not already in some valueholder...
204   for (iterator X = First; X != Last; ++X) {
205     assert((*X)->getParent() == 0 &&
206            "Cannot insert into valueholder, value already has a parent!");
207     (*X)->setParent(ItemParent);
208   }
209
210   // Add all of the values to the value holder...
211   ValueList.insert(Pos, First, Last);
212
213   // Insert all of the instructions in the symbol table...
214   if (Parent)
215     for (;First != Last; ++First)
216       if ((*First)->hasName())
217         Parent->getSymbolTableSure()->insert(*First);
218
219   return begin()+Offset;
220 }
221
222 #endif