@@ -1890,6 +1890,13 @@ bool Converter::VisitExplicitCastExpr(clang::ExplicitCastExpr *expr) {
18901890 StrCat (' )' );
18911891 return false ;
18921892 }
1893+ if (type->isEnumeralType () && !sub_expr->getType ()->isEnumeralType ()) {
1894+ StrCat (std::format (" {}::from" , GetUnsafeTypeAsString (type)));
1895+ PushParen paren (*this );
1896+ Convert (sub_expr);
1897+ StrCat (keyword::kAs , " i32" );
1898+ return false ;
1899+ }
18931900 {
18941901 PushParen paren (*this );
18951902 Convert (sub_expr);
@@ -2704,9 +2711,29 @@ bool Converter::VisitEnumDecl(clang::EnumDecl *decl) {
27042711 std::string_view (init.data (), init.size ())));
27052712 }
27062713 StrCat (" }" );
2714+
2715+ AddFromImpl (decl);
27072716 return false ;
27082717}
27092718
2719+ void Converter::AddFromImpl (clang::EnumDecl *decl) {
2720+ auto name = GetRecordName (decl);
2721+ StrCat (std::format (" impl From<i32> for {}" , name));
2722+ PushBrace impl (*this );
2723+ StrCat (std::format (" fn from(n: i32) -> {}" , name));
2724+ PushBrace fn (*this );
2725+ StrCat (" match n" );
2726+ PushBrace match (*this );
2727+ for (auto e : decl->enumerators ()) {
2728+ llvm::SmallVector<char , 32 > init;
2729+ e->getInitVal ().toString (init, 10 );
2730+ StrCat (std::format (" {} => {}::{}," ,
2731+ std::string_view (init.data (), init.size ()), name,
2732+ std::string_view (e->getName ())));
2733+ }
2734+ StrCat (std::format (" _ => panic!(\" invalid {} value: {{}}\" , n)," , name));
2735+ }
2736+
27102737bool Converter::VisitCXXDefaultArgExpr (clang::CXXDefaultArgExpr *expr) {
27112738 if (expr->getType ()->isPointerType ()) {
27122739 StrCat (keyword_default_);
0 commit comments