Skip to content

[interop][SwiftToCxx] do not emit unsupported function signatures yet #60583

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 3 commits into from
Aug 17, 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
39 changes: 27 additions & 12 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,18 +685,20 @@ class DeclAndTypePrinter::Implementation
.printSwiftABIFunctionSignatureAsCxxFunction(
AFD, methodTy,
/*selfTypeDeclContext=*/typeDeclContext);
if (!funcABI)
return;
owningPrinter.prologueOS << cFuncPrologueOS.str();

DeclAndTypeClangFunctionPrinter declPrinter(os, owningPrinter.prologueOS,
owningPrinter.typeMapping,
owningPrinter.interopContext);
if (auto *accessor = dyn_cast<AccessorDecl>(AFD)) {
declPrinter.printCxxPropertyAccessorMethod(
typeDeclContext, accessor, funcABI.getSymbolName(), resultTy,
typeDeclContext, accessor, funcABI->getSymbolName(), resultTy,
/*isDefinition=*/false);
} else {
declPrinter.printCxxMethod(typeDeclContext, AFD,
funcABI.getSymbolName(), resultTy,
funcABI->getSymbolName(), resultTy,
/*isDefinition=*/false);
}

Expand All @@ -708,11 +710,12 @@ class DeclAndTypePrinter::Implementation
if (auto *accessor = dyn_cast<AccessorDecl>(AFD)) {

defPrinter.printCxxPropertyAccessorMethod(
typeDeclContext, accessor, funcABI.getSymbolName(), resultTy,
typeDeclContext, accessor, funcABI->getSymbolName(), resultTy,
/*isDefinition=*/true);
} else {
defPrinter.printCxxMethod(typeDeclContext, AFD, funcABI.getSymbolName(),
resultTy, /*isDefinition=*/true);
defPrinter.printCxxMethod(typeDeclContext, AFD,
funcABI->getSymbolName(), resultTy,
/*isDefinition=*/true);
}

// FIXME: SWIFT_WARN_UNUSED_RESULT
Expand Down Expand Up @@ -1043,7 +1046,8 @@ class DeclAndTypePrinter::Implementation
}

// Print out the extern C Swift ABI function signature.
FuncionSwiftABIInformation printSwiftABIFunctionSignatureAsCxxFunction(
Optional<FuncionSwiftABIInformation>
printSwiftABIFunctionSignatureAsCxxFunction(
AbstractFunctionDecl *FD, Optional<FunctionType *> givenFuncType = None,
Optional<NominalTypeDecl *> selfTypeDeclContext = None) {
assert(outputLang == OutputLanguageMode::Cxx);
Expand All @@ -1063,13 +1067,16 @@ class DeclAndTypePrinter::Implementation
auto resultTy =
getForeignResultType(FD, funcTy, asyncConvention, errorConvention);

std::string cRepresentationString;
llvm::raw_string_ostream cRepresentationOS(cRepresentationString);

FuncionSwiftABIInformation funcABI(FD);

os << "SWIFT_EXTERN ";
cRepresentationOS << "SWIFT_EXTERN ";

DeclAndTypeClangFunctionPrinter funcPrinter(os, owningPrinter.prologueOS,
owningPrinter.typeMapping,
owningPrinter.interopContext);
DeclAndTypeClangFunctionPrinter funcPrinter(
cRepresentationOS, owningPrinter.prologueOS, owningPrinter.typeMapping,
owningPrinter.interopContext);
auto ABIparams = owningPrinter.interopContext.getIrABIDetails()
.getFunctionABIAdditionalParams(FD);
llvm::SmallVector<DeclAndTypeClangFunctionPrinter::AdditionalParam, 2>
Expand All @@ -1085,10 +1092,16 @@ class DeclAndTypePrinter::Implementation
if (!selfTypeDeclContext && !ABIparams.empty())
convertABIAdditionalParams(FD, resultTy, ABIparams, additionalParams);

funcPrinter.printFunctionSignature(
auto representation = funcPrinter.printFunctionSignature(
FD, funcABI.getSymbolName(), resultTy,
DeclAndTypeClangFunctionPrinter::FunctionSignatureKind::CFunctionProto,
additionalParams);
if (representation.isUnsupported()) {
// FIXME: Emit remark about unemitted declaration.
return None;
}

os << cRepresentationOS.str();
// Swift functions can't throw exceptions, we can only
// throw them from C++ when emitting C++ inline thunks for the Swift
// functions.
Expand Down Expand Up @@ -1371,8 +1384,10 @@ class DeclAndTypePrinter::Implementation
llvm::raw_string_ostream cFuncPrologueOS(cFuncDecl);
auto funcABI = Implementation(cFuncPrologueOS, owningPrinter, outputLang)
.printSwiftABIFunctionSignatureAsCxxFunction(FD);
if (!funcABI)
return;
owningPrinter.prologueOS << cFuncPrologueOS.str();
printAbstractFunctionAsCxxFunctionThunk(FD, funcABI);
printAbstractFunctionAsCxxFunctionThunk(FD, *funcABI);
return;
}
if (FD->getDeclContext()->isTypeContext())
Expand Down
11 changes: 6 additions & 5 deletions lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,12 @@ class ModuleWriter {
if ((superclass = CD->getSuperclassDecl())) {
allRequirementsSatisfied &= require(superclass);
}
for (auto proto : CD->getLocalProtocols(
ConformanceLookupKind::OnlyExplicit))
if (printer.shouldInclude(proto))
allRequirementsSatisfied &= require(proto);

if (outputLangMode != OutputLanguageMode::Cxx) {
for (auto proto :
CD->getLocalProtocols(ConformanceLookupKind::OnlyExplicit))
if (printer.shouldInclude(proto))
allRequirementsSatisfied &= require(proto);
}
if (!allRequirementsSatisfied)
return false;

Expand Down
Loading