Skip to content

Commit 20aa65a

Browse files
adetayloremilio
authored andcommitted
Provide option to get real virtual fn receiver.
For virtual functions, bindgen has traditionally emitted a void* pointer because that's the least bad option for Rust code directly consuming the bindings. For downstream postprocessors and code generators, this throws away useful information about the actual type of the receiver. Provide a command-line option to put that back. Part of google/autocxx#124
1 parent 0df4256 commit 20aa65a

File tree

5 files changed

+59
-1
lines changed

5 files changed

+59
-1
lines changed

bindgen-tests/tests/expectations/tests/specific_receiver.rs

+28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// bindgen-flags: --use-specific-virtual-function-receiver --generate-inline-functions -- -x c++ -std=c++14
2+
3+
class Fish {
4+
public:
5+
virtual void swim() {
6+
}
7+
};

bindgen/ir/function.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,10 @@ impl FunctionSig {
533533
let is_const = is_method && cursor.method_is_const();
534534
let is_virtual = is_method && cursor.method_is_virtual();
535535
let is_static = is_method && cursor.method_is_static();
536-
if !is_static && !is_virtual {
536+
if !is_static &&
537+
(!is_virtual ||
538+
ctx.options().use_specific_virtual_function_receiver)
539+
{
537540
let parent = cursor.semantic_parent();
538541
let class = Item::parse(parent, None, ctx)
539542
.expect("Expected to parse the class");

bindgen/options/cli.rs

+5
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ struct BindgenCommand {
441441
/// Always output explicit padding fields.
442442
#[arg(long)]
443443
explicit_padding: bool,
444+
/// Always be specific about the 'receiver' of a virtual function.
445+
#[arg(long)]
446+
use_specific_virtual_function_receiver: bool,
444447
/// Use distinct char16_t
445448
#[arg(long)]
446449
use_distinct_char16_t: bool,
@@ -632,6 +635,7 @@ where
632635
translate_enum_integer_types,
633636
c_naming,
634637
explicit_padding,
638+
use_specific_virtual_function_receiver,
635639
use_distinct_char16_t,
636640
vtable_generation,
637641
sort_semantically,
@@ -930,6 +934,7 @@ where
930934
translate_enum_integer_types,
931935
c_naming,
932936
explicit_padding,
937+
use_specific_virtual_function_receiver,
933938
use_distinct_char16_t,
934939
vtable_generation,
935940
sort_semantically,

bindgen/options/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,21 @@ macro_rules! options {
153153
}
154154

155155
options! {
156+
/// Whether to specify the type of a virtual function receiver
157+
use_specific_virtual_function_receiver: bool {
158+
methods: {
159+
/// Normally, virtual functions have void* as their 'this' type.
160+
/// If this flag is enabled, override that behavior to indicate a
161+
/// pointer of the specific type.
162+
/// Disabled by default.
163+
pub fn use_specific_virtual_function_receiver(mut self, doit: bool) -> Builder {
164+
self.options.use_specific_virtual_function_receiver = doit;
165+
self
166+
}
167+
},
168+
as_args: "--use-specific-virtual-function-receiver",
169+
},
170+
156171
/// Whether we should distinguish between C++'s 'char16_t' and 'u16'.
157172
/// The C++ type `char16_t` is its own special type; it's not a typedef
158173
/// of some other integer (this differs from C).

0 commit comments

Comments
 (0)