[Support] Update MSVC compiler support in Compiler.h.
[oota-llvm.git] / include / llvm / Support / Compiler.h
1 //===-- llvm/Support/Compiler.h - Compiler abstraction support --*- 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 several macros, based on the current compiler.  This allows
11 // use of compiler-specific features in a way that remains portable.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_COMPILER_H
16 #define LLVM_SUPPORT_COMPILER_H
17
18 #ifndef __has_feature
19 # define __has_feature(x) 0
20 #endif
21
22 /// \brief Does the compiler support r-value references?
23 /// This implies that <utility> provides the one-argument std::move;  it
24 /// does not imply the existence of any other C++ library features.
25 #if (__has_feature(cxx_rvalue_references)   \
26      || defined(__GXX_EXPERIMENTAL_CXX0X__) \
27      || (defined(_MSC_VER) && _MSC_VER >= 1600))
28 #define LLVM_HAS_RVALUE_REFERENCES 1
29 #else
30 #define LLVM_HAS_RVALUE_REFERENCES 0
31 #endif
32
33 /// \brief Does the compiler support r-value reference *this?
34 ///
35 /// Sadly, this is separate from just r-value reference support because GCC
36 /// implemented everything but this thus far. No release of GCC yet has support
37 /// for this feature so it is enabled with Clang only.
38 /// FIXME: This should change to a version check when GCC grows support for it.
39 #if __has_feature(cxx_rvalue_references)
40 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
41 #else
42 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
43 #endif
44
45 /// llvm_move - Expands to ::std::move if the compiler supports
46 /// r-value references; otherwise, expands to the argument.
47 #if LLVM_HAS_RVALUE_REFERENCES
48 #define llvm_move(value) (::std::move(value))
49 #else
50 #define llvm_move(value) (value)
51 #endif
52
53 /// Expands to '&' if r-value references are supported.
54 ///
55 /// This can be used to provide l-value/r-value overrides of member functions.
56 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
57 #if LLVM_HAS_RVALUE_REFERENCE_THIS
58 #define LLVM_LVALUE_FUNCTION &
59 #else
60 #define LLVM_LVALUE_FUNCTION
61 #endif
62
63 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
64 /// Use to mark functions as uncallable. Member functions with this should
65 /// be declared private so that some behavior is kept in C++03 mode.
66 ///
67 /// class DontCopy {
68 /// private:
69 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
70 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
71 /// public:
72 ///   ...
73 /// };
74 #if (__has_feature(cxx_deleted_functions) \
75      || defined(__GXX_EXPERIMENTAL_CXX0X__))
76      // No version of MSVC currently supports this.
77 #define LLVM_DELETED_FUNCTION = delete
78 #else
79 #define LLVM_DELETED_FUNCTION
80 #endif
81
82 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
83 /// Use to mark classes or virtual methods as final.
84 #if __has_feature(cxx_override_control) \
85     || (defined(_MSC_VER) && _MSC_VER >= 1700)
86 #define LLVM_FINAL final
87 #else
88 #define LLVM_FINAL
89 #endif
90
91 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
92 /// Use to mark virtual methods as overriding a base class method.
93 #if __has_feature(cxx_override_control) \
94     || (defined(_MSC_VER) && _MSC_VER >= 1700)
95 #define LLVM_OVERRIDE override
96 #else
97 #define LLVM_OVERRIDE
98 #endif
99
100 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
101 # define LLVM_CONSTEXPR constexpr
102 #else
103 # define LLVM_CONSTEXPR
104 #endif
105
106 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
107 /// into a shared library, then the class should be private to the library and
108 /// not accessible from outside it.  Can also be used to mark variables and
109 /// functions, making them private to any shared library they are linked into.
110 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
111 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
112 #else
113 #define LLVM_LIBRARY_VISIBILITY
114 #endif
115
116 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
117 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
118 #else
119 #define LLVM_ATTRIBUTE_USED
120 #endif
121
122 // Some compilers warn about unused functions. When a function is sometimes
123 // used or not depending on build settings (e.g. a function only called from
124 // within "assert"), this attribute can be used to suppress such warnings.
125 //
126 // However, it shouldn't be used for unused *variables*, as those have a much
127 // more portable solution:
128 //   (void)unused_var_name;
129 // Prefer cast-to-void wherever it is sufficient.
130 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
131 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
132 #else
133 #define LLVM_ATTRIBUTE_UNUSED
134 #endif
135
136 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
137 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
138 #else
139 #define LLVM_ATTRIBUTE_WEAK
140 #endif
141
142 #ifdef __GNUC__ // aka 'CONST' but following LLVM Conventions.
143 #define LLVM_READNONE __attribute__((__const__))
144 #else
145 #define LLVM_READNONE
146 #endif
147
148 #ifdef __GNUC__  // aka 'PURE' but following LLVM Conventions.
149 #define LLVM_READONLY __attribute__((__pure__))
150 #else
151 #define LLVM_READONLY
152 #endif
153
154 #if (__GNUC__ >= 4)
155 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
156 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
157 #else
158 #define LLVM_LIKELY(EXPR) (EXPR)
159 #define LLVM_UNLIKELY(EXPR) (EXPR)
160 #endif
161
162 // C++ doesn't support 'extern template' of template specializations.  GCC does,
163 // but requires __extension__ before it.  In the header, use this:
164 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
165 // in the .cpp file, use this:
166 //   TEMPLATE_INSTANTIATION(class foo<bar>);
167 #ifdef __GNUC__
168 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
169 #define TEMPLATE_INSTANTIATION(X) template X
170 #else
171 #define EXTERN_TEMPLATE_INSTANTIATION(X)
172 #define TEMPLATE_INSTANTIATION(X)
173 #endif
174
175 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
176 /// mark a method "not for inlining".
177 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
178 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
179 #elif defined(_MSC_VER)
180 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
181 #else
182 #define LLVM_ATTRIBUTE_NOINLINE
183 #endif
184
185 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
186 /// so, mark a method "always inline" because it is performance sensitive. GCC
187 /// 3.4 supported this but is buggy in various cases and produces unimplemented
188 /// errors, just use it in GCC 4.0 and later.
189 #if __GNUC__ > 3
190 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
191 #elif defined(_MSC_VER)
192 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
193 #else
194 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
195 #endif
196
197 #ifdef __GNUC__
198 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
199 #elif defined(_MSC_VER)
200 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
201 #else
202 #define LLVM_ATTRIBUTE_NORETURN
203 #endif
204
205 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
206 /// pedantic diagnostics.
207 #ifdef __GNUC__
208 #define LLVM_EXTENSION __extension__
209 #else
210 #define LLVM_EXTENSION
211 #endif
212
213 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
214 #if __has_feature(attribute_deprecated_with_message)
215 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
216   decl __attribute__((deprecated(message)))
217 #elif defined(__GNUC__)
218 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
219   decl __attribute__((deprecated))
220 #elif defined(_MSC_VER)
221 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
222   __declspec(deprecated(message)) decl
223 #else
224 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
225   decl
226 #endif
227
228 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
229 /// to an expression which states that it is undefined behavior for the
230 /// compiler to reach this point.  Otherwise is not defined.
231 #if defined(__clang__) || (__GNUC__ > 4) \
232  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
233 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
234 #elif defined(_MSC_VER)
235 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
236 #endif
237
238 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
239 /// which causes the program to exit abnormally.
240 #if defined(__clang__) || (__GNUC__ > 4) \
241  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
242 # define LLVM_BUILTIN_TRAP __builtin_trap()
243 #else
244 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
245 #endif
246
247 /// \macro LLVM_ASSUME_ALIGNED
248 /// \brief Returns a pointer with an assumed alignment.
249 #if !defined(__clang__) && ((__GNUC__ > 4) \
250  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
251 // FIXME: Enable on clang when it supports it.
252 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
253 #elif defined(LLVM_BUILTIN_UNREACHABLE)
254 # define LLVM_ASSUME_ALIGNED(p, a) \
255            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
256 #else
257 # define LLVM_ASSUME_ALIGNED(p, a) (p)
258 #endif
259
260 /// \macro LLVM_FUNCTION_NAME
261 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
262 /// expands to a compiler-dependent replacement.
263 #if defined(_MSC_VER)
264 # define LLVM_FUNCTION_NAME __FUNCTION__
265 #else
266 # define LLVM_FUNCTION_NAME __func__
267 #endif
268
269 #endif