Files
swift-mirror/test/Interop/Cxx/namespace/templates-module-interface.swift
Egor Zhdan 041005af7c [cxx-interop] Use more correct type names in C++ template parameters
When importing a C++ class template instantiation, Swift translates the template parameter type names from C++ into their Swift equivalent.

For instance, `basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>` gets imported as `basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>`: `wchar_t` is imported as `CWideChar`, which is a typealias for `Scalar` on most platforms including Darwin. Notice that Swift goes through the `CWideChar` typealias on the specific platform. Another instantiation `basic_string<uint32_t, char_traits<uint32_t>, allocator<uint32_t>>` also gets imported as `basic_string<Scalar, char_traits<Scalar>, allocator<Scalar>>`: `uint32_t` is also imported as `Scalar`. This is problematic because we have two distinct C++ types that have the same name in Swift.

This change makes sure Swift doesn't go through typealiases when emitting names of template parameters, so `wchar_t` would now get printed as `CWideChar`, `int` would get printed as `CInt`, etc.

This also encourages clients to use the correct type (`CInt`, `CWideChar`, etc) instead of relying on platform-specific typealiases.

rdar://115673622
2023-10-09 14:57:10 +01:00

65 lines
3.9 KiB
Swift

// RUN: %target-swift-ide-test -print-module -module-to-print=Templates -I %S/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
// CHECK: enum TemplatesNS1 {
// CHECK-NEXT: enum TemplatesNS2 {
// CHECK-NEXT: static func forwardDeclaredFunctionTemplate<T>(_: T) -> UnsafePointer<CChar>!
// CHECK-NEXT: struct ForwardDeclaredClassTemplate<CChar> {
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct ForwardDeclaredClassTemplate<> {
// CHECK-NEXT: }
// CHECK-NEXT: static func forwardDeclaredFunctionTemplateOutOfLine<T>(_: T) -> UnsafePointer<CChar>!
// CHECK-NEXT: struct ForwardDeclaredClassTemplateOutOfLine<CChar> {
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct ForwardDeclaredClassTemplateOutOfLine<> {
// CHECK-NEXT: }
// CHECK-NEXT: typealias BasicClassTemplateChar = TemplatesNS1.TemplatesNS3.BasicClassTemplate<CChar>
// CHECK-NEXT: static func takesClassTemplateFromSibling(_: TemplatesNS1.TemplatesNS2.BasicClassTemplateChar) -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: static func basicFunctionTemplate<T>(_: T) -> UnsafePointer<CChar>!
// CHECK-NEXT: struct BasicClassTemplate<CChar> {
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct BasicClassTemplate<> {
// CHECK-NEXT: }
// CHECK-NEXT: typealias BasicClassTemplateChar = TemplatesNS1.BasicClassTemplate<CChar>
// CHECK-NEXT: static func basicFunctionTemplateDefinedInDefs<T>(_: T) -> UnsafePointer<CChar>!
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct BasicClassTemplateDefinedInDefs<> {
// CHECK-NEXT: }
// CHECK-NEXT: typealias UseTemplate = TemplatesNS4.HasSpecialization<CChar>
// CHECK-NEXT: typealias UseSpecialized = TemplatesNS4.HasSpecialization<CInt>
// CHECK-NEXT: enum TemplatesNS3 {
// CHECK-NEXT: struct BasicClassTemplate<CChar> {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct BasicClassTemplate<> {
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: struct ForwardDeclaredClassTemplate<CChar> {
// CHECK-NEXT: init()
// CHECK-NEXT: mutating func basicMember() -> UnsafePointer<CChar>!
// CHECK-NEXT: }
// CHECK-NEXT: typealias ForwardDeclaredClassTemplateChar = TemplatesNS1.TemplatesNS2.ForwardDeclaredClassTemplate<CChar>
// CHECK-NEXT: }
// CHECK-NEXT: typealias ForwardDeclaredClassTemplateOutOfLineChar = TemplatesNS1.TemplatesNS2.ForwardDeclaredClassTemplateOutOfLine<CChar>
// CHECK-NEXT: enum TemplatesNS4 {
// CHECK-NEXT: struct HasSpecialization<CChar> {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: struct HasSpecialization<CInt> {
// CHECK-NEXT: init()
// CHECK-NEXT: }
// CHECK-NEXT: @available(*, unavailable, message: "Un-specialized class templates are not currently supported. Please use a specialization of this type.")
// CHECK-NEXT: struct HasSpecialization<> {
// CHECK-NEXT: }
// CHECK-NEXT: }