WinAdapter.h 24 KB

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