-
Notifications
You must be signed in to change notification settings - Fork 712
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
Extremely basic Vtable generation #2145
Conversation
Thanks for working on this!
Presumably you mean target-specific outputs right? If so that should work already by passing the right
The
We could generate intrinsic methods that go to the vtable or something, but for now just having the virtual methods is a huge improvement. |
Thanks for the tips! I would say this is ready to go, however it seems that the call to In addition, it'd be nice to get a canonical name for these functions that do not include the classname, as the classname is already implicit for the vtable structure, e.g: #[repr(C)]
pub struct C__bindgen_vtable {
C_do_thing: fn(&mut self, arg1: ::std::os::raw::c_char),
C_do_thing1: fn(&mut self, arg1: ::std::os::raw::c_int),
} // FIXME: Is there a canonical name without the class prepended?
let function_name = function_item.canonical_name(ctx); Any thoughts on how to resolve these problems? |
The only differences I'm aware of are related to overloaded functions. Leaving that as a TODO / TBD seems fair.
If we're generating vtables at all,
From looking at that code, two nits:
Re. the function name, does Another thing that we need to get right is probably virtual destructors... Any non-trivial use of virtual methods involves virtual destructors, so generating broken vtables for such classes would be pretty bad. It's ok to merge most of the code as-is behind a flag / default-off or something though, so we can iterate on it. |
Ah yes, good point! We'd want
Also true - let me fix that!
It works except for overloaded functions, where multiple functions will be generated with the same name.
Yeah, I need to look at virtual destructors some more. It looks like they aren't being emitted for some reason. |
It also appears that virtual destructor functions are not showing up in the |
Alright, this should be ready to review now. |
To add, this is a simple project you can use to play around and validate the VTable generation as part of this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, sorry for the lag. One question is whether we should enable this by default or put it behind a runtime flag for now, since we expect various edge-casey and not-so-edge-casey things to be potentially broken, and as-is it's not really ergonomic to use.
I tend to think we should (even if we enable it for tests or what not). I'm happy to do the work if so though.
Let's get this merged, I'll add a flag in a follow-up. Thanks so much! |
Awesome, thanks for the review! And yeah, I agree that this should probably be gated behind a flag. |
Great feature! Maybe I'm a bit too late, but what was the rationale behind using regular references instead of pointers for the At least in my use case, where vtables entries will be called directly from C++, this can easily cause mutable aliasing in multithreaded code, which is probably fine, but a bit too close to UB in my opinion. I think having a simple pointer is safer, as library writers can pick what reference type suites it the most. For example I would probably keep it immutable, just to be safe. (BTW, I've been using it for the past couple of days, and everything seems to be running perfectly out of the box) Edit: Ok I've created a new issue detailing the problem, #2163 |
This is an extremely naïve and basic implementation for generating C++ vtable bindings.
Vtables are only generated if the class with the virtual methods has no base classes, and the virtual tables themselves are laid out as done so with MSVC.
Below is an immediate todo list. I'd appreciate tips or pointers on how to address each :)
rust-bindgen
users.Sample Output
VTable layouts
MSVC:
GCC:
For reference, you can check the Vtable layouts on GCC/Clang/MSVC with these compiler commands.
Note that for best results, the classes must be used at least once in code (to prevent DCE).
This PR addresses #27.