@@ -4643,6 +4643,32 @@ std::string Converter::GetComparisonCall(const clang::FunctionDecl *op,
46434643 arg (0 , lhs), arg (1 , rhs));
46444644}
46454645
4646+ std::string Converter::GetComparisonBool (const clang::FunctionDecl *op,
4647+ const clang::CXXRecordDecl *decl,
4648+ std::string_view lhs,
4649+ std::string_view rhs) {
4650+ auto call = GetComparisonCall (op, decl, lhs, rhs);
4651+ auto ret = op->getReturnType ();
4652+ if (ret->isBooleanType ()) {
4653+ return call;
4654+ }
4655+ if (ret->isIntegerType ()) {
4656+ return std::format (" (({}) != 0)" , call);
4657+ }
4658+ const auto *record = ret->getAsCXXRecordDecl ();
4659+ assert (record && " unsupported comparison operator return type" );
4660+ for (const auto *found : record->getVisibleConversionFunctions ()) {
4661+ const auto *conversion = clang::dyn_cast<clang::CXXConversionDecl>(found);
4662+ if (conversion && conversion->getConversionType ()->isBooleanType ()) {
4663+ return std::format (" {{ let __cmp = &{}; {}::{}({}) }}" , call,
4664+ GetUFCSName (conversion), GetMethodName (conversion),
4665+ GetComparisonReceiver (conversion, record, " __cmp" ));
4666+ }
4667+ }
4668+ assert (0 && " comparison operator result has no conversion to bool" );
4669+ return call;
4670+ }
4671+
46464672std::string
46474673Converter::GetComparisonReferenceArg (const clang::CXXRecordDecl *decl,
46484674 std::string_view value) {
@@ -4670,19 +4696,19 @@ void Converter::ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl,
46704696 cmp_body = std::format (" if {} {{ std::cmp::Ordering::Less }} else if {} {{ "
46714697 " std::cmp::Ordering::Greater }} else {{ "
46724698 " std::cmp::Ordering::Equal }}" ,
4673- GetComparisonCall (lt, decl, " self" , " other" ),
4674- GetComparisonCall (lt, decl, " other" , " self" ));
4699+ GetComparisonBool (lt, decl, " self" , " other" ),
4700+ GetComparisonBool (lt, decl, " other" , " self" ));
46754701 }
46764702
46774703 if (eq) {
4678- eq_body = GetComparisonCall (eq, decl, " self" , " other" );
4704+ eq_body = GetComparisonBool (eq, decl, " self" , " other" );
46794705 } else if (cmp) {
46804706 eq_body = std::format (" {} == std::cmp::Ordering::Equal" ,
46814707 GetComparisonCall (cmp, decl, " self" , " other" ));
46824708 } else {
46834709 eq_body = std::format (" !({}) && !({})" ,
4684- GetComparisonCall (lt, decl, " self" , " other" ),
4685- GetComparisonCall (lt, decl, " other" , " self" ));
4710+ GetComparisonBool (lt, decl, " self" , " other" ),
4711+ GetComparisonBool (lt, decl, " other" , " self" ));
46864712 }
46874713
46884714 ConvertOrdAndPartialOrdTraitsBase (cmp_body, eq_body, GetRecordName (decl));
0 commit comments