Skip to content

Commit

Permalink
expose discovered composite types and aliases to parse callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyns committed Oct 8, 2023
1 parent 7b95673 commit e1aa4bf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
23 changes: 23 additions & 0 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -159,6 +161,27 @@ pub trait ParseCallbacks: fmt::Debug {
fn wrap_as_variadic_fn(&self, _name: &str) -> Option<String> {
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
Expand Down
17 changes: 17 additions & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,14 @@ 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 {
Expand Down Expand Up @@ -2252,6 +2260,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| {
Expand Down
2 changes: 1 addition & 1 deletion bindgen/ir/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions bindgen/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ mod clang;
mod diagnostics;
mod features;
mod ir;

pub use ir::comp::CompKind;

mod parse;
mod regex_set;

Expand Down

0 comments on commit e1aa4bf

Please sign in to comment.