xref: /llvm-project/clang/test/CodeGenCXX/cxx2b-static-subscript-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 
GetAFunctor()10 Functor GetAFunctor() {
11   return {};
12 }
13 
call_static_subscript_operator()14 void call_static_subscript_operator() {
15   Functor f;
16   f[101, 102];
17   f.operator[](201, 202);
18   Functor{}[301, 302];
19   Functor::operator[](401, 402);
20   GetAFunctor()[501, 502];
21 }
22 
23 // CHECK:      define {{.*}}call_static_subscript_operator{{.*}}
24 // CHECK-NEXT: entry:
25 // CHECK:        {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 101, i32 noundef 102)
26 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 201, i32 noundef 202)
27 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 301, i32 noundef 302)
28 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 401, i32 noundef 402)
29 // CHECK:        {{.*}}call {{.*}}GetAFunctor{{.*}}()
30 // CHECK-NEXT:   {{.*}} = call noundef i32 {{.*}}Functor{{.*}}(i32 noundef 501, i32 noundef 502)
31 // CHECK-NEXT:   ret void
32 // CHECK-NEXT: }
33 
34 struct FunctorConsteval {
operator []FunctorConsteval35   consteval static int operator[](int x, int y) {
36       return x + y;
37   }
38 };
39 
40 struct FunctorConstexpr {
operator []FunctorConstexpr41   constexpr static int operator[](int x, int y) {
42       return x + y;
43   }
44 };
45 
test_consteval_constexpr()46 void test_consteval_constexpr() {
47   int x = 0;
48   int y = FunctorConstexpr{}[x, 2];
49   constexpr int z1 = FunctorConsteval{}[2, 2];
50   constexpr int z2 = FunctorConstexpr{}[2, 2];
51 
52   static_assert(z1 == 4);
53   static_assert(z2 == 4);
54 }
55 
56 template <class T>
57 struct DepFunctor {
operator []DepFunctor58   static int operator[](T t) {
59     return int(t);
60   }
61 };
62 
test_dep_functors()63 void test_dep_functors() {
64   int x = DepFunctor<float>{}[1.0f];
65   int y = DepFunctor<bool>{}[true];
66 }
67 
68 // CHECK:      define {{.*}}test_dep_functors{{.*}}
69 // CHECK-NEXT: entry:
70 // CHECK:        {{.*}} = call noundef i32 {{.*}}DepFunctor{{.*}}(float noundef 1.000000e+00)
71 // CHECK:        {{.*}} = call noundef i32 {{.*}}DepFunctor{{.*}}(i1 noundef zeroext true)
72 // CHECK:        ret void
73 // CHECK-NEXT: }
74