1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Platform headers for feature detection below.
32 #if defined(__ANDROID__)
33 # include <sys/cdefs.h>
34 #elif defined(__APPLE__)
35 # include <TargetConditionals.h>
36 #elif defined(__linux__)
37 # include <features.h>
41 // This macro allows to test for the version of the GNU C library (or
42 // a compatible C library that masquerades as glibc). It evaluates to
43 // 0 if libc is not GNU libc or compatible.
45 // #if V8_GLIBC_PREREQ(2, 3)
48 #if defined(__GLIBC__) && defined(__GLIBC_MINOR__)
49 # define V8_GLIBC_PREREQ(major, minor) \
50 ((__GLIBC__ * 100 + __GLIBC_MINOR__) >= ((major) * 100 + (minor)))
52 # define V8_GLIBC_PREREQ(major, minor) 0
56 // This macro allows to test for the version of the GNU C++ compiler.
57 // Note that this also applies to compilers that masquerade as GCC,
58 // for example clang and the Intel C++ compiler for Linux.
60 // #if V8_GNUC_PREREQ(4, 3, 1)
63 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
64 # define V8_GNUC_PREREQ(major, minor, patchlevel) \
65 ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= \
66 ((major) * 10000 + (minor) * 100 + (patchlevel)))
67 #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
68 # define V8_GNUC_PREREQ(major, minor, patchlevel) \
69 ((__GNUC__ * 10000 + __GNUC_MINOR__) >= \
70 ((major) * 10000 + (minor) * 100 + (patchlevel)))
72 # define V8_GNUC_PREREQ(major, minor, patchlevel) 0
77 // -----------------------------------------------------------------------------
78 // Operating system detection
80 // V8_OS_ANDROID - Android
81 // V8_OS_BSD - BSDish (Mac OS X, Net/Free/Open/DragonFlyBSD)
82 // V8_OS_CYGWIN - Cygwin
83 // V8_OS_DRAGONFLYBSD - DragonFlyBSD
84 // V8_OS_FREEBSD - FreeBSD
85 // V8_OS_LINUX - Linux
86 // V8_OS_MACOSX - Mac OS X
87 // V8_OS_NACL - Native Client
88 // V8_OS_NETBSD - NetBSD
89 // V8_OS_OPENBSD - OpenBSD
90 // V8_OS_POSIX - POSIX compatible (mostly everything except Windows)
91 // V8_OS_QNX - QNX Neutrino
92 // V8_OS_SOLARIS - Sun Solaris and OpenSolaris
93 // V8_OS_WIN - Microsoft Windows
95 #if defined(__ANDROID__)
96 # define V8_OS_ANDROID 1
97 # define V8_OS_LINUX 1
98 # define V8_OS_POSIX 1
99 #elif defined(__APPLE__)
101 # define V8_OS_MACOSX 1
102 # define V8_OS_POSIX 1
103 #elif defined(__native_client__)
104 # define V8_OS_NACL 1
105 # define V8_OS_POSIX 1
106 #elif defined(__CYGWIN__)
107 # define V8_OS_CYGWIN 1
108 # define V8_OS_POSIX 1
109 #elif defined(__linux__)
110 # define V8_OS_LINUX 1
111 # define V8_OS_POSIX 1
113 # define V8_OS_POSIX 1
114 # define V8_OS_SOLARIS 1
115 #elif defined(__FreeBSD__)
117 # define V8_OS_FREEBSD 1
118 # define V8_OS_POSIX 1
119 #elif defined(__DragonFly__)
121 # define V8_OS_DRAGONFLYBSD 1
122 # define V8_OS_POSIX 1
123 #elif defined(__NetBSD__)
125 # define V8_OS_NETBSD 1
126 # define V8_OS_POSIX 1
127 #elif defined(__OpenBSD__)
129 # define V8_OS_OPENBSD 1
130 # define V8_OS_POSIX 1
131 #elif defined(__QNXNTO__)
132 # define V8_OS_POSIX 1
134 #elif defined(_WIN32)
139 // -----------------------------------------------------------------------------
140 // C library detection
142 // V8_LIBC_BIONIC - Bionic libc
143 // V8_LIBC_BSD - BSD libc derivate
144 // V8_LIBC_GLIBC - GNU C library
145 // V8_LIBC_UCLIBC - uClibc
147 // Note that testing for libc must be done using #if not #ifdef. For example,
148 // to test for the GNU C library, use:
153 #if defined(__BIONIC__)
154 # define V8_LIBC_BIONIC 1
155 # define V8_LIBC_BSD 1
156 #elif defined(__UCLIBC__)
157 # define V8_LIBC_UCLIBC 1
158 #elif defined(__GLIBC__) || defined(__GNU_LIBRARY__)
159 # define V8_LIBC_GLIBC 1
161 # define V8_LIBC_BSD V8_OS_BSD
165 // -----------------------------------------------------------------------------
166 // Compiler detection
168 // V8_CC_CLANG - Clang
169 // V8_CC_GNU - GNU C++
170 // V8_CC_INTEL - Intel C++
171 // V8_CC_MINGW - Minimalist GNU for Windows
172 // V8_CC_MINGW32 - Minimalist GNU for Windows (mingw32)
173 // V8_CC_MINGW64 - Minimalist GNU for Windows (mingw-w64)
174 // V8_CC_MSVC - Microsoft Visual C/C++
176 // C++11 feature detection
178 // V8_HAS_CXX11_ALIGNAS - alignas specifier supported
179 // V8_HAS_CXX11_ALIGNOF - alignof(type) operator supported
180 // V8_HAS_CXX11_STATIC_ASSERT - static_assert() supported
181 // V8_HAS_CXX11_DELETE - deleted functions supported
182 // V8_HAS_CXX11_FINAL - final marker supported
183 // V8_HAS_CXX11_OVERRIDE - override marker supported
185 // Compiler-specific feature detection
187 // V8_HAS___ALIGNOF - __alignof(type) operator supported
188 // V8_HAS___ALIGNOF__ - __alignof__(type) operator supported
189 // V8_HAS_ATTRIBUTE_ALIGNED - __attribute__((aligned(n))) supported
190 // V8_HAS_ATTRIBUTE_ALWAYS_INLINE - __attribute__((always_inline))
192 // V8_HAS_ATTRIBUTE_DEPRECATED - __attribute__((deprecated)) supported
193 // V8_HAS_ATTRIBUTE_NOINLINE - __attribute__((noinline)) supported
194 // V8_HAS_ATTRIBUTE_UNUSED - __attribute__((unused)) supported
195 // V8_HAS_ATTRIBUTE_VISIBILITY - __attribute__((visibility)) supported
196 // V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT - __attribute__((warn_unused_result))
198 // V8_HAS_BUILTIN_EXPECT - __builtin_expect() supported
199 // V8_HAS_DECLSPEC_ALIGN - __declspec(align(n)) supported
200 // V8_HAS_DECLSPEC_DEPRECATED - __declspec(deprecated) supported
201 // V8_HAS_DECLSPEC_NOINLINE - __declspec(noinline) supported
202 // V8_HAS___FINAL - __final supported in non-C++11 mode
203 // V8_HAS___FORCEINLINE - __forceinline supported
204 // V8_HAS_SEALED - MSVC style sealed marker supported
206 // Note that testing for compilers and/or features must be done using #if
207 // not #ifdef. For example, to test for Intel C++ Compiler, use:
212 #if defined(__clang__)
214 # define V8_CC_CLANG 1
216 // Clang defines __alignof__ as alias for __alignof
217 # define V8_HAS___ALIGNOF 1
218 # define V8_HAS___ALIGNOF__ V8_HAS___ALIGNOF
220 # define V8_HAS_ATTRIBUTE_ALIGNED (__has_attribute(aligned))
221 # define V8_HAS_ATTRIBUTE_ALWAYS_INLINE (__has_attribute(always_inline))
222 # define V8_HAS_ATTRIBUTE_DEPRECATED (__has_attribute(deprecated))
223 # define V8_HAS_ATTRIBUTE_NOINLINE (__has_attribute(noinline))
224 # define V8_HAS_ATTRIBUTE_UNUSED (__has_attribute(unused))
225 # define V8_HAS_ATTRIBUTE_VISIBILITY (__has_attribute(visibility))
226 # define V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT \
227 (__has_attribute(warn_unused_result))
229 # define V8_HAS_BUILTIN_EXPECT (__has_builtin(__builtin_expect))
231 # define V8_HAS_CXX11_ALIGNAS (__has_feature(cxx_alignas))
232 # define V8_HAS_CXX11_STATIC_ASSERT (__has_feature(cxx_static_assert))
233 # define V8_HAS_CXX11_DELETE (__has_feature(cxx_deleted_functions))
234 # define V8_HAS_CXX11_FINAL (__has_feature(cxx_override_control))
235 # define V8_HAS_CXX11_OVERRIDE (__has_feature(cxx_override_control))
237 #elif defined(__GNUC__)
240 // Intel C++ also masquerades as GCC 3.2.0
241 # define V8_CC_INTEL (defined(__INTEL_COMPILER))
242 # define V8_CC_MINGW32 (defined(__MINGW32__))
243 # define V8_CC_MINGW64 (defined(__MINGW64__))
244 # define V8_CC_MINGW (V8_CC_MINGW32 || V8_CC_MINGW64)
246 # define V8_HAS___ALIGNOF__ (V8_GNUC_PREREQ(4, 3, 0))
248 # define V8_HAS_ATTRIBUTE_ALIGNED (V8_GNUC_PREREQ(2, 95, 0))
249 // always_inline is available in gcc 4.0 but not very reliable until 4.4.
250 // Works around "sorry, unimplemented: inlining failed" build errors with
252 # define V8_HAS_ATTRIBUTE_ALWAYS_INLINE (V8_GNUC_PREREQ(4, 4, 0))
253 # define V8_HAS_ATTRIBUTE_DEPRECATED (V8_GNUC_PREREQ(3, 4, 0))
254 # define V8_HAS_ATTRIBUTE_DEPRECATED_MESSAGE (V8_GNUC_PREREQ(4, 5, 0))
255 # define V8_HAS_ATTRIBUTE_NOINLINE (V8_GNUC_PREREQ(3, 4, 0))
256 # define V8_HAS_ATTRIBUTE_UNUSED (V8_GNUC_PREREQ(2, 95, 0))
257 # define V8_HAS_ATTRIBUTE_VISIBILITY (V8_GNUC_PREREQ(4, 3, 0))
258 # define V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT \
259 (!V8_CC_INTEL && V8_GNUC_PREREQ(4, 1, 0))
261 # define V8_HAS_BUILTIN_EXPECT (V8_GNUC_PREREQ(2, 96, 0))
263 // g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality
264 // without warnings (functionality used by the macros below). These modes
265 // are detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or,
266 // more standardly, by checking whether __cplusplus has a C++11 or greater
267 // value. Current versions of g++ do not correctly set __cplusplus, so we check
268 // both for forward compatibility.
269 # if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
270 # define V8_HAS_CXX11_ALIGNAS (V8_GNUC_PREREQ(4, 8, 0))
271 # define V8_HAS_CXX11_ALIGNOF (V8_GNUC_PREREQ(4, 8, 0))
272 # define V8_HAS_CXX11_STATIC_ASSERT (V8_GNUC_PREREQ(4, 3, 0))
273 # define V8_HAS_CXX11_DELETE (V8_GNUC_PREREQ(4, 4, 0))
274 # define V8_HAS_CXX11_OVERRIDE (V8_GNUC_PREREQ(4, 7, 0))
275 # define V8_HAS_CXX11_FINAL (V8_GNUC_PREREQ(4, 7, 0))
277 // '__final' is a non-C++11 GCC synonym for 'final', per GCC r176655.
278 # define V8_HAS___FINAL (V8_GNUC_PREREQ(4, 7, 0))
281 #elif defined(_MSC_VER)
283 # define V8_CC_MSVC 1
285 # define V8_HAS___ALIGNOF 1
287 // Override control was added with Visual Studio 2005, but
288 // Visual Studio 2010 and earlier spell "final" as "sealed".
289 # define V8_HAS_CXX11_FINAL (_MSC_VER >= 1700)
290 # define V8_HAS_CXX11_OVERRIDE (_MSC_VER >= 1400)
291 # define V8_HAS_SEALED (_MSC_VER >= 1400)
293 # define V8_HAS_DECLSPEC_ALIGN 1
294 # define V8_HAS_DECLSPEC_DEPRECATED (_MSC_VER >= 1300)
295 # define V8_HAS_DECLSPEC_NOINLINE 1
297 # define V8_HAS___FORCEINLINE 1
302 // -----------------------------------------------------------------------------
305 // A macro used to make better inlining. Don't bother for debug builds.
307 // V8_INLINE int GetZero() { return 0; }
308 #if !defined(DEBUG) && V8_HAS_ATTRIBUTE_ALWAYS_INLINE
309 # define V8_INLINE inline __attribute__((always_inline))
310 #elif !defined(DEBUG) && V8_HAS___FORCEINLINE
311 # define V8_INLINE __forceinline
313 # define V8_INLINE inline
317 // A macro used to tell the compiler to never inline a particular function.
318 // Don't bother for debug builds.
320 // V8_NOINLINE int GetMinusOne() { return -1; }
321 #if !defined(DEBUG) && V8_HAS_ATTRIBUTE_NOINLINE
322 # define V8_NOINLINE __attribute__((noinline))
323 #elif !defined(DEBUG) && V8_HAS_DECLSPEC_NOINLINE
324 # define V8_NOINLINE __declspec(noinline)
326 # define V8_NOINLINE /* NOT SUPPORTED */
330 // A macro to mark classes or functions as deprecated.
331 #if defined(V8_DEPRECATION_WARNINGS) && V8_HAS_ATTRIBUTE_DEPRECATED_MESSAGE
332 # define V8_DEPRECATED(message, declarator) \
333 declarator __attribute__((deprecated(message)))
334 #elif defined(V8_DEPRECATION_WARNINGS) && V8_HAS_ATTRIBUTE_DEPRECATED
335 # define V8_DEPRECATED(message, declarator) \
336 declarator __attribute__((deprecated))
337 #elif defined(V8_DEPRECATION_WARNINGS) && V8_HAS_DECLSPEC_DEPRECATED
338 # define V8_DEPRECATED(message, declarator) __declspec(deprecated) declarator
340 # define V8_DEPRECATED(message, declarator) declarator
344 // A macro to mark variables or types as unused, avoiding compiler warnings.
345 #if V8_HAS_ATTRIBUTE_UNUSED
346 # define V8_UNUSED __attribute__((unused))
352 // Annotate a function indicating the caller must examine the return value.
354 // int foo() V8_WARN_UNUSED_RESULT;
355 #if V8_HAS_ATTRIBUTE_WARN_UNUSED_RESULT
356 # define V8_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
358 # define V8_WARN_UNUSED_RESULT /* NOT SUPPORTED */
362 // A macro to provide the compiler with branch prediction information.
363 #if V8_HAS_BUILTIN_EXPECT
364 # define V8_UNLIKELY(condition) (__builtin_expect(!!(condition), 0))
365 # define V8_LIKELY(condition) (__builtin_expect(!!(condition), 1))
367 # define V8_UNLIKELY(condition) (condition)
368 # define V8_LIKELY(condition) (condition)
372 // A macro to specify that a method is deleted from the corresponding class.
373 // Any attempt to use the method will always produce an error at compile time
374 // when this macro can be implemented (i.e. if the compiler supports C++11).
375 // If the current compiler does not support C++11, use of the annotated method
376 // will still cause an error, but the error will most likely occur at link time
377 // rather than at compile time. As a backstop, method declarations using this
378 // macro should be private.
382 // A(const A& other) V8_DELETE;
383 // A& operator=(const A& other) V8_DELETE;
385 #if V8_HAS_CXX11_DELETE
386 # define V8_DELETE = delete
388 # define V8_DELETE /* NOT SUPPORTED */
392 // Annotate a virtual method indicating it must be overriding a virtual
393 // method in the parent class.
395 // virtual void bar() V8_OVERRIDE;
396 #if V8_HAS_CXX11_OVERRIDE
397 # define V8_OVERRIDE override
399 # define V8_OVERRIDE /* NOT SUPPORTED */
403 // Annotate a virtual method indicating that subclasses must not override it,
404 // or annotate a class to indicate that it cannot be subclassed.
406 // class B V8_FINAL : public A {};
407 // virtual void bar() V8_FINAL;
408 #if V8_HAS_CXX11_FINAL
409 # define V8_FINAL final
411 # define V8_FINAL __final
413 # define V8_FINAL sealed
415 # define V8_FINAL /* NOT SUPPORTED */
419 // This macro allows to specify memory alignment for structs, classes, etc.
421 // class V8_ALIGNED(16) MyClass { ... };
422 // V8_ALIGNED(32) int array[42];
423 #if V8_HAS_CXX11_ALIGNAS
424 # define V8_ALIGNED(n) alignas(n)
425 #elif V8_HAS_ATTRIBUTE_ALIGNED
426 # define V8_ALIGNED(n) __attribute__((aligned(n)))
427 #elif V8_HAS_DECLSPEC_ALIGN
428 # define V8_ALIGNED(n) __declspec(align(n))
430 # define V8_ALIGNED(n) /* NOT SUPPORTED */
434 // This macro is similar to V8_ALIGNED(), but takes a type instead of size
435 // in bytes. If the compiler does not supports using the alignment of the
436 // |type|, it will align according to the |alignment| instead. For example,
437 // Visual Studio C++ cannot combine __declspec(align) and __alignof. The
438 // |alignment| must be a literal that is used as a kind of worst-case fallback
441 // struct V8_ALIGNAS(AnotherClass, 16) NewClass { ... };
442 // V8_ALIGNAS(double, 8) int array[100];
443 #if V8_HAS_CXX11_ALIGNAS
444 # define V8_ALIGNAS(type, alignment) alignas(type)
445 #elif V8_HAS___ALIGNOF__ && V8_HAS_ATTRIBUTE_ALIGNED
446 # define V8_ALIGNAS(type, alignment) __attribute__((aligned(__alignof__(type))))
448 # define V8_ALIGNAS(type, alignment) V8_ALIGNED(alignment)
452 // This macro returns alignment in bytes (an integer power of two) required for
453 // any instance of the given type, which is either complete type, an array type,
454 // or a reference type.
456 // size_t alignment = V8_ALIGNOF(double);
457 #if V8_HAS_CXX11_ALIGNOF
458 # define V8_ALIGNOF(type) alignof(type)
459 #elif V8_HAS___ALIGNOF
460 # define V8_ALIGNOF(type) __alignof(type)
461 #elif V8_HAS___ALIGNOF__
462 # define V8_ALIGNOF(type) __alignof__(type)
464 // Note that alignment of a type within a struct can be less than the
465 // alignment of the type stand-alone (because of ancient ABIs), so this
466 // should only be used as a last resort.
467 namespace v8 { template <typename T> class AlignOfHelper { char c; T t; }; }
468 # define V8_ALIGNOF(type) (sizeof(::v8::AlignOfHelper<type>) - sizeof(type))
471 #endif // V8CONFIG_H_