Skip to content

Commit ccae460

Browse files
authored
Remove Value<> boxing from struct fields except arrays/vectors (#437)
1 parent 7af3a77 commit ccae460

282 files changed

Lines changed: 13742 additions & 10670 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.

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# CMake
22
build/
33
_buildconfig.py
4+
Cargo.lock
5+
target/
46

57
rules/rustls/rustls.h
68
rules/brotli/brotli/

‎cpp2rust/converter/converter.cpp‎

Lines changed: 18 additions & 17 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,10 @@ 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+
// The value may have been copied out already.
2441+
if (!isFresh()) {
2442+
SetValueFreshness(type);
2443+
}
24482444
break;
24492445
}
24502446
case clang::CastKind::CK_ArrayToPointerDecay: {
@@ -3398,7 +3394,7 @@ bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
33983394
int i = 0;
33993395
for (const auto *field : record->fields()) {
34003396
StrCat(GetNamedDeclAsString(field), token::kColon);
3401-
ConvertVarInit(field->getType(), expr->getInit(i++));
3397+
ConvertFieldInit(field, expr->getInit(i++));
34023398
StrCat(token::kComma);
34033399
}
34043400
} else {
@@ -4665,15 +4661,20 @@ void Converter::EmitDefaultStructLiteral(const clang::RecordDecl *decl) {
46654661
PushBrace brace(*this);
46664662
for (auto *field : decl->fields()) {
46674663
StrCat(GetNamedDeclAsString(field), token::kColon);
4668-
if (auto *init = field->getInClassInitializer()) {
4669-
ConvertVarInit(field->getType(), init);
4670-
} else {
4671-
StrCat(GetDefaultAsString(field->getType()));
4672-
}
4664+
ConvertFieldInit(field, field->getInClassInitializer());
46734665
StrCat(token::kComma);
46744666
}
46754667
}
46764668

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

46794680
void Converter::ConvertUnsignedArithBinaryOperator(clang::BinaryOperator *op,

‎cpp2rust/converter/converter.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,11 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
597597

598598
virtual void ConvertVarInit(clang::QualType qual_type, clang::Expr *expr);
599599

600+
// The initial value of a field of a struct: `init`, or the default value
601+
// if null.
602+
virtual void ConvertFieldInit(const clang::FieldDecl *field,
603+
clang::Expr *init);
604+
600605
virtual void ConvertUnsignedArithOperand(clang::Expr *expr,
601606
clang::QualType type);
602607

0 commit comments

Comments
 (0)