Skip to content

Commit cdaa002

Browse files
committed
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 67b12a7 commit cdaa002

File tree

1 file changed

+52
-56
lines changed

1 file changed

+52
-56
lines changed

bindgen/codegen/mod.rs

Lines changed: 52 additions & 56 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) {
@@ -2481,28 +2478,23 @@ impl CodeGenerator for CompInfo {
24812478

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

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

25082500
// The custom derives callback may return a list of derive attributes;
@@ -2700,6 +2692,7 @@ impl CodeGenerator for CompInfo {
27002692
}
27012693

27022694
let mut method_names = Default::default();
2695+
let discovered_id = DiscoveredItemId::new(item.id().as_usize());
27032696
if ctx.options().codegen_config.methods() {
27042697
for method in self.methods() {
27052698
assert_ne!(method.kind(), MethodKind::Constructor);
@@ -3021,7 +3014,6 @@ impl Method {
30213014

30223015
// First of all, output the actual function.
30233016
let function_item = ctx.resolve_item(self.signature());
3024-
let id = DiscoveredItemId::new(function_item.id().as_usize());
30253017
if !function_item.process_before_codegen(ctx, result) {
30263018
return;
30273019
}
@@ -3068,14 +3060,11 @@ impl Method {
30683060

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

3071-
ctx.options().for_each_callback(|cb| {
3072-
cb.new_item_found(
3073-
id,
3074-
DiscoveredItem::Method {
3075-
parent: parent_id,
3076-
final_name: name.clone(),
3077-
},
3078-
)
3063+
utils::call_discovered_item_callback(ctx, function_item, || {
3064+
DiscoveredItem::Method {
3065+
parent: parent_id,
3066+
final_name: name.clone(),
3067+
}
30793068
});
30803069

30813070
let mut function_name = function_item.canonical_name(ctx);
@@ -3783,13 +3772,10 @@ impl CodeGenerator for Enum {
37833772
let repr = repr.to_rust_ty_or_opaque(ctx, item);
37843773
let has_typedef = ctx.is_enum_typedef_combo(item.id());
37853774

3786-
ctx.options().for_each_callback(|cb| {
3787-
cb.new_item_found(
3788-
DiscoveredItemId::new(item.id().as_usize()),
3789-
DiscoveredItem::Enum {
3790-
final_name: name.to_string(),
3791-
},
3792-
);
3775+
utils::call_discovered_item_callback(ctx, item, || {
3776+
DiscoveredItem::Enum {
3777+
final_name: name.to_string(),
3778+
}
37933779
});
37943780

37953781
let mut builder =
@@ -4553,7 +4539,6 @@ impl CodeGenerator for Function {
45534539
) -> Self::Return {
45544540
debug!("<Function as CodeGenerator>::codegen: item = {item:?}");
45554541
debug_assert!(item.is_enabled_for_codegen(ctx));
4556-
let id = DiscoveredItemId::new(item.id().as_usize());
45574542

45584543
let is_internal = matches!(self.linkage(), Linkage::Internal);
45594544

@@ -4664,13 +4649,10 @@ impl CodeGenerator for Function {
46644649
if times_seen > 0 {
46654650
write!(&mut canonical_name, "{times_seen}").unwrap();
46664651
}
4667-
ctx.options().for_each_callback(|cb| {
4668-
cb.new_item_found(
4669-
id,
4670-
DiscoveredItem::Function {
4671-
final_name: canonical_name.to_string(),
4672-
},
4673-
);
4652+
utils::call_discovered_item_callback(ctx, item, || {
4653+
DiscoveredItem::Function {
4654+
final_name: canonical_name.to_string(),
4655+
}
46744656
});
46754657

46764658
let link_name_attr = self.link_name().or_else(|| {
@@ -5211,6 +5193,7 @@ pub(crate) mod utils {
52115193
use super::helpers::BITFIELD_UNIT;
52125194
use super::serialize::CSerialize;
52135195
use super::{error, CodegenError, CodegenResult, ToRustTyOrOpaque};
5196+
use crate::callbacks::DiscoveredItemId;
52145197
use crate::ir::context::BindgenContext;
52155198
use crate::ir::context::TypeId;
52165199
use crate::ir::function::{Abi, ClangAbi, FunctionSig};
@@ -5940,4 +5923,17 @@ pub(crate) mod utils {
59405923

59415924
true
59425925
}
5926+
5927+
pub(super) fn call_discovered_item_callback(
5928+
ctx: &BindgenContext,
5929+
item: &Item,
5930+
discovered_item_creator: impl Fn() -> crate::callbacks::DiscoveredItem,
5931+
) {
5932+
ctx.options().for_each_callback(|cb| {
5933+
cb.new_item_found(
5934+
DiscoveredItemId::new(item.id().as_usize()),
5935+
discovered_item_creator(),
5936+
);
5937+
});
5938+
}
59435939
}

0 commit comments

Comments
 (0)