From d19c9eb32be42a01a2991a3289675dbdca943595 Mon Sep 17 00:00:00 2001 From: Egor Zhdan Date: Wed, 4 Sep 2024 14:37:44 +0100 Subject: [PATCH] [cxx-interop] Disambiguate template instantiations with SIMD parameters When converting a C++ class template instantiation name into Swift, we previously didn't account for possible SIMD types. Those types were printed as `_`. This meant that e.g. `std::vector` and `std::vector` would get the same Swift name, causing compiler errors down the road. rdar://134214091 --- lib/ClangImporter/ClangClassTemplateNamePrinter.cpp | 6 ++++++ .../Inputs/class-template-with-simd-parameter.h | 12 ++++++++++++ test/Interop/Cxx/templates/Inputs/module.modulemap | 5 +++++ ...mplate-with-simd-parameter-module-interface.swift | 9 +++++++++ 4 files changed, 32 insertions(+) create mode 100644 test/Interop/Cxx/templates/Inputs/class-template-with-simd-parameter.h create mode 100644 test/Interop/Cxx/templates/class-template-with-simd-parameter-module-interface.swift diff --git a/lib/ClangImporter/ClangClassTemplateNamePrinter.cpp b/lib/ClangImporter/ClangClassTemplateNamePrinter.cpp index fad5d3dc73254..19fad0fea8050 100644 --- a/lib/ClangImporter/ClangClassTemplateNamePrinter.cpp +++ b/lib/ClangImporter/ClangClassTemplateNamePrinter.cpp @@ -129,6 +129,12 @@ struct TemplateInstantiationNamePrinter return buffer.str().str(); } + + std::string VisitVectorType(const clang::VectorType *type) { + return (Twine("SIMD") + std::to_string(type->getNumElements()) + "<" + + Visit(type->getElementType().getTypePtr()) + ">") + .str(); + } }; std::string swift::importer::printClassTemplateSpecializationName( diff --git a/test/Interop/Cxx/templates/Inputs/class-template-with-simd-parameter.h b/test/Interop/Cxx/templates/Inputs/class-template-with-simd-parameter.h new file mode 100644 index 0000000000000..3e879a33f21f6 --- /dev/null +++ b/test/Interop/Cxx/templates/Inputs/class-template-with-simd-parameter.h @@ -0,0 +1,12 @@ +#include + +template +struct Templated { + T t; +}; + +typedef Templated TemplatedSIMDUInt1; +typedef Templated TemplatedSIMDUInt16; +typedef Templated TemplatedSIMDFloat3; +typedef Templated TemplatedSIMDFloat4; +typedef Templated TemplatedSIMDDouble8; diff --git a/test/Interop/Cxx/templates/Inputs/module.modulemap b/test/Interop/Cxx/templates/Inputs/module.modulemap index a7fc0b8fbbd99..8fbcea80b696e 100644 --- a/test/Interop/Cxx/templates/Inputs/module.modulemap +++ b/test/Interop/Cxx/templates/Inputs/module.modulemap @@ -18,6 +18,11 @@ module ClassTemplateWithOutOfLineMember { requires cplusplus } +module ClassTemplateWithSIMDParameter { + header "class-template-with-simd-parameter.h" + requires cplusplus +} + module NotPreDefinedClassTemplate { header "not-pre-defined-class-template.h" requires cplusplus diff --git a/test/Interop/Cxx/templates/class-template-with-simd-parameter-module-interface.swift b/test/Interop/Cxx/templates/class-template-with-simd-parameter-module-interface.swift new file mode 100644 index 0000000000000..7c300ab6c5748 --- /dev/null +++ b/test/Interop/Cxx/templates/class-template-with-simd-parameter-module-interface.swift @@ -0,0 +1,9 @@ +// RUN: %target-swift-ide-test -print-module -module-to-print=ClassTemplateWithSIMDParameter -I %S/Inputs -source-filename=x -cxx-interoperability-mode=upcoming-swift | %FileCheck %s + +// REQUIRES: OS=macosx || OS=ios + +// CHECK: typealias TemplatedSIMDUInt1 = Templated +// CHECK: typealias TemplatedSIMDUInt16 = Templated> +// CHECK: typealias TemplatedSIMDFloat3 = Templated> +// CHECK: typealias TemplatedSIMDFloat4 = Templated> +// CHECK: typealias TemplatedSIMDDouble8 = Templated>