Skip to content

Commit

Permalink
Deduplicate logic to call discovery callback.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
adetaylor committed Feb 20, 2025
1 parent 67b12a7 commit cdaa002
Showing 1 changed file with 52 additions and 56 deletions.
108 changes: 52 additions & 56 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,16 +995,13 @@ impl CodeGenerator for Type {

let rust_name = ctx.rust_ident(&name);

ctx.options().for_each_callback(|cb| {
cb.new_item_found(
DiscoveredItemId::new(item.id().as_usize()),
DiscoveredItem::Alias {
alias_name: rust_name.to_string(),
alias_for: DiscoveredItemId::new(
inner_item.id().as_usize(),
),
},
);
utils::call_discovered_item_callback(ctx, item, || {
DiscoveredItem::Alias {
alias_name: rust_name.to_string(),
alias_for: DiscoveredItemId::new(
inner_item.id().as_usize(),
),
}
});

let mut tokens = if let Some(comment) = item.comment(ctx) {
Expand Down Expand Up @@ -2481,28 +2478,23 @@ impl CodeGenerator for CompInfo {

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

let discovered_id = DiscoveredItemId::new(item.id().as_usize());
ctx.options().for_each_callback(|cb| {
let discovered_item = match self.kind() {
CompKind::Struct => DiscoveredItem::Struct {
original_name: item
.kind()
.expect_type()
.name()
.map(String::from),
final_name: canonical_ident.to_string(),
},
CompKind::Union => DiscoveredItem::Union {
original_name: item
.kind()
.expect_type()
.name()
.map(String::from),
final_name: canonical_ident.to_string(),
},
};

cb.new_item_found(discovered_id, discovered_item);
utils::call_discovered_item_callback(ctx, item, || match self.kind() {
CompKind::Struct => DiscoveredItem::Struct {
original_name: item
.kind()
.expect_type()
.name()
.map(String::from),
final_name: canonical_ident.to_string(),
},
CompKind::Union => DiscoveredItem::Union {
original_name: item
.kind()
.expect_type()
.name()
.map(String::from),
final_name: canonical_ident.to_string(),
},
});

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

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

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

method_names.insert(name.clone());

ctx.options().for_each_callback(|cb| {
cb.new_item_found(
id,
DiscoveredItem::Method {
parent: parent_id,
final_name: name.clone(),
},
)
utils::call_discovered_item_callback(ctx, function_item, || {
DiscoveredItem::Method {
parent: parent_id,
final_name: name.clone(),
}
});

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

ctx.options().for_each_callback(|cb| {
cb.new_item_found(
DiscoveredItemId::new(item.id().as_usize()),
DiscoveredItem::Enum {
final_name: name.to_string(),
},
);
utils::call_discovered_item_callback(ctx, item, || {
DiscoveredItem::Enum {
final_name: name.to_string(),
}
});

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

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

Expand Down Expand Up @@ -4664,13 +4649,10 @@ impl CodeGenerator for Function {
if times_seen > 0 {
write!(&mut canonical_name, "{times_seen}").unwrap();
}
ctx.options().for_each_callback(|cb| {
cb.new_item_found(
id,
DiscoveredItem::Function {
final_name: canonical_name.to_string(),
},
);
utils::call_discovered_item_callback(ctx, item, || {
DiscoveredItem::Function {
final_name: canonical_name.to_string(),
}
});

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

true
}

pub(super) fn call_discovered_item_callback(
ctx: &BindgenContext,
item: &Item,
discovered_item_creator: impl Fn() -> crate::callbacks::DiscoveredItem,
) {
ctx.options().for_each_callback(|cb| {
cb.new_item_found(
DiscoveredItemId::new(item.id().as_usize()),
discovered_item_creator(),
);
});
}
}

0 comments on commit cdaa002

Please sign in to comment.