Skip to content

Commit 36d7905

Browse files
Ruakkysen
authored andcommitted
transpile: Minor refactor to struct functions
1 parent 0a97f62 commit 36d7905

File tree

3 files changed

+56
-61
lines changed

3 files changed

+56
-61
lines changed

c2rust-transpile/src/translator/literals.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -290,21 +290,7 @@ impl<'c> Translation<'c> {
290290
}
291291
}
292292
CTypeKind::Struct(struct_id) => {
293-
let mut literal = self.convert_struct_literal(ctx, struct_id, ids.as_ref());
294-
if self.ast_context.has_inner_struct_decl(struct_id) {
295-
// If the structure is split into an outer/inner,
296-
// wrap the inner initializer using the outer structure
297-
let outer_name = self
298-
.type_converter
299-
.borrow()
300-
.resolve_decl_name(struct_id)
301-
.unwrap();
302-
303-
let outer_path = mk().path_expr(vec![outer_name]);
304-
literal = literal
305-
.map(|lit_ws| lit_ws.map(|lit| mk().call_expr(outer_path, vec![lit])));
306-
};
307-
literal
293+
self.convert_struct_literal(ctx, struct_id, ids.as_ref())
308294
}
309295
CTypeKind::Union(union_id) => {
310296
self.convert_union_literal(ctx, union_id, ids.as_ref(), ty, opt_union_field_id)

c2rust-transpile/src/translator/mod.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4763,16 +4763,13 @@ impl<'c> Translation<'c> {
47634763
fields: Some(ref fields),
47644764
platform_byte_size,
47654765
..
4766-
} => {
4767-
let name = self.resolve_decl_inner_name(name_decl_id);
4768-
self.convert_struct_zero_initializer(
4769-
name,
4770-
decl_id,
4771-
fields,
4772-
platform_byte_size,
4773-
is_static,
4774-
)?
4775-
}
4766+
} => self.convert_struct_zero_initializer(
4767+
decl_id,
4768+
name_decl_id,
4769+
fields,
4770+
platform_byte_size,
4771+
is_static,
4772+
)?,
47764773

47774774
CDeclKind::Struct { fields: None, .. } => {
47784775
return Err(TranslationError::generic(

c2rust-transpile/src/translator/structs.rs

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -456,65 +456,77 @@ impl<'a> Translation<'a> {
456456
let fields = fields
457457
.into_iter()
458458
.collect::<WithStmts<Vec<syn::FieldValue>>>();
459-
let struct_expr = fields.map(|fields| mk().struct_expr(name.as_str(), fields));
460-
461-
if bitfield_inits.is_empty() {
462-
return Ok(struct_expr);
463-
}
464-
465-
struct_expr.and_then(|struct_expr| {
466-
let local_pat = mk().mutbl().ident_pat("init");
467-
let local_variable = Box::new(mk().local(local_pat, None, Some(struct_expr)));
468-
469-
let mut is_unsafe = false;
470-
let mut stmts = vec![mk().local_stmt(local_variable)];
459+
let mut val = fields.map(|fields| mk().struct_expr(name.as_str(), fields));
460+
461+
if !bitfield_inits.is_empty() {
462+
val = val.and_then(|val| -> TranslationResult<_> {
463+
let local_pat = mk().mutbl().ident_pat("init");
464+
let local_variable = Box::new(mk().local(local_pat, None, Some(val)));
465+
466+
let mut is_unsafe = false;
467+
let mut stmts = vec![mk().local_stmt(local_variable)];
468+
469+
// Now we must use the bitfield methods to initialize bitfields
470+
for (field_name, val) in bitfield_inits {
471+
let field_name_setter = format!("set_{}", field_name);
472+
let struct_ident = mk().ident_expr("init");
473+
is_unsafe |= val.is_unsafe();
474+
let val = val
475+
.to_pure_expr()
476+
.expect("Expected no statements in bitfield initializer");
477+
let expr = mk().method_call_expr(struct_ident, field_name_setter, vec![val]);
478+
479+
stmts.push(mk().semi_stmt(expr));
480+
}
471481

472-
// Now we must use the bitfield methods to initialize bitfields
473-
for (field_name, val) in bitfield_inits {
474-
let field_name_setter = format!("set_{}", field_name);
475-
let struct_ident = mk().ident_expr("init");
476-
is_unsafe |= val.is_unsafe();
477-
let val = val
478-
.to_pure_expr()
479-
.expect("Expected no statements in bitfield initializer");
480-
let expr = mk().method_call_expr(struct_ident, field_name_setter, vec![val]);
482+
stmts.push(mk().expr_stmt(mk().ident_expr("init")));
481483

482-
stmts.push(mk().semi_stmt(expr));
483-
}
484+
let val = mk().block_expr(mk().block(stmts));
484485

485-
let struct_ident = mk().ident_expr("init");
486+
if is_unsafe {
487+
Ok(WithStmts::new_unsafe_val(val))
488+
} else {
489+
Ok(WithStmts::new_val(val))
490+
}
491+
})?;
492+
}
486493

487-
stmts.push(mk().expr_stmt(struct_ident));
494+
// If the structure is split into an outer/inner,
495+
// wrap the inner initializer using the outer structure
496+
if self.ast_context.has_inner_struct_decl(struct_id) {
497+
let outer_name = self
498+
.type_converter
499+
.borrow()
500+
.resolve_decl_name(struct_id)
501+
.unwrap();
488502

489-
let val = mk().block_expr(mk().block(stmts));
503+
let outer_path = mk().path_expr(vec![outer_name]);
504+
val = val.map(|val| mk().call_expr(outer_path, vec![val]));
505+
}
490506

491-
if is_unsafe {
492-
Ok(WithStmts::new_unsafe_val(val))
493-
} else {
494-
Ok(WithStmts::new_val(val))
495-
}
496-
})
507+
Ok(val)
497508
}
498509

499510
/// This method handles zero-initializing bitfield structs including bitfields
500511
/// & padding fields
501512
pub fn convert_struct_zero_initializer(
502513
&self,
503-
name: String,
504-
struct_id: CRecordId,
514+
decl_id: CRecordId,
515+
name_decl_id: CDeclId,
505516
field_ids: &[CDeclId],
506517
platform_byte_size: u64,
507518
is_static: bool,
508519
) -> TranslationResult<WithStmts<Box<Expr>>> {
509-
let reorganized_fields = self.get_field_types(struct_id, field_ids, platform_byte_size)?;
520+
let name = self.resolve_decl_inner_name(name_decl_id);
521+
let reorganized_fields = self.get_field_types(decl_id, field_ids, platform_byte_size)?;
510522
let mut fields = Vec::with_capacity(reorganized_fields.len());
511523

512524
let mut padding_count = 0;
513525
let mut next_padding_field = || {
514526
let field_name = self
515527
.type_converter
516528
.borrow_mut()
517-
.declare_padding(struct_id, padding_count);
529+
.declare_padding(decl_id, padding_count);
518530
padding_count += 1;
519531
field_name
520532
};

0 commit comments

Comments
 (0)