Skip to content

Commit bcae6f4

Browse files
committed
Merge branch 'master' into 12-empty-init-list-rule
2 parents 167832e + 26ff6db commit bcae6f4

20 files changed

Lines changed: 240 additions & 359 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 73 additions & 137 deletions
Large diffs are not rendered by default.

‎cpp2rust/converter/converter.h‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
166166

167167
virtual void ConvertGlobalVarDecl(clang::VarDecl *decl);
168168

169-
void ConvertVaListVarDecl(clang::VarDecl *decl);
170-
171169
virtual bool ConvertVarDeclSkipInit(clang::VarDecl *decl);
172170

173171
bool VisitRecordDecl(clang::RecordDecl *decl);
@@ -198,6 +196,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
198196

199197
std::string GetMethodName(const clang::CXXMethodDecl *decl);
200198

199+
std::string GetConversionName(const clang::CXXConversionDecl *decl) const;
200+
201201
virtual std::string GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl);
202202

203203
std::string GetCtorName(clang::CXXConstructorDecl *decl);
@@ -225,7 +225,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
225225
virtual bool ThisIsRustPtr() const { return false; }
226226

227227
virtual void ConvertCXXConstructorBody(clang::CXXConstructorDecl *decl);
228-
void EmitConstructorFieldInits(clang::CXXConstructorDecl *decl);
228+
// The object that decl constructs, before its body runs.
229+
void EmitConstructorInit(clang::CXXConstructorDecl *decl);
229230

230231
virtual bool VisitCXXConstructorDecl(clang::CXXConstructorDecl *decl);
231232

@@ -285,7 +286,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
285286

286287
const std::string &GetOrMaterialize(
287288
unsigned argument_num,
288-
std::function<std::pair<std::string, std::string>(clang::QualType)>
289+
llvm::function_ref<std::pair<std::string, std::string>(clang::QualType)>
289290
materialize_fn);
290291

291292
// str, preceded by the bindings of the temporaries that it refers to.
@@ -388,6 +389,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
388389

389390
void EmitArgList(const CallInfo &info);
390391

392+
// The variadic arguments, each followed by a comma, converted to VaArg if
393+
// into_va_arg.
394+
void EmitVariadicArgs(llvm::ArrayRef<clang::Expr *> args, bool into_va_arg);
395+
391396
void EmitCall(CallInfo &&info);
392397

393398
void ConvertGenericCallExpr(clang::CallExpr *expr);
@@ -432,8 +437,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
432437

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

435-
void DefineImplicitMembers(clang::CXXRecordDecl *decl);
436-
437440
bool VisitCallExpr(clang::CallExpr *expr);
438441

439442
virtual bool VisitIntegerLiteral(clang::IntegerLiteral *expr);
@@ -602,8 +605,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
602605
VisitCXXStdInitializerListExpr(clang::CXXStdInitializerListExpr *expr);
603606

604607
protected:
605-
const clang::Expr *GetParentExpr(const clang::Expr *expr);
606-
607608
#define StrCat(...) _StrCat(__FUNCTION__, __LINE__, __VA_ARGS__)
608609

609610
inline bool is_empty(char c) { return false; }
@@ -681,7 +682,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
681682
}
682683

683684
virtual bool Convert(clang::QualType qual_type);
684-
virtual bool ConvertMappedType(clang::QualType qual_type);
685685

686686
virtual std::string ConvertPointeeType(clang::QualType ptr_type);
687687

@@ -742,8 +742,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
742742
void ConvertParamList(llvm::ArrayRef<clang::ParmVarDecl *> params,
743743
bool is_variadic);
744744

745-
virtual void ConvertFunctionQualifiers(clang::FunctionDecl *decl);
746-
747745
virtual void ConvertFunctionReturnType(clang::FunctionDecl *decl);
748746

749747
virtual void ConvertFunctionMain(const clang::FunctionDecl *decl,

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <clang/Sema/Initialization.h>
1616
#include <clang/Sema/Sema.h>
1717
#include <llvm/Support/ErrorHandling.h>
18-
#include <llvm/Support/Path.h>
1918
#include <llvm/Support/raw_ostream.h>
2019

2120
#include <algorithm>
@@ -384,13 +383,6 @@ bool IsMutatingCall(const clang::CallExpr *expr) {
384383
return true;
385384
}
386385

387-
bool IsOverloadedFunction(const clang::FunctionDecl *decl) {
388-
const auto *ctx = decl->getDeclContext();
389-
const auto decl_name = decl->getDeclName();
390-
const auto lookup_result = ctx->lookup(decl_name);
391-
return !lookup_result.isSingleResult();
392-
}
393-
394386
// Calls fn on the defined instantiations of the method templates of decl.
395387
static void ForEachTemplateInstantiation(
396388
const clang::CXXRecordDecl *decl,
@@ -501,13 +493,7 @@ bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,
501493
return !method->isDeleted() && method_name &&
502494
method_name == name;
503495
}) == 1;
504-
bool is_unique_name =
505-
std::none_of(record->method_begin(), record->method_end(),
506-
[&name](const clang::CXXMethodDecl *method) {
507-
return method->getDeclName().isIdentifier() &&
508-
method->getName() == name;
509-
});
510-
return is_unique_member && is_unique_name;
496+
return is_unique_member && !HasMethodNamed(record, name);
511497
}
512498

513499
bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor) {
@@ -853,15 +839,6 @@ GetDefaultArgTemporary(const clang::ParmVarDecl *param) {
853839
param->getDefaultArg()->IgnoreParens());
854840
}
855841

856-
std::string GetMainFileName(const clang::ASTContext &ctx) {
857-
const auto &src_mgr = ctx.getSourceManager();
858-
auto file_id = src_mgr.getMainFileID();
859-
auto file_entry = src_mgr.getFileEntryRefForID(file_id);
860-
auto file_path = file_entry->getName();
861-
auto file_name = llvm::sys::path::filename(file_path);
862-
return llvm::sys::path::stem(file_name).str();
863-
}
864-
865842
std::string GetFileName(const clang::Decl *decl) {
866843
const auto &ctx = decl->getASTContext();
867844
const auto full_location = ctx.getFullLoc(decl->getBeginLoc());
@@ -1543,18 +1520,12 @@ void ToIdentifier(std::string &name) {
15431520
std::replace(name.begin(), name.end(), '*', 'p');
15441521
}
15451522

1546-
std::string GetConversionName(const clang::CXXConversionDecl *decl,
1547-
const std::string &rust_type) {
1548-
auto name = "to_" + rust_type;
1549-
ToIdentifier(name);
1550-
const auto *record = decl->getParent();
1551-
bool is_unique_name =
1552-
std::none_of(record->method_begin(), record->method_end(),
1553-
[&name](const clang::CXXMethodDecl *method) {
1554-
return method->getDeclName().isIdentifier() &&
1555-
method->getName() == name;
1556-
});
1557-
return is_unique_name ? name : GetFunctionBaseName(decl);
1523+
bool HasMethodNamed(const clang::CXXRecordDecl *record, llvm::StringRef name) {
1524+
return llvm::any_of(record->methods(),
1525+
[&](const clang::CXXMethodDecl *method) {
1526+
return method->getDeclName().isIdentifier() &&
1527+
method->getName() == name;
1528+
});
15581529
}
15591530

15601531
clang::CXXDestructorDecl *

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ bool RustSizeDivergesFromC(clang::QualType qt);
7272

7373
bool IsMutatingCall(const clang::CallExpr *expr);
7474

75-
bool IsOverloadedFunction(const clang::FunctionDecl *decl);
76-
7775
// The method of a prefix or postfix increment or decrement, or nullptr if
7876
// opcode is neither.
7977
const char *GetIncDecMethod(clang::UnaryOperatorKind opcode);
@@ -183,8 +181,6 @@ bool HasUsableDefaultArg(const clang::ParmVarDecl *param);
183181
const clang::MaterializeTemporaryExpr *
184182
GetDefaultArgTemporary(const clang::ParmVarDecl *param);
185183

186-
std::string GetMainFileName(const clang::ASTContext &ctx);
187-
188184
std::string GetFileName(const clang::Decl *decl);
189185

190186
unsigned GetLineNumber(const clang::Decl *decl);
@@ -229,8 +225,8 @@ std::string GetFunctionBaseName(const clang::FunctionDecl *decl);
229225

230226
void ToIdentifier(std::string &name);
231227

232-
std::string GetConversionName(const clang::CXXConversionDecl *decl,
233-
const std::string &rust_type);
228+
// Whether record has a method with the given identifier.
229+
bool HasMethodNamed(const clang::CXXRecordDecl *record, llvm::StringRef name);
234230

235231
bool IsImplicitAssignmentCall(const clang::CallExpr *expr);
236232
bool IsUserOperatorCall(const clang::CXXOperatorCallExpr *expr);
@@ -297,22 +293,6 @@ clang::CXXConstructExpr *MakeConstructExpr(clang::ASTContext &ctx,
297293
std::optional<clang::ArrayRef<clang::TemplateArgument>>
298294
GetTemplateArgs(clang::QualType qual_type, clang::Expr *expr = nullptr);
299295

300-
template <class UnaryFunction>
301-
void ForEachTemplateArgument(
302-
clang::ArrayRef<clang::TemplateArgument> template_arguments,
303-
UnaryFunction unary_function) {
304-
for (auto template_argument : template_arguments) {
305-
switch (template_argument.getKind()) {
306-
case clang::TemplateArgument::ArgKind::Type:
307-
unary_function(template_argument.getAsType());
308-
break;
309-
default:
310-
// FIXME: improve logging
311-
log() << "unsupported template argument kind\n";
312-
}
313-
}
314-
}
315-
316296
clang::Expr *GetCallObject(clang::CallExpr *expr);
317297

318298
clang::Expr *GetCallee(clang::CallExpr *expr);

0 commit comments

Comments
 (0)