Skip to content

Commit 944f6ad

Browse files
adetaylormmdriley
authored andcommitted
Deduplicate logic to call discovery callback.
No functional change - just deduplicating the logic which calls this callback, which will make it easier to make further changes in future. Part of google/autocxx#124
1 parent 1f02556 commit 944f6ad

File tree

1 file changed

+49
-50
lines changed

1 file changed

+49
-50
lines changed

bindgen/codegen/mod.rs

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -995,16 +995,13 @@ impl CodeGenerator for Type {
995995

996996
let rust_name = ctx.rust_ident(&name);
997997

998-
ctx.options().for_each_callback(|cb| {
999-
cb.new_item_found(
1000-
DiscoveredItemId::new(item.id().as_usize()),
1001-
DiscoveredItem::Alias {
1002-
alias_name: rust_name.to_string(),
1003-
alias_for: DiscoveredItemId::new(
1004-
inner_item.id().as_usize(),
1005-
),
1006-
},
1007-
);
998+
utils::call_discovered_item_callback(ctx, item, || {
999+
DiscoveredItem::Alias {
1000+
alias_name: rust_name.to_string(),
1001+
alias_for: DiscoveredItemId::new(
1002+
inner_item.id().as_usize(),
1003+
),
1004+
}
10081005
});
10091006

10101007
let mut tokens = if let Some(comment) = item.comment(ctx) {
@@ -2480,28 +2477,23 @@ impl CodeGenerator for CompInfo {
24802477

24812478
let is_rust_union = is_union && struct_layout.is_rust_union();
24822479

2483-
let discovered_id = DiscoveredItemId::new(item.id().as_usize());
2484-
ctx.options().for_each_callback(|cb| {
2485-
let discovered_item = match self.kind() {
2486-
CompKind::Struct => DiscoveredItem::Struct {
2487-
original_name: item
2488-
.kind()
2489-
.expect_type()
2490-
.name()
2491-
.map(String::from),
2492-
final_name: canonical_ident.to_string(),
2493-
},
2494-
CompKind::Union => DiscoveredItem::Union {
2495-
original_name: item
2496-
.kind()
2497-
.expect_type()
2498-
.name()
2499-
.map(String::from),
2500-
final_name: canonical_ident.to_string(),
2501-
},
2502-
};
2503-
2504-
cb.new_item_found(discovered_id, discovered_item);
2480+
utils::call_discovered_item_callback(ctx, item, || match self.kind() {
2481+
CompKind::Struct => DiscoveredItem::Struct {
2482+
original_name: item
2483+
.kind()
2484+
.expect_type()
2485+
.name()
2486+
.map(String::from),
2487+
final_name: canonical_ident.to_string(),
2488+
},
2489+
CompKind::Union => DiscoveredItem::Union {
2490+
original_name: item
2491+
.kind()
2492+
.expect_type()
2493+
.name()
2494+
.map(String::from),
2495+
final_name: canonical_ident.to_string(),
2496+
},
25052497
});
25062498

25072499
// The custom derives callback may return a list of derive attributes;
@@ -2699,6 +2691,7 @@ impl CodeGenerator for CompInfo {
26992691
}
27002692

27012693
let mut method_names = Default::default();
2694+
let discovered_id = DiscoveredItemId::new(item.id().as_usize());
27022695
if ctx.options().codegen_config.methods() {
27032696
for method in self.methods() {
27042697
assert_ne!(method.kind(), MethodKind::Constructor);
@@ -3020,7 +3013,6 @@ impl Method {
30203013

30213014
// First of all, output the actual function.
30223015
let function_item = ctx.resolve_item(self.signature());
3023-
let id = DiscoveredItemId::new(function_item.id().as_usize());
30243016
if !function_item.process_before_codegen(ctx, result) {
30253017
return;
30263018
}
@@ -3067,8 +3059,8 @@ impl Method {
30673059

30683060
method_names.insert(name.clone());
30693061

3070-
ctx.options().for_each_callback(|cb| {
3071-
cb.new_item_found(
3062+
utils::call_discovered_item_callback(ctx, function_item, || {
3063+
DiscoveredItem::Method {
30723064
id,
30733065
DiscoveredItem::Method {
30743066
parent: parent_id,
@@ -3804,13 +3796,10 @@ impl CodeGenerator for Enum {
38043796
let repr = repr.to_rust_ty_or_opaque(ctx, item);
38053797
let has_typedef = ctx.is_enum_typedef_combo(item.id());
38063798

3807-
ctx.options().for_each_callback(|cb| {
3808-
cb.new_item_found(
3809-
DiscoveredItemId::new(item.id().as_usize()),
3810-
DiscoveredItem::Enum {
3811-
final_name: name.to_string(),
3812-
},
3813-
);
3799+
utils::call_discovered_item_callback(ctx, item, || {
3800+
DiscoveredItem::Enum {
3801+
final_name: name.to_string(),
3802+
}
38143803
});
38153804

38163805
let mut builder = EnumBuilder::new(
@@ -4595,7 +4584,6 @@ impl CodeGenerator for Function {
45954584
) -> Self::Return {
45964585
debug!("<Function as CodeGenerator>::codegen: item = {item:?}");
45974586
debug_assert!(item.is_enabled_for_codegen(ctx));
4598-
let id = DiscoveredItemId::new(item.id().as_usize());
45994587

46004588
let is_internal = matches!(self.linkage(), Linkage::Internal);
46014589

@@ -4712,13 +4700,10 @@ impl CodeGenerator for Function {
47124700
if times_seen > 0 {
47134701
write!(&mut canonical_name, "{times_seen}").unwrap();
47144702
}
4715-
ctx.options().for_each_callback(|cb| {
4716-
cb.new_item_found(
4717-
id,
4718-
DiscoveredItem::Function {
4719-
final_name: canonical_name.to_string(),
4720-
},
4721-
);
4703+
utils::call_discovered_item_callback(ctx, item, || {
4704+
DiscoveredItem::Function {
4705+
final_name: canonical_name.to_string(),
4706+
}
47224707
});
47234708

47244709
let link_name_attr = self.link_name().or_else(|| {
@@ -5259,6 +5244,7 @@ pub(crate) mod utils {
52595244
use super::helpers::BITFIELD_UNIT;
52605245
use super::serialize::CSerialize;
52615246
use super::{error, CodegenError, CodegenResult, ToRustTyOrOpaque};
5247+
use crate::callbacks::DiscoveredItemId;
52625248
use crate::ir::context::BindgenContext;
52635249
use crate::ir::context::TypeId;
52645250
use crate::ir::function::{Abi, ClangAbi, FunctionSig};
@@ -5988,4 +5974,17 @@ pub(crate) mod utils {
59885974

59895975
true
59905976
}
5977+
5978+
pub(super) fn call_discovered_item_callback(
5979+
ctx: &BindgenContext,
5980+
item: &Item,
5981+
discovered_item_creator: impl Fn() -> crate::callbacks::DiscoveredItem,
5982+
) {
5983+
ctx.options().for_each_callback(|cb| {
5984+
cb.new_item_found(
5985+
DiscoveredItemId::new(item.id().as_usize()),
5986+
discovered_item_creator(),
5987+
);
5988+
});
5989+
}
59915990
}

0 commit comments

Comments
 (0)