Automatic date update in version.in
[binutils-gdb.git] / gdbsupport / enum-flags.h
blob764d521966376682dfa1707d5c95b6b5b9498025
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 /* Use this to mark an enum as flags enum. It defines FLAGS_TYPE as
55 enum_flags wrapper class for ENUM, and enables the global operator
56 overloads for ENUM. */
57 #define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
58 using flags_type = enum_flags<enum_type>; \
59 void is_enum_flags_enum_type (enum_type *)
61 /* To enable the global enum_flags operators for enum, declare an
62 "is_enum_flags_enum_type" overload that has exactly one parameter,
63 of type a pointer to that enum class. E.g.,:
65 void is_enum_flags_enum_type (enum some_flag *);
67 The function does not need to be defined, only declared.
68 DEF_ENUM_FLAGS_TYPE declares this.
70 A function declaration is preferred over a traits type, because the
71 former allows calling the DEF_ENUM_FLAGS_TYPE macro inside a
72 namespace to define the corresponding enum flags type in that
73 namespace. The compiler finds the corresponding
74 is_enum_flags_enum_type function via ADL. */
76 /* Note that std::underlying_type<enum_type> is not what we want here,
77 since that returns unsigned int even when the enum decays to signed
78 int. */
79 template<int size, bool sign> class integer_for_size { using type = void; };
80 template<> struct integer_for_size<1, 0> { using type = uint8_t; };
81 template<> struct integer_for_size<2, 0> { using type = uint16_t; };
82 template<> struct integer_for_size<4, 0> { using type = uint32_t; };
83 template<> struct integer_for_size<8, 0> { using type = uint64_t; };
84 template<> struct integer_for_size<1, 1> { using type = int8_t; };
85 template<> struct integer_for_size<2, 1> { using type = int16_t; };
86 template<> struct integer_for_size<4, 1> { using type = int32_t; };
87 template<> struct integer_for_size<8, 1> { using type = int64_t; };
89 template<typename T>
90 struct enum_underlying_type
92 DIAGNOSTIC_PUSH
93 DIAGNOSTIC_IGNORE_ENUM_CONSTEXPR_CONVERSION
94 using type
95 = typename integer_for_size<sizeof (T),
96 static_cast<bool>(T (-1) < T (0))>::type;
97 DIAGNOSTIC_POP
100 namespace enum_flags_detail
103 /* Private type used to support initializing flag types with zero:
105 foo_flags f = 0;
107 but not other integers:
109 foo_flags f = 1;
111 The way this works is that we define an implicit constructor that
112 takes a pointer to this private type. Since nothing can
113 instantiate an object of this type, the only possible pointer to
114 pass to the constructor is the NULL pointer, or, zero. */
115 struct zero_type;
117 /* gdb::Requires trait helpers. */
118 template <typename enum_type>
119 using EnumIsUnsigned
120 = std::is_unsigned<typename enum_underlying_type<enum_type>::type>;
121 template <typename enum_type>
122 using EnumIsSigned
123 = std::is_signed<typename enum_underlying_type<enum_type>::type>;
127 template <typename E>
128 class enum_flags
130 public:
131 using enum_type = E;
132 using underlying_type = typename enum_underlying_type<enum_type>::type;
134 /* For to_string. Maps one enumerator of E to a string. */
135 struct string_mapping
137 E flag;
138 const char *str;
141 /* Convenience for to_string implementations, to build a
142 string_mapping array. */
143 #define MAP_ENUM_FLAG(ENUM_FLAG) { ENUM_FLAG, #ENUM_FLAG }
145 public:
146 /* Allow default construction. */
147 constexpr enum_flags ()
148 : m_enum_value ((enum_type) 0)
151 /* The default move/copy ctor/assignment do the right thing. */
153 /* If you get an error saying these two overloads are ambiguous,
154 then you tried to mix values of different enum types. */
155 constexpr enum_flags (enum_type e)
156 : m_enum_value (e)
158 constexpr enum_flags (enum_flags_detail::zero_type *zero)
159 : m_enum_value ((enum_type) 0)
162 enum_flags &operator&= (enum_flags e) &
164 m_enum_value = (enum_type) (m_enum_value & e.m_enum_value);
165 return *this;
167 enum_flags &operator|= (enum_flags e) &
169 m_enum_value = (enum_type) (m_enum_value | e.m_enum_value);
170 return *this;
172 enum_flags &operator^= (enum_flags e) &
174 m_enum_value = (enum_type) (m_enum_value ^ e.m_enum_value);
175 return *this;
178 /* Delete rval versions. */
179 void operator&= (enum_flags e) && = delete;
180 void operator|= (enum_flags e) && = delete;
181 void operator^= (enum_flags e) && = delete;
183 /* Like raw enums, allow conversion to the underlying type. */
184 constexpr operator underlying_type () const
186 return m_enum_value;
189 /* Get the underlying value as a raw enum. */
190 constexpr enum_type raw () const
192 return m_enum_value;
195 /* Binary operations involving some unrelated type (which would be a
196 bug) are implemented as non-members, and deleted. */
198 /* Convert this object to a std::string, using MAPPING as
199 enumerator-to-string mapping array. This is not meant to be
200 called directly. Instead, enum_flags specializations should have
201 their own to_string function wrapping this one, thus hiding the
202 mapping array from callers.
204 Note: this is defined outside the template class so it can use
205 the global operators for enum_type, which are only defined after
206 the template class. */
207 template<size_t N>
208 std::string to_string (const string_mapping (&mapping)[N]) const;
210 private:
211 /* Stored as enum_type because GDB knows to print the bit flags
212 neatly if the enum values look like bit flags. */
213 enum_type m_enum_value;
216 template <typename E>
217 using is_enum_flags_enum_type_t
218 = decltype (is_enum_flags_enum_type (std::declval<E *> ()));
220 /* Global operator overloads. */
222 /* Generate binary operators. */
224 #define ENUM_FLAGS_GEN_BINOP(OPERATOR_OP, OP) \
226 /* Raw enum on both LHS/RHS. Returns raw enum type. */ \
227 template <typename enum_type, \
228 typename = is_enum_flags_enum_type_t<enum_type>> \
229 constexpr enum_type \
230 OPERATOR_OP (enum_type e1, enum_type e2) \
232 using underlying = typename enum_flags<enum_type>::underlying_type; \
233 return (enum_type) (underlying (e1) OP underlying (e2)); \
236 /* enum_flags on the LHS. */ \
237 template <typename enum_type, \
238 typename = is_enum_flags_enum_type_t<enum_type>> \
239 constexpr enum_flags<enum_type> \
240 OPERATOR_OP (enum_flags<enum_type> e1, enum_type e2) \
241 { return e1.raw () OP e2; } \
243 /* enum_flags on the RHS. */ \
244 template <typename enum_type, \
245 typename = is_enum_flags_enum_type_t<enum_type>> \
246 constexpr enum_flags<enum_type> \
247 OPERATOR_OP (enum_type e1, enum_flags<enum_type> e2) \
248 { return e1 OP e2.raw (); } \
250 /* enum_flags on both LHS/RHS. */ \
251 template <typename enum_type, \
252 typename = is_enum_flags_enum_type_t<enum_type>> \
253 constexpr enum_flags<enum_type> \
254 OPERATOR_OP (enum_flags<enum_type> e1, enum_flags<enum_type> e2) \
255 { return e1.raw () OP e2.raw (); } \
257 /* Delete cases involving unrelated types. */ \
259 template <typename enum_type, typename unrelated_type, \
260 typename = is_enum_flags_enum_type_t<enum_type>> \
261 constexpr enum_flags<enum_type> \
262 OPERATOR_OP (enum_type e1, unrelated_type e2) = delete; \
264 template <typename enum_type, typename unrelated_type, \
265 typename = is_enum_flags_enum_type_t<enum_type>> \
266 constexpr enum_flags<enum_type> \
267 OPERATOR_OP (unrelated_type e1, enum_type e2) = delete; \
269 template <typename enum_type, typename unrelated_type, \
270 typename = is_enum_flags_enum_type_t<enum_type>> \
271 constexpr enum_flags<enum_type> \
272 OPERATOR_OP (enum_flags<enum_type> e1, unrelated_type e2) = delete; \
274 template <typename enum_type, typename unrelated_type, \
275 typename = is_enum_flags_enum_type_t<enum_type>> \
276 constexpr enum_flags<enum_type> \
277 OPERATOR_OP (unrelated_type e1, enum_flags<enum_type> e2) = delete;
279 /* Generate non-member compound assignment operators. Only the raw
280 enum versions are defined here. The enum_flags versions are
281 defined as member functions, simply because it's less code that
282 way.
284 Note we delete operators that would allow e.g.,
286 "enum_type | 1" or "enum_type1 | enum_type2"
288 because that would allow a mistake like :
289 enum flags1 { F1_FLAGS1 = 1 };
290 enum flags2 { F2_FLAGS2 = 2 };
291 enum flags1 val;
292 switch (val) {
293 case F1_FLAGS1 | F2_FLAGS2:
296 If you really need to 'or' enumerators of different flag types,
297 cast to integer first.
299 #define ENUM_FLAGS_GEN_COMPOUND_ASSIGN(OPERATOR_OP, OP) \
300 /* lval reference version. */ \
301 template <typename enum_type, \
302 typename = is_enum_flags_enum_type_t<enum_type>> \
303 constexpr enum_type & \
304 OPERATOR_OP (enum_type &e1, enum_type e2) \
305 { return e1 = e1 OP e2; } \
307 /* rval reference version. */ \
308 template <typename enum_type, \
309 typename = is_enum_flags_enum_type_t<enum_type>> \
310 void \
311 OPERATOR_OP (enum_type &&e1, enum_type e2) = delete; \
313 /* Delete compound assignment from unrelated types. */ \
315 template <typename enum_type, typename other_enum_type, \
316 typename = is_enum_flags_enum_type_t<enum_type>> \
317 constexpr enum_type & \
318 OPERATOR_OP (enum_type &e1, other_enum_type e2) = delete; \
320 template <typename enum_type, typename other_enum_type, \
321 typename = is_enum_flags_enum_type_t<enum_type>> \
322 void \
323 OPERATOR_OP (enum_type &&e1, other_enum_type e2) = delete;
325 ENUM_FLAGS_GEN_BINOP (operator|, |)
326 ENUM_FLAGS_GEN_BINOP (operator&, &)
327 ENUM_FLAGS_GEN_BINOP (operator^, ^)
329 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator|=, |)
330 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator&=, &)
331 ENUM_FLAGS_GEN_COMPOUND_ASSIGN (operator^=, ^)
333 /* Allow comparison with enum_flags, raw enum, and integers, only.
334 The latter case allows "== 0". As side effect, it allows comparing
335 with integer variables too, but that's not a common mistake to
336 make. It's important to disable comparison with unrelated types to
337 prevent accidentally comparing with unrelated enum values, which
338 are convertible to integer, and thus coupled with enum_flags
339 conversion to underlying type too, would trigger the built-in 'bool
340 operator==(unsigned, int)' operator. */
342 #define ENUM_FLAGS_GEN_COMP(OPERATOR_OP, OP) \
344 /* enum_flags OP enum_flags */ \
346 template <typename enum_type> \
347 constexpr bool \
348 OPERATOR_OP (enum_flags<enum_type> lhs, enum_flags<enum_type> rhs) \
349 { return lhs.raw () OP rhs.raw (); } \
351 /* enum_flags OP other */ \
353 template <typename enum_type> \
354 constexpr bool \
355 OPERATOR_OP (enum_flags<enum_type> lhs, enum_type rhs) \
356 { return lhs.raw () OP rhs; } \
358 template <typename enum_type> \
359 constexpr bool \
360 OPERATOR_OP (enum_flags<enum_type> lhs, int rhs) \
361 { return lhs.raw () OP rhs; } \
363 template <typename enum_type, typename U> \
364 constexpr bool \
365 OPERATOR_OP (enum_flags<enum_type> lhs, U rhs) = delete; \
367 /* other OP enum_flags */ \
369 template <typename enum_type> \
370 constexpr bool \
371 OPERATOR_OP (enum_type lhs, enum_flags<enum_type> rhs) \
372 { return lhs OP rhs.raw (); } \
374 template <typename enum_type> \
375 constexpr bool \
376 OPERATOR_OP (int lhs, enum_flags<enum_type> rhs) \
377 { return lhs OP rhs.raw (); } \
379 template <typename enum_type, typename U> \
380 constexpr bool \
381 OPERATOR_OP (U lhs, enum_flags<enum_type> rhs) = delete;
383 ENUM_FLAGS_GEN_COMP (operator==, ==)
384 ENUM_FLAGS_GEN_COMP (operator!=, !=)
386 /* Unary operators for the raw flags enum. */
388 /* We require underlying type to be unsigned when using operator~ --
389 if it were not unsigned, undefined behavior could result. However,
390 asserting this in the class itself would require too many
391 unnecessary changes to usages of otherwise OK enum types. */
392 template <typename enum_type,
393 typename = is_enum_flags_enum_type_t<enum_type>,
394 typename
395 = gdb::Requires<enum_flags_detail::EnumIsUnsigned<enum_type>>>
396 constexpr enum_type
397 operator~ (enum_type e)
399 using underlying = typename enum_flags<enum_type>::underlying_type;
400 return (enum_type) ~underlying (e);
403 template <typename enum_type,
404 typename = is_enum_flags_enum_type_t<enum_type>,
405 typename = gdb::Requires<enum_flags_detail::EnumIsSigned<enum_type>>>
406 constexpr void operator~ (enum_type e) = delete;
408 template <typename enum_type,
409 typename = is_enum_flags_enum_type_t<enum_type>,
410 typename
411 = gdb::Requires<enum_flags_detail::EnumIsUnsigned<enum_type>>>
412 constexpr enum_flags<enum_type>
413 operator~ (enum_flags<enum_type> e)
415 using underlying = typename enum_flags<enum_type>::underlying_type;
416 return (enum_type) ~underlying (e);
419 template <typename enum_type,
420 typename = is_enum_flags_enum_type_t<enum_type>,
421 typename = gdb::Requires<enum_flags_detail::EnumIsSigned<enum_type>>>
422 constexpr void operator~ (enum_flags<enum_type> e) = delete;
424 /* Delete operator<< and operator>>. */
426 template <typename enum_type, typename any_type,
427 typename = is_enum_flags_enum_type_t<enum_type>>
428 void operator<< (const enum_type &, const any_type &) = delete;
430 template <typename enum_type, typename any_type,
431 typename = is_enum_flags_enum_type_t<enum_type>>
432 void operator<< (const enum_flags<enum_type> &, const any_type &) = delete;
434 template <typename enum_type, typename any_type,
435 typename = is_enum_flags_enum_type_t<enum_type>>
436 void operator>> (const enum_type &, const any_type &) = delete;
438 template <typename enum_type, typename any_type,
439 typename = is_enum_flags_enum_type_t<enum_type>>
440 void operator>> (const enum_flags<enum_type> &, const any_type &) = delete;
442 template<typename E>
443 template<size_t N>
444 std::string
445 enum_flags<E>::to_string (const string_mapping (&mapping)[N]) const
447 enum_type flags = raw ();
448 std::string res = hex_string (flags);
449 res += " [";
451 bool need_space = false;
452 for (const auto &entry : mapping)
454 if ((flags & entry.flag) != 0)
456 /* Work with an unsigned version of the underlying type,
457 because if enum_type's underlying type is signed, op~
458 won't be defined for it, and, bitwise operations on
459 signed types are implementation defined. */
460 using uns = typename std::make_unsigned<underlying_type>::type;
461 flags &= (enum_type) ~(uns) entry.flag;
463 if (need_space)
464 res += " ";
465 res += entry.str;
467 need_space = true;
471 /* If there were flags not included in the mapping, print them as
472 a hex number. */
473 if (flags != 0)
475 if (need_space)
476 res += " ";
477 res += hex_string (flags);
480 res += "]";
482 return res;
485 #endif /* COMMON_ENUM_FLAGS_H */