Debian Codegraph @

src/mpt/base/macros.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_BASE_MACROS_HPP
4 #define MPT_BASE_MACROS_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9
10 #include <type_traits>
11
12 #if MPT_COMPILER_MSVC && MPT_OS_WINDOWS
13 #include <windows.h>
14 #endif // MPT_COMPILER_MSVC && MPT_OS_WINDOWS
15
16
17
18 // Advanced inline attributes
19 #if MPT_COMPILER_MSVC
20 #define MPT_FORCEINLINE __forceinline
21 #define MPT_NOINLINE __declspec(noinline)
22 #elif MPT_COMPILER_GCC || MPT_COMPILER_CLANG
23 #define MPT_FORCEINLINE __attribute__((always_inline)) inline
24 #define MPT_NOINLINE __attribute__((noinline))
25 #else
26 #define MPT_FORCEINLINE inline
27 #define MPT_NOINLINE
28 #endif
29
30
31
32 // constexpr
33
34 #define MPT_CONSTEXPRINLINE constexpr MPT_FORCEINLINE
35
36 #if MPT_CXX_AT_LEAST(23)
37 #define MPT_CONSTEXPR20_FUN constexpr MPT_FORCEINLINE
38 #define MPT_CONSTEXPR20_VAR constexpr
39 #define MPT_CONSTEXPR23_FUN constexpr MPT_FORCEINLINE
40 #define MPT_CONSTEXPR23_VAR constexpr
41 #elif MPT_CXX_AT_LEAST(20)
42 #define MPT_CONSTEXPR20_FUN constexpr MPT_FORCEINLINE
43 #define MPT_CONSTEXPR20_VAR constexpr
44 #define MPT_CONSTEXPR23_FUN MPT_FORCEINLINE
45 #define MPT_CONSTEXPR23_VAR const
46 #else // C++
47 #define MPT_CONSTEXPR20_FUN MPT_FORCEINLINE
48 #define MPT_CONSTEXPR20_VAR const
49 #define MPT_CONSTEXPR23_FUN MPT_FORCEINLINE
50 #define MPT_CONSTEXPR23_VAR const
51 #endif // C++
52
53 #if !defined(MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_ALGORITHM)
54 #define MPT_CONSTEXPR20_ALGORITHM_FUN MPT_CONSTEXPR20_FUN
55 #define MPT_CONSTEXPR20_ALGORITHM_VAR MPT_CONSTEXPR20_VAR
56 #else
57 #define MPT_CONSTEXPR20_ALGORITHM_FUN MPT_CONSTEXPR23_FUN
58 #define MPT_CONSTEXPR20_ALGORITHM_VAR MPT_CONSTEXPR23_VAR
59 #endif
60
61 #if !defined(MPT_LIBCXX_QUIRK_NO_CXX20_CONSTEXPR_CONTAINER)
62 #define MPT_CONSTEXPR20_CONTAINER_FUN MPT_CONSTEXPR20_FUN
63 #define MPT_CONSTEXPR20_CONTAINER_VAR MPT_CONSTEXPR20_VAR
64 #else
65 #define MPT_CONSTEXPR20_CONTAINER_FUN MPT_CONSTEXPR23_FUN
66 #define MPT_CONSTEXPR20_CONTAINER_VAR MPT_CONSTEXPR23_VAR
67 #endif
68
69
70
71 #if MPT_CXX_AT_LEAST(20)
72 #define MPT_CONSTEVAL consteval
73 #else // !C++20
74 // fallback to constexpr
75 #define MPT_CONSTEVAL MPT_CONSTEXPRINLINE
76 #endif // C++20
77
78
79
80 #if MPT_CXX_AT_LEAST(20)
81 #define MPT_CONSTEVAL_NOEXCEPT noexcept
82 #else // !C++20
83 #define MPT_CONSTEVAL_NOEXCEPT
84 #endif // C++20
85
86
87
88 #define MPT_FORCE_CONSTEXPR_EXPRESSION(expr) [&]() { \
89 constexpr auto x = (expr); \
90 return x; \
91 }()
92 #define MPT_FORCE_CONSTEXPR_VALUE(val) []() { \
93 constexpr auto x = (val); \
94 return x; \
95 }()
96
97
98
99 #if MPT_CXX_AT_LEAST(20)
100 // this assumes that for C++20, a consteval function will be used
101 #define MPT_FORCE_CONSTEVAL_EXPRESSION(expr) (expr)
102 #define MPT_FORCE_CONSTEVAL_VALUE(val) (val)
103 #else // !C++20
104 #define MPT_FORCE_CONSTEVAL_EXPRESSION(expr) [&]() { \
105 constexpr auto x = (expr); \
106 return x; \
107 }()
108 #define MPT_FORCE_CONSTEVAL_VALUE(val) []() { \
109 constexpr auto x = (val); \
110 return x; \
111 }()
112 #endif // C++20
113
114
115
116 #if MPT_CXX_AT_LEAST(20)
117 #define MPT_CONSTINIT constinit
118 #else // !C++20
119 // fallback to nothing
120 #define MPT_CONSTINIT
121 #endif // C++20
122
123
124
125 #if MPT_CXX_AT_LEAST(20)
126 #define MPT_IS_CONSTANT_EVALUATED20() std::is_constant_evaluated()
127 #define MPT_IS_CONSTANT_EVALUATED() std::is_constant_evaluated()
128 #else // !C++20
129 #define MPT_IS_CONSTANT_EVALUATED20() false
130 // this pessimizes the case for C++17 by always assuming constexpr context, which implies always running constexpr-friendly code
131 #define MPT_IS_CONSTANT_EVALUATED() true
132 #endif // C++20
133
134
135
136 #if MPT_COMPILER_MSVC
137 #define MPT_MAYBE_CONSTANT_IF(x) \
138 __pragma(warning(push)) \
139 __pragma(warning(disable : 4127)) \
140 if (x) \
141 __pragma(warning(pop)) \
142 /**/
143 #endif
144
145 #if MPT_COMPILER_GCC
146 #define MPT_MAYBE_CONSTANT_IF(x) \
147 _Pragma("GCC diagnostic push") \
148 _Pragma("GCC diagnostic ignored \"-Wtype-limits\"") \
149 if (x) \
150 _Pragma("GCC diagnostic pop") \
151 /**/
152 #endif
153
154 #if MPT_COMPILER_CLANG
155 #define MPT_MAYBE_CONSTANT_IF(x) \
156 _Pragma("clang diagnostic push") \
157 _Pragma("clang diagnostic ignored \"-Wunknown-pragmas\"") \
158 _Pragma("clang diagnostic ignored \"-Wunreachable-code\"") \
159 _Pragma("clang diagnostic ignored \"-Wtautological-constant-out-of-range-compare\"") \
160 _Pragma("clang diagnostic ignored \"-Wtype-limits\"") \
161 if (x) \
162 _Pragma("clang diagnostic pop") \
163 /**/
164 #endif
165
166 #if !defined(MPT_MAYBE_CONSTANT_IF)
167 // MPT_MAYBE_CONSTANT_IF disables compiler warnings for conditions that may in some case be either always false or always true (this may turn out to be useful in ASSERTions in some cases).
168 #define MPT_MAYBE_CONSTANT_IF(x) if (x)
169 #endif
170
171
172
173 #if MPT_COMPILER_MSVC && MPT_OS_WINDOWS
174 #define MPT_UNUSED(x) UNREFERENCED_PARAMETER(x)
175 #else
176 #define MPT_UNUSED(x) static_cast<void>(x)
177 #endif
178
179
180
181 #define MPT_DISCARD(expr) static_cast<void>(expr)
182
183
184
185 // Use MPT_RESTRICT to indicate that a pointer is guaranteed to not be aliased.
186 #if MPT_COMPILER_MSVC || MPT_COMPILER_GCC || MPT_COMPILER_CLANG
187 #define MPT_RESTRICT __restrict
188 #else
189 #define MPT_RESTRICT
190 #endif
191
192
193
194 #if MPT_CXX_AT_LEAST(23) && !MPT_GCC_BEFORE(13, 0, 0) && !MPT_CLANG_BEFORE(19, 0, 0) && !MPT_COMPILER_MSVC
195 #define MPT_ASSUME(expr) [[assume(expr)]]
196 #else // !C++23
197 #if MPT_COMPILER_CLANG
198 #define MPT_ASSUME(expr) __builtin_assume(expr)
199 #endif
200 #if MPT_COMPILER_MSVC
201 #define MPT_ASSUME(expr) __assume(expr)
202 #endif
203 #if MPT_COMPILER_GCC
204 #define MPT_ASSUME(expr) \
205 do { \
206 if (!expr) { \
207 __builtin_unreachable(); \
208 } \
209 } while (0)
210 #endif
211 #if !defined(MPT_ASSUME)
212 #define MPT_ASSUME(expr) MPT_DISCARD(expr)
213 #endif
214 #endif // C++23
215
216
217
218 #endif // MPT_BASE_MACROS_HPP