Skip to content

Commit 0dd316c

Browse files
authored
Merge pull request #1437 from google/further-name-tweaks
Further name tweaks
2 parents 0889034 + 52a8430 commit 0dd316c

File tree

8 files changed

+140
-116
lines changed

8 files changed

+140
-116
lines changed

engine/src/conversion/analysis/fun/function_wrapper.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,8 @@ pub(crate) enum CppFunctionKind {
257257
pub(crate) struct CppFunction {
258258
pub(crate) payload: CppFunctionBody,
259259
pub(crate) wrapper_function_name: crate::minisyn::Ident,
260+
/// The following field is apparently only used for
261+
/// subclass calls.
260262
pub(crate) original_cpp_name: CppEffectiveName,
261263
pub(crate) return_conversion: Option<TypeConversionPolicy>,
262264
pub(crate) argument_conversion: Vec<TypeConversionPolicy>,

engine/src/conversion/analysis/fun/mod.rs

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

engine/src/conversion/api.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,17 +359,18 @@ impl ApiName {
359359
Self::new(&Namespace::new(), id)
360360
}
361361

362+
/// Return the C++ name to use here, which will use the Rust
363+
/// name unless it's been overridden by a C++ name.
362364
pub(crate) fn cpp_name(&self) -> CppEffectiveName {
363365
CppEffectiveName::from_api_details(&self.cpp_name, &self.name)
364366
}
365367

366368
pub(crate) fn qualified_cpp_name(&self) -> String {
367-
let cpp_name = self.cpp_name();
368369
self.name
369370
.ns_segment_iter()
370371
.chain(std::iter::once(
371-
cpp_name
372-
.to_string_to_make_qualified_name()
372+
self.cpp_name()
373+
.to_string_for_cpp_generation()
373374
.to_string()
374375
.as_str(),
375376
))

engine/src/conversion/codegen_rs/fun_codegen.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use crate::{
3232
MethodKind, RustRenameStrategy, TraitMethodDetails,
3333
},
3434
api::UnsafetyNeeded,
35-
CppEffectiveName,
3635
},
3736
minisyn::minisynize_vec,
3837
types::{Namespace, QualifiedName},
@@ -89,14 +88,14 @@ pub(super) fn gen_function(
8988
ns: &Namespace,
9089
fun: FuncToConvert,
9190
analysis: FnAnalysis,
92-
cpp_call_name: CppEffectiveName,
9391
non_pod_types: &HashSet<QualifiedName>,
9492
) -> RsCodegenResult {
9593
if analysis.ignore_reason.is_err() || !analysis.externally_callable {
9694
return RsCodegenResult::default();
9795
}
9896
let cxxbridge_name = analysis.cxxbridge_name;
9997
let rust_name = &analysis.rust_name;
98+
let cpp_call_name = &analysis.cpp_call_name;
10099
let ret_type = analysis.ret_type;
101100
let ret_conversion = analysis.ret_conversion;
102101
let param_details = analysis.param_details;
@@ -177,10 +176,13 @@ pub(super) fn gen_function(
177176
_ => Some(Use::UsedFromCxxBridge),
178177
},
179178
};
180-
if cpp_call_name.does_not_match_cxxbridge_name(&cxxbridge_name) && !wrapper_function_needed {
181-
cpp_name_attr = Attribute::parse_outer
182-
.parse2(cpp_call_name.generate_cxxbridge_name_attribute())
183-
.unwrap();
179+
if let Some(cpp_call_name) = cpp_call_name {
180+
if cpp_call_name.does_not_match_cxxbridge_name(&cxxbridge_name) && !wrapper_function_needed
181+
{
182+
cpp_name_attr = Attribute::parse_outer
183+
.parse2(cpp_call_name.generate_cxxbridge_name_attribute())
184+
.unwrap();
185+
}
184186
}
185187

186188
// Finally - namespace support. All the Types in everything

engine/src/conversion/codegen_rs/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,6 @@ impl<'a> RsCodeGenerator<'a> {
464464
) -> RsCodegenResult {
465465
let name = api.name().clone();
466466
let id = name.get_final_ident();
467-
let cpp_call_name = api.effective_cpp_name();
468467
match api {
469468
Api::StringConstructor { .. } => {
470469
let make_string_name = make_ident(self.config.get_makestring_name());
@@ -479,13 +478,9 @@ impl<'a> RsCodeGenerator<'a> {
479478
..Default::default()
480479
}
481480
}
482-
Api::Function { fun, analysis, .. } => gen_function(
483-
name.get_namespace(),
484-
*fun,
485-
analysis,
486-
cpp_call_name,
487-
non_pod_types,
488-
),
481+
Api::Function { fun, analysis, .. } => {
482+
gen_function(name.get_namespace(), *fun, analysis, non_pod_types)
483+
}
489484
Api::Const { const_item, .. } => RsCodegenResult {
490485
bindgen_mod_items: vec![Item::Const(const_item.into())],
491486
materializations: vec![Use::UsedFromBindgen],

0 commit comments

Comments
 (0)