-
Notifications
You must be signed in to change notification settings - Fork 20
Support for casts in macros with smaller integers #15
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
use std::collections::HashMap; | ||
use std::ops::{AddAssign,BitAndAssign,BitOrAssign,BitXorAssign,DivAssign,MulAssign,RemAssign,ShlAssign,ShrAssign,SubAssign}; | ||
use std::num::Wrapping; | ||
use std::mem::size_of; | ||
use std::os::raw::*; | ||
|
||
use literal::{self,CChar}; | ||
use token::{Token,Kind as TokenKind}; | ||
|
@@ -65,7 +67,7 @@ impl EvalResult { | |
result_opt!(fn as_float: Float -> f64); | ||
result_opt!(fn as_char: Char -> CChar); | ||
result_opt!(fn as_str: Str -> Vec<u8>); | ||
|
||
fn as_numeric(self) -> Option<EvalResult> { | ||
match self { | ||
EvalResult::Int(_) | EvalResult::Float(_) => Some(self), | ||
|
@@ -130,6 +132,14 @@ macro_rules! p ( | |
($i:expr, $c:expr) => (exact_token!($i,Punctuation,$c.as_bytes())) | ||
); | ||
|
||
macro_rules! k ( | ||
($i:expr, $c:expr) => (exact_token!($i,Keyword,$c.as_bytes())) | ||
); | ||
|
||
macro_rules! i ( | ||
($i:expr, $c:expr) => (exact_token!($i,Identifier,$c.as_bytes())) | ||
); | ||
|
||
macro_rules! one_of_punctuation ( | ||
($i:expr, $c:expr) => ({ | ||
if $i.is_empty() { | ||
|
@@ -298,9 +308,57 @@ macro_rules! numeric ( | |
($i:expr, $f:expr ) => (map_opt!($i,call!($f),EvalResult::as_numeric)); | ||
); | ||
|
||
enum TypeCast { | ||
SignedInt(usize), | ||
UnsignedInt(usize), | ||
} | ||
|
||
impl EvalResult { | ||
fn cast(self, ctype: TypeCast) -> Self { | ||
use self::TypeCast::*; | ||
let round = |bits| 2i64.pow((bits) as u32); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this doesn't implement rounding, maybe give it another name? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
let unsigned_mask = |i, b| (i+round(b*8)) % round(b*8); | ||
let signed_mask = |i, b| { | ||
let rounded = unsigned_mask(i, b); | ||
let half = round(8*b-1); | ||
if rounded >= half {rounded - half * 2} else { rounded } | ||
}; | ||
match self { | ||
EvalResult::Float(f) => match ctype { | ||
SignedInt(b) => EvalResult::Int(Wrapping(signed_mask(f as i64, b))), | ||
UnsignedInt(b) => EvalResult::Int(Wrapping(unsigned_mask(f as i64, b))), | ||
}, | ||
EvalResult::Int(Wrapping(i)) => match ctype { | ||
SignedInt(b) => EvalResult::Int(Wrapping(signed_mask(i, b))), | ||
UnsignedInt(b) => EvalResult::Int(Wrapping(unsigned_mask(i, b))), | ||
}, | ||
_ => self | ||
} | ||
} | ||
} | ||
|
||
|
||
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>()))) | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!(k!("unsigned") >> k!("short") >> (TypeCast::UnsignedInt(size_of::<c_ushort>()))) | | ||
do_parse!(k!("unsigned") >> k!("char") >> (TypeCast::UnsignedInt(size_of::<c_uchar>()))) | | ||
do_parse!(opt!(k!("signed")) >> k!("int") >> (TypeCast::SignedInt(size_of::<c_int>()))) | | ||
do_parse!(opt!(k!("signed")) >> k!("short") >> (TypeCast::SignedInt(size_of::<c_short>()))) | | ||
do_parse!(opt!(k!("signed")) >> k!("char") >> (TypeCast::SignedInt(size_of::<c_char>()))) | | ||
do_parse!(i!("uint8_t") >> (TypeCast::UnsignedInt(1))) | | ||
do_parse!(i!("uint16_t") >> (TypeCast::UnsignedInt(2))) | | ||
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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
) ,p!(")")) | ||
); | ||
|
||
method!(unary<PRef<'a>,&[Token],EvalResult,::Error>, mut self, | ||
alt!( | ||
map!(pair!(call_m!(self.cast), call_m!(self.unary)), |(c, r)| r.cast(c) ) | | ||
delimited!(p!("("),call_m!(self.numeric_expr),p!(")")) | | ||
numeric!(call_m!(self.literal)) | | ||
numeric!(call_m!(self.identifier)) | | ||
|
@@ -489,7 +547,7 @@ impl<'ident> IdentifierParser<'ident> { | |
fn as_ref(&self) -> PRef { | ||
PRef(self) | ||
} | ||
|
||
/// Create a new `IdentifierParser` with a set of known identifiers. When | ||
/// a known identifier is encountered during parsing, it is substituted | ||
/// for the value specified. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -254,3 +254,4 @@ test_file!(strings); | |
test_file!(int_signed); | ||
test_file!(int_unsigned); | ||
test_file!(fail); | ||
test_file!(casts); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#define Int_n2 (int)-2 | ||
#define Int_254 (uint8_t)-2 | ||
#define Float_236p3 (int)233.3 + 3.3 | ||
#define Int_255 (uint8_t)255.6 | ||
#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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing newline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary parentheses around
bits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove it.