Skip to content

Minor vtable tweaks. #2164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 18, 2022
Merged
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
9 changes: 5 additions & 4 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,8 @@ impl<'a> CodeGenerator for Vtable<'a> {
// For now, we will only generate vtables for classes that:
// - do not inherit from others (compilers merge VTable from primary parent class).
// - do not contain a virtual destructor (requires ordering; platforms generate different vtables).
if self.comp_info.base_members().is_empty() &&
if ctx.options().vtable_generation &&
self.comp_info.base_members().is_empty() &&
self.comp_info.destructor().is_none()
{
let class_ident = ctx.rust_ident(self.item_id.canonical_name(ctx));
Expand Down Expand Up @@ -1076,9 +1077,9 @@ impl<'a> CodeGenerator for Vtable<'a> {
let ret = utils::fnsig_return_ty(ctx, signature);

args[0] = if m.is_const() {
quote! { this: & #class_ident }
quote! { this: *const #class_ident }
} else {
quote! { this: &mut #class_ident }
quote! { this: *mut #class_ident }
};

Some(quote! {
Expand Down Expand Up @@ -1793,7 +1794,7 @@ impl CodeGenerator for CompInfo {

if !is_opaque {
if item.has_vtable_ptr(ctx) {
let vtable = Vtable::new(item.id(), &self);
let vtable = Vtable::new(item.id(), self);
vtable.codegen(ctx, result, item);

let vtable_type = vtable
Expand Down
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ impl Builder {
output_vector.push("--explicit-padding".into());
}

if self.options.vtable_generation {
output_vector.push("--vtable-generation".into());
}

// Add clang arguments

output_vector.push("--".into());
Expand Down Expand Up @@ -1450,6 +1454,14 @@ impl Builder {
self
}

/// If true, enables experimental support to generate vtable functions.
///
/// Should mostly work, though some edge cases are likely to be broken.
pub fn vtable_generation(mut self, doit: bool) -> Self {
self.options.vtable_generation = doit;
self
}

/// Generate the Rust bindings using the options built up thus far.
pub fn generate(mut self) -> Result<Bindings, BindgenError> {
// Add any extra arguments from the environment to the clang command line.
Expand Down Expand Up @@ -1981,6 +1993,9 @@ struct BindgenOptions {

/// Always output explicit padding fields
force_explicit_padding: bool,

/// Emit vtable functions.
vtable_generation: bool,
}

/// TODO(emilio): This is sort of a lie (see the error message that results from
Expand Down Expand Up @@ -2128,6 +2143,7 @@ impl Default for BindgenOptions {
translate_enum_integer_types: false,
c_naming: false,
force_explicit_padding: false,
vtable_generation: false,
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ where
Arg::new("explicit-padding")
.long("explicit-padding")
.help("Always output explicit padding fields."),
Arg::new("vtable-generation")
.long("vtable-generation")
.help("Enables generation of vtable functions."),
]) // .args()
.get_matches_from(args);

Expand Down Expand Up @@ -1008,6 +1011,10 @@ where
builder = builder.explicit_padding(true);
}

if matches.is_present("vtable-generation") {
builder = builder.vtable_generation(true);
}

let verbose = matches.is_present("verbose");

Ok((builder, output, verbose))
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum_and_vtable_mangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum _bindgen_ty_1 {
}
#[repr(C)]
pub struct C__bindgen_vtable {
pub C_match: unsafe extern "C" fn(this: &mut C),
pub C_match: unsafe extern "C" fn(this: *mut C),
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/nested_vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#[repr(C)]
pub struct nsISupports__bindgen_vtable {
pub nsISupports_QueryInterface:
unsafe extern "C" fn(this: &mut nsISupports) -> *mut nsISupports,
unsafe extern "C" fn(this: *mut nsISupports) -> *mut nsISupports,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/ref_argument_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub const NSID_LENGTH: u32 = 10;
#[repr(C)]
pub struct nsID__bindgen_vtable {
pub nsID_ToProvidedString: unsafe extern "C" fn(
this: &mut nsID,
this: *mut nsID,
aDest: *mut [::std::os::raw::c_char; 10usize],
),
}
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/virtual_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#[repr(C)]
pub struct PureVirtualIFace__bindgen_vtable {
pub PureVirtualIFace_Foo: unsafe extern "C" fn(this: &mut PureVirtualIFace),
pub PureVirtualIFace_Foo: unsafe extern "C" fn(this: *mut PureVirtualIFace),
pub PureVirtualIFace_Bar: unsafe extern "C" fn(
this: &mut PureVirtualIFace,
this: *mut PureVirtualIFace,
arg1: ::std::os::raw::c_uint,
),
}
Expand Down Expand Up @@ -42,7 +42,7 @@ impl Default for PureVirtualIFace {
}
#[repr(C)]
pub struct AnotherInterface__bindgen_vtable {
pub AnotherInterface_Baz: unsafe extern "C" fn(this: &mut AnotherInterface),
pub AnotherInterface_Baz: unsafe extern "C" fn(this: *mut AnotherInterface),
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/virtual_overloaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#[repr(C)]
pub struct C__bindgen_vtable {
pub C_do_thing:
unsafe extern "C" fn(this: &mut C, arg1: ::std::os::raw::c_char),
unsafe extern "C" fn(this: *mut C, arg1: ::std::os::raw::c_char),
pub C_do_thing1:
unsafe extern "C" fn(this: &mut C, arg1: ::std::os::raw::c_int),
unsafe extern "C" fn(this: *mut C, arg1: ::std::os::raw::c_int),
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/vtable_recursive_sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#[repr(C)]
pub struct Base__bindgen_vtable {
pub Base_AsDerived: unsafe extern "C" fn(this: &mut Base) -> *mut Derived,
pub Base_AsDerived: unsafe extern "C" fn(this: *mut Base) -> *mut Derived,
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
Expand Down
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ fn create_bindgen_builder(header: &Path) -> Result<BuilderState, Error> {
"--no-rustfmt-bindings",
"--with-derive-default",
"--disable-header-comment",
"--vtable-generation",
header_str,
"--raw-line",
"",
Expand Down