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