|
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) do { ATLASSERT(expr); __analysis_assume(!!(expr)); } while(0) |
| 89 | +#define ATLENSURE_THROW(expr, hr) \ |
| 90 | +do { \ |
| 91 | + int __atl_condVal=!!(expr); \ |
| 92 | + ATLASSUME(__atl_condVal); \ |
| 93 | + if (!(__atl_condVal)) throw(hr); \ |
| 94 | +} while (0) |
| 95 | + |
| 96 | +// Disable all calling conventions |
| 97 | +#define __cdecl |
| 98 | +#define __stdcall |
| 99 | +#define __vectorcall |
| 100 | +#define __thiscall |
| 101 | +#define __fastcall |
| 102 | +#define __clrcall |
| 103 | + |
| 104 | +// FIXME: missing from intsafe.h |
| 105 | +#define Int32ToUInt32 IntToUInt |
| 106 | +#define UInt32Add UIntAdd |
| 107 | +#define UInt32Mult UIntMult |
| 108 | + |
| 109 | +// FIXME: missing from sal.h |
| 110 | +#define _Maybenull_ |
| 111 | +#define _Notnull_ |
| 112 | + |
| 113 | +// FIXME: missing from d3d12shader.h |
| 114 | +#define D3D_SHADER_REQUIRES_EARLY_DEPTH_STENCIL 0x00000002 |
| 115 | +#define D3D12_SHVER_GET_TYPE(_Version) (((_Version) >> 16) & 0xffff) |
| 116 | +#define D3D12_SHVER_GET_MAJOR(_Version) (((_Version) >> 4) & 0xf) |
| 117 | +#define D3D12_SHVER_GET_MINOR(_Version) (((_Version) >> 0) & 0xf) |
| 118 | + |
| 119 | +#ifdef __cplusplus |
| 120 | + |
| 121 | +constexpr uint8_t nybble_from_hex(char c) { |
| 122 | + return ((c >= '0' && c <= '9') |
| 123 | + ? (c - '0') |
| 124 | + : ((c >= 'a' && c <= 'f') |
| 125 | + ? (c - 'a' + 10) |
| 126 | + : ((c >= 'A' && c <= 'F') ? (c - 'A' + 10) |
| 127 | + : /* Should be an error */ -1))); |
| 128 | +} |
| 129 | + |
| 130 | +constexpr uint8_t byte_from_hex(char c1, char c2) { |
| 131 | + return nybble_from_hex(c1) << 4 | nybble_from_hex(c2); |
| 132 | +} |
| 133 | + |
| 134 | +constexpr uint8_t byte_from_hexstr(const char str[2]) { |
| 135 | + return nybble_from_hex(str[0]) << 4 | nybble_from_hex(str[1]); |
| 136 | +} |
| 137 | + |
| 138 | +constexpr GUID guid_from_string(const char str[37]) { |
| 139 | + return GUID{static_cast<uint32_t>(byte_from_hexstr(str)) << 24 | |
| 140 | + static_cast<uint32_t>(byte_from_hexstr(str + 2)) << 16 | |
| 141 | + static_cast<uint32_t>(byte_from_hexstr(str + 4)) << 8 | |
| 142 | + byte_from_hexstr(str + 6), |
| 143 | + static_cast<uint16_t>( |
| 144 | + static_cast<uint16_t>(byte_from_hexstr(str + 9)) << 8 | |
| 145 | + byte_from_hexstr(str + 11)), |
| 146 | + static_cast<uint16_t>( |
| 147 | + static_cast<uint16_t>(byte_from_hexstr(str + 14)) << 8 | |
| 148 | + byte_from_hexstr(str + 16)), |
| 149 | + {byte_from_hexstr(str + 19), byte_from_hexstr(str + 21), |
| 150 | + byte_from_hexstr(str + 24), byte_from_hexstr(str + 26), |
| 151 | + byte_from_hexstr(str + 28), byte_from_hexstr(str + 30), |
| 152 | + byte_from_hexstr(str + 32), byte_from_hexstr(str + 34)}}; |
| 153 | +} |
| 154 | + |
| 155 | +#define CROSS_PLATFORM_UUIDOF(name, spec) \ |
| 156 | + struct __declspec(uuid(spec)) name; \ |
| 157 | + extern "C++" { \ |
| 158 | + template <> struct __mingw_uuidof_s<name> { \ |
| 159 | + static constexpr IID __uuid_inst = guid_from_string(spec); \ |
| 160 | + }; \ |
| 161 | + template <> constexpr const GUID &__mingw_uuidof<name>() { \ |
| 162 | + return __mingw_uuidof_s<name>::__uuid_inst; \ |
| 163 | + } \ |
| 164 | + template <> constexpr const GUID &__mingw_uuidof<name *>() { \ |
| 165 | + return __mingw_uuidof_s<name>::__uuid_inst; \ |
| 166 | + } \ |
| 167 | + } |
| 168 | + |
| 169 | +#endif // __cplusplus |
| 170 | + |
| 171 | +#else // !__MINGW32__ |
| 172 | + |
36 | 173 | #define COM_NO_WINDOWS_H // needed to inform d3d headers that this isn't windows |
37 | 174 |
|
38 | 175 | //===----------------------------------------------------------------------===// |
@@ -631,6 +768,12 @@ CROSS_PLATFORM_UUIDOF(ID3D12LibraryReflection, |
631 | 768 | CROSS_PLATFORM_UUIDOF(ID3D12ShaderReflection, |
632 | 769 | "5A58797D-A72C-478D-8BA2-EFC6B0EFE88E") |
633 | 770 |
|
| 771 | +#endif // __cplusplus |
| 772 | + |
| 773 | +#endif // !__MINGW32__ |
| 774 | + |
| 775 | +#ifdef __cplusplus |
| 776 | + |
634 | 777 | //===--------------------- COM Pointer Types ------------------------------===// |
635 | 778 |
|
636 | 779 | class CAllocator { |
@@ -898,6 +1041,7 @@ class CHeapPtr : public CHeapPtrBase<T, Allocator> { |
898 | 1041 |
|
899 | 1042 | #define CComHeapPtr CHeapPtr |
900 | 1043 |
|
| 1044 | +#ifndef __MINGW32__ |
901 | 1045 | //===--------------------------- BSTR Allocation --------------------------===// |
902 | 1046 |
|
903 | 1047 | void SysFreeString(BSTR bstrString); |
@@ -999,6 +1143,155 @@ template <int t_nBufferLength = 128> class CA2WEX { |
999 | 1143 |
|
1000 | 1144 | typedef CA2WEX<> CA2W; |
1001 | 1145 |
|
| 1146 | +#else // __MINGW32__ |
| 1147 | + |
| 1148 | +inline UINT WINAPI _AtlGetConversionACP() { |
| 1149 | + return CP_THREAD_ACP; |
| 1150 | +} |
| 1151 | + |
| 1152 | +template <class _CharType> |
| 1153 | +inline void AtlConvAllocMemory(_CharType** ppBuff, int nLength, _CharType* pszFixedBuffer, int nFixedBufferLength) { |
| 1154 | + ATLENSURE_THROW(ppBuff != NULL, E_INVALIDARG); |
| 1155 | + ATLENSURE_THROW(nLength >= 0, E_INVALIDARG); |
| 1156 | + ATLENSURE_THROW(pszFixedBuffer != NULL, E_INVALIDARG); |
| 1157 | + |
| 1158 | + if (*ppBuff != pszFixedBuffer) { |
| 1159 | + if( nLength > nFixedBufferLength ) { |
| 1160 | +#if __MSVCRT_VERSION__ >= 0x900 |
| 1161 | + _CharType* ppReallocBuf = static_cast< _CharType* >( _recalloc(*ppBuff, nLength,sizeof( _CharType ) ) ); |
| 1162 | + if (ppReallocBuf == NULL) { |
| 1163 | + E_OUTOFMEMORY; |
| 1164 | + } |
| 1165 | + *ppBuff = ppReallocBuf; |
| 1166 | + } else { |
| 1167 | +#endif |
| 1168 | + free(*ppBuff); |
| 1169 | + *ppBuff=pszFixedBuffer; |
| 1170 | + } |
| 1171 | + |
| 1172 | + } else { |
| 1173 | + if( nLength > nFixedBufferLength ) { |
| 1174 | + *ppBuff = static_cast< _CharType* >( calloc(nLength,sizeof( _CharType ) ) ); |
| 1175 | + } else { |
| 1176 | + *ppBuff=pszFixedBuffer; |
| 1177 | + } |
| 1178 | + } |
| 1179 | + |
| 1180 | + if (*ppBuff == NULL) { |
| 1181 | + E_OUTOFMEMORY; |
| 1182 | + } |
| 1183 | +} |
| 1184 | + |
| 1185 | +template <class _CharType> |
| 1186 | +inline void AtlConvFreeMemory(_CharType* pBuff, _CharType* pszFixedBuffer, int nFixedBufferLength) { |
| 1187 | + if ( pBuff != pszFixedBuffer ) { |
| 1188 | + free( pBuff ); |
| 1189 | + } |
| 1190 | +} |
| 1191 | + |
| 1192 | +template< int t_nBufferLength = 128 > |
| 1193 | +class CW2AEX { |
| 1194 | +public: |
| 1195 | + CW2AEX(LPCWSTR psz) : |
| 1196 | + m_psz( m_szBuffer ) { |
| 1197 | + Init( psz, _AtlGetConversionACP() ); |
| 1198 | + } |
| 1199 | + CW2AEX(LPCWSTR psz, UINT nCodePage) : |
| 1200 | + m_psz( m_szBuffer ) { |
| 1201 | + Init( psz, nCodePage ); |
| 1202 | + } |
| 1203 | + ~CW2AEX() { |
| 1204 | + AtlConvFreeMemory(m_psz,m_szBuffer,t_nBufferLength); |
| 1205 | + } |
| 1206 | + |
| 1207 | + operator LPSTR() const { |
| 1208 | + return( m_psz ); |
| 1209 | + } |
| 1210 | + |
| 1211 | +private: |
| 1212 | + void Init(LPCWSTR psz, UINT nConvertCodePage) { |
| 1213 | + if (psz == NULL) { |
| 1214 | + m_psz = NULL; |
| 1215 | + return; |
| 1216 | + } |
| 1217 | + int nLengthW = lstrlenW( psz )+1; |
| 1218 | + int nLengthA = nLengthW*4; |
| 1219 | + |
| 1220 | + AtlConvAllocMemory(&m_psz,nLengthA,m_szBuffer,t_nBufferLength); |
| 1221 | + |
| 1222 | + BOOL bFailed=(0 == ::WideCharToMultiByte( nConvertCodePage, 0, psz, nLengthW, m_psz, nLengthA, NULL, NULL )); |
| 1223 | + if (bFailed) { |
| 1224 | + if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) { |
| 1225 | + nLengthA = ::WideCharToMultiByte( nConvertCodePage, 0, psz, nLengthW, NULL, 0, NULL, NULL ); |
| 1226 | + AtlConvAllocMemory(&m_psz,nLengthA,m_szBuffer,t_nBufferLength); |
| 1227 | + bFailed=(0 == ::WideCharToMultiByte( nConvertCodePage, 0, psz, nLengthW, m_psz, nLengthA, NULL, NULL )); |
| 1228 | + } |
| 1229 | + } |
| 1230 | + } |
| 1231 | + |
| 1232 | +public: |
| 1233 | + LPSTR m_psz; |
| 1234 | + char m_szBuffer[t_nBufferLength]; |
| 1235 | + |
| 1236 | +private: |
| 1237 | + CW2AEX(const CW2AEX&); |
| 1238 | + CW2AEX& operator=(const CW2AEX&); |
| 1239 | +}; |
| 1240 | +typedef CW2AEX<> CW2A; |
| 1241 | + |
| 1242 | +template< int t_nBufferLength = 128 > |
| 1243 | +class CA2WEX |
| 1244 | +{ |
| 1245 | +public: |
| 1246 | + CA2WEX(LPCSTR psz) : |
| 1247 | + m_psz( m_szBuffer ) { |
| 1248 | + Init( psz, _AtlGetConversionACP() ); |
| 1249 | + } |
| 1250 | + CA2WEX(LPCSTR psz, UINT nCodePage) : |
| 1251 | + m_psz( m_szBuffer ) { |
| 1252 | + Init( psz, nCodePage ); |
| 1253 | + } |
| 1254 | + ~CA2WEX() { |
| 1255 | + AtlConvFreeMemory(m_psz,m_szBuffer,t_nBufferLength); |
| 1256 | + } |
| 1257 | + |
| 1258 | + operator LPWSTR() const { |
| 1259 | + return( m_psz ); |
| 1260 | + } |
| 1261 | + |
| 1262 | +private: |
| 1263 | + void Init(LPCSTR psz, UINT nCodePage) { |
| 1264 | + if (psz == NULL) { |
| 1265 | + m_psz = NULL; |
| 1266 | + return; |
| 1267 | + } |
| 1268 | + int nLengthA = lstrlenA( psz )+1; |
| 1269 | + int nLengthW = nLengthA; |
| 1270 | + |
| 1271 | + AtlConvAllocMemory(&m_psz,nLengthW,m_szBuffer,t_nBufferLength); |
| 1272 | + |
| 1273 | + BOOL bFailed=(0 == ::MultiByteToWideChar( nCodePage, 0, psz, nLengthA, m_psz, nLengthW ) ); |
| 1274 | + if (bFailed) { |
| 1275 | + if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) { |
| 1276 | + nLengthW = ::MultiByteToWideChar( nCodePage, 0, psz, nLengthA, NULL, 0); |
| 1277 | + AtlConvAllocMemory(&m_psz,nLengthW,m_szBuffer,t_nBufferLength); |
| 1278 | + bFailed=(0 == ::MultiByteToWideChar( nCodePage, 0, psz, nLengthA, m_psz, nLengthW ) ); |
| 1279 | + } |
| 1280 | + } |
| 1281 | + } |
| 1282 | + |
| 1283 | +public: |
| 1284 | + LPWSTR m_psz; |
| 1285 | + wchar_t m_szBuffer[t_nBufferLength]; |
| 1286 | + |
| 1287 | +private: |
| 1288 | + CA2WEX(const CA2WEX&); |
| 1289 | + CA2WEX& operator=(const CA2WEX&); |
| 1290 | +}; |
| 1291 | +typedef CA2WEX<> CA2W; |
| 1292 | + |
| 1293 | +#endif // __MINGW32__ |
| 1294 | + |
1002 | 1295 | //===--------- File IO Related Types ----------------===// |
1003 | 1296 |
|
1004 | 1297 | class CHandle { |
@@ -1051,6 +1344,6 @@ class WArgV { |
1051 | 1344 |
|
1052 | 1345 | #endif // __cplusplus |
1053 | 1346 |
|
1054 | | -#endif // _WIN32 |
| 1347 | +#endif // _MSC_VER |
1055 | 1348 |
|
1056 | 1349 | #endif // LLVM_SUPPORT_WIN_ADAPTER_H |
0 commit comments