Skip to content

Commit 2d5b6f2

Browse files
committed
Merge branch 'master' into remove-emplace-back-plugin
2 parents 72ffa06 + 4e148ca commit 2d5b6f2

68 files changed

Lines changed: 546 additions & 502 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
@@ -1099,6 +1099,10 @@ std::string Converter::GetMethodName(const clang::CXXMethodDecl *decl) {
10991099
if (IsOverloadedMethod(decl)) {
11001100
return GetOverloadedFunctionName(decl);
11011101
}
1102+
if (auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(decl)) {
1103+
return GetConversionName(
1104+
conversion, GetUnsafeTypeAsString(conversion->getConversionType()));
1105+
}
11021106
return GetNamedDeclAsString(decl);
11031107
}
11041108

@@ -1143,10 +1147,9 @@ std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) {
11431147
if (decl->isCopyOrMoveConstructor()) {
11441148
return GetOverloadedFunctionName(decl);
11451149
}
1146-
return GetRecordName(decl->getParent()) +
1147-
(GetNumberOfConvertingCtors(decl->getParent()) != 1
1148-
? std::to_string(GetCtorIndex(decl))
1149-
: "");
1150+
return GetNumberOfConvertingCtors(decl->getParent()) != 1
1151+
? std::format("new_{}", GetCtorIndex(decl))
1152+
: "new";
11501153
}
11511154

11521155
bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) {
@@ -4068,6 +4071,10 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) {
40684071
std::string
40694072
Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
40704073
auto name = GetFunctionBaseName(decl);
4074+
if (auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(decl)) {
4075+
name = GetConversionName(
4076+
conversion, GetUnsafeTypeAsString(conversion->getConversionType()));
4077+
}
40714078
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(decl);
40724079
ctor && !ctor->getParent()->getIdentifier()) {
40734080
name = GetRecordName(ctor->getParent());
@@ -4137,19 +4144,7 @@ Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
41374144
}
41384145
}
41394146

4140-
ReplaceAll(name, "[", "arr");
4141-
ReplaceAll(name, "]", "arr");
4142-
ReplaceAll(name, ";", "_");
4143-
ReplaceAll(name, ",", "_");
4144-
name.erase(std::remove_if(name.begin(), name.end(),
4145-
[](char c) {
4146-
return c == '<' || c == '>' || c == ' ' ||
4147-
c == ':' || c == '(' || c == ')' ||
4148-
c == '-';
4149-
}),
4150-
name.end());
4151-
std::replace(name.begin(), name.end(), '*', 'p');
4152-
4147+
ToIdentifier(name);
41534148
return name;
41544149
}
41554150

‎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
@@ -944,6 +944,35 @@ std::string GetFunctionBaseName(const clang::FunctionDecl *decl) {
944944
return decl->getNameAsString();
945945
}
946946

947+
void ToIdentifier(std::string &name) {
948+
ReplaceAll(name, "[", "arr");
949+
ReplaceAll(name, "]", "arr");
950+
ReplaceAll(name, ";", "_");
951+
ReplaceAll(name, ",", "_");
952+
name.erase(std::remove_if(name.begin(), name.end(),
953+
[](char c) {
954+
return c == '<' || c == '>' || c == ' ' ||
955+
c == ':' || c == '(' || c == ')' ||
956+
c == '-';
957+
}),
958+
name.end());
959+
std::replace(name.begin(), name.end(), '*', 'p');
960+
}
961+
962+
std::string GetConversionName(const clang::CXXConversionDecl *decl,
963+
const std::string &rust_type) {
964+
auto name = "to_" + rust_type;
965+
ToIdentifier(name);
966+
const auto *record = decl->getParent();
967+
bool is_unique_name =
968+
std::none_of(record->method_begin(), record->method_end(),
969+
[&name](const clang::CXXMethodDecl *method) {
970+
return method->getDeclName().isIdentifier() &&
971+
method->getName() == name;
972+
});
973+
return is_unique_name ? name : GetFunctionBaseName(decl);
974+
}
975+
947976
clang::CXXDestructorDecl *
948977
GetUserDefinedDestructor(const clang::CXXRecordDecl *decl) {
949978
if (!decl->hasDefinition() || !IsUserDefinedDecl(decl) ||

‎cpp2rust/converter/converter_lib.h‎

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

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

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

‎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: 4 additions & 4 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![
@@ -76,17 +76,17 @@ 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()) })));
86+
let a: Value<S> = Rc::new(RefCell::new(S::new({ (*x.borrow()) })));
8787
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 })));
89+
let c: Value<S> = Rc::new(RefCell::new(S::new({ 1 })));
9090
({ 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()) });

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ 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))],
@@ -60,16 +60,16 @@ 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 });
69+
let mut a: S = S::new({ x });
7070
let mut b: S = S::S_pmutS_rv({ &mut a });
7171
assert!(a.v.is_empty());
72-
let mut c: S = S::S({ 1 });
72+
let mut c: S = S::new({ 1 });
7373
(unsafe { S::operator_assign_pmutS_rv(&mut c, &mut b) });
7474
assert!(b.v.is_empty());
7575
return (unsafe { sum_0(&c) });

‎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)