1/*
2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WTF_Compiler_h
27#define WTF_Compiler_h
28
29/* COMPILER() - the compiler being used to build the project */
30#define COMPILER(WTF_FEATURE) (defined WTF_COMPILER_##WTF_FEATURE  && WTF_COMPILER_##WTF_FEATURE)
31
32/* COMPILER_SUPPORTS() - whether the compiler being used to build the project supports the given feature. */
33#define COMPILER_SUPPORTS(WTF_COMPILER_FEATURE) (defined WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE  && WTF_COMPILER_SUPPORTS_##WTF_COMPILER_FEATURE)
34
35/* COMPILER_QUIRK() - whether the compiler being used to build the project requires a given quirk. */
36#define COMPILER_QUIRK(WTF_COMPILER_QUIRK) (defined WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK  && WTF_COMPILER_QUIRK_##WTF_COMPILER_QUIRK)
37
38/* ==== COMPILER() - the compiler being used to build the project ==== */
39
40/* COMPILER(CLANG) - Clang  */
41#if defined(__clang__)
42#define WTF_COMPILER_CLANG 1
43
44#define CLANG_PRAGMA(PRAGMA) _Pragma(PRAGMA)
45
46/* Specific compiler features */
47#define WTF_COMPILER_SUPPORTS_CXX_VARIADIC_TEMPLATES __has_extension(cxx_variadic_templates)
48
49/* There is a bug in clang that comes with Xcode 4.2 where AtomicStrings can't be implicitly converted to Strings
50   in the presence of move constructors and/or move assignment operators. This bug has been fixed in Xcode 4.3 clang, so we
51   check for both cxx_rvalue_references as well as the unrelated cxx_nonstatic_member_init feature which we know was added in 4.3 */
52#define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES __has_extension(cxx_rvalue_references) && __has_extension(cxx_nonstatic_member_init)
53
54#define WTF_COMPILER_SUPPORTS_CXX_DELETED_FUNCTIONS __has_extension(cxx_deleted_functions)
55#define WTF_COMPILER_SUPPORTS_CXX_NULLPTR __has_feature(cxx_nullptr)
56#define WTF_COMPILER_SUPPORTS_CXX_EXPLICIT_CONVERSIONS __has_feature(cxx_explicit_conversions)
57#define WTF_COMPILER_SUPPORTS_BLOCKS __has_feature(blocks)
58#define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT __has_extension(c_static_assert)
59#define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT __has_extension(cxx_static_assert)
60#define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL __has_extension(cxx_override_control)
61#define WTF_COMPILER_SUPPORTS_HAS_TRIVIAL_DESTRUCTOR __has_extension(has_trivial_destructor)
62#define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS __has_extension(cxx_strong_enums)
63
64#endif
65
66#ifndef CLANG_PRAGMA
67#define CLANG_PRAGMA(PRAGMA)
68#endif
69
70/* COMPILER(MSVC) - Microsoft Visual C++ */
71#if defined(_MSC_VER)
72#define WTF_COMPILER_MSVC 1
73
74/* Specific compiler features */
75#if !COMPILER(CLANG) && _MSC_VER >= 1600
76#define WTF_COMPILER_SUPPORTS_CXX_NULLPTR 1
77#endif
78
79#if COMPILER(CLANG)
80/* Keep strong enums turned off when building with clang-cl: We cannot yet build all of Blink without fallback to cl.exe, and strong enums are exposed at ABI boundaries. */
81#undef WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS
82#else
83#define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL 1
84#endif
85
86#endif
87
88/* COMPILER(GCC) - GNU Compiler Collection */
89#if defined(__GNUC__)
90#define WTF_COMPILER_GCC 1
91#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
92#define GCC_VERSION_AT_LEAST(major, minor, patch) (GCC_VERSION >= (major * 10000 + minor * 100 + patch))
93#else
94/* Define this for !GCC compilers, just so we can write things like GCC_VERSION_AT_LEAST(4, 1, 0). */
95#define GCC_VERSION_AT_LEAST(major, minor, patch) 0
96#endif
97
98/* Specific compiler features */
99#if COMPILER(GCC) && !COMPILER(CLANG)
100#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
101/* C11 support */
102#define WTF_COMPILER_SUPPORTS_C_STATIC_ASSERT 1
103#endif
104#if defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(__cplusplus) && __cplusplus >= 201103L)
105/* C++11 support */
106#if GCC_VERSION_AT_LEAST(4, 3, 0)
107#define WTF_COMPILER_SUPPORTS_CXX_RVALUE_REFERENCES 1
108#define WTF_COMPILER_SUPPORTS_CXX_STATIC_ASSERT 1
109#define WTF_COMPILER_SUPPORTS_CXX_VARIADIC_TEMPLATES 1
110#endif
111#if GCC_VERSION_AT_LEAST(4, 4, 0)
112#define WTF_COMPILER_SUPPORTS_CXX_DELETED_FUNCTIONS 1
113#endif
114#if GCC_VERSION_AT_LEAST(4, 5, 0)
115#define WTF_COMPILER_SUPPORTS_CXX_EXPLICIT_CONVERSIONS 1
116#endif
117#if GCC_VERSION_AT_LEAST(4, 6, 0)
118#define WTF_COMPILER_SUPPORTS_CXX_NULLPTR 1
119/* Strong enums should work from gcc 4.4, but doesn't seem to support some operators */
120#define WTF_COMPILER_SUPPORTS_CXX_STRONG_ENUMS 1
121#endif
122#if GCC_VERSION_AT_LEAST(4, 7, 0)
123#define WTF_COMPILER_SUPPORTS_CXX_OVERRIDE_CONTROL 1
124#endif
125#endif /* defined(__GXX_EXPERIMENTAL_CXX0X__) || (defined(__cplusplus) && __cplusplus >= 201103L) */
126#endif /* COMPILER(GCC) */
127
128/* ==== Compiler features ==== */
129
130
131/* ALWAYS_INLINE */
132
133#ifndef ALWAYS_INLINE
134#if COMPILER(GCC) && defined(NDEBUG) && !COMPILER(MINGW)
135#define ALWAYS_INLINE inline __attribute__((__always_inline__))
136#elif COMPILER(MSVC) && defined(NDEBUG)
137#define ALWAYS_INLINE __forceinline
138#else
139#define ALWAYS_INLINE inline
140#endif
141#endif
142
143
144/* NEVER_INLINE */
145
146#ifndef NEVER_INLINE
147#if COMPILER(GCC)
148#define NEVER_INLINE __attribute__((__noinline__))
149#elif COMPILER(MSVC)
150#define NEVER_INLINE __declspec(noinline)
151#else
152#define NEVER_INLINE
153#endif
154#endif
155
156
157/* UNLIKELY */
158
159#ifndef UNLIKELY
160#if COMPILER(GCC)
161#define UNLIKELY(x) __builtin_expect((x), 0)
162#else
163#define UNLIKELY(x) (x)
164#endif
165#endif
166
167
168/* LIKELY */
169
170#ifndef LIKELY
171#if COMPILER(GCC)
172#define LIKELY(x) __builtin_expect((x), 1)
173#else
174#define LIKELY(x) (x)
175#endif
176#endif
177
178
179/* NO_RETURN */
180
181
182#ifndef NO_RETURN
183#if COMPILER(GCC)
184#define NO_RETURN __attribute((__noreturn__))
185#elif COMPILER(MSVC)
186#define NO_RETURN __declspec(noreturn)
187#else
188#define NO_RETURN
189#endif
190#endif
191
192
193/* WARN_UNUSED_RETURN */
194
195#if COMPILER(GCC)
196#define WARN_UNUSED_RETURN __attribute__ ((warn_unused_result))
197#else
198#define WARN_UNUSED_RETURN
199#endif
200
201/* ALLOW_UNUSED */
202
203#if COMPILER(GCC)
204#define ALLOW_UNUSED __attribute__((unused))
205#else
206#define ALLOW_UNUSED
207#endif
208
209/* OVERRIDE and FINAL */
210
211#if COMPILER_SUPPORTS(CXX_OVERRIDE_CONTROL)
212#define OVERRIDE override
213#define FINAL final
214#else
215#define OVERRIDE
216#define FINAL
217#endif
218
219/* WTF_DELETED_FUNCTION */
220
221#if COMPILER_SUPPORTS(CXX_DELETED_FUNCTIONS)
222#define WTF_DELETED_FUNCTION = delete
223#else
224#define WTF_DELETED_FUNCTION
225#endif
226
227/* REFERENCED_FROM_ASM */
228
229#ifndef REFERENCED_FROM_ASM
230#if COMPILER(GCC)
231#define REFERENCED_FROM_ASM __attribute__((used))
232#else
233#define REFERENCED_FROM_ASM
234#endif
235#endif
236
237/* OBJC_CLASS */
238
239#ifndef OBJC_CLASS
240#ifdef __OBJC__
241#define OBJC_CLASS @class
242#else
243#define OBJC_CLASS class
244#endif
245#endif
246
247/* WTF_PRETTY_FUNCTION */
248
249#if COMPILER(GCC)
250#define WTF_COMPILER_SUPPORTS_PRETTY_FUNCTION 1
251#define WTF_PRETTY_FUNCTION __PRETTY_FUNCTION__
252#elif COMPILER(MSVC)
253#define WTF_COMPILER_SUPPORTS_PRETTY_FUNCTION 1
254#define WTF_PRETTY_FUNCTION __FUNCSIG__
255#else
256#define WTF_PRETTY_FUNCTION __FUNCTION__
257#endif
258
259#endif /* WTF_Compiler_h */
260