@@ -1558,48 +1558,57 @@ bool Converter::VisitContinueStmt([[maybe_unused]] clang::ContinueStmt *stmt) {
15581558bool Converter::Convert (clang::Expr *expr,
15591559 std::optional<clang::QualType> implicit_convert_to) {
15601560 if (expr && implicit_convert_to) {
1561- if (auto pointee =
1562- GetMismatchedExprPointee (ctx_, expr, *implicit_convert_to);
1563- !pointee.isNull ()) {
1564- StrCat (ConvertPointeeCast (ToString (expr), pointee, *implicit_convert_to));
1565- return true ;
1561+ auto to = *implicit_convert_to;
1562+ if (NeedsImplicitPointeeCast (ctx_, expr, to)) {
1563+ StrCat (ConvertPointeeCast (ToString (expr), expr, to));
1564+ return false ;
1565+ }
1566+ if (NeedsImplicitScalarCast (ctx_, expr->IgnoreImplicit ()->getType (), to)) {
1567+ StrCat (ConvertScalarCast (ToString (expr), to));
1568+ return false ;
15661569 }
15671570 }
1568- bool needs_conversion =
1569- expr && implicit_convert_to &&
1570- NeedsImplicitScalarCast (ctx_, expr->IgnoreImplicit ()->getType (),
1571- *implicit_convert_to);
1572- PushParen paren (*this , needs_conversion);
15731571 computed_expr_type_ = ComputedExprType::Unknown;
15741572 bool result = TraverseStmt (expr);
15751573 if (expr && computed_expr_type_ == ComputedExprType::Unknown) {
15761574 expr->dump ();
15771575 assert (false && " computed_expr_type_ not set" );
15781576 }
1579- if (needs_conversion) {
1580- ConvertCast (*implicit_convert_to);
1581- computed_expr_type_ = ComputedExprType::FreshValue;
1582- }
15831577 return result;
15841578}
15851579
1580+ std::string Converter::ConvertScalarCast (std::string str, clang::QualType to) {
1581+ Buffer buf (*this );
1582+ {
1583+ PushParen paren (*this );
1584+ StrCat (str);
1585+ ConvertCast (to);
1586+ }
1587+ computed_expr_type_ = ComputedExprType::FreshValue;
1588+ return std::move (buf).str ();
1589+ }
1590+
15861591std::string Converter::ConvertPointeeCast (std::string str,
1587- clang::QualType from,
1592+ const clang::Expr * from,
15881593 clang::QualType to) {
1594+ auto pointee = GetExprPointee (ctx_, from, to);
15891595 if (to->isFunctionPointerType ()) {
15901596 return std::format (" std::mem::transmute::<{}, {}>({})" ,
1591- ToString (ctx_.getPointerType (from )), ToString (to),
1597+ ToString (ctx_.getPointerType (pointee )), ToString (to),
15921598 str);
15931599 }
1600+ if (to->getPointeeType ().isConstQualified ()) {
1601+ pointee = pointee.withConst ();
1602+ }
15941603 auto target = GetUnsafeTypeAsString (to->getPointeeType ());
15951604 if (to->isReferenceType () && !isAddrOf ()) {
15961605 return std::format (" *(&raw {} {}).cast::<{}>()" ,
1597- from .isConstQualified () ? " const" : " mut" , str,
1606+ pointee .isConstQualified () ? " const" : " mut" , str,
15981607 target);
15991608 }
16001609 computed_expr_type_ = ComputedExprType::FreshPointer;
16011610 return std::format (" ({} as {}).cast::<{}>()" , str,
1602- GetUnsafeTypeAsString (ctx_.getPointerType (from )),
1611+ GetUnsafeTypeAsString (ctx_.getPointerType (pointee )),
16031612 target);
16041613}
16051614
0 commit comments