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