@@ -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
726724bool 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) {
15681566bool 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
16801678bool 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
63966401void Converter::ConvertConstructedValue (clang::QualType type,
0 commit comments