Skip to content

Commit e1aa4bf

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

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

bindgen/callbacks.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! A public API for more fine-grained customization of bindgen behavior.
22
33
pub use crate::ir::analysis::DeriveTrait;
4+
use crate::ir::comp::CompKind;
45
pub use crate::ir::derive::CanDerive as ImplementsTrait;
56
pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue};
67
pub use crate::ir::int::IntKind;
8+
use proc_macro2::Ident;
79
use std::fmt;
810

911
/// An enum to allow ignoring parsing of macros.
@@ -159,6 +161,27 @@ pub trait ParseCallbacks: fmt::Debug {
159161
fn wrap_as_variadic_fn(&self, _name: &str) -> Option<String> {
160162
None
161163
}
164+
165+
/// This will get called everytime a composite type is found with some information about it
166+
fn new_composite_found(
167+
&self,
168+
_id: usize,
169+
_kind: CompKind,
170+
_original_name: Option<&str>,
171+
_final_ident: &Ident,
172+
) {
173+
}
174+
175+
/// This will get called everytime an alias is found with some information about it
176+
fn new_alias_found(
177+
&self,
178+
_id: usize,
179+
_alias_name: &Ident,
180+
_alias_for: usize,
181+
) {
182+
}
183+
184+
// TODO add callback for ResolvedTypeRef
162185
}
163186

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

bindgen/codegen/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,14 @@ impl CodeGenerator for Type {
967967

968968
let rust_name = ctx.rust_ident(&name);
969969

970+
ctx.options().for_each_callback(|cb| {
971+
cb.new_alias_found(
972+
item.id().as_usize(),
973+
&rust_name,
974+
inner_item.id().as_usize(),
975+
);
976+
});
977+
970978
let mut tokens = if let Some(comment) = item.comment(ctx) {
971979
attributes::doc(comment)
972980
} else {
@@ -2252,6 +2260,15 @@ impl CodeGenerator for CompInfo {
22522260

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

2263+
ctx.options().for_each_callback(|cb| {
2264+
cb.new_composite_found(
2265+
item.id().as_usize(),
2266+
self.kind(),
2267+
item.kind().expect_type().name(),
2268+
&canonical_ident,
2269+
);
2270+
});
2271+
22552272
// The custom derives callback may return a list of derive attributes;
22562273
// add them to the end of the list.
22572274
let custom_derives = ctx.options().all_callbacks(|cb| {

bindgen/ir/comp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::mem;
2222

2323
/// The kind of compound type.
2424
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
25-
pub(crate) enum CompKind {
25+
pub enum CompKind {
2626
/// A struct.
2727
Struct,
2828
/// A union.

bindgen/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ mod clang;
4646
mod diagnostics;
4747
mod features;
4848
mod ir;
49+
50+
pub use ir::comp::CompKind;
51+
4952
mod parse;
5053
mod regex_set;
5154

0 commit comments

Comments
 (0)