Skip to content

Commit d8b9d76

Browse files
committed
Map default initializer of translated type
1 parent 1ac73cc commit d8b9d76

7 files changed

Lines changed: 23 additions & 11 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,6 +3003,11 @@ std::string Converter::GetDefaultAsString(clang::QualType qual_type) {
30033003
return "VaList::default()";
30043004
}
30053005

3006+
if (auto init = Mapper::MapInitializer(qual_type); !init.empty()) {
3007+
computed_expr_type_ = ComputedExprType::FreshValue;
3008+
return init;
3009+
}
3010+
30063011
if (qual_type->isPointerType()) {
30073012
auto pointee = qual_type->getPointeeType();
30083013
if (pointee->isFunctionType()) {
@@ -3026,13 +3031,7 @@ std::string Converter::GetDefaultAsString(clang::QualType qual_type) {
30263031
return GetDefaultAsString(array_type->getElementType());
30273032
} else {
30283033
auto qual_type_str = Mapper::ToString(qual_type);
3029-
if (qual_type_str == "struct std::pair") {
3030-
auto template_args = *GetTemplateArgs(qual_type);
3031-
auto first_type = template_args[0].getAsType();
3032-
auto second_type = template_args[1].getAsType();
3033-
return std::format("({}, {})", GetDefaultAsString(first_type),
3034-
GetDefaultAsString(second_type));
3035-
} else if (qual_type_str.contains("std::array")) {
3034+
if (qual_type_str.contains("std::array")) {
30363035
assert(GetTemplateArgs(qual_type).has_value());
30373036
auto template_args = *GetTemplateArgs(qual_type);
30383037
assert(template_args.size() == 2);

cpp2rust/converter/mapper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,18 @@ std::string Map(clang::QualType qual_type) {
625625
return {};
626626
}
627627

628+
std::string MapInitializer(clang::QualType qual_type) {
629+
auto type_str = ToString(qual_type);
630+
auto [rule, subs] = search(types_, type_str, GetTypeMapKey(type_str));
631+
if (rule && !rule->initializer.empty()) {
632+
for (auto &ty : subs) {
633+
ty = mapTypeStringRecursive(ty);
634+
}
635+
return instantiateTgt(subs, rule->initializer);
636+
}
637+
return {};
638+
}
639+
628640
bool MapsToPointer(clang::QualType qual_type) {
629641
auto rule = search(qual_type);
630642
return rule && rule->type_info.is_pointer();

cpp2rust/converter/mapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ bool Contains(clang::QualType qual_type);
2828
bool Contains(const clang::Expr *expr);
2929

3030
std::string Map(clang::QualType qual_type);
31+
std::string MapInitializer(clang::QualType qual_type);
3132
const TranslationRule::ExprRule *GetExprRule(const clang::Expr *expr);
3233
std::string MapFunctionName(const clang::FunctionDecl *decl);
3334
std::string InstantiateTemplate(const clang::Expr *expr, unsigned n);

rules/pair/ir_unsafe.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@
459459
}
460460
},
461461
"t1": {
462-
"init": "(T1::default(), T2::default())",
462+
"init": "<(T1, T2)>::default()",
463463
"type": "(T1, T2)"
464464
}
465465
}

rules/pair/tgt_unsafe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct T1;
88
struct T2;
99

1010
fn types() {
11-
let t1: (T1, T2) = (T1::default(), T2::default());
11+
let t1: (T1, T2) = <(T1, T2)>::default();
1212
}
1313

1414
unsafe fn f1<T1, T2>(a0: (T1, T2)) -> T2 {

rules/stdio/ir_unsafe.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@
756756
}
757757
},
758758
"t1": {
759-
"init": "Default::default()",
759+
"init": "std::ptr::null_mut()",
760760
"type": "*mut ::std::fs::File",
761761
"is_unsafe_pointer": true
762762
}

rules/stdio/tgt_unsafe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use libcc2rs::*;
55
use std::io::prelude::*;
66

77
fn types() -> Result<(), Box<dyn std::error::Error>> {
8-
let t1: *mut ::std::fs::File = Default::default();
8+
let t1: *mut ::std::fs::File = std::ptr::null_mut();
99
Ok(())
1010
}
1111

0 commit comments

Comments
 (0)