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