File tree Expand file tree Collapse file tree 6 files changed +71
-1
lines changed Expand file tree Collapse file tree 6 files changed +71
-1
lines changed Original file line number Diff line number Diff line change
1
+ // bindgen-flags: --represent-cxx-operators --generate-inline-functions -- -x c++
2
+ // bindgen-parse-callbacks: operator-rename
3
+
4
+ class SomeClass {
5
+ public:
6
+ bool operator =(const SomeClass& another) {
7
+ return false ;
8
+ }
9
+ };
Original file line number Diff line number Diff line change @@ -146,6 +146,19 @@ impl ParseCallbacks for WrapAsVariadicFn {
146
146
}
147
147
}
148
148
149
+ #[ derive( Debug ) ]
150
+ pub ( super ) struct OperatorRename ;
151
+
152
+ impl ParseCallbacks for OperatorRename {
153
+ fn generated_name_override ( & self , info : ItemInfo ) -> Option < String > {
154
+ if info. name == "operator=" {
155
+ Some ( "operatorequals" . to_string ( ) )
156
+ } else {
157
+ None
158
+ }
159
+ }
160
+ }
161
+
149
162
pub fn lookup ( cb : & str ) -> Box < dyn ParseCallbacks > {
150
163
match cb {
151
164
"enum-variant-rename" => Box :: new ( EnumVariantRename ) ,
@@ -154,6 +167,7 @@ pub fn lookup(cb: &str) -> Box<dyn ParseCallbacks> {
154
167
}
155
168
"wrap-as-variadic-fn" => Box :: new ( WrapAsVariadicFn ) ,
156
169
"type-visibility" => Box :: new ( TypeVisibility ) ,
170
+ "operator-rename" => Box :: new ( OperatorRename ) ,
157
171
call_back => {
158
172
if let Some ( prefix) =
159
173
call_back. strip_prefix ( "remove-function-prefix-" )
Original file line number Diff line number Diff line change @@ -433,7 +433,7 @@ impl FunctionSig {
433
433
spelling. starts_with ( "operator" ) &&
434
434
!clang:: is_valid_identifier ( spelling)
435
435
} ;
436
- if is_operator ( & spelling) {
436
+ if is_operator ( & spelling) && !ctx . options ( ) . represent_cxx_operators {
437
437
return Err ( ParseError :: Continue ) ;
438
438
}
439
439
Original file line number Diff line number Diff line change @@ -447,6 +447,9 @@ struct BindgenCommand {
447
447
/// Use distinct char16_t
448
448
#[ arg( long) ]
449
449
use_distinct_char16_t : bool ,
450
+ /// Output C++ overloaded operators
451
+ #[ arg( long) ]
452
+ represent_cxx_operators : bool ,
450
453
/// Enables generation of vtable functions.
451
454
#[ arg( long) ]
452
455
vtable_generation : bool ,
@@ -649,6 +652,7 @@ where
649
652
explicit_padding,
650
653
use_specific_virtual_function_receiver,
651
654
use_distinct_char16_t,
655
+ represent_cxx_operators,
652
656
vtable_generation,
653
657
sort_semantically,
654
658
merge_extern_blocks,
@@ -951,6 +955,7 @@ where
951
955
explicit_padding,
952
956
use_specific_virtual_function_receiver,
953
957
use_distinct_char16_t,
958
+ represent_cxx_operators,
954
959
vtable_generation,
955
960
sort_semantically,
956
961
merge_extern_blocks,
Original file line number Diff line number Diff line change @@ -192,6 +192,24 @@ options! {
192
192
} ,
193
193
as_args: "--use-distinct-char16-t" ,
194
194
} ,
195
+ /// Whether we should output C++ overloaded operators. By itself,
196
+ /// this option is not sufficient to produce valid output, because
197
+ /// such operators will have names that are not acceptable Rust
198
+ /// names (for example `operator=`). If you use this option, you'll also
199
+ /// have to rename the resulting functions - for example by using
200
+ /// [`ParseCallbacks::generated_name_override`].
201
+ represent_cxx_operators: bool {
202
+ methods: {
203
+ /// If this is true, output existence of C++ overloaded operators.
204
+ /// At present, only operator= is noted.
205
+ /// Disabled by default.
206
+ pub fn represent_cxx_operators( mut self , doit: bool ) -> Builder {
207
+ self . options. represent_cxx_operators = doit;
208
+ self
209
+ }
210
+ } ,
211
+ as_args: "--represent-cxx-operators" ,
212
+ } ,
195
213
196
214
/// Types that have been blocklisted and should not appear anywhere in the generated code.
197
215
blocklisted_types: RegexSet {
You can’t perform that action at this time.
0 commit comments