Skip to content

Commit 021d09d

Browse files
committed
bitflags: Make more operations constexpr.
1 parent 2e2c87e commit 021d09d

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

src/bindgen/ir/structure.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,16 @@ impl Struct {
205205

206206
fn emit_bitflags_binop<F: Write>(
207207
&self,
208+
constexpr_prefix: &str,
208209
operator: char,
209210
other: &str,
210211
out: &mut SourceWriter<F>,
211212
) {
212213
out.new_line();
213214
write!(
214215
out,
215-
"{} operator{}(const {}& {}) const",
216+
"{}{} operator{}(const {}& {}) const",
217+
constexpr_prefix,
216218
self.export_name(),
217219
operator,
218220
self.export_name(),
@@ -531,21 +533,31 @@ impl Source for Struct {
531533
wrote_start_newline = true;
532534
out.new_line();
533535
}
536+
let constexpr_prefix = if config.constant.allow_constexpr {
537+
"constexpr "
538+
} else {
539+
""
540+
};
541+
534542
out.new_line();
535-
write!(out, "explicit operator bool() const");
543+
write!(out, "{}explicit operator bool() const", constexpr_prefix);
536544
out.open_brace();
537545
write!(out, "return !!bits;");
538546
out.close_brace(false);
539547

540548
out.new_line();
541-
write!(out, "{} operator~() const", self.export_name());
549+
write!(
550+
out,
551+
"{}{} operator~() const",
552+
constexpr_prefix,
553+
self.export_name()
554+
);
542555
out.open_brace();
543556
write!(out, "return {{static_cast<decltype(bits)>(~bits)}};");
544557
out.close_brace(false);
545-
546-
self.emit_bitflags_binop('|', &other, out);
547-
self.emit_bitflags_binop('&', &other, out);
548-
self.emit_bitflags_binop('^', &other, out);
558+
self.emit_bitflags_binop(constexpr_prefix, '|', &other, out);
559+
self.emit_bitflags_binop(constexpr_prefix, '&', &other, out);
560+
self.emit_bitflags_binop(constexpr_prefix, '^', &other, out);
549561
}
550562

551563
// Generate a serializer function that allows dumping this struct

tests/expectations/associated_in_body.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@
1010
struct StyleAlignFlags {
1111
uint8_t bits;
1212

13-
explicit operator bool() const {
13+
constexpr explicit operator bool() const {
1414
return !!bits;
1515
}
16-
StyleAlignFlags operator~() const {
16+
constexpr StyleAlignFlags operator~() const {
1717
return {static_cast<decltype(bits)>(~bits)};
1818
}
19-
StyleAlignFlags operator|(const StyleAlignFlags& other) const {
19+
constexpr StyleAlignFlags operator|(const StyleAlignFlags& other) const {
2020
return {static_cast<decltype(bits)>(this->bits | other.bits)};
2121
}
2222
StyleAlignFlags& operator|=(const StyleAlignFlags& other) {
2323
*this = (*this | other);
2424
return *this;
2525
}
26-
StyleAlignFlags operator&(const StyleAlignFlags& other) const {
26+
constexpr StyleAlignFlags operator&(const StyleAlignFlags& other) const {
2727
return {static_cast<decltype(bits)>(this->bits & other.bits)};
2828
}
2929
StyleAlignFlags& operator&=(const StyleAlignFlags& other) {
3030
*this = (*this & other);
3131
return *this;
3232
}
33-
StyleAlignFlags operator^(const StyleAlignFlags& other) const {
33+
constexpr StyleAlignFlags operator^(const StyleAlignFlags& other) const {
3434
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
3535
}
3636
StyleAlignFlags& operator^=(const StyleAlignFlags& other) {

tests/expectations/bitflags.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,27 @@
1010
struct AlignFlags {
1111
uint8_t bits;
1212

13-
explicit operator bool() const {
13+
constexpr explicit operator bool() const {
1414
return !!bits;
1515
}
16-
AlignFlags operator~() const {
16+
constexpr AlignFlags operator~() const {
1717
return {static_cast<decltype(bits)>(~bits)};
1818
}
19-
AlignFlags operator|(const AlignFlags& other) const {
19+
constexpr AlignFlags operator|(const AlignFlags& other) const {
2020
return {static_cast<decltype(bits)>(this->bits | other.bits)};
2121
}
2222
AlignFlags& operator|=(const AlignFlags& other) {
2323
*this = (*this | other);
2424
return *this;
2525
}
26-
AlignFlags operator&(const AlignFlags& other) const {
26+
constexpr AlignFlags operator&(const AlignFlags& other) const {
2727
return {static_cast<decltype(bits)>(this->bits & other.bits)};
2828
}
2929
AlignFlags& operator&=(const AlignFlags& other) {
3030
*this = (*this & other);
3131
return *this;
3232
}
33-
AlignFlags operator^(const AlignFlags& other) const {
33+
constexpr AlignFlags operator^(const AlignFlags& other) const {
3434
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
3535
}
3636
AlignFlags& operator^=(const AlignFlags& other) {
@@ -55,27 +55,27 @@ constexpr static const AlignFlags AlignFlags_MIXED_SELF = AlignFlags{ /* .bits =
5555
struct DebugFlags {
5656
uint32_t bits;
5757

58-
explicit operator bool() const {
58+
constexpr explicit operator bool() const {
5959
return !!bits;
6060
}
61-
DebugFlags operator~() const {
61+
constexpr DebugFlags operator~() const {
6262
return {static_cast<decltype(bits)>(~bits)};
6363
}
64-
DebugFlags operator|(const DebugFlags& other) const {
64+
constexpr DebugFlags operator|(const DebugFlags& other) const {
6565
return {static_cast<decltype(bits)>(this->bits | other.bits)};
6666
}
6767
DebugFlags& operator|=(const DebugFlags& other) {
6868
*this = (*this | other);
6969
return *this;
7070
}
71-
DebugFlags operator&(const DebugFlags& other) const {
71+
constexpr DebugFlags operator&(const DebugFlags& other) const {
7272
return {static_cast<decltype(bits)>(this->bits & other.bits)};
7373
}
7474
DebugFlags& operator&=(const DebugFlags& other) {
7575
*this = (*this & other);
7676
return *this;
7777
}
78-
DebugFlags operator^(const DebugFlags& other) const {
78+
constexpr DebugFlags operator^(const DebugFlags& other) const {
7979
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
8080
}
8181
DebugFlags& operator^=(const DebugFlags& other) {
@@ -89,27 +89,27 @@ constexpr static const DebugFlags DebugFlags_BIGGEST_ALLOWED = DebugFlags{ /* .b
8989
struct LargeFlags {
9090
uint64_t bits;
9191

92-
explicit operator bool() const {
92+
constexpr explicit operator bool() const {
9393
return !!bits;
9494
}
95-
LargeFlags operator~() const {
95+
constexpr LargeFlags operator~() const {
9696
return {static_cast<decltype(bits)>(~bits)};
9797
}
98-
LargeFlags operator|(const LargeFlags& other) const {
98+
constexpr LargeFlags operator|(const LargeFlags& other) const {
9999
return {static_cast<decltype(bits)>(this->bits | other.bits)};
100100
}
101101
LargeFlags& operator|=(const LargeFlags& other) {
102102
*this = (*this | other);
103103
return *this;
104104
}
105-
LargeFlags operator&(const LargeFlags& other) const {
105+
constexpr LargeFlags operator&(const LargeFlags& other) const {
106106
return {static_cast<decltype(bits)>(this->bits & other.bits)};
107107
}
108108
LargeFlags& operator&=(const LargeFlags& other) {
109109
*this = (*this & other);
110110
return *this;
111111
}
112-
LargeFlags operator^(const LargeFlags& other) const {
112+
constexpr LargeFlags operator^(const LargeFlags& other) const {
113113
return {static_cast<decltype(bits)>(this->bits ^ other.bits)};
114114
}
115115
LargeFlags& operator^=(const LargeFlags& other) {

0 commit comments

Comments
 (0)