Skip to content

Commit 77c505e

Browse files
Copilotnunoplopes
andauthored
Change Mapper::Map to return std::optional<std::string>
Agent-Logs-Url: https://github.com/Cpp2Rust/cpp2rust/sessions/67f75cb5-f531-44bf-bf7e-122072105ac0 Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
1 parent b474ede commit 77c505e

5 files changed

Lines changed: 22 additions & 22 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ bool Converter::Convert(clang::QualType qual_type) {
6565
return false;
6666
}
6767

68-
if (Mapper::Contains(qual_type) &&
69-
Mapper::Map(qual_type) != ignore_rule_type_) {
70-
StrCat(Mapper::Map(qual_type));
68+
if (auto mapped = Mapper::Map(qual_type);
69+
mapped && *mapped != ignore_rule_type_) {
70+
StrCat(*mapped);
7171
return false;
7272
}
7373

@@ -76,7 +76,7 @@ bool Converter::Convert(clang::QualType qual_type) {
7676
}
7777

7878
bool Converter::ConvertMappedType(clang::QualType qual_type) {
79-
std::string type_as_string = Mapper::Map(qual_type);
79+
std::string type_as_string = Mapper::Map(qual_type).value_or(std::string{});
8080
if (type_as_string == ignore_rule_type_) {
8181
return false;
8282
}
@@ -551,7 +551,7 @@ static bool recordDerivesCopy(const clang::RecordDecl *decl) {
551551
for (auto f : decl->fields()) {
552552
// Records that contain std::vector, std::array, std::string or anything
553553
// that is translated to Vec<>, do not derive Copy
554-
auto mapped = Mapper::Map(f->getType());
554+
auto mapped = Mapper::Map(f->getType()).value_or(std::string{});
555555
if (mapped.starts_with("Vec<")) {
556556
return false;
557557
}
@@ -1105,7 +1105,7 @@ bool Converter::VisitCXXForRangeStmtMap(clang::CXXForRangeStmt *stmt) {
11051105
auto loop_var_name = GetNamedDeclAsString(loop_var);
11061106

11071107
StrCat("'loop_:");
1108-
auto map_type = Mapper::Map(stmt->getRangeInit()->getType());
1108+
auto map_type = Mapper::Map(stmt->getRangeInit()->getType()).value_or(std::string{});
11091109
StrCat(keyword::kFor, loop_var_name, keyword::kIn,
11101110
"UnsafeMapIterator::begin(&");
11111111
Convert(stmt->getRangeInit());
@@ -1234,7 +1234,7 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
12341234
fmt_width.erase(0, fmt_width.find_first_not_of(' '));
12351235
fmt_width.erase(fmt_width.find_last_not_of(' ') + 1);
12361236
} else if (!arg->getType()->isCharType() &&
1237-
Mapper::Map(arg->getType()) != "Vec<u8>") {
1237+
Mapper::Map(arg->getType()).value_or(std::string{}) != "Vec<u8>") {
12381238
fmt += ("{:" + fmt_width + fmt_trait + "}");
12391239
fmt_width.clear(); // Reset setw after first usage
12401240
arg_str = ToString(arg);
@@ -1251,7 +1251,7 @@ bool Converter::GetFmtArg(clang::Expr *arg, std::string &fmt,
12511251
bool Converter::GetRawArg(clang::Expr *arg, std::string &raw_args) {
12521252
if (arg->getType()->isCharType()) {
12531253
raw_args += "(&[" + ToString(arg) + "]";
1254-
} else if (Mapper::Map(arg->getType()) == "Vec<u8>") {
1254+
} else if (Mapper::Map(arg->getType()).value_or(std::string{}) == "Vec<u8>") {
12551255
PushExprKind push(*this, ExprKind::RValue);
12561256
std::string str = ToString(arg);
12571257
raw_args += "(&(" + str + ")[..(" + str + ").len() - 1]";
@@ -1630,7 +1630,7 @@ std::string Converter::getIntegerLiteral(clang::IntegerLiteral *expr,
16301630
}
16311631

16321632
bool Converter::VisitIntegerLiteral(clang::IntegerLiteral *expr) {
1633-
StrCat(getIntegerLiteral(expr, Mapper::Map(expr->getType()) != "i32"));
1633+
StrCat(getIntegerLiteral(expr, Mapper::Map(expr->getType()).value_or(std::string{}) != "i32"));
16341634
computed_expr_type_ = ComputedExprType::FreshValue;
16351635
return false;
16361636
}
@@ -2672,7 +2672,7 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
26722672
}
26732673
Mapper::AddRuleForUserDefinedType(decl);
26742674
StrCat("#[derive(Clone, Copy, PartialEq, Debug, Default)]");
2675-
StrCat(std::format("enum {}", Mapper::Map(ctx_.getCanonicalTagType(decl))));
2675+
StrCat(std::format("enum {}", Mapper::Map(ctx_.getCanonicalTagType(decl)).value_or(std::string{})));
26762676
StrCat("{");
26772677
bool first_enumerator = true;
26782678
for (auto e : decl->enumerators()) {
@@ -3055,9 +3055,11 @@ void Converter::ConvertVarInit(clang::QualType qual_type, clang::Expr *expr) {
30553055
void Converter::ConvertUnsignedArithOperand(clang::Expr *expr,
30563056
clang::QualType type) {
30573057
Convert(expr);
3058+
auto expr_mapped = Mapper::Map(expr->getType()).value_or(std::string{});
3059+
auto type_mapped = Mapper::Map(type).value_or(std::string{});
30583060
if ((expr->isIntegerConstantExpr(ctx_) &&
30593061
!clang::isa<clang::ImplicitCastExpr>(expr)) ||
3060-
Mapper::Map(expr->getType()) != Mapper::Map(type)) {
3062+
expr_mapped != type_mapped) {
30613063
ConvertCast(type);
30623064
}
30633065
}

cpp2rust/converter/converter_lib.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -567,18 +567,15 @@ bool HasReceiver(clang::Expr *expr) {
567567
std::optional<IteratorCategory>
568568
GetStrongestIteratorCategory(clang::QualType type) {
569569
type = type.getNonReferenceType().getUnqualifiedType();
570-
if (!Mapper::Contains(type)) {
570+
auto mapped = Mapper::Map(type);
571+
if (!mapped) {
571572
return std::nullopt;
572573
}
573574
if (Mapper::MapsToRefcountPointer(type)) {
574575
return IteratorCategory::Contiguous;
575576
}
576-
auto mapped = Mapper::Map(type);
577-
if (mapped.empty()) {
578-
return std::nullopt;
579-
}
580-
if (mapped.starts_with("RefcountMapIter<") ||
581-
mapped.starts_with("UnsafeMapIterator<")) {
577+
if (mapped->starts_with("RefcountMapIter<") ||
578+
mapped->starts_with("UnsafeMapIterator<")) {
582579
return IteratorCategory::Bidirectional;
583580
}
584581
return std::nullopt;

cpp2rust/converter/mapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -612,15 +612,15 @@ std::string InstantiateTemplate(const clang::Expr *expr,
612612
return instantiateTgt(types_map, text);
613613
}
614614

615-
std::string Map(clang::QualType qual_type) {
615+
std::optional<std::string> Map(clang::QualType qual_type) {
616616
if (auto it = search(qual_type); it != types_.end()) {
617617
auto types_map = matchTemplate(it->first, ToString(qual_type)).value();
618618
for (auto &kv : types_map) {
619619
kv.second = mapTypeStringRecursive(kv.second);
620620
}
621621
return instantiateTgt(types_map, it->second.type_info.type);
622622
}
623-
return {};
623+
return std::nullopt;
624624
}
625625

626626
bool MapsToPointer(clang::QualType qual_type) {

cpp2rust/converter/mapper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <clang/AST/Type.h>
99

1010
#include <filesystem>
11+
#include <optional>
1112
#include <string>
1213
#include <unordered_map>
1314

@@ -29,7 +30,7 @@ class PushASTContext {
2930
bool Contains(clang::QualType qual_type);
3031
bool Contains(const clang::Expr *expr);
3132

32-
std::string Map(clang::QualType qual_type);
33+
std::optional<std::string> Map(clang::QualType qual_type);
3334
const TranslationRule::ExprTgt *GetExprTgt(const clang::Expr *expr);
3435
std::string MapFunctionName(const clang::FunctionDecl *decl);
3536
std::string InstantiateTemplate(const clang::Expr *expr,

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static bool IsBoxedType(std::string_view type) {
3636
}
3737

3838
static bool IsBoxedType(clang::QualType type) {
39-
return IsBoxedType(Mapper::Map(type.getUnqualifiedType()));
39+
return IsBoxedType(Mapper::Map(type.getUnqualifiedType()).value_or(std::string{}));
4040
}
4141

4242
static bool NeedsMutAccess(const clang::CXXMethodDecl *method,

0 commit comments

Comments
 (0)