Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pxbind fix for combo bit flags with partially unnamed bits #226

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions physx-sys/pxbind/src/generator/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,24 @@ impl<'ast> crate::consumer::FlagsBinding<'ast> {
writes!(w, "1 << {}", val.ilog2());
} else {
let mut is_combo = false;
// If we're not a power of 2, we're a combination of flags,

// If we're not a power of 2, we're a combination of flags (or constants),
// find which ones and emit them in a friendly way
for (i, which) in enum_binding

// We keep track of what remaining flag(s) we have to mask into the bitmask
// by removing the bits that have been found as named flags.
let mut remainder = val;

for (i, (which, bit)) in enum_binding
.variants
.iter()
.filter_map(|var| {
let prev = var.value as u64;
(prev & (prev - 1) == 0 && (prev & val) != 0).then_some(var.name)
(prev & (prev - 1) == 0 && (prev & val) != 0).then_some((var.name, prev))
})
.enumerate()
{
remainder &= !bit;
is_combo = true;
if i > 0 {
writes!(w, " | ");
Expand All @@ -133,6 +140,12 @@ impl<'ast> crate::consumer::FlagsBinding<'ast> {
// emit the raw value
if !is_combo {
writes!(w, "0x{val:08x}");
} else if remainder != 0 {
if remainder & (remainder - 1) == 0 {
writes!(w, "| 1 << {}", remainder.ilog2());
} else {
writes!(w, "| 0x{remainder:08x}");
}
}
}

Expand Down
Loading