1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
* License, v. 2.0. If a copy of the MPL was not distributed with this
4
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
/* Implementations of various class and method modifier attributes. */
8
#ifndef mozilla_Attributes_h_
9
#define mozilla_Attributes_h_
12
* This header does not include any other headers so that it can be included by
13
* code that is (only currently) mfbt-incompatible.
16
/* Suppress any warnings about c++0x extensions. */
17
#if defined(__clang__)
18
# pragma clang diagnostic ignored "-Wc++0x-extensions"
22
* MOZ_INLINE is a macro which expands to tell the compiler that the method
23
* decorated with it should be inlined. This macro is usable from C and C++
24
* code, even though C89 does not support the |inline| keyword. The compiler
25
* may ignore this directive if it chooses.
27
#if defined(__cplusplus)
28
# define MOZ_INLINE inline
29
#elif defined(_MSC_VER)
30
# define MOZ_INLINE __inline
31
#elif defined(__GNUC__)
32
# define MOZ_INLINE __inline__
34
# define MOZ_INLINE inline
38
* MOZ_ALWAYS_INLINE is a macro which expands to tell the compiler that the
39
* method decorated with it must be inlined, even if the compiler thinks
40
* otherwise. This is only a (much) stronger version of the MOZ_INLINE hint:
41
* compilers are not guaranteed to respect it (although they're much more likely
45
# define MOZ_ALWAYS_INLINE MOZ_INLINE
46
#elif defined(_MSC_VER)
47
# define MOZ_ALWAYS_INLINE __forceinline
48
#elif defined(__GNUC__)
49
# define MOZ_ALWAYS_INLINE __attribute__((always_inline)) MOZ_INLINE
51
# define MOZ_ALWAYS_INLINE MOZ_INLINE
55
* g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality
56
* without warnings (functionality used by the macros below). These modes are
57
* detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or, more
58
* standardly, by checking whether __cplusplus has a C++11 or greater value.
59
* Current versions of g++ do not correctly set __cplusplus, so we check both
60
* for forward compatibility.
62
#if defined(__clang__)
64
* Per Clang documentation, "Note that marketing version numbers should not
65
* be used to check for language features, as different vendors use different
66
* numbering schemes. Instead, use the feature checking macros."
68
# ifndef __has_extension
69
# define __has_extension __has_feature /* compatibility, for older versions of clang */
71
# if __has_extension(cxx_deleted_functions)
72
# define MOZ_HAVE_CXX11_DELETE
74
# if __has_extension(cxx_override_control)
75
# define MOZ_HAVE_CXX11_OVERRIDE
76
# define MOZ_HAVE_CXX11_FINAL final
78
# if __has_extension(cxx_strong_enums)
79
# define MOZ_HAVE_CXX11_ENUM_TYPE
80
# define MOZ_HAVE_CXX11_STRONG_ENUMS
82
# if __has_attribute(noinline)
83
# define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
85
# if __has_attribute(noreturn)
86
# define MOZ_HAVE_NORETURN __attribute__((noreturn))
88
#elif defined(__GNUC__)
89
# if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
91
# define MOZ_HAVE_CXX11_DELETE
92
# define MOZ_HAVE_CXX11_OVERRIDE
93
# define MOZ_HAVE_CXX11_FINAL final
95
# if __GNUC_MINOR__ >= 7
96
# define MOZ_HAVE_CXX11_OVERRIDE
97
# define MOZ_HAVE_CXX11_FINAL final
99
# if __GNUC_MINOR__ >= 4
100
# define MOZ_HAVE_CXX11_DELETE
101
# define MOZ_HAVE_CXX11_ENUM_TYPE
102
# define MOZ_HAVE_CXX11_STRONG_ENUMS
106
/* __final is a non-C++11 GCC synonym for 'final', per GCC r176655. */
108
# define MOZ_HAVE_CXX11_FINAL __final
110
# if __GNUC_MINOR__ >= 7
111
# define MOZ_HAVE_CXX11_FINAL __final
115
# define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
116
# define MOZ_HAVE_NORETURN __attribute__((noreturn))
117
#elif defined(_MSC_VER)
118
# if _MSC_VER >= 1400
119
# define MOZ_HAVE_CXX11_OVERRIDE
120
/* MSVC currently spells "final" as "sealed". */
121
# define MOZ_HAVE_CXX11_FINAL sealed
122
# define MOZ_HAVE_CXX11_ENUM_TYPE
124
# if _MSC_VER >= 1700
125
# define MOZ_HAVE_CXX11_STRONG_ENUMS
127
# define MOZ_HAVE_NEVER_INLINE __declspec(noinline)
128
# define MOZ_HAVE_NORETURN __declspec(noreturn)
132
* MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the
133
* method decorated with it must never be inlined, even if the compiler would
134
* otherwise choose to inline the method. Compilers aren't absolutely
135
* guaranteed to support this, but most do.
137
#if defined(MOZ_HAVE_NEVER_INLINE)
138
# define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE
140
# define MOZ_NEVER_INLINE /* no support */
144
* MOZ_NORETURN, specified at the start of a function declaration, indicates
145
* that the given function does not return. (The function definition does not
146
* need to be annotated.)
148
* MOZ_NORETURN void abort(const char* msg);
150
* This modifier permits the compiler to optimize code assuming a call to such a
151
* function will never return. It also enables the compiler to avoid spurious
152
* warnings about not initializing variables, or about any other seemingly-dodgy
153
* operations performed after the function returns.
155
* This modifier does not affect the corresponding function's linking behavior.
157
#if defined(MOZ_HAVE_NORETURN)
158
# define MOZ_NORETURN MOZ_HAVE_NORETURN
160
# define MOZ_NORETURN /* no support */
164
* MOZ_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
165
* instrumentation shipped with Clang) to not instrument the annotated function.
166
* Furthermore, it will prevent the compiler from inlining the function because
167
* inlining currently breaks the blacklisting mechanism of AddressSanitizer.
169
#if defined(MOZ_ASAN)
170
# define MOZ_ASAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_address_safety_analysis))
172
# define MOZ_ASAN_BLACKLIST
179
* MOZ_DELETE, specified immediately prior to the ';' terminating an undefined-
180
* method declaration, attempts to delete that method from the corresponding
181
* class. An attempt to use the method will always produce an error *at compile
182
* time* (instead of sometimes as late as link time) when this macro can be
183
* implemented. For example, you can use MOZ_DELETE to produce classes with no
184
* implicit copy constructor or assignment operator:
189
* NonCopyable(const NonCopyable& other) MOZ_DELETE;
190
* void operator=(const NonCopyable& other) MOZ_DELETE;
193
* If MOZ_DELETE can't be implemented for the current compiler, use of the
194
* annotated method will still cause an error, but the error might occur at link
195
* time in some cases rather than at compile time.
197
* MOZ_DELETE relies on C++11 functionality not universally implemented. As a
198
* backstop, method declarations using MOZ_DELETE should be private.
200
#if defined(MOZ_HAVE_CXX11_DELETE)
201
# define MOZ_DELETE = delete
203
# define MOZ_DELETE /* no support */
207
* MOZ_OVERRIDE explicitly indicates that a virtual member function in a class
208
* overrides a member function of a base class, rather than potentially being a
209
* new member function. MOZ_OVERRIDE should be placed immediately before the
210
* ';' terminating the member function's declaration, or before '= 0;' if the
211
* member function is pure. If the member function is defined in the class
212
* definition, it should appear before the opening brace of the function body.
217
* virtual void f() = 0;
219
* class Derived1 : public Base
222
* virtual void f() MOZ_OVERRIDE;
224
* class Derived2 : public Base
227
* virtual void f() MOZ_OVERRIDE = 0;
229
* class Derived3 : public Base
232
* virtual void f() MOZ_OVERRIDE { }
235
* In compilers supporting C++11 override controls, MOZ_OVERRIDE *requires* that
236
* the function marked with it override a member function of a base class: it
237
* is a compile error if it does not. Otherwise MOZ_OVERRIDE does not affect
238
* semantics and merely documents the override relationship to the reader (but
239
* of course must still be used correctly to not break C++11 compilers).
241
#if defined(MOZ_HAVE_CXX11_OVERRIDE)
242
# define MOZ_OVERRIDE override
244
# define MOZ_OVERRIDE /* no support */
248
* MOZ_FINAL indicates that some functionality cannot be overridden through
249
* inheritance. It can be used to annotate either classes/structs or virtual
252
* To annotate a class/struct with MOZ_FINAL, place MOZ_FINAL immediately after
253
* the name of the class, before the list of classes from which it derives (if
254
* any) and before its opening brace. MOZ_FINAL must not be used to annotate
255
* unnamed classes or structs. (With some compilers, and with C++11 proper, the
256
* underlying expansion is ambiguous with specifying a class name.)
258
* class Base MOZ_FINAL
263
* virtual void f() { }
265
* // This will be an error in some compilers:
266
* class Derived : public Base
272
* One particularly common reason to specify MOZ_FINAL upon a class is to tell
273
* the compiler that it's not dangerous for it to have a non-virtual destructor
274
* yet have one or more virtual functions, silencing the warning it might emit
275
* in this case. Suppose Base above weren't annotated with MOZ_FINAL. Because
276
* ~Base() is non-virtual, an attempt to delete a Derived* through a Base*
277
* wouldn't call ~Derived(), so any cleanup ~Derived() might do wouldn't happen.
278
* (Formally C++ says behavior is undefined, but compilers will likely just call
279
* ~Base() and not ~Derived().) Specifying MOZ_FINAL tells the compiler that
280
* it's safe for the destructor to be non-virtual.
282
* In compilers implementing final controls, it is an error to inherit from a
283
* class annotated with MOZ_FINAL. In other compilers it serves only as
286
* To annotate a virtual member function with MOZ_FINAL, place MOZ_FINAL
287
* immediately before the ';' terminating the member function's declaration, or
288
* before '= 0;' if the member function is pure. If the member function is
289
* defined in the class definition, it should appear before the opening brace of
290
* the function body. (This placement is identical to that for MOZ_OVERRIDE.
291
* If both are used, they should appear in the order 'MOZ_FINAL MOZ_OVERRIDE'
297
* virtual void f() MOZ_FINAL;
302
* // This will be an error in some compilers:
306
* In compilers implementing final controls, it is an error for a derived class
307
* to override a method annotated with MOZ_FINAL. In other compilers it serves
308
* only as documentation.
310
#if defined(MOZ_HAVE_CXX11_FINAL)
311
# define MOZ_FINAL MOZ_HAVE_CXX11_FINAL
313
# define MOZ_FINAL /* no support */
317
* MOZ_ENUM_TYPE specifies the underlying numeric type for an enum. It's
318
* specified by placing MOZ_ENUM_TYPE(type) immediately after the enum name in
319
* its declaration, and before the opening curly brace, like
321
* enum MyEnum MOZ_ENUM_TYPE(uint16_t)
328
* In supporting compilers, the macro will expand to ": uint16_t". The
329
* compiler will allocate exactly two bytes for MyEnum, and will require all
330
* enumerators to have values between 0 and 65535. (Thus specifying "B =
331
* 100000" instead of "B = 7" would fail to compile.) In old compilers, the
332
* macro expands to the empty string, and the underlying type is generally
335
#ifdef MOZ_HAVE_CXX11_ENUM_TYPE
336
# define MOZ_ENUM_TYPE(type) : type
338
# define MOZ_ENUM_TYPE(type) /* no support */
342
* MOZ_BEGIN_ENUM_CLASS and MOZ_END_ENUM_CLASS provide access to the
343
* strongly-typed enumeration feature of C++11 ("enum class"). If supported
344
* by the compiler, an enum defined using these macros will not be implicitly
345
* converted to any other type, and its enumerators will be scoped using the
346
* enumeration name. Place MOZ_BEGIN_ENUM_CLASS(EnumName, type) in place of
347
* "enum EnumName {", and MOZ_END_ENUM_CLASS(EnumName) in place of the closing
350
* MOZ_BEGIN_ENUM_CLASS(Enum, int32_t)
352
* MOZ_END_ENUM_CLASS(Enum)
354
* This will make "Enum::A" and "Enum::B" appear in the global scope, but "A"
355
* and "B" will not. In compilers that support C++11 strongly-typed
356
* enumerations, implicit conversions of Enum values to numeric types will
357
* fail. In other compilers, Enum itself will actually be defined as a class,
358
* and some implicit conversions will fail while others will succeed.
360
* The type argument specifies the underlying type for the enum where
361
* supported, as with MOZ_ENUM_TYPE(). For simplicity, it is currently
362
* mandatory. As with MOZ_ENUM_TYPE(), it will do nothing on compilers that do
365
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
366
/* All compilers that support strong enums also support an explicit
367
* underlying type, so no extra check is needed */
368
# define MOZ_BEGIN_ENUM_CLASS(Name, type) enum class Name : type {
369
# define MOZ_END_ENUM_CLASS(Name) };
372
* We need Name to both name a type, and scope the provided enumerator
373
* names. Namespaces and classes both provide scoping, but namespaces
374
* aren't types, so we need to use a class that wraps the enum values. We
375
* have an implicit conversion from the inner enum type to the class, so
380
* will still work. We need to define an implicit conversion from the class
381
* to the inner enum as well, so that (for instance) switch statements will
382
* work. This means that the class can be implicitly converted to a numeric
383
* value as well via the enum type, since C++ allows an implicit
384
* user-defined conversion followed by a standard conversion to still be
387
* We have an explicit constructor from int defined, so that casts like
388
* (Enum)7 will still work. We also have a zero-argument constructor with
389
* no arguments, so declaration without initialization (like "Enum foo;")
392
* Additionally, we'll delete as many operators as possible for the inner
393
* enum type, so statements like this will still fail:
395
* f(5 + Enum::B); // deleted operator+
397
* But we can't prevent things like this, because C++ doesn't allow
398
* overriding conversions or assignment operators for enums:
406
# define MOZ_BEGIN_ENUM_CLASS(Name, type) \
410
enum Enum MOZ_ENUM_TYPE(type) \
412
# define MOZ_END_ENUM_CLASS(Name) \
415
Name(Enum aEnum) : mEnum(aEnum) {} \
416
explicit Name(int num) : mEnum((Enum)num) {} \
417
operator Enum() const { return mEnum; } \
421
inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \
422
inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \
423
inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \
424
inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \
425
inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \
426
inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \
427
inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \
428
inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \
429
inline int operator%(const int&, const Name::Enum&) MOZ_DELETE; \
430
inline int operator%(const Name::Enum&, const int&) MOZ_DELETE; \
431
inline int operator+(const Name::Enum&) MOZ_DELETE; \
432
inline int operator-(const Name::Enum&) MOZ_DELETE; \
433
inline int& operator++(Name::Enum&) MOZ_DELETE; \
434
inline int operator++(Name::Enum&, int) MOZ_DELETE; \
435
inline int& operator--(Name::Enum&) MOZ_DELETE; \
436
inline int operator--(Name::Enum&, int) MOZ_DELETE; \
437
inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \
438
inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \
439
inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \
440
inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \
441
inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \
442
inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \
443
inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \
444
inline bool operator<(const Name::Enum&, const int&) MOZ_DELETE; \
445
inline bool operator>=(const int&, const Name::Enum&) MOZ_DELETE; \
446
inline bool operator>=(const Name::Enum&, const int&) MOZ_DELETE; \
447
inline bool operator<=(const int&, const Name::Enum&) MOZ_DELETE; \
448
inline bool operator<=(const Name::Enum&, const int&) MOZ_DELETE; \
449
inline bool operator!(const Name::Enum&) MOZ_DELETE; \
450
inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \
451
inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \
452
inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \
453
inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \
454
inline int operator~(const Name::Enum&) MOZ_DELETE; \
455
inline int operator&(const int&, const Name::Enum&) MOZ_DELETE; \
456
inline int operator&(const Name::Enum&, const int&) MOZ_DELETE; \
457
inline int operator|(const int&, const Name::Enum&) MOZ_DELETE; \
458
inline int operator|(const Name::Enum&, const int&) MOZ_DELETE; \
459
inline int operator^(const int&, const Name::Enum&) MOZ_DELETE; \
460
inline int operator^(const Name::Enum&, const int&) MOZ_DELETE; \
461
inline int operator<<(const int&, const Name::Enum&) MOZ_DELETE; \
462
inline int operator<<(const Name::Enum&, const int&) MOZ_DELETE; \
463
inline int operator>>(const int&, const Name::Enum&) MOZ_DELETE; \
464
inline int operator>>(const Name::Enum&, const int&) MOZ_DELETE; \
465
inline int& operator+=(int&, const Name::Enum&) MOZ_DELETE; \
466
inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \
467
inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \
468
inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \
469
inline int& operator%=(int&, const Name::Enum&) MOZ_DELETE; \
470
inline int& operator&=(int&, const Name::Enum&) MOZ_DELETE; \
471
inline int& operator|=(int&, const Name::Enum&) MOZ_DELETE; \
472
inline int& operator^=(int&, const Name::Enum&) MOZ_DELETE; \
473
inline int& operator<<=(int&, const Name::Enum&) MOZ_DELETE; \
474
inline int& operator>>=(int&, const Name::Enum&) MOZ_DELETE;
478
* MOZ_WARN_UNUSED_RESULT tells the compiler to emit a warning if a function's
479
* return value is not used by the caller.
481
* Place this attribute at the very beginning of a function definition. For
484
* MOZ_WARN_UNUSED_RESULT int foo();
488
* MOZ_WARN_UNUSED_RESULT int foo() { return 42; }
490
#if defined(__GNUC__) || defined(__clang__)
491
# define MOZ_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
493
# define MOZ_WARN_UNUSED_RESULT
496
#endif /* __cplusplus */
498
#endif /* mozilla_Attributes_h_ */