WinAdapter.h 24 KB

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