Registry.h 5.3 KB

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