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