@@ -152,6 +152,9 @@ pub(crate) struct FnAnalysis {
152152 pub ( crate ) cxxbridge_name : crate :: minisyn:: Ident ,
153153 /// ... so record also the name under which we wish to expose it in Rust.
154154 pub ( crate ) rust_name : String ,
155+ /// And also the name of the underlying C++ function if it differs
156+ /// from the cxxbridge_name.
157+ pub ( crate ) cpp_call_name : Option < CppOriginalName > ,
155158 pub ( crate ) rust_rename_strategy : RustRenameStrategy ,
156159 pub ( crate ) params : Punctuated < FnArg , Comma > ,
157160 pub ( crate ) kind : FnKind ,
@@ -722,21 +725,19 @@ impl<'a> FnAnalyzer<'a> {
722725 sophistication : TypeConversionSophistication ,
723726 predetermined_rust_name : Option < String > ,
724727 ) -> ( FnAnalysis , ApiName ) {
725- let mut cpp_name = name
726- . cpp_name_if_present ( )
727- . cloned ( )
728- . map ( |n| n. to_effective_name ( ) ) ;
728+ let cpp_original_name = name. cpp_name_if_present ( ) ;
729729 let ns = name. name . get_namespace ( ) ;
730730
731731 // Let's gather some pre-wisdom about the name of the function.
732732 // We're shortly going to plunge into analyzing the parameters,
733733 // and it would be nice to have some idea of the function name
734734 // for diagnostics whilst we do that.
735735 let initial_rust_name = fun. ident . to_string ( ) ;
736- let diagnostic_display_name = cpp_name
736+ let diagnostic_name = cpp_original_name
737737 . as_ref ( )
738738 . map ( |n| n. diagnostic_display_name ( ) )
739739 . unwrap_or ( & initial_rust_name) ;
740+ let diagnostic_name = QualifiedName :: new ( ns, make_ident ( diagnostic_name) ) ;
740741
741742 // Now let's analyze all the parameters.
742743 // See if any have annotations which our fork of bindgen has craftily inserted...
@@ -747,7 +748,7 @@ impl<'a> FnAnalyzer<'a> {
747748 self . convert_fn_arg (
748749 i,
749750 ns,
750- diagnostic_display_name ,
751+ & diagnostic_name ,
751752 & fun. synthesized_this_type ,
752753 & fun. references ,
753754 true ,
@@ -788,15 +789,15 @@ impl<'a> FnAnalyzer<'a> {
788789 // method, IRN=A_foo, CN=foo output: foo case 4
789790 // method, IRN=A_move, CN=move (keyword problem) output: move_ case 5
790791 // method, IRN=A_foo1, CN=foo (overload) output: foo case 6
791- let ideal_rust_name = match & cpp_name {
792+ let ideal_rust_name = match cpp_original_name {
792793 None => initial_rust_name, // case 1
793- Some ( cpp_name ) => {
794+ Some ( cpp_original_name ) => {
794795 if initial_rust_name. ends_with ( '_' ) {
795796 initial_rust_name // case 2
796- } else if validate_ident_ok_for_rust ( cpp_name ) . is_err ( ) {
797- format ! ( "{}_" , cpp_name . to_string_for_rust_name( ) ) // case 5
797+ } else if validate_ident_ok_for_rust ( cpp_original_name ) . is_err ( ) {
798+ format ! ( "{}_" , cpp_original_name . to_string_for_rust_name( ) ) // case 5
798799 } else {
799- cpp_name . to_string_for_rust_name ( ) // cases 3, 4, 6
800+ cpp_original_name . to_string_for_rust_name ( ) // cases 3, 4, 6
800801 }
801802 }
802803 } ;
@@ -1018,7 +1019,7 @@ impl<'a> FnAnalyzer<'a> {
10181019 0 ,
10191020 fun,
10201021 ns,
1021- & rust_name ,
1022+ & diagnostic_name ,
10221023 & mut params,
10231024 & mut param_details,
10241025 None ,
@@ -1037,7 +1038,7 @@ impl<'a> FnAnalyzer<'a> {
10371038 0 ,
10381039 fun,
10391040 ns,
1040- & rust_name ,
1041+ & diagnostic_name ,
10411042 & mut params,
10421043 & mut param_details,
10431044 Some ( RustConversionType :: FromTypeToPtr ) ,
@@ -1061,7 +1062,7 @@ impl<'a> FnAnalyzer<'a> {
10611062 0 ,
10621063 fun,
10631064 ns,
1064- & rust_name ,
1065+ & diagnostic_name ,
10651066 & mut params,
10661067 & mut param_details,
10671068 Some ( RustConversionType :: FromPinMaybeUninitToPtr ) ,
@@ -1086,7 +1087,7 @@ impl<'a> FnAnalyzer<'a> {
10861087 0 ,
10871088 fun,
10881089 ns,
1089- & rust_name ,
1090+ & diagnostic_name ,
10901091 & mut params,
10911092 & mut param_details,
10921093 Some ( RustConversionType :: FromPinMaybeUninitToPtr ) ,
@@ -1099,7 +1100,7 @@ impl<'a> FnAnalyzer<'a> {
10991100 1 ,
11001101 fun,
11011102 ns,
1102- & rust_name ,
1103+ & diagnostic_name ,
11031104 & mut params,
11041105 & mut param_details,
11051106 Some ( RustConversionType :: FromPinMoveRefToPtr ) ,
@@ -1192,15 +1193,30 @@ impl<'a> FnAnalyzer<'a> {
11921193 & rust_name,
11931194 ns,
11941195 ) ;
1195- if cxxbridge_name != rust_name && cpp_name. is_none ( ) {
1196- cpp_name = Some ( CppEffectiveName :: from_rust_name ( rust_name. clone ( ) ) ) ;
1197- }
1196+
1197+ let api_name_cpp_override = match cpp_original_name {
1198+ Some ( name) => Some ( name. clone ( ) ) ,
1199+ None if cxxbridge_name != rust_name => {
1200+ Some ( CppOriginalName :: from_rust_name ( rust_name. clone ( ) ) )
1201+ }
1202+ None => None ,
1203+ } ;
1204+ let underlying_cpp_function_name = cpp_original_name
1205+ . cloned ( )
1206+ . map ( |n| n. to_effective_name ( ) )
1207+ . unwrap_or_else ( || CppEffectiveName :: from_rust_name ( rust_name. clone ( ) ) ) ;
11981208 let mut cxxbridge_name = make_ident ( & cxxbridge_name) ;
11991209
12001210 // Analyze the return type, just as we previously did for the
12011211 // parameters.
12021212 let mut return_analysis = self
1203- . convert_return_type ( & fun. output , ns, & fun. references , sophistication)
1213+ . convert_return_type (
1214+ & fun. output ,
1215+ ns,
1216+ & diagnostic_name,
1217+ & fun. references ,
1218+ sophistication,
1219+ )
12041220 . unwrap_or_else ( |err| {
12051221 set_ignore_reason ( err) ;
12061222 ReturnTypeAnalysis :: default ( )
@@ -1272,10 +1288,9 @@ impl<'a> FnAnalyzer<'a> {
12721288 . unwrap_or_default ( ) ;
12731289
12741290 // See https://github.com/dtolnay/cxx/issues/878 for the reason for this next line.
1275- let effective_cpp_name =
1276- CppEffectiveName :: from_cpp_name_and_rust_name ( cpp_name. as_ref ( ) , & rust_name) ;
1277- let cpp_name_incompatible_with_cxx =
1278- validate_ident_ok_for_rust ( & effective_cpp_name) . is_err ( ) ;
1291+ let cpp_name_incompatible_with_cxx = cpp_original_name
1292+ . map ( |n| validate_ident_ok_for_rust ( n) . is_err ( ) )
1293+ . unwrap_or_default ( ) ;
12791294 // If possible, we'll put knowledge of the C++ API directly into the cxx::bridge
12801295 // mod. However, there are various circumstances where cxx can't work with the existing
12811296 // C++ API and we need to create a C++ wrapper function which is more cxx-compliant.
@@ -1350,16 +1365,16 @@ impl<'a> FnAnalyzer<'a> {
13501365 CppFunctionBody :: StaticMethodCall (
13511366 ns. clone ( ) ,
13521367 impl_for. get_final_ident ( ) ,
1353- effective_cpp_name ,
1368+ underlying_cpp_function_name ,
13541369 ) ,
13551370 CppFunctionKind :: Function ,
13561371 ) ,
13571372 FnKind :: Method { .. } => (
1358- CppFunctionBody :: FunctionCall ( ns. clone ( ) , effective_cpp_name ) ,
1373+ CppFunctionBody :: FunctionCall ( ns. clone ( ) , underlying_cpp_function_name ) ,
13591374 CppFunctionKind :: Method ,
13601375 ) ,
13611376 _ => (
1362- CppFunctionBody :: FunctionCall ( ns. clone ( ) , effective_cpp_name ) ,
1377+ CppFunctionBody :: FunctionCall ( ns. clone ( ) , underlying_cpp_function_name ) ,
13631378 CppFunctionKind :: Function ,
13641379 ) ,
13651380 } ,
@@ -1388,12 +1403,15 @@ impl<'a> FnAnalyzer<'a> {
13881403 ) ) ;
13891404 }
13901405
1406+ let original_cpp_name = cpp_original_name
1407+ . cloned ( )
1408+ . map ( |n| n. to_effective_name ( ) )
1409+ . unwrap_or_else ( || CppEffectiveName :: from_cxxbridge_name ( & cxxbridge_name) ) ;
1410+
13911411 Some ( CppFunction {
13921412 payload,
13931413 wrapper_function_name : cxxbridge_name. clone ( ) ,
1394- original_cpp_name : cpp_name. clone ( ) . unwrap_or_else ( || {
1395- CppEffectiveName :: from_cxxbridge_name ( cxxbridge_name. clone ( ) )
1396- } ) ,
1414+ original_cpp_name,
13971415 return_conversion : ret_type_conversion. clone ( ) ,
13981416 argument_conversion : param_details. iter ( ) . map ( |d| d. conversion . clone ( ) ) . collect ( ) ,
13991417 kind : cpp_function_kind,
@@ -1435,6 +1453,7 @@ impl<'a> FnAnalyzer<'a> {
14351453 let analysis = FnAnalysis {
14361454 cxxbridge_name : cxxbridge_name. clone ( ) ,
14371455 rust_name : rust_name. clone ( ) ,
1456+ cpp_call_name : api_name_cpp_override,
14381457 rust_rename_strategy,
14391458 params,
14401459 ret_conversion : ret_type_conversion,
@@ -1449,11 +1468,11 @@ impl<'a> FnAnalyzer<'a> {
14491468 externally_callable,
14501469 rust_wrapper_needed,
14511470 } ;
1452- let name = ApiName :: new_with_cpp_name (
1453- ns ,
1454- cxxbridge_name ,
1455- cpp_name . map ( CppOriginalName :: from_effective_name ) ,
1456- ) ;
1471+ // For everything other than functions, the API name is immutable.
1472+ // It would be nice to get to that point with functions, but at present
1473+ // the API name is used in Rust codegen to generate "use" statements ,
1474+ // so we override it.
1475+ let name = ApiName :: new_with_cpp_name ( ns , cxxbridge_name , cpp_original_name . cloned ( ) ) ;
14571476 ( analysis, name)
14581477 }
14591478
@@ -1478,7 +1497,7 @@ impl<'a> FnAnalyzer<'a> {
14781497 param_idx : usize ,
14791498 fun : & FuncToConvert ,
14801499 ns : & Namespace ,
1481- rust_name : & str ,
1500+ diagnostic_name : & QualifiedName ,
14821501 params : & mut Punctuated < FnArg , Comma > ,
14831502 param_details : & mut [ ArgumentAnalysis ] ,
14841503 force_rust_conversion : Option < RustConversionType > ,
@@ -1489,7 +1508,7 @@ impl<'a> FnAnalyzer<'a> {
14891508 self . convert_fn_arg (
14901509 fun. inputs . iter ( ) . nth ( param_idx) . unwrap ( ) ,
14911510 ns,
1492- rust_name ,
1511+ diagnostic_name ,
14931512 & fun. synthesized_this_type ,
14941513 & fun. references ,
14951514 false ,
@@ -1645,7 +1664,7 @@ impl<'a> FnAnalyzer<'a> {
16451664 & mut self ,
16461665 arg : & FnArg ,
16471666 ns : & Namespace ,
1648- fn_name : & str ,
1667+ diagnostic_name : & QualifiedName ,
16491668 virtual_this : & Option < QualifiedName > ,
16501669 references : & References ,
16511670 treat_this_as_reference : bool ,
@@ -1691,13 +1710,12 @@ impl<'a> FnAnalyzer<'a> {
16911710 Ok ( ( this_type, receiver_mutability) )
16921711 }
16931712 _ => Err ( ConvertErrorFromCpp :: UnexpectedThisType (
1694- QualifiedName :: new ( ns , make_ident ( fn_name ) ) ,
1713+ diagnostic_name . clone ( ) ,
16951714 ) ) ,
16961715 } ,
1697- _ => Err ( ConvertErrorFromCpp :: UnexpectedThisType ( QualifiedName :: new (
1698- ns,
1699- make_ident ( fn_name) ,
1700- ) ) ) ,
1716+ _ => Err ( ConvertErrorFromCpp :: UnexpectedThisType (
1717+ diagnostic_name. clone ( ) ,
1718+ ) ) ,
17011719 } ?;
17021720 self_type = Some ( this_type) ;
17031721 is_placement_return_destination = construct_into_self;
@@ -1924,6 +1942,7 @@ impl<'a> FnAnalyzer<'a> {
19241942 & mut self ,
19251943 rt : & ReturnType ,
19261944 ns : & Namespace ,
1945+ diagnostic_name : & QualifiedName ,
19271946 references : & References ,
19281947 sophistication : TypeConversionSophistication ,
19291948 ) -> Result < ReturnTypeAnalysis , ConvertErrorFromCpp > {
@@ -1955,7 +1974,7 @@ impl<'a> FnAnalyzer<'a> {
19551974 let ( fnarg, analysis) = self . convert_fn_arg (
19561975 & fnarg,
19571976 ns,
1958- "" ,
1977+ diagnostic_name ,
19591978 & None ,
19601979 & References :: default ( ) ,
19611980 false ,
@@ -2148,7 +2167,7 @@ impl<'a> FnAnalyzer<'a> {
21482167 . get ( & self_ty. name )
21492168 . cloned ( )
21502169 . or_else ( || Some ( self_ty. name . get_final_item ( ) . to_string ( ) ) )
2151- . map ( CppOriginalName :: from_string_for_constructor )
2170+ . map ( CppOriginalName :: from_type_name_for_constructor )
21522171 } else {
21532172 None
21542173 } ;
0 commit comments