]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/SymbolTableListTraits.h
MFV r314911: 7867 ARC space accounting leak
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / IR / SymbolTableListTraits.h
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 metafunction to get the parent type for a symbol table list.
34 ///
35 /// Implementations create a typedef called \c type so that we only need a
36 /// single template parameter for the list and traits.
37 template <typename NodeTy> struct SymbolTableListParentType {};
38 class Argument;
39 class BasicBlock;
40 class Function;
41 class Instruction;
42 class GlobalVariable;
43 class GlobalAlias;
44 class GlobalIFunc;
45 class Module;
46 #define DEFINE_SYMBOL_TABLE_PARENT_TYPE(NODE, PARENT)                          \
47   template <> struct SymbolTableListParentType<NODE> { typedef PARENT type; };
48 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Instruction, BasicBlock)
49 DEFINE_SYMBOL_TABLE_PARENT_TYPE(BasicBlock, Function)
50 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Argument, Function)
51 DEFINE_SYMBOL_TABLE_PARENT_TYPE(Function, Module)
52 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalVariable, Module)
53 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalAlias, Module)
54 DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalIFunc, Module)
55 #undef DEFINE_SYMBOL_TABLE_PARENT_TYPE
56
57 template <typename NodeTy> class SymbolTableList;
58
59 // ValueSubClass   - The type of objects that I hold, e.g. Instruction.
60 // ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
61 //
62 template <typename ValueSubClass>
63 class SymbolTableListTraits : public ilist_alloc_traits<ValueSubClass> {
64   typedef SymbolTableList<ValueSubClass> ListTy;
65   typedef typename simple_ilist<ValueSubClass>::iterator iterator;
66   typedef
67       typename SymbolTableListParentType<ValueSubClass>::type ItemParentClass;
68
69 public:
70   SymbolTableListTraits() {}
71
72 private:
73   /// getListOwner - Return the object that owns this list.  If this is a list
74   /// of instructions, it returns the BasicBlock that owns them.
75   ItemParentClass *getListOwner() {
76     size_t Offset(size_t(&((ItemParentClass*)nullptr->*ItemParentClass::
77                            getSublistAccess(static_cast<ValueSubClass*>(nullptr)))));
78     ListTy *Anchor(static_cast<ListTy *>(this));
79     return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
80                                               Offset);
81   }
82
83   static ListTy &getList(ItemParentClass *Par) {
84     return Par->*(Par->getSublistAccess((ValueSubClass*)nullptr));
85   }
86
87   static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
88     return Par ? toPtr(Par->getValueSymbolTable()) : nullptr;
89   }
90
91 public:
92   void addNodeToList(ValueSubClass *V);
93   void removeNodeFromList(ValueSubClass *V);
94   void transferNodesFromList(SymbolTableListTraits &L2, iterator first,
95                              iterator last);
96   // private:
97   template<typename TPtr>
98   void setSymTabObject(TPtr *, TPtr);
99   static ValueSymbolTable *toPtr(ValueSymbolTable *P) { return P; }
100   static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
101 };
102
103 /// List that automatically updates parent links and symbol tables.
104 ///
105 /// When nodes are inserted into and removed from this list, the associated
106 /// symbol table will be automatically updated.  Similarly, parent links get
107 /// updated automatically.
108 template <class T>
109 class SymbolTableList
110     : public iplist_impl<simple_ilist<T>, SymbolTableListTraits<T>> {};
111
112 } // End llvm namespace
113
114 #endif