[C++11] Turn off compiler-based detection of R-value references, relying
[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 #include "llvm/Config/llvm-config.h"
19
20 #ifndef __has_feature
21 # define __has_feature(x) 0
22 #endif
23
24 #ifndef __has_extension
25 # define __has_extension(x) 0
26 #endif
27
28 #ifndef __has_attribute
29 # define __has_attribute(x) 0
30 #endif
31
32 #ifndef __has_builtin
33 # define __has_builtin(x) 0
34 #endif
35
36 /// \macro __GNUC_PREREQ
37 /// \brief Defines __GNUC_PREREQ if glibc's features.h isn't available.
38 #ifndef __GNUC_PREREQ
39 # if defined(__GNUC__) && defined(__GNUC_MINOR__)
40 #  define __GNUC_PREREQ(maj, min) \
41     ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
42 # else
43 #  define __GNUC_PREREQ(maj, min) 0
44 # endif
45 #endif
46
47 /// \macro LLVM_MSC_PREREQ
48 /// \brief Is the compiler MSVC of at least the specified version?
49 /// The common \param version values to check for are:
50 ///  * 1600: Microsoft Visual Studio 2010 / 10.0
51 ///  * 1700: Microsoft Visual Studio 2012 / 11.0
52 ///  * 1800: Microsoft Visual Studio 2013 / 12.0
53 #ifdef _MSC_VER
54 #define LLVM_MSC_PREREQ(version) (_MSC_VER >= (version))
55 #else
56 #define LLVM_MSC_PREREQ(version) 0
57 #endif
58
59 /// \brief We require the host compiler to support r-value references.
60 #define LLVM_HAS_RVALUE_REFERENCES 1
61
62 /// \brief Does the compiler support r-value reference *this?
63 ///
64 /// Sadly, this is separate from just r-value reference support because GCC
65 /// implemented everything but this thus far. No release of GCC yet has support
66 /// for this feature so it is enabled with Clang only.
67 /// FIXME: This should change to a version check when GCC grows support for it.
68 #if __has_feature(cxx_rvalue_references)
69 #define LLVM_HAS_RVALUE_REFERENCE_THIS 1
70 #else
71 #define LLVM_HAS_RVALUE_REFERENCE_THIS 0
72 #endif
73
74 /// \macro LLVM_HAS_CXX11_TYPETRAITS
75 /// \brief Does the compiler have the C++11 type traits.
76 ///
77 /// #include <type_traits>
78 ///
79 /// * enable_if
80 /// * {true,false}_type
81 /// * is_constructible
82 /// * etc...
83 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1700)
84 #define LLVM_HAS_CXX11_TYPETRAITS 1
85 #else
86 #define LLVM_HAS_CXX11_TYPETRAITS 0
87 #endif
88
89 /// \macro LLVM_HAS_CXX11_STDLIB
90 /// \brief Does the compiler have the C++11 standard library.
91 ///
92 /// Implies LLVM_HAS_RVALUE_REFERENCES, LLVM_HAS_CXX11_TYPETRAITS
93 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1700)
94 #define LLVM_HAS_CXX11_STDLIB 1
95 #else
96 #define LLVM_HAS_CXX11_STDLIB 0
97 #endif
98
99 /// \macro LLVM_HAS_VARIADIC_TEMPLATES
100 /// \brief Does this compiler support variadic templates.
101 ///
102 /// Implies LLVM_HAS_RVALUE_REFERENCES and the existence of std::forward.
103 #if __has_feature(cxx_variadic_templates) || LLVM_MSC_PREREQ(1800)
104 # define LLVM_HAS_VARIADIC_TEMPLATES 1
105 #else
106 # define LLVM_HAS_VARIADIC_TEMPLATES 0
107 #endif
108
109 /// llvm_move - Expands to ::std::move if the compiler supports
110 /// r-value references; otherwise, expands to the argument.
111 #if LLVM_HAS_RVALUE_REFERENCES
112 #define llvm_move(value) (::std::move(value))
113 #else
114 #define llvm_move(value) (value)
115 #endif
116
117 /// Expands to '&' if r-value references are supported.
118 ///
119 /// This can be used to provide l-value/r-value overrides of member functions.
120 /// The r-value override should be guarded by LLVM_HAS_RVALUE_REFERENCE_THIS
121 #if LLVM_HAS_RVALUE_REFERENCE_THIS
122 #define LLVM_LVALUE_FUNCTION &
123 #else
124 #define LLVM_LVALUE_FUNCTION
125 #endif
126
127 /// LLVM_DELETED_FUNCTION - Expands to = delete if the compiler supports it.
128 /// Use to mark functions as uncallable. Member functions with this should
129 /// be declared private so that some behavior is kept in C++03 mode.
130 ///
131 /// class DontCopy {
132 /// private:
133 ///   DontCopy(const DontCopy&) LLVM_DELETED_FUNCTION;
134 ///   DontCopy &operator =(const DontCopy&) LLVM_DELETED_FUNCTION;
135 /// public:
136 ///   ...
137 /// };
138 #if __has_feature(cxx_deleted_functions) || \
139     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
140 #define LLVM_DELETED_FUNCTION = delete
141 #else
142 #define LLVM_DELETED_FUNCTION
143 #endif
144
145 /// LLVM_FINAL - Expands to 'final' if the compiler supports it.
146 /// Use to mark classes or virtual methods as final.
147 #if __has_feature(cxx_override_control) || \
148     (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
149     LLVM_MSC_PREREQ(1700)
150 #define LLVM_FINAL final
151 #else
152 #define LLVM_FINAL
153 #endif
154
155 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
156 /// Use to mark virtual methods as overriding a base class method.
157 #if __has_feature(cxx_override_control) || \
158     (defined(__GXX_EXPERIMENTAL_CXX0X__) && __GNUC_PREREQ(4, 7)) || \
159     LLVM_MSC_PREREQ(1700)
160 #define LLVM_OVERRIDE override
161 #else
162 #define LLVM_OVERRIDE
163 #endif
164
165 #if __has_feature(cxx_constexpr) || defined(__GXX_EXPERIMENTAL_CXX0X__)
166 # define LLVM_CONSTEXPR constexpr
167 #else
168 # define LLVM_CONSTEXPR
169 #endif
170
171 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
172 /// into a shared library, then the class should be private to the library and
173 /// not accessible from outside it.  Can also be used to mark variables and
174 /// functions, making them private to any shared library they are linked into.
175 /// On PE/COFF targets, library visibility is the default, so this isn't needed.
176 #if (__has_attribute(visibility) || __GNUC_PREREQ(4, 0)) &&                    \
177     !defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32)
178 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
179 #else
180 #define LLVM_LIBRARY_VISIBILITY
181 #endif
182
183 #if __has_attribute(used) || __GNUC_PREREQ(3, 1)
184 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
185 #else
186 #define LLVM_ATTRIBUTE_USED
187 #endif
188
189 #if __has_attribute(warn_unused_result) || __GNUC_PREREQ(3, 4)
190 #define LLVM_ATTRIBUTE_UNUSED_RESULT __attribute__((__warn_unused_result__))
191 #else
192 #define LLVM_ATTRIBUTE_UNUSED_RESULT
193 #endif
194
195 // Some compilers warn about unused functions. When a function is sometimes
196 // used or not depending on build settings (e.g. a function only called from
197 // within "assert"), this attribute can be used to suppress such warnings.
198 //
199 // However, it shouldn't be used for unused *variables*, as those have a much
200 // more portable solution:
201 //   (void)unused_var_name;
202 // Prefer cast-to-void wherever it is sufficient.
203 #if __has_attribute(unused) || __GNUC_PREREQ(3, 1)
204 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
205 #else
206 #define LLVM_ATTRIBUTE_UNUSED
207 #endif
208
209 // FIXME: Provide this for PE/COFF targets.
210 #if (__has_attribute(weak) || __GNUC_PREREQ(4, 0)) &&                          \
211     (!defined(__MINGW32__) && !defined(__CYGWIN__) && !defined(LLVM_ON_WIN32))
212 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
213 #else
214 #define LLVM_ATTRIBUTE_WEAK
215 #endif
216
217 // Prior to clang 3.2, clang did not accept any spelling of
218 // __has_attribute(const), so assume it is supported.
219 #if defined(__clang__) || defined(__GNUC__)
220 // aka 'CONST' but following LLVM Conventions.
221 #define LLVM_READNONE __attribute__((__const__))
222 #else
223 #define LLVM_READNONE
224 #endif
225
226 #if __has_attribute(pure) || defined(__GNUC__)
227 // aka 'PURE' but following LLVM Conventions.
228 #define LLVM_READONLY __attribute__((__pure__))
229 #else
230 #define LLVM_READONLY
231 #endif
232
233 #if __has_builtin(__builtin_expect) || __GNUC_PREREQ(4, 0)
234 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
235 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
236 #else
237 #define LLVM_LIKELY(EXPR) (EXPR)
238 #define LLVM_UNLIKELY(EXPR) (EXPR)
239 #endif
240
241 // C++ doesn't support 'extern template' of template specializations.  GCC does,
242 // but requires __extension__ before it.  In the header, use this:
243 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
244 // in the .cpp file, use this:
245 //   TEMPLATE_INSTANTIATION(class foo<bar>);
246 #ifdef __GNUC__
247 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
248 #define TEMPLATE_INSTANTIATION(X) template X
249 #else
250 #define EXTERN_TEMPLATE_INSTANTIATION(X)
251 #define TEMPLATE_INSTANTIATION(X)
252 #endif
253
254 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
255 /// mark a method "not for inlining".
256 #if __has_attribute(noinline) || __GNUC_PREREQ(3, 4)
257 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
258 #elif defined(_MSC_VER)
259 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
260 #else
261 #define LLVM_ATTRIBUTE_NOINLINE
262 #endif
263
264 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
265 /// so, mark a method "always inline" because it is performance sensitive. GCC
266 /// 3.4 supported this but is buggy in various cases and produces unimplemented
267 /// errors, just use it in GCC 4.0 and later.
268 #if __has_attribute(always_inline) || __GNUC_PREREQ(4, 0)
269 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
270 #elif defined(_MSC_VER)
271 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
272 #else
273 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
274 #endif
275
276 #ifdef __GNUC__
277 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
278 #elif defined(_MSC_VER)
279 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
280 #else
281 #define LLVM_ATTRIBUTE_NORETURN
282 #endif
283
284 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
285 /// pedantic diagnostics.
286 #ifdef __GNUC__
287 #define LLVM_EXTENSION __extension__
288 #else
289 #define LLVM_EXTENSION
290 #endif
291
292 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
293 #if __has_feature(attribute_deprecated_with_message)
294 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
295   decl __attribute__((deprecated(message)))
296 #elif defined(__GNUC__)
297 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
298   decl __attribute__((deprecated))
299 #elif defined(_MSC_VER)
300 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
301   __declspec(deprecated(message)) decl
302 #else
303 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
304   decl
305 #endif
306
307 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
308 /// to an expression which states that it is undefined behavior for the
309 /// compiler to reach this point.  Otherwise is not defined.
310 #if __has_builtin(__builtin_unreachable) || __GNUC_PREREQ(4, 5)
311 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
312 #elif defined(_MSC_VER)
313 # define LLVM_BUILTIN_UNREACHABLE __assume(false)
314 #endif
315
316 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
317 /// which causes the program to exit abnormally.
318 #if __has_builtin(__builtin_trap) || __GNUC_PREREQ(4, 3)
319 # define LLVM_BUILTIN_TRAP __builtin_trap()
320 #else
321 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
322 #endif
323
324 /// \macro LLVM_ASSUME_ALIGNED
325 /// \brief Returns a pointer with an assumed alignment.
326 #if __has_builtin(__builtin_assume_aligned) && __GNUC_PREREQ(4, 7)
327 # define LLVM_ASSUME_ALIGNED(p, a) __builtin_assume_aligned(p, a)
328 #elif defined(LLVM_BUILTIN_UNREACHABLE)
329 // As of today, clang does not support __builtin_assume_aligned.
330 # define LLVM_ASSUME_ALIGNED(p, a) \
331            (((uintptr_t(p) % (a)) == 0) ? (p) : (LLVM_BUILTIN_UNREACHABLE, (p)))
332 #else
333 # define LLVM_ASSUME_ALIGNED(p, a) (p)
334 #endif
335
336 /// \macro LLVM_FUNCTION_NAME
337 /// \brief Expands to __func__ on compilers which support it.  Otherwise,
338 /// expands to a compiler-dependent replacement.
339 #if defined(_MSC_VER)
340 # define LLVM_FUNCTION_NAME __FUNCTION__
341 #else
342 # define LLVM_FUNCTION_NAME __func__
343 #endif
344
345 /// \macro LLVM_MEMORY_SANITIZER_BUILD
346 /// \brief Whether LLVM itself is built with MemorySanitizer instrumentation.
347 #if __has_feature(memory_sanitizer)
348 # define LLVM_MEMORY_SANITIZER_BUILD 1
349 # include <sanitizer/msan_interface.h>
350 #else
351 # define LLVM_MEMORY_SANITIZER_BUILD 0
352 # define __msan_allocated_memory(p, size)
353 # define __msan_unpoison(p, size)
354 #endif
355
356 /// \macro LLVM_ADDRESS_SANITIZER_BUILD
357 /// \brief Whether LLVM itself is built with AddressSanitizer instrumentation.
358 #if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
359 # define LLVM_ADDRESS_SANITIZER_BUILD 1
360 #else
361 # define LLVM_ADDRESS_SANITIZER_BUILD 0
362 #endif
363
364 /// \macro LLVM_IS_UNALIGNED_ACCESS_FAST
365 /// \brief Is unaligned memory access fast on the host machine.
366 ///
367 /// Don't specialize on alignment for platforms where unaligned memory accesses
368 /// generates the same code as aligned memory accesses for common types.
369 #if defined(_M_AMD64) || defined(_M_IX86) || defined(__amd64) || \
370     defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || \
371     defined(_X86_) || defined(__i386) || defined(__i386__)
372 # define LLVM_IS_UNALIGNED_ACCESS_FAST 1
373 #else
374 # define LLVM_IS_UNALIGNED_ACCESS_FAST 0
375 #endif
376
377 /// \macro LLVM_EXPLICIT
378 /// \brief Expands to explicit on compilers which support explicit conversion
379 /// operators. Otherwise expands to nothing.
380 #if __has_feature(cxx_explicit_conversions) || \
381     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1800)
382 #define LLVM_EXPLICIT explicit
383 #else
384 #define LLVM_EXPLICIT
385 #endif
386
387 /// \macro LLVM_STATIC_ASSERT
388 /// \brief Expands to C/C++'s static_assert on compilers which support it.
389 #if __has_feature(cxx_static_assert) || \
390     defined(__GXX_EXPERIMENTAL_CXX0X__) || LLVM_MSC_PREREQ(1600)
391 # define LLVM_STATIC_ASSERT(expr, msg) static_assert(expr, msg)
392 #elif __has_feature(c_static_assert)
393 # define LLVM_STATIC_ASSERT(expr, msg) _Static_assert(expr, msg)
394 #elif __has_extension(c_static_assert)
395 # define LLVM_STATIC_ASSERT(expr, msg) LLVM_EXTENSION _Static_assert(expr, msg)
396 #else
397 # define LLVM_STATIC_ASSERT(expr, msg)
398 #endif
399
400 /// \macro LLVM_ENUM_INT_TYPE
401 /// \brief Expands to colon followed by the given integral type on compilers
402 /// which support C++11 strong enums.  This can be used to make enums unsigned
403 /// with MSVC.
404 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1600)
405 # define LLVM_ENUM_INT_TYPE(intty) : intty
406 #else
407 # define LLVM_ENUM_INT_TYPE(intty)
408 #endif
409
410 /// \brief Does the compiler support C++11 semantics for strongly typed forward
411 /// declared enums?
412 #if __has_feature(cxx_strong_enums) || LLVM_MSC_PREREQ(1700)
413 #define LLVM_HAS_STRONG_ENUMS 1
414 #else
415 #define LLVM_HAS_STRONG_ENUMS 0
416 #endif
417
418 /// \brief Does the compiler support generalized initializers (using braced
419 /// lists and std::initializer_list).  While clang may claim it supports general
420 /// initializers, if we're using MSVC's headers, we might not have a usable
421 /// std::initializer list type from the STL.  Disable this for now.
422 #if __has_feature(cxx_generalized_initializers) && !defined(_MSC_VER)
423 #define LLVM_HAS_INITIALIZER_LISTS 1
424 #else
425 #define LLVM_HAS_INITIALIZER_LISTS 0
426 #endif
427
428 /// \brief Mark debug helper function definitions like dump() that should not be
429 /// stripped from debug builds.
430 // FIXME: Move this to a private config.h as it's not usable in public headers.
431 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
432 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE LLVM_ATTRIBUTE_USED
433 #else
434 #define LLVM_DUMP_METHOD LLVM_ATTRIBUTE_NOINLINE
435 #endif
436
437 #endif