Skip to content

Commit f28f2cf

Browse files
committed
Move MethodsOnPtr in refcount
1 parent f2f97e4 commit f28f2cf

5 files changed

Lines changed: 29 additions & 21 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ std::unordered_set<std::string> Converter::globals_;
2929
std::vector<std::string> Converter::global_inits_;
3030
std::unordered_set<std::string> Converter::abstract_structs_;
3131
Converter::RecordIndex Converter::record_decls_;
32-
std::map<std::string, Converter::MethodsOnPtr> Converter::methods_on_ptr_;
3332
std::map<std::string, Converter::DeferredBlock> Converter::vtable_impls_;
3433

3534
void Converter::ConvertUniquePtrDeref(clang::CXXOperatorCallExpr *expr) {
@@ -75,13 +74,6 @@ void Converter::EmitVTableImpls(std::string &out) {
7574
}
7675
}
7776

78-
void Converter::EmitMethodsOnPtr(std::string &out) {
79-
for (const auto &[name, methods] : methods_on_ptr_) {
80-
EmitDeferredBlock(methods.trait, out);
81-
EmitDeferredBlock(methods.impl, out);
82-
}
83-
}
84-
8577
std::string Converter::ForceGlobalInit(const clang::VarDecl *decl) {
8678
return std::format("std::cell::LazyCell::force(&*&raw const {});",
8779
GetNamedDeclAsString(decl));

‎cpp2rust/converter/converter.h‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
5555
static void EmitOpaqueRecords(std::string &out);
5656
static void EmitGlobalInits(Model model, std::string &out);
5757

58-
static void EmitMethodsOnPtr(std::string &out);
59-
6058
static void EmitVTableImpls(std::string &out);
6159

6260
virtual bool VisitBuiltinType(clang::BuiltinType *type);
@@ -916,14 +914,6 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
916914
std::string header;
917915
std::string body;
918916
};
919-
struct MethodsOnPtr {
920-
DeferredBlock trait;
921-
DeferredBlock impl;
922-
};
923-
// record name -> trait and impl for Ptr<record>, emitted after all
924-
// translation units.
925-
static std::map<std::string, MethodsOnPtr> methods_on_ptr_;
926-
927917
static std::map<std::string, DeferredBlock> vtable_impls_;
928918

929919
static void EmitDeferredBlock(const DeferredBlock &block,

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
#include "converter/mapper.h"
1818

1919
namespace cpp2rust {
20+
std::map<std::string, ConverterRefCount::MethodsOnPtr>
21+
ConverterRefCount::methods_on_ptr_;
22+
23+
void ConverterRefCount::EmitMethodsOnPtr(std::string &out) {
24+
for (const auto &[name, methods] : methods_on_ptr_) {
25+
EmitDeferredBlock(methods.trait, out);
26+
EmitDeferredBlock(methods.impl, out);
27+
}
28+
}
29+
2030
ConverterRefCount::ConverterRefCount(std::string &rs_code,
2131
clang::ASTContext &ctx)
2232
: Converter(rs_code, ctx, "", ""),
@@ -2879,7 +2889,7 @@ ConverterRefCount::TraitName(const clang::CXXRecordDecl *decl) const {
28792889
return GetRecordName(decl) + "Impl";
28802890
}
28812891

2882-
Converter::MethodsOnPtr &
2892+
ConverterRefCount::MethodsOnPtr &
28832893
ConverterRefCount::MethodsOnPtrFor(const clang::CXXRecordDecl *decl) {
28842894
auto name = GetRecordName(decl);
28852895
auto [it, inserted] = methods_on_ptr_.try_emplace(name);

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class ConverterRefCount final : public Converter {
1212

1313
void EmitFilePreamble() override;
1414

15+
static void EmitMethodsOnPtr(std::string &out);
16+
1517
bool VisitRecordType(clang::RecordType *type) override;
1618

1719
bool VisitConstantArrayType(clang::ConstantArrayType *type) override;
@@ -247,6 +249,15 @@ class ConverterRefCount final : public Converter {
247249

248250
std::string TraitName(const clang::CXXRecordDecl *decl) const;
249251

252+
struct MethodsOnPtr {
253+
DeferredBlock trait;
254+
DeferredBlock impl;
255+
};
256+
257+
// record name -> trait and impl for Ptr<record>, emitted after all
258+
// translation units.
259+
static std::map<std::string, MethodsOnPtr> methods_on_ptr_;
260+
250261
MethodsOnPtr &MethodsOnPtrFor(const clang::CXXRecordDecl *decl);
251262

252263
std::string DestroyMembers(const clang::CXXRecordDecl *decl) override;

‎cpp2rust/cpp2rust_lib.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "compat/platform_flags.h"
1313
#include "converter/converter.h"
14+
#include "converter/models/converter_refcount.h"
1415
#include "frontend_action.h"
1516

1617
namespace cpp2rust {
@@ -32,7 +33,9 @@ std::string TranspileSrc(std::string_view cc_code, Model model,
3233
filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER);
3334
Converter::EmitOpaqueRecords(rs_code);
3435
Converter::EmitVTableImpls(rs_code);
35-
Converter::EmitMethodsOnPtr(rs_code);
36+
if (model == Model::kRefCount) {
37+
ConverterRefCount::EmitMethodsOnPtr(rs_code);
38+
}
3639
Converter::EmitGlobalInits(model, rs_code);
3740
return rs_code;
3841
}
@@ -75,7 +78,9 @@ std::string TranspileDir(std::string_view build_dir, Model model,
7578
Tool.run(&factory);
7679
Converter::EmitOpaqueRecords(rs_code);
7780
Converter::EmitVTableImpls(rs_code);
78-
Converter::EmitMethodsOnPtr(rs_code);
81+
if (model == Model::kRefCount) {
82+
ConverterRefCount::EmitMethodsOnPtr(rs_code);
83+
}
7984
Converter::EmitGlobalInits(model, rs_code);
8085
return rs_code;
8186
}

0 commit comments

Comments
 (0)