Skip to content

[interop][SwiftToCxx] add additional type representation emission che… #65397

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
Apr 28, 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
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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just going by the comment, is there any code duplication between the checks done in getDeclRepresentation and the checks here. Is there an opportunity to factor this out to a common helper function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes as a follow-up

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