WinAdapter.h 27 KB

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