testsuite: Fix tail_call and musttail effective targets [PR116080]
[official-gcc.git] / gcc / testsuite / gcc.dg / c23-constexpr-1.c
blobe7eaf3dd5ab539ca253a983521b89ede7e10a3f0
1 /* Test C23 constexpr. Valid code, compilation tests. */
2 /* { dg-do compile } */
3 /* { dg-options "-std=c23 -pedantic-errors" } */
5 #include <float.h>
7 constexpr int v1 = 1;
8 static_assert (v1 == 1);
9 extern typeof (v1) *pci;
10 extern const int *pci;
11 extern typeof (&(constexpr int) {}) pci;
12 /* Redeclaring a constexpr object is OK (although it can't be declared before
13 the definition without undefined behavior). */
14 extern const int v1;
15 static_assert (v1 == 1);
16 unsigned int constexpr v2 = 2;
17 static_assert (v2 == 2);
18 extern typeof (v2) *pcui;
19 extern const unsigned int *pcui;
20 static constexpr char v3 = 3;
21 static_assert (v3 == 3);
22 extern typeof (v3) *pcc;
23 extern const char *pcc;
24 constexpr void *v4 = 0;
25 extern typeof (v4) *pcpv;
26 extern void *const *pcpv;
27 constexpr int *v5 = nullptr;
28 extern typeof (v5) *pcpi;
29 extern int *const *pcpi;
30 constexpr double v6 = 3.5;
31 extern typeof (v6) *pcd;
32 extern const double *pcd;
33 auto constexpr v7 = 1.0;
34 extern typeof (v7) *pcd;
35 constexpr auto v8 = 1.5f;
36 extern typeof (v8) *pcf;
37 extern const float *pcf;
38 constexpr static long v9 = 2ULL;
39 static_assert (v9 == 2);
40 extern typeof (v9) *pcl;
41 extern const long *pcl;
42 const short *v10 = &(constexpr short) { 10 };
43 /* Qualifiers that aren't permitted on a constexpr object itself are OK in a
44 pointer target. */
45 constexpr volatile int *v11 = nullptr;
46 extern typeof (v11) *pcpvi;
47 extern volatile int *const *pcpvi;
48 constexpr _Atomic int *v12 = nullptr;
49 extern typeof (v12) *pcpai;
50 extern _Atomic int *const *pcpai;
51 constexpr int *restrict *v13 = nullptr;
52 extern typeof (v13) cprpi;
53 extern int *restrict *const cprpi;
54 typedef int *P;
55 constexpr restrict P *v14 = nullptr;
56 extern typeof (v14) cprpi;
57 struct s15 { volatile int a; _Atomic int b; int *restrict p; };
58 constexpr struct s15 *v16 = nullptr;
59 constexpr char v17[3] = { 1, 2, 3 };
60 struct s18 { int a; int *b; double c; };
61 constexpr struct s18 v19 = { 12345ULL, 0, 19.0L };
62 static_assert (v19.a == 12345);
63 union u20 { int a; float b; };
64 constexpr union u20 v21 = { 1 };
65 static_assert (v21.a == 1);
66 constexpr union u20 v22 = { .b = 23.0 };
67 constexpr float v23 = (float) (1.0f / 3.0f);
68 constexpr double v24 = (double) (1.0 / 3.0);
69 constexpr struct s18 v25 = { 0, 0, (double) (1.0 / 3.0) };
70 static_assert (v25.a == 0);
71 constexpr char v26[] = "abc\xfe";
72 constexpr unsigned char v27[] = u8"xyz\xff";
73 constexpr unsigned char v28[] = "\x12\x7f";
74 constexpr signed char v29[] = "\x34\x66";
75 constexpr double v30 = (int) (double) 3.0 - (long) (double) 2.0;
76 constexpr int v31 = 1 + 2 + (int) 3.0;
77 static_assert (v31 == 6);
78 constexpr typeof (nullptr) v32 = nullptr;
79 constexpr _Complex double v33 = __builtin_complex (1.0f, 3.0f / 2.0f);
80 constexpr float v34 = 1234.0L;
81 constexpr char v35 = 127ULL;
82 #if FLT_MIN_EXP == -125 && FLT_MANT_DIG == 24
83 constexpr float v36 = 0x1p-149;
84 constexpr float _Complex v37 = __builtin_complex (0x1p-149, 0x1p127);
85 constexpr float v38 = 0xffffffUL;
86 constexpr float v39 = -0xffffffL;
87 constexpr float v40 = 0xffffff0L;
88 constexpr float v41 = 1ULL << 63;
89 #endif
90 #if DBL_MIN_EXP == -1021 && DBL_MANT_DIG == 53
91 constexpr double v42 = 0x1p-1074L;
92 constexpr _Complex double v43 = __builtin_complex (0x1p1023L, 0x1p-1074L);
93 constexpr double v44 = 0x1fffffffffffffULL;
94 constexpr double v45 = -0x1fffffffffffffLL;
95 constexpr double v46 = 0x3ffffffffffffeULL;
96 constexpr double v47 = 1ULL << 63;
97 #endif
98 constexpr void *v48 = (void *) 0;
99 constexpr int *v49 = (void *) 0L;
100 constexpr long *v50 = 0LL;
101 constexpr int v51 = {};
102 static_assert (v51 == 0);
103 constexpr float v52 = {};
104 constexpr long double v53 = {};
105 constexpr int *v54 = {};
106 constexpr void *v55 = {};
107 constexpr typeof (nullptr) v56 = {};
108 struct s57 { int *p; };
109 union u58 { int *p; };
110 constexpr int *v59 = 0;
111 constexpr int *v60 = { 0 };
112 constexpr struct s57 v61 = { 0 };
113 constexpr struct s57 v62 = { { } }; /* { dg-warning "braces around scalar initializer" } */
114 constexpr struct s57 v63 = { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
115 constexpr union u58 v64 = { 0 };
116 constexpr union u58 v65 = { { } }; /* { dg-warning "braces around scalar initializer" } */
117 constexpr union u58 v66 = { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
118 struct s67 { int a; float b; void *c; int *d; typeof (nullptr) e; int f; int g[2]; };
119 struct s68 { struct s67 x; };
120 union u69 { int a; float b; void *c; int *d; struct s68 e; };
121 struct s70 { union u69 x; };
122 constexpr struct s67 v71 = { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } };
123 static_assert (v71.a == 1);
124 static_assert (v71.f == 7);
125 constexpr struct s67 v72 = v71;
126 static_assert (v72.a == 1);
127 static_assert (v72.f == 7);
128 extern const struct s67 v71;
129 constexpr auto v73 = v71;
130 static_assert (v73.a == 1);
131 static_assert (v73.f == 7);
132 auto v74 = v71;
133 constexpr struct s68 v75 = { v72 };
134 static_assert (v75.x.a == 1);
135 static_assert (v75.x.f == 7);
136 constexpr union u69 v76 = { };
137 static_assert (v76.a == 0);
138 constexpr union u69 v77 = { .e = v75 };
139 static_assert (v77.e.x.a == 1);
140 static_assert (v77.e.x.f == 7);
141 constexpr union u69 v78 = { .a = 1 };
142 static_assert (v78.a == 1);
143 constexpr union u69 v79 = { .e = { v72 } };
144 static_assert (v79.e.x.a == 1);
145 static_assert (v79.e.x.f == 7);
146 enum e80 { E80 = v79.e.x.f };
147 static_assert (E80 == 7);
148 constexpr struct s70 v81 = { v79 };
149 static_assert (v81.x.e.x.f == 7);
150 constexpr struct s68 v82 = { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } };
151 static_assert (v82.x.a == 5);
152 static_assert (v82.x.f == 9);
153 constexpr auto v83 = (constexpr int) { (constexpr int) { 0 } };
154 /* These are null pointers but not null pointer constants. */
155 constexpr typeof (nullptr) v84 = nullptr;
156 constexpr void *v85 = 0;
157 int *v86 = v85;
158 int *v87 = v84;
159 typeof (1 ? v85 : (int *) 0) v88;
160 extern void *v88;
161 typeof (1 ? (void *) 0 : (int *) 0) v89;
162 extern int *v89;
163 constexpr struct s68 v90 = { };
164 static_assert (v90.x.a == 0);
165 static_assert (v90.x.f == 0);
166 constexpr int v91 = { 123 };
167 static_assert (v91 == 123);
168 constexpr int v92 = { v91 };
169 static_assert (v92 == 123);
170 /* Verify that constexpr values can be used in various contexts requiring
171 (integer) constant expressions. */
172 struct s93 { int x : v79.e.x.f; };
173 constexpr int v94 = alignof (int);
174 alignas (v94) int v95;
175 constexpr int v97[100] = { [v82.x.f] = 7 };
176 static int v98[v94];
177 constexpr _Complex double v99 = 1.0;
178 constexpr _Complex float v100 = 12345;
179 constexpr int *v101 = (int *) 0;
180 constexpr void *v102 = (void *) (void *) 0;
181 constexpr void *v103 = v101;
182 constexpr void *v104 = v84;
183 struct s105 { void *p; };
184 constexpr struct s105 v106 = { (int *) 0 };
186 void
187 f0 ()
189 constexpr int fv0 = 3;
190 static_assert (fv0 == 3);
191 auto constexpr fv1 = 4;
192 static_assert (fv1 == 4);
193 register constexpr float fv2 = 1.0;
194 constexpr auto fv3 = 123;
195 static_assert (fv3 == 123);
196 constexpr register void *fv4 = (void *) 0;
197 const int *fv5 = &(constexpr int) { 234 };
198 const int *fv6 = &(constexpr static int) { 234 };
199 const int *fv7 = &(static constexpr int) { 234 };
200 typeof ((constexpr register int) { 234 }) *fv8;
201 typeof ((register constexpr int) { 234 }) *fv9;
202 int fv10 = (constexpr int) { 1 } + sizeof (struct fs *);
203 constexpr auto fv11 = (constexpr int) { (constexpr int) { 0 } };
204 static_assert (fv11 == 0);
205 constexpr char fv12[3] = { 1, 2, 3 };
206 (constexpr short [4]) { 9, 8, 7, -6 };
207 constexpr struct s18 fv13 = { 1234ULL, 0, 13.0f };
208 (constexpr struct s18) { 123, (void *) 0, 11 };
209 constexpr union u20 fv14 = { 2 };
210 (constexpr union u20) { 5 };
211 constexpr union u20 fv15 = { .b = 15.0 };
212 (constexpr union u20) { .b = 20 };
213 (constexpr float) { (float) (1.0f / 3.0f) };
214 (constexpr double) { (double) (1.0 / 3.0) };
215 (constexpr struct s18) { 0, 0, (double) (1.0 / 3.0) };
216 (constexpr char []) { "abc\xfe" };
217 (constexpr unsigned char []) { u8"xyz\xff" };
218 (constexpr unsigned char []) { "\x12\x7f" };
219 (constexpr signed char []) { "\x34\x66" };
220 (constexpr double) { (int) (double) 3.0 - (long) (double) 2.0 };
221 (constexpr int) { 1 + 2 + (int) 3.0 };
222 (constexpr typeof (nullptr)) { nullptr };
223 (constexpr _Complex double) { __builtin_complex (1.0f, 3.0f / 2.0f) };
224 (constexpr float) { 1234.0L };
225 (constexpr char) { 127ULL };
226 #if FLT_MIN_EXP == -125 && FLT_MANT_DIG == 24
227 (constexpr float) { 0x1p-149 };
228 (constexpr float _Complex) { __builtin_complex (0x1p-149, 0x1p127) };
229 (constexpr float) { 0xffffffUL };
230 (constexpr float) { -0xffffffL };
231 (constexpr float) { 0xffffff0L };
232 (constexpr float) { 1ULL << 63 };
233 #endif
234 #if DBL_MIN_EXP == -1021 && DBL_MANT_DIG == 53
235 (constexpr double) { 0x1p-1074L };
236 (constexpr _Complex double) { __builtin_complex (0x1p1023L, 0x1p-1074L) };
237 (constexpr double) { 0x1fffffffffffffULL };
238 (constexpr double) { -0x1fffffffffffffLL };
239 (constexpr double) { 0x3ffffffffffffeULL };
240 (constexpr double) { 1ULL << 63 };
241 #endif
242 (constexpr void *) { (void *) 0 };
243 (constexpr int *) { (void *) 0L };
244 (constexpr long *) { 0LL };
245 (constexpr int) {};
246 (constexpr float) {};
247 (constexpr long double) {};
248 (constexpr int *) {};
249 (constexpr void *) {};
250 (constexpr typeof (nullptr)) {};
251 (constexpr int *) { 0 };
252 (constexpr struct s57) { 0 };
253 (constexpr struct s57) { { } }; /* { dg-warning "braces around scalar initializer" } */
254 (constexpr struct s57) { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
255 (constexpr union u58) { 0 };
256 (constexpr union u58) { { } }; /* { dg-warning "braces around scalar initializer" } */
257 (constexpr union u58) { { 0 } }; /* { dg-warning "braces around scalar initializer" } */
258 (constexpr _Complex double) { 1.0 };
259 (constexpr _Complex float) { 12345 };
260 (constexpr int *) { (int *) 0 };
261 (constexpr void *) { (void *) (void *) 0 };
262 (constexpr void *) { v101 };
263 (constexpr void *) { v84 };
264 (constexpr struct s105) { (int *) 0 };
265 /* It's not entirely clear if constexpr declarations are allowed in this
266 position in a for loop; presume they are, as implicitly auto just as if no
267 storage class specifiers were used. */
268 for (constexpr int fv16 = 1;;)
269 break;
270 constexpr struct s67 fv17 = { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } };
271 static_assert (fv17.a == 1);
272 static_assert (fv17.f == 7);
273 constexpr struct s67 fv18 = fv17;
274 static_assert (fv18.a == 1);
275 static_assert (fv18.f == 7);
276 constexpr auto fv19 = fv17;
277 static_assert (fv19.a == 1);
278 static_assert (fv19.f == 7);
279 auto fv20 = fv17;
280 constexpr struct s68 fv21 = { fv18 };
281 static_assert (fv21.x.a == 1);
282 static_assert (fv21.x.f == 7);
283 constexpr union u69 fv22 = { };
284 static_assert (fv22.a == 0);
285 constexpr union u69 fv23 = { .e = fv21 };
286 static_assert (fv23.e.x.a == 1);
287 static_assert (fv23.e.x.f == 7);
288 constexpr union u69 fv24 = { .a = 1 };
289 static_assert (fv24.a == 1);
290 constexpr union u69 fv25 = { .e = { fv18 } };
291 static_assert (fv25.e.x.a == 1);
292 static_assert (fv25.e.x.f == 7);
293 enum fe80 { FE80 = fv25.e.x.f };
294 static_assert (FE80 == 7);
295 constexpr struct s70 fv26 = { fv25 };
296 static_assert (fv26.x.e.x.f == 7);
297 constexpr struct s68 fv27 = { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } };
298 static_assert (fv27.x.a == 5);
299 static_assert (fv27.x.f == 9);
300 constexpr struct s68 fv28 = { };
301 static_assert (fv28.x.a == 0);
302 static_assert (fv28.x.f == 0);
303 constexpr int fv29 = { 123 };
304 static_assert (fv29 == 123);
305 constexpr int fv30 = { fv29 };
306 static_assert (fv30 == 123);
307 static_assert ((constexpr struct s67) { 1, 2.0, 0, 0, nullptr, 7, { 3, 4 } }.f == 7);
308 static_assert ((constexpr struct s68) { fv18 }.x.a == 1);
309 static_assert ((constexpr union u69) { }.a == 0);
310 static_assert ((constexpr union u69) { .e = fv21 }.e.x.f == 7);
311 static_assert ((constexpr union u69) { .a = 1 }.a == 1);
312 static_assert ((constexpr union u69) { .e = { fv18 } }.e.x.a == 1);
313 static_assert ((constexpr struct s70) { fv25 }.x.e.x.f == 7);
314 static_assert ((constexpr struct s68) { (constexpr struct s67) { 5, 6, 0, 0, nullptr, 9, { 1, 2 } } }.x.f == 9);
315 static_assert ((constexpr struct s68) { }.x.f == 0);
316 /* Verify that constexpr values can be used in various contexts requiring
317 (integer) constant expressions. */
318 struct fs93 { int x : fv25.e.x.f; };
319 constexpr int fv31 = alignof (int);
320 alignas (fv31) int fv32;
321 constexpr int fv33[100] = { [fv27.x.f] = 7 };
322 static int fv34[fv31];
323 switch (fv0)
325 case fv27.x.f: ;