@@ -53,7 +53,13 @@ namespace llvm {
5353 Registry () = delete ;
5454
5555 friend class node ;
56- static node *Head, *Tail;
56+ // These must be must two separate declarations to workaround a 20 year
57+ // old MSVC bug with dllexport and multiple static fields in the same
58+ // declaration causing error C2487 "member of dll interface class may not
59+ // be declared with dll interface".
60+ // https://developercommunity.visualstudio.com/t/c2487-in-dllexport-class-with-static-members/69878
61+ static node *Head;
62+ static node *Tail;
5763
5864 public:
5965 // / Node in linked list of entries.
@@ -76,7 +82,13 @@ namespace llvm {
7682 // / add a node to the executable's registry. Therefore it's not defined here
7783 // / to avoid it being instantiated in the plugin and is instead defined in
7884 // / the executable (see LLVM_INSTANTIATE_REGISTRY below).
79- static void add_node (node *N);
85+ static void add_node (node *N) {
86+ if (Tail)
87+ Tail->Next = N;
88+ else
89+ Head = N;
90+ Tail = N;
91+ }
8092
8193 // / Iterators for registry entries.
8294 // /
@@ -95,7 +107,7 @@ namespace llvm {
95107
96108 // begin is not defined here in order to avoid usage of an undefined static
97109 // data member, instead it's instantiated by LLVM_INSTANTIATE_REGISTRY.
98- static iterator begin ();
110+ static iterator begin () { return iterator (Head); }
99111 static iterator end () { return iterator (nullptr ); }
100112
101113 static iterator_range<iterator> entries () {
@@ -124,36 +136,28 @@ namespace llvm {
124136 }
125137 };
126138 };
139+
127140} // end namespace llvm
128141
142+ #ifdef _WIN32
129143// / Instantiate a registry class.
130- // /
131- // / This provides template definitions of add_node, begin, and the Head and Tail
132- // / pointers, then explicitly instantiates them. We could explicitly specialize
133- // / them, instead of the two-step process of define then instantiate, but
134- // / strictly speaking that's not allowed by the C++ standard (we would need to
135- // / have explicit specialization declarations in all translation units where the
136- // / specialization is used) so we don't.
137- #define LLVM_INSTANTIATE_REGISTRY (REGISTRY_CLASS ) \
138- namespace llvm { \
139- template <typename T> typename Registry<T>::node *Registry<T>::Head = nullptr ;\
140- template <typename T> typename Registry<T>::node *Registry<T>::Tail = nullptr ;\
141- template <typename T> \
142- void Registry<T>::add_node(typename Registry<T>::node *N) { \
143- if (Tail) \
144- Tail->Next = N; \
145- else \
146- Head = N; \
147- Tail = N; \
148- } \
149- template <typename T> typename Registry<T>::iterator Registry<T>::begin() { \
150- return iterator (Head); \
151- } \
152- template REGISTRY_CLASS::node *Registry<REGISTRY_CLASS::type>::Head; \
153- template REGISTRY_CLASS::node *Registry<REGISTRY_CLASS::type>::Tail; \
154- template \
155- void Registry<REGISTRY_CLASS::type>::add_node(REGISTRY_CLASS::node*); \
156- template REGISTRY_CLASS::iterator Registry<REGISTRY_CLASS::type>::begin(); \
144+ #define LLVM_INSTANTIATE_REGISTRY (REGISTRY_CLASS ) \
145+ namespace llvm { \
146+ template <typename T> \
147+ typename Registry<T>::node *Registry<T>::Head = nullptr ; \
148+ template <typename T> \
149+ typename Registry<T>::node *Registry<T>::Tail = nullptr ; \
150+ template class LLVM_ABI_EXPORT Registry<REGISTRY_CLASS::type>; \
151+ }
152+ #else
153+ #define LLVM_INSTANTIATE_REGISTRY (REGISTRY_CLASS ) \
154+ namespace llvm { \
155+ template <typename T> \
156+ typename Registry<T>::node *Registry<T>::Head = nullptr ; \
157+ template <typename T> \
158+ typename Registry<T>::node *Registry<T>::Tail = nullptr ; \
159+ template class Registry <REGISTRY_CLASS::type>; \
157160 }
161+ #endif
158162
159163#endif // LLVM_SUPPORT_REGISTRY_H
0 commit comments