Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 94 additions & 43 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {

if (!record_decls_.MarkDefined(GetRecordName(decl))) {
// Other translation units may instantiate members this one did not.
if (clang::isa<clang::ClassTemplateSpecializationDecl>(decl)) {
if (!decl->isAbstract()) {
ConvertLateInstantiatedMethods(decl);
}
return false;
Expand All @@ -967,25 +967,7 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {
return false;
}

sema_->ForceDeclarationOfImplicitMembers(decl);
for (auto ctor : decl->ctors()) {
if (ctor->isCopyConstructor() && ctor->isImplicit() &&
!ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted()) {
sema_->DefineImplicitCopyConstructor(decl->getLocation(), ctor);
}
}
for (auto *method : decl->methods()) {
if (IsComparisonOperator(method) && method->isDefaulted() &&
!method->doesThisDeclarationHaveABody()) {
#if CLANG_VERSION_MAJOR >= 24
auto kind = method->getDefaultedComparisonKind();
#else
auto kind = sema_->getDefaultedComparisonKind(method);
#endif
sema_->DefineDefaultedComparison(decl->getLocation(), method, kind);
}
}

DefineImplicitMembers(decl);
EmitRustStructOrUnion(decl);
} else if (decl->isUnion()) {
if (!record_decls_.MarkDefined(GetRecordName(decl))) {
Expand All @@ -1000,6 +982,44 @@ bool Converter::VisitCXXRecordDecl(clang::CXXRecordDecl *decl) {
return false;
}

void Converter::DefineImplicitMembers(clang::CXXRecordDecl *decl) {
clang::Scope tu_scope(nullptr, clang::Scope::DeclScope,
sema_->getDiagnostics());
tu_scope.setEntity(ctx_.getTranslationUnitDecl());
auto *saved_tu_scope = std::exchange(sema_->TUScope, &tu_scope);
sema_->ForceDeclarationOfImplicitMembers(decl);
for (auto ctor : decl->ctors()) {
if (ctor->isCopyConstructor() && ctor->isImplicit() &&
!ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted()) {
sema_->DefineImplicitCopyConstructor(decl->getLocation(), ctor);
}
if (ctor->isMoveConstructor() && !ctor->isUserProvided() &&
!ctor->doesThisDeclarationHaveABody() && !ctor->isDeleted() &&
!HasDefaultedCopyConstructor(decl)) {
sema_->DefineImplicitMoveConstructor(decl->getLocation(), ctor);
}
}
for (auto *method : decl->methods()) {
if (method->isMoveAssignmentOperator() && !method->isUserProvided() &&
!method->doesThisDeclarationHaveABody() && !method->isDeleted() &&
!HasDefaultedCopyAssignment(decl)) {
sema_->DefineImplicitMoveAssignment(decl->getLocation(), method);
}
}
for (auto *method : decl->methods()) {
if (IsComparisonOperator(method) && method->isDefaulted() &&
!method->doesThisDeclarationHaveABody()) {
#if CLANG_VERSION_MAJOR >= 24
auto kind = method->getDefaultedComparisonKind();
#else
auto kind = sema_->getDefaultedComparisonKind(method);
#endif
sema_->DefineDefaultedComparison(decl->getLocation(), method, kind);
}
}
sema_->TUScope = saved_tu_scope;
}

bool Converter::VisitCXXMethodDecl(clang::CXXMethodDecl *decl) {
decl->dump(log());
if (!ShouldConvertMethod(decl)) {
Expand Down Expand Up @@ -1087,7 +1107,8 @@ std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) {
}

bool Converter::VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl) {
if (decl->isOutOfLine() || decl->isImplicit()) {
if (decl->isOutOfLine() ||
(decl->isImplicit() && !IsConvertibleImplicitMember(decl))) {
return false;
}
PushCurrFunction push_fn(*this, decl);
Expand Down Expand Up @@ -1744,6 +1765,12 @@ bool Converter::VisitCallExpr(clang::CallExpr *expr) {
return false;
}

if (IsImplicitAssignmentCall(expr) && !Mapper::Contains(expr->getCallee())) {
auto *call = clang::cast<clang::CXXMemberCallExpr>(expr);
ConvertAssignment(call->getImplicitObjectArgument(), call->getArg(0), "=");
return false;
}

if (auto plugin_str = TryPluginConvert(expr)) {
StrCat(*plugin_str);
SetFreshType(expr->getType());
Expand Down Expand Up @@ -2691,7 +2718,7 @@ void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) {
}

bool Converter::IsReferenceType(const clang::Expr *expr) const {
const auto *e = expr->IgnoreCasts();
const auto *e = IgnoreStdMove(expr->IgnoreCasts())->IgnoreCasts();
if (const auto *call = clang::dyn_cast<clang::CallExpr>(e)) {
return !clang::isa<clang::CXXOperatorCallExpr>(call) &&
GetReturnTypeOfFunction(call)->isReferenceType();
Expand Down Expand Up @@ -3198,6 +3225,27 @@ bool Converter::VisitCXXThisExpr(clang::CXXThisExpr *expr) {
return false;
}

bool Converter::VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr) {
Convert(expr->getSourceExpr());
return false;
}

bool Converter::VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr) {
StrCat("__i");
computed_expr_type_ = ComputedExprType::FreshValue;
return false;
}

bool Converter::VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr) {
StrCat(std::format("std::array::from_fn::<_, {}, _>",
GetArraySize(expr->getType())));
PushParen paren(*this);
StrCat("|__i: usize|");
ConvertVarInit(expr->getSubExpr()->getType(), expr->getSubExpr());
computed_expr_type_ = ComputedExprType::FreshValue;
return false;
}

bool Converter::VisitInitListExpr(clang::InitListExpr *expr) {
if (auto form = expr->getSemanticForm())
expr = form;
Expand Down Expand Up @@ -3467,16 +3515,6 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
}

auto *ctor = expr->getConstructor();
// Default move is translated using a bitwise .clone() implementation.
// Bitwise clone is only satisfied by default copy constructor. If the copy
// constructor is user defined, then default move calls copy constructor,
// which is wrong.
if (IsDefaultedMoveConstructor(ctor) &&
!HasDefaultedCopyConstructor(ctor->getParent())) {
llvm::report_fatal_error("defaulted move constructor without a fieldwise "
"copy constructor is not supported");
}

if (IsPassThroughConstructor(ctor)) {
// Take suppress before recursing into the child.
bool suppress = PushSuppressIteratorClone::take(*this);
Expand All @@ -3496,7 +3534,6 @@ bool Converter::VisitCXXConstructExpr(clang::CXXConstructExpr *expr) {
return false;
}

assert(ctor->isUserProvided());
if (expr->getType()->isArrayType()) {
ConvertArrayCXXConstructExpr(expr);
} else {
Expand Down Expand Up @@ -3908,6 +3945,10 @@ std::string Converter::ConvertVarDefaultInit(clang::QualType qual_type) {
std::string
Converter::GetOverloadedFunctionName(const clang::FunctionDecl *decl) {
auto name = GetFunctionBaseName(decl);
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(decl);
ctor && !ctor->getParent()->getIdentifier()) {
name = GetRecordName(ctor->getParent());
}

if (decl->getNumParams() != 0U) {
name += '_';
Expand Down Expand Up @@ -4440,15 +4481,8 @@ void Converter::AddDefaultTrait(const clang::RecordDecl *decl) {
if (auto *default_ctor = GetUserDefinedDefaultConstructor(cxx)) {
StrCat(keyword_unsafe_);
PushBrace unsafe_brace(*this);
Convert(clang::CXXConstructExpr::Create(
ctx_, ctx_.getCanonicalTagType(decl), clang::SourceLocation(),
default_ctor,
/*Elidable=*/false, llvm::ArrayRef<clang::Expr *>(),
/*HadMultipleCandidates=*/false,
/*ListInitialization=*/false,
/*StdInitListInitialization=*/false,
/*ZeroInitialization=*/false, clang::CXXConstructionKind::Complete,
clang::SourceRange()));
Convert(MakeConstructExpr(ctx_, ctx_.getCanonicalTagType(decl),
default_ctor, {}));
return;
}
}
Expand Down Expand Up @@ -4683,7 +4717,24 @@ std::string Converter::ConvertPlaceholder(clang::Expr *expr, clang::Expr *arg,
if (clang::isa<clang::MaterializeTemporaryExpr>(arg)) {
return ConvertRValue(arg);
}
return std::format("std::mem::take(&mut {})", ConvertLValue(arg));
if (auto *record = arg->getType()->getAsCXXRecordDecl();
record && IsUserDefinedDecl(record)) {
for (auto *ctor : record->ctors()) {
if (!IsConvertibleMoveConstructor(ctor)) {
continue;
}
Buffer buf(*this);
Convert(MakeConstructExpr(ctx_, arg->getType(), ctor, arg));
return std::move(buf).str();
}
if (TypeIsCopyable(arg->getType())) {
return ConvertRValue(arg);
}
return ConvertFreshRValue(arg);
}
auto lvalue = ConvertLValue(arg);
SetFresh();
return std::format("std::mem::take(&mut {})", std::move(lvalue));
}

if (ph_ctx.access == TranslationRule::Access::kMove) {
Expand Down
9 changes: 7 additions & 2 deletions cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual void ConvertVariadicArg(clang::Expr *arg);

void DefineImplicitMembers(clang::CXXRecordDecl *decl);

virtual bool VisitCallExpr(clang::CallExpr *expr);

virtual bool VisitIntegerLiteral(clang::IntegerLiteral *expr);
Expand Down Expand Up @@ -384,6 +386,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
virtual bool VisitCXXThisExpr(clang::CXXThisExpr *expr);

virtual bool VisitInitListExpr(clang::InitListExpr *expr);
bool VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr);
bool VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr);
virtual bool VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr);

virtual bool VisitCompoundLiteralExpr(clang::CompoundLiteralExpr *expr);

Expand Down Expand Up @@ -1003,8 +1008,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
virtual bool emplace_back_plugin_convert(clang::CallExpr *call);
virtual void emplace_back_plugin_construct_arg(clang::QualType elem_type,
clang::CXXConstructExpr *ctor);
virtual void emplace_back_emit_push_open(clang::CXXMemberCallExpr *call);
virtual void emplace_back_emit_push_close(clang::CXXMemberCallExpr *call);
virtual void emplace_back_emit_push(clang::CXXMemberCallExpr *call,
std::string_view arg);

virtual const char *GetPointerDerefPrefix(clang::QualType pointee_type);

Expand Down
Loading
Loading