Skip to content

Commit 4726559

Browse files
committed
Unbox all struct fields including arrays and vectors
1 parent 70d9a7f commit 4726559

95 files changed

Lines changed: 2902 additions & 1651 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎cpp2rust/converter/converter.cpp‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5241,7 +5241,7 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
52415241
}
52425242

52435243
if (ph_ctx.needs_lvalue()) {
5244-
return ConvertLValue(arg);
5244+
return ConvertBorrowMut(arg);
52455245
}
52465246

52475247
if (ph_ctx.access == TranslationRule::Access::kTake) {
@@ -5263,9 +5263,7 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
52635263
}
52645264
return ConvertFreshRValue(arg);
52655265
}
5266-
auto lvalue = ConvertLValue(arg);
5267-
SetFresh();
5268-
return std::format("std::mem::take(&mut {})", std::move(lvalue));
5266+
return ConvertTake(arg);
52695267
}
52705268

52715269
if (ph_ctx.access == TranslationRule::Access::kMove) {
@@ -5275,6 +5273,16 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
52755273
return ConvertRValue(arg, ph_ctx.implicit_convert_to);
52765274
}
52775275

5276+
std::string Converter::ConvertBorrowMut(clang::Expr *arg) {
5277+
return ConvertLValue(arg);
5278+
}
5279+
5280+
std::string Converter::ConvertTake(clang::Expr *arg) {
5281+
auto lvalue = ConvertLValue(arg);
5282+
SetFresh();
5283+
return std::format("std::mem::take(&mut {})", std::move(lvalue));
5284+
}
5285+
52785286
std::string Converter::ConvertMappedMethodCall(
52795287
clang::Expr *expr, const TranslationRule::MethodCallFragment &mc,
52805288
clang::Expr **args, unsigned num_args, TempMaterializationCtx *ctx) {
@@ -5289,10 +5297,16 @@ std::string Converter::GetMappedAsString(clang::Expr *expr, clang::Expr **args,
52895297
if (!tgt_ir)
52905298
return {};
52915299

5300+
auto saved_borrows = std::exchange(mut_borrows_, {});
52925301
auto result = ConvertIRFragment(tgt_ir->body, expr, args, num_args, ctx);
52935302
if (tgt_ir->multi_statement) {
5294-
return '{' + result + '}';
5303+
result = '{' + result + '}';
5304+
}
5305+
// The arguments borrowed through pointers are borrowed for the whole call.
5306+
for (auto &[place, name] : std::ranges::reverse_view(mut_borrows_)) {
5307+
result = std::format("{}.with_mut(|{}| {})", place, name, result);
52955308
}
5309+
mut_borrows_ = std::move(saved_borrows);
52965310
return result;
52975311
}
52985312

‎cpp2rust/converter/converter.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,17 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
769769
std::string ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
770770
const PlaceholderCtx &ph_ctx);
771771

772+
// Moves the value out of `arg`, leaving a default value behind.
773+
virtual std::string ConvertTake(clang::Expr *arg);
774+
775+
// The place of `arg`, which is borrowed mutably by a rule.
776+
virtual std::string ConvertBorrowMut(clang::Expr *arg);
777+
778+
// The places that the arguments of the rule being converted are borrowed
779+
// through, with the names they are borrowed as, e.g., a pointer and the
780+
// closure parameter of its with_mut.
781+
std::vector<std::pair<std::string, std::string>> mut_borrows_;
782+
772783
std::string ConvertVariadicTail(clang::Expr *expr,
773784
const std::vector<clang::Expr *> &all_args);
774785

0 commit comments

Comments
 (0)