Files
swift-mirror/test/Interop/Cxx/templates/Inputs/member-templates.h
Egor Zhdan e5899ee167 [cxx-interop] Use fully-qualified type names of C++ template parameters
When importing C++ class template instantiations, Swift generates a type name for each instantiation. The generated names must be unique, since they are used for mangling.

If multiple different C++ types declare nested types with the same name, which are then used as template arguments, Swift was generating the same name for those template instantiations (e.g. `shared_ptr<Impl>` for different `Impl` types).

This change makes sure we use fully-qualified type names of template parameters when generating Swift type names for class template instantiations (e.g. `shared_ptr<MyNamespace.MyClass.Impl>`).

This fixes an assertion failure coming out of IRGen:
```
Assertion failed: (Buffer.empty() && "didn't claim all values out of buffer"), function ~ConstantInitBuilderBase, file ConstantInitBuilder.h, line 75.
```

rdar://141962480
2025-01-02 18:03:56 +00:00

82 lines
2.3 KiB
C++

#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_MEMBER_TEMPLATES_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_MEMBER_TEMPLATES_H
struct HasMemberTemplates {
template <class T> T addSameTypeParams(T a, T b) { return a + b; }
template <class T, class U> T addMixedTypeParams(T a, U b) { return a + b; }
template <class T, class U> int addAll(int a, T b, U c) { return a + b + c; }
template <class T> T passThrough(T val) { return val; }
template <class T> T passThroughConst(const T val) { return val; }
template <class T> T passThroughOnConst(T val) const { return val; }
template <class T> T passThroughConstOnConst(const T val) const {
return val;
}
template <class T> void doNothingConstRef(const T &val) {}
template <class T> void make42Ref(T &val) {}
};
template <class T> struct TemplateClassWithMemberTemplates {
T value;
template <class U> void setValue(U val) { value = val; }
template<class U> TemplateClassWithMemberTemplates<U> toOtherSpec(const U& u) const {
return {u};
}
TemplateClassWithMemberTemplates(T val) : value(val) {}
};
using IntWrapper = TemplateClassWithMemberTemplates<int>;
struct HasStaticMemberTemplates {
template <class T> static T add(T a, T b) { return a + b; }
template <class T, class U> static T addTwoTemplates(T a, U b) { return a + b; }
template <class T> static T removeReference(T &a) { return a; }
};
template <typename T>
struct MyTemplatedStruct {};
struct HasTemplatedField {
MyTemplatedStruct<int> x;
};
struct HasNestedInstantiation {
template <typename T>
struct MyNestedTemplatedStruct {};
using NestedInst = MyTemplatedStruct<MyNestedTemplatedStruct<int>>;
};
namespace NS {
struct HasNestedInstantiation {
template <typename T>
struct MyNestedTemplatedStruct {};
using NestedInst = MyTemplatedStruct<MyNestedTemplatedStruct<int>>;
};
}
template <typename A, typename R = TemplateClassWithMemberTemplates<A>>
struct HasUninstantiatableTemplateMember {
R *pointer; // R cannot be instantiated here, because R is an incomplete type,
// so this should be imported as OpaquePointer.
};
struct HasTemplateInstantiationWithForwardDecl {
class NoDefinition;
HasUninstantiatableTemplateMember<NoDefinition> noDefMember;
};
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_MEMBER_TEMPLATES_H