Skip to content

[5.9\[interop][SwiftToCxx] add additional type representation emission checks #65496

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 2 commits into from
May 1, 2023
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
8 changes: 5 additions & 3 deletions lib/AST/SwiftNameTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,11 @@ swift::cxx_translation::getDeclRepresentation(const ValueDecl *VD) {
return {Unsupported, UnrepresentableEnumCaseTuple};
for (const auto *param : *params) {
auto paramType = param->getInterfaceType();
if (!paramType->is<GenericTypeParamType>() &&
!paramType->getNominalOrBoundGenericNominal())
return {Unsupported, UnrepresentableEnumCaseType};
if (!paramType->is<GenericTypeParamType>()) {
auto *nominal = paramType->getNominalOrBoundGenericNominal();
if (!nominal || isa<ProtocolDecl>(nominal))
return {Unsupported, UnrepresentableEnumCaseType};
}
}
}
}
Expand Down
34 changes: 32 additions & 2 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ class DeclAndTypePrinter::Implementation
void visitEnumDeclCxx(EnumDecl *ED) {
assert(owningPrinter.outputLang == OutputLanguageMode::Cxx);

// FIXME: Print enum's availability
ClangValueTypePrinter valueTypePrinter(os, owningPrinter.prologueOS,
owningPrinter.interopContext);
ClangSyntaxPrinter syntaxPrinter(os);
Expand Down Expand Up @@ -2793,12 +2792,43 @@ static bool isExposedToThisModule(const ModuleDecl &M, const ValueDecl *VD,
return exposedModules.count(mc->getName().str());
}

static bool isEnumExposableToCxx(const ValueDecl *VD,
DeclAndTypePrinter &printer) {
auto *enumDecl = dyn_cast<EnumDecl>(VD);
if (!enumDecl)
return true;
// The supported set of enum elements is restricted by
// the types that can be represented in C++. We already
// check for different type categories in `getDeclRepresentation`, however,
// we also need to perform additional check on whether the type can be
// emitted here as well, to ensure that we don't emit types from dependent
// modules that do not have a C++ representation.
for (const auto *enumCase : enumDecl->getAllCases()) {
for (const auto *elementDecl : enumCase->getElements()) {
if (!elementDecl->hasAssociatedValues())
continue;
if (auto *params = elementDecl->getParameterList()) {
for (const auto *param : *params) {
auto paramType = param->getInterfaceType();
if (DeclAndTypeClangFunctionPrinter::getTypeRepresentation(
printer.getTypeMapping(), printer.getInteropContext(),
printer, enumDecl->getModuleContext(), paramType)
.isUnsupported())
return false;
}
}
}
}
return true;
}

bool DeclAndTypePrinter::shouldInclude(const ValueDecl *VD) {
return !VD->isInvalid() && (!requiresExposedAttribute || hasExposeAttr(VD)) &&
(outputLang == OutputLanguageMode::Cxx
? cxx_translation::isVisibleToCxx(VD, minRequiredAccess) &&
isExposedToThisModule(M, VD, exposedModules) &&
cxx_translation::isExposableToCxx(VD)
cxx_translation::isExposableToCxx(VD) &&
isEnumExposableToCxx(VD, *this)
: isVisibleToObjC(VD, minRequiredAccess)) &&
!VD->getAttrs().hasAttribute<ImplementationOnlyAttr>() &&
!isAsyncAlternativeOfOtherDecl(VD) &&
Expand Down
2 changes: 2 additions & 0 deletions lib/PrintAsClang/DeclAndTypePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class DeclAndTypePrinter {
requiresExposedAttribute(requiresExposedAttribute),
exposedModules(exposedModules), outputLang(outputLang) {}

PrimitiveTypeMapping &getTypeMapping() { return typeMapping; }

SwiftToClangInteropContext &getInteropContext() { return interopContext; }

/// Returns true if \p VD should be included in a compatibility header for
Expand Down
13 changes: 13 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,3 +1581,16 @@ void DeclAndTypeClangFunctionPrinter::printCustomCxxFunction(
bodyPrinter(typeRefs);
outOfLineOS << "}\n";
}

ClangRepresentation DeclAndTypeClangFunctionPrinter::getTypeRepresentation(
PrimitiveTypeMapping &typeMapping,
SwiftToClangInteropContext &interopContext, DeclAndTypePrinter &declPrinter,
const ModuleDecl *emittedModule, Type ty) {
CFunctionSignatureTypePrinterModifierDelegate delegate;
CFunctionSignatureTypePrinter typePrinter(
llvm::nulls(), llvm::nulls(), typeMapping, OutputLanguageMode::Cxx,
interopContext, delegate, emittedModule, declPrinter,
FunctionSignatureTypeUse::TypeReference);
return typePrinter.visit(ty, OptionalTypeKind::OTK_None,
/*isInOutParam=*/false);
}
6 changes: 6 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ class DeclAndTypeClangFunctionPrinter {
ModuleDecl *emittedModule,
raw_ostream &outOfLineOS);

static ClangRepresentation
getTypeRepresentation(PrimitiveTypeMapping &typeMapping,
SwiftToClangInteropContext &interopContext,
DeclAndTypePrinter &declPrinter,
const ModuleDecl *emittedModule, Type ty);

private:
void printCxxToCFunctionParameterUse(Type type, StringRef name,
const ModuleDecl *moduleContext,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -typecheck %s -typecheck -module-name UseFoundation -enable-experimental-cxx-interop -emit-clang-header-path %t/UseFoundation.h
// RUN: %FileCheck %s < %t/UseFoundation.h

#if canImport(Foundation)

import Foundation

public enum UseFoundationEnum {
case A(Data)
case B
}

#endif

// CHECK-NOT: UseFoundationEnum
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ public indirect enum unsupportedEnumIndirect {
case B
}

@_expose(Cxx) // expected-error {{enum 'unsupportedEnumProtocolType' can not be represented in C++ as one of its cases has an associated value with type that can't be represented in C++}}
public enum unsupportedEnumProtocolType {
case A
case B(Error)
}