blob: 1236fe56521798f007754879d459eb93ca4e4086 [file] [log] [blame]
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00001//===- Win32/Win32.h - Common Win32 Include File ----------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer0f0c5cf2004-09-15 05:48:11 +00008//===----------------------------------------------------------------------===//
9//
Jeff Cohen3800a282005-07-13 02:15:18 +000010// This file defines things specific to Win32 implementations.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000011//
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
Jeff Cohen3800a282005-07-13 02:15:18 +000015//=== WARNING: Implementation here must contain only generic Win32 code that
16//=== is guaranteed to work on *all* Win32 variants.
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000017//===----------------------------------------------------------------------===//
18
NAKAMURA Takumiaeb4c0d2011-02-09 04:18:30 +000019// mingw-w64 tends to define it as 0x0502 in its headers.
20#undef _WIN32_WINNT
21
NAKAMURA Takumi613655f2011-08-20 06:35:31 +000022// Require at least Windows XP(5.1) API.
23#define _WIN32_WINNT 0x0501
NAKAMURA Takumifd6cb642011-08-23 03:49:11 +000024#define _WIN32_IE 0x0600 // MinGW at it again.
Michael J. Spencerbb6e51c2010-11-09 15:11:07 +000025#define WIN32_LEAN_AND_MEAN
Jeff Cohen3800a282005-07-13 02:15:18 +000026
Rui Ueyama471d0c52013-09-10 19:45:51 +000027#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringRef.h"
Michael J. Spencer4b263dd2010-11-09 15:10:29 +000029#include "llvm/Config/config.h" // Get build system configuration settings
Timur Iskhodzhanov05885132013-05-15 09:00:30 +000030#include "llvm/Support/Compiler.h"
Rui Ueyama471d0c52013-09-10 19:45:51 +000031#include "llvm/Support/system_error.h"
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000032#include <windows.h>
Michael J. Spencer513f1b62011-12-12 06:03:33 +000033#include <wincrypt.h>
NAKAMURA Takumi5a3ff5b2011-02-04 12:53:04 +000034#include <shlobj.h>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000035#include <cassert>
36#include <string>
Rui Ueyama471d0c52013-09-10 19:45:51 +000037#include <vector>
Reid Spencer0f0c5cf2004-09-15 05:48:11 +000038
Reid Spencer879ed5a2006-08-23 07:30:48 +000039inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
Reid Spencer42bcf6e2006-08-21 06:02:44 +000040 if (!ErrMsg)
Reid Spencer879ed5a2006-08-23 07:30:48 +000041 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000042 char *buffer = NULL;
43 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
44 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
Anton Korobeynikov6c6a70f2006-09-01 20:35:17 +000045 *ErrMsg = prefix + buffer;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000046 LocalFree(buffer);
Reid Spencer879ed5a2006-08-23 07:30:48 +000047 return true;
Reid Spencer42bcf6e2006-08-21 06:02:44 +000048}
Jeff Cohena531d042007-03-05 05:22:08 +000049
Michael J. Spencer513f1b62011-12-12 06:03:33 +000050template <typename HandleTraits>
Michael J. Spencer39c46212010-12-06 04:28:13 +000051class ScopedHandle {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000052 typedef typename HandleTraits::handle_type handle_type;
53 handle_type Handle;
Michael J. Spencer39c46212010-12-06 04:28:13 +000054
Michael J. Spencer513f1b62011-12-12 06:03:33 +000055 ScopedHandle(const ScopedHandle &other); // = delete;
56 void operator=(const ScopedHandle &other); // = delete;
Michael J. Spencer39c46212010-12-06 04:28:13 +000057public:
Michael J. Spencer513f1b62011-12-12 06:03:33 +000058 ScopedHandle()
59 : Handle(HandleTraits::GetInvalid()) {}
60
61 explicit ScopedHandle(handle_type h)
62 : Handle(h) {}
Michael J. Spencer39c46212010-12-06 04:28:13 +000063
64 ~ScopedHandle() {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000065 if (HandleTraits::IsValid(Handle))
66 HandleTraits::Close(Handle);
Michael J. Spencer39c46212010-12-06 04:28:13 +000067 }
68
Michael J. Spencer513f1b62011-12-12 06:03:33 +000069 handle_type take() {
70 handle_type t = Handle;
71 Handle = HandleTraits::GetInvalid();
72 return t;
Michael J. Spencer39c46212010-12-06 04:28:13 +000073 }
74
Michael J. Spencer513f1b62011-12-12 06:03:33 +000075 ScopedHandle &operator=(handle_type h) {
76 if (HandleTraits::IsValid(Handle))
77 HandleTraits::Close(Handle);
78 Handle = h;
Michael J. Spencer39c46212010-12-06 04:28:13 +000079 return *this;
80 }
81
Michael J. Spencer39c46212010-12-06 04:28:13 +000082 // True if Handle is valid.
David Blaikie041f1aa2013-05-15 07:36:59 +000083 LLVM_EXPLICIT operator bool() const {
Michael J. Spencer513f1b62011-12-12 06:03:33 +000084 return HandleTraits::IsValid(Handle) ? true : false;
Michael J. Spencer39c46212010-12-06 04:28:13 +000085 }
86
Michael J. Spencer513f1b62011-12-12 06:03:33 +000087 operator handle_type() const {
88 return Handle;
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000089 }
Michael J. Spencer39c46212010-12-06 04:28:13 +000090};
Michael J. Spencer7ecd94c2010-12-06 04:28:42 +000091
Michael J. Spencer513f1b62011-12-12 06:03:33 +000092struct CommonHandleTraits {
93 typedef HANDLE handle_type;
94
95 static handle_type GetInvalid() {
96 return INVALID_HANDLE_VALUE;
97 }
98
99 static void Close(handle_type h) {
100 ::CloseHandle(h);
101 }
102
103 static bool IsValid(handle_type h) {
104 return h != GetInvalid();
105 }
106};
107
108struct JobHandleTraits : CommonHandleTraits {
109 static handle_type GetInvalid() {
110 return NULL;
111 }
112};
113
114struct CryptContextTraits : CommonHandleTraits {
115 typedef HCRYPTPROV handle_type;
116
117 static handle_type GetInvalid() {
118 return 0;
119 }
120
121 static void Close(handle_type h) {
122 ::CryptReleaseContext(h, 0);
123 }
124
125 static bool IsValid(handle_type h) {
126 return h != GetInvalid();
127 }
128};
129
130struct FindHandleTraits : CommonHandleTraits {
131 static void Close(handle_type h) {
132 ::FindClose(h);
133 }
134};
135
136struct FileHandleTraits : CommonHandleTraits {};
137
138typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
139typedef ScopedHandle<FileHandleTraits> ScopedFileHandle;
140typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
141typedef ScopedHandle<FindHandleTraits> ScopedFindHandle;
142typedef ScopedHandle<JobHandleTraits> ScopedJobHandle;
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000143
144namespace llvm {
145template <class T>
146class SmallVectorImpl;
147
148template <class T>
149typename SmallVectorImpl<T>::const_pointer
150c_str(SmallVectorImpl<T> &str) {
151 str.push_back(0);
152 str.pop_back();
153 return str.data();
154}
Rui Ueyama471d0c52013-09-10 19:45:51 +0000155
156namespace sys {
157namespace windows {
158error_code UTF8ToUTF16(StringRef utf8,
159 SmallVectorImpl<wchar_t> &utf16);
160error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
161 SmallVectorImpl<char> &utf8);
162} // end namespace windows
163} // end namespace sys
Michael J. Spencer751e9aa2010-12-09 17:37:18 +0000164} // end namespace llvm.