1 /* Copyright (C) 2015-2023 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #ifndef COMMON_ENUM_FLAGS_H
19 #define COMMON_ENUM_FLAGS_H
23 /* Type-safe wrapper for enum flags. enum flags are enums where the
24 values are bits that are meant to be ORed together.
26 This allows writing code like the below, while with raw enums this
27 would fail to compile without casts to enum type at the assignments
37 DEF_ENUM_FLAGS_TYPE(enum some_flag, some_flags);
39 some_flags f = flag_val1 | flag_val2;
42 It's also possible to assign literal zero to an enum flags variable
43 (meaning, no flags), dispensing adding an awkward explicit "no
44 value" value to the enumeration. For example:
47 f |= flag_val3 | flag_val4;
49 Note that literal integers other than zero fail to compile:
51 some_flags f = 1; // error
56 /* Use this to mark an enum as flags enum. It defines FLAGS_TYPE as
57 enum_flags wrapper class for ENUM, and enables the global operator
58 overloads for ENUM. */
59 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
60 typedef enum_flags<enum_type> flags_type; \
61 void is_enum_flags_enum_type (enum_type *)
63 /* To enable the global enum_flags operators for enum, declare an
64 "is_enum_flags_enum_type" overload that has exactly one parameter,
65 of type a pointer to that enum class. E.g.,:
67 void is_enum_flags_enum_type (enum some_flag *);
69 The function does not need to be defined, only declared.
70 DEF_ENUM_FLAGS_TYPE declares this.
72 A function declaration is preferred over a traits type, because the
73 former allows calling the DEF_ENUM_FLAGS_TYPE macro inside a
74 namespace to define the corresponding enum flags type in that
75 namespace. The compiler finds the corresponding
76 is_enum_flags_enum_type function via ADL. */
78 /* Note that std::underlying_type<enum_type> is not what we want here,
79 since that returns unsigned int even when the enum decays to signed
81 template<int size
, bool sign
> class integer_for_size
{ typedef void type
; };
82 template<> struct integer_for_size
<1, 0> { typedef uint8_t type
; };
83 template<> struct integer_for_size
<2, 0> { typedef uint16_t type
; };
84 template<> struct integer_for_size
<4, 0> { typedef uint32_t type
; };
85 template<> struct integer_for_size
<8, 0> { typedef uint64_t type
; };
86 template<> struct integer_for_size
<1, 1> { typedef int8_t type
; };
87 template<> struct integer_for_size
<2, 1> { typedef int16_t type
; };
88 template<> struct integer_for_size
<4, 1> { typedef int32_t type
; };
89 template<> struct integer_for_size
<8, 1> { typedef int64_t type
; };
92 struct enum_underlying_type
95 integer_for_size
<sizeof (T
), static_cast<bool>(T (-1) < T (0))>::type
99 namespace enum_flags_detail
102 /* Private type used to support initializing flag types with zero:
106 but not other integers:
110 The way this works is that we define an implicit constructor that
111 takes a pointer to this private type. Since nothing can
112 instantiate an object of this type, the only possible pointer to
113 pass to the constructor is the NULL pointer, or, zero. */
116 /* gdb::Requires trait helpers. */
117 template <typename enum_type
>
119 = std::is_unsigned
<typename enum_underlying_type
<enum_type
>::type
>;
120 template <typename enum_type
>
122 = std::is_signed
<typename enum_underlying_type
<enum_type
>::type
>;
126 template <typename E
>
131 typedef typename enum_underlying_type
<enum_type
>::type underlying_type
;
133 /* For to_string. Maps one enumerator of E to a string. */
134 struct string_mapping
140 /* Convenience for to_string implementations, to build a
141 string_mapping array. */
142 #define MAP_ENUM_FLAG(ENUM_FLAG) { ENUM_FLAG, #ENUM_FLAG }
145 /* Allow default construction. */
146 constexpr enum_flags ()
147 : m_enum_value ((enum_type
) 0)
150 /* The default move/copy ctor/assignment do the right thing. */
152 /* If you get an error saying these two overloads are ambiguous,
153 then you tried to mix values of different enum types. */
154 constexpr enum_flags (enum_type e
)
157 constexpr enum_flags (enum_flags_detail::zero_type
*zero
)
158 : m_enum_value ((enum_type
) 0)
161 enum_flags
&operator&= (enum_flags e
) &
163 m_enum_value
= (enum_type
) (m_enum_value
& e
.m_enum_value
);
166 enum_flags
&operator|= (enum_flags e
) &
168 m_enum_value
= (enum_type
) (m_enum_value
| e
.m_enum_value
);
171 enum_flags
&operator^= (enum_flags e
) &
173 m_enum_value
= (enum_type
) (m_enum_value
^ e
.m_enum_value
);
177 /* Delete rval versions. */
178 void operator&= (enum_flags e
) && = delete;
179 void operator|= (enum_flags e
) && = delete;
180 void operator^= (enum_flags e
) && = delete;
182 /* Like raw enums, allow conversion to the underlying type. */
183 constexpr operator underlying_type () const
188 /* Get the underlying value as a raw enum. */
189 constexpr enum_type
raw () const
194 /* Binary operations involving some unrelated type (which would be a
195 bug) are implemented as non-members, and deleted. */
197 /* Convert this object to a std::string, using MAPPING as
198 enumerator-to-string mapping array. This is not meant to be
199 called directly. Instead, enum_flags specializations should have
200 their own to_string function wrapping this one, thus hidding the
201 mapping array from callers.
203 Note: this is defined outside the template class so it can use
204 the global operators for enum_type, which are only defined after
205 the template class. */
207 std::string
to_string (const string_mapping (&mapping
)[N
]) const;
210 /* Stored as enum_type because GDB knows to print the bit flags
211 neatly if the enum values look like bit flags. */
212 enum_type m_enum_value
;
215 template <typename E
>
216 using is_enum_flags_enum_type_t
217 = decltype (is_enum_flags_enum_type (std::declval
<E
*> ()));
219 /* Global operator overloads. */
221 /* Generate binary operators. */
223 #define ENUM_FLAGS_GEN_BINOP(OPERATOR_OP, OP) \
225 /* Raw enum on both LHS/RHS. Returns raw enum type. */ \
226 template <typename enum_type, \
227 typename = is_enum_flags_enum_type_t<enum_type>> \
228 constexpr enum_type \
229 OPERATOR_OP (enum_type e1, enum_type e2) \
231 using underlying = typename enum_flags<enum_type>::underlying_type; \
232 return (enum_type) (underlying (e1) OP underlying (e2)); \
235 /* enum_flags on the LHS. */ \
236 template <typename enum_type, \
237 typename = is_enum_flags_enum_type_t<enum_type>> \
238 constexpr enum_flags<enum_type> \
239 OPERATOR_OP (enum_flags<enum_type> e1, enum_type e2) \
240 { return e1.raw () OP e2; } \
242 /* enum_flags on the RHS. */ \
243 template <typename enum_type, \
244 typename = is_enum_flags_enum_type_t<enum_type>> \
245 constexpr enum_flags<enum_type> \
246 OPERATOR_OP (enum_type e1, enum_flags<enum_type> e2) \
247 { return e1 OP e2.raw (); } \
249 /* enum_flags on both LHS/RHS. */ \
250 template <typename enum_type, \
251 typename = is_enum_flags_enum_type_t<enum_type>> \
252 constexpr enum_flags<enum_type> \
253 OPERATOR_OP (enum_flags<enum_type> e1, enum_flags<enum_type> e2) \
254 { return e1.raw () OP e2.raw (); } \
256 /* Delete cases involving unrelated types. */ \
258 template <typename enum_type, typename unrelated_type, \
259 typename = is_enum_flags_enum_type_t<enum_type>> \
260 constexpr enum_flags<enum_type> \
261 OPERATOR_OP (enum_type e1, unrelated_type e2) = delete; \
263 template <typename enum_type, typename unrelated_type, \
264 typename = is_enum_flags_enum_type_t<enum_type>> \
265 constexpr enum_flags<enum_type> \
266 OPERATOR_OP (unrelated_type e1, enum_type e2) = delete; \
268 template <typename enum_type, typename unrelated_type, \
269 typename = is_enum_flags_enum_type_t<enum_type>> \
270 constexpr enum_flags<enum_type> \
271 OPERATOR_OP (enum_flags<enum_type> e1, unrelated_type e2) = delete; \
273 template <typename enum_type, typename unrelated_type, \
274 typename = is_enum_flags_enum_type_t<enum_type>> \
275 constexpr enum_flags<enum_type> \
276 OPERATOR_OP (unrelated_type e1, enum_flags<enum_type> e2) = delete;
278 /* Generate non-member compound assignment operators. Only the raw
279 enum versions are defined here. The enum_flags versions are
280 defined as member functions, simply because it's less code that
283 Note we delete operators that would allow e.g.,
285 "enum_type | 1" or "enum_type1 | enum_type2"
287 because that would allow a mistake like :
288 enum flags1 { F1_FLAGS1 = 1 };
289 enum flags2 { F2_FLAGS2 = 2 };
292 case F1_FLAGS1 | F2_FLAGS2:
295 If you really need to 'or' enumerators of different flag types,
296 cast to integer first.
298 #define ENUM_FLAGS_GEN_COMPOUND_ASSIGN(OPERATOR_OP, OP) \
299 /* lval reference version. */ \
300 template <typename enum_type, \
301 typename = is_enum_flags_enum_type_t<enum_type>> \
302 constexpr enum_type & \
303 OPERATOR_OP (enum_type &e1, enum_type e2) \
304 { return e1 = e1 OP e2; } \
306 /* rval reference version. */ \
307 template <typename enum_type, \
308 typename = is_enum_flags_enum_type_t<enum_type>> \
310 OPERATOR_OP (enum_type &&e1, enum_type e2) = delete; \
312 /* Delete compound assignment from unrelated types. */ \
314 template <typename enum_type, typename other_enum_type, \
315 typename = is_enum_flags_enum_type_t<enum_type>> \
316 constexpr enum_type & \
317 OPERATOR_OP (enum_type &e1, other_enum_type e2) = delete; \
319 template <typename enum_type, typename other_enum_type, \
320 typename = is_enum_flags_enum_type_t<enum_type>> \
322 OPERATOR_OP (enum_type &&e1, other_enum_type e2) = delete;
324 ENUM_FLAGS_GEN_BINOP (operator|, |)
325 ENUM_FLAGS_GEN_BINOP (operator&, &)
326 ENUM_FLAGS_GEN_BINOP (operator^, ^)
328 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator|=, |)
329 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator&=, &)
330 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator^=, ^)
332 /* Allow comparison with enum_flags, raw enum, and integers, only.
333 The latter case allows "== 0". As side effect, it allows comparing
334 with integer variables too, but that's not a common mistake to
335 make. It's important to disable comparison with unrelated types to
336 prevent accidentally comparing with unrelated enum values, which
337 are convertible to integer, and thus coupled with enum_flags
338 convertion to underlying type too, would trigger the built-in 'bool
339 operator==(unsigned, int)' operator. */
341 #define ENUM_FLAGS_GEN_COMP(OPERATOR_OP, OP) \
343 /* enum_flags OP enum_flags */ \
345 template <typename enum_type> \
347 OPERATOR_OP (enum_flags<enum_type> lhs, enum_flags<enum_type> rhs) \
348 { return lhs.raw () OP rhs.raw (); } \
350 /* enum_flags OP other */ \
352 template <typename enum_type> \
354 OPERATOR_OP (enum_flags<enum_type> lhs, enum_type rhs) \
355 { return lhs.raw () OP rhs; } \
357 template <typename enum_type> \
359 OPERATOR_OP (enum_flags<enum_type> lhs, int rhs) \
360 { return lhs.raw () OP rhs; } \
362 template <typename enum_type, typename U> \
364 OPERATOR_OP (enum_flags<enum_type> lhs, U rhs) = delete; \
366 /* other OP enum_flags */ \
368 template <typename enum_type> \
370 OPERATOR_OP (enum_type lhs, enum_flags<enum_type> rhs) \
371 { return lhs OP rhs.raw (); } \
373 template <typename enum_type> \
375 OPERATOR_OP (int lhs, enum_flags<enum_type> rhs) \
376 { return lhs OP rhs.raw (); } \
378 template <typename enum_type, typename U> \
380 OPERATOR_OP (U lhs, enum_flags<enum_type> rhs) = delete;
382 ENUM_FLAGS_GEN_COMP (operator==, ==)
383 ENUM_FLAGS_GEN_COMP (operator!=, !=)
385 /* Unary operators for the raw flags enum. */
387 /* We require underlying type to be unsigned when using operator~ --
388 if it were not unsigned, undefined behavior could result. However,
389 asserting this in the class itself would require too many
390 unnecessary changes to usages of otherwise OK enum types. */
391 template <typename enum_type
,
392 typename
= is_enum_flags_enum_type_t
<enum_type
>,
394 = gdb::Requires
<enum_flags_detail::EnumIsUnsigned
<enum_type
>>>
396 operator~ (enum_type e
)
398 using underlying
= typename enum_flags
<enum_type
>::underlying_type
;
399 return (enum_type
) ~underlying (e
);
402 template <typename enum_type
,
403 typename
= is_enum_flags_enum_type_t
<enum_type
>,
404 typename
= gdb::Requires
<enum_flags_detail::EnumIsSigned
<enum_type
>>>
405 constexpr void operator~ (enum_type e
) = delete;
407 template <typename enum_type
,
408 typename
= is_enum_flags_enum_type_t
<enum_type
>,
410 = gdb::Requires
<enum_flags_detail::EnumIsUnsigned
<enum_type
>>>
411 constexpr enum_flags
<enum_type
>
412 operator~ (enum_flags
<enum_type
> e
)
414 using underlying
= typename enum_flags
<enum_type
>::underlying_type
;
415 return (enum_type
) ~underlying (e
);
418 template <typename enum_type
,
419 typename
= is_enum_flags_enum_type_t
<enum_type
>,
420 typename
= gdb::Requires
<enum_flags_detail::EnumIsSigned
<enum_type
>>>
421 constexpr void operator~ (enum_flags
<enum_type
> e
) = delete;
423 /* Delete operator<< and operator>>. */
425 template <typename enum_type
, typename any_type
,
426 typename
= is_enum_flags_enum_type_t
<enum_type
>>
427 void operator<< (const enum_type
&, const any_type
&) = delete;
429 template <typename enum_type
, typename any_type
,
430 typename
= is_enum_flags_enum_type_t
<enum_type
>>
431 void operator<< (const enum_flags
<enum_type
> &, const any_type
&) = delete;
433 template <typename enum_type
, typename any_type
,
434 typename
= is_enum_flags_enum_type_t
<enum_type
>>
435 void operator>> (const enum_type
&, const any_type
&) = delete;
437 template <typename enum_type
, typename any_type
,
438 typename
= is_enum_flags_enum_type_t
<enum_type
>>
439 void operator>> (const enum_flags
<enum_type
> &, const any_type
&) = delete;
444 enum_flags
<E
>::to_string (const string_mapping (&mapping
)[N
]) const
446 enum_type flags
= raw ();
447 std::string res
= hex_string (flags
);
450 bool need_space
= false;
451 for (const auto &entry
: mapping
)
453 if ((flags
& entry
.flag
) != 0)
455 /* Work with an unsigned version of the underlying type,
456 because if enum_type's underlying type is signed, op~
457 won't be defined for it, and, bitwise operations on
458 signed types are implementation defined. */
459 using uns
= typename
std::make_unsigned
<underlying_type
>::type
;
460 flags
&= (enum_type
) ~(uns
) entry
.flag
;
470 /* If there were flags not included in the mapping, print them as
476 res
+= hex_string (flags
);
484 #else /* __cplusplus */
486 /* In C, the flags type is just a typedef for the enum type. */
488 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
489 typedef enum_type flags_type
491 #endif /* __cplusplus */
493 #endif /* COMMON_ENUM_FLAGS_H */