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