Skip to content

Commit 4e99d36

Browse files
committed
code simplifications
1 parent 7e86e69 commit 4e99d36

9 files changed

Lines changed: 261 additions & 411 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -432,14 +432,12 @@ bool Converter::VisitFunctionDecl(clang::FunctionDecl *decl) {
432432
ScopedValue push_fn(curr_function_, decl);
433433
std::string function_name;
434434
if (decl->isMain()) {
435+
// main_0 is private.
436+
ConvertFunctionMain(decl);
435437
function_name = "main_0";
436-
ConvertFunctionMain(decl, function_name);
437438
} else {
438-
function_name = GetNamedDeclAsString(decl->getCanonicalDecl());
439-
}
440-
// main_0 should be static
441-
if (!decl->isMain()) {
442439
StrCat(AccessSpecifierAsString(decl->getAccess()));
440+
function_name = GetNamedDeclAsString(decl->getCanonicalDecl());
443441
}
444442
StrCat(keyword_unsafe_, keyword::kFn, std::move(function_name));
445443
{
@@ -724,7 +722,7 @@ bool Converter::VisitVarDecl(clang::VarDecl *decl) {
724722
}
725723

726724
bool Converter::NeedsScopedDestructor(const clang::VarDecl *decl) const {
727-
if (in_function_formals_ || !decl->isLocalVarDecl() || IsGlobalVar(decl)) {
725+
if (!decl->isLocalVarDecl() || IsGlobalVar(decl)) {
728726
return false;
729727
}
730728
return VarTypeNeedsDestructor(decl->getType());
@@ -1568,7 +1566,7 @@ bool Converter::VisitAttributedStmt(clang::AttributedStmt *stmt) {
15681566
bool Converter::VisitWhileStmt(clang::WhileStmt *stmt) {
15691567
ScopedPush push(break_target_, BreakTarget::Loop);
15701568
StrCat("'loop_:");
1571-
ScopedPush no_inc(curr_for_inc_, nullptr);
1569+
ScopedValue no_inc(curr_for_inc_, nullptr);
15721570
if (auto *cond_var = stmt->getConditionVariableDeclStmt()) {
15731571
StrCat(keyword::kLoop);
15741572
PushBrace loop_brace(*this);
@@ -1607,7 +1605,7 @@ bool Converter::VisitDoStmt(clang::DoStmt *stmt) {
16071605
{
16081606
PushBrace loop_brace(*this);
16091607
StrCat(control_var, token::kAssign, keyword::kFalse, token::kSemiColon);
1610-
ScopedPush no_inc(curr_for_inc_, nullptr);
1608+
ScopedValue no_inc(curr_for_inc_, nullptr);
16111609
ConvertBodyStmts(stmt->getBody());
16121610
}
16131611
return false;
@@ -1637,7 +1635,7 @@ bool Converter::VisitForStmt(clang::ForStmt *stmt) {
16371635
ConvertConditionVariableTest(cond_var, stmt->getCond());
16381636
}
16391637
{
1640-
ScopedPush inc(curr_for_inc_, stmt->getInc());
1638+
ScopedValue inc(curr_for_inc_, stmt->getInc());
16411639
ConvertBodyStmts(stmt->getBody());
16421640
}
16431641
Convert(stmt->getInc());
@@ -1662,7 +1660,7 @@ bool Converter::VisitCXXForRangeStmt(clang::CXXForRangeStmt *stmt) {
16621660
ConvertFreshRValue(end->getInit()), ')');
16631661
{
16641662
PushBrace brace(*this);
1665-
ScopedPush no_inc(curr_for_inc_, nullptr);
1663+
ScopedValue no_inc(curr_for_inc_, nullptr);
16661664
Convert(stmt->getLoopVarStmt());
16671665
ConvertBodyStmts(stmt->getBody());
16681666
}
@@ -1678,8 +1676,8 @@ bool Converter::VisitBreakStmt([[maybe_unused]] clang::BreakStmt *stmt) {
16781676
}
16791677

16801678
bool Converter::VisitContinueStmt([[maybe_unused]] clang::ContinueStmt *stmt) {
1681-
if (!curr_for_inc_.empty()) {
1682-
Convert(curr_for_inc_.back());
1679+
if (curr_for_inc_) {
1680+
Convert(curr_for_inc_);
16831681
StrCat(token::kSemiColon);
16841682
}
16851683
StrCat(keyword::kContinue);
@@ -2800,6 +2798,7 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
28002798
ConvertIntegralToBooleanCast(expr);
28012799
break;
28022800
case clang::CastKind::CK_PointerToBoolean:
2801+
case clang::CastKind::CK_MemberPointerToBoolean:
28032802
StrCat(token::kNot);
28042803
ConvertEqualsNullPtr(sub_expr);
28052804
break;
@@ -2809,10 +2808,6 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
28092808
case clang::CastKind::CK_NullToMemberPointer:
28102809
EmitFresh(GetUnboxedDefaultAsString(type));
28112810
break;
2812-
case clang::CastKind::CK_MemberPointerToBoolean:
2813-
StrCat(token::kNot);
2814-
ConvertEqualsNullPtr(sub_expr);
2815-
break;
28162811
case clang::CastKind::CK_BaseToDerivedMemberPointer:
28172812
case clang::CastKind::CK_DerivedToBaseMemberPointer:
28182813
llvm::report_fatal_error(
@@ -4946,25 +4941,22 @@ void Converter::ConvertFunctionReturnType(clang::FunctionDecl *decl) {
49464941
}
49474942
}
49484943

4949-
void Converter::ConvertFunctionMain(const clang::FunctionDecl *decl,
4950-
const std::string_view main_function_name) {
4944+
void Converter::ConvertFunctionMain(const clang::FunctionDecl *decl) {
49514945
if (decl->getNumParams() != 0U) {
4952-
StrCat(std::format(R"(
4953-
pub fn main() {{
4946+
StrCat(R"(
4947+
pub fn main() {
49544948
let mut args: Vec<Vec<u8>> = std::env::args().map(|arg| arg.as_bytes().to_vec()).collect();
49554949
args.iter_mut().for_each(|v| v.push(0));
49564950
let mut argv: Vec<*mut libc::c_char> = args.iter().map(|arg| arg.as_ptr() as *mut libc::c_char).collect();
49574951
argv.push(::std::ptr::null_mut());
4958-
unsafe {{
4952+
unsafe {
49594953
__cpp2rust_init_globals();
49604954
libcc2rs::cpp2rust_exit(main_0((argv.len() - 1) as i32, argv.as_mut_ptr()) as i32)
4961-
}}
4962-
}})",
4963-
main_function_name));
4955+
}
4956+
})");
49644957
} else {
4965-
StrCat(std::format("pub fn main() {{ unsafe {{ __cpp2rust_init_globals(); "
4966-
"libcc2rs::cpp2rust_exit({}() as i32); }} }}",
4967-
main_function_name));
4958+
StrCat("pub fn main() { unsafe { __cpp2rust_init_globals(); "
4959+
"libcc2rs::cpp2rust_exit(main_0() as i32); } }");
49684960
}
49694961
}
49704962

@@ -6387,10 +6379,23 @@ void Converter::SetValueFreshness(clang::QualType type) {
63876379
}
63886380
}
63896381

6390-
void Converter::dump_expr_kinds() {
6391-
log() << "isRValue: " << isRValue() << ", isXValue: " << isXValue()
6392-
<< ", isAddrOf: " << isAddrOf() << ", isObject: " << isObject()
6393-
<< ", isVoid: " << isVoid() << '\n';
6382+
Converter::PushExprKind::PushExprKind(Converter &c, ExprKind k, bool enabled,
6383+
const char *file, int line)
6384+
: c(c), pushed(enabled) {
6385+
if (pushed) {
6386+
c.curr_expr_kind_.push_back(k);
6387+
}
6388+
6389+
// In the order of ExprKind.
6390+
static constexpr const char *kNames[] = {
6391+
"Callee", "LValue", "RValue", "FreshRValue",
6392+
"XValue", "AddrOf", "Object", "Void",
6393+
};
6394+
log() << "PushExprKind " << file << ':' << line << " [";
6395+
for (auto kind : c.curr_expr_kind_) {
6396+
log() << kNames[static_cast<unsigned>(kind)] << ", ";
6397+
}
6398+
log() << "]\n";
63946399
}
63956400

63966401
void Converter::ConvertConstructedValue(clang::QualType type,

‎cpp2rust/converter/converter.h‎

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -804,8 +804,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
804804

805805
virtual void ConvertFunctionReturnType(clang::FunctionDecl *decl);
806806

807-
virtual void ConvertFunctionMain(const clang::FunctionDecl *decl,
808-
const std::string_view main_function_name);
807+
virtual void ConvertFunctionMain(const clang::FunctionDecl *decl);
809808

810809
virtual void ConvertAbstractClass(clang::CXXRecordDecl *decl);
811810

@@ -1058,7 +1057,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
10581057
bool constant_evaluated_ = false;
10591058
std::optional<bool> autoref_mut_;
10601059

1061-
std::vector<clang::Expr *> curr_for_inc_;
1060+
// The increment of the innermost loop, which continue runs.
1061+
clang::Expr *curr_for_inc_ = nullptr;
10621062
// The names of the iterators of range-based for loops.
10631063
std::unordered_map<const clang::ValueDecl *, std::string>
10641064
range_iterator_names_;
@@ -1190,29 +1190,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
11901190
Void,
11911191
};
11921192

1193-
static const char *expr_kind_to_string(ExprKind kind) {
1194-
switch (kind) {
1195-
case ExprKind::Callee:
1196-
return "Callee";
1197-
case ExprKind::LValue:
1198-
return "LValue";
1199-
case ExprKind::RValue:
1200-
return "RValue";
1201-
case ExprKind::FreshRValue:
1202-
return "FreshRValue";
1203-
case ExprKind::XValue:
1204-
return "XValue";
1205-
case ExprKind::AddrOf:
1206-
return "AddrOf";
1207-
case ExprKind::Object:
1208-
return "Object";
1209-
case ExprKind::Void:
1210-
return "Void";
1211-
default:
1212-
return "Unknown";
1213-
}
1214-
}
1215-
12161193
bool isLValue() const;
12171194
bool isRValue() const;
12181195
bool isFreshRValue() const;
@@ -1222,26 +1199,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
12221199
bool isVoid() const;
12231200
bool isCallee() const;
12241201

1225-
void dump_expr_kinds();
1226-
12271202
struct PushExprKind {
12281203
Converter &c;
12291204
bool pushed;
12301205
PushExprKind(Converter &c, ExprKind k, bool enabled = true,
12311206
const char *file = __builtin_FILE(),
1232-
int line = __builtin_LINE())
1233-
: c(c), pushed(enabled) {
1234-
if (pushed) {
1235-
c.curr_expr_kind_.push_back(k);
1236-
}
1237-
log() << "PushExprKind " << file << ':' << line << ' ';
1238-
c.dump_expr_kinds();
1239-
log() << '[';
1240-
for (const auto k : c.curr_expr_kind_) {
1241-
log() << c.expr_kind_to_string(k) << ", ";
1242-
}
1243-
log() << "]\n";
1244-
}
1207+
int line = __builtin_LINE());
12451208
~PushExprKind() {
12461209
if (pushed) {
12471210
c.curr_expr_kind_.pop_back();

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2794,36 +2794,32 @@ std::string ConverterRefCount::ForceGlobalInit(const clang::VarDecl *decl) {
27942794
return std::format("let _ = {}.with(|_| ());", GetNamedDeclAsString(decl));
27952795
}
27962796

2797-
void ConverterRefCount::ConvertFunctionMain(
2798-
const clang::FunctionDecl *decl,
2799-
const std::string_view main_function_name) {
2797+
void ConverterRefCount::ConvertFunctionMain(const clang::FunctionDecl *decl) {
28002798
if (decl->getNumParams() != 0U) {
2801-
StrCat(std::format(R"(
2802-
pub fn main() {{
2799+
StrCat(R"(
2800+
pub fn main() {
28032801
let size: usize = ::std::env::args().map(|arg| arg.len() + 1).sum();
28042802
let args: Value<Box<[i8]>> = Rc::new(RefCell::new(vec![0; size].into_boxed_slice()));
28052803
let args_ptr: Ptr<i8> = args.decay();
28062804
let mut off = 0;
28072805
let argv: Value<Box<[Ptr<i8>]>> = Rc::new(RefCell::new(
28082806
::std::env::args()
2809-
.map(|arg| {{
2807+
.map(|arg| {
28102808
let ptr = args_ptr.offset(off);
28112809
ptr.write_c_bytes(arg.as_bytes());
28122810
off += arg.len() + 1;
28132811
ptr
2814-
}})
2812+
})
28152813
.chain(::std::iter::once(Ptr::null()))
28162814
.collect(),
28172815
));
28182816
__cpp2rust_init_globals();
2819-
libcc2rs::cpp2rust_exit({}(::std::env::args().len() as i32,
2817+
libcc2rs::cpp2rust_exit(main_0(::std::env::args().len() as i32,
28202818
argv.decay()));
2821-
}})",
2822-
main_function_name));
2819+
})");
28232820
} else {
2824-
StrCat(std::format("pub fn main() {{ __cpp2rust_init_globals(); "
2825-
"libcc2rs::cpp2rust_exit({}()); }}",
2826-
main_function_name));
2821+
StrCat("pub fn main() { __cpp2rust_init_globals(); "
2822+
"libcc2rs::cpp2rust_exit(main_0()); }");
28272823
}
28282824
}
28292825

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ class ConverterRefCount final : public Converter {
314314

315315
bool IsPointerSubscript(const clang::ArraySubscriptExpr *expr) const;
316316

317-
void ConvertFunctionMain(const clang::FunctionDecl *decl,
318-
const std::string_view main_function_name) override;
317+
void ConvertFunctionMain(const clang::FunctionDecl *decl) override;
319318

320319
void ConvertAddrOf(clang::Expr *expr, clang::QualType pointer_type) override;
321320

0 commit comments

Comments
 (0)