From: Andrew Burgess <andrew.burgess@embecosm.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 1/8] gdbsupport: Provide global operators |=, &=, and ^= for enum bit flags
Date: Thu, 13 Aug 2020 13:58:38 +0100 [thread overview]
Message-ID: <8eb7f409ca572c526de08ab23878be905f5fef42.1597319264.git.andrew.burgess@embecosm.com> (raw)
In-Reply-To: <cover.1597319264.git.andrew.burgess@embecosm.com>
The current enum bit flags mechanism provides global operators &, |,
^, ~, but does not provide &=, |=, ^=.
The implementation for one of these, |=, would look like this:
template <typename enum_type>
typename enum_flags_type<enum_type>::type &
operator|= (enum_type &e1, enum_type e2)
{
e1 = enum_flags<enum_type> (e1) | e2;
return e1;
}
Then if we create a test within GDB like this:
enum some_flag
{
flag_val1 = 1 << 1,
flag_val2 = 1 << 2,
flag_val3 = 1 << 3,
flag_val4 = 1 << 4,
};
DEF_ENUM_FLAGS_TYPE(enum some_flag, some_flags);
...
enum some_flag f = flag_val1 | flag_val2;
f |= flag_val3;
Initially this didn't compile, with an error like:
..../gdbsupport/enum-flags.h: In instantiation of ‘typename enum_flags_type<T>::type& operator|=(enum_type&, enum_type) [with enum_type = some_flag; typename enum_flags_type<T>::type = enum_flags<some_flag>]’:
..../gdb/main.c:168:8: required from here
..../gdbsupport/enum-flags.h:217:10: error: cannot bind non-const lvalue reference of type ‘enum_flags_type<some_flag>::type&’ {aka ‘enum_flags<some_flag>&’} to an rvalue of type ‘enum_flags_type<some_flag>::type’ {aka ‘enum_flags<some_flag>’}
217 | return e1;
| ^~
..../gdbsupport/enum-flags.h:125:3: note: after user-defined conversion: ‘enum_flags<E>::enum_flags(enum_flags<E>::enum_type) [with E = some_flag; enum_flags<E>::enum_type = some_flag]’
125 | enum_flags (enum_type e)
| ^~~~~~~~~~
If we look at enum_flags_type, which is used in the return type of our
operator, it looks like this:
template<>
struct enum_flags_type<enum_type>
{
typedef enum_flags<enum_type> type;
}
So, with `typename' and `typedef' resolved, our new operator actually
compiled like this:
template <typename enum_type>
enum_flags<enum_type> &
operator|= (enum_type &e1, enum_type e2)
{
e1 = enum_flags<enum_type> (e1) | e2;
return e1;
}
And I think this makes the problem clearer (maybe), the return type
reference is different to the argument types, so the compiler is
forced to try and create a temporary (the result of converting `e1' to
type `enum_flags<enum_type>') and return a reference to that temporary.
What I'd like is for the return type to match the arguments, and this
can be achieved I think by changing the enum_flags_type definition to
this:
template<>
struct enum_flags_type<enum_type>
{
typedef enum_type type;
}
Now, once the `typename' and `typedef' are resolved, our new operator
compiles as this:
template <typename enum_type>
enum_type &
operator|= (enum_type &e1, enum_type e2)
{
e1 = enum_flags<enum_type> (e1) | e2;
return e1;
}
And indeed our original example now compiles fine.
The final thing we must take care to check is that the global
operators are not now picking up _all_ enums, after all, this is part
of what the enum-flags.h file does for us, bitwise operators where we
ask for it, and not otherwise.
A manual test on my demo above (with some_flags) confirms that
removing the DEF_ENUM_FLAGS_TYPE line still prevents the code from
compiling as we'd hope, this is because, just like before
`enum_flags_type<enum_type>::type' is only defined for the enums that
have DEF_ENUM_FLAGS_TYPE applied to them.
gdbsupport/ChangeLog:
* enum-flags.h (DEF_ENUM_FLAGS_TYPE): Update header comment, and
define enum_flags_type<enum_type>::type differently.
(operator|=): New.
(operator&=): New.
(operator^=): New.
---
gdbsupport/ChangeLog | 8 ++++++++
gdbsupport/enum-flags.h | 30 +++++++++++++++++++++++++++---
2 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/gdbsupport/enum-flags.h b/gdbsupport/enum-flags.h
index 825ff4faf2c..78db3c7d88e 100644
--- a/gdbsupport/enum-flags.h
+++ b/gdbsupport/enum-flags.h
@@ -55,15 +55,15 @@
instantiating for non-flag enums. */
template<typename T> struct enum_flags_type {};
-/* Use this to mark an enum as flags enum. It defines FLAGS as
+/* Use this to mark an enum as flags enum. It defines FLAGS_TYPE as
enum_flags wrapper class for ENUM, and enables the global operator
- overloads for ENUM. */
+ overloads for ENUM_TYPE. */
#define DEF_ENUM_FLAGS_TYPE(enum_type, flags_type) \
typedef enum_flags<enum_type> flags_type; \
template<> \
struct enum_flags_type<enum_type> \
{ \
- typedef enum_flags<enum_type> type; \
+ typedef enum_type type; \
}
/* Until we can rely on std::underlying type being universally
@@ -209,6 +209,30 @@ operator~ (enum_type e)
return ~enum_flags<enum_type> (e);
}
+template <typename enum_type>
+typename enum_flags_type<enum_type>::type
+operator|= (enum_type &e1, const enum_type &e2)
+{
+ e1 = enum_flags<enum_type> (e1) | e2;
+ return e1;
+}
+
+template <typename enum_type>
+typename enum_flags_type<enum_type>::type
+operator&= (enum_type &e1, const enum_type &e2)
+{
+ e1 = enum_flags<enum_type> (e1) & e2;
+ return e1;
+}
+
+template <typename enum_type>
+typename enum_flags_type<enum_type>::type
+operator^= (enum_type &e1, const enum_type &e2)
+{
+ e1 = enum_flags<enum_type> (e1) ^ e2;
+ return e1;
+}
+
#else /* __cplusplus */
/* In C, the flags type is just a typedef for the enum type. */
--
2.25.4
next prev parent reply other threads:[~2020-08-13 12:58 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-08-13 12:58 [PATCH 0/8] Fortran Array Slicing and Striding Support Andrew Burgess
2020-08-13 12:58 ` Andrew Burgess [this message]
2020-08-15 17:16 ` [PATCH 1/8] gdbsupport: Provide global operators |=, &=, and ^= for enum bit flags Tom Tromey
2020-08-16 9:13 ` Andrew Burgess
2020-08-17 10:40 ` Andrew Burgess
2020-08-20 16:00 ` Pedro Alves
2020-08-21 14:49 ` Pedro Alves
2020-08-21 15:57 ` Andrew Burgess
2020-08-21 18:10 ` Pedro Alves
2020-08-13 12:58 ` [PATCH 2/8] gdbsupport: Make function arguments constant in enum-flags.h Andrew Burgess
2020-08-15 19:45 ` Tom Tromey
2020-08-16 9:08 ` Andrew Burgess
2020-08-13 12:58 ` [PATCH 3/8] gdb/fortran: Clean up array/string expression evaluation Andrew Burgess
2020-08-13 12:58 ` [PATCH 4/8] gdb/fortran: Move Fortran expression handling into f-lang.c Andrew Burgess
2020-08-13 12:58 ` [PATCH 5/8] gdb/fortran: Change whitespace when printing arrays Andrew Burgess
2020-08-13 12:58 ` [PATCH 6/8] gdb: Convert enum range_type to a bit field enum Andrew Burgess
2020-08-13 12:58 ` [PATCH 7/8] gdb/testsuite: Add missing expected results Andrew Burgess
2020-08-13 12:58 ` [PATCH 8/8] gdb/fortran: Add support for Fortran array slices at the GDB prompt Andrew Burgess
2020-08-13 13:31 ` Eli Zaretskii
2020-08-26 14:49 ` [PATCHv2 00/10] Fortran Array Slicing and Striding Support Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 01/10] Rewrite valid-expr.h's internals in terms of the detection idiom (C++17/N4502) Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 02/10] Use type_instance_flags more throughout Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 03/10] Rewrite enum_flags, add unit tests, fix problems Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 04/10] gdb: additional changes to make use of type_instance_flags more Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 05/10] gdb/fortran: Clean up array/string expression evaluation Andrew Burgess
2020-09-19 8:53 ` Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 06/10] gdb/fortran: Move Fortran expression handling into f-lang.c Andrew Burgess
2020-09-19 8:53 ` Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 07/10] gdb/fortran: Change whitespace when printing arrays Andrew Burgess
2020-09-19 8:54 ` Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 08/10] gdb: Convert enum range_type to a bit field enum Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 09/10] gdb/testsuite: Add missing expected results Andrew Burgess
2020-09-18 9:53 ` Andrew Burgess
2020-08-26 14:49 ` [PATCHv2 10/10] gdb/fortran: Add support for Fortran array slices at the GDB prompt Andrew Burgess
2020-08-26 17:02 ` Eli Zaretskii
2020-09-19 9:47 ` [PATCHv3 0/2] Fortran Array Slicing and Striding Support Andrew Burgess
2020-09-19 9:48 ` [PATCHv3 1/2] gdb: Convert enum range_type to a bit field enum Andrew Burgess
2020-09-19 13:50 ` Simon Marchi
2020-09-19 9:48 ` [PATCHv3 2/2] gdb/fortran: Add support for Fortran array slices at the GDB prompt Andrew Burgess
2020-09-19 10:03 ` Eli Zaretskii
2020-09-28 9:40 ` [PATCHv4 0/3] Fortran Array Slicing and Striding Support Andrew Burgess
2020-09-28 9:40 ` [PATCHv4 1/3] gdb: Convert enum range_type to a bit field enum Andrew Burgess
2020-09-28 9:40 ` [PATCHv4 2/3] gdb: rename 'enum range_type' to 'enum range_flag' Andrew Burgess
2020-09-28 9:40 ` [PATCHv4 3/3] gdb/fortran: Add support for Fortran array slices at the GDB prompt Andrew Burgess
2020-09-28 9:52 ` Eli Zaretskii via Gdb-patches
2020-10-11 18:12 ` [PATCHv5 0/4] Fortran Array Slicing and Striding Support Andrew Burgess
2020-10-11 18:12 ` [PATCHv5 1/4] gdb: Convert enum range_type to a bit field enum Andrew Burgess
2020-10-20 20:16 ` Tom Tromey
2020-10-11 18:12 ` [PATCHv5 2/4] gdb: rename 'enum range_type' to 'enum range_flag' Andrew Burgess
2020-10-20 20:16 ` Tom Tromey
2020-10-11 18:12 ` [PATCHv5 3/4] gdb/fortran: add support for parsing array strides in expressions Andrew Burgess
2020-10-12 13:21 ` Simon Marchi
2020-10-20 20:17 ` Tom Tromey
2020-10-22 10:42 ` Andrew Burgess
2020-10-11 18:12 ` [PATCHv5 4/4] gdb/fortran: Add support for Fortran array slices at the GDB prompt Andrew Burgess
2020-10-12 14:10 ` Simon Marchi
2020-10-20 20:45 ` Tom Tromey
2020-10-29 11:08 ` Andrew Burgess
2020-10-31 22:16 ` [PATCHv6] " Andrew Burgess
2020-11-12 12:09 ` Andrew Burgess
2020-11-12 18:58 ` Tom Tromey
2020-11-19 11:56 ` Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8eb7f409ca572c526de08ab23878be905f5fef42.1597319264.git.andrew.burgess@embecosm.com \
--to=andrew.burgess@embecosm.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox