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

Support for casts in macros with smaller integers #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

crab2313
Copy link

@crab2313 crab2313 commented Jun 22, 2019

Partially fix #4 in the most common cases.

Any integer type longer than 32 bits(size_t, uint64_t, long) is not supported since EvalResult::Int can not hold it. For example:

#define Int_18446744073709551615 (unsigned long)-1

Please give me some ideas to simplify the implementation. I know it is ugly.

@jethrogb
Copy link
Owner

Thanks for your contribution! I won't have time to look at it for at least a week, but maybe @emilio can

@emilio
Copy link
Collaborator

emilio commented Jun 27, 2019

It looks reasonable, but I don't have enough nom expertise to properly review. Thanks a lot for working on this!

@crab2313 crab2313 force-pushed the fix-casts-in-macros branch from 7e60551 to 2f087fd Compare June 28, 2019 06:56
@crab2313
Copy link
Author

@jethrogb Is there any chance to get this pull request merged? I am trying to upload my first crate to crates.io and this feature is the only blocker.

Copy link
Owner

@jethrogb jethrogb left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! I like the general direction.

#define Int_n128 (int8_t)128
#define Int_n121 (int8_t)1234567
#define Int_111 (unsigned int)111.111
#define Int_4294967295 (unsigned int)-1
Copy link
Owner

Choose a reason for hiding this comment

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

Missing newline

impl<'a> PRef<'a> {
method!(cast<PRef<'a>,&[Token],TypeCast,::Error>, mut self,
delimited!(p!("("), alt!(
do_parse!(k!("unsigned") >> k!("int") >> (TypeCast::UnsignedInt(size_of::<c_uint>()))) |
Copy link
Owner

Choose a reason for hiding this comment

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

This will choose the size based on the platform that bindgen runs on (generally the compilation host), that seems incorrect.

Copy link
Author

Choose a reason for hiding this comment

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

Should we handle cross compilation? unsigned int may be different size on different architectures.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes we should, but I don't have a good idea for how.

do_parse!(i!("uint32_t") >> (TypeCast::UnsignedInt(4))) |
do_parse!(i!("int8_t") >> (TypeCast::SignedInt(1))) |
do_parse!(i!("int16_t") >> (TypeCast::SignedInt(2))) |
do_parse!(i!("int32_t") >> (TypeCast::SignedInt(4)))
Copy link
Owner

Choose a reason for hiding this comment

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

What about long, long long, and 64-bit types? In general, it may be useful to handle unknown types here in some graceful manner.

Copy link
Author

Choose a reason for hiding this comment

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

Any integer type longer than 32 bits(size_t, uint64_t, long) is not supported since EvalResult::Int can not hold it. We need change the API to support 64-bit types.

impl EvalResult {
fn cast(self, ctype: TypeCast) -> Self {
use self::TypeCast::*;
let round = |bits| 2i64.pow((bits) as u32);
Copy link
Owner

Choose a reason for hiding this comment

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

Unnecessary parentheses around bits

Copy link
Author

Choose a reason for hiding this comment

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

I will remove it.

impl EvalResult {
fn cast(self, ctype: TypeCast) -> Self {
use self::TypeCast::*;
let round = |bits| 2i64.pow((bits) as u32);
Copy link
Owner

Choose a reason for hiding this comment

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

I think this doesn't implement rounding, maybe give it another name?

Copy link
Author

Choose a reason for hiding this comment

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

Is pow2 a proper name?

@reitermarkus
Copy link

@crab2313, any update on this?

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.

Support for casts in macros
4 participants