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

Add support for bitfields #585

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
16 changes: 12 additions & 4 deletions src/analysis/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,22 @@ fn into_inner(res: Result) -> String {
}
}

impl IntoString for Result {
impl IntoString for TypeError {
fn into_string(self) -> String {
use self::TypeError::*;
match self {
Ignored(s) => format!("/*Ignored*/{}", s),
Mismatch(s) => format!("/*Metadata mismatch*/{}", s),
Unimplemented(s) => format!("/*Unimplemented*/{}", s),
}
}
}

impl IntoString for Result {
fn into_string(self) -> String {
match self {
Ok(s) => s,
Err(Ignored(s)) => format!("/*Ignored*/{}", s),
Err(Mismatch(s)) => format!("/*Metadata mismatch*/{}", s),
Err(Unimplemented(s)) => format!("/*Unimplemented*/{}", s),
Err(e) => e.into_string(),
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/analysis/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ impl IsIncomplete for Alias {

impl IsIncomplete for Field {
fn is_incomplete(&self, lib: &Library) -> bool {
if self.bits.is_some() {
// Bitfields are unrepresentable in Rust,
// so from our perspective they are incomplete.
return true;
}
if self.is_ptr() {
// Pointers are always complete.
return false;
Expand Down
49 changes: 42 additions & 7 deletions src/codegen/sys/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,50 @@ pub fn from_union(env: &Env, union: &Union) -> Fields {
fn analyze_fields(env: &Env, unsafe_access: bool, fields: &[Field]) -> (Vec<FieldInfo>, Option<String>) {
let mut truncated = None;
let mut infos = Vec::with_capacity(fields.len());
let mut skip_bits = false;

for field in fields {
let typ = match field_ffi_type(env, field) {
e @ Err(..) => {
truncated = Some(e.into_string());
break;
for (i, field) in fields.iter().enumerate() {
let typ =
if field.bits.is_some() {
if skip_bits {
continue;
}
skip_bits = true;
let mut bits = 0;
for i in i..fields.len() {
if let Some(bs) = fields[i].bits {
bits += bs;
}
}
let typ =
if bits <= 8 {
"u8"
}
else if bits <= 16 {
"u16"
}
else if bits <= 32 {
"u32"
}
else if bits <= 64 {
"u64"
}
Copy link
Member

Choose a reason for hiding this comment

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

Maybe better stop on 8, 16, 32 bit and create field?
Not sure how you plan decombining it actually.

else {
truncated = Some("Bit fields too large".to_string());
Copy link

Choose a reason for hiding this comment

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

The inconsistency between this case and IsIncomplete implementation will cause problems for any other struct that contains truncated one. But if such large bitfields don't happen in practice then LGTM.

break;
};
typ.to_string()
}
Ok(typ) => typ,
};
else {
skip_bits = false;
match field_ffi_type(env, field) {
Err(e) => {
truncated = Some(e.into_string());
break;
}
Ok(typ) => typ,
}
};
// Skip private fields from Debug impl. Ignore volatile as well,
// they are usually used as synchronization primites,
// so we wouldn't want to introduce additional reads.
Expand Down