]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/constexpr.cc
ad43897b076c252c24ffe29fdd4ee254408dbb1e
[gcc.git] / gcc / cp / constexpr.cc
1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
4
5 Copyright (C) 1998-2022 Free Software Foundation, Inc.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "cp-tree.h"
27 #include "varasm.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
30 #include "gimplify.h"
31 #include "builtins.h"
32 #include "tree-inline.h"
33 #include "ubsan.h"
34 #include "timevar.h"
35 #include "fold-const-call.h"
36 #include "stor-layout.h"
37 #include "cgraph.h"
38 #include "opts.h"
39 #include "stringpool.h"
40 #include "attribs.h"
41 #include "fold-const.h"
42 #include "intl.h"
43
44 static bool verify_constant (tree, bool, bool *, bool *);
45 #define VERIFY_CONSTANT(X) \
46 do { \
47 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
48 return t; \
49 } while (0)
50
51 static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
52 bool insert = false);
53 static int array_index_cmp (tree key, tree index);
54
55 /* Returns true iff FUN is an instantiation of a constexpr function
56 template or a defaulted constexpr function. */
57
58 bool
59 is_instantiation_of_constexpr (tree fun)
60 {
61 return ((DECL_TEMPLOID_INSTANTIATION (fun)
62 && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
63 || (DECL_DEFAULTED_FN (fun)
64 && DECL_DECLARED_CONSTEXPR_P (fun)));
65 }
66
67 /* Return true if T is a literal type. */
68
69 bool
70 literal_type_p (tree t)
71 {
72 if (SCALAR_TYPE_P (t)
73 || VECTOR_TYPE_P (t)
74 || TYPE_REF_P (t)
75 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
76 return true;
77 if (CLASS_TYPE_P (t))
78 {
79 t = complete_type (t);
80 gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
81 return CLASSTYPE_LITERAL_P (t);
82 }
83 if (TREE_CODE (t) == ARRAY_TYPE)
84 return literal_type_p (strip_array_types (t));
85 return false;
86 }
87
88 /* If DECL is a variable declared `constexpr', require its type
89 be literal. Return error_mark_node if we give an error, the
90 DECL otherwise. */
91
92 tree
93 ensure_literal_type_for_constexpr_object (tree decl)
94 {
95 tree type = TREE_TYPE (decl);
96 if (VAR_P (decl)
97 && (DECL_DECLARED_CONSTEXPR_P (decl)
98 || var_in_constexpr_fn (decl))
99 && !processing_template_decl)
100 {
101 tree stype = strip_array_types (type);
102 if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
103 /* Don't complain here, we'll complain about incompleteness
104 when we try to initialize the variable. */;
105 else if (!literal_type_p (type))
106 {
107 if (DECL_DECLARED_CONSTEXPR_P (decl))
108 {
109 auto_diagnostic_group d;
110 error_at (DECL_SOURCE_LOCATION (decl),
111 "the type %qT of %<constexpr%> variable %qD "
112 "is not literal", type, decl);
113 explain_non_literal_class (type);
114 decl = error_mark_node;
115 }
116 else if (cxx_dialect < cxx23)
117 {
118 if (!is_instantiation_of_constexpr (current_function_decl))
119 {
120 auto_diagnostic_group d;
121 error_at (DECL_SOURCE_LOCATION (decl),
122 "variable %qD of non-literal type %qT in "
123 "%<constexpr%> function only available with "
124 "%<-std=c++2b%> or %<-std=gnu++2b%>", decl, type);
125 explain_non_literal_class (type);
126 decl = error_mark_node;
127 }
128 cp_function_chain->invalid_constexpr = true;
129 }
130 }
131 else if (DECL_DECLARED_CONSTEXPR_P (decl)
132 && variably_modified_type_p (type, NULL_TREE))
133 {
134 error_at (DECL_SOURCE_LOCATION (decl),
135 "%<constexpr%> variable %qD has variably-modified "
136 "type %qT", decl, type);
137 decl = error_mark_node;
138 }
139 }
140 return decl;
141 }
142
143 /* Issue a diagnostic with text GMSGID for constructs that are invalid in
144 constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking
145 a constexpr function body; if so, don't report hard errors and issue
146 a pedwarn pre-C++23, or a warning in C++23, if requested by
147 -Winvalid-constexpr. Otherwise, we're not in the context where we are
148 checking if a function can be marked 'constexpr', so give a hard error. */
149
150 ATTRIBUTE_GCC_DIAG(3,4)
151 static bool
152 constexpr_error (location_t location, bool constexpr_fundef_p,
153 const char *gmsgid, ...)
154 {
155 diagnostic_info diagnostic;
156 va_list ap;
157 rich_location richloc (line_table, location);
158 va_start (ap, gmsgid);
159 bool ret;
160 if (!constexpr_fundef_p)
161 {
162 /* Report an error that cannot be suppressed. */
163 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc, DK_ERROR);
164 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
165 }
166 else if (warn_invalid_constexpr)
167 {
168 diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
169 cxx_dialect < cxx23 ? DK_PEDWARN : DK_WARNING);
170 diagnostic.option_index = OPT_Winvalid_constexpr;
171 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
172 }
173 else
174 ret = false;
175 va_end (ap);
176 return ret;
177 }
178
179 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
180 {
181 static hashval_t hash (const constexpr_fundef *);
182 static bool equal (const constexpr_fundef *, const constexpr_fundef *);
183 };
184
185 /* This table holds all constexpr function definitions seen in
186 the current translation unit. */
187
188 static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
189
190 /* Utility function used for managing the constexpr function table.
191 Return true if the entries pointed to by P and Q are for the
192 same constexpr function. */
193
194 inline bool
195 constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
196 const constexpr_fundef *rhs)
197 {
198 return lhs->decl == rhs->decl;
199 }
200
201 /* Utility function used for managing the constexpr function table.
202 Return a hash value for the entry pointed to by Q. */
203
204 inline hashval_t
205 constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
206 {
207 return DECL_UID (fundef->decl);
208 }
209
210 /* Return a previously saved definition of function FUN. */
211
212 constexpr_fundef *
213 retrieve_constexpr_fundef (tree fun)
214 {
215 if (constexpr_fundef_table == NULL)
216 return NULL;
217
218 constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
219 return constexpr_fundef_table->find (&fundef);
220 }
221
222 /* Check whether the parameter and return types of FUN are valid for a
223 constexpr function, and complain if COMPLAIN. */
224
225 bool
226 is_valid_constexpr_fn (tree fun, bool complain)
227 {
228 bool ret = true;
229
230 if (DECL_INHERITED_CTOR (fun)
231 && TREE_CODE (fun) == TEMPLATE_DECL)
232 {
233 ret = false;
234 if (complain)
235 error ("inherited constructor %qD is not %<constexpr%>",
236 DECL_INHERITED_CTOR (fun));
237 }
238 else
239 {
240 for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
241 parm != NULL_TREE; parm = TREE_CHAIN (parm))
242 if (!literal_type_p (TREE_TYPE (parm)))
243 {
244 ret = false;
245 if (complain)
246 {
247 auto_diagnostic_group d;
248 if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
249 "invalid type for parameter %d of "
250 "%<constexpr%> function %q+#D",
251 DECL_PARM_INDEX (parm), fun))
252 explain_non_literal_class (TREE_TYPE (parm));
253 }
254 }
255 }
256
257 if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
258 {
259 ret = false;
260 if (complain)
261 inform (DECL_SOURCE_LOCATION (fun),
262 "lambdas are implicitly %<constexpr%> only in C++17 and later");
263 }
264 else if (DECL_DESTRUCTOR_P (fun))
265 {
266 if (cxx_dialect < cxx20)
267 {
268 ret = false;
269 if (complain)
270 error_at (DECL_SOURCE_LOCATION (fun),
271 "%<constexpr%> destructors only available"
272 " with %<-std=c++20%> or %<-std=gnu++20%>");
273 }
274 }
275 else if (!DECL_CONSTRUCTOR_P (fun))
276 {
277 tree rettype = TREE_TYPE (TREE_TYPE (fun));
278 if (!literal_type_p (rettype))
279 {
280 ret = false;
281 if (complain)
282 {
283 auto_diagnostic_group d;
284 if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
285 "invalid return type %qT of %<constexpr%> "
286 "function %q+D", rettype, fun))
287 explain_non_literal_class (rettype);
288 }
289 }
290
291 /* C++14 DR 1684 removed this restriction. */
292 if (cxx_dialect < cxx14
293 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
294 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
295 {
296 ret = false;
297 if (complain)
298 {
299 auto_diagnostic_group d;
300 if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
301 "enclosing class of %<constexpr%> non-static"
302 " member function %q+#D is not a literal type",
303 fun))
304 explain_non_literal_class (DECL_CONTEXT (fun));
305 }
306 }
307 }
308 else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)))
309 {
310 ret = false;
311 if (complain)
312 error ("%q#T has virtual base classes", DECL_CONTEXT (fun));
313 }
314
315 return ret;
316 }
317
318 /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
319 for a member of an anonymous aggregate, INIT is the initializer for that
320 member, and VEC_OUTER is the vector of constructor elements for the class
321 whose constructor we are processing. Add the initializer to the vector
322 and return true to indicate success. */
323
324 static bool
325 build_anon_member_initialization (tree member, tree init,
326 vec<constructor_elt, va_gc> **vec_outer)
327 {
328 /* MEMBER presents the relevant fields from the inside out, but we need
329 to build up the initializer from the outside in so that we can reuse
330 previously built CONSTRUCTORs if this is, say, the second field in an
331 anonymous struct. So we use a vec as a stack. */
332 auto_vec<tree, 2> fields;
333 do
334 {
335 fields.safe_push (TREE_OPERAND (member, 1));
336 member = TREE_OPERAND (member, 0);
337 }
338 while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
339 && TREE_CODE (member) == COMPONENT_REF);
340
341 /* VEC has the constructor elements vector for the context of FIELD.
342 If FIELD is an anonymous aggregate, we will push inside it. */
343 vec<constructor_elt, va_gc> **vec = vec_outer;
344 tree field;
345 while (field = fields.pop(),
346 ANON_AGGR_TYPE_P (TREE_TYPE (field)))
347 {
348 tree ctor;
349 /* If there is already an outer constructor entry for the anonymous
350 aggregate FIELD, use it; otherwise, insert one. */
351 if (vec_safe_is_empty (*vec)
352 || (*vec)->last().index != field)
353 {
354 ctor = build_constructor (TREE_TYPE (field), NULL);
355 CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
356 }
357 else
358 ctor = (*vec)->last().value;
359 vec = &CONSTRUCTOR_ELTS (ctor);
360 }
361
362 /* Now we're at the innermost field, the one that isn't an anonymous
363 aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
364 gcc_assert (fields.is_empty());
365 CONSTRUCTOR_APPEND_ELT (*vec, field, init);
366
367 return true;
368 }
369
370 /* Subroutine of build_constexpr_constructor_member_initializers.
371 The expression tree T represents a data member initialization
372 in a (constexpr) constructor definition. Build a pairing of
373 the data member with its initializer, and prepend that pair
374 to the existing initialization pair INITS. */
375
376 static bool
377 build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
378 {
379 tree member, init;
380 if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
381 t = TREE_OPERAND (t, 0);
382 if (TREE_CODE (t) == EXPR_STMT)
383 t = TREE_OPERAND (t, 0);
384 if (t == error_mark_node)
385 return false;
386 if (TREE_CODE (t) == STATEMENT_LIST)
387 {
388 for (tree stmt : tsi_range (t))
389 if (! build_data_member_initialization (stmt, vec))
390 return false;
391 return true;
392 }
393 if (TREE_CODE (t) == CLEANUP_STMT)
394 {
395 /* We can't see a CLEANUP_STMT in a constructor for a literal class,
396 but we can in a constexpr constructor for a non-literal class. Just
397 ignore it; either all the initialization will be constant, in which
398 case the cleanup can't run, or it can't be constexpr.
399 Still recurse into CLEANUP_BODY. */
400 return build_data_member_initialization (CLEANUP_BODY (t), vec);
401 }
402 if (TREE_CODE (t) == CONVERT_EXPR)
403 t = TREE_OPERAND (t, 0);
404 if (TREE_CODE (t) == INIT_EXPR
405 /* vptr initialization shows up as a MODIFY_EXPR. In C++14 we only
406 use what this function builds for cx_check_missing_mem_inits, and
407 assignment in the ctor body doesn't count. */
408 || (cxx_dialect < cxx14 && TREE_CODE (t) == MODIFY_EXPR))
409 {
410 member = TREE_OPERAND (t, 0);
411 init = break_out_target_exprs (TREE_OPERAND (t, 1));
412 }
413 else if (TREE_CODE (t) == CALL_EXPR)
414 {
415 tree fn = get_callee_fndecl (t);
416 if (!fn || !DECL_CONSTRUCTOR_P (fn))
417 /* We're only interested in calls to subobject constructors. */
418 return true;
419 member = CALL_EXPR_ARG (t, 0);
420 /* We don't use build_cplus_new here because it complains about
421 abstract bases. Leaving the call unwrapped means that it has the
422 wrong type, but cxx_eval_constant_expression doesn't care. */
423 init = break_out_target_exprs (t);
424 }
425 else if (TREE_CODE (t) == BIND_EXPR)
426 return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
427 else
428 /* Don't add anything else to the CONSTRUCTOR. */
429 return true;
430 if (INDIRECT_REF_P (member))
431 member = TREE_OPERAND (member, 0);
432 if (TREE_CODE (member) == NOP_EXPR)
433 {
434 tree op = member;
435 STRIP_NOPS (op);
436 if (TREE_CODE (op) == ADDR_EXPR)
437 {
438 gcc_assert (same_type_ignoring_top_level_qualifiers_p
439 (TREE_TYPE (TREE_TYPE (op)),
440 TREE_TYPE (TREE_TYPE (member))));
441 /* Initializing a cv-qualified member; we need to look through
442 the const_cast. */
443 member = op;
444 }
445 else if (op == current_class_ptr
446 && (same_type_ignoring_top_level_qualifiers_p
447 (TREE_TYPE (TREE_TYPE (member)),
448 current_class_type)))
449 /* Delegating constructor. */
450 member = op;
451 else
452 {
453 /* This is an initializer for an empty base; keep it for now so
454 we can check it in cxx_eval_bare_aggregate. */
455 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
456 }
457 }
458 if (TREE_CODE (member) == ADDR_EXPR)
459 member = TREE_OPERAND (member, 0);
460 if (TREE_CODE (member) == COMPONENT_REF)
461 {
462 tree aggr = TREE_OPERAND (member, 0);
463 if (TREE_CODE (aggr) == VAR_DECL)
464 /* Initializing a local variable, don't add anything. */
465 return true;
466 if (TREE_CODE (aggr) != COMPONENT_REF)
467 /* Normal member initialization. */
468 member = TREE_OPERAND (member, 1);
469 else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
470 /* Initializing a member of an anonymous union. */
471 return build_anon_member_initialization (member, init, vec);
472 else
473 /* We're initializing a vtable pointer in a base. Leave it as
474 COMPONENT_REF so we remember the path to get to the vfield. */
475 gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
476 }
477
478 /* Value-initialization can produce multiple initializers for the
479 same field; use the last one. */
480 if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
481 (*vec)->last().value = init;
482 else
483 CONSTRUCTOR_APPEND_ELT (*vec, member, init);
484 return true;
485 }
486
487 /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
488 In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
489 BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
490
491 static bool
492 check_constexpr_bind_expr_vars (tree t)
493 {
494 gcc_assert (TREE_CODE (t) == BIND_EXPR);
495
496 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
497 if (TREE_CODE (var) == TYPE_DECL
498 && DECL_IMPLICIT_TYPEDEF_P (var)
499 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
500 return false;
501 return true;
502 }
503
504 /* Subroutine of check_constexpr_ctor_body. */
505
506 static bool
507 check_constexpr_ctor_body_1 (tree last, tree list)
508 {
509 switch (TREE_CODE (list))
510 {
511 case DECL_EXPR:
512 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
513 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
514 return true;
515 return false;
516
517 case CLEANUP_POINT_EXPR:
518 return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
519 /*complain=*/false);
520
521 case BIND_EXPR:
522 if (!check_constexpr_bind_expr_vars (list)
523 || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
524 /*complain=*/false))
525 return false;
526 return true;
527
528 case USING_STMT:
529 case STATIC_ASSERT:
530 case DEBUG_BEGIN_STMT:
531 return true;
532
533 default:
534 return false;
535 }
536 }
537
538 /* Make sure that there are no statements after LAST in the constructor
539 body represented by LIST. */
540
541 bool
542 check_constexpr_ctor_body (tree last, tree list, bool complain)
543 {
544 /* C++14 doesn't require a constexpr ctor to have an empty body. */
545 if (cxx_dialect >= cxx14)
546 return true;
547
548 bool ok = true;
549 if (TREE_CODE (list) == STATEMENT_LIST)
550 {
551 tree_stmt_iterator i = tsi_last (list);
552 for (; !tsi_end_p (i); tsi_prev (&i))
553 {
554 tree t = tsi_stmt (i);
555 if (t == last)
556 break;
557 if (!check_constexpr_ctor_body_1 (last, t))
558 {
559 ok = false;
560 break;
561 }
562 }
563 }
564 else if (list != last
565 && !check_constexpr_ctor_body_1 (last, list))
566 ok = false;
567 if (!ok)
568 {
569 if (complain)
570 error ("%<constexpr%> constructor does not have empty body");
571 DECL_DECLARED_CONSTEXPR_P (current_function_decl) = false;
572 }
573 return ok;
574 }
575
576 /* V is a vector of constructor elements built up for the base and member
577 initializers of a constructor for TYPE. They need to be in increasing
578 offset order, which they might not be yet if TYPE has a primary base
579 which is not first in the base-clause or a vptr and at least one base
580 all of which are non-primary. */
581
582 static vec<constructor_elt, va_gc> *
583 sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
584 {
585 tree pri = CLASSTYPE_PRIMARY_BINFO (type);
586 tree field_type;
587 unsigned i;
588 constructor_elt *ce;
589
590 if (pri)
591 field_type = BINFO_TYPE (pri);
592 else if (TYPE_CONTAINS_VPTR_P (type))
593 field_type = vtbl_ptr_type_node;
594 else
595 return v;
596
597 /* Find the element for the primary base or vptr and move it to the
598 beginning of the vec. */
599 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
600 if (TREE_TYPE (ce->index) == field_type)
601 break;
602
603 if (i > 0 && i < vec_safe_length (v))
604 {
605 vec<constructor_elt, va_gc> &vref = *v;
606 constructor_elt elt = vref[i];
607 for (; i > 0; --i)
608 vref[i] = vref[i-1];
609 vref[0] = elt;
610 }
611
612 return v;
613 }
614
615 /* Build compile-time evalable representations of member-initializer list
616 for a constexpr constructor. */
617
618 static tree
619 build_constexpr_constructor_member_initializers (tree type, tree body)
620 {
621 vec<constructor_elt, va_gc> *vec = NULL;
622 bool ok = true;
623 while (true)
624 switch (TREE_CODE (body))
625 {
626 case MUST_NOT_THROW_EXPR:
627 case EH_SPEC_BLOCK:
628 body = TREE_OPERAND (body, 0);
629 break;
630
631 case STATEMENT_LIST:
632 for (tree stmt : tsi_range (body))
633 {
634 body = stmt;
635 if (TREE_CODE (body) == BIND_EXPR)
636 break;
637 }
638 break;
639
640 case BIND_EXPR:
641 body = BIND_EXPR_BODY (body);
642 goto found;
643
644 default:
645 gcc_unreachable ();
646 }
647 found:
648 if (TREE_CODE (body) == TRY_BLOCK)
649 {
650 body = TREE_OPERAND (body, 0);
651 if (TREE_CODE (body) == BIND_EXPR)
652 body = BIND_EXPR_BODY (body);
653 }
654 if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
655 {
656 body = TREE_OPERAND (body, 0);
657 if (TREE_CODE (body) == EXPR_STMT)
658 body = TREE_OPERAND (body, 0);
659 if (TREE_CODE (body) == INIT_EXPR
660 && (same_type_ignoring_top_level_qualifiers_p
661 (TREE_TYPE (TREE_OPERAND (body, 0)),
662 current_class_type)))
663 {
664 /* Trivial copy. */
665 return TREE_OPERAND (body, 1);
666 }
667 ok = build_data_member_initialization (body, &vec);
668 }
669 else if (TREE_CODE (body) == STATEMENT_LIST)
670 {
671 for (tree stmt : tsi_range (body))
672 {
673 ok = build_data_member_initialization (stmt, &vec);
674 if (!ok)
675 break;
676 }
677 }
678 else if (EXPR_P (body))
679 ok = build_data_member_initialization (body, &vec);
680 else
681 gcc_assert (errorcount > 0);
682 if (ok)
683 {
684 if (vec_safe_length (vec) > 0)
685 {
686 /* In a delegating constructor, return the target. */
687 constructor_elt *ce = &(*vec)[0];
688 if (ce->index == current_class_ptr)
689 {
690 body = ce->value;
691 vec_free (vec);
692 return body;
693 }
694 }
695 vec = sort_constexpr_mem_initializers (type, vec);
696 return build_constructor (type, vec);
697 }
698 else
699 return error_mark_node;
700 }
701
702 /* We have an expression tree T that represents a call, either CALL_EXPR
703 or AGGR_INIT_EXPR. If the call is lexically to a named function,
704 retrun the _DECL for that function. */
705
706 static tree
707 get_function_named_in_call (tree t)
708 {
709 tree fun = cp_get_callee (t);
710 if (fun && TREE_CODE (fun) == ADDR_EXPR
711 && TREE_CODE (TREE_OPERAND (fun, 0)) == FUNCTION_DECL)
712 fun = TREE_OPERAND (fun, 0);
713 return fun;
714 }
715
716 /* Subroutine of check_constexpr_fundef. BODY is the body of a function
717 declared to be constexpr, or a sub-statement thereof. Returns the
718 return value if suitable, error_mark_node for a statement not allowed in
719 a constexpr function, or NULL_TREE if no return value was found. */
720
721 tree
722 constexpr_fn_retval (tree body)
723 {
724 switch (TREE_CODE (body))
725 {
726 case STATEMENT_LIST:
727 {
728 tree expr = NULL_TREE;
729 for (tree stmt : tsi_range (body))
730 {
731 tree s = constexpr_fn_retval (stmt);
732 if (s == error_mark_node)
733 return error_mark_node;
734 else if (s == NULL_TREE)
735 /* Keep iterating. */;
736 else if (expr)
737 /* Multiple return statements. */
738 return error_mark_node;
739 else
740 expr = s;
741 }
742 return expr;
743 }
744
745 case RETURN_EXPR:
746 return break_out_target_exprs (TREE_OPERAND (body, 0));
747
748 case DECL_EXPR:
749 {
750 tree decl = DECL_EXPR_DECL (body);
751 if (TREE_CODE (decl) == USING_DECL
752 /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
753 || DECL_ARTIFICIAL (decl))
754 return NULL_TREE;
755 return error_mark_node;
756 }
757
758 case CLEANUP_POINT_EXPR:
759 return constexpr_fn_retval (TREE_OPERAND (body, 0));
760
761 case BIND_EXPR:
762 if (!check_constexpr_bind_expr_vars (body))
763 return error_mark_node;
764 return constexpr_fn_retval (BIND_EXPR_BODY (body));
765
766 case USING_STMT:
767 case DEBUG_BEGIN_STMT:
768 return NULL_TREE;
769
770 case CALL_EXPR:
771 {
772 tree fun = get_function_named_in_call (body);
773 if (fun != NULL_TREE
774 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
775 return NULL_TREE;
776 }
777 /* Fallthru. */
778
779 default:
780 return error_mark_node;
781 }
782 }
783
784 /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
785 FUN; do the necessary transformations to turn it into a single expression
786 that we can store in the hash table. */
787
788 static tree
789 massage_constexpr_body (tree fun, tree body)
790 {
791 if (DECL_CONSTRUCTOR_P (fun))
792 body = build_constexpr_constructor_member_initializers
793 (DECL_CONTEXT (fun), body);
794 else if (cxx_dialect < cxx14)
795 {
796 if (TREE_CODE (body) == EH_SPEC_BLOCK)
797 body = EH_SPEC_STMTS (body);
798 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
799 body = TREE_OPERAND (body, 0);
800 body = constexpr_fn_retval (body);
801 }
802 return body;
803 }
804
805 /* CTYPE is a type constructed from BODY. Return true if some
806 bases/fields are uninitialized, and complain if COMPLAIN. */
807
808 static bool
809 cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
810 {
811 /* We allow uninitialized bases/fields in C++20. */
812 if (cxx_dialect >= cxx20)
813 return false;
814
815 unsigned nelts = 0;
816
817 if (body)
818 {
819 if (TREE_CODE (body) != CONSTRUCTOR)
820 return false;
821 nelts = CONSTRUCTOR_NELTS (body);
822 }
823 tree field = TYPE_FIELDS (ctype);
824
825 if (TREE_CODE (ctype) == UNION_TYPE)
826 {
827 if (nelts == 0 && next_aggregate_field (field))
828 {
829 if (complain)
830 error ("%<constexpr%> constructor for union %qT must "
831 "initialize exactly one non-static data member", ctype);
832 return true;
833 }
834 return false;
835 }
836
837 /* Iterate over the CONSTRUCTOR, checking any missing fields don't
838 need an explicit initialization. */
839 bool bad = false;
840 for (unsigned i = 0; i <= nelts; ++i)
841 {
842 tree index = NULL_TREE;
843 if (i < nelts)
844 {
845 index = CONSTRUCTOR_ELT (body, i)->index;
846 /* Skip base and vtable inits. */
847 if (TREE_CODE (index) != FIELD_DECL
848 || DECL_ARTIFICIAL (index))
849 continue;
850 }
851
852 for (; field != index; field = DECL_CHAIN (field))
853 {
854 tree ftype;
855 if (TREE_CODE (field) != FIELD_DECL)
856 continue;
857 if (DECL_UNNAMED_BIT_FIELD (field))
858 continue;
859 if (DECL_ARTIFICIAL (field))
860 continue;
861 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
862 {
863 /* Recurse to check the anonymous aggregate member. */
864 bad |= cx_check_missing_mem_inits
865 (TREE_TYPE (field), NULL_TREE, complain);
866 if (bad && !complain)
867 return true;
868 continue;
869 }
870 ftype = TREE_TYPE (field);
871 if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
872 /* A flexible array can't be intialized here, so don't complain
873 that it isn't. */
874 continue;
875 if (is_empty_field (field))
876 /* An empty field doesn't need an initializer. */
877 continue;
878 ftype = strip_array_types (ftype);
879 if (type_has_constexpr_default_constructor (ftype))
880 {
881 /* It's OK to skip a member with a trivial constexpr ctor.
882 A constexpr ctor that isn't trivial should have been
883 added in by now. */
884 gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
885 || errorcount != 0);
886 continue;
887 }
888 if (!complain)
889 return true;
890 auto_diagnostic_group d;
891 error ("member %qD must be initialized by mem-initializer "
892 "in %<constexpr%> constructor", field);
893 inform (DECL_SOURCE_LOCATION (field), "declared here");
894 bad = true;
895 }
896 if (field == NULL_TREE)
897 break;
898
899 if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
900 {
901 /* Check the anonymous aggregate initializer is valid. */
902 bad |= cx_check_missing_mem_inits
903 (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
904 if (bad && !complain)
905 return true;
906 }
907 field = DECL_CHAIN (field);
908 }
909
910 return bad;
911 }
912
913 /* We are processing the definition of the constexpr function FUN.
914 Check that its body fulfills the apropriate requirements and
915 enter it in the constexpr function definition table. */
916
917 void
918 maybe_save_constexpr_fundef (tree fun)
919 {
920 if (processing_template_decl
921 || cp_function_chain->invalid_constexpr
922 || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
923 return;
924
925 /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
926 actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
927 bool implicit = false;
928 if (flag_implicit_constexpr)
929 {
930 if (DECL_DELETING_DESTRUCTOR_P (fun)
931 && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
932 /* Don't inherit implicit constexpr from the non-deleting
933 destructor. */
934 DECL_DECLARED_CONSTEXPR_P (fun) = false;
935
936 if (!DECL_DECLARED_CONSTEXPR_P (fun)
937 && DECL_DECLARED_INLINE_P (fun)
938 && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
939 implicit = true;
940 }
941
942 if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
943 return;
944
945 bool complain = !DECL_GENERATED_P (fun) && !implicit;
946
947 if (!is_valid_constexpr_fn (fun, complain))
948 return;
949
950 tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
951 if (massaged == NULL_TREE || massaged == error_mark_node)
952 {
953 if (!DECL_CONSTRUCTOR_P (fun) && complain)
954 error ("body of %<constexpr%> function %qD not a return-statement",
955 fun);
956 return;
957 }
958
959 bool potential = potential_rvalue_constant_expression (massaged);
960 if (!potential && complain)
961 require_potential_rvalue_constant_expression_fncheck (massaged);
962
963 if (DECL_CONSTRUCTOR_P (fun) && potential
964 && !DECL_DEFAULTED_FN (fun))
965 {
966 if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
967 massaged, complain))
968 potential = false;
969 else if (cxx_dialect > cxx11)
970 {
971 /* What we got from massage_constexpr_body is pretty much just the
972 ctor-initializer, also check the body. */
973 massaged = DECL_SAVED_TREE (fun);
974 potential = potential_rvalue_constant_expression (massaged);
975 if (!potential && complain)
976 require_potential_rvalue_constant_expression_fncheck (massaged);
977 }
978 }
979
980 if (!potential && complain
981 /* If -Wno-invalid-constexpr was specified, we haven't complained
982 about non-constant expressions yet. Register the function and
983 complain in explain_invalid_constexpr_fn if the function is
984 called. */
985 && warn_invalid_constexpr != 0)
986 return;
987
988 if (implicit)
989 {
990 if (potential)
991 {
992 DECL_DECLARED_CONSTEXPR_P (fun) = true;
993 DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
994 if (DECL_CONSTRUCTOR_P (fun))
995 TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
996 }
997 else
998 /* Don't bother keeping the pre-generic body of unsuitable functions
999 not explicitly declared constexpr. */
1000 return;
1001 }
1002
1003 constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1004 bool clear_ctx = false;
1005 if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1006 {
1007 clear_ctx = true;
1008 DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1009 }
1010 tree saved_fn = current_function_decl;
1011 current_function_decl = fun;
1012 entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1013 current_function_decl = saved_fn;
1014 if (clear_ctx)
1015 DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1016 if (!potential)
1017 /* For a template instantiation, we want to remember the pre-generic body
1018 for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1019 that it doesn't need to bother trying to expand the function. */
1020 entry.result = error_mark_node;
1021
1022 register_constexpr_fundef (entry);
1023 }
1024
1025 /* BODY is a validated and massaged definition of a constexpr
1026 function. Register it in the hash table. */
1027
1028 void
1029 register_constexpr_fundef (const constexpr_fundef &value)
1030 {
1031 /* Create the constexpr function table if necessary. */
1032 if (constexpr_fundef_table == NULL)
1033 constexpr_fundef_table
1034 = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1035
1036 constexpr_fundef **slot = constexpr_fundef_table->find_slot
1037 (const_cast<constexpr_fundef *> (&value), INSERT);
1038
1039 gcc_assert (*slot == NULL);
1040 *slot = ggc_alloc<constexpr_fundef> ();
1041 **slot = value;
1042 }
1043
1044 /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1045 function called in a context that requires a constant expression).
1046 If it comes from a constexpr template, explain why the instantiation
1047 isn't constexpr. Otherwise, explain why the function cannot be used
1048 in a constexpr context. */
1049
1050 void
1051 explain_invalid_constexpr_fn (tree fun)
1052 {
1053 static hash_set<tree> *diagnosed;
1054 tree body;
1055 /* In C++23, a function marked 'constexpr' may not actually be a constant
1056 expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1057 wasn't enabled. The function was called, so diagnose why it cannot be
1058 used in a constant expression. */
1059 if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1060 /* Go on. */;
1061 /* Only diagnose defaulted functions, lambdas, or instantiations. */
1062 else if (!DECL_DEFAULTED_FN (fun)
1063 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1064 && !is_instantiation_of_constexpr (fun))
1065 {
1066 inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1067 return;
1068 }
1069 if (diagnosed == NULL)
1070 diagnosed = new hash_set<tree>;
1071 if (diagnosed->add (fun))
1072 /* Already explained. */
1073 return;
1074
1075 iloc_sentinel ils = input_location;
1076 if (!lambda_static_thunk_p (fun))
1077 {
1078 /* Diagnostics should completely ignore the static thunk, so leave
1079 input_location set to our caller's location. */
1080 input_location = DECL_SOURCE_LOCATION (fun);
1081 inform (input_location,
1082 "%qD is not usable as a %<constexpr%> function because:", fun);
1083 }
1084 /* First check the declaration. */
1085 if (is_valid_constexpr_fn (fun, true))
1086 {
1087 /* Then if it's OK, the body. */
1088 if (!DECL_DECLARED_CONSTEXPR_P (fun)
1089 && DECL_DEFAULTED_FN (fun))
1090 explain_implicit_non_constexpr (fun);
1091 else
1092 {
1093 if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1094 body = fd->body;
1095 else
1096 body = DECL_SAVED_TREE (fun);
1097 body = massage_constexpr_body (fun, body);
1098 require_potential_rvalue_constant_expression (body);
1099 if (DECL_CONSTRUCTOR_P (fun))
1100 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
1101 }
1102 }
1103 }
1104
1105 /* Objects of this type represent calls to constexpr functions
1106 along with the bindings of parameters to their arguments, for
1107 the purpose of compile time evaluation. */
1108
1109 struct GTY((for_user)) constexpr_call {
1110 /* Description of the constexpr function definition. */
1111 constexpr_fundef *fundef;
1112 /* Parameter bindings environment. A TREE_VEC of arguments. */
1113 tree bindings;
1114 /* Result of the call.
1115 NULL means the call is being evaluated.
1116 error_mark_node means that the evaluation was erroneous;
1117 otherwise, the actuall value of the call. */
1118 tree result;
1119 /* The hash of this call; we remember it here to avoid having to
1120 recalculate it when expanding the hash table. */
1121 hashval_t hash;
1122 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1123 bool manifestly_const_eval;
1124 };
1125
1126 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1127 {
1128 static hashval_t hash (constexpr_call *);
1129 static bool equal (constexpr_call *, constexpr_call *);
1130 };
1131
1132 enum constexpr_switch_state {
1133 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1134 and default: label for that switch has not been seen yet. */
1135 css_default_not_seen,
1136 /* Used when processing a switch for the first time by cxx_eval_switch_expr
1137 and default: label for that switch has been seen already. */
1138 css_default_seen,
1139 /* Used when processing a switch for the second time by
1140 cxx_eval_switch_expr, where default: label should match. */
1141 css_default_processing
1142 };
1143
1144 /* The constexpr expansion context part which needs one instance per
1145 cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1146 variables initialized within the expression. */
1147
1148 class constexpr_global_ctx {
1149 /* Values for any temporaries or local variables within the
1150 constant-expression. */
1151 hash_map<tree,tree> values;
1152 public:
1153 /* Number of cxx_eval_constant_expression calls (except skipped ones,
1154 on simple constants or location wrappers) encountered during current
1155 cxx_eval_outermost_constant_expr call. */
1156 HOST_WIDE_INT constexpr_ops_count;
1157 /* Heap VAR_DECLs created during the evaluation of the outermost constant
1158 expression. */
1159 auto_vec<tree, 16> heap_vars;
1160 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1161 vec<tree> *cleanups;
1162 /* If non-null, only allow modification of existing values of the variables
1163 in this set. Set by modifiable_tracker, below. */
1164 hash_set<tree> *modifiable;
1165 /* Number of heap VAR_DECL deallocations. */
1166 unsigned heap_dealloc_count;
1167 /* Constructor. */
1168 constexpr_global_ctx ()
1169 : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1170 heap_dealloc_count (0) {}
1171
1172 tree get_value (tree t)
1173 {
1174 if (tree *p = values.get (t))
1175 return *p;
1176 return NULL_TREE;
1177 }
1178 tree *get_value_ptr (tree t)
1179 {
1180 if (modifiable && !modifiable->contains (t))
1181 return nullptr;
1182 return values.get (t);
1183 }
1184 void put_value (tree t, tree v)
1185 {
1186 bool already_in_map = values.put (t, v);
1187 if (!already_in_map && modifiable)
1188 modifiable->add (t);
1189 }
1190 void remove_value (tree t) { values.remove (t); }
1191 };
1192
1193 /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1194 side-effects from evaluation of a particular subexpression of a
1195 constant-expression. In such cases we use modifiable_tracker to prevent
1196 modification of variables created outside of that subexpression.
1197
1198 ??? We could change the hash_set to a hash_map, allow and track external
1199 modifications, and roll them back in the destructor. It's not clear to me
1200 that this would be worthwhile. */
1201
1202 class modifiable_tracker
1203 {
1204 hash_set<tree> set;
1205 constexpr_global_ctx *global;
1206 public:
1207 modifiable_tracker (constexpr_global_ctx *g): global(g)
1208 {
1209 global->modifiable = &set;
1210 }
1211 ~modifiable_tracker ()
1212 {
1213 for (tree t: set)
1214 global->remove_value (t);
1215 global->modifiable = nullptr;
1216 }
1217 };
1218
1219 /* The constexpr expansion context. CALL is the current function
1220 expansion, CTOR is the current aggregate initializer, OBJECT is the
1221 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1222
1223 struct constexpr_ctx {
1224 /* The part of the context that needs to be unique to the whole
1225 cxx_eval_outermost_constant_expr invocation. */
1226 constexpr_global_ctx *global;
1227 /* The innermost call we're evaluating. */
1228 constexpr_call *call;
1229 /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1230 within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1231 vec<tree> *save_exprs;
1232 /* The CONSTRUCTOR we're currently building up for an aggregate
1233 initializer. */
1234 tree ctor;
1235 /* The object we're building the CONSTRUCTOR for. */
1236 tree object;
1237 /* If inside SWITCH_EXPR. */
1238 constexpr_switch_state *css_state;
1239 /* The aggregate initialization context inside which this one is nested. This
1240 is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1241 const constexpr_ctx *parent;
1242
1243 /* Whether we should error on a non-constant expression or fail quietly.
1244 This flag needs to be here, but some of the others could move to global
1245 if they get larger than a word. */
1246 bool quiet;
1247 /* Whether we are strictly conforming to constant expression rules or
1248 trying harder to get a constant value. */
1249 bool strict;
1250 /* Whether __builtin_is_constant_evaluated () should be true. */
1251 bool manifestly_const_eval;
1252 };
1253
1254 /* This internal flag controls whether we should avoid doing anything during
1255 constexpr evaluation that would cause extra DECL_UID generation, such as
1256 template instantiation and function body copying. */
1257
1258 static bool uid_sensitive_constexpr_evaluation_value;
1259
1260 /* An internal counter that keeps track of the number of times
1261 uid_sensitive_constexpr_evaluation_p returned true. */
1262
1263 static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1264
1265 /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1266 increments the corresponding counter. */
1267
1268 static bool
1269 uid_sensitive_constexpr_evaluation_p ()
1270 {
1271 if (uid_sensitive_constexpr_evaluation_value)
1272 {
1273 ++uid_sensitive_constexpr_evaluation_true_counter;
1274 return true;
1275 }
1276 else
1277 return false;
1278 }
1279
1280 /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1281 enables the internal flag for uid_sensitive_constexpr_evaluation_p
1282 during the lifetime of the sentinel object. Upon its destruction, the
1283 previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1284
1285 uid_sensitive_constexpr_evaluation_sentinel
1286 ::uid_sensitive_constexpr_evaluation_sentinel ()
1287 : ovr (uid_sensitive_constexpr_evaluation_value, true)
1288 {
1289 }
1290
1291 /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1292 records the current number of times that uid_sensitive_constexpr_evaluation_p
1293 has been called and returned true. */
1294
1295 uid_sensitive_constexpr_evaluation_checker
1296 ::uid_sensitive_constexpr_evaluation_checker ()
1297 : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1298 {
1299 }
1300
1301 /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1302 some constexpr evaluation was restricted due to u_s_c_e_p being called
1303 and returning true during the lifetime of this checker object. */
1304
1305 bool
1306 uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1307 {
1308 return (uid_sensitive_constexpr_evaluation_value
1309 && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1310 }
1311
1312
1313 /* A table of all constexpr calls that have been evaluated by the
1314 compiler in this translation unit. */
1315
1316 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1317
1318 /* Compute a hash value for a constexpr call representation. */
1319
1320 inline hashval_t
1321 constexpr_call_hasher::hash (constexpr_call *info)
1322 {
1323 return info->hash;
1324 }
1325
1326 /* Return true if the objects pointed to by P and Q represent calls
1327 to the same constexpr function with the same arguments.
1328 Otherwise, return false. */
1329
1330 bool
1331 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1332 {
1333 if (lhs == rhs)
1334 return true;
1335 if (lhs->hash != rhs->hash)
1336 return false;
1337 if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
1338 return false;
1339 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1340 return false;
1341 return cp_tree_equal (lhs->bindings, rhs->bindings);
1342 }
1343
1344 /* Initialize the constexpr call table, if needed. */
1345
1346 static void
1347 maybe_initialize_constexpr_call_table (void)
1348 {
1349 if (constexpr_call_table == NULL)
1350 constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1351 }
1352
1353 /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1354 a function happens to get called recursively, we unshare the callee
1355 function's body and evaluate this unshared copy instead of evaluating the
1356 original body.
1357
1358 FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1359 copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1360 that's keyed off of the original FUNCTION_DECL and whose value is a
1361 TREE_LIST of this function's unused copies awaiting reuse.
1362
1363 This is not GC-deletable to avoid GC affecting UID generation. */
1364
1365 static GTY(()) decl_tree_map *fundef_copies_table;
1366
1367 /* Reuse a copy or create a new unshared copy of the function FUN.
1368 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1369 is parms, TYPE is result. */
1370
1371 static tree
1372 get_fundef_copy (constexpr_fundef *fundef)
1373 {
1374 tree copy;
1375 bool existed;
1376 tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1377 (fundef_copies_table, fundef->decl, &existed, 127));
1378
1379 if (!existed)
1380 {
1381 /* There is no cached function available, or in use. We can use
1382 the function directly. That the slot is now created records
1383 that this function is now in use. */
1384 copy = build_tree_list (fundef->body, fundef->parms);
1385 TREE_TYPE (copy) = fundef->result;
1386 }
1387 else if (*slot == NULL_TREE)
1388 {
1389 if (uid_sensitive_constexpr_evaluation_p ())
1390 return NULL_TREE;
1391
1392 /* We've already used the function itself, so make a copy. */
1393 copy = build_tree_list (NULL, NULL);
1394 tree saved_body = DECL_SAVED_TREE (fundef->decl);
1395 tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1396 tree saved_result = DECL_RESULT (fundef->decl);
1397 tree saved_fn = current_function_decl;
1398 DECL_SAVED_TREE (fundef->decl) = fundef->body;
1399 DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1400 DECL_RESULT (fundef->decl) = fundef->result;
1401 current_function_decl = fundef->decl;
1402 TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1403 TREE_TYPE (copy));
1404 current_function_decl = saved_fn;
1405 DECL_RESULT (fundef->decl) = saved_result;
1406 DECL_ARGUMENTS (fundef->decl) = saved_parms;
1407 DECL_SAVED_TREE (fundef->decl) = saved_body;
1408 }
1409 else
1410 {
1411 /* We have a cached function available. */
1412 copy = *slot;
1413 *slot = TREE_CHAIN (copy);
1414 }
1415
1416 return copy;
1417 }
1418
1419 /* Save the copy COPY of function FUN for later reuse by
1420 get_fundef_copy(). By construction, there will always be an entry
1421 to find. */
1422
1423 static void
1424 save_fundef_copy (tree fun, tree copy)
1425 {
1426 tree *slot = fundef_copies_table->get (fun);
1427 TREE_CHAIN (copy) = *slot;
1428 *slot = copy;
1429 }
1430
1431 /* Whether our evaluation wants a prvalue (e.g. CONSTRUCTOR or _CST),
1432 a glvalue (e.g. VAR_DECL or _REF), or nothing. */
1433
1434 enum value_cat {
1435 vc_prvalue = 0,
1436 vc_glvalue = 1,
1437 vc_discard = 2
1438 };
1439
1440 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1441 value_cat, bool *, bool *, tree * = NULL);
1442 static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1443 bool * = NULL);
1444 static tree find_heap_var_refs (tree *, int *, void *);
1445
1446 /* Attempt to evaluate T which represents a call to a builtin function.
1447 We assume here that all builtin functions evaluate to scalar types
1448 represented by _CST nodes. */
1449
1450 static tree
1451 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1452 value_cat lval,
1453 bool *non_constant_p, bool *overflow_p)
1454 {
1455 const int nargs = call_expr_nargs (t);
1456 tree *args = (tree *) alloca (nargs * sizeof (tree));
1457 tree new_call;
1458 int i;
1459
1460 /* Don't fold __builtin_constant_p within a constexpr function. */
1461 bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
1462
1463 /* If we aren't requiring a constant expression, defer __builtin_constant_p
1464 in a constexpr function until we have values for the parameters. */
1465 if (bi_const_p
1466 && !ctx->manifestly_const_eval
1467 && current_function_decl
1468 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1469 {
1470 *non_constant_p = true;
1471 return t;
1472 }
1473
1474 /* For __builtin_is_constant_evaluated, defer it if not
1475 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
1476 without manifestly_const_eval even expressions or parts thereof which
1477 will later be manifestly const_eval evaluated), otherwise fold it to
1478 true. */
1479 if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1480 BUILT_IN_FRONTEND))
1481 {
1482 if (!ctx->manifestly_const_eval)
1483 {
1484 *non_constant_p = true;
1485 return t;
1486 }
1487 return boolean_true_node;
1488 }
1489
1490 if (fndecl_built_in_p (fun, CP_BUILT_IN_SOURCE_LOCATION, BUILT_IN_FRONTEND))
1491 {
1492 temp_override<tree> ovr (current_function_decl);
1493 if (ctx->call && ctx->call->fundef)
1494 current_function_decl = ctx->call->fundef->decl;
1495 return fold_builtin_source_location (EXPR_LOCATION (t));
1496 }
1497
1498 int strops = 0;
1499 int strret = 0;
1500 if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
1501 switch (DECL_FUNCTION_CODE (fun))
1502 {
1503 case BUILT_IN_STRLEN:
1504 case BUILT_IN_STRNLEN:
1505 strops = 1;
1506 break;
1507 case BUILT_IN_MEMCHR:
1508 case BUILT_IN_STRCHR:
1509 case BUILT_IN_STRRCHR:
1510 strops = 1;
1511 strret = 1;
1512 break;
1513 case BUILT_IN_MEMCMP:
1514 case BUILT_IN_STRCMP:
1515 strops = 2;
1516 break;
1517 case BUILT_IN_STRSTR:
1518 strops = 2;
1519 strret = 1;
1520 break;
1521 case BUILT_IN_ASAN_POINTER_COMPARE:
1522 case BUILT_IN_ASAN_POINTER_SUBTRACT:
1523 /* These builtins shall be ignored during constant expression
1524 evaluation. */
1525 return void_node;
1526 case BUILT_IN_UNREACHABLE:
1527 case BUILT_IN_TRAP:
1528 if (!*non_constant_p && !ctx->quiet)
1529 {
1530 /* Do not allow__builtin_unreachable in constexpr function.
1531 The __builtin_unreachable call with BUILTINS_LOCATION
1532 comes from cp_maybe_instrument_return. */
1533 if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
1534 error ("%<constexpr%> call flows off the end of the function");
1535 else
1536 error ("%q+E is not a constant expression", t);
1537 }
1538 *non_constant_p = true;
1539 return t;
1540 default:
1541 break;
1542 }
1543
1544 /* Be permissive for arguments to built-ins; __builtin_constant_p should
1545 return constant false for a non-constant argument. */
1546 constexpr_ctx new_ctx = *ctx;
1547 new_ctx.quiet = true;
1548 for (i = 0; i < nargs; ++i)
1549 {
1550 tree arg = CALL_EXPR_ARG (t, i);
1551 tree oarg = arg;
1552
1553 /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
1554 expand_builtin doesn't know how to look in the values table. */
1555 bool strop = i < strops;
1556 if (strop)
1557 {
1558 STRIP_NOPS (arg);
1559 if (TREE_CODE (arg) == ADDR_EXPR)
1560 arg = TREE_OPERAND (arg, 0);
1561 else
1562 strop = false;
1563 }
1564
1565 /* If builtin_valid_in_constant_expr_p is true,
1566 potential_constant_expression_1 has not recursed into the arguments
1567 of the builtin, verify it here. */
1568 if (!builtin_valid_in_constant_expr_p (fun)
1569 || potential_constant_expression (arg))
1570 {
1571 bool dummy1 = false, dummy2 = false;
1572 arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
1573 &dummy1, &dummy2);
1574 }
1575
1576 if (bi_const_p)
1577 /* For __builtin_constant_p, fold all expressions with constant values
1578 even if they aren't C++ constant-expressions. */
1579 arg = cp_fold_rvalue (arg);
1580 else if (strop)
1581 {
1582 if (TREE_CODE (arg) == CONSTRUCTOR)
1583 arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
1584 if (TREE_CODE (arg) == STRING_CST)
1585 arg = build_address (arg);
1586 else
1587 arg = oarg;
1588 }
1589
1590 args[i] = arg;
1591 }
1592
1593 bool save_ffbcp = force_folding_builtin_constant_p;
1594 force_folding_builtin_constant_p |= ctx->manifestly_const_eval;
1595 tree save_cur_fn = current_function_decl;
1596 /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
1597 if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
1598 && ctx->call
1599 && ctx->call->fundef)
1600 current_function_decl = ctx->call->fundef->decl;
1601 if (fndecl_built_in_p (fun,
1602 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
1603 BUILT_IN_FRONTEND))
1604 {
1605 location_t loc = EXPR_LOCATION (t);
1606 if (nargs >= 1)
1607 VERIFY_CONSTANT (args[0]);
1608 new_call
1609 = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
1610 args);
1611 }
1612 else if (fndecl_built_in_p (fun,
1613 CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
1614 BUILT_IN_FRONTEND))
1615 {
1616 location_t loc = EXPR_LOCATION (t);
1617 if (nargs >= 2)
1618 {
1619 VERIFY_CONSTANT (args[0]);
1620 VERIFY_CONSTANT (args[1]);
1621 }
1622 new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
1623 }
1624 else
1625 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1626 CALL_EXPR_FN (t), nargs, args);
1627 current_function_decl = save_cur_fn;
1628 force_folding_builtin_constant_p = save_ffbcp;
1629 if (new_call == NULL)
1630 {
1631 if (!*non_constant_p && !ctx->quiet)
1632 {
1633 new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
1634 CALL_EXPR_FN (t), nargs, args);
1635 error ("%q+E is not a constant expression", new_call);
1636 }
1637 *non_constant_p = true;
1638 return t;
1639 }
1640
1641 if (!potential_constant_expression (new_call))
1642 {
1643 if (!*non_constant_p && !ctx->quiet)
1644 error ("%q+E is not a constant expression", new_call);
1645 *non_constant_p = true;
1646 return t;
1647 }
1648
1649 if (strret)
1650 {
1651 /* memchr returns a pointer into the first argument, but we replaced the
1652 argument above with a STRING_CST; put it back it now. */
1653 tree op = CALL_EXPR_ARG (t, strret-1);
1654 STRIP_NOPS (new_call);
1655 if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
1656 TREE_OPERAND (new_call, 0) = op;
1657 else if (TREE_CODE (new_call) == ADDR_EXPR)
1658 new_call = op;
1659 }
1660
1661 return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1662 non_constant_p, overflow_p);
1663 }
1664
1665 /* TEMP is the constant value of a temporary object of type TYPE. Adjust
1666 the type of the value to match. */
1667
1668 static tree
1669 adjust_temp_type (tree type, tree temp)
1670 {
1671 if (same_type_p (TREE_TYPE (temp), type))
1672 return temp;
1673 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1674 if (TREE_CODE (temp) == CONSTRUCTOR)
1675 {
1676 /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
1677 tree t = copy_node (temp);
1678 TREE_TYPE (t) = type;
1679 return t;
1680 }
1681 if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1682 return build0 (EMPTY_CLASS_EXPR, type);
1683 gcc_assert (scalarish_type_p (type));
1684 /* Now we know we're dealing with a scalar, and a prvalue of non-class
1685 type is cv-unqualified. */
1686 return cp_fold_convert (cv_unqualified (type), temp);
1687 }
1688
1689 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1690 sub-CONSTRUCTORs. Otherwise return T.
1691
1692 We use this whenever we initialize an object as a whole, whether it's a
1693 parameter, a local variable, or a subobject, so that subsequent
1694 modifications don't affect other places where it was used. */
1695
1696 tree
1697 unshare_constructor (tree t MEM_STAT_DECL)
1698 {
1699 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1700 return t;
1701 auto_vec <tree*, 4> ptrs;
1702 ptrs.safe_push (&t);
1703 while (!ptrs.is_empty ())
1704 {
1705 tree *p = ptrs.pop ();
1706 tree n = copy_node (*p PASS_MEM_STAT);
1707 CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
1708 *p = n;
1709 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
1710 constructor_elt *ce;
1711 for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
1712 if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
1713 ptrs.safe_push (&ce->value);
1714 }
1715 return t;
1716 }
1717
1718 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1719
1720 static void
1721 free_constructor (tree t)
1722 {
1723 if (!t || TREE_CODE (t) != CONSTRUCTOR)
1724 return;
1725 releasing_vec ctors;
1726 vec_safe_push (ctors, t);
1727 while (!ctors->is_empty ())
1728 {
1729 tree c = ctors->pop ();
1730 if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
1731 {
1732 constructor_elt *ce;
1733 for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
1734 if (TREE_CODE (ce->value) == CONSTRUCTOR)
1735 vec_safe_push (ctors, ce->value);
1736 ggc_free (elts);
1737 }
1738 ggc_free (c);
1739 }
1740 }
1741
1742 /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
1743 if *TP is address of a static variable (or part of it) currently being
1744 constructed or of a heap artificial variable. */
1745
1746 static tree
1747 addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
1748 {
1749 if (TREE_CODE (*tp) == ADDR_EXPR)
1750 if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
1751 if (VAR_P (var) && TREE_STATIC (var))
1752 {
1753 if (DECL_NAME (var) == heap_uninit_identifier
1754 || DECL_NAME (var) == heap_identifier
1755 || DECL_NAME (var) == heap_vec_uninit_identifier
1756 || DECL_NAME (var) == heap_vec_identifier)
1757 return var;
1758
1759 constexpr_global_ctx *global = (constexpr_global_ctx *) data;
1760 if (global->get_value (var))
1761 return var;
1762 }
1763 if (TYPE_P (*tp))
1764 *walk_subtrees = false;
1765 return NULL_TREE;
1766 }
1767
1768 /* Subroutine of cxx_eval_call_expression.
1769 We are processing a call expression (either CALL_EXPR or
1770 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1771 all arguments and bind their values to correspondings
1772 parameters, making up the NEW_CALL context. */
1773
1774 static tree
1775 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
1776 bool *non_constant_p, bool *overflow_p,
1777 bool *non_constant_args)
1778 {
1779 const int nargs = call_expr_nargs (t);
1780 tree parms = DECL_ARGUMENTS (fun);
1781 int i;
1782 /* We don't record ellipsis args below. */
1783 int nparms = list_length (parms);
1784 int nbinds = nargs < nparms ? nargs : nparms;
1785 tree binds = make_tree_vec (nbinds);
1786 for (i = 0; i < nargs; ++i)
1787 {
1788 tree x, arg;
1789 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1790 if (parms && DECL_BY_REFERENCE (parms))
1791 type = TREE_TYPE (type);
1792 x = get_nth_callarg (t, i);
1793 /* For member function, the first argument is a pointer to the implied
1794 object. For a constructor, it might still be a dummy object, in
1795 which case we get the real argument from ctx. */
1796 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1797 && is_dummy_object (x))
1798 {
1799 x = ctx->object;
1800 x = build_address (x);
1801 }
1802 if (TREE_ADDRESSABLE (type))
1803 /* Undo convert_for_arg_passing work here. */
1804 x = convert_from_reference (x);
1805 /* Normally we would strip a TARGET_EXPR in an initialization context
1806 such as this, but here we do the elision differently: we keep the
1807 TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
1808 arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
1809 non_constant_p, overflow_p);
1810 /* Don't VERIFY_CONSTANT here. */
1811 if (*non_constant_p && ctx->quiet)
1812 break;
1813 /* Just discard ellipsis args after checking their constantitude. */
1814 if (!parms)
1815 continue;
1816
1817 if (!*non_constant_p)
1818 {
1819 /* Make sure the binding has the same type as the parm. But
1820 only for constant args. */
1821 if (!TYPE_REF_P (type))
1822 arg = adjust_temp_type (type, arg);
1823 if (!TREE_CONSTANT (arg))
1824 *non_constant_args = true;
1825 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
1826 /* The destructor needs to see any modifications the callee makes
1827 to the argument. */
1828 *non_constant_args = true;
1829 /* If arg is or contains address of a heap artificial variable or
1830 of a static variable being constructed, avoid caching the
1831 function call, as those variables might be modified by the
1832 function, or might be modified by the callers in between
1833 the cached function and just read by the function. */
1834 else if (!*non_constant_args
1835 && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
1836 NULL))
1837 *non_constant_args = true;
1838
1839 /* For virtual calls, adjust the this argument, so that it is
1840 the object on which the method is called, rather than
1841 one of its bases. */
1842 if (i == 0 && DECL_VIRTUAL_P (fun))
1843 {
1844 tree addr = arg;
1845 STRIP_NOPS (addr);
1846 if (TREE_CODE (addr) == ADDR_EXPR)
1847 {
1848 tree obj = TREE_OPERAND (addr, 0);
1849 while (TREE_CODE (obj) == COMPONENT_REF
1850 && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
1851 && !same_type_ignoring_top_level_qualifiers_p
1852 (TREE_TYPE (obj), DECL_CONTEXT (fun)))
1853 obj = TREE_OPERAND (obj, 0);
1854 if (obj != TREE_OPERAND (addr, 0))
1855 arg = build_fold_addr_expr_with_type (obj,
1856 TREE_TYPE (arg));
1857 }
1858 }
1859 TREE_VEC_ELT (binds, i) = arg;
1860 }
1861 parms = TREE_CHAIN (parms);
1862 }
1863
1864 return binds;
1865 }
1866
1867 /* Variables and functions to manage constexpr call expansion context.
1868 These do not need to be marked for PCH or GC. */
1869
1870 /* FIXME remember and print actual constant arguments. */
1871 static vec<tree> call_stack;
1872 static int call_stack_tick;
1873 static int last_cx_error_tick;
1874
1875 static int
1876 push_cx_call_context (tree call)
1877 {
1878 ++call_stack_tick;
1879 if (!EXPR_HAS_LOCATION (call))
1880 SET_EXPR_LOCATION (call, input_location);
1881 call_stack.safe_push (call);
1882 int len = call_stack.length ();
1883 if (len > max_constexpr_depth)
1884 return false;
1885 return len;
1886 }
1887
1888 static void
1889 pop_cx_call_context (void)
1890 {
1891 ++call_stack_tick;
1892 call_stack.pop ();
1893 }
1894
1895 vec<tree>
1896 cx_error_context (void)
1897 {
1898 vec<tree> r = vNULL;
1899 if (call_stack_tick != last_cx_error_tick
1900 && !call_stack.is_empty ())
1901 r = call_stack;
1902 last_cx_error_tick = call_stack_tick;
1903 return r;
1904 }
1905
1906 /* E is an operand of a failed assertion, fold it either with or without
1907 constexpr context. */
1908
1909 static tree
1910 fold_operand (tree e, const constexpr_ctx *ctx)
1911 {
1912 if (ctx)
1913 {
1914 bool new_non_constant_p = false, new_overflow_p = false;
1915 e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
1916 &new_non_constant_p,
1917 &new_overflow_p);
1918 }
1919 else
1920 e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
1921 return e;
1922 }
1923
1924 /* If we have a condition in conjunctive normal form (CNF), find the first
1925 failing clause. In other words, given an expression like
1926
1927 true && true && false && true && false
1928
1929 return the first 'false'. EXPR is the expression. */
1930
1931 static tree
1932 find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
1933 {
1934 if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
1935 {
1936 /* First check the left side... */
1937 tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
1938 if (e == NULL_TREE)
1939 /* ...if we didn't find a false clause, check the right side. */
1940 e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
1941 return e;
1942 }
1943 tree e = contextual_conv_bool (expr, tf_none);
1944 e = fold_operand (e, ctx);
1945 if (integer_zerop (e))
1946 /* This is the failing clause. */
1947 return expr;
1948 return NULL_TREE;
1949 }
1950
1951 /* Wrapper for find_failing_clause_r. */
1952
1953 tree
1954 find_failing_clause (const constexpr_ctx *ctx, tree expr)
1955 {
1956 if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
1957 if (tree e = find_failing_clause_r (ctx, expr))
1958 expr = e;
1959 return expr;
1960 }
1961
1962 /* Emit additional diagnostics for failing condition BAD.
1963 Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
1964 If SHOW_EXPR_P is true, print the condition (because it was
1965 instantiation-dependent). */
1966
1967 void
1968 diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
1969 const constexpr_ctx *ctx /* = nullptr */)
1970 {
1971 /* Nobody wants to see the artificial (bool) cast. */
1972 bad = tree_strip_nop_conversions (bad);
1973 if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
1974 bad = TREE_OPERAND (bad, 0);
1975
1976 /* Actually explain the failure if this is a concept check or a
1977 requires-expression. */
1978 if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
1979 diagnose_constraints (cloc, bad, NULL_TREE);
1980 else if (COMPARISON_CLASS_P (bad)
1981 && ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0))))
1982 {
1983 tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
1984 tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
1985 tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
1986 inform (cloc, "the comparison reduces to %qE", cond);
1987 }
1988 else if (show_expr_p)
1989 inform (cloc, "%qE evaluates to false", bad);
1990 }
1991
1992 /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
1993 do it without changing the current evaluation state. If it evaluates to
1994 false, complain and return false; otherwise, return true. */
1995
1996 static bool
1997 cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
1998 location_t loc, bool evaluated,
1999 bool *non_constant_p, bool *overflow_p)
2000 {
2001 if (*non_constant_p)
2002 return true;
2003
2004 tree eval;
2005 if (!evaluated)
2006 {
2007 if (!potential_rvalue_constant_expression (arg))
2008 return true;
2009
2010 constexpr_ctx new_ctx = *ctx;
2011 new_ctx.quiet = true;
2012 bool new_non_constant_p = false, new_overflow_p = false;
2013 /* Avoid modification of existing values. */
2014 modifiable_tracker ms (new_ctx.global);
2015 eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2016 &new_non_constant_p,
2017 &new_overflow_p);
2018 }
2019 else
2020 eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2021 non_constant_p,
2022 overflow_p);
2023 if (!*non_constant_p && integer_zerop (eval))
2024 {
2025 if (!ctx->quiet)
2026 {
2027 /* See if we can find which clause was failing
2028 (for logical AND). */
2029 tree bad = find_failing_clause (ctx, arg);
2030 /* If not, or its location is unusable, fall back to the
2031 previous location. */
2032 location_t cloc = cp_expr_loc_or_loc (bad, loc);
2033
2034 /* Report the error. */
2035 auto_diagnostic_group d;
2036 error_at (cloc, msg);
2037 diagnose_failing_condition (bad, cloc, true, ctx);
2038 return bad;
2039 }
2040 *non_constant_p = true;
2041 return false;
2042 }
2043
2044 return true;
2045 }
2046
2047 /* Evaluate a call T to a GCC internal function when possible and return
2048 the evaluated result or, under the control of CTX, give an error, set
2049 NON_CONSTANT_P, and return the unevaluated call T otherwise. */
2050
2051 static tree
2052 cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
2053 value_cat lval,
2054 bool *non_constant_p, bool *overflow_p)
2055 {
2056 enum tree_code opcode = ERROR_MARK;
2057
2058 switch (CALL_EXPR_IFN (t))
2059 {
2060 case IFN_UBSAN_NULL:
2061 case IFN_UBSAN_BOUNDS:
2062 case IFN_UBSAN_VPTR:
2063 case IFN_FALLTHROUGH:
2064 return void_node;
2065
2066 case IFN_ASSUME:
2067 if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
2068 G_("failed %<assume%> attribute assumption"),
2069 EXPR_LOCATION (t), /*eval*/false,
2070 non_constant_p, overflow_p))
2071 return t;
2072 return void_node;
2073
2074 case IFN_ADD_OVERFLOW:
2075 opcode = PLUS_EXPR;
2076 break;
2077 case IFN_SUB_OVERFLOW:
2078 opcode = MINUS_EXPR;
2079 break;
2080 case IFN_MUL_OVERFLOW:
2081 opcode = MULT_EXPR;
2082 break;
2083
2084 case IFN_LAUNDER:
2085 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2086 vc_prvalue, non_constant_p,
2087 overflow_p);
2088
2089 case IFN_VEC_CONVERT:
2090 {
2091 tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
2092 vc_prvalue, non_constant_p,
2093 overflow_p);
2094 if (TREE_CODE (arg) == VECTOR_CST)
2095 if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
2096 return r;
2097 }
2098 /* FALLTHRU */
2099
2100 default:
2101 if (!ctx->quiet)
2102 error_at (cp_expr_loc_or_input_loc (t),
2103 "call to internal function %qE", t);
2104 *non_constant_p = true;
2105 return t;
2106 }
2107
2108 /* Evaluate constant arguments using OPCODE and return a complex
2109 number containing the result and the overflow bit. */
2110 tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
2111 non_constant_p, overflow_p);
2112 tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
2113 non_constant_p, overflow_p);
2114
2115 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2116 {
2117 location_t loc = cp_expr_loc_or_input_loc (t);
2118 tree type = TREE_TYPE (TREE_TYPE (t));
2119 tree result = fold_binary_loc (loc, opcode, type,
2120 fold_convert_loc (loc, type, arg0),
2121 fold_convert_loc (loc, type, arg1));
2122 tree ovf
2123 = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
2124 /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
2125 if (TREE_OVERFLOW (result))
2126 TREE_OVERFLOW (result) = 0;
2127
2128 return build_complex (TREE_TYPE (t), result, ovf);
2129 }
2130
2131 *non_constant_p = true;
2132 return t;
2133 }
2134
2135 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
2136
2137 static void
2138 clear_no_implicit_zero (tree ctor)
2139 {
2140 if (CONSTRUCTOR_NO_CLEARING (ctor))
2141 {
2142 CONSTRUCTOR_NO_CLEARING (ctor) = false;
2143 for (auto &e: CONSTRUCTOR_ELTS (ctor))
2144 if (TREE_CODE (e.value) == CONSTRUCTOR)
2145 clear_no_implicit_zero (e.value);
2146 }
2147 }
2148
2149 /* Complain about a const object OBJ being modified in a constant expression.
2150 EXPR is the MODIFY_EXPR expression performing the modification. */
2151
2152 static void
2153 modifying_const_object_error (tree expr, tree obj)
2154 {
2155 location_t loc = cp_expr_loc_or_input_loc (expr);
2156 auto_diagnostic_group d;
2157 error_at (loc, "modifying a const object %qE is not allowed in "
2158 "a constant expression", TREE_OPERAND (expr, 0));
2159 inform (location_of (obj), "originally declared %<const%> here");
2160 }
2161
2162 /* Return true if FNDECL is a replaceable global allocation function that
2163 should be useable during constant expression evaluation. */
2164
2165 static inline bool
2166 cxx_replaceable_global_alloc_fn (tree fndecl)
2167 {
2168 return (cxx_dialect >= cxx20
2169 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
2170 && CP_DECL_CONTEXT (fndecl) == global_namespace
2171 && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2172 || DECL_IS_OPERATOR_DELETE_P (fndecl)));
2173 }
2174
2175 /* Return true if FNDECL is a placement new function that should be
2176 useable during constant expression evaluation of std::construct_at. */
2177
2178 static inline bool
2179 cxx_placement_new_fn (tree fndecl)
2180 {
2181 if (cxx_dialect >= cxx20
2182 && IDENTIFIER_NEW_OP_P (DECL_NAME (fndecl))
2183 && CP_DECL_CONTEXT (fndecl) == global_namespace
2184 && !DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
2185 && TREE_CODE (TREE_TYPE (fndecl)) == FUNCTION_TYPE)
2186 {
2187 tree first_arg = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
2188 if (TREE_VALUE (first_arg) == ptr_type_node
2189 && TREE_CHAIN (first_arg) == void_list_node)
2190 return true;
2191 }
2192 return false;
2193 }
2194
2195 /* Return true if FNDECL is std::construct_at. */
2196
2197 static inline bool
2198 is_std_construct_at (tree fndecl)
2199 {
2200 if (!decl_in_std_namespace_p (fndecl))
2201 return false;
2202
2203 tree name = DECL_NAME (fndecl);
2204 return name && id_equal (name, "construct_at");
2205 }
2206
2207 /* Overload for the above taking constexpr_call*. */
2208
2209 static inline bool
2210 is_std_construct_at (const constexpr_call *call)
2211 {
2212 return (call
2213 && call->fundef
2214 && is_std_construct_at (call->fundef->decl));
2215 }
2216
2217 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
2218
2219 static inline bool
2220 is_std_allocator_allocate (tree fndecl)
2221 {
2222 tree name = DECL_NAME (fndecl);
2223 if (name == NULL_TREE
2224 || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
2225 return false;
2226
2227 tree ctx = DECL_CONTEXT (fndecl);
2228 if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
2229 return false;
2230
2231 tree decl = TYPE_MAIN_DECL (ctx);
2232 name = DECL_NAME (decl);
2233 if (name == NULL_TREE || !id_equal (name, "allocator"))
2234 return false;
2235
2236 return decl_in_std_namespace_p (decl);
2237 }
2238
2239 /* Overload for the above taking constexpr_call*. */
2240
2241 static inline bool
2242 is_std_allocator_allocate (const constexpr_call *call)
2243 {
2244 return (call
2245 && call->fundef
2246 && is_std_allocator_allocate (call->fundef->decl));
2247 }
2248
2249 /* Return true if FNDECL is __dynamic_cast. */
2250
2251 static inline bool
2252 cxx_dynamic_cast_fn_p (tree fndecl)
2253 {
2254 return (cxx_dialect >= cxx20
2255 && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
2256 && CP_DECL_CONTEXT (fndecl) == abi_node);
2257 }
2258
2259 /* Often, we have an expression in the form of address + offset, e.g.
2260 "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
2261
2262 static tree
2263 extract_obj_from_addr_offset (tree expr)
2264 {
2265 if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
2266 expr = TREE_OPERAND (expr, 0);
2267 STRIP_NOPS (expr);
2268 if (TREE_CODE (expr) == ADDR_EXPR)
2269 expr = TREE_OPERAND (expr, 0);
2270 return expr;
2271 }
2272
2273 /* Given a PATH like
2274
2275 g.D.2181.D.2154.D.2102.D.2093
2276
2277 find a component with type TYPE. Return NULL_TREE if not found, and
2278 error_mark_node if the component is not accessible. If STOP is non-null,
2279 this function will return NULL_TREE if STOP is found before TYPE. */
2280
2281 static tree
2282 get_component_with_type (tree path, tree type, tree stop)
2283 {
2284 while (true)
2285 {
2286 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
2287 /* Found it. */
2288 return path;
2289 else if (stop
2290 && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
2291 stop)))
2292 return NULL_TREE;
2293 else if (TREE_CODE (path) == COMPONENT_REF
2294 && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
2295 {
2296 /* We need to check that the component we're accessing is in fact
2297 accessible. */
2298 if (TREE_PRIVATE (TREE_OPERAND (path, 1))
2299 || TREE_PROTECTED (TREE_OPERAND (path, 1)))
2300 return error_mark_node;
2301 path = TREE_OPERAND (path, 0);
2302 }
2303 else
2304 return NULL_TREE;
2305 }
2306 }
2307
2308 /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
2309
2310 The declaration of __dynamic_cast is:
2311
2312 void* __dynamic_cast (const void* __src_ptr,
2313 const __class_type_info* __src_type,
2314 const __class_type_info* __dst_type,
2315 ptrdiff_t __src2dst);
2316
2317 where src2dst has the following possible values
2318
2319 >-1: src_type is a unique public non-virtual base of dst_type
2320 dst_ptr + src2dst == src_ptr
2321 -1: unspecified relationship
2322 -2: src_type is not a public base of dst_type
2323 -3: src_type is a multiple public non-virtual base of dst_type
2324
2325 Since literal types can't have virtual bases, we only expect hint >=0,
2326 -2, or -3. */
2327
2328 static tree
2329 cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
2330 bool *non_constant_p, bool *overflow_p)
2331 {
2332 /* T will be something like
2333 __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
2334 dismantle it. */
2335 gcc_assert (call_expr_nargs (call) == 4);
2336 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2337 tree obj = CALL_EXPR_ARG (call, 0);
2338 tree type = CALL_EXPR_ARG (call, 2);
2339 HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
2340 location_t loc = cp_expr_loc_or_input_loc (call);
2341
2342 /* Get the target type of the dynamic_cast. */
2343 gcc_assert (TREE_CODE (type) == ADDR_EXPR);
2344 type = TREE_OPERAND (type, 0);
2345 type = TREE_TYPE (DECL_NAME (type));
2346
2347 /* TYPE can only be either T* or T&. We can't know which of these it
2348 is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
2349 and something like "(T*)(T&)(T*) x" in the second case. */
2350 bool reference_p = false;
2351 while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
2352 {
2353 reference_p |= TYPE_REF_P (TREE_TYPE (obj));
2354 obj = TREE_OPERAND (obj, 0);
2355 }
2356
2357 /* Evaluate the object so that we know its dynamic type. */
2358 obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
2359 overflow_p);
2360 if (*non_constant_p)
2361 return call;
2362
2363 /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
2364 but when HINT is > 0, it can also be something like
2365 &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
2366 obj = extract_obj_from_addr_offset (obj);
2367 const tree objtype = TREE_TYPE (obj);
2368 /* If OBJ doesn't refer to a base field, we're done. */
2369 if (tree t = (TREE_CODE (obj) == COMPONENT_REF
2370 ? TREE_OPERAND (obj, 1) : obj))
2371 if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
2372 {
2373 if (reference_p)
2374 {
2375 if (!ctx->quiet)
2376 {
2377 error_at (loc, "reference %<dynamic_cast%> failed");
2378 inform (loc, "dynamic type %qT of its operand does "
2379 "not have a base class of type %qT",
2380 objtype, type);
2381 }
2382 *non_constant_p = true;
2383 }
2384 return integer_zero_node;
2385 }
2386
2387 /* [class.cdtor] When a dynamic_cast is used in a constructor ...
2388 or in a destructor ... if the operand of the dynamic_cast refers
2389 to the object under construction or destruction, this object is
2390 considered to be a most derived object that has the type of the
2391 constructor or destructor's class. */
2392 tree vtable = build_vfield_ref (obj, objtype);
2393 vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
2394 non_constant_p, overflow_p);
2395 if (*non_constant_p)
2396 return call;
2397 /* With -fsanitize=vptr, we initialize all vtable pointers to null,
2398 so it's possible that we got a null pointer now. */
2399 if (integer_zerop (vtable))
2400 {
2401 if (!ctx->quiet)
2402 error_at (loc, "virtual table pointer is used uninitialized");
2403 *non_constant_p = true;
2404 return integer_zero_node;
2405 }
2406 /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
2407 vtable = extract_obj_from_addr_offset (vtable);
2408 const tree mdtype = DECL_CONTEXT (vtable);
2409
2410 /* Given dynamic_cast<T>(v),
2411
2412 [expr.dynamic.cast] If C is the class type to which T points or refers,
2413 the runtime check logically executes as follows:
2414
2415 If, in the most derived object pointed (referred) to by v, v points
2416 (refers) to a public base class subobject of a C object, and if only
2417 one object of type C is derived from the subobject pointed (referred)
2418 to by v the result points (refers) to that C object.
2419
2420 In this case, HINT >= 0 or -3. */
2421 if (hint >= 0 || hint == -3)
2422 {
2423 /* Look for a component with type TYPE. */
2424 tree t = get_component_with_type (obj, type, mdtype);
2425 /* If not accessible, give an error. */
2426 if (t == error_mark_node)
2427 {
2428 if (reference_p)
2429 {
2430 if (!ctx->quiet)
2431 {
2432 error_at (loc, "reference %<dynamic_cast%> failed");
2433 inform (loc, "static type %qT of its operand is a "
2434 "non-public base class of dynamic type %qT",
2435 objtype, type);
2436
2437 }
2438 *non_constant_p = true;
2439 }
2440 return integer_zero_node;
2441 }
2442 else if (t)
2443 /* The result points to the TYPE object. */
2444 return cp_build_addr_expr (t, complain);
2445 /* Else, TYPE was not found, because the HINT turned out to be wrong.
2446 Fall through to the normal processing. */
2447 }
2448
2449 /* Otherwise, if v points (refers) to a public base class subobject of the
2450 most derived object, and the type of the most derived object has a base
2451 class, of type C, that is unambiguous and public, the result points
2452 (refers) to the C subobject of the most derived object.
2453
2454 But it can also be an invalid case. */
2455
2456 /* Get the most derived object. */
2457 obj = get_component_with_type (obj, mdtype, NULL_TREE);
2458 if (obj == error_mark_node)
2459 {
2460 if (reference_p)
2461 {
2462 if (!ctx->quiet)
2463 {
2464 error_at (loc, "reference %<dynamic_cast%> failed");
2465 inform (loc, "static type %qT of its operand is a non-public"
2466 " base class of dynamic type %qT", objtype, mdtype);
2467 }
2468 *non_constant_p = true;
2469 }
2470 return integer_zero_node;
2471 }
2472 else
2473 gcc_assert (obj);
2474
2475 /* Check that the type of the most derived object has a base class
2476 of type TYPE that is unambiguous and public. */
2477 base_kind b_kind;
2478 tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
2479 if (!binfo || binfo == error_mark_node)
2480 {
2481 if (reference_p)
2482 {
2483 if (!ctx->quiet)
2484 {
2485 error_at (loc, "reference %<dynamic_cast%> failed");
2486 if (b_kind == bk_ambig)
2487 inform (loc, "%qT is an ambiguous base class of dynamic "
2488 "type %qT of its operand", type, mdtype);
2489 else
2490 inform (loc, "dynamic type %qT of its operand does not "
2491 "have an unambiguous public base class %qT",
2492 mdtype, type);
2493 }
2494 *non_constant_p = true;
2495 }
2496 return integer_zero_node;
2497 }
2498 /* If so, return the TYPE subobject of the most derived object. */
2499 obj = convert_to_base_statically (obj, binfo);
2500 return cp_build_addr_expr (obj, complain);
2501 }
2502
2503 /* Data structure used by replace_decl and replace_decl_r. */
2504
2505 struct replace_decl_data
2506 {
2507 /* The _DECL we want to replace. */
2508 tree decl;
2509 /* The replacement for DECL. */
2510 tree replacement;
2511 /* Trees we've visited. */
2512 hash_set<tree> *pset;
2513 /* Whether we've performed any replacements. */
2514 bool changed;
2515 };
2516
2517 /* Helper function for replace_decl, called through cp_walk_tree. */
2518
2519 static tree
2520 replace_decl_r (tree *tp, int *walk_subtrees, void *data)
2521 {
2522 replace_decl_data *d = (replace_decl_data *) data;
2523
2524 if (*tp == d->decl)
2525 {
2526 *tp = unshare_expr (d->replacement);
2527 d->changed = true;
2528 *walk_subtrees = 0;
2529 }
2530 else if (TYPE_P (*tp)
2531 || d->pset->add (*tp))
2532 *walk_subtrees = 0;
2533
2534 return NULL_TREE;
2535 }
2536
2537 /* Replace every occurrence of DECL with (an unshared copy of)
2538 REPLACEMENT within the expression *TP. Returns true iff a
2539 replacement was performed. */
2540
2541 bool
2542 replace_decl (tree *tp, tree decl, tree replacement)
2543 {
2544 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
2545 (TREE_TYPE (decl), TREE_TYPE (replacement)));
2546 hash_set<tree> pset;
2547 replace_decl_data data = { decl, replacement, &pset, false };
2548 cp_walk_tree (tp, replace_decl_r, &data, NULL);
2549 return data.changed;
2550 }
2551
2552 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2553
2554 static tree
2555 cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
2556 value_cat lval,
2557 bool *non_constant_p, bool *overflow_p)
2558 {
2559 tree function = THUNK_TARGET (thunk_fndecl);
2560
2561 if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
2562 {
2563 if (!ctx->quiet)
2564 {
2565 if (!DECL_DECLARED_CONSTEXPR_P (function))
2566 {
2567 error ("call to non-%<constexpr%> function %qD", function);
2568 explain_invalid_constexpr_fn (function);
2569 }
2570 else
2571 /* virtual_offset is only set for virtual bases, which make the
2572 class non-literal, so we don't need to handle it here. */
2573 error ("calling constexpr member function %qD through virtual "
2574 "base subobject", function);
2575 }
2576 *non_constant_p = true;
2577 return t;
2578 }
2579
2580 tree new_call = copy_node (t);
2581 CALL_EXPR_FN (new_call) = function;
2582 TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
2583
2584 tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
2585
2586 if (DECL_THIS_THUNK_P (thunk_fndecl))
2587 {
2588 /* 'this'-adjusting thunk. */
2589 tree this_arg = CALL_EXPR_ARG (t, 0);
2590 this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
2591 this_arg, offset);
2592 CALL_EXPR_ARG (new_call, 0) = this_arg;
2593 }
2594 else
2595 /* Return-adjusting thunk. */
2596 new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
2597 new_call, offset);
2598
2599 return cxx_eval_constant_expression (ctx, new_call, lval,
2600 non_constant_p, overflow_p);
2601 }
2602
2603 /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
2604 its TREE_READONLY flag according to READONLY_P. Used for constexpr
2605 'tors to detect modifying const objects in a constexpr context. */
2606
2607 static void
2608 cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
2609 bool readonly_p, bool *non_constant_p,
2610 bool *overflow_p)
2611 {
2612 if (CLASS_TYPE_P (TREE_TYPE (object))
2613 && CP_TYPE_CONST_P (TREE_TYPE (object)))
2614 {
2615 /* Subobjects might not be stored in ctx->global->values but we
2616 can get its CONSTRUCTOR by evaluating *this. */
2617 tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
2618 non_constant_p, overflow_p);
2619 if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2620 TREE_READONLY (e) = readonly_p;
2621 }
2622 }
2623
2624 /* Subroutine of cxx_eval_constant_expression.
2625 Evaluate the call expression tree T in the context of OLD_CALL expression
2626 evaluation. */
2627
2628 static tree
2629 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
2630 value_cat lval,
2631 bool *non_constant_p, bool *overflow_p)
2632 {
2633 /* Handle concept checks separately. */
2634 if (concept_check_p (t))
2635 return evaluate_concept_check (t);
2636
2637 location_t loc = cp_expr_loc_or_input_loc (t);
2638 tree fun = get_function_named_in_call (t);
2639 constexpr_call new_call
2640 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
2641 int depth_ok;
2642
2643 if (fun == NULL_TREE)
2644 return cxx_eval_internal_function (ctx, t, lval,
2645 non_constant_p, overflow_p);
2646
2647 if (TREE_CODE (fun) != FUNCTION_DECL)
2648 {
2649 /* Might be a constexpr function pointer. */
2650 fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
2651 non_constant_p, overflow_p);
2652 STRIP_NOPS (fun);
2653 if (TREE_CODE (fun) == ADDR_EXPR)
2654 fun = TREE_OPERAND (fun, 0);
2655 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2656 indirection, the called expression is a pointer into the
2657 virtual table which should contain FDESC_EXPR. Extract the
2658 FUNCTION_DECL from there. */
2659 else if (TARGET_VTABLE_USES_DESCRIPTORS
2660 && TREE_CODE (fun) == POINTER_PLUS_EXPR
2661 && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2662 && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2663 {
2664 tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2665 if (VAR_P (d)
2666 && DECL_VTABLE_OR_VTT_P (d)
2667 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2668 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2669 && DECL_INITIAL (d)
2670 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2671 {
2672 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2673 TYPE_SIZE_UNIT (vtable_entry_type));
2674 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2675 if (idx >= 0)
2676 {
2677 tree fdesc
2678 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2679 if (TREE_CODE (fdesc) == FDESC_EXPR
2680 && integer_zerop (TREE_OPERAND (fdesc, 1)))
2681 fun = TREE_OPERAND (fdesc, 0);
2682 }
2683 }
2684 }
2685 }
2686 if (TREE_CODE (fun) != FUNCTION_DECL)
2687 {
2688 if (!ctx->quiet && !*non_constant_p)
2689 error_at (loc, "expression %qE does not designate a %<constexpr%> "
2690 "function", fun);
2691 *non_constant_p = true;
2692 return t;
2693 }
2694 if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
2695 fun = DECL_CLONED_FUNCTION (fun);
2696
2697 if (is_ubsan_builtin_p (fun))
2698 return void_node;
2699
2700 if (fndecl_built_in_p (fun))
2701 return cxx_eval_builtin_function_call (ctx, t, fun,
2702 lval, non_constant_p, overflow_p);
2703 if (DECL_THUNK_P (fun))
2704 return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
2705 if (!maybe_constexpr_fn (fun))
2706 {
2707 if (TREE_CODE (t) == CALL_EXPR
2708 && cxx_replaceable_global_alloc_fn (fun)
2709 && (CALL_FROM_NEW_OR_DELETE_P (t)
2710 || is_std_allocator_allocate (ctx->call)))
2711 {
2712 const int nargs = call_expr_nargs (t);
2713 tree arg0 = NULL_TREE;
2714 for (int i = 0; i < nargs; ++i)
2715 {
2716 tree arg = CALL_EXPR_ARG (t, i);
2717 arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2718 non_constant_p, overflow_p);
2719 VERIFY_CONSTANT (arg);
2720 if (i == 0)
2721 arg0 = arg;
2722 }
2723 gcc_assert (arg0);
2724 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
2725 {
2726 tree type = build_array_type_nelts (char_type_node,
2727 tree_to_uhwi (arg0));
2728 tree var = build_decl (loc, VAR_DECL,
2729 (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2730 & OVL_OP_FLAG_VEC)
2731 ? heap_vec_uninit_identifier
2732 : heap_uninit_identifier,
2733 type);
2734 DECL_ARTIFICIAL (var) = 1;
2735 TREE_STATIC (var) = 1;
2736 // Temporarily register the artificial var in varpool,
2737 // so that comparisons of its address against NULL are folded
2738 // through nonzero_address even with
2739 // -fno-delete-null-pointer-checks or that comparison of
2740 // addresses of different heap artificial vars is folded too.
2741 // See PR98988 and PR99031.
2742 varpool_node::finalize_decl (var);
2743 ctx->global->heap_vars.safe_push (var);
2744 ctx->global->put_value (var, NULL_TREE);
2745 return fold_convert (ptr_type_node, build_address (var));
2746 }
2747 else
2748 {
2749 STRIP_NOPS (arg0);
2750 if (TREE_CODE (arg0) == ADDR_EXPR
2751 && VAR_P (TREE_OPERAND (arg0, 0)))
2752 {
2753 tree var = TREE_OPERAND (arg0, 0);
2754 if (DECL_NAME (var) == heap_uninit_identifier
2755 || DECL_NAME (var) == heap_identifier)
2756 {
2757 if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2758 & OVL_OP_FLAG_VEC)
2759 {
2760 if (!ctx->quiet)
2761 {
2762 error_at (loc, "array deallocation of object "
2763 "allocated with non-array "
2764 "allocation");
2765 inform (DECL_SOURCE_LOCATION (var),
2766 "allocation performed here");
2767 }
2768 *non_constant_p = true;
2769 return t;
2770 }
2771 DECL_NAME (var) = heap_deleted_identifier;
2772 ctx->global->remove_value (var);
2773 ctx->global->heap_dealloc_count++;
2774 return void_node;
2775 }
2776 else if (DECL_NAME (var) == heap_vec_uninit_identifier
2777 || DECL_NAME (var) == heap_vec_identifier)
2778 {
2779 if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
2780 & OVL_OP_FLAG_VEC) == 0)
2781 {
2782 if (!ctx->quiet)
2783 {
2784 error_at (loc, "non-array deallocation of "
2785 "object allocated with array "
2786 "allocation");
2787 inform (DECL_SOURCE_LOCATION (var),
2788 "allocation performed here");
2789 }
2790 *non_constant_p = true;
2791 return t;
2792 }
2793 DECL_NAME (var) = heap_deleted_identifier;
2794 ctx->global->remove_value (var);
2795 ctx->global->heap_dealloc_count++;
2796 return void_node;
2797 }
2798 else if (DECL_NAME (var) == heap_deleted_identifier)
2799 {
2800 if (!ctx->quiet)
2801 error_at (loc, "deallocation of already deallocated "
2802 "storage");
2803 *non_constant_p = true;
2804 return t;
2805 }
2806 }
2807 if (!ctx->quiet)
2808 error_at (loc, "deallocation of storage that was "
2809 "not previously allocated");
2810 *non_constant_p = true;
2811 return t;
2812 }
2813 }
2814 /* Allow placement new in std::construct_at, just return the second
2815 argument. */
2816 if (TREE_CODE (t) == CALL_EXPR
2817 && cxx_placement_new_fn (fun)
2818 && is_std_construct_at (ctx->call))
2819 {
2820 const int nargs = call_expr_nargs (t);
2821 tree arg1 = NULL_TREE;
2822 for (int i = 0; i < nargs; ++i)
2823 {
2824 tree arg = CALL_EXPR_ARG (t, i);
2825 arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2826 non_constant_p, overflow_p);
2827 if (i == 1)
2828 arg1 = arg;
2829 else
2830 VERIFY_CONSTANT (arg);
2831 }
2832 gcc_assert (arg1);
2833 return arg1;
2834 }
2835 else if (cxx_dynamic_cast_fn_p (fun))
2836 return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
2837
2838 if (!ctx->quiet)
2839 {
2840 if (!lambda_static_thunk_p (fun))
2841 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2842 explain_invalid_constexpr_fn (fun);
2843 }
2844 *non_constant_p = true;
2845 return t;
2846 }
2847
2848 constexpr_ctx new_ctx = *ctx;
2849 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
2850 && TREE_CODE (t) == AGGR_INIT_EXPR)
2851 {
2852 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2853 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2854 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
2855 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
2856 CONSTRUCTOR_NO_CLEARING (ctor) = true;
2857 ctx->global->put_value (new_ctx.object, ctor);
2858 ctx = &new_ctx;
2859 }
2860
2861 /* Shortcut trivial constructor/op=. */
2862 if (trivial_fn_p (fun))
2863 {
2864 tree init = NULL_TREE;
2865 if (call_expr_nargs (t) == 2)
2866 init = convert_from_reference (get_nth_callarg (t, 1));
2867 else if (TREE_CODE (t) == AGGR_INIT_EXPR
2868 && AGGR_INIT_ZERO_FIRST (t))
2869 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
2870 if (init)
2871 {
2872 tree op = get_nth_callarg (t, 0);
2873 if (is_dummy_object (op))
2874 op = ctx->object;
2875 else
2876 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
2877 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
2878 new_ctx.call = &new_call;
2879 return cxx_eval_constant_expression (&new_ctx, set, lval,
2880 non_constant_p, overflow_p);
2881 }
2882 }
2883
2884 bool non_constant_args = false;
2885 new_call.bindings
2886 = cxx_bind_parameters_in_call (ctx, t, fun, non_constant_p,
2887 overflow_p, &non_constant_args);
2888
2889 /* We build up the bindings list before we know whether we already have this
2890 call cached. If we don't end up saving these bindings, ggc_free them when
2891 this function exits. */
2892 class free_bindings
2893 {
2894 tree *bindings;
2895 public:
2896 free_bindings (tree &b): bindings (&b) { }
2897 ~free_bindings () { if (bindings) ggc_free (*bindings); }
2898 void preserve () { bindings = NULL; }
2899 } fb (new_call.bindings);
2900
2901 if (*non_constant_p)
2902 return t;
2903
2904 /* We can't defer instantiating the function any longer. */
2905 if (!DECL_INITIAL (fun)
2906 && DECL_TEMPLOID_INSTANTIATION (fun)
2907 && !uid_sensitive_constexpr_evaluation_p ())
2908 {
2909 location_t save_loc = input_location;
2910 input_location = loc;
2911 ++function_depth;
2912 if (ctx->manifestly_const_eval)
2913 FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
2914 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
2915 --function_depth;
2916 input_location = save_loc;
2917 }
2918
2919 /* If in direct recursive call, optimize definition search. */
2920 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
2921 new_call.fundef = ctx->call->fundef;
2922 else
2923 {
2924 new_call.fundef = retrieve_constexpr_fundef (fun);
2925 if (new_call.fundef == NULL || new_call.fundef->body == NULL
2926 || new_call.fundef->result == error_mark_node
2927 || fun == current_function_decl)
2928 {
2929 if (!ctx->quiet)
2930 {
2931 /* We need to check for current_function_decl here in case we're
2932 being called during cp_fold_function, because at that point
2933 DECL_INITIAL is set properly and we have a fundef but we
2934 haven't lowered invisirefs yet (c++/70344). */
2935 if (DECL_INITIAL (fun) == error_mark_node
2936 || fun == current_function_decl)
2937 error_at (loc, "%qD called in a constant expression before its "
2938 "definition is complete", fun);
2939 else if (DECL_INITIAL (fun))
2940 {
2941 /* The definition of fun was somehow unsuitable. But pretend
2942 that lambda static thunks don't exist. */
2943 if (!lambda_static_thunk_p (fun))
2944 error_at (loc, "%qD called in a constant expression", fun);
2945 explain_invalid_constexpr_fn (fun);
2946 }
2947 else
2948 error_at (loc, "%qD used before its definition", fun);
2949 }
2950 *non_constant_p = true;
2951 return t;
2952 }
2953 }
2954
2955 depth_ok = push_cx_call_context (t);
2956
2957 /* Remember the object we are constructing or destructing. */
2958 tree new_obj = NULL_TREE;
2959 if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
2960 {
2961 /* In a cdtor, it should be the first `this' argument.
2962 At this point it has already been evaluated in the call
2963 to cxx_bind_parameters_in_call. */
2964 new_obj = TREE_VEC_ELT (new_call.bindings, 0);
2965 new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj);
2966
2967 if (ctx->call && ctx->call->fundef
2968 && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
2969 {
2970 tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
2971 STRIP_NOPS (cur_obj);
2972 if (TREE_CODE (cur_obj) == ADDR_EXPR)
2973 cur_obj = TREE_OPERAND (cur_obj, 0);
2974 if (new_obj == cur_obj)
2975 /* We're calling the target constructor of a delegating
2976 constructor, or accessing a base subobject through a
2977 NOP_EXPR as part of a call to a base constructor, so
2978 there is no new (sub)object. */
2979 new_obj = NULL_TREE;
2980 }
2981 }
2982
2983 tree result = NULL_TREE;
2984
2985 constexpr_call *entry = NULL;
2986 if (depth_ok && !non_constant_args && ctx->strict)
2987 {
2988 new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
2989 new_call.hash
2990 = iterative_hash_template_arg (new_call.bindings, new_call.hash);
2991 new_call.hash
2992 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
2993
2994 /* If we have seen this call before, we are done. */
2995 maybe_initialize_constexpr_call_table ();
2996 constexpr_call **slot
2997 = constexpr_call_table->find_slot (&new_call, INSERT);
2998 entry = *slot;
2999 if (entry == NULL)
3000 {
3001 /* Only cache up to constexpr_cache_depth to limit memory use. */
3002 if (depth_ok < constexpr_cache_depth)
3003 {
3004 /* We need to keep a pointer to the entry, not just the slot, as
3005 the slot can move during evaluation of the body. */
3006 *slot = entry = ggc_alloc<constexpr_call> ();
3007 *entry = new_call;
3008 fb.preserve ();
3009 }
3010 }
3011 /* Calls that are in progress have their result set to NULL, so that we
3012 can detect circular dependencies. Now that we only cache up to
3013 constexpr_cache_depth this won't catch circular dependencies that
3014 start deeper, but they'll hit the recursion or ops limit. */
3015 else if (entry->result == NULL)
3016 {
3017 if (!ctx->quiet)
3018 error ("call has circular dependency");
3019 *non_constant_p = true;
3020 entry->result = result = error_mark_node;
3021 }
3022 else
3023 result = entry->result;
3024 }
3025
3026 if (!depth_ok)
3027 {
3028 if (!ctx->quiet)
3029 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
3030 "%<-fconstexpr-depth=%> to increase the maximum)",
3031 max_constexpr_depth);
3032 *non_constant_p = true;
3033 result = error_mark_node;
3034 }
3035 else
3036 {
3037 bool cacheable = true;
3038 if (result && result != error_mark_node)
3039 /* OK */;
3040 else if (!DECL_SAVED_TREE (fun))
3041 {
3042 /* When at_eof >= 2, cgraph has started throwing away
3043 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
3044 late code generation for VEC_INIT_EXPR, which needs to be
3045 completely reconsidered. */
3046 gcc_assert (at_eof >= 2 && ctx->quiet);
3047 *non_constant_p = true;
3048 }
3049 else if (tree copy = get_fundef_copy (new_call.fundef))
3050 {
3051 tree body, parms, res;
3052 releasing_vec ctors;
3053
3054 /* Reuse or create a new unshared copy of this function's body. */
3055 body = TREE_PURPOSE (copy);
3056 parms = TREE_VALUE (copy);
3057 res = TREE_TYPE (copy);
3058
3059 /* Associate the bindings with the remapped parms. */
3060 tree bound = new_call.bindings;
3061 tree remapped = parms;
3062 for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
3063 {
3064 tree arg = TREE_VEC_ELT (bound, i);
3065 if (entry)
3066 {
3067 /* Unshare args going into the hash table to separate them
3068 from the caller's context, for better GC and to avoid
3069 problems with verify_gimple. */
3070 arg = unshare_expr_without_location (arg);
3071 TREE_VEC_ELT (bound, i) = arg;
3072
3073 /* And then unshare again so the callee doesn't change the
3074 argument values in the hash table. XXX Could we unshare
3075 lazily in cxx_eval_store_expression? */
3076 arg = unshare_constructor (arg);
3077 if (TREE_CODE (arg) == CONSTRUCTOR)
3078 vec_safe_push (ctors, arg);
3079 }
3080 ctx->global->put_value (remapped, arg);
3081 remapped = DECL_CHAIN (remapped);
3082 }
3083 /* Add the RESULT_DECL to the values map, too. */
3084 gcc_assert (!DECL_BY_REFERENCE (res));
3085 ctx->global->put_value (res, NULL_TREE);
3086
3087 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
3088 we can forget their values after the call. */
3089 constexpr_ctx ctx_with_save_exprs = *ctx;
3090 auto_vec<tree, 10> save_exprs;
3091 ctx_with_save_exprs.save_exprs = &save_exprs;
3092 ctx_with_save_exprs.call = &new_call;
3093 unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
3094 unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
3095
3096 /* If this is a constexpr destructor, the object's const and volatile
3097 semantics are no longer in effect; see [class.dtor]p5. */
3098 if (new_obj && DECL_DESTRUCTOR_P (fun))
3099 cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
3100 non_constant_p, overflow_p);
3101
3102 tree jump_target = NULL_TREE;
3103 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
3104 vc_discard, non_constant_p, overflow_p,
3105 &jump_target);
3106
3107 if (DECL_CONSTRUCTOR_P (fun))
3108 {
3109 /* This can be null for a subobject constructor call, in
3110 which case what we care about is the initialization
3111 side-effects rather than the value. We could get at the
3112 value by evaluating *this, but we don't bother; there's
3113 no need to put such a call in the hash table. */
3114 result = lval ? ctx->object : ctx->ctor;
3115
3116 /* If we've just evaluated a subobject constructor call for an
3117 empty union member, it might not have produced a side effect
3118 that actually activated the union member. So produce such a
3119 side effect now to ensure the union appears initialized. */
3120 if (!result && new_obj
3121 && TREE_CODE (new_obj) == COMPONENT_REF
3122 && TREE_CODE (TREE_TYPE
3123 (TREE_OPERAND (new_obj, 0))) == UNION_TYPE
3124 && is_really_empty_class (TREE_TYPE (new_obj),
3125 /*ignore_vptr*/false))
3126 {
3127 tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj),
3128 new_obj,
3129 build_constructor (TREE_TYPE (new_obj),
3130 NULL));
3131 cxx_eval_constant_expression (ctx, activate, lval,
3132 non_constant_p, overflow_p);
3133 ggc_free (activate);
3134 }
3135 }
3136 else if (VOID_TYPE_P (TREE_TYPE (res)))
3137 result = void_node;
3138 else
3139 {
3140 result = ctx->global->get_value (res);
3141 if (result == NULL_TREE && !*non_constant_p
3142 && !DECL_DESTRUCTOR_P (fun))
3143 {
3144 if (!ctx->quiet)
3145 error ("%<constexpr%> call flows off the end "
3146 "of the function");
3147 *non_constant_p = true;
3148 }
3149 }
3150
3151 /* At this point, the object's constructor will have run, so
3152 the object is no longer under construction, and its possible
3153 'const' semantics now apply. Make a note of this fact by
3154 marking the CONSTRUCTOR TREE_READONLY. */
3155 if (new_obj && DECL_CONSTRUCTOR_P (fun))
3156 cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
3157 non_constant_p, overflow_p);
3158
3159 /* Forget the saved values of the callee's SAVE_EXPRs and
3160 TARGET_EXPRs. */
3161 for (tree save_expr : save_exprs)
3162 ctx->global->remove_value (save_expr);
3163
3164 /* Remove the parms/result from the values map. Is it worth
3165 bothering to do this when the map itself is only live for
3166 one constexpr evaluation? If so, maybe also clear out
3167 other vars from call, maybe in BIND_EXPR handling? */
3168 ctx->global->remove_value (res);
3169 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
3170 ctx->global->remove_value (parm);
3171
3172 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
3173 while (!ctors->is_empty ())
3174 {
3175 tree c = ctors->pop ();
3176 if (c != result)
3177 free_constructor (c);
3178 }
3179
3180 /* Make the unshared function copy we used available for re-use. */
3181 save_fundef_copy (fun, copy);
3182
3183 /* If the call allocated some heap object that hasn't been
3184 deallocated during the call, or if it deallocated some heap
3185 object it has not allocated, the call isn't really stateless
3186 for the constexpr evaluation and should not be cached.
3187 It is fine if the call allocates something and deallocates it
3188 too. */
3189 if (entry
3190 && (save_heap_alloc_count != ctx->global->heap_vars.length ()
3191 || (save_heap_dealloc_count
3192 != ctx->global->heap_dealloc_count)))
3193 {
3194 tree heap_var;
3195 unsigned int i;
3196 if ((ctx->global->heap_vars.length ()
3197 - ctx->global->heap_dealloc_count)
3198 != save_heap_alloc_count - save_heap_dealloc_count)
3199 cacheable = false;
3200 else
3201 FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
3202 save_heap_alloc_count)
3203 if (DECL_NAME (heap_var) != heap_deleted_identifier)
3204 {
3205 cacheable = false;
3206 break;
3207 }
3208 /* Also don't cache a call that returns a deallocated pointer. */
3209 if (cacheable && (cp_walk_tree_without_duplicates
3210 (&result, find_heap_var_refs, NULL)))
3211 cacheable = false;
3212 }
3213
3214 /* Rewrite all occurrences of the function's RESULT_DECL with the
3215 current object under construction. */
3216 if (!*non_constant_p && ctx->object
3217 && CLASS_TYPE_P (TREE_TYPE (res))
3218 && !is_empty_class (TREE_TYPE (res)))
3219 if (replace_decl (&result, res, ctx->object))
3220 cacheable = false;
3221 }
3222 else
3223 /* Couldn't get a function copy to evaluate. */
3224 *non_constant_p = true;
3225
3226 if (result == error_mark_node)
3227 *non_constant_p = true;
3228 if (*non_constant_p || *overflow_p)
3229 result = error_mark_node;
3230 else if (!result)
3231 result = void_node;
3232 if (entry)
3233 entry->result = cacheable ? result : error_mark_node;
3234 }
3235
3236 /* The result of a constexpr function must be completely initialized.
3237
3238 However, in C++20, a constexpr constructor doesn't necessarily have
3239 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
3240 in order to detect reading an unitialized object in constexpr instead
3241 of value-initializing it. (reduced_constant_expression_p is expected to
3242 take care of clearing the flag.) */
3243 if (TREE_CODE (result) == CONSTRUCTOR
3244 && (cxx_dialect < cxx20
3245 || !DECL_CONSTRUCTOR_P (fun)))
3246 clear_no_implicit_zero (result);
3247
3248 pop_cx_call_context ();
3249 return result;
3250 }
3251
3252 /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
3253 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
3254 cleared.
3255 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
3256
3257 bool
3258 reduced_constant_expression_p (tree t)
3259 {
3260 if (t == NULL_TREE)
3261 return false;
3262
3263 switch (TREE_CODE (t))
3264 {
3265 case PTRMEM_CST:
3266 /* Even if we can't lower this yet, it's constant. */
3267 return true;
3268
3269 case CONSTRUCTOR:
3270 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
3271 tree field;
3272 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
3273 /* An initialized vector would have a VECTOR_CST. */
3274 return false;
3275 if (CONSTRUCTOR_NO_CLEARING (t))
3276 {
3277 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
3278 {
3279 /* There must be a valid constant initializer at every array
3280 index. */
3281 tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3282 tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
3283 tree cursor = min;
3284 for (auto &e: CONSTRUCTOR_ELTS (t))
3285 {
3286 if (!reduced_constant_expression_p (e.value))
3287 return false;
3288 if (array_index_cmp (cursor, e.index) != 0)
3289 return false;
3290 if (TREE_CODE (e.index) == RANGE_EXPR)
3291 cursor = TREE_OPERAND (e.index, 1);
3292 cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node);
3293 }
3294 if (find_array_ctor_elt (t, max) == -1)
3295 return false;
3296 goto ok;
3297 }
3298 else if (cxx_dialect >= cxx20
3299 && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
3300 {
3301 if (CONSTRUCTOR_NELTS (t) == 0)
3302 /* An initialized union has a constructor element. */
3303 return false;
3304 /* And it only initializes one member. */
3305 field = NULL_TREE;
3306 }
3307 else
3308 field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
3309 }
3310 else
3311 field = NULL_TREE;
3312 for (auto &e: CONSTRUCTOR_ELTS (t))
3313 {
3314 /* If VAL is null, we're in the middle of initializing this
3315 element. */
3316 if (!reduced_constant_expression_p (e.value))
3317 return false;
3318 /* We want to remove initializers for empty fields in a struct to
3319 avoid confusing output_constructor. */
3320 if (is_empty_field (e.index)
3321 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
3322 return false;
3323 /* Check for non-empty fields between initialized fields when
3324 CONSTRUCTOR_NO_CLEARING. */
3325 for (; field && e.index != field;
3326 field = next_subobject_field (DECL_CHAIN (field)))
3327 if (!is_really_empty_class (TREE_TYPE (field),
3328 /*ignore_vptr*/false))
3329 return false;
3330 if (field)
3331 field = next_subobject_field (DECL_CHAIN (field));
3332 }
3333 /* There could be a non-empty field at the end. */
3334 for (; field; field = next_subobject_field (DECL_CHAIN (field)))
3335 if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
3336 return false;
3337 ok:
3338 if (CONSTRUCTOR_NO_CLEARING (t))
3339 /* All the fields are initialized. */
3340 CONSTRUCTOR_NO_CLEARING (t) = false;
3341 return true;
3342
3343 default:
3344 /* FIXME are we calling this too much? */
3345 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
3346 }
3347 }
3348
3349 /* Some expressions may have constant operands but are not constant
3350 themselves, such as 1/0. Call this function to check for that
3351 condition.
3352
3353 We only call this in places that require an arithmetic constant, not in
3354 places where we might have a non-constant expression that can be a
3355 component of a constant expression, such as the address of a constexpr
3356 variable that might be dereferenced later. */
3357
3358 static bool
3359 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
3360 bool *overflow_p)
3361 {
3362 if (!*non_constant_p && !reduced_constant_expression_p (t)
3363 && t != void_node)
3364 {
3365 if (!allow_non_constant)
3366 error ("%q+E is not a constant expression", t);
3367 *non_constant_p = true;
3368 }
3369 if (TREE_OVERFLOW_P (t))
3370 {
3371 if (!allow_non_constant)
3372 {
3373 permerror (input_location, "overflow in constant expression");
3374 /* If we're being permissive (and are in an enforcing
3375 context), ignore the overflow. */
3376 if (flag_permissive)
3377 return *non_constant_p;
3378 }
3379 *overflow_p = true;
3380 }
3381 return *non_constant_p;
3382 }
3383
3384 /* Check whether the shift operation with code CODE and type TYPE on LHS
3385 and RHS is undefined. If it is, give an error with an explanation,
3386 and return true; return false otherwise. */
3387
3388 static bool
3389 cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
3390 enum tree_code code, tree type, tree lhs, tree rhs)
3391 {
3392 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
3393 || TREE_CODE (lhs) != INTEGER_CST
3394 || TREE_CODE (rhs) != INTEGER_CST)
3395 return false;
3396
3397 tree lhstype = TREE_TYPE (lhs);
3398 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
3399
3400 /* [expr.shift] The behavior is undefined if the right operand
3401 is negative, or greater than or equal to the length in bits
3402 of the promoted left operand. */
3403 if (tree_int_cst_sgn (rhs) == -1)
3404 {
3405 if (!ctx->quiet)
3406 permerror (loc, "right operand of shift expression %q+E is negative",
3407 build2_loc (loc, code, type, lhs, rhs));
3408 return (!flag_permissive || ctx->quiet);
3409 }
3410 if (compare_tree_int (rhs, uprec) >= 0)
3411 {
3412 if (!ctx->quiet)
3413 permerror (loc, "right operand of shift expression %q+E is greater "
3414 "than or equal to the precision %wu of the left operand",
3415 build2_loc (loc, code, type, lhs, rhs), uprec);
3416 return (!flag_permissive || ctx->quiet);
3417 }
3418
3419 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
3420 if E1 has a signed type and non-negative value, and E1x2^E2 is
3421 representable in the corresponding unsigned type of the result type,
3422 then that value, converted to the result type, is the resulting value;
3423 otherwise, the behavior is undefined.
3424 For C++20:
3425 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
3426 2^N, where N is the range exponent of the type of the result. */
3427 if (code == LSHIFT_EXPR
3428 && !TYPE_OVERFLOW_WRAPS (lhstype)
3429 && cxx_dialect >= cxx11
3430 && cxx_dialect < cxx20)
3431 {
3432 if (tree_int_cst_sgn (lhs) == -1)
3433 {
3434 if (!ctx->quiet)
3435 permerror (loc,
3436 "left operand of shift expression %q+E is negative",
3437 build2_loc (loc, code, type, lhs, rhs));
3438 return (!flag_permissive || ctx->quiet);
3439 }
3440 /* For signed x << y the following:
3441 (unsigned) x >> ((prec (lhs) - 1) - y)
3442 if > 1, is undefined. The right-hand side of this formula
3443 is the highest bit of the LHS that can be set (starting from 0),
3444 so that the shift doesn't overflow. We then right-shift the LHS
3445 to see whether any other bit is set making the original shift
3446 undefined -- the result is not representable in the corresponding
3447 unsigned type. */
3448 tree t = build_int_cst (unsigned_type_node, uprec - 1);
3449 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
3450 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
3451 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
3452 if (tree_int_cst_lt (integer_one_node, t))
3453 {
3454 if (!ctx->quiet)
3455 permerror (loc, "shift expression %q+E overflows",
3456 build2_loc (loc, code, type, lhs, rhs));
3457 return (!flag_permissive || ctx->quiet);
3458 }
3459 }
3460 return false;
3461 }
3462
3463 /* Subroutine of cxx_eval_constant_expression.
3464 Attempt to reduce the unary expression tree T to a compile time value.
3465 If successful, return the value. Otherwise issue a diagnostic
3466 and return error_mark_node. */
3467
3468 static tree
3469 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
3470 bool /*lval*/,
3471 bool *non_constant_p, bool *overflow_p)
3472 {
3473 tree r;
3474 tree orig_arg = TREE_OPERAND (t, 0);
3475 tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
3476 non_constant_p, overflow_p);
3477 VERIFY_CONSTANT (arg);
3478 location_t loc = EXPR_LOCATION (t);
3479 enum tree_code code = TREE_CODE (t);
3480 tree type = TREE_TYPE (t);
3481 r = fold_unary_loc (loc, code, type, arg);
3482 if (r == NULL_TREE)
3483 {
3484 if (arg == orig_arg)
3485 r = t;
3486 else
3487 r = build1_loc (loc, code, type, arg);
3488 }
3489 VERIFY_CONSTANT (r);
3490 return r;
3491 }
3492
3493 /* Helper function for cxx_eval_binary_expression. Try to optimize
3494 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
3495 generic folding should be used. */
3496
3497 static tree
3498 cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
3499 tree lhs, tree rhs, bool *non_constant_p,
3500 bool *overflow_p)
3501 {
3502 STRIP_NOPS (lhs);
3503 if (TREE_CODE (lhs) != ADDR_EXPR)
3504 return NULL_TREE;
3505
3506 lhs = TREE_OPERAND (lhs, 0);
3507
3508 /* &A[i] p+ j => &A[i + j] */
3509 if (TREE_CODE (lhs) == ARRAY_REF
3510 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
3511 && TREE_CODE (rhs) == INTEGER_CST
3512 && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
3513 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
3514 {
3515 tree orig_type = TREE_TYPE (t);
3516 location_t loc = EXPR_LOCATION (t);
3517 tree type = TREE_TYPE (lhs);
3518
3519 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
3520 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
3521 nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
3522 non_constant_p, overflow_p);
3523 if (*non_constant_p)
3524 return NULL_TREE;
3525 /* Don't fold an out-of-bound access. */
3526 if (!tree_int_cst_le (t, nelts))
3527 return NULL_TREE;
3528 rhs = cp_fold_convert (ssizetype, rhs);
3529 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
3530 constexpr int A[1]; ... (char *)&A[0] + 1 */
3531 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
3532 rhs, TYPE_SIZE_UNIT (type))))
3533 return NULL_TREE;
3534 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
3535 as signed. */
3536 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
3537 TYPE_SIZE_UNIT (type));
3538 t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
3539 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
3540 t, NULL_TREE, NULL_TREE);
3541 t = cp_build_addr_expr (t, tf_warning_or_error);
3542 t = cp_fold_convert (orig_type, t);
3543 return cxx_eval_constant_expression (ctx, t, vc_prvalue,
3544 non_constant_p, overflow_p);
3545 }
3546
3547 return NULL_TREE;
3548 }
3549
3550 /* Try to fold expressions like
3551 (struct S *) (&a[0].D.2378 + 12)
3552 into
3553 &MEM <struct T> [(void *)&a + 12B]
3554 This is something normally done by gimple_fold_stmt_to_constant_1
3555 on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
3556 dereference the address because some details are lost.
3557 For pointer comparisons we want such folding though so that
3558 match.pd address_compare optimization works. */
3559
3560 static tree
3561 cxx_maybe_fold_addr_pointer_plus (tree t)
3562 {
3563 while (CONVERT_EXPR_P (t)
3564 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
3565 t = TREE_OPERAND (t, 0);
3566 if (TREE_CODE (t) != POINTER_PLUS_EXPR)
3567 return NULL_TREE;
3568 tree op0 = TREE_OPERAND (t, 0);
3569 tree op1 = TREE_OPERAND (t, 1);
3570 if (TREE_CODE (op1) != INTEGER_CST)
3571 return NULL_TREE;
3572 while (CONVERT_EXPR_P (op0)
3573 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
3574 op0 = TREE_OPERAND (op0, 0);
3575 if (TREE_CODE (op0) != ADDR_EXPR)
3576 return NULL_TREE;
3577 op1 = fold_convert (ptr_type_node, op1);
3578 tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
3579 return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
3580 }
3581
3582 /* Subroutine of cxx_eval_constant_expression.
3583 Like cxx_eval_unary_expression, except for binary expressions. */
3584
3585 static tree
3586 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
3587 value_cat lval,
3588 bool *non_constant_p, bool *overflow_p)
3589 {
3590 tree r = NULL_TREE;
3591 tree orig_lhs = TREE_OPERAND (t, 0);
3592 tree orig_rhs = TREE_OPERAND (t, 1);
3593 tree lhs, rhs;
3594 lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
3595 non_constant_p, overflow_p);
3596 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3597 subtraction. */
3598 if (*non_constant_p)
3599 return t;
3600 rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
3601 non_constant_p, overflow_p);
3602 if (*non_constant_p)
3603 return t;
3604
3605 location_t loc = EXPR_LOCATION (t);
3606 enum tree_code code = TREE_CODE (t);
3607 tree type = TREE_TYPE (t);
3608
3609 if (code == EQ_EXPR || code == NE_EXPR)
3610 {
3611 bool is_code_eq = (code == EQ_EXPR);
3612
3613 if (TREE_CODE (lhs) == PTRMEM_CST
3614 && TREE_CODE (rhs) == PTRMEM_CST)
3615 {
3616 tree lmem = PTRMEM_CST_MEMBER (lhs);
3617 tree rmem = PTRMEM_CST_MEMBER (rhs);
3618 bool eq;
3619 if (TREE_CODE (lmem) == TREE_CODE (rmem)
3620 && TREE_CODE (lmem) == FIELD_DECL
3621 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3622 && same_type_p (DECL_CONTEXT (lmem),
3623 DECL_CONTEXT (rmem)))
3624 /* If both refer to (possibly different) members of the same union
3625 (12.3), they compare equal. */
3626 eq = true;
3627 else
3628 eq = cp_tree_equal (lhs, rhs);
3629 r = constant_boolean_node (eq == is_code_eq, type);
3630 }
3631 else if ((TREE_CODE (lhs) == PTRMEM_CST
3632 || TREE_CODE (rhs) == PTRMEM_CST)
3633 && (null_member_pointer_value_p (lhs)
3634 || null_member_pointer_value_p (rhs)))
3635 r = constant_boolean_node (!is_code_eq, type);
3636 else if (TREE_CODE (lhs) == PTRMEM_CST)
3637 lhs = cplus_expand_constant (lhs);
3638 else if (TREE_CODE (rhs) == PTRMEM_CST)
3639 rhs = cplus_expand_constant (rhs);
3640 }
3641 if (r == NULL_TREE
3642 && TREE_CODE_CLASS (code) == tcc_comparison
3643 && POINTER_TYPE_P (TREE_TYPE (lhs)))
3644 {
3645 if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
3646 lhs = fold_convert (TREE_TYPE (lhs), lhso);
3647 if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
3648 rhs = fold_convert (TREE_TYPE (rhs), rhso);
3649 }
3650 if (code == POINTER_PLUS_EXPR && !*non_constant_p
3651 && integer_zerop (lhs) && !integer_zerop (rhs))
3652 {
3653 if (!ctx->quiet)
3654 error ("arithmetic involving a null pointer in %qE", lhs);
3655 *non_constant_p = true;
3656 return t;
3657 }
3658 else if (code == POINTER_PLUS_EXPR)
3659 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3660 overflow_p);
3661 else if (code == SPACESHIP_EXPR)
3662 {
3663 r = genericize_spaceship (loc, type, lhs, rhs);
3664 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3665 overflow_p);
3666 }
3667
3668 if (r == NULL_TREE)
3669 {
3670 if (ctx->manifestly_const_eval
3671 && (flag_constexpr_fp_except
3672 || TREE_CODE (type) != REAL_TYPE))
3673 {
3674 auto ofcc = make_temp_override (folding_cxx_constexpr, true);
3675 r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
3676 }
3677 else
3678 r = fold_binary_loc (loc, code, type, lhs, rhs);
3679 }
3680
3681 if (r == NULL_TREE
3682 && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
3683 && TREE_CODE (lhs) == INTEGER_CST
3684 && TREE_CODE (rhs) == INTEGER_CST
3685 && wi::neg_p (wi::to_wide (rhs)))
3686 {
3687 /* For diagnostics and -fpermissive emulate previous behavior of
3688 handling shifts by negative amount. */
3689 tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
3690 if (nrhs)
3691 r = fold_binary_loc (loc,
3692 code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
3693 type, lhs, nrhs);
3694 }
3695
3696 if (r == NULL_TREE)
3697 {
3698 if (lhs == orig_lhs && rhs == orig_rhs)
3699 r = t;
3700 else
3701 r = build2_loc (loc, code, type, lhs, rhs);
3702 }
3703 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
3704 *non_constant_p = true;
3705 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3706 a local array in a constexpr function. */
3707 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
3708 if (!ptr)
3709 VERIFY_CONSTANT (r);
3710 return r;
3711 }
3712
3713 /* Subroutine of cxx_eval_constant_expression.
3714 Attempt to evaluate condition expressions. Dead branches are not
3715 looked into. */
3716
3717 static tree
3718 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
3719 value_cat lval,
3720 bool *non_constant_p, bool *overflow_p,
3721 tree *jump_target)
3722 {
3723 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3724 vc_prvalue,
3725 non_constant_p, overflow_p);
3726 VERIFY_CONSTANT (val);
3727 if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
3728 {
3729 /* Evaluate the condition as if it was
3730 if (__builtin_is_constant_evaluated ()), i.e. defer it if not
3731 ctx->manifestly_const_eval (as sometimes we try to constant evaluate
3732 without manifestly_const_eval even expressions or parts thereof which
3733 will later be manifestly const_eval evaluated), otherwise fold it to
3734 true. */
3735 if (ctx->manifestly_const_eval)
3736 val = boolean_true_node;
3737 else
3738 {
3739 *non_constant_p = true;
3740 return t;
3741 }
3742 }
3743 /* Don't VERIFY_CONSTANT the other operands. */
3744 if (integer_zerop (val))
3745 val = TREE_OPERAND (t, 2);
3746 else
3747 val = TREE_OPERAND (t, 1);
3748 if (TREE_CODE (t) == IF_STMT && !val)
3749 val = void_node;
3750 /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
3751 serve as the initializer for the same object as the outer TARGET_EXPR,
3752 as in
3753 A a = true ? A{} : A{};
3754 so strip the inner TARGET_EXPR so we don't materialize a temporary. */
3755 if (TREE_CODE (val) == TARGET_EXPR)
3756 val = TARGET_EXPR_INITIAL (val);
3757 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3758 overflow_p, jump_target);
3759 }
3760
3761 /* Subroutine of cxx_eval_constant_expression.
3762 Attempt to evaluate vector condition expressions. Unlike
3763 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3764 ternary arithmetics operation, where all 3 arguments have to be
3765 evaluated as constants and then folding computes the result from
3766 them. */
3767
3768 static tree
3769 cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
3770 bool *non_constant_p, bool *overflow_p)
3771 {
3772 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3773 vc_prvalue,
3774 non_constant_p, overflow_p);
3775 VERIFY_CONSTANT (arg1);
3776 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3777 vc_prvalue,
3778 non_constant_p, overflow_p);
3779 VERIFY_CONSTANT (arg2);
3780 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
3781 vc_prvalue,
3782 non_constant_p, overflow_p);
3783 VERIFY_CONSTANT (arg3);
3784 location_t loc = EXPR_LOCATION (t);
3785 tree type = TREE_TYPE (t);
3786 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3787 if (r == NULL_TREE)
3788 {
3789 if (arg1 == TREE_OPERAND (t, 0)
3790 && arg2 == TREE_OPERAND (t, 1)
3791 && arg3 == TREE_OPERAND (t, 2))
3792 r = t;
3793 else
3794 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3795 }
3796 VERIFY_CONSTANT (r);
3797 return r;
3798 }
3799
3800 /* Returns less than, equal to, or greater than zero if KEY is found to be
3801 less than, to match, or to be greater than the constructor_elt's INDEX. */
3802
3803 static int
3804 array_index_cmp (tree key, tree index)
3805 {
3806 gcc_assert (TREE_CODE (key) == INTEGER_CST);
3807
3808 switch (TREE_CODE (index))
3809 {
3810 case INTEGER_CST:
3811 return tree_int_cst_compare (key, index);
3812 case RANGE_EXPR:
3813 {
3814 tree lo = TREE_OPERAND (index, 0);
3815 tree hi = TREE_OPERAND (index, 1);
3816 if (tree_int_cst_lt (key, lo))
3817 return -1;
3818 else if (tree_int_cst_lt (hi, key))
3819 return 1;
3820 else
3821 return 0;
3822 }
3823 default:
3824 gcc_unreachable ();
3825 }
3826 }
3827
3828 /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3829 if none. If INSERT is true, insert a matching element rather than fail. */
3830
3831 static HOST_WIDE_INT
3832 find_array_ctor_elt (tree ary, tree dindex, bool insert)
3833 {
3834 if (tree_int_cst_sgn (dindex) < 0)
3835 return -1;
3836
3837 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
3838 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
3839 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
3840
3841 unsigned HOST_WIDE_INT end = len;
3842 unsigned HOST_WIDE_INT begin = 0;
3843
3844 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3845 that the same is true of the other elements and index directly. */
3846 if (end > 0)
3847 {
3848 tree cindex = (*elts)[end - 1].index;
3849 if (cindex == NULL_TREE)
3850 {
3851 /* Verify that if the last index is missing, all indexes
3852 are missing. */
3853 if (flag_checking)
3854 for (unsigned int j = 0; j < len - 1; ++j)
3855 gcc_assert ((*elts)[j].index == NULL_TREE);
3856 if (i < end)
3857 return i;
3858 else
3859 {
3860 begin = end;
3861 if (i == end)
3862 /* If the element is to be added right at the end,
3863 make sure it is added with cleared index too. */
3864 dindex = NULL_TREE;
3865 else if (insert)
3866 /* Otherwise, in order not to break the assumption
3867 that CONSTRUCTOR either has all indexes or none,
3868 we need to add indexes to all elements. */
3869 for (unsigned int j = 0; j < len; ++j)
3870 (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
3871 }
3872 }
3873 else if (TREE_CODE (cindex) == INTEGER_CST
3874 && compare_tree_int (cindex, end - 1) == 0)
3875 {
3876 if (i < end)
3877 return i;
3878 else
3879 begin = end;
3880 }
3881 }
3882
3883 /* Otherwise, find a matching index by means of a binary search. */
3884 while (begin != end)
3885 {
3886 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
3887 constructor_elt &elt = (*elts)[middle];
3888 tree idx = elt.index;
3889
3890 int cmp = array_index_cmp (dindex, idx);
3891 if (cmp < 0)
3892 end = middle;
3893 else if (cmp > 0)
3894 begin = middle + 1;
3895 else
3896 {
3897 if (insert && TREE_CODE (idx) == RANGE_EXPR)
3898 {
3899 /* We need to split the range. */
3900 constructor_elt e;
3901 tree lo = TREE_OPERAND (idx, 0);
3902 tree hi = TREE_OPERAND (idx, 1);
3903 tree value = elt.value;
3904 dindex = fold_convert (sizetype, dindex);
3905 if (tree_int_cst_lt (lo, dindex))
3906 {
3907 /* There are still some lower elts; shorten the range. */
3908 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
3909 size_one_node);
3910 if (tree_int_cst_equal (lo, new_hi))
3911 /* Only one element left, no longer a range. */
3912 elt.index = lo;
3913 else
3914 TREE_OPERAND (idx, 1) = new_hi;
3915 /* Append the element we want to insert. */
3916 ++middle;
3917 e.index = dindex;
3918 e.value = unshare_constructor (value);
3919 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
3920 }
3921 else
3922 /* No lower elts, the range elt is now ours. */
3923 elt.index = dindex;
3924
3925 if (tree_int_cst_lt (dindex, hi))
3926 {
3927 /* There are still some higher elts; append a range. */
3928 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
3929 size_one_node);
3930 if (tree_int_cst_equal (new_lo, hi))
3931 e.index = hi;
3932 else
3933 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
3934 e.value = unshare_constructor (value);
3935 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
3936 }
3937 }
3938 return middle;
3939 }
3940 }
3941
3942 if (insert)
3943 {
3944 constructor_elt e = { dindex, NULL_TREE };
3945 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
3946 return end;
3947 }
3948
3949 return -1;
3950 }
3951
3952 /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3953 matching constructor_elt exists, then add one to CTOR.
3954
3955 As an optimization, if POS_HINT is non-negative then it is used as a guess
3956 for the (integer) index of the matching constructor_elt within CTOR. */
3957
3958 static constructor_elt *
3959 get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
3960 {
3961 /* Check the hint first. */
3962 if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
3963 && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
3964 return CONSTRUCTOR_ELT (ctor, pos_hint);
3965
3966 tree type = TREE_TYPE (ctor);
3967 if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
3968 {
3969 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
3970 return &CONSTRUCTOR_ELTS (ctor)->last();
3971 }
3972 else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
3973 {
3974 if (TREE_CODE (index) == RANGE_EXPR)
3975 {
3976 /* Support for RANGE_EXPR index lookups is currently limited to
3977 accessing an existing element via POS_HINT, or appending a new
3978 element to the end of CTOR. ??? Support for other access
3979 patterns may also be needed. */
3980 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3981 if (vec_safe_length (elts))
3982 {
3983 tree lo = TREE_OPERAND (index, 0);
3984 gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
3985 }
3986 CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
3987 return &elts->last();
3988 }
3989
3990 HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
3991 gcc_assert (i >= 0);
3992 constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
3993 gcc_assert (cep->index == NULL_TREE
3994 || TREE_CODE (cep->index) != RANGE_EXPR);
3995 return cep;
3996 }
3997 else
3998 {
3999 gcc_assert (TREE_CODE (index) == FIELD_DECL
4000 && (same_type_ignoring_top_level_qualifiers_p
4001 (DECL_CONTEXT (index), TREE_TYPE (ctor))));
4002
4003 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
4004 Usually we meet initializers in that order, but it is
4005 possible for base types to be placed not in program
4006 order. */
4007 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
4008 unsigned HOST_WIDE_INT idx = 0;
4009 constructor_elt *cep = NULL;
4010
4011 /* Check if we're changing the active member of a union. */
4012 if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
4013 && CONSTRUCTOR_ELT (ctor, 0)->index != index)
4014 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
4015 /* If the bit offset of INDEX is larger than that of the last
4016 constructor_elt, then we can just immediately append a new
4017 constructor_elt to the end of CTOR. */
4018 else if (CONSTRUCTOR_NELTS (ctor)
4019 && tree_int_cst_compare (bit_position (index),
4020 bit_position (CONSTRUCTOR_ELTS (ctor)
4021 ->last().index)) > 0)
4022 {
4023 idx = CONSTRUCTOR_NELTS (ctor);
4024 goto insert;
4025 }
4026
4027 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
4028 appropriately. */
4029
4030 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
4031 idx++, fields = DECL_CHAIN (fields))
4032 {
4033 if (index == cep->index)
4034 goto found;
4035
4036 /* The field we're initializing must be on the field
4037 list. Look to see if it is present before the
4038 field the current ELT initializes. */
4039 for (; fields != cep->index; fields = DECL_CHAIN (fields))
4040 if (index == fields)
4041 goto insert;
4042 }
4043 /* We fell off the end of the CONSTRUCTOR, so insert a new
4044 entry at the end. */
4045
4046 insert:
4047 {
4048 constructor_elt ce = { index, NULL_TREE };
4049
4050 vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
4051 cep = CONSTRUCTOR_ELT (ctor, idx);
4052 }
4053 found:;
4054
4055 return cep;
4056 }
4057 }
4058
4059 /* Under the control of CTX, issue a detailed diagnostic for
4060 an out-of-bounds subscript INDEX into the expression ARRAY. */
4061
4062 static void
4063 diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
4064 {
4065 if (!ctx->quiet)
4066 {
4067 tree arraytype = TREE_TYPE (array);
4068
4069 /* Convert the unsigned array subscript to a signed integer to avoid
4070 printing huge numbers for small negative values. */
4071 tree sidx = fold_convert (ssizetype, index);
4072 STRIP_ANY_LOCATION_WRAPPER (array);
4073 if (DECL_P (array))
4074 {
4075 if (TYPE_DOMAIN (arraytype))
4076 error_at (loc, "array subscript value %qE is outside the bounds "
4077 "of array %qD of type %qT", sidx, array, arraytype);
4078 else
4079 error_at (loc, "nonzero array subscript %qE is used with array %qD of "
4080 "type %qT with unknown bounds", sidx, array, arraytype);
4081 inform (DECL_SOURCE_LOCATION (array), "declared here");
4082 }
4083 else if (TYPE_DOMAIN (arraytype))
4084 error_at (loc, "array subscript value %qE is outside the bounds "
4085 "of array type %qT", sidx, arraytype);
4086 else
4087 error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
4088 "with unknown bounds", sidx, arraytype);
4089 }
4090 }
4091
4092 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
4093 a VECTOR_TYPE). */
4094
4095 static tree
4096 get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
4097 bool *non_constant_p, bool *overflow_p)
4098 {
4099 tree nelts;
4100 if (TREE_CODE (type) == ARRAY_TYPE)
4101 {
4102 if (TYPE_DOMAIN (type))
4103 nelts = array_type_nelts_top (type);
4104 else
4105 nelts = size_zero_node;
4106 }
4107 else if (VECTOR_TYPE_P (type))
4108 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
4109 else
4110 gcc_unreachable ();
4111
4112 /* For VLAs, the number of elements won't be an integer constant. */
4113 nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
4114 non_constant_p, overflow_p);
4115 return nelts;
4116 }
4117
4118 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
4119 STRING_CST STRING. */
4120
4121 static tree
4122 extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
4123 {
4124 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
4125 tree r;
4126
4127 if (chars_per_elt == 1)
4128 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
4129 else
4130 {
4131 const unsigned char *ptr
4132 = ((const unsigned char *)TREE_STRING_POINTER (string)
4133 + index * chars_per_elt);
4134 r = native_interpret_expr (type, ptr, chars_per_elt);
4135 }
4136 return r;
4137 }
4138
4139 /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
4140 subscript, diagnose any problems with it, and return the result. */
4141
4142 static tree
4143 eval_and_check_array_index (const constexpr_ctx *ctx,
4144 tree t, bool allow_one_past,
4145 bool *non_constant_p, bool *overflow_p)
4146 {
4147 location_t loc = cp_expr_loc_or_input_loc (t);
4148 tree ary = TREE_OPERAND (t, 0);
4149 t = TREE_OPERAND (t, 1);
4150 tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
4151 non_constant_p, overflow_p);
4152 VERIFY_CONSTANT (index);
4153
4154 if (!tree_fits_shwi_p (index)
4155 || tree_int_cst_sgn (index) < 0)
4156 {
4157 diag_array_subscript (loc, ctx, ary, index);
4158 *non_constant_p = true;
4159 return t;
4160 }
4161
4162 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
4163 overflow_p);
4164 VERIFY_CONSTANT (nelts);
4165 if (allow_one_past
4166 ? !tree_int_cst_le (index, nelts)
4167 : !tree_int_cst_lt (index, nelts))
4168 {
4169 diag_array_subscript (loc, ctx, ary, index);
4170 *non_constant_p = true;
4171 return t;
4172 }
4173
4174 return index;
4175 }
4176
4177 /* Subroutine of cxx_eval_constant_expression.
4178 Attempt to reduce a reference to an array slot. */
4179
4180 static tree
4181 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
4182 value_cat lval,
4183 bool *non_constant_p, bool *overflow_p)
4184 {
4185 tree oldary = TREE_OPERAND (t, 0);
4186 tree ary = cxx_eval_constant_expression (ctx, oldary,
4187 lval,
4188 non_constant_p, overflow_p);
4189 if (*non_constant_p)
4190 return t;
4191 if (!lval
4192 && TREE_CODE (ary) == VIEW_CONVERT_EXPR
4193 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
4194 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
4195 ary = TREE_OPERAND (ary, 0);
4196
4197 tree oldidx = TREE_OPERAND (t, 1);
4198 tree index = eval_and_check_array_index (ctx, t, lval,
4199 non_constant_p, overflow_p);
4200 if (*non_constant_p)
4201 return t;
4202
4203 if (lval && ary == oldary && index == oldidx)
4204 return t;
4205 else if (lval == vc_discard)
4206 return t;
4207 else if (lval)
4208 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
4209
4210 unsigned len = 0, elem_nchars = 1;
4211 tree elem_type = TREE_TYPE (TREE_TYPE (ary));
4212 if (TREE_CODE (ary) == CONSTRUCTOR)
4213 len = CONSTRUCTOR_NELTS (ary);
4214 else if (TREE_CODE (ary) == STRING_CST)
4215 {
4216 elem_nchars = (TYPE_PRECISION (elem_type)
4217 / TYPE_PRECISION (char_type_node));
4218 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
4219 }
4220 else if (TREE_CODE (ary) == VECTOR_CST)
4221 /* We don't create variable-length VECTOR_CSTs. */
4222 len = VECTOR_CST_NELTS (ary).to_constant ();
4223 else
4224 {
4225 /* We can't do anything with other tree codes, so use
4226 VERIFY_CONSTANT to complain and fail. */
4227 VERIFY_CONSTANT (ary);
4228 gcc_unreachable ();
4229 }
4230
4231 bool found;
4232 HOST_WIDE_INT i = 0;
4233 if (TREE_CODE (ary) == CONSTRUCTOR)
4234 {
4235 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
4236 found = (ix >= 0);
4237 if (found)
4238 i = ix;
4239 }
4240 else
4241 {
4242 i = tree_to_shwi (index);
4243 found = (i < len);
4244 }
4245
4246 if (found)
4247 {
4248 tree r;
4249 if (TREE_CODE (ary) == CONSTRUCTOR)
4250 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
4251 else if (TREE_CODE (ary) == VECTOR_CST)
4252 r = VECTOR_CST_ELT (ary, i);
4253 else
4254 r = extract_string_elt (ary, elem_nchars, i);
4255
4256 if (r)
4257 /* Don't VERIFY_CONSTANT here. */
4258 return r;
4259
4260 /* Otherwise the element doesn't have a value yet. */
4261 }
4262
4263 /* Not found. */
4264
4265 if (TREE_CODE (ary) == CONSTRUCTOR
4266 && CONSTRUCTOR_NO_CLEARING (ary))
4267 {
4268 /* 'ary' is part of the aggregate initializer we're currently
4269 building; if there's no initializer for this element yet,
4270 that's an error. */
4271 if (!ctx->quiet)
4272 error ("accessing uninitialized array element");
4273 *non_constant_p = true;
4274 return t;
4275 }
4276
4277 /* If it's within the array bounds but doesn't have an explicit
4278 initializer, it's initialized from {}. But use build_value_init
4279 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
4280 tree val;
4281 constexpr_ctx new_ctx;
4282 if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
4283 return build_constructor (elem_type, NULL);
4284 else if (CP_AGGREGATE_TYPE_P (elem_type))
4285 {
4286 tree empty_ctor = build_constructor (init_list_type_node, NULL);
4287 val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
4288 }
4289 else
4290 val = build_value_init (elem_type, tf_warning_or_error);
4291
4292 if (!SCALAR_TYPE_P (elem_type))
4293 {
4294 new_ctx = *ctx;
4295 if (ctx->object)
4296 /* If there was no object, don't add one: it could confuse us
4297 into thinking we're modifying a const object. */
4298 new_ctx.object = t;
4299 new_ctx.ctor = build_constructor (elem_type, NULL);
4300 ctx = &new_ctx;
4301 }
4302 t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
4303 overflow_p);
4304 if (!SCALAR_TYPE_P (elem_type) && t != ctx->ctor)
4305 free_constructor (ctx->ctor);
4306 return t;
4307 }
4308
4309 /* Subroutine of cxx_eval_constant_expression.
4310 Attempt to reduce a field access of a value of class type. */
4311
4312 static tree
4313 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
4314 value_cat lval,
4315 bool *non_constant_p, bool *overflow_p)
4316 {
4317 unsigned HOST_WIDE_INT i;
4318 tree field;
4319 tree value;
4320 tree part = TREE_OPERAND (t, 1);
4321 tree orig_whole = TREE_OPERAND (t, 0);
4322 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4323 lval,
4324 non_constant_p, overflow_p);
4325 if (*non_constant_p)
4326 return t;
4327 if (INDIRECT_REF_P (whole)
4328 && integer_zerop (TREE_OPERAND (whole, 0)))
4329 {
4330 if (!ctx->quiet)
4331 error ("dereferencing a null pointer in %qE", orig_whole);
4332 *non_constant_p = true;
4333 return t;
4334 }
4335
4336 if (TREE_CODE (whole) == PTRMEM_CST)
4337 whole = cplus_expand_constant (whole);
4338 if (whole == orig_whole)
4339 return t;
4340 if (lval == vc_discard)
4341 return t;
4342 if (lval)
4343 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
4344 whole, part, NULL_TREE);
4345 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4346 CONSTRUCTOR. */
4347 if (TREE_CODE (whole) != CONSTRUCTOR)
4348 {
4349 if (!ctx->quiet)
4350 error ("%qE is not a constant expression", orig_whole);
4351 *non_constant_p = true;
4352 return t;
4353 }
4354 if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
4355 && DECL_MUTABLE_P (part))
4356 {
4357 if (!ctx->quiet)
4358 error ("mutable %qD is not usable in a constant expression", part);
4359 *non_constant_p = true;
4360 return t;
4361 }
4362 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
4363 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4364 {
4365 /* Use name match for PMF fields, as a variant will have a
4366 different FIELD_DECL with a different type. */
4367 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
4368 : field == part)
4369 {
4370 if (value)
4371 {
4372 STRIP_ANY_LOCATION_WRAPPER (value);
4373 return value;
4374 }
4375 else
4376 /* We're in the middle of initializing it. */
4377 break;
4378 }
4379 }
4380 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
4381 && CONSTRUCTOR_NELTS (whole) > 0)
4382 {
4383 /* DR 1188 says we don't have to deal with this. */
4384 if (!ctx->quiet)
4385 {
4386 constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
4387 if (cep->value == NULL_TREE)
4388 error ("accessing uninitialized member %qD", part);
4389 else
4390 error ("accessing %qD member instead of initialized %qD member in "
4391 "constant expression", part, cep->index);
4392 }
4393 *non_constant_p = true;
4394 return t;
4395 }
4396
4397 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
4398 classes never get represented; throw together a value now. */
4399 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
4400 return build_constructor (TREE_TYPE (t), NULL);
4401
4402 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
4403
4404 if (CONSTRUCTOR_NO_CLEARING (whole))
4405 {
4406 /* 'whole' is part of the aggregate initializer we're currently
4407 building; if there's no initializer for this member yet, that's an
4408 error. */
4409 if (!ctx->quiet)
4410 error ("accessing uninitialized member %qD", part);
4411 *non_constant_p = true;
4412 return t;
4413 }
4414
4415 /* If there's no explicit init for this field, it's value-initialized. */
4416 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
4417 return cxx_eval_constant_expression (ctx, value,
4418 lval,
4419 non_constant_p, overflow_p);
4420 }
4421
4422 /* Subroutine of cxx_eval_constant_expression.
4423 Attempt to reduce a field access of a value of class type that is
4424 expressed as a BIT_FIELD_REF. */
4425
4426 static tree
4427 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
4428 value_cat lval,
4429 bool *non_constant_p, bool *overflow_p)
4430 {
4431 tree orig_whole = TREE_OPERAND (t, 0);
4432 tree retval, fldval, utype, mask;
4433 bool fld_seen = false;
4434 HOST_WIDE_INT istart, isize;
4435 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
4436 lval,
4437 non_constant_p, overflow_p);
4438 tree start, field, value;
4439 unsigned HOST_WIDE_INT i;
4440
4441 if (whole == orig_whole)
4442 return t;
4443 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
4444 CONSTRUCTOR. */
4445 if (!*non_constant_p
4446 && TREE_CODE (whole) != VECTOR_CST
4447 && TREE_CODE (whole) != CONSTRUCTOR)
4448 {
4449 if (!ctx->quiet)
4450 error ("%qE is not a constant expression", orig_whole);
4451 *non_constant_p = true;
4452 }
4453 if (*non_constant_p)
4454 return t;
4455
4456 if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
4457 {
4458 if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
4459 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
4460 return r;
4461 if (!ctx->quiet)
4462 error ("%qE is not a constant expression", orig_whole);
4463 *non_constant_p = true;
4464 return t;
4465 }
4466
4467 start = TREE_OPERAND (t, 2);
4468 istart = tree_to_shwi (start);
4469 isize = tree_to_shwi (TREE_OPERAND (t, 1));
4470 utype = TREE_TYPE (t);
4471 if (!TYPE_UNSIGNED (utype))
4472 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
4473 retval = build_int_cst (utype, 0);
4474 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
4475 {
4476 tree bitpos = bit_position (field);
4477 STRIP_ANY_LOCATION_WRAPPER (value);
4478 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
4479 return value;
4480 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
4481 && TREE_CODE (value) == INTEGER_CST
4482 && tree_fits_shwi_p (bitpos)
4483 && tree_fits_shwi_p (DECL_SIZE (field)))
4484 {
4485 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
4486 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
4487 HOST_WIDE_INT shift;
4488 if (bit >= istart && bit + sz <= istart + isize)
4489 {
4490 fldval = fold_convert (utype, value);
4491 mask = build_int_cst_type (utype, -1);
4492 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
4493 size_int (TYPE_PRECISION (utype) - sz));
4494 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
4495 size_int (TYPE_PRECISION (utype) - sz));
4496 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
4497 shift = bit - istart;
4498 if (BYTES_BIG_ENDIAN)
4499 shift = TYPE_PRECISION (utype) - shift - sz;
4500 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
4501 size_int (shift));
4502 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
4503 fld_seen = true;
4504 }
4505 }
4506 }
4507 if (fld_seen)
4508 return fold_convert (TREE_TYPE (t), retval);
4509 gcc_unreachable ();
4510 return error_mark_node;
4511 }
4512
4513 /* Helper for cxx_eval_bit_cast.
4514 Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
4515 types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
4516 is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
4517 data members of reference type. */
4518
4519 static bool
4520 check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
4521 tree orig_type)
4522 {
4523 if (TREE_CODE (type) == UNION_TYPE)
4524 {
4525 if (!ctx->quiet)
4526 {
4527 if (type == orig_type)
4528 error_at (loc, "%qs is not a constant expression because %qT is "
4529 "a union type", "__builtin_bit_cast", type);
4530 else
4531 error_at (loc, "%qs is not a constant expression because %qT "
4532 "contains a union type", "__builtin_bit_cast",
4533 orig_type);
4534 }
4535 return true;
4536 }
4537 if (TREE_CODE (type) == POINTER_TYPE)
4538 {
4539 if (!ctx->quiet)
4540 {
4541 if (type == orig_type)
4542 error_at (loc, "%qs is not a constant expression because %qT is "
4543 "a pointer type", "__builtin_bit_cast", type);
4544 else
4545 error_at (loc, "%qs is not a constant expression because %qT "
4546 "contains a pointer type", "__builtin_bit_cast",
4547 orig_type);
4548 }
4549 return true;
4550 }
4551 if (TREE_CODE (type) == REFERENCE_TYPE)
4552 {
4553 if (!ctx->quiet)
4554 {
4555 if (type == orig_type)
4556 error_at (loc, "%qs is not a constant expression because %qT is "
4557 "a reference type", "__builtin_bit_cast", type);
4558 else
4559 error_at (loc, "%qs is not a constant expression because %qT "
4560 "contains a reference type", "__builtin_bit_cast",
4561 orig_type);
4562 }
4563 return true;
4564 }
4565 if (TYPE_PTRMEM_P (type))
4566 {
4567 if (!ctx->quiet)
4568 {
4569 if (type == orig_type)
4570 error_at (loc, "%qs is not a constant expression because %qT is "
4571 "a pointer to member type", "__builtin_bit_cast",
4572 type);
4573 else
4574 error_at (loc, "%qs is not a constant expression because %qT "
4575 "contains a pointer to member type",
4576 "__builtin_bit_cast", orig_type);
4577 }
4578 return true;
4579 }
4580 if (TYPE_VOLATILE (type))
4581 {
4582 if (!ctx->quiet)
4583 {
4584 if (type == orig_type)
4585 error_at (loc, "%qs is not a constant expression because %qT is "
4586 "volatile", "__builtin_bit_cast", type);
4587 else
4588 error_at (loc, "%qs is not a constant expression because %qT "
4589 "contains a volatile subobject",
4590 "__builtin_bit_cast", orig_type);
4591 }
4592 return true;
4593 }
4594 if (TREE_CODE (type) == RECORD_TYPE)
4595 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
4596 if (TREE_CODE (field) == FIELD_DECL
4597 && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
4598 return true;
4599 return false;
4600 }
4601
4602 /* Helper function for cxx_eval_bit_cast. For unsigned char or
4603 std::byte members of CONSTRUCTOR (recursively) if they contain
4604 some indeterminate bits (as set in MASK), remove the ctor elts,
4605 mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
4606 bits in MASK. */
4607
4608 static void
4609 clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
4610 {
4611 if (TREE_CODE (t) != CONSTRUCTOR)
4612 return;
4613
4614 unsigned i, j = 0;
4615 tree index, value;
4616 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
4617 {
4618 tree type = TREE_TYPE (value);
4619 if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
4620 && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
4621 {
4622 if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
4623 {
4624 HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
4625 gcc_assert (fldsz != 0);
4626 HOST_WIDE_INT pos = int_byte_position (index);
4627 HOST_WIDE_INT bpos
4628 = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
4629 bpos %= BITS_PER_UNIT;
4630 HOST_WIDE_INT end
4631 = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
4632 gcc_assert (end == 1 || end == 2);
4633 unsigned char *p = mask + pos;
4634 unsigned char mask_save[2];
4635 mask_save[0] = mask[pos];
4636 mask_save[1] = end == 2 ? mask[pos + 1] : 0;
4637 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4638 sorry_at (loc, "PDP11 bit-field handling unsupported"
4639 " in %qs", "__builtin_bit_cast");
4640 else if (BYTES_BIG_ENDIAN)
4641 {
4642 /* Big endian. */
4643 if (bpos + fldsz <= BITS_PER_UNIT)
4644 *p &= ~(((1 << fldsz) - 1)
4645 << (BITS_PER_UNIT - bpos - fldsz));
4646 else
4647 {
4648 gcc_assert (bpos);
4649 *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
4650 p++;
4651 fldsz -= BITS_PER_UNIT - bpos;
4652 gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4653 *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
4654 }
4655 }
4656 else
4657 {
4658 /* Little endian. */
4659 if (bpos + fldsz <= BITS_PER_UNIT)
4660 *p &= ~(((1 << fldsz) - 1) << bpos);
4661 else
4662 {
4663 gcc_assert (bpos);
4664 *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
4665 p++;
4666 fldsz -= BITS_PER_UNIT - bpos;
4667 gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
4668 *p &= ~((1 << fldsz) - 1);
4669 }
4670 }
4671 if (mask_save[0] != mask[pos]
4672 || (end == 2 && mask_save[1] != mask[pos + 1]))
4673 {
4674 CONSTRUCTOR_NO_CLEARING (t) = 1;
4675 continue;
4676 }
4677 }
4678 }
4679 else if (is_byte_access_type_not_plain_char (type))
4680 {
4681 HOST_WIDE_INT pos;
4682 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4683 pos = tree_to_shwi (index);
4684 else
4685 pos = int_byte_position (index);
4686 if (mask[pos])
4687 {
4688 CONSTRUCTOR_NO_CLEARING (t) = 1;
4689 mask[pos] = 0;
4690 continue;
4691 }
4692 }
4693 if (TREE_CODE (value) == CONSTRUCTOR)
4694 {
4695 HOST_WIDE_INT pos;
4696 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4697 pos = tree_to_shwi (index)
4698 * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
4699 else
4700 pos = int_byte_position (index);
4701 clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
4702 }
4703 if (i != j)
4704 {
4705 CONSTRUCTOR_ELT (t, j)->index = index;
4706 CONSTRUCTOR_ELT (t, j)->value = value;
4707 }
4708 ++j;
4709 }
4710 if (CONSTRUCTOR_NELTS (t) != j)
4711 vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
4712 }
4713
4714 /* Subroutine of cxx_eval_constant_expression.
4715 Attempt to evaluate a BIT_CAST_EXPR. */
4716
4717 static tree
4718 cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
4719 bool *overflow_p)
4720 {
4721 if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
4722 TREE_TYPE (t))
4723 || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
4724 EXPR_LOCATION (t)),
4725 TREE_TYPE (TREE_OPERAND (t, 0)),
4726 TREE_TYPE (TREE_OPERAND (t, 0))))
4727 {
4728 *non_constant_p = true;
4729 return t;
4730 }
4731
4732 tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
4733 non_constant_p, overflow_p);
4734 if (*non_constant_p)
4735 return t;
4736
4737 location_t loc = EXPR_LOCATION (t);
4738 if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
4739 {
4740 if (!ctx->quiet)
4741 sorry_at (loc, "%qs cannot be constant evaluated on the target",
4742 "__builtin_bit_cast");
4743 *non_constant_p = true;
4744 return t;
4745 }
4746
4747 if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
4748 {
4749 if (!ctx->quiet)
4750 sorry_at (loc, "%qs cannot be constant evaluated because the "
4751 "type is too large", "__builtin_bit_cast");
4752 *non_constant_p = true;
4753 return t;
4754 }
4755
4756 HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
4757 if (len < 0 || (int) len != len)
4758 {
4759 if (!ctx->quiet)
4760 sorry_at (loc, "%qs cannot be constant evaluated because the "
4761 "type is too large", "__builtin_bit_cast");
4762 *non_constant_p = true;
4763 return t;
4764 }
4765
4766 unsigned char buf[64];
4767 unsigned char *ptr, *mask;
4768 size_t alen = (size_t) len * 2;
4769 if (alen <= sizeof (buf))
4770 ptr = buf;
4771 else
4772 ptr = XNEWVEC (unsigned char, alen);
4773 mask = ptr + (size_t) len;
4774 /* At the beginning consider everything indeterminate. */
4775 memset (mask, ~0, (size_t) len);
4776
4777 if (native_encode_initializer (op, ptr, len, 0, mask) != len)
4778 {
4779 if (!ctx->quiet)
4780 sorry_at (loc, "%qs cannot be constant evaluated because the "
4781 "argument cannot be encoded", "__builtin_bit_cast");
4782 *non_constant_p = true;
4783 if (ptr != buf)
4784 XDELETE (ptr);
4785 return t;
4786 }
4787
4788 tree r = NULL_TREE;
4789 if (can_native_interpret_type_p (TREE_TYPE (t)))
4790 {
4791 r = native_interpret_expr (TREE_TYPE (t), ptr, len);
4792 if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
4793 {
4794 gcc_assert (len == 1);
4795 if (mask[0])
4796 {
4797 memset (mask, 0, len);
4798 r = build_constructor (TREE_TYPE (r), NULL);
4799 CONSTRUCTOR_NO_CLEARING (r) = 1;
4800 }
4801 }
4802 }
4803 else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4804 {
4805 r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
4806 if (r != NULL_TREE)
4807 {
4808 clear_type_padding_in_mask (TREE_TYPE (t), mask);
4809 clear_uchar_or_std_byte_in_mask (loc, r, mask);
4810 }
4811 }
4812
4813 if (r != NULL_TREE)
4814 {
4815 for (int i = 0; i < len; i++)
4816 if (mask[i])
4817 {
4818 if (!ctx->quiet)
4819 error_at (loc, "%qs accessing uninitialized byte at offset %d",
4820 "__builtin_bit_cast", i);
4821 *non_constant_p = true;
4822 r = t;
4823 break;
4824 }
4825 if (ptr != buf)
4826 XDELETE (ptr);
4827 return r;
4828 }
4829
4830 if (!ctx->quiet)
4831 sorry_at (loc, "%qs cannot be constant evaluated because the "
4832 "argument cannot be interpreted", "__builtin_bit_cast");
4833 *non_constant_p = true;
4834 if (ptr != buf)
4835 XDELETE (ptr);
4836 return t;
4837 }
4838
4839 /* Subroutine of cxx_eval_constant_expression.
4840 Evaluate a short-circuited logical expression T in the context
4841 of a given constexpr CALL. BAILOUT_VALUE is the value for
4842 early return. CONTINUE_VALUE is used here purely for
4843 sanity check purposes. */
4844
4845 static tree
4846 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
4847 tree bailout_value, tree continue_value,
4848 bool *non_constant_p, bool *overflow_p)
4849 {
4850 tree r;
4851 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
4852 vc_prvalue, non_constant_p,
4853 overflow_p);
4854 VERIFY_CONSTANT (lhs);
4855 if (tree_int_cst_equal (lhs, bailout_value))
4856 return lhs;
4857 gcc_assert (tree_int_cst_equal (lhs, continue_value));
4858 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
4859 vc_prvalue, non_constant_p,
4860 overflow_p);
4861 VERIFY_CONSTANT (r);
4862 return r;
4863 }
4864
4865 /* REF is a COMPONENT_REF designating a particular field. V is a vector of
4866 CONSTRUCTOR elements to initialize (part of) an object containing that
4867 field. Return a pointer to the constructor_elt corresponding to the
4868 initialization of the field. */
4869
4870 static constructor_elt *
4871 base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
4872 {
4873 tree aggr = TREE_OPERAND (ref, 0);
4874 tree field = TREE_OPERAND (ref, 1);
4875 HOST_WIDE_INT i;
4876 constructor_elt *ce;
4877
4878 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
4879
4880 if (TREE_CODE (aggr) == COMPONENT_REF)
4881 {
4882 constructor_elt *base_ce
4883 = base_field_constructor_elt (v, aggr);
4884 v = CONSTRUCTOR_ELTS (base_ce->value);
4885 }
4886
4887 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
4888 if (ce->index == field)
4889 return ce;
4890
4891 gcc_unreachable ();
4892 return NULL;
4893 }
4894
4895 /* Some of the expressions fed to the constexpr mechanism are calls to
4896 constructors, which have type void. In that case, return the type being
4897 initialized by the constructor. */
4898
4899 static tree
4900 initialized_type (tree t)
4901 {
4902 if (TYPE_P (t))
4903 return t;
4904 tree type = TREE_TYPE (t);
4905 if (TREE_CODE (t) == CALL_EXPR)
4906 {
4907 /* A constructor call has void type, so we need to look deeper. */
4908 tree fn = get_function_named_in_call (t);
4909 if (fn && TREE_CODE (fn) == FUNCTION_DECL
4910 && DECL_CXX_CONSTRUCTOR_P (fn))
4911 type = DECL_CONTEXT (fn);
4912 }
4913 else if (TREE_CODE (t) == COMPOUND_EXPR)
4914 return initialized_type (TREE_OPERAND (t, 1));
4915 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
4916 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
4917 return cv_unqualified (type);
4918 }
4919
4920 /* We're about to initialize element INDEX of an array or class from VALUE.
4921 Set up NEW_CTX appropriately by adjusting .object to refer to the
4922 subobject and creating a new CONSTRUCTOR if the element is itself
4923 a class or array. */
4924
4925 static void
4926 init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
4927 tree index, tree &value)
4928 {
4929 new_ctx = *ctx;
4930
4931 if (index && TREE_CODE (index) != INTEGER_CST
4932 && TREE_CODE (index) != FIELD_DECL
4933 && TREE_CODE (index) != RANGE_EXPR)
4934 /* This won't have an element in the new CONSTRUCTOR. */
4935 return;
4936
4937 tree type = initialized_type (value);
4938 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
4939 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
4940 return;
4941 if (VECTOR_TYPE_P (type)
4942 && VECTOR_TYPE_P (TREE_TYPE (ctx->ctor))
4943 && index == NULL_TREE)
4944 /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
4945 vector is constructed from smaller vectors, doesn't get its own
4946 CONSTRUCTOR either. */
4947 return;
4948
4949 /* The sub-aggregate initializer might contain a placeholder;
4950 update object to refer to the subobject and ctor to refer to
4951 the (newly created) sub-initializer. */
4952 if (ctx->object)
4953 {
4954 if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
4955 /* There's no well-defined subobject for this index. */
4956 new_ctx.object = NULL_TREE;
4957 else
4958 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
4959 }
4960
4961 if (is_empty_class (type))
4962 /* Leave ctor null for an empty subobject, they aren't represented in the
4963 result of evaluation. */
4964 new_ctx.ctor = NULL_TREE;
4965 else
4966 {
4967 tree elt = build_constructor (type, NULL);
4968 CONSTRUCTOR_NO_CLEARING (elt) = true;
4969 new_ctx.ctor = elt;
4970 }
4971
4972 if (TREE_CODE (value) == TARGET_EXPR)
4973 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
4974 value = TARGET_EXPR_INITIAL (value);
4975 }
4976
4977 /* We're about to process an initializer for a class or array TYPE. Make
4978 sure that CTX is set up appropriately. */
4979
4980 static void
4981 verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
4982 {
4983 /* We don't bother building a ctor for an empty base subobject. */
4984 if (is_empty_class (type))
4985 return;
4986
4987 /* We're in the middle of an initializer that might involve placeholders;
4988 our caller should have created a CONSTRUCTOR for us to put the
4989 initializer into. We will either return that constructor or T. */
4990 gcc_assert (ctx->ctor);
4991 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4992 (type, TREE_TYPE (ctx->ctor)));
4993 /* We used to check that ctx->ctor was empty, but that isn't the case when
4994 the object is zero-initialized before calling the constructor. */
4995 if (ctx->object)
4996 {
4997 tree otype = TREE_TYPE (ctx->object);
4998 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
4999 /* Handle flexible array members. */
5000 || (TREE_CODE (otype) == ARRAY_TYPE
5001 && TYPE_DOMAIN (otype) == NULL_TREE
5002 && TREE_CODE (type) == ARRAY_TYPE
5003 && (same_type_ignoring_top_level_qualifiers_p
5004 (TREE_TYPE (type), TREE_TYPE (otype)))));
5005 }
5006 gcc_assert (!ctx->object || !DECL_P (ctx->object)
5007 || ctx->global->get_value (ctx->object) == ctx->ctor);
5008 }
5009
5010 /* Subroutine of cxx_eval_constant_expression.
5011 The expression tree T denotes a C-style array or a C-style
5012 aggregate. Reduce it to a constant expression. */
5013
5014 static tree
5015 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
5016 value_cat lval,
5017 bool *non_constant_p, bool *overflow_p)
5018 {
5019 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
5020 bool changed = false;
5021 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
5022 tree type = TREE_TYPE (t);
5023
5024 constexpr_ctx new_ctx;
5025 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
5026 {
5027 /* We don't really need the ctx->ctor business for a PMF or
5028 vector, but it's simpler to use the same code. */
5029 new_ctx = *ctx;
5030 new_ctx.ctor = build_constructor (type, NULL);
5031 new_ctx.object = NULL_TREE;
5032 ctx = &new_ctx;
5033 };
5034 verify_ctor_sanity (ctx, type);
5035 vec<constructor_elt, va_gc> **p = nullptr;
5036 if (ctx->ctor)
5037 {
5038 p = &CONSTRUCTOR_ELTS (ctx->ctor);
5039 vec_alloc (*p, vec_safe_length (v));
5040 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
5041 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
5042 }
5043
5044 unsigned i;
5045 tree index, value;
5046 bool constant_p = true;
5047 bool side_effects_p = false;
5048 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
5049 {
5050 tree orig_value = value;
5051 /* Like in cxx_eval_store_expression, omit entries for empty fields. */
5052 bool no_slot = TREE_CODE (type) == RECORD_TYPE && is_empty_field (index);
5053 init_subob_ctx (ctx, new_ctx, index, value);
5054 int pos_hint = -1;
5055 if (new_ctx.ctor != ctx->ctor && !no_slot)
5056 {
5057 /* If we built a new CONSTRUCTOR, attach it now so that other
5058 initializers can refer to it. */
5059 constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
5060 cep->value = new_ctx.ctor;
5061 pos_hint = cep - (*p)->begin();
5062 }
5063 else if (TREE_CODE (type) == UNION_TYPE)
5064 /* Otherwise if we're constructing a non-aggregate union member, set
5065 the active union member now so that we can later detect and diagnose
5066 if its initializer attempts to activate another member. */
5067 get_or_insert_ctor_field (ctx->ctor, index);
5068 tree elt = cxx_eval_constant_expression (&new_ctx, value,
5069 lval,
5070 non_constant_p, overflow_p);
5071 /* Don't VERIFY_CONSTANT here. */
5072 if (ctx->quiet && *non_constant_p)
5073 break;
5074 if (elt != orig_value)
5075 changed = true;
5076
5077 if (!TREE_CONSTANT (elt))
5078 constant_p = false;
5079 if (TREE_SIDE_EFFECTS (elt))
5080 side_effects_p = true;
5081 if (index && TREE_CODE (index) == COMPONENT_REF)
5082 {
5083 /* This is an initialization of a vfield inside a base
5084 subaggregate that we already initialized; push this
5085 initialization into the previous initialization. */
5086 constructor_elt *inner = base_field_constructor_elt (*p, index);
5087 inner->value = elt;
5088 changed = true;
5089 }
5090 else if (no_slot)
5091 /* This is an initializer for an empty field; now that we've
5092 checked that it's constant, we can ignore it. */
5093 changed = true;
5094 else if (index
5095 && (TREE_CODE (index) == NOP_EXPR
5096 || TREE_CODE (index) == POINTER_PLUS_EXPR))
5097 {
5098 /* Old representation of empty bases. FIXME remove. */
5099 gcc_checking_assert (false);
5100 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
5101 changed = true;
5102 }
5103 else
5104 {
5105 if (TREE_CODE (type) == UNION_TYPE
5106 && (*p)->last().index != index)
5107 /* The initializer erroneously changed the active union member that
5108 we're initializing. */
5109 gcc_assert (*non_constant_p);
5110 else
5111 {
5112 /* The initializer might have mutated the underlying CONSTRUCTOR,
5113 so recompute the location of the target constructer_elt. */
5114 constructor_elt *cep
5115 = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
5116 cep->value = elt;
5117 }
5118
5119 /* Adding or replacing an element might change the ctor's flags. */
5120 TREE_CONSTANT (ctx->ctor) = constant_p;
5121 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
5122 }
5123 }
5124 if (*non_constant_p)
5125 return t;
5126 if (!changed)
5127 {
5128 if (VECTOR_TYPE_P (type))
5129 t = fold (t);
5130 return t;
5131 }
5132 t = ctx->ctor;
5133 if (!t)
5134 t = build_constructor (type, NULL);
5135 /* We're done building this CONSTRUCTOR, so now we can interpret an
5136 element without an explicit initializer as value-initialized. */
5137 CONSTRUCTOR_NO_CLEARING (t) = false;
5138 TREE_CONSTANT (t) = constant_p;
5139 TREE_SIDE_EFFECTS (t) = side_effects_p;
5140 if (VECTOR_TYPE_P (type))
5141 t = fold (t);
5142 return t;
5143 }
5144
5145 /* Subroutine of cxx_eval_constant_expression.
5146 The expression tree T is a VEC_INIT_EXPR which denotes the desired
5147 initialization of a non-static data member of array type. Reduce it to a
5148 CONSTRUCTOR.
5149
5150 Note that apart from value-initialization (when VALUE_INIT is true),
5151 this is only intended to support value-initialization and the
5152 initializations done by defaulted constructors for classes with
5153 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
5154 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
5155 for the copy/move constructor. */
5156
5157 static tree
5158 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
5159 bool value_init, value_cat lval,
5160 bool *non_constant_p, bool *overflow_p)
5161 {
5162 tree elttype = TREE_TYPE (atype);
5163 verify_ctor_sanity (ctx, atype);
5164 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
5165 bool pre_init = false;
5166 unsigned HOST_WIDE_INT i;
5167 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5168
5169 if (init && TREE_CODE (init) == CONSTRUCTOR)
5170 return cxx_eval_bare_aggregate (ctx, init, lval,
5171 non_constant_p, overflow_p);
5172
5173 /* For the default constructor, build up a call to the default
5174 constructor of the element type. We only need to handle class types
5175 here, as for a constructor to be constexpr, all members must be
5176 initialized, which for a defaulted default constructor means they must
5177 be of a class type with a constexpr default constructor. */
5178 if (TREE_CODE (elttype) == ARRAY_TYPE)
5179 /* We only do this at the lowest level. */;
5180 else if (value_init)
5181 {
5182 init = build_value_init (elttype, complain);
5183 pre_init = true;
5184 }
5185 else if (!init)
5186 {
5187 releasing_vec argvec;
5188 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
5189 &argvec, elttype, LOOKUP_NORMAL,
5190 complain);
5191 init = build_aggr_init_expr (elttype, init);
5192 pre_init = true;
5193 }
5194
5195 bool zeroed_out = false;
5196 if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
5197 {
5198 /* We're initializing an array object that had been zero-initialized
5199 earlier. Truncate ctx->ctor, and propagate its zeroed state by
5200 clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
5201 initializers we append to it. */
5202 gcc_checking_assert (initializer_zerop (ctx->ctor));
5203 zeroed_out = true;
5204 vec_safe_truncate (*p, 0);
5205 }
5206
5207 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
5208 overflow_p);
5209 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
5210 for (i = 0; i < max; ++i)
5211 {
5212 tree idx = build_int_cst (size_type_node, i);
5213 tree eltinit;
5214 bool reuse = false;
5215 constexpr_ctx new_ctx;
5216 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
5217 if (new_ctx.ctor != ctx->ctor)
5218 {
5219 if (zeroed_out)
5220 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
5221 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
5222 }
5223 if (TREE_CODE (elttype) == ARRAY_TYPE)
5224 {
5225 /* A multidimensional array; recurse. */
5226 if (value_init || init == NULL_TREE)
5227 {
5228 eltinit = NULL_TREE;
5229 reuse = i == 0;
5230 }
5231 else
5232 eltinit = cp_build_array_ref (input_location, init, idx, complain);
5233 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
5234 lval,
5235 non_constant_p, overflow_p);
5236 }
5237 else if (pre_init)
5238 {
5239 /* Initializing an element using value or default initialization
5240 we just pre-built above. */
5241 if (init == void_node)
5242 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
5243 return ctx->ctor;
5244 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
5245 non_constant_p, overflow_p);
5246 reuse = i == 0;
5247 }
5248 else
5249 {
5250 /* Copying an element. */
5251 gcc_assert (same_type_ignoring_top_level_qualifiers_p
5252 (atype, TREE_TYPE (init)));
5253 eltinit = cp_build_array_ref (input_location, init, idx, complain);
5254 if (!lvalue_p (init))
5255 eltinit = move (eltinit);
5256 eltinit = force_rvalue (eltinit, complain);
5257 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
5258 non_constant_p, overflow_p);
5259 }
5260 if (*non_constant_p)
5261 break;
5262 if (new_ctx.ctor != ctx->ctor)
5263 {
5264 /* We appended this element above; update the value. */
5265 gcc_assert ((*p)->last().index == idx);
5266 (*p)->last().value = eltinit;
5267 }
5268 else
5269 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
5270 /* Reuse the result of cxx_eval_constant_expression call
5271 from the first iteration to all others if it is a constant
5272 initializer that doesn't require relocations. */
5273 if (reuse
5274 && max > 1
5275 && (eltinit == NULL_TREE
5276 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
5277 == null_pointer_node)))
5278 {
5279 if (new_ctx.ctor != ctx->ctor)
5280 eltinit = new_ctx.ctor;
5281 tree range = build2 (RANGE_EXPR, size_type_node,
5282 build_int_cst (size_type_node, 1),
5283 build_int_cst (size_type_node, max - 1));
5284 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
5285 break;
5286 }
5287 else if (i == 0)
5288 vec_safe_reserve (*p, max);
5289 }
5290
5291 if (!*non_constant_p)
5292 {
5293 init = ctx->ctor;
5294 CONSTRUCTOR_NO_CLEARING (init) = false;
5295 }
5296 return init;
5297 }
5298
5299 static tree
5300 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
5301 value_cat lval,
5302 bool *non_constant_p, bool *overflow_p)
5303 {
5304 tree atype = TREE_TYPE (t);
5305 tree init = VEC_INIT_EXPR_INIT (t);
5306 bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
5307 if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
5308 ;
5309 else if (CONSTRUCTOR_NELTS (init) == 0
5310 && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
5311 {
5312 /* Handle {} as value-init. */
5313 init = NULL_TREE;
5314 value_init = true;
5315 }
5316 else
5317 {
5318 /* This is a more complicated case, like needing to loop over trailing
5319 elements; call build_vec_init and evaluate the result. */
5320 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
5321 constexpr_ctx new_ctx = *ctx;
5322 if (!ctx->object)
5323 {
5324 /* We want to have an initialization target for an VEC_INIT_EXPR.
5325 If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
5326 new_ctx.object = VEC_INIT_EXPR_SLOT (t);
5327 tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
5328 CONSTRUCTOR_NO_CLEARING (ctor) = true;
5329 ctx->global->put_value (new_ctx.object, ctor);
5330 ctx = &new_ctx;
5331 }
5332 init = expand_vec_init_expr (ctx->object, t, complain);
5333 return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
5334 overflow_p);
5335 }
5336 tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
5337 lval, non_constant_p, overflow_p);
5338 if (*non_constant_p)
5339 return t;
5340 else
5341 return r;
5342 }
5343
5344 /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
5345 where the desired type is an array of unknown bounds because the variable
5346 has had its bounds deduced since the wrapping expression was created. */
5347
5348 static bool
5349 same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
5350 {
5351 while (TREE_CODE (type1) == ARRAY_TYPE
5352 && TREE_CODE (type2) == ARRAY_TYPE
5353 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
5354 {
5355 type1 = TREE_TYPE (type1);
5356 type2 = TREE_TYPE (type2);
5357 }
5358 return same_type_ignoring_top_level_qualifiers_p (type1, type2);
5359 }
5360
5361 /* Try to determine the currently active union member for an expression
5362 with UNION_TYPE. If it can be determined, return the FIELD_DECL,
5363 otherwise return NULL_TREE. */
5364
5365 static tree
5366 cxx_union_active_member (const constexpr_ctx *ctx, tree t)
5367 {
5368 constexpr_ctx new_ctx = *ctx;
5369 new_ctx.quiet = true;
5370 bool non_constant_p = false, overflow_p = false;
5371 tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
5372 &non_constant_p,
5373 &overflow_p);
5374 if (TREE_CODE (ctor) == CONSTRUCTOR
5375 && CONSTRUCTOR_NELTS (ctor) == 1
5376 && CONSTRUCTOR_ELT (ctor, 0)->index
5377 && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
5378 return CONSTRUCTOR_ELT (ctor, 0)->index;
5379 return NULL_TREE;
5380 }
5381
5382 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
5383
5384 static tree
5385 cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
5386 tree op, unsigned HOST_WIDE_INT off, bool *empty_base)
5387 {
5388 tree optype = TREE_TYPE (op);
5389 unsigned HOST_WIDE_INT const_nunits;
5390 if (off == 0 && similar_type_p (optype, type))
5391 return op;
5392 else if (TREE_CODE (optype) == COMPLEX_TYPE
5393 && similar_type_p (type, TREE_TYPE (optype)))
5394 {
5395 /* *(foo *)&complexfoo => __real__ complexfoo */
5396 if (off == 0)
5397 return build1_loc (loc, REALPART_EXPR, type, op);
5398 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
5399 else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
5400 return build1_loc (loc, IMAGPART_EXPR, type, op);
5401 }
5402 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
5403 else if (VECTOR_TYPE_P (optype)
5404 && similar_type_p (type, TREE_TYPE (optype))
5405 && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
5406 {
5407 unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5408 unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
5409 if (off < max_offset && off % part_width == 0)
5410 {
5411 tree index = bitsize_int (off * BITS_PER_UNIT);
5412 return build3_loc (loc, BIT_FIELD_REF, type, op,
5413 TYPE_SIZE (type), index);
5414 }
5415 }
5416 /* ((foo *)&fooarray)[x] => fooarray[x] */
5417 else if (TREE_CODE (optype) == ARRAY_TYPE
5418 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
5419 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
5420 {
5421 tree type_domain = TYPE_DOMAIN (optype);
5422 tree min_val = size_zero_node;
5423 if (type_domain && TYPE_MIN_VALUE (type_domain))
5424 min_val = TYPE_MIN_VALUE (type_domain);
5425 unsigned HOST_WIDE_INT el_sz
5426 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
5427 unsigned HOST_WIDE_INT idx = off / el_sz;
5428 unsigned HOST_WIDE_INT rem = off % el_sz;
5429 if (tree_fits_uhwi_p (min_val))
5430 {
5431 tree index = size_int (idx + tree_to_uhwi (min_val));
5432 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
5433 NULL_TREE, NULL_TREE);
5434 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
5435 empty_base);
5436 }
5437 }
5438 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
5439 else if (TREE_CODE (optype) == RECORD_TYPE
5440 || TREE_CODE (optype) == UNION_TYPE)
5441 {
5442 if (TREE_CODE (optype) == UNION_TYPE)
5443 /* For unions prefer the currently active member. */
5444 if (tree field = cxx_union_active_member (ctx, op))
5445 {
5446 unsigned HOST_WIDE_INT el_sz
5447 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5448 if (off < el_sz)
5449 {
5450 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5451 op, field, NULL_TREE);
5452 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5453 off, empty_base))
5454 return ret;
5455 }
5456 }
5457 for (tree field = TYPE_FIELDS (optype);
5458 field; field = DECL_CHAIN (field))
5459 if (TREE_CODE (field) == FIELD_DECL
5460 && TREE_TYPE (field) != error_mark_node
5461 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
5462 {
5463 tree pos = byte_position (field);
5464 if (!tree_fits_uhwi_p (pos))
5465 continue;
5466 unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
5467 unsigned HOST_WIDE_INT el_sz
5468 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
5469 if (upos <= off && off < upos + el_sz)
5470 {
5471 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
5472 op, field, NULL_TREE);
5473 if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
5474 off - upos,
5475 empty_base))
5476 return ret;
5477 }
5478 }
5479 /* Also handle conversion to an empty base class, which
5480 is represented with a NOP_EXPR. */
5481 if (is_empty_class (type)
5482 && CLASS_TYPE_P (optype)
5483 && DERIVED_FROM_P (type, optype))
5484 {
5485 if (empty_base)
5486 *empty_base = true;
5487 return op;
5488 }
5489 }
5490
5491 return NULL_TREE;
5492 }
5493
5494 /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
5495 match. We want to be less strict for simple *& folding; if we have a
5496 non-const temporary that we access through a const pointer, that should
5497 work. We handle this here rather than change fold_indirect_ref_1
5498 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
5499 don't really make sense outside of constant expression evaluation. Also
5500 we want to allow folding to COMPONENT_REF, which could cause trouble
5501 with TBAA in fold_indirect_ref_1. */
5502
5503 static tree
5504 cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
5505 tree op0, bool *empty_base /* = NULL*/)
5506 {
5507 tree sub = op0;
5508 tree subtype;
5509 poly_uint64 const_op01;
5510
5511 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
5512 while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
5513 || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
5514 {
5515 if (TREE_CODE (sub) == NOP_EXPR
5516 && REINTERPRET_CAST_P (sub))
5517 return NULL_TREE;
5518 sub = TREE_OPERAND (sub, 0);
5519 }
5520
5521 subtype = TREE_TYPE (sub);
5522 if (!INDIRECT_TYPE_P (subtype))
5523 return NULL_TREE;
5524
5525 /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
5526 the innermost component into the offset until it would make the
5527 offset positive, so that cxx_fold_indirect_ref_1 can identify
5528 more folding opportunities. */
5529 auto canonicalize_obj_off = [] (tree& obj, tree& off) {
5530 while (TREE_CODE (obj) == COMPONENT_REF
5531 && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
5532 {
5533 tree field = TREE_OPERAND (obj, 1);
5534 tree pos = byte_position (field);
5535 if (integer_zerop (off) && integer_nonzerop (pos))
5536 /* If the offset is already 0, keep going as long as the
5537 component is at position 0. */
5538 break;
5539 off = int_const_binop (PLUS_EXPR, off, pos);
5540 obj = TREE_OPERAND (obj, 0);
5541 }
5542 };
5543
5544 if (TREE_CODE (sub) == ADDR_EXPR)
5545 {
5546 tree op = TREE_OPERAND (sub, 0);
5547 tree optype = TREE_TYPE (op);
5548
5549 /* *&CONST_DECL -> to the value of the const decl. */
5550 if (TREE_CODE (op) == CONST_DECL)
5551 return DECL_INITIAL (op);
5552 /* *&p => p; make sure to handle *&"str"[cst] here. */
5553 if (similar_type_p (optype, type))
5554 {
5555 tree fop = fold_read_from_constant_string (op);
5556 if (fop)
5557 return fop;
5558 else
5559 return op;
5560 }
5561 else
5562 {
5563 tree off = integer_zero_node;
5564 canonicalize_obj_off (op, off);
5565 gcc_assert (integer_zerop (off));
5566 return cxx_fold_indirect_ref_1 (ctx, loc, type, op, 0, empty_base);
5567 }
5568 }
5569 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
5570 && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
5571 {
5572 tree op00 = TREE_OPERAND (sub, 0);
5573 tree off = TREE_OPERAND (sub, 1);
5574
5575 STRIP_NOPS (op00);
5576 if (TREE_CODE (op00) == ADDR_EXPR)
5577 {
5578 tree obj = TREE_OPERAND (op00, 0);
5579 canonicalize_obj_off (obj, off);
5580 return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
5581 tree_to_uhwi (off), empty_base);
5582 }
5583 }
5584 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
5585 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
5586 && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
5587 {
5588 tree type_domain;
5589 tree min_val = size_zero_node;
5590 tree newsub
5591 = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL);
5592 if (newsub)
5593 sub = newsub;
5594 else
5595 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
5596 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
5597 if (type_domain && TYPE_MIN_VALUE (type_domain))
5598 min_val = TYPE_MIN_VALUE (type_domain);
5599 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
5600 NULL_TREE);
5601 }
5602
5603 return NULL_TREE;
5604 }
5605
5606 static tree
5607 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
5608 value_cat lval,
5609 bool *non_constant_p, bool *overflow_p)
5610 {
5611 tree orig_op0 = TREE_OPERAND (t, 0);
5612 bool empty_base = false;
5613
5614 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
5615 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
5616
5617 if (TREE_CODE (t) == MEM_REF
5618 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
5619 {
5620 gcc_assert (ctx->quiet);
5621 *non_constant_p = true;
5622 return t;
5623 }
5624
5625 /* First try to simplify it directly. */
5626 tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
5627 orig_op0, &empty_base);
5628 if (!r)
5629 {
5630 /* If that didn't work, evaluate the operand first. */
5631 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
5632 vc_prvalue, non_constant_p,
5633 overflow_p);
5634 /* Don't VERIFY_CONSTANT here. */
5635 if (*non_constant_p)
5636 return t;
5637
5638 if (!lval && integer_zerop (op0))
5639 {
5640 if (!ctx->quiet)
5641 error ("dereferencing a null pointer");
5642 *non_constant_p = true;
5643 return t;
5644 }
5645
5646 r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
5647 &empty_base);
5648 if (r == NULL_TREE)
5649 {
5650 /* We couldn't fold to a constant value. Make sure it's not
5651 something we should have been able to fold. */
5652 tree sub = op0;
5653 STRIP_NOPS (sub);
5654 if (TREE_CODE (sub) == ADDR_EXPR)
5655 {
5656 gcc_assert (!similar_type_p
5657 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
5658 /* DR 1188 says we don't have to deal with this. */
5659 if (!ctx->quiet)
5660 error_at (cp_expr_loc_or_input_loc (t),
5661 "accessing value of %qE through a %qT glvalue in a "
5662 "constant expression", build_fold_indirect_ref (sub),
5663 TREE_TYPE (t));
5664 *non_constant_p = true;
5665 return t;
5666 }
5667
5668 if (lval == vc_glvalue && op0 != orig_op0)
5669 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
5670 if (!lval)
5671 VERIFY_CONSTANT (t);
5672 return t;
5673 }
5674 }
5675
5676 r = cxx_eval_constant_expression (ctx, r,
5677 lval, non_constant_p, overflow_p);
5678 if (*non_constant_p)
5679 return t;
5680
5681 /* If we're pulling out the value of an empty base, just return an empty
5682 CONSTRUCTOR. */
5683 if (empty_base && !lval)
5684 {
5685 r = build_constructor (TREE_TYPE (t), NULL);
5686 TREE_CONSTANT (r) = true;
5687 }
5688
5689 return r;
5690 }
5691
5692 /* Complain about R, a VAR_DECL, not being usable in a constant expression.
5693 FUNDEF_P is true if we're checking a constexpr function body.
5694 Shared between potential_constant_expression and
5695 cxx_eval_constant_expression. */
5696
5697 static void
5698 non_const_var_error (location_t loc, tree r, bool fundef_p)
5699 {
5700 auto_diagnostic_group d;
5701 tree type = TREE_TYPE (r);
5702 if (DECL_NAME (r) == heap_uninit_identifier
5703 || DECL_NAME (r) == heap_identifier
5704 || DECL_NAME (r) == heap_vec_uninit_identifier
5705 || DECL_NAME (r) == heap_vec_identifier)
5706 {
5707 if (constexpr_error (loc, fundef_p, "the content of uninitialized "
5708 "storage is not usable in a constant expression"))
5709 inform (DECL_SOURCE_LOCATION (r), "allocated here");
5710 return;
5711 }
5712 if (DECL_NAME (r) == heap_deleted_identifier)
5713 {
5714 if (constexpr_error (loc, fundef_p, "use of allocated storage after "
5715 "deallocation in a constant expression"))
5716 inform (DECL_SOURCE_LOCATION (r), "allocated here");
5717 return;
5718 }
5719 if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
5720 "a constant expression", r))
5721 return;
5722 /* Avoid error cascade. */
5723 if (DECL_INITIAL (r) == error_mark_node)
5724 return;
5725 if (DECL_DECLARED_CONSTEXPR_P (r))
5726 inform (DECL_SOURCE_LOCATION (r),
5727 "%qD used in its own initializer", r);
5728 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
5729 {
5730 if (!CP_TYPE_CONST_P (type))
5731 inform (DECL_SOURCE_LOCATION (r),
5732 "%q#D is not const", r);
5733 else if (CP_TYPE_VOLATILE_P (type))
5734 inform (DECL_SOURCE_LOCATION (r),
5735 "%q#D is volatile", r);
5736 else if (!DECL_INITIAL (r)
5737 || !TREE_CONSTANT (DECL_INITIAL (r))
5738 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
5739 inform (DECL_SOURCE_LOCATION (r),
5740 "%qD was not initialized with a constant "
5741 "expression", r);
5742 else
5743 gcc_unreachable ();
5744 }
5745 else if (TYPE_REF_P (type))
5746 inform (DECL_SOURCE_LOCATION (r),
5747 "%qD was not initialized with a constant "
5748 "expression", r);
5749 else
5750 {
5751 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
5752 inform (DECL_SOURCE_LOCATION (r),
5753 "%qD was not declared %<constexpr%>", r);
5754 else
5755 inform (DECL_SOURCE_LOCATION (r),
5756 "%qD does not have integral or enumeration type",
5757 r);
5758 }
5759 }
5760
5761 /* Subroutine of cxx_eval_constant_expression.
5762 Like cxx_eval_unary_expression, except for trinary expressions. */
5763
5764 static tree
5765 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
5766 value_cat lval,
5767 bool *non_constant_p, bool *overflow_p)
5768 {
5769 int i;
5770 tree args[3];
5771 tree val;
5772
5773 for (i = 0; i < 3; i++)
5774 {
5775 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
5776 lval,
5777 non_constant_p, overflow_p);
5778 VERIFY_CONSTANT (args[i]);
5779 }
5780
5781 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
5782 args[0], args[1], args[2]);
5783 if (val == NULL_TREE)
5784 return t;
5785 VERIFY_CONSTANT (val);
5786 return val;
5787 }
5788
5789 /* True if T was declared in a function declared to be constexpr, and
5790 therefore potentially constant in C++14. */
5791
5792 bool
5793 var_in_constexpr_fn (tree t)
5794 {
5795 tree ctx = DECL_CONTEXT (t);
5796 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
5797 && DECL_DECLARED_CONSTEXPR_P (ctx));
5798 }
5799
5800 /* True if a function might be constexpr: either a function that was
5801 declared constexpr, or a C++17 lambda op(). */
5802
5803 bool
5804 maybe_constexpr_fn (tree t)
5805 {
5806 return (DECL_DECLARED_CONSTEXPR_P (t)
5807 || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
5808 || (flag_implicit_constexpr
5809 && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
5810 }
5811
5812 /* True if T was declared in a function that might be constexpr: either a
5813 function that was declared constexpr, or a C++17 lambda op(). */
5814
5815 bool
5816 var_in_maybe_constexpr_fn (tree t)
5817 {
5818 return (DECL_FUNCTION_SCOPE_P (t)
5819 && maybe_constexpr_fn (DECL_CONTEXT (t)));
5820 }
5821
5822 /* We're assigning INIT to TARGET. In do_build_copy_constructor and
5823 build_over_call we implement trivial copy of a class with tail padding using
5824 assignment of character arrays, which is valid in normal code, but not in
5825 constexpr evaluation. We don't need to worry about clobbering tail padding
5826 in constexpr evaluation, so strip the type punning. */
5827
5828 static void
5829 maybe_simplify_trivial_copy (tree &target, tree &init)
5830 {
5831 if (TREE_CODE (target) == MEM_REF
5832 && TREE_CODE (init) == MEM_REF
5833 && TREE_TYPE (target) == TREE_TYPE (init)
5834 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
5835 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
5836 {
5837 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
5838 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
5839 }
5840 }
5841
5842 /* Returns true if REF, which is a COMPONENT_REF, has any fields
5843 of constant type. This does not check for 'mutable', so the
5844 caller is expected to be mindful of that. */
5845
5846 static bool
5847 cref_has_const_field (tree ref)
5848 {
5849 while (TREE_CODE (ref) == COMPONENT_REF)
5850 {
5851 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
5852 return true;
5853 ref = TREE_OPERAND (ref, 0);
5854 }
5855 return false;
5856 }
5857
5858 /* Return true if we are modifying something that is const during constant
5859 expression evaluation. CODE is the code of the statement, OBJ is the
5860 object in question, MUTABLE_P is true if one of the subobjects were
5861 declared mutable. */
5862
5863 static bool
5864 modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
5865 {
5866 /* If this is initialization, there's no problem. */
5867 if (code != MODIFY_EXPR)
5868 return false;
5869
5870 /* [basic.type.qualifier] "A const object is an object of type
5871 const T or a non-mutable subobject of a const object." */
5872 if (mutable_p)
5873 return false;
5874
5875 if (TREE_READONLY (obj))
5876 return true;
5877
5878 if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
5879 {
5880 /* Although a COMPONENT_REF may have a const type, we should
5881 only consider it modifying a const object when any of the
5882 field components is const. This can happen when using
5883 constructs such as const_cast<const T &>(m), making something
5884 const even though it wasn't declared const. */
5885 if (TREE_CODE (obj) == COMPONENT_REF)
5886 return cref_has_const_field (obj);
5887 else
5888 return true;
5889 }
5890
5891 return false;
5892 }
5893
5894 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
5895
5896 static tree
5897 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
5898 value_cat lval,
5899 bool *non_constant_p, bool *overflow_p)
5900 {
5901 constexpr_ctx new_ctx = *ctx;
5902
5903 tree init = TREE_OPERAND (t, 1);
5904 if (TREE_CLOBBER_P (init))
5905 /* Just ignore clobbers. */
5906 return void_node;
5907
5908 /* First we figure out where we're storing to. */
5909 tree target = TREE_OPERAND (t, 0);
5910
5911 maybe_simplify_trivial_copy (target, init);
5912
5913 tree type = TREE_TYPE (target);
5914 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
5915 if (preeval)
5916 {
5917 /* Evaluate the value to be stored without knowing what object it will be
5918 stored in, so that any side-effects happen first. */
5919 if (!SCALAR_TYPE_P (type))
5920 new_ctx.ctor = new_ctx.object = NULL_TREE;
5921 init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
5922 non_constant_p, overflow_p);
5923 if (*non_constant_p)
5924 return t;
5925 }
5926
5927 bool evaluated = false;
5928 if (lval == vc_glvalue)
5929 {
5930 /* If we want to return a reference to the target, we need to evaluate it
5931 as a whole; otherwise, only evaluate the innermost piece to avoid
5932 building up unnecessary *_REFs. */
5933 target = cxx_eval_constant_expression (ctx, target, lval,
5934 non_constant_p, overflow_p);
5935 evaluated = true;
5936 if (*non_constant_p)
5937 return t;
5938 }
5939
5940 /* Find the underlying variable. */
5941 releasing_vec refs;
5942 tree object = NULL_TREE;
5943 /* If we're modifying a const object, save it. */
5944 tree const_object_being_modified = NULL_TREE;
5945 bool mutable_p = false;
5946 for (tree probe = target; object == NULL_TREE; )
5947 {
5948 switch (TREE_CODE (probe))
5949 {
5950 case BIT_FIELD_REF:
5951 case COMPONENT_REF:
5952 case ARRAY_REF:
5953 {
5954 tree ob = TREE_OPERAND (probe, 0);
5955 tree elt = TREE_OPERAND (probe, 1);
5956 if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
5957 mutable_p = true;
5958 if (TREE_CODE (probe) == ARRAY_REF)
5959 {
5960 elt = eval_and_check_array_index (ctx, probe, false,
5961 non_constant_p, overflow_p);
5962 if (*non_constant_p)
5963 return t;
5964 }
5965 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
5966 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
5967 the array isn't const. Instead, check "a" in the next iteration;
5968 that will detect modifying "const int a[10]". */
5969 else if (evaluated
5970 && modifying_const_object_p (TREE_CODE (t), probe,
5971 mutable_p)
5972 && const_object_being_modified == NULL_TREE)
5973 const_object_being_modified = probe;
5974 vec_safe_push (refs, elt);
5975 vec_safe_push (refs, TREE_TYPE (probe));
5976 probe = ob;
5977 }
5978 break;
5979
5980 case REALPART_EXPR:
5981 gcc_assert (probe == target);
5982 vec_safe_push (refs, probe);
5983 vec_safe_push (refs, TREE_TYPE (probe));
5984 probe = TREE_OPERAND (probe, 0);
5985 break;
5986
5987 case IMAGPART_EXPR:
5988 gcc_assert (probe == target);
5989 vec_safe_push (refs, probe);
5990 vec_safe_push (refs, TREE_TYPE (probe));
5991 probe = TREE_OPERAND (probe, 0);
5992 break;
5993
5994 default:
5995 if (evaluated)
5996 object = probe;
5997 else
5998 {
5999 probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
6000 non_constant_p, overflow_p);
6001 evaluated = true;
6002 if (*non_constant_p)
6003 return t;
6004 }
6005 break;
6006 }
6007 }
6008
6009 if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
6010 && const_object_being_modified == NULL_TREE)
6011 const_object_being_modified = object;
6012
6013 /* And then find/build up our initializer for the path to the subobject
6014 we're initializing. */
6015 tree *valp;
6016 if (DECL_P (object))
6017 valp = ctx->global->get_value_ptr (object);
6018 else
6019 valp = NULL;
6020 if (!valp)
6021 {
6022 /* A constant-expression cannot modify objects from outside the
6023 constant-expression. */
6024 if (!ctx->quiet)
6025 error ("modification of %qE is not a constant expression", object);
6026 *non_constant_p = true;
6027 return t;
6028 }
6029 type = TREE_TYPE (object);
6030 bool no_zero_init = true;
6031
6032 auto_vec<tree *> ctors;
6033 releasing_vec indexes;
6034 auto_vec<int> index_pos_hints;
6035 bool activated_union_member_p = false;
6036 bool empty_base = false;
6037 while (!refs->is_empty ())
6038 {
6039 if (*valp == NULL_TREE)
6040 {
6041 *valp = build_constructor (type, NULL);
6042 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6043 }
6044 else if (TREE_CODE (*valp) == STRING_CST)
6045 {
6046 /* An array was initialized with a string constant, and now
6047 we're writing into one of its elements. Explode the
6048 single initialization into a set of element
6049 initializations. */
6050 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
6051
6052 tree string = *valp;
6053 tree elt_type = TREE_TYPE (type);
6054 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
6055 / TYPE_PRECISION (char_type_node));
6056 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
6057 tree ary_ctor = build_constructor (type, NULL);
6058
6059 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
6060 for (unsigned ix = 0; ix != num_elts; ix++)
6061 {
6062 constructor_elt elt =
6063 {
6064 build_int_cst (size_type_node, ix),
6065 extract_string_elt (string, chars_per_elt, ix)
6066 };
6067 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
6068 }
6069
6070 *valp = ary_ctor;
6071 }
6072
6073 enum tree_code code = TREE_CODE (type);
6074 tree reftype = refs->pop();
6075 tree index = refs->pop();
6076
6077 if (code == COMPLEX_TYPE)
6078 {
6079 if (TREE_CODE (*valp) == COMPLEX_CST)
6080 *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
6081 TREE_IMAGPART (*valp));
6082 else if (TREE_CODE (*valp) == CONSTRUCTOR
6083 && CONSTRUCTOR_NELTS (*valp) == 0
6084 && CONSTRUCTOR_NO_CLEARING (*valp))
6085 {
6086 tree r = build_constructor (reftype, NULL);
6087 CONSTRUCTOR_NO_CLEARING (r) = 1;
6088 *valp = build2 (COMPLEX_EXPR, type, r, r);
6089 }
6090 gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
6091 ctors.safe_push (valp);
6092 vec_safe_push (indexes, index);
6093 valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
6094 gcc_checking_assert (refs->is_empty ());
6095 type = reftype;
6096 break;
6097 }
6098
6099 /* If the value of object is already zero-initialized, any new ctors for
6100 subobjects will also be zero-initialized. */
6101 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
6102
6103 if (code == RECORD_TYPE && is_empty_field (index))
6104 /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
6105 have no data and might have an offset lower than previously declared
6106 fields, which confuses the middle-end. The code below will notice
6107 that we don't have a CONSTRUCTOR for our inner target and just
6108 return init. */
6109 {
6110 empty_base = true;
6111 break;
6112 }
6113
6114 type = reftype;
6115
6116 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
6117 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
6118 {
6119 if (cxx_dialect < cxx20)
6120 {
6121 if (!ctx->quiet)
6122 error_at (cp_expr_loc_or_input_loc (t),
6123 "change of the active member of a union "
6124 "from %qD to %qD",
6125 CONSTRUCTOR_ELT (*valp, 0)->index,
6126 index);
6127 *non_constant_p = true;
6128 }
6129 else if (TREE_CODE (t) == MODIFY_EXPR
6130 && CONSTRUCTOR_NO_CLEARING (*valp))
6131 {
6132 /* Diagnose changing the active union member while the union
6133 is in the process of being initialized. */
6134 if (!ctx->quiet)
6135 error_at (cp_expr_loc_or_input_loc (t),
6136 "change of the active member of a union "
6137 "from %qD to %qD during initialization",
6138 CONSTRUCTOR_ELT (*valp, 0)->index,
6139 index);
6140 *non_constant_p = true;
6141 }
6142 no_zero_init = true;
6143 }
6144
6145 ctors.safe_push (valp);
6146 vec_safe_push (indexes, index);
6147
6148 constructor_elt *cep
6149 = get_or_insert_ctor_field (*valp, index);
6150 index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
6151
6152 if (code == UNION_TYPE)
6153 activated_union_member_p = true;
6154
6155 valp = &cep->value;
6156 }
6157
6158 /* For initialization of an empty base, the original target will be
6159 *(base*)this, evaluation of which resolves to the object
6160 argument, which has the derived type rather than the base type. */
6161 if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
6162 (initialized_type (init), type)))
6163 {
6164 gcc_assert (is_empty_class (TREE_TYPE (target)));
6165 empty_base = true;
6166 }
6167
6168 /* Detect modifying a constant object in constexpr evaluation.
6169 We have found a const object that is being modified. Figure out
6170 if we need to issue an error. Consider
6171
6172 struct A {
6173 int n;
6174 constexpr A() : n(1) { n = 2; } // #1
6175 };
6176 struct B {
6177 const A a;
6178 constexpr B() { a.n = 3; } // #2
6179 };
6180 constexpr B b{};
6181
6182 #1 is OK, since we're modifying an object under construction, but
6183 #2 is wrong, since "a" is const and has been fully constructed.
6184 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
6185 which means that the object is read-only. For the example above, the
6186 *ctors stack at the point of #2 will look like:
6187
6188 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
6189 ctors[1] = {.n=2} TREE_READONLY = 1
6190
6191 and we're modifying "b.a", so we search the stack and see if the
6192 constructor for "b.a" has already run. */
6193 if (const_object_being_modified)
6194 {
6195 bool fail = false;
6196 tree const_objtype
6197 = strip_array_types (TREE_TYPE (const_object_being_modified));
6198 if (!CLASS_TYPE_P (const_objtype))
6199 fail = true;
6200 else
6201 {
6202 /* [class.ctor]p5 "A constructor can be invoked for a const,
6203 volatile, or const volatile object. const and volatile
6204 semantics are not applied on an object under construction.
6205 They come into effect when the constructor for the most
6206 derived object ends." */
6207 for (tree *elt : ctors)
6208 if (same_type_ignoring_top_level_qualifiers_p
6209 (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
6210 {
6211 fail = TREE_READONLY (*elt);
6212 break;
6213 }
6214 }
6215 if (fail)
6216 {
6217 if (!ctx->quiet)
6218 modifying_const_object_error (t, const_object_being_modified);
6219 *non_constant_p = true;
6220 return t;
6221 }
6222 }
6223
6224 if (!preeval)
6225 {
6226 /* We're handling an INIT_EXPR of class type, so the value of the
6227 initializer can depend on the object it's initializing. */
6228
6229 /* Create a new CONSTRUCTOR in case evaluation of the initializer
6230 wants to modify it. */
6231 if (*valp == NULL_TREE)
6232 {
6233 *valp = build_constructor (type, NULL);
6234 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6235 }
6236 new_ctx.ctor = empty_base ? NULL_TREE : *valp;
6237 new_ctx.object = target;
6238 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
6239 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
6240 expansion of those trees uses ctx instead. */
6241 if (TREE_CODE (init) == TARGET_EXPR)
6242 if (tree tinit = TARGET_EXPR_INITIAL (init))
6243 init = tinit;
6244 init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
6245 non_constant_p, overflow_p);
6246 /* The hash table might have moved since the get earlier, and the
6247 initializer might have mutated the underlying CONSTRUCTORs, so we must
6248 recompute VALP. */
6249 valp = ctx->global->get_value_ptr (object);
6250 for (unsigned i = 0; i < vec_safe_length (indexes); i++)
6251 {
6252 ctors[i] = valp;
6253 constructor_elt *cep
6254 = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
6255 valp = &cep->value;
6256 }
6257 }
6258
6259 if (*non_constant_p)
6260 return t;
6261
6262 /* Don't share a CONSTRUCTOR that might be changed later. */
6263 init = unshare_constructor (init);
6264
6265 gcc_checking_assert (!*valp || (same_type_ignoring_top_level_qualifiers_p
6266 (TREE_TYPE (*valp), type)));
6267 if (empty_base)
6268 {
6269 /* Just evaluate the initializer and return, since there's no actual data
6270 to store, and we didn't build a CONSTRUCTOR. */
6271 if (!*valp)
6272 {
6273 /* But do make sure we have something in *valp. */
6274 *valp = build_constructor (type, nullptr);
6275 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
6276 }
6277 }
6278 else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
6279 && TREE_CODE (init) == CONSTRUCTOR)
6280 {
6281 /* An outer ctx->ctor might be pointing to *valp, so replace
6282 its contents. */
6283 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
6284 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
6285 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
6286 CONSTRUCTOR_NO_CLEARING (*valp)
6287 = CONSTRUCTOR_NO_CLEARING (init);
6288 }
6289 else
6290 *valp = init;
6291
6292 /* After initialization, 'const' semantics apply to the value of the
6293 object. Make a note of this fact by marking the CONSTRUCTOR
6294 TREE_READONLY. */
6295 if (TREE_CODE (t) == INIT_EXPR
6296 && TREE_CODE (*valp) == CONSTRUCTOR
6297 && TYPE_READONLY (type))
6298 {
6299 if (INDIRECT_REF_P (target)
6300 && (is_this_parameter
6301 (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
6302 /* We've just initialized '*this' (perhaps via the target
6303 constructor of a delegating constructor). Leave it up to the
6304 caller that set 'this' to set TREE_READONLY appropriately. */
6305 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
6306 (TREE_TYPE (target), type) || empty_base);
6307 else
6308 TREE_READONLY (*valp) = true;
6309 }
6310
6311 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
6312 CONSTRUCTORs, if any. */
6313 bool c = TREE_CONSTANT (init);
6314 bool s = TREE_SIDE_EFFECTS (init);
6315 if (!indexes->is_empty ())
6316 {
6317 tree last = indexes->last ();
6318 if (TREE_CODE (last) == REALPART_EXPR
6319 || TREE_CODE (last) == IMAGPART_EXPR)
6320 {
6321 /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
6322 possible. */
6323 tree *cexpr = ctors.last ();
6324 if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
6325 TREE_OPERAND (*cexpr, 0),
6326 TREE_OPERAND (*cexpr, 1)))
6327 *cexpr = c;
6328 else
6329 {
6330 TREE_CONSTANT (*cexpr)
6331 = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
6332 & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
6333 TREE_SIDE_EFFECTS (*cexpr)
6334 = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
6335 | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
6336 }
6337 c = TREE_CONSTANT (*cexpr);
6338 s = TREE_SIDE_EFFECTS (*cexpr);
6339 }
6340 }
6341 if (!c || s || activated_union_member_p)
6342 for (tree *elt : ctors)
6343 {
6344 if (TREE_CODE (*elt) != CONSTRUCTOR)
6345 continue;
6346 if (!c)
6347 TREE_CONSTANT (*elt) = false;
6348 if (s)
6349 TREE_SIDE_EFFECTS (*elt) = true;
6350 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
6351 this union. */
6352 if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
6353 CONSTRUCTOR_NO_CLEARING (*elt) = false;
6354 }
6355
6356 if (lval)
6357 return target;
6358 else
6359 return init;
6360 }
6361
6362 /* Evaluate a ++ or -- expression. */
6363
6364 static tree
6365 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
6366 value_cat lval,
6367 bool *non_constant_p, bool *overflow_p)
6368 {
6369 enum tree_code code = TREE_CODE (t);
6370 tree type = TREE_TYPE (t);
6371 tree op = TREE_OPERAND (t, 0);
6372 tree offset = TREE_OPERAND (t, 1);
6373 gcc_assert (TREE_CONSTANT (offset));
6374
6375 /* OFFSET is constant, but perhaps not constant enough. We need to
6376 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
6377 offset = fold_simple (offset);
6378
6379 /* The operand as an lvalue. */
6380 op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
6381 non_constant_p, overflow_p);
6382
6383 /* The operand as an rvalue. */
6384 tree val
6385 = cxx_eval_constant_expression (ctx, op, vc_prvalue,
6386 non_constant_p, overflow_p);
6387 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
6388 a local array in a constexpr function. */
6389 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
6390 if (!ptr)
6391 VERIFY_CONSTANT (val);
6392
6393 /* The modified value. */
6394 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
6395 tree mod;
6396 if (INDIRECT_TYPE_P (type))
6397 {
6398 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
6399 offset = convert_to_ptrofftype (offset);
6400 if (!inc)
6401 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
6402 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
6403 }
6404 else if (c_promoting_integer_type_p (type)
6405 && !TYPE_UNSIGNED (type)
6406 && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
6407 {
6408 offset = fold_convert (integer_type_node, offset);
6409 mod = fold_convert (integer_type_node, val);
6410 tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
6411 mod, offset);
6412 mod = fold_convert (type, t);
6413 if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
6414 TREE_OVERFLOW (mod) = false;
6415 }
6416 else
6417 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
6418 if (!ptr)
6419 VERIFY_CONSTANT (mod);
6420
6421 /* Storing the modified value. */
6422 tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
6423 MODIFY_EXPR, type, op, mod);
6424 mod = cxx_eval_constant_expression (ctx, store, lval,
6425 non_constant_p, overflow_p);
6426 ggc_free (store);
6427 if (*non_constant_p)
6428 return t;
6429
6430 /* And the value of the expression. */
6431 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
6432 /* Prefix ops are lvalues, but the caller might want an rvalue;
6433 lval has already been taken into account in the store above. */
6434 return mod;
6435 else
6436 /* Postfix ops are rvalues. */
6437 return val;
6438 }
6439
6440 /* Predicates for the meaning of *jump_target. */
6441
6442 static bool
6443 returns (tree *jump_target)
6444 {
6445 return *jump_target
6446 && TREE_CODE (*jump_target) == RETURN_EXPR;
6447 }
6448
6449 static bool
6450 breaks (tree *jump_target)
6451 {
6452 return *jump_target
6453 && ((TREE_CODE (*jump_target) == LABEL_DECL
6454 && LABEL_DECL_BREAK (*jump_target))
6455 || TREE_CODE (*jump_target) == BREAK_STMT
6456 || TREE_CODE (*jump_target) == EXIT_EXPR);
6457 }
6458
6459 static bool
6460 continues (tree *jump_target)
6461 {
6462 return *jump_target
6463 && ((TREE_CODE (*jump_target) == LABEL_DECL
6464 && LABEL_DECL_CONTINUE (*jump_target))
6465 || TREE_CODE (*jump_target) == CONTINUE_STMT);
6466
6467 }
6468
6469 static bool
6470 switches (tree *jump_target)
6471 {
6472 return *jump_target
6473 && TREE_CODE (*jump_target) == INTEGER_CST;
6474 }
6475
6476 /* Subroutine of cxx_eval_statement_list. Determine whether the statement
6477 STMT matches *jump_target. If we're looking for a case label and we see
6478 the default label, note it in ctx->css_state. */
6479
6480 static bool
6481 label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
6482 {
6483 switch (TREE_CODE (*jump_target))
6484 {
6485 case LABEL_DECL:
6486 if (TREE_CODE (stmt) == LABEL_EXPR
6487 && LABEL_EXPR_LABEL (stmt) == *jump_target)
6488 return true;
6489 break;
6490
6491 case INTEGER_CST:
6492 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
6493 {
6494 gcc_assert (ctx->css_state != NULL);
6495 if (!CASE_LOW (stmt))
6496 {
6497 /* default: should appear just once in a SWITCH_EXPR
6498 body (excluding nested SWITCH_EXPR). */
6499 gcc_assert (*ctx->css_state != css_default_seen);
6500 /* When evaluating SWITCH_EXPR body for the second time,
6501 return true for the default: label. */
6502 if (*ctx->css_state == css_default_processing)
6503 return true;
6504 *ctx->css_state = css_default_seen;
6505 }
6506 else if (CASE_HIGH (stmt))
6507 {
6508 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
6509 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
6510 return true;
6511 }
6512 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
6513 return true;
6514 }
6515 break;
6516
6517 case BREAK_STMT:
6518 case CONTINUE_STMT:
6519 /* These two are handled directly in cxx_eval_loop_expr by testing
6520 breaks (jump_target) or continues (jump_target). */
6521 break;
6522
6523 default:
6524 gcc_unreachable ();
6525 }
6526 return false;
6527 }
6528
6529 /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
6530 semantics, for switch, break, continue, and return. */
6531
6532 static tree
6533 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
6534 bool *non_constant_p, bool *overflow_p,
6535 tree *jump_target)
6536 {
6537 tree local_target;
6538 /* In a statement-expression we want to return the last value.
6539 For empty statement expression return void_node. */
6540 tree r = void_node;
6541 if (!jump_target)
6542 {
6543 local_target = NULL_TREE;
6544 jump_target = &local_target;
6545 }
6546 for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
6547 {
6548 tree stmt = *i;
6549
6550 /* We've found a continue, so skip everything until we reach
6551 the label its jumping to. */
6552 if (continues (jump_target))
6553 {
6554 if (label_matches (ctx, jump_target, stmt))
6555 /* Found it. */
6556 *jump_target = NULL_TREE;
6557 else
6558 continue;
6559 }
6560 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
6561 continue;
6562
6563 value_cat lval = vc_discard;
6564 /* The result of a statement-expression is not wrapped in EXPR_STMT. */
6565 if (tsi_one_before_end_p (i) && TREE_CODE (stmt) != EXPR_STMT)
6566 lval = vc_prvalue;
6567
6568 r = cxx_eval_constant_expression (ctx, stmt, lval,
6569 non_constant_p, overflow_p,
6570 jump_target);
6571 if (*non_constant_p)
6572 break;
6573 if (returns (jump_target) || breaks (jump_target))
6574 break;
6575 }
6576 if (*jump_target && jump_target == &local_target)
6577 {
6578 /* We aren't communicating the jump to our caller, so give up. We don't
6579 need to support evaluation of jumps out of statement-exprs. */
6580 if (!ctx->quiet)
6581 error_at (cp_expr_loc_or_input_loc (r),
6582 "statement is not a constant expression");
6583 *non_constant_p = true;
6584 }
6585 return r;
6586 }
6587
6588 /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
6589 semantics; continue semantics are covered by cxx_eval_statement_list. */
6590
6591 static tree
6592 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
6593 bool *non_constant_p, bool *overflow_p,
6594 tree *jump_target)
6595 {
6596 constexpr_ctx new_ctx = *ctx;
6597 tree local_target;
6598 if (!jump_target)
6599 {
6600 local_target = NULL_TREE;
6601 jump_target = &local_target;
6602 }
6603
6604 tree body, cond = NULL_TREE, expr = NULL_TREE;
6605 int count = 0;
6606 switch (TREE_CODE (t))
6607 {
6608 case LOOP_EXPR:
6609 body = LOOP_EXPR_BODY (t);
6610 break;
6611 case DO_STMT:
6612 body = DO_BODY (t);
6613 cond = DO_COND (t);
6614 break;
6615 case WHILE_STMT:
6616 body = WHILE_BODY (t);
6617 cond = WHILE_COND (t);
6618 count = -1;
6619 break;
6620 case FOR_STMT:
6621 if (FOR_INIT_STMT (t))
6622 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
6623 non_constant_p, overflow_p, jump_target);
6624 if (*non_constant_p)
6625 return NULL_TREE;
6626 body = FOR_BODY (t);
6627 cond = FOR_COND (t);
6628 expr = FOR_EXPR (t);
6629 count = -1;
6630 break;
6631 default:
6632 gcc_unreachable ();
6633 }
6634 auto_vec<tree, 10> save_exprs;
6635 new_ctx.save_exprs = &save_exprs;
6636 do
6637 {
6638 if (count != -1)
6639 {
6640 if (body)
6641 cxx_eval_constant_expression (&new_ctx, body, vc_discard,
6642 non_constant_p, overflow_p,
6643 jump_target);
6644 if (breaks (jump_target))
6645 {
6646 *jump_target = NULL_TREE;
6647 break;
6648 }
6649
6650 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
6651 *jump_target = NULL_TREE;
6652
6653 if (expr)
6654 cxx_eval_constant_expression (&new_ctx, expr, vc_prvalue,
6655 non_constant_p, overflow_p,
6656 jump_target);
6657 }
6658
6659 if (cond)
6660 {
6661 tree res
6662 = cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
6663 non_constant_p, overflow_p,
6664 jump_target);
6665 if (res)
6666 {
6667 if (verify_constant (res, ctx->quiet, non_constant_p,
6668 overflow_p))
6669 break;
6670 if (integer_zerop (res))
6671 break;
6672 }
6673 else
6674 gcc_assert (*jump_target);
6675 }
6676
6677 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6678 for (tree save_expr : save_exprs)
6679 ctx->global->remove_value (save_expr);
6680 save_exprs.truncate (0);
6681
6682 if (++count >= constexpr_loop_limit)
6683 {
6684 if (!ctx->quiet)
6685 error_at (cp_expr_loc_or_input_loc (t),
6686 "%<constexpr%> loop iteration count exceeds limit of %d "
6687 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
6688 constexpr_loop_limit);
6689 *non_constant_p = true;
6690 break;
6691 }
6692 }
6693 while (!returns (jump_target)
6694 && !breaks (jump_target)
6695 && !continues (jump_target)
6696 && (!switches (jump_target) || count == 0)
6697 && !*non_constant_p);
6698
6699 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
6700 for (tree save_expr : save_exprs)
6701 ctx->global->remove_value (save_expr);
6702
6703 return NULL_TREE;
6704 }
6705
6706 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
6707 semantics. */
6708
6709 static tree
6710 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
6711 bool *non_constant_p, bool *overflow_p,
6712 tree *jump_target)
6713 {
6714 tree cond
6715 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
6716 cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
6717 non_constant_p, overflow_p);
6718 VERIFY_CONSTANT (cond);
6719 *jump_target = cond;
6720
6721 tree body
6722 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
6723 constexpr_ctx new_ctx = *ctx;
6724 constexpr_switch_state css = css_default_not_seen;
6725 new_ctx.css_state = &css;
6726 cxx_eval_constant_expression (&new_ctx, body, vc_discard,
6727 non_constant_p, overflow_p, jump_target);
6728 if (switches (jump_target) && css == css_default_seen)
6729 {
6730 /* If the SWITCH_EXPR body has default: label, process it once again,
6731 this time instructing label_matches to return true for default:
6732 label on switches (jump_target). */
6733 css = css_default_processing;
6734 cxx_eval_constant_expression (&new_ctx, body, vc_discard,
6735 non_constant_p, overflow_p, jump_target);
6736 }
6737 if (breaks (jump_target) || switches (jump_target))
6738 *jump_target = NULL_TREE;
6739 return NULL_TREE;
6740 }
6741
6742 /* Find the object of TYPE under initialization in CTX. */
6743
6744 static tree
6745 lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
6746 {
6747 if (!ctx)
6748 return NULL_TREE;
6749
6750 /* Prefer the outermost matching object, but don't cross
6751 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
6752 if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
6753 if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
6754 return outer_ob;
6755
6756 /* We could use ctx->object unconditionally, but using ctx->ctor when we
6757 can is a minor optimization. */
6758 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
6759 return ctx->ctor;
6760
6761 if (!ctx->object)
6762 return NULL_TREE;
6763
6764 /* Since an object cannot have a field of its own type, we can search outward
6765 from ctx->object to find the unique containing object of TYPE. */
6766 tree ob = ctx->object;
6767 while (ob)
6768 {
6769 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
6770 break;
6771 if (handled_component_p (ob))
6772 ob = TREE_OPERAND (ob, 0);
6773 else
6774 ob = NULL_TREE;
6775 }
6776
6777 return ob;
6778 }
6779
6780 /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
6781 true, we're checking a constexpr function body. */
6782
6783 static void
6784 inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
6785 {
6786 auto_diagnostic_group d;
6787 if (constexpr_error (loc, fundef_p, "inline assembly is not a "
6788 "constant expression"))
6789 inform (loc, "only unevaluated inline assembly is allowed in a "
6790 "%<constexpr%> function in C++20");
6791 }
6792
6793 /* We're getting the constant value of DECL in a manifestly constant-evaluated
6794 context; maybe complain about that. */
6795
6796 static void
6797 maybe_warn_about_constant_value (location_t loc, tree decl)
6798 {
6799 static bool explained = false;
6800 if (cxx_dialect >= cxx17
6801 && warn_interference_size
6802 && !OPTION_SET_P (param_destruct_interfere_size)
6803 && DECL_CONTEXT (decl) == std_node
6804 && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
6805 && (LOCATION_FILE (input_location) != main_input_filename
6806 || module_exporting_p ())
6807 && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
6808 && !explained)
6809 {
6810 explained = true;
6811 inform (loc, "its value can vary between compiler versions or "
6812 "with different %<-mtune%> or %<-mcpu%> flags");
6813 inform (loc, "if this use is part of a public ABI, change it to "
6814 "instead use a constant variable you define");
6815 inform (loc, "the default value for the current CPU tuning "
6816 "is %d bytes", param_destruct_interfere_size);
6817 inform (loc, "you can stabilize this value with %<--param "
6818 "hardware_destructive_interference_size=%d%>, or disable "
6819 "this warning with %<-Wno-interference-size%>",
6820 param_destruct_interfere_size);
6821 }
6822 }
6823
6824 /* For element type ELT_TYPE, return the appropriate type of the heap object
6825 containing such element(s). COOKIE_SIZE is NULL or the size of cookie
6826 in bytes. If COOKIE_SIZE is NULL, return array type
6827 ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
6828 struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
6829 where N is is computed such that the size of the struct fits into FULL_SIZE.
6830 If ARG_SIZE is non-NULL, it is the first argument to the new operator.
6831 It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
6832 will be also 0 and so it is not possible to determine the actual array
6833 size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
6834 expression evaluation of subexpressions of ARG_SIZE. */
6835
6836 static tree
6837 build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
6838 tree cookie_size, tree full_size, tree arg_size,
6839 bool *non_constant_p, bool *overflow_p)
6840 {
6841 gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
6842 gcc_assert (tree_fits_uhwi_p (full_size));
6843 unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
6844 if (arg_size)
6845 {
6846 STRIP_NOPS (arg_size);
6847 if (cookie_size)
6848 {
6849 if (TREE_CODE (arg_size) != PLUS_EXPR)
6850 arg_size = NULL_TREE;
6851 else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
6852 && tree_int_cst_equal (cookie_size,
6853 TREE_OPERAND (arg_size, 0)))
6854 {
6855 arg_size = TREE_OPERAND (arg_size, 1);
6856 STRIP_NOPS (arg_size);
6857 }
6858 else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
6859 && tree_int_cst_equal (cookie_size,
6860 TREE_OPERAND (arg_size, 1)))
6861 {
6862 arg_size = TREE_OPERAND (arg_size, 0);
6863 STRIP_NOPS (arg_size);
6864 }
6865 else
6866 arg_size = NULL_TREE;
6867 }
6868 if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
6869 {
6870 tree op0 = TREE_OPERAND (arg_size, 0);
6871 tree op1 = TREE_OPERAND (arg_size, 1);
6872 if (integer_zerop (op0))
6873 arg_size
6874 = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
6875 non_constant_p, overflow_p);
6876 else if (integer_zerop (op1))
6877 arg_size
6878 = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
6879 non_constant_p, overflow_p);
6880 else
6881 arg_size = NULL_TREE;
6882 }
6883 else
6884 arg_size = NULL_TREE;
6885 }
6886
6887 unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
6888 if (!arg_size)
6889 {
6890 unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
6891 gcc_assert (fsz >= csz);
6892 fsz -= csz;
6893 if (esz)
6894 fsz /= esz;
6895 }
6896 tree itype2 = build_index_type (size_int (fsz - 1));
6897 if (!cookie_size)
6898 return build_cplus_array_type (elt_type, itype2);
6899 return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
6900 }
6901
6902 /* Attempt to reduce the expression T to a constant value.
6903 On failure, issue diagnostic and return error_mark_node. */
6904 /* FIXME unify with c_fully_fold */
6905 /* FIXME overflow_p is too global */
6906
6907 static tree
6908 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
6909 value_cat lval,
6910 bool *non_constant_p, bool *overflow_p,
6911 tree *jump_target /* = NULL */)
6912 {
6913 if (jump_target && *jump_target)
6914 {
6915 /* If we are jumping, ignore all statements/expressions except those
6916 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
6917 switch (TREE_CODE (t))
6918 {
6919 case BIND_EXPR:
6920 case STATEMENT_LIST:
6921 case LOOP_EXPR:
6922 case COND_EXPR:
6923 case IF_STMT:
6924 case DO_STMT:
6925 case WHILE_STMT:
6926 case FOR_STMT:
6927 break;
6928 case LABEL_EXPR:
6929 case CASE_LABEL_EXPR:
6930 if (label_matches (ctx, jump_target, t))
6931 /* Found it. */
6932 *jump_target = NULL_TREE;
6933 return NULL_TREE;
6934 default:
6935 return NULL_TREE;
6936 }
6937 }
6938 if (error_operand_p (t))
6939 {
6940 *non_constant_p = true;
6941 return t;
6942 }
6943
6944 location_t loc = cp_expr_loc_or_input_loc (t);
6945
6946 STRIP_ANY_LOCATION_WRAPPER (t);
6947
6948 if (CONSTANT_CLASS_P (t))
6949 {
6950 if (TREE_OVERFLOW (t))
6951 {
6952 if (!ctx->quiet)
6953 permerror (input_location, "overflow in constant expression");
6954 if (!flag_permissive || ctx->quiet)
6955 *overflow_p = true;
6956 }
6957
6958 if (TREE_CODE (t) == INTEGER_CST
6959 && TYPE_PTR_P (TREE_TYPE (t))
6960 /* INTEGER_CST with pointer-to-method type is only used
6961 for a virtual method in a pointer to member function.
6962 Don't reject those. */
6963 && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
6964 && !integer_zerop (t))
6965 {
6966 if (!ctx->quiet)
6967 error ("value %qE of type %qT is not a constant expression",
6968 t, TREE_TYPE (t));
6969 *non_constant_p = true;
6970 }
6971
6972 return t;
6973 }
6974
6975 /* Avoid excessively long constexpr evaluations. */
6976 if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
6977 {
6978 if (!ctx->quiet)
6979 error_at (loc,
6980 "%<constexpr%> evaluation operation count exceeds limit of "
6981 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
6982 constexpr_ops_limit);
6983 ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
6984 *non_constant_p = true;
6985 return t;
6986 }
6987
6988 constexpr_ctx new_ctx;
6989 tree r = t;
6990
6991 tree_code tcode = TREE_CODE (t);
6992 switch (tcode)
6993 {
6994 case RESULT_DECL:
6995 if (lval)
6996 return t;
6997 /* We ask for an rvalue for the RESULT_DECL when indirecting
6998 through an invisible reference, or in named return value
6999 optimization. */
7000 if (tree v = ctx->global->get_value (t))
7001 return v;
7002 else
7003 {
7004 if (!ctx->quiet)
7005 error ("%qE is not a constant expression", t);
7006 *non_constant_p = true;
7007 }
7008 break;
7009
7010 case VAR_DECL:
7011 if (DECL_HAS_VALUE_EXPR_P (t))
7012 {
7013 if (is_normal_capture_proxy (t)
7014 && current_function_decl == DECL_CONTEXT (t))
7015 {
7016 /* Function parms aren't constexpr within the function
7017 definition, so don't try to look at the closure. But if the
7018 captured variable is constant, try to evaluate it directly. */
7019 r = DECL_CAPTURED_VARIABLE (t);
7020 tree type = TREE_TYPE (t);
7021 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
7022 {
7023 /* Adjust r to match the reference-ness of t. */
7024 if (TYPE_REF_P (type))
7025 r = build_address (r);
7026 else
7027 r = convert_from_reference (r);
7028 }
7029 }
7030 else
7031 r = DECL_VALUE_EXPR (t);
7032 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
7033 overflow_p);
7034 }
7035 /* fall through */
7036 case CONST_DECL:
7037 /* We used to not check lval for CONST_DECL, but darwin.cc uses
7038 CONST_DECL for aggregate constants. */
7039 if (lval)
7040 return t;
7041 else if (t == ctx->object)
7042 return ctx->ctor;
7043 if (VAR_P (t))
7044 if (tree v = ctx->global->get_value (t))
7045 {
7046 r = v;
7047 break;
7048 }
7049 if (ctx->manifestly_const_eval)
7050 maybe_warn_about_constant_value (loc, t);
7051 if (COMPLETE_TYPE_P (TREE_TYPE (t))
7052 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7053 {
7054 /* If the class is empty, we aren't actually loading anything. */
7055 r = build_constructor (TREE_TYPE (t), NULL);
7056 TREE_CONSTANT (r) = true;
7057 }
7058 else if (ctx->strict)
7059 r = decl_really_constant_value (t, /*unshare_p=*/false);
7060 else
7061 r = decl_constant_value (t, /*unshare_p=*/false);
7062 if (TREE_CODE (r) == TARGET_EXPR
7063 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
7064 r = TARGET_EXPR_INITIAL (r);
7065 if (DECL_P (r))
7066 {
7067 if (!ctx->quiet)
7068 non_const_var_error (loc, r, /*fundef_p*/false);
7069 *non_constant_p = true;
7070 }
7071 break;
7072
7073 case DEBUG_BEGIN_STMT:
7074 /* ??? It might be nice to retain this information somehow, so
7075 as to be able to step into a constexpr function call. */
7076 /* Fall through. */
7077
7078 case FUNCTION_DECL:
7079 case TEMPLATE_DECL:
7080 case LABEL_DECL:
7081 case LABEL_EXPR:
7082 case CASE_LABEL_EXPR:
7083 case PREDICT_EXPR:
7084 return t;
7085
7086 case PARM_DECL:
7087 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
7088 /* glvalue use. */;
7089 else if (tree v = ctx->global->get_value (r))
7090 r = v;
7091 else if (lval)
7092 /* Defer in case this is only used for its type. */;
7093 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
7094 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7095 {
7096 /* If the class is empty, we aren't actually loading anything. */
7097 r = build_constructor (TREE_TYPE (t), NULL);
7098 TREE_CONSTANT (r) = true;
7099 }
7100 else
7101 {
7102 if (!ctx->quiet)
7103 error ("%qE is not a constant expression", t);
7104 *non_constant_p = true;
7105 }
7106 break;
7107
7108 case CALL_EXPR:
7109 case AGGR_INIT_EXPR:
7110 r = cxx_eval_call_expression (ctx, t, lval,
7111 non_constant_p, overflow_p);
7112 break;
7113
7114 case DECL_EXPR:
7115 {
7116 r = DECL_EXPR_DECL (t);
7117 if (TREE_CODE (r) == USING_DECL)
7118 {
7119 r = void_node;
7120 break;
7121 }
7122
7123 if (VAR_P (r)
7124 && (TREE_STATIC (r)
7125 || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
7126 /* Allow __FUNCTION__ etc. */
7127 && !DECL_ARTIFICIAL (r))
7128 {
7129 if (!ctx->quiet)
7130 {
7131 if (CP_DECL_THREAD_LOCAL_P (r))
7132 error_at (loc, "control passes through definition of %qD "
7133 "with thread storage duration", r);
7134 else
7135 error_at (loc, "control passes through definition of %qD "
7136 "with static storage duration", r);
7137 }
7138 *non_constant_p = true;
7139 break;
7140 }
7141
7142 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
7143 || VECTOR_TYPE_P (TREE_TYPE (r)))
7144 {
7145 new_ctx = *ctx;
7146 new_ctx.object = r;
7147 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
7148 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7149 ctx->global->put_value (r, new_ctx.ctor);
7150 ctx = &new_ctx;
7151 }
7152
7153 if (tree init = DECL_INITIAL (r))
7154 {
7155 init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
7156 non_constant_p, overflow_p);
7157 /* Don't share a CONSTRUCTOR that might be changed. */
7158 init = unshare_constructor (init);
7159 /* Remember that a constant object's constructor has already
7160 run. */
7161 if (CLASS_TYPE_P (TREE_TYPE (r))
7162 && CP_TYPE_CONST_P (TREE_TYPE (r)))
7163 TREE_READONLY (init) = true;
7164 ctx->global->put_value (r, init);
7165 }
7166 else if (ctx == &new_ctx)
7167 /* We gave it a CONSTRUCTOR above. */;
7168 else
7169 ctx->global->put_value (r, NULL_TREE);
7170 }
7171 break;
7172
7173 case TARGET_EXPR:
7174 {
7175 tree type = TREE_TYPE (t);
7176
7177 if (!literal_type_p (type))
7178 {
7179 if (!ctx->quiet)
7180 {
7181 auto_diagnostic_group d;
7182 error ("temporary of non-literal type %qT in a "
7183 "constant expression", type);
7184 explain_non_literal_class (type);
7185 }
7186 *non_constant_p = true;
7187 break;
7188 }
7189 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
7190 /* Avoid evaluating a TARGET_EXPR more than once. */
7191 tree slot = TARGET_EXPR_SLOT (t);
7192 if (tree v = ctx->global->get_value (slot))
7193 {
7194 if (lval)
7195 return slot;
7196 r = v;
7197 break;
7198 }
7199 if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
7200 {
7201 /* We're being expanded without an explicit target, so start
7202 initializing a new object; expansion with an explicit target
7203 strips the TARGET_EXPR before we get here. */
7204 new_ctx = *ctx;
7205 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
7206 any PLACEHOLDER_EXPR within the initializer that refers to the
7207 former object under construction. */
7208 new_ctx.parent = ctx;
7209 new_ctx.ctor = build_constructor (type, NULL);
7210 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
7211 new_ctx.object = slot;
7212 ctx->global->put_value (new_ctx.object, new_ctx.ctor);
7213 ctx = &new_ctx;
7214 }
7215 /* Pass vc_prvalue because this indicates
7216 initialization of a temporary. */
7217 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_prvalue,
7218 non_constant_p, overflow_p);
7219 if (*non_constant_p)
7220 break;
7221 /* Adjust the type of the result to the type of the temporary. */
7222 r = adjust_temp_type (type, r);
7223 if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
7224 ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
7225 r = unshare_constructor (r);
7226 ctx->global->put_value (slot, r);
7227 if (ctx->save_exprs)
7228 ctx->save_exprs->safe_push (slot);
7229 if (lval)
7230 return slot;
7231 }
7232 break;
7233
7234 case INIT_EXPR:
7235 case MODIFY_EXPR:
7236 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
7237 r = cxx_eval_store_expression (ctx, t, lval,
7238 non_constant_p, overflow_p);
7239 break;
7240
7241 case SCOPE_REF:
7242 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
7243 lval,
7244 non_constant_p, overflow_p);
7245 break;
7246
7247 case RETURN_EXPR:
7248 if (TREE_OPERAND (t, 0) != NULL_TREE)
7249 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7250 lval,
7251 non_constant_p, overflow_p);
7252 /* FALLTHRU */
7253 case BREAK_STMT:
7254 case CONTINUE_STMT:
7255 if (jump_target)
7256 *jump_target = t;
7257 else
7258 {
7259 /* Can happen with ({ return true; }) && false; passed to
7260 maybe_constant_value. There is nothing to jump over in this
7261 case, and the bug will be diagnosed later. */
7262 gcc_assert (ctx->quiet);
7263 *non_constant_p = true;
7264 }
7265 break;
7266
7267 case SAVE_EXPR:
7268 /* Avoid evaluating a SAVE_EXPR more than once. */
7269 if (tree v = ctx->global->get_value (t))
7270 r = v;
7271 else
7272 {
7273 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
7274 non_constant_p, overflow_p);
7275 if (*non_constant_p)
7276 break;
7277 ctx->global->put_value (t, r);
7278 if (ctx->save_exprs)
7279 ctx->save_exprs->safe_push (t);
7280 }
7281 break;
7282
7283 case TRY_CATCH_EXPR:
7284 if (TREE_OPERAND (t, 0) == NULL_TREE)
7285 {
7286 r = void_node;
7287 break;
7288 }
7289 /* FALLTHRU */
7290 case NON_LVALUE_EXPR:
7291 case TRY_BLOCK:
7292 case MUST_NOT_THROW_EXPR:
7293 case EXPR_STMT:
7294 case EH_SPEC_BLOCK:
7295 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7296 lval,
7297 non_constant_p, overflow_p,
7298 jump_target);
7299 break;
7300
7301 case CLEANUP_POINT_EXPR:
7302 {
7303 auto_vec<tree, 2> cleanups;
7304 vec<tree> *prev_cleanups = ctx->global->cleanups;
7305 ctx->global->cleanups = &cleanups;
7306 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7307 lval,
7308 non_constant_p, overflow_p,
7309 jump_target);
7310 ctx->global->cleanups = prev_cleanups;
7311 unsigned int i;
7312 tree cleanup;
7313 /* Evaluate the cleanups. */
7314 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
7315 cxx_eval_constant_expression (ctx, cleanup, vc_discard,
7316 non_constant_p, overflow_p);
7317 }
7318 break;
7319
7320 case TRY_FINALLY_EXPR:
7321 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7322 non_constant_p, overflow_p,
7323 jump_target);
7324 if (!*non_constant_p)
7325 /* Also evaluate the cleanup. */
7326 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
7327 non_constant_p, overflow_p);
7328 break;
7329
7330 case CLEANUP_STMT:
7331 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
7332 non_constant_p, overflow_p,
7333 jump_target);
7334 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
7335 {
7336 iloc_sentinel ils (loc);
7337 /* Also evaluate the cleanup. */
7338 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
7339 non_constant_p, overflow_p);
7340 }
7341 break;
7342
7343 /* These differ from cxx_eval_unary_expression in that this doesn't
7344 check for a constant operand or result; an address can be
7345 constant without its operand being, and vice versa. */
7346 case MEM_REF:
7347 case INDIRECT_REF:
7348 r = cxx_eval_indirect_ref (ctx, t, lval,
7349 non_constant_p, overflow_p);
7350 break;
7351
7352 case ADDR_EXPR:
7353 {
7354 tree oldop = TREE_OPERAND (t, 0);
7355 tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
7356 non_constant_p, overflow_p);
7357 /* Don't VERIFY_CONSTANT here. */
7358 if (*non_constant_p)
7359 return t;
7360 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
7361 /* This function does more aggressive folding than fold itself. */
7362 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
7363 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7364 {
7365 ggc_free (r);
7366 return t;
7367 }
7368 break;
7369 }
7370
7371 case REALPART_EXPR:
7372 case IMAGPART_EXPR:
7373 if (lval)
7374 {
7375 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7376 non_constant_p, overflow_p);
7377 if (r == error_mark_node)
7378 ;
7379 else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
7380 r = t;
7381 else
7382 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
7383 break;
7384 }
7385 /* FALLTHRU */
7386 case CONJ_EXPR:
7387 case FIX_TRUNC_EXPR:
7388 case FLOAT_EXPR:
7389 case NEGATE_EXPR:
7390 case ABS_EXPR:
7391 case ABSU_EXPR:
7392 case BIT_NOT_EXPR:
7393 case TRUTH_NOT_EXPR:
7394 case FIXED_CONVERT_EXPR:
7395 r = cxx_eval_unary_expression (ctx, t, lval,
7396 non_constant_p, overflow_p);
7397 break;
7398
7399 case SIZEOF_EXPR:
7400 r = fold_sizeof_expr (t);
7401 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
7402 which could lead to an infinite recursion. */
7403 if (TREE_CODE (r) != SIZEOF_EXPR)
7404 r = cxx_eval_constant_expression (ctx, r, lval,
7405 non_constant_p, overflow_p,
7406 jump_target);
7407 else
7408 {
7409 *non_constant_p = true;
7410 gcc_assert (ctx->quiet);
7411 }
7412
7413 break;
7414
7415 case COMPOUND_EXPR:
7416 {
7417 /* check_return_expr sometimes wraps a TARGET_EXPR in a
7418 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
7419 introduced by build_call_a. */
7420 tree op0 = TREE_OPERAND (t, 0);
7421 tree op1 = TREE_OPERAND (t, 1);
7422 STRIP_NOPS (op1);
7423 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
7424 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
7425 r = cxx_eval_constant_expression (ctx, op0,
7426 lval, non_constant_p, overflow_p,
7427 jump_target);
7428 else
7429 {
7430 /* Check that the LHS is constant and then discard it. */
7431 cxx_eval_constant_expression (ctx, op0, vc_discard,
7432 non_constant_p, overflow_p,
7433 jump_target);
7434 if (*non_constant_p)
7435 return t;
7436 op1 = TREE_OPERAND (t, 1);
7437 r = cxx_eval_constant_expression (ctx, op1,
7438 lval, non_constant_p, overflow_p,
7439 jump_target);
7440 }
7441 }
7442 break;
7443
7444 case POINTER_PLUS_EXPR:
7445 case POINTER_DIFF_EXPR:
7446 case PLUS_EXPR:
7447 case MINUS_EXPR:
7448 case MULT_EXPR:
7449 case TRUNC_DIV_EXPR:
7450 case CEIL_DIV_EXPR:
7451 case FLOOR_DIV_EXPR:
7452 case ROUND_DIV_EXPR:
7453 case TRUNC_MOD_EXPR:
7454 case CEIL_MOD_EXPR:
7455 case ROUND_MOD_EXPR:
7456 case RDIV_EXPR:
7457 case EXACT_DIV_EXPR:
7458 case MIN_EXPR:
7459 case MAX_EXPR:
7460 case LSHIFT_EXPR:
7461 case RSHIFT_EXPR:
7462 case LROTATE_EXPR:
7463 case RROTATE_EXPR:
7464 case BIT_IOR_EXPR:
7465 case BIT_XOR_EXPR:
7466 case BIT_AND_EXPR:
7467 case TRUTH_XOR_EXPR:
7468 case LT_EXPR:
7469 case LE_EXPR:
7470 case GT_EXPR:
7471 case GE_EXPR:
7472 case EQ_EXPR:
7473 case NE_EXPR:
7474 case SPACESHIP_EXPR:
7475 case UNORDERED_EXPR:
7476 case ORDERED_EXPR:
7477 case UNLT_EXPR:
7478 case UNLE_EXPR:
7479 case UNGT_EXPR:
7480 case UNGE_EXPR:
7481 case UNEQ_EXPR:
7482 case LTGT_EXPR:
7483 case RANGE_EXPR:
7484 case COMPLEX_EXPR:
7485 r = cxx_eval_binary_expression (ctx, t, lval,
7486 non_constant_p, overflow_p);
7487 break;
7488
7489 /* fold can introduce non-IF versions of these; still treat them as
7490 short-circuiting. */
7491 case TRUTH_AND_EXPR:
7492 case TRUTH_ANDIF_EXPR:
7493 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
7494 boolean_true_node,
7495 non_constant_p, overflow_p);
7496 break;
7497
7498 case TRUTH_OR_EXPR:
7499 case TRUTH_ORIF_EXPR:
7500 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
7501 boolean_false_node,
7502 non_constant_p, overflow_p);
7503 break;
7504
7505 case ARRAY_REF:
7506 r = cxx_eval_array_reference (ctx, t, lval,
7507 non_constant_p, overflow_p);
7508 break;
7509
7510 case COMPONENT_REF:
7511 if (is_overloaded_fn (t))
7512 {
7513 /* We can only get here in checking mode via
7514 build_non_dependent_expr, because any expression that
7515 calls or takes the address of the function will have
7516 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
7517 gcc_checking_assert (ctx->quiet || errorcount);
7518 *non_constant_p = true;
7519 return t;
7520 }
7521 r = cxx_eval_component_reference (ctx, t, lval,
7522 non_constant_p, overflow_p);
7523 break;
7524
7525 case BIT_FIELD_REF:
7526 r = cxx_eval_bit_field_ref (ctx, t, lval,
7527 non_constant_p, overflow_p);
7528 break;
7529
7530 case COND_EXPR:
7531 case IF_STMT:
7532 if (jump_target && *jump_target)
7533 {
7534 tree orig_jump = *jump_target;
7535 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
7536 ? TREE_OPERAND (t, 1) : void_node);
7537 /* When jumping to a label, the label might be either in the
7538 then or else blocks, so process then block first in skipping
7539 mode first, and if we are still in the skipping mode at its end,
7540 process the else block too. */
7541 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7542 overflow_p, jump_target);
7543 /* It's possible that we found the label in the then block. But
7544 it could have been followed by another jumping statement, e.g.
7545 say we're looking for case 1:
7546 if (cond)
7547 {
7548 // skipped statements
7549 case 1:; // clears up *jump_target
7550 return 1; // and sets it to a RETURN_EXPR
7551 }
7552 else { ... }
7553 in which case we need not go looking to the else block.
7554 (goto is not allowed in a constexpr function.) */
7555 if (*jump_target == orig_jump)
7556 {
7557 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
7558 ? TREE_OPERAND (t, 2) : void_node);
7559 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
7560 overflow_p, jump_target);
7561 }
7562 break;
7563 }
7564 r = cxx_eval_conditional_expression (ctx, t, lval,
7565 non_constant_p, overflow_p,
7566 jump_target);
7567 break;
7568 case VEC_COND_EXPR:
7569 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
7570 overflow_p);
7571 break;
7572
7573 case CONSTRUCTOR:
7574 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
7575 {
7576 /* Don't re-process a constant CONSTRUCTOR. */
7577 verify_constructor_flags (t);
7578 if (TREE_CONSTANT (t))
7579 return t;
7580 }
7581 r = cxx_eval_bare_aggregate (ctx, t, lval,
7582 non_constant_p, overflow_p);
7583 break;
7584
7585 case VEC_INIT_EXPR:
7586 /* We can get this in a defaulted constructor for a class with a
7587 non-static data member of array type. Either the initializer will
7588 be NULL, meaning default-initialization, or it will be an lvalue
7589 or xvalue of the same type, meaning direct-initialization from the
7590 corresponding member. */
7591 r = cxx_eval_vec_init (ctx, t, lval,
7592 non_constant_p, overflow_p);
7593 break;
7594
7595 case VEC_PERM_EXPR:
7596 r = cxx_eval_trinary_expression (ctx, t, lval,
7597 non_constant_p, overflow_p);
7598 break;
7599
7600 case PAREN_EXPR:
7601 gcc_assert (!REF_PARENTHESIZED_P (t));
7602 /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
7603 constant expressions since it's unaffected by -fassociative-math. */
7604 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
7605 non_constant_p, overflow_p);
7606 break;
7607
7608 case NOP_EXPR:
7609 if (REINTERPRET_CAST_P (t))
7610 {
7611 if (!ctx->quiet)
7612 error_at (loc,
7613 "%<reinterpret_cast%> is not a constant expression");
7614 *non_constant_p = true;
7615 return t;
7616 }
7617 /* FALLTHROUGH. */
7618 case CONVERT_EXPR:
7619 case VIEW_CONVERT_EXPR:
7620 case UNARY_PLUS_EXPR:
7621 {
7622 tree oldop = TREE_OPERAND (t, 0);
7623
7624 tree op = cxx_eval_constant_expression (ctx, oldop,
7625 lval,
7626 non_constant_p, overflow_p);
7627 if (*non_constant_p)
7628 return t;
7629 tree type = TREE_TYPE (t);
7630
7631 if (VOID_TYPE_P (type))
7632 return void_node;
7633
7634 if (TREE_CODE (t) == CONVERT_EXPR
7635 && ARITHMETIC_TYPE_P (type)
7636 && INDIRECT_TYPE_P (TREE_TYPE (op))
7637 && ctx->manifestly_const_eval)
7638 {
7639 if (!ctx->quiet)
7640 error_at (loc,
7641 "conversion from pointer type %qT to arithmetic type "
7642 "%qT in a constant expression", TREE_TYPE (op), type);
7643 *non_constant_p = true;
7644 return t;
7645 }
7646
7647 /* [expr.const]: a conversion from type cv void* to a pointer-to-object
7648 type cannot be part of a core constant expression as a resolution to
7649 DR 1312. */
7650 if (TYPE_PTROB_P (type)
7651 && TYPE_PTR_P (TREE_TYPE (op))
7652 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
7653 /* Inside a call to std::construct_at or to
7654 std::allocator<T>::{,de}allocate, we permit casting from void*
7655 because that is compiler-generated code. */
7656 && !is_std_construct_at (ctx->call)
7657 && !is_std_allocator_allocate (ctx->call))
7658 {
7659 /* Likewise, don't error when casting from void* when OP is
7660 &heap uninit and similar. */
7661 tree sop = tree_strip_nop_conversions (op);
7662 if (TREE_CODE (sop) == ADDR_EXPR
7663 && VAR_P (TREE_OPERAND (sop, 0))
7664 && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0)))
7665 /* OK */;
7666 else
7667 {
7668 if (!ctx->quiet)
7669 error_at (loc, "cast from %qT is not allowed",
7670 TREE_TYPE (op));
7671 *non_constant_p = true;
7672 return t;
7673 }
7674 }
7675
7676 if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
7677 op = cplus_expand_constant (op);
7678
7679 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
7680 {
7681 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
7682 && !can_convert_qual (type, op))
7683 op = cplus_expand_constant (op);
7684 return cp_fold_convert (type, op);
7685 }
7686
7687 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
7688 {
7689 if (integer_zerop (op))
7690 {
7691 if (TYPE_REF_P (type))
7692 {
7693 if (!ctx->quiet)
7694 error_at (loc, "dereferencing a null pointer");
7695 *non_constant_p = true;
7696 return t;
7697 }
7698 }
7699 else
7700 {
7701 /* This detects for example:
7702 reinterpret_cast<void*>(sizeof 0)
7703 */
7704 if (!ctx->quiet)
7705 error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
7706 "a constant expression",
7707 type, op);
7708 *non_constant_p = true;
7709 return t;
7710 }
7711 }
7712
7713 if (INDIRECT_TYPE_P (type)
7714 && TREE_CODE (op) == NOP_EXPR
7715 && TREE_TYPE (op) == ptr_type_node
7716 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
7717 && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
7718 && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7719 0)) == heap_uninit_identifier
7720 || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
7721 0)) == heap_vec_uninit_identifier))
7722 {
7723 tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
7724 tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
7725 tree elt_type = TREE_TYPE (type);
7726 tree cookie_size = NULL_TREE;
7727 tree arg_size = NULL_TREE;
7728 if (TREE_CODE (elt_type) == RECORD_TYPE
7729 && TYPE_NAME (elt_type) == heap_identifier)
7730 {
7731 tree fld1 = TYPE_FIELDS (elt_type);
7732 tree fld2 = DECL_CHAIN (fld1);
7733 elt_type = TREE_TYPE (TREE_TYPE (fld2));
7734 cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
7735 }
7736 DECL_NAME (var)
7737 = (DECL_NAME (var) == heap_uninit_identifier
7738 ? heap_identifier : heap_vec_identifier);
7739 /* For zero sized elt_type, try to recover how many outer_nelts
7740 it should have. */
7741 if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
7742 : integer_zerop (var_size))
7743 && !int_size_in_bytes (elt_type)
7744 && TREE_CODE (oldop) == CALL_EXPR
7745 && call_expr_nargs (oldop) >= 1)
7746 if (tree fun = get_function_named_in_call (oldop))
7747 if (cxx_replaceable_global_alloc_fn (fun)
7748 && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
7749 arg_size = CALL_EXPR_ARG (oldop, 0);
7750 TREE_TYPE (var)
7751 = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
7752 var_size, arg_size,
7753 non_constant_p, overflow_p);
7754 TREE_TYPE (TREE_OPERAND (op, 0))
7755 = build_pointer_type (TREE_TYPE (var));
7756 }
7757
7758 if (op == oldop && tcode != UNARY_PLUS_EXPR)
7759 /* We didn't fold at the top so we could check for ptr-int
7760 conversion. */
7761 return fold (t);
7762
7763 tree sop;
7764
7765 /* Handle an array's bounds having been deduced after we built
7766 the wrapping expression. */
7767 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
7768 r = op;
7769 else if (sop = tree_strip_nop_conversions (op),
7770 sop != op && (same_type_ignoring_tlq_and_bounds_p
7771 (type, TREE_TYPE (sop))))
7772 r = sop;
7773 else if (tcode == UNARY_PLUS_EXPR)
7774 r = fold_convert (TREE_TYPE (t), op);
7775 else
7776 r = fold_build1 (tcode, type, op);
7777
7778 /* Conversion of an out-of-range value has implementation-defined
7779 behavior; the language considers it different from arithmetic
7780 overflow, which is undefined. */
7781 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
7782 TREE_OVERFLOW (r) = false;
7783 }
7784 break;
7785
7786 case EXCESS_PRECISION_EXPR:
7787 {
7788 tree oldop = TREE_OPERAND (t, 0);
7789
7790 tree op = cxx_eval_constant_expression (ctx, oldop,
7791 lval,
7792 non_constant_p, overflow_p);
7793 if (*non_constant_p)
7794 return t;
7795 r = fold_convert (TREE_TYPE (t), op);
7796 break;
7797 }
7798
7799 case EMPTY_CLASS_EXPR:
7800 /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
7801 it to an appropriate CONSTRUCTOR. */
7802 return build_constructor (TREE_TYPE (t), NULL);
7803
7804 case STATEMENT_LIST:
7805 new_ctx = *ctx;
7806 new_ctx.ctor = new_ctx.object = NULL_TREE;
7807 return cxx_eval_statement_list (&new_ctx, t,
7808 non_constant_p, overflow_p, jump_target);
7809
7810 case BIND_EXPR:
7811 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
7812 lval,
7813 non_constant_p, overflow_p,
7814 jump_target);
7815
7816 case PREINCREMENT_EXPR:
7817 case POSTINCREMENT_EXPR:
7818 case PREDECREMENT_EXPR:
7819 case POSTDECREMENT_EXPR:
7820 return cxx_eval_increment_expression (ctx, t,
7821 lval, non_constant_p, overflow_p);
7822
7823 case LAMBDA_EXPR:
7824 case NEW_EXPR:
7825 case VEC_NEW_EXPR:
7826 case DELETE_EXPR:
7827 case VEC_DELETE_EXPR:
7828 case THROW_EXPR:
7829 case MODOP_EXPR:
7830 /* GCC internal stuff. */
7831 case VA_ARG_EXPR:
7832 case NON_DEPENDENT_EXPR:
7833 case BASELINK:
7834 case OFFSET_REF:
7835 if (!ctx->quiet)
7836 error_at (loc, "expression %qE is not a constant expression", t);
7837 *non_constant_p = true;
7838 break;
7839
7840 case OBJ_TYPE_REF:
7841 /* Virtual function lookup. We don't need to do anything fancy. */
7842 return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
7843 lval, non_constant_p, overflow_p);
7844
7845 case PLACEHOLDER_EXPR:
7846 /* Use of the value or address of the current object. */
7847 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
7848 {
7849 if (TREE_CODE (ctor) == CONSTRUCTOR)
7850 return ctor;
7851 else
7852 return cxx_eval_constant_expression (ctx, ctor, lval,
7853 non_constant_p, overflow_p);
7854 }
7855 /* A placeholder without a referent. We can get here when
7856 checking whether NSDMIs are noexcept, or in massage_init_elt;
7857 just say it's non-constant for now. */
7858 gcc_assert (ctx->quiet);
7859 *non_constant_p = true;
7860 break;
7861
7862 case EXIT_EXPR:
7863 {
7864 tree cond = TREE_OPERAND (t, 0);
7865 cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
7866 non_constant_p, overflow_p);
7867 VERIFY_CONSTANT (cond);
7868 if (integer_nonzerop (cond))
7869 *jump_target = t;
7870 }
7871 break;
7872
7873 case GOTO_EXPR:
7874 if (breaks (&TREE_OPERAND (t, 0))
7875 || continues (&TREE_OPERAND (t, 0)))
7876 *jump_target = TREE_OPERAND (t, 0);
7877 else
7878 {
7879 gcc_assert (cxx_dialect >= cxx23);
7880 if (!ctx->quiet)
7881 error_at (loc, "%<goto%> is not a constant expression");
7882 *non_constant_p = true;
7883 }
7884 break;
7885
7886 case LOOP_EXPR:
7887 case DO_STMT:
7888 case WHILE_STMT:
7889 case FOR_STMT:
7890 cxx_eval_loop_expr (ctx, t,
7891 non_constant_p, overflow_p, jump_target);
7892 break;
7893
7894 case SWITCH_EXPR:
7895 case SWITCH_STMT:
7896 cxx_eval_switch_expr (ctx, t,
7897 non_constant_p, overflow_p, jump_target);
7898 break;
7899
7900 case REQUIRES_EXPR:
7901 /* It's possible to get a requires-expression in a constant
7902 expression. For example:
7903
7904 template<typename T> concept bool C() {
7905 return requires (T t) { t; };
7906 }
7907
7908 template<typename T> requires !C<T>() void f(T);
7909
7910 Normalization leaves f with the associated constraint
7911 '!requires (T t) { ... }' which is not transformed into
7912 a constraint. */
7913 if (!processing_template_decl)
7914 return evaluate_requires_expr (t);
7915 else
7916 *non_constant_p = true;
7917 return t;
7918
7919 case ANNOTATE_EXPR:
7920 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7921 lval,
7922 non_constant_p, overflow_p,
7923 jump_target);
7924 break;
7925
7926 case USING_STMT:
7927 r = void_node;
7928 break;
7929
7930 case ASSERTION_STMT:
7931 case PRECONDITION_STMT:
7932 case POSTCONDITION_STMT:
7933 {
7934 contract_semantic semantic = get_contract_semantic (t);
7935 if (semantic == CCS_IGNORE)
7936 break;
7937
7938 if (!cxx_eval_assert (ctx, CONTRACT_CONDITION (t),
7939 G_("contract predicate is false in "
7940 "constant expression"),
7941 EXPR_LOCATION (t), checked_contract_p (semantic),
7942 non_constant_p, overflow_p))
7943 *non_constant_p = true;
7944 r = void_node;
7945 }
7946 break;
7947
7948 case TEMPLATE_ID_EXPR:
7949 {
7950 /* We can evaluate template-id that refers to a concept only if
7951 the template arguments are non-dependent. */
7952 tree id = unpack_concept_check (t);
7953 tree tmpl = TREE_OPERAND (id, 0);
7954 if (!concept_definition_p (tmpl))
7955 internal_error ("unexpected template-id %qE", t);
7956
7957 if (function_concept_p (tmpl))
7958 {
7959 if (!ctx->quiet)
7960 error_at (cp_expr_loc_or_input_loc (t),
7961 "function concept must be called");
7962 r = error_mark_node;
7963 break;
7964 }
7965
7966 if (!value_dependent_expression_p (t)
7967 && !uid_sensitive_constexpr_evaluation_p ())
7968 r = evaluate_concept_check (t);
7969 else
7970 *non_constant_p = true;
7971
7972 break;
7973 }
7974
7975 case ASM_EXPR:
7976 if (!ctx->quiet)
7977 inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
7978 *non_constant_p = true;
7979 return t;
7980
7981 case BIT_CAST_EXPR:
7982 if (lval)
7983 {
7984 if (!ctx->quiet)
7985 error_at (EXPR_LOCATION (t),
7986 "address of a call to %qs is not a constant expression",
7987 "__builtin_bit_cast");
7988 *non_constant_p = true;
7989 return t;
7990 }
7991 r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p);
7992 break;
7993
7994 default:
7995 if (STATEMENT_CODE_P (TREE_CODE (t)))
7996 {
7997 /* This function doesn't know how to deal with pre-genericize
7998 statements; this can only happen with statement-expressions,
7999 so for now just fail. */
8000 if (!ctx->quiet)
8001 error_at (EXPR_LOCATION (t),
8002 "statement is not a constant expression");
8003 }
8004 else
8005 internal_error ("unexpected expression %qE of kind %s", t,
8006 get_tree_code_name (TREE_CODE (t)));
8007 *non_constant_p = true;
8008 break;
8009 }
8010
8011 if (r == error_mark_node)
8012 *non_constant_p = true;
8013
8014 if (*non_constant_p)
8015 return t;
8016 else
8017 return r;
8018 }
8019
8020 /* P0859: A function is needed for constant evaluation if it is a constexpr
8021 function that is named by an expression ([basic.def.odr]) that is
8022 potentially constant evaluated.
8023
8024 So we need to instantiate any constexpr functions mentioned by the
8025 expression even if the definition isn't needed for evaluating the
8026 expression. */
8027
8028 static tree
8029 instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
8030 {
8031 if (TREE_CODE (*tp) == FUNCTION_DECL
8032 && DECL_DECLARED_CONSTEXPR_P (*tp)
8033 && !DECL_INITIAL (*tp)
8034 && !trivial_fn_p (*tp)
8035 && DECL_TEMPLOID_INSTANTIATION (*tp)
8036 && !uid_sensitive_constexpr_evaluation_p ())
8037 {
8038 ++function_depth;
8039 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
8040 --function_depth;
8041 }
8042 else if (TREE_CODE (*tp) == CALL_EXPR
8043 || TREE_CODE (*tp) == AGGR_INIT_EXPR)
8044 {
8045 if (EXPR_HAS_LOCATION (*tp))
8046 input_location = EXPR_LOCATION (*tp);
8047 }
8048
8049 if (!EXPR_P (*tp))
8050 *walk_subtrees = 0;
8051
8052 return NULL_TREE;
8053 }
8054
8055 static void
8056 instantiate_constexpr_fns (tree t)
8057 {
8058 location_t loc = input_location;
8059 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
8060 input_location = loc;
8061 }
8062
8063 /* Look for heap variables in the expression *TP. */
8064
8065 static tree
8066 find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
8067 {
8068 if (VAR_P (*tp)
8069 && (DECL_NAME (*tp) == heap_uninit_identifier
8070 || DECL_NAME (*tp) == heap_identifier
8071 || DECL_NAME (*tp) == heap_vec_uninit_identifier
8072 || DECL_NAME (*tp) == heap_vec_identifier
8073 || DECL_NAME (*tp) == heap_deleted_identifier))
8074 return *tp;
8075
8076 if (TYPE_P (*tp))
8077 *walk_subtrees = 0;
8078 return NULL_TREE;
8079 }
8080
8081 /* Find immediate function decls in *TP if any. */
8082
8083 static tree
8084 find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
8085 {
8086 if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
8087 return *tp;
8088 if (TREE_CODE (*tp) == PTRMEM_CST
8089 && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
8090 && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
8091 return PTRMEM_CST_MEMBER (*tp);
8092 return NULL_TREE;
8093 }
8094
8095 /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
8096 expression. Return a version of T that has TREE_CONSTANT cleared. */
8097
8098 static tree
8099 mark_non_constant (tree t)
8100 {
8101 gcc_checking_assert (TREE_CONSTANT (t));
8102
8103 /* This isn't actually constant, so unset TREE_CONSTANT.
8104 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
8105 it to be set if it is invariant address, even when it is not
8106 a valid C++ constant expression. Wrap it with a NOP_EXPR
8107 instead. */
8108 if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
8109 t = copy_node (t);
8110 else if (TREE_CODE (t) == CONSTRUCTOR)
8111 t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
8112 else
8113 t = build_nop (TREE_TYPE (t), t);
8114 TREE_CONSTANT (t) = false;
8115 return t;
8116 }
8117
8118 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8119 STRICT has the same sense as for constant_value_1: true if we only allow
8120 conforming C++ constant expressions, or false if we want a constant value
8121 even if it doesn't conform.
8122 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8123 per P0595 even when ALLOW_NON_CONSTANT is true.
8124 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
8125 OBJECT must be non-NULL in that case. */
8126
8127 static tree
8128 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
8129 bool strict = true,
8130 bool manifestly_const_eval = false,
8131 bool constexpr_dtor = false,
8132 tree object = NULL_TREE)
8133 {
8134 auto_timevar time (TV_CONSTEXPR);
8135
8136 bool non_constant_p = false;
8137 bool overflow_p = false;
8138
8139 if (BRACE_ENCLOSED_INITIALIZER_P (t))
8140 {
8141 gcc_checking_assert (allow_non_constant);
8142 return t;
8143 }
8144
8145 constexpr_global_ctx global_ctx;
8146 constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
8147 allow_non_constant, strict,
8148 manifestly_const_eval || !allow_non_constant };
8149
8150 /* Turn off -frounding-math for manifestly constant evaluation. */
8151 warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval);
8152 tree type = initialized_type (t);
8153 tree r = t;
8154 bool is_consteval = false;
8155 if (VOID_TYPE_P (type))
8156 {
8157 if (constexpr_dtor)
8158 /* Used for destructors of array elements. */
8159 type = TREE_TYPE (object);
8160 else
8161 {
8162 if (cxx_dialect < cxx20)
8163 return t;
8164 if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
8165 return t;
8166 /* Calls to immediate functions returning void need to be
8167 evaluated. */
8168 tree fndecl = cp_get_callee_fndecl_nofold (t);
8169 if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
8170 return t;
8171 else
8172 is_consteval = true;
8173 }
8174 }
8175 else if (cxx_dialect >= cxx20
8176 && (TREE_CODE (t) == CALL_EXPR
8177 || TREE_CODE (t) == AGGR_INIT_EXPR
8178 || TREE_CODE (t) == TARGET_EXPR))
8179 {
8180 /* For non-concept checks, determine if it is consteval. */
8181 if (!concept_check_p (t))
8182 {
8183 tree x = t;
8184 if (TREE_CODE (x) == TARGET_EXPR)
8185 x = TARGET_EXPR_INITIAL (x);
8186 tree fndecl = cp_get_callee_fndecl_nofold (x);
8187 if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
8188 is_consteval = true;
8189 }
8190 }
8191 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
8192 {
8193 /* In C++14 an NSDMI can participate in aggregate initialization,
8194 and can refer to the address of the object being initialized, so
8195 we need to pass in the relevant VAR_DECL if we want to do the
8196 evaluation in a single pass. The evaluation will dynamically
8197 update ctx.values for the VAR_DECL. We use the same strategy
8198 for C++11 constexpr constructors that refer to the object being
8199 initialized. */
8200 if (constexpr_dtor)
8201 {
8202 gcc_assert (object && VAR_P (object));
8203 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
8204 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
8205 if (error_operand_p (DECL_INITIAL (object)))
8206 return t;
8207 ctx.ctor = unshare_expr (DECL_INITIAL (object));
8208 TREE_READONLY (ctx.ctor) = false;
8209 /* Temporarily force decl_really_constant_value to return false
8210 for it, we want to use ctx.ctor for the current value instead. */
8211 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
8212 }
8213 else
8214 {
8215 ctx.ctor = build_constructor (type, NULL);
8216 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
8217 }
8218 if (!object)
8219 {
8220 if (TREE_CODE (t) == TARGET_EXPR)
8221 object = TARGET_EXPR_SLOT (t);
8222 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
8223 object = AGGR_INIT_EXPR_SLOT (t);
8224 }
8225 ctx.object = object;
8226 if (object)
8227 gcc_assert (same_type_ignoring_top_level_qualifiers_p
8228 (type, TREE_TYPE (object)));
8229 if (object && DECL_P (object))
8230 global_ctx.put_value (object, ctx.ctor);
8231 if (TREE_CODE (r) == TARGET_EXPR)
8232 /* Avoid creating another CONSTRUCTOR when we expand the
8233 TARGET_EXPR. */
8234 r = TARGET_EXPR_INITIAL (r);
8235 }
8236
8237 auto_vec<tree, 16> cleanups;
8238 global_ctx.cleanups = &cleanups;
8239
8240 if (manifestly_const_eval)
8241 instantiate_constexpr_fns (r);
8242 r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
8243 &non_constant_p, &overflow_p);
8244
8245 if (!constexpr_dtor)
8246 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
8247 else
8248 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
8249
8250 unsigned int i;
8251 tree cleanup;
8252 /* Evaluate the cleanups. */
8253 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
8254 cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
8255 &non_constant_p, &overflow_p);
8256
8257 /* Mutable logic is a bit tricky: we want to allow initialization of
8258 constexpr variables with mutable members, but we can't copy those
8259 members to another constexpr variable. */
8260 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
8261 {
8262 if (!allow_non_constant)
8263 error ("%qE is not a constant expression because it refers to "
8264 "mutable subobjects of %qT", t, type);
8265 non_constant_p = true;
8266 }
8267
8268 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
8269 {
8270 if (!allow_non_constant)
8271 error ("%qE is not a constant expression because it refers to "
8272 "an incompletely initialized variable", t);
8273 TREE_CONSTANT (r) = false;
8274 non_constant_p = true;
8275 }
8276
8277 if (!global_ctx.heap_vars.is_empty ())
8278 {
8279 tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
8280 NULL);
8281 unsigned int i;
8282 if (heap_var)
8283 {
8284 if (!allow_non_constant && !non_constant_p)
8285 error_at (DECL_SOURCE_LOCATION (heap_var),
8286 "%qE is not a constant expression because it refers to "
8287 "a result of %<operator new%>", t);
8288 r = t;
8289 non_constant_p = true;
8290 }
8291 FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
8292 {
8293 if (DECL_NAME (heap_var) != heap_deleted_identifier)
8294 {
8295 if (!allow_non_constant && !non_constant_p)
8296 error_at (DECL_SOURCE_LOCATION (heap_var),
8297 "%qE is not a constant expression because allocated "
8298 "storage has not been deallocated", t);
8299 r = t;
8300 non_constant_p = true;
8301 }
8302 varpool_node::get (heap_var)->remove ();
8303 }
8304 }
8305
8306 /* Check that immediate invocation does not return an expression referencing
8307 any immediate function decls. */
8308 if (is_consteval || in_immediate_context ())
8309 if (tree immediate_fndecl
8310 = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
8311 NULL))
8312 {
8313 if (!allow_non_constant && !non_constant_p)
8314 error_at (cp_expr_loc_or_input_loc (t),
8315 "immediate evaluation returns address of immediate "
8316 "function %qD", immediate_fndecl);
8317 r = t;
8318 non_constant_p = true;
8319 }
8320
8321 if (non_constant_p)
8322 /* If we saw something bad, go back to our argument. The wrapping below is
8323 only for the cases of TREE_CONSTANT argument or overflow. */
8324 r = t;
8325
8326 if (!non_constant_p && overflow_p)
8327 non_constant_p = true;
8328
8329 /* Unshare the result. */
8330 bool should_unshare = true;
8331 if (r == t || (TREE_CODE (t) == TARGET_EXPR
8332 && TARGET_EXPR_INITIAL (t) == r))
8333 should_unshare = false;
8334
8335 if (non_constant_p && !allow_non_constant)
8336 return error_mark_node;
8337 else if (constexpr_dtor)
8338 return r;
8339 else if (non_constant_p && TREE_CONSTANT (r))
8340 r = mark_non_constant (r);
8341 else if (non_constant_p)
8342 return t;
8343
8344 if (should_unshare)
8345 r = unshare_expr (r);
8346
8347 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
8348 {
8349 r = adjust_temp_type (type, r);
8350 if (TREE_CODE (t) == TARGET_EXPR
8351 && TARGET_EXPR_INITIAL (t) == r)
8352 return t;
8353 else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR)
8354 /* Don't add a TARGET_EXPR if our argument didn't have one. */;
8355 else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
8356 r = get_target_expr (r);
8357 else
8358 {
8359 r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
8360 TREE_CONSTANT (r) = true;
8361 }
8362 }
8363
8364 /* Remember the original location if that wouldn't need a wrapper. */
8365 if (location_t loc = EXPR_LOCATION (t))
8366 protected_set_expr_location (r, loc);
8367
8368 return r;
8369 }
8370
8371 /* If T represents a constant expression returns its reduced value.
8372 Otherwise return error_mark_node. */
8373
8374 tree
8375 cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
8376 tsubst_flags_t complain /* = tf_error */)
8377 {
8378 bool sfinae = !(complain & tf_error);
8379 tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, true, false, decl);
8380 if (sfinae && !TREE_CONSTANT (r))
8381 r = error_mark_node;
8382 return r;
8383 }
8384
8385 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
8386 of constexpr variables. The actual initializer of DECL is not modified. */
8387
8388 void
8389 cxx_constant_dtor (tree t, tree decl)
8390 {
8391 cxx_eval_outermost_constant_expr (t, false, true, true, true, decl);
8392 }
8393
8394 /* Helper routine for fold_simple function. Either return simplified
8395 expression T, otherwise NULL_TREE.
8396 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
8397 even if we are within template-declaration. So be careful on call, as in
8398 such case types can be undefined. */
8399
8400 static tree
8401 fold_simple_1 (tree t)
8402 {
8403 tree op1;
8404 enum tree_code code = TREE_CODE (t);
8405
8406 switch (code)
8407 {
8408 case INTEGER_CST:
8409 case REAL_CST:
8410 case VECTOR_CST:
8411 case FIXED_CST:
8412 case COMPLEX_CST:
8413 return t;
8414
8415 case SIZEOF_EXPR:
8416 return fold_sizeof_expr (t);
8417
8418 case ABS_EXPR:
8419 case ABSU_EXPR:
8420 case CONJ_EXPR:
8421 case REALPART_EXPR:
8422 case IMAGPART_EXPR:
8423 case NEGATE_EXPR:
8424 case BIT_NOT_EXPR:
8425 case TRUTH_NOT_EXPR:
8426 case VIEW_CONVERT_EXPR:
8427 CASE_CONVERT:
8428 case FLOAT_EXPR:
8429 case FIX_TRUNC_EXPR:
8430 case FIXED_CONVERT_EXPR:
8431 case ADDR_SPACE_CONVERT_EXPR:
8432
8433 op1 = TREE_OPERAND (t, 0);
8434
8435 t = const_unop (code, TREE_TYPE (t), op1);
8436 if (!t)
8437 return NULL_TREE;
8438
8439 if (CONVERT_EXPR_CODE_P (code)
8440 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
8441 TREE_OVERFLOW (t) = false;
8442 return t;
8443
8444 default:
8445 return NULL_TREE;
8446 }
8447 }
8448
8449 /* If T is a simple constant expression, returns its simplified value.
8450 Otherwise returns T. In contrast to maybe_constant_value we
8451 simplify only few operations on constant-expressions, and we don't
8452 try to simplify constexpressions. */
8453
8454 tree
8455 fold_simple (tree t)
8456 {
8457 if (processing_template_decl)
8458 return t;
8459
8460 tree r = fold_simple_1 (t);
8461 if (r)
8462 return r;
8463
8464 return t;
8465 }
8466
8467 /* If T is a constant expression, returns its reduced value.
8468 Otherwise, if T does not have TREE_CONSTANT set, returns T.
8469 Otherwise, returns a version of T without TREE_CONSTANT.
8470 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
8471 as per P0595. */
8472
8473 static GTY((deletable)) hash_map<tree, tree> *cv_cache;
8474
8475 tree
8476 maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
8477 {
8478 tree r;
8479
8480 if (!is_nondependent_constant_expression (t))
8481 {
8482 if (TREE_OVERFLOW_P (t)
8483 || (!processing_template_decl && TREE_CONSTANT (t)))
8484 t = mark_non_constant (t);
8485 return t;
8486 }
8487 else if (CONSTANT_CLASS_P (t))
8488 /* No caching or evaluation needed. */
8489 return t;
8490
8491 if (manifestly_const_eval)
8492 return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl);
8493
8494 if (cv_cache == NULL)
8495 cv_cache = hash_map<tree, tree>::create_ggc (101);
8496 if (tree *cached = cv_cache->get (t))
8497 {
8498 r = *cached;
8499 if (r != t)
8500 {
8501 r = break_out_target_exprs (r, /*clear_loc*/true);
8502 protected_set_expr_location (r, EXPR_LOCATION (t));
8503 }
8504 return r;
8505 }
8506
8507 /* Don't evaluate an unevaluated operand. */
8508 if (cp_unevaluated_operand)
8509 return t;
8510
8511 uid_sensitive_constexpr_evaluation_checker c;
8512 r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl);
8513 gcc_checking_assert (r == t
8514 || CONVERT_EXPR_P (t)
8515 || TREE_CODE (t) == VIEW_CONVERT_EXPR
8516 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8517 || !cp_tree_equal (r, t));
8518 if (!c.evaluation_restricted_p ())
8519 cv_cache->put (t, r);
8520 return r;
8521 }
8522
8523 /* Dispose of the whole CV_CACHE. */
8524
8525 static void
8526 clear_cv_cache (void)
8527 {
8528 if (cv_cache != NULL)
8529 cv_cache->empty ();
8530 }
8531
8532 /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
8533
8534 void
8535 clear_cv_and_fold_caches ()
8536 {
8537 clear_cv_cache ();
8538 clear_fold_cache ();
8539 }
8540
8541 /* Internal function handling expressions in templates for
8542 fold_non_dependent_expr and fold_non_dependent_init.
8543
8544 If we're in a template, but T isn't value dependent, simplify
8545 it. We're supposed to treat:
8546
8547 template <typename T> void f(T[1 + 1]);
8548 template <typename T> void f(T[2]);
8549
8550 as two declarations of the same function, for example. */
8551
8552 static tree
8553 fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
8554 bool manifestly_const_eval,
8555 tree object)
8556 {
8557 gcc_assert (processing_template_decl);
8558
8559 if (is_nondependent_constant_expression (t))
8560 {
8561 processing_template_decl_sentinel s;
8562 t = instantiate_non_dependent_expr_internal (t, complain);
8563
8564 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
8565 {
8566 if (TREE_OVERFLOW_P (t))
8567 {
8568 t = build_nop (TREE_TYPE (t), t);
8569 TREE_CONSTANT (t) = false;
8570 }
8571 return t;
8572 }
8573
8574 if (cp_unevaluated_operand && !manifestly_const_eval)
8575 return t;
8576
8577 tree r = cxx_eval_outermost_constant_expr (t, true, true,
8578 manifestly_const_eval,
8579 false, object);
8580 /* cp_tree_equal looks through NOPs, so allow them. */
8581 gcc_checking_assert (r == t
8582 || CONVERT_EXPR_P (t)
8583 || TREE_CODE (t) == VIEW_CONVERT_EXPR
8584 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
8585 || !cp_tree_equal (r, t));
8586 return r;
8587 }
8588 else if (TREE_OVERFLOW_P (t))
8589 {
8590 t = build_nop (TREE_TYPE (t), t);
8591 TREE_CONSTANT (t) = false;
8592 }
8593
8594 return t;
8595 }
8596
8597 /* Like maybe_constant_value but first fully instantiate the argument.
8598
8599 Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
8600 followed by maybe_constant_value but is more efficient,
8601 because it calls instantiation_dependent_expression_p and
8602 potential_constant_expression at most once.
8603 The manifestly_const_eval argument is passed to maybe_constant_value.
8604
8605 Callers should generally pass their active complain, or if they are in a
8606 non-template, diagnosing context, they can use the default of
8607 tf_warning_or_error. Callers that might be within a template context, don't
8608 have a complain parameter, and aren't going to remember the result for long
8609 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
8610 appropriately. */
8611
8612 tree
8613 fold_non_dependent_expr (tree t,
8614 tsubst_flags_t complain /* = tf_warning_or_error */,
8615 bool manifestly_const_eval /* = false */,
8616 tree object /* = NULL_TREE */)
8617 {
8618 if (t == NULL_TREE)
8619 return NULL_TREE;
8620
8621 if (processing_template_decl)
8622 return fold_non_dependent_expr_template (t, complain,
8623 manifestly_const_eval, object);
8624
8625 return maybe_constant_value (t, object, manifestly_const_eval);
8626 }
8627
8628 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
8629 return the original expression. */
8630
8631 tree
8632 maybe_fold_non_dependent_expr (tree expr,
8633 tsubst_flags_t complain/*=tf_warning_or_error*/)
8634 {
8635 tree t = fold_non_dependent_expr (expr, complain);
8636 if (t && TREE_CONSTANT (t))
8637 return t;
8638
8639 return expr;
8640 }
8641
8642 /* Like maybe_constant_init but first fully instantiate the argument. */
8643
8644 tree
8645 fold_non_dependent_init (tree t,
8646 tsubst_flags_t complain /*=tf_warning_or_error*/,
8647 bool manifestly_const_eval /*=false*/,
8648 tree object /* = NULL_TREE */)
8649 {
8650 if (t == NULL_TREE)
8651 return NULL_TREE;
8652
8653 if (processing_template_decl)
8654 {
8655 t = fold_non_dependent_expr_template (t, complain,
8656 manifestly_const_eval, object);
8657 /* maybe_constant_init does this stripping, so do it here too. */
8658 if (TREE_CODE (t) == TARGET_EXPR)
8659 {
8660 tree init = TARGET_EXPR_INITIAL (t);
8661 if (TREE_CODE (init) == CONSTRUCTOR)
8662 t = init;
8663 }
8664 return t;
8665 }
8666
8667 return maybe_constant_init (t, object, manifestly_const_eval);
8668 }
8669
8670 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
8671 than wrapped in a TARGET_EXPR.
8672 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
8673 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8674 per P0595 even when ALLOW_NON_CONSTANT is true. */
8675
8676 static tree
8677 maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
8678 bool manifestly_const_eval)
8679 {
8680 if (!t)
8681 return t;
8682 if (TREE_CODE (t) == EXPR_STMT)
8683 t = TREE_OPERAND (t, 0);
8684 if (TREE_CODE (t) == CONVERT_EXPR
8685 && VOID_TYPE_P (TREE_TYPE (t)))
8686 t = TREE_OPERAND (t, 0);
8687 if (TREE_CODE (t) == INIT_EXPR)
8688 t = TREE_OPERAND (t, 1);
8689 if (TREE_CODE (t) == TARGET_EXPR)
8690 t = TARGET_EXPR_INITIAL (t);
8691 if (!is_nondependent_static_init_expression (t))
8692 /* Don't try to evaluate it. */;
8693 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
8694 /* No evaluation needed. */;
8695 else
8696 {
8697 /* [basic.start.static] allows constant-initialization of variables with
8698 static or thread storage duration even if it isn't required, but we
8699 shouldn't bend the rules the same way for automatic variables. */
8700 bool is_static = (decl && DECL_P (decl)
8701 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
8702 t = cxx_eval_outermost_constant_expr (t, allow_non_constant, !is_static,
8703 manifestly_const_eval, false, decl);
8704 }
8705 if (TREE_CODE (t) == TARGET_EXPR)
8706 {
8707 tree init = TARGET_EXPR_INITIAL (t);
8708 if (TREE_CODE (init) == CONSTRUCTOR)
8709 t = init;
8710 }
8711 return t;
8712 }
8713
8714 /* Wrapper for maybe_constant_init_1 which permits non constants. */
8715
8716 tree
8717 maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
8718 {
8719 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
8720 }
8721
8722 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
8723
8724 tree
8725 cxx_constant_init (tree t, tree decl)
8726 {
8727 return maybe_constant_init_1 (t, decl, false, true);
8728 }
8729
8730 #if 0
8731 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
8732 /* Return true if the object referred to by REF has automatic or thread
8733 local storage. */
8734
8735 enum { ck_ok, ck_bad, ck_unknown };
8736 static int
8737 check_automatic_or_tls (tree ref)
8738 {
8739 machine_mode mode;
8740 poly_int64 bitsize, bitpos;
8741 tree offset;
8742 int volatilep = 0, unsignedp = 0;
8743 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
8744 &mode, &unsignedp, &volatilep, false);
8745 duration_kind dk;
8746
8747 /* If there isn't a decl in the middle, we don't know the linkage here,
8748 and this isn't a constant expression anyway. */
8749 if (!DECL_P (decl))
8750 return ck_unknown;
8751 dk = decl_storage_duration (decl);
8752 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
8753 }
8754 #endif
8755
8756 /* Data structure for passing data from potential_constant_expression_1
8757 to check_for_return_continue via cp_walk_tree. */
8758 struct check_for_return_continue_data {
8759 hash_set<tree> *pset;
8760 tree continue_stmt;
8761 tree break_stmt;
8762 };
8763
8764 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
8765 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
8766 the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found. */
8767 static tree
8768 check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
8769 {
8770 tree t = *tp, s, b;
8771 check_for_return_continue_data *d = (check_for_return_continue_data *) data;
8772 switch (TREE_CODE (t))
8773 {
8774 case RETURN_EXPR:
8775 return t;
8776
8777 case CONTINUE_STMT:
8778 if (d->continue_stmt == NULL_TREE)
8779 d->continue_stmt = t;
8780 break;
8781
8782 case BREAK_STMT:
8783 if (d->break_stmt == NULL_TREE)
8784 d->break_stmt = t;
8785 break;
8786
8787 #define RECUR(x) \
8788 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
8789 d->pset)) \
8790 return r
8791
8792 /* For loops, walk subtrees manually, so that continue stmts found
8793 inside of the bodies of the loops are ignored. */
8794 case DO_STMT:
8795 *walk_subtrees = 0;
8796 RECUR (DO_COND (t));
8797 s = d->continue_stmt;
8798 b = d->break_stmt;
8799 RECUR (DO_BODY (t));
8800 d->continue_stmt = s;
8801 d->break_stmt = b;
8802 break;
8803
8804 case WHILE_STMT:
8805 *walk_subtrees = 0;
8806 RECUR (WHILE_COND (t));
8807 s = d->continue_stmt;
8808 b = d->break_stmt;
8809 RECUR (WHILE_BODY (t));
8810 d->continue_stmt = s;
8811 d->break_stmt = b;
8812 break;
8813
8814 case FOR_STMT:
8815 *walk_subtrees = 0;
8816 RECUR (FOR_INIT_STMT (t));
8817 RECUR (FOR_COND (t));
8818 RECUR (FOR_EXPR (t));
8819 s = d->continue_stmt;
8820 b = d->break_stmt;
8821 RECUR (FOR_BODY (t));
8822 d->continue_stmt = s;
8823 d->break_stmt = b;
8824 break;
8825
8826 case RANGE_FOR_STMT:
8827 *walk_subtrees = 0;
8828 RECUR (RANGE_FOR_EXPR (t));
8829 s = d->continue_stmt;
8830 b = d->break_stmt;
8831 RECUR (RANGE_FOR_BODY (t));
8832 d->continue_stmt = s;
8833 d->break_stmt = b;
8834 break;
8835
8836 case SWITCH_STMT:
8837 *walk_subtrees = 0;
8838 RECUR (SWITCH_STMT_COND (t));
8839 b = d->break_stmt;
8840 RECUR (SWITCH_STMT_BODY (t));
8841 d->break_stmt = b;
8842 break;
8843 #undef RECUR
8844
8845 case STATEMENT_LIST:
8846 case CONSTRUCTOR:
8847 break;
8848
8849 default:
8850 if (!EXPR_P (t))
8851 *walk_subtrees = 0;
8852 break;
8853 }
8854
8855 return NULL_TREE;
8856 }
8857
8858 /* Return true if T denotes a potentially constant expression. Issue
8859 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
8860 an lvalue-rvalue conversion is implied. If NOW is true, we want to
8861 consider the expression in the current context, independent of constexpr
8862 substitution. If FUNDEF_P is true, we're checking a constexpr function body
8863 and hard errors should not be reported by constexpr_error.
8864
8865 C++0x [expr.const] used to say
8866
8867 6 An expression is a potential constant expression if it is
8868 a constant expression where all occurrences of function
8869 parameters are replaced by arbitrary constant expressions
8870 of the appropriate type.
8871
8872 2 A conditional expression is a constant expression unless it
8873 involves one of the following as a potentially evaluated
8874 subexpression (3.2), but subexpressions of logical AND (5.14),
8875 logical OR (5.15), and conditional (5.16) operations that are
8876 not evaluated are not considered. */
8877
8878 static bool
8879 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
8880 bool fundef_p, tsubst_flags_t flags,
8881 tree *jump_target)
8882 {
8883 #define RECUR(T,RV) \
8884 potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
8885 jump_target)
8886
8887 enum { any = false, rval = true };
8888 int i;
8889 tree tmp;
8890
8891 if (t == error_mark_node)
8892 return false;
8893 if (t == NULL_TREE)
8894 return true;
8895 location_t loc = cp_expr_loc_or_input_loc (t);
8896
8897 if (*jump_target)
8898 /* If we are jumping, ignore everything. This is simpler than the
8899 cxx_eval_constant_expression handling because we only need to be
8900 conservatively correct, and we don't necessarily have a constant value
8901 available, so we don't bother with switch tracking. */
8902 return true;
8903
8904 if (TREE_THIS_VOLATILE (t) && want_rval)
8905 {
8906 if (flags & tf_error)
8907 constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
8908 "a volatile lvalue %qE with type %qT", t,
8909 TREE_TYPE (t));
8910 return false;
8911 }
8912 if (CONSTANT_CLASS_P (t))
8913 return true;
8914 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
8915 && TREE_TYPE (t) == error_mark_node)
8916 return false;
8917
8918 switch (TREE_CODE (t))
8919 {
8920 case FUNCTION_DECL:
8921 case BASELINK:
8922 case TEMPLATE_DECL:
8923 case OVERLOAD:
8924 case TEMPLATE_ID_EXPR:
8925 case LABEL_DECL:
8926 case CASE_LABEL_EXPR:
8927 case PREDICT_EXPR:
8928 case CONST_DECL:
8929 case SIZEOF_EXPR:
8930 case ALIGNOF_EXPR:
8931 case OFFSETOF_EXPR:
8932 case NOEXCEPT_EXPR:
8933 case TEMPLATE_PARM_INDEX:
8934 case TRAIT_EXPR:
8935 case IDENTIFIER_NODE:
8936 case USERDEF_LITERAL:
8937 /* We can see a FIELD_DECL in a pointer-to-member expression. */
8938 case FIELD_DECL:
8939 case RESULT_DECL:
8940 case USING_DECL:
8941 case USING_STMT:
8942 case PLACEHOLDER_EXPR:
8943 case REQUIRES_EXPR:
8944 case STATIC_ASSERT:
8945 case DEBUG_BEGIN_STMT:
8946 return true;
8947
8948 case RETURN_EXPR:
8949 if (!RECUR (TREE_OPERAND (t, 0), any))
8950 return false;
8951 /* FALLTHROUGH */
8952
8953 case BREAK_STMT:
8954 case CONTINUE_STMT:
8955 *jump_target = t;
8956 return true;
8957
8958 case PARM_DECL:
8959 if (now && want_rval)
8960 {
8961 tree type = TREE_TYPE (t);
8962 if ((processing_template_decl && !COMPLETE_TYPE_P (type))
8963 || dependent_type_p (type)
8964 || is_really_empty_class (type, /*ignore_vptr*/false))
8965 /* An empty class has no data to read. */
8966 return true;
8967 if (flags & tf_error)
8968 constexpr_error (input_location, fundef_p,
8969 "%qE is not a constant expression", t);
8970 return false;
8971 }
8972 return true;
8973
8974 case AGGR_INIT_EXPR:
8975 case CALL_EXPR:
8976 /* -- an invocation of a function other than a constexpr function
8977 or a constexpr constructor. */
8978 {
8979 tree fun = get_function_named_in_call (t);
8980 const int nargs = call_expr_nargs (t);
8981 i = 0;
8982
8983 if (fun == NULL_TREE)
8984 {
8985 /* Reset to allow the function to continue past the end
8986 of the block below. Otherwise return early. */
8987 bool bail = true;
8988
8989 if (TREE_CODE (t) == CALL_EXPR
8990 && CALL_EXPR_FN (t) == NULL_TREE)
8991 switch (CALL_EXPR_IFN (t))
8992 {
8993 /* These should be ignored, they are optimized away from
8994 constexpr functions. */
8995 case IFN_UBSAN_NULL:
8996 case IFN_UBSAN_BOUNDS:
8997 case IFN_UBSAN_VPTR:
8998 case IFN_FALLTHROUGH:
8999 case IFN_ASSUME:
9000 return true;
9001
9002 case IFN_ADD_OVERFLOW:
9003 case IFN_SUB_OVERFLOW:
9004 case IFN_MUL_OVERFLOW:
9005 case IFN_LAUNDER:
9006 case IFN_VEC_CONVERT:
9007 bail = false;
9008 break;
9009
9010 default:
9011 break;
9012 }
9013
9014 if (bail)
9015 {
9016 /* fold_call_expr can't do anything with IFN calls. */
9017 if (flags & tf_error)
9018 constexpr_error (loc, fundef_p,
9019 "call to internal function %qE", t);
9020 return false;
9021 }
9022 }
9023
9024 if (fun && is_overloaded_fn (fun))
9025 {
9026 if (TREE_CODE (fun) == FUNCTION_DECL)
9027 {
9028 if (builtin_valid_in_constant_expr_p (fun))
9029 return true;
9030 if (!maybe_constexpr_fn (fun)
9031 /* Allow any built-in function; if the expansion
9032 isn't constant, we'll deal with that then. */
9033 && !fndecl_built_in_p (fun)
9034 /* In C++20, replaceable global allocation functions
9035 are constant expressions. */
9036 && (!cxx_replaceable_global_alloc_fn (fun)
9037 || TREE_CODE (t) != CALL_EXPR
9038 || (!CALL_FROM_NEW_OR_DELETE_P (t)
9039 && (current_function_decl == NULL_TREE
9040 || !is_std_allocator_allocate
9041 (current_function_decl))))
9042 /* Allow placement new in std::construct_at. */
9043 && (!cxx_placement_new_fn (fun)
9044 || TREE_CODE (t) != CALL_EXPR
9045 || current_function_decl == NULL_TREE
9046 || !is_std_construct_at (current_function_decl))
9047 && !cxx_dynamic_cast_fn_p (fun))
9048 {
9049 if ((flags & tf_error)
9050 && constexpr_error (loc, fundef_p,
9051 "call to non-%<constexpr%> "
9052 "function %qD", fun))
9053 explain_invalid_constexpr_fn (fun);
9054 return false;
9055 }
9056 /* A call to a non-static member function takes the address
9057 of the object as the first argument. But in a constant
9058 expression the address will be folded away, so look
9059 through it now. */
9060 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
9061 && !DECL_CONSTRUCTOR_P (fun))
9062 {
9063 tree x = get_nth_callarg (t, 0);
9064 if (is_this_parameter (x))
9065 return true;
9066 /* Don't require an immediately constant value, as
9067 constexpr substitution might not use the value. */
9068 bool sub_now = false;
9069 if (!potential_constant_expression_1 (x, rval, strict,
9070 sub_now, fundef_p,
9071 flags, jump_target))
9072 return false;
9073 i = 1;
9074 }
9075 }
9076 else
9077 {
9078 if (!RECUR (fun, true))
9079 return false;
9080 fun = get_first_fn (fun);
9081 }
9082 /* Skip initial arguments to base constructors. */
9083 if (DECL_BASE_CONSTRUCTOR_P (fun))
9084 i = num_artificial_parms_for (fun);
9085 fun = DECL_ORIGIN (fun);
9086 }
9087 else if (fun)
9088 {
9089 if (RECUR (fun, rval))
9090 /* Might end up being a constant function pointer. */;
9091 else
9092 return false;
9093 }
9094 for (; i < nargs; ++i)
9095 {
9096 tree x = get_nth_callarg (t, i);
9097 /* In a template, reference arguments haven't been converted to
9098 REFERENCE_TYPE and we might not even know if the parameter
9099 is a reference, so accept lvalue constants too. */
9100 bool rv = processing_template_decl ? any : rval;
9101 /* Don't require an immediately constant value, as constexpr
9102 substitution might not use the value of the argument. */
9103 bool sub_now = false;
9104 if (!potential_constant_expression_1 (x, rv, strict,
9105 sub_now, fundef_p, flags,
9106 jump_target))
9107 return false;
9108 }
9109 return true;
9110 }
9111
9112 case NON_LVALUE_EXPR:
9113 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
9114 -- an lvalue of integral type that refers to a non-volatile
9115 const variable or static data member initialized with
9116 constant expressions, or
9117
9118 -- an lvalue of literal type that refers to non-volatile
9119 object defined with constexpr, or that refers to a
9120 sub-object of such an object; */
9121 return RECUR (TREE_OPERAND (t, 0), rval);
9122
9123 case EXCESS_PRECISION_EXPR:
9124 return RECUR (TREE_OPERAND (t, 0), rval);
9125
9126 case VAR_DECL:
9127 if (DECL_HAS_VALUE_EXPR_P (t))
9128 {
9129 if (now && is_normal_capture_proxy (t))
9130 {
9131 /* -- in a lambda-expression, a reference to this or to a
9132 variable with automatic storage duration defined outside that
9133 lambda-expression, where the reference would be an
9134 odr-use. */
9135
9136 if (want_rval)
9137 /* Since we're doing an lvalue-rvalue conversion, this might
9138 not be an odr-use, so evaluate the variable directly. */
9139 return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
9140
9141 if (flags & tf_error)
9142 {
9143 tree cap = DECL_CAPTURED_VARIABLE (t);
9144 if (constexpr_error (input_location, fundef_p,
9145 "lambda capture of %qE is not a "
9146 "constant expression", cap)
9147 && decl_constant_var_p (cap))
9148 inform (input_location, "because it is used as a glvalue");
9149 }
9150 return false;
9151 }
9152 /* Treat __PRETTY_FUNCTION__ inside a template function as
9153 potentially-constant. */
9154 else if (DECL_PRETTY_FUNCTION_P (t)
9155 && DECL_VALUE_EXPR (t) == error_mark_node)
9156 return true;
9157 return RECUR (DECL_VALUE_EXPR (t), rval);
9158 }
9159 if (want_rval
9160 && !var_in_maybe_constexpr_fn (t)
9161 && !type_dependent_expression_p (t)
9162 && !decl_maybe_constant_var_p (t)
9163 && (strict
9164 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
9165 || (DECL_INITIAL (t)
9166 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
9167 && COMPLETE_TYPE_P (TREE_TYPE (t))
9168 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9169 {
9170 if (flags & tf_error)
9171 non_const_var_error (loc, t, fundef_p);
9172 return false;
9173 }
9174 return true;
9175
9176 case NOP_EXPR:
9177 if (REINTERPRET_CAST_P (t))
9178 {
9179 if (flags & tf_error)
9180 constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
9181 "constant expression");
9182 return false;
9183 }
9184 /* FALLTHRU */
9185 case CONVERT_EXPR:
9186 case VIEW_CONVERT_EXPR:
9187 /* -- a reinterpret_cast. FIXME not implemented, and this rule
9188 may change to something more specific to type-punning (DR 1312). */
9189 {
9190 tree from = TREE_OPERAND (t, 0);
9191 if (location_wrapper_p (t))
9192 {
9193 iloc_sentinel ils = loc;
9194 return (RECUR (from, want_rval));
9195 }
9196 if (INDIRECT_TYPE_P (TREE_TYPE (t)))
9197 {
9198 STRIP_ANY_LOCATION_WRAPPER (from);
9199 if (TREE_CODE (from) == INTEGER_CST
9200 && !integer_zerop (from))
9201 {
9202 if (flags & tf_error)
9203 constexpr_error (loc, fundef_p,
9204 "%<reinterpret_cast%> from integer to "
9205 "pointer");
9206 return false;
9207 }
9208 }
9209 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
9210 }
9211
9212 case ADDRESSOF_EXPR:
9213 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
9214 t = TREE_OPERAND (t, 0);
9215 goto handle_addr_expr;
9216
9217 case ADDR_EXPR:
9218 /* -- a unary operator & that is applied to an lvalue that
9219 designates an object with thread or automatic storage
9220 duration; */
9221 t = TREE_OPERAND (t, 0);
9222
9223 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
9224 /* A pointer-to-member constant. */
9225 return true;
9226
9227 handle_addr_expr:
9228 #if 0
9229 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
9230 any checking here, as we might dereference the pointer later. If
9231 we remove this code, also remove check_automatic_or_tls. */
9232 i = check_automatic_or_tls (t);
9233 if (i == ck_ok)
9234 return true;
9235 if (i == ck_bad)
9236 {
9237 if (flags & tf_error)
9238 error ("address-of an object %qE with thread local or "
9239 "automatic storage is not a constant expression", t);
9240 return false;
9241 }
9242 #endif
9243 return RECUR (t, any);
9244
9245 case COMPONENT_REF:
9246 case ARROW_EXPR:
9247 case OFFSET_REF:
9248 /* -- a class member access unless its postfix-expression is
9249 of literal type or of pointer to literal type. */
9250 /* This test would be redundant, as it follows from the
9251 postfix-expression being a potential constant expression. */
9252 if (type_unknown_p (t))
9253 return true;
9254 if (is_overloaded_fn (t))
9255 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
9256 which uses ob as an lvalue. */
9257 want_rval = false;
9258 gcc_fallthrough ();
9259
9260 case REALPART_EXPR:
9261 case IMAGPART_EXPR:
9262 case BIT_FIELD_REF:
9263 return RECUR (TREE_OPERAND (t, 0), want_rval);
9264
9265 case EXPR_PACK_EXPANSION:
9266 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
9267
9268 case INDIRECT_REF:
9269 {
9270 tree x = TREE_OPERAND (t, 0);
9271 STRIP_NOPS (x);
9272 if (is_this_parameter (x) && !is_capture_proxy (x))
9273 {
9274 if (!var_in_maybe_constexpr_fn (x))
9275 {
9276 if (flags & tf_error)
9277 constexpr_error (loc, fundef_p, "use of %<this%> in a "
9278 "constant expression");
9279 return false;
9280 }
9281 return true;
9282 }
9283 return RECUR (x, rval);
9284 }
9285
9286 case STATEMENT_LIST:
9287 for (tree stmt : tsi_range (t))
9288 if (!RECUR (stmt, any))
9289 return false;
9290 return true;
9291
9292 case MODIFY_EXPR:
9293 if (cxx_dialect < cxx14)
9294 goto fail;
9295 if (!RECUR (TREE_OPERAND (t, 0), any))
9296 return false;
9297 /* Just ignore clobbers. */
9298 if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
9299 return true;
9300 if (!RECUR (TREE_OPERAND (t, 1), rval))
9301 return false;
9302 return true;
9303
9304 case MODOP_EXPR:
9305 if (cxx_dialect < cxx14)
9306 goto fail;
9307 if (!RECUR (TREE_OPERAND (t, 0), rval))
9308 return false;
9309 if (!RECUR (TREE_OPERAND (t, 2), rval))
9310 return false;
9311 return true;
9312
9313 case DO_STMT:
9314 if (!RECUR (DO_COND (t), rval))
9315 return false;
9316 if (!RECUR (DO_BODY (t), any))
9317 return false;
9318 if (breaks (jump_target) || continues (jump_target))
9319 *jump_target = NULL_TREE;
9320 return true;
9321
9322 case FOR_STMT:
9323 if (!RECUR (FOR_INIT_STMT (t), any))
9324 return false;
9325 tmp = FOR_COND (t);
9326 if (!RECUR (tmp, rval))
9327 return false;
9328 if (tmp)
9329 {
9330 if (!processing_template_decl)
9331 tmp = cxx_eval_outermost_constant_expr (tmp, true);
9332 /* If we couldn't evaluate the condition, it might not ever be
9333 true. */
9334 if (!integer_onep (tmp))
9335 {
9336 /* Before returning true, check if the for body can contain
9337 a return. */
9338 hash_set<tree> pset;
9339 check_for_return_continue_data data = { &pset, NULL_TREE,
9340 NULL_TREE };
9341 if (tree ret_expr
9342 = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
9343 &data, &pset))
9344 *jump_target = ret_expr;
9345 return true;
9346 }
9347 }
9348 if (!RECUR (FOR_EXPR (t), any))
9349 return false;
9350 if (!RECUR (FOR_BODY (t), any))
9351 return false;
9352 if (breaks (jump_target) || continues (jump_target))
9353 *jump_target = NULL_TREE;
9354 return true;
9355
9356 case RANGE_FOR_STMT:
9357 if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
9358 return false;
9359 if (!RECUR (RANGE_FOR_EXPR (t), any))
9360 return false;
9361 if (!RECUR (RANGE_FOR_BODY (t), any))
9362 return false;
9363 if (breaks (jump_target) || continues (jump_target))
9364 *jump_target = NULL_TREE;
9365 return true;
9366
9367 case WHILE_STMT:
9368 tmp = WHILE_COND (t);
9369 if (!RECUR (tmp, rval))
9370 return false;
9371 if (!processing_template_decl)
9372 tmp = cxx_eval_outermost_constant_expr (tmp, true);
9373 /* If we couldn't evaluate the condition, it might not ever be true. */
9374 if (!integer_onep (tmp))
9375 {
9376 /* Before returning true, check if the while body can contain
9377 a return. */
9378 hash_set<tree> pset;
9379 check_for_return_continue_data data = { &pset, NULL_TREE,
9380 NULL_TREE };
9381 if (tree ret_expr
9382 = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
9383 &data, &pset))
9384 *jump_target = ret_expr;
9385 return true;
9386 }
9387 if (!RECUR (WHILE_BODY (t), any))
9388 return false;
9389 if (breaks (jump_target) || continues (jump_target))
9390 *jump_target = NULL_TREE;
9391 return true;
9392
9393 case SWITCH_STMT:
9394 if (!RECUR (SWITCH_STMT_COND (t), rval))
9395 return false;
9396 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
9397 unreachable labels would be checked and it is enough if there is
9398 a single switch cond value for which it is a valid constant
9399 expression. We need to check if there are any RETURN_EXPRs
9400 or CONTINUE_STMTs inside of the body though, as in that case
9401 we need to set *jump_target. */
9402 else
9403 {
9404 hash_set<tree> pset;
9405 check_for_return_continue_data data = { &pset, NULL_TREE,
9406 NULL_TREE };
9407 if (tree ret_expr
9408 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
9409 &data, &pset))
9410 /* The switch might return. */
9411 *jump_target = ret_expr;
9412 else if (data.continue_stmt)
9413 /* The switch can't return, but might continue. */
9414 *jump_target = data.continue_stmt;
9415 }
9416 return true;
9417
9418 case STMT_EXPR:
9419 return RECUR (STMT_EXPR_STMT (t), rval);
9420
9421 case LAMBDA_EXPR:
9422 if (cxx_dialect >= cxx17)
9423 /* In C++17 lambdas can be constexpr, don't give up yet. */
9424 return true;
9425 else if (flags & tf_error)
9426 constexpr_error (loc, fundef_p, "lambda-expression is not a "
9427 "constant expression before C++17");
9428 return false;
9429
9430 case NEW_EXPR:
9431 case VEC_NEW_EXPR:
9432 case DELETE_EXPR:
9433 case VEC_DELETE_EXPR:
9434 if (cxx_dialect >= cxx20)
9435 /* In C++20, new-expressions are potentially constant. */
9436 return true;
9437 else if (flags & tf_error)
9438 constexpr_error (loc, fundef_p, "new-expression is not a "
9439 "constant expression before C++20");
9440 return false;
9441
9442 case DYNAMIC_CAST_EXPR:
9443 case PSEUDO_DTOR_EXPR:
9444 case THROW_EXPR:
9445 case OMP_PARALLEL:
9446 case OMP_TASK:
9447 case OMP_FOR:
9448 case OMP_SIMD:
9449 case OMP_DISTRIBUTE:
9450 case OMP_TASKLOOP:
9451 case OMP_LOOP:
9452 case OMP_TEAMS:
9453 case OMP_TARGET_DATA:
9454 case OMP_TARGET:
9455 case OMP_SECTIONS:
9456 case OMP_ORDERED:
9457 case OMP_CRITICAL:
9458 case OMP_SINGLE:
9459 case OMP_SECTION:
9460 case OMP_MASTER:
9461 case OMP_MASKED:
9462 case OMP_TASKGROUP:
9463 case OMP_TARGET_UPDATE:
9464 case OMP_TARGET_ENTER_DATA:
9465 case OMP_TARGET_EXIT_DATA:
9466 case OMP_ATOMIC:
9467 case OMP_ATOMIC_READ:
9468 case OMP_ATOMIC_CAPTURE_OLD:
9469 case OMP_ATOMIC_CAPTURE_NEW:
9470 case OMP_DEPOBJ:
9471 case OACC_PARALLEL:
9472 case OACC_KERNELS:
9473 case OACC_SERIAL:
9474 case OACC_DATA:
9475 case OACC_HOST_DATA:
9476 case OACC_LOOP:
9477 case OACC_CACHE:
9478 case OACC_DECLARE:
9479 case OACC_ENTER_DATA:
9480 case OACC_EXIT_DATA:
9481 case OACC_UPDATE:
9482 /* GCC internal stuff. */
9483 case VA_ARG_EXPR:
9484 case TRANSACTION_EXPR:
9485 case AT_ENCODE_EXPR:
9486 fail:
9487 if (flags & tf_error)
9488 constexpr_error (loc, fundef_p, "expression %qE is not a constant "
9489 "expression", t);
9490 return false;
9491
9492 case ASM_EXPR:
9493 if (flags & tf_error)
9494 inline_asm_in_constexpr_error (loc, fundef_p);
9495 return false;
9496
9497 case OBJ_TYPE_REF:
9498 if (cxx_dialect >= cxx20)
9499 /* In C++20 virtual calls can be constexpr, don't give up yet. */
9500 return true;
9501 else if (flags & tf_error)
9502 constexpr_error (loc, fundef_p, "virtual functions cannot be "
9503 "%<constexpr%> before C++20");
9504 return false;
9505
9506 case TYPEID_EXPR:
9507 /* In C++20, a typeid expression whose operand is of polymorphic
9508 class type can be constexpr. */
9509 {
9510 tree e = TREE_OPERAND (t, 0);
9511 if (cxx_dialect < cxx20
9512 && strict
9513 && !TYPE_P (e)
9514 && !type_dependent_expression_p (e)
9515 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
9516 {
9517 if (flags & tf_error)
9518 constexpr_error (loc, fundef_p, "%<typeid%> is not a "
9519 "constant expression because %qE is "
9520 "of polymorphic type", e);
9521 return false;
9522 }
9523 return true;
9524 }
9525
9526 case POINTER_DIFF_EXPR:
9527 case MINUS_EXPR:
9528 want_rval = true;
9529 goto binary;
9530
9531 case LT_EXPR:
9532 case LE_EXPR:
9533 case GT_EXPR:
9534 case GE_EXPR:
9535 case EQ_EXPR:
9536 case NE_EXPR:
9537 case SPACESHIP_EXPR:
9538 want_rval = true;
9539 goto binary;
9540
9541 case PREINCREMENT_EXPR:
9542 case POSTINCREMENT_EXPR:
9543 case PREDECREMENT_EXPR:
9544 case POSTDECREMENT_EXPR:
9545 if (cxx_dialect < cxx14)
9546 goto fail;
9547 goto unary;
9548
9549 case BIT_NOT_EXPR:
9550 /* A destructor. */
9551 if (TYPE_P (TREE_OPERAND (t, 0)))
9552 return true;
9553 /* fall through. */
9554
9555 case CONJ_EXPR:
9556 case SAVE_EXPR:
9557 case FIX_TRUNC_EXPR:
9558 case FLOAT_EXPR:
9559 case NEGATE_EXPR:
9560 case ABS_EXPR:
9561 case ABSU_EXPR:
9562 case TRUTH_NOT_EXPR:
9563 case FIXED_CONVERT_EXPR:
9564 case UNARY_PLUS_EXPR:
9565 case UNARY_LEFT_FOLD_EXPR:
9566 case UNARY_RIGHT_FOLD_EXPR:
9567 unary:
9568 return RECUR (TREE_OPERAND (t, 0), rval);
9569
9570 case CAST_EXPR:
9571 case CONST_CAST_EXPR:
9572 case STATIC_CAST_EXPR:
9573 case REINTERPRET_CAST_EXPR:
9574 case IMPLICIT_CONV_EXPR:
9575 if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
9576 /* In C++98, a conversion to non-integral type can't be part of a
9577 constant expression. */
9578 {
9579 if (flags & tf_error)
9580 constexpr_error (loc, fundef_p,
9581 "cast to non-integral type %qT in a constant "
9582 "expression", TREE_TYPE (t));
9583 return false;
9584 }
9585 /* This might be a conversion from a class to a (potentially) literal
9586 type. Let's consider it potentially constant since the conversion
9587 might be a constexpr user-defined conversion. */
9588 else if (cxx_dialect >= cxx11
9589 && (dependent_type_p (TREE_TYPE (t))
9590 || !COMPLETE_TYPE_P (TREE_TYPE (t))
9591 || literal_type_p (TREE_TYPE (t)))
9592 && TREE_OPERAND (t, 0))
9593 {
9594 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
9595 /* If this is a dependent type, it could end up being a class
9596 with conversions. */
9597 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
9598 return true;
9599 /* Or a non-dependent class which has conversions. */
9600 else if (CLASS_TYPE_P (type)
9601 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
9602 return true;
9603 }
9604
9605 return (RECUR (TREE_OPERAND (t, 0),
9606 !TYPE_REF_P (TREE_TYPE (t))));
9607
9608 case BIND_EXPR:
9609 return RECUR (BIND_EXPR_BODY (t), want_rval);
9610
9611 case NON_DEPENDENT_EXPR:
9612 /* Treat NON_DEPENDENT_EXPR as non-constant: it's not handled by
9613 constexpr evaluation or tsubst, so fold_non_dependent_expr can't
9614 do anything useful with it. And we shouldn't see it in a context
9615 where a constant expression is strictly required, hence the assert. */
9616 gcc_checking_assert (!(flags & tf_error));
9617 return false;
9618
9619 case CLEANUP_POINT_EXPR:
9620 case MUST_NOT_THROW_EXPR:
9621 case TRY_CATCH_EXPR:
9622 case TRY_BLOCK:
9623 case EH_SPEC_BLOCK:
9624 case EXPR_STMT:
9625 case PAREN_EXPR:
9626 /* For convenience. */
9627 case LOOP_EXPR:
9628 case EXIT_EXPR:
9629 return RECUR (TREE_OPERAND (t, 0), want_rval);
9630
9631 case DECL_EXPR:
9632 tmp = DECL_EXPR_DECL (t);
9633 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
9634 {
9635 if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
9636 {
9637 if (flags & tf_error)
9638 constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
9639 "%qD defined %<thread_local%> in "
9640 "%<constexpr%> context", tmp);
9641 return false;
9642 }
9643 else if (TREE_STATIC (tmp))
9644 {
9645 if (flags & tf_error)
9646 constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
9647 "%qD defined %<static%> in %<constexpr%> "
9648 "context", tmp);
9649 return false;
9650 }
9651 else if (!check_for_uninitialized_const_var
9652 (tmp, /*constexpr_context_p=*/true, flags))
9653 return false;
9654 }
9655 return RECUR (DECL_INITIAL (tmp), want_rval);
9656
9657 case TRY_FINALLY_EXPR:
9658 return (RECUR (TREE_OPERAND (t, 0), want_rval)
9659 && RECUR (TREE_OPERAND (t, 1), any));
9660
9661 case SCOPE_REF:
9662 return RECUR (TREE_OPERAND (t, 1), want_rval);
9663
9664 case TARGET_EXPR:
9665 if (!TARGET_EXPR_DIRECT_INIT_P (t)
9666 && !literal_type_p (TREE_TYPE (t)))
9667 {
9668 if (flags & tf_error)
9669 {
9670 auto_diagnostic_group d;
9671 if (constexpr_error (loc, fundef_p,
9672 "temporary of non-literal type %qT in a "
9673 "constant expression", TREE_TYPE (t)))
9674 explain_non_literal_class (TREE_TYPE (t));
9675 }
9676 return false;
9677 }
9678 /* FALLTHRU */
9679 case INIT_EXPR:
9680 return RECUR (TREE_OPERAND (t, 1), rval);
9681
9682 case CONSTRUCTOR:
9683 {
9684 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
9685 constructor_elt *ce;
9686 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
9687 if (!RECUR (ce->value, want_rval))
9688 return false;
9689 return true;
9690 }
9691
9692 case TREE_LIST:
9693 {
9694 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
9695 || DECL_P (TREE_PURPOSE (t)));
9696 if (!RECUR (TREE_VALUE (t), want_rval))
9697 return false;
9698 if (TREE_CHAIN (t) == NULL_TREE)
9699 return true;
9700 return RECUR (TREE_CHAIN (t), want_rval);
9701 }
9702
9703 case TRUNC_DIV_EXPR:
9704 case CEIL_DIV_EXPR:
9705 case FLOOR_DIV_EXPR:
9706 case ROUND_DIV_EXPR:
9707 case TRUNC_MOD_EXPR:
9708 case CEIL_MOD_EXPR:
9709 case ROUND_MOD_EXPR:
9710 {
9711 tree denom = TREE_OPERAND (t, 1);
9712 if (!RECUR (denom, rval))
9713 return false;
9714 /* We can't call cxx_eval_outermost_constant_expr on an expression
9715 that hasn't been through instantiate_non_dependent_expr yet. */
9716 if (!processing_template_decl)
9717 denom = cxx_eval_outermost_constant_expr (denom, true);
9718 if (integer_zerop (denom))
9719 {
9720 if (flags & tf_error)
9721 constexpr_error (input_location, fundef_p,
9722 "division by zero is not a constant expression");
9723 return false;
9724 }
9725 else
9726 {
9727 want_rval = true;
9728 return RECUR (TREE_OPERAND (t, 0), want_rval);
9729 }
9730 }
9731
9732 case COMPOUND_EXPR:
9733 {
9734 /* check_return_expr sometimes wraps a TARGET_EXPR in a
9735 COMPOUND_EXPR; don't get confused. */
9736 tree op0 = TREE_OPERAND (t, 0);
9737 tree op1 = TREE_OPERAND (t, 1);
9738 STRIP_NOPS (op1);
9739 if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9740 return RECUR (op0, want_rval);
9741 else
9742 goto binary;
9743 }
9744
9745 /* If the first operand is the non-short-circuit constant, look at
9746 the second operand; otherwise we only care about the first one for
9747 potentiality. */
9748 case TRUTH_AND_EXPR:
9749 case TRUTH_ANDIF_EXPR:
9750 tmp = boolean_true_node;
9751 goto truth;
9752 case TRUTH_OR_EXPR:
9753 case TRUTH_ORIF_EXPR:
9754 tmp = boolean_false_node;
9755 truth:
9756 {
9757 tree op0 = TREE_OPERAND (t, 0);
9758 tree op1 = TREE_OPERAND (t, 1);
9759 if (!RECUR (op0, rval))
9760 return false;
9761 if (!(flags & tf_error) && RECUR (op1, rval))
9762 /* When quiet, try to avoid expensive trial evaluation by first
9763 checking potentiality of the second operand. */
9764 return true;
9765 if (!processing_template_decl)
9766 op0 = cxx_eval_outermost_constant_expr (op0, true);
9767 if (tree_int_cst_equal (op0, tmp))
9768 return (flags & tf_error) ? RECUR (op1, rval) : false;
9769 else
9770 return true;
9771 }
9772
9773 case PLUS_EXPR:
9774 case MULT_EXPR:
9775 case POINTER_PLUS_EXPR:
9776 case RDIV_EXPR:
9777 case EXACT_DIV_EXPR:
9778 case MIN_EXPR:
9779 case MAX_EXPR:
9780 case LSHIFT_EXPR:
9781 case RSHIFT_EXPR:
9782 case LROTATE_EXPR:
9783 case RROTATE_EXPR:
9784 case BIT_IOR_EXPR:
9785 case BIT_XOR_EXPR:
9786 case BIT_AND_EXPR:
9787 case TRUTH_XOR_EXPR:
9788 case UNORDERED_EXPR:
9789 case ORDERED_EXPR:
9790 case UNLT_EXPR:
9791 case UNLE_EXPR:
9792 case UNGT_EXPR:
9793 case UNGE_EXPR:
9794 case UNEQ_EXPR:
9795 case LTGT_EXPR:
9796 case RANGE_EXPR:
9797 case COMPLEX_EXPR:
9798 want_rval = true;
9799 /* Fall through. */
9800 case ARRAY_REF:
9801 case ARRAY_RANGE_REF:
9802 case MEMBER_REF:
9803 case DOTSTAR_EXPR:
9804 case MEM_REF:
9805 case BINARY_LEFT_FOLD_EXPR:
9806 case BINARY_RIGHT_FOLD_EXPR:
9807 binary:
9808 for (i = 0; i < 2; ++i)
9809 if (!RECUR (TREE_OPERAND (t, i), want_rval))
9810 return false;
9811 return true;
9812
9813 case VEC_PERM_EXPR:
9814 for (i = 0; i < 3; ++i)
9815 if (!RECUR (TREE_OPERAND (t, i), true))
9816 return false;
9817 return true;
9818
9819 case COND_EXPR:
9820 if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
9821 {
9822 if (flags & tf_error)
9823 constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
9824 "constant expression");
9825 return false;
9826 }
9827 /* Fall through. */
9828 case IF_STMT:
9829 case VEC_COND_EXPR:
9830 /* If the condition is a known constant, we know which of the legs we
9831 care about; otherwise we only require that the condition and
9832 either of the legs be potentially constant. */
9833 tmp = TREE_OPERAND (t, 0);
9834 if (!RECUR (tmp, rval))
9835 return false;
9836 if (!processing_template_decl)
9837 tmp = cxx_eval_outermost_constant_expr (tmp, true);
9838 /* potential_constant_expression* isn't told if it is called for
9839 manifestly_const_eval or not, so for consteval if always
9840 process both branches as if the condition is not a known
9841 constant. */
9842 if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
9843 {
9844 if (integer_zerop (tmp))
9845 return RECUR (TREE_OPERAND (t, 2), want_rval);
9846 else if (TREE_CODE (tmp) == INTEGER_CST)
9847 return RECUR (TREE_OPERAND (t, 1), want_rval);
9848 }
9849 tmp = *jump_target;
9850 for (i = 1; i < 3; ++i)
9851 {
9852 tree this_jump_target = tmp;
9853 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
9854 want_rval, strict, now, fundef_p,
9855 tf_none, &this_jump_target))
9856 {
9857 if (returns (&this_jump_target))
9858 *jump_target = this_jump_target;
9859 else if (!returns (jump_target))
9860 {
9861 if (breaks (&this_jump_target)
9862 || continues (&this_jump_target))
9863 *jump_target = this_jump_target;
9864 if (i == 1)
9865 {
9866 /* If the then branch is potentially constant, but
9867 does not return, check if the else branch
9868 couldn't return, break or continue. */
9869 hash_set<tree> pset;
9870 check_for_return_continue_data data = { &pset, NULL_TREE,
9871 NULL_TREE };
9872 if (tree ret_expr
9873 = cp_walk_tree (&TREE_OPERAND (t, 2),
9874 check_for_return_continue, &data,
9875 &pset))
9876 *jump_target = ret_expr;
9877 else if (*jump_target == NULL_TREE)
9878 {
9879 if (data.continue_stmt)
9880 *jump_target = data.continue_stmt;
9881 else if (data.break_stmt)
9882 *jump_target = data.break_stmt;
9883 }
9884 }
9885 }
9886 return true;
9887 }
9888 }
9889 if (flags & tf_error)
9890 {
9891 if (TREE_CODE (t) == IF_STMT)
9892 constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
9893 "constant expression");
9894 else
9895 constexpr_error (loc, fundef_p, "expression %qE is not a "
9896 "constant expression", t);
9897 }
9898 return false;
9899
9900 case VEC_INIT_EXPR:
9901 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
9902 return true;
9903 if (flags & tf_error)
9904 {
9905 if (constexpr_error (loc, fundef_p, "non-constant array "
9906 "initialization"))
9907 diagnose_non_constexpr_vec_init (t);
9908 }
9909 return false;
9910
9911 case TYPE_DECL:
9912 case TAG_DEFN:
9913 /* We can see these in statement-expressions. */
9914 return true;
9915
9916 case CLEANUP_STMT:
9917 if (!RECUR (CLEANUP_BODY (t), any))
9918 return false;
9919 if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
9920 return false;
9921 return true;
9922
9923 case EMPTY_CLASS_EXPR:
9924 return true;
9925
9926 case GOTO_EXPR:
9927 {
9928 tree *target = &TREE_OPERAND (t, 0);
9929 /* Gotos representing break, continue and cdtor return are OK. */
9930 if (breaks (target) || continues (target) || returns (target))
9931 {
9932 *jump_target = *target;
9933 return true;
9934 }
9935 if (flags & tf_error)
9936 constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
9937 "expression");
9938 return false;
9939 }
9940
9941 case ASSERTION_STMT:
9942 case PRECONDITION_STMT:
9943 case POSTCONDITION_STMT:
9944 if (!checked_contract_p (get_contract_semantic (t)))
9945 return true;
9946 return RECUR (CONTRACT_CONDITION (t), rval);
9947
9948 case LABEL_EXPR:
9949 t = LABEL_EXPR_LABEL (t);
9950 if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
9951 return true;
9952 else if (flags & tf_error)
9953 constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
9954 "function only available with %<-std=c++2b%> or "
9955 "%<-std=gnu++2b%>");
9956 return false;
9957
9958 case ANNOTATE_EXPR:
9959 return RECUR (TREE_OPERAND (t, 0), rval);
9960
9961 case BIT_CAST_EXPR:
9962 return RECUR (TREE_OPERAND (t, 0), rval);
9963
9964 /* Coroutine await, yield and return expressions are not. */
9965 case CO_AWAIT_EXPR:
9966 case CO_YIELD_EXPR:
9967 case CO_RETURN_EXPR:
9968 return false;
9969
9970 case NONTYPE_ARGUMENT_PACK:
9971 {
9972 tree args = ARGUMENT_PACK_ARGS (t);
9973 int len = TREE_VEC_LENGTH (args);
9974 for (int i = 0; i < len; ++i)
9975 if (!RECUR (TREE_VEC_ELT (args, i), any))
9976 return false;
9977 return true;
9978 }
9979
9980 default:
9981 if (objc_non_constant_expr_p (t))
9982 return false;
9983
9984 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
9985 gcc_unreachable ();
9986 return false;
9987 }
9988 #undef RECUR
9989 }
9990
9991 bool
9992 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
9993 bool fundef_p, tsubst_flags_t flags)
9994 {
9995 if (flags & tf_error)
9996 {
9997 /* Check potentiality quietly first, as that could be performed more
9998 efficiently in some cases (currently only for TRUTH_*_EXPR). If
9999 that fails, replay the check noisily to give errors. */
10000 flags &= ~tf_error;
10001 if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
10002 flags))
10003 return true;
10004 flags |= tf_error;
10005 }
10006
10007 tree target = NULL_TREE;
10008 return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
10009 flags, &target);
10010 }
10011
10012 /* The main entry point to the above. */
10013
10014 bool
10015 potential_constant_expression (tree t)
10016 {
10017 return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10018 /*now*/false, /*fundef_p*/false,
10019 tf_none);
10020 }
10021
10022 /* As above, but require a constant rvalue. */
10023
10024 bool
10025 potential_rvalue_constant_expression (tree t)
10026 {
10027 return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10028 /*now*/false, /*fundef_p*/false,
10029 tf_none);
10030 }
10031
10032 /* Like above, but complain about non-constant expressions. */
10033
10034 bool
10035 require_potential_constant_expression (tree t)
10036 {
10037 return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10038 /*now*/false, /*fundef_p*/false,
10039 tf_warning_or_error);
10040 }
10041
10042 /* Cross product of the above. */
10043
10044 bool
10045 require_potential_rvalue_constant_expression (tree t)
10046 {
10047 return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10048 /*now*/false, /*fundef_p*/false,
10049 tf_warning_or_error);
10050 }
10051
10052 /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
10053
10054 bool
10055 require_potential_rvalue_constant_expression_fncheck (tree t)
10056 {
10057 return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10058 /*now*/false, /*fundef_p*/true,
10059 tf_warning_or_error);
10060 }
10061
10062 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
10063
10064 bool
10065 require_rvalue_constant_expression (tree t)
10066 {
10067 return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10068 /*now*/true, /*fundef_p*/false,
10069 tf_warning_or_error);
10070 }
10071
10072 /* Like potential_constant_expression, but don't consider possible constexpr
10073 substitution of the current function. That is, PARM_DECL qualifies under
10074 potential_constant_expression, but not here.
10075
10076 This is basically what you can check when any actual constant values might
10077 be value-dependent. */
10078
10079 bool
10080 is_constant_expression (tree t)
10081 {
10082 return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10083 /*now*/true, /*fundef_p*/false,
10084 tf_none);
10085 }
10086
10087 /* As above, but expect an rvalue. */
10088
10089 bool
10090 is_rvalue_constant_expression (tree t)
10091 {
10092 return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
10093 /*now*/true, /*fundef_p*/false,
10094 tf_none);
10095 }
10096
10097 /* Like above, but complain about non-constant expressions. */
10098
10099 bool
10100 require_constant_expression (tree t)
10101 {
10102 return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
10103 /*now*/true, /*fundef_p*/false,
10104 tf_warning_or_error);
10105 }
10106
10107 /* Like is_constant_expression, but allow const variables that are not allowed
10108 under constexpr rules. */
10109
10110 bool
10111 is_static_init_expression (tree t)
10112 {
10113 return potential_constant_expression_1 (t, /*want_rval*/false,
10114 /*strict*/false, /*now*/true,
10115 /*fundef_p*/false, tf_none);
10116 }
10117
10118 /* Returns true if T is a potential constant expression that is not
10119 instantiation-dependent, and therefore a candidate for constant folding even
10120 in a template. */
10121
10122 bool
10123 is_nondependent_constant_expression (tree t)
10124 {
10125 return (!type_unknown_p (t)
10126 && is_constant_expression (t)
10127 && !instantiation_dependent_expression_p (t));
10128 }
10129
10130 /* Returns true if T is a potential static initializer expression that is not
10131 instantiation-dependent. */
10132
10133 bool
10134 is_nondependent_static_init_expression (tree t)
10135 {
10136 return (!type_unknown_p (t)
10137 && is_static_init_expression (t)
10138 && !instantiation_dependent_expression_p (t));
10139 }
10140
10141 /* True iff FN is an implicitly constexpr function. */
10142
10143 bool
10144 decl_implicit_constexpr_p (tree fn)
10145 {
10146 if (!(flag_implicit_constexpr
10147 && TREE_CODE (fn) == FUNCTION_DECL
10148 && DECL_DECLARED_CONSTEXPR_P (fn)))
10149 return false;
10150
10151 if (DECL_CLONED_FUNCTION_P (fn))
10152 fn = DECL_CLONED_FUNCTION (fn);
10153
10154 return (DECL_LANG_SPECIFIC (fn)
10155 && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
10156 }
10157
10158 /* Finalize constexpr processing after parsing. */
10159
10160 void
10161 fini_constexpr (void)
10162 {
10163 /* The contexpr call and fundef copies tables are no longer needed. */
10164 constexpr_call_table = NULL;
10165 fundef_copies_table = NULL;
10166 }
10167
10168 #include "gt-cp-constexpr.h"
This page took 0.479791 seconds and 4 git commands to generate.