Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion examples/cpp/method/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod foo {
/// Generated from: examples/cpp/method/example.h;l=14
#[inline(always)]
pub unsafe fn MyMethod(__this: *mut Self) {
crate::detail::__rust_thunk___ZN3foo3Bar8MyMethodEv(__this)
self::bar::MyMethod(__this)
}
}

Expand All @@ -52,6 +52,22 @@ pub mod foo {
}
}
}

pub mod bar {
#[allow(unused_imports)]
use super::*;

/// # Safety
///
/// The caller must ensure that the following unsafe arguments are not misused by the function:
/// * `__this`: raw pointer
///
/// Generated from: examples/cpp/method/example.h;l=14
#[inline(always)]
pub unsafe fn MyMethod(__this: *mut crate::foo::Bar) {
crate::detail::__rust_thunk___ZN3foo3Bar8MyMethodEv(__this)
}
}
}

// namespace foo
Expand Down
30 changes: 28 additions & 2 deletions examples/cpp/virtual/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl RustDerived {
/// Generated from: examples/cpp/virtual/example.h;l=16
#[inline(always)]
pub unsafe fn Method1(__this: *const Self) -> ::ffi_11::c_int {
crate::detail::__rust_thunk___ZNK11RustDerived7Method1Ev(__this)
self::rust_derived::Method1(__this)
}
/// # Safety
///
Expand All @@ -49,7 +49,7 @@ impl RustDerived {
/// Generated from: examples/cpp/virtual/example.h;l=18
#[inline(always)]
pub unsafe fn Upcast(__this: *mut Self) -> *mut ::base::ExampleBase {
crate::detail::__rust_thunk___ZN11RustDerived6UpcastEv(__this)
self::rust_derived::Upcast(__this)
}
}

Expand Down Expand Up @@ -132,6 +132,32 @@ unsafe impl ::operator::Delete for crate::RustDerived {
}
}

pub mod rust_derived {
#[allow(unused_imports)]
use super::*;

/// # Safety
///
/// The caller must ensure that the following unsafe arguments are not misused by the function:
/// * `__this`: raw pointer
///
/// Generated from: examples/cpp/virtual/example.h;l=16
#[inline(always)]
pub unsafe fn Method1(__this: *const crate::RustDerived) -> ::ffi_11::c_int {
crate::detail::__rust_thunk___ZNK11RustDerived7Method1Ev(__this)
}
/// # Safety
///
/// The caller must ensure that the following unsafe arguments are not misused by the function:
/// * `__this`: raw pointer
///
/// Generated from: examples/cpp/virtual/example.h;l=18
#[inline(always)]
pub unsafe fn Upcast(__this: *mut crate::RustDerived) -> *mut ::base::ExampleBase {
crate::detail::__rust_thunk___ZN11RustDerived6UpcastEv(__this)
}
}

// Generated from: nowhere/llvm/src/libcxx/include/__type_traits/integral_constant.h;l=21
// error: struct `std::integral_constant<bool, false>` could not be bound
// template instantiation is not yet supported
Expand Down
27 changes: 23 additions & 4 deletions rs_bindings_from_cc/generate_bindings/database/code_snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub struct ApiSnippets {
pub features: FlagSet<Feature>,

pub member_functions: HashMap<ItemId, Vec<TokenStream>>,

pub free_functions: HashMap<ItemId, Vec<TokenStream>>,
}

impl ApiSnippets {
Expand All @@ -75,6 +77,9 @@ impl ApiSnippets {
for (item_id, member_functions) in other.member_functions {
self.member_functions.entry(item_id).or_default().extend(member_functions);
}
for (item_id, free_methods) in other.free_functions {
self.free_functions.entry(item_id).or_default().extend(free_methods);
}
self.thunks.extend(other.thunks);
self.assertions.extend(other.assertions);
self.cc_details.extend(other.cc_details);
Expand Down Expand Up @@ -559,6 +564,18 @@ pub fn integer_constant_to_token_stream(
})
}

/// Returns the name of the snake-cased module that exposes the given record's nested items.
pub fn record_name_to_associated_module_name(record_ident: &Ident) -> Ident {
let ident_string = record_ident.to_string();
let snake_case_name = ident_string.to_snake_case();
let name = if snake_case_name == ident_string {
format!("{}_items", snake_case_name)
} else {
snake_case_name
};
make_rs_ident(&name)
}

pub fn generated_items_to_tokens<'db>(
generated_items: &HashMap<ItemId, GeneratedItem>,
db: &'db crate::BindingsGenerator<'db>,
Expand Down Expand Up @@ -601,6 +618,7 @@ pub fn generated_items_to_tokens<'db>(
delete,
owned_type_name,
member_methods,
free_functions,
lifetime_params,
} = record_item.as_ref();

Expand Down Expand Up @@ -790,16 +808,16 @@ pub fn generated_items_to_tokens<'db>(
}
.to_tokens(tokens);
}

if !nested_items.is_empty() {
let snake_case_name = make_rs_ident(&ident.to_string().to_snake_case());
let module_name = record_name_to_associated_module_name(&ident);
if !free_functions.is_empty() || !nested_items.is_empty() {
let nested_items_to_tokens =
generated_items_to_token_stream(generated_items, db, nested_items);
quote! {
pub mod #snake_case_name {
pub mod #module_name {
#[allow(unused_imports)]
use super::*; __NEWLINE__
__NEWLINE__
#( #free_functions )*
#nested_items_to_tokens
}
}
Expand Down Expand Up @@ -1028,6 +1046,7 @@ pub struct Record {
/// The name of the owning wrapper type when the type was annotated with CRUBIT_OWNED_POINTEE.
pub owned_type_name: Option<Ident>,
pub member_methods: Vec<TokenStream>,
pub free_functions: Vec<TokenStream>,
pub lifetime_params: Vec<syn::Lifetime>,
}

Expand Down
Loading