1 //===- Win32/Win32.h - Common Win32 Include File ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines things specific to Win32 implementations.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Win32 code that
16 //=== is guaranteed to work on *all* Win32 variants.
17 //===----------------------------------------------------------------------===//
19 // mingw-w64 tends to define it as 0x0502 in its headers.
22 // Require at least Windows XP(5.1) API.
23 #define _WIN32_WINNT 0x0501
24 #define _WIN32_IE 0x0600 // MinGW at it again.
25 #define WIN32_LEAN_AND_MEAN
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/Config/config.h" // Get build system configuration settings
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/system_error.h"
38 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
42 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
43 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
44 *ErrMsg = prefix + buffer;
49 template <typename HandleTraits>
51 typedef typename HandleTraits::handle_type handle_type;
54 ScopedHandle(const ScopedHandle &other); // = delete;
55 void operator=(const ScopedHandle &other); // = delete;
58 : Handle(HandleTraits::GetInvalid()) {}
60 explicit ScopedHandle(handle_type h)
64 if (HandleTraits::IsValid(Handle))
65 HandleTraits::Close(Handle);
69 handle_type t = Handle;
70 Handle = HandleTraits::GetInvalid();
74 ScopedHandle &operator=(handle_type h) {
75 if (HandleTraits::IsValid(Handle))
76 HandleTraits::Close(Handle);
81 // True if Handle is valid.
82 LLVM_EXPLICIT operator bool() const {
83 return HandleTraits::IsValid(Handle) ? true : false;
86 operator handle_type() const {
91 struct CommonHandleTraits {
92 typedef HANDLE handle_type;
94 static handle_type GetInvalid() {
95 return INVALID_HANDLE_VALUE;
98 static void Close(handle_type h) {
102 static bool IsValid(handle_type h) {
103 return h != GetInvalid();
107 struct JobHandleTraits : CommonHandleTraits {
108 static handle_type GetInvalid() {
113 struct CryptContextTraits : CommonHandleTraits {
114 typedef HCRYPTPROV handle_type;
116 static handle_type GetInvalid() {
120 static void Close(handle_type h) {
121 ::CryptReleaseContext(h, 0);
124 static bool IsValid(handle_type h) {
125 return h != GetInvalid();
129 struct FindHandleTraits : CommonHandleTraits {
130 static void Close(handle_type h) {
135 struct FileHandleTraits : CommonHandleTraits {};
137 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
138 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
139 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
140 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
141 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
145 class SmallVectorImpl;
148 typename SmallVectorImpl<T>::const_pointer
149 c_str(SmallVectorImpl<T> &str) {
157 error_code UTF8ToUTF16(StringRef utf8,
158 SmallVectorImpl<wchar_t> &utf16);
159 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
160 SmallVectorImpl<char> &utf8);
161 } // end namespace windows
162 } // end namespace sys
163 } // end namespace llvm.