1 //===-- llvm/SymbolTableListTraitsImpl.h - Implementation ------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_SYMBOLTABLELISTTRAITS_IMPL_H
17 #define LLVM_SYMBOLTABLELISTTRAITS_IMPL_H
19 #include "llvm/SymbolTableListTraits.h"
20 #include "llvm/ValueSymbolTable.h"
24 /// setSymTabObject - This is called when (f.e.) the parent of a basic block
25 /// changes. This requires us to remove all the instruction symtab entries from
26 /// the current function and reinsert them into the new function.
27 template<typename ValueSubClass, typename ItemParentClass>
28 template<typename TPtr>
29 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
30 ::setSymTabObject(TPtr *Dest, TPtr Src) {
31 // Get the old symtab and value list before doing the assignment.
32 ValueSymbolTable *OldST = TraitsClass::getSymTab(ItemParent);
37 // Get the new SymTab object.
38 ValueSymbolTable *NewST = TraitsClass::getSymTab(ItemParent);
40 // If there is nothing to do, quick exit.
41 if (OldST == NewST) return;
43 // Move all the elements from the old symtab to the new one.
44 iplist<ValueSubClass> &ItemList = TraitsClass::getList(ItemParent);
45 if (ItemList.empty()) return;
48 // Remove all entries from the previous symtab.
49 for (typename iplist<ValueSubClass>::iterator I = ItemList.begin();
50 I != ItemList.end(); ++I)
52 OldST->removeValueName(I->getValueName());
56 // Add all of the items to the new symtab.
57 for (typename iplist<ValueSubClass>::iterator I = ItemList.begin();
58 I != ItemList.end(); ++I)
60 NewST->reinsertValue(I);
65 template<typename ValueSubClass, typename ItemParentClass>
66 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
67 ::addNodeToList(ValueSubClass *V) {
68 assert(V->getParent() == 0 && "Value already in a container!!");
69 V->setParent(ItemParent);
71 if (ValueSymbolTable *ST = TraitsClass::getSymTab(ItemParent))
75 template<typename ValueSubClass, typename ItemParentClass>
76 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
77 ::removeNodeFromList(ValueSubClass *V) {
80 if (ValueSymbolTable *ST = TraitsClass::getSymTab(ItemParent))
81 ST->removeValueName(V->getValueName());
84 template<typename ValueSubClass, typename ItemParentClass>
85 void SymbolTableListTraits<ValueSubClass,ItemParentClass>
86 ::transferNodesFromList(iplist<ValueSubClass, ilist_traits<ValueSubClass> > &L2,
87 ilist_iterator<ValueSubClass> first,
88 ilist_iterator<ValueSubClass> last) {
89 // We only have to do work here if transferring instructions between BBs
90 ItemParentClass *NewIP = ItemParent, *OldIP = L2.ItemParent;
91 if (NewIP == OldIP) return; // No work to do at all...
93 // We only have to update symbol table entries if we are transferring the
94 // instructions to a different symtab object...
95 ValueSymbolTable *NewST = TraitsClass::getSymTab(ItemParent);
96 ValueSymbolTable *OldST = TraitsClass::getSymTab(OldIP);
98 for (; first != last; ++first) {
99 ValueSubClass &V = *first;
100 bool HasName = V.hasName();
101 if (OldST && HasName)
102 OldST->removeValueName(V.getValueName());
104 if (NewST && HasName)
105 NewST->reinsertValue(&V);
108 // Just transferring between blocks in the same function, simply update the
109 // parent fields in the instructions...
110 for (; first != last; ++first)
111 first->setParent(NewIP);
115 } // End llvm namespace