Skip to content

Commit c08428c

Browse files
committed
[interop][SwiftToCxx] do not support generics with requirements for now
1 parent e22fc3e commit c08428c

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,32 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::printFunctionSignature(
318318
const AbstractFunctionDecl *FD, StringRef name, Type resultTy,
319319
FunctionSignatureKind kind, ArrayRef<AdditionalParam> additionalParams,
320320
FunctionSignatureModifiers modifiers) {
321-
if (kind == FunctionSignatureKind::CxxInlineThunk && FD->isGeneric()) {
322-
os << "template<";
323-
llvm::interleaveComma(FD->getGenericParams()->getParams(), os,
324-
[&](const GenericTypeParamDecl *genericParam) {
325-
os << "class ";
326-
ClangSyntaxPrinter(os).printBaseName(genericParam);
327-
});
328-
os << ">\n";
329-
os << "requires ";
330-
llvm::interleave(
331-
FD->getGenericParams()->getParams(), os,
332-
[&](const GenericTypeParamDecl *genericParam) {
333-
os << "swift::isUsableInGenericContext<";
334-
ClangSyntaxPrinter(os).printBaseName(genericParam);
335-
os << ">";
336-
},
337-
" && ");
338-
os << "\n";
321+
if (FD->isGeneric()) {
322+
auto Signature = FD->getGenericSignature();
323+
auto Requirements = Signature.getRequirements();
324+
// FIXME: Support generic requirements.
325+
if (!Requirements.empty())
326+
return ClangRepresentation::unsupported;
327+
if (kind == FunctionSignatureKind::CxxInlineThunk) {
328+
os << "template<";
329+
llvm::interleaveComma(FD->getGenericParams()->getParams(), os,
330+
[&](const GenericTypeParamDecl *genericParam) {
331+
os << "class ";
332+
ClangSyntaxPrinter(os).printBaseName(
333+
genericParam);
334+
});
335+
os << ">\n";
336+
os << "requires ";
337+
llvm::interleave(
338+
FD->getGenericParams()->getParams(), os,
339+
[&](const GenericTypeParamDecl *genericParam) {
340+
os << "swift::isUsableInGenericContext<";
341+
ClangSyntaxPrinter(os).printBaseName(genericParam);
342+
os << ">";
343+
},
344+
" && ");
345+
os << "\n";
346+
}
339347
}
340348
auto emittedModule = FD->getModuleContext();
341349
OutputLanguageMode outputLang = kind == FunctionSignatureKind::CFunctionProto

test/Interop/SwiftToCxx/generics/generic-function-in-cxx.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ public func genericRet<T>(_ x: T) -> T {
3535
return x
3636
}
3737

38+
public func genericRequirementProtocol<T: Hashable>(_ x: T) {
39+
}
40+
41+
public func genericRequirementClass<T>(_ x: T) where T: TestClass {
42+
}
43+
3844
public class TestClass {
3945
let field: Int
4046

@@ -84,6 +90,8 @@ public func createTestSmallStruct(_ x: UInt32) -> TestSmallStruct {
8490
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions10genericRetyxxlF(SWIFT_INDIRECT_RESULT void * _Nonnull, const void * _Nonnull x, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericRet(_:)
8591
// CHECK-NEXT: SWIFT_EXTERN void $s9Functions11genericSwapyyxz_xztlF(void * _Nonnull x, void * _Nonnull y, void * _Nonnull ) SWIFT_NOEXCEPT SWIFT_CALL; // genericSwap(_:_:)
8692

93+
// CHECK-NOT: genericRequirement
94+
8795
// Skip templates in impl classes.
8896
// CHECK: _impl_TestSmallStruct
8997
// CHECK: template<class T>

0 commit comments

Comments
 (0)