xref: /llvm-project/clang/test/CodeGenCXX/cxx2b-static-call-operator.cpp (revision ee01a2c3996f9647f3158f5acdb921a6ede94dc1)
1 // RUN: %clang_cc1 -std=c++23 %s -emit-llvm -triple x86_64-linux -o - | FileCheck %s
2 // RUN: %clang_cc1 -std=c++23 %s -emit-llvm -triple x86_64-windows-msvc -o - | FileCheck %s
3 
4 struct Functor {
operator ()Functor5   static int operator()(int x, int y) {
6     return x + y;
7   }
8 };
9 
GetALambda()10 auto GetALambda() {
11   return [](int x, int y) static {
12     return x + y;
13   };
14 }
15 
CallsTheLambda()16 void CallsTheLambda() {
17   GetALambda()(1, 2);
18 }
19 
20 // CHECK:      define {{.*}}CallsTheLambda{{.*}}
21 // CHECK-NEXT: entry:
22 // CHECK:        {{.*}}call {{.*}}GetALambda{{.*}}()
23 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}(i32 noundef 1, i32 noundef 2)
24 // CHECK-NEXT:   ret void
25 // CHECK-NEXT: }
26 
GetAFunctor()27 Functor GetAFunctor() {
28   return {};
29 }
30 
call_static_call_operator()31 void call_static_call_operator() {
32   Functor f;
33   f(101, 102);
34   f.operator()(201, 202);
35   Functor{}(301, 302);
36   Functor::operator()(401, 402);
37   GetAFunctor()(501, 502);
38 }
39 
40 // CHECK:      define {{.*}}call_static_call_operator{{.*}}
41 // CHECK-NEXT: entry:
42 // CHECK:        {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 101, i32 noundef 102)
43 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 201, i32 noundef 202)
44 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 301, i32 noundef 302)
45 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 401, i32 noundef 402)
46 // CHECK:        {{.*}}call {{.*}}GetAFunctor{{.*}}()
47 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 501, i32 noundef 502)
48 // CHECK-NEXT:   ret void
49 // CHECK-NEXT: }
50 
51 struct FunctorConsteval {
operator ()FunctorConsteval52   consteval static int operator()(int x, int y) {
53       return x + y;
54   }
55 };
56 
57 struct FunctorConstexpr {
operator ()FunctorConstexpr58   constexpr static int operator()(int x, int y) {
59       return x + y;
60   }
61 };
62 
__anonc8d4a32b0202() 63 constexpr auto my_lambda = []() constexpr {
64   return 3;
65 };
66 
test_consteval_constexpr()67 void test_consteval_constexpr() {
68   int x = 0;
69   int y = FunctorConstexpr{}(x, 2);
70   constexpr int z1 = FunctorConsteval{}(2, 2);
71   constexpr int z2 = FunctorConstexpr{}(2, 2);
72 
73   static_assert(z1 == 4);
74   static_assert(z2 == 4);
75 
76   constexpr auto my_lambda = []() constexpr static {
77       return 3;
78   };
79   constexpr int (*f)(void) = my_lambda;
80   constexpr int k = f();
81   static_assert(k == 3);
82 }
83 
84 template <class T>
85 struct DepFunctor {
operator ()DepFunctor86   static int operator()(T t) {
87     return int(t);
88   }
89 };
90 
91 template<class T>
dep_lambda1()92 auto dep_lambda1() {
93   return [](T t) static -> int {
94     return t;
95   };
96 }
97 
dep_lambda2()98 auto dep_lambda2() {
99   return [](auto t) static -> int {
100     return t;
101   };
102 }
103 
test_dep_functors()104 void test_dep_functors() {
105   int x = DepFunctor<float>{}(1.0f);
106   int y = DepFunctor<bool>{}(true);
107 
108   int a = dep_lambda1<float>()(1.0f);
109   int b = dep_lambda1<bool>()(true);
110 
111   int h = dep_lambda2()(1.0f);
112   int i = dep_lambda2()(true);
113 }
114 
115 // CHECK:      define {{.*}}test_dep_functors{{.*}}
116 // CHECK-NEXT: entry:
117 // CHECK:        {{.*}} = call noundef i32 {{.*}}DepFunctor{{.*}}(float noundef 1.000000e+00)
118 // CHECK:        {{.*}} = call noundef i32 {{.*}}DepFunctor{{.*}}(i1 noundef zeroext true)
119 // CHECK:        {{.*}}call {{.*}}dep_lambda1{{.*}}()
120 // CHECK:        {{.*}} = call noundef i32 {{.*}}dep_lambda1{{.*}}(float noundef 1.000000e+00)
121 // CHECK:        {{.*}}call {{.*}}dep_lambda1{{.*}}()
122 // CHECK:        {{.*}} = call noundef i32 {{.*}}dep_lambda1{{.*}}(i1 noundef zeroext true)
123 // CHECK:        {{.*}}call {{.*}}dep_lambda2{{.*}}()
124 // CHECK:        {{.*}} = call noundef i32 {{.*}}dep_lambda2{{.*}}(float noundef 1.000000e+00)
125 // CHECK:        {{.*}}call {{.*}}dep_lambda2{{.*}}()
126 // CHECK:        {{.*}} = call noundef i32 {{.*}}dep_lambda2{{.*}}(i1 noundef zeroext true)
127 // CHECK:        ret void
128 // CHECK-NEXT: }
129 
130 
131 struct __unique {
operator ()__unique132     static constexpr auto operator()() { return 4; };
133 
134     using P = int();
operator P*__unique135     constexpr operator P*() { return operator(); }
136 };
137 
138 __unique four{};
139 
test_four()140 int test_four() {
141   // Checks that overload resolution works.
142   return four();
143 }
144