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