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