Skip to content

[cxx-interop] Support optional generic cases in enums #75100

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
Jul 18, 2024
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
68 changes: 27 additions & 41 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,25 +545,29 @@ class DeclAndTypePrinter::Implementation
std::tie(objectType, optKind) = getObjectTypeAndOptionality(
paramType->getNominalOrBoundGenericNominal(), paramType);
auto objectTypeDecl = objectType->getNominalOrBoundGenericNominal();
assert(objectTypeDecl != nullptr || paramType->isOptional());

if (auto knownCxxType =
owningPrinter.typeMapping.getKnownCxxTypeInfo(
objectTypeDecl)) {
if (objectTypeDecl &&
owningPrinter.typeMapping.getKnownCxxTypeInfo(objectTypeDecl)) {
outOfLineOS << " " << types[paramType] << " result;\n";
outOfLineOS << " "
"memcpy(&result, payloadFromDestruction, "
"sizeof(result));\n";
outOfLineOS << " return result;\n ";
} else {
bool isOptional = false;
if (!objectTypeDecl) {
objectTypeDecl = paramType->getNominalOrBoundGenericNominal();
isOptional = true;
}
outOfLineOS << " return swift::";
outOfLineOS << cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl,
elementDecl->getParentEnum()->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineOS << ">::type";
if (isa<ClassDecl>(objectTypeDecl)) {
if (!isOptional && isa<ClassDecl>(objectTypeDecl)) {
outOfLineOS << "::makeRetained(*reinterpret_cast<void "
"**>(payloadFromDestruction));\n ";
} else {
Expand All @@ -572,10 +576,9 @@ class DeclAndTypePrinter::Implementation
outOfLineOS << " swift::"
<< cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl,
elementDecl->getParentEnum()->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineOS << ">::type";
outOfLineOS << "::initializeWithTake(result, "
"payloadFromDestruction);\n";
Expand Down Expand Up @@ -709,14 +712,15 @@ class DeclAndTypePrinter::Implementation
ED, paramType);
auto objectTypeDecl =
objectType->getNominalOrBoundGenericNominal();
assert(objectTypeDecl != nullptr);
assert(objectTypeDecl != nullptr || paramType->isOptional());

if (owningPrinter.typeMapping.getKnownCxxTypeInfo(
if (objectTypeDecl &&
owningPrinter.typeMapping.getKnownCxxTypeInfo(
objectTypeDecl)) {
outOfLineOS
<< " memcpy(result._getOpaquePointer(), &val, "
"sizeof(val));\n";
} else if (isa<ClassDecl>(objectTypeDecl)) {
} else if (isa_and_nonnull<ClassDecl>(objectTypeDecl)) {
outOfLineOS
<< " auto op = swift::"
<< cxx_synthesis::getCxxImplNamespaceName()
Expand All @@ -727,46 +731,31 @@ class DeclAndTypePrinter::Implementation
objectTypeDecl =
paramType->getNominalOrBoundGenericNominal();
outOfLineOS << " alignas(";
outOfLineSyntaxPrinter
.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
ED->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
outOfLineOS << ") unsigned char buffer[sizeof(";
outOfLineSyntaxPrinter
.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
ED->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
outOfLineOS << ")];\n";
outOfLineOS << " auto *valCopy = new(buffer) ";
outOfLineSyntaxPrinter
.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
ED->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
outOfLineOS << "(val);\n";
outOfLineOS << " ";
outOfLineOS << cxx_synthesis::getCxxSwiftNamespaceName()
<< "::";
outOfLineOS << cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter
.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
ED->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
outOfLineOS << ">::type::initializeWithTake(result._"
"getOpaquePointer(), ";
outOfLineOS << cxx_synthesis::getCxxSwiftNamespaceName()
<< "::";
outOfLineOS << cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter
.printModuleNamespaceQualifiersIfNeeded(
objectTypeDecl->getModuleContext(),
ED->getModuleContext());
outOfLineSyntaxPrinter.printBaseName(objectTypeDecl);
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
outOfLineOS << ">::type::getOpaquePointer(*valCopy)";
outOfLineOS << ");\n";
}
Expand Down Expand Up @@ -2914,9 +2903,6 @@ static bool isEnumExposableToCxx(const ValueDecl *VD,
if (auto *params = elementDecl->getParameterList()) {
for (const auto *param : *params) {
auto paramType = param->getInterfaceType();
// TODO: properly support exporting these optionals. rdar://131112273
if (paramType->isOptional() && paramType->getOptionalObjectType()->isTypeParameter())
return false;
if (DeclAndTypeClangFunctionPrinter::getTypeRepresentation(
printer.getTypeMapping(), printer.getInteropContext(),
printer, enumDecl->getModuleContext(), paramType)
Expand Down
13 changes: 8 additions & 5 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ void ClangValueTypePrinter::printTypeGenericTraits(
os << "namespace swift SWIFT_PRIVATE_ATTR {\n";

if (typeDecl->hasClangNode()) {
/// Print a reference to the type metadata fucntion for a C++ type.
/// Print a reference to the type metadata function for a C++ type.
ClangSyntaxPrinter(os).printNamespace(
cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &os) {
ClangSyntaxPrinter(os).printCTypeMetadataTypeFunction(
Expand Down Expand Up @@ -696,20 +696,23 @@ void ClangValueTypePrinter::printTypeGenericTraits(
os << "> = true;\n";
}

// FIXME: generic support.
if (!typeDecl->hasClangNode() && typeMetadataFuncRequirements.empty()) {
if (!typeDecl->hasClangNode()) {
assert(NTD);
os << "template<>\n";
if (printer.printNominalTypeOutsideMemberDeclTemplateSpecifiers(NTD))
os << "template<>\n";
os << "struct";
declAndTypePrinter.printAvailability(os, typeDecl);
os << " implClassFor<";
printer.printBaseName(typeDecl->getModuleContext());
os << "::";
printer.printBaseName(typeDecl);
printer.printNominalTypeReference(NTD, moduleContext);
os << "> { using type = ";
printer.printBaseName(typeDecl->getModuleContext());
os << "::" << cxx_synthesis::getCxxImplNamespaceName() << "::";
printCxxImplClassName(os, NTD);
if (NTD->isGeneric())
printer.printGenericSignatureParams(
NTD->getGenericSignature().getCanonicalSignature());
os << "; };\n";
}
os << "} // namespace\n";
Expand Down
7 changes: 7 additions & 0 deletions test/Interop/SwiftToCxx/generics/generic-enum-execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ int main() {
// CHECK-NEXT: after some
// CHECK-NEXT: destroy-TracksDeinit
}
{
auto opt = swift::Optional<int>::some(14);
auto x = GenericCustomType<int>::success(opt);
auto y = x;
assert(y.isSuccess());
assert(y.getSuccess().getSome() == 14);
}
puts("EOF");
// CHECK-NEXT: EOF
return 0;
Expand Down
14 changes: 13 additions & 1 deletion test/Interop/SwiftToCxx/generics/generic-enum-in-cxx.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -typecheck -module-name Generics -clang-header-expose-decls=all-public -emit-clang-header-path %t/generics.h
// RUN: %target-swift-frontend %s -typecheck -module-name Generics -enable-experimental-cxx-interop -emit-clang-header-path %t/generics.h
// RUN: %FileCheck %s < %t/generics.h
// RUN: %check-interop-cxx-header-in-clang(%t/generics.h -Wno-reserved-identifier -DSWIFT_CXX_INTEROP_HIDE_STL_OVERLAY)

Expand Down Expand Up @@ -96,6 +96,16 @@ public func inoutConcreteOpt(_ x: inout GenericOpt<UInt16>) {

// CHECK: namespace Generics SWIFT_PRIVATE_ATTR SWIFT_SYMBOL_MODULE("Generics") {

// CHECK: template<class T_0_0>
// CHECK-NEXT: #ifdef __cpp_concepts
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0>
// CHECK-NEXT: #endif
// CHECK-NEXT: class SWIFT_SYMBOL("s:8Generics17GenericCustomTypeO") GenericCustomType;

// CHECK: static inline const constexpr bool isOpaqueLayout<Generics::GenericCustomType<T_0_0>> = true;

// CHECK: template<class T_0_0>

// CHECK: template<class T_0_0>
// CHECK-NEXT: #ifdef __cpp_concepts
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0>
Expand Down Expand Up @@ -218,6 +228,8 @@ public func inoutConcreteOpt(_ x: inout GenericOpt<UInt16>) {
// CHECK-NEXT: _impl::$s8Generics14takeGenericOptyyAA0cD0OyxGlF(_impl::_impl_GenericOpt<T_0_0>::getOpaquePointer(x), swift::TypeMetadataTrait<T_0_0>::getTypeMetadata());
// CHECK-NEXT: }

// CHECK: SWIFT_INLINE_THUNK bool GenericCustomType<T_0_0>::isFailure() const {

// CHECK: template<class T_0_0>
// CHECK-NEXT: #ifdef __cpp_concepts
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ public func inoutConcretePair(_ x: UInt16, _ y: inout GenericPair<UInt16, UInt16
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
// CHECK-NEXT: #endif // __cpp_concepts
// CHECK-NEXT: static inline const constexpr bool isOpaqueLayout<Generics::GenericPair<T_0_0, T_0_1>> = true;
// CHECK-NEXT: template<class T_0_0, class T_0_1>
// CHECK-NEXT: #ifdef __cpp_concepts
// CHECK-NEXT: requires swift::isUsableInGenericContext<T_0_0> && swift::isUsableInGenericContext<T_0_1>
// CHECK-NEXT: #endif // __cpp_concepts
// CHECK-NEXT: struct implClassFor<Generics::GenericPair<T_0_0, T_0_1>> { using type = Generics::_impl::_impl_GenericPair<T_0_0, T_0_1>; }
// CHECK-NEXT: } // namespace
// CHECK-NEXT: #pragma clang diagnostic pop
// CHECK-NEXT: } // namespace swift
Expand Down
1 change: 1 addition & 0 deletions test/Interop/SwiftToCxx/stdlib/swift-stdlib-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
// CHECK: template<class T_0_0>
// CHECK: template<class T_0_0>

// CHECK: template<class T_0_0>
// CHECK: template<class T_0_0>
// CHECK: template<class T_0_0>
// CHECK-NEXT: #ifdef __cpp_concepts
Expand Down