Separate out the tests for whether the compiler suports R-value
[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 /// LLVM_HAS_RVALUE_REFERENCES - Does the compiler provide 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_USE_RVALUE_REFERENCES 1
29 #else
30 #define LLVM_USE_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_USE_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 #define LLVM_FINAL final
86 #else
87 #define LLVM_FINAL
88 #endif
89
90 /// LLVM_OVERRIDE - Expands to 'override' if the compiler supports it.
91 /// Use to mark virtual methods as overriding a base class method.
92 #if (__has_feature(cxx_override_control))
93 #define LLVM_OVERRIDE override
94 #else
95 #define LLVM_OVERRIDE
96 #endif
97
98 /// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
99 /// into a shared library, then the class should be private to the library and
100 /// not accessible from outside it.  Can also be used to mark variables and
101 /// functions, making them private to any shared library they are linked into.
102 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
103 #define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
104 #else
105 #define LLVM_LIBRARY_VISIBILITY
106 #endif
107
108 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
109 #define LLVM_ATTRIBUTE_USED __attribute__((__used__))
110 #else
111 #define LLVM_ATTRIBUTE_USED
112 #endif
113
114 // Some compilers warn about unused functions. When a function is sometimes
115 // used or not depending on build settings (e.g. a function only called from
116 // within "assert"), this attribute can be used to suppress such warnings.
117 //
118 // However, it shouldn't be used for unused *variables*, as those have a much
119 // more portable solution:
120 //   (void)unused_var_name;
121 // Prefer cast-to-void wherever it is sufficient.
122 #if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
123 #define LLVM_ATTRIBUTE_UNUSED __attribute__((__unused__))
124 #else
125 #define LLVM_ATTRIBUTE_UNUSED
126 #endif
127
128 #if (__GNUC__ >= 4) && !defined(__MINGW32__) && !defined(__CYGWIN__)
129 #define LLVM_ATTRIBUTE_WEAK __attribute__((__weak__))
130 #else
131 #define LLVM_ATTRIBUTE_WEAK
132 #endif
133
134 #ifdef __GNUC__ // aka 'CONST' but following LLVM Conventions.
135 #define LLVM_READNONE __attribute__((__const__))
136 #else
137 #define LLVM_READNONE
138 #endif
139
140 #ifdef __GNUC__  // aka 'PURE' but following LLVM Conventions.
141 #define LLVM_READONLY __attribute__((__pure__))
142 #else
143 #define LLVM_READONLY
144 #endif
145
146 #if (__GNUC__ >= 4)
147 #define LLVM_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
148 #define LLVM_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
149 #else
150 #define LLVM_LIKELY(EXPR) (EXPR)
151 #define LLVM_UNLIKELY(EXPR) (EXPR)
152 #endif
153
154
155 // C++ doesn't support 'extern template' of template specializations.  GCC does,
156 // but requires __extension__ before it.  In the header, use this:
157 //   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
158 // in the .cpp file, use this:
159 //   TEMPLATE_INSTANTIATION(class foo<bar>);
160 #ifdef __GNUC__
161 #define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
162 #define TEMPLATE_INSTANTIATION(X) template X
163 #else
164 #define EXTERN_TEMPLATE_INSTANTIATION(X)
165 #define TEMPLATE_INSTANTIATION(X)
166 #endif
167
168 /// LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so,
169 /// mark a method "not for inlining".
170 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
171 #define LLVM_ATTRIBUTE_NOINLINE __attribute__((noinline))
172 #elif defined(_MSC_VER)
173 #define LLVM_ATTRIBUTE_NOINLINE __declspec(noinline)
174 #else
175 #define LLVM_ATTRIBUTE_NOINLINE
176 #endif
177
178 /// LLVM_ATTRIBUTE_ALWAYS_INLINE - On compilers where we have a directive to do
179 /// so, mark a method "always inline" because it is performance sensitive. GCC
180 /// 3.4 supported this but is buggy in various cases and produces unimplemented
181 /// errors, just use it in GCC 4.0 and later.
182 #if __GNUC__ > 3
183 #define LLVM_ATTRIBUTE_ALWAYS_INLINE inline __attribute__((always_inline))
184 #elif defined(_MSC_VER)
185 #define LLVM_ATTRIBUTE_ALWAYS_INLINE __forceinline
186 #else
187 #define LLVM_ATTRIBUTE_ALWAYS_INLINE
188 #endif
189
190
191 #ifdef __GNUC__
192 #define LLVM_ATTRIBUTE_NORETURN __attribute__((noreturn))
193 #elif defined(_MSC_VER)
194 #define LLVM_ATTRIBUTE_NORETURN __declspec(noreturn)
195 #else
196 #define LLVM_ATTRIBUTE_NORETURN
197 #endif
198
199 /// LLVM_EXTENSION - Support compilers where we have a keyword to suppress
200 /// pedantic diagnostics.
201 #ifdef __GNUC__
202 #define LLVM_EXTENSION __extension__
203 #else
204 #define LLVM_EXTENSION
205 #endif
206
207 // LLVM_ATTRIBUTE_DEPRECATED(decl, "message")
208 #if __has_feature(attribute_deprecated_with_message)
209 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
210   decl __attribute__((deprecated(message)))
211 #elif defined(__GNUC__)
212 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
213   decl __attribute__((deprecated))
214 #elif defined(_MSC_VER)
215 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
216   __declspec(deprecated(message)) decl
217 #else
218 # define LLVM_ATTRIBUTE_DEPRECATED(decl, message) \
219   decl
220 #endif
221
222 /// LLVM_BUILTIN_UNREACHABLE - On compilers which support it, expands
223 /// to an expression which states that it is undefined behavior for the
224 /// compiler to reach this point.  Otherwise is not defined.
225 #if defined(__clang__) || (__GNUC__ > 4) \
226  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
227 # define LLVM_BUILTIN_UNREACHABLE __builtin_unreachable()
228 #endif
229
230 /// LLVM_BUILTIN_TRAP - On compilers which support it, expands to an expression
231 /// which causes the program to exit abnormally.
232 #if defined(__clang__) || (__GNUC__ > 4) \
233  || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
234 # define LLVM_BUILTIN_TRAP __builtin_trap()
235 #else
236 # define LLVM_BUILTIN_TRAP *(volatile int*)0x11 = 0
237 #endif
238
239 #endif