[LoongArch][Clang] Make the parameters and return value of {x,}vorn.v builti ns ...
[llvm-project.git] / clang / test / CodeGenCXX / cxx2b-deducing-this.cpp
blob1c8835a3986ea0efc799f6f19d04d6d36a0e6a45
1 // RUN: %clang_cc1 -std=c++2b %s -emit-llvm -triple x86_64-linux -o - | FileCheck %s
3 struct TrivialStruct {
4 void explicit_object_function(this TrivialStruct) {}
5 };
6 void test() {
7 TrivialStruct s;
8 s.explicit_object_function();
10 // CHECK: define {{.*}}test{{.*}}
11 // CHECK-NEXT: entry:
12 // CHECK: {{.*}} = alloca %struct.TrivialStruct, align 1
13 // CHECK: {{.*}} = alloca %struct.TrivialStruct, align 1
14 // CHECK: call void {{.*}}explicit_object_function{{.*}}
15 // CHECK-NEXT: ret void
16 // CHECK-NEXT: }
18 // CHECK: define {{.*}}explicit_object_function{{.*}}
19 // CHECK-NEXT: entry:
20 // CHECK: {{.*}} = alloca %struct.TrivialStruct, align 1
21 // CHECK: ret void
22 // CHECK-NEXT: }
25 void test_lambda() {
26 [](this auto This) -> int {
27 return This();
28 }();
31 //CHECK: define dso_local void @{{.*}}test_lambda{{.*}}() #0 {
32 //CHECK: entry:
33 //CHECK: %agg.tmp = alloca %class.anon, align 1
34 //CHECK: %ref.tmp = alloca %class.anon, align 1
35 //CHECK: %call = call noundef i32 @"_ZZ11test_lambdavENH3$_0clIS_EEiT_"()
36 //CHECK: ret void
37 //CHECK: }
39 //CHECK: define internal noundef i32 @"_ZZ11test_lambdavENH3$_0clIS_EEiT_"() #0 align 2 {
40 //CHECK: entry:
41 //CHECK: %This = alloca %class.anon, align 1
42 //CHECK: %agg.tmp = alloca %class.anon, align 1
43 //CHECK: %call = call noundef i32 @"_ZZ11test_lambdavENH3$_0clIS_EEiT_"()
44 //CHECK: ret i32 %call
45 //CHECK: }
47 void test_lambda_ref() {
48 auto l = [i = 42](this auto & This, int j) -> int {
49 return This(j);
51 l(0);
54 // CHECK: define dso_local void @_Z15test_lambda_refv() #0 {
55 // CHECK: entry:
56 // CHECK: %[[This_address:.]] = alloca %class.anon{{.*}}, align 4
57 // CHECK: %[[i_addr:.*]] = getelementptr inbounds nuw %class.anon{{.*}}, ptr %[[This_address]], i32 0, i32 0
58 // CHECK: store i32 42, ptr %[[i_addr]], align 4
59 // CHECK: %call = call noundef i32 @"_ZZ15test_lambda_refvENH3$_0clIS_EEiRT_i"{{.*}}
60 // CHECK: ret void
61 // CHECK: }
63 // CHECK: define internal noundef i32 @"_ZZ15test_lambda_refvENH3$_0clIS_EEiRT_i"{{.*}}
64 // CHECK: entry:
65 // CHECK: %This.addr = alloca ptr, align 8
66 // CHECK: %j.addr = alloca i32, align 4
67 // CHECK: store ptr %This, ptr %This.addr, align 8
68 // CHECK: store i32 %j, ptr %j.addr, align 4
69 // CHECK: %[[this_addr:.*]] = load ptr, ptr %This.addr, align 8
70 // CHECK: %[[j_addr:.*]] = load i32, ptr %j.addr, align 4
71 // CHECK: %call = call noundef i32 @"_ZZ15test_lambda_refvENH3$_0clIS_EEiRT_i"(ptr noundef nonnull align 4 dereferenceable(4) %[[this_addr]], i32 noundef %[[j_addr]])
72 // CHECK: ret i32 %call
73 // CHECK: }
76 struct TestPointer {
77 void f(this TestPointer &);
80 void test_pointer() {
81 TestPointer t;
82 using Fn = void(TestPointer&);
83 Fn* fn = &TestPointer::f;
84 fn(t);
86 //CHECK: define dso_local void @_Z12test_pointerv() #0 {
87 //CHECK-NEXT: entry:
88 //CHECK-NEXT: %t = alloca %struct.TestPointer, align 1
89 //CHECK-NEXT: %fn = alloca ptr, align 8
90 //CHECK-NEXT: store ptr @_ZNH11TestPointer1fERS_, ptr %fn, align 8
91 //CHECK: %[[fn_ptr:.*]] = load ptr, ptr %fn, align 8
92 //CHECK-NEXT: call void %[[fn_ptr]](ptr noundef nonnull align 1 dereferenceable(1) %t)
93 //CHECK-NEXT: ret void
94 //CHECK-NEXT: }
97 struct MaterializedTemporary {
98 void foo(this MaterializedTemporary&&);
99 MaterializedTemporary();
100 ~MaterializedTemporary();
103 void test_temporary() {
104 MaterializedTemporary{}.foo();
107 //CHECK: define dso_local void @_Z14test_temporaryv(){{.*}}
108 //CHECK-NEXT: entry:
109 //CHECK: %ref.tmp = alloca %struct.MaterializedTemporary, align 1
110 //CHECK: call void @_ZN21MaterializedTemporaryC1Ev(ptr noundef nonnull align 1 dereferenceable(1) %ref.tmp){{.*}}
111 //CHECK invoke void @_ZNH21MaterializedTemporary3fooEOS_(ptr noundef nonnull align 1 dereferenceable(1) %ref.tmp){{.*}}
113 namespace GH86399 {
114 volatile int a = 0;
115 struct function {
116 function& operator=(function const&) {
117 a = 1;
118 return *this;
122 void f() {
123 function list;
125 //CHECK-LABEL: define internal void @"_ZZN7GH863991f{{.*}}"(ptr %{{.*}})
126 //CHECK: call {{.*}} @_ZN7GH863998functionaSERKS0_
127 //CHECK-NEXT: ret void
128 [&list](this auto self) {
129 list = function{};
130 }();
134 namespace GH84163 {
135 // Just check that this doesn't crash (we were previously not instantiating
136 // everything that needs instantiating in here).
137 template <typename> struct S {};
139 void a() {
140 int x;
141 const auto l = [&x](this auto&) { S<decltype(x)> q; };
142 l();
146 namespace GH84425 {
147 // As above.
148 void do_thing(int x) {
149 auto second = [&](this auto const& self, int b) -> int {
150 if (x) return x;
151 else return self(x);
154 second(1);
157 void do_thing2(int x) {
158 auto second = [&](this auto const& self) {
159 if (true) return x;
160 else return x;
163 second();
167 namespace GH79754 {
168 // As above.
169 void f() {
170 int x;
171 [&x](this auto&&) {return x;}();
175 namespace GH70604 {
176 auto dothing(int num)
178 auto fun = [&num](this auto&& self) -> void {
179 auto copy = num;
182 fun();
186 namespace GH87210 {
187 template <typename... Ts>
188 struct Overloaded : Ts... {
189 using Ts::operator()...;
192 template <typename... Ts>
193 Overloaded(Ts...) -> Overloaded<Ts...>;
195 // CHECK-LABEL: define dso_local void @_ZN7GH872101fEv()
196 // CHECK-NEXT: entry:
197 // CHECK-NEXT: [[X:%.*]] = alloca i32
198 // CHECK-NEXT: [[Over:%.*]] = alloca %"{{.*}}Overloaded"
199 // CHECK: call noundef ptr @"_ZZN7GH872101fEvENH3$_0clINS_10OverloadedIJS0_EEEEEDaRT_"(ptr {{.*}} [[Over]])
200 void f() {
201 int x;
202 Overloaded o {
203 // CHECK: define internal noundef ptr @"_ZZN7GH872101fEvENH3$_0clINS_10OverloadedIJS0_EEEEEDaRT_"(ptr {{.*}} [[Self:%.*]])
204 // CHECK-NEXT: entry:
205 // CHECK-NEXT: [[SelfAddr:%.*]] = alloca ptr
206 // CHECK-NEXT: store ptr [[Self]], ptr [[SelfAddr]]
207 // CHECK-NEXT: [[SelfPtr:%.*]] = load ptr, ptr [[SelfAddr]]
208 // CHECK-NEXT: [[XRef:%.*]] = getelementptr inbounds nuw %{{.*}}, ptr [[SelfPtr]], i32 0, i32 0
209 // CHECK-NEXT: [[X:%.*]] = load ptr, ptr [[XRef]]
210 // CHECK-NEXT: ret ptr [[X]]
211 [&](this auto& self) {
212 return &x;
215 o();
218 void g() {
219 int x;
220 Overloaded o {
221 [=](this auto& self) {
222 return x;
225 o();
229 namespace GH89541 {
230 // Same as above; just check that this doesn't crash.
231 int one = 1;
232 auto factory(int& x = one) {
233 return [&](this auto self) {
238 using Base = decltype(factory());
239 struct Derived : Base {
240 Derived() : Base(factory()) {}
243 void f() {
244 Derived d;
245 d();
250 namespace P2797 {
251 struct C {
252 void c(this const C&); // #first
253 void c() &; // #second
254 static void c(int = 0); // #third
256 void d() {
257 (&C::c)(C{});
258 (&C::c)();
261 void test() {
262 (void)C{}.d();
264 // CHECK-LABEL: {{.*}} @_ZN5P27971C1dEv
265 // CHECK: call void @_ZNH5P27971C1cERKS0_
266 // CHECK: call void @_ZN5P27971C1cEi