5666de267d72efb2a8a81910ec3ddf4336ee8a31
[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 #undef _WIN32_IE
22
23 // Require at least Windows XP(5.1) API.
24 #define _WIN32_WINNT 0x0501
25 #define _WIN32_IE    0x0600 // MinGW at it again.
26 #define WIN32_LEAN_AND_MEAN
27
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/Config/config.h" // Get build system configuration settings
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/system_error.h"
33 #include <windows.h>
34 #include <wincrypt.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   DWORD R = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
44                           FORMAT_MESSAGE_FROM_SYSTEM,
45                           NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
46   if (R)
47     *ErrMsg = prefix + buffer;
48   else
49     *ErrMsg = prefix + "Unknown error";
50
51   LocalFree(buffer);
52   return R != 0;
53 }
54
55 template <typename HandleTraits>
56 class ScopedHandle {
57   typedef typename HandleTraits::handle_type handle_type;
58   handle_type Handle;
59
60   ScopedHandle(const ScopedHandle &other); // = delete;
61   void operator=(const ScopedHandle &other); // = delete;
62 public:
63   ScopedHandle()
64     : Handle(HandleTraits::GetInvalid()) {}
65
66   explicit ScopedHandle(handle_type h)
67     : Handle(h) {}
68
69   ~ScopedHandle() {
70     if (HandleTraits::IsValid(Handle))
71       HandleTraits::Close(Handle);
72   }
73
74   handle_type take() {
75     handle_type t = Handle;
76     Handle = HandleTraits::GetInvalid();
77     return t;
78   }
79
80   ScopedHandle &operator=(handle_type h) {
81     if (HandleTraits::IsValid(Handle))
82       HandleTraits::Close(Handle);
83     Handle = h;
84     return *this;
85   }
86
87   // True if Handle is valid.
88   LLVM_EXPLICIT operator bool() const {
89     return HandleTraits::IsValid(Handle) ? true : false;
90   }
91
92   operator handle_type() const {
93     return Handle;
94   }
95 };
96
97 struct CommonHandleTraits {
98   typedef HANDLE handle_type;
99
100   static handle_type GetInvalid() {
101     return INVALID_HANDLE_VALUE;
102   }
103
104   static void Close(handle_type h) {
105     ::CloseHandle(h);
106   }
107
108   static bool IsValid(handle_type h) {
109     return h != GetInvalid();
110   }
111 };
112
113 struct JobHandleTraits : CommonHandleTraits {
114   static handle_type GetInvalid() {
115     return NULL;
116   }
117 };
118
119 struct CryptContextTraits : CommonHandleTraits {
120   typedef HCRYPTPROV handle_type;
121
122   static handle_type GetInvalid() {
123     return 0;
124   }
125
126   static void Close(handle_type h) {
127     ::CryptReleaseContext(h, 0);
128   }
129
130   static bool IsValid(handle_type h) {
131     return h != GetInvalid();
132   }
133 };
134
135 struct FindHandleTraits : CommonHandleTraits {
136   static void Close(handle_type h) {
137     ::FindClose(h);
138   }
139 };
140
141 struct FileHandleTraits : CommonHandleTraits {};
142
143 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle;
144 typedef ScopedHandle<FileHandleTraits>   ScopedFileHandle;
145 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext;
146 typedef ScopedHandle<FindHandleTraits>   ScopedFindHandle;
147 typedef ScopedHandle<JobHandleTraits>    ScopedJobHandle;
148
149 namespace llvm {
150 template <class T>
151 class SmallVectorImpl;
152
153 template <class T>
154 typename SmallVectorImpl<T>::const_pointer
155 c_str(SmallVectorImpl<T> &str) {
156   str.push_back(0);
157   str.pop_back();
158   return str.data();
159 }
160
161 namespace sys {
162 namespace windows {
163 error_code UTF8ToUTF16(StringRef utf8,
164                        SmallVectorImpl<wchar_t> &utf16);
165 error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
166                        SmallVectorImpl<char> &utf8);
167 } // end namespace windows
168 } // end namespace sys
169 } // end namespace llvm.