WinAdapter.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  1. //===- WinAdapter.h - Windows Adapter for non-Windows platforms -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines Windows-specific types, macros, and SAL annotations used
  11. // in the codebase for non-Windows platforms.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_SUPPORT_WIN_ADAPTER_H
  15. #define LLVM_SUPPORT_WIN_ADAPTER_H
  16. #ifndef _WIN32
  17. #ifdef __cplusplus
  18. #include <atomic>
  19. #include <cassert>
  20. #include <climits>
  21. #include <cstring>
  22. #include <cwchar>
  23. #include <fstream>
  24. #include <stdarg.h>
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include <string>
  28. #include <typeindex>
  29. #include <typeinfo>
  30. #include <vector>
  31. #endif // __cplusplus
  32. #define COM_NO_WINDOWS_H // needed to inform d3d headers that this isn't windows
  33. //===----------------------------------------------------------------------===//
  34. //
  35. // Begin: Macro Definitions
  36. //
  37. //===----------------------------------------------------------------------===//
  38. #define C_ASSERT(expr) static_assert((expr), "")
  39. #define ATLASSERT assert
  40. #define CoTaskMemAlloc malloc
  41. #define CoTaskMemFree free
  42. #define ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
  43. #define _countof(a) (sizeof(a) / sizeof(*(a)))
  44. // If it is GCC, there is no UUID support and we must emulate it.
  45. #ifndef __clang__
  46. #define __EMULATE_UUID 1
  47. #endif // __clang__
  48. #ifdef __EMULATE_UUID
  49. #define __declspec(x)
  50. #endif // __EMULATE_UUID
  51. #define DECLSPEC_SELECTANY
  52. #ifdef __EMULATE_UUID
  53. #define uuid(id)
  54. #endif // __EMULATE_UUID
  55. #define STDMETHODCALLTYPE
  56. #define STDMETHODIMP_(type) type STDMETHODCALLTYPE
  57. #define STDMETHODIMP STDMETHODIMP_(HRESULT)
  58. #define STDMETHOD_(type, name) virtual STDMETHODIMP_(type) name
  59. #define STDMETHOD(name) STDMETHOD_(HRESULT, name)
  60. #define EXTERN_C extern "C"
  61. #define UNREFERENCED_PARAMETER(P) (void)(P)
  62. #define RtlEqualMemory(Destination, Source, Length) \
  63. (!memcmp((Destination), (Source), (Length)))
  64. #define RtlMoveMemory(Destination, Source, Length) \
  65. memmove((Destination), (Source), (Length))
  66. #define RtlCopyMemory(Destination, Source, Length) \
  67. memcpy((Destination), (Source), (Length))
  68. #define RtlFillMemory(Destination, Length, Fill) \
  69. memset((Destination), (Fill), (Length))
  70. #define RtlZeroMemory(Destination, Length) memset((Destination), 0, (Length))
  71. #define MoveMemory RtlMoveMemory
  72. #define CopyMemory RtlCopyMemory
  73. #define FillMemory RtlFillMemory
  74. #define ZeroMemory RtlZeroMemory
  75. #define FALSE 0
  76. #define TRUE 1
  77. // We ignore the code page completely on Linux.
  78. #define GetConsoleOutputCP() 0
  79. #define _HRESULT_TYPEDEF_(_sc) ((HRESULT)_sc)
  80. #define DISP_E_BADINDEX _HRESULT_TYPEDEF_(0x8002000BL)
  81. #define REGDB_E_CLASSNOTREG _HRESULT_TYPEDEF_(0x80040154L)
  82. // This is an unsafe conversion. If needed, we can later implement a safe
  83. // conversion that throws exceptions for overflow cases.
  84. #define UIntToInt(uint_arg, int_ptr_arg) *int_ptr_arg = uint_arg
  85. #define INVALID_HANDLE_VALUE ((HANDLE)(LONG_PTR)-1)
  86. // Use errno to implement {Get|Set}LastError
  87. #define GetLastError() errno
  88. #define SetLastError(ERR) errno = ERR
  89. // Map these errors to equivalent errnos.
  90. #define ERROR_SUCCESS 0L
  91. #define ERROR_ARITHMETIC_OVERFLOW EOVERFLOW
  92. #define ERROR_FILE_NOT_FOUND ENOENT
  93. #define ERROR_FUNCTION_NOT_CALLED ENOSYS
  94. #define ERROR_IO_DEVICE EIO
  95. #define ERROR_INSUFFICIENT_BUFFER ENOBUFS
  96. #define ERROR_INVALID_HANDLE EBADF
  97. #define ERROR_INVALID_PARAMETER EINVAL
  98. #define ERROR_OUT_OF_STRUCTURES ENOMEM
  99. #define ERROR_NOT_CAPABLE EPERM
  100. #define ERROR_NOT_FOUND ENOTSUP
  101. #define ERROR_UNHANDLED_EXCEPTION EBADF
  102. #define ERROR_BROKEN_PIPE EPIPE
  103. // Used by HRESULT <--> WIN32 error code conversion
  104. #define SEVERITY_ERROR 1
  105. #define FACILITY_WIN32 7
  106. #define HRESULT_CODE(hr) ((hr) & 0xFFFF)
  107. #define MAKE_HRESULT(severity, facility, code) \
  108. ((HRESULT)(((unsigned long)(severity) << 31) | \
  109. ((unsigned long)(facility) << 16) | ((unsigned long)(code))))
  110. #define FILE_TYPE_UNKNOWN 0x0000
  111. #define FILE_TYPE_DISK 0x0001
  112. #define FILE_TYPE_CHAR 0x0002
  113. #define FILE_TYPE_PIPE 0x0003
  114. #define FILE_TYPE_REMOTE 0x8000
  115. #define FILE_ATTRIBUTE_NORMAL 0x00000080
  116. #define FILE_ATTRIBUTE_DIRECTORY 0x00000010
  117. #define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
  118. #define STDOUT_FILENO 1
  119. #define STDERR_FILENO 2
  120. // STGTY ENUMS
  121. #define STGTY_STORAGE 1
  122. #define STGTY_STREAM 2
  123. #define STGTY_LOCKBYTES 3
  124. #define STGTY_PROPERTY 4
  125. // Storage errors
  126. #define STG_E_INVALIDFUNCTION 1L
  127. #define STG_E_ACCESSDENIED 2L
  128. #define STREAM_SEEK_SET 0
  129. #define STREAM_SEEK_CUR 1
  130. #define STREAM_SEEK_END 2
  131. #define HEAP_NO_SERIALIZE 0x1
  132. #define HEAP_ZERO_MEMORY 0x8
  133. #define MB_ERR_INVALID_CHARS 0x00000008 // error for invalid chars
  134. // File IO
  135. #define CREATE_ALWAYS 2
  136. #define CREATE_NEW 1
  137. #define OPEN_ALWAYS 4
  138. #define OPEN_EXISTING 3
  139. #define TRUNCATE_EXISTING 5
  140. #define FILE_SHARE_DELETE 0x00000004
  141. #define FILE_SHARE_READ 0x00000001
  142. #define FILE_SHARE_WRITE 0x00000002
  143. #define GENERIC_READ 0x80000000
  144. #define GENERIC_WRITE 0x40000000
  145. #define _atoi64 atoll
  146. #define sprintf_s snprintf
  147. #define _strdup strdup
  148. #define _strnicmp strnicmp
  149. #define vsnprintf_s vsnprintf
  150. #define strcat_s strcat
  151. #define strcpy_s(dst, n, src) strncpy(dst, src, n)
  152. #define _vscwprintf vwprintf
  153. #define vswprintf_s vswprintf
  154. #define swprintf_s swprintf
  155. #define StringCchCopyW(dst, n, src) wcsncpy(dst, src, n)
  156. #define OutputDebugStringW(msg) fputws(msg, stderr)
  157. #define OutputDebugStringA(msg) fputs(msg, stderr)
  158. #define OutputDebugFormatA(...) fprintf(stderr, __VA_ARGS__)
  159. // Event Tracing for Windows (ETW) provides application programmers the ability
  160. // to start and stop event tracing sessions, instrument an application to
  161. // provide trace events, and consume trace events.
  162. #define DxcEtw_DXCompilerCreateInstance_Start()
  163. #define DxcEtw_DXCompilerCreateInstance_Stop(hr)
  164. #define DxcEtw_DXCompilerCompile_Start()
  165. #define DxcEtw_DXCompilerCompile_Stop(hr)
  166. #define DxcEtw_DXCompilerDisassemble_Start()
  167. #define DxcEtw_DXCompilerDisassemble_Stop(hr)
  168. #define DxcEtw_DXCompilerPreprocess_Start()
  169. #define DxcEtw_DXCompilerPreprocess_Stop(hr)
  170. #define DxcEtw_DxcValidation_Start()
  171. #define DxcEtw_DxcValidation_Stop(hr)
  172. #define UInt32Add UIntAdd
  173. #define Int32ToUInt32 IntToUInt
  174. //===--------------------- HRESULT Related Macros -------------------------===//
  175. #define S_OK ((HRESULT)0L)
  176. #define S_FALSE ((HRESULT)1L)
  177. #define E_ABORT (HRESULT)0x80004004
  178. #define E_ACCESSDENIED (HRESULT)0x80070005
  179. #define E_BOUNDS (HRESULT)0x8000000B
  180. #define E_FAIL (HRESULT)0x80004005
  181. #define E_HANDLE (HRESULT)0x80070006
  182. #define E_INVALIDARG (HRESULT)0x80070057
  183. #define E_NOINTERFACE (HRESULT)0x80004002
  184. #define E_NOTIMPL (HRESULT)0x80004001
  185. #define E_NOT_VALID_STATE (HRESULT)0x8007139F
  186. #define E_OUTOFMEMORY (HRESULT)0x8007000E
  187. #define E_POINTER (HRESULT)0x80004003
  188. #define E_UNEXPECTED (HRESULT)0x8000FFFF
  189. #define SUCCEEDED(hr) (((HRESULT)(hr)) >= 0)
  190. #define FAILED(hr) (((HRESULT)(hr)) < 0)
  191. #define DXC_FAILED(hr) (((HRESULT)(hr)) < 0)
  192. #define HRESULT_FROM_WIN32(x) \
  193. (HRESULT)(x) <= 0 ? (HRESULT)(x) \
  194. : (HRESULT)(((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)
  195. //===----------------------------------------------------------------------===//
  196. //
  197. // Begin: Disable SAL Annotations
  198. //
  199. //===----------------------------------------------------------------------===//
  200. #define _In_
  201. #define _In_z_
  202. #define _In_opt_
  203. #define _In_opt_count_(size)
  204. #define _In_opt_z_
  205. #define _In_count_(size)
  206. #define _In_bytecount_(size)
  207. #define _Out_
  208. #define _Out_opt_
  209. #define _Outptr_
  210. #define _Outptr_opt_
  211. #define _Outptr_result_z_
  212. #define _Outptr_opt_result_z_
  213. #define _Outptr_result_maybenull_
  214. #define _Outptr_result_nullonfailure_
  215. #define _Outptr_result_buffer_maybenull_(ptr)
  216. #define _Outptr_result_buffer_(ptr)
  217. #define _COM_Outptr_
  218. #define _COM_Outptr_opt_
  219. #define _COM_Outptr_result_maybenull_
  220. #define _COM_Outptr_opt_result_maybenull_
  221. #define THIS_
  222. #define THIS
  223. #define PURE = 0
  224. #define _Maybenull_
  225. #define __debugbreak()
  226. // GCC produces erros on calling convention attributes.
  227. #ifdef __GNUC__
  228. #define __cdecl
  229. #define __CRTDECL
  230. #define __stdcall
  231. #define __vectorcall
  232. #define __thiscall
  233. #define __fastcall
  234. #define __clrcall
  235. #endif // __GNUC__
  236. //===----------------------------------------------------------------------===//
  237. //
  238. // Begin: Type Definitions
  239. //
  240. //===----------------------------------------------------------------------===//
  241. #ifdef __cplusplus
  242. typedef unsigned char BYTE, UINT8;
  243. typedef unsigned char *LPBYTE;
  244. typedef BYTE BOOLEAN;
  245. typedef BOOLEAN *PBOOLEAN;
  246. typedef bool BOOL;
  247. typedef BOOL *LPBOOL;
  248. typedef int INT;
  249. typedef long LONG;
  250. typedef unsigned int UINT;
  251. typedef unsigned long ULONG;
  252. typedef long long LONGLONG;
  253. typedef long long LONG_PTR;
  254. typedef unsigned long long ULONG_PTR;
  255. typedef unsigned long long ULONGLONG;
  256. typedef uint16_t WORD;
  257. typedef uint32_t DWORD;
  258. typedef DWORD *LPDWORD;
  259. typedef uint32_t UINT32;
  260. typedef uint64_t UINT64;
  261. typedef signed char INT8, *PINT8;
  262. typedef signed int INT32, *PINT32;
  263. typedef size_t SIZE_T;
  264. typedef const char *LPCSTR;
  265. typedef const char *PCSTR;
  266. typedef int errno_t;
  267. typedef wchar_t WCHAR;
  268. typedef wchar_t *LPWSTR;
  269. typedef wchar_t *PWCHAR;
  270. typedef const wchar_t *LPCWSTR;
  271. typedef const wchar_t *PCWSTR;
  272. typedef WCHAR OLECHAR;
  273. typedef OLECHAR *BSTR;
  274. typedef OLECHAR *LPOLESTR;
  275. typedef char *LPSTR;
  276. typedef void *LPVOID;
  277. typedef const void *LPCVOID;
  278. typedef std::nullptr_t nullptr_t;
  279. typedef signed int HRESULT;
  280. //===--------------------- Handle Types -----------------------------------===//
  281. typedef void *HANDLE;
  282. typedef void *RPC_IF_HANDLE;
  283. #define DECLARE_HANDLE(name) \
  284. struct name##__ { \
  285. int unused; \
  286. }; \
  287. typedef struct name##__ *name
  288. DECLARE_HANDLE(HINSTANCE);
  289. typedef void *HMODULE;
  290. #define STD_INPUT_HANDLE ((DWORD)-10)
  291. #define STD_OUTPUT_HANDLE ((DWORD)-11)
  292. #define STD_ERROR_HANDLE ((DWORD)-12)
  293. //===--------------------- ID Types and Macros for COM --------------------===//
  294. #ifdef __EMULATE_UUID
  295. struct GUID
  296. #else // __EMULATE_UUID
  297. // These specific definitions are required by clang -fms-extensions.
  298. typedef struct _GUID
  299. #endif // __EMULATE_UUID
  300. {
  301. uint32_t Data1;
  302. uint16_t Data2;
  303. uint16_t Data3;
  304. uint8_t Data4[8];
  305. }
  306. #ifdef __EMULATE_UUID
  307. ;
  308. #else // __EMULATE_UUID
  309. GUID;
  310. #endif // __EMULATE_UUID
  311. typedef GUID CLSID;
  312. typedef const GUID &REFGUID;
  313. typedef const GUID &REFCLSID;
  314. typedef GUID IID;
  315. typedef IID *LPIID;
  316. typedef const IID &REFIID;
  317. inline bool IsEqualGUID(REFGUID rguid1, REFGUID rguid2) {
  318. // Optimization:
  319. if (&rguid1 == &rguid2)
  320. return true;
  321. return !memcmp(&rguid1, &rguid2, sizeof(GUID));
  322. }
  323. inline bool operator==(REFGUID guidOne, REFGUID guidOther) {
  324. return !!IsEqualGUID(guidOne, guidOther);
  325. }
  326. inline bool operator!=(REFGUID guidOne, REFGUID guidOther) {
  327. return !(guidOne == guidOther);
  328. }
  329. inline bool IsEqualIID(REFIID riid1, REFIID riid2) {
  330. return IsEqualGUID(riid1, riid2);
  331. }
  332. inline bool IsEqualCLSID(REFCLSID rclsid1, REFCLSID rclsid2) {
  333. return IsEqualGUID(rclsid1, rclsid2);
  334. }
  335. //===--------------------- Struct Types -----------------------------------===//
  336. typedef struct _FILETIME {
  337. DWORD dwLowDateTime;
  338. DWORD dwHighDateTime;
  339. } FILETIME, *PFILETIME, *LPFILETIME;
  340. typedef struct _BY_HANDLE_FILE_INFORMATION {
  341. DWORD dwFileAttributes;
  342. FILETIME ftCreationTime;
  343. FILETIME ftLastAccessTime;
  344. FILETIME ftLastWriteTime;
  345. DWORD dwVolumeSerialNumber;
  346. DWORD nFileSizeHigh;
  347. DWORD nFileSizeLow;
  348. DWORD nNumberOfLinks;
  349. DWORD nFileIndexHigh;
  350. DWORD nFileIndexLow;
  351. } BY_HANDLE_FILE_INFORMATION, *PBY_HANDLE_FILE_INFORMATION,
  352. *LPBY_HANDLE_FILE_INFORMATION;
  353. typedef struct _WIN32_FIND_DATAW {
  354. DWORD dwFileAttributes;
  355. FILETIME ftCreationTime;
  356. FILETIME ftLastAccessTime;
  357. FILETIME ftLastWriteTime;
  358. DWORD nFileSizeHigh;
  359. DWORD nFileSizeLow;
  360. DWORD dwReserved0;
  361. DWORD dwReserved1;
  362. WCHAR cFileName[260];
  363. WCHAR cAlternateFileName[14];
  364. } WIN32_FIND_DATAW, *PWIN32_FIND_DATAW, *LPWIN32_FIND_DATAW;
  365. typedef union _LARGE_INTEGER {
  366. struct {
  367. DWORD LowPart;
  368. DWORD HighPart;
  369. } u;
  370. LONGLONG QuadPart;
  371. } LARGE_INTEGER;
  372. typedef LARGE_INTEGER *PLARGE_INTEGER;
  373. typedef union _ULARGE_INTEGER {
  374. struct {
  375. DWORD LowPart;
  376. DWORD HighPart;
  377. } u;
  378. ULONGLONG QuadPart;
  379. } ULARGE_INTEGER;
  380. typedef ULARGE_INTEGER *PULARGE_INTEGER;
  381. typedef struct tagSTATSTG {
  382. LPOLESTR pwcsName;
  383. DWORD type;
  384. ULARGE_INTEGER cbSize;
  385. FILETIME mtime;
  386. FILETIME ctime;
  387. FILETIME atime;
  388. DWORD grfMode;
  389. DWORD grfLocksSupported;
  390. CLSID clsid;
  391. DWORD grfStateBits;
  392. DWORD reserved;
  393. } STATSTG;
  394. enum tagSTATFLAG {
  395. STATFLAG_DEFAULT = 0,
  396. STATFLAG_NONAME = 1,
  397. STATFLAG_NOOPEN = 2
  398. };
  399. //===--------------------- UUID Related Macros ----------------------------===//
  400. #ifdef __EMULATE_UUID
  401. // The following macros are defined to facilitate the lack of 'uuid' on Linux.
  402. constexpr uint8_t nybble_from_hex(char c) {
  403. return ((c >= '0' && c <= '9')
  404. ? (c - '0')
  405. : ((c >= 'a' && c <= 'f')
  406. ? (c - 'a' + 10)
  407. : ((c >= 'A' && c <= 'F') ? (c - 'A' + 10)
  408. : /* Should be an error */ -1)));
  409. }
  410. constexpr uint8_t byte_from_hex(char c1, char c2) {
  411. return nybble_from_hex(c1) << 4 | nybble_from_hex(c2);
  412. }
  413. constexpr uint8_t byte_from_hexstr(const char str[2]) {
  414. return nybble_from_hex(str[0]) << 4 | nybble_from_hex(str[1]);
  415. }
  416. constexpr GUID guid_from_string(const char str[37]) {
  417. return GUID{static_cast<uint32_t>(byte_from_hexstr(str)) << 24 |
  418. static_cast<uint32_t>(byte_from_hexstr(str + 2)) << 16 |
  419. static_cast<uint32_t>(byte_from_hexstr(str + 4)) << 8 |
  420. byte_from_hexstr(str + 6),
  421. static_cast<uint16_t>(
  422. static_cast<uint16_t>(byte_from_hexstr(str + 9)) << 8 |
  423. byte_from_hexstr(str + 11)),
  424. static_cast<uint16_t>(
  425. static_cast<uint16_t>(byte_from_hexstr(str + 14)) << 8 |
  426. byte_from_hexstr(str + 16)),
  427. {byte_from_hexstr(str + 19), byte_from_hexstr(str + 21),
  428. byte_from_hexstr(str + 24), byte_from_hexstr(str + 26),
  429. byte_from_hexstr(str + 28), byte_from_hexstr(str + 30),
  430. byte_from_hexstr(str + 32), byte_from_hexstr(str + 34)}};
  431. }
  432. template <typename interface> inline GUID __emulated_uuidof();
  433. #define CROSS_PLATFORM_UUIDOF(interface, spec) \
  434. struct interface; \
  435. template <> inline GUID __emulated_uuidof<interface>() { \
  436. static const IID _IID = guid_from_string(spec); \
  437. return _IID; \
  438. }
  439. #define __uuidof(T) __emulated_uuidof<typename std::decay<T>::type>()
  440. #define IID_PPV_ARGS(ppType) \
  441. __uuidof(decltype(**(ppType))), reinterpret_cast<void **>(ppType)
  442. #else // __EMULATE_UUID
  443. #ifndef CROSS_PLATFORM_UUIDOF
  444. // Warning: This macro exists in dxcapi.h as well
  445. #define CROSS_PLATFORM_UUIDOF(interface, spec) \
  446. struct __declspec(uuid(spec)) interface;
  447. #endif
  448. template <typename T> inline void **IID_PPV_ARGS_Helper(T **pp) {
  449. return reinterpret_cast<void **>(pp);
  450. }
  451. #define IID_PPV_ARGS(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
  452. #endif // __EMULATE_UUID
  453. // Needed for d3d headers, but fail to create actual interfaces
  454. #define DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
  455. const GUID name = {l, w1, w2, {b1, b2, b3, b4, b5, b6, b7, b8}}
  456. #define DECLSPEC_UUID(x)
  457. #define MIDL_INTERFACE(x) struct DECLSPEC_UUID(x)
  458. #define DECLARE_INTERFACE(iface) struct iface
  459. #define DECLARE_INTERFACE_(iface, parent) DECLARE_INTERFACE(iface) : parent
  460. //===--------------------- COM Interfaces ---------------------------------===//
  461. CROSS_PLATFORM_UUIDOF(IUnknown, "00000000-0000-0000-C000-000000000046")
  462. struct IUnknown {
  463. IUnknown(){};
  464. virtual HRESULT QueryInterface(REFIID riid, void **ppvObject) = 0;
  465. virtual ULONG AddRef() = 0;
  466. virtual ULONG Release() = 0;
  467. template <class Q> HRESULT QueryInterface(Q **pp) {
  468. return QueryInterface(__uuidof(Q), (void **)pp);
  469. }
  470. };
  471. CROSS_PLATFORM_UUIDOF(INoMarshal, "ECC8691B-C1DB-4DC0-855E-65F6C551AF49")
  472. struct INoMarshal : public IUnknown {};
  473. CROSS_PLATFORM_UUIDOF(IMalloc, "00000002-0000-0000-C000-000000000046")
  474. struct IMalloc : public IUnknown {
  475. virtual void *Alloc(SIZE_T size) = 0;
  476. virtual void *Realloc(void *ptr, SIZE_T size) = 0;
  477. virtual void Free(void *ptr) = 0;
  478. virtual SIZE_T GetSize(void *pv) = 0;
  479. virtual int DidAlloc(void *pv) = 0;
  480. virtual void HeapMinimize(void) = 0;
  481. };
  482. CROSS_PLATFORM_UUIDOF(ISequentialStream, "0C733A30-2A1C-11CE-ADE5-00AA0044773D")
  483. struct ISequentialStream : public IUnknown {
  484. virtual HRESULT Read(void *pv, ULONG cb, ULONG *pcbRead) = 0;
  485. virtual HRESULT Write(const void *pv, ULONG cb, ULONG *pcbWritten) = 0;
  486. };
  487. CROSS_PLATFORM_UUIDOF(IStream, "0000000c-0000-0000-C000-000000000046")
  488. struct IStream : public ISequentialStream {
  489. virtual HRESULT Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin,
  490. ULARGE_INTEGER *plibNewPosition) = 0;
  491. virtual HRESULT SetSize(ULARGE_INTEGER libNewSize) = 0;
  492. virtual HRESULT CopyTo(IStream *pstm, ULARGE_INTEGER cb,
  493. ULARGE_INTEGER *pcbRead,
  494. ULARGE_INTEGER *pcbWritten) = 0;
  495. virtual HRESULT Commit(DWORD grfCommitFlags) = 0;
  496. virtual HRESULT Revert(void) = 0;
  497. virtual HRESULT LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  498. DWORD dwLockType) = 0;
  499. virtual HRESULT UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  500. DWORD dwLockType) = 0;
  501. virtual HRESULT Stat(STATSTG *pstatstg, DWORD grfStatFlag) = 0;
  502. virtual HRESULT Clone(IStream **ppstm) = 0;
  503. };
  504. // These don't need stub implementations as they come from the DirectX Headers
  505. // They still need the __uuidof() though
  506. CROSS_PLATFORM_UUIDOF(ID3D12LibraryReflection,
  507. "8E349D19-54DB-4A56-9DC9-119D87BDB804")
  508. CROSS_PLATFORM_UUIDOF(ID3D12ShaderReflection,
  509. "5A58797D-A72C-478D-8BA2-EFC6B0EFE88E")
  510. //===--------------------- COM Pointer Types ------------------------------===//
  511. class CAllocator {
  512. public:
  513. static void *Reallocate(void *p, size_t nBytes) throw();
  514. static void *Allocate(size_t nBytes) throw();
  515. static void Free(void *p) throw();
  516. };
  517. template <class T> class CComPtrBase {
  518. protected:
  519. CComPtrBase() throw() { p = nullptr; }
  520. CComPtrBase(T *lp) throw() {
  521. p = lp;
  522. if (p != nullptr)
  523. p->AddRef();
  524. }
  525. void Swap(CComPtrBase &other) {
  526. T *pTemp = p;
  527. p = other.p;
  528. other.p = pTemp;
  529. }
  530. public:
  531. ~CComPtrBase() throw() {
  532. if (p) {
  533. p->Release();
  534. p = nullptr;
  535. }
  536. }
  537. operator T *() const throw() { return p; }
  538. T &operator*() const { return *p; }
  539. T *operator->() const { return p; }
  540. T **operator&() throw() {
  541. assert(p == nullptr);
  542. return &p;
  543. }
  544. bool operator!() const throw() { return (p == nullptr); }
  545. bool operator<(T *pT) const throw() { return p < pT; }
  546. bool operator!=(T *pT) const { return !operator==(pT); }
  547. bool operator==(T *pT) const throw() { return p == pT; }
  548. // Release the interface and set to nullptr
  549. void Release() throw() {
  550. T *pTemp = p;
  551. if (pTemp) {
  552. p = nullptr;
  553. pTemp->Release();
  554. }
  555. }
  556. // Attach to an existing interface (does not AddRef)
  557. void Attach(T *p2) throw() {
  558. if (p) {
  559. ULONG ref = p->Release();
  560. (void)(ref);
  561. // Attaching to the same object only works if duplicate references are
  562. // being coalesced. Otherwise re-attaching will cause the pointer to be
  563. // released and may cause a crash on a subsequent dereference.
  564. assert(ref != 0 || p2 != p);
  565. }
  566. p = p2;
  567. }
  568. // Detach the interface (does not Release)
  569. T *Detach() throw() {
  570. T *pt = p;
  571. p = nullptr;
  572. return pt;
  573. }
  574. HRESULT CopyTo(T **ppT) throw() {
  575. assert(ppT != nullptr);
  576. if (ppT == nullptr)
  577. return E_POINTER;
  578. *ppT = p;
  579. if (p)
  580. p->AddRef();
  581. return S_OK;
  582. }
  583. template <class Q> HRESULT QueryInterface(Q **pp) const throw() {
  584. assert(pp != nullptr);
  585. return p->QueryInterface(__uuidof(Q), (void **)pp);
  586. }
  587. T *p;
  588. };
  589. template <class T> class CComPtr : public CComPtrBase<T> {
  590. public:
  591. CComPtr() throw() {}
  592. CComPtr(T *lp) throw() : CComPtrBase<T>(lp) {}
  593. CComPtr(const CComPtr<T> &lp) throw() : CComPtrBase<T>(lp.p) {}
  594. T *operator=(T *lp) throw() {
  595. if (*this != lp) {
  596. CComPtr(lp).Swap(*this);
  597. }
  598. return *this;
  599. }
  600. inline bool IsEqualObject(IUnknown *pOther) throw() {
  601. if (this->p == nullptr && pOther == nullptr)
  602. return true; // They are both NULL objects
  603. if (this->p == nullptr || pOther == nullptr)
  604. return false; // One is NULL the other is not
  605. CComPtr<IUnknown> punk1;
  606. CComPtr<IUnknown> punk2;
  607. this->p->QueryInterface(__uuidof(IUnknown), (void **)&punk1);
  608. pOther->QueryInterface(__uuidof(IUnknown), (void **)&punk2);
  609. return punk1 == punk2;
  610. }
  611. void ComPtrAssign(IUnknown **pp, IUnknown *lp, REFIID riid) {
  612. IUnknown *pTemp = *pp; // takes ownership
  613. if (lp == nullptr || FAILED(lp->QueryInterface(riid, (void **)pp)))
  614. *pp = nullptr;
  615. if (pTemp)
  616. pTemp->Release();
  617. }
  618. template <typename Q> T *operator=(const CComPtr<Q> &lp) throw() {
  619. if (!this->IsEqualObject(lp)) {
  620. ComPtrAssign((IUnknown **)&this->p, lp, __uuidof(T));
  621. }
  622. return *this;
  623. }
  624. // NOTE: This conversion constructor is not part of the official CComPtr spec;
  625. // however, it is needed to convert CComPtr<Q> to CComPtr<T> where T derives
  626. // from Q on Clang. MSVC compiles this conversion as first a call to
  627. // CComPtr<Q>::operator T*, followed by CComPtr<T>(T*), but Clang fails to
  628. // compile with error: no viable conversion from 'CComPtr<Q>' to 'CComPtr<T>'.
  629. template <typename Q>
  630. CComPtr(const CComPtr<Q> &lp) throw() : CComPtrBase<T>(lp.p) {}
  631. T *operator=(const CComPtr<T> &lp) throw() {
  632. if (*this != lp) {
  633. CComPtr(lp).Swap(*this);
  634. }
  635. return *this;
  636. }
  637. CComPtr(CComPtr<T> &&lp) throw() : CComPtrBase<T>() { lp.Swap(*this); }
  638. T *operator=(CComPtr<T> &&lp) throw() {
  639. if (*this != lp) {
  640. CComPtr(static_cast<CComPtr &&>(lp)).Swap(*this);
  641. }
  642. return *this;
  643. }
  644. };
  645. template <class T> class CSimpleArray : public std::vector<T> {
  646. public:
  647. bool Add(const T &t) {
  648. this->push_back(t);
  649. return true;
  650. }
  651. int GetSize() { return this->size(); }
  652. T *GetData() { return this->data(); }
  653. void RemoveAll() { this->clear(); }
  654. };
  655. template <class T, class Allocator = CAllocator> class CHeapPtrBase {
  656. protected:
  657. CHeapPtrBase() throw() : m_pData(NULL) {}
  658. CHeapPtrBase(CHeapPtrBase<T, Allocator> &p) throw() {
  659. m_pData = p.Detach(); // Transfer ownership
  660. }
  661. explicit CHeapPtrBase(T *pData) throw() : m_pData(pData) {}
  662. public:
  663. ~CHeapPtrBase() throw() { Free(); }
  664. protected:
  665. CHeapPtrBase<T, Allocator> &operator=(CHeapPtrBase<T, Allocator> &p) throw() {
  666. if (m_pData != p.m_pData)
  667. Attach(p.Detach()); // Transfer ownership
  668. return *this;
  669. }
  670. public:
  671. operator T *() const throw() { return m_pData; }
  672. T *operator->() const throw() {
  673. assert(m_pData != NULL);
  674. return m_pData;
  675. }
  676. T **operator&() throw() {
  677. assert(m_pData == NULL);
  678. return &m_pData;
  679. }
  680. // Allocate a buffer with the given number of bytes
  681. bool AllocateBytes(size_t nBytes) throw() {
  682. assert(m_pData == NULL);
  683. m_pData = static_cast<T *>(Allocator::Allocate(nBytes * sizeof(char)));
  684. if (m_pData == NULL)
  685. return false;
  686. return true;
  687. }
  688. // Attach to an existing pointer (takes ownership)
  689. void Attach(T *pData) throw() {
  690. Allocator::Free(m_pData);
  691. m_pData = pData;
  692. }
  693. // Detach the pointer (releases ownership)
  694. T *Detach() throw() {
  695. T *pTemp = m_pData;
  696. m_pData = NULL;
  697. return pTemp;
  698. }
  699. // Free the memory pointed to, and set the pointer to NULL
  700. void Free() throw() {
  701. Allocator::Free(m_pData);
  702. m_pData = NULL;
  703. }
  704. // Reallocate the buffer to hold a given number of bytes
  705. bool ReallocateBytes(size_t nBytes) throw() {
  706. T *pNew;
  707. pNew =
  708. static_cast<T *>(Allocator::Reallocate(m_pData, nBytes * sizeof(char)));
  709. if (pNew == NULL)
  710. return false;
  711. m_pData = pNew;
  712. return true;
  713. }
  714. public:
  715. T *m_pData;
  716. };
  717. template <typename T, class Allocator = CAllocator>
  718. class CHeapPtr : public CHeapPtrBase<T, Allocator> {
  719. public:
  720. CHeapPtr() throw() {}
  721. CHeapPtr(CHeapPtr<T, Allocator> &p) throw() : CHeapPtrBase<T, Allocator>(p) {}
  722. explicit CHeapPtr(T *p) throw() : CHeapPtrBase<T, Allocator>(p) {}
  723. CHeapPtr<T> &operator=(CHeapPtr<T, Allocator> &p) throw() {
  724. CHeapPtrBase<T, Allocator>::operator=(p);
  725. return *this;
  726. }
  727. // Allocate a buffer with the given number of elements
  728. bool Allocate(size_t nElements = 1) throw() {
  729. size_t nBytes = nElements * sizeof(T);
  730. return this->AllocateBytes(nBytes);
  731. }
  732. // Reallocate the buffer to hold a given number of elements
  733. bool Reallocate(size_t nElements) throw() {
  734. size_t nBytes = nElements * sizeof(T);
  735. return this->ReallocateBytes(nBytes);
  736. }
  737. };
  738. #define CComHeapPtr CHeapPtr
  739. //===--------------------------- BSTR Allocation --------------------------===//
  740. void SysFreeString(BSTR bstrString);
  741. // Allocate string with length prefix
  742. BSTR SysAllocStringLen(const OLECHAR *strIn, UINT ui);
  743. //===--------------------------- BSTR Length ------------------------------===//
  744. unsigned int SysStringLen(const BSTR bstrString);
  745. //===--------------------- UTF-8 Related Types ----------------------------===//
  746. // Code Page
  747. #define CP_ACP 0
  748. #define CP_UTF8 65001 // UTF-8 translation.
  749. // RAII style mechanism for setting/unsetting a locale for the specified Windows
  750. // codepage
  751. class ScopedLocale {
  752. const char *m_prevLocale;
  753. public:
  754. explicit ScopedLocale(uint32_t codePage)
  755. : m_prevLocale(setlocale(LC_ALL, nullptr)) {
  756. assert((codePage == CP_UTF8) &&
  757. "Support for Linux only handles UTF8 code pages");
  758. setlocale(LC_ALL, "en_US.UTF-8");
  759. }
  760. ~ScopedLocale() {
  761. if (m_prevLocale != nullptr) {
  762. setlocale(LC_ALL, m_prevLocale);
  763. }
  764. }
  765. };
  766. // The t_nBufferLength parameter is part of the published interface, but not
  767. // used here.
  768. template <int t_nBufferLength = 128> class CW2AEX {
  769. public:
  770. CW2AEX(LPCWSTR psz) {
  771. ScopedLocale locale(CP_UTF8);
  772. if (!psz) {
  773. m_psz = NULL;
  774. return;
  775. }
  776. int len = (wcslen(psz) + 1) * 4;
  777. m_psz = new char[len];
  778. std::wcstombs(m_psz, psz, len);
  779. }
  780. ~CW2AEX() { delete[] m_psz; }
  781. operator LPSTR() const { return m_psz; }
  782. char *m_psz;
  783. };
  784. typedef CW2AEX<> CW2A;
  785. // The t_nBufferLength parameter is part of the published interface, but not
  786. // used here.
  787. template <int t_nBufferLength = 128> class CA2WEX {
  788. public:
  789. CA2WEX(LPCSTR psz) {
  790. ScopedLocale locale(CP_UTF8);
  791. if (!psz) {
  792. m_psz = NULL;
  793. return;
  794. }
  795. int len = strlen(psz) + 1;
  796. m_psz = new wchar_t[len];
  797. std::mbstowcs(m_psz, psz, len);
  798. }
  799. ~CA2WEX() { delete[] m_psz; }
  800. operator LPWSTR() const { return m_psz; }
  801. wchar_t *m_psz;
  802. };
  803. typedef CA2WEX<> CA2W;
  804. //===--------- File IO Related Types ----------------===//
  805. class CHandle {
  806. public:
  807. CHandle(HANDLE h);
  808. ~CHandle();
  809. operator HANDLE() const throw();
  810. private:
  811. HANDLE m_h;
  812. };
  813. /////////////////////////////////////////////////////////////////////////////
  814. // CComBSTR
  815. class CComBSTR {
  816. public:
  817. BSTR m_str;
  818. CComBSTR() : m_str(nullptr){};
  819. CComBSTR(int nSize, LPCWSTR sz);
  820. ~CComBSTR() throw() { SysFreeString(m_str); }
  821. unsigned int Length() const throw() { return SysStringLen(m_str); }
  822. operator BSTR() const throw() { return m_str; }
  823. bool operator==(const CComBSTR &bstrSrc) const throw();
  824. BSTR *operator&() throw() { return &m_str; }
  825. BSTR Detach() throw() {
  826. BSTR s = m_str;
  827. m_str = NULL;
  828. return s;
  829. }
  830. void Empty() throw() {
  831. SysFreeString(m_str);
  832. m_str = NULL;
  833. }
  834. };
  835. //===--------- Convert argv to wchar ----------------===//
  836. class WArgV {
  837. std::vector<std::wstring> WStringVector;
  838. std::vector<const wchar_t *> WCharPtrVector;
  839. public:
  840. WArgV(int argc, const char **argv);
  841. const wchar_t **argv() { return WCharPtrVector.data(); }
  842. };
  843. #endif // __cplusplus
  844. #endif // _WIN32
  845. #endif // LLVM_SUPPORT_WIN_ADAPTER_H