Automatic date update in version.in
[binutils-gdb.git] / gdbsupport / enum-flags.h
blob50780043477c72292e16692381d8477140eebd67
1 /* Copyright (C) 2015-2024 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
21 #include "traits.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
28 to 'f':
30 enum some_flag
32 flag_val1 = 1 << 1,
33 flag_val2 = 1 << 2,
34 flag_val3 = 1 << 3,
35 flag_val4 = 1 << 4,
37 DEF_ENUM_FLAGS_TYPE(enum some_flag, some_flags);
39 some_flags f = flag_val1 | flag_val2;
40 f |= flag_val3;
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:
46 some_flags f = 0;
47 f |= flag_val3 | flag_val4;
49 Note that literal integers other than zero fail to compile:
51 some_flags f = 1; // error
54 #ifdef __cplusplus
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
80 int. */
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; };
91 template<typename T>
92 struct enum_underlying_type
94 DIAGNOSTIC_PUSH
95 DIAGNOSTIC_IGNORE_ENUM_CONSTEXPR_CONVERSION
96 typedef typename
97 integer_for_size<sizeof (T), static_cast<bool>(T (-1) < T (0))>::type
98 type;
99 DIAGNOSTIC_POP
102 namespace enum_flags_detail
105 /* Private type used to support initializing flag types with zero:
107 foo_flags f = 0;
109 but not other integers:
111 foo_flags f = 1;
113 The way this works is that we define an implicit constructor that
114 takes a pointer to this private type. Since nothing can
115 instantiate an object of this type, the only possible pointer to
116 pass to the constructor is the NULL pointer, or, zero. */
117 struct zero_type;
119 /* gdb::Requires trait helpers. */
120 template <typename enum_type>
121 using EnumIsUnsigned
122 = std::is_unsigned<typename enum_underlying_type<enum_type>::type>;
123 template <typename enum_type>
124 using EnumIsSigned
125 = std::is_signed<typename enum_underlying_type<enum_type>::type>;
129 template <typename E>
130 class enum_flags
132 public:
133 typedef E enum_type;
134 typedef typename enum_underlying_type<enum_type>::type underlying_type;
136 /* For to_string. Maps one enumerator of E to a string. */
137 struct string_mapping
139 E flag;
140 const char *str;
143 /* Convenience for to_string implementations, to build a
144 string_mapping array. */
145 #define MAP_ENUM_FLAG(ENUM_FLAG) { ENUM_FLAG, #ENUM_FLAG }
147 public:
148 /* Allow default construction. */
149 constexpr enum_flags ()
150 : m_enum_value ((enum_type) 0)
153 /* The default move/copy ctor/assignment do the right thing. */
155 /* If you get an error saying these two overloads are ambiguous,
156 then you tried to mix values of different enum types. */
157 constexpr enum_flags (enum_type e)
158 : m_enum_value (e)
160 constexpr enum_flags (enum_flags_detail::zero_type *zero)
161 : m_enum_value ((enum_type) 0)
164 enum_flags &operator&= (enum_flags e) &
166 m_enum_value = (enum_type) (m_enum_value & e.m_enum_value);
167 return *this;
169 enum_flags &operator|= (enum_flags e) &
171 m_enum_value = (enum_type) (m_enum_value | e.m_enum_value);
172 return *this;
174 enum_flags &operator^= (enum_flags e) &
176 m_enum_value = (enum_type) (m_enum_value ^ e.m_enum_value);
177 return *this;
180 /* Delete rval versions. */
181 void operator&= (enum_flags e) && = delete;
182 void operator|= (enum_flags e) && = delete;
183 void operator^= (enum_flags e) && = delete;
185 /* Like raw enums, allow conversion to the underlying type. */
186 constexpr operator underlying_type () const
188 return m_enum_value;
191 /* Get the underlying value as a raw enum. */
192 constexpr enum_type raw () const
194 return m_enum_value;
197 /* Binary operations involving some unrelated type (which would be a
198 bug) are implemented as non-members, and deleted. */
200 /* Convert this object to a std::string, using MAPPING as
201 enumerator-to-string mapping array. This is not meant to be
202 called directly. Instead, enum_flags specializations should have
203 their own to_string function wrapping this one, thus hiding the
204 mapping array from callers.
206 Note: this is defined outside the template class so it can use
207 the global operators for enum_type, which are only defined after
208 the template class. */
209 template<size_t N>
210 std::string to_string (const string_mapping (&mapping)[N]) const;
212 private:
213 /* Stored as enum_type because GDB knows to print the bit flags
214 neatly if the enum values look like bit flags. */
215 enum_type m_enum_value;
218 template <typename E>
219 using is_enum_flags_enum_type_t
220 = decltype (is_enum_flags_enum_type (std::declval<E *> ()));
222 /* Global operator overloads. */
224 /* Generate binary operators. */
226 #define ENUM_FLAGS_GEN_BINOP(OPERATOR_OP, OP) \
228 /* Raw enum on both LHS/RHS. Returns raw enum type. */ \
229 template <typename enum_type, \
230 typename = is_enum_flags_enum_type_t<enum_type>> \
231 constexpr enum_type \
232 OPERATOR_OP (enum_type e1, enum_type e2) \
234 using underlying = typename enum_flags<enum_type>::underlying_type; \
235 return (enum_type) (underlying (e1) OP underlying (e2)); \
238 /* enum_flags on the LHS. */ \
239 template <typename enum_type, \
240 typename = is_enum_flags_enum_type_t<enum_type>> \
241 constexpr enum_flags<enum_type> \
242 OPERATOR_OP (enum_flags<enum_type> e1, enum_type e2) \
243 { return e1.raw () OP e2; } \
245 /* enum_flags on the RHS. */ \
246 template <typename enum_type, \
247 typename = is_enum_flags_enum_type_t<enum_type>> \
248 constexpr enum_flags<enum_type> \
249 OPERATOR_OP (enum_type e1, enum_flags<enum_type> e2) \
250 { return e1 OP e2.raw (); } \
252 /* enum_flags on both LHS/RHS. */ \
253 template <typename enum_type, \
254 typename = is_enum_flags_enum_type_t<enum_type>> \
255 constexpr enum_flags<enum_type> \
256 OPERATOR_OP (enum_flags<enum_type> e1, enum_flags<enum_type> e2) \
257 { return e1.raw () OP e2.raw (); } \
259 /* Delete cases involving unrelated types. */ \
261 template <typename enum_type, typename unrelated_type, \
262 typename = is_enum_flags_enum_type_t<enum_type>> \
263 constexpr enum_flags<enum_type> \
264 OPERATOR_OP (enum_type e1, unrelated_type e2) = delete; \
266 template <typename enum_type, typename unrelated_type, \
267 typename = is_enum_flags_enum_type_t<enum_type>> \
268 constexpr enum_flags<enum_type> \
269 OPERATOR_OP (unrelated_type e1, enum_type e2) = delete; \
271 template <typename enum_type, typename unrelated_type, \
272 typename = is_enum_flags_enum_type_t<enum_type>> \
273 constexpr enum_flags<enum_type> \
274 OPERATOR_OP (enum_flags<enum_type> e1, unrelated_type e2) = delete; \
276 template <typename enum_type, typename unrelated_type, \
277 typename = is_enum_flags_enum_type_t<enum_type>> \
278 constexpr enum_flags<enum_type> \
279 OPERATOR_OP (unrelated_type e1, enum_flags<enum_type> e2) = delete;
281 /* Generate non-member compound assignment operators. Only the raw
282 enum versions are defined here. The enum_flags versions are
283 defined as member functions, simply because it's less code that
284 way.
286 Note we delete operators that would allow e.g.,
288 "enum_type | 1" or "enum_type1 | enum_type2"
290 because that would allow a mistake like :
291 enum flags1 { F1_FLAGS1 = 1 };
292 enum flags2 { F2_FLAGS2 = 2 };
293 enum flags1 val;
294 switch (val) {
295 case F1_FLAGS1 | F2_FLAGS2:
298 If you really need to 'or' enumerators of different flag types,
299 cast to integer first.
301 #define ENUM_FLAGS_GEN_COMPOUND_ASSIGN(OPERATOR_OP, OP) \
302 /* lval reference version. */ \
303 template <typename enum_type, \
304 typename = is_enum_flags_enum_type_t<enum_type>> \
305 constexpr enum_type & \
306 OPERATOR_OP (enum_type &e1, enum_type e2) \
307 { return e1 = e1 OP e2; } \
309 /* rval reference version. */ \
310 template <typename enum_type, \
311 typename = is_enum_flags_enum_type_t<enum_type>> \
312 void \
313 OPERATOR_OP (enum_type &&e1, enum_type e2) = delete; \
315 /* Delete compound assignment from unrelated types. */ \
317 template <typename enum_type, typename other_enum_type, \
318 typename = is_enum_flags_enum_type_t<enum_type>> \
319 constexpr enum_type & \
320 OPERATOR_OP (enum_type &e1, other_enum_type e2) = delete; \
322 template <typename enum_type, typename other_enum_type, \
323 typename = is_enum_flags_enum_type_t<enum_type>> \
324 void \
325 OPERATOR_OP (enum_type &&e1, other_enum_type e2) = delete;
327 ENUM_FLAGS_GEN_BINOP (operator|, |)
328 ENUM_FLAGS_GEN_BINOP (operator&, &)
329 ENUM_FLAGS_GEN_BINOP (operator^, ^)
331 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator|=, |)
332 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator&=, &)
333 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator^=, ^)
335 /* Allow comparison with enum_flags, raw enum, and integers, only.
336 The latter case allows "== 0". As side effect, it allows comparing
337 with integer variables too, but that's not a common mistake to
338 make. It's important to disable comparison with unrelated types to
339 prevent accidentally comparing with unrelated enum values, which
340 are convertible to integer, and thus coupled with enum_flags
341 conversion to underlying type too, would trigger the built-in 'bool
342 operator==(unsigned, int)' operator. */
344 #define ENUM_FLAGS_GEN_COMP(OPERATOR_OP, OP) \
346 /* enum_flags OP enum_flags */ \
348 template <typename enum_type> \
349 constexpr bool \
350 OPERATOR_OP (enum_flags<enum_type> lhs, enum_flags<enum_type> rhs) \
351 { return lhs.raw () OP rhs.raw (); } \
353 /* enum_flags OP other */ \
355 template <typename enum_type> \
356 constexpr bool \
357 OPERATOR_OP (enum_flags<enum_type> lhs, enum_type rhs) \
358 { return lhs.raw () OP rhs; } \
360 template <typename enum_type> \
361 constexpr bool \
362 OPERATOR_OP (enum_flags<enum_type> lhs, int rhs) \
363 { return lhs.raw () OP rhs; } \
365 template <typename enum_type, typename U> \
366 constexpr bool \
367 OPERATOR_OP (enum_flags<enum_type> lhs, U rhs) = delete; \
369 /* other OP enum_flags */ \
371 template <typename enum_type> \
372 constexpr bool \
373 OPERATOR_OP (enum_type lhs, enum_flags<enum_type> rhs) \
374 { return lhs OP rhs.raw (); } \
376 template <typename enum_type> \
377 constexpr bool \
378 OPERATOR_OP (int lhs, enum_flags<enum_type> rhs) \
379 { return lhs OP rhs.raw (); } \
381 template <typename enum_type, typename U> \
382 constexpr bool \
383 OPERATOR_OP (U lhs, enum_flags<enum_type> rhs) = delete;
385 ENUM_FLAGS_GEN_COMP (operator==, ==)
386 ENUM_FLAGS_GEN_COMP (operator!=, !=)
388 /* Unary operators for the raw flags enum. */
390 /* We require underlying type to be unsigned when using operator~ --
391 if it were not unsigned, undefined behavior could result. However,
392 asserting this in the class itself would require too many
393 unnecessary changes to usages of otherwise OK enum types. */
394 template <typename enum_type,
395 typename = is_enum_flags_enum_type_t<enum_type>,
396 typename
397 = gdb::Requires<enum_flags_detail::EnumIsUnsigned<enum_type>>>
398 constexpr enum_type
399 operator~ (enum_type e)
401 using underlying = typename enum_flags<enum_type>::underlying_type;
402 return (enum_type) ~underlying (e);
405 template <typename enum_type,
406 typename = is_enum_flags_enum_type_t<enum_type>,
407 typename = gdb::Requires<enum_flags_detail::EnumIsSigned<enum_type>>>
408 constexpr void operator~ (enum_type e) = delete;
410 template <typename enum_type,
411 typename = is_enum_flags_enum_type_t<enum_type>,
412 typename
413 = gdb::Requires<enum_flags_detail::EnumIsUnsigned<enum_type>>>
414 constexpr enum_flags<enum_type>
415 operator~ (enum_flags<enum_type> e)
417 using underlying = typename enum_flags<enum_type>::underlying_type;
418 return (enum_type) ~underlying (e);
421 template <typename enum_type,
422 typename = is_enum_flags_enum_type_t<enum_type>,
423 typename = gdb::Requires<enum_flags_detail::EnumIsSigned<enum_type>>>
424 constexpr void operator~ (enum_flags<enum_type> e) = delete;
426 /* Delete operator<< and operator>>. */
428 template <typename enum_type, typename any_type,
429 typename = is_enum_flags_enum_type_t<enum_type>>
430 void operator<< (const enum_type &, const any_type &) = delete;
432 template <typename enum_type, typename any_type,
433 typename = is_enum_flags_enum_type_t<enum_type>>
434 void operator<< (const enum_flags<enum_type> &, const any_type &) = delete;
436 template <typename enum_type, typename any_type,
437 typename = is_enum_flags_enum_type_t<enum_type>>
438 void operator>> (const enum_type &, const any_type &) = delete;
440 template <typename enum_type, typename any_type,
441 typename = is_enum_flags_enum_type_t<enum_type>>
442 void operator>> (const enum_flags<enum_type> &, const any_type &) = delete;
444 template<typename E>
445 template<size_t N>
446 std::string
447 enum_flags<E>::to_string (const string_mapping (&mapping)[N]) const
449 enum_type flags = raw ();
450 std::string res = hex_string (flags);
451 res += " [";
453 bool need_space = false;
454 for (const auto &entry : mapping)
456 if ((flags & entry.flag) != 0)
458 /* Work with an unsigned version of the underlying type,
459 because if enum_type's underlying type is signed, op~
460 won't be defined for it, and, bitwise operations on
461 signed types are implementation defined. */
462 using uns = typename std::make_unsigned<underlying_type>::type;
463 flags &= (enum_type) ~(uns) entry.flag;
465 if (need_space)
466 res += " ";
467 res += entry.str;
469 need_space = true;
473 /* If there were flags not included in the mapping, print them as
474 a hex number. */
475 if (flags != 0)
477 if (need_space)
478 res += " ";
479 res += hex_string (flags);
482 res += "]";
484 return res;
487 #else /* __cplusplus */
489 /* In C, the flags type is just a typedef for the enum type. */
491 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
492 typedef enum_type flags_type
494 #endif /* __cplusplus */
496 #endif /* COMMON_ENUM_FLAGS_H */