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