Skip to content

Commit 83751c8

Browse files
flieremilio
authored andcommitted
generate type alias for the block type
1 parent f5368b3 commit 83751c8

19 files changed

+177
-52
lines changed

src/clang.rs

+1
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ impl Type {
940940
CXType_RValueReference |
941941
CXType_LValueReference |
942942
CXType_MemberPointer |
943+
CXType_BlockPointer |
943944
CXType_ObjCObjectPointer => {
944945
let ret = Type {
945946
x: unsafe { clang_getPointeeType(self.x) },

src/codegen/impl_debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ impl<'a> ImplDebug<'a> for Item {
156156
TypeKind::Function(..) |
157157
TypeKind::Enum(..) |
158158
TypeKind::Reference(..) |
159-
TypeKind::BlockPointer |
160159
TypeKind::UnresolvedTypeRef(..) |
161160
TypeKind::ObjCInterface(..) |
162161
TypeKind::ObjCId |
@@ -227,7 +226,8 @@ impl<'a> ImplDebug<'a> for Item {
227226

228227
TypeKind::ResolvedTypeRef(t) |
229228
TypeKind::TemplateAlias(t, _) |
230-
TypeKind::Alias(t) => {
229+
TypeKind::Alias(t) |
230+
TypeKind::BlockPointer(t) => {
231231
// We follow the aliases
232232
ctx.resolve_item(t).impl_debug(ctx, name)
233233
}

src/codegen/impl_partialeq.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ fn gen_field(ctx: &BindgenContext, ty_item: &Item, name: &str) -> quote::Tokens
8888
TypeKind::Enum(..) |
8989
TypeKind::TypeParam |
9090
TypeKind::UnresolvedTypeRef(..) |
91-
TypeKind::BlockPointer |
9291
TypeKind::Reference(..) |
9392
TypeKind::ObjCInterface(..) |
9493
TypeKind::ObjCId |
@@ -125,7 +124,8 @@ fn gen_field(ctx: &BindgenContext, ty_item: &Item, name: &str) -> quote::Tokens
125124

126125
TypeKind::ResolvedTypeRef(t) |
127126
TypeKind::TemplateAlias(t, _) |
128-
TypeKind::Alias(t) => {
127+
TypeKind::Alias(t) |
128+
TypeKind::BlockPointer(t) => {
129129
let inner_item = ctx.resolve_item(t);
130130
gen_field(ctx, inner_item, name)
131131
}

src/codegen/mod.rs

+85-16
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ struct CodegenResult<'a> {
103103
/// Whether Objective C types have been seen at least once.
104104
saw_objc: bool,
105105

106+
/// Whether Apple block types have been seen at least once.
107+
saw_block: bool,
108+
106109
/// Whether a bitfield allocation unit has been seen at least once.
107110
saw_bitfield_unit: bool,
108111

@@ -140,6 +143,7 @@ impl<'a> CodegenResult<'a> {
140143
saw_bindgen_union: false,
141144
saw_incomplete_array: false,
142145
saw_objc: false,
146+
saw_block: false,
143147
saw_bitfield_unit: false,
144148
codegen_id: codegen_id,
145149
items_seen: Default::default(),
@@ -166,6 +170,10 @@ impl<'a> CodegenResult<'a> {
166170
self.saw_objc = true;
167171
}
168172

173+
fn saw_block(&mut self) {
174+
self.saw_block = true;
175+
}
176+
169177
fn saw_bitfield_unit(&mut self) {
170178
self.saw_bitfield_unit = true;
171179
}
@@ -215,6 +223,7 @@ impl<'a> CodegenResult<'a> {
215223
self.saw_union |= new.saw_union;
216224
self.saw_incomplete_array |= new.saw_incomplete_array;
217225
self.saw_objc |= new.saw_objc;
226+
self.saw_block |= new.saw_block;
218227
self.saw_bitfield_unit |= new.saw_bitfield_unit;
219228

220229
new.items
@@ -293,7 +302,6 @@ impl AppendImplicitTemplateParams for quote::Tokens {
293302
TypeKind::Opaque |
294303
TypeKind::Function(..) |
295304
TypeKind::Enum(..) |
296-
TypeKind::BlockPointer |
297305
TypeKind::ObjCId |
298306
TypeKind::ObjCSel |
299307
TypeKind::TemplateInstantiation(..) => return,
@@ -394,6 +402,9 @@ impl CodeGenerator for Module {
394402
}
395403

396404
if item.id() == ctx.root_module() {
405+
if result.saw_block {
406+
utils::prepend_block_header(ctx, &mut *result);
407+
}
397408
if result.saw_bindgen_union {
398409
utils::prepend_union_types(ctx, &mut *result);
399410
}
@@ -597,7 +608,6 @@ impl CodeGenerator for Type {
597608
TypeKind::Array(..) |
598609
TypeKind::Vector(..) |
599610
TypeKind::Pointer(..) |
600-
TypeKind::BlockPointer |
601611
TypeKind::Reference(..) |
602612
TypeKind::Function(..) |
603613
TypeKind::ResolvedTypeRef(..) |
@@ -610,6 +620,35 @@ impl CodeGenerator for Type {
610620
TypeKind::TemplateInstantiation(ref inst) => {
611621
inst.codegen(ctx, result, item)
612622
}
623+
TypeKind::BlockPointer(inner) => {
624+
let inner_item = inner.into_resolver()
625+
.through_type_refs()
626+
.resolve(ctx);
627+
let name = item.canonical_name(ctx);
628+
629+
let inner_rust_type = {
630+
if let TypeKind::Function(fnsig) = inner_item.kind().expect_type().kind() {
631+
utils::fnsig_block(ctx, fnsig)
632+
} else {
633+
panic!("invalid block typedef: {:?}", inner_item)
634+
}
635+
};
636+
637+
let rust_name = ctx.rust_ident(&name);
638+
639+
let mut tokens = if let Some(comment) = item.comment(ctx) {
640+
attributes::doc(comment)
641+
} else {
642+
quote! {}
643+
};
644+
645+
tokens.append_all(quote! {
646+
pub type #rust_name = #inner_rust_type ;
647+
});
648+
649+
result.push(tokens);
650+
result.saw_block();
651+
}
613652
TypeKind::Comp(ref ci) => ci.codegen(ctx, result, item),
614653
TypeKind::TemplateAlias(inner, _) |
615654
TypeKind::Alias(inner) => {
@@ -3071,20 +3110,16 @@ impl TryToRustTy for Type {
30713110
}
30723111
TypeKind::ResolvedTypeRef(inner) => inner.try_to_rust_ty(ctx, &()),
30733112
TypeKind::TemplateAlias(..) |
3074-
TypeKind::Alias(..) => {
3113+
TypeKind::Alias(..) |
3114+
TypeKind::BlockPointer(..) => {
30753115
let template_params = item.used_template_params(ctx)
30763116
.into_iter()
30773117
.filter(|param| param.is_template_param(ctx, &()))
30783118
.collect::<Vec<_>>();
30793119

3080-
let spelling = self.name().expect("Unnamed alias?");
30813120
if item.is_opaque(ctx, &()) && !template_params.is_empty() {
30823121
self.try_to_opaque(ctx, item)
3083-
} else if let Some(ty) = utils::type_from_named(
3084-
ctx,
3085-
spelling,
3086-
)
3087-
{
3122+
} else if let Some(ty) = self.name().and_then(|name| utils::type_from_named(ctx, name)) {
30883123
Ok(ty)
30893124
} else {
30903125
utils::build_path(item, ctx)
@@ -3101,13 +3136,6 @@ impl TryToRustTy for Type {
31013136
utils::build_path(item, ctx)
31023137
}
31033138
TypeKind::Opaque => self.try_to_opaque(ctx, item),
3104-
TypeKind::BlockPointer => {
3105-
let void = raw_type(ctx, "c_void");
3106-
Ok(void.to_ptr(
3107-
/* is_const = */
3108-
false
3109-
))
3110-
}
31113139
TypeKind::Pointer(inner) |
31123140
TypeKind::Reference(inner) => {
31133141
let is_const = ctx.resolve_type(inner).is_const();
@@ -3560,6 +3588,25 @@ mod utils {
35603588
result.extend(old_items.into_iter());
35613589
}
35623590

3591+
pub fn prepend_block_header(
3592+
ctx: &BindgenContext,
3593+
result: &mut Vec<quote::Tokens>,
3594+
) {
3595+
let use_block = if ctx.options().block_extern_crate {
3596+
quote! {
3597+
extern crate block;
3598+
}
3599+
} else {
3600+
quote! {
3601+
use block;
3602+
}
3603+
};
3604+
3605+
let items = vec![use_block];
3606+
let old_items = mem::replace(result, items);
3607+
result.extend(old_items.into_iter());
3608+
}
3609+
35633610
pub fn prepend_union_types(
35643611
ctx: &BindgenContext,
35653612
result: &mut Vec<quote::Tokens>,
@@ -3871,4 +3918,26 @@ mod utils {
38713918

38723919
args
38733920
}
3921+
3922+
pub fn fnsig_block(
3923+
ctx: &BindgenContext,
3924+
sig: &FunctionSig,
3925+
) -> quote::Tokens {
3926+
let args = sig.argument_types().iter().map(|&(_, ty)| {
3927+
let arg_item = ctx.resolve_item(ty);
3928+
3929+
arg_item.to_rust_ty_or_opaque(ctx, &())
3930+
});
3931+
3932+
let return_item = ctx.resolve_item(sig.return_type());
3933+
let ret_ty = if let TypeKind::Void = *return_item.kind().expect_type().kind() {
3934+
quote! { () }
3935+
} else {
3936+
return_item.to_rust_ty_or_opaque(ctx, &())
3937+
};
3938+
3939+
quote! {
3940+
*const ::block::Block<(#(#args),*), #ret_ty>
3941+
}
3942+
}
38743943
}

src/ir/analysis/derive_copy.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> {
173173
TypeKind::Enum(..) |
174174
TypeKind::Reference(..) |
175175
TypeKind::TypeParam |
176-
TypeKind::BlockPointer |
177176
TypeKind::Pointer(..) |
178177
TypeKind::UnresolvedTypeRef(..) |
179178
TypeKind::ObjCInterface(..) |
@@ -204,7 +203,8 @@ impl<'ctx> MonotoneFramework for CannotDeriveCopy<'ctx> {
204203

205204
TypeKind::ResolvedTypeRef(t) |
206205
TypeKind::TemplateAlias(t, _) |
207-
TypeKind::Alias(t) => {
206+
TypeKind::Alias(t) |
207+
TypeKind::BlockPointer(t) => {
208208
let cant_derive_copy = self.is_not_copy(t);
209209
if cant_derive_copy {
210210
trace!(

src/ir/analysis/derive_debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
183183
TypeKind::Enum(..) |
184184
TypeKind::Reference(..) |
185185
TypeKind::Vector(..) |
186-
TypeKind::BlockPointer |
187186
TypeKind::TypeParam |
188187
TypeKind::UnresolvedTypeRef(..) |
189188
TypeKind::ObjCInterface(..) |
@@ -213,7 +212,8 @@ impl<'ctx> MonotoneFramework for CannotDeriveDebug<'ctx> {
213212

214213
TypeKind::ResolvedTypeRef(t) |
215214
TypeKind::TemplateAlias(t, _) |
216-
TypeKind::Alias(t) => {
215+
TypeKind::Alias(t) |
216+
TypeKind::BlockPointer(t) => {
217217
if self.is_not_debug(t) {
218218
trace!(
219219
" aliases and type refs to T which cannot derive \

src/ir/analysis/derive_default.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> {
215215
TypeKind::Reference(..) |
216216
TypeKind::NullPtr |
217217
TypeKind::Pointer(..) |
218-
TypeKind::BlockPointer |
219218
TypeKind::ObjCId |
220219
TypeKind::ObjCSel |
221220
TypeKind::ObjCInterface(..) |
@@ -244,7 +243,8 @@ impl<'ctx> MonotoneFramework for CannotDeriveDefault<'ctx> {
244243

245244
TypeKind::ResolvedTypeRef(t) |
246245
TypeKind::TemplateAlias(t, _) |
247-
TypeKind::Alias(t) => {
246+
TypeKind::Alias(t) |
247+
TypeKind::BlockPointer(t) => {
248248
if self.is_not_default(t) {
249249
trace!(
250250
" aliases and type refs to T which cannot derive \

src/ir/analysis/derive_hash.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
167167
TypeKind::Enum(..) |
168168
TypeKind::TypeParam |
169169
TypeKind::UnresolvedTypeRef(..) |
170-
TypeKind::BlockPointer |
171170
TypeKind::Reference(..) |
172171
TypeKind::ObjCInterface(..) |
173172
TypeKind::ObjCId |
@@ -241,7 +240,8 @@ impl<'ctx> MonotoneFramework for CannotDeriveHash<'ctx> {
241240

242241
TypeKind::ResolvedTypeRef(t) |
243242
TypeKind::TemplateAlias(t, _) |
244-
TypeKind::Alias(t) => {
243+
TypeKind::Alias(t) |
244+
TypeKind::BlockPointer(t) => {
245245
if self.cannot_derive_hash.contains(&t.into()) {
246246
trace!(
247247
" aliases and type refs to T which cannot derive \

src/ir/analysis/derive_partialeq_or_partialord.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::collections::hash_map::Entry;
4343
pub struct CannotDerivePartialEqOrPartialOrd<'ctx> {
4444
ctx: &'ctx BindgenContext,
4545

46-
// The incremental result of this analysis's computation.
46+
// The incremental result of this analysis's computation.
4747
// Contains information whether particular item can derive `PartialEq`/`PartialOrd`.
4848
can_derive_partialeq_or_partialord: HashMap<ItemId, CanDerive>,
4949

@@ -158,7 +158,6 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
158158
TypeKind::Enum(..) |
159159
TypeKind::TypeParam |
160160
TypeKind::UnresolvedTypeRef(..) |
161-
TypeKind::BlockPointer |
162161
TypeKind::Reference(..) |
163162
TypeKind::ObjCInterface(..) |
164163
TypeKind::ObjCId |
@@ -281,6 +280,7 @@ impl<'ctx> CannotDerivePartialEqOrPartialOrd<'ctx> {
281280
TypeKind::ResolvedTypeRef(..) |
282281
TypeKind::TemplateAlias(..) |
283282
TypeKind::Alias(..) |
283+
TypeKind::BlockPointer(..) |
284284
TypeKind::TemplateInstantiation(..) => {
285285
return self.constrain_join(item);
286286
}

src/ir/analysis/has_float.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ impl<'ctx> MonotoneFramework for HasFloat<'ctx> {
122122
TypeKind::Function(..) |
123123
TypeKind::Enum(..) |
124124
TypeKind::Reference(..) |
125-
TypeKind::BlockPointer |
126125
TypeKind::TypeParam |
127126
TypeKind::Opaque |
128127
TypeKind::Pointer(..) |
@@ -159,7 +158,8 @@ impl<'ctx> MonotoneFramework for HasFloat<'ctx> {
159158

160159
TypeKind::ResolvedTypeRef(t) |
161160
TypeKind::TemplateAlias(t, _) |
162-
TypeKind::Alias(t) => {
161+
TypeKind::Alias(t) |
162+
TypeKind::BlockPointer(t) => {
163163
if self.has_float.contains(&t.into()) {
164164
trace!(" aliases and type refs to T which have float \
165165
also have float");

src/ir/analysis/has_type_param_in_array.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ impl<'ctx> MonotoneFramework for HasTypeParameterInArray<'ctx> {
135135
TypeKind::Function(..) |
136136
TypeKind::Enum(..) |
137137
TypeKind::Reference(..) |
138-
TypeKind::BlockPointer |
139138
TypeKind::TypeParam |
140139
TypeKind::Opaque |
141140
TypeKind::Pointer(..) |
@@ -166,7 +165,8 @@ impl<'ctx> MonotoneFramework for HasTypeParameterInArray<'ctx> {
166165

167166
TypeKind::ResolvedTypeRef(t) |
168167
TypeKind::TemplateAlias(t, _) |
169-
TypeKind::Alias(t) => {
168+
TypeKind::Alias(t) |
169+
TypeKind::BlockPointer(t) => {
170170
if self.has_type_parameter_in_array.contains(&t.into()) {
171171
trace!(
172172
" aliases and type refs to T which have array \

src/ir/analysis/sizedness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,6 @@ impl<'ctx> MonotoneFramework for SizednessAnalysis<'ctx> {
261261
TypeKind::Enum(..) |
262262
TypeKind::Reference(..) |
263263
TypeKind::NullPtr |
264-
TypeKind::BlockPointer |
265264
TypeKind::ObjCId |
266265
TypeKind::ObjCSel |
267266
TypeKind::Pointer(..) => {
@@ -276,6 +275,7 @@ impl<'ctx> MonotoneFramework for SizednessAnalysis<'ctx> {
276275

277276
TypeKind::TemplateAlias(t, _) |
278277
TypeKind::Alias(t) |
278+
TypeKind::BlockPointer(t) |
279279
TypeKind::ResolvedTypeRef(t) => {
280280
trace!(" aliases and type refs forward to their inner type");
281281
self.forward(t, id)

0 commit comments

Comments
 (0)