SymbolTableListTraits.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 uses it, which include lists of instructions, basic
  20. // blocks, arguments, functions, global variables, etc...
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_IR_SYMBOLTABLELISTTRAITS_H
  24. #define LLVM_IR_SYMBOLTABLELISTTRAITS_H
  25. #include "llvm/ADT/ilist.h"
  26. #include "llvm/ADT/simple_ilist.h"
  27. #include <cstddef>
  28. namespace llvm {
  29. class Argument;
  30. class BasicBlock;
  31. class Function;
  32. class GlobalAlias;
  33. class GlobalIFunc;
  34. class GlobalVariable;
  35. class Instruction;
  36. class Module;
  37. class ValueSymbolTable;
  38. /// Template metafunction to get the parent type for a symbol table list.
  39. ///
  40. /// Implementations create a typedef called \c type so that we only need a
  41. /// single template parameter for the list and traits.
  42. template <typename NodeTy> struct SymbolTableListParentType {};
  43. #define DEFINE_SYMBOL_TABLE_PARENT_TYPE(NODE, PARENT) \
  44. template <> struct SymbolTableListParentType<NODE> { using type = PARENT; };
  45. DEFINE_SYMBOL_TABLE_PARENT_TYPE(Instruction, BasicBlock)
  46. DEFINE_SYMBOL_TABLE_PARENT_TYPE(BasicBlock, Function)
  47. DEFINE_SYMBOL_TABLE_PARENT_TYPE(Argument, Function)
  48. DEFINE_SYMBOL_TABLE_PARENT_TYPE(Function, Module)
  49. DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalVariable, Module)
  50. DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalAlias, Module)
  51. DEFINE_SYMBOL_TABLE_PARENT_TYPE(GlobalIFunc, Module)
  52. #undef DEFINE_SYMBOL_TABLE_PARENT_TYPE
  53. template <typename NodeTy> class SymbolTableList;
  54. // ValueSubClass - The type of objects that I hold, e.g. Instruction.
  55. // ItemParentClass - The type of object that owns the list, e.g. BasicBlock.
  56. //
  57. template <typename ValueSubClass>
  58. class SymbolTableListTraits : public ilist_alloc_traits<ValueSubClass> {
  59. using ListTy = SymbolTableList<ValueSubClass>;
  60. using iterator = typename simple_ilist<ValueSubClass>::iterator;
  61. using ItemParentClass =
  62. typename SymbolTableListParentType<ValueSubClass>::type;
  63. public:
  64. SymbolTableListTraits() = default;
  65. private:
  66. /// getListOwner - Return the object that owns this list. If this is a list
  67. /// of instructions, it returns the BasicBlock that owns them.
  68. ItemParentClass *getListOwner() {
  69. size_t Offset = reinterpret_cast<size_t>(
  70. &((ItemParentClass *)nullptr->*ItemParentClass::getSublistAccess(
  71. static_cast<ValueSubClass *>(
  72. nullptr))));
  73. ListTy *Anchor = static_cast<ListTy *>(this);
  74. return reinterpret_cast<ItemParentClass*>(reinterpret_cast<char*>(Anchor)-
  75. Offset);
  76. }
  77. static ListTy &getList(ItemParentClass *Par) {
  78. return Par->*(Par->getSublistAccess((ValueSubClass*)nullptr));
  79. }
  80. static ValueSymbolTable *getSymTab(ItemParentClass *Par) {
  81. return Par ? toPtr(Par->getValueSymbolTable()) : nullptr;
  82. }
  83. public:
  84. void addNodeToList(ValueSubClass *V);
  85. void removeNodeFromList(ValueSubClass *V);
  86. void transferNodesFromList(SymbolTableListTraits &L2, iterator first,
  87. iterator last);
  88. // private:
  89. template<typename TPtr>
  90. void setSymTabObject(TPtr *, TPtr);
  91. static ValueSymbolTable *toPtr(ValueSymbolTable *P) { return P; }
  92. static ValueSymbolTable *toPtr(ValueSymbolTable &R) { return &R; }
  93. };
  94. /// List that automatically updates parent links and symbol tables.
  95. ///
  96. /// When nodes are inserted into and removed from this list, the associated
  97. /// symbol table will be automatically updated. Similarly, parent links get
  98. /// updated automatically.
  99. template <class T>
  100. class SymbolTableList
  101. : public iplist_impl<simple_ilist<T>, SymbolTableListTraits<T>> {};
  102. } // end namespace llvm
  103. #endif // LLVM_IR_SYMBOLTABLELISTTRAITS_H