Skip to content

[interop][SwiftToCxx] generics: add support for returning primitive g… #60330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ bool isResilientType(Type t) {
return false;
}

bool isGenericType(Type t) { return t->is<GenericTypeParamType>(); }

bool isKnownCxxType(Type t, PrimitiveTypeMapping &typeMapping) {
return isKnownType(t, typeMapping, OutputLanguageMode::Cxx);
}
Expand Down Expand Up @@ -223,7 +225,12 @@ class CFunctionSignatureTypePrinter
Optional<OptionalTypeKind> optionalKind,
bool isInOutParam) {
// FIXME: handle optionalKind.
// FIXME: handle isInOutParam.
if (typeUseKind == FunctionSignatureTypeUse::ReturnType) {
// generic is always returned indirectly in C signature.
assert(languageMode == OutputLanguageMode::Cxx);
os << genericTpt->getName();
return;
}
if (!isInOutParam)
os << "const ";
if (languageMode == OutputLanguageMode::Cxx) {
Expand Down Expand Up @@ -321,7 +328,7 @@ void DeclAndTypeClangFunctionPrinter::printFunctionSignature(
bool isIndirectReturnType =
kind == FunctionSignatureKind::CFunctionProto &&
!isKnownCType(resultTy, typeMapping) &&
(isResilientType(resultTy) ||
(isResilientType(resultTy) || isGenericType(resultTy) ||
interopContext.getIrABIDetails().shouldReturnIndirectly(resultTy));
if (!isIndirectReturnType) {
OptionalTypeKind retKind;
Expand Down Expand Up @@ -542,6 +549,16 @@ void DeclAndTypeClangFunctionPrinter::printCxxThunkBody(
// indirectly by a pointer.
if (!isKnownCxxType(resultTy, typeMapping) &&
!hasKnownOptionalNullableCxxMapping(resultTy)) {
if (isGenericType(resultTy)) {
// FIXME: Support returning value types.
os << " T returnValue;\n";
std::string returnAddress;
llvm::raw_string_ostream ros(returnAddress);
ros << "reinterpret_cast<void *>(&returnValue)";
printCallToCFunc(/*additionalParam=*/StringRef(ros.str()));
os << ";\n return returnValue;\n";
return;
}
if (auto *decl = resultTy->getNominalOrBoundGenericNominal()) {
if ((isa<StructDecl>(decl) || isa<EnumDecl>(decl))) {
bool isIndirect =
Expand Down
10 changes: 10 additions & 0 deletions test/Interop/SwiftToCxx/generics/generic-function-execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,15 @@ int main() {
assert(x == -13);
assert(y == 42);
}

{
int x = 4;
assert(genericRet(x) == 4);
}

{
double x = -19.75;
assert(genericRet(x) == -19.75);
}
return 0;
}
13 changes: 13 additions & 0 deletions test/Interop/SwiftToCxx/generics/generic-function-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ public func genericSwap<T>(_ x: inout T, _ y: inout T) {
y = t
}

public func genericRet<T>(_ x: T) -> T {
return x
}

// CHECK: SWIFT_EXTERN void $s9Functions20genericPrintFunctionyyxlF(const void * _Nonnull x, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericPrintFunction(_:)
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions32genericPrintFunctionMultiGenericyySi_xxSiq_tr0_lF(ptrdiff_t x, const void * _Nonnull t1, const void * _Nonnull t1p, ptrdiff_t y, const void * _Nonnull t2, void * _Nonnull , void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericPrintFunctionMultiGeneric(_:_:_:_:_:)
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions26genericPrintFunctionTwoArgyyx_SitlF(const void * _Nonnull x, ptrdiff_t y, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericPrintFunctionTwoArg(_:_:)
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions10genericRetyxxlF(SWIFT_INDIRECT_RESULT void * _Nonnull, const void * _Nonnull x, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericRet(_:)
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions11genericSwapyyxz_xztlF(void * _Nonnull x, void * _Nonnull y, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericSwap(_:_:)

// CHECK: template<class T>
Expand All @@ -52,6 +57,14 @@ public func genericSwap<T>(_ x: inout T, _ y: inout T) {
// CHECK-NEXT: return _impl::$s9Functions26genericPrintFunctionTwoArgyyx_SitlF(reinterpret_cast<const void *>(&x), y, swift::getTypeMetadata<T>());
// CHECK-NEXT: }

// CHECK: template<class T>
// CHECK-NEXT: requires swift::isUsableInGenericContext<T>
// CHECK-NEXT: inline T genericRet(const T & x) noexcept SWIFT_WARN_UNUSED_RESULT {
// CHECK-NEXT: T returnValue;
// CHECK-NEXT: _impl::$s9Functions10genericRetyxxlF(reinterpret_cast<void *>(&returnValue), reinterpret_cast<const void *>(&x), swift::getTypeMetadata<T>());
// CHECK-NEXT: return returnValue;
// CHECK-NEXT: }

// CHECK: template<class T>
// CHECK-NEXT: requires swift::isUsableInGenericContext<T>
// CHECK-NEXT: inline void genericSwap(T & x, T & y) noexcept {
Expand Down