Skip to content

[cxx-interop] Disambiguate template instantiations with parameter packs #77450

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
Nov 11, 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
86 changes: 59 additions & 27 deletions lib/ClangImporter/ClangClassTemplateNamePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "ClangClassTemplateNamePrinter.h"
#include "ImporterImpl.h"
#include "clang/AST/TemplateArgumentVisitor.h"
#include "clang/AST/TypeVisitor.h"

using namespace swift;
Expand Down Expand Up @@ -150,41 +151,72 @@ struct TemplateInstantiationNamePrinter
}
};

struct TemplateArgumentPrinter
: clang::ConstTemplateArgumentVisitor<TemplateArgumentPrinter, void,
llvm::raw_svector_ostream &> {
TemplateInstantiationNamePrinter typePrinter;

TemplateArgumentPrinter(ASTContext &swiftCtx, NameImporter *nameImporter,
ImportNameVersion version)
: typePrinter(swiftCtx, nameImporter, version) {}

void VisitTemplateArgument(const clang::TemplateArgument &arg,
llvm::raw_svector_ostream &buffer) {
// Print "_" as a fallback if we couldn't emit a more meaningful type name.
buffer << "_";
}

void VisitTypeTemplateArgument(const clang::TemplateArgument &arg,
llvm::raw_svector_ostream &buffer) {
auto ty = arg.getAsType();
buffer << typePrinter.Visit(ty.getTypePtr());
if (ty.isConstQualified()) {
buffer << "_const";
}
}

void VisitIntegralTemplateArgument(const clang::TemplateArgument &arg,
llvm::raw_svector_ostream &buffer) {
buffer << "_";
if (arg.getIntegralType()->isBuiltinType()) {
buffer << typePrinter.Visit(arg.getIntegralType().getTypePtr()) << "_";
}
arg.getAsIntegral().print(buffer, true);
}

void VisitPackTemplateArgument(const clang::TemplateArgument &arg,
llvm::raw_svector_ostream &buffer) {
VisitTemplateArgumentArray(arg.getPackAsArray(), buffer);
}

void VisitTemplateArgumentArray(ArrayRef<clang::TemplateArgument> args,
llvm::raw_svector_ostream &buffer) {
bool needsComma = false;
for (auto &arg : args) {
// Do not try to print empty packs.
if (arg.getKind() == clang::TemplateArgument::ArgKind::Pack &&
arg.getPackAsArray().empty())
continue;

if (needsComma)
buffer << ", ";
Visit(arg, buffer);
needsComma = true;
}
}
Comment on lines +187 to +206
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This bit is the new functionality, the rest is just refactoring.

};

std::string swift::importer::printClassTemplateSpecializationName(
const clang::ClassTemplateSpecializationDecl *decl, ASTContext &swiftCtx,
NameImporter *nameImporter, ImportNameVersion version) {
TemplateInstantiationNamePrinter templateNamePrinter(swiftCtx, nameImporter,
version);
TemplateArgumentPrinter templateArgPrinter(swiftCtx, nameImporter, version);

// TODO: the following logic should probably be a ConstTemplateArgumentVisitor
llvm::SmallString<128> storage;
llvm::raw_svector_ostream buffer(storage);
decl->printName(buffer);
buffer << "<";
llvm::interleaveComma(
decl->getTemplateArgs().asArray(), buffer,
[&buffer, &templateNamePrinter](const clang::TemplateArgument &arg) {
// Use import name here so builtin types such as "int" map to their
// Swift equivalent ("CInt").
if (arg.getKind() == clang::TemplateArgument::Type) {
auto ty = arg.getAsType();
buffer << templateNamePrinter.Visit(ty.getTypePtr());
if (ty.isConstQualified()) {
buffer << "_const";
}
return;
} else if (arg.getKind() == clang::TemplateArgument::Integral) {
buffer << "_";
if (arg.getIntegralType()->isBuiltinType()) {
buffer << templateNamePrinter.Visit(
arg.getIntegralType().getTypePtr())
<< "_";
}
arg.getAsIntegral().print(buffer, true);
return;
}
buffer << "_";
});
templateArgPrinter.VisitTemplateArgumentArray(
decl->getTemplateArgs().asArray(), buffer);
buffer << ">";
return buffer.str().str();
}
3 changes: 3 additions & 0 deletions test/Interop/Cxx/templates/Inputs/class-template-variadic.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ struct IntWrapper {
int getValue() const { return value; }
};

typedef Tuple<IntWrapper> Single;
typedef Tuple<IntWrapper, IntWrapper> Pair;
typedef Tuple<IntWrapper, IntWrapper, IntWrapper> Triple;
typedef Tuple<Tuple<IntWrapper, IntWrapper>, Tuple<IntWrapper, IntWrapper>> Nested;

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_VARIADIC_H
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=ClassTemplateInNamespace -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s

// CHECK: enum Space {
// CHECK: struct Ship<_> {
// CHECK: init()
// CHECK: }
// CHECK: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK: struct Ship<> {
// CHECK: }
// CHECK: typealias Orbiter = Space.Ship<_>
// CHECK: typealias Orbiter = Space.Ship<((CBool) -> Void)>
// CHECK: }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=ClassTemplateVariadic -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
// RUN: %target-swift-ide-test -print-module -module-to-print=ClassTemplateVariadic -I %S/Inputs -source-filename=x -cxx-interoperability-mode=swift-6 | %FileCheck %s
// RUN: %target-swift-ide-test -print-module -module-to-print=ClassTemplateVariadic -I %S/Inputs -source-filename=x -cxx-interoperability-mode=upcoming-swift | %FileCheck %s

// CHECK: @available(*, unavailable
// CHECK: struct Tuple<Ts> {
// CHECK: }

// CHECK: typealias Single = Tuple<IntWrapper>
// CHECK: typealias Pair = Tuple<IntWrapper, IntWrapper>
// CHECK: typealias Triple = Tuple<IntWrapper, IntWrapper, IntWrapper>
// CHECK: typealias Nested = Tuple<Tuple<IntWrapper, IntWrapper>, Tuple<IntWrapper, IntWrapper>>