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