From 0ee47b3da388b5ec9f46ab6466c8f24ac0cefb4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 7 Jun 2022 16:35:46 +0200 Subject: [PATCH] bitflags: Make more operations constexpr. --- src/bindgen/ir/structure.rs | 26 ++++++++++++++------ tests/expectations/associated_in_body.cpp | 10 ++++---- tests/expectations/bitflags.cpp | 30 +++++++++++------------ 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/bindgen/ir/structure.rs b/src/bindgen/ir/structure.rs index be199739d..3db3e7a48 100644 --- a/src/bindgen/ir/structure.rs +++ b/src/bindgen/ir/structure.rs @@ -205,6 +205,7 @@ impl Struct { fn emit_bitflags_binop( &self, + constexpr_prefix: &str, operator: char, other: &str, out: &mut SourceWriter, @@ -212,7 +213,8 @@ impl Struct { out.new_line(); write!( out, - "{} operator{}(const {}& {}) const", + "{}{} operator{}(const {}& {}) const", + constexpr_prefix, self.export_name(), operator, self.export_name(), @@ -531,21 +533,31 @@ impl Source for Struct { wrote_start_newline = true; out.new_line(); } + let constexpr_prefix = if config.constant.allow_constexpr { + "constexpr " + } else { + "" + }; + out.new_line(); - write!(out, "explicit operator bool() const"); + write!(out, "{}explicit operator bool() const", constexpr_prefix); out.open_brace(); write!(out, "return !!bits;"); out.close_brace(false); out.new_line(); - write!(out, "{} operator~() const", self.export_name()); + write!( + out, + "{}{} operator~() const", + constexpr_prefix, + self.export_name() + ); out.open_brace(); write!(out, "return {{static_cast(~bits)}};"); out.close_brace(false); - - self.emit_bitflags_binop('|', &other, out); - self.emit_bitflags_binop('&', &other, out); - self.emit_bitflags_binop('^', &other, out); + self.emit_bitflags_binop(constexpr_prefix, '|', &other, out); + self.emit_bitflags_binop(constexpr_prefix, '&', &other, out); + self.emit_bitflags_binop(constexpr_prefix, '^', &other, out); } // Generate a serializer function that allows dumping this struct diff --git a/tests/expectations/associated_in_body.cpp b/tests/expectations/associated_in_body.cpp index 14c59e39c..d1319efb4 100644 --- a/tests/expectations/associated_in_body.cpp +++ b/tests/expectations/associated_in_body.cpp @@ -10,27 +10,27 @@ struct StyleAlignFlags { uint8_t bits; - explicit operator bool() const { + constexpr explicit operator bool() const { return !!bits; } - StyleAlignFlags operator~() const { + constexpr StyleAlignFlags operator~() const { return {static_cast(~bits)}; } - StyleAlignFlags operator|(const StyleAlignFlags& other) const { + constexpr StyleAlignFlags operator|(const StyleAlignFlags& other) const { return {static_cast(this->bits | other.bits)}; } StyleAlignFlags& operator|=(const StyleAlignFlags& other) { *this = (*this | other); return *this; } - StyleAlignFlags operator&(const StyleAlignFlags& other) const { + constexpr StyleAlignFlags operator&(const StyleAlignFlags& other) const { return {static_cast(this->bits & other.bits)}; } StyleAlignFlags& operator&=(const StyleAlignFlags& other) { *this = (*this & other); return *this; } - StyleAlignFlags operator^(const StyleAlignFlags& other) const { + constexpr StyleAlignFlags operator^(const StyleAlignFlags& other) const { return {static_cast(this->bits ^ other.bits)}; } StyleAlignFlags& operator^=(const StyleAlignFlags& other) { diff --git a/tests/expectations/bitflags.cpp b/tests/expectations/bitflags.cpp index c77100613..49dadbadc 100644 --- a/tests/expectations/bitflags.cpp +++ b/tests/expectations/bitflags.cpp @@ -10,27 +10,27 @@ struct AlignFlags { uint8_t bits; - explicit operator bool() const { + constexpr explicit operator bool() const { return !!bits; } - AlignFlags operator~() const { + constexpr AlignFlags operator~() const { return {static_cast(~bits)}; } - AlignFlags operator|(const AlignFlags& other) const { + constexpr AlignFlags operator|(const AlignFlags& other) const { return {static_cast(this->bits | other.bits)}; } AlignFlags& operator|=(const AlignFlags& other) { *this = (*this | other); return *this; } - AlignFlags operator&(const AlignFlags& other) const { + constexpr AlignFlags operator&(const AlignFlags& other) const { return {static_cast(this->bits & other.bits)}; } AlignFlags& operator&=(const AlignFlags& other) { *this = (*this & other); return *this; } - AlignFlags operator^(const AlignFlags& other) const { + constexpr AlignFlags operator^(const AlignFlags& other) const { return {static_cast(this->bits ^ other.bits)}; } AlignFlags& operator^=(const AlignFlags& other) { @@ -55,27 +55,27 @@ constexpr static const AlignFlags AlignFlags_MIXED_SELF = AlignFlags{ /* .bits = struct DebugFlags { uint32_t bits; - explicit operator bool() const { + constexpr explicit operator bool() const { return !!bits; } - DebugFlags operator~() const { + constexpr DebugFlags operator~() const { return {static_cast(~bits)}; } - DebugFlags operator|(const DebugFlags& other) const { + constexpr DebugFlags operator|(const DebugFlags& other) const { return {static_cast(this->bits | other.bits)}; } DebugFlags& operator|=(const DebugFlags& other) { *this = (*this | other); return *this; } - DebugFlags operator&(const DebugFlags& other) const { + constexpr DebugFlags operator&(const DebugFlags& other) const { return {static_cast(this->bits & other.bits)}; } DebugFlags& operator&=(const DebugFlags& other) { *this = (*this & other); return *this; } - DebugFlags operator^(const DebugFlags& other) const { + constexpr DebugFlags operator^(const DebugFlags& other) const { return {static_cast(this->bits ^ other.bits)}; } DebugFlags& operator^=(const DebugFlags& other) { @@ -89,27 +89,27 @@ constexpr static const DebugFlags DebugFlags_BIGGEST_ALLOWED = DebugFlags{ /* .b struct LargeFlags { uint64_t bits; - explicit operator bool() const { + constexpr explicit operator bool() const { return !!bits; } - LargeFlags operator~() const { + constexpr LargeFlags operator~() const { return {static_cast(~bits)}; } - LargeFlags operator|(const LargeFlags& other) const { + constexpr LargeFlags operator|(const LargeFlags& other) const { return {static_cast(this->bits | other.bits)}; } LargeFlags& operator|=(const LargeFlags& other) { *this = (*this | other); return *this; } - LargeFlags operator&(const LargeFlags& other) const { + constexpr LargeFlags operator&(const LargeFlags& other) const { return {static_cast(this->bits & other.bits)}; } LargeFlags& operator&=(const LargeFlags& other) { *this = (*this & other); return *this; } - LargeFlags operator^(const LargeFlags& other) const { + constexpr LargeFlags operator^(const LargeFlags& other) const { return {static_cast(this->bits ^ other.bits)}; } LargeFlags& operator^=(const LargeFlags& other) {