Skip to content
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

Generate methods that invoke vtable pointers #27

Closed
Tracked by #2343
jdm opened this issue Jul 28, 2016 · 6 comments
Closed
Tracked by #2343

Generate methods that invoke vtable pointers #27

jdm opened this issue Jul 28, 2016 · 6 comments

Comments

@jdm
Copy link
Contributor

jdm commented Jul 28, 2016

We generate glue for C++ classes with virtual tables (for example, CustomAutoRooter in rust-mozjs), but we don't generate methods that fetch the function pointer from the vtable and invoke it with the this pointer. That would be a significant improvement in usability.

@emilio
Copy link
Contributor

emilio commented Sep 22, 2016

Just a quick update: The vtables before were absolutely wrong anyway (didn't take into account destructors, etc.).

Also, MSVC packs virtual method overrides differently from the rest of the compilers, so this will require target-specific code (which is not a huge deal though, we already do it).

In any case, this is still a major amount of work, though certainly useful. I'll consider implementing it if there's enough need for it, though the logic is surely complex.

@ggris
Copy link

ggris commented May 17, 2018

I was searching for information regarding this feature in bindgen. As I understand, it still has to be done as of may 2018. Is there any hope it will be considered at some point? Thanks

@emilio
Copy link
Contributor

emilio commented May 17, 2018

It's not really trivial, someone needs to go ahead and implement it properly, and add a bunch of tests. I'd be happy to mentor, though I'm not sure if I'd have the time to do it myself.

mickvangelderen added a commit to mickvangelderen/openvr-sys-rust that referenced this issue Dec 13, 2018
@emilio
Copy link
Contributor

emilio commented Aug 29, 2020

This is basically a matter of implementing this:

impl<'a> CodeGenerator for Vtable<'a> {
type Extra = Item;
fn codegen<'b>(
&self,
ctx: &BindgenContext,
result: &mut CodegenResult<'b>,
item: &Item,
) {
assert_eq!(item.id(), self.item_id);
debug_assert!(item.is_enabled_for_codegen(ctx));
// For now, generate an empty struct, later we should generate function
// pointers and whatnot.
let name = ctx.rust_ident(&self.canonical_name(ctx));
let void = helpers::ast_ty::c_void(ctx);
result.push(quote! {
#[repr(C)]
pub struct #name ( #void );
});
}
}

Which already has a reference to all methods and base classes. Making this work for simple casses (no base classes, no overloaded methods) should be trivial. From there we can improve.

mstallmo added a commit to mstallmo/tensorrt-rs that referenced this issue Dec 1, 2020
Turns out that `bindgen` can actually parse the `NvInfer.h` file
pretty ok in most situations. This removes a lot of the need for
extra indirection and dynamic allocation that we were doing before
to wrap it in a C API.

Only outstanding issue is that it doesn't automatically generate
bindings to pure virtual functions. All member functions in the
`NvInfer.h` file are pure virtual so we just get the types but
not their methods. To fix this we still need a very thin wrapper
that accepts the type as the first parameter and then calls the
appropriate function directly on the type.

There is an issue tracking this
rust-lang/rust-bindgen#27
that hopefully will remove the need for this thin wrapper
completely.
mstallmo added a commit to mstallmo/tensorrt-rs that referenced this issue Dec 16, 2020
Turns out that `bindgen` can actually parse the `NvInfer.h` file
pretty ok in most situations. This removes a lot of the need for
extra indirection and dynamic allocation that we were doing before
to wrap it in a C API.

Only outstanding issue is that it doesn't automatically generate
bindings to pure virtual functions. All member functions in the
`NvInfer.h` file are pure virtual so we just get the types but
not their methods. To fix this we still need a very thin wrapper
that accepts the type as the first parameter and then calls the
appropriate function directly on the type.

There is an issue tracking this
rust-lang/rust-bindgen#27
that hopefully will remove the need for this thin wrapper
completely.
@pvdrz
Copy link
Contributor

pvdrz commented Sep 15, 2022

Is this fixed via #2145?

@linonetwo
Copy link

linonetwo commented Mar 14, 2024

Yes, use this to generate a basic vtable

    let bindings = bindgen::Builder::default()
        .header("wrapper.hpp")
        .clang_arg("-x")
        .clang_arg("c++")
        .clang_arg("-std=c++11")
        .derive_default(true)
        .derive_debug(true)
        .vtable_generation(true)
        .generate_comments(false)
        .layout_tests(false)
        .generate_comments(false)
        .derive_copy(true)
        .derive_hash(false)
        .parse_callbacks(Box::new(MyCallback {}))
        .generate()
        .expect("Unable to generate bindings");

With .vtable_generation(true).

@emilio emilio closed this as completed Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants