Skip to content

🍒[cxx-interop] Mangle numeric template arguments #67126

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 6, 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
71 changes: 45 additions & 26 deletions lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,37 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
return importNameImpl(classTemplateSpecDecl->getSpecializedTemplate(),
version, givenName);
if (!isa<clang::ClassTemplatePartialSpecializationDecl>(D)) {
auto getSwiftBuiltinTypeName =
[&](const clang::BuiltinType *builtin) -> std::optional<StringRef> {
Type swiftType = nullptr;
switch (builtin->getKind()) {
case clang::BuiltinType::Void:
swiftType = swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(),
"Void");
break;
#define MAP_BUILTIN_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
swiftType = swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), \
#SWIFT_TYPE_NAME); \
break;
#define MAP_BUILTIN_CCHAR_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
swiftType = swiftCtx.getNamedSwiftType(swiftCtx.getStdlibModule(), \
#SWIFT_TYPE_NAME); \
break;
#include "swift/ClangImporter/BuiltinMappedTypes.def"
default:
break;
}

if (swiftType) {
if (auto nominal = swiftType->getAs<NominalType>()) {
return nominal->getDecl()->getNameStr();
}
}
return std::nullopt;
};

// When constructing the name of a C++ template, don't expand all the
// template, only expand one layer. Here we want to prioritize
// readability over total completeness.
Expand All @@ -2204,38 +2235,16 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
buffer << "<";
llvm::interleaveComma(classTemplateSpecDecl->getTemplateArgs().asArray(),
buffer,
[&buffer, this, version](const clang::TemplateArgument& arg) {
[&buffer, this, version, &getSwiftBuiltinTypeName](const clang::TemplateArgument& arg) {
// Use import name here so builtin types such as "int" map to their
// Swift equivalent ("Int32").
if (arg.getKind() == clang::TemplateArgument::Type) {
auto ty = arg.getAsType().getTypePtr();
if (auto builtin = dyn_cast<clang::BuiltinType>(ty)) {
auto &ctx = swiftCtx;
Type swiftType = nullptr;
switch (builtin->getKind()) {
case clang::BuiltinType::Void:
swiftType = ctx.getNamedSwiftType(ctx.getStdlibModule(), "Void");
break;
#define MAP_BUILTIN_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
swiftType = ctx.getNamedSwiftType(ctx.getStdlibModule(), \
#SWIFT_TYPE_NAME); \
break;
#define MAP_BUILTIN_CCHAR_TYPE(CLANG_BUILTIN_KIND, SWIFT_TYPE_NAME) \
case clang::BuiltinType::CLANG_BUILTIN_KIND: \
swiftType = ctx.getNamedSwiftType(ctx.getStdlibModule(), \
#SWIFT_TYPE_NAME); \
break;
#include "swift/ClangImporter/BuiltinMappedTypes.def"
default:
break;
}

if (swiftType) {
if (auto nominal = dyn_cast<NominalType>(swiftType->getCanonicalType())) {
buffer << nominal->getDecl()->getNameStr();
return;
}
if (auto swiftTypeName = getSwiftBuiltinTypeName(builtin)) {
buffer << *swiftTypeName;
return;
}
} else {
// FIXME: Generalize this to cover pointer to
Expand Down Expand Up @@ -2276,6 +2285,16 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
return;
}
}
} else if (arg.getKind() == clang::TemplateArgument::Integral) {
buffer << "_";
if (arg.getIntegralType()->isBuiltinType()) {
if (auto swiftTypeName = getSwiftBuiltinTypeName(
arg.getIntegralType()->getAs<clang::BuiltinType>())) {
buffer << *swiftTypeName << "_";
}
}
arg.getAsIntegral().print(buffer, true);
return;
}
buffer << "_";
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ using size_t = __SIZE_TYPE__;
template <class T, size_t Size> struct MagicArray { T t[Size]; };

typedef MagicArray<int, 2> MagicIntPair;
typedef MagicArray<int, 3> MagicIntTriple;

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-experimental-cxx-interop | %FileCheck %s

import ClassTemplateNonTypeParameter

let p = MagicIntPair()
let t = MagicIntTriple()

// CHECK: @"${{s4main1pSo0034MagicArrayInt32_UInt_2_zoAFhhiEngcVvp|s4main1pSo0036MagicArrayInt32_UInt64_2_JsAEiFiuomcVvp}}"
// CHECK: @"${{s4main1tSo0034MagicArrayInt32_UInt_3_zoAFhhiEngcVvp|s4main1tSo0036MagicArrayInt32_UInt64_3_JsAEiFiuomcVvp}}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-ide-test -print-module -module-to-print=ClassTemplateNonTypeParameter -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s

// CHECK: struct MagicArray<T, Size> {
// CHECK: }

// CHECK: struct MagicArray<Int32, _UInt{{.*}}_2> {
// CHECK: init()
// CHECK: init(t: (Int32, Int32))
// CHECK: var t: (Int32, Int32)
// CHECK: }

// CHECK: struct MagicArray<Int32, _UInt{{.*}}_3> {
// CHECK: init()
// CHECK: init(t: (Int32, Int32, Int32))
// CHECK: var t: (Int32, Int32, Int32)
// CHECK: }

// CHECK: typealias MagicIntPair = MagicArray<Int32, _UInt{{.*}}_2>
// CHECK: typealias MagicIntTriple = MagicArray<Int32, _UInt{{.*}}_3>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var TemplatesTestSuite = TestSuite("TemplatesTestSuite")
TemplatesTestSuite.test("typedeffed-non-type-parameter") {
let pair = MagicIntPair(t: (1, 2))
expectEqual(pair.t, (1, 2))

let triple = MagicIntTriple(t: (1, 2, 3))
expectEqual(triple.t, (1, 2, 3))
}

// TODO: This test doesn't work because Swift doesn't support defaulted generic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

// TODO: we should not be importing functions that use this type in their
// signature (such as the function below).
// CHECK: mutating func test1() -> RegressionTest.ValExpr<SliceExpr<SliceExpr<Array<Int32>, _>, _>>
// CHECK: mutating func test1() -> RegressionTest.ValExpr<SliceExpr<SliceExpr<Array<Int32>, _Int32_1>, _Int32_1>>