* Rename get.*Operator to create seeing that it would have to be qualified
[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>
17 void ValueHolder<ValueSubclass,ItemParentType>::setParent(SymTabValue *P) { 
18   if (Parent) {     // Remove all of the items from the old symbol table..
19     SymbolTable *SymTab = Parent->getSymbolTable();
20     for (iterator I = begin(); I != end(); I++)
21       if ((*I)->hasName()) SymTab->remove(*I);
22   }
23
24   Parent = P; 
25
26   if (Parent) {     // Remove all of the items from the old symbol table..
27     SymbolTable *SymTab = Parent->getSymbolTableSure();
28     for (iterator I = begin(); I != end(); I++)
29       if ((*I)->hasName()) SymTab->insert(*I);
30   }
31 }
32
33
34 template<class ValueSubclass, class ItemParentType>
35 void ValueHolder<ValueSubclass,ItemParentType>::remove(ValueSubclass *D) {
36   iterator I(find(begin(), end(), D));
37   assert(I != end() && "Value not in ValueHolder!!");
38   remove(I);
39 }
40
41 // ValueHolder::remove(iterator &) this removes the element at the location
42 // specified by the iterator, and leaves the iterator pointing to the element
43 // that used to follow the element deleted.
44 //
45 template<class ValueSubclass, class ItemParentType>
46 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType>::remove(iterator &DI) {
47   assert(DI != ValueList.end() && 
48          "Trying to remove the end of the def list!!!");
49   
50   ValueSubclass *i = *DI;
51   DI = ValueList.erase(DI);
52
53   i->setParent(0);  // I don't own you anymore... byebye...
54   
55   // You don't get to be in the symbol table anymore... byebye
56   if (i->hasName() && Parent)
57     Parent->getSymbolTable()->remove(i);
58   
59   return i;
60 }
61
62 template<class ValueSubclass, class ItemParentType>
63 ValueSubclass *ValueHolder<ValueSubclass,ItemParentType>
64 ::remove(const iterator &DI) {
65   assert(DI != ValueList.end() && 
66          "Trying to remove the end of the def list!!!");
67   
68   ValueSubclass *i = *DI;
69   ValueList.erase(DI);
70
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 template<class ValueSubclass, class ItemParentType>
81 void ValueHolder<ValueSubclass,ItemParentType>::push_front(ValueSubclass *Inst) {
82   assert(Inst->getParent() == 0 && "Value already has parent!");
83   Inst->setParent(ItemParent);
84
85   //ValueList.push_front(Inst);
86   ValueList.insert(ValueList.begin(), Inst);
87  
88   if (Inst->hasName() && Parent)
89     Parent->getSymbolTableSure()->insert(Inst);
90 }
91
92 template<class ValueSubclass, class ItemParentType>
93 void ValueHolder<ValueSubclass,ItemParentType>::push_back(ValueSubclass *Inst) {
94   assert(Inst->getParent() == 0 && "Value already has parent!");
95   Inst->setParent(ItemParent);
96
97   ValueList.push_back(Inst);
98   
99   if (Inst->hasName() && Parent)
100     Parent->getSymbolTableSure()->insert(Inst);
101 }
102
103 // ValueHolder::insert - This method inserts the specified value *BEFORE* the 
104 // indicated iterator position, and returns an interator to the newly inserted
105 // value.
106 //
107 template<class ValueSubclass, class ItemParentType>
108 ValueHolder<ValueSubclass,ItemParentType>::iterator
109 ValueHolder<ValueSubclass,ItemParentType>::insert(iterator Pos,
110                                                            ValueSubclass *Inst){
111   assert(Inst->getParent() == 0 && "Value already has parent!");
112   Inst->setParent(ItemParent);
113
114   iterator I = ValueList.insert(Pos, Inst);
115   if (Inst->hasName() && Parent)
116     Parent->getSymbolTableSure()->insert(Inst);
117   return I;
118 }
119
120 #endif