1 // RUN: %clang_cc1 -verify -Wno-return-type -Wno-main -std=c++11 -fclang-abi-compat=latest -emit-llvm -triple %itanium_abi_triple -o - %s | FileCheck %s
2 // RUN: %clang_cc1 -verify -Wno-return-type -Wno-main -std=c++20 -fclang-abi-compat=latest -emit-llvm -triple x86_64-linux-gnu -o - %s | FileCheck %s --check-prefixes=CHECK,CXX20
3 // expected-no-diagnostics
4
5 namespace test1 {
6 int x;
7 template <int& D> class T { };
8 // CHECK: void @_ZN5test12f0ENS_1TIL_ZNS_1xEEEE(
f0(T<x> a0)9 void f0(T<x> a0) {}
10 }
11
12 namespace test1 {
13 // CHECK: void @_ZN5test12f0Ef
f0(float)14 void f0(float) {}
15 template<void (&)(float)> struct t1 {};
16 // CHECK: void @_ZN5test12f1ENS_2t1IL_ZNS_2f0EfEEE(
f1(t1<f0> a0)17 void f1(t1<f0> a0) {}
18 }
19
20 namespace test2 {
21 // CHECK: void @_ZN5test22f0Ef
f0(float)22 void f0(float) {}
23 template<void (*)(float)> struct t1 {};
24 // CHECK: void @_ZN5test22f1ENS_2t1IXadL_ZNS_2f0EfEEEE(
f1(t1<f0> a0)25 void f1(t1<f0> a0) {}
26 }
27
28 namespace test3 {
29 // CHECK: void @test3_f0
test3_f0(float)30 extern "C" void test3_f0(float) {}
31 template<void (&)(float)> struct t1 {};
32 // CHECK: void @_ZN5test32f1ENS_2t1IL_Z8test3_f0EEE(
f1(t1<test3_f0> a0)33 void f1(t1<test3_f0> a0) {}
34 }
35
36 namespace test4 {
37 // CHECK: void @test4_f0
test4_f0(float)38 extern "C" void test4_f0(float) {}
39 template<void (*)(float)> struct t1 {};
40 // CHECK: void @_ZN5test42f1ENS_2t1IXadL_Z8test4_f0EEEE(
f1(t1<test4_f0> a0)41 void f1(t1<test4_f0> a0) {}
42 }
43
44 // CHECK: void @test5_f0
test5_f0(float)45 extern "C" void test5_f0(float) {}
main(int)46 int main(int) {}
47
48 namespace test5 {
49 template<void (&)(float)> struct t1 {};
50 // CHECK: void @_ZN5test52f1ENS_2t1IL_Z8test5_f0EEE(
f1(t1<test5_f0> a0)51 void f1(t1<test5_f0> a0) {}
52
53 template<int (&)(int)> struct t2 {};
54 // CHECK: void @_ZN5test52f2ENS_2t2IL_Z4mainEEE
f2(t2<main> a0)55 void f2(t2<main> a0) {}
56 }
57
58 namespace test6 {
59 struct A { void im0(float); };
60 // CHECK: void @_ZN5test61A3im0Ef
im0(float)61 void A::im0(float) {}
62 template <void(A::*)(float)> class T { };
63 // CHECK: void @_ZN5test62f0ENS_1TIXadL_ZNS_1A3im0EfEEEE(
f0(T<& A::im0> a0)64 void f0(T<&A::im0> a0) {}
65 }
66
67 namespace test7 {
68 template<typename T>
69 struct meta {
70 static const unsigned value = sizeof(T);
71 };
72
73 template<unsigned> struct int_c {
74 typedef float type;
75 };
76
77 template<typename T>
78 struct X {
79 template<typename U>
Xtest7::X80 X(U*, typename int_c<(meta<T>::value + meta<U>::value)>::type *) { }
81 };
82
83 // CHECK: define weak_odr {{.*}} @_ZN5test71XIiEC1IdEEPT_PNS_5int_cIXplL_ZNS_4metaIiE5valueEEsr4metaIS3_EE5valueEE4typeE(
84 template X<int>::X(double*, float*);
85 }
86
87 namespace test8 {
88 template<typename T>
89 struct meta {
90 struct type {
91 static const unsigned value = sizeof(T);
92 };
93 };
94
95 template<unsigned> struct int_c {
96 typedef float type;
97 };
98
99 template<typename T>
f(int_c<meta<T>::type::value>)100 void f(int_c<meta<T>::type::value>) { }
101
102 // CHECK-LABEL: define weak_odr {{.*}}void @_ZN5test81fIiEEvNS_5int_cIXsr4metaIT_E4typeE5valueEEE(
103 template void f<int>(int_c<sizeof(int)>);
104 }
105
106 namespace test9 {
107 template<typename T>
108 struct supermeta {
109 template<typename U>
110 struct apply {
111 typedef T U::*type;
112 };
113 };
114
115 struct X { };
116
117 template<typename T, typename U>
118 typename supermeta<T>::template apply<U>::type f();
119
test_f()120 void test_f() {
121 // CHECK: @_ZN5test91fIiNS_1XEEENS_9supermetaIT_E5applyIT0_E4typeEv()
122 // Note: GCC incorrectly mangles this as
123 // _ZN5test91fIiNS_1XEEENS_9supermetaIT_E5apply4typeEv, while EDG
124 // gets it right.
125 f<int, X>();
126 }
127 }
128
129 namespace test10 {
130 template<typename T>
131 struct X {
132 template<typename U>
133 struct definition {
134 };
135 };
136
137 // CHECK: _ZN6test101fIidEENS_1XIT_E10definitionIT0_EES2_S5_
138 template<typename T, typename U>
f(T,U)139 typename X<T>::template definition<U> f(T, U) { }
140
g(int i,double d)141 void g(int i, double d) {
142 f(i, d);
143 }
144 }
145
146 // Report from cxx-abi-dev, 2012.01.04.
147 namespace test11 {
148 int cmp(char a, char b);
149 template <typename T, int (*cmp)(T, T)> struct A {};
f(A<T,cmp> &)150 template <typename T> void f(A<T,cmp> &) {}
151 template void f<char>(A<char,cmp> &);
152 // CHECK: @_ZN6test111fIcEEvRNS_1AIT_L_ZNS_3cmpEccEEE(
153 }
154
155 namespace test12 {
156 // Make sure we can mangle non-type template args with internal linkage.
f()157 static int f() {}
158 const int n = 10;
test()159 template<typename T, T v> void test() {}
use()160 void use() {
161 // CHECK-LABEL: define internal {{.*}}void @_ZN6test124testIFivETnT_XadL_ZNS_L1fEvEEEEvv(
162 test<int(), &f>();
163 // CHECK-LABEL: define internal {{.*}}void @_ZN6test124testIRFivETnT_L_ZNS_L1fEvEEEvv(
164 test<int(&)(), f>();
165 // CHECK-LABEL: define internal {{.*}}void @_ZN6test124testIPKiTnT_XadL_ZNS_L1nEEEEEvv(
166 test<const int*, &n>();
167 // CHECK-LABEL: define internal {{.*}}void @_ZN6test124testIRKiTnT_L_ZNS_L1nEEEEvv(
168 test<const int&, n>();
169 }
170 }
171
172 // Test the boundary condition of minimal signed integers.
173 namespace test13 {
returnChar()174 template <char c> char returnChar() { return c; }
175 template char returnChar<-128>();
176 // CHECK: @_ZN6test1310returnCharILcn128EEEcv()
177
returnShort()178 template <short s> short returnShort() { return s; }
179 template short returnShort<-32768>();
180 // CHECK: @_ZN6test1311returnShortILsn32768EEEsv()
181 }
182
183 namespace test14 {
inl(bool b)184 template <typename> inline int inl(bool b) {
185 if (b) {
186 static struct {
187 int field;
188 } a;
189 // CHECK: @_ZZN6test143inlIvEEibE1a
190
191 return a.field;
192 } else {
193 static struct {
194 int field;
195 } a;
196 // CHECK: @_ZZN6test143inlIvEEibE1a_0
197
198 return a.field;
199 }
200 }
201
call(bool b)202 int call(bool b) { return inl<void>(b); }
203 }
204
205 namespace std {
206 template <class _Tp, _Tp...> struct integer_sequence {};
207 }
208
209 namespace test15 {
210 template <int N>
make()211 __make_integer_seq<std::integer_sequence, int, N> make() {}
212 template __make_integer_seq<std::integer_sequence, int, 5> make<5>();
213 // CHECK: define weak_odr {{.*}} @_ZN6test154makeILi5EEE18__make_integer_seqISt16integer_sequenceiXT_EEv(
214 }
215
216 namespace test16 {
217 // Ensure we properly form substitutions for template names in prefixes.
218 // CHECK: @_ZN6test161fINS_1TEEEvNT_1UIiE1VIiEENS5_IfEE
219 template<typename T> void f(typename T::template U<int>::template V<int>, typename T::template U<int>::template V<float>);
220 struct T { template<typename I> struct U { template<typename J> using V = int; }; };
g()221 void g() { f<T>(1, 2); }
222 }
223
224 #if __cplusplus >= 202002L
225 namespace cxx20 {
226 template<auto> struct A {};
227 template<typename T, T V> struct B {};
228 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AILf3f800000EEE(
f(A<1.0f>)229 void f(A<1.0f>) {}
230 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AILd3ff0000000000000EEE(
f(A<1.0>)231 void f(A<1.0>) {}
232 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AILe3fff8000000000000000EEE(
f(A<1.0l>)233 void f(A<1.0l>) {}
234 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXtlCiLi0ELi1EEEEE(
f(A<1i>)235 void f(A<1i>) {}
236 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXtlCdLd0000000000000000ELd3ff0000000000000EEEEE(
f(A<1.0i>)237 void f(A<1.0i>) {}
238
239 int x;
240 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXadL_ZNS_1xEEEEE(
f(A<& x>)241 void f(A<&x>) {}
242 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPiXadL_ZNS_1xEEEEE(
f(B<int *,& x>)243 void f(B<int*, &x>) {}
244 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXcvPKiadL_ZNS_1xEEEEE(
f(A<(const int *)& x>)245 void f(A<(const int*)&x>) {}
246 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPKiXadL_ZNS_1xEEEEE(
f(B<const int *,& x>)247 void f(B<const int*, &x>) {}
248 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXcvPvadL_ZNS_1xEEEEE(
f(A<(void *)& x>)249 void f(A<(void*)&x>) {}
250 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPvXadL_ZNS_1xEEEEE(
f(B<void *,(void *)& x>)251 void f(B<void*, (void*)&x>) {}
252 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXcvPKvadL_ZNS_1xEEEEE(
f(A<(const void *)& x>)253 void f(A<(const void*)&x>) {}
254 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPKvXadL_ZNS_1xEEEEE(
f(B<const void *,(const void *)& x>)255 void f(B<const void*, (const void*)&x>) {}
256
257 struct Q { int x; } q;
258
259 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXadsoiL_ZNS_1qEEEEEE(
f(A<& q.x>)260 void f(A<&q.x>) {}
261 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPiXadsoiL_ZNS_1qEEEEEE(
f(B<int *,& q.x>)262 void f(B<int*, &q.x>) {}
263 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXadsoKiL_ZNS_1qEEEEEE(
f(A<(const int *)& q.x>)264 void f(A<(const int*)&q.x>) {}
265 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPKiXadsoS1_L_ZNS_1qEEEEEE
f(B<const int *,(const int *)& q.x>)266 void f(B<const int*, (const int*)&q.x>) {}
267 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXcvPvadsoiL_ZNS_1qEEEEEE(
f(A<(void *)& q.x>)268 void f(A<(void*)&q.x>) {}
269 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPvXadsoiL_ZNS_1qEEEEEE(
f(B<void *,(void *)& q.x>)270 void f(B<void*, (void*)&q.x>) {}
271 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXcvPKvadsoiL_ZNS_1qEEEEEE(
f(A<(const void *)& q.x>)272 void f(A<(const void*)&q.x>) {}
273 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIPKvXadsoiL_ZNS_1qEEEEEE(
f(B<const void *,(const void *)& q.x>)274 void f(B<const void*, (const void*)&q.x>) {}
275
276 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXadL_ZNS_1Q1xEEEEE(
f(A<& Q::x>)277 void f(A<&Q::x>) {}
278 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIMNS_1QEiXadL_ZNS1_1xEEEEE
f(B<int Q::*,& Q::x>)279 void f(B<int Q::*, &Q::x>) {}
280 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXcvMNS_1QEKiadL_ZNS1_1xEEEEE(
f(A<(const int Q::*)& Q::x>)281 void f(A<(const int Q::*)&Q::x>) {}
282 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIMNS_1QEKiXadL_ZNS1_1xEEEEE(
f(B<const int Q::*,(const int Q::*)& Q::x>)283 void f(B<const int Q::*, (const int Q::*)&Q::x>) {}
284
285 struct R : Q {};
286
287 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXmcMNS_1REiadL_ZNS_1Q1xEEEEEE(
f(A<(int R::*)& Q::x>)288 void f(A<(int R::*)&Q::x>) {}
289 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIMNS_1REiXmcS2_adL_ZNS_1Q1xEEEEEE(
f(B<int R::*,(int R::*)& Q::x>)290 void f(B<int R::*, (int R::*)&Q::x>) {}
291 // CXX20: define {{.*}} @_ZN5cxx201fENS_1AIXmcMNS_1REKiadL_ZNS_1Q1xEEEEEE(
f(A<(const int R::*)& Q::x>)292 void f(A<(const int R::*)&Q::x>) {}
293 // CXX20: define {{.*}} @_ZN5cxx201fENS_1BIMNS_1REKiXmcS3_adL_ZNS_1Q1xEEEEEE(
f(B<const int R::*,(const int R::*)& Q::x>)294 void f(B<const int R::*, (const int R::*)&Q::x>) {}
295 }
296 #endif
297
298 namespace test17 {
299 // Ensure we mangle the types for non-type template arguments if we've lost
300 // track of argument / parameter correspondence.
301 template<int A, int ...B> struct X {};
302
303 // CHECK: define {{.*}} @_ZN6test171fILi1EJLi2ELi3ELi4EEEEvNS_1XIXT_EJLi5EXspT0_ELi6EEEE
f(X<D,5u,C...,6u>)304 template<int D, int ...C> void f(X<D, 5u, C..., 6u>) {}
g()305 void g() { f<1, 2, 3, 4>({}); }
306
307 // Note: there is no J...E here, because we can't form a pack argument, and
308 // the 5u and 6u are mangled with the original type 'j' (unsigned int) not
309 // with the resolved type 'i' (signed int).
310 // CHECK: define {{.*}} @_ZN6test171hILi4EJLi1ELi2ELi3EEEEvNS_1XIXspT0_ELj5EXT_ELj6EEE
h(X<C...,5u,D,6u>)311 template<int D, int ...C> void h(X<C..., 5u, D, 6u>) {}
i()312 void i() { h<4, 1, 2, 3>({}); }
313
314 #if __cplusplus >= 201402L
315 template<int A, const volatile int*> struct Y {};
316 int n;
317 // Case 1: &n is a resolved template argument, with a known parameter:
318 // mangled with no conversion.
319 // CXX20: define {{.*}} @_ZN6test172j1ILi1EEEvNS_1YIXT_EXadL_ZNS_1nEEEEE
j1(Y<N,(const int *)& n>)320 template<int N> void j1(Y<N, (const int*)&n>) {}
321 // Case 2: &n is an unresolved template argument, with an unknown
322 // corresopnding parameter: mangled as the source expression.
323 // CXX20: define {{.*}} @_ZN6test172j2IJLi1EEEEvNS_1YIXspT_EXcvPKiadL_ZNS_1nEEEEE
j2(Y<Ns...,(const int *)& n>)324 template<int ...Ns> void j2(Y<Ns..., (const int*)&n>) {}
325 // Case 3: &n is a resolved template argument, with a known parameter, but
326 // for a template that can be overloaded on type: mangled with the parameter type.
327 // CXX20: define {{.*}} @_ZN6test172j3ILi1EEEvDTplT_clL_ZNS_1yIXcvPVKiadL_ZNS_1nEEEEEivEEE
328 template<const volatile int*> int y();
329 template<int N> void j3(decltype(N + y<(const int*)&n>())) {}
k()330 void k() {
331 j1<1>(Y<1, &n>());
332 j2<1>(Y<1, &n>());
333 j3<1>(0);
334 }
335 #endif
336 }
337
338 namespace partially_dependent_template_args {
339 namespace test1 {
340 template<bool B> struct enable { using type = int; };
341 template<typename ...> struct and_ { static constexpr bool value = true; };
f(T)342 template<typename T> inline typename enable<and_<T, T, T>::value>::type f(T) {}
343 // FIXME: GCC and ICC form a J...E mangling for the pack here. Clang
344 // doesn't do so when mangling an <unresolved-prefix>. It's not clear who's
345 // right. See https://github.com/itanium-cxx-abi/cxx-abi/issues/113.
346 // CHECK: @_ZN33partially_dependent_template_args5test11fIiEENS0_6enableIXsr4and_IT_S3_S3_EE5valueEE4typeES3_
g()347 void g() { f(0); }
348 }
349
350 namespace test2 {
351 struct X { int n; };
352 template<unsigned> int f(X);
353
354 template<typename T> void g1(decltype(f<0>(T()))) {}
355 template<typename T> void g2(decltype(f<0>({}) + T())) {}
356 template<typename T> void g3(decltype(f<0>(X{}) + T())) {}
357 template<int N> void g4(decltype(f<0>(X{N})));
358
359 // The first of these mangles the unconverted argument Li0E because the
360 // callee is unresolved, the rest mangle the converted argument Lj0E
361 // because the callee is resolved.
h()362 void h() {
363 // CHECK: @_ZN33partially_dependent_template_args5test22g1INS0_1XEEEvDTcl1fILi0EEcvT__EEE
364 g1<X>({});
365 // CHECK: @_ZN33partially_dependent_template_args5test22g2IiEEvDTplclL_ZNS0_1fILj0EEEiNS0_1XEEilEEcvT__EE
366 g2<int>({});
367 // CHECK: @_ZN33partially_dependent_template_args5test22g3IiEEvDTplclL_ZNS0_1fILj0EEEiNS0_1XEEtlS3_EEcvT__EE
368 g3<int>({});
369 // CHECK: @_ZN33partially_dependent_template_args5test22g4ILi0EEEvDTclL_ZNS0_1fILj0EEEiNS0_1XEEtlS3_T_EEE
370 g4<0>({});
371 }
372 }
373 }
374
375 namespace fixed_size_parameter_pack {
376 template<typename ...T> struct A {
377 template<T ...> struct B {};
378 };
379 template<int ...Ns> void f(A<unsigned, char, long long>::B<0, Ns...>);
g()380 void g() { f<1, 2>({}); }
381 }
382
383 namespace type_qualifier {
384 template<typename T> using int_t = int;
385 template<typename T> void f(decltype(int_t<T*>() + 1)) {}
386 // FIXME: This mangling doesn't work: we need to mangle the
387 // instantiation-dependent 'int_t' operand.
388 // CHECK: @_ZN14type_qualifier1fIPiEEvDTplcvi_ELi1EE
389 template void f<int*>(int);
390
391 // Note that this template has different constraints but would mangle the
392 // same:
393 //template<typename T> void f(decltype(int_t<typename T::type>() + 1)) {}
394
395 struct impl { using type = void; };
396 template<typename T> using alias = impl;
397 template<typename T> void g(decltype(alias<T*>::type(), 1)) {}
398 // FIXME: Similarly we need to mangle the `T*` in here.
399 // CHECK: @_ZN14type_qualifier1gIPiEEvDTcmcvv_ELi1EE
400 template void g<int*>(int);
401 }
402