From 9aba819103b5d5647d7d46c2db26cd95a39c4464 Mon Sep 17 00:00:00 2001 From: Maxence Younsi Date: Fri, 6 Oct 2023 01:21:58 +0200 Subject: [PATCH] expose discovered composite types and aliases to parse callbacks --- bindgen/callbacks.rs | 23 +++++++++++++++++++++++ bindgen/codegen/mod.rs | 18 ++++++++++++++++++ bindgen/ir/comp.rs | 2 +- bindgen/lib.rs | 3 +++ 4 files changed, 45 insertions(+), 1 deletion(-) diff --git a/bindgen/callbacks.rs b/bindgen/callbacks.rs index c22ba975dd..b44bbfec11 100644 --- a/bindgen/callbacks.rs +++ b/bindgen/callbacks.rs @@ -1,9 +1,11 @@ //! A public API for more fine-grained customization of bindgen behavior. pub use crate::ir::analysis::DeriveTrait; +use crate::ir::comp::CompKind; pub use crate::ir::derive::CanDerive as ImplementsTrait; pub use crate::ir::enum_ty::{EnumVariantCustomBehavior, EnumVariantValue}; pub use crate::ir::int::IntKind; +use proc_macro2::Ident; use std::fmt; /// An enum to allow ignoring parsing of macros. @@ -159,6 +161,27 @@ pub trait ParseCallbacks: fmt::Debug { fn wrap_as_variadic_fn(&self, _name: &str) -> Option { None } + + /// This will get called everytime a composite type is found with some information about it + fn new_composite_found( + &self, + _id: usize, + _kind: CompKind, + _original_name: Option<&str>, + _final_ident: &Ident, + ) { + } + + /// This will get called everytime an alias is found with some information about it + fn new_alias_found( + &self, + _id: usize, + _alias_name: &Ident, + _alias_for: usize, + ) { + } + + // TODO add callback for ResolvedTypeRef } /// Relevant information about a type to which new derive attributes will be added using diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 8e13606345..692d66797e 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -967,6 +967,15 @@ impl CodeGenerator for Type { let rust_name = ctx.rust_ident(&name); + ctx.options().for_each_callback(|cb| { + + cb.new_alias_found( + item.id().as_usize(), + &rust_name, + inner_item.id().as_usize(), + ); + }); + let mut tokens = if let Some(comment) = item.comment(ctx) { attributes::doc(comment) } else { @@ -2252,6 +2261,15 @@ impl CodeGenerator for CompInfo { let is_rust_union = is_union && struct_layout.is_rust_union(); + ctx.options().for_each_callback(|cb| { + cb.new_composite_found( + item.id().as_usize(), + self.kind(), + item.kind().expect_type().name(), + &canonical_ident, + ); + }); + // The custom derives callback may return a list of derive attributes; // add them to the end of the list. let custom_derives = ctx.options().all_callbacks(|cb| { diff --git a/bindgen/ir/comp.rs b/bindgen/ir/comp.rs index 89e77e160f..54d840d2ed 100644 --- a/bindgen/ir/comp.rs +++ b/bindgen/ir/comp.rs @@ -22,7 +22,7 @@ use std::mem; /// The kind of compound type. #[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub(crate) enum CompKind { +pub enum CompKind { /// A struct. Struct, /// A union. diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 54348e2232..10649f8aea 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -46,6 +46,9 @@ mod clang; mod diagnostics; mod features; mod ir; + +pub use ir::comp::CompKind; + mod parse; mod regex_set;