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

Generate and add options for bitfield-like enums. #226

Merged
merged 4 commits into from
Nov 8, 2016

Conversation

emilio
Copy link
Contributor

@emilio emilio commented Nov 8, 2016

r? @fitzgen or @Manishearth

cc @bholley @upsuper @heycam

Followups:

  • Reduce duplication a bit in that code (probably E-easy)
  • Generate an impl of BitOr for bitfield-like enums (E-easy/E-less-easy).

@highfive
Copy link

highfive commented Nov 8, 2016

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!

@upsuper
Copy link
Contributor

upsuper commented Nov 8, 2016

Is it possible to generate a bitflag! macro instead of the constants and tuple struct? That seems to be a better solution to this issue.

@emilio
Copy link
Contributor Author

emilio commented Nov 8, 2016

Ok, if it's not urgent, I can hold this off, otherwise I'd rather merge it and fix #227 separately.

@upsuper
Copy link
Contributor

upsuper commented Nov 8, 2016

I'm fine with this solution. Although bitflag is probably better, we can do that later if we want. I'm not a fan of adding dependency as well.

@@ -91,6 +92,10 @@ Options:
matching <regex>. Same behavior on emptyness
than the type whitelisting.

--bitfield-enum=<regex> Mark a bitfield as being used as an enum.
This makes bindgen don't generate a rust enum
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/don't/not

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me with comments addressed

Thanks!

@@ -91,6 +92,10 @@ Options:
matching <regex>. Same behavior on emptyness
than the type whitelisting.

--bitfield-enum=<regex> Mark a bitfield as being used as an enum.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: since this is a regex that presumably makes anything that matches it a bitfield enum (I haven't read the rest of the patch yet), we should rephrase this a little bit:

Mark any union whose name matches <regex> as a set of bitfield flags instead of an enumeration. Bindgen will generate a type with bitwise arithmetic operators, instead of a Rust enum. If the union is anonymous, match <regex> against its members.

(Does this patch handle anonymous unions as bitfields?)

I also think it might make sense to rename this option to --bitfield-union or simply --bitfield since enum is purely on the Rust side of things.

} else {
None
}
if rt.is_valid() { Some(rt) } else { None }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these changes are simply rustfmt right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, just for the record, you can review individual commits in the github UI, if that's easier :)

@@ -1224,6 +1224,79 @@ impl MethodCodegen for Method {
}
}

enum EnumBuilder<'a> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: it would be nice to have brief documentation comments here and throughout the new code (I know I never got around to documenting the codegen module, but we should aim to hold ourselves to documenting new code)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, thanks for pointing it out :)

}
}

fn with_variant_(self,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a trailing "_"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was for being consistent with the aster builder method, but it doesn't make any sense now.

self.variants()
.iter()
.any(|v| ctx.options().bitfield_enums.matches(&v.name())))
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should factor this out to a method on the context or on the Enum which takes a context as a parameter. No need for the logic of determining whether a type should be a bitfield to live inside code generation.

/// Mark the given enum (or set of enums, if using a pattern) as being
/// bitfield-like.
///
/// This makes bindgen to generate a type for it that isn't a rust `enum`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick:

This makes bindgen to generate a type for it that isn't a rust enum.

Baz = 1 << 2,
Duplicated = 1 << 2,
Negative = -3,
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add an anonymous union whose variants match the --bitfield-enum regex.

@emilio
Copy link
Contributor Author

emilio commented Nov 8, 2016

@bors-servo r=fitzgen

I ended up adding the BitOr impl, but it's straight-forward. I couldn't move is_bitfield to a method and make it look good, since you need access to the item name, so the place to add that would be ir::Item, where it feels out of place...

@bors-servo
Copy link

📌 Commit 4062713 has been approved by fitzgen

@bors-servo
Copy link

⌛ Testing commit 4062713 with merge c9eccea...

bors-servo pushed a commit that referenced this pull request Nov 8, 2016
Generate and add options for bitfield-like enums.

r? @fitzgen or @Manishearth

cc @bholley @upsuper @heycam

Followups:

 * Reduce duplication a bit in that code (probably E-easy)
 * Generate an impl of `BitOr` for bitfield-like enums (E-easy/E-less-easy).
@bors-servo
Copy link

☀️ Test successful - status-travis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants