Skip to content

Commit e073105

Browse files
committed
expose discovered composite types and aliases to parse callbacks
1 parent 7b95673 commit e073105

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

bindgen/callbacks.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,54 @@ pub trait ParseCallbacks: fmt::Debug {
159159
fn wrap_as_variadic_fn(&self, _name: &str) -> Option<String> {
160160
None
161161
}
162+
163+
/// This will get called everytime an item (currently struct, union, and alias) is found with some information about it
164+
fn new_item_found(&self, _id: DiscoveredItemId, _item: DiscoveredItem) {}
165+
166+
// TODO add callback for ResolvedTypeRef
167+
}
168+
169+
/// An identifier for a discovered item. Used to identify an aliased type (see [DiscoveredItem::Alias])
170+
#[derive(Ord, PartialOrd, PartialEq, Eq, Hash, Debug, Clone, Copy)]
171+
pub struct DiscoveredItemId(pub(crate) usize);
172+
173+
/// Struct passed to [ParseCallbacks::new_item_found] containing information about discovered
174+
/// items (struct, union, and alias)
175+
pub enum DiscoveredItem<'a> {
176+
/// Represents a struct with its original name in C and its generated binding name
177+
Struct {
178+
/// The original name (learnt from C) of the structure
179+
/// Can be None if the union is anonymous.
180+
original_name: Option<&'a str>,
181+
182+
/// The name of the generated binding
183+
final_name: String,
184+
},
185+
186+
/// Represents a union with its original name in C and its generated binding name
187+
Union {
188+
/// The original name (learnt from C) of the structure.
189+
/// Can be None if the union is anonymous.
190+
original_name: Option<&'a str>,
191+
192+
/// The name of the generated binding
193+
final_name: String,
194+
},
195+
196+
/// Represents an alias like a typedef
197+
/// ```c
198+
/// typedef struct MyStruct {
199+
/// ...
200+
/// } StructAlias;
201+
/// ```
202+
/// Here, the name of the alias is `StructAlias` and it's an alias for `struct MyStruct`
203+
Alias {
204+
/// The name of the alias in C (`StructAlias`)
205+
alias_name: String,
206+
207+
/// The identifier of the discovered type
208+
alias_for: DiscoveredItemId,
209+
}, // functions, modules, etc.
162210
}
163211

164212
/// Relevant information about a type to which new derive attributes will be added using

bindgen/codegen/mod.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use self::struct_layout::StructLayoutTracker;
2020

2121
use super::BindgenOptions;
2222

23-
use crate::callbacks::{DeriveInfo, FieldInfo, TypeKind as DeriveTypeKind};
23+
use crate::callbacks::{
24+
DeriveInfo, DiscoveredItem, DiscoveredItemId, FieldInfo,
25+
TypeKind as DeriveTypeKind,
26+
};
2427
use crate::codegen::error::Error;
2528
use crate::ir::analysis::{HasVtable, Sizedness};
2629
use crate::ir::annotations::{
@@ -967,6 +970,18 @@ impl CodeGenerator for Type {
967970

968971
let rust_name = ctx.rust_ident(&name);
969972

973+
ctx.options().for_each_callback(|cb| {
974+
cb.new_item_found(
975+
DiscoveredItemId(item.id().as_usize()),
976+
DiscoveredItem::Alias {
977+
alias_name: rust_name.to_string(),
978+
alias_for: DiscoveredItemId(
979+
inner_item.id().as_usize(),
980+
),
981+
},
982+
);
983+
});
984+
970985
let mut tokens = if let Some(comment) = item.comment(ctx) {
971986
attributes::doc(comment)
972987
} else {
@@ -2252,6 +2267,24 @@ impl CodeGenerator for CompInfo {
22522267

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

2270+
ctx.options().for_each_callback(|cb| {
2271+
let discovered_item = match self.kind() {
2272+
CompKind::Struct => DiscoveredItem::Struct {
2273+
original_name: item.kind().expect_type().name(),
2274+
final_name: canonical_ident.to_string(),
2275+
},
2276+
CompKind::Union => DiscoveredItem::Union {
2277+
original_name: item.kind().expect_type().name(),
2278+
final_name: canonical_ident.to_string(),
2279+
},
2280+
};
2281+
2282+
cb.new_item_found(
2283+
DiscoveredItemId(item.id().as_usize()),
2284+
discovered_item,
2285+
);
2286+
});
2287+
22552288
// The custom derives callback may return a list of derive attributes;
22562289
// add them to the end of the list.
22572290
let custom_derives = ctx.options().all_callbacks(|cb| {

0 commit comments

Comments
 (0)