Rename ConstPoolVal -> Constant
[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 def 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 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
101 ::push_front(ValueSubclass *Inst) {
102   assert(Inst->getParent() == 0 && "Value already has parent!");
103   Inst->setParent(ItemParent);
104
105   //ValueList.push_front(Inst);
106   ValueList.insert(ValueList.begin(), Inst);
107  
108   if (Inst->hasName() && Parent)
109     Parent->getSymbolTableSure()->insert(Inst);
110 }
111
112 template<class ValueSubclass, class ItemParentType, class SymTabType>
113 void ValueHolder<ValueSubclass,ItemParentType,SymTabType>
114 ::push_back(ValueSubclass *Inst) {
115   assert(Inst->getParent() == 0 && "Value already has parent!");
116   Inst->setParent(ItemParent);
117
118   ValueList.push_back(Inst);
119   
120   if (Inst->hasName() && Parent)
121     Parent->getSymbolTableSure()->insert(Inst);
122 }
123
124 // ValueHolder::insert - This method inserts the specified value *BEFORE* the 
125 // indicated iterator position, and returns an interator to the newly inserted
126 // value.
127 //
128 template<class ValueSubclass, class ItemParentType, class SymTabType>
129 ValueHolder<ValueSubclass,ItemParentType,SymTabType>::iterator
130 ValueHolder<ValueSubclass,ItemParentType,SymTabType>
131 ::insert(iterator Pos, ValueSubclass *Inst) {
132   assert(Inst->getParent() == 0 && "Value already has parent!");
133   Inst->setParent(ItemParent);
134
135   iterator I = ValueList.insert(Pos, Inst);
136   if (Inst->hasName() && Parent)
137     Parent->getSymbolTableSure()->insert(Inst);
138   return I;
139 }
140
141 #endif