Skip to content

[Interop][SwiftToCxx] Support enum creation from Cxx #60842

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
Aug 31, 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
2 changes: 2 additions & 0 deletions lib/PrintAsClang/ClangSyntaxPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using namespace swift;
using namespace cxx_synthesis;

StringRef cxx_synthesis::getCxxSwiftNamespaceName() { return "swift"; }

StringRef cxx_synthesis::getCxxImplNamespaceName() { return "_impl"; }

StringRef cxx_synthesis::getCxxOpaqueStorageClassName() {
Expand Down
3 changes: 3 additions & 0 deletions lib/PrintAsClang/ClangSyntaxPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class NominalTypeDecl;

namespace cxx_synthesis {

/// Return the name of the namespace for things exported from Swift stdlib
StringRef getCxxSwiftNamespaceName();

/// Return the name of the implementation namespace that is used to hide
/// declarations from the namespace that corresponds to the imported Swift
/// module in C++.
Expand Down
384 changes: 267 additions & 117 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion lib/PrintAsClang/ModuleContentsWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,11 @@ class ModuleWriter {
bool writeEnum(const EnumDecl *ED) {
if (addImport(ED))
return true;


if (outputLangMode == OutputLanguageMode::Cxx) {
forwardDeclareMemberTypes(ED->getMembers(), ED);
}

if (seenTypes[ED].first == EmissionState::Defined)
return true;

Expand Down
41 changes: 41 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,3 +824,44 @@ bool DeclAndTypeClangFunctionPrinter::hasKnownOptionalNullableCxxMapping(
}
return false;
}

void DeclAndTypeClangFunctionPrinter::printCustomCxxFunction(
const SmallVector<Type> &neededTypes, PrinterTy retTypeAndNamePrinter,
PrinterTy paramPrinter, bool isConstFunc, PrinterTy bodyPrinter,
ModuleDecl *emittedModule, raw_ostream &outOfLineOS) {
llvm::MapVector<Type, std::string> types;

for (auto &type : neededTypes) {
std::string typeStr;
llvm::raw_string_ostream typeOS(typeStr);
OptionalTypeKind optKind;
Type objectType;
std::tie(objectType, optKind) =
DeclAndTypePrinter::getObjectTypeAndOptionality(
type->getNominalOrBoundGenericNominal(), type);

// Use FunctionSignatureTypeUse::ReturnType to avoid printing extra const or
// references
CFunctionSignatureTypePrinter typePrinter(
typeOS, cPrologueOS, typeMapping, OutputLanguageMode::Cxx,
interopContext, CFunctionSignatureTypePrinterModifierDelegate(),
emittedModule, declPrinter, FunctionSignatureTypeUse::ReturnType);
typePrinter.visit(objectType, optKind, /* isInOutParam */ false);

types.insert({type, typeStr});
}

retTypeAndNamePrinter(types);
os << '(';
outOfLineOS << '(';
paramPrinter(types);
os << ')';
outOfLineOS << ')';
if (isConstFunc) {
os << " const;\n";
outOfLineOS << " const";
}
outOfLineOS << " {\n";
bodyPrinter(types);
outOfLineOS << "}\n";
}
11 changes: 11 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/Basic/LLVM.h"
#include "swift/ClangImporter/ClangImporter.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
Expand Down Expand Up @@ -140,6 +141,16 @@ class DeclAndTypeClangFunctionPrinter {
ModuleDecl *moduleContext,
OutputLanguageMode outputLang);

using PrinterTy =
llvm::function_ref<void(llvm::MapVector<Type, std::string> &)>;

/// Print generated C++ helper function
void printCustomCxxFunction(const SmallVector<Type> &neededTypes,
PrinterTy retTypeAndNamePrinter,
PrinterTy paramPrinter, bool isConstFunc,
PrinterTy bodyPrinter, ModuleDecl *emittedModule,
raw_ostream &outOfLineOS);

private:
void printCxxToCFunctionParameterUse(
Type type, StringRef name, const ModuleDecl *moduleContext, bool isInOut,
Expand Down
40 changes: 12 additions & 28 deletions lib/PrintAsClang/PrintClangValueType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,23 @@ void ClangValueTypePrinter::printValueTypeDecl(
"metadata._0);\n";
os << " return _getOpaquePointer();\n";
os << " }\n";
os << " inline void _destructiveInjectEnumTag(unsigned tag) {\n";
printEnumVWTableVariable();
os << " enumVWTable->destructiveInjectEnumTag(_getOpaquePointer(), tag, "
"metadata._0);\n";
os << " }\n";
os << " inline unsigned _getEnumTag() const {\n";
printEnumVWTableVariable();
os << " return enumVWTable->getEnumTag(_getOpaquePointer(), "
"metadata._0);\n";
os << " }\n";

for (const auto &pair : interopContext.getIrABIDetails().getEnumTagMapping(
cast<EnumDecl>(typeDecl))) {
os << " using _impl_" << pair.first->getNameStr() << " = decltype(";
ClangSyntaxPrinter(os).printIdentifier(pair.first->getNameStr());
os << ");\n";
}
}
// Print out the storage for the value type.
os << " ";
Expand All @@ -290,34 +302,6 @@ void ClangValueTypePrinter::printValueTypeDecl(
printCxxImplClassName(os, typeDecl);
os << ";\n";
os << "};\n";
// Print the definition of enum static struct data memebers
if (isa<EnumDecl>(typeDecl)) {
auto tagMapping = interopContext.getIrABIDetails().getEnumTagMapping(
cast<EnumDecl>(typeDecl));
for (const auto &pair : tagMapping) {
os << "decltype(";
printer.printBaseName(typeDecl);
os << "::";
printer.printIdentifier(pair.first->getNameStr());
os << ") ";
printer.printBaseName(typeDecl);
os << "::";
printer.printIdentifier(pair.first->getNameStr());
os << ";\n";
}
if (isOpaqueLayout) {
os << "decltype(";
printer.printBaseName(typeDecl);
// TODO: allow custom name for this special case
os << "::";
printer.printIdentifier("unknownDefault");
os << ") ";
printer.printBaseName(typeDecl);
os << "::";
printer.printIdentifier("unknownDefault");
os << ";\n";
}
}
os << '\n';

const auto *moduleContext = typeDecl->getModuleContext();
Expand Down
44 changes: 39 additions & 5 deletions test/Interop/SwiftToCxx/enums/resilient-enum-in-cxx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ public func printFoo(_ x: Foo) {
print(x)
}

public enum Empty {

}

// CHECK: // Tags for resilient enum Empty
// CHECK-NEXT: extern "C" {
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace _impl
// CHECK-EMPTY:
// CHECK-NEXT: class Empty final {
// CHECK: enum class cases {
// CHECK-NEXT: unknownDefault
// CHECK-NEXT: };
// CHECK: inline const static struct { // impl struct for case unknownDefault
// CHECK-NEXT: inline constexpr operator cases() const {
// CHECK-NEXT: return cases::unknownDefault;
// CHECK-NEXT: }
// CHECK-NEXT: } unknownDefault;
// CHECK-NEXT: inline bool isUnknownDefault() const;
// CHECK: inline operator cases() const {
// CHECK-NEXT: return cases::unknownDefault;
// CHECK-NEXT: }
// CHECK: // Tags for resilient enum Foo
// CHECK-NEXT: extern "C" {
// CHECK-NEXT: extern unsigned $s5Enums3FooO1ayACSdcACmFWC;
Expand All @@ -47,17 +70,28 @@ public func printFoo(_ x: Foo) {
// NEW_CASE-NEXT: b,
// CHECK-NEXT: unknownDefault
// CHECK-NEXT: }
// CHECK: static struct { // impl struct for case unknownDefault
// CHECK-NEXT: constexpr operator cases() const {
// CHECK: inline const static struct { // impl struct for case unknownDefault
// CHECK-NEXT: inline constexpr operator cases() const {
// CHECK-NEXT: return cases::unknownDefault;
// CHECK-NEXT: }
// CHECK-NEXT: } unknownDefault;
// CHECK-NEXT: inline bool isUnknownDefault() const {
// CHECK-NEXT: return *this == Foo::unknownDefault;
// CHECK-NEXT: }
// CHECK-NEXT: inline bool isUnknownDefault() const;
// CHECK-EMPTY:
// CHECK: inline operator cases() const {
// CHECK-NEXT: auto tag = _getEnumTag();
// CHECK-NEXT: if (tag == _impl::$s5Enums3FooO1ayACSdcACmFWC) return cases::a;
// NEW_CASE-NEXT: if (tag == _impl::$s5Enums3FooO1byACSicACmFWC) return cases::b;
// CHECK-NEXT: return cases::unknownDefault;
// CHECK-NEXT: }
// CHECK: inline Foo Foo::_impl_a::operator()(double val) const {
// CHECK-NEXT: auto result = Foo::_make();
// CHECK-NEXT: memcpy(result._getOpaquePointer(), &val, sizeof(val));
// CHECK-NEXT: result._destructiveInjectEnumTag(_impl::$s5Enums3FooO1ayACSdcACmFWC);
// CHECK-NEXT: return result;
// CHECK-NEXT: }
// NEW_CASE: inline Foo Foo::_impl_b::operator()(swift::Int val) const {
// NEW_CASE-NEXT: auto result = Foo::_make();
// NEW_CASE-NEXT: memcpy(result._getOpaquePointer(), &val, sizeof(val));
// NEW_CASE-NEXT: result._destructiveInjectEnumTag(_impl::$s5Enums3FooO1byACSicACmFWC);
// NEW_CASE-NEXT: return result;
// NEW_CASE-NEXT: }

This file was deleted.

Loading