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