Skip to content

[cxx-interop] Fix the printing of types with generic arguments #75372

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 22, 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
45 changes: 30 additions & 15 deletions lib/PrintAsClang/DeclAndTypePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@
#include "swift/Parse/Lexer.h"
#include "swift/Parse/Parser.h"

#include "SwiftToClangInteropContext.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/CharInfo.h"
#include "clang/Basic/SourceManager.h"
#include "SwiftToClangInteropContext.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;
using namespace swift::objc_translation;
Expand Down Expand Up @@ -561,8 +562,8 @@ class DeclAndTypePrinter::Implementation
outOfLineOS << " return swift::";
outOfLineOS << cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl,
owningPrinter.printTypeName(
outOfLineOS, paramType,
elementDecl->getParentEnum()->getModuleContext());
outOfLineOS << ">::type";
if (!isOptional && isa<ClassDecl>(objectTypeDecl)) {
Expand All @@ -574,8 +575,8 @@ class DeclAndTypePrinter::Implementation
outOfLineOS << " swift::"
<< cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl,
owningPrinter.printTypeName(
outOfLineOS, paramType,
elementDecl->getParentEnum()->getModuleContext());
outOfLineOS << ">::type";
outOfLineOS << "::initializeWithTake(result, "
Expand Down Expand Up @@ -729,31 +730,36 @@ class DeclAndTypePrinter::Implementation
objectTypeDecl =
paramType->getNominalOrBoundGenericNominal();
outOfLineOS << " alignas(";
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
owningPrinter.printTypeName(
outOfLineOS, paramType,
elementDecl->getParentEnum()->getModuleContext());
outOfLineOS << ") unsigned char buffer[sizeof(";
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
owningPrinter.printTypeName(
outOfLineOS, paramType,
elementDecl->getParentEnum()->getModuleContext());
outOfLineOS << ")];\n";
outOfLineOS << " auto *valCopy = new(buffer) ";
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
owningPrinter.printTypeName(
outOfLineOS, paramType,
elementDecl->getParentEnum()->getModuleContext());
outOfLineOS << "(val);\n";
outOfLineOS << " ";
outOfLineOS << cxx_synthesis::getCxxSwiftNamespaceName()
<< "::";
outOfLineOS << cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
owningPrinter.printTypeName(
outOfLineOS, paramType,
elementDecl->getParentEnum()->getModuleContext());
outOfLineOS << ">::type::initializeWithTake(result._"
"getOpaquePointer(), ";
outOfLineOS << cxx_synthesis::getCxxSwiftNamespaceName()
<< "::";
outOfLineOS << cxx_synthesis::getCxxImplNamespaceName();
outOfLineOS << "::implClassFor<";
outOfLineSyntaxPrinter.printNominalTypeReference(
objectTypeDecl, ED->getModuleContext());
owningPrinter.printTypeName(
outOfLineOS, paramType,
elementDecl->getParentEnum()->getModuleContext());
outOfLineOS << ">::type::getOpaquePointer(*valCopy)";
outOfLineOS << ");\n";
}
Expand Down Expand Up @@ -2958,6 +2964,15 @@ void DeclAndTypePrinter::print(Type ty) {
getImpl().print(ty, /*overridingOptionality*/ std::nullopt);
}

void DeclAndTypePrinter::printTypeName(raw_ostream &os, Type ty,
const ModuleDecl *moduleContext) {
std::string dummy;
llvm::raw_string_ostream dummyOS(dummy);
DeclAndTypeClangFunctionPrinter printer(os, dummyOS, typeMapping,
interopContext, *this);
printer.printTypeName(ty, moduleContext);
}

void DeclAndTypePrinter::printAvailability(raw_ostream &os, const Decl *D) {
getImpl().printAvailability(os, D);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/PrintAsClang/DeclAndTypePrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "OutputLanguageMode.h"

#include "swift/AST/Decl.h"
#include "swift/AST/Module.h"
#include "swift/AST/Type.h"
// for OptionalTypeKind
#include "swift/ClangImporter/ClangImporter.h"
Expand Down Expand Up @@ -117,6 +118,9 @@ class DeclAndTypePrinter {
void print(const Decl *D);
void print(Type ty);

/// Prints the name of the type including generic arguments.
void printTypeName(raw_ostream &os, Type ty, const ModuleDecl *moduleContext);

void printAvailability(raw_ostream &os, const Decl *D);

/// Is \p ED empty of members and protocol conformances to include?
Expand Down
10 changes: 10 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1760,3 +1760,13 @@ ClangRepresentation DeclAndTypeClangFunctionPrinter::getTypeRepresentation(
return typePrinter.visit(ty, OptionalTypeKind::OTK_None,
/*isInOutParam=*/false);
}

void DeclAndTypeClangFunctionPrinter::printTypeName(
Type ty, const ModuleDecl *moduleContext) {
CFunctionSignatureTypePrinterModifierDelegate delegate;
CFunctionSignatureTypePrinter typePrinter(
os, cPrologueOS, typeMapping, OutputLanguageMode::Cxx, interopContext,
delegate, moduleContext, declPrinter,
FunctionSignatureTypeUse::TypeReference);
typePrinter.visit(ty, std::nullopt, /*isInOut=*/false);
}
3 changes: 3 additions & 0 deletions lib/PrintAsClang/PrintClangFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ class DeclAndTypeClangFunctionPrinter {
DeclAndTypePrinter &declPrinter,
const ModuleDecl *emittedModule, Type ty);

/// Prints the name of the type including generic arguments.
void printTypeName(Type ty, const ModuleDecl *moduleContext);

private:
void printCxxToCFunctionParameterUse(Type type, StringRef name,
const ModuleDecl *moduleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ int main() {
assert(c.getX() == 5678);
}

{
auto c = C::init(5);
auto arr = swift::Array<C>::init(c, 2);
auto f = F::b(arr);
assert(f.getB().getCount() == 2);
}

{
auto arr = swift::Array<swift::Int>::init(42, 2);
auto g = G<swift::Int>::b(arr);
assert(g.getB().getCount() == 2);
}

assert(getRetainCount(c) == 1);
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ extension E {
}
}

public enum F {
case a(Int)
case b([C])
}

public enum G<T> {
case a(Int)
case b([T])
}

// CHECK: SWIFT_INLINE_THUNK E E::_impl_c::operator()(const C& val) const {
// CHECK-NEXT: auto result = E::_make();
// CHECK-NEXT: auto op = swift::_impl::_impl_RefCountedClass::copyOpaquePointer(val);
Expand All @@ -47,3 +57,23 @@ extension E {

// CHECK: SWIFT_INLINE_THUNK bool E::matchesIntValue(swift::Int value) const {
// CHECK-NEXT: return _impl::$s5Enums1EO15matchesIntValueySbSiF(value, _impl::swift_interop_passDirect_Enums_uint64_t_0_8_uint8_t_8_9(_getOpaquePointer()));

// CHECK: SWIFT_INLINE_THUNK swift::Array<C> F::getB() const {
// CHECK-NEXT: if (!isB()) abort();
// CHECK-NEXT: alignas(F) unsigned char buffer[sizeof(F)];
// CHECK-NEXT: auto *thisCopy = new(buffer) F(*this);
// CHECK-NEXT: char * _Nonnull payloadFromDestruction = thisCopy->_destructiveProjectEnumData();
// CHECK-NEXT: return swift::_impl::implClassFor<swift::Array<C>>::type::returnNewValue([&](char * _Nonnull result) SWIFT_INLINE_THUNK_ATTRIBUTES {
// CHECK-NEXT: swift::_impl::implClassFor<swift::Array<C>>::type::initializeWithTake(result, payloadFromDestruction);
// CHECK-NEXT: });
// CHECK-NEXT: }

// CHECK: SWIFT_INLINE_THUNK swift::Array<T_0_0> G<T_0_0>::getB() const {
// CHECK-NEXT: if (!isB()) abort();
// CHECK-NEXT: alignas(G) unsigned char buffer[sizeof(G)];
// CHECK-NEXT: auto *thisCopy = new(buffer) G(*this);
// CHECK-NEXT: char * _Nonnull payloadFromDestruction = thisCopy->_destructiveProjectEnumData();
// CHECK-NEXT: return swift::_impl::implClassFor<swift::Array<T_0_0>>::type::returnNewValue([&](char * _Nonnull result) SWIFT_INLINE_THUNK_ATTRIBUTES {
// CHECK-NEXT: swift::_impl::implClassFor<swift::Array<T_0_0>>::type::initializeWithTake(result, payloadFromDestruction);
// CHECK-NEXT: });
// CHECK-NEXT: }