@@ -912,6 +912,72 @@ void DeclAndTypeClangFunctionPrinter::printCxxToCFunctionParameterUse(
912
912
namePrinter ();
913
913
}
914
914
915
+ void DeclAndTypeClangFunctionPrinter::printGenericReturnSequence (
916
+ raw_ostream &os, const GenericTypeParamType *gtpt,
917
+ llvm::function_ref<void (StringRef)> invocationPrinter,
918
+ Optional<StringRef> initializeWithTakeFromValue) {
919
+ std::string returnAddress;
920
+ llvm::raw_string_ostream ros (returnAddress);
921
+ ros << " reinterpret_cast<void *>(&returnValue)" ;
922
+ std::string resultTyName;
923
+ {
924
+ llvm::raw_string_ostream os (resultTyName);
925
+ ClangSyntaxPrinter (os).printGenericTypeParamTypeName (gtpt);
926
+ }
927
+
928
+ os << " if constexpr (std::is_base_of<::swift::"
929
+ << cxx_synthesis::getCxxImplNamespaceName () << " ::RefCountedClass, "
930
+ << resultTyName << " >::value) {\n " ;
931
+ if (!initializeWithTakeFromValue) {
932
+ os << " void *returnValue;\n " ;
933
+ invocationPrinter (/* additionalParam=*/ StringRef (ros.str ()));
934
+ os << " ;\n " ;
935
+ os << " return ::swift::" << cxx_synthesis::getCxxImplNamespaceName ()
936
+ << " ::implClassFor<" << resultTyName
937
+ << " >::type::makeRetained(returnValue);\n " ;
938
+ } else {
939
+ // FIXME: support taking a class pointer.
940
+ os << " abort();\n " ;
941
+ }
942
+ os << " } else if constexpr (::swift::"
943
+ << cxx_synthesis::getCxxImplNamespaceName () << " ::isValueType<"
944
+ << resultTyName << " >) {\n " ;
945
+
946
+ os << " return ::swift::" << cxx_synthesis::getCxxImplNamespaceName ()
947
+ << " ::implClassFor<" << resultTyName
948
+ << " >::type::returnNewValue([&](void * _Nonnull returnValue) {\n " ;
949
+ if (!initializeWithTakeFromValue) {
950
+ invocationPrinter (/* additionalParam=*/ StringRef (" returnValue" ));
951
+ } else {
952
+ os << " return ::swift::" << cxx_synthesis::getCxxImplNamespaceName ()
953
+ << " ::implClassFor<" << resultTyName
954
+ << " >::type::initializeWithTake(reinterpret_cast<char * "
955
+ " _Nonnull>(returnValue), "
956
+ << *initializeWithTakeFromValue << " )" ;
957
+ }
958
+ os << " ;\n });\n " ;
959
+ os << " } else if constexpr (::swift::"
960
+ << cxx_synthesis::getCxxImplNamespaceName () << " ::isSwiftBridgedCxxRecord<"
961
+ << resultTyName << " >) {\n " ;
962
+ if (!initializeWithTakeFromValue) {
963
+ ClangTypeHandler::printGenericReturnScaffold (os, resultTyName,
964
+ invocationPrinter);
965
+ } else {
966
+ // FIXME: support taking a C++ record type.
967
+ os << " abort();\n " ;
968
+ }
969
+ os << " } else {\n " ;
970
+ os << " " << resultTyName << " returnValue;\n " ;
971
+ if (!initializeWithTakeFromValue) {
972
+ invocationPrinter (/* additionalParam=*/ StringRef (ros.str ()));
973
+ } else {
974
+ os << " memcpy(&returnValue, " << *initializeWithTakeFromValue
975
+ << " , sizeof(returnValue))" ;
976
+ }
977
+ os << " ;\n return returnValue;\n " ;
978
+ os << " }\n " ;
979
+ }
980
+
915
981
void DeclAndTypeClangFunctionPrinter::printCxxThunkBody (
916
982
const AbstractFunctionDecl *FD, const LoweredFunctionSignature &signature,
917
983
StringRef swiftSymbolName, const ModuleDecl *moduleContext, Type resultTy,
@@ -1012,42 +1078,7 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
1012
1078
if (!isKnownCxxType (resultTy, typeMapping) &&
1013
1079
!hasKnownOptionalNullableCxxMapping (resultTy)) {
1014
1080
if (const auto *gtpt = resultTy->getAs <GenericTypeParamType>()) {
1015
- std::string returnAddress;
1016
- llvm::raw_string_ostream ros (returnAddress);
1017
- ros << " reinterpret_cast<void *>(&returnValue)" ;
1018
- std::string resultTyName;
1019
- {
1020
- llvm::raw_string_ostream os (resultTyName);
1021
- ClangSyntaxPrinter (os).printGenericTypeParamTypeName (gtpt);
1022
- }
1023
-
1024
- os << " if constexpr (std::is_base_of<::swift::"
1025
- << cxx_synthesis::getCxxImplNamespaceName () << " ::RefCountedClass, "
1026
- << resultTyName << " >::value) {\n " ;
1027
- os << " void *returnValue;\n " ;
1028
- printCallToCFunc (/* additionalParam=*/ StringRef (ros.str ()));
1029
- os << " ;\n " ;
1030
- os << " return ::swift::" << cxx_synthesis::getCxxImplNamespaceName ()
1031
- << " ::implClassFor<" << resultTyName
1032
- << " >::type::makeRetained(returnValue);\n " ;
1033
- os << " } else if constexpr (::swift::"
1034
- << cxx_synthesis::getCxxImplNamespaceName () << " ::isValueType<"
1035
- << resultTyName << " >) {\n " ;
1036
- os << " return ::swift::" << cxx_synthesis::getCxxImplNamespaceName ()
1037
- << " ::implClassFor<" << resultTyName
1038
- << " >::type::returnNewValue([&](void * _Nonnull returnValue) {\n " ;
1039
- printCallToCFunc (/* additionalParam=*/ StringRef (" returnValue" ));
1040
- os << " ;\n });\n " ;
1041
- os << " } else if constexpr (::swift::"
1042
- << cxx_synthesis::getCxxImplNamespaceName ()
1043
- << " ::isSwiftBridgedCxxRecord<" << resultTyName << " >) {\n " ;
1044
- ClangTypeHandler::printGenericReturnScaffold (os, resultTyName,
1045
- printCallToCFunc);
1046
- os << " } else {\n " ;
1047
- os << " " << resultTyName << " returnValue;\n " ;
1048
- printCallToCFunc (/* additionalParam=*/ StringRef (ros.str ()));
1049
- os << " ;\n return returnValue;\n " ;
1050
- os << " }\n " ;
1081
+ printGenericReturnSequence (os, gtpt, printCallToCFunc);
1051
1082
return ;
1052
1083
}
1053
1084
if (auto *classDecl = resultTy->getClassOrBoundGenericClass ()) {
0 commit comments