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