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