Skip to content

Commit 4c824c3

Browse files
authored
Avoid extremely long names in overloaded methods (#431)
```diff - pub unsafe fn apply_Optionunsafefnpmuti32_i32__i32_const( - &self, - mut f: Option<unsafe fn(*mut i32, i32)>, - mut x: i32, - ) -> i32 { + pub unsafe fn apply_11(&self, mut f: Option<unsafe fn(*mut i32, i32)>, mut x: i32) -> i32 { ``` This generates more readable names and avoid special-casing characters in the mangled name. Also it fixes a problem in the codegen where a template is visited before the type it's instantiated with.
1 parent 6b7dc24 commit 4c824c3

40 files changed

Lines changed: 611 additions & 492 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) {
11081108
CanUseCopyOrMoveName(decl, name)) {
11091109
return name;
11101110
}
1111-
return GetOverloadedFunctionName(decl);
1111+
return std::format("new_{}", GetCtorIndex(decl));
11121112
}
11131113
return GetNumberOfConvertingCtors(decl->getParent()) != 1
11141114
? std::format("new_{}", GetCtorIndex(decl))
@@ -4044,28 +4044,13 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) {
40444044
}
40454045

40464046
std::string
4047-
Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
4047+
Converter::GetOverloadedFunctionName(const clang::CXXMethodDecl *decl) {
40484048
auto name = GetFunctionBaseName(decl);
40494049
if (auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(decl)) {
40504050
name = GetConversionName(
40514051
conversion, GetUnsafeTypeAsString(conversion->getConversionType()));
40524052
}
4053-
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(decl);
4054-
ctor && !ctor->getParent()->getIdentifier()) {
4055-
name = GetRecordName(ctor->getParent());
4056-
}
4057-
4058-
if (decl->getNumParams() != 0U) {
4059-
name += '_';
4060-
}
4061-
4062-
for (auto *parameter : decl->parameters()) {
4063-
name += GetUnsafeTypeAsString(parameter->getType());
4064-
if (parameter->getType()->isRValueReferenceType()) {
4065-
name += "_rv";
4066-
}
4067-
name += '_';
4068-
}
4053+
name += std::format("_{}", GetMethodIndex(decl));
40694054

40704055
if (const auto *targs = decl->getTemplateSpecializationArgs()) {
40714056
std::vector<clang::TemplateArgument> args;
@@ -4094,31 +4079,6 @@ Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
40944079
}
40954080
}
40964081

4097-
auto pred = [](char ch) { return ch != ' ' && ch != '_'; };
4098-
name.erase(std::find_if(name.rbegin(), name.rend(), pred).base(), name.end());
4099-
4100-
if (decl->isVariadic()) {
4101-
name += "_va";
4102-
}
4103-
if (const auto *method = clang::dyn_cast<clang::CXXMethodDecl>(decl)) {
4104-
if (method->isConst()) {
4105-
name += "_const";
4106-
}
4107-
if (method->isVolatile()) {
4108-
name += "_volatile";
4109-
}
4110-
switch (method->getRefQualifier()) {
4111-
case clang::RQ_LValue:
4112-
name += "_lref";
4113-
break;
4114-
case clang::RQ_RValue:
4115-
name += "_rref";
4116-
break;
4117-
case clang::RQ_None:
4118-
break;
4119-
}
4120-
}
4121-
41224082
ToIdentifier(name);
41234083
return name;
41244084
}

‎cpp2rust/converter/converter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
583583
virtual std::string ConvertVarDefaultInit(clang::QualType qual_type);
584584

585585
virtual std::string
586-
GetOverloadedFunctionName(const clang::FunctionDecl *decl);
586+
GetOverloadedFunctionName(const clang::CXXMethodDecl *decl);
587587

588588
virtual std::string GetRecordName(const clang::NamedDecl *decl) const;
589589

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,24 @@ bool IsOverloadedMethod(const clang::CXXMethodDecl *decl) {
352352
}) > 1;
353353
}
354354

355+
unsigned GetMethodIndex(const clang::CXXMethodDecl *decl) {
356+
const clang::Decl *key = decl->getCanonicalDecl();
357+
if (auto *tmpl = decl->getPrimaryTemplate()) {
358+
key = tmpl->getCanonicalDecl();
359+
}
360+
unsigned index = 0;
361+
for (auto *d : decl->getParent()->decls()) {
362+
if (clang::isa<clang::CXXMethodDecl, clang::FunctionTemplateDecl>(d)) {
363+
++index;
364+
if (d->getCanonicalDecl() == key) {
365+
return index;
366+
}
367+
}
368+
}
369+
assert(false && "method not found in its record");
370+
return 0;
371+
}
372+
355373
const char *GetCopyOrMoveName(const clang::CXXMethodDecl *method) {
356374
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method)) {
357375
if (ctor->isCopyConstructor()) {

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ void ForEachTemplateInstantiatedMethod(
8181

8282
bool IsOverloadedMethod(const clang::CXXMethodDecl *decl);
8383

84+
unsigned GetMethodIndex(const clang::CXXMethodDecl *decl);
85+
8486
const char *GetCopyOrMoveName(const clang::CXXMethodDecl *method);
8587

8688
bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,

‎tests/unit/out/refcount/10_struct.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ pub trait PartialImpl {
211211
fn get(&self) -> Ptr<i32> {
212212
unimplemented!()
213213
}
214-
fn next(&self) -> Ptr<Partial> {
214+
fn next_4(&self) -> Ptr<Partial> {
215215
unimplemented!()
216216
}
217-
fn next_i32(&self, _a0: i32) -> Partial {
217+
fn next_5(&self, _a0: i32) -> Partial {
218218
unimplemented!()
219219
}
220220
}

‎tests/unit/out/refcount/as_const.rs‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,39 @@ fn main_0() -> i32 {
4949
let s: Value<S> = Rc::new(RefCell::new(S {
5050
v: Rc::new(RefCell::new(7)),
5151
}));
52-
assert!(((({ SImpl::f(&s.as_pointer(),) }) as i32) == (Overload_kMutableOverload as i32)));
53-
assert!(((({ SImpl::f_const(&s.as_pointer(),) }) as i32) == (Overload_kConstOverload as i32)));
52+
assert!(((({ SImpl::f_1(&s.as_pointer(),) }) as i32) == (Overload_kMutableOverload as i32)));
53+
assert!(((({ SImpl::f_2(&s.as_pointer(),) }) as i32) == (Overload_kConstOverload as i32)));
5454
assert!(((({ g_0(s.as_pointer(),) }) as i32) == (Overload_kMutableOverload as i32)));
5555
assert!(((({ g_1(s.as_pointer(),) }) as i32) == (Overload_kConstOverload as i32)));
56-
({ SImpl::value_ref(&s.as_pointer()) }).write(9);
56+
({ SImpl::value_ref_3(&s.as_pointer()) }).write(9);
5757
assert!(((*(*s.borrow()).v.borrow()) == 9));
58-
assert!(((({ SImpl::value_ref_const(&s.as_pointer(),) }).read()) == 9));
58+
assert!(((({ SImpl::value_ref_4(&s.as_pointer(),) }).read()) == 9));
5959
let cs: Ptr<S> = s.as_pointer();
60-
assert!(((({ SImpl::f_const(&cs,) }) as i32) == (Overload_kConstOverload as i32)));
60+
assert!(((({ SImpl::f_2(&cs,) }) as i32) == (Overload_kConstOverload as i32)));
6161
assert!(((*(*cs.upgrade().deref()).v.borrow()) == 9));
6262
let p: Value<Ptr<S>> = Rc::new(RefCell::new((s.as_pointer())));
6363
(*(*(*p.borrow()).upgrade().deref()).v.borrow_mut()) = 11;
6464
assert!(((*(*s.borrow()).v.borrow()) == 11));
65-
assert!(((({ SImpl::f(&(*p.borrow()),) }) as i32) == (Overload_kMutableOverload as i32)));
65+
assert!(((({ SImpl::f_1(&(*p.borrow()),) }) as i32) == (Overload_kMutableOverload as i32)));
6666
return 0;
6767
}
6868
pub trait SImpl {
69-
fn f(&self) -> Overload;
70-
fn f_const(&self) -> Overload;
71-
fn value_ref(&self) -> Ptr<i32>;
72-
fn value_ref_const(&self) -> Ptr<i32>;
69+
fn f_1(&self) -> Overload;
70+
fn f_2(&self) -> Overload;
71+
fn value_ref_3(&self) -> Ptr<i32>;
72+
fn value_ref_4(&self) -> Ptr<i32>;
7373
}
7474
impl SImpl for Ptr<S> {
75-
fn f(&self) -> Overload {
75+
fn f_1(&self) -> Overload {
7676
return Overload_kMutableOverload;
7777
}
78-
fn f_const(&self) -> Overload {
78+
fn f_2(&self) -> Overload {
7979
return Overload_kConstOverload;
8080
}
81-
fn value_ref(&self) -> Ptr<i32> {
81+
fn value_ref_3(&self) -> Ptr<i32> {
8282
return (*(*self).upgrade().deref()).v.as_pointer();
8383
}
84-
fn value_ref_const(&self) -> Ptr<i32> {
84+
fn value_ref_4(&self) -> Ptr<i32> {
8585
return (*(*self).upgrade().deref()).v.as_pointer();
8686
}
8787
}

‎tests/unit/out/refcount/class_templates.rs‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ fn main_0() -> i32 {
346346
});
347347
assert!(
348348
(({ MyContainer_int_Impl::size(&imc.as_pointer(),) }) == 1_usize)
349-
&& ((({ MyContainer_int_Impl::back(&imc.as_pointer(),) }).read()) == 1)
349+
&& ((({ MyContainer_int_Impl::back_4(&imc.as_pointer(),) }).read()) == 1)
350350
);
351351
({ MyContainer_int_Impl::pop_back(&imc.as_pointer()) });
352352
assert!(({ MyContainer_int_Impl::empty(&imc.as_pointer(),) }));
@@ -358,7 +358,7 @@ fn main_0() -> i32 {
358358
});
359359
assert!(
360360
(({ MyContainer_char_Impl::size(&cmc.as_pointer(),) }) == 1_usize)
361-
&& (((({ MyContainer_char_Impl::back(&cmc.as_pointer(),) }).read()) as i32)
361+
&& (((({ MyContainer_char_Impl::back_4(&cmc.as_pointer(),) }).read()) as i32)
362362
== (('a' as u8) as i32))
363363
);
364364
({ MyContainer_char_Impl::pop_back(&cmc.as_pointer()) });
@@ -371,7 +371,8 @@ fn main_0() -> i32 {
371371
});
372372
assert!(
373373
(({ MyContainer_float_Impl::size(&fmc.as_pointer(),) }) == 1_usize)
374-
&& (((({ MyContainer_float_Impl::back(&fmc.as_pointer(),) }).read()) as f64) == 1.0E+0)
374+
&& (((({ MyContainer_float_Impl::back_4(&fmc.as_pointer(),) }).read()) as f64)
375+
== 1.0E+0)
375376
);
376377
({ MyContainer_float_Impl::pop_back(&fmc.as_pointer()) });
377378
assert!(({ MyContainer_float_Impl::empty(&fmc.as_pointer(),) }));
@@ -398,10 +399,10 @@ impl Boxed_long_Impl for Ptr<Boxed_long_> {
398399
pub trait MyContainer_char_Impl {
399400
fn empty(&self) -> bool;
400401
fn size(&self) -> usize;
401-
fn back_const(&self) -> Ptr<u8> {
402+
fn back_3(&self) -> Ptr<u8> {
402403
unimplemented!()
403404
}
404-
fn back(&self) -> Ptr<u8>;
405+
fn back_4(&self) -> Ptr<u8>;
405406
fn pop_back(&self);
406407
fn push_back(&self, item: Ptr<u8>);
407408
}
@@ -412,7 +413,7 @@ impl MyContainer_char_Impl for Ptr<MyContainer_char_> {
412413
fn size(&self) -> usize {
413414
return (*(*(*self).upgrade().deref()).vec_.borrow()).len();
414415
}
415-
fn back(&self) -> Ptr<u8> {
416+
fn back_4(&self) -> Ptr<u8> {
416417
return ((*(*self).upgrade().deref()).vec_.as_pointer() as Ptr<u8>).to_last();
417418
}
418419
fn pop_back(&self) {
@@ -429,10 +430,10 @@ impl MyContainer_char_Impl for Ptr<MyContainer_char_> {
429430
pub trait MyContainer_float_Impl {
430431
fn empty(&self) -> bool;
431432
fn size(&self) -> usize;
432-
fn back_const(&self) -> Ptr<f32> {
433+
fn back_3(&self) -> Ptr<f32> {
433434
unimplemented!()
434435
}
435-
fn back(&self) -> Ptr<f32>;
436+
fn back_4(&self) -> Ptr<f32>;
436437
fn pop_back(&self);
437438
fn push_back(&self, item: Ptr<f32>);
438439
}
@@ -443,7 +444,7 @@ impl MyContainer_float_Impl for Ptr<MyContainer_float_> {
443444
fn size(&self) -> usize {
444445
return (*(*(*self).upgrade().deref()).vec_.borrow()).len();
445446
}
446-
fn back(&self) -> Ptr<f32> {
447+
fn back_4(&self) -> Ptr<f32> {
447448
return ((*(*self).upgrade().deref()).vec_.as_pointer() as Ptr<f32>).to_last();
448449
}
449450
fn pop_back(&self) {
@@ -460,10 +461,10 @@ impl MyContainer_float_Impl for Ptr<MyContainer_float_> {
460461
pub trait MyContainer_int_Impl {
461462
fn empty(&self) -> bool;
462463
fn size(&self) -> usize;
463-
fn back_const(&self) -> Ptr<i32> {
464+
fn back_3(&self) -> Ptr<i32> {
464465
unimplemented!()
465466
}
466-
fn back(&self) -> Ptr<i32>;
467+
fn back_4(&self) -> Ptr<i32>;
467468
fn pop_back(&self);
468469
fn push_back(&self, item: Ptr<i32>);
469470
}
@@ -474,7 +475,7 @@ impl MyContainer_int_Impl for Ptr<MyContainer_int_> {
474475
fn size(&self) -> usize {
475476
return (*(*(*self).upgrade().deref()).vec_.borrow()).len();
476477
}
477-
fn back(&self) -> Ptr<i32> {
478+
fn back_4(&self) -> Ptr<i32> {
478479
return ((*(*self).upgrade().deref()).vec_.as_pointer() as Ptr<i32>).to_last();
479480
}
480481
fn pop_back(&self) {

‎tests/unit/out/refcount/copy_assign.rs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,8 @@ fn main_0() -> i32 {
254254
let n1: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::new()));
255255
let n2: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::new()));
256256
let cn: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::new()));
257-
({ NonConstAssignImpl::operator_assign_pmutNonConstAssign(&n1.as_pointer(), n.as_pointer()) });
258-
({
259-
NonConstAssignImpl::operator_assign_pconstNonConstAssign(&n2.as_pointer(), cn.as_pointer())
260-
});
257+
({ NonConstAssignImpl::operator_assign_2(&n1.as_pointer(), n.as_pointer()) });
258+
({ NonConstAssignImpl::operator_assign_3(&n2.as_pointer(), cn.as_pointer()) });
261259
assert!(((*(*n1.borrow()).mark.borrow()) == 1));
262260
assert!(((*(*n2.borrow()).mark.borrow()) == 10));
263261
let r: Value<RefQualified> = Rc::new(RefCell::new(RefQualified::new()));
@@ -267,16 +265,16 @@ fn main_0() -> i32 {
267265
return 0;
268266
}
269267
pub trait NonConstAssignImpl {
270-
fn operator_assign_pmutNonConstAssign(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign>;
271-
fn operator_assign_pconstNonConstAssign(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign>;
268+
fn operator_assign_2(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign>;
269+
fn operator_assign_3(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign>;
272270
}
273271
impl NonConstAssignImpl for Ptr<NonConstAssign> {
274-
fn operator_assign_pmutNonConstAssign(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign> {
272+
fn operator_assign_2(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign> {
275273
let __rhs = ((*(*o.upgrade().deref()).mark.borrow()) + 1);
276274
(*(*(*self).upgrade().deref()).mark.borrow_mut()) = __rhs;
277275
return (*self).clone();
278276
}
279-
fn operator_assign_pconstNonConstAssign(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign> {
277+
fn operator_assign_3(&self, o: Ptr<NonConstAssign>) -> Ptr<NonConstAssign> {
280278
let __rhs = ((*(*o.upgrade().deref()).mark.borrow()) + 10);
281279
(*(*(*self).upgrade().deref()).mark.borrow_mut()) = __rhs;
282280
return (*self).clone();

‎tests/unit/out/refcount/copy_ctor.rs‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ impl NonConst {
6262
let this: Ptr<NonConst> = __this.as_pointer();
6363
Rc::try_unwrap(__this).ok().unwrap().into_inner()
6464
}
65-
pub fn NonConst_pmutNonConst(o: Ptr<NonConst>) -> Self {
65+
pub fn new_1(o: Ptr<NonConst>) -> Self {
6666
let __this: Value<NonConst> = Rc::new(RefCell::new(Self {
6767
mark: Rc::new(RefCell::new(((*(*o.upgrade().deref()).mark.borrow()) + 1))),
6868
}));
6969
let this: Ptr<NonConst> = __this.as_pointer();
7070
Rc::try_unwrap(__this).ok().unwrap().into_inner()
7171
}
72-
pub fn NonConst_pconstNonConst(o: Ptr<NonConst>) -> Self {
72+
pub fn new_2(o: Ptr<NonConst>) -> Self {
7373
let __this: Value<NonConst> = Rc::new(RefCell::new(Self {
7474
mark: Rc::new(RefCell::new(((*(*o.upgrade().deref()).mark.borrow()) + 10))),
7575
}));
@@ -82,7 +82,7 @@ impl Clone for NonConst {
8282
let __src: Value<NonConst> = Rc::new(RefCell::new(NonConst {
8383
mark: self.mark.clone(),
8484
}));
85-
NonConst::NonConst_pmutNonConst(__src.as_pointer())
85+
NonConst::new_1(__src.as_pointer())
8686
}
8787
}
8888
impl Default for NonConst {
@@ -259,13 +259,9 @@ fn main_0() -> i32 {
259259
assert!(((*(*i1.borrow()).v.borrow()) == 1) && ((*(*i2.borrow()).v.borrow()) == -1_i32));
260260
assert!((copies_0.with(|rc| *rc.borrow()) == 11));
261261
let n: Value<NonConst> = Rc::new(RefCell::new(NonConst::new()));
262-
let n1: Value<NonConst> = Rc::new(RefCell::new(NonConst::NonConst_pmutNonConst({
263-
n.as_pointer()
264-
})));
262+
let n1: Value<NonConst> = Rc::new(RefCell::new(NonConst::new_1({ n.as_pointer() })));
265263
let cn: Value<NonConst> = Rc::new(RefCell::new(NonConst::new()));
266-
let n2: Value<NonConst> = Rc::new(RefCell::new(NonConst::NonConst_pconstNonConst({
267-
cn.as_pointer()
268-
})));
264+
let n2: Value<NonConst> = Rc::new(RefCell::new(NonConst::new_2({ cn.as_pointer() })));
269265
assert!(((*(*n1.borrow()).mark.borrow()) == 1));
270266
assert!(((*(*n2.borrow()).mark.borrow()) == 10));
271267
return 0;

‎tests/unit/out/refcount/fn_ptr_struct.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ pub fn negate_1(x: i32) -> i32 {
5757
#[derive(Clone, ByteRepr, VaArg, FnPtrArg, Default)]
5858
pub struct S {}
5959
impl S {
60-
pub fn pick_i32(x: i32) -> i32 {
60+
pub fn pick_1(x: i32) -> i32 {
6161
let x: Value<i32> = Rc::new(RefCell::new(x));
6262
return ((*x.borrow()) + 1);
6363
}
64-
pub fn pick_i64(x: i64) -> i32 {
64+
pub fn pick_2(x: i64) -> i32 {
6565
let x: Value<i64> = Rc::new(RefCell::new(x));
6666
return (((*x.borrow()) as i32) + 2);
6767
}
@@ -76,15 +76,15 @@ pub fn main() {
7676
}
7777
fn main_0() -> i32 {
7878
let p1: Value<FnPtr<fn(i32) -> i32>> =
79-
Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(S::pick_i32))));
79+
Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(S::pick_1))));
8080
let p2: Value<FnPtr<fn(i32) -> i32>> =
8181
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(S::solo)));
8282
assert!((({ (*p1.borrow()).call(5,) }) == 6));
8383
assert!((({ (*p2.borrow()).call(5,) }) == 8));
84-
assert!((({ S::pick_i64(5_i64,) }) == 7));
84+
assert!((({ S::pick_2(5_i64,) }) == 7));
8585
let h3: Value<Handler> = Rc::new(RefCell::new(Handler {
8686
tag: Rc::new(RefCell::new(3)),
87-
cb: Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(S::pick_i32)))),
87+
cb: Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(S::pick_1)))),
8888
}));
8989
assert!((({ (*(*h3.borrow()).cb.borrow()).call(1,) }) == 2));
9090
let h1: Value<Handler> = Rc::new(RefCell::new(Handler {

0 commit comments

Comments
 (0)