Registry.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //=== Registry.h - Linker-supported plugin registries -----------*- 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. // Defines a registry template for discovering pluggable modules.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_SUPPORT_REGISTRY_H
  13. #define LLVM_SUPPORT_REGISTRY_H
  14. #include "llvm/ADT/STLExtras.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/iterator_range.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/DynamicLibrary.h"
  19. #include <memory>
  20. namespace llvm {
  21. /// A simple registry entry which provides only a name, description, and
  22. /// no-argument constructor.
  23. template <typename T>
  24. class SimpleRegistryEntry {
  25. StringRef Name, Desc;
  26. std::unique_ptr<T> (*Ctor)();
  27. public:
  28. SimpleRegistryEntry(StringRef N, StringRef D, std::unique_ptr<T> (*C)())
  29. : Name(N), Desc(D), Ctor(C) {}
  30. StringRef getName() const { return Name; }
  31. StringRef getDesc() const { return Desc; }
  32. std::unique_ptr<T> instantiate() const { return Ctor(); }
  33. };
  34. /// A global registry used in conjunction with static constructors to make
  35. /// pluggable components (like targets or garbage collectors) "just work" when
  36. /// linked with an executable.
  37. template <typename T>
  38. class Registry {
  39. public:
  40. typedef T type;
  41. typedef SimpleRegistryEntry<T> entry;
  42. class node;
  43. class iterator;
  44. private:
  45. Registry() = delete;
  46. friend class node;
  47. static node *Head, *Tail;
  48. public:
  49. /// Node in linked list of entries.
  50. ///
  51. class node {
  52. friend class iterator;
  53. friend Registry<T>;
  54. node *Next;
  55. const entry& Val;
  56. public:
  57. node(const entry &V) : Next(nullptr), Val(V) {}
  58. };
  59. /// Add a node to the Registry: this is the interface between the plugin and
  60. /// the executable.
  61. ///
  62. /// This function is exported by the executable and called by the plugin to
  63. /// add a node to the executable's registry. Therefore it's not defined here
  64. /// to avoid it being instantiated in the plugin and is instead defined in
  65. /// the executable (see LLVM_INSTANTIATE_REGISTRY below).
  66. static void add_node(node *N);
  67. /// Iterators for registry entries.
  68. ///
  69. class iterator
  70. : public llvm::iterator_facade_base<iterator, std::forward_iterator_tag,
  71. const entry> {
  72. const node *Cur;
  73. public:
  74. explicit iterator(const node *N) : Cur(N) {}
  75. bool operator==(const iterator &That) const { return Cur == That.Cur; }
  76. iterator &operator++() { Cur = Cur->Next; return *this; }
  77. const entry &operator*() const { return Cur->Val; }
  78. };
  79. // begin is not defined here in order to avoid usage of an undefined static
  80. // data member, instead it's instantiated by LLVM_INSTANTIATE_REGISTRY.
  81. static iterator begin();
  82. static iterator end() { return iterator(nullptr); }
  83. static iterator_range<iterator> entries() {
  84. return make_range(begin(), end());
  85. }
  86. /// A static registration template. Use like such:
  87. ///
  88. /// Registry<Collector>::Add<FancyGC>
  89. /// X("fancy-gc", "Newfangled garbage collector.");
  90. ///
  91. /// Use of this template requires that:
  92. ///
  93. /// 1. The registered subclass has a default constructor.
  94. template <typename V>
  95. class Add {
  96. entry Entry;
  97. node Node;
  98. static std::unique_ptr<T> CtorFn() { return std::make_unique<V>(); }
  99. public:
  100. Add(StringRef Name, StringRef Desc)
  101. : Entry(Name, Desc, CtorFn), Node(Entry) {
  102. add_node(&Node);
  103. }
  104. };
  105. };
  106. } // end namespace llvm
  107. /// Instantiate a registry class.
  108. ///
  109. /// This provides template definitions of add_node, begin, and the Head and Tail
  110. /// pointers, then explicitly instantiates them. We could explicitly specialize
  111. /// them, instead of the two-step process of define then instantiate, but
  112. /// strictly speaking that's not allowed by the C++ standard (we would need to
  113. /// have explicit specialization declarations in all translation units where the
  114. /// specialization is used) so we don't.
  115. #define LLVM_INSTANTIATE_REGISTRY(REGISTRY_CLASS) \
  116. namespace llvm { \
  117. template<typename T> typename Registry<T>::node *Registry<T>::Head = nullptr;\
  118. template<typename T> typename Registry<T>::node *Registry<T>::Tail = nullptr;\
  119. template<typename T> \
  120. void Registry<T>::add_node(typename Registry<T>::node *N) { \
  121. if (Tail) \
  122. Tail->Next = N; \
  123. else \
  124. Head = N; \
  125. Tail = N; \
  126. } \
  127. template<typename T> typename Registry<T>::iterator Registry<T>::begin() { \
  128. return iterator(Head); \
  129. } \
  130. template REGISTRY_CLASS::node *Registry<REGISTRY_CLASS::type>::Head; \
  131. template REGISTRY_CLASS::node *Registry<REGISTRY_CLASS::type>::Tail; \
  132. template \
  133. void Registry<REGISTRY_CLASS::type>::add_node(REGISTRY_CLASS::node*); \
  134. template REGISTRY_CLASS::iterator Registry<REGISTRY_CLASS::type>::begin(); \
  135. }
  136. #endif // LLVM_SUPPORT_REGISTRY_H