Skip to content

Commit 3ed38b5

Browse files
committed
Remove Value<> boxing from struct fields
1 parent ec6c063 commit 3ed38b5

192 files changed

Lines changed: 9130 additions & 9574 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: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,7 +1171,6 @@ void Converter::EmitConstructorFieldInits(clang::CXXConstructorDecl *decl) {
11711171

11721172
for (const auto *field : record_decl->fields()) {
11731173
auto field_name = GetNamedDeclAsString(field);
1174-
auto field_type = field->getType();
11751174
const clang::CXXCtorInitializer *ctor_initializer = nullptr;
11761175
for (const auto *init : definition->inits()) {
11771176
if (init->isMemberInitializer() && init->getMember() == field) {
@@ -1180,15 +1179,9 @@ void Converter::EmitConstructorFieldInits(clang::CXXConstructorDecl *decl) {
11801179
}
11811180
}
11821181

1183-
if (ctor_initializer) {
1184-
StrCat(field_name, token::kColon);
1185-
ConvertVarInit(field_type, ctor_initializer->getInit());
1186-
} else if (auto *init = field->getInClassInitializer()) {
1187-
StrCat(field_name, token::kColon);
1188-
ConvertVarInit(field_type, init);
1189-
} else {
1190-
StrCat(field_name, token::kColon, GetDefaultAsString(field_type));
1191-
}
1182+
StrCat(field_name, token::kColon);
1183+
ConvertFieldInit(field, ctor_initializer ? ctor_initializer->getInit()
1184+
: field->getInClassInitializer());
11921185
StrCat(token::kComma);
11931186
}
11941187
}
@@ -2444,7 +2437,9 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
24442437
case clang::CastKind::CK_LValueToRValue: {
24452438
PushExprKind push(*this, ExprKind::RValue);
24462439
Convert(sub_expr);
2447-
SetValueFreshness(type);
2440+
if (!isFresh()) {
2441+
SetValueFreshness(type);
2442+
}
24482443
break;
24492444
}
24502445
case clang::CastKind::CK_ArrayToPointerDecay: {
@@ -3398,7 +3393,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
33983393
int i = 0;
33993394
for (const auto *field : record->fields()) {
34003395
StrCat(GetNamedDeclAsString(field), token::kColon);
3401-
ConvertVarInit(field->getType(), expr->getInit(i++));
3396+
ConvertFieldInit(field, expr->getInit(i++));
34023397
StrCat(token::kComma);
34033398
}
34043399
} else {
@@ -4665,15 +4660,20 @@ void Converter::EmitDefaultStructLiteral(const clang::RecordDecl *decl) {
46654660
PushBrace brace(*this);
46664661
for (auto *field : decl->fields()) {
46674662
StrCat(GetNamedDeclAsString(field), token::kColon);
4668-
if (auto *init = field->getInClassInitializer()) {
4669-
ConvertVarInit(field->getType(), init);
4670-
} else {
4671-
StrCat(GetDefaultAsString(field->getType()));
4672-
}
4663+
ConvertFieldInit(field, field->getInClassInitializer());
46734664
StrCat(token::kComma);
46744665
}
46754666
}
46764667

4668+
void Converter::ConvertFieldInit(const clang::FieldDecl *field,
4669+
clang::Expr *init) {
4670+
if (init) {
4671+
ConvertVarInit(field->getType(), init);
4672+
} else {
4673+
StrCat(GetDefaultAsString(field->getType()));
4674+
}
4675+
}
4676+
46774677
void Converter::AddByteReprTrait(const clang::RecordDecl *decl) {}
46784678

46794679
void Converter::ConvertUnsignedArithBinaryOperator(clang::BinaryOperator *op,
@@ -4838,8 +4838,16 @@ void Converter::PlaceholderCtx::dump() const {
48384838
<< ", materialize_idx: " << materialize_idx << '\n';
48394839
}
48404840

4841+
std::string Converter::ConvertTake(clang::Expr *arg) {
4842+
return std::format("std::mem::take(&mut {})", ConvertLValue(arg));
4843+
}
4844+
48414845
std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
48424846
const PlaceholderCtx &ph_ctx) {
4847+
if (auto it = hoisted_exprs_.find(arg); it != hoisted_exprs_.end()) {
4848+
return it->second;
4849+
}
4850+
48434851
if (arg->getType()->isFunctionPointerType()) {
48444852
return ConvertFnPtrPlaceholder(arg);
48454853
}
@@ -4907,9 +4915,9 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
49074915
}
49084916
return ConvertFreshRValue(arg);
49094917
}
4910-
auto lvalue = ConvertLValue(arg);
4918+
auto str = ConvertTake(arg);
49114919
SetFresh();
4912-
return std::format("std::mem::take(&mut {})", std::move(lvalue));
4920+
return str;
49134921
}
49144922

49154923
if (ph_ctx.access == TranslationRule::Access::kMove) {

‎cpp2rust/converter/converter.h‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
340340
std::string ConvertFnPtrCallee(clang::Expr *arg);
341341
virtual std::string ConvertFnPtrPlaceholder(clang::Expr *arg);
342342

343+
// Moves the value of the lvalue arg out, leaving a default value behind.
344+
virtual std::string ConvertTake(clang::Expr *arg);
345+
343346
// Option<fn> implements Copy
344347
virtual bool FunctionPointerImplementsCopy() const { return true; }
345348

@@ -597,6 +600,11 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
597600

598601
virtual void ConvertVarInit(clang::QualType qual_type, clang::Expr *expr);
599602

603+
// The initial value of a field of a struct: `init`, or the default value
604+
// if null.
605+
virtual void ConvertFieldInit(const clang::FieldDecl *field,
606+
clang::Expr *init);
607+
600608
virtual void ConvertUnsignedArithOperand(clang::Expr *expr,
601609
clang::QualType type);
602610

@@ -696,7 +704,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
696704
unsigned num_args = 0,
697705
TempMaterializationCtx *ctx = nullptr);
698706

699-
std::string
707+
virtual std::string
700708
ConvertIRFragment(const std::vector<TranslationRule::BodyFragment> &fragments,
701709
clang::Expr *expr, clang::Expr **args, unsigned num_args,
702710
TempMaterializationCtx *ctx);
@@ -760,6 +768,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
760768
};
761769

762770
std::string ufcs_receiver_;
771+
772+
// Expressions that were evaluated into local variables ahead of the
773+
// expression that contains them, and the names of these variables.
774+
std::unordered_map<const clang::Expr *, std::string> hoisted_exprs_;
763775
bool in_const_initializer_ = false;
764776
std::optional<bool> autoref_mut_;
765777
bool suppress_iterator_clone_ = false;

0 commit comments

Comments
 (0)