@@ -165,7 +165,7 @@ bool ConverterRefCount::NeedsMut(const clang::VarDecl *decl,
165165
166166bool ConverterRefCount::IsUnboxedVar (const clang::ValueDecl *decl) const {
167167 auto *var = clang::dyn_cast<clang::VarDecl>(decl);
168- return var && (bound_values_. contains (var) || !boxed_vars_->contains (var));
168+ return var && (GetBoundVar (var). by_value || !boxed_vars_->contains (var));
169169}
170170
171171bool ConverterRefCount::IsUnboxedPlace (const clang::Expr *expr) const {
@@ -205,6 +205,41 @@ ConverterRefCount::VarConversionKind(const clang::VarDecl *decl) const {
205205 : ConversionKind::FullRefCount;
206206}
207207
208+ // In the order of ConversionKind.
209+ static constexpr const char *kConversionKindNames [] = {
210+ " Unboxed" ,
211+ " Pointee" ,
212+ " Ptr" ,
213+ " FullRefCount" ,
214+ };
215+
216+ ConverterRefCount::PushConversionKind::PushConversionKind (ConverterRefCount &c,
217+ ConversionKind k,
218+ bool cond, int line)
219+ : c(c), pushed(cond) {
220+ if (pushed) {
221+ c.conversion_kind_ .push_back (k);
222+ }
223+
224+ log () << " [PushConversionKind:" << line << " ] " ;
225+ for (auto kind : c.conversion_kind_ ) {
226+ log () << kConversionKindNames [static_cast <unsigned >(kind)] << " , " ;
227+ }
228+ log () << ' \n ' ;
229+ }
230+
231+ ConverterRefCount::PushConversionKind::~PushConversionKind () {
232+ if (pushed) {
233+ c.conversion_kind_ .pop_back ();
234+ }
235+
236+ log () << " [PopConversionKind] " ;
237+ for (auto kind : c.conversion_kind_ ) {
238+ log () << kConversionKindNames [static_cast <unsigned >(kind)] << " , " ;
239+ }
240+ log () << ' \n ' ;
241+ }
242+
208243std::string ConverterRefCount::BoxType (std::string &&str) const {
209244 if (getConversionKind () != ConversionKind::FullRefCount) {
210245 return std::move (str);
@@ -1041,7 +1076,7 @@ void ConverterRefCount::ConvertDeclRefValue(clang::Expr *expr,
10411076 // references are not boxed
10421077 if (isAddrOf ()) {
10431078 StrCat (str);
1044- if (moved_values_. contains (clang::dyn_cast<clang::VarDecl>(decl))) {
1079+ if (GetBoundVar (clang::dyn_cast<clang::VarDecl>(decl)). moved ) {
10451080 MarkFresh ();
10461081 } else {
10471082 MarkNotFresh ();
@@ -1063,7 +1098,7 @@ void ConverterRefCount::ConvertDeclRefValue(clang::Expr *expr,
10631098 StrCat (str);
10641099 if (isRValue () &&
10651100 (moved_reads_.contains (clang::dyn_cast<clang::DeclRefExpr>(expr)) ||
1066- moved_values_. contains (clang::dyn_cast<clang::VarDecl>(decl)))) {
1101+ GetBoundVar (clang::dyn_cast<clang::VarDecl>(decl)). moved )) {
10671102 MarkFresh ();
10681103 } else {
10691104 SetValueFreshness (expr->getType ());
@@ -1236,7 +1271,8 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
12361271 return false ;
12371272 }
12381273
1239- if (expr->getCastKind () == clang::CastKind::CK_BitCast) {
1274+ switch (expr->getCastKind ()) {
1275+ case clang::CastKind::CK_BitCast:
12401276 if (expr->getType ()->isVoidPointerType ()) {
12411277 if (sub_expr->getType ()->isVoidPointerType ()) {
12421278 return Convert (sub_expr);
@@ -1258,47 +1294,45 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
12581294 Convert (sub_expr);
12591295 }
12601296 return false ;
1261- }
1262-
1263- if (expr->getCastKind () == clang::CastKind::CK_DerivedToBase) {
1264- if (expr->getType ()->isPointerType ()) {
1265- auto ptype = clang::dyn_cast<clang::PointerType>(expr->getType ());
1266- auto pointee_type = ptype->getPointeeType ()->getAsCXXRecordDecl ();
12671297
1268- if (pointee_type && IsAbstractUserClass (pointee_type)) {
1269- PushConversionKind push (*this , ConversionKind::Unboxed);
1270- EmitFresh (std::format (" {}.to_dyn::<{}>(|w| w)" ,
1271- ToString (sub_expr->IgnoreCasts ()),
1272- ConvertPointeeType (expr->getType ())));
1273- return false ;
1274- }
1298+ case clang::CastKind::CK_DerivedToBase:
1299+ if (auto *record = expr->getType ()->getPointeeCXXRecordDecl ();
1300+ record && IsAbstractUserClass (record)) {
1301+ PushConversionKind push (*this , ConversionKind::Unboxed);
1302+ EmitFresh (std::format (" {}.to_dyn::<{}>(|w| w)" ,
1303+ ToString (sub_expr->IgnoreCasts ()),
1304+ ConvertPointeeType (expr->getType ())));
1305+ return false ;
12751306 }
1276- }
1307+ break ;
12771308
1278- if (expr-> getCastKind () == clang::CastKind::CK_ArrayToPointerDecay) {
1309+ case clang::CastKind::CK_ArrayToPointerDecay:
12791310 if (IsVaListType (sub_expr->getType ())) {
12801311 Convert (sub_expr);
12811312 return false ;
12821313 }
12831314 if (IsStringLiteralExpr (sub_expr)) {
12841315 EmitFresh (ConvertStringLiteralPtr (sub_expr));
12851316 return false ;
1286- } else {
1317+ }
1318+ {
12871319 PushConversionKind push (*this , ConversionKind::Unboxed);
12881320 StrCat (ConvertObject (sub_expr, ObjectShape::Element));
1289- return false ;
12901321 }
1291- }
1322+ return false ;
12921323
1293- if (expr-> getCastKind () == clang::CastKind::CK_NullToPointer) {
1324+ case clang::CastKind::CK_NullToPointer: {
12941325 PushConversionKind push (*this , ConversionKind::Unboxed);
12951326 EmitFresh (GetDefaultAsString (expr->getType ()));
12961327 return false ;
12971328 }
12981329
1299- if (expr-> getCastKind () == clang::CastKind::CK_NoOp) {
1330+ case clang::CastKind::CK_NoOp:
13001331 Convert (sub_expr);
13011332 return false ;
1333+
1334+ default :
1335+ break ;
13021336 }
13031337
13041338 return Converter::VisitImplicitCastExpr (expr);
@@ -1402,24 +1436,28 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
14021436 if (expr->getTypeAsWritten ()->isVoidType ()) {
14031437 return Converter::VisitExplicitCastExpr (expr);
14041438 }
1439+
14051440 if (expr->getCastKind () == clang::CK_NullToPointer) {
14061441 PushConversionKind push (*this , ConversionKind::Unboxed);
14071442 EmitFresh (GetDefaultAsString (expr->getType ()));
14081443 return false ;
14091444 }
1445+
14101446 if (auto type = expr->getTypeAsWritten ();
14111447 type->isReferenceType () &&
14121448 ctx_.hasSameUnqualifiedType (type.getNonReferenceType (),
14131449 expr->getSubExpr ()->getType ())) {
14141450 return Convert (expr->getSubExpr ());
14151451 }
1452+
14161453 if (expr->getCastKind () == clang::CK_PointerToIntegral ||
14171454 expr->getCastKind () == clang::CK_IntegralToPointer) {
14181455 std::string dst_type;
14191456 {
14201457 PushConversionKind push (*this , ConversionKind::Unboxed);
14211458 dst_type = ToString (expr->getType ());
14221459 }
1460+
14231461 // The integers of pointers are usize, which other integers are cast
14241462 // from and to.
14251463 auto sub_expr = ToString (expr->getSubExpr ());
@@ -1445,9 +1483,11 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
14451483 MarkFresh ();
14461484 return false ;
14471485 }
1486+
14481487 if (expr->getCastKind () == clang::CK_ReinterpretMemberPointer) {
14491488 return Converter::VisitExplicitCastExpr (expr);
14501489 }
1490+
14511491 switch (expr->getStmtClass ()) {
14521492 case clang::Stmt::CXXReinterpretCastExprClass:
14531493 assert (expr->getType ()->isPointerType () &&
@@ -1496,6 +1536,7 @@ bool ConverterRefCount::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
14961536 MarkFresh ();
14971537 return false ;
14981538 }
1539+
14991540 EmitFresh (std::format (" {}.reinterpret_cast::<{}>()" , ToString (sub_expr),
15001541 ConvertPointeeType (expr->getType ())));
15011542 return false ;
@@ -1582,7 +1623,7 @@ void ConverterRefCount::ConvertBinaryOperator(clang::BinaryOperator *expr) {
15821623 rhs_type->isPointerType ()) {
15831624 {
15841625 PushParen paren (*this );
1585- StrCat (ConvertFreshPointer (lhs), expr-> getOpcodeStr () ,
1626+ StrCat (ConvertFreshPointer (lhs), opcode_as_string ,
15861627 ConvertFreshPointer (rhs));
15871628 }
15881629 ConvertCast (expr->getType ());
@@ -1682,11 +1723,13 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) {
16821723 ConvertDeclRefValue (expr, member);
16831724 return false ;
16841725 }
1726+
16851727 if (auto field = clang::dyn_cast<clang::FieldDecl>(member);
16861728 field && AsLambdaCapture (field)) {
16871729 ConvertDeclRefValue (expr, member);
16881730 return false ;
16891731 }
1732+
16901733 bool known = Mapper::Contains (ctx_, expr);
16911734
16921735 if (known && RuleRegistry::ReturnsPointer (ctx_, expr)) {
@@ -2063,8 +2106,7 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
20632106
20642107 if (ctor->isDefaultConstructor () &&
20652108 !(ctor->isUserProvided () && IsUserDefinedDecl (ctor))) {
2066- auto ty = expr->getType ();
2067- EmitFresh (GetDefaultAsString (ty));
2109+ EmitFresh (GetDefaultAsString (expr->getType ()));
20682110 return false ;
20692111 }
20702112
@@ -2074,7 +2116,6 @@ bool ConverterRefCount::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
20742116 ConvertCXXConstructExprArgs (expr);
20752117 }
20762118 MarkFresh ();
2077-
20782119 return false ;
20792120}
20802121
@@ -2512,19 +2553,18 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
25122553 break ;
25132554
25142555 case clang::OverloadedOperatorKind::OO_Subscript: {
2515- bool is_inner_boxed =
2516- IsBoxedType (expr->getType ().getNonReferenceType ()) &&
2517- IsBoxedType (expr->getArg (0 )->getType ().getNonReferenceType ());
2556+ auto *base = expr->getArg (0 );
2557+ auto *idx = expr->getArg (1 );
2558+ bool is_inner_boxed = IsBoxedType (expr->getType ().getNonReferenceType ()) &&
2559+ IsBoxedType (base->getType ().getNonReferenceType ());
25182560
25192561 // An element of an unboxed container is accessed directly, as is one of
25202562 // a container stored in a Value that can be borrowed.
2521- auto *idx = expr->getArg (1 );
2522- bool is_unboxed = IsUnboxedPlace (expr->getArg (0 ));
2563+ bool is_unboxed = IsUnboxedPlace (base);
25232564 if ((isLValue () || isRValue ()) && !is_inner_boxed &&
25242565 (is_unboxed || IsElemOfBorrowedValue (expr))) {
25252566 // The index is a size_t, i.e., a usize.
2526- auto str =
2527- std::format (" {}[{}]" , ToString (expr->getArg (0 )), ConvertRValue (idx));
2567+ auto str = std::format (" {}[{}]" , ToString (base), ConvertRValue (idx));
25282568 // A copied element is read in a block, which ends the borrow.
25292569 if (!is_unboxed && isRValue () && TypeIsCopyable (expr->getType ())) {
25302570 EmitFresh (std::format (" {{ {} }}" , str));
@@ -2537,7 +2577,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
25372577
25382578 if (isLValue ()) {
25392579 PushConversionKind push_ck (*this , ConversionKind::Unboxed);
2540- pending_deref_.set (ConvertElemPlace (expr-> getArg ( 0 ), expr-> getArg ( 1 ) ),
2580+ pending_deref_.set (ConvertElemPlace (base, idx ),
25412581 /* fresh=*/ true );
25422582 break ;
25432583 }
@@ -2547,8 +2587,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
25472587 if (expr->getType ()->isRecordType () && isRValue () && !is_inner_boxed &&
25482588 (record_ptr_ || expr == copied_expr_)) {
25492589 PushConversionKind push (*this , ConversionKind::Unboxed);
2550- EmitRecordElemRead (ConvertElemPlace (expr->getArg (0 ), expr->getArg (1 )),
2551- expr->getType ());
2590+ EmitRecordElemRead (ConvertElemPlace (base, idx), expr->getType ());
25522591 break ;
25532592 }
25542593
@@ -2557,22 +2596,24 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
25572596 PushDeref push_deref (*this , expr->getType (), deref);
25582597 PushConversionKind push (*this , ConversionKind::Unboxed);
25592598 if (deref && !is_inner_boxed) {
2560- StrCat (ConvertElemPlace (expr-> getArg ( 0 ), expr-> getArg ( 1 ) ));
2599+ StrCat (ConvertElemPlace (base, idx ));
25612600 } else {
25622601 StrCat (std::format (" {}.offset({})" ,
2563- ConvertObject (expr-> getArg ( 0 ) , ObjectShape::Element),
2564- ConvertSubscriptIndex (expr-> getArg ( 1 ) )));
2602+ ConvertObject (base , ObjectShape::Element),
2603+ ConvertSubscriptIndex (idx )));
25652604 }
25662605 if (!deref) {
25672606 MarkFresh ();
25682607 }
25692608 }
25702609 break ;
25712610 }
2611+
25722612 case clang::OverloadedOperatorKind::OO_Call: {
25732613 PushConversionKind push (*this , ConversionKind::Unboxed);
25742614 return Converter::ConvertCXXOperatorCallExpr (expr);
25752615 }
2616+
25762617 default :
25772618 return Converter::ConvertCXXOperatorCallExpr (expr);
25782619 }
@@ -2622,6 +2663,7 @@ std::string ConverterRefCount::ConvertElemPlace(clang::Expr *container,
26222663 return std::format (" vec_elem!({}, {}, {})" , ptr,
26232664 GetNamedDeclAsString (field), ConvertSubscriptIndex (idx));
26242665 }
2666+
26252667 // An element of a vector reached through a pointer, e.g., a reference, is
26262668 // accessed in place, through the pointer.
26272669 auto *deref = clang::dyn_cast<clang::UnaryOperator>(
@@ -2635,6 +2677,7 @@ std::string ConverterRefCount::ConvertElemPlace(clang::Expr *container,
26352677 return std::format (" index!({}, {})" , place, ConvertSubscriptIndex (idx));
26362678 }
26372679 }
2680+
26382681 return std::format (" elem!({}, {})" ,
26392682 ConvertObject (container, ObjectShape::Element),
26402683 ConvertSubscriptIndex (idx));
0 commit comments