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