Skip to content

Commit aa6804a

Browse files
committed
[cxx-interop] Disambiguate template instantiations with array type parameters
When Swift imports C++ template class instantiations, it generates a human-readable Swift name for each instantiation. Having name collisions causes multiple Swift type with the same name, which confuses the compiler. `MyClass<int[]>` and `MyClass<long[]>` were both being imported as `MyClass<_>` into Swift. This patch fixes that: * `MyClass<int[]>` is now imported as `MyClass<[CInt]>` * `MyClass<int[123]>` is now imported as `MyClass<Vector<CInt, 123>>` rdar://138921102
1 parent ffb22d7 commit aa6804a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

lib/ClangImporter/ClangClassTemplateNamePrinter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ struct TemplateInstantiationNamePrinter
137137
Visit(type->getElementType().getTypePtr()) + ">")
138138
.str();
139139
}
140+
141+
std::string VisitArrayType(const clang::ArrayType *type) {
142+
return (Twine("[") + Visit(type->getElementType().getTypePtr()) + "]")
143+
.str();
144+
}
145+
146+
std::string VisitConstantArrayType(const clang::ConstantArrayType *type) {
147+
return (Twine("Vector<") + Visit(type->getElementType().getTypePtr()) +
148+
", " + std::to_string(type->getSExtSize()) + ">")
149+
.str();
150+
}
140151
};
141152

142153
std::string swift::importer::printClassTemplateSpecializationName(

test/Interop/Cxx/templates/Inputs/class-template-with-primitive-argument.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,19 @@ typedef MagicWrapper<const long> WrappedMagicLongConst;
1919
typedef MagicWrapper<int*> WrappedMagicIntPtr;
2020
typedef MagicWrapper<const int*> WrappedMagicIntConstPtr;
2121
typedef MagicWrapper<int**> WrappedMagicIntPtrPtr;
22+
typedef MagicWrapper<int[]> WrappedMagicIntArr;
23+
typedef MagicWrapper<long[]> WrappedMagicLongArr;
24+
typedef MagicWrapper<int[123]> WrappedMagicIntFixedSizeArr1;
25+
typedef MagicWrapper<int[124]> WrappedMagicIntFixedSizeArr2;
2226

2327
typedef DoubleWrapper<MagicWrapper<int>> DoubleWrappedInt;
2428
typedef DoubleWrapper<MagicWrapper<const int>> DoubleWrappedIntConst;
2529
typedef DoubleWrapper<MagicWrapper<const long>> DoubleWrappedLongConst;
2630
typedef DoubleWrapper<MagicWrapper<int*>> DoubleWrappedIntPtr;
2731
typedef DoubleWrapper<MagicWrapper<const int*>> DoubleWrappedIntConstPtr;
32+
typedef DoubleWrapper<MagicWrapper<int[]>> DoubleWrappedMagicIntArr;
33+
typedef DoubleWrapper<MagicWrapper<long[]>> DoubleWrappedMagicLongArr;
34+
typedef DoubleWrapper<MagicWrapper<int[42]>> DoubleWrappedMagicIntFixedSizeArr1;
35+
typedef DoubleWrapper<MagicWrapper<int[43]>> DoubleWrappedMagicIntFixedSizeArr2;
2836

2937
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_WITH_PRIMITIVE_ARGUMENT_H

test/Interop/Cxx/templates/class-template-with-primitive-argument-module-interface.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313
// CHECK: typealias WrappedMagicIntPtr = MagicWrapper<UnsafeMutablePointer<CInt>>
1414
// CHECK: typealias WrappedMagicIntConstPtr = MagicWrapper<UnsafePointer<CInt>>
1515
// CHECK: typealias WrappedMagicIntPtrPtr = MagicWrapper<UnsafeMutablePointer<UnsafeMutablePointer<CInt>>>
16+
// CHECK: typealias WrappedMagicIntArr = MagicWrapper<[CInt]>
17+
// CHECK: typealias WrappedMagicLongArr = MagicWrapper<[CLong]>
18+
// CHECK: typealias WrappedMagicIntFixedSizeArr1 = MagicWrapper<Vector<CInt, 123>>
19+
// CHECK: typealias WrappedMagicIntFixedSizeArr2 = MagicWrapper<Vector<CInt, 124>>
1620

1721
// CHECK: typealias DoubleWrappedInt = DoubleWrapper<MagicWrapper<CInt>>
1822
// CHECK: typealias DoubleWrappedIntConst = DoubleWrapper<MagicWrapper<CInt_const>>
1923
// CHECK: typealias DoubleWrappedLongConst = DoubleWrapper<MagicWrapper<CLong_const>>
2024
// CHECK: typealias DoubleWrappedIntPtr = DoubleWrapper<MagicWrapper<UnsafeMutablePointer<CInt>>>
2125
// CHECK: typealias DoubleWrappedIntConstPtr = DoubleWrapper<MagicWrapper<UnsafePointer<CInt>>>
26+
// CHECK: typealias DoubleWrappedMagicIntArr = DoubleWrapper<MagicWrapper<[CInt]>>
27+
// CHECK: typealias DoubleWrappedMagicLongArr = DoubleWrapper<MagicWrapper<[CLong]>>
28+
// CHECK: typealias DoubleWrappedMagicIntFixedSizeArr1 = DoubleWrapper<MagicWrapper<Vector<CInt, 42>>>
29+
// CHECK: typealias DoubleWrappedMagicIntFixedSizeArr2 = DoubleWrapper<MagicWrapper<Vector<CInt, 43>>>

0 commit comments

Comments
 (0)