Skip to content

Commit 0aa2387

Browse files
committed
Use lazy initialization for statics
1 parent ea70456 commit 0aa2387

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,10 @@ bool Converter::ConvertVarDeclSkipInit(clang::VarDecl *decl) {
532532
if (is_parm_with_default_value) {
533533
StrCat("Option<");
534534
}
535-
Convert(qual_type);
535+
{
536+
PushLazyType lazy(*this, IsGlobalVar(decl) && LazyStaticInit());
537+
Convert(qual_type);
538+
}
536539
if (is_parm_with_default_value) {
537540
StrCat('>');
538541
}
@@ -597,8 +600,9 @@ void Converter::ConvertGlobalVarDecl(clang::VarDecl *decl) {
597600
PushConstInitializer static_init(*this, decl->isFileVarDecl() ||
598601
decl->isStaticLocal());
599602
StrCat(token::kAssign);
600-
StrCat(keyword_unsafe_);
601603
{
604+
PushLazyInit lazy(*this, LazyStaticInit());
605+
StrCat(keyword_unsafe_);
602606
PushBrace push(*this);
603607
ConvertVarDeclInitializer(decl);
604608
}
@@ -2860,7 +2864,11 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
28602864
}
28612865

28622866
if (IsGlobalVar(expr)) {
2863-
return GetNamedDeclAsString(expr->getDecl());
2867+
if (LazyStaticInit()) {
2868+
return std::format("(*std::cell::LazyCell::force_mut(&mut *&raw mut {}))",
2869+
GetNamedDeclAsString(decl));
2870+
}
2871+
return GetNamedDeclAsString(decl);
28642872
}
28652873

28662874
return GetNamedDeclAsString(decl);

‎cpp2rust/converter/converter.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
9898
virtual bool VisitFunctionTemplateDecl(clang::FunctionTemplateDecl *decl);
9999

100100
virtual bool VisitVarDecl(clang::VarDecl *decl);
101+
virtual bool LazyStaticInit() const { return true; }
101102

102103
void ConvertVarDecl(clang::VarDecl *decl);
103104

@@ -464,7 +465,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
464465
std::string str() && { return std::move(partial_code); }
465466
};
466467

467-
template <char kOpen, char kClose> class PushDelim {
468+
template <auto kOpen, auto kClose> class PushDelim {
468469
Converter &c;
469470
bool enabled;
470471

@@ -489,6 +490,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
489490
PushDelim<token::kOpenCurlyBracket, token::kCloseCurlyBracket>;
490491
using PushParen = PushDelim<token::kOpenParen, token::kCloseParen>;
491492
using PushBracket = PushDelim<token::kOpenBracket, token::kCloseBracket>;
493+
using PushLazyType = PushDelim<token::kLazyCellType, token::kGt>;
494+
using PushLazyInit = PushDelim<token::kLazyCellNew, token::kCloseParen>;
492495

493496
template <typename T>
494497
inline std::string

‎cpp2rust/converter/lex.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ inline constexpr char kDiv = '/';
3030
inline constexpr char kLt = '<';
3131
inline constexpr char kGt = '>';
3232
inline constexpr char kNewLine = '\n';
33+
inline constexpr const char kLazyCellType[] = "std::cell::LazyCell<";
34+
inline constexpr const char kLazyCellNew[] = "std::cell::LazyCell::new(||";
3335
} // namespace token
3436

3537
namespace keyword {

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class ConverterRefCount final : public Converter {
7979
void EmitFunctionPreamble(clang::FunctionDecl *decl) override;
8080

8181
bool VisitVarDecl(clang::VarDecl *decl) override;
82+
bool LazyStaticInit() const override { return false; }
8283

8384
void ConvertGlobalVarDecl(clang::VarDecl *decl) override;
8485

0 commit comments

Comments
 (0)