Skip to content

Commit 5e3e487

Browse files
committed
Handle enums which have type int
1 parent 3c7f08e commit 5e3e487

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,6 +1846,13 @@ bool Converter::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) {
18461846
Convert(sub_expr);
18471847
break;
18481848
}
1849+
if (type->isEnumeralType() && !sub_expr->getType()->isEnumeralType()) {
1850+
StrCat(std::format("{}::from", GetUnsafeTypeAsString(type)));
1851+
PushParen paren(*this);
1852+
Convert(sub_expr);
1853+
StrCat(keyword::kAs, "i32");
1854+
break;
1855+
}
18491856
{
18501857
PushParen outer(*this);
18511858
if (clang::isa<clang::BinaryOperator>(sub_expr)) {
@@ -2207,10 +2214,14 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
22072214
}
22082215

22092216
if (auto enum_constant = clang::dyn_cast<clang::EnumConstantDecl>(decl)) {
2210-
return std::format("{}::{}",
2211-
GetRecordName(clang::dyn_cast<clang::EnumDecl>(
2212-
enum_constant->getDeclContext())),
2213-
std::string_view(enum_constant->getName()));
2217+
auto qualified = std::format("{}::{}",
2218+
GetRecordName(clang::dyn_cast<clang::EnumDecl>(
2219+
enum_constant->getDeclContext())),
2220+
std::string_view(enum_constant->getName()));
2221+
if (!expr->getType()->isEnumeralType()) {
2222+
return std::format("({} as i32)", qualified);
2223+
}
2224+
return qualified;
22142225
}
22152226

22162227
if (IsGlobalVar(expr)) {

0 commit comments

Comments
 (0)