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.
17
* MOZ_INLINE is a macro which expands to tell the compiler that the method
18
* decorated with it should be inlined. This macro is usable from C and C++
19
* code, even though C89 does not support the |inline| keyword. The compiler
20
* may ignore this directive if it chooses.
22
#if defined(__cplusplus)
23
# define MOZ_INLINE inline
24
#elif defined(_MSC_VER)
25
# define MOZ_INLINE __inline
26
#elif defined(__GNUC__)
27
# define MOZ_INLINE __inline__
29
# define MOZ_INLINE inline
33
* MOZ_ALWAYS_INLINE is a macro which expands to tell the compiler that the
34
* method decorated with it must be inlined, even if the compiler thinks
35
* otherwise. This is only a (much) stronger version of the MOZ_INLINE hint:
36
* compilers are not guaranteed to respect it (although they're much more likely
40
# define MOZ_ALWAYS_INLINE MOZ_INLINE
41
#elif defined(_MSC_VER)
42
# define MOZ_ALWAYS_INLINE __forceinline
43
#elif defined(__GNUC__)
44
# define MOZ_ALWAYS_INLINE __attribute__((always_inline)) MOZ_INLINE
46
# define MOZ_ALWAYS_INLINE MOZ_INLINE
50
* g++ requires -std=c++0x or -std=gnu++0x to support C++11 functionality
51
* without warnings (functionality used by the macros below). These modes are
52
* detectable by checking whether __GXX_EXPERIMENTAL_CXX0X__ is defined or, more
53
* standardly, by checking whether __cplusplus has a C++11 or greater value.
54
* Current versions of g++ do not correctly set __cplusplus, so we check both
55
* for forward compatibility.
57
#if defined(__clang__)
59
* Per Clang documentation, "Note that marketing version numbers should not
60
* be used to check for language features, as different vendors use different
61
* numbering schemes. Instead, use the feature checking macros."
63
# ifndef __has_extension
64
# define __has_extension __has_feature /* compatibility, for older versions of clang */
66
# if __has_extension(cxx_deleted_functions)
67
# define MOZ_HAVE_CXX11_DELETE
69
# if __has_extension(cxx_override_control)
70
# define MOZ_HAVE_CXX11_OVERRIDE
71
# define MOZ_HAVE_CXX11_FINAL final
73
# if __has_extension(cxx_strong_enums)
74
# define MOZ_HAVE_CXX11_ENUM_TYPE
75
# define MOZ_HAVE_CXX11_STRONG_ENUMS
77
# if __has_attribute(noinline)
78
# define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
80
# if __has_attribute(noreturn)
81
# define MOZ_HAVE_NORETURN __attribute__((noreturn))
83
#elif defined(__GNUC__)
84
# if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
86
# define MOZ_HAVE_CXX11_DELETE
87
# define MOZ_HAVE_CXX11_OVERRIDE
88
# define MOZ_HAVE_CXX11_FINAL final
90
# if __GNUC_MINOR__ >= 7
91
# define MOZ_HAVE_CXX11_OVERRIDE
92
# define MOZ_HAVE_CXX11_FINAL final
94
# if __GNUC_MINOR__ >= 4
95
# define MOZ_HAVE_CXX11_DELETE
96
# define MOZ_HAVE_CXX11_ENUM_TYPE
97
# define MOZ_HAVE_CXX11_STRONG_ENUMS
101
/* __final is a non-C++11 GCC synonym for 'final', per GCC r176655. */
103
# define MOZ_HAVE_CXX11_FINAL __final
105
# if __GNUC_MINOR__ >= 7
106
# define MOZ_HAVE_CXX11_FINAL __final
110
# define MOZ_HAVE_NEVER_INLINE __attribute__((noinline))
111
# define MOZ_HAVE_NORETURN __attribute__((noreturn))
112
#elif defined(_MSC_VER)
113
# if _MSC_VER >= 1400
114
# define MOZ_HAVE_CXX11_OVERRIDE
115
/* MSVC currently spells "final" as "sealed". */
116
# define MOZ_HAVE_CXX11_FINAL sealed
117
# define MOZ_HAVE_CXX11_ENUM_TYPE
119
# if _MSC_VER >= 1700
120
# define MOZ_HAVE_CXX11_STRONG_ENUMS
122
# define MOZ_HAVE_NEVER_INLINE __declspec(noinline)
123
# define MOZ_HAVE_NORETURN __declspec(noreturn)
127
* MOZ_NEVER_INLINE is a macro which expands to tell the compiler that the
128
* method decorated with it must never be inlined, even if the compiler would
129
* otherwise choose to inline the method. Compilers aren't absolutely
130
* guaranteed to support this, but most do.
132
#if defined(MOZ_HAVE_NEVER_INLINE)
133
# define MOZ_NEVER_INLINE MOZ_HAVE_NEVER_INLINE
135
# define MOZ_NEVER_INLINE /* no support */
139
* MOZ_NORETURN, specified at the start of a function declaration, indicates
140
* that the given function does not return. (The function definition does not
141
* need to be annotated.)
143
* MOZ_NORETURN void abort(const char* msg);
145
* This modifier permits the compiler to optimize code assuming a call to such a
146
* function will never return. It also enables the compiler to avoid spurious
147
* warnings about not initializing variables, or about any other seemingly-dodgy
148
* operations performed after the function returns.
150
* This modifier does not affect the corresponding function's linking behavior.
152
#if defined(MOZ_HAVE_NORETURN)
153
# define MOZ_NORETURN MOZ_HAVE_NORETURN
155
# define MOZ_NORETURN /* no support */
159
* MOZ_ASAN_BLACKLIST is a macro to tell AddressSanitizer (a compile-time
160
* instrumentation shipped with Clang) to not instrument the annotated function.
161
* Furthermore, it will prevent the compiler from inlining the function because
162
* inlining currently breaks the blacklisting mechanism of AddressSanitizer.
164
#if defined(MOZ_ASAN)
165
# define MOZ_ASAN_BLACKLIST MOZ_NEVER_INLINE __attribute__((no_address_safety_analysis))
167
# define MOZ_ASAN_BLACKLIST
174
* MOZ_DELETE, specified immediately prior to the ';' terminating an undefined-
175
* method declaration, attempts to delete that method from the corresponding
176
* class. An attempt to use the method will always produce an error *at compile
177
* time* (instead of sometimes as late as link time) when this macro can be
178
* implemented. For example, you can use MOZ_DELETE to produce classes with no
179
* implicit copy constructor or assignment operator:
184
* NonCopyable(const NonCopyable& other) MOZ_DELETE;
185
* void operator=(const NonCopyable& other) MOZ_DELETE;
188
* If MOZ_DELETE can't be implemented for the current compiler, use of the
189
* annotated method will still cause an error, but the error might occur at link
190
* time in some cases rather than at compile time.
192
* MOZ_DELETE relies on C++11 functionality not universally implemented. As a
193
* backstop, method declarations using MOZ_DELETE should be private.
195
#if defined(MOZ_HAVE_CXX11_DELETE)
196
# define MOZ_DELETE = delete
198
# define MOZ_DELETE /* no support */
202
* MOZ_OVERRIDE explicitly indicates that a virtual member function in a class
203
* overrides a member function of a base class, rather than potentially being a
204
* new member function. MOZ_OVERRIDE should be placed immediately before the
205
* ';' terminating the member function's declaration, or before '= 0;' if the
206
* member function is pure. If the member function is defined in the class
207
* definition, it should appear before the opening brace of the function body.
212
* virtual void f() = 0;
214
* class Derived1 : public Base
217
* virtual void f() MOZ_OVERRIDE;
219
* class Derived2 : public Base
222
* virtual void f() MOZ_OVERRIDE = 0;
224
* class Derived3 : public Base
227
* virtual void f() MOZ_OVERRIDE { }
230
* In compilers supporting C++11 override controls, MOZ_OVERRIDE *requires* that
231
* the function marked with it override a member function of a base class: it
232
* is a compile error if it does not. Otherwise MOZ_OVERRIDE does not affect
233
* semantics and merely documents the override relationship to the reader (but
234
* of course must still be used correctly to not break C++11 compilers).
236
#if defined(MOZ_HAVE_CXX11_OVERRIDE)
237
# define MOZ_OVERRIDE override
239
# define MOZ_OVERRIDE /* no support */
243
* MOZ_FINAL indicates that some functionality cannot be overridden through
244
* inheritance. It can be used to annotate either classes/structs or virtual
247
* To annotate a class/struct with MOZ_FINAL, place MOZ_FINAL immediately after
248
* the name of the class, before the list of classes from which it derives (if
249
* any) and before its opening brace. MOZ_FINAL must not be used to annotate
250
* unnamed classes or structs. (With some compilers, and with C++11 proper, the
251
* underlying expansion is ambiguous with specifying a class name.)
253
* class Base MOZ_FINAL
258
* virtual void f() { }
260
* // This will be an error in some compilers:
261
* class Derived : public Base
267
* One particularly common reason to specify MOZ_FINAL upon a class is to tell
268
* the compiler that it's not dangerous for it to have a non-virtual destructor
269
* yet have one or more virtual functions, silencing the warning it might emit
270
* in this case. Suppose Base above weren't annotated with MOZ_FINAL. Because
271
* ~Base() is non-virtual, an attempt to delete a Derived* through a Base*
272
* wouldn't call ~Derived(), so any cleanup ~Derived() might do wouldn't happen.
273
* (Formally C++ says behavior is undefined, but compilers will likely just call
274
* ~Base() and not ~Derived().) Specifying MOZ_FINAL tells the compiler that
275
* it's safe for the destructor to be non-virtual.
277
* In compilers implementing final controls, it is an error to inherit from a
278
* class annotated with MOZ_FINAL. In other compilers it serves only as
281
* To annotate a virtual member function with MOZ_FINAL, place MOZ_FINAL
282
* immediately before the ';' terminating the member function's declaration, or
283
* before '= 0;' if the member function is pure. If the member function is
284
* defined in the class definition, it should appear before the opening brace of
285
* the function body. (This placement is identical to that for MOZ_OVERRIDE.
286
* If both are used, they should appear in the order 'MOZ_FINAL MOZ_OVERRIDE'
292
* virtual void f() MOZ_FINAL;
297
* // This will be an error in some compilers:
301
* In compilers implementing final controls, it is an error for a derived class
302
* to override a method annotated with MOZ_FINAL. In other compilers it serves
303
* only as documentation.
305
#if defined(MOZ_HAVE_CXX11_FINAL)
306
# define MOZ_FINAL MOZ_HAVE_CXX11_FINAL
308
# define MOZ_FINAL /* no support */
312
* MOZ_ENUM_TYPE specifies the underlying numeric type for an enum. It's
313
* specified by placing MOZ_ENUM_TYPE(type) immediately after the enum name in
314
* its declaration, and before the opening curly brace, like
316
* enum MyEnum MOZ_ENUM_TYPE(uint16_t)
323
* In supporting compilers, the macro will expand to ": uint16_t". The
324
* compiler will allocate exactly two bytes for MyEnum, and will require all
325
* enumerators to have values between 0 and 65535. (Thus specifying "B =
326
* 100000" instead of "B = 7" would fail to compile.) In old compilers, the
327
* macro expands to the empty string, and the underlying type is generally
330
#ifdef MOZ_HAVE_CXX11_ENUM_TYPE
331
# define MOZ_ENUM_TYPE(type) : type
333
# define MOZ_ENUM_TYPE(type) /* no support */
337
* MOZ_BEGIN_ENUM_CLASS and MOZ_END_ENUM_CLASS provide access to the
338
* strongly-typed enumeration feature of C++11 ("enum class"). If supported
339
* by the compiler, an enum defined using these macros will not be implicitly
340
* converted to any other type, and its enumerators will be scoped using the
341
* enumeration name. Place MOZ_BEGIN_ENUM_CLASS(EnumName, type) in place of
342
* "enum EnumName {", and MOZ_END_ENUM_CLASS(EnumName) in place of the closing
345
* MOZ_BEGIN_ENUM_CLASS(Enum, int32_t)
347
* MOZ_END_ENUM_CLASS(Enum)
349
* This will make "Enum::A" and "Enum::B" appear in the global scope, but "A"
350
* and "B" will not. In compilers that support C++11 strongly-typed
351
* enumerations, implicit conversions of Enum values to numeric types will
352
* fail. In other compilers, Enum itself will actually be defined as a class,
353
* and some implicit conversions will fail while others will succeed.
355
* The type argument specifies the underlying type for the enum where
356
* supported, as with MOZ_ENUM_TYPE(). For simplicity, it is currently
357
* mandatory. As with MOZ_ENUM_TYPE(), it will do nothing on compilers that do
360
#if defined(MOZ_HAVE_CXX11_STRONG_ENUMS)
361
/* All compilers that support strong enums also support an explicit
362
* underlying type, so no extra check is needed */
363
# define MOZ_BEGIN_ENUM_CLASS(Name, type) enum class Name : type {
364
# define MOZ_END_ENUM_CLASS(Name) };
367
* We need Name to both name a type, and scope the provided enumerator
368
* names. Namespaces and classes both provide scoping, but namespaces
369
* aren't types, so we need to use a class that wraps the enum values. We
370
* have an implicit conversion from the inner enum type to the class, so
375
* will still work. We need to define an implicit conversion from the class
376
* to the inner enum as well, so that (for instance) switch statements will
377
* work. This means that the class can be implicitly converted to a numeric
378
* value as well via the enum type, since C++ allows an implicit
379
* user-defined conversion followed by a standard conversion to still be
382
* We have an explicit constructor from int defined, so that casts like
383
* (Enum)7 will still work. We also have a zero-argument constructor with
384
* no arguments, so declaration without initialization (like "Enum foo;")
387
* Additionally, we'll delete as many operators as possible for the inner
388
* enum type, so statements like this will still fail:
390
* f(5 + Enum::B); // deleted operator+
392
* But we can't prevent things like this, because C++ doesn't allow
393
* overriding conversions or assignment operators for enums:
401
# define MOZ_BEGIN_ENUM_CLASS(Name, type) \
405
enum Enum MOZ_ENUM_TYPE(type) \
407
# define MOZ_END_ENUM_CLASS(Name) \
410
Name(Enum aEnum) : mEnum(aEnum) {} \
411
explicit Name(int num) : mEnum((Enum)num) {} \
412
operator Enum() const { return mEnum; } \
416
inline int operator+(const int&, const Name::Enum&) MOZ_DELETE; \
417
inline int operator+(const Name::Enum&, const int&) MOZ_DELETE; \
418
inline int operator-(const int&, const Name::Enum&) MOZ_DELETE; \
419
inline int operator-(const Name::Enum&, const int&) MOZ_DELETE; \
420
inline int operator*(const int&, const Name::Enum&) MOZ_DELETE; \
421
inline int operator*(const Name::Enum&, const int&) MOZ_DELETE; \
422
inline int operator/(const int&, const Name::Enum&) MOZ_DELETE; \
423
inline int operator/(const Name::Enum&, const int&) MOZ_DELETE; \
424
inline int operator%(const int&, const Name::Enum&) MOZ_DELETE; \
425
inline int operator%(const Name::Enum&, const int&) MOZ_DELETE; \
426
inline int operator+(const Name::Enum&) MOZ_DELETE; \
427
inline int operator-(const Name::Enum&) MOZ_DELETE; \
428
inline int& operator++(Name::Enum&) MOZ_DELETE; \
429
inline int operator++(Name::Enum&, int) MOZ_DELETE; \
430
inline int& operator--(Name::Enum&) MOZ_DELETE; \
431
inline int operator--(Name::Enum&, int) MOZ_DELETE; \
432
inline bool operator==(const int&, const Name::Enum&) MOZ_DELETE; \
433
inline bool operator==(const Name::Enum&, const int&) MOZ_DELETE; \
434
inline bool operator!=(const int&, const Name::Enum&) MOZ_DELETE; \
435
inline bool operator!=(const Name::Enum&, const int&) MOZ_DELETE; \
436
inline bool operator>(const int&, const Name::Enum&) MOZ_DELETE; \
437
inline bool operator>(const Name::Enum&, const int&) MOZ_DELETE; \
438
inline bool operator<(const int&, const Name::Enum&) MOZ_DELETE; \
439
inline bool operator<(const Name::Enum&, const int&) MOZ_DELETE; \
440
inline bool operator>=(const int&, const Name::Enum&) MOZ_DELETE; \
441
inline bool operator>=(const Name::Enum&, const int&) MOZ_DELETE; \
442
inline bool operator<=(const int&, const Name::Enum&) MOZ_DELETE; \
443
inline bool operator<=(const Name::Enum&, const int&) MOZ_DELETE; \
444
inline bool operator!(const Name::Enum&) MOZ_DELETE; \
445
inline bool operator&&(const bool&, const Name::Enum&) MOZ_DELETE; \
446
inline bool operator&&(const Name::Enum&, const bool&) MOZ_DELETE; \
447
inline bool operator||(const bool&, const Name::Enum&) MOZ_DELETE; \
448
inline bool operator||(const Name::Enum&, const bool&) MOZ_DELETE; \
449
inline int operator~(const Name::Enum&) MOZ_DELETE; \
450
inline int operator&(const int&, const Name::Enum&) MOZ_DELETE; \
451
inline int operator&(const Name::Enum&, const int&) MOZ_DELETE; \
452
inline int operator|(const int&, const Name::Enum&) MOZ_DELETE; \
453
inline int operator|(const Name::Enum&, const int&) MOZ_DELETE; \
454
inline int operator^(const int&, const Name::Enum&) MOZ_DELETE; \
455
inline int operator^(const Name::Enum&, const int&) MOZ_DELETE; \
456
inline int operator<<(const int&, const Name::Enum&) MOZ_DELETE; \
457
inline int operator<<(const Name::Enum&, const int&) MOZ_DELETE; \
458
inline int operator>>(const int&, const Name::Enum&) MOZ_DELETE; \
459
inline int operator>>(const Name::Enum&, const int&) MOZ_DELETE; \
460
inline int& operator+=(int&, const Name::Enum&) MOZ_DELETE; \
461
inline int& operator-=(int&, const Name::Enum&) MOZ_DELETE; \
462
inline int& operator*=(int&, const Name::Enum&) MOZ_DELETE; \
463
inline int& operator/=(int&, const Name::Enum&) MOZ_DELETE; \
464
inline int& operator%=(int&, const Name::Enum&) 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;
473
* MOZ_WARN_UNUSED_RESULT tells the compiler to emit a warning if a function's
474
* return value is not used by the caller.
476
* Place this attribute at the very beginning of a function definition. For
479
* MOZ_WARN_UNUSED_RESULT int foo();
483
* MOZ_WARN_UNUSED_RESULT int foo() { return 42; }
485
#if defined(__GNUC__) || defined(__clang__)
486
# define MOZ_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
488
# define MOZ_WARN_UNUSED_RESULT
491
#endif /* __cplusplus */
493
#endif /* mozilla_Attributes_h_ */