xref: /llvm-project/clang/test/CodeGenCXX/mangle-ms-templates.cpp (revision 5518a9d7673bfe55b4110bea049140316d032fbf)
1 // RUN: %clang_cc1 -std=c++11 -fms-compatibility-version=19 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=i386-pc-win32 | FileCheck %s
2 // RUN: %clang_cc1 -std=c++11 -fms-compatibility-version=19 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-win32 | FileCheck -check-prefix X64 %s
3 // RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=i386-pc-win32 | FileCheck %s
4 // RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-win32 | FileCheck -check-prefix X64 %s
5 // RUN: %clang_cc1 -std=c++20 -fms-compatibility-version=19 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-win32 | FileCheck -check-prefix CXX20-X64 %s
6 
7 // Check that array-to-pointer decay is mangled as the underlying declaration.
8 extern const char arr[4] = "foo";
9 template<const char*> struct Decay1 {};
10 // CHECK: "?decay1@@3U?$Decay1@$1?arr@@3QBDB@@A"
11 Decay1<arr> decay1;
12 #if __cplusplus >= 201702L
13 // Note that this mangling approach can lead to collisions.
14 template<const void*> struct Decay2 {};
15 // CXX20-X64: "?decay2a@@3U?$Decay2@$1?arr@@3QBDB@@A"
16 Decay2<(const void*)arr> decay2a;
17 // CXX20-X64: "?decay2b@@3U?$Decay2@$1?arr@@3QBDB@@A"
18 Decay2<(const void*)&arr> decay2b;
19 #endif
20 
21 template<typename T>
22 class Class {
23  public:
Class()24   Class() {}
25 };
26 
27 class Typename { };
28 
29 template<typename T>
30 class Nested { };
31 
32 template<bool flag>
33 class BoolTemplate {
34  public:
BoolTemplate()35   BoolTemplate() {}
36 };
37 
38 template<int param>
39 class IntTemplate {
40  public:
IntTemplate()41   IntTemplate() {}
42 };
43 
44 template<unsigned param>
45 class UnsignedIntTemplate {
46 public:
UnsignedIntTemplate()47   UnsignedIntTemplate() {}
48 };
49 
50 template<long long param>
51 class LongLongTemplate {
52  public:
LongLongTemplate()53   LongLongTemplate() {}
54 };
55 
56 template<unsigned long long param>
57 class UnsignedLongLongTemplate {
58  public:
UnsignedLongLongTemplate()59   UnsignedLongLongTemplate() {}
60 };
61 
62 template<>
63 class BoolTemplate<true> {
64  public:
BoolTemplate()65   BoolTemplate() {}
Foo(T arg)66   template<class T> void Foo(T arg) {}
67 };
68 
template_mangling()69 void template_mangling() {
70   Class<Typename> c1;
71 // CHECK: call {{.*}} @"??0?$Class@VTypename@@@@QAE@XZ"
72 // X64: call {{.*}} @"??0?$Class@VTypename@@@@QEAA@XZ"
73 
74   Class<const Typename> c1_const;
75 // CHECK: call {{.*}} @"??0?$Class@$$CBVTypename@@@@QAE@XZ"
76 // X64: call {{.*}} @"??0?$Class@$$CBVTypename@@@@QEAA@XZ"
77   Class<volatile Typename> c1_volatile;
78 // CHECK: call {{.*}} @"??0?$Class@$$CCVTypename@@@@QAE@XZ"
79 // X64: call {{.*}} @"??0?$Class@$$CCVTypename@@@@QEAA@XZ"
80   Class<const volatile Typename> c1_cv;
81 // CHECK: call {{.*}} @"??0?$Class@$$CDVTypename@@@@QAE@XZ"
82 // X64: call {{.*}} @"??0?$Class@$$CDVTypename@@@@QEAA@XZ"
83 
84   Class<Nested<Typename> > c2;
85 // CHECK: call {{.*}} @"??0?$Class@V?$Nested@VTypename@@@@@@QAE@XZ"
86 // X64: call {{.*}} @"??0?$Class@V?$Nested@VTypename@@@@@@QEAA@XZ"
87 
88   Class<int * const> c_intpc;
89 // CHECK: call {{.*}} @"??0?$Class@QAH@@QAE@XZ"
90 // X64: call {{.*}} @"??0?$Class@QEAH@@QEAA@XZ"
91   Class<int()> c_ft;
92 // CHECK: call {{.*}} @"??0?$Class@$$A6AHXZ@@QAE@XZ"
93 // X64: call {{.*}} @"??0?$Class@$$A6AHXZ@@QEAA@XZ"
94   Class<int[]> c_inti;
95 // CHECK: call {{.*}} @"??0?$Class@$$BY0A@H@@QAE@XZ"
96 // X64: call {{.*}} @"??0?$Class@$$BY0A@H@@QEAA@XZ"
97   Class<int[5]> c_int5;
98 // CHECK: call {{.*}} @"??0?$Class@$$BY04H@@QAE@XZ"
99 // X64: call {{.*}} @"??0?$Class@$$BY04H@@QEAA@XZ"
100   Class<const int[5]> c_intc5;
101 // CHECK: call {{.*}} @"??0?$Class@$$BY04$$CBH@@QAE@XZ"
102 // X64: call {{.*}} @"??0?$Class@$$BY04$$CBH@@QEAA@XZ"
103   Class<int * const[5]> c_intpc5;
104 // CHECK: call {{.*}} @"??0?$Class@$$BY04QAH@@QAE@XZ"
105 // X64: call {{.*}} @"??0?$Class@$$BY04QEAH@@QEAA@XZ"
106 
107   BoolTemplate<false> _false;
108 // CHECK: call {{.*}} @"??0?$BoolTemplate@$0A@@@QAE@XZ"
109 // X64: call {{.*}} @"??0?$BoolTemplate@$0A@@@QEAA@XZ"
110 
111   BoolTemplate<true> _true;
112   // PR13158
113   _true.Foo(1);
114 // CHECK: call {{.*}} @"??0?$BoolTemplate@$00@@QAE@XZ"
115 // X64: call {{.*}} @"??0?$BoolTemplate@$00@@QEAA@XZ"
116 // CHECK: call {{.*}} @"??$Foo@H@?$BoolTemplate@$00@@QAEXH@Z"
117 // X64: call {{.*}} @"??$Foo@H@?$BoolTemplate@$00@@QEAAXH@Z"
118 
119   IntTemplate<0> zero;
120 // CHECK: call {{.*}} @"??0?$IntTemplate@$0A@@@QAE@XZ"
121 // X64: call {{.*}} @"??0?$IntTemplate@$0A@@@QEAA@XZ"
122 
123   IntTemplate<5> five;
124 // CHECK: call {{.*}} @"??0?$IntTemplate@$04@@QAE@XZ"
125 // X64: call {{.*}} @"??0?$IntTemplate@$04@@QEAA@XZ"
126 
127   IntTemplate<11> eleven;
128 // CHECK: call {{.*}} @"??0?$IntTemplate@$0L@@@QAE@XZ"
129 // X64: call {{.*}} @"??0?$IntTemplate@$0L@@@QEAA@XZ"
130 
131   IntTemplate<256> _256;
132 // CHECK: call {{.*}} @"??0?$IntTemplate@$0BAA@@@QAE@XZ"
133 // X64: call {{.*}} @"??0?$IntTemplate@$0BAA@@@QEAA@XZ"
134 
135   IntTemplate<513> _513;
136 // CHECK: call {{.*}} @"??0?$IntTemplate@$0CAB@@@QAE@XZ"
137 // X64: call {{.*}} @"??0?$IntTemplate@$0CAB@@@QEAA@XZ"
138 
139   IntTemplate<1026> _1026;
140 // CHECK: call {{.*}} @"??0?$IntTemplate@$0EAC@@@QAE@XZ"
141 // X64: call {{.*}} @"??0?$IntTemplate@$0EAC@@@QEAA@XZ"
142 
143   IntTemplate<65535> ffff;
144 // CHECK: call {{.*}} @"??0?$IntTemplate@$0PPPP@@@QAE@XZ"
145 // X64: call {{.*}} @"??0?$IntTemplate@$0PPPP@@@QEAA@XZ"
146 
147   IntTemplate<-1>  neg_1;
148 // CHECK: call {{.*}} @"??0?$IntTemplate@$0?0@@QAE@XZ"
149 // X64: call {{.*}} @"??0?$IntTemplate@$0?0@@QEAA@XZ"
150   IntTemplate<-9>  neg_9;
151 // CHECK: call {{.*}} @"??0?$IntTemplate@$0?8@@QAE@XZ"
152 // X64: call {{.*}} @"??0?$IntTemplate@$0?8@@QEAA@XZ"
153   IntTemplate<-10> neg_10;
154 // CHECK: call {{.*}} @"??0?$IntTemplate@$0?9@@QAE@XZ"
155 // X64: call {{.*}} @"??0?$IntTemplate@$0?9@@QEAA@XZ"
156   IntTemplate<-11> neg_11;
157 // CHECK: call {{.*}} @"??0?$IntTemplate@$0?L@@@QAE@XZ"
158 // X64: call {{.*}} @"??0?$IntTemplate@$0?L@@@QEAA@XZ"
159 
160   UnsignedIntTemplate<4294967295> ffffffff;
161 // CHECK: call {{.*}} @"??0?$UnsignedIntTemplate@$0PPPPPPPP@@@QAE@XZ"
162 // X64: call {{.*}} @"??0?$UnsignedIntTemplate@$0PPPPPPPP@@@QEAA@XZ"
163 
164   LongLongTemplate<-9223372036854775807LL-1LL> int64_min;
165 // CHECK: call {{.*}} @"??0?$LongLongTemplate@$0?IAAAAAAAAAAAAAAA@@@QAE@XZ"
166 // X64: call {{.*}} @"??0?$LongLongTemplate@$0?IAAAAAAAAAAAAAAA@@@QEAA@XZ"
167   LongLongTemplate<9223372036854775807LL>      int64_max;
168 // CHECK: call {{.*}} @"??0?$LongLongTemplate@$0HPPPPPPPPPPPPPPP@@@QAE@XZ"
169 // X64: call {{.*}} @"??0?$LongLongTemplate@$0HPPPPPPPPPPPPPPP@@@QEAA@XZ"
170   UnsignedLongLongTemplate<18446744073709551615ULL> uint64_max;
171 // CHECK: call {{.*}} @"??0?$UnsignedLongLongTemplate@$0?0@@QAE@XZ"
172 // X64: call {{.*}} @"??0?$UnsignedLongLongTemplate@$0?0@@QEAA@XZ"
173   UnsignedLongLongTemplate<(unsigned long long)-1>  uint64_neg_1;
174 // CHECK: call {{.*}} @"??0?$UnsignedLongLongTemplate@$0?0@@QAE@XZ"
175 // X64: call {{.*}} @"??0?$UnsignedLongLongTemplate@$0?0@@QEAA@XZ"
176 }
177 
178 namespace space {
foo(const T & l)179   template<class T> const T& foo(const T& l) { return l; }
180 }
181 // CHECK: "??$foo@H@space@@YAABHABH@Z"
182 // X64: "??$foo@H@space@@YAAEBHAEBH@Z"
183 
use()184 void use() {
185   space::foo(42);
186 }
187 
188 // PR13455
189 typedef void (*FunctionPointer)(void);
190 
191 template <FunctionPointer function>
FunctionPointerTemplate()192 void FunctionPointerTemplate() {
193   function();
194 }
195 
spam()196 void spam() {
197   FunctionPointerTemplate<spam>();
198 // CHECK: "??$FunctionPointerTemplate@$1?spam@@YAXXZ@@YAXXZ"
199 // X64: "??$FunctionPointerTemplate@$1?spam@@YAXXZ@@YAXXZ"
200 }
201 
202 // Unlike Itanium, there is no character code to indicate an argument pack.
203 // Tested with MSVC 2013, the first version which supports variadic templates.
204 
205 template <typename ...Ts> void variadic_fn_template(const Ts &...args);
206 template <typename... Ts, typename... Us>
207 void multi_variadic_fn(Ts... ts, Us... us);
208 template <typename... Ts, typename C, typename... Us>
209 void multi_variadic_mixed(Ts... ts, C c, Us... us);
variadic_fn_instantiate()210 void variadic_fn_instantiate() {
211   variadic_fn_template(0, 1, 3, 4);
212   variadic_fn_template(0, 1, 'a', "b");
213 
214   // Directlly consecutive packs are separated by $$Z...
215   multi_variadic_fn<int, int>(1, 2, 3, 4, 5);
216   multi_variadic_fn<int, int, int>(1, 2, 3, 4, 5);
217 
218   // ...but not if another template parameter is between them.
219   multi_variadic_mixed<int, int>(1, 2, 3);
220   multi_variadic_mixed<int, int>(1, 2, 3, 4);
221 }
222 // CHECK: "??$variadic_fn_template@HHHH@@YAXABH000@Z"
223 // X64:   "??$variadic_fn_template@HHHH@@YAXAEBH000@Z"
224 // CHECK: "??$variadic_fn_template@HHD$$BY01D@@YAXABH0ABDAAY01$$CBD@Z"
225 // X64:   "??$variadic_fn_template@HHD$$BY01D@@YAXAEBH0AEBDAEAY01$$CBD@Z"
226 // CHECK: "??$multi_variadic_fn@HH$$ZHHH@@YAXHHHHH@Z"
227 // X64:   "??$multi_variadic_fn@HH$$ZHHH@@YAXHHHHH@Z"
228 // CHECK: "??$multi_variadic_fn@HHH$$ZHH@@YAXHHHHH@Z"
229 // X64:   "??$multi_variadic_fn@HHH$$ZHH@@YAXHHHHH@Z"
230 // CHECK: "??$multi_variadic_mixed@HHH$$V@@YAXHHH@Z"
231 // X64:   "??$multi_variadic_mixed@HHH$$V@@YAXHHH@Z"
232 // CHECK: "??$multi_variadic_mixed@HHHH@@YAXHHHH@Z"
233 // X64:   "??$multi_variadic_mixed@HHHH@@YAXHHHH@Z"
234 
235 template <typename ...Ts>
236 struct VariadicClass {
VariadicClassVariadicClass237   VariadicClass() { }
238   int x;
239 };
variadic_class_instantiate()240 void variadic_class_instantiate() {
241   VariadicClass<int, char, bool> a;
242   VariadicClass<bool, char, int> b;
243 }
244 // CHECK: call {{.*}} @"??0?$VariadicClass@HD_N@@QAE@XZ"
245 // CHECK: call {{.*}} @"??0?$VariadicClass@_NDH@@QAE@XZ"
246 
247 template <typename T>
248 struct Second {};
249 
250 template <typename T, template <class> class>
251 struct Type {};
252 
253 template <template <class> class T>
254 struct Type2 {};
255 
256 template <template <class> class T, bool B>
257 struct Thing;
258 
259 template <template <class> class T>
260 struct Thing<T, false> { };
261 
262 template <template <class> class T>
263 struct Thing<T, true> { };
264 
template_template_fun(Type<Thing<Second,true>,Second>)265 void template_template_fun(Type<Thing<Second, true>, Second>) { }
266 // CHECK: "?template_template_fun@@YAXU?$Type@U?$Thing@USecond@@$00@@USecond@@@@@Z"
267 
268 template <typename T>
269 void template_template_specialization();
270 
271 template <>
template_template_specialization()272 void template_template_specialization<void (Type<Thing<Second, true>, Second>)>() {
273 }
274 // CHECK: "??$template_template_specialization@$$A6AXU?$Type@U?$Thing@USecond@@$00@@USecond@@@@@Z@@YAXXZ"
275 
276 // PR16788
277 template <decltype(nullptr)> struct S1 {};
f(S1<nullptr>)278 void f(S1<nullptr>) {}
279 // CHECK: "?f@@YAXU?$S1@$0A@@@@Z"
280 
281 struct record {
282   int first;
283   int second;
284 };
285 template <const record &>
286 struct type1 {
287 };
288 extern const record inst;
recref(type1<inst>)289 void recref(type1<inst>) {}
290 // CHECK: "?recref@@YAXU?$type1@$1?inst@@3Urecord@@B@@@Z"
291 
292 struct _GUID {};
293 struct __declspec(uuid("{12345678-1234-1234-1234-1234567890aB}")) uuid;
294 
295 template <typename T, const _GUID *G = &__uuidof(T)>
296 struct UUIDType1 {};
297 
298 template <typename T, const _GUID &G = __uuidof(T)>
299 struct UUIDType2 {};
300 
fun(UUIDType1<uuid> a)301 void fun(UUIDType1<uuid> a) {}
302 // CHECK: "?fun@@YAXU?$UUIDType1@Uuuid@@$1?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@@Z"
fun(UUIDType2<uuid> b)303 void fun(UUIDType2<uuid> b) {}
304 // CHECK: "?fun@@YAXU?$UUIDType2@Uuuid@@$1?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@@Z"
305 
306 template <typename T> struct TypeWithFriendDefinition {
FunctionDefinedWithInjectedName(TypeWithFriendDefinition<T>)307   friend void FunctionDefinedWithInjectedName(TypeWithFriendDefinition<T>) {}
308 };
309 // CHECK: call {{.*}} @"?FunctionDefinedWithInjectedName@@YAXU?$TypeWithFriendDefinition@H@@@Z"
CallFunctionDefinedWithInjectedName()310 void CallFunctionDefinedWithInjectedName() {
311   FunctionDefinedWithInjectedName(TypeWithFriendDefinition<int>());
312 }
313 // CHECK: @"?FunctionDefinedWithInjectedName@@YAXU?$TypeWithFriendDefinition@H@@@Z"
314 
315 // We need to be able to feed GUIDs through a couple rounds of template
316 // substitution.
317 template <const _GUID *G>
318 struct UUIDType3 {
fooUUIDType3319   void foo() {}
320 };
321 template <const _GUID *G>
322 struct UUIDType4 : UUIDType3<G> {
barUUIDType4323   void bar() { UUIDType4::foo(); }
324 };
325 template struct UUIDType4<&__uuidof(uuid)>;
326 // CHECK: "?bar@?$UUIDType4@$1?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@QAEXXZ"
327 // CHECK: "?foo@?$UUIDType3@$1?_GUID_12345678_1234_1234_1234_1234567890ab@@3U__s_GUID@@B@@QAEXXZ"
328 
329 #ifdef _WIN64
330 template<__int128 N> struct Int128 {};
331 template<unsigned __int128 N> struct UInt128 {};
332 // X64: define {{.*}} @"?fun_int128@@YAXU?$Int128@$0A@@@@Z"(
fun_int128(Int128<0>)333 void fun_int128(Int128<0>) {}
334 // X64: define {{.*}} @"?fun_int128@@YAXU?$Int128@$0?0@@@Z"(
fun_int128(Int128<-1>)335 void fun_int128(Int128<-1>) {}
336 // X64: define {{.*}} @"?fun_int128@@YAXU?$Int128@$0DPPPPPPPPPPPPPPPAAAAAAAAAAAAAAAB@@@@Z"(
fun_int128(Int128<(__int128)9223372036854775807* (__int128)9223372036854775807>)337 void fun_int128(Int128<(__int128)9223372036854775807 * (__int128)9223372036854775807>) {}
338 // X64: define {{.*}} @"?fun_uint128@@YAXU?$UInt128@$0A@@@@Z"(
fun_uint128(UInt128<0>)339 void fun_uint128(UInt128<0>) {}
340 // X64: define {{.*}} @"?fun_uint128@@YAXU?$UInt128@$0?0@@@Z"(
fun_uint128(UInt128<(unsigned __int128)-1>)341 void fun_uint128(UInt128<(unsigned __int128)-1>) {}
342 // X64: define {{.*}} @"?fun_uint128@@YAXU?$UInt128@$0DPPPPPPPPPPPPPPPAAAAAAAAAAAAAAAB@@@@Z"(
fun_uint128(UInt128<(unsigned __int128)9223372036854775807* (unsigned __int128)9223372036854775807>)343 void fun_uint128(UInt128<(unsigned __int128)9223372036854775807 * (unsigned __int128)9223372036854775807>) {}
344 #endif
345 
346 #if __cplusplus >= 202002L
347 
348 template<float> struct Float {};
349 // CXX20-X64: define {{.*}} @"?f@@YAXU?$Float@$ADPIAAAAA@@@@Z"(
f(Float<1.0f>)350 void f(Float<1.0f>) {}
351 template<auto> struct Auto {};
352 // CXX20-X64: define {{.*}} @"?f@@YAXU?$Auto@$MMADPIAAAAA@@@@Z"(
f(Auto<1.0f>)353 void f(Auto<1.0f>) {}
354 
355 struct S2 {
356   int arr[2][3];
357   int i;
358   void fn();
359 } s2;
360 
361 template<int&> struct TplSubobjectRef {};
362 // CXX20-X64: define {{.*}} @"?f@@YAXU?$TplSubobjectRef@$CC61?s2@@3US2@@Aarr@@00@01@@@@Z"(
f(TplSubobjectRef<s2.arr[1][2]>)363 void f(TplSubobjectRef<s2.arr[1][2]>) {}
364 template<int*> struct TplSubobjectPtr {};
365 // CXX20-X64: define {{.*}} @"?f@@YAXU?$TplSubobjectPtr@$CC61?s2@@3US2@@Aarr@@00@01@@@@Z"(
f(TplSubobjectPtr<& s2.arr[1][2]>)366 void f(TplSubobjectPtr<&s2.arr[1][2]>) {}
367 
368 struct Derived : S2 {};
369 
370 template <int Derived::*> struct TplMemberPtr {};
371 // CXX20-X64: define {{.*}} @"?f@@YAXU?$TplMemberPtr@$0BI@@@@Z"(
f(TplMemberPtr<(int Derived::*)& S2::i>)372 void f(TplMemberPtr<(int Derived::*)&S2::i>) {}
373 template <void (Derived::*)()> struct TplMemberFnPtr {};
374 // CXX20-X64: define {{.*}} @"?f@@YAXU?$TplMemberFnPtr@$1?fn@S2@@QEAAXXZ@@@Z"(
f(TplMemberFnPtr<(void (Derived::*)())& S2::fn>)375 void f(TplMemberFnPtr<(void (Derived::*)())&S2::fn>) {}
376 
377 #endif
378