WinAdapter.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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 _strnicmp strnicmp
  141. #define vsprintf_s vsprintf
  142. #define strcat_s strcat
  143. #define strcpy_s(dst, n, src) strncpy(dst, src, n)
  144. #define _vscwprintf vwprintf
  145. #define vswprintf_s vswprintf
  146. #define swprintf_s swprintf
  147. #define StringCchCopyW(dst, n, src) wcsncpy(dst, src, n)
  148. #define OutputDebugStringW(msg) fputws(msg, stderr)
  149. #define OutputDebugStringA(msg) fputs(msg, stderr)
  150. #define OutputDebugFormatA(...) fprintf(stderr, __VA_ARGS__)
  151. #define CaptureStackBackTrace(FramesToSkip, FramesToCapture, BackTrace, BackTraceHash)\
  152. backtrace(BackTrace, FramesToCapture)
  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_(expr)
  245. #define _Analysis_assume_nullterminated_(x)
  246. #define _Success_(expr)
  247. #define __inexpressible_readableTo(size)
  248. #define __inexpressible_writableTo(size)
  249. #define _Printf_format_string_
  250. #define _Null_terminated_
  251. #define __fallthrough
  252. #define _Field_size_(size)
  253. #define _Field_size_full_(size)
  254. #define _Field_size_opt_(size)
  255. #define _Post_writable_byte_size_(size)
  256. #define _Post_readable_byte_size_(size)
  257. #define __drv_allocatesMem(mem)
  258. #define _COM_Outptr_
  259. #define _COM_Outptr_opt_
  260. #define _COM_Outptr_result_maybenull_
  261. #define _Null_
  262. #define _Notnull_
  263. #define _Maybenull_
  264. #define _Outptr_result_bytebuffer_(size)
  265. #define __debugbreak()
  266. // GCC produces erros on calling convention attributes.
  267. #ifdef __GNUC__
  268. #define __cdecl
  269. #define __CRTDECL
  270. #define __stdcall
  271. #define __vectorcall
  272. #define __thiscall
  273. #define __fastcall
  274. #define __clrcall
  275. #endif // __GNUC__
  276. //===----------------------------------------------------------------------===//
  277. //
  278. // Begin: Type Definitions
  279. //
  280. //===----------------------------------------------------------------------===//
  281. #ifdef __cplusplus
  282. typedef unsigned char BYTE;
  283. typedef unsigned char *LPBYTE;
  284. typedef BYTE BOOLEAN;
  285. typedef BOOLEAN *PBOOLEAN;
  286. typedef bool BOOL;
  287. typedef BOOL *LPBOOL;
  288. typedef int INT;
  289. typedef long LONG;
  290. typedef unsigned int UINT;
  291. typedef unsigned long ULONG;
  292. typedef long long LONGLONG;
  293. typedef long long LONG_PTR;
  294. typedef unsigned long long ULONGLONG;
  295. typedef uint16_t WORD;
  296. typedef uint32_t DWORD;
  297. typedef DWORD *LPDWORD;
  298. typedef uint32_t UINT32;
  299. typedef uint64_t UINT64;
  300. typedef signed char INT8, *PINT8;
  301. typedef signed int INT32, *PINT32;
  302. typedef size_t SIZE_T;
  303. typedef const char *LPCSTR;
  304. typedef const char *PCSTR;
  305. typedef int errno_t;
  306. typedef wchar_t WCHAR;
  307. typedef wchar_t *LPWSTR;
  308. typedef wchar_t *PWCHAR;
  309. typedef const wchar_t *LPCWSTR;
  310. typedef const wchar_t *PCWSTR;
  311. typedef WCHAR OLECHAR;
  312. typedef OLECHAR *BSTR;
  313. typedef OLECHAR *LPOLESTR;
  314. typedef char *LPSTR;
  315. typedef void *LPVOID;
  316. typedef const void *LPCVOID;
  317. typedef std::nullptr_t nullptr_t;
  318. typedef signed int HRESULT;
  319. //===--------------------- Handle Types -----------------------------------===//
  320. typedef void *HANDLE;
  321. #define DECLARE_HANDLE(name) \
  322. struct name##__ { \
  323. int unused; \
  324. }; \
  325. typedef struct name##__ *name
  326. DECLARE_HANDLE(HINSTANCE);
  327. typedef void *HMODULE;
  328. #define STD_INPUT_HANDLE ((DWORD)-10)
  329. #define STD_OUTPUT_HANDLE ((DWORD)-11)
  330. #define STD_ERROR_HANDLE ((DWORD)-12)
  331. //===--------------------- ID Types and Macros for COM --------------------===//
  332. struct GUID {
  333. uint32_t Data1;
  334. uint16_t Data2;
  335. uint16_t Data3;
  336. uint8_t Data4[8];
  337. };
  338. typedef GUID CLSID;
  339. typedef const GUID &REFGUID;
  340. typedef const void *REFIID;
  341. typedef const GUID &REFCLSID;
  342. #define IsEqualIID(a, b) a == b
  343. #define IsEqualCLSID(a, b) !memcmp(&a, &b, sizeof(GUID))
  344. //===--------------------- Struct Types -----------------------------------===//
  345. typedef struct _FILETIME {
  346. DWORD dwLowDateTime;
  347. DWORD dwHighDateTime;
  348. } FILETIME, *PFILETIME, *LPFILETIME;
  349. typedef struct _BY_HANDLE_FILE_INFORMATION {
  350. DWORD dwFileAttributes;
  351. FILETIME ftCreationTime;
  352. FILETIME ftLastAccessTime;
  353. FILETIME ftLastWriteTime;
  354. DWORD dwVolumeSerialNumber;
  355. DWORD nFileSizeHigh;
  356. DWORD nFileSizeLow;
  357. DWORD nNumberOfLinks;
  358. DWORD nFileIndexHigh;
  359. DWORD nFileIndexLow;
  360. } BY_HANDLE_FILE_INFORMATION, *PBY_HANDLE_FILE_INFORMATION,
  361. *LPBY_HANDLE_FILE_INFORMATION;
  362. typedef struct _WIN32_FIND_DATAW {
  363. DWORD dwFileAttributes;
  364. FILETIME ftCreationTime;
  365. FILETIME ftLastAccessTime;
  366. FILETIME ftLastWriteTime;
  367. DWORD nFileSizeHigh;
  368. DWORD nFileSizeLow;
  369. DWORD dwReserved0;
  370. DWORD dwReserved1;
  371. WCHAR cFileName[260];
  372. WCHAR cAlternateFileName[14];
  373. } WIN32_FIND_DATAW, *PWIN32_FIND_DATAW, *LPWIN32_FIND_DATAW;
  374. typedef union _LARGE_INTEGER {
  375. struct {
  376. DWORD LowPart;
  377. DWORD HighPart;
  378. } u;
  379. LONGLONG QuadPart;
  380. } LARGE_INTEGER;
  381. typedef LARGE_INTEGER *PLARGE_INTEGER;
  382. typedef union _ULARGE_INTEGER {
  383. struct {
  384. DWORD LowPart;
  385. DWORD HighPart;
  386. } u;
  387. ULONGLONG QuadPart;
  388. } ULARGE_INTEGER;
  389. typedef ULARGE_INTEGER *PULARGE_INTEGER;
  390. typedef struct tagSTATSTG {
  391. LPOLESTR pwcsName;
  392. DWORD type;
  393. ULARGE_INTEGER cbSize;
  394. FILETIME mtime;
  395. FILETIME ctime;
  396. FILETIME atime;
  397. DWORD grfMode;
  398. DWORD grfLocksSupported;
  399. CLSID clsid;
  400. DWORD grfStateBits;
  401. DWORD reserved;
  402. } STATSTG;
  403. enum tagSTATFLAG {
  404. STATFLAG_DEFAULT = 0,
  405. STATFLAG_NONAME = 1,
  406. STATFLAG_NOOPEN = 2
  407. };
  408. //===--------------------- UUID Related Macros ----------------------------===//
  409. // The following macros are defined to facilitate the lack of 'uuid' on Linux.
  410. #define DECLARE_CROSS_PLATFORM_UUIDOF(T) \
  411. public: \
  412. static REFIID uuidof() { return static_cast<REFIID>(&T##_ID); } \
  413. \
  414. private: \
  415. static const char T##_ID;
  416. #define DEFINE_CROSS_PLATFORM_UUIDOF(T) const char T::T##_ID = '\0';
  417. #define __uuidof(T) T::uuidof()
  418. #define IID_PPV_ARGS(ppType) \
  419. (**(ppType)).uuidof(), reinterpret_cast<void **>(ppType)
  420. //===--------------------- COM Interfaces ---------------------------------===//
  421. struct IUnknown {
  422. virtual HRESULT QueryInterface(REFIID riid, void **ppvObject) = 0;
  423. virtual ULONG AddRef();
  424. virtual ULONG Release();
  425. virtual ~IUnknown();
  426. template <class Q> HRESULT QueryInterface(Q **pp) {
  427. return QueryInterface(__uuidof(Q), (void **)pp);
  428. }
  429. private:
  430. std::atomic<unsigned long> m_count;
  431. DECLARE_CROSS_PLATFORM_UUIDOF(IUnknown)
  432. };
  433. struct INoMarshal : public IUnknown {
  434. DECLARE_CROSS_PLATFORM_UUIDOF(INoMarshal)
  435. };
  436. struct IMalloc : public IUnknown {
  437. virtual void *Alloc(size_t size);
  438. virtual void *Realloc(void *ptr, size_t size);
  439. virtual void Free(void *ptr);
  440. virtual HRESULT QueryInterface(REFIID riid, void **ppvObject);
  441. };
  442. struct ISequentialStream : public IUnknown {
  443. virtual HRESULT Read(void *pv, ULONG cb, ULONG *pcbRead) = 0;
  444. virtual HRESULT Write(const void *pv, ULONG cb, ULONG *pcbWritten) = 0;
  445. DECLARE_CROSS_PLATFORM_UUIDOF(ISequentialStream)
  446. };
  447. struct IStream : public ISequentialStream {
  448. virtual HRESULT Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin,
  449. ULARGE_INTEGER *plibNewPosition) = 0;
  450. virtual HRESULT SetSize(ULARGE_INTEGER libNewSize) = 0;
  451. virtual HRESULT CopyTo(IStream *pstm, ULARGE_INTEGER cb,
  452. ULARGE_INTEGER *pcbRead,
  453. ULARGE_INTEGER *pcbWritten) = 0;
  454. virtual HRESULT Commit(DWORD grfCommitFlags) = 0;
  455. virtual HRESULT Revert(void) = 0;
  456. virtual HRESULT LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  457. DWORD dwLockType) = 0;
  458. virtual HRESULT UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  459. DWORD dwLockType) = 0;
  460. virtual HRESULT Stat(STATSTG *pstatstg, DWORD grfStatFlag) = 0;
  461. virtual HRESULT Clone(IStream **ppstm) = 0;
  462. DECLARE_CROSS_PLATFORM_UUIDOF(IStream)
  463. };
  464. //===--------------------- COM Pointer Types ------------------------------===//
  465. class CAllocator {
  466. public:
  467. static void *Reallocate(void *p, size_t nBytes) throw();
  468. static void *Allocate(size_t nBytes) throw();
  469. static void Free(void *p) throw();
  470. };
  471. template <class T> class CComPtrBase {
  472. protected:
  473. CComPtrBase() throw() { p = nullptr; }
  474. CComPtrBase(T *lp) throw() {
  475. p = lp;
  476. if (p != nullptr)
  477. p->AddRef();
  478. }
  479. void Swap(CComPtrBase &other) {
  480. T *pTemp = p;
  481. p = other.p;
  482. other.p = pTemp;
  483. }
  484. public:
  485. ~CComPtrBase() throw() {
  486. if (p) {
  487. p->Release();
  488. p = nullptr;
  489. }
  490. }
  491. operator T *() const throw() { return p; }
  492. T &operator*() const { return *p; }
  493. T *operator->() const { return p; }
  494. T **operator&() throw() {
  495. assert(p == nullptr);
  496. return &p;
  497. }
  498. bool operator!() const throw() { return (p == nullptr); }
  499. bool operator<(T *pT) const throw() { return p < pT; }
  500. bool operator!=(T *pT) const { return !operator==(pT); }
  501. bool operator==(T *pT) const throw() { return p == pT; }
  502. // Release the interface and set to nullptr
  503. void Release() throw() {
  504. T *pTemp = p;
  505. if (pTemp) {
  506. p = nullptr;
  507. pTemp->Release();
  508. }
  509. }
  510. // Attach to an existing interface (does not AddRef)
  511. void Attach(T *p2) throw() {
  512. if (p) {
  513. ULONG ref = p->Release();
  514. (void)(ref);
  515. // Attaching to the same object only works if duplicate references are
  516. // being coalesced. Otherwise re-attaching will cause the pointer to be
  517. // released and may cause a crash on a subsequent dereference.
  518. assert(ref != 0 || p2 != p);
  519. }
  520. p = p2;
  521. }
  522. // Detach the interface (does not Release)
  523. T *Detach() throw() {
  524. T *pt = p;
  525. p = nullptr;
  526. return pt;
  527. }
  528. HRESULT CopyTo(T **ppT) throw() {
  529. assert(ppT != nullptr);
  530. if (ppT == nullptr)
  531. return E_POINTER;
  532. *ppT = p;
  533. if (p)
  534. p->AddRef();
  535. return S_OK;
  536. }
  537. template <class Q> HRESULT QueryInterface(Q **pp) const throw() {
  538. assert(pp != nullptr);
  539. return p->QueryInterface(__uuidof(Q), (void **)pp);
  540. }
  541. T *p;
  542. };
  543. template <class T> class CComPtr : public CComPtrBase<T> {
  544. public:
  545. CComPtr() throw() {}
  546. CComPtr(T *lp) throw() : CComPtrBase<T>(lp) {}
  547. CComPtr(const CComPtr<T> &lp) throw() : CComPtrBase<T>(lp.p) {}
  548. T *operator=(T *lp) throw() {
  549. if (*this != lp) {
  550. CComPtr(lp).Swap(*this);
  551. }
  552. return *this;
  553. }
  554. inline bool IsEqualObject(IUnknown *pOther) throw() {
  555. if (this->p == nullptr && pOther == nullptr)
  556. return true; // They are both NULL objects
  557. if (this->p == nullptr || pOther == nullptr)
  558. return false; // One is NULL the other is not
  559. CComPtr<IUnknown> punk1;
  560. CComPtr<IUnknown> punk2;
  561. this->p->QueryInterface(__uuidof(IUnknown), (void **)&punk1);
  562. pOther->QueryInterface(__uuidof(IUnknown), (void **)&punk2);
  563. return punk1 == punk2;
  564. }
  565. void ComPtrAssign(IUnknown **pp, IUnknown *lp, REFIID riid) {
  566. IUnknown *pTemp = *pp; // takes ownership
  567. if (lp == nullptr || FAILED(lp->QueryInterface(riid, (void **)pp)))
  568. *pp = nullptr;
  569. if (pTemp)
  570. pTemp->Release();
  571. }
  572. template <typename Q> T *operator=(const CComPtr<Q> &lp) throw() {
  573. if (!this->IsEqualObject(lp)) {
  574. ComPtrAssign((IUnknown **)&this->p, lp, __uuidof(T));
  575. }
  576. return *this;
  577. }
  578. T *operator=(const CComPtr<T> &lp) throw() {
  579. if (*this != lp) {
  580. CComPtr(lp).Swap(*this);
  581. }
  582. return *this;
  583. }
  584. CComPtr(CComPtr<T> &&lp) throw() : CComPtrBase<T>() { lp.Swap(*this); }
  585. T *operator=(CComPtr<T> &&lp) throw() {
  586. if (*this != lp) {
  587. CComPtr(static_cast<CComPtr &&>(lp)).Swap(*this);
  588. }
  589. return *this;
  590. }
  591. };
  592. template <class T> class CSimpleArray : public std::vector<T> {
  593. public:
  594. bool Add(const T &t) {
  595. this->push_back(t);
  596. return true;
  597. }
  598. int GetSize() { return this->size(); }
  599. T *GetData() { return this->data(); }
  600. void RemoveAll() { this->clear(); }
  601. };
  602. template <class T, class Allocator = CAllocator> class CHeapPtrBase {
  603. protected:
  604. CHeapPtrBase() throw() : m_pData(NULL) {}
  605. CHeapPtrBase(CHeapPtrBase<T, Allocator> &p) throw() {
  606. m_pData = p.Detach(); // Transfer ownership
  607. }
  608. explicit CHeapPtrBase(T *pData) throw() : m_pData(pData) {}
  609. public:
  610. ~CHeapPtrBase() throw() { Free(); }
  611. protected:
  612. CHeapPtrBase<T, Allocator> &operator=(CHeapPtrBase<T, Allocator> &p) throw() {
  613. if (m_pData != p.m_pData)
  614. Attach(p.Detach()); // Transfer ownership
  615. return *this;
  616. }
  617. public:
  618. operator T *() const throw() { return m_pData; }
  619. T *operator->() const throw() {
  620. assert(m_pData != NULL);
  621. return m_pData;
  622. }
  623. T **operator&() throw() {
  624. assert(m_pData == NULL);
  625. return &m_pData;
  626. }
  627. // Allocate a buffer with the given number of bytes
  628. bool AllocateBytes(size_t nBytes) throw() {
  629. assert(m_pData == NULL);
  630. m_pData = static_cast<T *>(Allocator::Allocate(nBytes * sizeof(char)));
  631. if (m_pData == NULL)
  632. return false;
  633. return true;
  634. }
  635. // Attach to an existing pointer (takes ownership)
  636. void Attach(T *pData) throw() {
  637. Allocator::Free(m_pData);
  638. m_pData = pData;
  639. }
  640. // Detach the pointer (releases ownership)
  641. T *Detach() throw() {
  642. T *pTemp = m_pData;
  643. m_pData = NULL;
  644. return pTemp;
  645. }
  646. // Free the memory pointed to, and set the pointer to NULL
  647. void Free() throw() {
  648. Allocator::Free(m_pData);
  649. m_pData = NULL;
  650. }
  651. // Reallocate the buffer to hold a given number of bytes
  652. bool ReallocateBytes(size_t nBytes) throw() {
  653. T *pNew;
  654. pNew =
  655. static_cast<T *>(Allocator::Reallocate(m_pData, nBytes * sizeof(char)));
  656. if (pNew == NULL)
  657. return false;
  658. m_pData = pNew;
  659. return true;
  660. }
  661. public:
  662. T *m_pData;
  663. };
  664. template <typename T, class Allocator = CAllocator>
  665. class CHeapPtr : public CHeapPtrBase<T, Allocator> {
  666. public:
  667. CHeapPtr() throw() {}
  668. CHeapPtr(CHeapPtr<T, Allocator> &p) throw() : CHeapPtrBase<T, Allocator>(p) {}
  669. explicit CHeapPtr(T *p) throw() : CHeapPtrBase<T, Allocator>(p) {}
  670. CHeapPtr<T> &operator=(CHeapPtr<T, Allocator> &p) throw() {
  671. CHeapPtrBase<T, Allocator>::operator=(p);
  672. return *this;
  673. }
  674. // Allocate a buffer with the given number of elements
  675. bool Allocate(size_t nElements = 1) throw() {
  676. size_t nBytes = nElements * sizeof(T);
  677. return this->AllocateBytes(nBytes);
  678. }
  679. // Reallocate the buffer to hold a given number of elements
  680. bool Reallocate(size_t nElements) throw() {
  681. size_t nBytes = nElements * sizeof(T);
  682. return this->ReallocateBytes(nBytes);
  683. }
  684. };
  685. #define CComHeapPtr CHeapPtr
  686. //===--------------------- UTF-8 Related Types ----------------------------===//
  687. // Code Page
  688. #define CP_ACP 0
  689. #define CP_UTF8 65001 // UTF-8 translation.
  690. // Convert Windows codepage value to locale string
  691. const char *CPToLocale(uint32_t CodePage);
  692. // The t_nBufferLength parameter is part of the published interface, but not
  693. // used here.
  694. template <int t_nBufferLength = 128> class CW2AEX {
  695. public:
  696. CW2AEX(LPCWSTR psz, UINT nCodePage = CP_UTF8) {
  697. const char *locale = CPToLocale(nCodePage);
  698. if (locale == nullptr) {
  699. // Current Implementation only supports CP_UTF8, and CP_ACP
  700. assert(false && "CW2AEX implementation for Linux only handles "
  701. "UTF8 and ACP code pages");
  702. return;
  703. }
  704. if (!psz) {
  705. m_psz = NULL;
  706. return;
  707. }
  708. locale = setlocale(LC_ALL, locale);
  709. int len = (wcslen(psz) + 1) * 4;
  710. m_psz = new char[len];
  711. std::wcstombs(m_psz, psz, len);
  712. setlocale(LC_ALL, locale);
  713. }
  714. ~CW2AEX() { delete[] m_psz; }
  715. operator LPSTR() const { return m_psz; }
  716. char *m_psz;
  717. };
  718. typedef CW2AEX<> CW2A;
  719. // The t_nBufferLength parameter is part of the published interface, but not
  720. // used here.
  721. template <int t_nBufferLength = 128> class CA2WEX {
  722. public:
  723. CA2WEX(LPCSTR psz, UINT nCodePage = CP_UTF8) {
  724. const char *locale = CPToLocale(nCodePage);
  725. if (locale == nullptr) {
  726. // Current Implementation only supports CP_UTF8, and CP_ACP
  727. assert(false && "CA2WEX implementation for Linux only handles "
  728. "UTF8 and ACP code pages");
  729. return;
  730. }
  731. if (!psz) {
  732. m_psz = NULL;
  733. return;
  734. }
  735. locale = setlocale(LC_ALL, locale);
  736. int len = strlen(psz) + 1;
  737. m_psz = new wchar_t[len];
  738. std::mbstowcs(m_psz, psz, len);
  739. setlocale(LC_ALL, locale);
  740. }
  741. ~CA2WEX() { delete[] m_psz; }
  742. operator LPWSTR() const { return m_psz; }
  743. wchar_t *m_psz;
  744. };
  745. typedef CA2WEX<> CA2W;
  746. //===--------- File IO Related Types ----------------===//
  747. class CHandle {
  748. public:
  749. CHandle(HANDLE h);
  750. ~CHandle();
  751. operator HANDLE() const throw();
  752. private:
  753. HANDLE m_h;
  754. };
  755. #endif // __cplusplus
  756. #endif // _WIN32
  757. #endif // LLVM_SUPPORT_WIN_ADAPTER_H