1 //===-- llvm/SymbolTableListTraits.h - Traits for iplist --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a generic class that is used to implement the automatic
11 // symbol table manipulation that occurs when you put (for example) a named
12 // instruction into a basic block.
13 //
14 // The way that this is implemented is by using a special traits class with the
15 // intrusive list that makes up the list of instructions in a basic block.  When
16 // a new element is added to the list of instructions, the traits class is
17 // notified, allowing the symbol table to be updated.
18 //
19 // This generic class implements the traits class.  It must be generic so that
20 // it can work for all uses it, which include lists of instructions, basic
21 // blocks, arguments, functions, global variables, etc...
22 //
23 //===----------------------------------------------------------------------===//
24 
25 #ifndef LLVM_IR_SYMBOLTABLELISTTRAITS_H
26 #define LLVM_IR_SYMBOLTABLELISTTRAITS_H
27 
28 #include "llvm/ADT/ilist.h"
29 
30 namespace llvm {
31 class ValueSymbolTable;
32 
33 template <typename NodeTy> class ilist_iterator;
34 template <typename NodeTy, typename Traits> class iplist;
35 template <typename Ty> struct ilist_traits;
36 
37 template <typename NodeTy>
38 struct SymbolTableListSentinelTraits
39     : public ilist_embedded_sentinel_traits<NodeTy> {};
40 
41 /// Template metafunction to get the parent type for a symbol table list.
42 ///
43 /// Implementations create a typedef called \c type so that we only need a
44 /// single template parameter for the list and traits.
45 template <typename NodeTy> struct SymbolTableListParentType {};
46 class Argument;
47 class BasicBlock;
48 class Function;
49 class Instruction;
50 class GlobalVariable;
51 class GlobalAlias;
52 class Module;
53 #define DEFINE_SYMBOL_TABLE_PARENT_TYPE(NODE, PARENT)                          \
54   template <> struct SymbolTableListParentType<NODE> { typedef PARENT type; };
55 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Instruction, BasicBlock)
56 DEFINE_SYMBOL_TABLE_PARENT_TYPE(BasicBlock, Function)
57 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Argument, Function)
58 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Function, Module)
59 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalVariable, Module)
60 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalAlias, Module)
61 #undef DEFINE_SYMBOL_TABLE_PARENT_TYPE
62 
63 template <typename NodeTy> class SymbolTableList;
64 
65 // ValueSubClass   - The type of objects that I hold, e.g. Instruction.
66 // ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
67 //
68 template <typename ValueSubClass>
69 class SymbolTableListTraits
70     : public ilist_nextprev_traits<ValueSubClass>,
71       public SymbolTableListSentinelTraits<ValueSubClass>,
72       public ilist_node_traits<ValueSubClass> {
73   typedef SymbolTableList<ValueSubClass> ListTy;
74   typedef
75       typename SymbolTableListParentType<ValueSubClass>::type ItemParentClass;
76 
77 public:
SymbolTableListTraits()78   SymbolTableListTraits() {}
79 
80 private:
81   /// getListOwner - Return the object that owns this list.  If this is a list
82   /// of instructions, it returns the BasicBlock that owns them.
getListOwner()83   ItemParentClass *getListOwner() {
84     size_t Offset(size_t(&((ItemParentClass*)nullptr->*ItemParentClass::
85                            getSublistAccess(static_cast<ValueSubClass*>(nullptr)))));
86     ListTy *Anchor(static_cast<ListTy *>(this));
87     return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
88                                               Offset);
89   }
90 
getList(ItemParentClass * Par)91   static ListTy &getList(ItemParentClass *Par) {
92     return Par->*(Par->getSublistAccess((ValueSubClass*)nullptr));
93   }
94 
getSymTab(ItemParentClass * Par)95   static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
96     return Par ? toPtr(Par->getValueSymbolTable()) : nullptr;
97   }
98 
99 public:
100   void addNodeToList(ValueSubClass *V);
101   void removeNodeFromList(ValueSubClass *V);
102   void transferNodesFromList(SymbolTableListTraits &L2,
103                              ilist_iterator<ValueSubClass> first,
104                              ilist_iterator<ValueSubClass> last);
105 //private:
106   template<typename TPtr>
107   void setSymTabObject(TPtr *, TPtr);
toPtr(ValueSymbolTable * P)108   static ValueSymbolTable *toPtr(ValueSymbolTable *P) { return P; }
toPtr(ValueSymbolTable & R)109   static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
110 };
111 
112 /// List that automatically updates parent links and symbol tables.
113 ///
114 /// When nodes are inserted into and removed from this list, the associated
115 /// symbol table will be automatically updated.  Similarly, parent links get
116 /// updated automatically.
117 template <typename NodeTy>
118 class SymbolTableList : public iplist<NodeTy, SymbolTableListTraits<NodeTy>> {};
119 
120 } // End llvm namespace
121 
122 #endif
123