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