1 //===-- llvm/ValueHolderImpl.h - Implement ValueHolder template --*- C++ -*--=//
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.
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_VALUEHOLDER_IMPL_H
10 #define LLVM_VALUEHOLDER_IMPL_H
12 #include "llvm/ValueHolder.h"
13 #include "llvm/SymbolTable.h"
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);
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);
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!!");
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.
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!!!");
53 ValueSubclass *i = *DI;
54 DI = ValueList.erase(DI);
56 i->setParent(0); // I don't own you anymore... byebye...
58 // You don't get to be in the symbol table anymore... byebye
59 if (i->hasName() && Parent)
60 Parent->getSymbolTable()->remove(i);
65 template<class ValueSubclass, class ItemParentType, class SymTabType>
66 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType,SymTabType>
68 assert(!ValueList.empty() && "Can't pop_back an empty valuelist!");
69 ValueSubclass *i = ValueList.back();
71 i->setParent(0); // I don't own you anymore... byebye...
73 // You don't get to be in the symbol table anymore... byebye
74 if (i->hasName() && Parent)
75 Parent->getSymbolTable()->remove(i);
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!!!");
87 ValueSubclass *i = *DI;
90 i->setParent(0); // I don't own you anymore... byebye...
92 // You don't get to be in the symbol table anymore... byebye
93 if (i->hasName() && Parent)
94 Parent->getSymbolTable()->remove(i);
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!!!");
105 // Remove the value from the current container...
106 ValueSubclass *Ret = *DI;
107 Ret->setParent(0); // I don't own you anymore... byebye...
109 // You don't get to be in the symbol table anymore... byebye
110 if (Ret->hasName() && Parent)
111 Parent->getSymbolTable()->remove(Ret);
113 // Insert the new value into the container...
114 assert(NewVal->getParent() == 0 && "Value already has parent!");
115 NewVal->setParent(ItemParent);
118 if (NewVal->hasName() && Parent)
119 Parent->getSymbolTableSure()->insert(NewVal);
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);
131 //ValueList.push_front(Inst);
132 ValueList.insert(ValueList.begin(), Inst);
134 if (Inst->hasName() && Parent)
135 Parent->getSymbolTableSure()->insert(Inst);
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);
144 ValueList.push_back(Inst);
146 if (Inst->hasName() && Parent)
147 Parent->getSymbolTableSure()->insert(Inst);
150 // ValueHolder::insert - This method inserts the specified value *BEFORE* the
151 // indicated iterator position, and returns an interator to the newly inserted
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);
161 iterator I = ValueList.insert(Pos, Inst);
162 if (Inst->hasName() && Parent)
163 Parent->getSymbolTableSure()->insert(Inst);
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...
171 // FIXME: This is not generic so that the code does not have to be around
172 // to be used... is this ok?
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
179 // Check to make sure that the values are not already in some valueholder...
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);
187 // Add all of the values to the value holder...
188 ValueList.insert(Pos, First, Last);
190 // Insert all of the instructions in the symbol table...
192 for (;First != Last; ++First)
193 if ((*First)->hasName())
194 Parent->getSymbolTableSure()->insert(*First);