Skip to content

Commit 4e23c19

Browse files
authored
Generate to_bool instead of operator__Bool (#419)
1 parent 08d1bf2 commit 4e23c19

8 files changed

Lines changed: 82 additions & 41 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,10 @@ std::string Converter::GetMethodName(const clang::CXXMethodDecl *decl) {
10981098
if (IsOverloadedMethod(decl)) {
10991099
return GetOverloadedFunctionName(decl);
11001100
}
1101+
if (auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(decl)) {
1102+
return GetConversionName(
1103+
conversion, GetUnsafeTypeAsString(conversion->getConversionType()));
1104+
}
11011105
return GetNamedDeclAsString(decl);
11021106
}
11031107

@@ -4082,6 +4086,10 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) {
40824086
std::string
40834087
Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
40844088
auto name = GetFunctionBaseName(decl);
4089+
if (auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(decl)) {
4090+
name = GetConversionName(
4091+
conversion, GetUnsafeTypeAsString(conversion->getConversionType()));
4092+
}
40854093
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(decl);
40864094
ctor && !ctor->getParent()->getIdentifier()) {
40874095
name = GetRecordName(ctor->getParent());
@@ -4151,19 +4159,7 @@ Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
41514159
}
41524160
}
41534161

4154-
ReplaceAll(name, "[", "arr");
4155-
ReplaceAll(name, "]", "arr");
4156-
ReplaceAll(name, ";", "_");
4157-
ReplaceAll(name, ",", "_");
4158-
name.erase(std::remove_if(name.begin(), name.end(),
4159-
[](char c) {
4160-
return c == '<' || c == '>' || c == ' ' ||
4161-
c == ':' || c == '(' || c == ')' ||
4162-
c == '-';
4163-
}),
4164-
name.end());
4165-
std::replace(name.begin(), name.end(), '*', 'p');
4166-
4162+
ToIdentifier(name);
41674163
return name;
41684164
}
41694165

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

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

‎cpp2rust/converter/converter_lib.h‎

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

166166
std::string GetFunctionBaseName(const clang::FunctionDecl *decl);
167167

168+
void ToIdentifier(std::string &name);
169+
170+
std::string GetConversionName(const clang::CXXConversionDecl *decl,
171+
const std::string &rust_type);
172+
168173
bool IsImplicitAssignmentCall(const clang::CallExpr *expr);
169174
bool IsUserOperatorCall(const clang::CXXOperatorCallExpr *expr);
170175

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl ByteRepr for Flag {
5353
}
5454
pub fn use_4(f: Flag) -> i32 {
5555
let f: Value<Flag> = Rc::new(RefCell::new(f));
56-
assert!(({ FlagImpl::operator__Bool(&f.as_pointer(),) }));
56+
assert!(({ FlagImpl::to_bool(&f.as_pointer(),) }));
5757
return (*(*f.borrow()).v.borrow());
5858
}
5959
pub fn checked_5(x: i32) -> i32 {
@@ -127,10 +127,10 @@ fn main_0() -> i32 {
127127
return 0;
128128
}
129129
pub trait FlagImpl {
130-
fn operator__Bool(&self) -> bool;
130+
fn to_bool(&self) -> bool;
131131
}
132132
impl FlagImpl for Ptr<Flag> {
133-
fn operator__Bool(&self) -> bool {
133+
fn to_bool(&self) -> bool {
134134
return ((*(*(*self).upgrade().deref()).v.borrow()) != 0);
135135
}
136136
}

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,20 @@ fn main_0() -> i32 {
6161
.borrow())
6262
== 34)
6363
);
64-
let i: Value<i32> = Rc::new(RefCell::new(({ SImpl::operator_int(&s.as_pointer()) })));
64+
let i: Value<i32> = Rc::new(RefCell::new(({ SImpl::to_i32(&s.as_pointer()) })));
6565
assert!(((*i.borrow()) == 3));
66-
assert!(((({ SImpl::operator_int(&s.as_pointer(),) }) + 1) == 4));
67-
if ({ SImpl::operator__Bool(&s.as_pointer()) }) {
68-
assert!(({ SImpl::operator__Bool(&s.as_pointer(),) }));
66+
assert!(((({ SImpl::to_i32(&s.as_pointer(),) }) + 1) == 4));
67+
if ({ SImpl::to_bool(&s.as_pointer()) }) {
68+
assert!(({ SImpl::to_bool(&s.as_pointer(),) }));
6969
} else {
7070
assert!(false);
7171
}
7272
let z: Value<S> = Rc::new(RefCell::new(S {
7373
v: Rc::new(RefCell::new(0)),
7474
}));
75-
assert!(({ SImpl::operator__Bool(&s.as_pointer(),) }));
76-
assert!(!({ SImpl::operator__Bool(&z.as_pointer(),) }));
77-
assert!(
78-
({ SImpl::operator__Bool(&s.as_pointer(),) })
79-
&& (!({ SImpl::operator__Bool(&z.as_pointer(),) }))
80-
);
75+
assert!(({ SImpl::to_bool(&s.as_pointer(),) }));
76+
assert!(!({ SImpl::to_bool(&z.as_pointer(),) }));
77+
assert!(({ SImpl::to_bool(&s.as_pointer(),) }) && (!({ SImpl::to_bool(&z.as_pointer(),) })));
8178
let st: Value<Static> = Rc::new(RefCell::new(<Static>::default()));
8279
assert!((({ Static::operator_call(6, 7,) }) == 42));
8380
assert!(
@@ -109,8 +106,8 @@ pub trait SImpl {
109106
fn operator_call_i32_const(&self, a: i32) -> i32;
110107
fn operator_call_i32_i32_const(&self, a: i32, b: i32) -> i32;
111108
fn operator_comma(&self, o: Ptr<S>) -> S;
112-
fn operator_int(&self) -> i32;
113-
fn operator__Bool(&self) -> bool;
109+
fn to_i32(&self) -> i32;
110+
fn to_bool(&self) -> bool;
114111
}
115112
impl SImpl for Ptr<S> {
116113
fn operator_call_const(&self) -> i32 {
@@ -133,10 +130,10 @@ impl SImpl for Ptr<S> {
133130
})),
134131
};
135132
}
136-
fn operator_int(&self) -> i32 {
133+
fn to_i32(&self) -> i32 {
137134
return (*(*(*self).upgrade().deref()).v.borrow());
138135
}
139-
fn operator__Bool(&self) -> bool {
136+
fn to_bool(&self) -> bool {
140137
return ((*(*(*self).upgrade().deref()).v.borrow()) != 0);
141138
}
142139
}

‎tests/unit/out/unsafe/constexpr_function.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ pub struct Flag {
2727
pub v: i32,
2828
}
2929
impl Flag {
30-
pub unsafe fn operator__Bool(&self) -> bool {
30+
pub unsafe fn to_bool(&self) -> bool {
3131
return ((self.v) != (0));
3232
}
3333
}
3434
pub unsafe fn use_4(mut f: Flag) -> i32 {
35-
assert!((unsafe { Flag::operator__Bool(&f,) }));
35+
assert!((unsafe { Flag::to_bool(&f,) }));
3636
return f.v;
3737
}
3838
pub unsafe fn checked_5(mut x: i32) -> i32 {

‎tests/unit/out/unsafe/operator_other_member.rs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ impl S {
3434
v: (((self.v) * (10)) + ((*o).v)),
3535
};
3636
}
37-
pub unsafe fn operator_int(&self) -> i32 {
37+
pub unsafe fn to_i32(&self) -> i32 {
3838
return self.v;
3939
}
40-
pub unsafe fn operator__Bool(&self) -> bool {
40+
pub unsafe fn to_bool(&self) -> bool {
4141
return ((self.v) != (0));
4242
}
4343
}
@@ -54,18 +54,18 @@ unsafe fn main_0() -> i32 {
5454
assert!(((unsafe { S::operator_call_i32_const(&s, 1,) }) == (4)));
5555
assert!(((unsafe { S::operator_call_i32_i32_const(&s, 1, 2,) }) == (6)));
5656
assert!((((unsafe { S::operator_comma(&s, &t,) }).v) == (34)));
57-
let mut i: i32 = (unsafe { S::operator_int(&s) });
57+
let mut i: i32 = (unsafe { S::to_i32(&s) });
5858
assert!(((i) == (3)));
59-
assert!((((unsafe { S::operator_int(&s,) }) + (1)) == (4)));
60-
if (unsafe { S::operator__Bool(&s) }) {
61-
assert!((unsafe { S::operator__Bool(&s,) }));
59+
assert!((((unsafe { S::to_i32(&s,) }) + (1)) == (4)));
60+
if (unsafe { S::to_bool(&s) }) {
61+
assert!((unsafe { S::to_bool(&s,) }));
6262
} else {
6363
assert!(false);
6464
}
6565
let mut z: S = S { v: 0 };
66-
assert!((unsafe { S::operator__Bool(&s,) }));
67-
assert!(!(unsafe { S::operator__Bool(&z,) }));
68-
assert!((unsafe { S::operator__Bool(&s,) }) && (!(unsafe { S::operator__Bool(&z,) })));
66+
assert!((unsafe { S::to_bool(&s,) }));
67+
assert!(!(unsafe { S::to_bool(&z,) }));
68+
assert!((unsafe { S::to_bool(&s,) }) && (!(unsafe { S::to_bool(&z,) })));
6969
let mut st: Static = <Static>::default();
7070
assert!(((unsafe { Static::operator_call(6, 7,) }) == (42)));
7171
assert!(((unsafe { S::operator_call_const(&S { v: 5 },) }) == (5)));

0 commit comments

Comments
 (0)