File tree 6 files changed +71
-1
lines changed
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 @@ -444,6 +444,9 @@ struct BindgenCommand {
444
444
/// Use distinct char16_t
445
445
#[ arg( long) ]
446
446
use_distinct_char16_t : bool ,
447
+ /// Output C++ overloaded operators
448
+ #[ arg( long) ]
449
+ represent_cxx_operators : bool ,
447
450
/// Enables generation of vtable functions.
448
451
#[ arg( long) ]
449
452
vtable_generation : bool ,
@@ -633,6 +636,7 @@ where
633
636
c_naming,
634
637
explicit_padding,
635
638
use_distinct_char16_t,
639
+ represent_cxx_operators,
636
640
vtable_generation,
637
641
sort_semantically,
638
642
merge_extern_blocks,
@@ -931,6 +935,7 @@ where
931
935
c_naming,
932
936
explicit_padding,
933
937
use_distinct_char16_t,
938
+ represent_cxx_operators,
934
939
vtable_generation,
935
940
sort_semantically,
936
941
merge_extern_blocks,
Original file line number Diff line number Diff line change @@ -177,6 +177,24 @@ options! {
177
177
} ,
178
178
as_args: "--use-distinct-char16-t" ,
179
179
} ,
180
+ /// Whether we should output C++ overloaded operators. By itself,
181
+ /// this option is not sufficient to produce valid output, because
182
+ /// such operators will have names that are not acceptable Rust
183
+ /// names (for example `operator=`). If you use this option, you'll also
184
+ /// have to rename the resulting functions - for example by using
185
+ /// [`ParseCallbacks::generated_name_override`].
186
+ represent_cxx_operators: bool {
187
+ methods: {
188
+ /// If this is true, output existence of C++ overloaded operators.
189
+ /// At present, only operator= is noted.
190
+ /// Disabled by default.
191
+ pub fn represent_cxx_operators( mut self , doit: bool ) -> Builder {
192
+ self . options. represent_cxx_operators = doit;
193
+ self
194
+ }
195
+ } ,
196
+ as_args: "--represent-cxx-operators" ,
197
+ } ,
180
198
181
199
/// Types that have been blocklisted and should not appear anywhere in the generated code.
182
200
blocklisted_types: RegexSet {
You can’t perform that action at this time.
0 commit comments