1 // RUN: %clang_cc1 -std=gnu++2a -emit-pch %s -o %t.pch
2 // RUN: %clang_cc1 -std=gnu++2a %s -DEMIT -ast-merge %t.pch -ast-dump-all | FileCheck %s
3
4 // XFAIL: *
5
6 #ifndef EMIT
7 #define EMIT
8
9 namespace Integer {
10
fint()11 consteval int fint() {
12 return 6789;
13 }
14
15 int Unique_Int = fint();
16 //CHECK: VarDecl {{.*}} Unique_Int
17 //CHECK-NEXT: ConstantExpr {{.*}} 'int'
18 //CHECK-NEXT: value: Int 6789
19
fint128()20 consteval __uint128_t fint128() {
21 return ((__uint128_t)0x75f17d6b3588f843 << 64) | 0xb13dea7c9c324e51;
22 }
23
24 constexpr __uint128_t Unique_Int128 = fint128();
25 //CHECK: VarDecl {{.*}} Unique_Int128
26 //CHECK-NEXT: value: Int 156773562844924187900898496343692168785
27 //CHECK-NEXT: ConstantExpr
28 //CHECK-NEXT: value: Int 156773562844924187900898496343692168785
29
30 } // namespace Integer
31
32 namespace FloatingPoint {
33
fdouble()34 consteval double fdouble() {
35 return double(567890.67890);
36 }
37
38 double Unique_Double = fdouble();
39 //CHECK: VarDecl {{.*}} Unique_Double
40 //CHECK-NEXT: ConstantExpr {{.*}}
41 //CHECK-NEXT: value: Float 5.678907e+05
42
43 } // namespace FloatingPoint
44
45 // FIXME: Add test for FixedPoint, ComplexInt, ComplexFloat, AddrLabelDiff.
46
47 namespace Struct {
48
49 struct B {
50 int i;
51 double d;
52 };
53
fB()54 consteval B fB() {
55 return B{1, 0.7};
56 }
57
58 constexpr B Basic_Struct = fB();
59 //CHECK: VarDecl {{.*}} Basic_Struct
60 //CHECK-NEXT: value: Struct
61 //CHECK-NEXT: fields: Int 1, Float 7.000000e-01
62 //CHECK-NEXT: ImplicitCastExpr
63 //CHECK-NEXT: ConstantExpr
64 //CHECK-NEXT: value: Struct
65 //CHECK-NEXT: fields: Int 1, Float 7.000000e-01
66
67 struct C {
68 int i = 9;
69 };
70
71 struct A : B {
AStruct::A72 constexpr A(B b, int I, double D, C _c) : B(b), i(I), d(D), c(_c) {}
73 int i;
74 double d;
75 C c;
76 };
77
fA()78 consteval A fA() {
79 return A(Basic_Struct, 1, 79.789, {});
80 }
81
82 A Advanced_Struct = fA();
83 //CHECK: VarDecl {{.*}} Advanced_Struct
84 //CHECK-NEXT: ConstantExpr {{.*}}
85 //CHECK-NEXT: value: Struct
86 //CHECK-NEXT: base: Struct
87 //CHECK-NEXT: fields: Int 1, Float 7.000000e-01
88 //CHECK-NEXT: fields: Int 1, Float 7.978900e+01
89 //CHECK-NEXT: field: Struct
90 //CHECK-NEXT: field: Int 9
91
92 } // namespace Struct
93
94 namespace Vector {
95
96 using v4si = int __attribute__((__vector_size__(16)));
97
fv4si()98 consteval v4si fv4si() {
99 return (v4si){8, 2, 3};
100 }
101
102 v4si Vector_Int = fv4si();
103 //CHECK: VarDecl {{.*}} Vector_Int
104 //CHECK-NEXT: ConstantExpr
105 //CHECK-NEXT: value: Vector length=4
106 //CHECK-NEXT: elements: Int 8, Int 2, Int 3, Int 0
107
108 } // namespace Vector
109
110 namespace Array {
111
112 struct B {
113 int arr[6];
114 };
115
fint()116 consteval B fint() {
117 return B{1, 2, 3, 4, 5, 6};
118 }
119
120 B Array_Int = fint();
121 //CHECK: VarDecl {{.*}} Array_Int
122 //CHECK-NEXT: ConstantExpr
123 //CHECK-NEXT: value: Struct
124 //CHECK-NEXT: field: Array size=6
125 //CHECK-NEXT: elements: Int 1, Int 2, Int 3, Int 4
126 //CHECK-NEXT: elements: Int 5, Int 6
127
128 struct A {
129 int i = 789;
130 double d = 67890.09876;
131 };
132
133 struct C {
134 A arr[3];
135 };
136
fA()137 consteval C fA() {
138 return {{A{}, A{-45678, 9.8}, A{9}}};
139 }
140
141 C Array2_Struct = fA();
142 //CHECK: VarDecl {{.*}} Array2_Struct
143 //CHECK-NEXT: ConstantExpr {{.*}}
144
145 using v4si = int __attribute__((__vector_size__(16)));
146
147 struct D {
148 v4si arr[2];
149 };
150
fv4si()151 consteval D fv4si() {
152 return {{{1, 2, 3, 4}, {4, 5, 6, 7}}};
153 }
154
155 D Array_Vector = fv4si();
156 //CHECK: VarDecl {{.*}} Array_Vector
157 //CHECK-NEXT: ConstantExpr {{.*}}
158 //CHECK-NEXT: value: Struct
159 //CHECK-NEXT: field: Array size=2
160 //CHECK-NEXT: element: Vector length=4
161 //CHECK-NEXT: elements: Int 1, Int 2, Int 3, Int 4
162 //CHECK-NEXT: element: Vector length=4
163 //CHECK-NEXT: elements: Int 4, Int 5, Int 6, Int 7
164
165 } // namespace Array
166
167 namespace Union {
168
169 struct A {
170 int i = 6789;
171 float f = 987.9876;
172 };
173
174 union U {
175 int i;
176 A a{567890, 9876.5678f};
177 };
178
fU1()179 consteval U fU1() {
180 return U{0};
181 }
182
183 U Unique_Union1 = fU1();
184 //CHECK: VarDecl {{.*}} Unique_Union
185 //CHECK-NEXT: ConstantExpr
186 //CHECK-NEXT: value: Union .i Int 0
187
fU()188 consteval U fU() {
189 return U{};
190 }
191
192 U Unique_Union2 = fU();
193 //CHECK: VarDecl {{.*}} Unique_Union
194 //CHECK-NEXT: ConstantExpr
195 //CHECK-NEXT: value: Union .a
196 //CHECK-NEXT: Struct
197 //CHECK-NEXT: fields: Int 567890, Float 9.876567e+03
198
199 } // namespace Union
200
201 namespace MemberPointer {
202
203 struct A {
204 struct B {
205 struct C {
206 struct D {
207 struct E {
208 struct F {
209 struct G {
210 int i;
211 };
212 };
213 };
214 };
215 };
216 };
217 };
218
fmem_ptr()219 consteval auto fmem_ptr() -> decltype(&A::B::C::D::E::F::G::i) {
220 return &A::B::C::D::E::F::G::i;
221 }
222
223 auto MemberPointer1 = fmem_ptr();
224 //CHECK: VarDecl {{.*}} MemberPointer1
225 //CHECK-NEXT: ConstantExpr
226 //CHECK-NEXT: value: MemberPointer &G::i
227
228 struct A1 {
229 struct B1 {
fMemberPointer::A1::B1230 int f() const {
231 return 0;
232 }
233 };
234 };
235
fmem_ptr2()236 consteval auto fmem_ptr2() {
237 return &A1::B1::f;
238 }
239
240 auto MemberPointer2 = fmem_ptr2();
241 //CHECK: VarDecl {{.*}} MemberPointer2
242 //CHECK-NEXT: ConstantExpr
243 //CHECK-NEXT: value: MemberPointer &B1::f
244
245 } // namespace MemberPointer
246
247 namespace std {
248 struct type_info;
249 };
250
251 namespace LValue {
252
253 constexpr int g = 0;
254
fg_ref()255 consteval const int &fg_ref() {
256 return g;
257 }
258
259 const int &g_ref = fg_ref();
260 //CHECK: VarDecl {{.*}} g_ref
261 //CHECK-NEXT: ConstantExpr
262 //CHECK-NEXT: value: LValue &g
263
fint_ptr()264 consteval const int *fint_ptr() {
265 return &g;
266 }
267
268 const int *g_ptr = fint_ptr();
269 //CHECK: VarDecl {{.*}} g_ptr
270 //CHECK-NEXT: ConstantExpr
271 //CHECK-NEXT: value: LValue &g
272
fnull_ptr()273 consteval const int *fnull_ptr() {
274 return nullptr;
275 }
276
277 const int *ptr2 = fnull_ptr();
278 //CHECK: VarDecl {{.*}} ptr2
279 //CHECK-NEXT: ConstantExpr
280 //CHECK-NEXT: value: LValue nullptr
281
282 int fconst();
283
ffunc_ptr()284 consteval auto ffunc_ptr() {
285 return &fconst;
286 }
287
288 int (*func_ptr)() = ffunc_ptr();
289 //CHECK: VarDecl {{.*}} func_ptr
290 //CHECK-NEXT: ConstantExpr {{.*}}
291 //CHECK-NEXT: value: LValue &fconst
292
293 struct A {
294 int Arr[6] = {0, 1, 3, 4, 5, 9};
295 int i = 0;
296 };
297
298 struct D {
299 A arr[6] = {};
300 };
301
fA()302 consteval D fA() {
303 return {};
304 }
305
306 constexpr D Arr = fA();
307 // CHECK: VarDecl {{.*}} Arr
308 // CHECK-NEXT: value: Struct
309 // CHECK-NEXT: field: Array size=6
310 // CHECK-NEXT: element: Struct
311 // CHECK-NEXT: field: Array size=6
312 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
313 // CHECK-NEXT: elements: Int 5, Int 9
314 // CHECK-NEXT: field: Int 0
315 // CHECK-NEXT: element: Struct
316 // CHECK-NEXT: field: Array size=6
317 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
318 // CHECK-NEXT: elements: Int 5, Int 9
319 // CHECK-NEXT: field: Int 0
320 // CHECK-NEXT: element: Struct
321 // CHECK-NEXT: field: Array size=6
322 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
323 // CHECK-NEXT: elements: Int 5, Int 9
324 // CHECK-NEXT: field: Int 0
325 // CHECK-NEXT: element: Struct
326 // CHECK-NEXT: field: Array size=6
327 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
328 // CHECK-NEXT: elements: Int 5, Int 9
329 // CHECK-NEXT: field: Int 0
330 // CHECK-NEXT: element: Struct
331 // CHECK-NEXT: field: Array size=6
332 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
333 // CHECK-NEXT: elements: Int 5, Int 9
334 // CHECK-NEXT: field: Int 0
335 // CHECK-NEXT: element: Struct
336 // CHECK-NEXT: field: Array size=6
337 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
338 // CHECK-NEXT: elements: Int 5, Int 9
339 // CHECK-NEXT: field: Int 0
340 // CHECK-NEXT: ImplicitCastExpr
341 // CHECK-NEXT: ConstantExpr
342 // CHECK-NEXT: value: Struct
343 // CHECK-NEXT: field: Array size=6
344 // CHECK-NEXT: element: Struct
345 // CHECK-NEXT: field: Array size=6
346 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
347 // CHECK-NEXT: elements: Int 5, Int 9
348 // CHECK-NEXT: field: Int 0
349 // CHECK-NEXT: element: Struct
350 // CHECK-NEXT: field: Array size=6
351 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
352 // CHECK-NEXT: elements: Int 5, Int 9
353 // CHECK-NEXT: field: Int 0
354 // CHECK-NEXT: element: Struct
355 // CHECK-NEXT: field: Array size=6
356 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
357 // CHECK-NEXT: elements: Int 5, Int 9
358 // CHECK-NEXT: field: Int 0
359 // CHECK-NEXT: element: Struct
360 // CHECK-NEXT: field: Array size=6
361 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
362 // CHECK-NEXT: elements: Int 5, Int 9
363 // CHECK-NEXT: field: Int 0
364 // CHECK-NEXT: element: Struct
365 // CHECK-NEXT: field: Array size=6
366 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
367 // CHECK-NEXT: elements: Int 5, Int 9
368 // CHECK-NEXT: field: Int 0
369 // CHECK-NEXT: element: Struct
370 // CHECK-NEXT: field: Array size=6
371 // CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
372 // CHECK-NEXT: elements: Int 5, Int 9
373 // CHECK-NEXT: field: Int 0
374
fconstintref()375 consteval const int &fconstintref() {
376 return Arr.arr[0].i;
377 }
378
379 const int &ArrayStructRef1 = fconstintref();
380 //CHECK: VarDecl {{.*}} ArrayStructRef1
381 //CHECK-NEXT: ConstantExpr
382 //CHECK-NEXT: value: LValue &Arr.arr[0].i
383
fconstintref2()384 consteval const int &fconstintref2() {
385 return Arr.arr[1].Arr[5];
386 }
387
388 const int &ArrayStructRef2 = fconstintref2();
389 //CHECK: VarDecl {{.*}} ArrayStructRef2
390 //CHECK-NEXT: ConstantExpr
391 //CHECK-NEXT: value: LValue &Arr.arr[1].Arr[5]
392
fconststar()393 consteval const int *fconststar() {
394 return &ArrayStructRef2;
395 }
396
397 const int *ArrayStructRef3 = fconststar();
398 //CHECK: VarDecl {{.*}} ArrayStructRef3
399 //CHECK-NEXT: ConstantExpr
400 //CHECK-NEXT: value: LValue &Arr.arr[1].Arr[5]
401
402 struct B : A {
403 };
404
405 struct C {
406 B b;
407 };
408
fC()409 consteval C fC() {
410 return {};
411 }
412
413 C c = fC();
414 //CHECK: VarDecl {{.*}} c
415 //CHECK-NEXT: ConstantExpr
416 //CHECK-NEXT: value: Struct
417 //CHECK-NEXT: field: Struct
418 //CHECK-NEXT: base: Struct
419 //CHECK-NEXT: field: Array size=6
420 //CHECK-NEXT: elements: Int 0, Int 1, Int 3, Int 4
421 //CHECK-NEXT: elements: Int 5, Int 9
422 //CHECK-NEXT: field: Int 0
423
f2constintref()424 consteval const int &f2constintref() {
425 return c.b.i;
426 }
427
428 const int &StructPathRef = f2constintref();
429 //CHECK: VarDecl {{.*}} StructPathRef
430 //CHECK-NEXT: ConstantExpr
431 //CHECK-NEXT: value: LValue &c.b.A::i
432
ftype_info()433 consteval const std::type_info *ftype_info() {
434 return &typeid(c);
435 }
436
437 const std::type_info *T1 = ftype_info();
438 //CHECK: VarDecl {{.*}} T1
439 //CHECK-NEXT: ConstantExpr
440 //CHECK-NEXT:value: LValue &typeid(LValue::C)
441
ftype_info2()442 consteval const std::type_info *ftype_info2() {
443 return &typeid(Arr.arr[1].Arr[2]);
444 }
445
446 const std::type_info *T2 = ftype_info2();
447 //CHECK: VarDecl {{.*}} T2
448 //CHECK-NEXT: ConstantExpr
449 //CHECK-NEXT: value: LValue &typeid(int)
450
fstring()451 consteval const char *fstring() {
452 return "test";
453 }
454
455 const char *cptr = fstring();
456 //CHECK: VarDecl {{.*}} cptr
457 //CHECK-NEXT: ConstantExpr
458 //CHECK-NEXT: value: LValue &"test"[0]
459
460 } // namespace LValue
461
462 #endif
463