Skip to content

Commit 68bcced

Browse files
committed
expose discovered composite types and aliases to parse callbacks
1 parent f518815 commit 68bcced

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

bindgen/callbacks.rs

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

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

bindgen/codegen/mod.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use self::struct_layout::StructLayoutTracker;
2121
use super::BindgenOptions;
2222

2323
use crate::callbacks::{
24-
AttributeInfo, DeriveInfo, FieldInfo, TypeKind as DeriveTypeKind,
24+
AttributeInfo, DeriveInfo, DiscoveredItem, DiscoveredItemId, FieldInfo,
25+
TypeKind as DeriveTypeKind,
2526
};
2627
use crate::codegen::error::Error;
2728
use crate::ir::analysis::{HasVtable, Sizedness};
@@ -982,6 +983,18 @@ impl CodeGenerator for Type {
982983

983984
let rust_name = ctx.rust_ident(&name);
984985

986+
ctx.options().for_each_callback(|cb| {
987+
cb.new_item_found(
988+
DiscoveredItemId(item.id().as_usize()),
989+
DiscoveredItem::Alias {
990+
alias_name: rust_name.to_string(),
991+
alias_for: DiscoveredItemId(
992+
inner_item.id().as_usize(),
993+
),
994+
},
995+
);
996+
});
997+
985998
let mut tokens = if let Some(comment) = item.comment(ctx) {
986999
attributes::doc(comment)
9871000
} else {
@@ -2374,6 +2387,24 @@ impl CodeGenerator for CompInfo {
23742387

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

2390+
ctx.options().for_each_callback(|cb| {
2391+
let discovered_item = match self.kind() {
2392+
CompKind::Struct => DiscoveredItem::Struct {
2393+
original_name: item.kind().expect_type().name(),
2394+
final_name: canonical_ident.to_string(),
2395+
},
2396+
CompKind::Union => DiscoveredItem::Union {
2397+
original_name: item.kind().expect_type().name(),
2398+
final_name: canonical_ident.to_string(),
2399+
},
2400+
};
2401+
2402+
cb.new_item_found(
2403+
DiscoveredItemId(item.id().as_usize()),
2404+
discovered_item,
2405+
);
2406+
});
2407+
23772408
// The custom derives callback may return a list of derive attributes;
23782409
// add them to the end of the list.
23792410
let custom_derives = ctx.options().all_callbacks(|cb| {

0 commit comments

Comments
 (0)