Skip to content

Commit 7f3aebc

Browse files
committed
Merge branch 'master' into idiomatic-name
2 parents 8e258de + 4e148ca commit 7f3aebc

68 files changed

Lines changed: 983 additions & 810 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,10 @@ std::string Converter::GetMethodName(const clang::CXXMethodDecl *decl) {
11021102
if (IsOverloadedMethod(decl)) {
11031103
return GetOverloadedFunctionName(decl);
11041104
}
1105+
if (auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(decl)) {
1106+
return GetConversionName(
1107+
conversion, GetUnsafeTypeAsString(conversion->getConversionType()));
1108+
}
11051109
return GetNamedDeclAsString(decl);
11061110
}
11071111

@@ -1150,10 +1154,9 @@ std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) {
11501154
}
11511155
return GetOverloadedFunctionName(decl);
11521156
}
1153-
return GetRecordName(decl->getParent()) +
1154-
(GetNumberOfConvertingCtors(decl->getParent()) != 1
1155-
? std::to_string(GetCtorIndex(decl))
1156-
: "");
1157+
return GetNumberOfConvertingCtors(decl->getParent()) != 1
1158+
? std::format("new_{}", GetCtorIndex(decl))
1159+
: "new";
11571160
}
11581161

11591162
bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) {
@@ -4090,6 +4093,10 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) {
40904093
std::string
40914094
Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
40924095
auto name = GetFunctionBaseName(decl);
4096+
if (auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(decl)) {
4097+
name = GetConversionName(
4098+
conversion, GetUnsafeTypeAsString(conversion->getConversionType()));
4099+
}
40934100
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(decl);
40944101
ctor && !ctor->getParent()->getIdentifier()) {
40954102
name = GetRecordName(ctor->getParent());
@@ -4159,19 +4166,7 @@ Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
41594166
}
41604167
}
41614168

4162-
ReplaceAll(name, "[", "arr");
4163-
ReplaceAll(name, "]", "arr");
4164-
ReplaceAll(name, ";", "_");
4165-
ReplaceAll(name, ",", "_");
4166-
name.erase(std::remove_if(name.begin(), name.end(),
4167-
[](char c) {
4168-
return c == '<' || c == '>' || c == ' ' ||
4169-
c == ':' || c == '(' || c == ')' ||
4170-
c == '-';
4171-
}),
4172-
name.end());
4173-
std::replace(name.begin(), name.end(), '*', 'p');
4174-
4169+
ToIdentifier(name);
41754170
return name;
41764171
}
41774172

‎cpp2rust/converter/converter.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,35 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
132132
virtual const char *CharRustType() const { return "libc::c_char"; }
133133

134134
virtual bool VisitCXXMethodDecl(clang::CXXMethodDecl *decl);
135+
135136
virtual bool ShouldConvertMethod(const clang::CXXMethodDecl *decl);
137+
136138
virtual bool ConvertOutOfLineMethod(clang::CXXMethodDecl *decl);
139+
137140
bool ConvertCXXMethodDecl(clang::CXXMethodDecl *decl);
141+
138142
std::string GetMethodName(const clang::CXXMethodDecl *decl);
143+
139144
virtual std::string GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl);
145+
140146
std::string GetCtorName(clang::CXXConstructorDecl *decl);
147+
141148
virtual void ConvertCXXRecordMethods(clang::CXXRecordDecl *decl);
149+
142150
virtual void ConvertLateInstantiatedMethods(clang::CXXRecordDecl *decl);
151+
143152
virtual std::string DestroyMembers(const clang::CXXRecordDecl *decl);
153+
144154
virtual void EmitScopedDestructor(const clang::VarDecl *decl);
155+
145156
void EmitDeallocation(clang::CXXDeleteExpr *expr,
146157
const std::string &argument_as_string);
158+
147159
virtual void SetUFCSReceiver(clang::Expr *base, bool is_arrow,
148160
const clang::CXXMethodDecl *method);
161+
149162
void ConvertUserOperatorCall(clang::CXXOperatorCallExpr *expr);
163+
150164
virtual std::string GetUFCSName(const clang::CXXMethodDecl *method) const;
151165

152166
virtual bool ThisIsRustPtr() const { return false; }

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,35 @@ std::string GetFunctionBaseName(const clang::FunctionDecl *decl) {
980980
return decl->getNameAsString();
981981
}
982982

983+
void ToIdentifier(std::string &name) {
984+
ReplaceAll(name, "[", "arr");
985+
ReplaceAll(name, "]", "arr");
986+
ReplaceAll(name, ";", "_");
987+
ReplaceAll(name, ",", "_");
988+
name.erase(std::remove_if(name.begin(), name.end(),
989+
[](char c) {
990+
return c == '<' || c == '>' || c == ' ' ||
991+
c == ':' || c == '(' || c == ')' ||
992+
c == '-';
993+
}),
994+
name.end());
995+
std::replace(name.begin(), name.end(), '*', 'p');
996+
}
997+
998+
std::string GetConversionName(const clang::CXXConversionDecl *decl,
999+
const std::string &rust_type) {
1000+
auto name = "to_" + rust_type;
1001+
ToIdentifier(name);
1002+
const auto *record = decl->getParent();
1003+
bool is_unique_name =
1004+
std::none_of(record->method_begin(), record->method_end(),
1005+
[&name](const clang::CXXMethodDecl *method) {
1006+
return method->getDeclName().isIdentifier() &&
1007+
method->getName() == name;
1008+
});
1009+
return is_unique_name ? name : GetFunctionBaseName(decl);
1010+
}
1011+
9831012
clang::CXXDestructorDecl *
9841013
GetUserDefinedDestructor(const clang::CXXRecordDecl *decl) {
9851014
if (!decl->hasDefinition() || !IsUserDefinedDecl(decl) ||

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ const char *GetOverloadedOperator(const clang::FunctionDecl *decl);
170170

171171
std::string GetFunctionBaseName(const clang::FunctionDecl *decl);
172172

173+
void ToIdentifier(std::string &name);
174+
175+
std::string GetConversionName(const clang::CXXConversionDecl *decl,
176+
const std::string &rust_type);
177+
173178
bool IsImplicitAssignmentCall(const clang::CallExpr *expr);
174179
bool IsUserOperatorCall(const clang::CXXOperatorCallExpr *expr);
175180

‎rules/new/src.cpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ void f2(void *a0) { return ::operator delete(a0); }
1010
void *f3(std::size_t a0) { return ::operator new[](a0); }
1111

1212
void f4(void *a0) { return ::operator delete[](a0); }
13+
14+
template <typename T1> T1 *f5(T1 *a0) { return std::launder(a0); }

‎rules/new/tgt_refcount.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ fn f3(a0: usize) -> AnyPtr {
1818
fn f4(a0: AnyPtr) {
1919
libcc2rs::free_refcount(a0)
2020
}
21+
22+
fn f5<T1>(a0: Ptr<T1>) -> Ptr<T1> {
23+
a0
24+
}

‎rules/new/tgt_unsafe.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ unsafe fn f3(a0: usize) -> *mut ::libc::c_void {
1616
unsafe fn f4(a0: *mut ::libc::c_void) {
1717
libcc2rs::free_unsafe(a0)
1818
}
19+
20+
unsafe fn f5<T1>(a0: *mut T1) -> *mut T1 {
21+
a0
22+
}

‎tests/multi-file/defaulted_move_cross_tu/out/refcount/defaulted_move_cross_tu.rs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub struct S {
1212
pub n: Value<Box<[i32]>>,
1313
}
1414
impl S {
15-
pub fn S(x: i32) -> Self {
15+
pub fn new(x: i32) -> Self {
1616
let x: Value<i32> = Rc::new(RefCell::new(x));
1717
let __this: Value<S> = Rc::new(RefCell::new(Self {
1818
v: Rc::new(RefCell::new(vec![
@@ -24,7 +24,7 @@ impl S {
2424
let this: Ptr<S> = __this.as_pointer();
2525
Rc::try_unwrap(__this).ok().unwrap().into_inner()
2626
}
27-
pub fn move_from(_a0: Ptr<S>) -> Self {
27+
pub fn S_pmutS_rv(_a0: Ptr<S>) -> Self {
2828
let __this: Value<S> = Rc::new(RefCell::new(Self {
2929
v: Rc::new(RefCell::new(std::mem::take(
3030
&mut (*(*_a0.upgrade().deref()).v.borrow_mut()),
@@ -76,26 +76,26 @@ pub fn main() {
7676
std::process::exit(main_0());
7777
}
7878
fn main_0() -> i32 {
79-
let s: Value<S> = Rc::new(RefCell::new(S::S({ 2 })));
79+
let s: Value<S> = Rc::new(RefCell::new(S::new({ 2 })));
8080
assert!((({ sum_0(s.as_pointer(),) }) == 7));
8181
assert!((({ shuffle_1(3,) }) == 10));
8282
return 0;
8383
}
8484
pub fn shuffle_1(x: i32) -> i32 {
8585
let x: Value<i32> = Rc::new(RefCell::new(x));
86-
let a: Value<S> = Rc::new(RefCell::new(S::S({ (*x.borrow()) })));
87-
let b: Value<S> = Rc::new(RefCell::new(S::move_from({ a.as_pointer() })));
86+
let a: Value<S> = Rc::new(RefCell::new(S::new({ (*x.borrow()) })));
87+
let b: Value<S> = Rc::new(RefCell::new(S::S_pmutS_rv({ a.as_pointer() })));
8888
assert!((*(*a.borrow()).v.borrow()).is_empty());
89-
let c: Value<S> = Rc::new(RefCell::new(S::S({ 1 })));
90-
({ SImpl::move_assign(&c.as_pointer(), b.as_pointer()) });
89+
let c: Value<S> = Rc::new(RefCell::new(S::new({ 1 })));
90+
({ SImpl::operator_assign_pmutS_rv(&c.as_pointer(), b.as_pointer()) });
9191
assert!((*(*b.borrow()).v.borrow()).is_empty());
9292
return ({ sum_0(c.as_pointer()) });
9393
}
9494
pub trait SImpl {
95-
fn move_assign(&self, _a0: Ptr<S>) -> Ptr<S>;
95+
fn operator_assign_pmutS_rv(&self, _a0: Ptr<S>) -> Ptr<S>;
9696
}
9797
impl SImpl for Ptr<S> {
98-
fn move_assign(&self, _a0: Ptr<S>) -> Ptr<S> {
98+
fn operator_assign_pmutS_rv(&self, _a0: Ptr<S>) -> Ptr<S> {
9999
((*(*self).upgrade().deref()).v.as_pointer() as Ptr<Vec<i32>>).write(std::mem::take(
100100
&mut (*(*_a0.upgrade().deref()).v.borrow_mut()),
101101
));

‎tests/multi-file/defaulted_move_cross_tu/out/unsafe/defaulted_move_cross_tu.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ pub struct S {
1313
pub n: [i32; 2],
1414
}
1515
impl S {
16-
pub unsafe fn S(mut x: i32) -> Self {
16+
pub unsafe fn new(mut x: i32) -> Self {
1717
let mut this = Self {
1818
v: vec![x; (x as usize) as usize],
1919
n: [x, ((x) + (1))],
2020
};
2121
this
2222
}
23-
pub unsafe fn move_from(_a0: *mut S) -> Self {
23+
pub unsafe fn S_pmutS_rv(_a0: *mut S) -> Self {
2424
let mut this = Self {
2525
v: std::mem::take(&mut (*_a0).v),
2626
n: std::array::from_fn::<_, 2, _>(|__i: usize| (*_a0).n[(__i)]),
2727
};
2828
this
2929
}
30-
pub unsafe fn move_assign(&mut self, _a0: *mut S) -> *mut S {
30+
pub unsafe fn operator_assign_pmutS_rv(&mut self, _a0: *mut S) -> *mut S {
3131
self.v = std::mem::take(&mut (*_a0).v);
3232
{
3333
if 8_usize != 0 {
@@ -60,17 +60,17 @@ pub fn main() {
6060
}
6161
}
6262
unsafe fn main_0() -> i32 {
63-
let mut s: S = S::S({ 2 });
63+
let mut s: S = S::new({ 2 });
6464
assert!(((unsafe { sum_0(&s,) }) == (7)));
6565
assert!(((unsafe { shuffle_1(3,) }) == (10)));
6666
return 0;
6767
}
6868
pub unsafe fn shuffle_1(mut x: i32) -> i32 {
69-
let mut a: S = S::S({ x });
70-
let mut b: S = S::move_from({ &mut a });
69+
let mut a: S = S::new({ x });
70+
let mut b: S = S::S_pmutS_rv({ &mut a });
7171
assert!(a.v.is_empty());
72-
let mut c: S = S::S({ 1 });
73-
(unsafe { S::move_assign(&mut c, &mut b) });
72+
let mut c: S = S::new({ 1 });
73+
(unsafe { S::operator_assign_pmutS_rv(&mut c, &mut b) });
7474
assert!(b.v.is_empty());
7575
return (unsafe { sum_0(&c) });
7676
}

‎tests/multi-file/out_of_line_methods_cross_tu/out/refcount/out_of_line_methods_cross_tu.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ pub fn main() {
6868
std::process::exit(main_0());
6969
}
7070
fn main_0() -> i32 {
71-
let s: Value<S> = Rc::new(RefCell::new(S::S({ 1 })));
71+
let s: Value<S> = Rc::new(RefCell::new(S::new({ 1 })));
7272
assert!((({ SImpl::get(&s.as_pointer(),) }) == 1));
7373
({ SImpl::set(&s.as_pointer(), 4) });
7474
assert!((({ SImpl::get(&s.as_pointer(),) }) == 4));
7575
assert!((({ SImpl::add(&s.as_pointer(), 2,) }) == 6));
76-
let derived: Value<Derived> = Rc::new(RefCell::new(Derived::Derived({ 3 })));
76+
let derived: Value<Derived> = Rc::new(RefCell::new(Derived::new({ 3 })));
7777
let base: Value<PtrDyn<dyn Base>> = Rc::new(RefCell::new(
7878
(derived.as_pointer()).to_dyn::<dyn Base>(|w| w),
7979
));
8080
assert!((({ (*(*base.borrow()).upgrade().deref()).apply(5,) }) == 15));
8181
return 0;
8282
}
8383
impl S {
84-
pub fn S(x: i32) -> Self {
84+
pub fn new(x: i32) -> Self {
8585
let x: Value<i32> = Rc::new(RefCell::new(x));
8686
let __this: Value<S> = Rc::new(RefCell::new(Self {
8787
v: Rc::new(RefCell::new((*x.borrow()))),
@@ -91,7 +91,7 @@ impl S {
9191
}
9292
}
9393
impl Derived {
94-
pub fn Derived(factor: i32) -> Self {
94+
pub fn new(factor: i32) -> Self {
9595
let factor: Value<i32> = Rc::new(RefCell::new(factor));
9696
let __this: Value<Derived> = Rc::new(RefCell::new(Self {
9797
factor: Rc::new(RefCell::new((*factor.borrow()))),

0 commit comments

Comments
 (0)