Revert "Revert "Windows: Add support for unicode command lines""
[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 <cassert>
35 #include <string>
36 #include <vector>
37
38 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) {
39   if (!ErrMsg)
40     return true;
41   char *buffer = NULL;
42   FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
43       NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL);
44   *ErrMsg = prefix + buffer;
45   LocalFree(buffer);
46   return true;
47 }
48
49 template <typename HandleTraits>
50 class ScopedHandle {
51   typedef typename HandleTraits::handle_type handle_type;
52   handle_type Handle;
53
54   ScopedHandle(const ScopedHandle &other); // = delete;
55   void operator=(const ScopedHandle &other); // = delete;
56 public:
57   ScopedHandle()
58     : Handle(HandleTraits::GetInvalid()) {}
59
60   explicit ScopedHandle(handle_type h)
61     : Handle(h) {}
62
63   ~ScopedHandle() {
64     if (HandleTraits::IsValid(Handle))
65       HandleTraits::Close(Handle);
66   }
67
68   handle_type take() {
69     handle_type t = Handle;
70     Handle = HandleTraits::GetInvalid();
71     return t;
72   }
73
74   ScopedHandle &operator=(handle_type h) {
75     if (HandleTraits::IsValid(Handle))
76       HandleTraits::Close(Handle);
77     Handle = h;
78     return *this;
79   }
80
81   // True if Handle is valid.
82   LLVM_EXPLICIT operator bool() const {
83     return HandleTraits::IsValid(Handle) ? true : false;
84   }
85
86   operator handle_type() const {
87     return Handle;
88   }
89 };
90
91 struct CommonHandleTraits {
92   typedef HANDLE handle_type;
93
94   static handle_type GetInvalid() {
95     return INVALID_HANDLE_VALUE;
96   }
97
98   static void Close(handle_type h) {
99     ::CloseHandle(h);
100   }
101
102   static bool IsValid(handle_type h) {
103     return h != GetInvalid();
104   }
105 };
106
107 struct JobHandleTraits : CommonHandleTraits {
108   static handle_type GetInvalid() {
109     return NULL;
110   }
111 };
112
113 struct CryptContextTraits : CommonHandleTraits {
114   typedef HCRYPTPROV handle_type;
115
116   static handle_type GetInvalid() {
117     return 0;
118   }
119
120   static void Close(handle_type h) {
121     ::CryptReleaseContext(h, 0);
122   }
123
124   static bool IsValid(handle_type h) {
125     return h != GetInvalid();
126   }
127 };
128
129 struct FindHandleTraits : CommonHandleTraits {
130   static void Close(handle_type h) {
131     ::FindClose(h);
132   }
133 };
134
135 struct FileHandleTraits : CommonHandleTraits {};
136
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;
142
143 namespace llvm {
144 template <class T>
145 class SmallVectorImpl;
146
147 template <class T>
148 typename SmallVectorImpl<T>::const_pointer
149 c_str(SmallVectorImpl<T> &str) {
150   str.push_back(0);
151   str.pop_back();
152   return str.data();
153 }
154
155 namespace sys {
156 namespace windows {
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.