Skip to content

Commit dd28f0b

Browse files
mxynspvdrz
authored andcommitted
expose discovered composite types and aliases to parse callbacks
1 parent d9576ea commit dd28f0b

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

bindgen/callbacks.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,62 @@ 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(usize);
175+
176+
impl DiscoveredItemId {
177+
/// Constructor
178+
pub fn new(value: usize) -> Self {
179+
Self(value)
180+
}
181+
}
182+
183+
/// Struct passed to [ParseCallbacks::new_item_found] containing information about discovered
184+
/// items (struct, union, and alias)
185+
#[derive(Debug, Hash, Clone, Ord, PartialOrd, Eq, PartialEq)]
186+
pub enum DiscoveredItem {
187+
/// Represents a struct with its original name in C and its generated binding name
188+
Struct {
189+
/// The original name (learnt from C) of the structure
190+
/// Can be None if the union is anonymous.
191+
original_name: Option<String>,
192+
193+
/// The name of the generated binding
194+
final_name: String,
195+
},
196+
197+
/// Represents a union with its original name in C and its generated binding name
198+
Union {
199+
/// The original name (learnt from C) of the structure.
200+
/// Can be None if the union is anonymous.
201+
original_name: Option<String>,
202+
203+
/// The name of the generated binding
204+
final_name: String,
205+
},
206+
207+
/// Represents an alias like a typedef
208+
/// ```c
209+
/// typedef struct MyStruct {
210+
/// ...
211+
/// } StructAlias;
212+
/// ```
213+
/// Here, the name of the alias is `StructAlias` and it's an alias for `MyStruct`
214+
Alias {
215+
/// The name of the alias in C (`StructAlias`)
216+
alias_name: String,
217+
218+
/// The identifier of the discovered type
219+
alias_for: DiscoveredItemId,
220+
}, // functions, modules, etc.
165221
}
166222

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

bindgen/codegen/mod.rs

Lines changed: 40 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};
@@ -983,6 +984,18 @@ impl CodeGenerator for Type {
983984

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

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

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

2465+
ctx.options().for_each_callback(|cb| {
2466+
let discovered_item = match self.kind() {
2467+
CompKind::Struct => DiscoveredItem::Struct {
2468+
original_name: item
2469+
.kind()
2470+
.expect_type()
2471+
.name()
2472+
.map(String::from),
2473+
final_name: canonical_ident.to_string(),
2474+
},
2475+
CompKind::Union => DiscoveredItem::Union {
2476+
original_name: item
2477+
.kind()
2478+
.expect_type()
2479+
.name()
2480+
.map(String::from),
2481+
final_name: canonical_ident.to_string(),
2482+
},
2483+
};
2484+
2485+
cb.new_item_found(
2486+
DiscoveredItemId::new(item.id().as_usize()),
2487+
discovered_item,
2488+
);
2489+
});
2490+
24522491
// The custom derives callback may return a list of derive attributes;
24532492
// add them to the end of the list.
24542493
let custom_derives = ctx.options().all_callbacks(|cb| {

0 commit comments

Comments
 (0)