Debian Codegraph @

.cargo/vendor/bindgen/ir/layout.rs

raw
1 //! Intermediate representation for the physical layout of some type.
2
3 use super::derive::CanDerive;
4 use super::ty::{Type, TypeKind, RUST_DERIVE_IN_ARRAY_LIMIT};
5 use crate::clang;
6 use crate::ir::context::BindgenContext;
7 use std::cmp;
8
9 /// A type that represents the struct layout of a type.
10 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
11 pub(crate) struct Layout {
12 /// The size (in bytes) of this layout.
13 pub(crate) size: usize,
14 /// The alignment (in bytes) of this layout.
15 pub(crate) align: usize,
16 /// Whether this layout's members are packed or not.
17 pub(crate) packed: bool,
18 }
19
20 #[test]
21 fn test_layout_for_size() {
22 use std::mem;
23
24 let ptr_size = mem::size_of::<*mut ()>();
25 assert_eq!(
26 Layout::for_size_internal(ptr_size, ptr_size),
27 Layout::new(ptr_size, ptr_size)
28 );
29 assert_eq!(
30 Layout::for_size_internal(ptr_size, 3 * ptr_size),
31 Layout::new(3 * ptr_size, ptr_size)
32 );
33 }
34
35 impl Layout {
36 /// Gets the integer type name for a given known size.
37 pub(crate) fn known_type_for_size(
38 ctx: &BindgenContext,
39 size: usize,
40 ) -> Option<&'static str> {
41 Some(match size {
42 16 if ctx.options().rust_features.i128_and_u128 => "u128",
43 8 => "u64",
44 4 => "u32",
45 2 => "u16",
46 1 => "u8",
47 _ => return None,
48 })
49 }
50
51 /// Construct a new `Layout` with the given `size` and `align`. It is not
52 /// packed.
53 pub(crate) fn new(size: usize, align: usize) -> Self {
54 Layout {
55 size,
56 align,
57 packed: false,
58 }
59 }
60
61 fn for_size_internal(ptr_size: usize, size: usize) -> Self {
62 let mut next_align = 2;
63 while size % next_align == 0 && next_align <= ptr_size {
64 next_align *= 2;
65 }
66 Layout {
67 size,
68 align: next_align / 2,
69 packed: false,
70 }
71 }
72
73 /// Creates a non-packed layout for a given size, trying to use the maximum
74 /// alignment possible.
75 pub(crate) fn for_size(ctx: &BindgenContext, size: usize) -> Self {
76 Self::for_size_internal(ctx.target_pointer_size(), size)
77 }
78
79 /// Get this layout as an opaque type.
80 pub(crate) fn opaque(&self) -> Opaque {
81 Opaque(*self)
82 }
83 }
84
85 /// When we are treating a type as opaque, it is just a blob with a `Layout`.
86 #[derive(Clone, Debug, PartialEq, Eq)]
87 pub(crate) struct Opaque(pub(crate) Layout);
88
89 impl Opaque {
90 /// Construct a new opaque type from the given clang type.
91 pub(crate) fn from_clang_ty(
92 ty: &clang::Type,
93 ctx: &BindgenContext,
94 ) -> Type {
95 let layout = Layout::new(ty.size(ctx), ty.align(ctx));
96 let ty_kind = TypeKind::Opaque;
97 let is_const = ty.is_const();
98 Type::new(None, Some(layout), ty_kind, is_const)
99 }
100
101 /// Return the known rust type we should use to create a correctly-aligned
102 /// field with this layout.
103 pub(crate) fn known_rust_type_for_array(
104 &self,
105 ctx: &BindgenContext,
106 ) -> Option<&'static str> {
107 Layout::known_type_for_size(ctx, self.0.align)
108 }
109
110 /// Return the array size that an opaque type for this layout should have if
111 /// we know the correct type for it, or `None` otherwise.
112 pub(crate) fn array_size(&self, ctx: &BindgenContext) -> Option<usize> {
113 if self.known_rust_type_for_array(ctx).is_some() {
114 Some(self.0.size / cmp::max(self.0.align, 1))
115 } else {
116 None
117 }
118 }
119
120 /// Return `true` if this opaque layout's array size will fit within the
121 /// maximum number of array elements that Rust allows deriving traits
122 /// with. Return `false` otherwise.
123 pub(crate) fn array_size_within_derive_limit(
124 &self,
125 ctx: &BindgenContext,
126 ) -> CanDerive {
127 if self
128 .array_size(ctx)
129 .map_or(false, |size| size <= RUST_DERIVE_IN_ARRAY_LIMIT)
130 {
131 CanDerive::Yes
132 } else {
133 CanDerive::Manually
134 }
135 }
136 }