MEGAPATCH checkin.
[oota-llvm.git] / lib / VMCore / SymbolTableListTraitsImpl.h
1 //===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
2 //
3 // This file implements the stickier parts of the SymbolTableListTraits class,
4 // and is explicitly instantiated where needed to avoid defining all this code
5 // in a widely used header.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_SYMBOLTABLELISTTRAITS_IMPL_H
10 #define LLVM_SYMBOLTABLELISTTRAITS_IMPL_H
11
12 #include "llvm/SymbolTableListTraits.h"
13 #include "llvm/SymbolTable.h"
14
15 template<typename ValueSubClass, typename ItemParentClass,typename SymTabClass,
16          typename SubClass>
17 void SymbolTableListTraits<ValueSubClass,ItemParentClass,SymTabClass,SubClass>
18 ::setParent(SymTabClass *STO) {
19   iplist<ValueSubClass> &List = SubClass::getList(ItemParent);
20
21   // Remove all of the items from the old symtab..
22   if (SymTabObject && !List.empty()) {
23     SymbolTable *SymTab = SymTabObject->getSymbolTable();
24     for (iplist<ValueSubClass>::iterator I = List.begin(); I != List.end(); ++I)
25       if (I->hasName()) SymTab->remove(I);
26   }
27
28   SymTabObject = STO;
29
30   // Add all of the items to the new symtab...
31   if (SymTabObject && !List.empty()) {
32     SymbolTable *SymTab = SymTabObject->getSymbolTableSure();
33     for (iplist<ValueSubClass>::iterator I = List.begin(); I != List.end(); ++I)
34       if (I->hasName()) SymTab->insert(I);
35   }
36 }
37
38 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
39          typename SubClass>
40 void SymbolTableListTraits<ValueSubClass,ItemParentClass,SymTabClass,SubClass>
41 ::addNodeToList(ValueSubClass *V) {
42   assert(V->getParent() == 0 && "Value already in a container!!");
43   V->setParent(ItemParent);
44   if (V->hasName() && SymTabObject)
45     SymTabObject->getSymbolTableSure()->insert(V);
46 }
47
48 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
49          typename SubClass>
50 void SymbolTableListTraits<ValueSubClass,ItemParentClass,SymTabClass,SubClass>
51 ::removeNodeFromList(ValueSubClass *V) {
52   V->setParent(0);
53   if (V->hasName() && SymTabObject)
54     SymTabObject->getSymbolTable()->remove(V);
55 }
56
57 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
58          typename SubClass>
59 void SymbolTableListTraits<ValueSubClass,ItemParentClass,SymTabClass,SubClass>
60 ::transferNodesFromList(iplist<ValueSubClass, ilist_traits<ValueSubClass> > &L2,
61                         ilist_iterator<ValueSubClass> first,
62                         ilist_iterator<ValueSubClass> last) {
63   // We only have to do work here if transfering instructions between BB's
64   ItemParentClass *NewIP = ItemParent, *OldIP = L2.ItemParent;
65   if (NewIP == OldIP) return;  // No work to do at all...
66
67   // We only have to update symbol table entries if we are transfering the
68   // instructions to a different symtab object...
69   SymTabClass *NewSTO = SymTabObject, *OldSTO = L2.SymTabObject;
70   if (NewSTO != OldSTO) {
71     for (; first != last; ++first) {
72       ValueSubClass &V = *first;
73       bool HasName = V.hasName();
74       if (OldSTO && HasName)
75         OldSTO->getSymbolTable()->remove(&V);
76       V.setParent(NewIP);
77       if (NewSTO && HasName)
78         NewSTO->getSymbolTableSure()->insert(&V);
79     }
80   } else {
81     // Just transfering between blocks in the same function, simply update the
82     // parent fields in the instructions...
83     for (; first != last; ++first)
84       first->setParent(NewIP);
85   }
86 }
87
88 #endif