LLVM API Documentation
00001 //===- Win32/Win32.h - Common Win32 Include File ----------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines things specific to Win32 implementations. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 //===----------------------------------------------------------------------===// 00015 //=== WARNING: Implementation here must contain only generic Win32 code that 00016 //=== is guaranteed to work on *all* Win32 variants. 00017 //===----------------------------------------------------------------------===// 00018 00019 // mingw-w64 tends to define it as 0x0502 in its headers. 00020 #undef _WIN32_WINNT 00021 00022 // Require at least Windows XP(5.1) API. 00023 #define _WIN32_WINNT 0x0501 00024 #define _WIN32_IE 0x0600 // MinGW at it again. 00025 #define WIN32_LEAN_AND_MEAN 00026 00027 #include "llvm/Config/config.h" // Get build system configuration settings 00028 #include "llvm/Support/Compiler.h" 00029 #include <windows.h> 00030 #include <wincrypt.h> 00031 #include <shlobj.h> 00032 #include <cassert> 00033 #include <string> 00034 00035 inline bool MakeErrMsg(std::string* ErrMsg, const std::string& prefix) { 00036 if (!ErrMsg) 00037 return true; 00038 char *buffer = NULL; 00039 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM, 00040 NULL, GetLastError(), 0, (LPSTR)&buffer, 1, NULL); 00041 *ErrMsg = prefix + buffer; 00042 LocalFree(buffer); 00043 return true; 00044 } 00045 00046 template <typename HandleTraits> 00047 class ScopedHandle { 00048 typedef typename HandleTraits::handle_type handle_type; 00049 handle_type Handle; 00050 00051 ScopedHandle(const ScopedHandle &other); // = delete; 00052 void operator=(const ScopedHandle &other); // = delete; 00053 public: 00054 ScopedHandle() 00055 : Handle(HandleTraits::GetInvalid()) {} 00056 00057 explicit ScopedHandle(handle_type h) 00058 : Handle(h) {} 00059 00060 ~ScopedHandle() { 00061 if (HandleTraits::IsValid(Handle)) 00062 HandleTraits::Close(Handle); 00063 } 00064 00065 handle_type take() { 00066 handle_type t = Handle; 00067 Handle = HandleTraits::GetInvalid(); 00068 return t; 00069 } 00070 00071 ScopedHandle &operator=(handle_type h) { 00072 if (HandleTraits::IsValid(Handle)) 00073 HandleTraits::Close(Handle); 00074 Handle = h; 00075 return *this; 00076 } 00077 00078 // True if Handle is valid. 00079 LLVM_EXPLICIT operator bool() const { 00080 return HandleTraits::IsValid(Handle) ? true : false; 00081 } 00082 00083 operator handle_type() const { 00084 return Handle; 00085 } 00086 }; 00087 00088 struct CommonHandleTraits { 00089 typedef HANDLE handle_type; 00090 00091 static handle_type GetInvalid() { 00092 return INVALID_HANDLE_VALUE; 00093 } 00094 00095 static void Close(handle_type h) { 00096 ::CloseHandle(h); 00097 } 00098 00099 static bool IsValid(handle_type h) { 00100 return h != GetInvalid(); 00101 } 00102 }; 00103 00104 struct JobHandleTraits : CommonHandleTraits { 00105 static handle_type GetInvalid() { 00106 return NULL; 00107 } 00108 }; 00109 00110 struct CryptContextTraits : CommonHandleTraits { 00111 typedef HCRYPTPROV handle_type; 00112 00113 static handle_type GetInvalid() { 00114 return 0; 00115 } 00116 00117 static void Close(handle_type h) { 00118 ::CryptReleaseContext(h, 0); 00119 } 00120 00121 static bool IsValid(handle_type h) { 00122 return h != GetInvalid(); 00123 } 00124 }; 00125 00126 struct FindHandleTraits : CommonHandleTraits { 00127 static void Close(handle_type h) { 00128 ::FindClose(h); 00129 } 00130 }; 00131 00132 struct FileHandleTraits : CommonHandleTraits {}; 00133 00134 typedef ScopedHandle<CommonHandleTraits> ScopedCommonHandle; 00135 typedef ScopedHandle<FileHandleTraits> ScopedFileHandle; 00136 typedef ScopedHandle<CryptContextTraits> ScopedCryptContext; 00137 typedef ScopedHandle<FindHandleTraits> ScopedFindHandle; 00138 typedef ScopedHandle<JobHandleTraits> ScopedJobHandle; 00139 00140 namespace llvm { 00141 template <class T> 00142 class SmallVectorImpl; 00143 00144 template <class T> 00145 typename SmallVectorImpl<T>::const_pointer 00146 c_str(SmallVectorImpl<T> &str) { 00147 str.push_back(0); 00148 str.pop_back(); 00149 return str.data(); 00150 } 00151 } // end namespace llvm.