SymbolTableListTraits.h 4.7 KB

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