@@ -5726,17 +5726,23 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
57265726 auto bind = tgt_ir->argsToBind (ctx_, expr, all_args);
57275727 auto *operand = tgt_ir->refExprOperand ();
57285728 auto effect = tgt_ir->refExprEffect ();
5729+ auto setup = tgt_ir->refExprSetup ();
57295730 // The C++ expression is a reference to the argument, or an element of it.
57305731 auto *base = expr->isGLValue () ? tgt_ir->refExprBase () : nullptr ;
5731- bool discards_ref =
5732- base && operand->size () == 1 && !effect.empty () && isVoid ();
5732+ bool discards_ref = DiscardsRef (expr, *tgt_ir);
57335733 // The temporaries of an argument that a pointer is returned to live as long
57345734 // as the binding.
57355735 if (base && !discards_ref && all_args[base->n ]->HasSideEffects (ctx_) &&
57365736 ReturnsPointerToArg (all_args[base->n ])) {
57375737 bind[base->n ] = true ;
57385738 }
57395739 auto first = ArgsToEvaluateFirst (expr, *tgt_ir, all_args, bind);
5740+ // The object that init constructs is constructed first, rather than its
5741+ // arguments evaluated first one by one.
5742+ auto num_params = std::min (tgt_ir->params .size (), all_args.size ());
5743+ bool init_first =
5744+ tgt_ir->alwaysConstructsInit () &&
5745+ std::find (first.begin () + num_params, first.end (), true ) != first.end ();
57405746 std::vector<std::string> values, names;
57415747 // Bindings in a global initializer are local to the expansion too.
57425748 clang::DeclContext *binding_context = curr_function_;
@@ -5745,7 +5751,7 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
57455751 ctx_, ctx_.getTranslationUnitDecl (), expr->getExprLoc ());
57465752 }
57475753 for (size_t i = 0 ; binding_context && i < all_args.size (); ++i) {
5748- if (!bind[i] && !first[i]) {
5754+ if (( !bind[i] && !first[i]) || (init_first && i >= num_params) ) {
57495755 continue ;
57505756 }
57515757 // The rule takes the value of a transparent union.
@@ -5806,6 +5812,14 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
58065812 }
58075813 }
58085814
5815+ if (init_first) {
5816+ values.push_back (
5817+ std::format (" {{ {} }}" , ConvertInitFragment (expr, all_args)));
5818+ auto name = NewTempName ();
5819+ names.push_back (" mut " + name);
5820+ bound_inits_.emplace (expr, std::move (name));
5821+ }
5822+
58095823 auto saved_borrows = std::exchange (mut_borrows_, {});
58105824 auto saved_borrowed_args = std::exchange (borrowed_args_, {});
58115825 auto outer_borrows = std::exchange (borrows_, false );
@@ -5817,7 +5831,8 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
58175831 result =
58185832 ' {' + ConvertIRFragment (effect, bindings, expr, all_args, ctx) + ' }' ;
58195833 } else if (base && ReturnsPointerToArg (all_args[base->n ])) {
5820- auto str = ConvertIRFragment (effect, bindings, expr, all_args, ctx);
5834+ auto str = ConvertIRFragment (effect, bindings, expr, all_args, ctx) +
5835+ ConvertIRFragment (setup, bindings, expr, all_args, ctx);
58215836 std::string index;
58225837 if (operand->size () > 1 ) {
58235838 index = ConvertIRFragment (llvm::ArrayRef (*operand).drop_front (), bindings,
@@ -5834,19 +5849,22 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
58345849 tgt_ir->body .back ())
58355850 ->prefix ;
58365851 result = std::format (
5837- " {}{} {}" , ConvertIRFragment (effect, bindings, expr, all_args, ctx),
5852+ " {}{}{} {}" , ConvertIRFragment (effect, bindings, expr, all_args, ctx),
5853+ ConvertIRFragment (setup, bindings, expr, all_args, ctx),
58385854 prefix == " &mut" ? " &raw mut"
58395855 : prefix == " &" ? " &raw const"
58405856 : prefix,
58415857 ConvertIRFragment (*operand, bindings, expr, all_args, ctx));
5842- if (!effect.empty ()) {
5858+ if (!effect.empty () || !setup. empty () ) {
58435859 result = ' {' + result + ' }' ;
58445860 }
58455861 } else {
58465862 place = operand && !isAddrOf () && !isVoid ();
5847- result = ConvertIRFragment (
5848- place && values.empty () && effect.empty () ? *operand : tgt_ir->body ,
5849- bindings, expr, all_args, ctx);
5863+ result = ConvertIRFragment (place && values.empty () && effect.empty () &&
5864+ setup.empty ()
5865+ ? *operand
5866+ : tgt_ir->body ,
5867+ bindings, expr, all_args, ctx);
58505868 if (tgt_ir->multi_statement ) {
58515869 result = ' {' + result + ' }' ;
58525870 }
@@ -5874,10 +5892,11 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
58745892 }
58755893 // A place is reached through a reference to it, out of the match or the
58765894 // block that computes it, so that it can be assigned.
5877- if (place && (!values.empty () || !effect.empty ())) {
5895+ if (place && (!values.empty () || !effect.empty () || !setup. empty () )) {
58785896 result = std::format (" (*{})" , result);
58795897 }
58805898 returned_place_ = place && base;
5899+ bound_inits_.erase (expr);
58815900 if (!effects.empty ()) {
58825901 result = std::format (" {{ {} {} }}" , effects, result);
58835902 }
@@ -5896,11 +5915,54 @@ bool Converter::UsedOnce(const TranslationRule::ExprRule &rule, unsigned n) {
58965915 return uses == 1 ;
58975916}
58985917
5899- std::vector<bool >
5900- Converter::ArgsToEvaluateFirst (clang::Expr *, const TranslationRule::ExprRule &,
5901- llvm::ArrayRef<clang::Expr *> args,
5902- const std::vector<bool > &) {
5903- return std::vector<bool >(args.size ());
5918+ namespace {
5919+ // The variables that expr names, other than pointers and references, whose
5920+ // targets Rust doesn't borrow through the names.
5921+ void CollectNamedObjects (const clang::Stmt *stmt,
5922+ std::vector<const clang::VarDecl *> &vars) {
5923+ if (auto *ref = clang::dyn_cast<clang::DeclRefExpr>(stmt)) {
5924+ auto *var = clang::dyn_cast<clang::VarDecl>(ref->getDecl ());
5925+ if (var && !var->getType ()->isPointerType () &&
5926+ !var->getType ()->isReferenceType ()) {
5927+ vars.push_back (var);
5928+ }
5929+ }
5930+ for (auto *child : stmt->children ()) {
5931+ if (child) {
5932+ CollectNamedObjects (child, vars);
5933+ }
5934+ }
5935+ }
5936+ } // namespace
5937+
5938+ // The object that init constructs from the remaining arguments, unless it may
5939+ // not be constructed, is constructed first if any of them names an object that
5940+ // the body borrows mutably, which Rust doesn't let the construction borrow too.
5941+ std::vector<bool > Converter::ArgsToEvaluateFirst (
5942+ clang::Expr *, const TranslationRule::ExprRule &rule,
5943+ llvm::ArrayRef<clang::Expr *> args, const std::vector<bool > &) {
5944+ std::vector<bool > first (args.size ());
5945+ if (!rule.alwaysConstructsInit ()) {
5946+ return first;
5947+ }
5948+ auto num_params = std::min (rule.params .size (), args.size ());
5949+ std::vector<const clang::VarDecl *> borrowed;
5950+ TranslationRule::AnyFragment (rule.body , [&](const auto &frag) {
5951+ auto *ph = std::get_if<TranslationRule::PlaceholderFragment>(&frag);
5952+ if (ph && ph->n < num_params &&
5953+ ph->access == TranslationRule::Access::kBorrowMut ) {
5954+ CollectNamedObjects (args[ph->n ], borrowed);
5955+ }
5956+ return false ;
5957+ });
5958+ if (llvm::any_of (args.drop_front (num_params), [&](clang::Expr *arg) {
5959+ return llvm::any_of (borrowed, [&](const clang::VarDecl *var) {
5960+ return ReferencesVar (arg, var);
5961+ });
5962+ })) {
5963+ std::fill (first.begin () + num_params, first.end (), true );
5964+ }
5965+ return first;
59045966}
59055967
59065968bool Converter::ReturnsPointerToArg (clang::Expr *) { return false ; }
@@ -5978,7 +6040,8 @@ std::string Converter::ConvertIRFragment(
59786040 result += ConvertMappedMethodCall (expr, **mc, bindings, all_args, ctx);
59796041 } else if (auto *re =
59806042 std::get_if<std::unique_ptr<RefExprFragment>>(&frag)) {
5981- result += (*re)->prefix + ' ' +
6043+ result += ConvertIRFragment ((*re)->setup , bindings, expr, all_args, ctx) +
6044+ (*re)->prefix + ' ' +
59826045 ConvertIRFragment ((*re)->inner , bindings, expr, all_args, ctx);
59836046 } else if (auto *call =
59846047 std::get_if<std::unique_ptr<GenericCallFragment>>(&frag)) {
@@ -6137,6 +6200,9 @@ Converter::ConvertInitFragment(clang::Expr *expr,
61376200 if (!tgt_ir || !tgt_ir->usesInit ()) {
61386201 llvm::report_fatal_error (" expression rule does not use Init" );
61396202 }
6203+ if (auto it = bound_inits_.find (expr); it != bound_inits_.end ()) {
6204+ return it->second ;
6205+ }
61406206 auto type = RuleRegistry::GetInitType (ctx_, expr);
61416207
61426208 Buffer buf (*this );
@@ -6210,6 +6276,11 @@ bool Converter::isObject() const {
62106276 return !curr_expr_kind_.empty () && curr_expr_kind_.back () == ExprKind::Object;
62116277}
62126278
6279+ bool Converter::DiscardsRef (const clang::Expr *expr,
6280+ const TranslationRule::ExprRule &rule) const {
6281+ return expr->isGLValue () && isVoid () && rule.refExprDiscardable ();
6282+ }
6283+
62136284bool Converter::isVoid () const {
62146285 return curr_expr_kind_.empty () || curr_expr_kind_.back () == ExprKind::Void;
62156286}
0 commit comments