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