1 //=== Registry.h - Linker-supported plugin registries -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Defines a registry template for discovering pluggable modules.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_REGISTRY_H
15 #define LLVM_SUPPORT_REGISTRY_H
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/DynamicLibrary.h"
24 /// A simple registry entry which provides only a name, description, and
25 /// no-argument constructor.
27 class SimpleRegistryEntry {
28 const char *Name, *Desc;
29 std::unique_ptr<T> (*Ctor)();
32 SimpleRegistryEntry(const char *N, const char *D, std::unique_ptr<T> (*C)())
33 : Name(N), Desc(D), Ctor(C)
36 const char *getName() const { return Name; }
37 const char *getDesc() const { return Desc; }
38 std::unique_ptr<T> instantiate() const { return Ctor(); }
41 /// A global registry used in conjunction with static constructors to make
42 /// pluggable components (like targets or garbage collectors) "just work" when
43 /// linked with an executable.
47 typedef SimpleRegistryEntry<T> entry;
56 static node *Head, *Tail;
59 /// Node in linked list of entries.
62 friend class iterator;
69 node(const entry &V) : Next(nullptr), Val(V) {}
72 static void add_node(node *N) {
80 /// Iterators for registry entries.
86 explicit iterator(const node *N) : Cur(N) {}
88 bool operator==(const iterator &That) const { return Cur == That.Cur; }
89 bool operator!=(const iterator &That) const { return Cur != That.Cur; }
90 iterator &operator++() { Cur = Cur->Next; return *this; }
91 const entry &operator*() const { return Cur->Val; }
92 const entry *operator->() const { return &Cur->Val; }
95 static iterator begin() { return iterator(Head); }
96 static iterator end() { return iterator(nullptr); }
98 static iterator_range<iterator> entries() {
99 return make_range(begin(), end());
102 /// A static registration template. Use like such:
104 /// Registry<Collector>::Add<FancyGC>
105 /// X("fancy-gc", "Newfangled garbage collector.");
107 /// Use of this template requires that:
109 /// 1. The registered subclass has a default constructor.
110 template <typename V>
115 static std::unique_ptr<T> CtorFn() { return make_unique<V>(); }
118 Add(const char *Name, const char *Desc)
119 : Entry(Name, Desc, CtorFn), Node(Entry) {
124 /// A dynamic import facility. This is used on Windows to
125 /// import the entries added in the plugin.
126 static void import(sys::DynamicLibrary &DL, const char *RegistryName) {
127 typedef void *(*GetRegistry)();
128 std::string Name("LLVMGetRegistry_");
129 Name.append(RegistryName);
131 (GetRegistry)(intptr_t)DL.getAddressOfSymbol(Name.c_str());
133 // Call the getter function in order to get the full copy of the
134 // registry defined in the plugin DLL, and copy them over to the
136 typedef std::pair<const node *, const node *> Info;
137 Info *I = static_cast<Info *>(Getter());
138 iterator begin(I->first);
139 iterator end(I->second);
140 for (++end; begin != end; ++begin) {
141 // This Node object needs to remain alive for the
142 // duration of the program.
143 add_node(new node(*begin));
148 /// Retrieve the data to be passed across DLL boundaries when
149 /// importing registries from another DLL on Windows.
150 static void *exportRegistry() {
151 static std::pair<const node *, const node *> Info(Head, Tail);
157 // Since these are defined in a header file, plugins must be sure to export
159 template <typename T>
160 typename Registry<T>::node *Registry<T>::Head;
162 template <typename T>
163 typename Registry<T>::node *Registry<T>::Tail;
164 } // end namespace llvm
167 #define LLVM_EXPORT_REGISTRY(REGISTRY_CLASS) \
169 __declspec(dllexport) void *__cdecl LLVMGetRegistry_##REGISTRY_CLASS() { \
170 return REGISTRY_CLASS::exportRegistry(); \
173 #define LLVM_IMPORT_REGISTRY(REGISTRY_CLASS, DL) \
174 REGISTRY_CLASS::import(DL, #REGISTRY_CLASS)
176 #define LLVM_EXPORT_REGISTRY(REGISTRY_CLASS)
177 #define LLVM_IMPORT_REGISTRY(REGISTRY_CLASS, DL)
180 #endif // LLVM_SUPPORT_REGISTRY_H