xref: /minix3/external/bsd/llvm/dist/clang/test/CodeGenCXX/mangle-lambdas.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-macosx10.7.0 -emit-llvm -o - %s | FileCheck %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // CHECK: @_ZZNK7PR12917IJiiEE1nMUlvE_clEvE1n = linkonce_odr global i32 0
4*f4a2713aSLionel Sambuc // CHECK: @_ZZZN7PR12917IJicdEEC1EicdEd_NKUlvE_clEvE1n = linkonce_odr global i32 0
5*f4a2713aSLionel Sambuc // CHECK: @_ZZZN7PR12917IJicdEEC1EicdEd0_NKUlvE_clEvE1n = linkonce_odr global i32 0
6*f4a2713aSLionel Sambuc // CHECK: @_ZZZN7PR12917IJicdEEC1EicdEd1_NKUlvE_clEvE1n = linkonce_odr global i32 0
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr void @_Z11inline_funci
9*f4a2713aSLionel Sambuc inline void inline_func(int n) {
10*f4a2713aSLionel Sambuc   // CHECK: call i32 @_ZZ11inline_funciENKUlvE_clEv
11*f4a2713aSLionel Sambuc   int i = []{ return 1; }();
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc   // CHECK: call i32 @_ZZ11inline_funciENKUlvE0_clEv
14*f4a2713aSLionel Sambuc   int j = [=] { return n + i; }();
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc   // CHECK: call double @_ZZ11inline_funciENKUlvE1_clEv
17*f4a2713aSLionel Sambuc   int k = [=] () -> double { return n + i; }();
18*f4a2713aSLionel Sambuc 
19*f4a2713aSLionel Sambuc   // CHECK: call i32 @_ZZ11inline_funciENKUliE_clEi
20*f4a2713aSLionel Sambuc   int l = [=] (int x) -> int { return x + i; }(n);
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc   int inner(int i = []{ return 17; }());
23*f4a2713aSLionel Sambuc   // CHECK: call i32 @_ZZ11inline_funciENKUlvE2_clEv
24*f4a2713aSLionel Sambuc   // CHECK-NEXT: call i32 @_Z5inneri
25*f4a2713aSLionel Sambuc   inner();
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
28*f4a2713aSLionel Sambuc }
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc void call_inline_func() {
31*f4a2713aSLionel Sambuc   inline_func(17);
32*f4a2713aSLionel Sambuc }
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc struct S {
35*f4a2713aSLionel Sambuc   void f(int = []{return 1;}()
36*f4a2713aSLionel Sambuc              + []{return 2;}(),
37*f4a2713aSLionel Sambuc          int = []{return 3;}());
38*f4a2713aSLionel Sambuc   void g(int, int);
39*f4a2713aSLionel Sambuc };
40*f4a2713aSLionel Sambuc 
41*f4a2713aSLionel Sambuc void S::g(int i = []{return 1;}(),
42*f4a2713aSLionel Sambuc           int j = []{return 2; }()) {}
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_Z6test_S1S
45*f4a2713aSLionel Sambuc void test_S(S s) {
46*f4a2713aSLionel Sambuc   // CHECK: call i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
47*f4a2713aSLionel Sambuc   // CHECK-NEXT: call i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
48*f4a2713aSLionel Sambuc   // CHECK-NEXT: add nsw i32
49*f4a2713aSLionel Sambuc   // CHECK-NEXT: call i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
50*f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN1S1fEii
51*f4a2713aSLionel Sambuc   s.f();
52*f4a2713aSLionel Sambuc 
53*f4a2713aSLionel Sambuc   // NOTE: These manglings don't actually matter that much, because
54*f4a2713aSLionel Sambuc   // the lambdas in the default arguments of g() won't be seen by
55*f4a2713aSLionel Sambuc   // multiple translation units. We check them mainly to ensure that they don't
56*f4a2713aSLionel Sambuc   // get the special mangling for lambdas in in-class default arguments.
57*f4a2713aSLionel Sambuc   // CHECK: call i32 @"_ZNK1S3$_0clEv"
58*f4a2713aSLionel Sambuc   // CHECK-NEXT: call i32 @"_ZNK1S3$_1clEv"
59*f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN1S1gEi
60*f4a2713aSLionel Sambuc   s.g();
61*f4a2713aSLionel Sambuc 
62*f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
63*f4a2713aSLionel Sambuc }
64*f4a2713aSLionel Sambuc 
65*f4a2713aSLionel Sambuc // Check the linkage of the lambda call operators used in test_S.
66*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd0_NKUlvE_clEv
67*f4a2713aSLionel Sambuc // CHECK: ret i32 1
68*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd0_NKUlvE0_clEv
69*f4a2713aSLionel Sambuc // CHECK: ret i32 2
70*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZN1S1fEiiEd_NKUlvE_clEv
71*f4a2713aSLionel Sambuc // CHECK: ret i32 3
72*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZNK1S3$_0clEv"
73*f4a2713aSLionel Sambuc // CHECK: ret i32 1
74*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZNK1S3$_1clEv"
75*f4a2713aSLionel Sambuc // CHECK: ret i32 2
76*f4a2713aSLionel Sambuc 
77*f4a2713aSLionel Sambuc template<typename T>
78*f4a2713aSLionel Sambuc struct ST {
79*f4a2713aSLionel Sambuc   void f(T = []{return T() + 1;}()
80*f4a2713aSLionel Sambuc            + []{return T() + 2;}(),
81*f4a2713aSLionel Sambuc          T = []{return T(3);}());
82*f4a2713aSLionel Sambuc };
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_Z7test_ST2STIdE
85*f4a2713aSLionel Sambuc void test_ST(ST<double> st) {
86*f4a2713aSLionel Sambuc   // CHECK: call double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
87*f4a2713aSLionel Sambuc   // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
88*f4a2713aSLionel Sambuc   // CHECK-NEXT: fadd double
89*f4a2713aSLionel Sambuc   // CHECK-NEXT: call double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
90*f4a2713aSLionel Sambuc   // CHECK-NEXT: call void @_ZN2STIdE1fEdd
91*f4a2713aSLionel Sambuc   st.f();
92*f4a2713aSLionel Sambuc 
93*f4a2713aSLionel Sambuc   // CHECK-NEXT: ret void
94*f4a2713aSLionel Sambuc }
95*f4a2713aSLionel Sambuc 
96*f4a2713aSLionel Sambuc // Check the linkage of the lambda call operators used in test_ST.
97*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE_clEv
98*f4a2713aSLionel Sambuc // CHECK: ret double 1
99*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr double @_ZZN2STIdE1fEddEd0_NKUlvE0_clEv
100*f4a2713aSLionel Sambuc // CHECK: ret double 2
101*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr double @_ZZN2STIdE1fEddEd_NKUlvE_clEv
102*f4a2713aSLionel Sambuc // CHECK: ret double 3
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc template<typename T>
105*f4a2713aSLionel Sambuc struct StaticMembers {
106*f4a2713aSLionel Sambuc   static T x;
107*f4a2713aSLionel Sambuc   static T y;
108*f4a2713aSLionel Sambuc   static T z;
109*f4a2713aSLionel Sambuc   static int (*f)();
110*f4a2713aSLionel Sambuc };
111*f4a2713aSLionel Sambuc 
112*f4a2713aSLionel Sambuc template<typename T> int accept_lambda(T);
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc template<typename T>
115*f4a2713aSLionel Sambuc T StaticMembers<T>::x = []{return 1;}() + []{return 2;}();
116*f4a2713aSLionel Sambuc 
117*f4a2713aSLionel Sambuc template<typename T>
118*f4a2713aSLionel Sambuc T StaticMembers<T>::y = []{return 3;}();
119*f4a2713aSLionel Sambuc 
120*f4a2713aSLionel Sambuc template<typename T>
121*f4a2713aSLionel Sambuc T StaticMembers<T>::z = accept_lambda([]{return 4;});
122*f4a2713aSLionel Sambuc 
123*f4a2713aSLionel Sambuc template<typename T>
124*f4a2713aSLionel Sambuc int (*StaticMembers<T>::f)() = []{return 5;};
125*f4a2713aSLionel Sambuc 
126*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal void @__cxx_global_var_init()
127*f4a2713aSLionel Sambuc // CHECK: call i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
128*f4a2713aSLionel Sambuc // CHECK-NEXT: call i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
129*f4a2713aSLionel Sambuc // CHECK-NEXT: add nsw
130*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZNK13StaticMembersIfE1xMUlvE_clEv
131*f4a2713aSLionel Sambuc // CHECK: ret i32 1
132*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZNK13StaticMembersIfE1xMUlvE0_clEv
133*f4a2713aSLionel Sambuc // CHECK: ret i32 2
134*f4a2713aSLionel Sambuc template float StaticMembers<float>::x;
135*f4a2713aSLionel Sambuc 
136*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal void @__cxx_global_var_init1()
137*f4a2713aSLionel Sambuc // CHECK: call i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
138*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZNK13StaticMembersIfE1yMUlvE_clEv
139*f4a2713aSLionel Sambuc // CHECK: ret i32 3
140*f4a2713aSLionel Sambuc template float StaticMembers<float>::y;
141*f4a2713aSLionel Sambuc 
142*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal void @__cxx_global_var_init2()
143*f4a2713aSLionel Sambuc // CHECK: call i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_
144*f4a2713aSLionel Sambuc // CHECK: declare i32 @_Z13accept_lambdaIN13StaticMembersIfE1zMUlvE_EEiT_()
145*f4a2713aSLionel Sambuc template float StaticMembers<float>::z;
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal void @__cxx_global_var_init3()
148*f4a2713aSLionel Sambuc // CHECK: call {{.*}} @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
149*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 ()* @_ZNK13StaticMembersIfE1fMUlvE_cvPFivEEv
150*f4a2713aSLionel Sambuc template int (*StaticMembers<float>::f)();
151*f4a2713aSLionel Sambuc 
152*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal void @__cxx_global_var_init4
153*f4a2713aSLionel Sambuc // CHECK: call i32 @"_ZNK13StaticMembersIdE3$_2clEv"
154*f4a2713aSLionel Sambuc // CHECK-LABEL: define internal i32 @"_ZNK13StaticMembersIdE3$_2clEv"
155*f4a2713aSLionel Sambuc // CHECK: ret i32 42
156*f4a2713aSLionel Sambuc template<> double StaticMembers<double>::z = []{return 42; }();
157*f4a2713aSLionel Sambuc 
158*f4a2713aSLionel Sambuc template<typename T>
159*f4a2713aSLionel Sambuc void func_template(T = []{ return T(); }());
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc // CHECK-LABEL: define void @_Z17use_func_templatev()
162*f4a2713aSLionel Sambuc void use_func_template() {
163*f4a2713aSLionel Sambuc   // CHECK: call i32 @"_ZZ13func_templateIiEvT_ENK3$_3clEv"
164*f4a2713aSLionel Sambuc   func_template<int>();
165*f4a2713aSLionel Sambuc }
166*f4a2713aSLionel Sambuc 
167*f4a2713aSLionel Sambuc 
168*f4a2713aSLionel Sambuc template<typename...T> struct PR12917 {
169*f4a2713aSLionel Sambuc   PR12917(T ...t = []{ static int n = 0; return ++n; }());
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc   static int n[3];
172*f4a2713aSLionel Sambuc };
173*f4a2713aSLionel Sambuc template<typename...T> int PR12917<T...>::n[3] = {
174*f4a2713aSLionel Sambuc   []{ static int n = 0; return ++n; }()
175*f4a2713aSLionel Sambuc };
176*f4a2713aSLionel Sambuc 
177*f4a2713aSLionel Sambuc // CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd1_NKUlvE_clEv(
178*f4a2713aSLionel Sambuc // CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd0_NKUlvE_clEv(
179*f4a2713aSLionel Sambuc // CHECK: call i32 @_ZZN7PR12917IJicdEEC1EicdEd_NKUlvE_clEv(
180*f4a2713aSLionel Sambuc // CHECK: call void @_ZN7PR12917IJicdEEC1Eicd(
181*f4a2713aSLionel Sambuc PR12917<int, char, double> pr12917;
182*f4a2713aSLionel Sambuc int *pr12917_p = PR12917<int, int>::n;
183*f4a2713aSLionel Sambuc 
184*f4a2713aSLionel Sambuc namespace std {
185*f4a2713aSLionel Sambuc   struct type_info;
186*f4a2713aSLionel Sambuc }
187*f4a2713aSLionel Sambuc namespace PR12123 {
188*f4a2713aSLionel Sambuc   struct A { virtual ~A(); } g;
189*f4a2713aSLionel Sambuc   struct B {
190*f4a2713aSLionel Sambuc     void f(const std::type_info& x = typeid([]()->A& { return g; }()));
191*f4a2713aSLionel Sambuc     void h();
192*f4a2713aSLionel Sambuc   };
193*f4a2713aSLionel Sambuc   void B::h() { f(); }
194*f4a2713aSLionel Sambuc }
195*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr %"struct.PR12123::A"* @_ZZN7PR121231B1fERKSt9type_infoEd_NKUlvE_clEv
196*f4a2713aSLionel Sambuc 
197*f4a2713aSLionel Sambuc namespace PR12808 {
198*f4a2713aSLionel Sambuc   template <typename> struct B {
199*f4a2713aSLionel Sambuc     int a;
200*f4a2713aSLionel Sambuc     template <typename L> constexpr B(L&& x) : a(x()) { }
201*f4a2713aSLionel Sambuc   };
202*f4a2713aSLionel Sambuc   template <typename> void b(int) {
203*f4a2713aSLionel Sambuc     [&]{ (void)B<int>([&]{ return 1; }); }();
204*f4a2713aSLionel Sambuc   }
205*f4a2713aSLionel Sambuc   void f() {
206*f4a2713aSLionel Sambuc     b<int>(1);
207*f4a2713aSLionel Sambuc   }
208*f4a2713aSLionel Sambuc   // CHECK-LABEL: define linkonce_odr void @_ZZN7PR128081bIiEEviENKUlvE_clEv
209*f4a2713aSLionel Sambuc   // CHECK-LABEL: define linkonce_odr i32 @_ZZZN7PR128081bIiEEviENKUlvE_clEvENKUlvE_clEv
210*f4a2713aSLionel Sambuc }
211*f4a2713aSLionel Sambuc 
212*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr void @_Z1fIZZNK23TestNestedInstantiationclEvENKUlvE_clEvEUlvE_EvT_
213*f4a2713aSLionel Sambuc 
214*f4a2713aSLionel Sambuc struct Members {
215*f4a2713aSLionel Sambuc   int x = [] { return 1; }() + [] { return 2; }();
216*f4a2713aSLionel Sambuc   int y = [] { return 3; }();
217*f4a2713aSLionel Sambuc };
218*f4a2713aSLionel Sambuc 
219*f4a2713aSLionel Sambuc void test_Members() {
220*f4a2713aSLionel Sambuc   // CHECK-LABEL: define linkonce_odr void @_ZN7MembersC2Ev
221*f4a2713aSLionel Sambuc   // CHECK: call i32 @_ZNK7Members1xMUlvE_clEv
222*f4a2713aSLionel Sambuc   // CHECK-NEXT: call i32 @_ZNK7Members1xMUlvE0_clE
223*f4a2713aSLionel Sambuc   // CHECK-NEXT: add nsw i32
224*f4a2713aSLionel Sambuc   // CHECK: call i32 @_ZNK7Members1yMUlvE_clEv
225*f4a2713aSLionel Sambuc   Members members;
226*f4a2713aSLionel Sambuc   // CHECK: ret void
227*f4a2713aSLionel Sambuc }
228*f4a2713aSLionel Sambuc 
229*f4a2713aSLionel Sambuc template<typename P> void f(P) { }
230*f4a2713aSLionel Sambuc 
231*f4a2713aSLionel Sambuc struct TestNestedInstantiation {
232*f4a2713aSLionel Sambuc    void operator()() const {
233*f4a2713aSLionel Sambuc      []() -> void {
234*f4a2713aSLionel Sambuc        return f([]{});
235*f4a2713aSLionel Sambuc      }();
236*f4a2713aSLionel Sambuc    }
237*f4a2713aSLionel Sambuc };
238*f4a2713aSLionel Sambuc 
239*f4a2713aSLionel Sambuc void test_NestedInstantiation() {
240*f4a2713aSLionel Sambuc   TestNestedInstantiation()();
241*f4a2713aSLionel Sambuc }
242*f4a2713aSLionel Sambuc 
243*f4a2713aSLionel Sambuc // Check the linkage of the lambdas used in test_Members.
244*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1xMUlvE_clEv
245*f4a2713aSLionel Sambuc // CHECK: ret i32 1
246*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1xMUlvE0_clEv
247*f4a2713aSLionel Sambuc // CHECK: ret i32 2
248*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZNK7Members1yMUlvE_clEv
249*f4a2713aSLionel Sambuc // CHECK: ret i32 3
250*f4a2713aSLionel Sambuc 
251*f4a2713aSLionel Sambuc // Check linkage of the various lambdas.
252*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE_clEv
253*f4a2713aSLionel Sambuc // CHECK: ret i32 1
254*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE0_clEv
255*f4a2713aSLionel Sambuc // CHECK: ret i32
256*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr double @_ZZ11inline_funciENKUlvE1_clEv
257*f4a2713aSLionel Sambuc // CHECK: ret double
258*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUliE_clEi
259*f4a2713aSLionel Sambuc // CHECK: ret i32
260*f4a2713aSLionel Sambuc // CHECK-LABEL: define linkonce_odr i32 @_ZZ11inline_funciENKUlvE2_clEv
261*f4a2713aSLionel Sambuc // CHECK: ret i32 17
262