Skip to content

Commit 3d83c67

Browse files
authored
Improve compatibility with mingw-w64 in generated headers (#1200)
1 parent 631c8f7 commit 3d83c67

File tree

12 files changed

+70
-43
lines changed

12 files changed

+70
-43
lines changed

cppwinrt/code_writers.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ namespace cppwinrt
348348
return;
349349
}
350350

351-
auto format = R"( template <%> struct __declspec(empty_bases) %;
351+
auto format = R"( template <%> struct WINRT_IMPL_EMPTY_BASES %;
352352
)";
353353

354354
w.write(format,
@@ -765,7 +765,7 @@ namespace cppwinrt
765765
{
766766
auto format = R"( template <> struct abi<%>
767767
{
768-
struct __declspec(novtable) type : inspectable_abi
768+
struct WINRT_IMPL_NOVTABLE type : inspectable_abi
769769
{
770770
)";
771771

@@ -775,7 +775,7 @@ namespace cppwinrt
775775
{
776776
auto format = R"( template <%> struct abi<%>
777777
{
778-
struct __declspec(novtable) type : inspectable_abi
778+
struct WINRT_IMPL_NOVTABLE type : inspectable_abi
779779
{
780780
)";
781781

@@ -816,7 +816,7 @@ namespace cppwinrt
816816
{
817817
auto format = R"( template <%> struct abi<%>
818818
{
819-
struct __declspec(novtable) type : unknown_abi
819+
struct WINRT_IMPL_NOVTABLE type : unknown_abi
820820
{
821821
virtual int32_t __stdcall Invoke(%) noexcept = 0;
822822
};
@@ -1962,7 +1962,7 @@ namespace cppwinrt
19621962
static void write_dispatch_overridable(writer& w, TypeDef const& class_type)
19631963
{
19641964
auto format = R"(template <typename T, typename D>
1965-
struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
1965+
struct WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable<T, D, %>
19661966
: produce_dispatch_to_overridable_base<T, D, %>
19671967
{
19681968
%};
@@ -2378,7 +2378,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
23782378

23792379
if (empty(generics))
23802380
{
2381-
auto format = R"( struct __declspec(empty_bases) % :
2381+
auto format = R"( struct WINRT_IMPL_EMPTY_BASES % :
23822382
winrt::Windows::Foundation::IInspectable,
23832383
impl::consume_t<%>%
23842384
{
@@ -2401,7 +2401,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
24012401
type_name = remove_tick(type_name);
24022402

24032403
auto format = R"( template <%>
2404-
struct __declspec(empty_bases) % :
2404+
struct WINRT_IMPL_EMPTY_BASES % :
24052405
winrt::Windows::Foundation::IInspectable,
24062406
impl::consume_t<%>%
24072407
{%
@@ -3147,7 +3147,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
31473147
auto type_name = type.TypeName();
31483148
auto factories = get_factories(w, type);
31493149

3150-
auto format = R"( struct __declspec(empty_bases) % : %%%
3150+
auto format = R"( struct WINRT_IMPL_EMPTY_BASES % : %%%
31513151
{
31523152
%(std::nullptr_t) noexcept {}
31533153
%(void* ptr, take_ownership_from_abi_t) noexcept : %(ptr, take_ownership_from_abi) {}
@@ -3172,7 +3172,7 @@ struct __declspec(empty_bases) produce_dispatch_to_overridable<T, D, %>
31723172
auto type_name = type.TypeName();
31733173
auto factories = get_factories(w, type);
31743174

3175-
auto format = R"( struct __declspec(empty_bases) % : %%
3175+
auto format = R"( struct WINRT_IMPL_EMPTY_BASES % : %%
31763176
{
31773177
%(std::nullptr_t) noexcept {}
31783178
%(void* ptr, take_ownership_from_abi_t) noexcept : %(ptr, take_ownership_from_abi) {}

cppwinrt/component_writers.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ catch (...) { return winrt::to_hresult(); }
743743
auto format = R"(namespace winrt::@::implementation
744744
{
745745
template <typename D%, typename... I>
746-
struct __declspec(empty_bases) %_base : implements<D, @::%%%, %I...>%%%
746+
struct WINRT_IMPL_EMPTY_BASES %_base : implements<D, @::%%%, %I...>%%%
747747
{
748748
using base_type = %_base;
749749
using class_type = @::%;
@@ -836,7 +836,7 @@ catch (...) { return winrt::to_hresult(); }
836836
auto format = R"(namespace winrt::@::factory_implementation
837837
{
838838
template <typename D, typename T, typename... I>
839-
struct __declspec(empty_bases) %T : implements<D, winrt::Windows::Foundation::IActivationFactory%, I...>
839+
struct WINRT_IMPL_EMPTY_BASES %T : implements<D, winrt::Windows::Foundation::IActivationFactory%, I...>
840840
{
841841
using instance_type = @::%;
842842

strings/base_abi.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ namespace winrt::impl
33
{
44
template <> struct abi<Windows::Foundation::IUnknown>
55
{
6-
struct __declspec(novtable) type
6+
struct WINRT_IMPL_NOVTABLE type
77
{
88
virtual int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept = 0;
99
virtual uint32_t __stdcall AddRef() noexcept = 0;
@@ -15,7 +15,7 @@ namespace winrt::impl
1515

1616
template <> struct abi<Windows::Foundation::IInspectable>
1717
{
18-
struct __declspec(novtable) type : unknown_abi
18+
struct WINRT_IMPL_NOVTABLE type : unknown_abi
1919
{
2020
virtual int32_t __stdcall GetIids(uint32_t* count, guid** ids) noexcept = 0;
2121
virtual int32_t __stdcall GetRuntimeClassName(void** name) noexcept = 0;
@@ -27,20 +27,20 @@ namespace winrt::impl
2727

2828
template <> struct abi<Windows::Foundation::IActivationFactory>
2929
{
30-
struct __declspec(novtable) type : inspectable_abi
30+
struct WINRT_IMPL_NOVTABLE type : inspectable_abi
3131
{
3232
virtual int32_t __stdcall ActivateInstance(void** instance) noexcept = 0;
3333
};
3434
};
3535

36-
struct __declspec(novtable) IAgileObject : unknown_abi {};
36+
struct WINRT_IMPL_NOVTABLE IAgileObject : unknown_abi {};
3737

38-
struct __declspec(novtable) IAgileReference : unknown_abi
38+
struct WINRT_IMPL_NOVTABLE IAgileReference : unknown_abi
3939
{
4040
virtual int32_t __stdcall Resolve(guid const& id, void** object) noexcept = 0;
4141
};
4242

43-
struct __declspec(novtable) IMarshal : unknown_abi
43+
struct WINRT_IMPL_NOVTABLE IMarshal : unknown_abi
4444
{
4545
virtual int32_t __stdcall GetUnmarshalClass(guid const& riid, void* pv, uint32_t dwDestContext, void* pvDestContext, uint32_t mshlflags, guid* pCid) noexcept = 0;
4646
virtual int32_t __stdcall GetMarshalSizeMax(guid const& riid, void* pv, uint32_t dwDestContext, void* pvDestContext, uint32_t mshlflags, uint32_t* pSize) noexcept = 0;
@@ -50,20 +50,20 @@ namespace winrt::impl
5050
virtual int32_t __stdcall DisconnectObject(uint32_t dwReserved) noexcept = 0;
5151
};
5252

53-
struct __declspec(novtable) IGlobalInterfaceTable : unknown_abi
53+
struct WINRT_IMPL_NOVTABLE IGlobalInterfaceTable : unknown_abi
5454
{
5555
virtual int32_t __stdcall RegisterInterfaceInGlobal(void* object, guid const& iid, uint32_t* cookie) noexcept = 0;
5656
virtual int32_t __stdcall RevokeInterfaceFromGlobal(uint32_t cookie) noexcept = 0;
5757
virtual int32_t __stdcall GetInterfaceFromGlobal(uint32_t cookie, guid const& iid, void** object) noexcept = 0;
5858
};
5959

60-
struct __declspec(novtable) IStaticLifetime : inspectable_abi
60+
struct WINRT_IMPL_NOVTABLE IStaticLifetime : inspectable_abi
6161
{
6262
virtual int32_t __stdcall unused() noexcept = 0;
6363
virtual int32_t __stdcall GetCollection(void** value) noexcept = 0;
6464
};
6565

66-
struct __declspec(novtable) IStaticLifetimeCollection : inspectable_abi
66+
struct WINRT_IMPL_NOVTABLE IStaticLifetimeCollection : inspectable_abi
6767
{
6868
virtual int32_t __stdcall Lookup(void*, void**) noexcept = 0;
6969
virtual int32_t __stdcall unused() noexcept = 0;
@@ -74,23 +74,23 @@ namespace winrt::impl
7474
virtual int32_t __stdcall unused4() noexcept = 0;
7575
};
7676

77-
struct __declspec(novtable) IWeakReference : unknown_abi
77+
struct WINRT_IMPL_NOVTABLE IWeakReference : unknown_abi
7878
{
7979
virtual int32_t __stdcall Resolve(guid const& iid, void** objectReference) noexcept = 0;
8080
};
8181

82-
struct __declspec(novtable) IWeakReferenceSource : unknown_abi
82+
struct WINRT_IMPL_NOVTABLE IWeakReferenceSource : unknown_abi
8383
{
8484
virtual int32_t __stdcall GetWeakReference(IWeakReference** weakReference) noexcept = 0;
8585
};
8686

87-
struct __declspec(novtable) IRestrictedErrorInfo : unknown_abi
87+
struct WINRT_IMPL_NOVTABLE IRestrictedErrorInfo : unknown_abi
8888
{
8989
virtual int32_t __stdcall GetErrorDetails(bstr* description, int32_t* error, bstr* restrictedDescription, bstr* capabilitySid) noexcept = 0;
9090
virtual int32_t __stdcall GetReference(bstr* reference) noexcept = 0;
9191
};
9292

93-
struct __declspec(novtable) IErrorInfo : unknown_abi
93+
struct WINRT_IMPL_NOVTABLE IErrorInfo : unknown_abi
9494
{
9595
virtual int32_t __stdcall GetGUID(guid* value) noexcept = 0;
9696
virtual int32_t __stdcall GetSource(bstr* value) noexcept = 0;
@@ -99,7 +99,7 @@ namespace winrt::impl
9999
virtual int32_t __stdcall GetHelpContext(uint32_t* value) noexcept = 0;
100100
};
101101

102-
struct __declspec(novtable) ILanguageExceptionErrorInfo2 : unknown_abi
102+
struct WINRT_IMPL_NOVTABLE ILanguageExceptionErrorInfo2 : unknown_abi
103103
{
104104
virtual int32_t __stdcall GetLanguageException(void** exception) noexcept = 0;
105105
virtual int32_t __stdcall GetPreviousLanguageExceptionErrorInfo(ILanguageExceptionErrorInfo2** previous) noexcept = 0;
@@ -109,25 +109,25 @@ namespace winrt::impl
109109

110110
struct ICallbackWithNoReentrancyToApplicationSTA;
111111

112-
struct __declspec(novtable) IContextCallback : unknown_abi
112+
struct WINRT_IMPL_NOVTABLE IContextCallback : unknown_abi
113113
{
114114
virtual int32_t __stdcall ContextCallback(int32_t(__stdcall* callback)(com_callback_args*), com_callback_args* args, guid const& iid, int method, void* reserved) noexcept = 0;
115115
};
116116

117-
struct __declspec(novtable) IServerSecurity : unknown_abi
117+
struct WINRT_IMPL_NOVTABLE IServerSecurity : unknown_abi
118118
{
119119
virtual int32_t __stdcall QueryBlanket(uint32_t*, uint32_t*, wchar_t**, uint32_t*, uint32_t*, void**, uint32_t*) noexcept = 0;
120120
virtual int32_t __stdcall ImpersonateClient() noexcept = 0;
121121
virtual int32_t __stdcall RevertToSelf() noexcept = 0;
122122
virtual int32_t __stdcall IsImpersonating() noexcept = 0;
123123
};
124124

125-
struct __declspec(novtable) IBufferByteAccess : unknown_abi
125+
struct WINRT_IMPL_NOVTABLE IBufferByteAccess : unknown_abi
126126
{
127127
virtual int32_t __stdcall Buffer(uint8_t** value) noexcept = 0;
128128
};
129129

130-
struct __declspec(novtable) IMemoryBufferByteAccess : unknown_abi
130+
struct WINRT_IMPL_NOVTABLE IMemoryBufferByteAccess : unknown_abi
131131
{
132132
virtual int32_t __stdcall GetBuffer(uint8_t** value, uint32_t* capacity) noexcept = 0;
133133
};

strings/base_composable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace winrt::impl
2727
};
2828

2929
template <typename T, typename D, typename I>
30-
class __declspec(empty_bases) produce_dispatch_to_overridable_base
30+
class WINRT_IMPL_EMPTY_BASES produce_dispatch_to_overridable_base
3131
{
3232
protected:
3333
D& shim() noexcept

strings/base_delegate.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace winrt::impl
9191
}
9292

9393
template <typename R, typename... Args>
94-
struct __declspec(novtable) variadic_delegate_abi : unknown_abi
94+
struct WINRT_IMPL_NOVTABLE variadic_delegate_abi : unknown_abi
9595
{
9696
virtual R invoke(Args const& ...) = 0;
9797
};
@@ -151,7 +151,7 @@ namespace winrt::impl
151151
};
152152

153153
template <typename R, typename... Args>
154-
struct __declspec(empty_bases) delegate_base : Windows::Foundation::IUnknown
154+
struct WINRT_IMPL_EMPTY_BASES delegate_base : Windows::Foundation::IUnknown
155155
{
156156
delegate_base(std::nullptr_t = nullptr) noexcept {}
157157
delegate_base(void* ptr, take_ownership_from_abi_t) noexcept : IUnknown(ptr, take_ownership_from_abi) {}
@@ -201,13 +201,13 @@ namespace winrt::impl
201201
WINRT_EXPORT namespace winrt
202202
{
203203
template <typename... Args>
204-
struct __declspec(empty_bases) delegate : impl::delegate_base<void, Args...>
204+
struct WINRT_IMPL_EMPTY_BASES delegate : impl::delegate_base<void, Args...>
205205
{
206206
using impl::delegate_base<void, Args...>::delegate_base;
207207
};
208208

209209
template <typename R, typename... Args>
210-
struct __declspec(empty_bases) delegate<R(Args...)> : impl::delegate_base<R, Args...>
210+
struct WINRT_IMPL_EMPTY_BASES delegate<R(Args...)> : impl::delegate_base<R, Args...>
211211
{
212212
using impl::delegate_base<R, Args...>::delegate_base;
213213
};

strings/base_error.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
#if defined(_MSC_VER)
3-
#include <intrin.h>
43
#define WINRT_IMPL_RETURNADDRESS() _ReturnAddress()
54
#elif defined(__GNUC__)
65
#define WINRT_IMPL_RETURNADDRESS() __builtin_extract_return_addr(__builtin_return_address(0))

strings/base_extern.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ extern "C"
8383
int32_t __stdcall WINRT_GetActivationFactory(void* classId, void** factory) noexcept;
8484
}
8585

86+
#if defined(_MSC_VER)
8687
#ifdef _M_HYBRID
8788
#define WINRT_IMPL_LINK(function, count) __pragma(comment(linker, "/alternatename:#WINRT_IMPL_" #function "@" #count "=#" #function "@" #count))
8889
#elif _M_ARM64EC
@@ -92,6 +93,13 @@ extern "C"
9293
#else
9394
#define WINRT_IMPL_LINK(function, count) __pragma(comment(linker, "/alternatename:WINRT_IMPL_" #function "=" #function))
9495
#endif
96+
#elif defined(__GNUC__)
97+
#if defined(__i386__)
98+
#define WINRT_IMPL_LINK(function, count) __asm__(".weak _WINRT_IMPL_" #function "@" #count "\n.set _WINRT_IMPL_" #function "@" #count ", _" #function "@" #count);
99+
#else
100+
#define WINRT_IMPL_LINK(function, count) __asm__(".weak WINRT_IMPL_" #function "\n.set WINRT_IMPL_" #function ", " #function);
101+
#endif
102+
#endif
95103

96104
WINRT_IMPL_LINK(LoadLibraryW, 4)
97105
WINRT_IMPL_LINK(FreeLibrary, 4)

strings/base_fast_forward.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
#define WINRT_IMPL_STRING_1(expression) #expression
55
#define WINRT_IMPL_STRING(expression) WINRT_IMPL_STRING_1(expression)
66

7+
#if defined(_MSC_VER)
8+
#define WINRT_IMPL_FF_NOVTABLE __declspec(novtable)
9+
#else
10+
#define WINRT_IMPL_FF_NOVTABLE
11+
#endif
12+
713
#if !defined(WINRT_FAST_ABI_SIZE)
814
#define WINRT_FAST_ABI_SIZE %
915
#endif
@@ -30,7 +36,7 @@ namespace winrt::impl
3036
}
3137
};
3238

33-
struct __declspec(novtable) inspectable
39+
struct WINRT_IMPL_FF_NOVTABLE inspectable
3440
{
3541
virtual int32_t __stdcall QueryInterface(guid const& id, void** object) noexcept = 0;
3642
virtual uint32_t __stdcall AddRef() noexcept = 0;
@@ -130,3 +136,4 @@ namespace winrt
130136

131137
#undef WINRT_IMPL_STRING
132138
#undef WINRT_IMPL_STRING_1
139+
#undef WINRT_IMPL_FF_NOVTABLE

strings/base_implements.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -749,15 +749,15 @@ namespace winrt::impl
749749
};
750750

751751
template <bool>
752-
struct __declspec(empty_bases) root_implements_composing_outer
752+
struct WINRT_IMPL_EMPTY_BASES root_implements_composing_outer
753753
{
754754
protected:
755755
static constexpr bool is_composing = false;
756756
static constexpr inspectable_abi* m_inner = nullptr;
757757
};
758758

759759
template <>
760-
struct __declspec(empty_bases) root_implements_composing_outer<true>
760+
struct WINRT_IMPL_EMPTY_BASES root_implements_composing_outer<true>
761761
{
762762
template <typename Qi>
763763
auto try_as() const noexcept
@@ -775,7 +775,7 @@ namespace winrt::impl
775775
};
776776

777777
template <typename D, bool>
778-
struct __declspec(empty_bases) root_implements_composable_inner
778+
struct WINRT_IMPL_EMPTY_BASES root_implements_composable_inner
779779
{
780780
protected:
781781
static constexpr inspectable_abi* outer() noexcept { return nullptr; }
@@ -785,7 +785,7 @@ namespace winrt::impl
785785
};
786786

787787
template <typename D>
788-
struct __declspec(empty_bases) root_implements_composable_inner<D, true> : producer<D, INonDelegatingInspectable>
788+
struct WINRT_IMPL_EMPTY_BASES root_implements_composable_inner<D, true> : producer<D, INonDelegatingInspectable>
789789
{
790790
protected:
791791
inspectable_abi* outer() noexcept { return m_outer; }
@@ -800,7 +800,7 @@ namespace winrt::impl
800800
};
801801

802802
template <typename D, typename... I>
803-
struct __declspec(novtable) root_implements
803+
struct WINRT_IMPL_NOVTABLE root_implements
804804
: root_implements_composing_outer<std::disjunction_v<std::is_same<composing, I>...>>
805805
, root_implements_composable_inner<D, std::disjunction_v<std::is_same<composable, I>...>>
806806
, module_lock_updater<!std::disjunction_v<std::is_same<no_module_lock, I>...>>

strings/base_includes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
#include <intrin.h>
23
#include <algorithm>
34
#include <array>
45
#include <atomic>

0 commit comments

Comments
 (0)