|
15 | 15 | #ifndef LLVM_SUPPORT_WIN_ADAPTER_H |
16 | 16 | #define LLVM_SUPPORT_WIN_ADAPTER_H |
17 | 17 |
|
18 | | -#ifndef _WIN32 |
| 18 | +#ifndef _MSC_VER |
19 | 19 |
|
20 | 20 | #ifdef __cplusplus |
21 | 21 | #include <atomic> |
|
33 | 33 | #include <vector> |
34 | 34 | #endif // __cplusplus |
35 | 35 |
|
| 36 | +#ifdef __MINGW32__ |
| 37 | + |
| 38 | +#undef _WIN32_WINNT |
| 39 | +#undef _WIN32_IE |
| 40 | + |
| 41 | +// Require at least Windows 7 (Updated from XP) |
| 42 | +#define _WIN32_WINNT 0x0601 |
| 43 | +#define _WIN32_IE 0x0800 |
| 44 | + |
| 45 | +#define WIN32_LEAN_AND_MEAN |
| 46 | +#define STRSAFE_NO_DEPRECATE |
| 47 | + |
| 48 | +#include <intsafe.h> |
| 49 | +#include <objidl.h> |
| 50 | +#include <sal.h> |
| 51 | +#include <strsafe.h> |
| 52 | +#include <unknwn.h> |
| 53 | +#include <windows.h> |
| 54 | + |
| 55 | +#undef EN |
| 56 | +#undef IN |
| 57 | +#undef OUT |
| 58 | + |
| 59 | +#ifdef MemoryFence |
| 60 | +#undef MemoryFence |
| 61 | +#endif |
| 62 | + |
| 63 | +#define EventRegisterMicrosoft_Windows_DXCompiler_API() |
| 64 | +#define EventRegisterMicrosoft_Windows_DxcRuntime_API() |
| 65 | +#define EventUnregisterMicrosoft_Windows_DXCompiler_API() |
| 66 | +#define EventUnregisterMicrosoft_Windows_DxcRuntime_API() |
| 67 | + |
| 68 | +#define DxcEtw_DXCompilerCompile_Start() |
| 69 | +#define DxcEtw_DXCompilerCompile_Stop(hr) |
| 70 | +#define DxcEtw_DXCompilerCreateInstance_Start() |
| 71 | +#define DxcEtw_DXCompilerCreateInstance_Stop(hr) |
| 72 | +#define DxcEtw_DXCompilerDisassemble_Start() |
| 73 | +#define DxcEtw_DXCompilerDisassemble_Stop(hr) |
| 74 | +#define DxcEtw_DXCompilerInitialization_Start() |
| 75 | +#define DxcEtw_DXCompilerInitialization_Stop(hr) |
| 76 | +#define DxcEtw_DXCompilerPreprocess_Start() |
| 77 | +#define DxcEtw_DXCompilerPreprocess_Stop(hr) |
| 78 | +#define DxcEtw_DXCompilerShutdown_Start() |
| 79 | +#define DxcEtw_DXCompilerShutdown_Stop(hr) |
| 80 | +#define DxcEtw_DxcValidation_Start() |
| 81 | +#define DxcEtw_DxcValidation_Stop(hr) |
| 82 | +#define DxcRuntimeEtw_DxcRuntimeInitialization_Start() |
| 83 | +#define DxcRuntimeEtw_DxcRuntimeInitialization_Stop(HR) |
| 84 | +#define DxcRuntimeEtw_DxcRuntimeShutdown_Start() |
| 85 | +#define DxcRuntimeEtw_DxcRuntimeShutdown_Stop(S_OK) |
| 86 | + |
| 87 | +#define ATLASSERT assert |
| 88 | +#define ATLASSUME(expr) \ |
| 89 | + do { \ |
| 90 | + ATLASSERT(expr); \ |
| 91 | + __analysis_assume(!!(expr)); \ |
| 92 | + } while (0) |
| 93 | +#define ATLENSURE_THROW(expr, hr) \ |
| 94 | + do { \ |
| 95 | + int __atl_condVal = !!(expr); \ |
| 96 | + ATLASSUME(__atl_condVal); \ |
| 97 | + if (!(__atl_condVal)) \ |
| 98 | + throw(hr); \ |
| 99 | + } while (0) |
| 100 | + |
| 101 | +// Disable all calling conventions |
| 102 | +#define __cdecl |
| 103 | +#define __stdcall |
| 104 | +#define __vectorcall |
| 105 | +#define __thiscall |
| 106 | +#define __fastcall |
| 107 | +#define __clrcall |
| 108 | + |
| 109 | +// FIXME: missing from intsafe.h |
| 110 | +#define Int32ToUInt32 IntToUInt |
| 111 | +#define UInt32Add UIntAdd |
| 112 | +#define UInt32Mult UIntMult |
| 113 | + |
| 114 | +// FIXME: missing from sal.h |
| 115 | +#define _Maybenull_ |
| 116 | +#define _Notnull_ |
| 117 | + |
| 118 | +// FIXME: missing from d3d12shader.h |
| 119 | +#define D3D_SHADER_REQUIRES_EARLY_DEPTH_STENCIL 0x00000002 |
| 120 | +#define D3D12_SHVER_GET_TYPE(_Version) (((_Version) >> 16) & 0xffff) |
| 121 | +#define D3D12_SHVER_GET_MAJOR(_Version) (((_Version) >> 4) & 0xf) |
| 122 | +#define D3D12_SHVER_GET_MINOR(_Version) (((_Version) >> 0) & 0xf) |
| 123 | + |
| 124 | +#ifdef __cplusplus |
| 125 | + |
| 126 | +constexpr uint8_t nybble_from_hex(char c) { |
| 127 | + return ((c >= '0' && c <= '9') |
| 128 | + ? (c - '0') |
| 129 | + : ((c >= 'a' && c <= 'f') |
| 130 | + ? (c - 'a' + 10) |
| 131 | + : ((c >= 'A' && c <= 'F') ? (c - 'A' + 10) |
| 132 | + : /* Should be an error */ -1))); |
| 133 | +} |
| 134 | + |
| 135 | +constexpr uint8_t byte_from_hex(char c1, char c2) { |
| 136 | + return nybble_from_hex(c1) << 4 | nybble_from_hex(c2); |
| 137 | +} |
| 138 | + |
| 139 | +constexpr uint8_t byte_from_hexstr(const char str[2]) { |
| 140 | + return nybble_from_hex(str[0]) << 4 | nybble_from_hex(str[1]); |
| 141 | +} |
| 142 | + |
| 143 | +constexpr GUID guid_from_string(const char str[37]) { |
| 144 | + return GUID{static_cast<uint32_t>(byte_from_hexstr(str)) << 24 | |
| 145 | + static_cast<uint32_t>(byte_from_hexstr(str + 2)) << 16 | |
| 146 | + static_cast<uint32_t>(byte_from_hexstr(str + 4)) << 8 | |
| 147 | + byte_from_hexstr(str + 6), |
| 148 | + static_cast<uint16_t>( |
| 149 | + static_cast<uint16_t>(byte_from_hexstr(str + 9)) << 8 | |
| 150 | + byte_from_hexstr(str + 11)), |
| 151 | + static_cast<uint16_t>( |
| 152 | + static_cast<uint16_t>(byte_from_hexstr(str + 14)) << 8 | |
| 153 | + byte_from_hexstr(str + 16)), |
| 154 | + {byte_from_hexstr(str + 19), byte_from_hexstr(str + 21), |
| 155 | + byte_from_hexstr(str + 24), byte_from_hexstr(str + 26), |
| 156 | + byte_from_hexstr(str + 28), byte_from_hexstr(str + 30), |
| 157 | + byte_from_hexstr(str + 32), byte_from_hexstr(str + 34)}}; |
| 158 | +} |
| 159 | + |
| 160 | +#define CROSS_PLATFORM_UUIDOF(name, spec) \ |
| 161 | + struct __declspec(uuid(spec)) name; \ |
| 162 | + extern "C++" { \ |
| 163 | + template <> struct __mingw_uuidof_s<name> { \ |
| 164 | + static constexpr IID __uuid_inst = guid_from_string(spec); \ |
| 165 | + }; \ |
| 166 | + template <> constexpr const GUID &__mingw_uuidof<name>() { \ |
| 167 | + return __mingw_uuidof_s<name>::__uuid_inst; \ |
| 168 | + } \ |
| 169 | + template <> constexpr const GUID &__mingw_uuidof<name *>() { \ |
| 170 | + return __mingw_uuidof_s<name>::__uuid_inst; \ |
| 171 | + } \ |
| 172 | + } |
| 173 | + |
| 174 | +#endif // __cplusplus |
| 175 | + |
| 176 | +#else // !__MINGW32__ |
| 177 | + |
36 | 178 | #define COM_NO_WINDOWS_H // needed to inform d3d headers that this isn't windows |
37 | 179 |
|
38 | 180 | //===----------------------------------------------------------------------===// |
@@ -631,6 +773,12 @@ CROSS_PLATFORM_UUIDOF(ID3D12LibraryReflection, |
631 | 773 | CROSS_PLATFORM_UUIDOF(ID3D12ShaderReflection, |
632 | 774 | "5A58797D-A72C-478D-8BA2-EFC6B0EFE88E") |
633 | 775 |
|
| 776 | +#endif // __cplusplus |
| 777 | + |
| 778 | +#endif // !__MINGW32__ |
| 779 | + |
| 780 | +#ifdef __cplusplus |
| 781 | + |
634 | 782 | //===--------------------- COM Pointer Types ------------------------------===// |
635 | 783 |
|
636 | 784 | class CAllocator { |
@@ -898,6 +1046,7 @@ class CHeapPtr : public CHeapPtrBase<T, Allocator> { |
898 | 1046 |
|
899 | 1047 | #define CComHeapPtr CHeapPtr |
900 | 1048 |
|
| 1049 | +#ifndef __MINGW32__ |
901 | 1050 | //===--------------------------- BSTR Allocation --------------------------===// |
902 | 1051 |
|
903 | 1052 | void SysFreeString(BSTR bstrString); |
@@ -999,6 +1148,145 @@ template <int t_nBufferLength = 128> class CA2WEX { |
999 | 1148 |
|
1000 | 1149 | typedef CA2WEX<> CA2W; |
1001 | 1150 |
|
| 1151 | +#else // __MINGW32__ |
| 1152 | + |
| 1153 | +inline UINT WINAPI _AtlGetConversionACP() { return CP_THREAD_ACP; } |
| 1154 | + |
| 1155 | +template <class _CharType> |
| 1156 | +inline void AtlConvAllocMemory(_CharType **ppBuff, int nLength, |
| 1157 | + _CharType *pszFixedBuffer, |
| 1158 | + int nFixedBufferLength) { |
| 1159 | + ATLENSURE_THROW(ppBuff != NULL, E_INVALIDARG); |
| 1160 | + ATLENSURE_THROW(nLength >= 0, E_INVALIDARG); |
| 1161 | + ATLENSURE_THROW(pszFixedBuffer != NULL, E_INVALIDARG); |
| 1162 | + |
| 1163 | + if (*ppBuff != pszFixedBuffer) { |
| 1164 | + if (nLength > nFixedBufferLength) { |
| 1165 | +#if __MSVCRT_VERSION__ >= 0x900 |
| 1166 | + _CharType *ppReallocBuf = static_cast<_CharType *>( |
| 1167 | + _recalloc(*ppBuff, nLength, sizeof(_CharType))); |
| 1168 | + if (ppReallocBuf == NULL) { |
| 1169 | + E_OUTOFMEMORY; |
| 1170 | + } |
| 1171 | + *ppBuff = ppReallocBuf; |
| 1172 | + } else { |
| 1173 | +#endif |
| 1174 | + free(*ppBuff); |
| 1175 | + *ppBuff = pszFixedBuffer; |
| 1176 | + } |
| 1177 | + |
| 1178 | + } else { |
| 1179 | + if (nLength > nFixedBufferLength) { |
| 1180 | + *ppBuff = static_cast<_CharType *>(calloc(nLength, sizeof(_CharType))); |
| 1181 | + } else { |
| 1182 | + *ppBuff = pszFixedBuffer; |
| 1183 | + } |
| 1184 | + } |
| 1185 | + |
| 1186 | + if (*ppBuff == NULL) { |
| 1187 | + E_OUTOFMEMORY; |
| 1188 | + } |
| 1189 | +} |
| 1190 | + |
| 1191 | +template <class _CharType> |
| 1192 | +inline void AtlConvFreeMemory(_CharType *pBuff, _CharType *pszFixedBuffer, |
| 1193 | + int nFixedBufferLength) { |
| 1194 | + if (pBuff != pszFixedBuffer) { |
| 1195 | + free(pBuff); |
| 1196 | + } |
| 1197 | +} |
| 1198 | + |
| 1199 | +template <int t_nBufferLength = 128> class CW2AEX { |
| 1200 | +public: |
| 1201 | + CW2AEX(LPCWSTR psz) : m_psz(m_szBuffer) { Init(psz, _AtlGetConversionACP()); } |
| 1202 | + CW2AEX(LPCWSTR psz, UINT nCodePage) : m_psz(m_szBuffer) { |
| 1203 | + Init(psz, nCodePage); |
| 1204 | + } |
| 1205 | + ~CW2AEX() { AtlConvFreeMemory(m_psz, m_szBuffer, t_nBufferLength); } |
| 1206 | + |
| 1207 | + operator LPSTR() const { return (m_psz); } |
| 1208 | + |
| 1209 | +private: |
| 1210 | + void Init(LPCWSTR psz, UINT nConvertCodePage) { |
| 1211 | + if (psz == NULL) { |
| 1212 | + m_psz = NULL; |
| 1213 | + return; |
| 1214 | + } |
| 1215 | + int nLengthW = lstrlenW(psz) + 1; |
| 1216 | + int nLengthA = nLengthW * 4; |
| 1217 | + |
| 1218 | + AtlConvAllocMemory(&m_psz, nLengthA, m_szBuffer, t_nBufferLength); |
| 1219 | + |
| 1220 | + BOOL bFailed = |
| 1221 | + (0 == ::WideCharToMultiByte(nConvertCodePage, 0, psz, nLengthW, m_psz, |
| 1222 | + nLengthA, NULL, NULL)); |
| 1223 | + if (bFailed) { |
| 1224 | + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
| 1225 | + nLengthA = ::WideCharToMultiByte(nConvertCodePage, 0, psz, nLengthW, |
| 1226 | + NULL, 0, NULL, NULL); |
| 1227 | + AtlConvAllocMemory(&m_psz, nLengthA, m_szBuffer, t_nBufferLength); |
| 1228 | + bFailed = |
| 1229 | + (0 == ::WideCharToMultiByte(nConvertCodePage, 0, psz, nLengthW, |
| 1230 | + m_psz, nLengthA, NULL, NULL)); |
| 1231 | + } |
| 1232 | + } |
| 1233 | + } |
| 1234 | + |
| 1235 | +public: |
| 1236 | + LPSTR m_psz; |
| 1237 | + char m_szBuffer[t_nBufferLength]; |
| 1238 | + |
| 1239 | +private: |
| 1240 | + CW2AEX(const CW2AEX &); |
| 1241 | + CW2AEX &operator=(const CW2AEX &); |
| 1242 | +}; |
| 1243 | +typedef CW2AEX<> CW2A; |
| 1244 | + |
| 1245 | +template <int t_nBufferLength = 128> class CA2WEX { |
| 1246 | +public: |
| 1247 | + CA2WEX(LPCSTR psz) : m_psz(m_szBuffer) { Init(psz, _AtlGetConversionACP()); } |
| 1248 | + CA2WEX(LPCSTR psz, UINT nCodePage) : m_psz(m_szBuffer) { |
| 1249 | + Init(psz, nCodePage); |
| 1250 | + } |
| 1251 | + ~CA2WEX() { AtlConvFreeMemory(m_psz, m_szBuffer, t_nBufferLength); } |
| 1252 | + |
| 1253 | + operator LPWSTR() const { return (m_psz); } |
| 1254 | + |
| 1255 | +private: |
| 1256 | + void Init(LPCSTR psz, UINT nCodePage) { |
| 1257 | + if (psz == NULL) { |
| 1258 | + m_psz = NULL; |
| 1259 | + return; |
| 1260 | + } |
| 1261 | + int nLengthA = lstrlenA(psz) + 1; |
| 1262 | + int nLengthW = nLengthA; |
| 1263 | + |
| 1264 | + AtlConvAllocMemory(&m_psz, nLengthW, m_szBuffer, t_nBufferLength); |
| 1265 | + |
| 1266 | + BOOL bFailed = (0 == ::MultiByteToWideChar(nCodePage, 0, psz, nLengthA, |
| 1267 | + m_psz, nLengthW)); |
| 1268 | + if (bFailed) { |
| 1269 | + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
| 1270 | + nLengthW = ::MultiByteToWideChar(nCodePage, 0, psz, nLengthA, NULL, 0); |
| 1271 | + AtlConvAllocMemory(&m_psz, nLengthW, m_szBuffer, t_nBufferLength); |
| 1272 | + bFailed = (0 == ::MultiByteToWideChar(nCodePage, 0, psz, nLengthA, |
| 1273 | + m_psz, nLengthW)); |
| 1274 | + } |
| 1275 | + } |
| 1276 | + } |
| 1277 | + |
| 1278 | +public: |
| 1279 | + LPWSTR m_psz; |
| 1280 | + wchar_t m_szBuffer[t_nBufferLength]; |
| 1281 | + |
| 1282 | +private: |
| 1283 | + CA2WEX(const CA2WEX &); |
| 1284 | + CA2WEX &operator=(const CA2WEX &); |
| 1285 | +}; |
| 1286 | +typedef CA2WEX<> CA2W; |
| 1287 | + |
| 1288 | +#endif // __MINGW32__ |
| 1289 | + |
1002 | 1290 | //===--------- File IO Related Types ----------------===// |
1003 | 1291 |
|
1004 | 1292 | class CHandle { |
@@ -1051,6 +1339,6 @@ class WArgV { |
1051 | 1339 |
|
1052 | 1340 | #endif // __cplusplus |
1053 | 1341 |
|
1054 | | -#endif // _WIN32 |
| 1342 | +#endif // _MSC_VER |
1055 | 1343 |
|
1056 | 1344 | #endif // LLVM_SUPPORT_WIN_ADAPTER_H |
0 commit comments