Skip to content

Commit ee98fe0

Browse files
committed
Code refactoring
1 parent c435d38 commit ee98fe0

15 files changed

Lines changed: 726 additions & 715 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 261 additions & 316 deletions
Large diffs are not rendered by default.

‎cpp2rust/converter/converter.h‎

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
131131

132132
virtual void EmitFunctionPreamble(clang::FunctionDecl *decl);
133133

134+
// Declares the storage of the temporary temp that the default argument of
135+
// param is bound to, and returns its name.
136+
std::string
137+
EmitDefaultArgStorage(const clang::ParmVarDecl *param,
138+
const clang::MaterializeTemporaryExpr *temp);
139+
134140
virtual void ConvertFunctionBody(clang::FunctionDecl *decl);
135141

136142
void ConvertGotoBlock(clang::CompoundStmt *body);
@@ -381,6 +387,9 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
381387
ConvertLambdaToFunctionPointer(clang::Expr *lambda,
382388
const clang::CXXConversionDecl *conversion);
383389

390+
void EmitLambdaFunctionPointer(const clang::CXXRecordDecl *lambda,
391+
clang::CXXMethodDecl *call_operator);
392+
384393
virtual void
385394
ConvertFunctionToFunctionPointer(const clang::FunctionDecl *fn_decl);
386395

@@ -458,6 +467,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
458467

459468
virtual void ConvertBinaryOperator(clang::BinaryOperator *expr);
460469

470+
// The value that the compound assignment expr assigns to its left operand,
471+
// whose type differs from the computation type.
472+
std::string ConvertCompoundAssignValue(clang::CompoundAssignOperator *expr);
473+
461474
virtual bool ConvertIncAndDec(clang::UnaryOperator *expr);
462475

463476
virtual bool VisitUnaryOperator(clang::UnaryOperator *expr);
@@ -487,6 +500,12 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
487500
virtual bool VisitCXXThisExpr(clang::CXXThisExpr *expr);
488501

489502
virtual bool VisitInitListExpr(clang::InitListExpr *expr);
503+
virtual void ConvertScalarInitList(clang::InitListExpr *expr);
504+
// is_empty: whether the list, as written, has no initializers.
505+
virtual void ConvertRecordInitList(clang::InitListExpr *expr, bool is_empty);
506+
virtual void ConvertUnionInitList(clang::InitListExpr *expr,
507+
const clang::FieldDecl *field);
508+
virtual void ConvertArrayInitList(clang::InitListExpr *expr);
490509
bool VisitOpaqueValueExpr(clang::OpaqueValueExpr *expr);
491510
bool VisitArrayInitIndexExpr(clang::ArrayInitIndexExpr *expr);
492511
virtual bool VisitArrayInitLoopExpr(clang::ArrayInitLoopExpr *expr);
@@ -1037,28 +1056,28 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
10371056
}
10381057
};
10391058

1059+
// Whether the converted expression evaluates to a new value, which can be
1060+
// moved, rather than to an existing one, which must be copied.
10401061
enum class ComputedExprType : uint8_t {
1041-
Value,
1042-
FreshValue,
1043-
Pointer,
1044-
FreshPointer,
1062+
Fresh,
1063+
NotFresh,
10451064
Unknown,
10461065
Pending,
10471066
};
10481067
ComputedExprType computed_expr_type_ = ComputedExprType::Unknown;
10491068

10501069
bool isFresh() const {
1051-
assert(computed_expr_type_ != ComputedExprType::Unknown);
1052-
assert(computed_expr_type_ != ComputedExprType::Pending);
1053-
return computed_expr_type_ == ComputedExprType::FreshValue ||
1054-
computed_expr_type_ == ComputedExprType::FreshPointer;
1070+
return computed_expr_type_ == ComputedExprType::Fresh;
10551071
}
10561072

1057-
void SetFresh();
1073+
void MarkFresh() { computed_expr_type_ = ComputedExprType::Fresh; }
1074+
void MarkNotFresh() { computed_expr_type_ = ComputedExprType::NotFresh; }
1075+
// Emits str, which evaluates to a new value.
1076+
void EmitFresh(std::string_view str);
10581077
// str, cloned unless the expression it was converted from is fresh.
10591078
std::string MakeFresh(std::string str);
1079+
// A read of a value of the given type is fresh if the value is copied.
10601080
void SetValueFreshness(clang::QualType type);
1061-
void SetFreshType(clang::QualType type);
10621081

10631082
std::string ConvertLValue(clang::Expr *expr);
10641083
std::string

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -391,23 +391,33 @@ bool IsOverloadedFunction(const clang::FunctionDecl *decl) {
391391
return !lookup_result.isSingleResult();
392392
}
393393

394-
void ForEachTemplateInstantiatedMethod(
394+
// Calls fn on the defined instantiations of the method templates of decl.
395+
static void ForEachTemplateInstantiation(
395396
const clang::CXXRecordDecl *decl,
396397
llvm::function_ref<void(clang::CXXMethodDecl *)> fn) {
397398
for (auto d : decl->decls()) {
398399
if (auto function_template_decl =
399400
llvm::dyn_cast<clang::FunctionTemplateDecl>(d)) {
400401
for (auto s : function_template_decl->specializations()) {
401402
if (auto m = clang::dyn_cast<clang::CXXMethodDecl>(s);
402-
m && !clang::isa<clang::CXXConstructorDecl>(m) &&
403-
m->getDefinition()) {
403+
m && m->getDefinition()) {
404404
fn(m);
405405
}
406406
}
407407
}
408408
}
409409
}
410410

411+
void ForEachTemplateInstantiatedMethod(
412+
const clang::CXXRecordDecl *decl,
413+
llvm::function_ref<void(clang::CXXMethodDecl *)> fn) {
414+
ForEachTemplateInstantiation(decl, [&](clang::CXXMethodDecl *method) {
415+
if (!clang::isa<clang::CXXConstructorDecl>(method)) {
416+
fn(method);
417+
}
418+
});
419+
}
420+
411421
void ForEachMethod(const clang::CXXRecordDecl *decl,
412422
llvm::function_ref<void(clang::CXXMethodDecl *)> fn) {
413423
for (auto *method : decl->methods()) {
@@ -788,18 +798,11 @@ bool IsInitExprOfStringLiteral(const clang::InitListExpr *expr) {
788798
std::vector<clang::CXXConstructorDecl *>
789799
GetTemplateInstantiatedCtors(clang::CXXRecordDecl *decl) {
790800
std::vector<clang::CXXConstructorDecl *> out;
791-
for (auto d : decl->decls()) {
792-
if (auto function_template_decl =
793-
llvm::dyn_cast<clang::FunctionTemplateDecl>(d)) {
794-
for (auto s : function_template_decl->specializations()) {
795-
if (auto c = clang::dyn_cast<clang::CXXConstructorDecl>(s)) {
796-
if (c->getDefinition()) {
797-
out.push_back(c);
798-
}
799-
}
800-
}
801+
ForEachTemplateInstantiation(decl, [&](clang::CXXMethodDecl *method) {
802+
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method)) {
803+
out.push_back(ctor);
801804
}
802-
}
805+
});
803806
return out;
804807
}
805808

0 commit comments

Comments
 (0)