WinAdapter.h 28 KB

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