Skip to content

Support for casts in macros with smaller integers#15

Open
crab2313 wants to merge 1 commit into
jethrogb:masterfrom
crab2313:fix-casts-in-macros
Open

Support for casts in macros with smaller integers#15
crab2313 wants to merge 1 commit into
jethrogb:masterfrom
crab2313:fix-casts-in-macros

Conversation

@crab2313

@crab2313 crab2313 commented Jun 22, 2019

Copy link
Copy Markdown

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
Copy Markdown
Owner

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

@emilio

emilio commented Jun 27, 2019

Copy link
Copy Markdown
Collaborator

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
Copy Markdown
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.

@jethrogb jethrogb left a comment

Copy link
Copy Markdown
Owner

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.

Comment thread tests/input/casts.h
#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 No newline at end of file

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Missing newline

Comment thread src/expr.rs
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/expr.rs
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
Copy Markdown
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
Copy Markdown
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.

Comment thread src/expr.rs
impl EvalResult {
fn cast(self, ctype: TypeCast) -> Self {
use self::TypeCast::*;
let round = |bits| 2i64.pow((bits) as u32);

Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread src/expr.rs
impl EvalResult {
fn cast(self, ctype: TypeCast) -> Self {
use self::TypeCast::*;
let round = |bits| 2i64.pow((bits) as u32);

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown

@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