Skip to content

Commit 7455e3b

Browse files
committed
Teach the blob code to generate i128 / u128 if available.
This is very mechanical and boring, but needed.
1 parent 9087c2f commit 7455e3b

14 files changed

+77
-76
lines changed

src/codegen/helpers.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ pub mod attributes {
5959

6060
/// Generates a proper type for a field or type with a given `Layout`, that is,
6161
/// a type with the correct size and alignment restrictions.
62-
pub fn blob(layout: Layout) -> quote::Tokens {
62+
pub fn blob(ctx: &BindgenContext, layout: Layout) -> quote::Tokens {
6363
let opaque = layout.opaque();
6464

6565
// FIXME(emilio, #412): We fall back to byte alignment, but there are
6666
// some things that legitimately are more than 8-byte aligned.
6767
//
6868
// Eventually we should be able to `unwrap` here, but...
69-
let ty_name = match opaque.known_rust_type_for_array() {
69+
let ty_name = match opaque.known_rust_type_for_array(ctx) {
7070
Some(ty) => ty,
7171
None => {
7272
warn!("Found unknown alignment on code generation!");
@@ -76,7 +76,7 @@ pub fn blob(layout: Layout) -> quote::Tokens {
7676

7777
let ty_name = Term::new(ty_name, Span::call_site());
7878

79-
let data_len = opaque.array_size().unwrap_or(layout.size);
79+
let data_len = opaque.array_size(ctx).unwrap_or(layout.size);
8080

8181
if data_len == 1 {
8282
quote! {
@@ -90,8 +90,8 @@ pub fn blob(layout: Layout) -> quote::Tokens {
9090
}
9191

9292
/// Integer type of the same size as the given `Layout`.
93-
pub fn integer_type(layout: Layout) -> Option<quote::Tokens> {
94-
let name = Layout::known_type_for_size(layout.size)?;
93+
pub fn integer_type(ctx: &BindgenContext, layout: Layout) -> Option<quote::Tokens> {
94+
let name = Layout::known_type_for_size(ctx, layout.size)?;
9595
let name = Term::new(name, Span::call_site());
9696
Some(quote! { #name })
9797
}

src/codegen/impl_debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'a> ImplDebug<'a> for Item {
236236
let inner_type = ctx.resolve_type(inner).canonical_type(ctx);
237237
match *inner_type.kind() {
238238
TypeKind::Function(ref sig)
239-
if !sig.can_trivially_derive_debug() => {
239+
if !sig.can_trivially_derive_debug(ctx) => {
240240
Some((format!("{}: FunctionPointer", name), vec![]))
241241
}
242242
_ => debug_print(name, quote! { #name_ident }),

src/codegen/mod.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ impl Bitfield {
11851185
let bitfield_ty_layout = bitfield_ty.layout(ctx).expect(
11861186
"Bitfield without layout? Gah!",
11871187
);
1188-
let bitfield_int_ty = helpers::blob(bitfield_ty_layout);
1188+
let bitfield_int_ty = helpers::blob(ctx, bitfield_ty_layout);
11891189

11901190
let offset = self.offset_into_unit();
11911191
let width = self.width() as u8;
@@ -1367,7 +1367,7 @@ impl<'a> FieldCodegen<'a> for Bitfield {
13671367
let bitfield_ty_layout = bitfield_ty.layout(ctx).expect(
13681368
"Bitfield without layout? Gah!",
13691369
);
1370-
let bitfield_int_ty = match helpers::integer_type(bitfield_ty_layout) {
1370+
let bitfield_int_ty = match helpers::integer_type(ctx, bitfield_ty_layout) {
13711371
Some(int_ty) => {
13721372
*bitfield_representable_as_int = true;
13731373
int_ty
@@ -1547,7 +1547,7 @@ impl CodeGenerator for CompInfo {
15471547
}
15481548

15491549
let layout = layout.expect("Unable to get layout information?");
1550-
let ty = helpers::blob(layout);
1550+
let ty = helpers::blob(ctx, layout);
15511551

15521552
fields.push(if self.can_be_rust_union(ctx) {
15531553
quote! {
@@ -1572,7 +1572,7 @@ impl CodeGenerator for CompInfo {
15721572
Some(l) => {
15731573
explicit_align = Some(l.align);
15741574

1575-
let ty = helpers::blob(l);
1575+
let ty = helpers::blob(ctx, l);
15761576
fields.push(quote! {
15771577
pub _bindgen_opaque_blob: #ty ,
15781578
});
@@ -1595,7 +1595,7 @@ impl CodeGenerator for CompInfo {
15951595
} else {
15961596
explicit_align = Some(layout.align);
15971597
if !ctx.options().rust_features.repr_align {
1598-
let ty = helpers::blob(Layout::new(0, layout.align));
1598+
let ty = helpers::blob(ctx, Layout::new(0, layout.align));
15991599
fields.push(quote! {
16001600
pub __bindgen_align: #ty ,
16011601
});
@@ -1629,7 +1629,7 @@ impl CodeGenerator for CompInfo {
16291629
};
16301630

16311631
if has_address {
1632-
let ty = helpers::blob(Layout::new(1, 1));
1632+
let ty = helpers::blob(ctx, Layout::new(1, 1));
16331633
fields.push(quote! {
16341634
pub _address: #ty,
16351635
});
@@ -2800,7 +2800,7 @@ trait TryToOpaque {
28002800
extra: &Self::Extra,
28012801
) -> error::Result<quote::Tokens> {
28022802
self.try_get_layout(ctx, extra).map(|layout| {
2803-
helpers::blob(layout)
2803+
helpers::blob(ctx, layout)
28042804
})
28052805
}
28062806
}
@@ -2827,7 +2827,7 @@ trait ToOpaque: TryToOpaque {
28272827
extra: &Self::Extra,
28282828
) -> quote::Tokens {
28292829
let layout = self.get_layout(ctx, extra);
2830-
helpers::blob(layout)
2830+
helpers::blob(ctx, layout)
28312831
}
28322832
}
28332833

@@ -2885,7 +2885,7 @@ where
28852885
|_| if let Ok(layout) =
28862886
self.try_get_layout(ctx, extra)
28872887
{
2888-
Ok(helpers::blob(layout))
2888+
Ok(helpers::blob(ctx, layout))
28892889
} else {
28902890
Err(error::Error::NoLayoutForOpaqueBlob)
28912891
},
@@ -3037,7 +3037,7 @@ impl TryToRustTy for Type {
30373037
IntKind::LongLong => Ok(raw_type(ctx, "c_longlong")),
30383038
IntKind::ULongLong => Ok(raw_type(ctx, "c_ulonglong")),
30393039
IntKind::WChar { size } => {
3040-
let ty = Layout::known_type_for_size(size)
3040+
let ty = Layout::known_type_for_size(ctx, size)
30413041
.expect("Non-representable wchar_t?");
30423042
let ident = ctx.rust_ident_raw(ty);
30433043
Ok(quote! { #ident })

src/codegen/struct_layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'a> StructLayoutTracker<'a> {
306306
}
307307

308308
fn padding_field(&mut self, layout: Layout) -> quote::Tokens {
309-
let ty = helpers::blob(layout);
309+
let ty = helpers::blob(self.ctx, layout);
310310
let padding_count = self.padding_count;
311311

312312
self.padding_count += 1;

src/ir/analysis/derive_copy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> {
149149

150150
if item.is_opaque(self.ctx, &()) {
151151
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
152-
l.opaque().can_trivially_derive_copy()
152+
l.opaque().can_trivially_derive_copy(self.ctx)
153153
});
154154
return if layout_can_derive {
155155
trace!(" we can trivially derive Copy for the layout");

src/ir/analysis/derive_debug.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
146146

147147
if item.is_opaque(self.ctx, &()) {
148148
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
149-
l.opaque().can_trivially_derive_debug()
149+
l.opaque().can_trivially_derive_debug(self.ctx)
150150
});
151151
return if layout_can_derive &&
152152
!(ty.is_union() &&
@@ -242,7 +242,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
242242
}
243243

244244
if ty.layout(self.ctx).map_or(true, |l| {
245-
l.opaque().can_trivially_derive_debug()
245+
l.opaque().can_trivially_derive_debug(self.ctx)
246246
})
247247
{
248248
trace!(" union layout can trivially derive Debug");
@@ -299,7 +299,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
299299
let inner_type =
300300
self.ctx.resolve_type(inner).canonical_type(self.ctx);
301301
if let TypeKind::Function(ref sig) = *inner_type.kind() {
302-
if !sig.can_trivially_derive_debug() {
302+
if !sig.can_trivially_derive_debug(self.ctx) {
303303
trace!(
304304
" function pointer that can't trivially derive Debug"
305305
);

src/ir/analysis/derive_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> {
173173

174174
if item.is_opaque(self.ctx, &()) {
175175
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
176-
l.opaque().can_trivially_derive_default()
176+
l.opaque().can_trivially_derive_default(self.ctx)
177177
});
178178
return if layout_can_derive &&
179179
!(ty.is_union() &&
@@ -278,7 +278,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> {
278278
}
279279

280280
if ty.layout(self.ctx).map_or(true, |l| {
281-
l.opaque().can_trivially_derive_default()
281+
l.opaque().can_trivially_derive_default(self.ctx)
282282
})
283283
{
284284
trace!(" union layout can trivially derive Default");

src/ir/analysis/derive_hash.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
133133

134134
if item.is_opaque(self.ctx, &()) {
135135
let layout_can_derive = ty.layout(self.ctx).map_or(true, |l| {
136-
l.opaque().can_trivially_derive_hash()
136+
l.opaque().can_trivially_derive_hash(self.ctx)
137137
});
138138
return if layout_can_derive &&
139139
!(ty.is_union() &&
@@ -218,7 +218,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
218218
let inner_type =
219219
self.ctx.resolve_type(inner).canonical_type(self.ctx);
220220
if let TypeKind::Function(ref sig) = *inner_type.kind() {
221-
if !sig.can_trivially_derive_hash() {
221+
if !sig.can_trivially_derive_hash(self.ctx) {
222222
trace!(
223223
" function pointer that can't trivially derive Hash"
224224
);
@@ -230,7 +230,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
230230
}
231231

232232
TypeKind::Function(ref sig) => {
233-
if !sig.can_trivially_derive_hash() {
233+
if !sig.can_trivially_derive_hash(self.ctx) {
234234
trace!(" function that can't trivially derive Hash");
235235
return self.insert(id);
236236
}
@@ -275,7 +275,7 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
275275
}
276276

277277
if ty.layout(self.ctx).map_or(true, |l| {
278-
l.opaque().can_trivially_derive_hash()
278+
l.opaque().can_trivially_derive_hash(self.ctx)
279279
})
280280
{
281281
trace!(" union layout can trivially derive Hash");

src/ir/analysis/derive_partialeq_or_partialord.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
129129

130130
let layout_can_derive = ty.layout(self.ctx)
131131
.map_or(CanDerive::Yes, |l| {
132-
l.opaque().can_trivially_derive_partialeq_or_partialord()
132+
l.opaque().can_trivially_derive_partialeq_or_partialord(self.ctx)
133133
});
134134

135135
match layout_can_derive {
@@ -210,7 +210,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
210210
let inner_type =
211211
self.ctx.resolve_type(inner).canonical_type(self.ctx);
212212
if let TypeKind::Function(ref sig) = *inner_type.kind() {
213-
if sig.can_trivially_derive_partialeq_or_partialord()
213+
if sig.can_trivially_derive_partialeq_or_partialord(self.ctx)
214214
!= CanDerive::Yes
215215
{
216216
trace!(
@@ -224,7 +224,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
224224
}
225225

226226
TypeKind::Function(ref sig) => {
227-
if sig.can_trivially_derive_partialeq_or_partialord()
227+
if sig.can_trivially_derive_partialeq_or_partialord(self.ctx)
228228
!= CanDerive::Yes
229229
{
230230
trace!(
@@ -258,7 +258,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
258258
let layout_can_derive =
259259
ty.layout(self.ctx).map_or(CanDerive::Yes, |l| {
260260
l.opaque()
261-
.can_trivially_derive_partialeq_or_partialord()
261+
.can_trivially_derive_partialeq_or_partialord(self.ctx)
262262
});
263263
match layout_can_derive {
264264
CanDerive::Yes => {

src/ir/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ where
226226
}
227227
}
228228

229-
impl<'a, T> CanDeriveCopy<'a> for T
229+
impl<T> CanDeriveCopy for T
230230
where
231231
T: Copy + Into<ItemId>
232232
{

src/ir/derive.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ pub trait CanDeriveDebug {
3030
pub trait CanTriviallyDeriveDebug {
3131
/// Return `true` if `Debug` can trivially be derived for this thing,
3232
/// `false` otherwise.
33-
fn can_trivially_derive_debug(&self) -> bool;
33+
fn can_trivially_derive_debug(&self, ctx: &BindgenContext) -> bool;
3434
}
3535

3636
/// A trait that encapsulates the logic for whether or not we can derive `Copy`
3737
/// for a given thing.
38-
pub trait CanDeriveCopy<'a> {
38+
pub trait CanDeriveCopy {
3939
/// Return `true` if `Copy` can be derived for this thing, `false`
4040
/// otherwise.
41-
fn can_derive_copy(&'a self, ctx: &'a BindgenContext) -> bool;
41+
fn can_derive_copy(&self, ctx: &BindgenContext) -> bool;
4242
}
4343

4444
/// A trait that encapsulates the logic for whether or not we can trivially
@@ -47,7 +47,7 @@ pub trait CanDeriveCopy<'a> {
4747
pub trait CanTriviallyDeriveCopy {
4848
/// Return `true` if `Copy` can be trivially derived for this thing, `false`
4949
/// otherwise.
50-
fn can_trivially_derive_copy(&self) -> bool;
50+
fn can_trivially_derive_copy(&self, ctx: &BindgenContext) -> bool;
5151
}
5252

5353
/// A trait that encapsulates the logic for whether or not we can derive
@@ -64,7 +64,7 @@ pub trait CanDeriveDefault {
6464
pub trait CanTriviallyDeriveDefault {
6565
/// Return `true` if `Default` can trivially derived for this thing, `false`
6666
/// otherwise.
67-
fn can_trivially_derive_default(&self) -> bool;
67+
fn can_trivially_derive_default(&self, ctx: &BindgenContext) -> bool;
6868
}
6969

7070
/// A trait that encapsulates the logic for whether or not we can derive `Hash`
@@ -111,7 +111,7 @@ pub trait CanDeriveOrd {
111111
pub trait CanTriviallyDeriveHash {
112112
/// Return `true` if `Hash` can trivially be derived for this thing, `false`
113113
/// otherwise.
114-
fn can_trivially_derive_hash(&self) -> bool;
114+
fn can_trivially_derive_hash(&self, ctx: &BindgenContext) -> bool;
115115
}
116116

117117
/// A trait that encapsulates the logic for whether or not we can trivially
@@ -120,11 +120,11 @@ pub trait CanTriviallyDeriveHash {
120120
pub trait CanTriviallyDerivePartialEqOrPartialOrd {
121121
/// Return `Yes` if `PartialEq` or `PartialOrd` can trivially be derived
122122
/// for this thing.
123-
fn can_trivially_derive_partialeq_or_partialord(&self) -> CanDerive;
123+
fn can_trivially_derive_partialeq_or_partialord(&self, ctx: &BindgenContext) -> CanDerive;
124124
}
125125

126126
/// Whether it is possible or not to automatically derive trait for an item.
127-
///
127+
///
128128
/// ```ignore
129129
/// No
130130
/// ^
@@ -134,7 +134,7 @@ pub trait CanTriviallyDerivePartialEqOrPartialOrd {
134134
/// |
135135
/// Yes
136136
/// ```
137-
///
137+
///
138138
/// Initially we assume that we can derive trait for all types and then
139139
/// update our understanding as we learn more about each type.
140140
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord)]
@@ -144,7 +144,7 @@ pub enum CanDerive {
144144

145145
/// The only thing that stops us from automatically deriving is that
146146
/// array with more than maximum number of elements is used.
147-
///
147+
///
148148
/// This means we probably can "manually" implement such trait.
149149
ArrayTooLarge,
150150

src/ir/function.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -581,26 +581,23 @@ impl Trace for FunctionSig {
581581
}
582582

583583
impl CanTriviallyDeriveDebug for FunctionSig {
584-
fn can_trivially_derive_debug(&self) -> bool {
584+
fn can_trivially_derive_debug(&self, _: &BindgenContext) -> bool {
585585
self.function_pointers_can_derive()
586586
}
587587
}
588588

589589
impl CanTriviallyDeriveHash for FunctionSig {
590-
fn can_trivially_derive_hash(&self) -> bool {
590+
fn can_trivially_derive_hash(&self, _: &BindgenContext) -> bool {
591591
self.function_pointers_can_derive()
592592
}
593593
}
594594

595595
impl CanTriviallyDerivePartialEqOrPartialOrd for FunctionSig {
596-
fn can_trivially_derive_partialeq_or_partialord(&self) -> CanDerive {
597-
if self.argument_types.len() > RUST_DERIVE_FUNPTR_LIMIT {
598-
return CanDerive::No;
599-
}
600-
601-
match self.abi {
602-
Abi::C | Abi::Unknown(..) => CanDerive::Yes,
603-
_ => CanDerive::No,
596+
fn can_trivially_derive_partialeq_or_partialord(&self, _: &BindgenContext) -> CanDerive {
597+
if self.function_pointers_can_derive() {
598+
CanDerive::Yes
599+
} else {
600+
CanDerive::No
604601
}
605602
}
606603
}

0 commit comments

Comments
 (0)