1236fe56521798f007754879d459eb93ca4e4086
[oota-llvm.git] / lib / Support / Windows / Windows.h
1 //===- Win32/Win32.h - Common Win32 Include File ----------------*- 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 things specific to Win32 implementations.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic Win32 code that
16 //===          is guaranteed to work on *all* Win32 variants.
17 //===----------------------------------------------------------------------===//
18
19 // mingw-w64 tends to define it as 0x0502 in its headers.
20 #undef _WIN32_WINNT
21
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
26
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"
32 #include <windows.h>
33 #include <wincrypt.h>
34 #include <shlobj.h>
35 #include <cassert>
36 #include <string>
37 #include <vector>
38
39 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
40   if (!ErrMsg)
41     return true;
42   char *buffer = NULL;
43   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
44       NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
45   *ErrMsg = prefix + buffer;
46   LocalFree(buffer);
47   return true;
48 }
49
50 template <typename HandleTraits>
51 class ScopedHandle {
52   typedef typename HandleTraits::handle_type handle_type;
53   handle_type Handle;
54
55   ScopedHandle(const ScopedHandle &other); // = delete;
56   void operator=(const ScopedHandle &other); // = delete;
57 public:
58   ScopedHandle()
59     : Handle(HandleTraits::GetInvalid()) {}
60
61   explicit ScopedHandle(handle_type h)
62     : Handle(h) {}
63
64   ~ScopedHandle() {
65     if (HandleTraits::IsValid(Handle))
66       HandleTraits::Close(Handle);
67   }
68
69   handle_type take() {
70     handle_type t = Handle;
71     Handle = HandleTraits::GetInvalid();
72     return t;
73   }
74
75   ScopedHandle &operator=(handle_type h) {
76     if (HandleTraits::IsValid(Handle))
77       HandleTraits::Close(Handle);
78     Handle = h;
79     return *this;
80   }
81
82   // True if Handle is valid.
83   LLVM_EXPLICIT operator bool() const {
84     return HandleTraits::IsValid(Handle) ? true : false;
85   }
86
87   operator handle_type() const {
88     return Handle;
89   }
90 };
91
92 struct 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
108 struct JobHandleTraits : CommonHandleTraits {
109   static handle_type GetInvalid() {
110     return NULL;
111   }
112 };
113
114 struct 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
130 struct FindHandleTraits : CommonHandleTraits {
131   static void Close(handle_type h) {
132     ::FindClose(h);
133   }
134 };
135
136 struct FileHandleTraits : CommonHandleTraits {};
137
138 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
139 typedef ScopedHandle<FileHandleTraits>   ScopedFileHandle;
140 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
141 typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
142 typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
143
144 namespace llvm {
145 template <class T>
146 class SmallVectorImpl;
147
148 template <class T>
149 typename SmallVectorImpl<T>::const_pointer
150 c_str(SmallVectorImpl<T> &str) {
151   str.push_back(0);
152   str.pop_back();
153   return str.data();
154 }
155
156 namespace sys {
157 namespace windows {
158 error_code UTF8ToUTF16(StringRef utf8,
159                        SmallVectorImpl<wchar_t> &utf16);
160 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
161                        SmallVectorImpl<char> &utf8);
162 } // end namespace windows
163 } // end namespace sys
164 } // end namespace llvm.