Initial revision
[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 specified
42 // by the iterator, and leaves the iterator pointing to the element that used to follow
43 // 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 void ValueHolder<ValueSubclass,ItemParentType>::push_front(ValueSubclass *Inst) {
64   assert(Inst->getParent() == 0 && "Value already has parent!");
65   Inst->setParent(ItemParent);
66
67   //ValueList.push_front(Inst);
68   ValueList.insert(ValueList.begin(), Inst);
69  
70   if (Inst->hasName() && Parent)
71     Parent->getSymbolTableSure()->insert(Inst);
72 }
73
74 template<class ValueSubclass, class ItemParentType>
75 void ValueHolder<ValueSubclass,ItemParentType>::push_back(ValueSubclass *Inst) {
76   assert(Inst->getParent() == 0 && "Value already has parent!");
77   Inst->setParent(ItemParent);
78
79   ValueList.push_back(Inst);
80   
81   if (Inst->hasName() && Parent)
82     Parent->getSymbolTableSure()->insert(Inst);
83 }
84
85 #endif