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 Sep 9, 2024
1 parent 7b95673 commit e073105
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
48 changes: 48 additions & 0 deletions bindgen/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,54 @@ pub trait ParseCallbacks: fmt::Debug {
fn wrap_as_variadic_fn(&self, _name: &str) -> Option<String> {
None
}

/// This will get called everytime an item (currently struct, union, and alias) is found with some information about it
fn new_item_found(&self, _id: DiscoveredItemId, _item: DiscoveredItem) {}

// TODO add callback for ResolvedTypeRef
}

/// An identifier for a discovered item. Used to identify an aliased type (see [DiscoveredItem::Alias])
#[derive(Ord, PartialOrd, PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub struct DiscoveredItemId(pub(crate) usize);

/// Struct passed to [ParseCallbacks::new_item_found] containing information about discovered
/// items (struct, union, and alias)
pub enum DiscoveredItem<'a> {
/// Represents a struct with its original name in C and its generated binding name
Struct {
/// The original name (learnt from C) of the structure
/// Can be None if the union is anonymous.
original_name: Option<&'a str>,

/// The name of the generated binding
final_name: String,
},

/// Represents a union with its original name in C and its generated binding name
Union {
/// The original name (learnt from C) of the structure.
/// Can be None if the union is anonymous.
original_name: Option<&'a str>,

/// The name of the generated binding
final_name: String,
},

/// Represents an alias like a typedef
/// ```c
/// typedef struct MyStruct {
/// ...
/// } StructAlias;
/// ```
/// Here, the name of the alias is `StructAlias` and it's an alias for `struct MyStruct`
Alias {
/// The name of the alias in C (`StructAlias`)
alias_name: String,

/// The identifier of the discovered type
alias_for: DiscoveredItemId,
}, // functions, modules, etc.
}

/// Relevant information about a type to which new derive attributes will be added using
Expand Down
35 changes: 34 additions & 1 deletion bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use self::struct_layout::StructLayoutTracker;

use super::BindgenOptions;

use crate::callbacks::{DeriveInfo, FieldInfo, TypeKind as DeriveTypeKind};
use crate::callbacks::{
DeriveInfo, DiscoveredItem, DiscoveredItemId, FieldInfo,
TypeKind as DeriveTypeKind,
};
use crate::codegen::error::Error;
use crate::ir::analysis::{HasVtable, Sizedness};
use crate::ir::annotations::{
Expand Down Expand Up @@ -967,6 +970,18 @@ impl CodeGenerator for Type {

let rust_name = ctx.rust_ident(&name);

ctx.options().for_each_callback(|cb| {
cb.new_item_found(
DiscoveredItemId(item.id().as_usize()),
DiscoveredItem::Alias {
alias_name: rust_name.to_string(),
alias_for: DiscoveredItemId(
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 +2267,24 @@ impl CodeGenerator for CompInfo {

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

ctx.options().for_each_callback(|cb| {
let discovered_item = match self.kind() {
CompKind::Struct => DiscoveredItem::Struct {
original_name: item.kind().expect_type().name(),
final_name: canonical_ident.to_string(),
},
CompKind::Union => DiscoveredItem::Union {
original_name: item.kind().expect_type().name(),
final_name: canonical_ident.to_string(),
},
};

cb.new_item_found(
DiscoveredItemId(item.id().as_usize()),
discovered_item,
);
});

// 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

0 comments on commit e073105

Please sign in to comment.