@@ -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