Skip to content

Commit 71cf038

Browse files
jsgfemilio
authored andcommitted
Update with review comments
1 parent 77102b2 commit 71cf038

File tree

2 files changed

+133
-116
lines changed

2 files changed

+133
-116
lines changed

bindgen/codegen/mod.rs

Lines changed: 130 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
16931693
accessor_kind: FieldAccessorKind,
16941694
parent: &CompInfo,
16951695
parent_item: &Item,
1696-
_last_field: bool,
1696+
last_field: bool,
16971697
result: &mut CodegenResult,
16981698
struct_layout: &mut StructLayoutTracker,
16991699
fields: &mut F,
@@ -1775,7 +1775,7 @@ impl<'a> FieldCodegen<'a> for BitfieldUnit {
17751775
accessor_kind,
17761776
parent,
17771777
parent_item,
1778-
idx == bfields.len() - 1,
1778+
last_field && idx == bfields.len() - 1,
17791779
result,
17801780
struct_layout,
17811781
fields,
@@ -2594,116 +2594,13 @@ impl CodeGenerator for CompInfo {
25942594
}
25952595

25962596
if needs_flexarray_impl {
2597-
let prefix = ctx.trait_prefix();
2598-
2599-
let flex_array =
2600-
flex_inner_ty.as_ref().map(|ty| quote! { [ #ty ] });
2601-
2602-
let dst_ty_for_impl = quote! {
2603-
#canonical_ident < #( #generic_param_names , )* #flex_array >
2604-
2605-
};
2606-
let sized_ty_for_impl = quote! {
2607-
#canonical_ident < #( #generic_param_names , )* [ #flex_inner_ty; 0 ] >
2608-
};
2609-
2610-
let layout = if ctx.options().rust_features().layout_for_ptr {
2611-
quote! {
2612-
pub fn layout(len: usize) -> ::#prefix::alloc::Layout {
2613-
// SAFETY: Null pointers are OK if we don't deref them
2614-
unsafe {
2615-
let p: *const Self = ::#prefix::ptr::from_raw_parts(::#prefix::ptr::null(), len);
2616-
::#prefix::alloc::Layout::for_value_raw(p)
2617-
}
2618-
}
2619-
}
2620-
} else {
2621-
quote!()
2622-
};
2623-
2624-
let (from_ptr_dst, from_ptr_sized) = if ctx
2625-
.options()
2626-
.rust_features()
2627-
.ptr_metadata
2628-
{
2629-
(
2630-
quote! {
2631-
pub fn fixed(&self) -> (& #sized_ty_for_impl, usize) {
2632-
unsafe {
2633-
let (ptr, len) = (self as *const Self).to_raw_parts();
2634-
(&*(ptr as *const #sized_ty_for_impl), len)
2635-
}
2636-
}
2637-
2638-
pub fn fixed_mut(&mut self) -> (&mut #sized_ty_for_impl, usize) {
2639-
unsafe {
2640-
let (ptr, len) = (self as *mut Self).to_raw_parts();
2641-
(&mut *(ptr as *mut #sized_ty_for_impl), len)
2642-
2643-
}
2644-
}
2645-
},
2646-
quote! {
2647-
/// Convert a sized prefix to an unsized structure with the given length.
2648-
///
2649-
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
2650-
pub unsafe fn flex_ref(&self, len: usize) -> &#dst_ty_for_impl {
2651-
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2652-
unsafe { Self::flex_ptr(self, len) }
2653-
}
2654-
2655-
/// Convert a mutable sized prefix to an unsized structure with the given length.
2656-
///
2657-
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
2658-
pub unsafe fn flex_mut_ref(&mut self, len: usize) -> &mut #dst_ty_for_impl {
2659-
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2660-
unsafe { Self::flex_ptr_mut(self, len).assume_init() }
2661-
}
2662-
2663-
/// Construct DST variant from a pointer and a size.
2664-
///
2665-
/// NOTE: lifetime of returned reference is not tied to any underlying storage.
2666-
/// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements.
2667-
pub unsafe fn flex_ptr<'unbounded>(ptr: *const Self, len: usize) -> &'unbounded #dst_ty_for_impl {
2668-
unsafe { &*::#prefix::ptr::from_raw_parts(ptr as *const (), len) }
2669-
}
2670-
2671-
/// Construct mutable DST variant from a pointer and a
2672-
/// size. The returned `&mut` reference is initialized
2673-
/// pointing to memory referenced by `ptr`, but there's
2674-
/// no requirement that that memory be initialized.
2675-
///
2676-
/// NOTE: lifetime of returned reference is not tied to any underlying storage.
2677-
/// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements.
2678-
pub unsafe fn flex_ptr_mut<'unbounded>(
2679-
ptr: *mut Self,
2680-
len: usize,
2681-
) -> ::#prefix::mem::MaybeUninit<&'unbounded mut #dst_ty_for_impl> {
2682-
unsafe {
2683-
// Initialize reference without ever exposing it, as its possibly uninitialized
2684-
let mut uninit = ::#prefix::mem::MaybeUninit::<&mut #dst_ty_for_impl>::uninit();
2685-
(uninit.as_mut_ptr() as *mut *mut #dst_ty_for_impl)
2686-
.write(::#prefix::ptr::from_raw_parts_mut(ptr as *mut (), len));
2687-
2688-
uninit
2689-
}
2690-
}
2691-
},
2692-
)
2693-
} else {
2694-
(quote!(), quote!())
2695-
};
2696-
2697-
result.push(quote! {
2698-
impl #impl_generics_labels #dst_ty_for_impl {
2699-
#layout
2700-
#from_ptr_dst
2701-
}
2702-
2703-
impl #impl_generics_labels #sized_ty_for_impl {
2704-
#from_ptr_sized
2705-
}
2706-
});
2597+
result.push(self.generate_flexarray(
2598+
ctx,
2599+
&canonical_ident,
2600+
flex_inner_ty,
2601+
&*generic_param_names,
2602+
&impl_generics_labels,
2603+
));
27072604
}
27082605

27092606
if needs_default_impl {
@@ -2790,6 +2687,127 @@ impl CodeGenerator for CompInfo {
27902687
}
27912688
}
27922689

2690+
impl CompInfo {
2691+
fn generate_flexarray(
2692+
&self,
2693+
ctx: &BindgenContext,
2694+
canonical_ident: &Ident,
2695+
flex_inner_ty: Option<proc_macro2::TokenStream>,
2696+
generic_param_names: &[Ident],
2697+
impl_generics_labels: &proc_macro2::TokenStream,
2698+
) -> proc_macro2::TokenStream {
2699+
let prefix = ctx.trait_prefix();
2700+
2701+
let flex_array = flex_inner_ty.as_ref().map(|ty| quote! { [ #ty ] });
2702+
2703+
let dst_ty_for_impl = quote! {
2704+
#canonical_ident < #( #generic_param_names , )* #flex_array >
2705+
2706+
};
2707+
let sized_ty_for_impl = quote! {
2708+
#canonical_ident < #( #generic_param_names , )* [ #flex_inner_ty; 0 ] >
2709+
};
2710+
2711+
let layout = if ctx.options().rust_features().layout_for_ptr {
2712+
quote! {
2713+
pub fn layout(len: usize) -> ::#prefix::alloc::Layout {
2714+
// SAFETY: Null pointers are OK if we don't deref them
2715+
unsafe {
2716+
let p: *const Self = ::#prefix::ptr::from_raw_parts(::#prefix::ptr::null(), len);
2717+
::#prefix::alloc::Layout::for_value_raw(p)
2718+
}
2719+
}
2720+
}
2721+
} else {
2722+
quote!()
2723+
};
2724+
2725+
let (from_ptr_dst, from_ptr_sized) = if ctx
2726+
.options()
2727+
.rust_features()
2728+
.ptr_metadata
2729+
{
2730+
(
2731+
quote! {
2732+
pub fn fixed(&self) -> (& #sized_ty_for_impl, usize) {
2733+
unsafe {
2734+
let (ptr, len) = (self as *const Self).to_raw_parts();
2735+
(&*(ptr as *const #sized_ty_for_impl), len)
2736+
}
2737+
}
2738+
2739+
pub fn fixed_mut(&mut self) -> (&mut #sized_ty_for_impl, usize) {
2740+
unsafe {
2741+
let (ptr, len) = (self as *mut Self).to_raw_parts();
2742+
(&mut *(ptr as *mut #sized_ty_for_impl), len)
2743+
2744+
}
2745+
}
2746+
},
2747+
quote! {
2748+
/// Convert a sized prefix to an unsized structure with the given length.
2749+
///
2750+
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
2751+
pub unsafe fn flex_ref(&self, len: usize) -> &#dst_ty_for_impl {
2752+
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2753+
unsafe { Self::flex_ptr(self, len) }
2754+
}
2755+
2756+
/// Convert a mutable sized prefix to an unsized structure with the given length.
2757+
///
2758+
/// SAFETY: Underlying storage is initialized up to at least `len` elements.
2759+
pub unsafe fn flex_mut_ref(&mut self, len: usize) -> &mut #dst_ty_for_impl {
2760+
// SAFETY: Reference is always valid as pointer. Caller is guaranteeing `len`.
2761+
unsafe { Self::flex_ptr_mut(self, len).assume_init() }
2762+
}
2763+
2764+
/// Construct DST variant from a pointer and a size.
2765+
///
2766+
/// NOTE: lifetime of returned reference is not tied to any underlying storage.
2767+
/// SAFETY: `ptr` is valid. Underlying storage is fully initialized up to at least `len` elements.
2768+
pub unsafe fn flex_ptr<'unbounded>(ptr: *const Self, len: usize) -> &'unbounded #dst_ty_for_impl {
2769+
unsafe { &*::#prefix::ptr::from_raw_parts(ptr as *const (), len) }
2770+
}
2771+
2772+
/// Construct mutable DST variant from a pointer and a
2773+
/// size. The returned `&mut` reference is initialized
2774+
/// pointing to memory referenced by `ptr`, but there's
2775+
/// no requirement that that memory be initialized.
2776+
///
2777+
/// NOTE: lifetime of returned reference is not tied to any underlying storage.
2778+
/// SAFETY: `ptr` is valid. Underlying storage has space for at least `len` elements.
2779+
pub unsafe fn flex_ptr_mut<'unbounded>(
2780+
ptr: *mut Self,
2781+
len: usize,
2782+
) -> ::#prefix::mem::MaybeUninit<&'unbounded mut #dst_ty_for_impl> {
2783+
unsafe {
2784+
// Initialize reference without ever exposing it, as its possibly uninitialized
2785+
let mut uninit = ::#prefix::mem::MaybeUninit::<&mut #dst_ty_for_impl>::uninit();
2786+
(uninit.as_mut_ptr() as *mut *mut #dst_ty_for_impl)
2787+
.write(::#prefix::ptr::from_raw_parts_mut(ptr as *mut (), len));
2788+
2789+
uninit
2790+
}
2791+
}
2792+
},
2793+
)
2794+
} else {
2795+
(quote!(), quote!())
2796+
};
2797+
2798+
quote! {
2799+
impl #impl_generics_labels #dst_ty_for_impl {
2800+
#layout
2801+
#from_ptr_dst
2802+
}
2803+
2804+
impl #impl_generics_labels #sized_ty_for_impl {
2805+
#from_ptr_sized
2806+
}
2807+
}
2808+
}
2809+
}
2810+
27932811
impl Method {
27942812
fn codegen_method(
27952813
&self,

bindgen/ir/comp.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,10 +834,9 @@ impl CompFields {
834834
CompFields::Error => return None, // panic?
835835
};
836836

837-
// XXX correct with padding on end?
838-
match fields.last() {
839-
None | Some(Field::Bitfields(..)) => None,
840-
Some(Field::DataMember(FieldData { ty, .. })) => ctx
837+
match fields.last()? {
838+
Field::Bitfields(..) => None,
839+
Field::DataMember(FieldData { ty, .. }) => ctx
841840
.resolve_type(*ty)
842841
.is_incomplete_array(ctx)
843842
.map(|item| item.expect_type_id(ctx)),

0 commit comments

Comments
 (0)