]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/constexpr.c
Fix use of inaccessible member in pr85503.C [PR95716]
[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
2d76680f
PC
2132/* Subroutine of cxx_eval_constant_expression.
2133 Evaluate the call expression tree T in the context of OLD_CALL expression
2134 evaluation. */
2135
2136static tree
3e605b20 2137cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
92a596e8 2138 bool lval,
2d76680f
PC
2139 bool *non_constant_p, bool *overflow_p)
2140{
861d4af8
AS
2141 /* Handle concept checks separately. */
2142 if (concept_check_p (t))
2143 return evaluate_concept_check (t, tf_warning_or_error);
2144
f9d0ca40 2145 location_t loc = cp_expr_loc_or_input_loc (t);
2d76680f 2146 tree fun = get_function_named_in_call (t);
cce3ae91 2147 constexpr_call new_call
13de99bc 2148 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
7ffc7de5 2149 int depth_ok;
2d76680f 2150
0b274c17 2151 if (fun == NULL_TREE)
44a845ca
MS
2152 return cxx_eval_internal_function (ctx, t, lval,
2153 non_constant_p, overflow_p);
0b274c17 2154
2d76680f
PC
2155 if (TREE_CODE (fun) != FUNCTION_DECL)
2156 {
2157 /* Might be a constexpr function pointer. */
2b3ab879 2158 fun = cxx_eval_constant_expression (ctx, fun,
92a596e8 2159 /*lval*/false, non_constant_p,
5a804683 2160 overflow_p);
2d76680f
PC
2161 STRIP_NOPS (fun);
2162 if (TREE_CODE (fun) == ADDR_EXPR)
2163 fun = TREE_OPERAND (fun, 0);
582d2481
JJ
2164 /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
2165 indirection, the called expression is a pointer into the
2166 virtual table which should contain FDESC_EXPR. Extract the
2167 FUNCTION_DECL from there. */
2168 else if (TARGET_VTABLE_USES_DESCRIPTORS
2169 && TREE_CODE (fun) == POINTER_PLUS_EXPR
2170 && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
2171 && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
2172 {
2173 tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
2174 if (VAR_P (d)
2175 && DECL_VTABLE_OR_VTT_P (d)
2176 && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
2177 && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
2178 && DECL_INITIAL (d)
2179 && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
2180 {
2181 tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
2182 TYPE_SIZE_UNIT (vtable_entry_type));
2183 HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
2184 if (idx >= 0)
2185 {
2186 tree fdesc
2187 = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
2188 if (TREE_CODE (fdesc) == FDESC_EXPR
2189 && integer_zerop (TREE_OPERAND (fdesc, 1)))
2190 fun = TREE_OPERAND (fdesc, 0);
2191 }
2192 }
2193 }
2d76680f
PC
2194 }
2195 if (TREE_CODE (fun) != FUNCTION_DECL)
2196 {
2b3ab879 2197 if (!ctx->quiet && !*non_constant_p)
84fa214d 2198 error_at (loc, "expression %qE does not designate a %<constexpr%> "
2d76680f
PC
2199 "function", fun);
2200 *non_constant_p = true;
2201 return t;
2202 }
2203 if (DECL_CLONED_FUNCTION_P (fun))
2204 fun = DECL_CLONED_FUNCTION (fun);
0b274c17
MP
2205
2206 if (is_ubsan_builtin_p (fun))
2207 return void_node;
2208
3d78e008 2209 if (fndecl_built_in_p (fun))
5756d0f9 2210 return cxx_eval_builtin_function_call (ctx, t, fun,
92a596e8 2211 lval, non_constant_p, overflow_p);
2d76680f
PC
2212 if (!DECL_DECLARED_CONSTEXPR_P (fun))
2213 {
8412b939
JJ
2214 if (TREE_CODE (t) == CALL_EXPR
2215 && cxx_replaceable_global_alloc_fn (fun)
2216 && (CALL_FROM_NEW_OR_DELETE_P (t)
2217 || (ctx->call
2218 && ctx->call->fundef
2219 && is_std_allocator_allocate (ctx->call->fundef->decl))))
8e007055
JJ
2220 {
2221 const int nargs = call_expr_nargs (t);
2222 tree arg0 = NULL_TREE;
2223 for (int i = 0; i < nargs; ++i)
2224 {
2225 tree arg = CALL_EXPR_ARG (t, i);
2226 arg = cxx_eval_constant_expression (ctx, arg, false,
2227 non_constant_p, overflow_p);
2228 VERIFY_CONSTANT (arg);
2229 if (i == 0)
2230 arg0 = arg;
2231 }
2232 gcc_assert (arg0);
2233 if (IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
2234 {
2235 tree type = build_array_type_nelts (char_type_node,
2236 tree_to_uhwi (arg0));
2237 tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier,
2238 type);
2239 DECL_ARTIFICIAL (var) = 1;
2240 TREE_STATIC (var) = 1;
2241 ctx->global->heap_vars.safe_push (var);
2242 ctx->global->values.put (var, NULL_TREE);
2243 return fold_convert (ptr_type_node, build_address (var));
2244 }
2245 else
2246 {
2247 STRIP_NOPS (arg0);
2248 if (TREE_CODE (arg0) == ADDR_EXPR
2249 && VAR_P (TREE_OPERAND (arg0, 0)))
2250 {
2251 tree var = TREE_OPERAND (arg0, 0);
2252 if (DECL_NAME (var) == heap_uninit_identifier
2253 || DECL_NAME (var) == heap_identifier)
2254 {
2255 DECL_NAME (var) = heap_deleted_identifier;
2256 ctx->global->values.remove (var);
f74f6092 2257 ctx->global->heap_dealloc_count++;
8e007055
JJ
2258 return void_node;
2259 }
2260 else if (DECL_NAME (var) == heap_deleted_identifier)
2261 {
2262 if (!ctx->quiet)
2263 error_at (loc, "deallocation of already deallocated "
2264 "storage");
2265 *non_constant_p = true;
2266 return t;
2267 }
2268 }
2269 if (!ctx->quiet)
2270 error_at (loc, "deallocation of storage that was "
2271 "not previously allocated");
2272 *non_constant_p = true;
2273 return t;
2274 }
2275 }
cf650568
JJ
2276 /* Allow placement new in std::construct_at, just return the second
2277 argument. */
8412b939
JJ
2278 if (TREE_CODE (t) == CALL_EXPR
2279 && cxx_placement_new_fn (fun)
cf650568
JJ
2280 && ctx->call
2281 && ctx->call->fundef
2282 && is_std_construct_at (ctx->call->fundef->decl))
2283 {
2284 const int nargs = call_expr_nargs (t);
2285 tree arg1 = NULL_TREE;
2286 for (int i = 0; i < nargs; ++i)
2287 {
2288 tree arg = CALL_EXPR_ARG (t, i);
2289 arg = cxx_eval_constant_expression (ctx, arg, false,
2290 non_constant_p, overflow_p);
2291 VERIFY_CONSTANT (arg);
2292 if (i == 1)
2293 arg1 = arg;
2294 }
2295 gcc_assert (arg1);
2296 return arg1;
2297 }
22edf943
MP
2298 else if (cxx_dynamic_cast_fn_p (fun))
2299 return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
2300
2b3ab879 2301 if (!ctx->quiet)
2d76680f 2302 {
11399477 2303 if (!lambda_static_thunk_p (fun))
84fa214d 2304 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2d76680f
PC
2305 explain_invalid_constexpr_fn (fun);
2306 }
2307 *non_constant_p = true;
2308 return t;
2309 }
2310
86461cad
JM
2311 constexpr_ctx new_ctx = *ctx;
2312 if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
2313 && TREE_CODE (t) == AGGR_INIT_EXPR)
2314 {
2315 /* We want to have an initialization target for an AGGR_INIT_EXPR.
2316 If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
2317 new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
2318 tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
e8c48716 2319 CONSTRUCTOR_NO_CLEARING (ctor) = true;
8e007055 2320 ctx->global->values.put (new_ctx.object, ctor);
86461cad
JM
2321 ctx = &new_ctx;
2322 }
2323
2d76680f
PC
2324 /* Shortcut trivial constructor/op=. */
2325 if (trivial_fn_p (fun))
2326 {
86461cad 2327 tree init = NULL_TREE;
2d76680f 2328 if (call_expr_nargs (t) == 2)
86461cad 2329 init = convert_from_reference (get_nth_callarg (t, 1));
2d76680f
PC
2330 else if (TREE_CODE (t) == AGGR_INIT_EXPR
2331 && AGGR_INIT_ZERO_FIRST (t))
86461cad
JM
2332 init = build_zero_init (DECL_CONTEXT (fun), NULL_TREE, false);
2333 if (init)
2334 {
2335 tree op = get_nth_callarg (t, 0);
2336 if (is_dummy_object (op))
2337 op = ctx->object;
2338 else
2339 op = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (op)), op);
2340 tree set = build2 (MODIFY_EXPR, TREE_TYPE (op), op, init);
c8816908
JM
2341 new_ctx.call = &new_call;
2342 return cxx_eval_constant_expression (&new_ctx, set, lval,
86461cad
JM
2343 non_constant_p, overflow_p);
2344 }
2d76680f
PC
2345 }
2346
81371eff
JM
2347 /* We can't defer instantiating the function any longer. */
2348 if (!DECL_INITIAL (fun)
f65a3299
PP
2349 && DECL_TEMPLOID_INSTANTIATION (fun)
2350 && !uid_sensitive_constexpr_evaluation_p ())
81371eff 2351 {
f065303f
JM
2352 location_t save_loc = input_location;
2353 input_location = loc;
81371eff
JM
2354 ++function_depth;
2355 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
2356 --function_depth;
f065303f 2357 input_location = save_loc;
81371eff
JM
2358 }
2359
2d76680f 2360 /* If in direct recursive call, optimize definition search. */
c8816908 2361 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
3e605b20 2362 new_call.fundef = ctx->call->fundef;
2d76680f
PC
2363 else
2364 {
2365 new_call.fundef = retrieve_constexpr_fundef (fun);
4ddcdbf9 2366 if (new_call.fundef == NULL || new_call.fundef->body == NULL
4f05d85a 2367 || new_call.fundef->result == error_mark_node
4ddcdbf9 2368 || fun == current_function_decl)
2d76680f 2369 {
2b3ab879 2370 if (!ctx->quiet)
2d76680f 2371 {
4ddcdbf9
JM
2372 /* We need to check for current_function_decl here in case we're
2373 being called during cp_fold_function, because at that point
2374 DECL_INITIAL is set properly and we have a fundef but we
2375 haven't lowered invisirefs yet (c++/70344). */
2376 if (DECL_INITIAL (fun) == error_mark_node
2377 || fun == current_function_decl)
ddd6d421
JM
2378 error_at (loc, "%qD called in a constant expression before its "
2379 "definition is complete", fun);
2380 else if (DECL_INITIAL (fun))
2d76680f 2381 {
98e5a19a
JM
2382 /* The definition of fun was somehow unsuitable. But pretend
2383 that lambda static thunks don't exist. */
2384 if (!lambda_static_thunk_p (fun))
2385 error_at (loc, "%qD called in a constant expression", fun);
2d76680f
PC
2386 explain_invalid_constexpr_fn (fun);
2387 }
2388 else
2389 error_at (loc, "%qD used before its definition", fun);
2390 }
2391 *non_constant_p = true;
2392 return t;
2393 }
2394 }
60813a46 2395
12d9ce19 2396 bool non_constant_args = false;
3e605b20 2397 cxx_bind_parameters_in_call (ctx, t, &new_call,
12d9ce19 2398 non_constant_p, overflow_p, &non_constant_args);
ecdcd560
JM
2399
2400 /* We build up the bindings list before we know whether we already have this
2401 call cached. If we don't end up saving these bindings, ggc_free them when
2402 this function exits. */
6c1dae73 2403 class free_bindings
ecdcd560 2404 {
4953b790 2405 tree *bindings;
6c1dae73 2406 public:
4953b790
JM
2407 free_bindings (tree &b): bindings (&b) { }
2408 ~free_bindings () { if (bindings) ggc_free (*bindings); }
2409 void preserve () { bindings = NULL; }
ecdcd560
JM
2410 } fb (new_call.bindings);
2411
2d76680f
PC
2412 if (*non_constant_p)
2413 return t;
2414
2415 depth_ok = push_cx_call_context (t);
2416
04e1749c
MP
2417 /* Remember the object we are constructing. */
2418 tree new_obj = NULL_TREE;
2419 if (DECL_CONSTRUCTOR_P (fun))
2420 {
2421 /* In a constructor, it should be the first `this' argument.
2422 At this point it has already been evaluated in the call
2423 to cxx_bind_parameters_in_call. */
2424 new_obj = TREE_VEC_ELT (new_call.bindings, 0);
2425 STRIP_NOPS (new_obj);
2426 if (TREE_CODE (new_obj) == ADDR_EXPR)
2427 new_obj = TREE_OPERAND (new_obj, 0);
64da1b76
PP
2428
2429 if (ctx->call && ctx->call->fundef
2430 && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
2431 {
2432 tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
2433 STRIP_NOPS (cur_obj);
2434 if (TREE_CODE (cur_obj) == ADDR_EXPR)
2435 cur_obj = TREE_OPERAND (cur_obj, 0);
2436 if (new_obj == cur_obj)
2437 /* We're calling the target constructor of a delegating
2438 constructor, or accessing a base subobject through a
2439 NOP_EXPR as part of a call to a base constructor, so
2440 there is no new (sub)object. */
2441 new_obj = NULL_TREE;
2442 }
04e1749c
MP
2443 }
2444
12d9ce19 2445 tree result = NULL_TREE;
2d76680f 2446
12d9ce19 2447 constexpr_call *entry = NULL;
4a58d2fe 2448 if (depth_ok && !non_constant_args && ctx->strict)
2d76680f 2449 {
cce3ae91
JJ
2450 new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
2451 new_call.hash
2452 = iterative_hash_template_arg (new_call.bindings, new_call.hash);
2453 new_call.hash
13de99bc 2454 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
12d9ce19
JM
2455
2456 /* If we have seen this call before, we are done. */
2457 maybe_initialize_constexpr_call_table ();
2458 constexpr_call **slot
2459 = constexpr_call_table->find_slot (&new_call, INSERT);
2460 entry = *slot;
2461 if (entry == NULL)
2462 {
7ffc7de5
JM
2463 /* Only cache up to constexpr_cache_depth to limit memory use. */
2464 if (depth_ok < constexpr_cache_depth)
2465 {
2466 /* We need to keep a pointer to the entry, not just the slot, as
2467 the slot can move during evaluation of the body. */
2468 *slot = entry = ggc_alloc<constexpr_call> ();
2469 *entry = new_call;
2470 fb.preserve ();
2471 }
12d9ce19 2472 }
7ffc7de5
JM
2473 /* Calls that are in progress have their result set to NULL, so that we
2474 can detect circular dependencies. Now that we only cache up to
2475 constexpr_cache_depth this won't catch circular dependencies that
2476 start deeper, but they'll hit the recursion or ops limit. */
12d9ce19
JM
2477 else if (entry->result == NULL)
2478 {
2479 if (!ctx->quiet)
2480 error ("call has circular dependency");
2481 *non_constant_p = true;
2482 entry->result = result = error_mark_node;
2483 }
2484 else
2485 result = entry->result;
2d76680f
PC
2486 }
2487
2488 if (!depth_ok)
2489 {
2b3ab879 2490 if (!ctx->quiet)
84fa214d 2491 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
7e7a6ed7 2492 "%<-fconstexpr-depth=%> to increase the maximum)",
2d76680f
PC
2493 max_constexpr_depth);
2494 *non_constant_p = true;
12d9ce19 2495 result = error_mark_node;
2d76680f
PC
2496 }
2497 else
2498 {
f74f6092 2499 bool cacheable = true;
de54de93
JM
2500 if (result && result != error_mark_node)
2501 /* OK */;
2502 else if (!DECL_SAVED_TREE (fun))
2503 {
2504 /* When at_eof >= 2, cgraph has started throwing away
2505 DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
2506 late code generation for VEC_INIT_EXPR, which needs to be
2507 completely reconsidered. */
2508 gcc_assert (at_eof >= 2 && ctx->quiet);
2509 *non_constant_p = true;
2510 }
f65a3299 2511 else if (tree copy = get_fundef_copy (new_call.fundef))
3e605b20 2512 {
c0daf32d 2513 tree body, parms, res;
620adbec 2514 releasing_vec ctors;
0567dcd2 2515
c0daf32d 2516 /* Reuse or create a new unshared copy of this function's body. */
97f3003f
JM
2517 body = TREE_PURPOSE (copy);
2518 parms = TREE_VALUE (copy);
2519 res = TREE_TYPE (copy);
0567dcd2
MP
2520
2521 /* Associate the bindings with the remapped parms. */
2522 tree bound = new_call.bindings;
2523 tree remapped = parms;
3c961dc7 2524 for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
60813a46 2525 {
3c961dc7 2526 tree arg = TREE_VEC_ELT (bound, i);
4953b790
JM
2527 if (entry)
2528 {
2529 /* Unshare args going into the hash table to separate them
2530 from the caller's context, for better GC and to avoid
2531 problems with verify_gimple. */
db38a029 2532 arg = unshare_expr_without_location (arg);
4953b790
JM
2533 TREE_VEC_ELT (bound, i) = arg;
2534 }
2535 /* Don't share a CONSTRUCTOR that might be changed. This is not
2536 redundant with the unshare just above; we also don't want to
2537 change the argument values in the hash table. XXX Could we
2538 unshare lazily in cxx_eval_store_expression? */
0146e25f 2539 arg = unshare_constructor (arg);
620adbec
JM
2540 if (TREE_CODE (arg) == CONSTRUCTOR)
2541 vec_safe_push (ctors, arg);
8e007055 2542 ctx->global->values.put (remapped, arg);
0567dcd2 2543 remapped = DECL_CHAIN (remapped);
60813a46 2544 }
0567dcd2 2545 /* Add the RESULT_DECL to the values map, too. */
cd3ca6cb
JM
2546 gcc_assert (!DECL_BY_REFERENCE (res));
2547 ctx->global->values.put (res, NULL_TREE);
60813a46 2548
ee1de08d
JJ
2549 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2550 we can forget their values after the call. */
c0daf32d 2551 constexpr_ctx ctx_with_save_exprs = *ctx;
0ee28590 2552 auto_vec<tree, 10> save_exprs;
c0daf32d 2553 ctx_with_save_exprs.save_exprs = &save_exprs;
1e163090 2554 ctx_with_save_exprs.call = &new_call;
f74f6092
JJ
2555 unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
2556 unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
c0daf32d 2557
0567dcd2 2558 tree jump_target = NULL_TREE;
c0daf32d 2559 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
0567dcd2
MP
2560 lval, non_constant_p, overflow_p,
2561 &jump_target);
60813a46 2562
0567dcd2
MP
2563 if (DECL_CONSTRUCTOR_P (fun))
2564 /* This can be null for a subobject constructor call, in
2565 which case what we care about is the initialization
2566 side-effects rather than the value. We could get at the
2567 value by evaluating *this, but we don't bother; there's
2568 no need to put such a call in the hash table. */
2569 result = lval ? ctx->object : ctx->ctor;
2570 else if (VOID_TYPE_P (TREE_TYPE (res)))
2571 result = void_node;
2572 else
2573 {
cd3ca6cb 2574 result = *ctx->global->values.get (res);
0567dcd2 2575 if (result == NULL_TREE && !*non_constant_p)
60813a46 2576 {
0567dcd2 2577 if (!ctx->quiet)
84fa214d 2578 error ("%<constexpr%> call flows off the end "
0567dcd2
MP
2579 "of the function");
2580 *non_constant_p = true;
60813a46 2581 }
60813a46 2582 }
0567dcd2 2583
04e1749c
MP
2584 /* At this point, the object's constructor will have run, so
2585 the object is no longer under construction, and its possible
2586 'const' semantics now apply. Make a note of this fact by
2587 marking the CONSTRUCTOR TREE_READONLY. */
2588 if (new_obj
2589 && CLASS_TYPE_P (TREE_TYPE (new_obj))
2590 && CP_TYPE_CONST_P (TREE_TYPE (new_obj)))
2591 {
8e007055
JJ
2592 /* Subobjects might not be stored in ctx->global->values but we
2593 can get its CONSTRUCTOR by evaluating *this. */
04e1749c
MP
2594 tree e = cxx_eval_constant_expression (ctx, new_obj,
2595 /*lval*/false,
2596 non_constant_p,
2597 overflow_p);
8f9dd1b0
MP
2598 if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2599 TREE_READONLY (e) = true;
04e1749c
MP
2600 }
2601
ee1de08d
JJ
2602 /* Forget the saved values of the callee's SAVE_EXPRs and
2603 TARGET_EXPRs. */
0ee28590
JJ
2604 unsigned int i;
2605 tree save_expr;
2606 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
8e007055 2607 ctx->global->values.remove (save_expr);
c0daf32d 2608
0567dcd2
MP
2609 /* Remove the parms/result from the values map. Is it worth
2610 bothering to do this when the map itself is only live for
2611 one constexpr evaluation? If so, maybe also clear out
2612 other vars from call, maybe in BIND_EXPR handling? */
8e007055 2613 ctx->global->values.remove (res);
0567dcd2 2614 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
8e007055 2615 ctx->global->values.remove (parm);
c0daf32d 2616
620adbec
JM
2617 /* Free any parameter CONSTRUCTORs we aren't returning directly. */
2618 while (!ctors->is_empty ())
2619 {
2620 tree c = ctors->pop ();
2621 if (c != result)
2622 free_constructor (c);
2623 }
2624
c0daf32d
PP
2625 /* Make the unshared function copy we used available for re-use. */
2626 save_fundef_copy (fun, copy);
f74f6092
JJ
2627
2628 /* If the call allocated some heap object that hasn't been
2629 deallocated during the call, or if it deallocated some heap
2630 object it has not allocated, the call isn't really stateless
2631 for the constexpr evaluation and should not be cached.
2632 It is fine if the call allocates something and deallocates it
2633 too. */
2634 if (entry
2635 && (save_heap_alloc_count != ctx->global->heap_vars.length ()
2636 || (save_heap_dealloc_count
2637 != ctx->global->heap_dealloc_count)))
2638 {
2639 tree heap_var;
2640 unsigned int i;
2641 if ((ctx->global->heap_vars.length ()
2642 - ctx->global->heap_dealloc_count)
2643 != save_heap_alloc_count - save_heap_dealloc_count)
2644 cacheable = false;
2645 else
2646 FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
2647 save_heap_alloc_count)
2648 if (DECL_NAME (heap_var) != heap_deleted_identifier)
2649 {
2650 cacheable = false;
2651 break;
2652 }
2653 }
b2562229
PP
2654
2655 /* Rewrite all occurrences of the function's RESULT_DECL with the
2656 current object under construction. */
2657 if (!*non_constant_p && ctx->object
2658 && AGGREGATE_TYPE_P (TREE_TYPE (res))
2659 && !is_empty_class (TREE_TYPE (res)))
2660 if (replace_result_decl (&result, res, ctx->object))
2661 cacheable = false;
3e605b20 2662 }
aaa26bf4
JM
2663 else
2664 /* Couldn't get a function copy to evaluate. */
2665 *non_constant_p = true;
60813a46 2666
2d76680f
PC
2667 if (result == error_mark_node)
2668 *non_constant_p = true;
52228180 2669 if (*non_constant_p || *overflow_p)
12d9ce19 2670 result = error_mark_node;
0567dcd2 2671 else if (!result)
60813a46 2672 result = void_node;
12d9ce19 2673 if (entry)
f74f6092 2674 entry->result = cacheable ? result : error_mark_node;
2d76680f
PC
2675 }
2676
7906797e
MP
2677 /* The result of a constexpr function must be completely initialized.
2678
2679 However, in C++20, a constexpr constructor doesn't necessarily have
2680 to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
2681 in order to detect reading an unitialized object in constexpr instead
2682 of value-initializing it. (reduced_constant_expression_p is expected to
2683 take care of clearing the flag.) */
2684 if (TREE_CODE (result) == CONSTRUCTOR
b04445d4 2685 && (cxx_dialect < cxx20
7906797e 2686 || !DECL_CONSTRUCTOR_P (fun)))
ecc57615 2687 clear_no_implicit_zero (result);
f64e0c02 2688
2d76680f 2689 pop_cx_call_context ();
9b9eb42a 2690 return result;
2d76680f
PC
2691}
2692
7906797e
MP
2693/* Return true if T is a valid constant initializer. If a CONSTRUCTOR
2694 initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
2695 cleared.
2696 FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
2d76680f
PC
2697
2698bool
2699reduced_constant_expression_p (tree t)
2700{
4930c53e
MP
2701 if (t == NULL_TREE)
2702 return false;
2703
2d76680f
PC
2704 switch (TREE_CODE (t))
2705 {
2706 case PTRMEM_CST:
2707 /* Even if we can't lower this yet, it's constant. */
2708 return true;
2709
2710 case CONSTRUCTOR:
2711 /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
7368cfa4 2712 tree idx, val, field; unsigned HOST_WIDE_INT i;
e8c48716 2713 if (CONSTRUCTOR_NO_CLEARING (t))
6f11ddd8
JM
2714 {
2715 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2716 /* An initialized vector would have a VECTOR_CST. */
2717 return false;
b04445d4 2718 else if (cxx_dialect >= cxx20
7906797e 2719 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
b599bf9d 2720 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7906797e 2721 field = NULL_TREE;
b04445d4 2722 else if (cxx_dialect >= cxx20
b599bf9d
PP
2723 && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
2724 {
2725 if (CONSTRUCTOR_NELTS (t) == 0)
2726 /* An initialized union has a constructor element. */
2727 return false;
2728 /* And it only initializes one member. */
2729 field = NULL_TREE;
2730 }
6f11ddd8
JM
2731 else
2732 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
2733 }
7368cfa4
JM
2734 else
2735 field = NULL_TREE;
2736 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val)
125db6a1 2737 {
4930c53e
MP
2738 /* If VAL is null, we're in the middle of initializing this
2739 element. */
7368cfa4 2740 if (!reduced_constant_expression_p (val))
125db6a1 2741 return false;
d6ff2207
MP
2742 /* Empty class field may or may not have an initializer. */
2743 for (; field && idx != field;
2744 field = next_initializable_field (DECL_CHAIN (field)))
2745 if (!is_really_empty_class (TREE_TYPE (field),
2746 /*ignore_vptr*/false))
2747 return false;
7368cfa4 2748 if (field)
d6ff2207 2749 field = next_initializable_field (DECL_CHAIN (field));
125db6a1 2750 }
7906797e
MP
2751 /* There could be a non-empty field at the end. */
2752 for (; field; field = next_initializable_field (DECL_CHAIN (field)))
2753 if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
2754 return false;
2755 if (CONSTRUCTOR_NO_CLEARING (t))
7368cfa4 2756 /* All the fields are initialized. */
e8c48716 2757 CONSTRUCTOR_NO_CLEARING (t) = false;
2d76680f
PC
2758 return true;
2759
2760 default:
2761 /* FIXME are we calling this too much? */
2762 return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
2763 }
2764}
2765
2766/* Some expressions may have constant operands but are not constant
7d75ea04
JJ
2767 themselves, such as 1/0. Call this function to check for that
2768 condition.
2d76680f
PC
2769
2770 We only call this in places that require an arithmetic constant, not in
2771 places where we might have a non-constant expression that can be a
2772 component of a constant expression, such as the address of a constexpr
2773 variable that might be dereferenced later. */
2774
2775static bool
2776verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
2777 bool *overflow_p)
2778{
596334fa
JM
2779 if (!*non_constant_p && !reduced_constant_expression_p (t)
2780 && t != void_node)
2d76680f
PC
2781 {
2782 if (!allow_non_constant)
2783 error ("%q+E is not a constant expression", t);
2784 *non_constant_p = true;
2785 }
2786 if (TREE_OVERFLOW_P (t))
2787 {
2788 if (!allow_non_constant)
2789 {
2790 permerror (input_location, "overflow in constant expression");
2791 /* If we're being permissive (and are in an enforcing
2792 context), ignore the overflow. */
2793 if (flag_permissive)
2794 return *non_constant_p;
2795 }
2796 *overflow_p = true;
2797 }
2798 return *non_constant_p;
2799}
2800
253a921b
MP
2801/* Check whether the shift operation with code CODE and type TYPE on LHS
2802 and RHS is undefined. If it is, give an error with an explanation,
2803 and return true; return false otherwise. */
2804
2805static bool
2806cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
2807 enum tree_code code, tree type, tree lhs, tree rhs)
2808{
2809 if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
2810 || TREE_CODE (lhs) != INTEGER_CST
2811 || TREE_CODE (rhs) != INTEGER_CST)
2812 return false;
2813
2814 tree lhstype = TREE_TYPE (lhs);
2815 unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
2816
2817 /* [expr.shift] The behavior is undefined if the right operand
2818 is negative, or greater than or equal to the length in bits
2819 of the promoted left operand. */
2820 if (tree_int_cst_sgn (rhs) == -1)
2821 {
2822 if (!ctx->quiet)
5342156c
MP
2823 permerror (loc, "right operand of shift expression %q+E is negative",
2824 build2_loc (loc, code, type, lhs, rhs));
2825 return (!flag_permissive || ctx->quiet);
253a921b
MP
2826 }
2827 if (compare_tree_int (rhs, uprec) >= 0)
2828 {
2829 if (!ctx->quiet)
a9c697b8
MS
2830 permerror (loc, "right operand of shift expression %q+E is greater "
2831 "than or equal to the precision %wu of the left operand",
2832 build2_loc (loc, code, type, lhs, rhs), uprec);
5342156c 2833 return (!flag_permissive || ctx->quiet);
253a921b
MP
2834 }
2835
2836 /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
2837 if E1 has a signed type and non-negative value, and E1x2^E2 is
2838 representable in the corresponding unsigned type of the result type,
2839 then that value, converted to the result type, is the resulting value;
8ee09943 2840 otherwise, the behavior is undefined.
b04445d4 2841 For C++20:
8ee09943
JJ
2842 The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
2843 2^N, where N is the range exponent of the type of the result. */
2844 if (code == LSHIFT_EXPR
2845 && !TYPE_UNSIGNED (lhstype)
2846 && cxx_dialect >= cxx11
b04445d4 2847 && cxx_dialect < cxx20)
253a921b
MP
2848 {
2849 if (tree_int_cst_sgn (lhs) == -1)
2850 {
2851 if (!ctx->quiet)
5342156c
MP
2852 permerror (loc,
2853 "left operand of shift expression %q+E is negative",
2854 build2_loc (loc, code, type, lhs, rhs));
2855 return (!flag_permissive || ctx->quiet);
253a921b
MP
2856 }
2857 /* For signed x << y the following:
2858 (unsigned) x >> ((prec (lhs) - 1) - y)
2859 if > 1, is undefined. The right-hand side of this formula
2860 is the highest bit of the LHS that can be set (starting from 0),
2861 so that the shift doesn't overflow. We then right-shift the LHS
2862 to see whether any other bit is set making the original shift
2863 undefined -- the result is not representable in the corresponding
2864 unsigned type. */
2865 tree t = build_int_cst (unsigned_type_node, uprec - 1);
2866 t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
2867 tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
2868 t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
2869 if (tree_int_cst_lt (integer_one_node, t))
2870 {
2871 if (!ctx->quiet)
5342156c
MP
2872 permerror (loc, "shift expression %q+E overflows",
2873 build2_loc (loc, code, type, lhs, rhs));
2874 return (!flag_permissive || ctx->quiet);
253a921b
MP
2875 }
2876 }
2877 return false;
2878}
2879
2d76680f
PC
2880/* Subroutine of cxx_eval_constant_expression.
2881 Attempt to reduce the unary expression tree T to a compile time value.
2882 If successful, return the value. Otherwise issue a diagnostic
2883 and return error_mark_node. */
2884
2885static tree
3e605b20 2886cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
12d9ce19 2887 bool /*lval*/,
2d76680f
PC
2888 bool *non_constant_p, bool *overflow_p)
2889{
2890 tree r;
2891 tree orig_arg = TREE_OPERAND (t, 0);
12d9ce19
JM
2892 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
2893 non_constant_p, overflow_p);
2d76680f 2894 VERIFY_CONSTANT (arg);
f899317e
JM
2895 location_t loc = EXPR_LOCATION (t);
2896 enum tree_code code = TREE_CODE (t);
2897 tree type = TREE_TYPE (t);
2898 r = fold_unary_loc (loc, code, type, arg);
2899 if (r == NULL_TREE)
2900 {
2901 if (arg == orig_arg)
2902 r = t;
2903 else
2904 r = build1_loc (loc, code, type, arg);
2905 }
2d76680f
PC
2906 VERIFY_CONSTANT (r);
2907 return r;
2908}
2909
ea8661cd
JJ
2910/* Helper function for cxx_eval_binary_expression. Try to optimize
2911 original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
2912 generic folding should be used. */
2913
2914static tree
2915cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
2916 tree lhs, tree rhs, bool *non_constant_p,
2917 bool *overflow_p)
2918{
2919 STRIP_NOPS (lhs);
2920 if (TREE_CODE (lhs) != ADDR_EXPR)
2921 return NULL_TREE;
2922
2923 lhs = TREE_OPERAND (lhs, 0);
2924
2925 /* &A[i] p+ j => &A[i + j] */
2926 if (TREE_CODE (lhs) == ARRAY_REF
2927 && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
2928 && TREE_CODE (rhs) == INTEGER_CST
2929 && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
2930 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
2931 {
2932 tree orig_type = TREE_TYPE (t);
2933 location_t loc = EXPR_LOCATION (t);
2934 tree type = TREE_TYPE (lhs);
2935
2936 t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
2937 tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
2938 nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p,
2939 overflow_p);
2940 if (*non_constant_p)
2941 return NULL_TREE;
2942 /* Don't fold an out-of-bound access. */
2943 if (!tree_int_cst_le (t, nelts))
2944 return NULL_TREE;
2945 rhs = cp_fold_convert (ssizetype, rhs);
2946 /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
2947 constexpr int A[1]; ... (char *)&A[0] + 1 */
2948 if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
2949 rhs, TYPE_SIZE_UNIT (type))))
2950 return NULL_TREE;
2951 /* Make sure to treat the second operand of POINTER_PLUS_EXPR
2952 as signed. */
2953 rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
2954 TYPE_SIZE_UNIT (type));
2955 t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
2956 t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
2957 t, NULL_TREE, NULL_TREE);
2958 t = cp_build_addr_expr (t, tf_warning_or_error);
2959 t = cp_fold_convert (orig_type, t);
2960 return cxx_eval_constant_expression (ctx, t, /*lval*/false,
2961 non_constant_p, overflow_p);
2962 }
2963
2964 return NULL_TREE;
2965}
2966
2d76680f
PC
2967/* Subroutine of cxx_eval_constant_expression.
2968 Like cxx_eval_unary_expression, except for binary expressions. */
2969
2970static tree
3e605b20 2971cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
dfffecb8 2972 bool lval,
2d76680f
PC
2973 bool *non_constant_p, bool *overflow_p)
2974{
618d6c1c 2975 tree r = NULL_TREE;
2d76680f
PC
2976 tree orig_lhs = TREE_OPERAND (t, 0);
2977 tree orig_rhs = TREE_OPERAND (t, 1);
2978 tree lhs, rhs;
12d9ce19 2979 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
5a804683 2980 non_constant_p, overflow_p);
c8a66fc9
JM
2981 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
2982 subtraction. */
2983 if (*non_constant_p)
2984 return t;
12d9ce19 2985 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
5a804683 2986 non_constant_p, overflow_p);
c8a66fc9
JM
2987 if (*non_constant_p)
2988 return t;
f899317e
JM
2989
2990 location_t loc = EXPR_LOCATION (t);
2991 enum tree_code code = TREE_CODE (t);
2992 tree type = TREE_TYPE (t);
618d6c1c
PP
2993
2994 if (code == EQ_EXPR || code == NE_EXPR)
2995 {
2996 bool is_code_eq = (code == EQ_EXPR);
2997
2998 if (TREE_CODE (lhs) == PTRMEM_CST
2999 && TREE_CODE (rhs) == PTRMEM_CST)
9b0607de
JM
3000 {
3001 tree lmem = PTRMEM_CST_MEMBER (lhs);
3002 tree rmem = PTRMEM_CST_MEMBER (rhs);
3003 bool eq;
3004 if (TREE_CODE (lmem) == TREE_CODE (rmem)
3005 && TREE_CODE (lmem) == FIELD_DECL
3006 && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
3007 && same_type_p (DECL_CONTEXT (lmem),
3008 DECL_CONTEXT (rmem)))
3009 /* If both refer to (possibly different) members of the same union
3010 (12.3), they compare equal. */
3011 eq = true;
3012 else
3013 eq = cp_tree_equal (lhs, rhs);
3014 r = constant_boolean_node (eq == is_code_eq, type);
3015 }
618d6c1c
PP
3016 else if ((TREE_CODE (lhs) == PTRMEM_CST
3017 || TREE_CODE (rhs) == PTRMEM_CST)
3018 && (null_member_pointer_value_p (lhs)
3019 || null_member_pointer_value_p (rhs)))
3020 r = constant_boolean_node (!is_code_eq, type);
dd5dda56
JM
3021 else if (TREE_CODE (lhs) == PTRMEM_CST)
3022 lhs = cplus_expand_constant (lhs);
3023 else if (TREE_CODE (rhs) == PTRMEM_CST)
3024 rhs = cplus_expand_constant (rhs);
618d6c1c 3025 }
8bada5cd
MS
3026 if (code == POINTER_PLUS_EXPR && !*non_constant_p
3027 && integer_zerop (lhs) && !integer_zerop (rhs))
3028 {
3029 if (!ctx->quiet)
3030 error ("arithmetic involving a null pointer in %qE", lhs);
bf533db8 3031 *non_constant_p = true;
8bada5cd
MS
3032 return t;
3033 }
ea8661cd
JJ
3034 else if (code == POINTER_PLUS_EXPR)
3035 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3036 overflow_p);
b7689b96
JM
3037 else if (code == SPACESHIP_EXPR)
3038 {
3039 r = genericize_spaceship (type, lhs, rhs);
dfffecb8 3040 r = cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
b7689b96
JM
3041 overflow_p);
3042 }
618d6c1c
PP
3043
3044 if (r == NULL_TREE)
3045 r = fold_binary_loc (loc, code, type, lhs, rhs);
3046
f899317e
JM
3047 if (r == NULL_TREE)
3048 {
3049 if (lhs == orig_lhs && rhs == orig_rhs)
3050 r = t;
3051 else
3052 r = build2_loc (loc, code, type, lhs, rhs);
3053 }
253a921b
MP
3054 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
3055 *non_constant_p = true;
c8a66fc9
JM
3056 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3057 a local array in a constexpr function. */
71a93b08 3058 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
caee690e 3059 if (!ptr)
134efa82 3060 VERIFY_CONSTANT (r);
2d76680f
PC
3061 return r;
3062}
3063
3064/* Subroutine of cxx_eval_constant_expression.
3065 Attempt to evaluate condition expressions. Dead branches are not
3066 looked into. */
3067
3068static tree
3e605b20 3069cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
92a596e8 3070 bool lval,
56632b27
JM
3071 bool *non_constant_p, bool *overflow_p,
3072 tree *jump_target)
2d76680f 3073{
3e605b20 3074 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
12d9ce19 3075 /*lval*/false,
5a804683 3076 non_constant_p, overflow_p);
2d76680f
PC
3077 VERIFY_CONSTANT (val);
3078 /* Don't VERIFY_CONSTANT the other operands. */
3079 if (integer_zerop (val))
43574e4f
JJ
3080 val = TREE_OPERAND (t, 2);
3081 else
3082 val = TREE_OPERAND (t, 1);
3083 if (TREE_CODE (t) == IF_STMT && !val)
3084 val = void_node;
3085 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3086 overflow_p, jump_target);
2d76680f
PC
3087}
3088
f370e36d
JJ
3089/* Subroutine of cxx_eval_constant_expression.
3090 Attempt to evaluate vector condition expressions. Unlike
3091 cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
3092 ternary arithmetics operation, where all 3 arguments have to be
3093 evaluated as constants and then folding computes the result from
3094 them. */
3095
3096static tree
3097cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
3098 bool *non_constant_p, bool *overflow_p)
3099{
3100 tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3101 /*lval*/false,
3102 non_constant_p, overflow_p);
3103 VERIFY_CONSTANT (arg1);
3104 tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3105 /*lval*/false,
3106 non_constant_p, overflow_p);
3107 VERIFY_CONSTANT (arg2);
3108 tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
3109 /*lval*/false,
3110 non_constant_p, overflow_p);
3111 VERIFY_CONSTANT (arg3);
3112 location_t loc = EXPR_LOCATION (t);
3113 tree type = TREE_TYPE (t);
3114 tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3115 if (r == NULL_TREE)
3116 {
3117 if (arg1 == TREE_OPERAND (t, 0)
3118 && arg2 == TREE_OPERAND (t, 1)
3119 && arg3 == TREE_OPERAND (t, 2))
3120 r = t;
3121 else
3122 r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
3123 }
3124 VERIFY_CONSTANT (r);
3125 return r;
3126}
3127
ceaaf873
JM
3128/* Returns less than, equal to, or greater than zero if KEY is found to be
3129 less than, to match, or to be greater than the constructor_elt's INDEX. */
3130
3131static int
3132array_index_cmp (tree key, tree index)
3133{
3134 gcc_assert (TREE_CODE (key) == INTEGER_CST);
3135
3136 switch (TREE_CODE (index))
3137 {
3138 case INTEGER_CST:
3139 return tree_int_cst_compare (key, index);
3140 case RANGE_EXPR:
3141 {
3142 tree lo = TREE_OPERAND (index, 0);
3143 tree hi = TREE_OPERAND (index, 1);
3144 if (tree_int_cst_lt (key, lo))
3145 return -1;
3146 else if (tree_int_cst_lt (hi, key))
3147 return 1;
3148 else
3149 return 0;
3150 }
3151 default:
3152 gcc_unreachable ();
3153 }
3154}
3155
3156/* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
3157 if none. If INSERT is true, insert a matching element rather than fail. */
3158
3159static HOST_WIDE_INT
582d2481 3160find_array_ctor_elt (tree ary, tree dindex, bool insert)
ceaaf873
JM
3161{
3162 if (tree_int_cst_sgn (dindex) < 0)
3163 return -1;
3164
3165 unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
3166 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
3167 unsigned HOST_WIDE_INT len = vec_safe_length (elts);
3168
3169 unsigned HOST_WIDE_INT end = len;
3170 unsigned HOST_WIDE_INT begin = 0;
3171
3172 /* If the last element of the CONSTRUCTOR has its own index, we can assume
3173 that the same is true of the other elements and index directly. */
3174 if (end > 0)
3175 {
fe217ba0 3176 tree cindex = (*elts)[end - 1].index;
c7c09af8
JJ
3177 if (cindex == NULL_TREE)
3178 {
3179 /* Verify that if the last index is missing, all indexes
3180 are missing. */
3181 if (flag_checking)
3182 for (unsigned int j = 0; j < len - 1; ++j)
3183 gcc_assert ((*elts)[j].index == NULL_TREE);
3184 if (i < end)
3185 return i;
3186 else
3187 {
3188 begin = end;
3189 if (i == end)
3190 /* If the element is to be added right at the end,
3191 make sure it is added with cleared index too. */
3192 dindex = NULL_TREE;
3193 else if (insert)
3194 /* Otherwise, in order not to break the assumption
3195 that CONSTRUCTOR either has all indexes or none,
3196 we need to add indexes to all elements. */
3197 for (unsigned int j = 0; j < len; ++j)
3198 (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
3199 }
3200 }
3201 else if (TREE_CODE (cindex) == INTEGER_CST
3202 && compare_tree_int (cindex, end - 1) == 0)
ceaaf873
JM
3203 {
3204 if (i < end)
3205 return i;
3206 else
3207 begin = end;
3208 }
3209 }
3210
3211 /* Otherwise, find a matching index by means of a binary search. */
3212 while (begin != end)
3213 {
3214 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
a7ccb9e7
JM
3215 constructor_elt &elt = (*elts)[middle];
3216 tree idx = elt.index;
ceaaf873 3217
a7ccb9e7 3218 int cmp = array_index_cmp (dindex, idx);
ceaaf873
JM
3219 if (cmp < 0)
3220 end = middle;
3221 else if (cmp > 0)
3222 begin = middle + 1;
3223 else
a7ccb9e7
JM
3224 {
3225 if (insert && TREE_CODE (idx) == RANGE_EXPR)
3226 {
3227 /* We need to split the range. */
3228 constructor_elt e;
3229 tree lo = TREE_OPERAND (idx, 0);
3230 tree hi = TREE_OPERAND (idx, 1);
fe217ba0
JJ
3231 tree value = elt.value;
3232 dindex = fold_convert (sizetype, dindex);
a7ccb9e7
JM
3233 if (tree_int_cst_lt (lo, dindex))
3234 {
3235 /* There are still some lower elts; shorten the range. */
3236 tree new_hi = int_const_binop (MINUS_EXPR, dindex,
3237 size_one_node);
3238 if (tree_int_cst_equal (lo, new_hi))
3239 /* Only one element left, no longer a range. */
3240 elt.index = lo;
3241 else
3242 TREE_OPERAND (idx, 1) = new_hi;
3243 /* Append the element we want to insert. */
3244 ++middle;
3245 e.index = dindex;
fe217ba0 3246 e.value = unshare_constructor (value);
a7ccb9e7
JM
3247 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
3248 }
3249 else
3250 /* No lower elts, the range elt is now ours. */
3251 elt.index = dindex;
3252
3253 if (tree_int_cst_lt (dindex, hi))
3254 {
3255 /* There are still some higher elts; append a range. */
3256 tree new_lo = int_const_binop (PLUS_EXPR, dindex,
3257 size_one_node);
3258 if (tree_int_cst_equal (new_lo, hi))
3259 e.index = hi;
3260 else
3261 e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
fe217ba0
JJ
3262 e.value = unshare_constructor (value);
3263 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
a7ccb9e7
JM
3264 }
3265 }
3266 return middle;
3267 }
ceaaf873
JM
3268 }
3269
3270 if (insert)
3271 {
3272 constructor_elt e = { dindex, NULL_TREE };
3273 vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
3274 return end;
3275 }
3276
3277 return -1;
3278}
3279
37244b21
PP
3280/* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
3281 matching constructor_elt exists, then add one to CTOR.
3282
3283 As an optimization, if POS_HINT is non-negative then it is used as a guess
3284 for the (integer) index of the matching constructor_elt within CTOR. */
3285
3286static constructor_elt *
077dd9b3 3287get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
37244b21
PP
3288{
3289 /* Check the hint first. */
3290 if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
3291 && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
3292 return CONSTRUCTOR_ELT (ctor, pos_hint);
3293
3294 tree type = TREE_TYPE (ctor);
3295 if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
3296 {
3297 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
3298 return &CONSTRUCTOR_ELTS (ctor)->last();
3299 }
3300 else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
3301 {
e069285c
PP
3302 if (TREE_CODE (index) == RANGE_EXPR)
3303 {
3304 /* Support for RANGE_EXPR index lookups is currently limited to
3305 accessing an existing element via POS_HINT, or appending a new
3306 element to the end of CTOR. ??? Support for other access
3307 patterns may also be needed. */
3308 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
3309 if (vec_safe_length (elts))
3310 {
3311 tree lo = TREE_OPERAND (index, 0);
3312 gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
3313 }
3314 CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
3315 return &elts->last();
3316 }
3317
37244b21
PP
3318 HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, /*insert*/true);
3319 gcc_assert (i >= 0);
3320 constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
3321 gcc_assert (cep->index == NULL_TREE
3322 || TREE_CODE (cep->index) != RANGE_EXPR);
3323 return cep;
3324 }
3325 else
3326 {
3327 gcc_assert (TREE_CODE (index) == FIELD_DECL);
3328
3329 /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
3330 Usually we meet initializers in that order, but it is
3331 possible for base types to be placed not in program
3332 order. */
3333 tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
3334 unsigned HOST_WIDE_INT idx = 0;
3335 constructor_elt *cep = NULL;
3336
3337 /* Check if we're changing the active member of a union. */
3338 if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
3339 && CONSTRUCTOR_ELT (ctor, 0)->index != index)
3340 vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
3341 /* If the bit offset of INDEX is larger than that of the last
3342 constructor_elt, then we can just immediately append a new
3343 constructor_elt to the end of CTOR. */
3344 else if (CONSTRUCTOR_NELTS (ctor)
3345 && tree_int_cst_compare (bit_position (index),
3346 bit_position (CONSTRUCTOR_ELTS (ctor)
3347 ->last().index)) > 0)
3348 {
3349 idx = CONSTRUCTOR_NELTS (ctor);
3350 goto insert;
3351 }
3352
3353 /* Otherwise, we need to iterate over CTOR to find or insert INDEX
3354 appropriately. */
3355
3356 for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
3357 idx++, fields = DECL_CHAIN (fields))
3358 {
3359 if (index == cep->index)
3360 goto found;
3361
3362 /* The field we're initializing must be on the field
3363 list. Look to see if it is present before the
3364 field the current ELT initializes. */
3365 for (; fields != cep->index; fields = DECL_CHAIN (fields))
3366 if (index == fields)
3367 goto insert;
3368 }
3369 /* We fell off the end of the CONSTRUCTOR, so insert a new
3370 entry at the end. */
3371
3372 insert:
3373 {
3374 constructor_elt ce = { index, NULL_TREE };
3375
3376 vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
3377 cep = CONSTRUCTOR_ELT (ctor, idx);
3378 }
3379 found:;
3380
3381 return cep;
3382 }
3383}
3384
abdc16c8
MS
3385/* Under the control of CTX, issue a detailed diagnostic for
3386 an out-of-bounds subscript INDEX into the expression ARRAY. */
3387
3388static void
043666e0 3389diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
abdc16c8
MS
3390{
3391 if (!ctx->quiet)
3392 {
3393 tree arraytype = TREE_TYPE (array);
3394
3395 /* Convert the unsigned array subscript to a signed integer to avoid
3396 printing huge numbers for small negative values. */
3397 tree sidx = fold_convert (ssizetype, index);
043666e0 3398 STRIP_ANY_LOCATION_WRAPPER (array);
abdc16c8
MS
3399 if (DECL_P (array))
3400 {
08b3748c 3401 if (TYPE_DOMAIN (arraytype))
043666e0
JM
3402 error_at (loc, "array subscript value %qE is outside the bounds "
3403 "of array %qD of type %qT", sidx, array, arraytype);
08b3748c 3404 else
043666e0
JM
3405 error_at (loc, "nonzero array subscript %qE is used with array %qD of "
3406 "type %qT with unknown bounds", sidx, array, arraytype);
abdc16c8
MS
3407 inform (DECL_SOURCE_LOCATION (array), "declared here");
3408 }
08b3748c 3409 else if (TYPE_DOMAIN (arraytype))
043666e0
JM
3410 error_at (loc, "array subscript value %qE is outside the bounds "
3411 "of array type %qT", sidx, arraytype);
08b3748c 3412 else
043666e0
JM
3413 error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
3414 "with unknown bounds", sidx, arraytype);
abdc16c8
MS
3415 }
3416}
ceaaf873 3417
74f8705e
MP
3418/* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3419 a VECTOR_TYPE). */
3420
3421static tree
3422get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
3423 bool *non_constant_p, bool *overflow_p)
3424{
3425 tree nelts;
3426 if (TREE_CODE (type) == ARRAY_TYPE)
3427 {
3428 if (TYPE_DOMAIN (type))
3429 nelts = array_type_nelts_top (type);
3430 else
3431 nelts = size_zero_node;
3432 }
3433 else if (VECTOR_TYPE_P (type))
3434 nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
3435 else
3436 gcc_unreachable ();
3437
3438 /* For VLAs, the number of elements won't be an integer constant. */
3439 nelts = cxx_eval_constant_expression (ctx, nelts, false,
3440 non_constant_p, overflow_p);
3441 return nelts;
3442}
3443
d6b46fca
NS
3444/* Extract element INDEX consisting of CHARS_PER_ELT chars from
3445 STRING_CST STRING. */
3446
3447static tree
3448extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
3449{
3450 tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
3451 tree r;
3452
3453 if (chars_per_elt == 1)
3454 r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
3455 else
3456 {
3457 const unsigned char *ptr
3458 = ((const unsigned char *)TREE_STRING_POINTER (string)
3459 + index * chars_per_elt);
3460 r = native_interpret_expr (type, ptr, chars_per_elt);
3461 }
3462 return r;
3463}
3464
043666e0
JM
3465/* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
3466 subscript, diagnose any problems with it, and return the result. */
3467
3468static tree
3469eval_and_check_array_index (const constexpr_ctx *ctx,
3470 tree t, bool allow_one_past,
3471 bool *non_constant_p, bool *overflow_p)
3472{
f9d0ca40 3473 location_t loc = cp_expr_loc_or_input_loc (t);
043666e0
JM
3474 tree ary = TREE_OPERAND (t, 0);
3475 t = TREE_OPERAND (t, 1);
3476 tree index = cxx_eval_constant_expression (ctx, t, false,
3477 non_constant_p, overflow_p);
3478 VERIFY_CONSTANT (index);
3479
3480 if (!tree_fits_shwi_p (index)
3481 || tree_int_cst_sgn (index) < 0)
3482 {
3483 diag_array_subscript (loc, ctx, ary, index);
3484 *non_constant_p = true;
3485 return t;
3486 }
3487
3488 tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
3489 overflow_p);
3490 VERIFY_CONSTANT (nelts);
3491 if (allow_one_past
3492 ? !tree_int_cst_le (index, nelts)
3493 : !tree_int_cst_lt (index, nelts))
3494 {
3495 diag_array_subscript (loc, ctx, ary, index);
3496 *non_constant_p = true;
3497 return t;
3498 }
3499
3500 return index;
3501}
3502
2d76680f
PC
3503/* Subroutine of cxx_eval_constant_expression.
3504 Attempt to reduce a reference to an array slot. */
3505
3506static tree
3e605b20 3507cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
92a596e8 3508 bool lval,
2d76680f
PC
3509 bool *non_constant_p, bool *overflow_p)
3510{
3511 tree oldary = TREE_OPERAND (t, 0);
3e605b20 3512 tree ary = cxx_eval_constant_expression (ctx, oldary,
92a596e8 3513 lval,
5a804683 3514 non_constant_p, overflow_p);
2d76680f
PC
3515 if (*non_constant_p)
3516 return t;
bc2687dd
JJ
3517 if (!lval
3518 && TREE_CODE (ary) == VIEW_CONVERT_EXPR
043666e0
JM
3519 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
3520 && TREE_TYPE (t) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))
3521 ary = TREE_OPERAND (ary, 0);
3b9997bb 3522
043666e0
JM
3523 tree oldidx = TREE_OPERAND (t, 1);
3524 tree index = eval_and_check_array_index (ctx, t, lval,
3525 non_constant_p, overflow_p);
3526 if (*non_constant_p)
3527 return t;
5453bfed 3528
bc2a38df
JJ
3529 if (lval && ary == oldary && index == oldidx)
3530 return t;
3531 else if (lval)
3532 return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
3533
043666e0
JM
3534 unsigned len = 0, elem_nchars = 1;
3535 tree elem_type = TREE_TYPE (TREE_TYPE (ary));
3536 if (TREE_CODE (ary) == CONSTRUCTOR)
3537 len = CONSTRUCTOR_NELTS (ary);
3538 else if (TREE_CODE (ary) == STRING_CST)
3539 {
3540 elem_nchars = (TYPE_PRECISION (elem_type)
3541 / TYPE_PRECISION (char_type_node));
3542 len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
3543 }
3544 else if (TREE_CODE (ary) == VECTOR_CST)
3545 /* We don't create variable-length VECTOR_CSTs. */
3546 len = VECTOR_CST_NELTS (ary).to_constant ();
3547 else
3548 {
3549 /* We can't do anything with other tree codes, so use
3550 VERIFY_CONSTANT to complain and fail. */
3551 VERIFY_CONSTANT (ary);
3552 gcc_unreachable ();
3553 }
3554
ceaaf873 3555 bool found;
043666e0 3556 HOST_WIDE_INT i = 0;
ceaaf873
JM
3557 if (TREE_CODE (ary) == CONSTRUCTOR)
3558 {
3559 HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
3560 found = (ix >= 0);
3561 if (found)
3562 i = ix;
3b9997bb 3563 }
ceaaf873 3564 else
043666e0
JM
3565 {
3566 i = tree_to_shwi (index);
3567 found = (i < len);
3568 }
3b9997bb 3569
fd2bfee5
JM
3570 if (found)
3571 {
3572 tree r;
3573 if (TREE_CODE (ary) == CONSTRUCTOR)
3574 r = (*CONSTRUCTOR_ELTS (ary))[i].value;
3575 else if (TREE_CODE (ary) == VECTOR_CST)
3576 r = VECTOR_CST_ELT (ary, i);
fd2bfee5 3577 else
d6b46fca
NS
3578 r = extract_string_elt (ary, elem_nchars, i);
3579
fd2bfee5
JM
3580 if (r)
3581 /* Don't VERIFY_CONSTANT here. */
3582 return r;
2d76680f 3583
fd2bfee5 3584 /* Otherwise the element doesn't have a value yet. */
2d76680f 3585 }
3b9997bb 3586
fd2bfee5
JM
3587 /* Not found. */
3588
3589 if (TREE_CODE (ary) == CONSTRUCTOR
e8c48716 3590 && CONSTRUCTOR_NO_CLEARING (ary))
2d76680f 3591 {
fd2bfee5
JM
3592 /* 'ary' is part of the aggregate initializer we're currently
3593 building; if there's no initializer for this element yet,
3594 that's an error. */
3595 if (!ctx->quiet)
3596 error ("accessing uninitialized array element");
3597 *non_constant_p = true;
3598 return t;
2d76680f 3599 }
fd2bfee5
JM
3600
3601 /* If it's within the array bounds but doesn't have an explicit
0712ea63
JM
3602 initializer, it's initialized from {}. But use build_value_init
3603 directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
3604 tree val;
3605 if (CP_AGGREGATE_TYPE_P (elem_type))
3606 {
3607 tree empty_ctor = build_constructor (init_list_type_node, NULL);
3608 val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
3609 }
3610 else
3611 val = build_value_init (elem_type, tf_warning_or_error);
fd2bfee5
JM
3612 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3613 overflow_p);
2d76680f
PC
3614}
3615
3616/* Subroutine of cxx_eval_constant_expression.
3617 Attempt to reduce a field access of a value of class type. */
3618
3619static tree
3e605b20 3620cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
92a596e8 3621 bool lval,
2d76680f
PC
3622 bool *non_constant_p, bool *overflow_p)
3623{
3624 unsigned HOST_WIDE_INT i;
3625 tree field;
3626 tree value;
3627 tree part = TREE_OPERAND (t, 1);
3628 tree orig_whole = TREE_OPERAND (t, 0);
3e605b20 3629 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 3630 lval,
5a804683 3631 non_constant_p, overflow_p);
a7f8415c 3632 if (INDIRECT_REF_P (whole)
bf533db8
JJ
3633 && integer_zerop (TREE_OPERAND (whole, 0)))
3634 {
3635 if (!ctx->quiet)
3636 error ("dereferencing a null pointer in %qE", orig_whole);
3637 *non_constant_p = true;
3638 return t;
3639 }
8bada5cd 3640
dcdbc004
JM
3641 if (TREE_CODE (whole) == PTRMEM_CST)
3642 whole = cplus_expand_constant (whole);
2d76680f
PC
3643 if (whole == orig_whole)
3644 return t;
92a596e8 3645 if (lval)
2d76680f
PC
3646 return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
3647 whole, part, NULL_TREE);
3648 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3649 CONSTRUCTOR. */
3650 if (!*non_constant_p && TREE_CODE (whole) != CONSTRUCTOR)
3651 {
2b3ab879 3652 if (!ctx->quiet)
2d76680f
PC
3653 error ("%qE is not a constant expression", orig_whole);
3654 *non_constant_p = true;
3655 }
3656 if (DECL_MUTABLE_P (part))
3657 {
2b3ab879 3658 if (!ctx->quiet)
2d76680f
PC
3659 error ("mutable %qD is not usable in a constant expression", part);
3660 *non_constant_p = true;
3661 }
3662 if (*non_constant_p)
3663 return t;
2db613e5 3664 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
2d76680f
PC
3665 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
3666 {
2db613e5
JM
3667 /* Use name match for PMF fields, as a variant will have a
3668 different FIELD_DECL with a different type. */
3669 if (pmf ? DECL_NAME (field) == DECL_NAME (part)
3670 : field == part)
60813a46
JM
3671 {
3672 if (value)
5b884e94
JJ
3673 {
3674 STRIP_ANY_LOCATION_WRAPPER (value);
3675 return value;
3676 }
60813a46
JM
3677 else
3678 /* We're in the middle of initializing it. */
3679 break;
3680 }
2d76680f
PC
3681 }
3682 if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
3683 && CONSTRUCTOR_NELTS (whole) > 0)
3684 {
3685 /* DR 1188 says we don't have to deal with this. */
2b3ab879 3686 if (!ctx->quiet)
b599bf9d
PP
3687 {
3688 constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
3689 if (cep->value == NULL_TREE)
3690 error ("accessing uninitialized member %qD", part);
3691 else
3692 error ("accessing %qD member instead of initialized %qD member in "
3693 "constant expression", part, cep->index);
3694 }
2d76680f
PC
3695 *non_constant_p = true;
3696 return t;
3697 }
3698
188e53bd
JM
3699 /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
3700 classes never get represented; throw together a value now. */
dbcd32f8 3701 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
188e53bd
JM
3702 return build_constructor (TREE_TYPE (t), NULL);
3703
2db613e5
JM
3704 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
3705
e8c48716 3706 if (CONSTRUCTOR_NO_CLEARING (whole))
3e605b20
JM
3707 {
3708 /* 'whole' is part of the aggregate initializer we're currently
3709 building; if there's no initializer for this member yet, that's an
188e53bd 3710 error. */
2b3ab879 3711 if (!ctx->quiet)
3e605b20
JM
3712 error ("accessing uninitialized member %qD", part);
3713 *non_constant_p = true;
3714 return t;
3715 }
3716
2d76680f
PC
3717 /* If there's no explicit init for this field, it's value-initialized. */
3718 value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
3e605b20 3719 return cxx_eval_constant_expression (ctx, value,
92a596e8 3720 lval,
5a804683 3721 non_constant_p, overflow_p);
2d76680f
PC
3722}
3723
3724/* Subroutine of cxx_eval_constant_expression.
3725 Attempt to reduce a field access of a value of class type that is
3726 expressed as a BIT_FIELD_REF. */
3727
3728static tree
3e605b20 3729cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
92a596e8 3730 bool lval,
2d76680f
PC
3731 bool *non_constant_p, bool *overflow_p)
3732{
3733 tree orig_whole = TREE_OPERAND (t, 0);
3734 tree retval, fldval, utype, mask;
3735 bool fld_seen = false;
3736 HOST_WIDE_INT istart, isize;
3e605b20 3737 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
92a596e8 3738 lval,
5a804683 3739 non_constant_p, overflow_p);
2d76680f
PC
3740 tree start, field, value;
3741 unsigned HOST_WIDE_INT i;
3742
3743 if (whole == orig_whole)
3744 return t;
3745 /* Don't VERIFY_CONSTANT here; we only want to check that we got a
3746 CONSTRUCTOR. */
3747 if (!*non_constant_p
3748 && TREE_CODE (whole) != VECTOR_CST
3749 && TREE_CODE (whole) != CONSTRUCTOR)
3750 {
2b3ab879 3751 if (!ctx->quiet)
2d76680f
PC
3752 error ("%qE is not a constant expression", orig_whole);
3753 *non_constant_p = true;
3754 }
3755 if (*non_constant_p)
3756 return t;
3757
3758 if (TREE_CODE (whole) == VECTOR_CST)
3759 return fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
3760 TREE_OPERAND (t, 1), TREE_OPERAND (t, 2));
3761
3762 start = TREE_OPERAND (t, 2);
3763 istart = tree_to_shwi (start);
3764 isize = tree_to_shwi (TREE_OPERAND (t, 1));
3765 utype = TREE_TYPE (t);
3766 if (!TYPE_UNSIGNED (utype))
3767 utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
3768 retval = build_int_cst (utype, 0);
3769 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
3770 {
3771 tree bitpos = bit_position (field);
5b884e94 3772 STRIP_ANY_LOCATION_WRAPPER (value);
2d76680f
PC
3773 if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
3774 return value;
3775 if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
3776 && TREE_CODE (value) == INTEGER_CST
3777 && tree_fits_shwi_p (bitpos)
3778 && tree_fits_shwi_p (DECL_SIZE (field)))
3779 {
3780 HOST_WIDE_INT bit = tree_to_shwi (bitpos);
3781 HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
3782 HOST_WIDE_INT shift;
3783 if (bit >= istart && bit + sz <= istart + isize)
3784 {
3785 fldval = fold_convert (utype, value);
3786 mask = build_int_cst_type (utype, -1);
3787 mask = fold_build2 (LSHIFT_EXPR, utype, mask,
3788 size_int (TYPE_PRECISION (utype) - sz));
3789 mask = fold_build2 (RSHIFT_EXPR, utype, mask,
3790 size_int (TYPE_PRECISION (utype) - sz));
3791 fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
3792 shift = bit - istart;
3793 if (BYTES_BIG_ENDIAN)
3794 shift = TYPE_PRECISION (utype) - shift - sz;
3795 fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
3796 size_int (shift));
3797 retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
3798 fld_seen = true;
3799 }
3800 }
3801 }
3802 if (fld_seen)
3803 return fold_convert (TREE_TYPE (t), retval);
3804 gcc_unreachable ();
3805 return error_mark_node;
3806}
3807
3808/* Subroutine of cxx_eval_constant_expression.
3809 Evaluate a short-circuited logical expression T in the context
3810 of a given constexpr CALL. BAILOUT_VALUE is the value for
3811 early return. CONTINUE_VALUE is used here purely for
3812 sanity check purposes. */
3813
3814static tree
3e605b20 3815cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
2d76680f 3816 tree bailout_value, tree continue_value,
92a596e8 3817 bool lval,
2d76680f
PC
3818 bool *non_constant_p, bool *overflow_p)
3819{
3820 tree r;
3e605b20 3821 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 3822 lval,
5a804683 3823 non_constant_p, overflow_p);
2d76680f
PC
3824 VERIFY_CONSTANT (lhs);
3825 if (tree_int_cst_equal (lhs, bailout_value))
3826 return lhs;
3827 gcc_assert (tree_int_cst_equal (lhs, continue_value));
3e605b20 3828 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 3829 lval, non_constant_p,
5a804683 3830 overflow_p);
2d76680f
PC
3831 VERIFY_CONSTANT (r);
3832 return r;
3833}
3834
3835/* REF is a COMPONENT_REF designating a particular field. V is a vector of
3836 CONSTRUCTOR elements to initialize (part of) an object containing that
3837 field. Return a pointer to the constructor_elt corresponding to the
3838 initialization of the field. */
3839
3840static constructor_elt *
3841base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
3842{
3843 tree aggr = TREE_OPERAND (ref, 0);
3844 tree field = TREE_OPERAND (ref, 1);
3845 HOST_WIDE_INT i;
3846 constructor_elt *ce;
3847
3848 gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
3849
3850 if (TREE_CODE (aggr) == COMPONENT_REF)
3851 {
3852 constructor_elt *base_ce
3853 = base_field_constructor_elt (v, aggr);
3854 v = CONSTRUCTOR_ELTS (base_ce->value);
3855 }
3856
3857 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
3858 if (ce->index == field)
3859 return ce;
3860
3861 gcc_unreachable ();
3862 return NULL;
3863}
3864
3e605b20
JM
3865/* Some of the expressions fed to the constexpr mechanism are calls to
3866 constructors, which have type void. In that case, return the type being
3867 initialized by the constructor. */
3868
3869static tree
3870initialized_type (tree t)
3871{
3872 if (TYPE_P (t))
3873 return t;
5dab8b11 3874 tree type = TREE_TYPE (t);
4d0c18c6 3875 if (TREE_CODE (t) == CALL_EXPR)
3e605b20
JM
3876 {
3877 /* A constructor call has void type, so we need to look deeper. */
3878 tree fn = get_function_named_in_call (t);
3879 if (fn && TREE_CODE (fn) == FUNCTION_DECL
3880 && DECL_CXX_CONSTRUCTOR_P (fn))
3881 type = DECL_CONTEXT (fn);
3882 }
4d0c18c6
JM
3883 else if (TREE_CODE (t) == COMPOUND_EXPR)
3884 return initialized_type (TREE_OPERAND (t, 1));
5dab8b11
JM
3885 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3886 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
3887 return cv_unqualified (type);
3e605b20
JM
3888}
3889
3890/* We're about to initialize element INDEX of an array or class from VALUE.
3891 Set up NEW_CTX appropriately by adjusting .object to refer to the
3892 subobject and creating a new CONSTRUCTOR if the element is itself
3893 a class or array. */
3894
3895static void
3896init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
3897 tree index, tree &value)
3898{
3899 new_ctx = *ctx;
3900
3901 if (index && TREE_CODE (index) != INTEGER_CST
3902 && TREE_CODE (index) != FIELD_DECL)
3903 /* This won't have an element in the new CONSTRUCTOR. */
3904 return;
3905
3906 tree type = initialized_type (value);
3907 if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
3908 /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
3909 return;
3910
3911 /* The sub-aggregate initializer might contain a placeholder;
3912 update object to refer to the subobject and ctor to refer to
3913 the (newly created) sub-initializer. */
3914 if (ctx->object)
3915 new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
3916 tree elt = build_constructor (type, NULL);
e8c48716 3917 CONSTRUCTOR_NO_CLEARING (elt) = true;
3e605b20
JM
3918 new_ctx.ctor = elt;
3919
3920 if (TREE_CODE (value) == TARGET_EXPR)
3921 /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
3922 value = TARGET_EXPR_INITIAL (value);
3923}
3924
3925/* We're about to process an initializer for a class or array TYPE. Make
3926 sure that CTX is set up appropriately. */
3927
3928static void
3929verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
3930{
3931 /* We don't bother building a ctor for an empty base subobject. */
3932 if (is_empty_class (type))
3933 return;
3934
3935 /* We're in the middle of an initializer that might involve placeholders;
3936 our caller should have created a CONSTRUCTOR for us to put the
3937 initializer into. We will either return that constructor or T. */
3938 gcc_assert (ctx->ctor);
3939 gcc_assert (same_type_ignoring_top_level_qualifiers_p
3940 (type, TREE_TYPE (ctx->ctor)));
fe69277d
JM
3941 /* We used to check that ctx->ctor was empty, but that isn't the case when
3942 the object is zero-initialized before calling the constructor. */
3e605b20 3943 if (ctx->object)
176e79b5
JM
3944 {
3945 tree otype = TREE_TYPE (ctx->object);
3946 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
3947 /* Handle flexible array members. */
3948 || (TREE_CODE (otype) == ARRAY_TYPE
3949 && TYPE_DOMAIN (otype) == NULL_TREE
3950 && TREE_CODE (type) == ARRAY_TYPE
3951 && (same_type_ignoring_top_level_qualifiers_p
3952 (TREE_TYPE (type), TREE_TYPE (otype)))));
3953 }
3e605b20 3954 gcc_assert (!ctx->object || !DECL_P (ctx->object)
8e007055 3955 || *(ctx->global->values.get (ctx->object)) == ctx->ctor);
3e605b20
JM
3956}
3957
2d76680f
PC
3958/* Subroutine of cxx_eval_constant_expression.
3959 The expression tree T denotes a C-style array or a C-style
3960 aggregate. Reduce it to a constant expression. */
3961
3962static tree
3e605b20 3963cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
92a596e8 3964 bool lval,
2d76680f
PC
3965 bool *non_constant_p, bool *overflow_p)
3966{
3967 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
2d76680f
PC
3968 bool changed = false;
3969 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
8a29084d 3970 tree type = TREE_TYPE (t);
3e605b20 3971
8a29084d 3972 constexpr_ctx new_ctx;
d4619dc1 3973 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
8a29084d 3974 {
d4619dc1
NS
3975 /* We don't really need the ctx->ctor business for a PMF or
3976 vector, but it's simpler to use the same code. */
8a29084d
JM
3977 new_ctx = *ctx;
3978 new_ctx.ctor = build_constructor (type, NULL);
3979 new_ctx.object = NULL_TREE;
3980 ctx = &new_ctx;
3981 };
3982 verify_ctor_sanity (ctx, type);
3e605b20
JM
3983 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
3984 vec_alloc (*p, vec_safe_length (v));
3985
49a86fce
PP
3986 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
3987 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
3988
2d63bc39
JM
3989 unsigned i;
3990 tree index, value;
3991 bool constant_p = true;
3992 bool side_effects_p = false;
3e605b20 3993 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
2d76680f 3994 {
bcb5f3c9 3995 tree orig_value = value;
3e605b20 3996 init_subob_ctx (ctx, new_ctx, index, value);
37244b21 3997 int pos_hint = -1;
3e605b20 3998 if (new_ctx.ctor != ctx->ctor)
37244b21
PP
3999 {
4000 /* If we built a new CONSTRUCTOR, attach it now so that other
4001 initializers can refer to it. */
077dd9b3
PP
4002 constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
4003 cep->value = new_ctx.ctor;
4004 pos_hint = cep - (*p)->begin();
37244b21 4005 }
b599bf9d 4006 else if (TREE_CODE (type) == UNION_TYPE)
37244b21
PP
4007 /* Otherwise if we're constructing a non-aggregate union member, set
4008 the active union member now so that we can later detect and diagnose
4009 if its initializer attempts to activate another member. */
077dd9b3 4010 get_or_insert_ctor_field (ctx->ctor, index);
3e605b20 4011 tree elt = cxx_eval_constant_expression (&new_ctx, value,
92a596e8 4012 lval,
5a804683 4013 non_constant_p, overflow_p);
2d76680f 4014 /* Don't VERIFY_CONSTANT here. */
2b3ab879 4015 if (ctx->quiet && *non_constant_p)
3e605b20 4016 break;
bcb5f3c9 4017 if (elt != orig_value)
2d76680f 4018 changed = true;
2d63bc39
JM
4019
4020 if (!TREE_CONSTANT (elt))
4021 constant_p = false;
4022 if (TREE_SIDE_EFFECTS (elt))
4023 side_effects_p = true;
3e605b20 4024 if (index && TREE_CODE (index) == COMPONENT_REF)
2d76680f
PC
4025 {
4026 /* This is an initialization of a vfield inside a base
4027 subaggregate that we already initialized; push this
4028 initialization into the previous initialization. */
3e605b20 4029 constructor_elt *inner = base_field_constructor_elt (*p, index);
2d76680f 4030 inner->value = elt;
3e605b20 4031 changed = true;
2d76680f 4032 }
3e605b20
JM
4033 else if (index
4034 && (TREE_CODE (index) == NOP_EXPR
4035 || TREE_CODE (index) == POINTER_PLUS_EXPR))
2d76680f
PC
4036 {
4037 /* This is an initializer for an empty base; now that we've
4038 checked that it's constant, we can ignore it. */
3e605b20
JM
4039 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
4040 changed = true;
4041 }
2d76680f 4042 else
ac9ec198 4043 {
b599bf9d
PP
4044 if (TREE_CODE (type) == UNION_TYPE
4045 && (*p)->last().index != index)
37244b21
PP
4046 /* The initializer erroneously changed the active union member that
4047 we're initializing. */
b599bf9d 4048 gcc_assert (*non_constant_p);
37244b21 4049 else
1bdbef09 4050 {
37244b21
PP
4051 /* The initializer might have mutated the underlying CONSTRUCTOR,
4052 so recompute the location of the target constructer_elt. */
4053 constructor_elt *cep
4054 = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
4055 cep->value = elt;
1bdbef09 4056 }
37244b21 4057
1bdbef09 4058 /* Adding or replacing an element might change the ctor's flags. */
ac9ec198
MP
4059 TREE_CONSTANT (ctx->ctor) = constant_p;
4060 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
4061 }
2d76680f
PC
4062 }
4063 if (*non_constant_p || !changed)
3e605b20
JM
4064 return t;
4065 t = ctx->ctor;
4066 /* We're done building this CONSTRUCTOR, so now we can interpret an
4067 element without an explicit initializer as value-initialized. */
e8c48716 4068 CONSTRUCTOR_NO_CLEARING (t) = false;
2d63bc39
JM
4069 TREE_CONSTANT (t) = constant_p;
4070 TREE_SIDE_EFFECTS (t) = side_effects_p;
8a29084d 4071 if (VECTOR_TYPE_P (type))
2d76680f
PC
4072 t = fold (t);
4073 return t;
4074}
4075
4076/* Subroutine of cxx_eval_constant_expression.
4077 The expression tree T is a VEC_INIT_EXPR which denotes the desired
4078 initialization of a non-static data member of array type. Reduce it to a
4079 CONSTRUCTOR.
4080
4081 Note that apart from value-initialization (when VALUE_INIT is true),
4082 this is only intended to support value-initialization and the
4083 initializations done by defaulted constructors for classes with
4084 non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
4085 will either be NULL_TREE for the default constructor, or a COMPONENT_REF
4086 for the copy/move constructor. */
4087
4088static tree
3e605b20 4089cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
92a596e8 4090 bool value_init, bool lval,
2d76680f
PC
4091 bool *non_constant_p, bool *overflow_p)
4092{
4093 tree elttype = TREE_TYPE (atype);
3e605b20
JM
4094 verify_ctor_sanity (ctx, atype);
4095 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
2d76680f 4096 bool pre_init = false;
4784470a 4097 unsigned HOST_WIDE_INT i;
dc5ca6c8 4098 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
2d76680f 4099
a1c9c9ff
JM
4100 if (init && TREE_CODE (init) == CONSTRUCTOR)
4101 return cxx_eval_bare_aggregate (ctx, init, lval,
4102 non_constant_p, overflow_p);
4103
2d76680f
PC
4104 /* For the default constructor, build up a call to the default
4105 constructor of the element type. We only need to handle class types
4106 here, as for a constructor to be constexpr, all members must be
4107 initialized, which for a defaulted default constructor means they must
4108 be of a class type with a constexpr default constructor. */
4109 if (TREE_CODE (elttype) == ARRAY_TYPE)
4110 /* We only do this at the lowest level. */;
4111 else if (value_init)
4112 {
dc5ca6c8 4113 init = build_value_init (elttype, complain);
2d76680f
PC
4114 pre_init = true;
4115 }
4116 else if (!init)
4117 {
cd9cf97b 4118 releasing_vec argvec;
2d76680f
PC
4119 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4120 &argvec, elttype, LOOKUP_NORMAL,
dc5ca6c8 4121 complain);
5dab8b11 4122 init = build_aggr_init_expr (elttype, init);
2d76680f
PC
4123 pre_init = true;
4124 }
4125
74f8705e
MP
4126 tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
4127 overflow_p);
4128 unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
4784470a 4129 for (i = 0; i < max; ++i)
2d76680f
PC
4130 {
4131 tree idx = build_int_cst (size_type_node, i);
4132 tree eltinit;
928af3bf 4133 bool reuse = false;
3e605b20
JM
4134 constexpr_ctx new_ctx;
4135 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
4136 if (new_ctx.ctor != ctx->ctor)
4137 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
2d76680f
PC
4138 if (TREE_CODE (elttype) == ARRAY_TYPE)
4139 {
4140 /* A multidimensional array; recurse. */
4141 if (value_init || init == NULL_TREE)
928af3bf
JJ
4142 {
4143 eltinit = NULL_TREE;
4144 reuse = i == 0;
4145 }
2d76680f 4146 else
dc5ca6c8 4147 eltinit = cp_build_array_ref (input_location, init, idx, complain);
3e605b20 4148 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
92a596e8 4149 lval,
2d76680f
PC
4150 non_constant_p, overflow_p);
4151 }
4152 else if (pre_init)
4153 {
4154 /* Initializing an element using value or default initialization
4155 we just pre-built above. */
3ee378fb
JM
4156 if (init == void_node)
4157 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4158 return ctx->ctor;
928af3bf
JJ
4159 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
4160 non_constant_p, overflow_p);
4161 reuse = i == 0;
2d76680f
PC
4162 }
4163 else
4164 {
4165 /* Copying an element. */
4166 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4167 (atype, TREE_TYPE (init)));
dc5ca6c8 4168 eltinit = cp_build_array_ref (input_location, init, idx, complain);
72b3e203 4169 if (!lvalue_p (init))
2d76680f 4170 eltinit = move (eltinit);
dc5ca6c8 4171 eltinit = force_rvalue (eltinit, complain);
c2236b9b
JJ
4172 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
4173 non_constant_p, overflow_p);
2d76680f 4174 }
2b3ab879 4175 if (*non_constant_p && !ctx->quiet)
3e605b20
JM
4176 break;
4177 if (new_ctx.ctor != ctx->ctor)
4178 {
4179 /* We appended this element above; update the value. */
4180 gcc_assert ((*p)->last().index == idx);
4181 (*p)->last().value = eltinit;
4182 }
4183 else
4184 CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
928af3bf 4185 /* Reuse the result of cxx_eval_constant_expression call
c2236b9b
JJ
4186 from the first iteration to all others if it is a constant
4187 initializer that doesn't require relocations. */
928af3bf
JJ
4188 if (reuse
4189 && max > 1
c2236b9b
JJ
4190 && (eltinit == NULL_TREE
4191 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
4192 == null_pointer_node)))
928af3bf
JJ
4193 {
4194 if (new_ctx.ctor != ctx->ctor)
4195 eltinit = new_ctx.ctor;
1e027956
RB
4196 tree range = build2 (RANGE_EXPR, size_type_node,
4197 build_int_cst (size_type_node, 1),
4198 build_int_cst (size_type_node, max - 1));
4199 CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
928af3bf
JJ
4200 break;
4201 }
1e027956
RB
4202 else if (i == 0)
4203 vec_safe_reserve (*p, max);
2d76680f
PC
4204 }
4205
4206 if (!*non_constant_p)
4207 {
3e605b20 4208 init = ctx->ctor;
e8c48716 4209 CONSTRUCTOR_NO_CLEARING (init) = false;
2d76680f 4210 }
2d76680f
PC
4211 return init;
4212}
4213
4214static tree
3e605b20 4215cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
92a596e8 4216 bool lval,
2d76680f
PC
4217 bool *non_constant_p, bool *overflow_p)
4218{
4219 tree atype = TREE_TYPE (t);
4220 tree init = VEC_INIT_EXPR_INIT (t);
3e605b20 4221 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
2d76680f 4222 VEC_INIT_EXPR_VALUE_INIT (t),
92a596e8 4223 lval, non_constant_p, overflow_p);
2d76680f
PC
4224 if (*non_constant_p)
4225 return t;
4226 else
4227 return r;
4228}
4229
e4511ca2
JM
4230/* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
4231 where the desired type is an array of unknown bounds because the variable
4232 has had its bounds deduced since the wrapping expression was created. */
4233
4234static bool
4235same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
4236{
4237 while (TREE_CODE (type1) == ARRAY_TYPE
4238 && TREE_CODE (type2) == ARRAY_TYPE
4239 && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
4240 {
4241 type1 = TREE_TYPE (type1);
4242 type2 = TREE_TYPE (type2);
4243 }
4244 return same_type_ignoring_top_level_qualifiers_p (type1, type2);
4245}
4246
0fe2ae29
JJ
4247/* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4248
4249static tree
4250cxx_fold_indirect_ref_1 (location_t loc, tree type, tree op,
4251 unsigned HOST_WIDE_INT off, bool *empty_base)
4252{
4253 tree optype = TREE_TYPE (op);
4254 unsigned HOST_WIDE_INT const_nunits;
4255 if (off == 0)
4256 {
4257 if (similar_type_p (optype, type))
4258 return op;
4259 /* Also handle conversion to an empty base class, which
4260 is represented with a NOP_EXPR. */
4261 /* *(foo *)&complexfoo => __real__ complexfoo */
4262 else if (TREE_CODE (optype) == COMPLEX_TYPE
4263 && similar_type_p (type, TREE_TYPE (optype)))
4264 return build1_loc (loc, REALPART_EXPR, type, op);
4265 }
4266 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
4267 else if (TREE_CODE (optype) == COMPLEX_TYPE
4268 && similar_type_p (type, TREE_TYPE (optype))
4269 && tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
4270 return build1_loc (loc, IMAGPART_EXPR, type, op);
4271 if (is_empty_class (type)
4272 && CLASS_TYPE_P (optype)
4273 && DERIVED_FROM_P (type, optype))
4274 {
4275 *empty_base = true;
4276 return op;
4277 }
4278 /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
4279 else if (VECTOR_TYPE_P (optype)
4280 && similar_type_p (type, TREE_TYPE (optype))
4281 && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
4282 {
4283 unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
4284 unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
4285 if (off < max_offset && off % part_width == 0)
4286 {
4287 tree index = bitsize_int (off * BITS_PER_UNIT);
4288 return build3_loc (loc, BIT_FIELD_REF, type, op,
4289 TYPE_SIZE (type), index);
4290 }
4291 }
4292 /* ((foo *)&fooarray)[x] => fooarray[x] */
4293 else if (TREE_CODE (optype) == ARRAY_TYPE
4294 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
4295 && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
4296 {
4297 tree type_domain = TYPE_DOMAIN (optype);
4298 tree min_val = size_zero_node;
4299 if (type_domain && TYPE_MIN_VALUE (type_domain))
4300 min_val = TYPE_MIN_VALUE (type_domain);
4301 unsigned HOST_WIDE_INT el_sz
4302 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
4303 unsigned HOST_WIDE_INT idx = off / el_sz;
4304 unsigned HOST_WIDE_INT rem = off % el_sz;
4305 if (tree_fits_uhwi_p (min_val))
4306 {
4307 tree index = size_int (idx + tree_to_uhwi (min_val));
4308 op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
4309 NULL_TREE, NULL_TREE);
4310 return cxx_fold_indirect_ref_1 (loc, type, op, rem,
4311 empty_base);
4312 }
4313 }
4314 /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
4315 else if (TREE_CODE (optype) == RECORD_TYPE)
4316 {
4317 for (tree field = TYPE_FIELDS (optype);
4318 field; field = DECL_CHAIN (field))
4319 if (TREE_CODE (field) == FIELD_DECL
4320 && TREE_TYPE (field) != error_mark_node
4321 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
4322 {
4323 tree pos = byte_position (field);
4324 if (!tree_fits_uhwi_p (pos))
4325 continue;
4326 unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
4327 unsigned el_sz
4328 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
4329 if (upos <= off && off < upos + el_sz)
4330 {
4331 tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
4332 op, field, NULL_TREE);
4333 if (tree ret = cxx_fold_indirect_ref_1 (loc, type, cop,
4334 off - upos,
4335 empty_base))
4336 return ret;
4337 }
4338 }
4339 }
4340
4341 return NULL_TREE;
4342}
4343
2d76680f
PC
4344/* A less strict version of fold_indirect_ref_1, which requires cv-quals to
4345 match. We want to be less strict for simple *& folding; if we have a
4346 non-const temporary that we access through a const pointer, that should
4347 work. We handle this here rather than change fold_indirect_ref_1
4348 because we're dealing with things like ADDR_EXPR of INTEGER_CST which
4349 don't really make sense outside of constant expression evaluation. Also
4350 we want to allow folding to COMPONENT_REF, which could cause trouble
0fe2ae29 4351 with TBAA in fold_indirect_ref_1. */
2d76680f
PC
4352
4353static tree
4354cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
4355{
ebe4bf41
MP
4356 tree sub = op0;
4357 tree subtype;
4358 poly_uint64 const_op01;
2d76680f 4359
80de0002
JM
4360 /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
4361 while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
4362 || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
4363 {
4364 if (TREE_CODE (sub) == NOP_EXPR
4365 && REINTERPRET_CAST_P (sub))
4366 return NULL_TREE;
4367 sub = TREE_OPERAND (sub, 0);
4368 }
4369
2d76680f 4370 subtype = TREE_TYPE (sub);
71a93b08 4371 if (!INDIRECT_TYPE_P (subtype))
2d76680f
PC
4372 return NULL_TREE;
4373
4374 if (TREE_CODE (sub) == ADDR_EXPR)
4375 {
4376 tree op = TREE_OPERAND (sub, 0);
4377 tree optype = TREE_TYPE (op);
4378
4379 /* *&CONST_DECL -> to the value of the const decl. */
4380 if (TREE_CODE (op) == CONST_DECL)
4381 return DECL_INITIAL (op);
4382 /* *&p => p; make sure to handle *&"str"[cst] here. */
1a120ec1 4383 if (similar_type_p (optype, type))
2d76680f
PC
4384 {
4385 tree fop = fold_read_from_constant_string (op);
4386 if (fop)
4387 return fop;
4388 else
4389 return op;
4390 }
0fe2ae29
JJ
4391 else
4392 return cxx_fold_indirect_ref_1 (loc, type, op, 0, empty_base);
2d76680f
PC
4393 }
4394 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
0fe2ae29 4395 && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
2d76680f
PC
4396 {
4397 tree op00 = TREE_OPERAND (sub, 0);
4398 tree op01 = TREE_OPERAND (sub, 1);
4399
4400 STRIP_NOPS (op00);
4401 if (TREE_CODE (op00) == ADDR_EXPR)
0fe2ae29
JJ
4402 return cxx_fold_indirect_ref_1 (loc, type, TREE_OPERAND (op00, 0),
4403 tree_to_uhwi (op01), empty_base);
2d76680f
PC
4404 }
4405 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4406 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
1a120ec1 4407 && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
2d76680f
PC
4408 {
4409 tree type_domain;
4410 tree min_val = size_zero_node;
ebe4bf41
MP
4411 tree newsub
4412 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
2d76680f
PC
4413 if (newsub)
4414 sub = newsub;
4415 else
4416 sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
4417 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
4418 if (type_domain && TYPE_MIN_VALUE (type_domain))
4419 min_val = TYPE_MIN_VALUE (type_domain);
4420 return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
4421 NULL_TREE);
4422 }
4423
4424 return NULL_TREE;
4425}
4426
4427static tree
3e605b20 4428cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
92a596e8 4429 bool lval,
2d76680f
PC
4430 bool *non_constant_p, bool *overflow_p)
4431{
4432 tree orig_op0 = TREE_OPERAND (t, 0);
2d76680f 4433 bool empty_base = false;
2d76680f 4434
cda0a029
JM
4435 /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
4436 operand is an integer-zero. Otherwise reject the MEM_REF for now. */
4437
4438 if (TREE_CODE (t) == MEM_REF
4439 && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
4440 {
4441 gcc_assert (ctx->quiet);
4442 *non_constant_p = true;
4443 return t;
4444 }
4445
255a48d6
JM
4446 /* First try to simplify it directly. */
4447 tree r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), orig_op0,
4448 &empty_base);
4449 if (!r)
2d76680f 4450 {
255a48d6
JM
4451 /* If that didn't work, evaluate the operand first. */
4452 tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
4453 /*lval*/false, non_constant_p,
4454 overflow_p);
4455 /* Don't VERIFY_CONSTANT here. */
4456 if (*non_constant_p)
4457 return t;
4458
8bada5cd
MS
4459 if (!lval && integer_zerop (op0))
4460 {
4461 if (!ctx->quiet)
4462 error ("dereferencing a null pointer");
4463 *non_constant_p = true;
4464 return t;
4465 }
4466
255a48d6
JM
4467 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
4468 &empty_base);
4469 if (r == NULL_TREE)
2d76680f
PC
4470 {
4471 /* We couldn't fold to a constant value. Make sure it's not
4472 something we should have been able to fold. */
255a48d6
JM
4473 tree sub = op0;
4474 STRIP_NOPS (sub);
4475 if (TREE_CODE (sub) == ADDR_EXPR)
4476 {
1a120ec1 4477 gcc_assert (!similar_type_p
255a48d6
JM
4478 (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
4479 /* DR 1188 says we don't have to deal with this. */
4480 if (!ctx->quiet)
1a120ec1
JM
4481 error_at (cp_expr_loc_or_input_loc (t),
4482 "accessing value of %qE through a %qT glvalue in a "
4483 "constant expression", build_fold_indirect_ref (sub),
4484 TREE_TYPE (t));
255a48d6
JM
4485 *non_constant_p = true;
4486 return t;
4487 }
4488
4489 if (lval && op0 != orig_op0)
4490 return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
4491 if (!lval)
4492 VERIFY_CONSTANT (t);
2d76680f
PC
4493 return t;
4494 }
4495 }
4496
255a48d6
JM
4497 r = cxx_eval_constant_expression (ctx, r,
4498 lval, non_constant_p, overflow_p);
4499 if (*non_constant_p)
4500 return t;
4501
125db6a1 4502 /* If we're pulling out the value of an empty base, just return an empty
2d76680f 4503 CONSTRUCTOR. */
92a596e8 4504 if (empty_base && !lval)
2d76680f 4505 {
2d76680f
PC
4506 r = build_constructor (TREE_TYPE (t), NULL);
4507 TREE_CONSTANT (r) = true;
4508 }
4509
2d76680f
PC
4510 return r;
4511}
4512
4513/* Complain about R, a VAR_DECL, not being usable in a constant expression.
4514 Shared between potential_constant_expression and
4515 cxx_eval_constant_expression. */
4516
4517static void
6821245b 4518non_const_var_error (location_t loc, tree r)
2d76680f 4519{
8e007055 4520 auto_diagnostic_group d;
2d76680f 4521 tree type = TREE_TYPE (r);
8e007055
JJ
4522 if (DECL_NAME (r) == heap_uninit_identifier
4523 || DECL_NAME (r) == heap_identifier)
4524 {
6821245b
JM
4525 error_at (loc, "the content of uninitialized storage is not usable "
4526 "in a constant expression");
8e007055
JJ
4527 inform (DECL_SOURCE_LOCATION (r), "allocated here");
4528 return;
4529 }
4530 if (DECL_NAME (r) == heap_deleted_identifier)
4531 {
6821245b
JM
4532 error_at (loc, "use of allocated storage after deallocation in a "
4533 "constant expression");
8e007055
JJ
4534 inform (DECL_SOURCE_LOCATION (r), "allocated here");
4535 return;
4536 }
6821245b
JM
4537 error_at (loc, "the value of %qD is not usable in a constant "
4538 "expression", r);
2d76680f
PC
4539 /* Avoid error cascade. */
4540 if (DECL_INITIAL (r) == error_mark_node)
4541 return;
4542 if (DECL_DECLARED_CONSTEXPR_P (r))
4543 inform (DECL_SOURCE_LOCATION (r),
4544 "%qD used in its own initializer", r);
4545 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4546 {
4547 if (!CP_TYPE_CONST_P (type))
4548 inform (DECL_SOURCE_LOCATION (r),
4549 "%q#D is not const", r);
4550 else if (CP_TYPE_VOLATILE_P (type))
4551 inform (DECL_SOURCE_LOCATION (r),
4552 "%q#D is volatile", r);
4553 else if (!DECL_INITIAL (r)
a3e2b438
PP
4554 || !TREE_CONSTANT (DECL_INITIAL (r))
4555 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
2d76680f
PC
4556 inform (DECL_SOURCE_LOCATION (r),
4557 "%qD was not initialized with a constant "
4558 "expression", r);
4559 else
4560 gcc_unreachable ();
4561 }
9f613f06 4562 else if (TYPE_REF_P (type))
fd338b13
JM
4563 inform (DECL_SOURCE_LOCATION (r),
4564 "%qD was not initialized with a constant "
4565 "expression", r);
2d76680f
PC
4566 else
4567 {
4568 if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
4569 inform (DECL_SOURCE_LOCATION (r),
4570 "%qD was not declared %<constexpr%>", r);
4571 else
4572 inform (DECL_SOURCE_LOCATION (r),
4573 "%qD does not have integral or enumeration type",
4574 r);
4575 }
4576}
4577
4578/* Subroutine of cxx_eval_constant_expression.
4579 Like cxx_eval_unary_expression, except for trinary expressions. */
4580
4581static tree
3e605b20 4582cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
92a596e8 4583 bool lval,
2d76680f
PC
4584 bool *non_constant_p, bool *overflow_p)
4585{
4586 int i;
4587 tree args[3];
4588 tree val;
4589
4590 for (i = 0; i < 3; i++)
4591 {
3e605b20 4592 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
92a596e8 4593 lval,
5a804683 4594 non_constant_p, overflow_p);
2d76680f
PC
4595 VERIFY_CONSTANT (args[i]);
4596 }
4597
4598 val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
4599 args[0], args[1], args[2]);
4600 if (val == NULL_TREE)
4601 return t;
4602 VERIFY_CONSTANT (val);
4603 return val;
4604}
4605
98e5a19a
JM
4606/* True if T was declared in a function declared to be constexpr, and
4607 therefore potentially constant in C++14. */
4608
2d76680f
PC
4609bool
4610var_in_constexpr_fn (tree t)
4611{
4612 tree ctx = DECL_CONTEXT (t);
98e5a19a 4613 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
2d76680f
PC
4614 && DECL_DECLARED_CONSTEXPR_P (ctx));
4615}
4616
98e5a19a
JM
4617/* True if T was declared in a function that might be constexpr: either a
4618 function that was declared constexpr, or a C++17 lambda op(). */
4619
4620bool
4621var_in_maybe_constexpr_fn (tree t)
4622{
7b936140 4623 if (cxx_dialect >= cxx17
98e5a19a
JM
4624 && DECL_FUNCTION_SCOPE_P (t)
4625 && LAMBDA_FUNCTION_P (DECL_CONTEXT (t)))
4626 return true;
4627 return var_in_constexpr_fn (t);
4628}
4629
68d01920
JM
4630/* We're assigning INIT to TARGET. In do_build_copy_constructor and
4631 build_over_call we implement trivial copy of a class with tail padding using
4632 assignment of character arrays, which is valid in normal code, but not in
4633 constexpr evaluation. We don't need to worry about clobbering tail padding
4634 in constexpr evaluation, so strip the type punning. */
4635
4636static void
4637maybe_simplify_trivial_copy (tree &target, tree &init)
4638{
4639 if (TREE_CODE (target) == MEM_REF
4640 && TREE_CODE (init) == MEM_REF
4641 && TREE_TYPE (target) == TREE_TYPE (init)
4642 && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
4643 && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
4644 {
4645 target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
4646 init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
4647 }
4648}
4649
7eb5be6a
MP
4650/* Returns true if REF, which is a COMPONENT_REF, has any fields
4651 of constant type. This does not check for 'mutable', so the
4652 caller is expected to be mindful of that. */
4653
4654static bool
4655cref_has_const_field (tree ref)
4656{
4657 while (TREE_CODE (ref) == COMPONENT_REF)
4658 {
4659 if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
4660 return true;
4661 ref = TREE_OPERAND (ref, 0);
4662 }
4663 return false;
4664}
4665
04e1749c
MP
4666/* Return true if we are modifying something that is const during constant
4667 expression evaluation. CODE is the code of the statement, OBJ is the
4668 object in question, MUTABLE_P is true if one of the subobjects were
4669 declared mutable. */
4670
4671static bool
4672modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
4673{
4674 /* If this is initialization, there's no problem. */
4675 if (code != MODIFY_EXPR)
4676 return false;
4677
4678 /* [basic.type.qualifier] "A const object is an object of type
4679 const T or a non-mutable subobject of a const object." */
4680 if (mutable_p)
4681 return false;
4682
7eb5be6a
MP
4683 if (TREE_READONLY (obj))
4684 return true;
4685
4686 if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
4687 {
4688 /* Although a COMPONENT_REF may have a const type, we should
4689 only consider it modifying a const object when any of the
4690 field components is const. This can happen when using
4691 constructs such as const_cast<const T &>(m), making something
4692 const even though it wasn't declared const. */
4693 if (TREE_CODE (obj) == COMPONENT_REF)
4694 return cref_has_const_field (obj);
4695 else
4696 return true;
4697 }
4698
4699 return false;
04e1749c
MP
4700}
4701
60813a46
JM
4702/* Evaluate an INIT_EXPR or MODIFY_EXPR. */
4703
4704static tree
4705cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
92a596e8 4706 bool lval,
60813a46
JM
4707 bool *non_constant_p, bool *overflow_p)
4708{
4709 constexpr_ctx new_ctx = *ctx;
4710
58cc255c
JM
4711 tree init = TREE_OPERAND (t, 1);
4712 if (TREE_CLOBBER_P (init))
4713 /* Just ignore clobbers. */
4714 return void_node;
4715
60813a46
JM
4716 /* First we figure out where we're storing to. */
4717 tree target = TREE_OPERAND (t, 0);
68d01920
JM
4718
4719 maybe_simplify_trivial_copy (target, init);
4720
3f8e2835 4721 tree type = TREE_TYPE (target);
e8b3c1bc
JM
4722 bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
4723 if (preeval)
4724 {
4725 /* Evaluate the value to be stored without knowing what object it will be
4726 stored in, so that any side-effects happen first. */
4727 if (!SCALAR_TYPE_P (type))
4728 new_ctx.ctor = new_ctx.object = NULL_TREE;
4729 init = cxx_eval_constant_expression (&new_ctx, init, false,
4730 non_constant_p, overflow_p);
4731 if (*non_constant_p)
4732 return t;
4733 }
60813a46 4734
d0aa42d2
JM
4735 bool evaluated = false;
4736 if (lval)
3f8e2835 4737 {
d0aa42d2
JM
4738 /* If we want to return a reference to the target, we need to evaluate it
4739 as a whole; otherwise, only evaluate the innermost piece to avoid
4740 building up unnecessary *_REFs. */
4741 target = cxx_eval_constant_expression (ctx, target, true,
4742 non_constant_p, overflow_p);
4743 evaluated = true;
4744 if (*non_constant_p)
4745 return t;
3f8e2835
JM
4746 }
4747
043666e0 4748 /* Find the underlying variable. */
cd9cf97b 4749 releasing_vec refs;
60813a46 4750 tree object = NULL_TREE;
04e1749c
MP
4751 /* If we're modifying a const object, save it. */
4752 tree const_object_being_modified = NULL_TREE;
4753 bool mutable_p = false;
60813a46
JM
4754 for (tree probe = target; object == NULL_TREE; )
4755 {
4756 switch (TREE_CODE (probe))
4757 {
4758 case BIT_FIELD_REF:
4759 case COMPONENT_REF:
4760 case ARRAY_REF:
043666e0
JM
4761 {
4762 tree ob = TREE_OPERAND (probe, 0);
4763 tree elt = TREE_OPERAND (probe, 1);
7d349dd8 4764 if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
04e1749c 4765 mutable_p = true;
043666e0
JM
4766 if (TREE_CODE (probe) == ARRAY_REF)
4767 {
4768 elt = eval_and_check_array_index (ctx, probe, false,
4769 non_constant_p, overflow_p);
4770 if (*non_constant_p)
4771 return t;
4772 }
0f184800
MP
4773 /* We don't check modifying_const_object_p for ARRAY_REFs. Given
4774 "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
4775 the array isn't const. Instead, check "a" in the next iteration;
4776 that will detect modifying "const int a[10]". */
4777 else if (evaluated
4778 && modifying_const_object_p (TREE_CODE (t), probe,
4779 mutable_p)
4780 && const_object_being_modified == NULL_TREE)
4781 const_object_being_modified = probe;
043666e0
JM
4782 vec_safe_push (refs, elt);
4783 vec_safe_push (refs, TREE_TYPE (probe));
4784 probe = ob;
4785 }
60813a46
JM
4786 break;
4787
4788 default:
d0aa42d2
JM
4789 if (evaluated)
4790 object = probe;
4791 else
4792 {
4793 probe = cxx_eval_constant_expression (ctx, probe, true,
4794 non_constant_p, overflow_p);
4795 evaluated = true;
4796 if (*non_constant_p)
4797 return t;
4798 }
4799 break;
60813a46
JM
4800 }
4801 }
4802
04e1749c
MP
4803 if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
4804 && const_object_being_modified == NULL_TREE)
4805 const_object_being_modified = object;
4806
60813a46
JM
4807 /* And then find/build up our initializer for the path to the subobject
4808 we're initializing. */
a1408b7e 4809 tree *valp;
077dd9b3 4810 if (DECL_P (object))
8e007055 4811 valp = ctx->global->values.get (object);
a1408b7e
JM
4812 else
4813 valp = NULL;
60813a46
JM
4814 if (!valp)
4815 {
4816 /* A constant-expression cannot modify objects from outside the
4817 constant-expression. */
2b3ab879 4818 if (!ctx->quiet)
64d6d399 4819 error ("modification of %qE is not a constant expression", object);
60813a46
JM
4820 *non_constant_p = true;
4821 return t;
4822 }
3f8e2835 4823 type = TREE_TYPE (object);
c75ce530 4824 bool no_zero_init = true;
2d63bc39 4825
37244b21
PP
4826 releasing_vec ctors, indexes;
4827 auto_vec<int> index_pos_hints;
4828 bool activated_union_member_p = false;
276a52d5 4829 while (!refs->is_empty ())
60813a46
JM
4830 {
4831 if (*valp == NULL_TREE)
4832 {
4833 *valp = build_constructor (type, NULL);
e8c48716 4834 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
60813a46 4835 }
d6b46fca
NS
4836 else if (TREE_CODE (*valp) == STRING_CST)
4837 {
4838 /* An array was initialized with a string constant, and now
4839 we're writing into one of its elements. Explode the
4840 single initialization into a set of element
4841 initializations. */
4842 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
4843
4844 tree string = *valp;
4845 tree elt_type = TREE_TYPE (type);
4846 unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
4847 / TYPE_PRECISION (char_type_node));
4848 unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
4849 tree ary_ctor = build_constructor (type, NULL);
4850
4851 vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
4852 for (unsigned ix = 0; ix != num_elts; ix++)
4853 {
4854 constructor_elt elt =
4855 {
4856 build_int_cst (size_type_node, ix),
4857 extract_string_elt (string, chars_per_elt, ix)
4858 };
4859 CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
4860 }
4861
4862 *valp = ary_ctor;
4863 }
4864
c75ce530
JM
4865 /* If the value of object is already zero-initialized, any new ctors for
4866 subobjects will also be zero-initialized. */
e8c48716 4867 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
60813a46 4868
ceaaf873 4869 enum tree_code code = TREE_CODE (type);
60813a46 4870 type = refs->pop();
ceaaf873 4871 tree index = refs->pop();
60813a46 4872
37244b21
PP
4873 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
4874 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
ceaaf873 4875 {
b04445d4 4876 if (cxx_dialect < cxx20)
ed4ec9ce 4877 {
37244b21
PP
4878 if (!ctx->quiet)
4879 error_at (cp_expr_loc_or_input_loc (t),
4880 "change of the active member of a union "
4881 "from %qD to %qD",
4882 CONSTRUCTOR_ELT (*valp, 0)->index,
4883 index);
4884 *non_constant_p = true;
ed4ec9ce 4885 }
37244b21
PP
4886 else if (TREE_CODE (t) == MODIFY_EXPR
4887 && CONSTRUCTOR_NO_CLEARING (*valp))
ceaaf873 4888 {
37244b21
PP
4889 /* Diagnose changing the active union member while the union
4890 is in the process of being initialized. */
4891 if (!ctx->quiet)
4892 error_at (cp_expr_loc_or_input_loc (t),
4893 "change of the active member of a union "
4894 "from %qD to %qD during initialization",
4895 CONSTRUCTOR_ELT (*valp, 0)->index,
4896 index);
4897 *non_constant_p = true;
ceaaf873 4898 }
37244b21
PP
4899 no_zero_init = true;
4900 }
88504f34 4901
37244b21
PP
4902 vec_safe_push (ctors, *valp);
4903 vec_safe_push (indexes, index);
88504f34 4904
37244b21 4905 constructor_elt *cep
077dd9b3 4906 = get_or_insert_ctor_field (*valp, index);
37244b21
PP
4907 index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
4908
4909 if (code == UNION_TYPE)
4910 activated_union_member_p = true;
b599bf9d 4911
60813a46
JM
4912 valp = &cep->value;
4913 }
60813a46 4914
04e1749c
MP
4915 /* Detect modifying a constant object in constexpr evaluation.
4916 We have found a const object that is being modified. Figure out
4917 if we need to issue an error. Consider
4918
4919 struct A {
4920 int n;
4921 constexpr A() : n(1) { n = 2; } // #1
4922 };
4923 struct B {
4924 const A a;
4925 constexpr B() { a.n = 3; } // #2
4926 };
4927 constexpr B b{};
4928
4929 #1 is OK, since we're modifying an object under construction, but
4930 #2 is wrong, since "a" is const and has been fully constructed.
4931 To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
4932 which means that the object is read-only. For the example above, the
4933 *ctors stack at the point of #2 will look like:
4934
4935 ctors[0] = {.a={.n=2}} TREE_READONLY = 0
4936 ctors[1] = {.n=2} TREE_READONLY = 1
4937
4938 and we're modifying "b.a", so we search the stack and see if the
4939 constructor for "b.a" has already run. */
4940 if (const_object_being_modified)
4941 {
4942 bool fail = false;
276a52d5
JJ
4943 tree const_objtype
4944 = strip_array_types (TREE_TYPE (const_object_being_modified));
4945 if (!CLASS_TYPE_P (const_objtype))
04e1749c
MP
4946 fail = true;
4947 else
4948 {
4949 /* [class.ctor]p5 "A constructor can be invoked for a const,
4950 volatile, or const volatile object. const and volatile
4951 semantics are not applied on an object under construction.
4952 They come into effect when the constructor for the most
4953 derived object ends." */
4954 tree elt;
4955 unsigned int i;
4956 FOR_EACH_VEC_ELT (*ctors, i, elt)
4957 if (same_type_ignoring_top_level_qualifiers_p
4958 (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt)))
4959 {
4960 fail = TREE_READONLY (elt);
4961 break;
4962 }
4963 }
4964 if (fail)
4965 {
4966 if (!ctx->quiet)
4967 modifying_const_object_error (t, const_object_being_modified);
4968 *non_constant_p = true;
4969 return t;
4970 }
4971 }
4972
e8b3c1bc 4973 if (!preeval)
60813a46
JM
4974 {
4975 /* Create a new CONSTRUCTOR in case evaluation of the initializer
4976 wants to modify it. */
acb2970c 4977 if (*valp == NULL_TREE)
73a84269 4978 {
664beaf2 4979 *valp = build_constructor (type, NULL);
e8c48716 4980 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
73a84269 4981 }
664beaf2 4982 new_ctx.ctor = *valp;
60813a46 4983 new_ctx.object = target;
10d2f801
JM
4984 /* Avoid temporary materialization when initializing from a TARGET_EXPR.
4985 We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
4986 expansion of those trees uses ctx instead. */
4987 if (TREE_CODE (init) == TARGET_EXPR)
4988 if (tree tinit = TARGET_EXPR_INITIAL (init))
4989 init = tinit;
e8b3c1bc
JM
4990 init = cxx_eval_constant_expression (&new_ctx, init, false,
4991 non_constant_p, overflow_p);
37244b21
PP
4992 /* The hash table might have moved since the get earlier, and the
4993 initializer might have mutated the underlying CONSTRUCTORs, so we must
4994 recompute VALP. */
4995 valp = ctx->global->values.get (object);
4996 for (unsigned i = 0; i < vec_safe_length (indexes); i++)
4997 {
4998 constructor_elt *cep
4999 = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
5000 valp = &cep->value;
5001 }
60813a46
JM
5002 }
5003
7574c916 5004 /* Don't share a CONSTRUCTOR that might be changed later. */
0146e25f 5005 init = unshare_constructor (init);
d96e8407 5006
e8b3c1bc
JM
5007 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
5008 && TREE_CODE (init) == CONSTRUCTOR)
acb2970c 5009 {
d96e8407
JM
5010 /* An outer ctx->ctor might be pointing to *valp, so replace
5011 its contents. */
d0aa42d2
JM
5012 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (init),
5013 TREE_TYPE (*valp)))
5014 {
5015 /* For initialization of an empty base, the original target will be
5016 *(base*)this, evaluation of which resolves to the object
5017 argument, which has the derived type rather than the base type. In
5018 this situation, just evaluate the initializer and return, since
5019 there's no actual data to store. */
5020 gcc_assert (is_empty_class (TREE_TYPE (init)) && !lval);
5021 return init;
5022 }
d96e8407
JM
5023 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
5024 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
5025 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
e8c48716
JM
5026 CONSTRUCTOR_NO_CLEARING (*valp)
5027 = CONSTRUCTOR_NO_CLEARING (init);
acb2970c 5028 }
60813a46 5029 else
d96e8407 5030 *valp = init;
2d63bc39 5031
7eb5be6a
MP
5032 /* After initialization, 'const' semantics apply to the value of the
5033 object. Make a note of this fact by marking the CONSTRUCTOR
5034 TREE_READONLY. */
5035 if (TREE_CODE (t) == INIT_EXPR
5036 && TREE_CODE (*valp) == CONSTRUCTOR
5037 && TYPE_READONLY (type))
64da1b76
PP
5038 {
5039 if (INDIRECT_REF_P (target)
5040 && (is_this_parameter
5041 (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
5042 /* We've just initialized '*this' (perhaps via the target
5043 constructor of a delegating constructor). Leave it up to the
5044 caller that set 'this' to set TREE_READONLY appropriately. */
5045 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
5046 (TREE_TYPE (target), type));
5047 else
5048 TREE_READONLY (*valp) = true;
5049 }
7eb5be6a 5050
d96e8407
JM
5051 /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
5052 CONSTRUCTORs, if any. */
5053 tree elt;
5054 unsigned i;
5055 bool c = TREE_CONSTANT (init);
5056 bool s = TREE_SIDE_EFFECTS (init);
37244b21 5057 if (!c || s || activated_union_member_p)
cd9cf97b 5058 FOR_EACH_VEC_ELT (*ctors, i, elt)
d96e8407
JM
5059 {
5060 if (!c)
5061 TREE_CONSTANT (elt) = false;
5062 if (s)
5063 TREE_SIDE_EFFECTS (elt) = true;
b599bf9d
PP
5064 /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
5065 this union. */
5066 if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE)
5067 CONSTRUCTOR_NO_CLEARING (elt) = false;
d96e8407 5068 }
60813a46
JM
5069
5070 if (*non_constant_p)
5071 return t;
92a596e8 5072 else if (lval)
60813a46
JM
5073 return target;
5074 else
3bdf0a9b 5075 return init;
60813a46
JM
5076}
5077
5078/* Evaluate a ++ or -- expression. */
5079
5080static tree
5081cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
92a596e8 5082 bool lval,
60813a46
JM
5083 bool *non_constant_p, bool *overflow_p)
5084{
5085 enum tree_code code = TREE_CODE (t);
5086 tree type = TREE_TYPE (t);
5087 tree op = TREE_OPERAND (t, 0);
5088 tree offset = TREE_OPERAND (t, 1);
5089 gcc_assert (TREE_CONSTANT (offset));
5090
d85569f6
MP
5091 /* OFFSET is constant, but perhaps not constant enough. We need to
5092 e.g. bash FLOAT_EXPRs to REAL_CSTs. */
5093 offset = fold_simple (offset);
5094
60813a46 5095 /* The operand as an lvalue. */
2b3ab879 5096 op = cxx_eval_constant_expression (ctx, op, true,
5a804683 5097 non_constant_p, overflow_p);
60813a46
JM
5098
5099 /* The operand as an rvalue. */
84dd815f
JM
5100 tree val
5101 = cxx_eval_constant_expression (ctx, op, false,
5102 non_constant_p, overflow_p);
caee690e
JM
5103 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5104 a local array in a constexpr function. */
71a93b08 5105 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
caee690e
JM
5106 if (!ptr)
5107 VERIFY_CONSTANT (val);
60813a46
JM
5108
5109 /* The modified value. */
5110 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
ac2f8d26 5111 tree mod;
71a93b08 5112 if (INDIRECT_TYPE_P (type))
ac2f8d26
JM
5113 {
5114 /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
5115 offset = convert_to_ptrofftype (offset);
5116 if (!inc)
5117 offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
5118 mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
5119 }
5120 else
5121 mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
caee690e
JM
5122 if (!ptr)
5123 VERIFY_CONSTANT (mod);
60813a46
JM
5124
5125 /* Storing the modified value. */
04e1749c
MP
5126 tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
5127 MODIFY_EXPR, type, op, mod);
2b3ab879 5128 cxx_eval_constant_expression (ctx, store,
5a804683 5129 true, non_constant_p, overflow_p);
ecdcd560 5130 ggc_free (store);
60813a46
JM
5131
5132 /* And the value of the expression. */
5133 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
5134 {
5135 /* Prefix ops are lvalues. */
92a596e8 5136 if (lval)
60813a46
JM
5137 return op;
5138 else
5139 /* But we optimize when the caller wants an rvalue. */
5140 return mod;
5141 }
5142 else
5143 /* Postfix ops are rvalues. */
5144 return val;
5145}
5146
56632b27
JM
5147/* Predicates for the meaning of *jump_target. */
5148
5149static bool
5150returns (tree *jump_target)
5151{
5152 return *jump_target
d49318d9
PC
5153 && (TREE_CODE (*jump_target) == RETURN_EXPR
5154 || (TREE_CODE (*jump_target) == LABEL_DECL
5155 && LABEL_DECL_CDTOR (*jump_target)));
56632b27
JM
5156}
5157
5158static bool
5159breaks (tree *jump_target)
5160{
5161 return *jump_target
06ec22b7
JM
5162 && ((TREE_CODE (*jump_target) == LABEL_DECL
5163 && LABEL_DECL_BREAK (*jump_target))
3075affd 5164 || TREE_CODE (*jump_target) == BREAK_STMT
06ec22b7 5165 || TREE_CODE (*jump_target) == EXIT_EXPR);
56632b27
JM
5166}
5167
5168static bool
5169continues (tree *jump_target)
5170{
5171 return *jump_target
3075affd
JM
5172 && ((TREE_CODE (*jump_target) == LABEL_DECL
5173 && LABEL_DECL_CONTINUE (*jump_target))
5174 || TREE_CODE (*jump_target) == CONTINUE_STMT);
5175
56632b27
JM
5176}
5177
5178static bool
5179switches (tree *jump_target)
5180{
5181 return *jump_target
5182 && TREE_CODE (*jump_target) == INTEGER_CST;
5183}
5184
5185/* Subroutine of cxx_eval_statement_list. Determine whether the statement
4b390698
JJ
5186 STMT matches *jump_target. If we're looking for a case label and we see
5187 the default label, note it in ctx->css_state. */
56632b27
JM
5188
5189static bool
4b390698 5190label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
56632b27 5191{
56632b27
JM
5192 switch (TREE_CODE (*jump_target))
5193 {
5194 case LABEL_DECL:
5195 if (TREE_CODE (stmt) == LABEL_EXPR
5196 && LABEL_EXPR_LABEL (stmt) == *jump_target)
5197 return true;
5198 break;
5199
5200 case INTEGER_CST:
5201 if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
5202 {
4b390698 5203 gcc_assert (ctx->css_state != NULL);
56632b27 5204 if (!CASE_LOW (stmt))
4b390698
JJ
5205 {
5206 /* default: should appear just once in a SWITCH_EXPR
5207 body (excluding nested SWITCH_EXPR). */
5208 gcc_assert (*ctx->css_state != css_default_seen);
5209 /* When evaluating SWITCH_EXPR body for the second time,
5210 return true for the default: label. */
5211 if (*ctx->css_state == css_default_processing)
5212 return true;
5213 *ctx->css_state = css_default_seen;
5214 }
385ed708
JJ
5215 else if (CASE_HIGH (stmt))
5216 {
5217 if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
5218 && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
5219 return true;
5220 }
56632b27
JM
5221 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
5222 return true;
5223 }
5224 break;
5225
43574e4f
JJ
5226 case BREAK_STMT:
5227 case CONTINUE_STMT:
5228 /* These two are handled directly in cxx_eval_loop_expr by testing
5229 breaks (jump_target) or continues (jump_target). */
5230 break;
5231
56632b27
JM
5232 default:
5233 gcc_unreachable ();
5234 }
5235 return false;
5236}
5237
5238/* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
5239 semantics, for switch, break, continue, and return. */
5240
5241static tree
5242cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
56632b27
JM
5243 bool *non_constant_p, bool *overflow_p,
5244 tree *jump_target)
5245{
5246 tree_stmt_iterator i;
58611fb6 5247 tree local_target;
345edb37
JJ
5248 /* In a statement-expression we want to return the last value.
5249 For empty statement expression return void_node. */
5250 tree r = void_node;
58611fb6
JM
5251 if (!jump_target)
5252 {
5253 local_target = NULL_TREE;
5254 jump_target = &local_target;
5255 }
56632b27
JM
5256 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5257 {
56632b27 5258 tree stmt = tsi_stmt (i);
0250ba58
MP
5259 /* We've found a continue, so skip everything until we reach
5260 the label its jumping to. */
5261 if (continues (jump_target))
5262 {
5263 if (label_matches (ctx, jump_target, stmt))
5264 /* Found it. */
5265 *jump_target = NULL_TREE;
5266 else
5267 continue;
5268 }
6ef72c36
JJ
5269 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
5270 continue;
58611fb6
JM
5271 r = cxx_eval_constant_expression (ctx, stmt, false,
5272 non_constant_p, overflow_p,
5273 jump_target);
56632b27
JM
5274 if (*non_constant_p)
5275 break;
5276 if (returns (jump_target) || breaks (jump_target))
5277 break;
5278 }
ce36ba09
JM
5279 if (*jump_target && jump_target == &local_target)
5280 {
5281 /* We aren't communicating the jump to our caller, so give up. We don't
5282 need to support evaluation of jumps out of statement-exprs. */
5283 if (!ctx->quiet)
f9d0ca40 5284 error_at (cp_expr_loc_or_input_loc (r),
ce36ba09
JM
5285 "statement is not a constant expression");
5286 *non_constant_p = true;
5287 }
58611fb6 5288 return r;
56632b27
JM
5289}
5290
5291/* Evaluate a LOOP_EXPR for side-effects. Handles break and return
5292 semantics; continue semantics are covered by cxx_eval_statement_list. */
5293
5294static tree
5295cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
5296 bool *non_constant_p, bool *overflow_p,
5297 tree *jump_target)
5298{
39dce2b7 5299 constexpr_ctx new_ctx = *ctx;
8e007055
JJ
5300 tree local_target;
5301 if (!jump_target)
5302 {
5303 local_target = NULL_TREE;
5304 jump_target = &local_target;
5305 }
39dce2b7 5306
43574e4f 5307 tree body, cond = NULL_TREE, expr = NULL_TREE;
5ec2cd9f 5308 int count = 0;
43574e4f
JJ
5309 switch (TREE_CODE (t))
5310 {
5311 case LOOP_EXPR:
5312 body = LOOP_EXPR_BODY (t);
5313 break;
5314 case DO_STMT:
5315 body = DO_BODY (t);
5316 cond = DO_COND (t);
5317 break;
5318 case WHILE_STMT:
5319 body = WHILE_BODY (t);
5320 cond = WHILE_COND (t);
5321 count = -1;
5322 break;
5323 case FOR_STMT:
5324 if (FOR_INIT_STMT (t))
5325 cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), /*lval*/false,
5326 non_constant_p, overflow_p, jump_target);
5327 if (*non_constant_p)
5328 return NULL_TREE;
5329 body = FOR_BODY (t);
5330 cond = FOR_COND (t);
5331 expr = FOR_EXPR (t);
5332 count = -1;
5333 break;
5334 default:
5335 gcc_unreachable ();
5336 }
0ee28590 5337 auto_vec<tree, 10> save_exprs;
43574e4f 5338 new_ctx.save_exprs = &save_exprs;
d259b234 5339 do
56632b27 5340 {
43574e4f
JJ
5341 if (count != -1)
5342 {
5343 if (body)
5344 cxx_eval_constant_expression (&new_ctx, body, /*lval*/false,
5345 non_constant_p, overflow_p,
5346 jump_target);
5347 if (breaks (jump_target))
5348 {
5349 *jump_target = NULL_TREE;
5350 break;
5351 }
39dce2b7 5352
43574e4f
JJ
5353 if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
5354 *jump_target = NULL_TREE;
5355
5356 if (expr)
5357 cxx_eval_constant_expression (&new_ctx, expr, /*lval*/false,
5358 non_constant_p, overflow_p,
5359 jump_target);
5360 }
5361
5362 if (cond)
5363 {
5364 tree res
5365 = cxx_eval_constant_expression (&new_ctx, cond, /*lval*/false,
5366 non_constant_p, overflow_p,
5367 jump_target);
5368 if (res)
5369 {
5370 if (verify_constant (res, ctx->quiet, non_constant_p,
5371 overflow_p))
5372 break;
5373 if (integer_zerop (res))
5374 break;
5375 }
5376 else
5377 gcc_assert (*jump_target);
5378 }
39dce2b7 5379
ee1de08d 5380 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
0ee28590
JJ
5381 unsigned int i;
5382 tree save_expr;
5383 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
8e007055 5384 ctx->global->values.remove (save_expr);
0ee28590 5385 save_exprs.truncate (0);
43574e4f 5386
5ec2cd9f
JM
5387 if (++count >= constexpr_loop_limit)
5388 {
5389 if (!ctx->quiet)
f9d0ca40 5390 error_at (cp_expr_loc_or_input_loc (t),
84fa214d 5391 "%<constexpr%> loop iteration count exceeds limit of %d "
a3f9f006 5392 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5ec2cd9f
JM
5393 constexpr_loop_limit);
5394 *non_constant_p = true;
5395 break;
5396 }
56632b27 5397 }
4b390698
JJ
5398 while (!returns (jump_target)
5399 && !breaks (jump_target)
43574e4f
JJ
5400 && !continues (jump_target)
5401 && (!switches (jump_target) || count == 0)
4b390698 5402 && !*non_constant_p);
d259b234 5403
ee1de08d 5404 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
0ee28590
JJ
5405 unsigned int i;
5406 tree save_expr;
5407 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
8e007055 5408 ctx->global->values.remove (save_expr);
39dce2b7 5409
56632b27
JM
5410 return NULL_TREE;
5411}
5412
5413/* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5414 semantics. */
5415
5416static tree
5417cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
56632b27
JM
5418 bool *non_constant_p, bool *overflow_p,
5419 tree *jump_target)
5420{
43574e4f
JJ
5421 tree cond
5422 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
2b3ab879 5423 cond = cxx_eval_constant_expression (ctx, cond, false,
5a804683 5424 non_constant_p, overflow_p);
56632b27
JM
5425 VERIFY_CONSTANT (cond);
5426 *jump_target = cond;
5427
43574e4f
JJ
5428 tree body
5429 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
4b390698
JJ
5430 constexpr_ctx new_ctx = *ctx;
5431 constexpr_switch_state css = css_default_not_seen;
5432 new_ctx.css_state = &css;
5433 cxx_eval_constant_expression (&new_ctx, body, false,
5434 non_constant_p, overflow_p, jump_target);
5435 if (switches (jump_target) && css == css_default_seen)
5436 {
5437 /* If the SWITCH_EXPR body has default: label, process it once again,
5438 this time instructing label_matches to return true for default:
5439 label on switches (jump_target). */
5440 css = css_default_processing;
5441 cxx_eval_constant_expression (&new_ctx, body, false,
5442 non_constant_p, overflow_p, jump_target);
5443 }
56632b27
JM
5444 if (breaks (jump_target) || switches (jump_target))
5445 *jump_target = NULL_TREE;
5446 return NULL_TREE;
5447}
5448
89262ec6
JM
5449/* Find the object of TYPE under initialization in CTX. */
5450
5451static tree
5452lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
5453{
fbd603c4 5454 if (!ctx)
89262ec6
JM
5455 return NULL_TREE;
5456
49a86fce
PP
5457 /* Prefer the outermost matching object, but don't cross
5458 CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
5459 if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
5460 if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
5461 return outer_ob;
5462
89262ec6
JM
5463 /* We could use ctx->object unconditionally, but using ctx->ctor when we
5464 can is a minor optimization. */
fbd603c4 5465 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
89262ec6
JM
5466 return ctx->ctor;
5467
fbd603c4
JM
5468 if (!ctx->object)
5469 return NULL_TREE;
5470
89262ec6
JM
5471 /* Since an object cannot have a field of its own type, we can search outward
5472 from ctx->object to find the unique containing object of TYPE. */
5473 tree ob = ctx->object;
5474 while (ob)
5475 {
5476 if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
5477 break;
5478 if (handled_component_p (ob))
5479 ob = TREE_OPERAND (ob, 0);
5480 else
5481 ob = NULL_TREE;
5482 }
5483
5484 return ob;
5485}
5486
7c814975
MP
5487/* Complain about an attempt to evaluate inline assembly. */
5488
5489static void
5490inline_asm_in_constexpr_error (location_t loc)
5491{
5492 auto_diagnostic_group d;
5493 error_at (loc, "inline assembly is not a constant expression");
5494 inform (loc, "only unevaluated inline assembly is allowed in a "
b04445d4 5495 "%<constexpr%> function in C++20");
7c814975
MP
5496}
5497
2d76680f
PC
5498/* Attempt to reduce the expression T to a constant value.
5499 On failure, issue diagnostic and return error_mark_node. */
5500/* FIXME unify with c_fully_fold */
60813a46 5501/* FIXME overflow_p is too global */
2d76680f
PC
5502
5503static tree
3e605b20 5504cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
92a596e8 5505 bool lval,
56632b27 5506 bool *non_constant_p, bool *overflow_p,
3075affd 5507 tree *jump_target /* = NULL */)
2d76680f 5508{
4b390698
JJ
5509 if (jump_target && *jump_target)
5510 {
5511 /* If we are jumping, ignore all statements/expressions except those
5512 that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
5513 switch (TREE_CODE (t))
5514 {
5515 case BIND_EXPR:
5516 case STATEMENT_LIST:
5517 case LOOP_EXPR:
5518 case COND_EXPR:
43574e4f
JJ
5519 case IF_STMT:
5520 case DO_STMT:
5521 case WHILE_STMT:
5522 case FOR_STMT:
4b390698
JJ
5523 break;
5524 case LABEL_EXPR:
5525 case CASE_LABEL_EXPR:
5526 if (label_matches (ctx, jump_target, t))
5527 /* Found it. */
5528 *jump_target = NULL_TREE;
5529 return NULL_TREE;
5530 default:
5531 return NULL_TREE;
5532 }
5533 }
3075affd 5534 if (error_operand_p (t))
2d76680f
PC
5535 {
5536 *non_constant_p = true;
5537 return t;
5538 }
7a649ef5 5539
6821245b
JM
5540 location_t loc = cp_expr_loc_or_input_loc (t);
5541
7a649ef5
JM
5542 STRIP_ANY_LOCATION_WRAPPER (t);
5543
2d76680f
PC
5544 if (CONSTANT_CLASS_P (t))
5545 {
61637db3
JJ
5546 if (TREE_OVERFLOW (t))
5547 {
5548 if (!ctx->quiet)
5549 permerror (input_location, "overflow in constant expression");
5550 if (!flag_permissive || ctx->quiet)
5551 *overflow_p = true;
5552 }
8bada5cd
MS
5553
5554 if (TREE_CODE (t) == INTEGER_CST
9f613f06 5555 && TYPE_PTR_P (TREE_TYPE (t))
8bada5cd
MS
5556 && !integer_zerop (t))
5557 {
5558 if (!ctx->quiet)
5559 error ("value %qE of type %qT is not a constant expression",
5560 t, TREE_TYPE (t));
5561 *non_constant_p = true;
5562 }
5563
2d76680f
PC
5564 return t;
5565 }
2d76680f 5566
a15ffa22 5567 /* Avoid excessively long constexpr evaluations. */
8e007055 5568 if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
a15ffa22
JJ
5569 {
5570 if (!ctx->quiet)
6821245b 5571 error_at (loc,
a15ffa22 5572 "%<constexpr%> evaluation operation count exceeds limit of "
a9c697b8 5573 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
a15ffa22 5574 constexpr_ops_limit);
8e007055 5575 ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
a15ffa22
JJ
5576 *non_constant_p = true;
5577 return t;
5578 }
5579
7a649ef5
JM
5580 constexpr_ctx new_ctx;
5581 tree r = t;
5582
8bada5cd
MS
5583 tree_code tcode = TREE_CODE (t);
5584 switch (tcode)
2d76680f 5585 {
60813a46 5586 case RESULT_DECL:
92a596e8 5587 if (lval)
60813a46
JM
5588 return t;
5589 /* We ask for an rvalue for the RESULT_DECL when indirecting
bf66b9b4
AH
5590 through an invisible reference, or in named return value
5591 optimization. */
8e007055 5592 if (tree *p = ctx->global->values.get (t))
1efb1dc2
MP
5593 return *p;
5594 else
5595 {
5596 if (!ctx->quiet)
5597 error ("%qE is not a constant expression", t);
5598 *non_constant_p = true;
5599 }
5600 break;
60813a46 5601
2d76680f 5602 case VAR_DECL:
70f40fea 5603 if (DECL_HAS_VALUE_EXPR_P (t))
c59fa7ea
JM
5604 {
5605 if (is_normal_capture_proxy (t)
5606 && current_function_decl == DECL_CONTEXT (t))
5607 {
5608 /* Function parms aren't constexpr within the function
5609 definition, so don't try to look at the closure. But if the
5610 captured variable is constant, try to evaluate it directly. */
5611 r = DECL_CAPTURED_VARIABLE (t);
5612 tree type = TREE_TYPE (t);
5613 if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
5614 {
5615 /* Adjust r to match the reference-ness of t. */
5616 if (TYPE_REF_P (type))
5617 r = build_address (r);
5618 else
5619 r = convert_from_reference (r);
5620 }
5621 }
5622 else
5623 r = DECL_VALUE_EXPR (t);
5624 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5625 overflow_p);
5626 }
191816a3 5627 /* fall through */
7c83622c
JM
5628 case CONST_DECL:
5629 /* We used to not check lval for CONST_DECL, but darwin.c uses
5630 CONST_DECL for aggregate constants. */
92a596e8 5631 if (lval)
2d76680f 5632 return t;
ef529765
JM
5633 else if (t == ctx->object)
5634 return ctx->ctor;
5635 if (VAR_P (t))
5636 if (tree *p = ctx->global->values.get (t))
5637 if (*p != NULL_TREE)
5638 {
5639 r = *p;
5640 break;
5641 }
f4fce183 5642 if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 5643 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7dc2b4a2
JM
5644 {
5645 /* If the class is empty, we aren't actually loading anything. */
5646 r = build_constructor (TREE_TYPE (t), NULL);
5647 TREE_CONSTANT (r) = true;
5648 }
5649 else if (ctx->strict)
69eb4fde
JM
5650 r = decl_really_constant_value (t);
5651 else
5652 r = decl_constant_value (t);
2d76680f
PC
5653 if (TREE_CODE (r) == TARGET_EXPR
5654 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
5655 r = TARGET_EXPR_INITIAL (r);
2d76680f
PC
5656 if (DECL_P (r))
5657 {
2b3ab879 5658 if (!ctx->quiet)
6821245b 5659 non_const_var_error (loc, r);
2d76680f
PC
5660 *non_constant_p = true;
5661 }
5662 break;
5663
96a95ac1
AO
5664 case DEBUG_BEGIN_STMT:
5665 /* ??? It might be nice to retain this information somehow, so
5666 as to be able to step into a constexpr function call. */
5667 /* Fall through. */
5668
2d76680f
PC
5669 case FUNCTION_DECL:
5670 case TEMPLATE_DECL:
5671 case LABEL_DECL:
56632b27
JM
5672 case LABEL_EXPR:
5673 case CASE_LABEL_EXPR:
4b390698 5674 case PREDICT_EXPR:
2d76680f
PC
5675 return t;
5676
5677 case PARM_DECL:
9f613f06 5678 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
60813a46 5679 /* glvalue use. */;
8e007055 5680 else if (tree *p = ctx->global->values.get (r))
60813a46 5681 r = *p;
92a596e8 5682 else if (lval)
2d76680f 5683 /* Defer in case this is only used for its type. */;
1c2f613f 5684 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 5685 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
052beba4
JM
5686 {
5687 /* If the class is empty, we aren't actually loading anything. */
5688 r = build_constructor (TREE_TYPE (t), NULL);
5689 TREE_CONSTANT (r) = true;
5690 }
2d76680f
PC
5691 else
5692 {
2b3ab879 5693 if (!ctx->quiet)
2d76680f
PC
5694 error ("%qE is not a constant expression", t);
5695 *non_constant_p = true;
5696 }
5697 break;
5698
5699 case CALL_EXPR:
5700 case AGGR_INIT_EXPR:
92a596e8 5701 r = cxx_eval_call_expression (ctx, t, lval,
2d76680f
PC
5702 non_constant_p, overflow_p);
5703 break;
5704
60813a46
JM
5705 case DECL_EXPR:
5706 {
5707 r = DECL_EXPR_DECL (t);
43574e4f
JJ
5708 if (TREE_CODE (r) == USING_DECL)
5709 {
5710 r = void_node;
5711 break;
5712 }
60813a46
JM
5713 if (AGGREGATE_TYPE_P (TREE_TYPE (r))
5714 || VECTOR_TYPE_P (TREE_TYPE (r)))
5715 {
5716 new_ctx = *ctx;
5717 new_ctx.object = r;
5718 new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
e8c48716 5719 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
8e007055 5720 ctx->global->values.put (r, new_ctx.ctor);
60813a46
JM
5721 ctx = &new_ctx;
5722 }
5723
5724 if (tree init = DECL_INITIAL (r))
5725 {
5726 init = cxx_eval_constant_expression (ctx, init,
2b3ab879 5727 false,
5a804683 5728 non_constant_p, overflow_p);
7f0e23e9 5729 /* Don't share a CONSTRUCTOR that might be changed. */
0146e25f 5730 init = unshare_constructor (init);
04e1749c
MP
5731 /* Remember that a constant object's constructor has already
5732 run. */
5733 if (CLASS_TYPE_P (TREE_TYPE (r))
5734 && CP_TYPE_CONST_P (TREE_TYPE (r)))
5735 TREE_READONLY (init) = true;
8e007055 5736 ctx->global->values.put (r, init);
60813a46
JM
5737 }
5738 else if (ctx == &new_ctx)
5739 /* We gave it a CONSTRUCTOR above. */;
5740 else
8e007055 5741 ctx->global->values.put (r, NULL_TREE);
60813a46
JM
5742 }
5743 break;
5744
2d76680f 5745 case TARGET_EXPR:
595f1b12
JM
5746 {
5747 tree type = TREE_TYPE (t);
5748
5749 if (!literal_type_p (type))
5750 {
5751 if (!ctx->quiet)
5752 {
5753 auto_diagnostic_group d;
5754 error ("temporary of non-literal type %qT in a "
5755 "constant expression", type);
5756 explain_non_literal_class (type);
5757 }
5758 *non_constant_p = true;
5759 break;
5760 }
5761 gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
5762 /* Avoid evaluating a TARGET_EXPR more than once. */
5763 tree slot = TARGET_EXPR_SLOT (t);
5764 if (tree *p = ctx->global->values.get (slot))
5765 {
5766 if (lval)
5767 return slot;
5768 r = *p;
5769 break;
5770 }
595f1b12
JM
5771 if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
5772 {
5773 /* We're being expanded without an explicit target, so start
5774 initializing a new object; expansion with an explicit target
5775 strips the TARGET_EXPR before we get here. */
5776 new_ctx = *ctx;
49a86fce
PP
5777 /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
5778 any PLACEHOLDER_EXPR within the initializer that refers to the
5779 former object under construction. */
5780 new_ctx.parent = ctx;
595f1b12
JM
5781 new_ctx.ctor = build_constructor (type, NULL);
5782 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
5783 new_ctx.object = slot;
5784 ctx->global->values.put (new_ctx.object, new_ctx.ctor);
5785 ctx = &new_ctx;
5786 }
5787 /* Pass false for 'lval' because this indicates
5788 initialization of a temporary. */
5789 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5790 false,
5791 non_constant_p, overflow_p);
5792 if (*non_constant_p)
ee1de08d 5793 break;
595f1b12
JM
5794 /* Adjust the type of the result to the type of the temporary. */
5795 r = adjust_temp_type (type, r);
5796 if (TARGET_EXPR_CLEANUP (t) && !CLEANUP_EH_ONLY (t))
5797 ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
5798 r = unshare_constructor (r);
5799 ctx->global->values.put (slot, r);
5800 if (ctx->save_exprs)
5801 ctx->save_exprs->safe_push (slot);
5802 if (lval)
5803 return slot;
5804 }
2d76680f
PC
5805 break;
5806
60813a46 5807 case INIT_EXPR:
60813a46 5808 case MODIFY_EXPR:
4b390698 5809 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
92a596e8 5810 r = cxx_eval_store_expression (ctx, t, lval,
60813a46
JM
5811 non_constant_p, overflow_p);
5812 break;
5813
2d76680f 5814 case SCOPE_REF:
3e605b20 5815 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
92a596e8 5816 lval,
5a804683 5817 non_constant_p, overflow_p);
2d76680f
PC
5818 break;
5819
5820 case RETURN_EXPR:
75e0295b
MP
5821 if (TREE_OPERAND (t, 0) != NULL_TREE)
5822 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5823 lval,
5824 non_constant_p, overflow_p);
43574e4f
JJ
5825 /* FALLTHRU */
5826 case BREAK_STMT:
5827 case CONTINUE_STMT:
019e0ae8
MP
5828 if (jump_target)
5829 *jump_target = t;
5830 else
5831 {
5832 /* Can happen with ({ return true; }) && false; passed to
5833 maybe_constant_value. There is nothing to jump over in this
5834 case, and the bug will be diagnosed later. */
5835 gcc_assert (ctx->quiet);
5836 *non_constant_p = true;
5837 }
56632b27
JM
5838 break;
5839
cabaf50a
JM
5840 case SAVE_EXPR:
5841 /* Avoid evaluating a SAVE_EXPR more than once. */
8e007055 5842 if (tree *p = ctx->global->values.get (t))
cabaf50a
JM
5843 r = *p;
5844 else
5845 {
12d9ce19 5846 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
cabaf50a 5847 non_constant_p, overflow_p);
0e0ffbfc
JJ
5848 if (*non_constant_p)
5849 break;
8e007055 5850 ctx->global->values.put (t, r);
39dce2b7 5851 if (ctx->save_exprs)
0ee28590 5852 ctx->save_exprs->safe_push (t);
cabaf50a
JM
5853 }
5854 break;
5855
2d76680f 5856 case TRY_CATCH_EXPR:
ee1de08d
JJ
5857 if (TREE_OPERAND (t, 0) == NULL_TREE)
5858 {
5859 r = void_node;
5860 break;
5861 }
5862 /* FALLTHRU */
5863 case NON_LVALUE_EXPR:
769430b2 5864 case TRY_BLOCK:
2d76680f 5865 case MUST_NOT_THROW_EXPR:
60813a46
JM
5866 case EXPR_STMT:
5867 case EH_SPEC_BLOCK:
3e605b20 5868 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
92a596e8 5869 lval,
56632b27
JM
5870 non_constant_p, overflow_p,
5871 jump_target);
2d76680f
PC
5872 break;
5873
ee1de08d
JJ
5874 case CLEANUP_POINT_EXPR:
5875 {
5876 auto_vec<tree, 2> cleanups;
5877 vec<tree> *prev_cleanups = ctx->global->cleanups;
5878 ctx->global->cleanups = &cleanups;
5879 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5880 lval,
5881 non_constant_p, overflow_p,
5882 jump_target);
5883 ctx->global->cleanups = prev_cleanups;
5884 unsigned int i;
5885 tree cleanup;
5886 /* Evaluate the cleanups. */
5887 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
5888 cxx_eval_constant_expression (ctx, cleanup, false,
5889 non_constant_p, overflow_p,
5890 jump_target);
5891 }
5892 break;
5893
769430b2
JM
5894 case TRY_FINALLY_EXPR:
5895 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
5896 non_constant_p, overflow_p,
5897 jump_target);
5898 if (!*non_constant_p)
5899 /* Also evaluate the cleanup. */
5900 cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), true,
5901 non_constant_p, overflow_p,
5902 jump_target);
5903 break;
5904
43574e4f 5905 case CLEANUP_STMT:
1006c9d4
JJ
5906 {
5907 tree initial_jump_target = jump_target ? *jump_target : NULL_TREE;
5908 r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
5909 non_constant_p, overflow_p,
5910 jump_target);
5911 if (!CLEANUP_EH_ONLY (t) && !*non_constant_p)
acfdb960 5912 {
6821245b 5913 iloc_sentinel ils (loc);
acfdb960
JJ
5914 /* Also evaluate the cleanup. If we weren't skipping at the
5915 start of the CLEANUP_BODY, change jump_target temporarily
5916 to &initial_jump_target, so that even a return or break or
5917 continue in the body doesn't skip the cleanup. */
5918 cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), true,
5919 non_constant_p, overflow_p,
5920 jump_target ? &initial_jump_target
5921 : NULL);
acfdb960 5922 }
1006c9d4 5923 }
43574e4f
JJ
5924 break;
5925
2d76680f
PC
5926 /* These differ from cxx_eval_unary_expression in that this doesn't
5927 check for a constant operand or result; an address can be
5928 constant without its operand being, and vice versa. */
cda0a029 5929 case MEM_REF:
2d76680f 5930 case INDIRECT_REF:
92a596e8 5931 r = cxx_eval_indirect_ref (ctx, t, lval,
2d76680f
PC
5932 non_constant_p, overflow_p);
5933 break;
5934
5935 case ADDR_EXPR:
5936 {
5937 tree oldop = TREE_OPERAND (t, 0);
3e605b20 5938 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 5939 /*lval*/true,
5a804683 5940 non_constant_p, overflow_p);
2d76680f
PC
5941 /* Don't VERIFY_CONSTANT here. */
5942 if (*non_constant_p)
5943 return t;
f2acb8ad 5944 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
2d76680f
PC
5945 /* This function does more aggressive folding than fold itself. */
5946 r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
5947 if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
7a649ef5
JM
5948 {
5949 ggc_free (r);
5950 return t;
5951 }
2d76680f
PC
5952 break;
5953 }
5954
5955 case REALPART_EXPR:
5956 case IMAGPART_EXPR:
87713c6a
JJ
5957 if (lval)
5958 {
5959 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
5960 non_constant_p, overflow_p);
5961 if (r == error_mark_node)
5962 ;
5963 else if (r == TREE_OPERAND (t, 0))
5964 r = t;
5965 else
5966 r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
5967 break;
5968 }
5969 /* FALLTHRU */
2d76680f
PC
5970 case CONJ_EXPR:
5971 case FIX_TRUNC_EXPR:
5972 case FLOAT_EXPR:
5973 case NEGATE_EXPR:
5974 case ABS_EXPR:
e28fadbc 5975 case ABSU_EXPR:
2d76680f
PC
5976 case BIT_NOT_EXPR:
5977 case TRUTH_NOT_EXPR:
5978 case FIXED_CONVERT_EXPR:
92a596e8 5979 r = cxx_eval_unary_expression (ctx, t, lval,
2d76680f
PC
5980 non_constant_p, overflow_p);
5981 break;
5982
5983 case SIZEOF_EXPR:
99e764a2
MP
5984 r = fold_sizeof_expr (t);
5985 /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
5986 which could lead to an infinite recursion. */
5987 if (TREE_CODE (r) != SIZEOF_EXPR)
5988 r = cxx_eval_constant_expression (ctx, r, lval,
5989 non_constant_p, overflow_p,
5990 jump_target);
5991 else
5992 {
5993 *non_constant_p = true;
5994 gcc_assert (ctx->quiet);
5995 }
5996
2d76680f
PC
5997 break;
5998
5999 case COMPOUND_EXPR:
6000 {
6001 /* check_return_expr sometimes wraps a TARGET_EXPR in a
6002 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
6003 introduced by build_call_a. */
6004 tree op0 = TREE_OPERAND (t, 0);
6005 tree op1 = TREE_OPERAND (t, 1);
6006 STRIP_NOPS (op1);
6007 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
6008 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
2b3ab879 6009 r = cxx_eval_constant_expression (ctx, op0,
92a596e8 6010 lval, non_constant_p, overflow_p,
56632b27 6011 jump_target);
2d76680f
PC
6012 else
6013 {
6014 /* Check that the LHS is constant and then discard it. */
2b3ab879 6015 cxx_eval_constant_expression (ctx, op0,
12d9ce19 6016 true, non_constant_p, overflow_p,
56632b27 6017 jump_target);
06ec22b7
JM
6018 if (*non_constant_p)
6019 return t;
2d76680f 6020 op1 = TREE_OPERAND (t, 1);
2b3ab879 6021 r = cxx_eval_constant_expression (ctx, op1,
92a596e8 6022 lval, non_constant_p, overflow_p,
56632b27 6023 jump_target);
2d76680f
PC
6024 }
6025 }
6026 break;
6027
6028 case POINTER_PLUS_EXPR:
1af4ebf5 6029 case POINTER_DIFF_EXPR:
2d76680f
PC
6030 case PLUS_EXPR:
6031 case MINUS_EXPR:
6032 case MULT_EXPR:
6033 case TRUNC_DIV_EXPR:
6034 case CEIL_DIV_EXPR:
6035 case FLOOR_DIV_EXPR:
6036 case ROUND_DIV_EXPR:
6037 case TRUNC_MOD_EXPR:
6038 case CEIL_MOD_EXPR:
6039 case ROUND_MOD_EXPR:
6040 case RDIV_EXPR:
6041 case EXACT_DIV_EXPR:
6042 case MIN_EXPR:
6043 case MAX_EXPR:
6044 case LSHIFT_EXPR:
6045 case RSHIFT_EXPR:
81fa6d73
JJ
6046 case LROTATE_EXPR:
6047 case RROTATE_EXPR:
2d76680f
PC
6048 case BIT_IOR_EXPR:
6049 case BIT_XOR_EXPR:
6050 case BIT_AND_EXPR:
6051 case TRUTH_XOR_EXPR:
6052 case LT_EXPR:
6053 case LE_EXPR:
6054 case GT_EXPR:
6055 case GE_EXPR:
6056 case EQ_EXPR:
6057 case NE_EXPR:
b7689b96 6058 case SPACESHIP_EXPR:
2d76680f
PC
6059 case UNORDERED_EXPR:
6060 case ORDERED_EXPR:
6061 case UNLT_EXPR:
6062 case UNLE_EXPR:
6063 case UNGT_EXPR:
6064 case UNGE_EXPR:
6065 case UNEQ_EXPR:
6066 case LTGT_EXPR:
6067 case RANGE_EXPR:
6068 case COMPLEX_EXPR:
92a596e8 6069 r = cxx_eval_binary_expression (ctx, t, lval,
2d76680f
PC
6070 non_constant_p, overflow_p);
6071 break;
6072
6073 /* fold can introduce non-IF versions of these; still treat them as
6074 short-circuiting. */
6075 case TRUTH_AND_EXPR:
6076 case TRUTH_ANDIF_EXPR:
3e605b20 6077 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
2d76680f 6078 boolean_true_node,
92a596e8 6079 lval,
2d76680f
PC
6080 non_constant_p, overflow_p);
6081 break;
6082
6083 case TRUTH_OR_EXPR:
6084 case TRUTH_ORIF_EXPR:
3e605b20 6085 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
2d76680f 6086 boolean_false_node,
92a596e8 6087 lval,
2d76680f
PC
6088 non_constant_p, overflow_p);
6089 break;
6090
6091 case ARRAY_REF:
92a596e8 6092 r = cxx_eval_array_reference (ctx, t, lval,
2d76680f
PC
6093 non_constant_p, overflow_p);
6094 break;
6095
6096 case COMPONENT_REF:
6097 if (is_overloaded_fn (t))
6098 {
6099 /* We can only get here in checking mode via
6100 build_non_dependent_expr, because any expression that
6101 calls or takes the address of the function will have
6102 pulled a FUNCTION_DECL out of the COMPONENT_REF. */
2b3ab879 6103 gcc_checking_assert (ctx->quiet || errorcount);
2d76680f
PC
6104 *non_constant_p = true;
6105 return t;
6106 }
92a596e8 6107 r = cxx_eval_component_reference (ctx, t, lval,
2d76680f
PC
6108 non_constant_p, overflow_p);
6109 break;
6110
6111 case BIT_FIELD_REF:
92a596e8 6112 r = cxx_eval_bit_field_ref (ctx, t, lval,
2d76680f
PC
6113 non_constant_p, overflow_p);
6114 break;
6115
6116 case COND_EXPR:
43574e4f 6117 case IF_STMT:
4b390698
JJ
6118 if (jump_target && *jump_target)
6119 {
e9fa2f6d 6120 tree orig_jump = *jump_target;
43574e4f
JJ
6121 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
6122 ? TREE_OPERAND (t, 1) : void_node);
4b390698
JJ
6123 /* When jumping to a label, the label might be either in the
6124 then or else blocks, so process then block first in skipping
6125 mode first, and if we are still in the skipping mode at its end,
6126 process the else block too. */
43574e4f
JJ
6127 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
6128 overflow_p, jump_target);
e9fa2f6d
MP
6129 /* It's possible that we found the label in the then block. But
6130 it could have been followed by another jumping statement, e.g.
6131 say we're looking for case 1:
6132 if (cond)
6133 {
6134 // skipped statements
6135 case 1:; // clears up *jump_target
6136 return 1; // and sets it to a RETURN_EXPR
6137 }
6138 else { ... }
6139 in which case we need not go looking to the else block.
6140 (goto is not allowed in a constexpr function.) */
6141 if (*jump_target == orig_jump)
43574e4f
JJ
6142 {
6143 arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
6144 ? TREE_OPERAND (t, 2) : void_node);
6145 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
6146 overflow_p, jump_target);
6147 }
4b390698
JJ
6148 break;
6149 }
92a596e8 6150 r = cxx_eval_conditional_expression (ctx, t, lval,
56632b27
JM
6151 non_constant_p, overflow_p,
6152 jump_target);
2d76680f 6153 break;
f370e36d
JJ
6154 case VEC_COND_EXPR:
6155 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
6156 overflow_p);
6157 break;
2d76680f
PC
6158
6159 case CONSTRUCTOR:
35d87f01 6160 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
2d63bc39
JM
6161 {
6162 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6163 VECTOR_CST if applicable. */
ac9ec198 6164 verify_constructor_flags (t);
2d63bc39
JM
6165 if (TREE_CONSTANT (t))
6166 return fold (t);
6167 }
92a596e8 6168 r = cxx_eval_bare_aggregate (ctx, t, lval,
2d76680f
PC
6169 non_constant_p, overflow_p);
6170 break;
6171
6172 case VEC_INIT_EXPR:
6173 /* We can get this in a defaulted constructor for a class with a
6174 non-static data member of array type. Either the initializer will
6175 be NULL, meaning default-initialization, or it will be an lvalue
6176 or xvalue of the same type, meaning direct-initialization from the
6177 corresponding member. */
92a596e8 6178 r = cxx_eval_vec_init (ctx, t, lval,
2d76680f
PC
6179 non_constant_p, overflow_p);
6180 break;
6181
2d76680f 6182 case VEC_PERM_EXPR:
92a596e8 6183 r = cxx_eval_trinary_expression (ctx, t, lval,
2d76680f
PC
6184 non_constant_p, overflow_p);
6185 break;
6186
7d75ea04
JJ
6187 case NOP_EXPR:
6188 if (REINTERPRET_CAST_P (t))
6189 {
6190 if (!ctx->quiet)
6821245b 6191 error_at (loc,
a9c697b8 6192 "%<reinterpret_cast%> is not a constant expression");
7d75ea04
JJ
6193 *non_constant_p = true;
6194 return t;
6195 }
6196 /* FALLTHROUGH. */
2d76680f
PC
6197 case CONVERT_EXPR:
6198 case VIEW_CONVERT_EXPR:
cda0a029 6199 case UNARY_PLUS_EXPR:
2d76680f
PC
6200 {
6201 tree oldop = TREE_OPERAND (t, 0);
cda0a029 6202
3e605b20 6203 tree op = cxx_eval_constant_expression (ctx, oldop,
92a596e8 6204 lval,
5a804683 6205 non_constant_p, overflow_p);
2d76680f
PC
6206 if (*non_constant_p)
6207 return t;
dcdbc004 6208 tree type = TREE_TYPE (t);
02a8575c
JM
6209
6210 if (VOID_TYPE_P (type))
6211 return void_node;
6212
eeb54a14
JJ
6213 if (TREE_CODE (t) == CONVERT_EXPR
6214 && ARITHMETIC_TYPE_P (type)
6215 && INDIRECT_TYPE_P (TREE_TYPE (op)))
6216 {
6217 if (!ctx->quiet)
6218 error_at (loc,
6219 "conversion from pointer type %qT to arithmetic type "
6220 "%qT in a constant expression", TREE_TYPE (op), type);
6221 *non_constant_p = true;
6222 return t;
6223 }
6224
8e007055 6225 if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
dcdbc004 6226 op = cplus_expand_constant (op);
7d75ea04 6227
05bf54c3
MP
6228 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
6229 {
7d75ea04
JJ
6230 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
6231 && !can_convert_qual (type, op))
6232 op = cplus_expand_constant (op);
6233 return cp_fold_convert (type, op);
05bf54c3 6234 }
8bada5cd 6235
71a93b08 6236 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
2d76680f 6237 {
8bada5cd
MS
6238 if (integer_zerop (op))
6239 {
9f613f06 6240 if (TYPE_REF_P (type))
8bada5cd
MS
6241 {
6242 if (!ctx->quiet)
6821245b 6243 error_at (loc,
8bada5cd
MS
6244 "dereferencing a null pointer");
6245 *non_constant_p = true;
6246 return t;
6247 }
9f613f06 6248 else if (TYPE_PTR_P (TREE_TYPE (op)))
8bada5cd
MS
6249 {
6250 tree from = TREE_TYPE (op);
6251
6252 if (!can_convert (type, from, tf_none))
6253 {
6254 if (!ctx->quiet)
6821245b 6255 error_at (loc,
8bada5cd
MS
6256 "conversion of %qT null pointer to %qT "
6257 "is not a constant expression",
6258 from, type);
6259 *non_constant_p = true;
6260 return t;
6261 }
6262 }
6263 }
6264 else
6265 {
6266 /* This detects for example:
6267 reinterpret_cast<void*>(sizeof 0)
6268 */
6269 if (!ctx->quiet)
6821245b 6270 error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
64d6d399 6271 "a constant expression",
8bada5cd
MS
6272 type, op);
6273 *non_constant_p = true;
6274 return t;
6275 }
2d76680f 6276 }
7d75ea04 6277
8e007055
JJ
6278 if (INDIRECT_TYPE_P (type)
6279 && TREE_CODE (op) == NOP_EXPR
6280 && TREE_TYPE (op) == ptr_type_node
6281 && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
6282 && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
6283 && DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
6284 0)) == heap_uninit_identifier)
6285 {
6286 tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
6287 tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
6288 tree elt_type = TREE_TYPE (type);
6289 tree cookie_size = NULL_TREE;
6290 if (TREE_CODE (elt_type) == RECORD_TYPE
6291 && TYPE_NAME (elt_type) == heap_identifier)
6292 {
6293 tree fld1 = TYPE_FIELDS (elt_type);
6294 tree fld2 = DECL_CHAIN (fld1);
6295 elt_type = TREE_TYPE (TREE_TYPE (fld2));
6296 cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
6297 }
6298 DECL_NAME (var) = heap_identifier;
6299 TREE_TYPE (var)
6300 = build_new_constexpr_heap_type (elt_type, cookie_size,
6301 var_size);
6302 TREE_TYPE (TREE_OPERAND (op, 0))
6303 = build_pointer_type (TREE_TYPE (var));
6304 }
6305
cda0a029 6306 if (op == oldop && tcode != UNARY_PLUS_EXPR)
2d76680f
PC
6307 /* We didn't fold at the top so we could check for ptr-int
6308 conversion. */
6309 return fold (t);
7d75ea04 6310
02a8575c
JM
6311 tree sop;
6312
e4511ca2
JM
6313 /* Handle an array's bounds having been deduced after we built
6314 the wrapping expression. */
6315 if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
6316 r = op;
02a8575c
JM
6317 else if (sop = tree_strip_nop_conversions (op),
6318 sop != op && (same_type_ignoring_tlq_and_bounds_p
6319 (type, TREE_TYPE (sop))))
6320 r = sop;
e4511ca2 6321 else if (tcode == UNARY_PLUS_EXPR)
cda0a029
JM
6322 r = fold_convert (TREE_TYPE (t), op);
6323 else
6324 r = fold_build1 (tcode, type, op);
7d75ea04 6325
2d76680f
PC
6326 /* Conversion of an out-of-range value has implementation-defined
6327 behavior; the language considers it different from arithmetic
6328 overflow, which is undefined. */
6329 if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
6330 TREE_OVERFLOW (r) = false;
6331 }
6332 break;
6333
6334 case EMPTY_CLASS_EXPR:
6335 /* This is good enough for a function argument that might not get
6336 used, and they can't do anything with it, so just return it. */
6337 return t;
6338
60813a46 6339 case STATEMENT_LIST:
56632b27
JM
6340 new_ctx = *ctx;
6341 new_ctx.ctor = new_ctx.object = NULL_TREE;
2b3ab879 6342 return cxx_eval_statement_list (&new_ctx, t,
56632b27 6343 non_constant_p, overflow_p, jump_target);
60813a46
JM
6344
6345 case BIND_EXPR:
6346 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
92a596e8 6347 lval,
56632b27
JM
6348 non_constant_p, overflow_p,
6349 jump_target);
60813a46 6350
2d76680f
PC
6351 case PREINCREMENT_EXPR:
6352 case POSTINCREMENT_EXPR:
6353 case PREDECREMENT_EXPR:
6354 case POSTDECREMENT_EXPR:
2b3ab879 6355 return cxx_eval_increment_expression (ctx, t,
92a596e8 6356 lval, non_constant_p, overflow_p);
60813a46
JM
6357
6358 case LAMBDA_EXPR:
2d76680f
PC
6359 case NEW_EXPR:
6360 case VEC_NEW_EXPR:
6361 case DELETE_EXPR:
6362 case VEC_DELETE_EXPR:
6363 case THROW_EXPR:
2d76680f
PC
6364 case MODOP_EXPR:
6365 /* GCC internal stuff. */
6366 case VA_ARG_EXPR:
2d76680f
PC
6367 case NON_DEPENDENT_EXPR:
6368 case BASELINK:
2d76680f 6369 case OFFSET_REF:
2b3ab879 6370 if (!ctx->quiet)
6821245b 6371 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
6372 *non_constant_p = true;
6373 break;
6374
bf8d8309 6375 case OBJ_TYPE_REF:
7ece3bd8
JM
6376 /* Virtual function lookup. We don't need to do anything fancy. */
6377 return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
6378 lval, non_constant_p, overflow_p);
bf8d8309 6379
3e605b20 6380 case PLACEHOLDER_EXPR:
89262ec6
JM
6381 /* Use of the value or address of the current object. */
6382 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
b599bf9d
PP
6383 {
6384 if (TREE_CODE (ctor) == CONSTRUCTOR)
6385 return ctor;
6386 else
6387 return cxx_eval_constant_expression (ctx, ctor, lval,
6388 non_constant_p, overflow_p);
6389 }
89262ec6
JM
6390 /* A placeholder without a referent. We can get here when
6391 checking whether NSDMIs are noexcept, or in massage_init_elt;
6392 just say it's non-constant for now. */
6393 gcc_assert (ctx->quiet);
6394 *non_constant_p = true;
3e605b20
JM
6395 break;
6396
06ec22b7
JM
6397 case EXIT_EXPR:
6398 {
6399 tree cond = TREE_OPERAND (t, 0);
6400 cond = cxx_eval_constant_expression (ctx, cond, /*lval*/false,
6401 non_constant_p, overflow_p);
6402 VERIFY_CONSTANT (cond);
6403 if (integer_nonzerop (cond))
6404 *jump_target = t;
6405 }
6406 break;
6407
60813a46 6408 case GOTO_EXPR:
56632b27 6409 *jump_target = TREE_OPERAND (t, 0);
d49318d9
PC
6410 gcc_assert (breaks (jump_target) || continues (jump_target)
6411 /* Allow for jumping to a cdtor_label. */
6412 || returns (jump_target));
56632b27
JM
6413 break;
6414
60813a46 6415 case LOOP_EXPR:
43574e4f
JJ
6416 case DO_STMT:
6417 case WHILE_STMT:
6418 case FOR_STMT:
2b3ab879 6419 cxx_eval_loop_expr (ctx, t,
56632b27
JM
6420 non_constant_p, overflow_p, jump_target);
6421 break;
6422
60813a46 6423 case SWITCH_EXPR:
43574e4f 6424 case SWITCH_STMT:
2b3ab879 6425 cxx_eval_switch_expr (ctx, t,
56632b27 6426 non_constant_p, overflow_p, jump_target);
60813a46
JM
6427 break;
6428
971e17ff
AS
6429 case REQUIRES_EXPR:
6430 /* It's possible to get a requires-expression in a constant
6431 expression. For example:
6432
6433 template<typename T> concept bool C() {
6434 return requires (T t) { t; };
6435 }
6436
6437 template<typename T> requires !C<T>() void f(T);
6438
6439 Normalization leaves f with the associated constraint
6440 '!requires (T t) { ... }' which is not transformed into
6441 a constraint. */
6442 if (!processing_template_decl)
cb57504a 6443 return satisfy_constraint_expression (t);
971e17ff
AS
6444 else
6445 *non_constant_p = true;
6446 return t;
6447
98e09245 6448 case ANNOTATE_EXPR:
98e09245
JJ
6449 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6450 lval,
6451 non_constant_p, overflow_p,
6452 jump_target);
6453 break;
6454
3664e317
JM
6455 case USING_STMT:
6456 r = void_node;
6457 break;
6458
cb57504a
JM
6459 case TEMPLATE_ID_EXPR:
6460 {
6461 /* We can evaluate template-id that refers to a concept only if
6462 the template arguments are non-dependent. */
861d4af8
AS
6463 tree id = unpack_concept_check (t);
6464 tree tmpl = TREE_OPERAND (id, 0);
6465 if (!concept_definition_p (tmpl))
cb57504a
JM
6466 internal_error ("unexpected template-id %qE", t);
6467
861d4af8
AS
6468 if (function_concept_p (tmpl))
6469 {
6470 if (!ctx->quiet)
6471 error_at (cp_expr_loc_or_input_loc (t),
6472 "function concept must be called");
6473 r = error_mark_node;
6474 break;
6475 }
6476
6d1556ec
PP
6477 if (!processing_template_decl
6478 && !uid_sensitive_constexpr_evaluation_p ())
861d4af8 6479 r = evaluate_concept_check (t, tf_warning_or_error);
cb57504a
JM
6480 else
6481 *non_constant_p = true;
861d4af8
AS
6482
6483 break;
cb57504a
JM
6484 }
6485
529bc410
MP
6486 case ASM_EXPR:
6487 if (!ctx->quiet)
6821245b 6488 inline_asm_in_constexpr_error (loc);
529bc410
MP
6489 *non_constant_p = true;
6490 return t;
6491
2d76680f 6492 default:
c6e7c499
JM
6493 if (STATEMENT_CODE_P (TREE_CODE (t)))
6494 {
6495 /* This function doesn't know how to deal with pre-genericize
6496 statements; this can only happen with statement-expressions,
6497 so for now just fail. */
6498 if (!ctx->quiet)
6499 error_at (EXPR_LOCATION (t),
64d6d399 6500 "statement is not a constant expression");
c6e7c499
JM
6501 }
6502 else
6503 internal_error ("unexpected expression %qE of kind %s", t,
6504 get_tree_code_name (TREE_CODE (t)));
2d76680f
PC
6505 *non_constant_p = true;
6506 break;
6507 }
6508
6509 if (r == error_mark_node)
6510 *non_constant_p = true;
6511
6512 if (*non_constant_p)
6513 return t;
6514 else
6515 return r;
6516}
6517
e079dced
JM
6518/* P0859: A function is needed for constant evaluation if it is a constexpr
6519 function that is named by an expression ([basic.def.odr]) that is
6520 potentially constant evaluated.
6521
6522 So we need to instantiate any constexpr functions mentioned by the
6523 expression even if the definition isn't needed for evaluating the
6524 expression. */
6525
6526static tree
6527instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
6528{
6529 if (TREE_CODE (*tp) == FUNCTION_DECL
6530 && DECL_DECLARED_CONSTEXPR_P (*tp)
6531 && !DECL_INITIAL (*tp)
4fdda3ce 6532 && !trivial_fn_p (*tp)
f65a3299
PP
6533 && DECL_TEMPLOID_INSTANTIATION (*tp)
6534 && !uid_sensitive_constexpr_evaluation_p ())
e079dced
JM
6535 {
6536 ++function_depth;
6537 instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
6538 --function_depth;
6539 }
6540 else if (TREE_CODE (*tp) == CALL_EXPR
6541 || TREE_CODE (*tp) == AGGR_INIT_EXPR)
6542 {
6543 if (EXPR_HAS_LOCATION (*tp))
6544 input_location = EXPR_LOCATION (*tp);
6545 }
6546
6547 if (!EXPR_P (*tp))
6548 *walk_subtrees = 0;
6549
6550 return NULL_TREE;
6551}
8e007055 6552
e079dced
JM
6553static void
6554instantiate_constexpr_fns (tree t)
6555{
6556 location_t loc = input_location;
6557 cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
6558 input_location = loc;
6559}
6560
8e007055
JJ
6561/* Look for heap variables in the expression *TP. */
6562
6563static tree
6564find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
6565{
6566 if (VAR_P (*tp)
6567 && (DECL_NAME (*tp) == heap_uninit_identifier
6568 || DECL_NAME (*tp) == heap_identifier
6569 || DECL_NAME (*tp) == heap_deleted_identifier))
6570 return *tp;
6571
6572 if (TYPE_P (*tp))
6573 *walk_subtrees = 0;
6574 return NULL_TREE;
6575}
6576
f968ef9b
JJ
6577/* Find immediate function decls in *TP if any. */
6578
6579static tree
6580find_immediate_fndecl (tree *tp, int */*walk_subtrees*/, void */*data*/)
6581{
6582 if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
6583 return *tp;
6584 return NULL_TREE;
6585}
6586
e4082611 6587/* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
d4accef3
JM
6588 STRICT has the same sense as for constant_value_1: true if we only allow
6589 conforming C++ constant expressions, or false if we want a constant value
6590 even if it doesn't conform.
13de99bc 6591 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
8e007055
JJ
6592 per P0595 even when ALLOW_NON_CONSTANT is true.
6593 CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
6594 OBJECT must be non-NULL in that case. */
e4082611 6595
2d76680f 6596static tree
3e605b20 6597cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
e4082611 6598 bool strict = true,
13de99bc 6599 bool manifestly_const_eval = false,
8e007055 6600 bool constexpr_dtor = false,
f65a3299 6601 tree object = NULL_TREE)
2d76680f 6602{
8108ea30
JM
6603 auto_timevar time (TV_CONSTEXPR);
6604
2d76680f
PC
6605 bool non_constant_p = false;
6606 bool overflow_p = false;
39dce2b7 6607
d419e176
MP
6608 if (BRACE_ENCLOSED_INITIALIZER_P (t))
6609 {
6610 gcc_checking_assert (allow_non_constant);
6611 return t;
6612 }
6613
8e007055 6614 constexpr_global_ctx global_ctx;
49a86fce 6615 constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
8e007055 6616 allow_non_constant, strict,
f65a3299 6617 manifestly_const_eval || !allow_non_constant };
39dce2b7 6618
3e605b20 6619 tree type = initialized_type (t);
3e605b20 6620 tree r = t;
f968ef9b 6621 bool is_consteval = false;
4d0c18c6 6622 if (VOID_TYPE_P (type))
8e007055
JJ
6623 {
6624 if (constexpr_dtor)
6625 /* Used for destructors of array elements. */
6626 type = TREE_TYPE (object);
6627 else
f968ef9b 6628 {
b04445d4 6629 if (cxx_dialect < cxx20)
f968ef9b
JJ
6630 return t;
6631 if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
6632 return t;
6633 /* Calls to immediate functions returning void need to be
6634 evaluated. */
6635 tree fndecl = cp_get_callee_fndecl_nofold (t);
6636 if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
6637 return t;
6638 else
6639 is_consteval = true;
6640 }
6641 }
b04445d4 6642 else if (cxx_dialect >= cxx20
f968ef9b
JJ
6643 && (TREE_CODE (t) == CALL_EXPR
6644 || TREE_CODE (t) == AGGR_INIT_EXPR
6645 || TREE_CODE (t) == TARGET_EXPR))
6646 {
861d4af8
AS
6647 /* For non-concept checks, determine if it is consteval. */
6648 if (!concept_check_p (t))
6649 {
6650 tree x = t;
6651 if (TREE_CODE (x) == TARGET_EXPR)
6652 x = TARGET_EXPR_INITIAL (x);
6653 tree fndecl = cp_get_callee_fndecl_nofold (x);
6654 if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
6655 is_consteval = true;
6656 }
8e007055 6657 }
3e605b20
JM
6658 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
6659 {
6660 /* In C++14 an NSDMI can participate in aggregate initialization,
6661 and can refer to the address of the object being initialized, so
6662 we need to pass in the relevant VAR_DECL if we want to do the
6663 evaluation in a single pass. The evaluation will dynamically
6664 update ctx.values for the VAR_DECL. We use the same strategy
6665 for C++11 constexpr constructors that refer to the object being
6666 initialized. */
8e007055
JJ
6667 if (constexpr_dtor)
6668 {
6669 gcc_assert (object && VAR_P (object));
6670 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
6671 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
fce6467b
JJ
6672 if (error_operand_p (DECL_INITIAL (object)))
6673 return t;
8e007055
JJ
6674 ctx.ctor = unshare_expr (DECL_INITIAL (object));
6675 TREE_READONLY (ctx.ctor) = false;
6676 /* Temporarily force decl_really_constant_value to return false
6677 for it, we want to use ctx.ctor for the current value instead. */
6678 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
6679 }
6680 else
6681 {
6682 ctx.ctor = build_constructor (type, NULL);
6683 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
6684 }
60813a46
JM
6685 if (!object)
6686 {
6687 if (TREE_CODE (t) == TARGET_EXPR)
6688 object = TARGET_EXPR_SLOT (t);
6689 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6690 object = AGGR_INIT_EXPR_SLOT (t);
6691 }
3e605b20
JM
6692 ctx.object = object;
6693 if (object)
6694 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6695 (type, TREE_TYPE (object)));
6696 if (object && DECL_P (object))
8e007055 6697 global_ctx.values.put (object, ctx.ctor);
3e605b20
JM
6698 if (TREE_CODE (r) == TARGET_EXPR)
6699 /* Avoid creating another CONSTRUCTOR when we expand the
6700 TARGET_EXPR. */
6701 r = TARGET_EXPR_INITIAL (r);
6702 }
6703
ee1de08d
JJ
6704 auto_vec<tree, 16> cleanups;
6705 global_ctx.cleanups = &cleanups;
6706
f65a3299 6707 instantiate_constexpr_fns (r);
2b3ab879 6708 r = cxx_eval_constant_expression (&ctx, r,
5a804683 6709 false, &non_constant_p, &overflow_p);
2d76680f 6710
8e007055
JJ
6711 if (!constexpr_dtor)
6712 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
6713 else
6714 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
2d76680f 6715
ee1de08d
JJ
6716 unsigned int i;
6717 tree cleanup;
6718 /* Evaluate the cleanups. */
6719 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
6720 cxx_eval_constant_expression (&ctx, cleanup, false,
6721 &non_constant_p, &overflow_p);
6722
023d89c7
JM
6723 /* Mutable logic is a bit tricky: we want to allow initialization of
6724 constexpr variables with mutable members, but we can't copy those
6725 members to another constexpr variable. */
8e007055 6726 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
2d76680f 6727 {
2d76680f 6728 if (!allow_non_constant)
023d89c7
JM
6729 error ("%qE is not a constant expression because it refers to "
6730 "mutable subobjects of %qT", t, type);
2d76680f
PC
6731 non_constant_p = true;
6732 }
6733
8e007055 6734 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
f64e0c02
JM
6735 {
6736 if (!allow_non_constant)
6737 error ("%qE is not a constant expression because it refers to "
6738 "an incompletely initialized variable", t);
6739 TREE_CONSTANT (r) = false;
6740 non_constant_p = true;
6741 }
6742
8e007055
JJ
6743 if (!global_ctx.heap_vars.is_empty ())
6744 {
6745 tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
6746 NULL);
6747 unsigned int i;
6748 if (heap_var)
6749 {
6750 if (!allow_non_constant && !non_constant_p)
6751 error_at (DECL_SOURCE_LOCATION (heap_var),
6752 "%qE is not a constant expression because it refers to "
6753 "a result of %<operator new%>", t);
6754 r = t;
6755 non_constant_p = true;
6756 }
6757 FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
6758 if (DECL_NAME (heap_var) != heap_deleted_identifier)
6759 {
6760 if (!allow_non_constant && !non_constant_p)
6761 error_at (DECL_SOURCE_LOCATION (heap_var),
6762 "%qE is not a constant expression because allocated "
6763 "storage has not been deallocated", t);
6764 r = t;
6765 non_constant_p = true;
6766 }
6767 }
6768
f968ef9b
JJ
6769 /* Check that immediate invocation does not return an expression referencing
6770 any immediate function decls. They need to be allowed while parsing
6771 immediate functions, but can't leak outside of them. */
6772 if (is_consteval
6773 && t != r
6774 && (current_function_decl == NULL_TREE
6775 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
6776 if (tree immediate_fndecl
6777 = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
6778 NULL))
6779 {
6780 if (!allow_non_constant && !non_constant_p)
6781 error_at (cp_expr_loc_or_input_loc (t),
6782 "immediate evaluation returns address of immediate "
6783 "function %qD", immediate_fndecl);
6784 r = t;
6785 non_constant_p = true;
6786 }
6787
2d76680f
PC
6788
6789 if (!non_constant_p && overflow_p)
6790 non_constant_p = true;
6791
e0804c9b 6792 /* Unshare the result. */
56cfb596 6793 bool should_unshare = true;
e0804c9b
JM
6794 if (r == t || (TREE_CODE (t) == TARGET_EXPR
6795 && TARGET_EXPR_INITIAL (t) == r))
56cfb596
PP
6796 should_unshare = false;
6797
2d76680f
PC
6798 if (non_constant_p && !allow_non_constant)
6799 return error_mark_node;
8e007055
JJ
6800 else if (constexpr_dtor)
6801 return r;
2d76680f
PC
6802 else if (non_constant_p && TREE_CONSTANT (r))
6803 {
e4082611
JJ
6804 /* If __builtin_is_constant_evaluated () was evaluated to true
6805 and the result is not a valid constant expression, we need to
6806 punt. */
13de99bc 6807 if (manifestly_const_eval)
e4082611 6808 return cxx_eval_outermost_constant_expr (t, true, strict,
8e007055 6809 false, false, object);
eddd715c
JM
6810 /* This isn't actually constant, so unset TREE_CONSTANT.
6811 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
6812 it to be set if it is invariant address, even when it is not
6813 a valid C++ constant expression. Wrap it with a NOP_EXPR
6814 instead. */
6815 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
2d76680f
PC
6816 r = copy_node (r);
6817 else if (TREE_CODE (r) == CONSTRUCTOR)
6818 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
6819 else
6820 r = build_nop (TREE_TYPE (r), r);
6821 TREE_CONSTANT (r) = false;
6822 }
5dab8b11 6823 else if (non_constant_p)
2d76680f
PC
6824 return t;
6825
56cfb596
PP
6826 if (should_unshare)
6827 r = unshare_expr (r);
6828
2d76680f
PC
6829 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
6830 {
5dab8b11 6831 r = adjust_temp_type (type, r);
2d76680f
PC
6832 if (TREE_CODE (t) == TARGET_EXPR
6833 && TARGET_EXPR_INITIAL (t) == r)
6834 return t;
5dab8b11 6835 else if (TREE_CODE (t) != CONSTRUCTOR)
2d76680f 6836 {
8e007055 6837 r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup);
2d76680f 6838 TREE_CONSTANT (r) = true;
2d76680f
PC
6839 }
6840 }
5dab8b11
JM
6841
6842 return r;
2d76680f
PC
6843}
6844
2d76680f
PC
6845/* If T represents a constant expression returns its reduced value.
6846 Otherwise return error_mark_node. If T is dependent, then
6847 return NULL. */
6848
6849tree
3e605b20 6850cxx_constant_value (tree t, tree decl)
2d76680f 6851{
8e007055
JJ
6852 return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl);
6853}
6854
6855/* Like cxx_constant_value, but used for evaluation of constexpr destructors
6856 of constexpr variables. The actual initializer of DECL is not modified. */
6857
6858void
6859cxx_constant_dtor (tree t, tree decl)
6860{
6861 cxx_eval_outermost_constant_expr (t, false, true, true, true, decl);
2d76680f
PC
6862}
6863
cda0a029
JM
6864/* Helper routine for fold_simple function. Either return simplified
6865 expression T, otherwise NULL_TREE.
6866 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
6867 even if we are within template-declaration. So be careful on call, as in
6868 such case types can be undefined. */
6869
6870static tree
6871fold_simple_1 (tree t)
6872{
6873 tree op1;
6874 enum tree_code code = TREE_CODE (t);
6875
6876 switch (code)
6877 {
6878 case INTEGER_CST:
6879 case REAL_CST:
6880 case VECTOR_CST:
6881 case FIXED_CST:
6882 case COMPLEX_CST:
6883 return t;
6884
6885 case SIZEOF_EXPR:
6886 return fold_sizeof_expr (t);
6887
6888 case ABS_EXPR:
e28fadbc 6889 case ABSU_EXPR:
cda0a029
JM
6890 case CONJ_EXPR:
6891 case REALPART_EXPR:
6892 case IMAGPART_EXPR:
6893 case NEGATE_EXPR:
6894 case BIT_NOT_EXPR:
6895 case TRUTH_NOT_EXPR:
6896 case NOP_EXPR:
6897 case VIEW_CONVERT_EXPR:
6898 case CONVERT_EXPR:
6899 case FLOAT_EXPR:
6900 case FIX_TRUNC_EXPR:
6901 case FIXED_CONVERT_EXPR:
6902 case ADDR_SPACE_CONVERT_EXPR:
6903
6904 op1 = TREE_OPERAND (t, 0);
6905
6906 t = const_unop (code, TREE_TYPE (t), op1);
6907 if (!t)
6908 return NULL_TREE;
6909
6910 if (CONVERT_EXPR_CODE_P (code)
6911 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
6912 TREE_OVERFLOW (t) = false;
6913 return t;
6914
6915 default:
6916 return NULL_TREE;
6917 }
6918}
6919
6920/* If T is a simple constant expression, returns its simplified value.
dba99244 6921 Otherwise returns T. In contrast to maybe_constant_value we
cda0a029
JM
6922 simplify only few operations on constant-expressions, and we don't
6923 try to simplify constexpressions. */
6924
6925tree
6926fold_simple (tree t)
6927{
cda0a029
JM
6928 if (processing_template_decl)
6929 return t;
6930
dba99244
MP
6931 tree r = fold_simple_1 (t);
6932 if (r)
6933 return r;
cda0a029 6934
dba99244 6935 return t;
cda0a029
JM
6936}
6937
2d76680f
PC
6938/* If T is a constant expression, returns its reduced value.
6939 Otherwise, if T does not have TREE_CONSTANT set, returns T.
13de99bc
JJ
6940 Otherwise, returns a version of T without TREE_CONSTANT.
6941 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
f65a3299 6942 as per P0595. */
2d76680f 6943
8a87daca
JM
6944static GTY((deletable)) hash_map<tree, tree> *cv_cache;
6945
6946tree
f65a3299 6947maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
2d76680f
PC
6948{
6949 tree r;
6950
a0ab7ccd 6951 if (!is_nondependent_constant_expression (t))
2d76680f
PC
6952 {
6953 if (TREE_OVERFLOW_P (t))
6954 {
6955 t = build_nop (TREE_TYPE (t), t);
6956 TREE_CONSTANT (t) = false;
6957 }
6958 return t;
6959 }
8a87daca
JM
6960 else if (CONSTANT_CLASS_P (t))
6961 /* No caching or evaluation needed. */
6962 return t;
6963
13de99bc 6964 if (manifestly_const_eval)
f65a3299 6965 return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl);
13de99bc 6966
8a87daca
JM
6967 if (cv_cache == NULL)
6968 cv_cache = hash_map<tree, tree>::create_ggc (101);
6969 if (tree *cached = cv_cache->get (t))
173c8def
JM
6970 {
6971 r = *cached;
6972 if (r != t)
6973 {
9f143008 6974 r = break_out_target_exprs (r, /*clear_loc*/true);
173c8def
JM
6975 protected_set_expr_location (r, EXPR_LOCATION (t));
6976 }
dfffecb8 6977 return r;
173c8def 6978 }
2d76680f 6979
f65a3299
PP
6980 uid_sensitive_constexpr_evaluation_checker c;
6981 r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl);
595278be
MM
6982 gcc_checking_assert (r == t
6983 || CONVERT_EXPR_P (t)
6984 || TREE_CODE (t) == VIEW_CONVERT_EXPR
6985 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
6986 || !cp_tree_equal (r, t));
f65a3299
PP
6987 if (!c.evaluation_restricted_p ())
6988 cv_cache->put (t, r);
2d76680f
PC
6989 return r;
6990}
6991
7a7ac32a
PP
6992/* Dispose of the whole CV_CACHE. */
6993
6994static void
6995clear_cv_cache (void)
6996{
6997 if (cv_cache != NULL)
6998 cv_cache->empty ();
6999}
7000
cb57504a 7001/* Dispose of the whole CV_CACHE, FOLD_CACHE, and satisfaction caches. */
1e297006
MP
7002
7003void
cb57504a 7004clear_cv_and_fold_caches (bool sat /*= true*/)
1e297006 7005{
7a7ac32a 7006 clear_cv_cache ();
eba9e839 7007 clear_fold_cache ();
cb57504a
JM
7008 if (sat)
7009 clear_satisfaction_cache ();
1e297006
MP
7010}
7011
a81c8e8c
MP
7012/* Internal function handling expressions in templates for
7013 fold_non_dependent_expr and fold_non_dependent_init.
7014
7015 If we're in a template, but T isn't value dependent, simplify
7016 it. We're supposed to treat:
7017
7018 template <typename T> void f(T[1 + 1]);
7019 template <typename T> void f(T[2]);
7020
7021 as two declarations of the same function, for example. */
7022
7023static tree
7024fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
f968ef9b
JJ
7025 bool manifestly_const_eval,
7026 tree object)
a81c8e8c
MP
7027{
7028 gcc_assert (processing_template_decl);
7029
7030 if (is_nondependent_constant_expression (t))
7031 {
7032 processing_template_decl_sentinel s;
7033 t = instantiate_non_dependent_expr_internal (t, complain);
7034
7035 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
7036 {
7037 if (TREE_OVERFLOW_P (t))
7038 {
7039 t = build_nop (TREE_TYPE (t), t);
7040 TREE_CONSTANT (t) = false;
7041 }
7042 return t;
7043 }
7044
7045 tree r = cxx_eval_outermost_constant_expr (t, true, true,
7046 manifestly_const_eval,
f968ef9b 7047 false, object);
a81c8e8c
MP
7048 /* cp_tree_equal looks through NOPs, so allow them. */
7049 gcc_checking_assert (r == t
7050 || CONVERT_EXPR_P (t)
7051 || TREE_CODE (t) == VIEW_CONVERT_EXPR
7052 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
7053 || !cp_tree_equal (r, t));
7054 return r;
7055 }
7056 else if (TREE_OVERFLOW_P (t))
7057 {
7058 t = build_nop (TREE_TYPE (t), t);
7059 TREE_CONSTANT (t) = false;
7060 }
7061
7062 return t;
7063}
7064
234bef96
PC
7065/* Like maybe_constant_value but first fully instantiate the argument.
7066
7067 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
e56f6629
JM
7068 (t, complain) followed by maybe_constant_value but is more efficient,
7069 because it calls instantiation_dependent_expression_p and
7070 potential_constant_expression at most once.
4cd3e7df 7071 The manifestly_const_eval argument is passed to maybe_constant_value.
e56f6629
JM
7072
7073 Callers should generally pass their active complain, or if they are in a
7074 non-template, diagnosing context, they can use the default of
7075 tf_warning_or_error. Callers that might be within a template context, don't
7076 have a complain parameter, and aren't going to remember the result for long
7077 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7078 appropriately. */
234bef96
PC
7079
7080tree
e56f6629 7081fold_non_dependent_expr (tree t,
4cd3e7df 7082 tsubst_flags_t complain /* = tf_warning_or_error */,
f968ef9b
JJ
7083 bool manifestly_const_eval /* = false */,
7084 tree object /* = NULL_TREE */)
234bef96
PC
7085{
7086 if (t == NULL_TREE)
7087 return NULL_TREE;
7088
a81c8e8c
MP
7089 if (processing_template_decl)
7090 return fold_non_dependent_expr_template (t, complain,
f968ef9b 7091 manifestly_const_eval, object);
234bef96 7092
f968ef9b 7093 return maybe_constant_value (t, object, manifestly_const_eval);
a81c8e8c 7094}
234bef96 7095
234bef96 7096
a81c8e8c 7097/* Like maybe_constant_init but first fully instantiate the argument. */
234bef96 7098
a81c8e8c
MP
7099tree
7100fold_non_dependent_init (tree t,
7101 tsubst_flags_t complain /*=tf_warning_or_error*/,
7102 bool manifestly_const_eval /*=false*/)
7103{
7104 if (t == NULL_TREE)
7105 return NULL_TREE;
7106
7107 if (processing_template_decl)
7108 {
7109 t = fold_non_dependent_expr_template (t, complain,
f968ef9b 7110 manifestly_const_eval, NULL_TREE);
a81c8e8c
MP
7111 /* maybe_constant_init does this stripping, so do it here too. */
7112 if (TREE_CODE (t) == TARGET_EXPR)
234bef96 7113 {
a81c8e8c
MP
7114 tree init = TARGET_EXPR_INITIAL (t);
7115 if (TREE_CODE (init) == CONSTRUCTOR)
7116 t = init;
234bef96
PC
7117 }
7118 return t;
7119 }
7120
a81c8e8c 7121 return maybe_constant_init (t, NULL_TREE, manifestly_const_eval);
234bef96
PC
7122}
7123
2d76680f 7124/* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
e4082611
JJ
7125 than wrapped in a TARGET_EXPR.
7126 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
13de99bc 7127 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
e4082611 7128 per P0595 even when ALLOW_NON_CONSTANT is true. */
2d76680f 7129
118cd6ba 7130static tree
e4082611 7131maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
13de99bc 7132 bool manifestly_const_eval)
2d76680f 7133{
cda0a029
JM
7134 if (!t)
7135 return t;
2d76680f
PC
7136 if (TREE_CODE (t) == EXPR_STMT)
7137 t = TREE_OPERAND (t, 0);
7138 if (TREE_CODE (t) == CONVERT_EXPR
7139 && VOID_TYPE_P (TREE_TYPE (t)))
7140 t = TREE_OPERAND (t, 0);
60813a46
JM
7141 if (TREE_CODE (t) == INIT_EXPR)
7142 t = TREE_OPERAND (t, 1);
f64e0c02
JM
7143 if (TREE_CODE (t) == TARGET_EXPR)
7144 t = TARGET_EXPR_INITIAL (t);
a0ab7ccd 7145 if (!is_nondependent_static_init_expression (t))
69eb4fde 7146 /* Don't try to evaluate it. */;
118cd6ba 7147 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
8a87daca 7148 /* No evaluation needed. */;
69eb4fde 7149 else
e4082611 7150 t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
38295573 7151 /*strict*/false,
8e007055 7152 manifestly_const_eval, false, decl);
2d76680f
PC
7153 if (TREE_CODE (t) == TARGET_EXPR)
7154 {
7155 tree init = TARGET_EXPR_INITIAL (t);
7156 if (TREE_CODE (init) == CONSTRUCTOR)
7157 t = init;
7158 }
7159 return t;
7160}
7161
118cd6ba
MP
7162/* Wrapper for maybe_constant_init_1 which permits non constants. */
7163
7164tree
13de99bc 7165maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
118cd6ba 7166{
13de99bc 7167 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
118cd6ba
MP
7168}
7169
7170/* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7171
7172tree
7173cxx_constant_init (tree t, tree decl)
7174{
e4082611 7175 return maybe_constant_init_1 (t, decl, false, true);
118cd6ba
MP
7176}
7177
2d76680f
PC
7178#if 0
7179/* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7180/* Return true if the object referred to by REF has automatic or thread
7181 local storage. */
7182
7183enum { ck_ok, ck_bad, ck_unknown };
7184static int
7185check_automatic_or_tls (tree ref)
7186{
ef4bddc2 7187 machine_mode mode;
f37fac2b 7188 poly_int64 bitsize, bitpos;
2d76680f
PC
7189 tree offset;
7190 int volatilep = 0, unsignedp = 0;
7191 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
7192 &mode, &unsignedp, &volatilep, false);
7193 duration_kind dk;
7194
7195 /* If there isn't a decl in the middle, we don't know the linkage here,
7196 and this isn't a constant expression anyway. */
7197 if (!DECL_P (decl))
7198 return ck_unknown;
7199 dk = decl_storage_duration (decl);
7200 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
7201}
7202#endif
7203
c7a53bdb
JJ
7204/* Data structure for passing data from potential_constant_expression_1
7205 to check_for_return_continue via cp_walk_tree. */
7206struct check_for_return_continue_data {
7207 hash_set<tree> *pset;
7208 tree continue_stmt;
7209};
7210
7211/* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7212 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7213 the first CONTINUE_STMT if RETURN_EXPR is not found. */
7214static tree
7215check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
7216{
7217 tree t = *tp, s;
7218 check_for_return_continue_data *d = (check_for_return_continue_data *) data;
7219 switch (TREE_CODE (t))
7220 {
7221 case RETURN_EXPR:
7222 return t;
7223
7224 case CONTINUE_STMT:
7225 if (d->continue_stmt == NULL_TREE)
7226 d->continue_stmt = t;
7227 break;
7228
7229#define RECUR(x) \
7230 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7231 d->pset)) \
7232 return r
7233
7234 /* For loops, walk subtrees manually, so that continue stmts found
7235 inside of the bodies of the loops are ignored. */
7236 case DO_STMT:
7237 *walk_subtrees = 0;
7238 RECUR (DO_COND (t));
7239 s = d->continue_stmt;
7240 RECUR (DO_BODY (t));
7241 d->continue_stmt = s;
7242 break;
7243
7244 case WHILE_STMT:
7245 *walk_subtrees = 0;
7246 RECUR (WHILE_COND (t));
7247 s = d->continue_stmt;
7248 RECUR (WHILE_BODY (t));
7249 d->continue_stmt = s;
7250 break;
7251
7252 case FOR_STMT:
7253 *walk_subtrees = 0;
7254 RECUR (FOR_INIT_STMT (t));
7255 RECUR (FOR_COND (t));
7256 RECUR (FOR_EXPR (t));
7257 s = d->continue_stmt;
7258 RECUR (FOR_BODY (t));
7259 d->continue_stmt = s;
7260 break;
7261
7262 case RANGE_FOR_STMT:
7263 *walk_subtrees = 0;
7264 RECUR (RANGE_FOR_EXPR (t));
7265 s = d->continue_stmt;
7266 RECUR (RANGE_FOR_BODY (t));
7267 d->continue_stmt = s;
7268 break;
7269#undef RECUR
7270
7271 case STATEMENT_LIST:
7272 case CONSTRUCTOR:
7273 break;
7274
7275 default:
7276 if (!EXPR_P (t))
7277 *walk_subtrees = 0;
7278 break;
7279 }
7280
7281 return NULL_TREE;
7282}
7283
2d76680f
PC
7284/* Return true if T denotes a potentially constant expression. Issue
7285 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
a0ab7ccd
JM
7286 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7287 consider the expression in the current context, independent of constexpr
7288 substitution.
2d76680f
PC
7289
7290 C++0x [expr.const] used to say
7291
7292 6 An expression is a potential constant expression if it is
7293 a constant expression where all occurrences of function
7294 parameters are replaced by arbitrary constant expressions
7295 of the appropriate type.
7296
7297 2 A conditional expression is a constant expression unless it
7298 involves one of the following as a potentially evaluated
7299 subexpression (3.2), but subexpressions of logical AND (5.14),
7300 logical OR (5.15), and conditional (5.16) operations that are
7301 not evaluated are not considered. */
7302
7303static bool
a0ab7ccd 7304potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
3075affd 7305 tsubst_flags_t flags, tree *jump_target)
2d76680f 7306{
a0ab7ccd 7307#define RECUR(T,RV) \
3075affd 7308 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
a0ab7ccd 7309
2d76680f
PC
7310 enum { any = false, rval = true };
7311 int i;
7312 tree tmp;
7313
7314 if (t == error_mark_node)
7315 return false;
7316 if (t == NULL_TREE)
7317 return true;
f9d0ca40 7318 location_t loc = cp_expr_loc_or_input_loc (t);
3075affd
JM
7319
7320 if (*jump_target)
7321 /* If we are jumping, ignore everything. This is simpler than the
7322 cxx_eval_constant_expression handling because we only need to be
7323 conservatively correct, and we don't necessarily have a constant value
7324 available, so we don't bother with switch tracking. */
7325 return true;
7326
3c393a2c 7327 if (TREE_THIS_VOLATILE (t) && want_rval)
2d76680f
PC
7328 {
7329 if (flags & tf_error)
3c393a2c
MP
7330 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue "
7331 "%qE with type %qT", t, TREE_TYPE (t));
2d76680f
PC
7332 return false;
7333 }
7334 if (CONSTANT_CLASS_P (t))
7335 return true;
7dc2b4a2
JM
7336 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
7337 && TREE_TYPE (t) == error_mark_node)
7338 return false;
2d76680f
PC
7339
7340 switch (TREE_CODE (t))
7341 {
7342 case FUNCTION_DECL:
7343 case BASELINK:
7344 case TEMPLATE_DECL:
7345 case OVERLOAD:
7346 case TEMPLATE_ID_EXPR:
7347 case LABEL_DECL:
7348 case LABEL_EXPR:
60813a46 7349 case CASE_LABEL_EXPR:
81012684 7350 case PREDICT_EXPR:
2d76680f
PC
7351 case CONST_DECL:
7352 case SIZEOF_EXPR:
7353 case ALIGNOF_EXPR:
7354 case OFFSETOF_EXPR:
7355 case NOEXCEPT_EXPR:
7356 case TEMPLATE_PARM_INDEX:
7357 case TRAIT_EXPR:
7358 case IDENTIFIER_NODE:
7359 case USERDEF_LITERAL:
7360 /* We can see a FIELD_DECL in a pointer-to-member expression. */
7361 case FIELD_DECL:
cda0a029 7362 case RESULT_DECL:
2d76680f 7363 case USING_DECL:
60813a46 7364 case USING_STMT:
3e605b20 7365 case PLACEHOLDER_EXPR:
971e17ff 7366 case REQUIRES_EXPR:
98e5a19a 7367 case STATIC_ASSERT:
96a95ac1 7368 case DEBUG_BEGIN_STMT:
2d76680f
PC
7369 return true;
7370
3075affd
JM
7371 case RETURN_EXPR:
7372 if (!RECUR (TREE_OPERAND (t, 0), any))
7373 return false;
7374 /* FALLTHROUGH */
7375
7376 case BREAK_STMT:
7377 case CONTINUE_STMT:
7378 *jump_target = t;
7379 return true;
7380
a0ab7ccd 7381 case PARM_DECL:
8fda2c27 7382 if (now && want_rval)
a0ab7ccd 7383 {
8fda2c27
JM
7384 tree type = TREE_TYPE (t);
7385 if (dependent_type_p (type)
7386 || is_really_empty_class (type, /*ignore_vptr*/false))
7387 /* An empty class has no data to read. */
7388 return true;
a0ab7ccd
JM
7389 if (flags & tf_error)
7390 error ("%qE is not a constant expression", t);
7391 return false;
7392 }
7393 return true;
7394
2d76680f
PC
7395 case AGGR_INIT_EXPR:
7396 case CALL_EXPR:
7397 /* -- an invocation of a function other than a constexpr function
7398 or a constexpr constructor. */
7399 {
7400 tree fun = get_function_named_in_call (t);
7401 const int nargs = call_expr_nargs (t);
7402 i = 0;
7403
60813a46
JM
7404 if (fun == NULL_TREE)
7405 {
44a845ca
MS
7406 /* Reset to allow the function to continue past the end
7407 of the block below. Otherwise return early. */
7408 bool bail = true;
7409
35228ac7
JJ
7410 if (TREE_CODE (t) == CALL_EXPR
7411 && CALL_EXPR_FN (t) == NULL_TREE)
7412 switch (CALL_EXPR_IFN (t))
7413 {
7414 /* These should be ignored, they are optimized away from
7415 constexpr functions. */
7416 case IFN_UBSAN_NULL:
7417 case IFN_UBSAN_BOUNDS:
7418 case IFN_UBSAN_VPTR:
81fea426 7419 case IFN_FALLTHROUGH:
35228ac7 7420 return true;
44a845ca
MS
7421
7422 case IFN_ADD_OVERFLOW:
7423 case IFN_SUB_OVERFLOW:
7424 case IFN_MUL_OVERFLOW:
e16f1cc7 7425 case IFN_LAUNDER:
d8fcab68 7426 case IFN_VEC_CONVERT:
44a845ca 7427 bail = false;
d8fcab68 7428 break;
44a845ca 7429
35228ac7
JJ
7430 default:
7431 break;
7432 }
44a845ca
MS
7433
7434 if (bail)
7435 {
7436 /* fold_call_expr can't do anything with IFN calls. */
7437 if (flags & tf_error)
e40b6fc7 7438 error_at (loc, "call to internal function %qE", t);
44a845ca
MS
7439 return false;
7440 }
60813a46 7441 }
44a845ca
MS
7442
7443 if (fun && is_overloaded_fn (fun))
2d76680f
PC
7444 {
7445 if (TREE_CODE (fun) == FUNCTION_DECL)
7446 {
7447 if (builtin_valid_in_constant_expr_p (fun))
7448 return true;
7449 if (!DECL_DECLARED_CONSTEXPR_P (fun)
7450 /* Allow any built-in function; if the expansion
7451 isn't constant, we'll deal with that then. */
8e007055 7452 && !fndecl_built_in_p (fun)
b04445d4 7453 /* In C++20, replaceable global allocation functions
8e007055 7454 are constant expressions. */
8412b939
JJ
7455 && (!cxx_replaceable_global_alloc_fn (fun)
7456 || TREE_CODE (t) != CALL_EXPR
7457 || (!CALL_FROM_NEW_OR_DELETE_P (t)
7458 && (current_function_decl == NULL_TREE
7459 || !is_std_allocator_allocate
7460 (current_function_decl))))
cf650568
JJ
7461 /* Allow placement new in std::construct_at. */
7462 && (!cxx_placement_new_fn (fun)
8412b939 7463 || TREE_CODE (t) != CALL_EXPR
cf650568 7464 || current_function_decl == NULL_TREE
22edf943
MP
7465 || !is_std_construct_at (current_function_decl))
7466 && !cxx_dynamic_cast_fn_p (fun))
2d76680f
PC
7467 {
7468 if (flags & tf_error)
7469 {
84fa214d 7470 error_at (loc, "call to non-%<constexpr%> function %qD",
e40b6fc7 7471 fun);
2d76680f
PC
7472 explain_invalid_constexpr_fn (fun);
7473 }
7474 return false;
7475 }
7476 /* A call to a non-static member function takes the address
7477 of the object as the first argument. But in a constant
7478 expression the address will be folded away, so look
7479 through it now. */
7480 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7481 && !DECL_CONSTRUCTOR_P (fun))
7482 {
7483 tree x = get_nth_callarg (t, 0);
7484 if (is_this_parameter (x))
3e605b20 7485 return true;
a0ab7ccd
JM
7486 /* Don't require an immediately constant value, as
7487 constexpr substitution might not use the value. */
7488 bool sub_now = false;
7489 if (!potential_constant_expression_1 (x, rval, strict,
3075affd
JM
7490 sub_now, flags,
7491 jump_target))
2d76680f
PC
7492 return false;
7493 i = 1;
7494 }
7495 }
7496 else
7497 {
69eb4fde 7498 if (!RECUR (fun, true))
2d76680f
PC
7499 return false;
7500 fun = get_first_fn (fun);
7501 }
7502 /* Skip initial arguments to base constructors. */
7503 if (DECL_BASE_CONSTRUCTOR_P (fun))
7504 i = num_artificial_parms_for (fun);
7505 fun = DECL_ORIGIN (fun);
7506 }
44a845ca 7507 else if (fun)
2d76680f 7508 {
69eb4fde 7509 if (RECUR (fun, rval))
2d76680f
PC
7510 /* Might end up being a constant function pointer. */;
7511 else
7512 return false;
7513 }
7514 for (; i < nargs; ++i)
7515 {
7516 tree x = get_nth_callarg (t, i);
c3c29ba5
JM
7517 /* In a template, reference arguments haven't been converted to
7518 REFERENCE_TYPE and we might not even know if the parameter
7519 is a reference, so accept lvalue constants too. */
7520 bool rv = processing_template_decl ? any : rval;
a0ab7ccd
JM
7521 /* Don't require an immediately constant value, as constexpr
7522 substitution might not use the value of the argument. */
7523 bool sub_now = false;
7524 if (!potential_constant_expression_1 (x, rv, strict,
3075affd 7525 sub_now, flags, jump_target))
2d76680f
PC
7526 return false;
7527 }
7528 return true;
7529 }
7530
7531 case NON_LVALUE_EXPR:
7532 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7533 -- an lvalue of integral type that refers to a non-volatile
7534 const variable or static data member initialized with
7535 constant expressions, or
7536
7537 -- an lvalue of literal type that refers to non-volatile
7538 object defined with constexpr, or that refers to a
7539 sub-object of such an object; */
69eb4fde 7540 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
7541
7542 case VAR_DECL:
70f40fea 7543 if (DECL_HAS_VALUE_EXPR_P (t))
c1051bf7 7544 {
68ad1bf7 7545 if (now && is_normal_capture_proxy (t))
c1051bf7
JM
7546 {
7547 /* -- in a lambda-expression, a reference to this or to a
7548 variable with automatic storage duration defined outside that
7549 lambda-expression, where the reference would be an
7550 odr-use. */
04b89192
JM
7551
7552 if (want_rval)
7553 /* Since we're doing an lvalue-rvalue conversion, this might
7554 not be an odr-use, so evaluate the variable directly. */
7555 return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
7556
c1051bf7
JM
7557 if (flags & tf_error)
7558 {
7559 tree cap = DECL_CAPTURED_VARIABLE (t);
7560 error ("lambda capture of %qE is not a constant expression",
7561 cap);
04b89192 7562 if (decl_constant_var_p (cap))
c1051bf7
JM
7563 inform (input_location, "because it is used as a glvalue");
7564 }
7565 return false;
7566 }
7567 return RECUR (DECL_VALUE_EXPR (t), rval);
7568 }
69eb4fde 7569 if (want_rval
98e5a19a 7570 && !var_in_maybe_constexpr_fn (t)
7dc2b4a2 7571 && !type_dependent_expression_p (t)
4b691b13 7572 && !decl_maybe_constant_var_p (t)
69eb4fde
JM
7573 && (strict
7574 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
4b691b13
JM
7575 || (DECL_INITIAL (t)
7576 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
7dc2b4a2 7577 && COMPLETE_TYPE_P (TREE_TYPE (t))
dbcd32f8 7578 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
2d76680f
PC
7579 {
7580 if (flags & tf_error)
6821245b 7581 non_const_var_error (loc, t);
2d76680f
PC
7582 return false;
7583 }
7584 return true;
7585
7586 case NOP_EXPR:
ed3ea9f2
JJ
7587 if (REINTERPRET_CAST_P (t))
7588 {
7589 if (flags & tf_error)
a9c697b8 7590 error_at (loc, "%<reinterpret_cast%> is not a constant expression");
ed3ea9f2
JJ
7591 return false;
7592 }
7593 /* FALLTHRU */
2d76680f
PC
7594 case CONVERT_EXPR:
7595 case VIEW_CONVERT_EXPR:
7596 /* -- a reinterpret_cast. FIXME not implemented, and this rule
7597 may change to something more specific to type-punning (DR 1312). */
7598 {
7599 tree from = TREE_OPERAND (t, 0);
dfd7fdca
DM
7600 if (location_wrapper_p (t))
7601 return (RECUR (from, want_rval));
7602 if (INDIRECT_TYPE_P (TREE_TYPE (t)))
2d76680f 7603 {
dfd7fdca
DM
7604 STRIP_ANY_LOCATION_WRAPPER (from);
7605 if (TREE_CODE (from) == INTEGER_CST
7606 && !integer_zerop (from))
7607 {
7608 if (flags & tf_error)
a9c697b8
MS
7609 error_at (loc,
7610 "%<reinterpret_cast%> from integer to pointer");
dfd7fdca
DM
7611 return false;
7612 }
2d76680f 7613 }
69eb4fde 7614 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
2d76680f
PC
7615 }
7616
be845b04
JJ
7617 case ADDRESSOF_EXPR:
7618 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
7619 t = TREE_OPERAND (t, 0);
7620 goto handle_addr_expr;
7621
2d76680f
PC
7622 case ADDR_EXPR:
7623 /* -- a unary operator & that is applied to an lvalue that
7624 designates an object with thread or automatic storage
7625 duration; */
7626 t = TREE_OPERAND (t, 0);
7627
7628 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
7629 /* A pointer-to-member constant. */
7630 return true;
7631
be845b04 7632 handle_addr_expr:
2d76680f
PC
7633#if 0
7634 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
7635 any checking here, as we might dereference the pointer later. If
7636 we remove this code, also remove check_automatic_or_tls. */
7637 i = check_automatic_or_tls (t);
7638 if (i == ck_ok)
7639 return true;
7640 if (i == ck_bad)
7641 {
7642 if (flags & tf_error)
7643 error ("address-of an object %qE with thread local or "
7644 "automatic storage is not a constant expression", t);
7645 return false;
7646 }
7647#endif
69eb4fde 7648 return RECUR (t, any);
2d76680f
PC
7649
7650 case COMPONENT_REF:
2d76680f
PC
7651 case ARROW_EXPR:
7652 case OFFSET_REF:
7653 /* -- a class member access unless its postfix-expression is
7654 of literal type or of pointer to literal type. */
7655 /* This test would be redundant, as it follows from the
7656 postfix-expression being a potential constant expression. */
19dedccf
JM
7657 if (type_unknown_p (t))
7658 return true;
8fda2c27
JM
7659 if (is_overloaded_fn (t))
7660 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
7661 which uses ob as an lvalue. */
7662 want_rval = false;
7663 gcc_fallthrough ();
7664
7665 case REALPART_EXPR:
7666 case IMAGPART_EXPR:
7667 case BIT_FIELD_REF:
69eb4fde 7668 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
7669
7670 case EXPR_PACK_EXPANSION:
69eb4fde 7671 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
2d76680f
PC
7672
7673 case INDIRECT_REF:
7674 {
7675 tree x = TREE_OPERAND (t, 0);
7676 STRIP_NOPS (x);
948d0d91 7677 if (is_this_parameter (x) && !is_capture_proxy (x))
2d76680f 7678 {
5d4e573b 7679 if (!var_in_maybe_constexpr_fn (x))
2d76680f
PC
7680 {
7681 if (flags & tf_error)
e40b6fc7 7682 error_at (loc, "use of %<this%> in a constant expression");
2d76680f
PC
7683 return false;
7684 }
2d76680f
PC
7685 return true;
7686 }
69eb4fde 7687 return RECUR (x, rval);
2d76680f
PC
7688 }
7689
60813a46
JM
7690 case STATEMENT_LIST:
7691 {
7692 tree_stmt_iterator i;
7693 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7694 {
69eb4fde 7695 if (!RECUR (tsi_stmt (i), any))
60813a46
JM
7696 return false;
7697 }
7698 return true;
7699 }
7700 break;
7701
7702 case MODIFY_EXPR:
7703 if (cxx_dialect < cxx14)
7704 goto fail;
69eb4fde 7705 if (!RECUR (TREE_OPERAND (t, 0), any))
60813a46 7706 return false;
8e007055
JJ
7707 /* Just ignore clobbers. */
7708 if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
7709 return true;
69eb4fde 7710 if (!RECUR (TREE_OPERAND (t, 1), rval))
60813a46
JM
7711 return false;
7712 return true;
7713
7714 case MODOP_EXPR:
7715 if (cxx_dialect < cxx14)
7716 goto fail;
69eb4fde 7717 if (!RECUR (TREE_OPERAND (t, 0), rval))
60813a46 7718 return false;
69eb4fde 7719 if (!RECUR (TREE_OPERAND (t, 2), rval))
60813a46
JM
7720 return false;
7721 return true;
7722
60813a46 7723 case DO_STMT:
69eb4fde 7724 if (!RECUR (DO_COND (t), rval))
60813a46 7725 return false;
69eb4fde 7726 if (!RECUR (DO_BODY (t), any))
60813a46 7727 return false;
3075affd
JM
7728 if (breaks (jump_target) || continues (jump_target))
7729 *jump_target = NULL_TREE;
60813a46
JM
7730 return true;
7731
7732 case FOR_STMT:
69eb4fde 7733 if (!RECUR (FOR_INIT_STMT (t), any))
60813a46 7734 return false;
8e9558f0
MP
7735 tmp = FOR_COND (t);
7736 if (!RECUR (tmp, rval))
60813a46 7737 return false;
8e9558f0
MP
7738 if (tmp)
7739 {
7740 if (!processing_template_decl)
7741 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
7742 /* If we couldn't evaluate the condition, it might not ever be
7743 true. */
7744 if (!integer_onep (tmp))
8e9558f0
MP
7745 return true;
7746 }
69eb4fde 7747 if (!RECUR (FOR_EXPR (t), any))
60813a46 7748 return false;
69eb4fde 7749 if (!RECUR (FOR_BODY (t), any))
60813a46 7750 return false;
3075affd
JM
7751 if (breaks (jump_target) || continues (jump_target))
7752 *jump_target = NULL_TREE;
60813a46
JM
7753 return true;
7754
98e5a19a 7755 case RANGE_FOR_STMT:
8112667c
MP
7756 if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
7757 return false;
98e5a19a
JM
7758 if (!RECUR (RANGE_FOR_EXPR (t), any))
7759 return false;
7760 if (!RECUR (RANGE_FOR_BODY (t), any))
7761 return false;
3075affd
JM
7762 if (breaks (jump_target) || continues (jump_target))
7763 *jump_target = NULL_TREE;
98e5a19a
JM
7764 return true;
7765
60813a46 7766 case WHILE_STMT:
8e9558f0
MP
7767 tmp = WHILE_COND (t);
7768 if (!RECUR (tmp, rval))
60813a46 7769 return false;
8e9558f0
MP
7770 if (!processing_template_decl)
7771 tmp = cxx_eval_outermost_constant_expr (tmp, true);
a385474c
MP
7772 /* If we couldn't evaluate the condition, it might not ever be true. */
7773 if (!integer_onep (tmp))
8e9558f0 7774 return true;
69eb4fde 7775 if (!RECUR (WHILE_BODY (t), any))
60813a46 7776 return false;
3075affd
JM
7777 if (breaks (jump_target) || continues (jump_target))
7778 *jump_target = NULL_TREE;
60813a46
JM
7779 return true;
7780
7781 case SWITCH_STMT:
69eb4fde 7782 if (!RECUR (SWITCH_STMT_COND (t), rval))
60813a46 7783 return false;
ce965730 7784 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
c7a53bdb
JJ
7785 unreachable labels would be checked and it is enough if there is
7786 a single switch cond value for which it is a valid constant
7787 expression. We need to check if there are any RETURN_EXPRs
7788 or CONTINUE_STMTs inside of the body though, as in that case
7789 we need to set *jump_target. */
7790 else
7791 {
7792 hash_set<tree> pset;
7793 check_for_return_continue_data data = { &pset, NULL_TREE };
7794 if (tree ret_expr
7795 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
7796 &data, &pset))
7797 /* The switch might return. */
7798 *jump_target = ret_expr;
7799 else if (data.continue_stmt)
7800 /* The switch can't return, but might continue. */
7801 *jump_target = data.continue_stmt;
7802 }
60813a46
JM
7803 return true;
7804
58611fb6 7805 case STMT_EXPR:
69eb4fde 7806 return RECUR (STMT_EXPR_STMT (t), rval);
58611fb6 7807
2d76680f 7808 case LAMBDA_EXPR:
899ac3b8
JM
7809 if (cxx_dialect >= cxx17)
7810 /* In C++17 lambdas can be constexpr, don't give up yet. */
7811 return true;
7812 else if (flags & tf_error)
7813 error_at (loc, "lambda-expression is not a constant expression "
7814 "before C++17");
7815 return false;
7816
2d76680f
PC
7817 case DYNAMIC_CAST_EXPR:
7818 case PSEUDO_DTOR_EXPR:
2d76680f
PC
7819 case NEW_EXPR:
7820 case VEC_NEW_EXPR:
7821 case DELETE_EXPR:
7822 case VEC_DELETE_EXPR:
7823 case THROW_EXPR:
e40b6fc7
JJ
7824 case OMP_PARALLEL:
7825 case OMP_TASK:
7826 case OMP_FOR:
897064e2 7827 case OMP_SIMD:
e40b6fc7
JJ
7828 case OMP_DISTRIBUTE:
7829 case OMP_TASKLOOP:
d81ab49d 7830 case OMP_LOOP:
e40b6fc7
JJ
7831 case OMP_TEAMS:
7832 case OMP_TARGET_DATA:
7833 case OMP_TARGET:
7834 case OMP_SECTIONS:
7835 case OMP_ORDERED:
7836 case OMP_CRITICAL:
7837 case OMP_SINGLE:
7838 case OMP_SECTION:
7839 case OMP_MASTER:
7840 case OMP_TASKGROUP:
7841 case OMP_TARGET_UPDATE:
7842 case OMP_TARGET_ENTER_DATA:
7843 case OMP_TARGET_EXIT_DATA:
2d76680f
PC
7844 case OMP_ATOMIC:
7845 case OMP_ATOMIC_READ:
7846 case OMP_ATOMIC_CAPTURE_OLD:
7847 case OMP_ATOMIC_CAPTURE_NEW:
28567c40 7848 case OMP_DEPOBJ:
e40b6fc7
JJ
7849 case OACC_PARALLEL:
7850 case OACC_KERNELS:
62aee289 7851 case OACC_SERIAL:
e40b6fc7
JJ
7852 case OACC_DATA:
7853 case OACC_HOST_DATA:
7854 case OACC_LOOP:
7855 case OACC_CACHE:
7856 case OACC_DECLARE:
7857 case OACC_ENTER_DATA:
7858 case OACC_EXIT_DATA:
7859 case OACC_UPDATE:
2d76680f
PC
7860 /* GCC internal stuff. */
7861 case VA_ARG_EXPR:
2d76680f 7862 case TRANSACTION_EXPR:
81b6a6c5 7863 case AT_ENCODE_EXPR:
60813a46 7864 fail:
2d76680f 7865 if (flags & tf_error)
e40b6fc7 7866 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
7867 return false;
7868
529bc410 7869 case ASM_EXPR:
7c814975
MP
7870 if (flags & tf_error)
7871 inline_asm_in_constexpr_error (loc);
7872 return false;
529bc410 7873
bf8d8309 7874 case OBJ_TYPE_REF:
b04445d4
JM
7875 if (cxx_dialect >= cxx20)
7876 /* In C++20 virtual calls can be constexpr, don't give up yet. */
bf8d8309
MP
7877 return true;
7878 else if (flags & tf_error)
a9c697b8 7879 error_at (loc,
b04445d4 7880 "virtual functions cannot be %<constexpr%> before C++20");
bf8d8309
MP
7881 return false;
7882
2d76680f 7883 case TYPEID_EXPR:
66acfb80
MP
7884 /* In C++20, a typeid expression whose operand is of polymorphic
7885 class type can be constexpr. */
2d76680f
PC
7886 {
7887 tree e = TREE_OPERAND (t, 0);
b04445d4 7888 if (cxx_dialect < cxx20
66acfb80
MP
7889 && strict
7890 && !TYPE_P (e)
7891 && !type_dependent_expression_p (e)
2d76680f
PC
7892 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
7893 {
7894 if (flags & tf_error)
a9c697b8 7895 error_at (loc, "%<typeid%> is not a constant expression "
e40b6fc7 7896 "because %qE is of polymorphic type", e);
2d76680f
PC
7897 return false;
7898 }
7899 return true;
7900 }
7901
1af4ebf5 7902 case POINTER_DIFF_EXPR:
2d76680f 7903 case MINUS_EXPR:
2d76680f
PC
7904 want_rval = true;
7905 goto binary;
7906
7907 case LT_EXPR:
7908 case LE_EXPR:
7909 case GT_EXPR:
7910 case GE_EXPR:
7911 case EQ_EXPR:
7912 case NE_EXPR:
b7689b96 7913 case SPACESHIP_EXPR:
2d76680f
PC
7914 want_rval = true;
7915 goto binary;
7916
60813a46
JM
7917 case PREINCREMENT_EXPR:
7918 case POSTINCREMENT_EXPR:
7919 case PREDECREMENT_EXPR:
7920 case POSTDECREMENT_EXPR:
7921 if (cxx_dialect < cxx14)
7922 goto fail;
7923 goto unary;
7924
2d76680f
PC
7925 case BIT_NOT_EXPR:
7926 /* A destructor. */
7927 if (TYPE_P (TREE_OPERAND (t, 0)))
7928 return true;
191816a3 7929 /* fall through. */
2d76680f 7930
2d76680f
PC
7931 case CONJ_EXPR:
7932 case SAVE_EXPR:
7933 case FIX_TRUNC_EXPR:
7934 case FLOAT_EXPR:
7935 case NEGATE_EXPR:
7936 case ABS_EXPR:
e197e64e 7937 case ABSU_EXPR:
2d76680f
PC
7938 case TRUTH_NOT_EXPR:
7939 case FIXED_CONVERT_EXPR:
7940 case UNARY_PLUS_EXPR:
4856a1f0
PC
7941 case UNARY_LEFT_FOLD_EXPR:
7942 case UNARY_RIGHT_FOLD_EXPR:
60813a46 7943 unary:
69eb4fde 7944 return RECUR (TREE_OPERAND (t, 0), rval);
2d76680f
PC
7945
7946 case CAST_EXPR:
7947 case CONST_CAST_EXPR:
7948 case STATIC_CAST_EXPR:
7949 case REINTERPRET_CAST_EXPR:
7950 case IMPLICIT_CONV_EXPR:
7951 if (cxx_dialect < cxx11
7952 && !dependent_type_p (TREE_TYPE (t))
7953 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
7954 /* In C++98, a conversion to non-integral type can't be part of a
7955 constant expression. */
7956 {
7957 if (flags & tf_error)
e40b6fc7
JJ
7958 error_at (loc,
7959 "cast to non-integral type %qT in a constant expression",
7960 TREE_TYPE (t));
2d76680f
PC
7961 return false;
7962 }
fe0604d3
MP
7963 /* This might be a conversion from a class to a (potentially) literal
7964 type. Let's consider it potentially constant since the conversion
7965 might be a constexpr user-defined conversion. */
7966 else if (cxx_dialect >= cxx11
7967 && (dependent_type_p (TREE_TYPE (t))
7968 || !COMPLETE_TYPE_P (TREE_TYPE (t))
7969 || literal_type_p (TREE_TYPE (t)))
7970 && TREE_OPERAND (t, 0))
7971 {
7972 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
7973 /* If this is a dependent type, it could end up being a class
7974 with conversions. */
7975 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
7976 return true;
7977 /* Or a non-dependent class which has conversions. */
7978 else if (CLASS_TYPE_P (type)
7979 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
7980 return true;
7981 }
2d76680f 7982
69eb4fde 7983 return (RECUR (TREE_OPERAND (t, 0),
9f613f06 7984 !TYPE_REF_P (TREE_TYPE (t))));
2d76680f 7985
60813a46 7986 case BIND_EXPR:
69eb4fde 7987 return RECUR (BIND_EXPR_BODY (t), want_rval);
60813a46 7988
60813a46
JM
7989 case CLEANUP_POINT_EXPR:
7990 case MUST_NOT_THROW_EXPR:
7991 case TRY_CATCH_EXPR:
769430b2 7992 case TRY_BLOCK:
60813a46
JM
7993 case EH_SPEC_BLOCK:
7994 case EXPR_STMT:
2d76680f
PC
7995 case PAREN_EXPR:
7996 case NON_DEPENDENT_EXPR:
7997 /* For convenience. */
f937929e
JM
7998 case LOOP_EXPR:
7999 case EXIT_EXPR:
69eb4fde 8000 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f 8001
98e5a19a
JM
8002 case DECL_EXPR:
8003 tmp = DECL_EXPR_DECL (t);
8004 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
8005 {
8006 if (TREE_STATIC (tmp))
8007 {
8008 if (flags & tf_error)
8009 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
4b691b13 8010 "%<static%> in %<constexpr%> context", tmp);
98e5a19a
JM
8011 return false;
8012 }
8013 else if (CP_DECL_THREAD_LOCAL_P (tmp))
8014 {
8015 if (flags & tf_error)
8016 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
4b691b13 8017 "%<thread_local%> in %<constexpr%> context", tmp);
98e5a19a
JM
8018 return false;
8019 }
3885527d
PC
8020 else if (!check_for_uninitialized_const_var
8021 (tmp, /*constexpr_context_p=*/true, flags))
8022 return false;
98e5a19a
JM
8023 }
8024 return RECUR (tmp, want_rval);
8025
769430b2
JM
8026 case TRY_FINALLY_EXPR:
8027 return (RECUR (TREE_OPERAND (t, 0), want_rval)
8028 && RECUR (TREE_OPERAND (t, 1), any));
8029
2d76680f 8030 case SCOPE_REF:
69eb4fde 8031 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
8032
8033 case TARGET_EXPR:
d3a9902e
JM
8034 if (!TARGET_EXPR_DIRECT_INIT_P (t)
8035 && !literal_type_p (TREE_TYPE (t)))
2d76680f
PC
8036 {
8037 if (flags & tf_error)
8038 {
097f82ec 8039 auto_diagnostic_group d;
e40b6fc7
JJ
8040 error_at (loc, "temporary of non-literal type %qT in a "
8041 "constant expression", TREE_TYPE (t));
2d76680f
PC
8042 explain_non_literal_class (TREE_TYPE (t));
8043 }
8044 return false;
8045 }
191816a3 8046 /* FALLTHRU */
2d76680f 8047 case INIT_EXPR:
69eb4fde 8048 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
8049
8050 case CONSTRUCTOR:
8051 {
8052 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8053 constructor_elt *ce;
8054 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
69eb4fde 8055 if (!RECUR (ce->value, want_rval))
2d76680f
PC
8056 return false;
8057 return true;
8058 }
8059
8060 case TREE_LIST:
8061 {
8062 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8063 || DECL_P (TREE_PURPOSE (t)));
69eb4fde 8064 if (!RECUR (TREE_VALUE (t), want_rval))
2d76680f
PC
8065 return false;
8066 if (TREE_CHAIN (t) == NULL_TREE)
8067 return true;
69eb4fde 8068 return RECUR (TREE_CHAIN (t), want_rval);
2d76680f
PC
8069 }
8070
8071 case TRUNC_DIV_EXPR:
8072 case CEIL_DIV_EXPR:
8073 case FLOOR_DIV_EXPR:
8074 case ROUND_DIV_EXPR:
8075 case TRUNC_MOD_EXPR:
8076 case CEIL_MOD_EXPR:
8077 case ROUND_MOD_EXPR:
8078 {
8079 tree denom = TREE_OPERAND (t, 1);
69eb4fde 8080 if (!RECUR (denom, rval))
2d76680f
PC
8081 return false;
8082 /* We can't call cxx_eval_outermost_constant_expr on an expression
234bef96 8083 that hasn't been through instantiate_non_dependent_expr yet. */
2d76680f
PC
8084 if (!processing_template_decl)
8085 denom = cxx_eval_outermost_constant_expr (denom, true);
8086 if (integer_zerop (denom))
8087 {
8088 if (flags & tf_error)
64d6d399 8089 error ("division by zero is not a constant expression");
2d76680f
PC
8090 return false;
8091 }
8092 else
8093 {
8094 want_rval = true;
69eb4fde 8095 return RECUR (TREE_OPERAND (t, 0), want_rval);
2d76680f
PC
8096 }
8097 }
8098
8099 case COMPOUND_EXPR:
8100 {
8101 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8102 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8103 introduced by build_call_a. */
8104 tree op0 = TREE_OPERAND (t, 0);
8105 tree op1 = TREE_OPERAND (t, 1);
8106 STRIP_NOPS (op1);
8107 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
8108 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
69eb4fde 8109 return RECUR (op0, want_rval);
2d76680f
PC
8110 else
8111 goto binary;
8112 }
8113
8114 /* If the first operand is the non-short-circuit constant, look at
8115 the second operand; otherwise we only care about the first one for
8116 potentiality. */
8117 case TRUTH_AND_EXPR:
8118 case TRUTH_ANDIF_EXPR:
8119 tmp = boolean_true_node;
8120 goto truth;
8121 case TRUTH_OR_EXPR:
8122 case TRUTH_ORIF_EXPR:
8123 tmp = boolean_false_node;
8124 truth:
8125 {
8126 tree op = TREE_OPERAND (t, 0);
69eb4fde 8127 if (!RECUR (op, rval))
2d76680f
PC
8128 return false;
8129 if (!processing_template_decl)
8130 op = cxx_eval_outermost_constant_expr (op, true);
8131 if (tree_int_cst_equal (op, tmp))
69eb4fde 8132 return RECUR (TREE_OPERAND (t, 1), rval);
2d76680f
PC
8133 else
8134 return true;
8135 }
8136
8137 case PLUS_EXPR:
8138 case MULT_EXPR:
8139 case POINTER_PLUS_EXPR:
8140 case RDIV_EXPR:
8141 case EXACT_DIV_EXPR:
8142 case MIN_EXPR:
8143 case MAX_EXPR:
8144 case LSHIFT_EXPR:
8145 case RSHIFT_EXPR:
81fa6d73
JJ
8146 case LROTATE_EXPR:
8147 case RROTATE_EXPR:
2d76680f
PC
8148 case BIT_IOR_EXPR:
8149 case BIT_XOR_EXPR:
8150 case BIT_AND_EXPR:
8151 case TRUTH_XOR_EXPR:
8152 case UNORDERED_EXPR:
8153 case ORDERED_EXPR:
8154 case UNLT_EXPR:
8155 case UNLE_EXPR:
8156 case UNGT_EXPR:
8157 case UNGE_EXPR:
8158 case UNEQ_EXPR:
8159 case LTGT_EXPR:
8160 case RANGE_EXPR:
8161 case COMPLEX_EXPR:
8162 want_rval = true;
8163 /* Fall through. */
8164 case ARRAY_REF:
8165 case ARRAY_RANGE_REF:
8166 case MEMBER_REF:
8167 case DOTSTAR_EXPR:
41b38772 8168 case MEM_REF:
4856a1f0
PC
8169 case BINARY_LEFT_FOLD_EXPR:
8170 case BINARY_RIGHT_FOLD_EXPR:
2d76680f
PC
8171 binary:
8172 for (i = 0; i < 2; ++i)
69eb4fde 8173 if (!RECUR (TREE_OPERAND (t, i), want_rval))
2d76680f
PC
8174 return false;
8175 return true;
8176
2d76680f
PC
8177 case VEC_PERM_EXPR:
8178 for (i = 0; i < 3; ++i)
69eb4fde 8179 if (!RECUR (TREE_OPERAND (t, i), true))
2d76680f
PC
8180 return false;
8181 return true;
8182
8183 case COND_EXPR:
b04445d4 8184 if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
d5bcd6d4
JM
8185 {
8186 if (flags & tf_error)
e40b6fc7 8187 error_at (loc, "%<delete[]%> is not a constant expression");
d5bcd6d4
JM
8188 return false;
8189 }
8190 /* Fall through. */
8191 case IF_STMT:
2d76680f
PC
8192 case VEC_COND_EXPR:
8193 /* If the condition is a known constant, we know which of the legs we
8194 care about; otherwise we only require that the condition and
8195 either of the legs be potentially constant. */
8196 tmp = TREE_OPERAND (t, 0);
69eb4fde 8197 if (!RECUR (tmp, rval))
2d76680f
PC
8198 return false;
8199 if (!processing_template_decl)
8200 tmp = cxx_eval_outermost_constant_expr (tmp, true);
8201 if (integer_zerop (tmp))
69eb4fde 8202 return RECUR (TREE_OPERAND (t, 2), want_rval);
2d76680f 8203 else if (TREE_CODE (tmp) == INTEGER_CST)
69eb4fde 8204 return RECUR (TREE_OPERAND (t, 1), want_rval);
2d76680f
PC
8205 for (i = 1; i < 3; ++i)
8206 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
3075affd
JM
8207 want_rval, strict, now,
8208 tf_none, jump_target))
2d76680f
PC
8209 return true;
8210 if (flags & tf_error)
e40b6fc7 8211 error_at (loc, "expression %qE is not a constant expression", t);
2d76680f
PC
8212 return false;
8213
8214 case VEC_INIT_EXPR:
8215 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
8216 return true;
8217 if (flags & tf_error)
8218 {
e40b6fc7 8219 error_at (loc, "non-constant array initialization");
2d76680f
PC
8220 diagnose_non_constexpr_vec_init (t);
8221 }
8222 return false;
8223
133bc698
JM
8224 case TYPE_DECL:
8225 case TAG_DEFN:
8226 /* We can see these in statement-expressions. */
8227 return true;
8228
baf9ebc8 8229 case CLEANUP_STMT:
1006c9d4
JJ
8230 if (!RECUR (CLEANUP_BODY (t), any))
8231 return false;
8232 if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
8233 return false;
8234 return true;
8235
cda0a029
JM
8236 case EMPTY_CLASS_EXPR:
8237 return false;
8238
f937929e
JM
8239 case GOTO_EXPR:
8240 {
8241 tree *target = &TREE_OPERAND (t, 0);
ab3af181
JJ
8242 /* Gotos representing break and continue are OK. */
8243 if (breaks (target) || continues (target))
3075affd
JM
8244 {
8245 *jump_target = *target;
8246 return true;
8247 }
ab3af181 8248 if (flags & tf_error)
e40b6fc7 8249 error_at (loc, "%<goto%> is not a constant expression");
ab3af181 8250 return false;
f937929e
JM
8251 }
8252
98e09245 8253 case ANNOTATE_EXPR:
98e09245
JJ
8254 return RECUR (TREE_OPERAND (t, 0), rval);
8255
49789fd0
IS
8256 /* Coroutine await, yield and return expressions are not. */
8257 case CO_AWAIT_EXPR:
8258 case CO_YIELD_EXPR:
8259 case CO_RETURN_EXPR:
8260 return false;
8261
2d76680f
PC
8262 default:
8263 if (objc_is_property_ref (t))
8264 return false;
8265
8266 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
ab3af181 8267 gcc_unreachable ();
2d76680f
PC
8268 return false;
8269 }
69eb4fde 8270#undef RECUR
2d76680f
PC
8271}
8272
3075affd
JM
8273bool
8274potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
8275 tsubst_flags_t flags)
8276{
8277 tree target = NULL_TREE;
8278 return potential_constant_expression_1 (t, want_rval, strict, now,
8279 flags, &target);
8280}
8281
2d76680f
PC
8282/* The main entry point to the above. */
8283
8284bool
8285potential_constant_expression (tree t)
8286{
a0ab7ccd 8287 return potential_constant_expression_1 (t, false, true, false, tf_none);
2d76680f
PC
8288}
8289
8290/* As above, but require a constant rvalue. */
8291
8292bool
8293potential_rvalue_constant_expression (tree t)
8294{
a0ab7ccd 8295 return potential_constant_expression_1 (t, true, true, false, tf_none);
2d76680f
PC
8296}
8297
8298/* Like above, but complain about non-constant expressions. */
8299
8300bool
8301require_potential_constant_expression (tree t)
8302{
d8cff23f
MP
8303 return potential_constant_expression_1 (t, false, true, false,
8304 tf_warning_or_error);
2d76680f
PC
8305}
8306
8307/* Cross product of the above. */
8308
8309bool
8310require_potential_rvalue_constant_expression (tree t)
8311{
d8cff23f
MP
8312 return potential_constant_expression_1 (t, true, true, false,
8313 tf_warning_or_error);
8314}
8315
8316/* Like above, but don't consider PARM_DECL a potential_constant_expression. */
8317
8318bool
8319require_rvalue_constant_expression (tree t)
8320{
8321 return potential_constant_expression_1 (t, true, true, true,
8322 tf_warning_or_error);
a0ab7ccd
JM
8323}
8324
8325/* Like potential_constant_expression, but don't consider possible constexpr
8326 substitution of the current function. That is, PARM_DECL qualifies under
8327 potential_constant_expression, but not here.
8328
8329 This is basically what you can check when any actual constant values might
8330 be value-dependent. */
8331
8332bool
8333is_constant_expression (tree t)
8334{
8335 return potential_constant_expression_1 (t, false, true, true, tf_none);
8336}
8337
beb019d3
JM
8338/* As above, but expect an rvalue. */
8339
8340bool
8341is_rvalue_constant_expression (tree t)
8342{
8343 return potential_constant_expression_1 (t, true, true, true, tf_none);
8344}
8345
a0ab7ccd
JM
8346/* Like above, but complain about non-constant expressions. */
8347
8348bool
8349require_constant_expression (tree t)
8350{
8351 return potential_constant_expression_1 (t, false, true, true,
8352 tf_warning_or_error);
8353}
8354
8355/* Like is_constant_expression, but allow const variables that are not allowed
8356 under constexpr rules. */
8357
8358bool
8359is_static_init_expression (tree t)
8360{
8361 return potential_constant_expression_1 (t, false, false, true, tf_none);
2d76680f
PC
8362}
8363
eb07f187
JM
8364/* Returns true if T is a potential constant expression that is not
8365 instantiation-dependent, and therefore a candidate for constant folding even
8366 in a template. */
8367
8368bool
a0ab7ccd 8369is_nondependent_constant_expression (tree t)
eb07f187
JM
8370{
8371 return (!type_unknown_p (t)
a0ab7ccd 8372 && is_constant_expression (t)
eb07f187
JM
8373 && !instantiation_dependent_expression_p (t));
8374}
8375
8376/* Returns true if T is a potential static initializer expression that is not
8377 instantiation-dependent. */
8378
8379bool
a0ab7ccd 8380is_nondependent_static_init_expression (tree t)
eb07f187
JM
8381{
8382 return (!type_unknown_p (t)
a0ab7ccd 8383 && is_static_init_expression (t)
eb07f187
JM
8384 && !instantiation_dependent_expression_p (t));
8385}
8386
97f3003f
JM
8387/* Finalize constexpr processing after parsing. */
8388
8389void
8390fini_constexpr (void)
8391{
8392 /* The contexpr call and fundef copies tables are no longer needed. */
8393 constexpr_call_table = NULL;
8394 fundef_copies_table = NULL;
8395}
8396
2d76680f 8397#include "gt-cp-constexpr.h"
This page took 3.729268 seconds and 5 git commands to generate.