c++: local-scope OMP UDR reductions have no template head
[gcc.git] / gcc / cp / constexpr.c
1 /* Perform -*- C++ -*- constant expression evaluation, including calls to
2 constexpr functions. These routines are used both during actual parsing
3 and during the instantiation of template functions.
4
5 Copyright (C) 1998-2020 Free Software Foundation, Inc.
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
19 You should have received a copy of the GNU General Public License
20 along 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"
26 #include "cp-tree.h"
27 #include "varasm.h"
28 #include "c-family/c-objc.h"
29 #include "tree-iterator.h"
30 #include "gimplify.h"
31 #include "builtins.h"
32 #include "tree-inline.h"
33 #include "ubsan.h"
34 #include "gimple-fold.h"
35 #include "timevar.h"
36 #include "fold-const-call.h"
37 #include "stor-layout.h"
38
39 static bool verify_constant (tree, bool, bool *, bool *);
40 #define VERIFY_CONSTANT(X) \
41 do { \
42 if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
43 return t; \
44 } while (0)
45
46 static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
47 bool insert = false);
48
49 /* Returns true iff FUN is an instantiation of a constexpr function
50 template or a defaulted constexpr function. */
51
52 bool
53 is_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
63 bool
64 literal_type_p (tree t)
65 {
66 if (SCALAR_TYPE_P (t)
67 || VECTOR_TYPE_P (t)
68 || TYPE_REF_P (t)
69 || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
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
83 be literal. Return error_mark_node if we give an error, the
84 DECL otherwise. */
85
86 tree
87 ensure_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))
102 {
103 auto_diagnostic_group d;
104 error_at (DECL_SOURCE_LOCATION (decl),
105 "the type %qT of %<constexpr%> variable %qD "
106 "is not literal", type, decl);
107 explain_non_literal_class (type);
108 decl = error_mark_node;
109 }
110 else
111 {
112 if (!is_instantiation_of_constexpr (current_function_decl))
113 {
114 auto_diagnostic_group d;
115 error_at (DECL_SOURCE_LOCATION (decl),
116 "variable %qD of non-literal type %qT in "
117 "%<constexpr%> function", decl, type);
118 explain_non_literal_class (type);
119 decl = error_mark_node;
120 }
121 cp_function_chain->invalid_constexpr = true;
122 }
123 }
124 else if (DECL_DECLARED_CONSTEXPR_P (decl)
125 && variably_modified_type_p (type, NULL_TREE))
126 {
127 error_at (DECL_SOURCE_LOCATION (decl),
128 "%<constexpr%> variable %qD has variably-modified "
129 "type %qT", decl, type);
130 decl = error_mark_node;
131 }
132 }
133 return decl;
134 }
135
136 /* Representation of entries in the constexpr function definition table. */
137
138 struct GTY((for_user)) constexpr_fundef {
139 tree decl;
140 tree body;
141 tree parms;
142 tree result;
143 };
144
145 struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
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
154 static 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
160 inline bool
161 constexpr_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
169 inline hashval_t
170 constexpr_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
177 static constexpr_fundef *
178 retrieve_constexpr_fundef (tree fun)
179 {
180 if (constexpr_fundef_table == NULL)
181 return NULL;
182
183 constexpr_fundef fundef = { fun, NULL, NULL, NULL };
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
190 bool
191 is_valid_constexpr_fn (tree fun, bool complain)
192 {
193 bool ret = true;
194
195 if (DECL_INHERITED_CTOR (fun)
196 && TREE_CODE (fun) == TEMPLATE_DECL)
197 {
198 ret = false;
199 if (complain)
200 error ("inherited constructor %qD is not %<constexpr%>",
201 DECL_INHERITED_CTOR (fun));
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 {
212 auto_diagnostic_group d;
213 error ("invalid type for parameter %d of %<constexpr%> "
214 "function %q+#D", DECL_PARM_INDEX (parm), fun);
215 explain_non_literal_class (TREE_TYPE (parm));
216 }
217 }
218 }
219
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),
225 "lambdas are implicitly %<constexpr%> only in C++17 and later");
226 }
227 else if (!DECL_CONSTRUCTOR_P (fun))
228 {
229 tree rettype = TREE_TYPE (TREE_TYPE (fun));
230 if (!literal_type_p (rettype))
231 {
232 ret = false;
233 if (complain)
234 {
235 auto_diagnostic_group d;
236 error ("invalid return type %qT of %<constexpr%> function %q+D",
237 rettype, fun);
238 explain_non_literal_class (rettype);
239 }
240 }
241
242 /* C++14 DR 1684 removed this restriction. */
243 if (cxx_dialect < cxx14
244 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
245 && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
246 {
247 ret = false;
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 }
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
275 static bool
276 build_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
327 static bool
328 build_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
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))
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 {
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;
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);
381 else
382 /* Don't add anything else to the CONSTRUCTOR. */
383 return true;
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);
417 if (TREE_CODE (aggr) == VAR_DECL)
418 /* Initializing a local variable, don't add anything. */
419 return true;
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
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);
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
445 static bool
446 check_constexpr_bind_expr_vars (tree t)
447 {
448 gcc_assert (TREE_CODE (t) == BIND_EXPR);
449
450 for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
451 if (TREE_CODE (var) == TYPE_DECL
452 && DECL_IMPLICIT_TYPEDEF_P (var)
453 && !LAMBDA_TYPE_P (TREE_TYPE (var)))
454 return false;
455 return true;
456 }
457
458 /* Subroutine of check_constexpr_ctor_body. */
459
460 static bool
461 check_constexpr_ctor_body_1 (tree last, tree list)
462 {
463 switch (TREE_CODE (list))
464 {
465 case DECL_EXPR:
466 if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
467 || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
468 return true;
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:
484 case DEBUG_BEGIN_STMT:
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
495 bool
496 check_constexpr_ctor_body (tree last, tree list, bool complain)
497 {
498 /* C++14 doesn't require a constexpr ctor to have an empty body. */
499 if (cxx_dialect >= cxx14)
500 return true;
501
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)
524 error ("%<constexpr%> constructor does not have empty body");
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
536 static vec<constructor_elt, va_gc> *
537 sort_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
572 static tree
573 build_constexpr_constructor_member_initializers (tree type, tree body)
574 {
575 vec<constructor_elt, va_gc> *vec = NULL;
576 bool ok = true;
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 ();
601 }
602 found:
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 }
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 }
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
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
662 static tree
663 get_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
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
677 tree
678 constexpr_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:
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 }
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:
724 case DEBUG_BEGIN_STMT:
725 return NULL_TREE;
726
727 case CALL_EXPR:
728 {
729 tree fun = get_function_named_in_call (body);
730 if (fun != NULL_TREE
731 && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
732 return NULL_TREE;
733 }
734 /* Fallthru. */
735
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
745 static tree
746 massage_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);
751 else if (cxx_dialect < cxx14)
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
762 /* CTYPE is a type constructed from BODY. Return true if some
763 bases/fields are uninitialized, and complain if COMPLAIN. */
764
765 static bool
766 cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
767 {
768 /* We allow uninitialized bases/fields in C++20. */
769 if (cxx_dialect >= cxx20)
770 return false;
771
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);
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
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)
798 {
799 tree index = NULL_TREE;
800 if (i < nelts)
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 }
808
809 for (; field != index; field = DECL_CHAIN (field))
810 {
811 tree ftype;
812 if (TREE_CODE (field) != FIELD_DECL)
813 continue;
814 if (DECL_UNNAMED_BIT_FIELD (field))
815 continue;
816 if (DECL_ARTIFICIAL (field))
817 continue;
818 if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
819 {
820 /* Recurse to check the anonymous aggregate member. */
821 bad |= cx_check_missing_mem_inits
822 (TREE_TYPE (field), NULL_TREE, complain);
823 if (bad && !complain)
824 return true;
825 continue;
826 }
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;
832 if (DECL_SIZE (field) && integer_zerop (DECL_SIZE (field)))
833 /* An empty field doesn't need an initializer. */
834 continue;
835 ftype = strip_array_types (ftype);
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;
847 auto_diagnostic_group d;
848 error ("member %qD must be initialized by mem-initializer "
849 "in %<constexpr%> constructor", field);
850 inform (DECL_SOURCE_LOCATION (field), "declared here");
851 bad = true;
852 }
853 if (field == NULL_TREE)
854 break;
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 }
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
876 tree
877 register_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
885 tree massaged = massage_constexpr_body (fun, body);
886 if (massaged == NULL_TREE || massaged == error_mark_node)
887 {
888 if (!DECL_CONSTRUCTOR_P (fun))
889 error ("body of %<constexpr%> function %qD not a return-statement",
890 fun);
891 return NULL;
892 }
893
894 bool potential = potential_rvalue_constant_expression (massaged);
895 if (!potential && !DECL_GENERATED_P (fun))
896 require_potential_rvalue_constant_expression (massaged);
897
898 if (DECL_CONSTRUCTOR_P (fun)
899 && cx_check_missing_mem_inits (DECL_CONTEXT (fun),
900 massaged, !DECL_GENERATED_P (fun)))
901 potential = false;
902
903 if (!potential && !DECL_GENERATED_P (fun))
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;
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;
922 slot = constexpr_fundef_table->find_slot (&entry, INSERT);
923 if (clear_ctx)
924 DECL_CONTEXT (DECL_RESULT (fun)) = NULL_TREE;
925
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
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
943 void
944 explain_invalid_constexpr_fn (tree fun)
945 {
946 static hash_set<tree> *diagnosed;
947 tree body;
948 location_t save_loc;
949 /* Only diagnose defaulted functions, lambdas, or instantiations. */
950 if (!DECL_DEFAULTED_FN (fun)
951 && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
952 && !is_instantiation_of_constexpr (fun))
953 {
954 inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
955 return;
956 }
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;
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,
970 "%qD is not usable as a %<constexpr%> function because:", fun);
971 }
972 /* First check the declaration. */
973 if (is_valid_constexpr_fn (fun, true))
974 {
975 /* Then if it's OK, the body. */
976 if (!DECL_DECLARED_CONSTEXPR_P (fun)
977 && DECL_DEFAULTED_FN (fun))
978 explain_implicit_non_constexpr (fun);
979 else
980 {
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);
986 require_potential_rvalue_constant_expression (body);
987 if (DECL_CONSTRUCTOR_P (fun))
988 cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
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
998 struct GTY((for_user)) constexpr_call {
999 /* Description of the constexpr function definition. */
1000 constexpr_fundef *fundef;
1001 /* Parameter bindings environment. A TREE_VEC of arguments. */
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;
1011 /* Whether __builtin_is_constant_evaluated() should evaluate to true. */
1012 bool manifestly_const_eval;
1013 };
1014
1015 struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1016 {
1017 static hashval_t hash (constexpr_call *);
1018 static bool equal (constexpr_call *, constexpr_call *);
1019 };
1020
1021 enum 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
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
1037 struct 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;
1048 /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1049 vec<tree> *cleanups;
1050 /* Number of heap VAR_DECL deallocations. */
1051 unsigned heap_dealloc_count;
1052 /* Constructor. */
1053 constexpr_global_ctx ()
1054 : constexpr_ops_count (0), cleanups (NULL), heap_dealloc_count (0) {}
1055 };
1056
1057 /* The constexpr expansion context. CALL is the current function
1058 expansion, CTOR is the current aggregate initializer, OBJECT is the
1059 object being initialized by CTOR, either a VAR_DECL or a _REF. */
1060
1061 struct constexpr_ctx {
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;
1065 /* The innermost call we're evaluating. */
1066 constexpr_call *call;
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. */
1069 vec<tree> *save_exprs;
1070 /* The CONSTRUCTOR we're currently building up for an aggregate
1071 initializer. */
1072 tree ctor;
1073 /* The object we're building the CONSTRUCTOR for. */
1074 tree object;
1075 /* If inside SWITCH_EXPR. */
1076 constexpr_switch_state *css_state;
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;
1080
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. */
1084 bool quiet;
1085 /* Whether we are strictly conforming to constant expression rules or
1086 trying harder to get a constant value. */
1087 bool strict;
1088 /* Whether __builtin_is_constant_evaluated () should be true. */
1089 bool manifestly_const_eval;
1090 };
1091
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
1096 static 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
1101 static 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
1106 static bool
1107 uid_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
1123 uid_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
1133 uid_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
1143 bool
1144 uid_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
1151 /* A table of all constexpr calls that have been evaluated by the
1152 compiler in this translation unit. */
1153
1154 static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1155
1156 static tree cxx_eval_constant_expression (const constexpr_ctx *, tree,
1157 bool, bool *, bool *, tree * = NULL);
1158
1159 /* Compute a hash value for a constexpr call representation. */
1160
1161 inline hashval_t
1162 constexpr_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
1171 bool
1172 constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1173 {
1174 if (lhs == rhs)
1175 return true;
1176 if (lhs->hash != rhs->hash)
1177 return false;
1178 if (lhs->manifestly_const_eval != rhs->manifestly_const_eval)
1179 return false;
1180 if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1181 return false;
1182 return cp_tree_equal (lhs->bindings, rhs->bindings);
1183 }
1184
1185 /* Initialize the constexpr call table, if needed. */
1186
1187 static void
1188 maybe_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
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
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.
1203
1204 This is not GC-deletable to avoid GC affecting UID generation. */
1205
1206 static GTY(()) decl_tree_map *fundef_copies_table;
1207
1208 /* Reuse a copy or create a new unshared copy of the function FUN.
1209 Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1210 is parms, TYPE is result. */
1211
1212 static tree
1213 get_fundef_copy (constexpr_fundef *fundef)
1214 {
1215 tree copy;
1216 bool existed;
1217 tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1218 (fundef_copies_table, fundef->decl, &existed, 127));
1219
1220 if (!existed)
1221 {
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. */
1225 copy = build_tree_list (fundef->body, fundef->parms);
1226 TREE_TYPE (copy) = fundef->result;
1227 }
1228 else if (*slot == NULL_TREE)
1229 {
1230 if (uid_sensitive_constexpr_evaluation_p ())
1231 return NULL_TREE;
1232
1233 /* We've already used the function itself, so make a copy. */
1234 copy = build_tree_list (NULL, NULL);
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;
1249 }
1250 else
1251 {
1252 /* We have a cached function available. */
1253 copy = *slot;
1254 *slot = TREE_CHAIN (copy);
1255 }
1256
1257 return copy;
1258 }
1259
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. */
1263
1264 static void
1265 save_fundef_copy (tree fun, tree copy)
1266 {
1267 tree *slot = fundef_copies_table->get (fun);
1268 TREE_CHAIN (copy) = *slot;
1269 *slot = copy;
1270 }
1271
1272 /* We have an expression tree T that represents a call, either CALL_EXPR
1273 or AGGR_INIT_EXPR. Return the Nth argument. */
1274
1275 static inline tree
1276 get_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
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
1296 static tree
1297 cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
1298 bool lval,
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;
1305
1306 /* Don't fold __builtin_constant_p within a constexpr function. */
1307 bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
1308
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. */
1311 if (bi_const_p
1312 && !ctx->manifestly_const_eval
1313 && current_function_decl
1314 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
1315 {
1316 *non_constant_p = true;
1317 return t;
1318 }
1319
1320 /* For __builtin_is_constant_evaluated, defer it if not
1321 ctx->manifestly_const_eval, otherwise fold it to true. */
1322 if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_CONSTANT_EVALUATED,
1323 BUILT_IN_FRONTEND))
1324 {
1325 if (!ctx->manifestly_const_eval)
1326 {
1327 *non_constant_p = true;
1328 return t;
1329 }
1330 return boolean_true_node;
1331 }
1332
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
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
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;
1366 for (i = 0; i < nargs; ++i)
1367 {
1368 tree arg = CALL_EXPR_ARG (t, i);
1369 tree oarg = arg;
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
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)
1387 || potential_constant_expression (arg))
1388 {
1389 bool dummy1 = false, dummy2 = false;
1390 arg = cxx_eval_constant_expression (&new_ctx, arg, false,
1391 &dummy1, &dummy2);
1392 }
1393
1394 if (bi_const_p)
1395 /* For __builtin_constant_p, fold all expressions with constant values
1396 even if they aren't C++ constant-expressions. */
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);
1404 else
1405 arg = oarg;
1406 }
1407
1408 args[i] = arg;
1409 }
1410
1411 bool save_ffbcp = force_folding_builtin_constant_p;
1412 force_folding_builtin_constant_p |= ctx->manifestly_const_eval;
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;
1419 new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
1420 CALL_EXPR_FN (t), nargs, args);
1421 current_function_decl = save_cur_fn;
1422 force_folding_builtin_constant_p = save_ffbcp;
1423 if (new_call == NULL)
1424 {
1425 if (!*non_constant_p && !ctx->quiet)
1426 {
1427 /* Do not allow__builtin_unreachable in constexpr function.
1428 The __builtin_unreachable call with BUILTINS_LOCATION
1429 comes from cp_maybe_instrument_return. */
1430 if (fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE)
1431 && EXPR_LOCATION (t) == BUILTINS_LOCATION)
1432 error ("%<constexpr%> call flows off the end of the function");
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 }
1439 }
1440 *non_constant_p = true;
1441 return t;
1442 }
1443
1444 if (!potential_constant_expression (new_call))
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
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
1464 return cxx_eval_constant_expression (&new_ctx, new_call, lval,
1465 non_constant_p, overflow_p);
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
1471 static tree
1472 adjust_temp_type (tree type, tree temp)
1473 {
1474 if (same_type_p (TREE_TYPE (temp), type))
1475 return temp;
1476 /* Avoid wrapping an aggregate value in a NOP_EXPR. */
1477 if (TREE_CODE (temp) == CONSTRUCTOR)
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 }
1484 if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
1485 return build0 (EMPTY_CLASS_EXPR, type);
1486 gcc_assert (scalarish_type_p (type));
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);
1490 }
1491
1492 /* If T is a CONSTRUCTOR, return an unshared copy of T and any
1493 sub-CONSTRUCTORs. Otherwise return T.
1494
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. */
1498
1499 tree
1500 unshare_constructor (tree t MEM_STAT_DECL)
1501 {
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 ();
1509 tree n = copy_node (*p PASS_MEM_STAT);
1510 CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
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 }
1518 return t;
1519 }
1520
1521 /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
1522
1523 static void
1524 free_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
1545 /* Subroutine of cxx_eval_call_expression.
1546 We are processing a call expression (either CALL_EXPR or
1547 AGGR_INIT_EXPR) in the context of CTX. Evaluate
1548 all arguments and bind their values to correspondings
1549 parameters, making up the NEW_CALL context. */
1550
1551 static void
1552 cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t,
1553 constexpr_call *new_call,
1554 bool *non_constant_p, bool *overflow_p,
1555 bool *non_constant_args)
1556 {
1557 const int nargs = call_expr_nargs (t);
1558 tree fun = new_call->fundef->decl;
1559 tree parms = new_call->fundef->parms;
1560 int i;
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);
1565 for (i = 0; i < nargs; ++i)
1566 {
1567 tree x, arg;
1568 tree type = parms ? TREE_TYPE (parms) : void_type_node;
1569 x = get_nth_callarg (t, i);
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
1572 which case we get the real argument from ctx. */
1573 if (i == 0 && DECL_CONSTRUCTOR_P (fun)
1574 && is_dummy_object (x))
1575 {
1576 x = ctx->object;
1577 x = build_address (x);
1578 }
1579 if (TREE_ADDRESSABLE (type))
1580 /* Undo convert_for_arg_passing work here. */
1581 x = convert_from_reference (x);
1582 arg = cxx_eval_constant_expression (ctx, x, /*lval=*/false,
1583 non_constant_p, overflow_p);
1584 /* Don't VERIFY_CONSTANT here. */
1585 if (*non_constant_p && ctx->quiet)
1586 return;
1587 /* Just discard ellipsis args after checking their constantitude. */
1588 if (!parms)
1589 continue;
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. */
1595 if (!TYPE_REF_P (type))
1596 arg = adjust_temp_type (type, arg);
1597 if (!TREE_CONSTANT (arg))
1598 *non_constant_args = true;
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 }
1619 TREE_VEC_ELT (binds, i) = arg;
1620 }
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. */
1629 static vec<tree> call_stack;
1630 static int call_stack_tick;
1631 static int last_cx_error_tick;
1632
1633 static int
1634 push_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);
1640 int len = call_stack.length ();
1641 if (len > max_constexpr_depth)
1642 return false;
1643 return len;
1644 }
1645
1646 static void
1647 pop_cx_call_context (void)
1648 {
1649 ++call_stack_tick;
1650 call_stack.pop ();
1651 }
1652
1653 vec<tree>
1654 cx_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
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
1668 static tree
1669 cxx_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:
1680 case IFN_FALLTHROUGH:
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
1693 case IFN_LAUNDER:
1694 return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
1695 false, non_constant_p, overflow_p);
1696
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
1711 default:
1712 if (!ctx->quiet)
1713 error_at (cp_expr_loc_or_input_loc (t),
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 {
1728 location_t loc = cp_expr_loc_or_input_loc (t);
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
1746 /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
1747
1748 static void
1749 clear_no_implicit_zero (tree ctor)
1750 {
1751 if (CONSTRUCTOR_NO_CLEARING (ctor))
1752 {
1753 CONSTRUCTOR_NO_CLEARING (ctor) = false;
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
1761 /* Complain about a const object OBJ being modified in a constant expression.
1762 EXPR is the MODIFY_EXPR expression performing the modification. */
1763
1764 static void
1765 modifying_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
1774 /* Return true if FNDECL is a replaceable global allocation function that
1775 should be useable during constant expression evaluation. */
1776
1777 static inline bool
1778 cxx_replaceable_global_alloc_fn (tree fndecl)
1779 {
1780 return (cxx_dialect >= cxx20
1781 && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
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
1790 static inline bool
1791 cxx_placement_new_fn (tree fndecl)
1792 {
1793 if (cxx_dialect >= cxx20
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
1809 static inline bool
1810 is_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");
1817 }
1818
1819 /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
1820
1821 static inline bool
1822 is_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
1841 /* Return true if FNDECL is __dynamic_cast. */
1842
1843 static inline bool
1844 cxx_dynamic_cast_fn_p (tree fndecl)
1845 {
1846 return (cxx_dialect >= cxx20
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
1854 static tree
1855 extract_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
1873 static tree
1874 get_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
1920 static tree
1921 cxx_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))
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 }
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
2086 /* Data structure used by replace_result_decl and replace_result_decl_r. */
2087
2088 struct 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
2100 static tree
2101 replace_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
2121 static bool
2122 replace_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
2132 /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
2133
2134 static tree
2135 cxx_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
2178 /* Subroutine of cxx_eval_constant_expression.
2179 Evaluate the call expression tree T in the context of OLD_CALL expression
2180 evaluation. */
2181
2182 static tree
2183 cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
2184 bool lval,
2185 bool *non_constant_p, bool *overflow_p)
2186 {
2187 /* Handle concept checks separately. */
2188 if (concept_check_p (t))
2189 return evaluate_concept_check (t, tf_warning_or_error);
2190
2191 location_t loc = cp_expr_loc_or_input_loc (t);
2192 tree fun = get_function_named_in_call (t);
2193 constexpr_call new_call
2194 = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval };
2195 int depth_ok;
2196
2197 if (fun == NULL_TREE)
2198 return cxx_eval_internal_function (ctx, t, lval,
2199 non_constant_p, overflow_p);
2200
2201 if (TREE_CODE (fun) != FUNCTION_DECL)
2202 {
2203 /* Might be a constexpr function pointer. */
2204 fun = cxx_eval_constant_expression (ctx, fun,
2205 /*lval*/false, non_constant_p,
2206 overflow_p);
2207 STRIP_NOPS (fun);
2208 if (TREE_CODE (fun) == ADDR_EXPR)
2209 fun = TREE_OPERAND (fun, 0);
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 }
2240 }
2241 if (TREE_CODE (fun) != FUNCTION_DECL)
2242 {
2243 if (!ctx->quiet && !*non_constant_p)
2244 error_at (loc, "expression %qE does not designate a %<constexpr%> "
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);
2251
2252 if (is_ubsan_builtin_p (fun))
2253 return void_node;
2254
2255 if (fndecl_built_in_p (fun))
2256 return cxx_eval_builtin_function_call (ctx, t, fun,
2257 lval, non_constant_p, overflow_p);
2258 if (DECL_THUNK_P (fun))
2259 return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p);
2260 if (!DECL_DECLARED_CONSTEXPR_P (fun))
2261 {
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))))
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);
2305 ctx->global->heap_dealloc_count++;
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 }
2324 /* Allow placement new in std::construct_at, just return the second
2325 argument. */
2326 if (TREE_CODE (t) == CALL_EXPR
2327 && cxx_placement_new_fn (fun)
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 }
2346 else if (cxx_dynamic_cast_fn_p (fun))
2347 return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p);
2348
2349 if (!ctx->quiet)
2350 {
2351 if (!lambda_static_thunk_p (fun))
2352 error_at (loc, "call to non-%<constexpr%> function %qD", fun);
2353 explain_invalid_constexpr_fn (fun);
2354 }
2355 *non_constant_p = true;
2356 return t;
2357 }
2358
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);
2367 CONSTRUCTOR_NO_CLEARING (ctor) = true;
2368 ctx->global->values.put (new_ctx.object, ctor);
2369 ctx = &new_ctx;
2370 }
2371
2372 /* Shortcut trivial constructor/op=. */
2373 if (trivial_fn_p (fun))
2374 {
2375 tree init = NULL_TREE;
2376 if (call_expr_nargs (t) == 2)
2377 init = convert_from_reference (get_nth_callarg (t, 1));
2378 else if (TREE_CODE (t) == AGGR_INIT_EXPR
2379 && AGGR_INIT_ZERO_FIRST (t))
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);
2389 new_ctx.call = &new_call;
2390 return cxx_eval_constant_expression (&new_ctx, set, lval,
2391 non_constant_p, overflow_p);
2392 }
2393 }
2394
2395 /* We can't defer instantiating the function any longer. */
2396 if (!DECL_INITIAL (fun)
2397 && DECL_TEMPLOID_INSTANTIATION (fun)
2398 && !uid_sensitive_constexpr_evaluation_p ())
2399 {
2400 location_t save_loc = input_location;
2401 input_location = loc;
2402 ++function_depth;
2403 instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
2404 --function_depth;
2405 input_location = save_loc;
2406 }
2407
2408 /* If in direct recursive call, optimize definition search. */
2409 if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
2410 new_call.fundef = ctx->call->fundef;
2411 else
2412 {
2413 new_call.fundef = retrieve_constexpr_fundef (fun);
2414 if (new_call.fundef == NULL || new_call.fundef->body == NULL
2415 || new_call.fundef->result == error_mark_node
2416 || fun == current_function_decl)
2417 {
2418 if (!ctx->quiet)
2419 {
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)
2426 error_at (loc, "%qD called in a constant expression before its "
2427 "definition is complete", fun);
2428 else if (DECL_INITIAL (fun))
2429 {
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);
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 }
2443
2444 bool non_constant_args = false;
2445 cxx_bind_parameters_in_call (ctx, t, &new_call,
2446 non_constant_p, overflow_p, &non_constant_args);
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. */
2451 class free_bindings
2452 {
2453 tree *bindings;
2454 public:
2455 free_bindings (tree &b): bindings (&b) { }
2456 ~free_bindings () { if (bindings) ggc_free (*bindings); }
2457 void preserve () { bindings = NULL; }
2458 } fb (new_call.bindings);
2459
2460 if (*non_constant_p)
2461 return t;
2462
2463 depth_ok = push_cx_call_context (t);
2464
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);
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 }
2491 }
2492
2493 tree result = NULL_TREE;
2494
2495 constexpr_call *entry = NULL;
2496 if (depth_ok && !non_constant_args && ctx->strict)
2497 {
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
2502 = iterative_hash_object (ctx->manifestly_const_eval, new_call.hash);
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 {
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 }
2520 }
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. */
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;
2534 }
2535
2536 if (!depth_ok)
2537 {
2538 if (!ctx->quiet)
2539 error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
2540 "%<-fconstexpr-depth=%> to increase the maximum)",
2541 max_constexpr_depth);
2542 *non_constant_p = true;
2543 result = error_mark_node;
2544 }
2545 else
2546 {
2547 bool cacheable = true;
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 }
2559 else if (tree copy = get_fundef_copy (new_call.fundef))
2560 {
2561 tree body, parms, res;
2562 releasing_vec ctors;
2563
2564 /* Reuse or create a new unshared copy of this function's body. */
2565 body = TREE_PURPOSE (copy);
2566 parms = TREE_VALUE (copy);
2567 res = TREE_TYPE (copy);
2568
2569 /* Associate the bindings with the remapped parms. */
2570 tree bound = new_call.bindings;
2571 tree remapped = parms;
2572 for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
2573 {
2574 tree arg = TREE_VEC_ELT (bound, i);
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. */
2580 arg = unshare_expr_without_location (arg);
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? */
2587 arg = unshare_constructor (arg);
2588 if (TREE_CODE (arg) == CONSTRUCTOR)
2589 vec_safe_push (ctors, arg);
2590 ctx->global->values.put (remapped, arg);
2591 remapped = DECL_CHAIN (remapped);
2592 }
2593 /* Add the RESULT_DECL to the values map, too. */
2594 gcc_assert (!DECL_BY_REFERENCE (res));
2595 ctx->global->values.put (res, NULL_TREE);
2596
2597 /* Track the callee's evaluated SAVE_EXPRs and TARGET_EXPRs so that
2598 we can forget their values after the call. */
2599 constexpr_ctx ctx_with_save_exprs = *ctx;
2600 auto_vec<tree, 10> save_exprs;
2601 ctx_with_save_exprs.save_exprs = &save_exprs;
2602 ctx_with_save_exprs.call = &new_call;
2603 unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
2604 unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
2605
2606 tree jump_target = NULL_TREE;
2607 cxx_eval_constant_expression (&ctx_with_save_exprs, body,
2608 lval, non_constant_p, overflow_p,
2609 &jump_target);
2610
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 {
2622 result = *ctx->global->values.get (res);
2623 if (result == NULL_TREE && !*non_constant_p)
2624 {
2625 if (!ctx->quiet)
2626 error ("%<constexpr%> call flows off the end "
2627 "of the function");
2628 *non_constant_p = true;
2629 }
2630 }
2631
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 {
2640 /* Subobjects might not be stored in ctx->global->values but we
2641 can get its CONSTRUCTOR by evaluating *this. */
2642 tree e = cxx_eval_constant_expression (ctx, new_obj,
2643 /*lval*/false,
2644 non_constant_p,
2645 overflow_p);
2646 if (TREE_CODE (e) == CONSTRUCTOR && !*non_constant_p)
2647 TREE_READONLY (e) = true;
2648 }
2649
2650 /* Forget the saved values of the callee's SAVE_EXPRs and
2651 TARGET_EXPRs. */
2652 unsigned int i;
2653 tree save_expr;
2654 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
2655 ctx->global->values.remove (save_expr);
2656
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? */
2661 ctx->global->values.remove (res);
2662 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
2663 ctx->global->values.remove (parm);
2664
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
2673 /* Make the unshared function copy we used available for re-use. */
2674 save_fundef_copy (fun, copy);
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 }
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;
2710 }
2711 else
2712 /* Couldn't get a function copy to evaluate. */
2713 *non_constant_p = true;
2714
2715 if (result == error_mark_node)
2716 *non_constant_p = true;
2717 if (*non_constant_p || *overflow_p)
2718 result = error_mark_node;
2719 else if (!result)
2720 result = void_node;
2721 if (entry)
2722 entry->result = cacheable ? result : error_mark_node;
2723 }
2724
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
2733 && (cxx_dialect < cxx20
2734 || !DECL_CONSTRUCTOR_P (fun)))
2735 clear_no_implicit_zero (result);
2736
2737 pop_cx_call_context ();
2738 return result;
2739 }
2740
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. */
2745
2746 bool
2747 reduced_constant_expression_p (tree t)
2748 {
2749 if (t == NULL_TREE)
2750 return false;
2751
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. */
2760 tree idx, val, field; unsigned HOST_WIDE_INT i;
2761 if (CONSTRUCTOR_NO_CLEARING (t))
2762 {
2763 if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE)
2764 /* An initialized vector would have a VECTOR_CST. */
2765 return false;
2766 else if (cxx_dialect >= cxx20
2767 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */
2768 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
2769 field = NULL_TREE;
2770 else if (cxx_dialect >= cxx20
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 }
2779 else
2780 field = next_initializable_field (TYPE_FIELDS (TREE_TYPE (t)));
2781 }
2782 else
2783 field = NULL_TREE;
2784 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val)
2785 {
2786 /* If VAL is null, we're in the middle of initializing this
2787 element. */
2788 if (!reduced_constant_expression_p (val))
2789 return false;
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;
2796 if (field)
2797 field = next_initializable_field (DECL_CHAIN (field));
2798 }
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))
2804 /* All the fields are initialized. */
2805 CONSTRUCTOR_NO_CLEARING (t) = false;
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
2815 themselves, such as 1/0. Call this function to check for that
2816 condition.
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
2823 static bool
2824 verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
2825 bool *overflow_p)
2826 {
2827 if (!*non_constant_p && !reduced_constant_expression_p (t)
2828 && t != void_node)
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
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
2853 static bool
2854 cxx_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)
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);
2874 }
2875 if (compare_tree_int (rhs, uprec) >= 0)
2876 {
2877 if (!ctx->quiet)
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);
2881 return (!flag_permissive || ctx->quiet);
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;
2888 otherwise, the behavior is undefined.
2889 For C++20:
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
2895 && cxx_dialect < cxx20)
2896 {
2897 if (tree_int_cst_sgn (lhs) == -1)
2898 {
2899 if (!ctx->quiet)
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);
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)
2920 permerror (loc, "shift expression %q+E overflows",
2921 build2_loc (loc, code, type, lhs, rhs));
2922 return (!flag_permissive || ctx->quiet);
2923 }
2924 }
2925 return false;
2926 }
2927
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
2933 static tree
2934 cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
2935 bool /*lval*/,
2936 bool *non_constant_p, bool *overflow_p)
2937 {
2938 tree r;
2939 tree orig_arg = TREE_OPERAND (t, 0);
2940 tree arg = cxx_eval_constant_expression (ctx, orig_arg, /*lval*/false,
2941 non_constant_p, overflow_p);
2942 VERIFY_CONSTANT (arg);
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 }
2954 VERIFY_CONSTANT (r);
2955 return r;
2956 }
2957
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
2962 static tree
2963 cxx_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
3015 /* Subroutine of cxx_eval_constant_expression.
3016 Like cxx_eval_unary_expression, except for binary expressions. */
3017
3018 static tree
3019 cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
3020 bool lval,
3021 bool *non_constant_p, bool *overflow_p)
3022 {
3023 tree r = NULL_TREE;
3024 tree orig_lhs = TREE_OPERAND (t, 0);
3025 tree orig_rhs = TREE_OPERAND (t, 1);
3026 tree lhs, rhs;
3027 lhs = cxx_eval_constant_expression (ctx, orig_lhs, /*lval*/false,
3028 non_constant_p, overflow_p);
3029 /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
3030 subtraction. */
3031 if (*non_constant_p)
3032 return t;
3033 rhs = cxx_eval_constant_expression (ctx, orig_rhs, /*lval*/false,
3034 non_constant_p, overflow_p);
3035 if (*non_constant_p)
3036 return t;
3037
3038 location_t loc = EXPR_LOCATION (t);
3039 enum tree_code code = TREE_CODE (t);
3040 tree type = TREE_TYPE (t);
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)
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 }
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);
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);
3073 }
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);
3079 *non_constant_p = true;
3080 return t;
3081 }
3082 else if (code == POINTER_PLUS_EXPR)
3083 r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
3084 overflow_p);
3085 else if (code == SPACESHIP_EXPR)
3086 {
3087 r = genericize_spaceship (type, lhs, rhs);
3088 return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
3089 overflow_p);
3090 }
3091
3092 if (r == NULL_TREE)
3093 r = fold_binary_loc (loc, code, type, lhs, rhs);
3094
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 }
3102 else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
3103 *non_constant_p = true;
3104 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
3105 a local array in a constexpr function. */
3106 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
3107 if (!ptr)
3108 VERIFY_CONSTANT (r);
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
3116 static tree
3117 cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
3118 bool lval,
3119 bool *non_constant_p, bool *overflow_p,
3120 tree *jump_target)
3121 {
3122 tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3123 /*lval*/false,
3124 non_constant_p, overflow_p);
3125 VERIFY_CONSTANT (val);
3126 /* Don't VERIFY_CONSTANT the other operands. */
3127 if (integer_zerop (val))
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);
3135 }
3136
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
3144 static tree
3145 cxx_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
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
3179 static int
3180 array_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
3207 static HOST_WIDE_INT
3208 find_array_ctor_elt (tree ary, tree dindex, bool insert)
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 {
3224 tree cindex = (*elts)[end - 1].index;
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)
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;
3263 constructor_elt &elt = (*elts)[middle];
3264 tree idx = elt.index;
3265
3266 int cmp = array_index_cmp (dindex, idx);
3267 if (cmp < 0)
3268 end = middle;
3269 else if (cmp > 0)
3270 begin = middle + 1;
3271 else
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);
3279 tree value = elt.value;
3280 dindex = fold_convert (sizetype, dindex);
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;
3294 e.value = unshare_constructor (value);
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);
3310 e.value = unshare_constructor (value);
3311 vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
3312 }
3313 }
3314 return middle;
3315 }
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
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
3334 static constructor_elt *
3335 get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
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 {
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
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
3433 /* Under the control of CTX, issue a detailed diagnostic for
3434 an out-of-bounds subscript INDEX into the expression ARRAY. */
3435
3436 static void
3437 diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
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);
3446 STRIP_ANY_LOCATION_WRAPPER (array);
3447 if (DECL_P (array))
3448 {
3449 if (TYPE_DOMAIN (arraytype))
3450 error_at (loc, "array subscript value %qE is outside the bounds "
3451 "of array %qD of type %qT", sidx, array, arraytype);
3452 else
3453 error_at (loc, "nonzero array subscript %qE is used with array %qD of "
3454 "type %qT with unknown bounds", sidx, array, arraytype);
3455 inform (DECL_SOURCE_LOCATION (array), "declared here");
3456 }
3457 else if (TYPE_DOMAIN (arraytype))
3458 error_at (loc, "array subscript value %qE is outside the bounds "
3459 "of array type %qT", sidx, arraytype);
3460 else
3461 error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
3462 "with unknown bounds", sidx, arraytype);
3463 }
3464 }
3465
3466 /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
3467 a VECTOR_TYPE). */
3468
3469 static tree
3470 get_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
3492 /* Extract element INDEX consisting of CHARS_PER_ELT chars from
3493 STRING_CST STRING. */
3494
3495 static tree
3496 extract_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
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
3516 static tree
3517 eval_and_check_array_index (const constexpr_ctx *ctx,
3518 tree t, bool allow_one_past,
3519 bool *non_constant_p, bool *overflow_p)
3520 {
3521 location_t loc = cp_expr_loc_or_input_loc (t);
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
3551 /* Subroutine of cxx_eval_constant_expression.
3552 Attempt to reduce a reference to an array slot. */
3553
3554 static tree
3555 cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
3556 bool lval,
3557 bool *non_constant_p, bool *overflow_p)
3558 {
3559 tree oldary = TREE_OPERAND (t, 0);
3560 tree ary = cxx_eval_constant_expression (ctx, oldary,
3561 lval,
3562 non_constant_p, overflow_p);
3563 if (*non_constant_p)
3564 return t;
3565 if (!lval
3566 && TREE_CODE (ary) == VIEW_CONVERT_EXPR
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);
3570
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;
3576
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
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
3603 bool found;
3604 HOST_WIDE_INT i = 0;
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;
3611 }
3612 else
3613 {
3614 i = tree_to_shwi (index);
3615 found = (i < len);
3616 }
3617
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);
3625 else
3626 r = extract_string_elt (ary, elem_nchars, i);
3627
3628 if (r)
3629 /* Don't VERIFY_CONSTANT here. */
3630 return r;
3631
3632 /* Otherwise the element doesn't have a value yet. */
3633 }
3634
3635 /* Not found. */
3636
3637 if (TREE_CODE (ary) == CONSTRUCTOR
3638 && CONSTRUCTOR_NO_CLEARING (ary))
3639 {
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;
3647 }
3648
3649 /* If it's within the array bounds but doesn't have an explicit
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);
3660 return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
3661 overflow_p);
3662 }
3663
3664 /* Subroutine of cxx_eval_constant_expression.
3665 Attempt to reduce a field access of a value of class type. */
3666
3667 static tree
3668 cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
3669 bool lval,
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);
3677 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
3678 lval,
3679 non_constant_p, overflow_p);
3680 if (INDIRECT_REF_P (whole)
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 }
3688
3689 if (TREE_CODE (whole) == PTRMEM_CST)
3690 whole = cplus_expand_constant (whole);
3691 if (whole == orig_whole)
3692 return t;
3693 if (lval)
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 {
3700 if (!ctx->quiet)
3701 error ("%qE is not a constant expression", orig_whole);
3702 *non_constant_p = true;
3703 }
3704 if (DECL_MUTABLE_P (part))
3705 {
3706 if (!ctx->quiet)
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;
3712 bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
3713 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
3714 {
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)
3719 {
3720 if (value)
3721 {
3722 STRIP_ANY_LOCATION_WRAPPER (value);
3723 return value;
3724 }
3725 else
3726 /* We're in the middle of initializing it. */
3727 break;
3728 }
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. */
3734 if (!ctx->quiet)
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 }
3743 *non_constant_p = true;
3744 return t;
3745 }
3746
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. */
3749 if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
3750 return build_constructor (TREE_TYPE (t), NULL);
3751
3752 gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
3753
3754 if (CONSTRUCTOR_NO_CLEARING (whole))
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
3758 error. */
3759 if (!ctx->quiet)
3760 error ("accessing uninitialized member %qD", part);
3761 *non_constant_p = true;
3762 return t;
3763 }
3764
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);
3767 return cxx_eval_constant_expression (ctx, value,
3768 lval,
3769 non_constant_p, overflow_p);
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
3776 static tree
3777 cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
3778 bool lval,
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;
3785 tree whole = cxx_eval_constant_expression (ctx, orig_whole,
3786 lval,
3787 non_constant_p, overflow_p);
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 {
3799 if (!ctx->quiet)
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);
3820 STRIP_ANY_LOCATION_WRAPPER (value);
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
3862 static tree
3863 cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
3864 tree bailout_value, tree continue_value,
3865 bool lval,
3866 bool *non_constant_p, bool *overflow_p)
3867 {
3868 tree r;
3869 tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
3870 lval,
3871 non_constant_p, overflow_p);
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));
3876 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
3877 lval, non_constant_p,
3878 overflow_p);
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
3888 static constructor_elt *
3889 base_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
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
3917 static tree
3918 initialized_type (tree t)
3919 {
3920 if (TYPE_P (t))
3921 return t;
3922 tree type = TREE_TYPE (t);
3923 if (TREE_CODE (t) == CALL_EXPR)
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 }
3931 else if (TREE_CODE (t) == COMPOUND_EXPR)
3932 return initialized_type (TREE_OPERAND (t, 1));
3933 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
3934 type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
3935 return cv_unqualified (type);
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
3943 static void
3944 init_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);
3965 CONSTRUCTOR_NO_CLEARING (elt) = true;
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
3976 static void
3977 verify_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)));
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. */
3991 if (ctx->object)
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 }
4002 gcc_assert (!ctx->object || !DECL_P (ctx->object)
4003 || *(ctx->global->values.get (ctx->object)) == ctx->ctor);
4004 }
4005
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
4010 static tree
4011 cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
4012 bool lval,
4013 bool *non_constant_p, bool *overflow_p)
4014 {
4015 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
4016 bool changed = false;
4017 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
4018 tree type = TREE_TYPE (t);
4019
4020 constexpr_ctx new_ctx;
4021 if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
4022 {
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. */
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);
4031 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
4032 vec_alloc (*p, vec_safe_length (v));
4033
4034 if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
4035 CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
4036
4037 unsigned i;
4038 tree index, value;
4039 bool constant_p = true;
4040 bool side_effects_p = false;
4041 FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
4042 {
4043 tree orig_value = value;
4044 init_subob_ctx (ctx, new_ctx, index, value);
4045 int pos_hint = -1;
4046 if (new_ctx.ctor != ctx->ctor)
4047 {
4048 /* If we built a new CONSTRUCTOR, attach it now so that other
4049 initializers can refer to it. */
4050 constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
4051 cep->value = new_ctx.ctor;
4052 pos_hint = cep - (*p)->begin();
4053 }
4054 else if (TREE_CODE (type) == UNION_TYPE)
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. */
4058 get_or_insert_ctor_field (ctx->ctor, index);
4059 tree elt = cxx_eval_constant_expression (&new_ctx, value,
4060 lval,
4061 non_constant_p, overflow_p);
4062 /* Don't VERIFY_CONSTANT here. */
4063 if (ctx->quiet && *non_constant_p)
4064 break;
4065 if (elt != orig_value)
4066 changed = true;
4067
4068 if (!TREE_CONSTANT (elt))
4069 constant_p = false;
4070 if (TREE_SIDE_EFFECTS (elt))
4071 side_effects_p = true;
4072 if (index && TREE_CODE (index) == COMPONENT_REF)
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. */
4077 constructor_elt *inner = base_field_constructor_elt (*p, index);
4078 inner->value = elt;
4079 changed = true;
4080 }
4081 else if (index
4082 && (TREE_CODE (index) == NOP_EXPR
4083 || TREE_CODE (index) == POINTER_PLUS_EXPR))
4084 {
4085 /* This is an initializer for an empty base; now that we've
4086 checked that it's constant, we can ignore it. */
4087 gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
4088 changed = true;
4089 }
4090 else
4091 {
4092 if (TREE_CODE (type) == UNION_TYPE
4093 && (*p)->last().index != index)
4094 /* The initializer erroneously changed the active union member that
4095 we're initializing. */
4096 gcc_assert (*non_constant_p);
4097 else
4098 {
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;
4104 }
4105
4106 /* Adding or replacing an element might change the ctor's flags. */
4107 TREE_CONSTANT (ctx->ctor) = constant_p;
4108 TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
4109 }
4110 }
4111 if (*non_constant_p || !changed)
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. */
4116 CONSTRUCTOR_NO_CLEARING (t) = false;
4117 TREE_CONSTANT (t) = constant_p;
4118 TREE_SIDE_EFFECTS (t) = side_effects_p;
4119 if (VECTOR_TYPE_P (type))
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
4136 static tree
4137 cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
4138 bool value_init, bool lval,
4139 bool *non_constant_p, bool *overflow_p)
4140 {
4141 tree elttype = TREE_TYPE (atype);
4142 verify_ctor_sanity (ctx, atype);
4143 vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
4144 bool pre_init = false;
4145 unsigned HOST_WIDE_INT i;
4146 tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
4147
4148 if (init && TREE_CODE (init) == CONSTRUCTOR)
4149 return cxx_eval_bare_aggregate (ctx, init, lval,
4150 non_constant_p, overflow_p);
4151
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 {
4161 init = build_value_init (elttype, complain);
4162 pre_init = true;
4163 }
4164 else if (!init)
4165 {
4166 releasing_vec argvec;
4167 init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
4168 &argvec, elttype, LOOKUP_NORMAL,
4169 complain);
4170 init = build_aggr_init_expr (elttype, init);
4171 pre_init = true;
4172 }
4173
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
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);
4189 for (i = 0; i < max; ++i)
4190 {
4191 tree idx = build_int_cst (size_type_node, i);
4192 tree eltinit;
4193 bool reuse = false;
4194 constexpr_ctx new_ctx;
4195 init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
4196 if (new_ctx.ctor != ctx->ctor)
4197 {
4198 if (zeroed_out)
4199 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
4200 CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
4201 }
4202 if (TREE_CODE (elttype) == ARRAY_TYPE)
4203 {
4204 /* A multidimensional array; recurse. */
4205 if (value_init || init == NULL_TREE)
4206 {
4207 eltinit = NULL_TREE;
4208 reuse = i == 0;
4209 }
4210 else
4211 eltinit = cp_build_array_ref (input_location, init, idx, complain);
4212 eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit, value_init,
4213 lval,
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. */
4220 if (init == void_node)
4221 /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
4222 return ctx->ctor;
4223 eltinit = cxx_eval_constant_expression (&new_ctx, init, lval,
4224 non_constant_p, overflow_p);
4225 reuse = i == 0;
4226 }
4227 else
4228 {
4229 /* Copying an element. */
4230 gcc_assert (same_type_ignoring_top_level_qualifiers_p
4231 (atype, TREE_TYPE (init)));
4232 eltinit = cp_build_array_ref (input_location, init, idx, complain);
4233 if (!lvalue_p (init))
4234 eltinit = move (eltinit);
4235 eltinit = force_rvalue (eltinit, complain);
4236 eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
4237 non_constant_p, overflow_p);
4238 }
4239 if (*non_constant_p && !ctx->quiet)
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);
4249 /* Reuse the result of cxx_eval_constant_expression call
4250 from the first iteration to all others if it is a constant
4251 initializer that doesn't require relocations. */
4252 if (reuse
4253 && max > 1
4254 && (eltinit == NULL_TREE
4255 || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
4256 == null_pointer_node)))
4257 {
4258 if (new_ctx.ctor != ctx->ctor)
4259 eltinit = new_ctx.ctor;
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));
4264 break;
4265 }
4266 else if (i == 0)
4267 vec_safe_reserve (*p, max);
4268 }
4269
4270 if (!*non_constant_p)
4271 {
4272 init = ctx->ctor;
4273 CONSTRUCTOR_NO_CLEARING (init) = false;
4274 }
4275 return init;
4276 }
4277
4278 static tree
4279 cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
4280 bool lval,
4281 bool *non_constant_p, bool *overflow_p)
4282 {
4283 tree atype = TREE_TYPE (t);
4284 tree init = VEC_INIT_EXPR_INIT (t);
4285 tree r = cxx_eval_vec_init_1 (ctx, atype, init,
4286 VEC_INIT_EXPR_VALUE_INIT (t),
4287 lval, non_constant_p, overflow_p);
4288 if (*non_constant_p)
4289 return t;
4290 else
4291 return r;
4292 }
4293
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
4298 static bool
4299 same_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
4311 /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
4312
4313 static tree
4314 cxx_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
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
4415 with TBAA in fold_indirect_ref_1. */
4416
4417 static tree
4418 cxx_fold_indirect_ref (location_t loc, tree type, tree op0, bool *empty_base)
4419 {
4420 tree sub = op0;
4421 tree subtype;
4422 poly_uint64 const_op01;
4423
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
4434 subtype = TREE_TYPE (sub);
4435 if (!INDIRECT_TYPE_P (subtype))
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. */
4447 if (similar_type_p (optype, type))
4448 {
4449 tree fop = fold_read_from_constant_string (op);
4450 if (fop)
4451 return fop;
4452 else
4453 return op;
4454 }
4455 else
4456 return cxx_fold_indirect_ref_1 (loc, type, op, 0, empty_base);
4457 }
4458 else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
4459 && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
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)
4466 return cxx_fold_indirect_ref_1 (loc, type, TREE_OPERAND (op00, 0),
4467 tree_to_uhwi (op01), empty_base);
4468 }
4469 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
4470 else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
4471 && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
4472 {
4473 tree type_domain;
4474 tree min_val = size_zero_node;
4475 tree newsub
4476 = cxx_fold_indirect_ref (loc, TREE_TYPE (subtype), sub, NULL);
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
4491 static tree
4492 cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
4493 bool lval,
4494 bool *non_constant_p, bool *overflow_p)
4495 {
4496 tree orig_op0 = TREE_OPERAND (t, 0);
4497 bool empty_base = false;
4498
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
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)
4514 {
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
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
4531 r = cxx_fold_indirect_ref (EXPR_LOCATION (t), TREE_TYPE (t), op0,
4532 &empty_base);
4533 if (r == NULL_TREE)
4534 {
4535 /* We couldn't fold to a constant value. Make sure it's not
4536 something we should have been able to fold. */
4537 tree sub = op0;
4538 STRIP_NOPS (sub);
4539 if (TREE_CODE (sub) == ADDR_EXPR)
4540 {
4541 gcc_assert (!similar_type_p
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)
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));
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);
4557 return t;
4558 }
4559 }
4560
4561 r = cxx_eval_constant_expression (ctx, r,
4562 lval, non_constant_p, overflow_p);
4563 if (*non_constant_p)
4564 return t;
4565
4566 /* If we're pulling out the value of an empty base, just return an empty
4567 CONSTRUCTOR. */
4568 if (empty_base && !lval)
4569 {
4570 r = build_constructor (TREE_TYPE (t), NULL);
4571 TREE_CONSTANT (r) = true;
4572 }
4573
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
4581 static void
4582 non_const_var_error (location_t loc, tree r)
4583 {
4584 auto_diagnostic_group d;
4585 tree type = TREE_TYPE (r);
4586 if (DECL_NAME (r) == heap_uninit_identifier
4587 || DECL_NAME (r) == heap_identifier)
4588 {
4589 error_at (loc, "the content of uninitialized storage is not usable "
4590 "in a constant expression");
4591 inform (DECL_SOURCE_LOCATION (r), "allocated here");
4592 return;
4593 }
4594 if (DECL_NAME (r) == heap_deleted_identifier)
4595 {
4596 error_at (loc, "use of allocated storage after deallocation in a "
4597 "constant expression");
4598 inform (DECL_SOURCE_LOCATION (r), "allocated here");
4599 return;
4600 }
4601 error_at (loc, "the value of %qD is not usable in a constant "
4602 "expression", r);
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)
4618 || !TREE_CONSTANT (DECL_INITIAL (r))
4619 || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
4620 inform (DECL_SOURCE_LOCATION (r),
4621 "%qD was not initialized with a constant "
4622 "expression", r);
4623 else
4624 gcc_unreachable ();
4625 }
4626 else if (TYPE_REF_P (type))
4627 inform (DECL_SOURCE_LOCATION (r),
4628 "%qD was not initialized with a constant "
4629 "expression", r);
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
4645 static tree
4646 cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
4647 bool lval,
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 {
4656 args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
4657 lval,
4658 non_constant_p, overflow_p);
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
4670 /* True if T was declared in a function declared to be constexpr, and
4671 therefore potentially constant in C++14. */
4672
4673 bool
4674 var_in_constexpr_fn (tree t)
4675 {
4676 tree ctx = DECL_CONTEXT (t);
4677 return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
4678 && DECL_DECLARED_CONSTEXPR_P (ctx));
4679 }
4680
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
4684 bool
4685 var_in_maybe_constexpr_fn (tree t)
4686 {
4687 if (cxx_dialect >= cxx17
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
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
4700 static void
4701 maybe_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
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
4718 static bool
4719 cref_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
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
4735 static bool
4736 modifying_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
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;
4764 }
4765
4766 /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
4767
4768 static tree
4769 cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
4770 bool lval,
4771 bool *non_constant_p, bool *overflow_p)
4772 {
4773 constexpr_ctx new_ctx = *ctx;
4774
4775 tree init = TREE_OPERAND (t, 1);
4776 if (TREE_CLOBBER_P (init))
4777 /* Just ignore clobbers. */
4778 return void_node;
4779
4780 /* First we figure out where we're storing to. */
4781 tree target = TREE_OPERAND (t, 0);
4782
4783 maybe_simplify_trivial_copy (target, init);
4784
4785 tree type = TREE_TYPE (target);
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 }
4798
4799 bool evaluated = false;
4800 if (lval)
4801 {
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;
4810 }
4811
4812 /* Find the underlying variable. */
4813 releasing_vec refs;
4814 tree object = NULL_TREE;
4815 /* If we're modifying a const object, save it. */
4816 tree const_object_being_modified = NULL_TREE;
4817 bool mutable_p = false;
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:
4825 {
4826 tree ob = TREE_OPERAND (probe, 0);
4827 tree elt = TREE_OPERAND (probe, 1);
4828 if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
4829 mutable_p = true;
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 }
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;
4846 vec_safe_push (refs, elt);
4847 vec_safe_push (refs, TREE_TYPE (probe));
4848 probe = ob;
4849 }
4850 break;
4851
4852 default:
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;
4864 }
4865 }
4866
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
4871 /* And then find/build up our initializer for the path to the subobject
4872 we're initializing. */
4873 tree *valp;
4874 if (DECL_P (object))
4875 valp = ctx->global->values.get (object);
4876 else
4877 valp = NULL;
4878 if (!valp)
4879 {
4880 /* A constant-expression cannot modify objects from outside the
4881 constant-expression. */
4882 if (!ctx->quiet)
4883 error ("modification of %qE is not a constant expression", object);
4884 *non_constant_p = true;
4885 return t;
4886 }
4887 type = TREE_TYPE (object);
4888 bool no_zero_init = true;
4889
4890 releasing_vec ctors, indexes;
4891 auto_vec<int> index_pos_hints;
4892 bool activated_union_member_p = false;
4893 while (!refs->is_empty ())
4894 {
4895 if (*valp == NULL_TREE)
4896 {
4897 *valp = build_constructor (type, NULL);
4898 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
4899 }
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
4929 /* If the value of object is already zero-initialized, any new ctors for
4930 subobjects will also be zero-initialized. */
4931 no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
4932
4933 enum tree_code code = TREE_CODE (type);
4934 type = refs->pop();
4935 tree index = refs->pop();
4936
4937 if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
4938 && CONSTRUCTOR_ELT (*valp, 0)->index != index)
4939 {
4940 if (cxx_dialect < cxx20)
4941 {
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;
4949 }
4950 else if (TREE_CODE (t) == MODIFY_EXPR
4951 && CONSTRUCTOR_NO_CLEARING (*valp))
4952 {
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;
4962 }
4963 no_zero_init = true;
4964 }
4965
4966 vec_safe_push (ctors, *valp);
4967 vec_safe_push (indexes, index);
4968
4969 constructor_elt *cep
4970 = get_or_insert_ctor_field (*valp, index);
4971 index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
4972
4973 if (code == UNION_TYPE)
4974 activated_union_member_p = true;
4975
4976 valp = &cep->value;
4977 }
4978
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;
5007 tree const_objtype
5008 = strip_array_types (TREE_TYPE (const_object_being_modified));
5009 if (!CLASS_TYPE_P (const_objtype))
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
5037 if (!preeval)
5038 {
5039 /* Create a new CONSTRUCTOR in case evaluation of the initializer
5040 wants to modify it. */
5041 if (*valp == NULL_TREE)
5042 {
5043 *valp = build_constructor (type, NULL);
5044 CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
5045 }
5046 new_ctx.ctor = *valp;
5047 new_ctx.object = target;
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;
5054 init = cxx_eval_constant_expression (&new_ctx, init, false,
5055 non_constant_p, overflow_p);
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 }
5066 }
5067
5068 /* Don't share a CONSTRUCTOR that might be changed later. */
5069 init = unshare_constructor (init);
5070
5071 if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
5072 && TREE_CODE (init) == CONSTRUCTOR)
5073 {
5074 /* An outer ctx->ctor might be pointing to *valp, so replace
5075 its contents. */
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 }
5087 CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
5088 TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
5089 TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
5090 CONSTRUCTOR_NO_CLEARING (*valp)
5091 = CONSTRUCTOR_NO_CLEARING (init);
5092 }
5093 else
5094 *valp = init;
5095
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))
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 }
5114
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);
5121 if (!c || s || activated_union_member_p)
5122 FOR_EACH_VEC_ELT (*ctors, i, elt)
5123 {
5124 if (!c)
5125 TREE_CONSTANT (elt) = false;
5126 if (s)
5127 TREE_SIDE_EFFECTS (elt) = true;
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;
5132 }
5133
5134 if (*non_constant_p)
5135 return t;
5136 else if (lval)
5137 return target;
5138 else
5139 return init;
5140 }
5141
5142 /* Evaluate a ++ or -- expression. */
5143
5144 static tree
5145 cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
5146 bool lval,
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
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
5159 /* The operand as an lvalue. */
5160 op = cxx_eval_constant_expression (ctx, op, true,
5161 non_constant_p, overflow_p);
5162
5163 /* The operand as an rvalue. */
5164 tree val
5165 = cxx_eval_constant_expression (ctx, op, false,
5166 non_constant_p, overflow_p);
5167 /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5168 a local array in a constexpr function. */
5169 bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
5170 if (!ptr)
5171 VERIFY_CONSTANT (val);
5172
5173 /* The modified value. */
5174 bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
5175 tree mod;
5176 if (INDIRECT_TYPE_P (type))
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);
5186 if (!ptr)
5187 VERIFY_CONSTANT (mod);
5188
5189 /* Storing the modified value. */
5190 tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
5191 MODIFY_EXPR, type, op, mod);
5192 cxx_eval_constant_expression (ctx, store,
5193 true, non_constant_p, overflow_p);
5194 ggc_free (store);
5195
5196 /* And the value of the expression. */
5197 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
5198 {
5199 /* Prefix ops are lvalues. */
5200 if (lval)
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
5211 /* Predicates for the meaning of *jump_target. */
5212
5213 static bool
5214 returns (tree *jump_target)
5215 {
5216 return *jump_target
5217 && (TREE_CODE (*jump_target) == RETURN_EXPR
5218 || (TREE_CODE (*jump_target) == LABEL_DECL
5219 && LABEL_DECL_CDTOR (*jump_target)));
5220 }
5221
5222 static bool
5223 breaks (tree *jump_target)
5224 {
5225 return *jump_target
5226 && ((TREE_CODE (*jump_target) == LABEL_DECL
5227 && LABEL_DECL_BREAK (*jump_target))
5228 || TREE_CODE (*jump_target) == BREAK_STMT
5229 || TREE_CODE (*jump_target) == EXIT_EXPR);
5230 }
5231
5232 static bool
5233 continues (tree *jump_target)
5234 {
5235 return *jump_target
5236 && ((TREE_CODE (*jump_target) == LABEL_DECL
5237 && LABEL_DECL_CONTINUE (*jump_target))
5238 || TREE_CODE (*jump_target) == CONTINUE_STMT);
5239
5240 }
5241
5242 static bool
5243 switches (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
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. */
5252
5253 static bool
5254 label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
5255 {
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 {
5267 gcc_assert (ctx->css_state != NULL);
5268 if (!CASE_LOW (stmt))
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 }
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 }
5285 else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
5286 return true;
5287 }
5288 break;
5289
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
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
5305 static tree
5306 cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
5307 bool *non_constant_p, bool *overflow_p,
5308 tree *jump_target)
5309 {
5310 tree_stmt_iterator i;
5311 tree local_target;
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;
5315 if (!jump_target)
5316 {
5317 local_target = NULL_TREE;
5318 jump_target = &local_target;
5319 }
5320 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
5321 {
5322 tree stmt = tsi_stmt (i);
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 }
5333 if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
5334 continue;
5335 r = cxx_eval_constant_expression (ctx, stmt, false,
5336 non_constant_p, overflow_p,
5337 jump_target);
5338 if (*non_constant_p)
5339 break;
5340 if (returns (jump_target) || breaks (jump_target))
5341 break;
5342 }
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)
5348 error_at (cp_expr_loc_or_input_loc (r),
5349 "statement is not a constant expression");
5350 *non_constant_p = true;
5351 }
5352 return r;
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
5358 static tree
5359 cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
5360 bool *non_constant_p, bool *overflow_p,
5361 tree *jump_target)
5362 {
5363 constexpr_ctx new_ctx = *ctx;
5364 tree local_target;
5365 if (!jump_target)
5366 {
5367 local_target = NULL_TREE;
5368 jump_target = &local_target;
5369 }
5370
5371 tree body, cond = NULL_TREE, expr = NULL_TREE;
5372 int count = 0;
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 }
5401 auto_vec<tree, 10> save_exprs;
5402 new_ctx.save_exprs = &save_exprs;
5403 do
5404 {
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 }
5416
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 }
5443
5444 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5445 unsigned int i;
5446 tree save_expr;
5447 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
5448 ctx->global->values.remove (save_expr);
5449 save_exprs.truncate (0);
5450
5451 if (++count >= constexpr_loop_limit)
5452 {
5453 if (!ctx->quiet)
5454 error_at (cp_expr_loc_or_input_loc (t),
5455 "%<constexpr%> loop iteration count exceeds limit of %d "
5456 "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
5457 constexpr_loop_limit);
5458 *non_constant_p = true;
5459 break;
5460 }
5461 }
5462 while (!returns (jump_target)
5463 && !breaks (jump_target)
5464 && !continues (jump_target)
5465 && (!switches (jump_target) || count == 0)
5466 && !*non_constant_p);
5467
5468 /* Forget saved values of SAVE_EXPRs and TARGET_EXPRs. */
5469 unsigned int i;
5470 tree save_expr;
5471 FOR_EACH_VEC_ELT (save_exprs, i, save_expr)
5472 ctx->global->values.remove (save_expr);
5473
5474 return NULL_TREE;
5475 }
5476
5477 /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
5478 semantics. */
5479
5480 static tree
5481 cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
5482 bool *non_constant_p, bool *overflow_p,
5483 tree *jump_target)
5484 {
5485 tree cond
5486 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
5487 cond = cxx_eval_constant_expression (ctx, cond, false,
5488 non_constant_p, overflow_p);
5489 VERIFY_CONSTANT (cond);
5490 *jump_target = cond;
5491
5492 tree body
5493 = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
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 }
5508 if (breaks (jump_target) || switches (jump_target))
5509 *jump_target = NULL_TREE;
5510 return NULL_TREE;
5511 }
5512
5513 /* Find the object of TYPE under initialization in CTX. */
5514
5515 static tree
5516 lookup_placeholder (const constexpr_ctx *ctx, bool lval, tree type)
5517 {
5518 if (!ctx)
5519 return NULL_TREE;
5520
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
5527 /* We could use ctx->object unconditionally, but using ctx->ctor when we
5528 can is a minor optimization. */
5529 if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
5530 return ctx->ctor;
5531
5532 if (!ctx->object)
5533 return NULL_TREE;
5534
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
5551 /* Complain about an attempt to evaluate inline assembly. */
5552
5553 static void
5554 inline_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 "
5559 "%<constexpr%> function in C++20");
5560 }
5561
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 */
5565 /* FIXME overflow_p is too global */
5566
5567 static tree
5568 cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
5569 bool lval,
5570 bool *non_constant_p, bool *overflow_p,
5571 tree *jump_target /* = NULL */)
5572 {
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:
5583 case IF_STMT:
5584 case DO_STMT:
5585 case WHILE_STMT:
5586 case FOR_STMT:
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 }
5598 if (error_operand_p (t))
5599 {
5600 *non_constant_p = true;
5601 return t;
5602 }
5603
5604 location_t loc = cp_expr_loc_or_input_loc (t);
5605
5606 STRIP_ANY_LOCATION_WRAPPER (t);
5607
5608 if (CONSTANT_CLASS_P (t))
5609 {
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 }
5617
5618 if (TREE_CODE (t) == INTEGER_CST
5619 && TYPE_PTR_P (TREE_TYPE (t))
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
5628 return t;
5629 }
5630
5631 /* Avoid excessively long constexpr evaluations. */
5632 if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
5633 {
5634 if (!ctx->quiet)
5635 error_at (loc,
5636 "%<constexpr%> evaluation operation count exceeds limit of "
5637 "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
5638 constexpr_ops_limit);
5639 ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
5640 *non_constant_p = true;
5641 return t;
5642 }
5643
5644 constexpr_ctx new_ctx;
5645 tree r = t;
5646
5647 tree_code tcode = TREE_CODE (t);
5648 switch (tcode)
5649 {
5650 case RESULT_DECL:
5651 if (lval)
5652 return t;
5653 /* We ask for an rvalue for the RESULT_DECL when indirecting
5654 through an invisible reference, or in named return value
5655 optimization. */
5656 if (tree *p = ctx->global->values.get (t))
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;
5665
5666 case VAR_DECL:
5667 if (DECL_HAS_VALUE_EXPR_P (t))
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 }
5691 /* fall through */
5692 case CONST_DECL:
5693 /* We used to not check lval for CONST_DECL, but darwin.c uses
5694 CONST_DECL for aggregate constants. */
5695 if (lval)
5696 return t;
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 }
5706 if (COMPLETE_TYPE_P (TREE_TYPE (t))
5707 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
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)
5714 r = decl_really_constant_value (t, /*unshare_p=*/false);
5715 else
5716 r = decl_constant_value (t, /*unshare_p=*/false);
5717 if (TREE_CODE (r) == TARGET_EXPR
5718 && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
5719 r = TARGET_EXPR_INITIAL (r);
5720 if (DECL_P (r))
5721 {
5722 if (!ctx->quiet)
5723 non_const_var_error (loc, r);
5724 *non_constant_p = true;
5725 }
5726 break;
5727
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
5733 case FUNCTION_DECL:
5734 case TEMPLATE_DECL:
5735 case LABEL_DECL:
5736 case LABEL_EXPR:
5737 case CASE_LABEL_EXPR:
5738 case PREDICT_EXPR:
5739 return t;
5740
5741 case PARM_DECL:
5742 if (lval && !TYPE_REF_P (TREE_TYPE (t)))
5743 /* glvalue use. */;
5744 else if (tree *p = ctx->global->values.get (r))
5745 r = *p;
5746 else if (lval)
5747 /* Defer in case this is only used for its type. */;
5748 else if (COMPLETE_TYPE_P (TREE_TYPE (t))
5749 && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
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 }
5755 else
5756 {
5757 if (!ctx->quiet)
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:
5765 r = cxx_eval_call_expression (ctx, t, lval,
5766 non_constant_p, overflow_p);
5767 break;
5768
5769 case DECL_EXPR:
5770 {
5771 r = DECL_EXPR_DECL (t);
5772 if (TREE_CODE (r) == USING_DECL)
5773 {
5774 r = void_node;
5775 break;
5776 }
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);
5783 CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
5784 ctx->global->values.put (r, new_ctx.ctor);
5785 ctx = &new_ctx;
5786 }
5787
5788 if (tree init = DECL_INITIAL (r))
5789 {
5790 init = cxx_eval_constant_expression (ctx, init,
5791 false,
5792 non_constant_p, overflow_p);
5793 /* Don't share a CONSTRUCTOR that might be changed. */
5794 init = unshare_constructor (init);
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;
5800 ctx->global->values.put (r, init);
5801 }
5802 else if (ctx == &new_ctx)
5803 /* We gave it a CONSTRUCTOR above. */;
5804 else
5805 ctx->global->values.put (r, NULL_TREE);
5806 }
5807 break;
5808
5809 case TARGET_EXPR:
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 }
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;
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;
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)
5857 break;
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 }
5869 break;
5870
5871 case INIT_EXPR:
5872 case MODIFY_EXPR:
5873 gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
5874 r = cxx_eval_store_expression (ctx, t, lval,
5875 non_constant_p, overflow_p);
5876 break;
5877
5878 case SCOPE_REF:
5879 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5880 lval,
5881 non_constant_p, overflow_p);
5882 break;
5883
5884 case RETURN_EXPR:
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);
5889 /* FALLTHRU */
5890 case BREAK_STMT:
5891 case CONTINUE_STMT:
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 }
5902 break;
5903
5904 case SAVE_EXPR:
5905 /* Avoid evaluating a SAVE_EXPR more than once. */
5906 if (tree *p = ctx->global->values.get (t))
5907 r = *p;
5908 else
5909 {
5910 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), false,
5911 non_constant_p, overflow_p);
5912 if (*non_constant_p)
5913 break;
5914 ctx->global->values.put (t, r);
5915 if (ctx->save_exprs)
5916 ctx->save_exprs->safe_push (t);
5917 }
5918 break;
5919
5920 case TRY_CATCH_EXPR:
5921 if (TREE_OPERAND (t, 0) == NULL_TREE)
5922 {
5923 r = void_node;
5924 break;
5925 }
5926 /* FALLTHRU */
5927 case NON_LVALUE_EXPR:
5928 case TRY_BLOCK:
5929 case MUST_NOT_THROW_EXPR:
5930 case EXPR_STMT:
5931 case EH_SPEC_BLOCK:
5932 r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5933 lval,
5934 non_constant_p, overflow_p,
5935 jump_target);
5936 break;
5937
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
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
5969 case CLEANUP_STMT:
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)
5976 {
5977 iloc_sentinel ils (loc);
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);
5986 }
5987 }
5988 break;
5989
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. */
5993 case MEM_REF:
5994 case INDIRECT_REF:
5995 r = cxx_eval_indirect_ref (ctx, t, lval,
5996 non_constant_p, overflow_p);
5997 break;
5998
5999 case ADDR_EXPR:
6000 {
6001 tree oldop = TREE_OPERAND (t, 0);
6002 tree op = cxx_eval_constant_expression (ctx, oldop,
6003 /*lval*/true,
6004 non_constant_p, overflow_p);
6005 /* Don't VERIFY_CONSTANT here. */
6006 if (*non_constant_p)
6007 return t;
6008 gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
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)
6012 {
6013 ggc_free (r);
6014 return t;
6015 }
6016 break;
6017 }
6018
6019 case REALPART_EXPR:
6020 case IMAGPART_EXPR:
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 */
6034 case CONJ_EXPR:
6035 case FIX_TRUNC_EXPR:
6036 case FLOAT_EXPR:
6037 case NEGATE_EXPR:
6038 case ABS_EXPR:
6039 case ABSU_EXPR:
6040 case BIT_NOT_EXPR:
6041 case TRUTH_NOT_EXPR:
6042 case FIXED_CONVERT_EXPR:
6043 r = cxx_eval_unary_expression (ctx, t, lval,
6044 non_constant_p, overflow_p);
6045 break;
6046
6047 case SIZEOF_EXPR:
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
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)
6073 r = cxx_eval_constant_expression (ctx, op0,
6074 lval, non_constant_p, overflow_p,
6075 jump_target);
6076 else
6077 {
6078 /* Check that the LHS is constant and then discard it. */
6079 cxx_eval_constant_expression (ctx, op0,
6080 true, non_constant_p, overflow_p,
6081 jump_target);
6082 if (*non_constant_p)
6083 return t;
6084 op1 = TREE_OPERAND (t, 1);
6085 r = cxx_eval_constant_expression (ctx, op1,
6086 lval, non_constant_p, overflow_p,
6087 jump_target);
6088 }
6089 }
6090 break;
6091
6092 case POINTER_PLUS_EXPR:
6093 case POINTER_DIFF_EXPR:
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:
6110 case LROTATE_EXPR:
6111 case RROTATE_EXPR:
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:
6122 case SPACESHIP_EXPR:
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:
6133 r = cxx_eval_binary_expression (ctx, t, lval,
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:
6141 r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
6142 boolean_true_node,
6143 lval,
6144 non_constant_p, overflow_p);
6145 break;
6146
6147 case TRUTH_OR_EXPR:
6148 case TRUTH_ORIF_EXPR:
6149 r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
6150 boolean_false_node,
6151 lval,
6152 non_constant_p, overflow_p);
6153 break;
6154
6155 case ARRAY_REF:
6156 r = cxx_eval_array_reference (ctx, t, lval,
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. */
6167 gcc_checking_assert (ctx->quiet || errorcount);
6168 *non_constant_p = true;
6169 return t;
6170 }
6171 r = cxx_eval_component_reference (ctx, t, lval,
6172 non_constant_p, overflow_p);
6173 break;
6174
6175 case BIT_FIELD_REF:
6176 r = cxx_eval_bit_field_ref (ctx, t, lval,
6177 non_constant_p, overflow_p);
6178 break;
6179
6180 case COND_EXPR:
6181 case IF_STMT:
6182 if (jump_target && *jump_target)
6183 {
6184 tree orig_jump = *jump_target;
6185 tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
6186 ? TREE_OPERAND (t, 1) : void_node);
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. */
6191 r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
6192 overflow_p, jump_target);
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)
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 }
6212 break;
6213 }
6214 r = cxx_eval_conditional_expression (ctx, t, lval,
6215 non_constant_p, overflow_p,
6216 jump_target);
6217 break;
6218 case VEC_COND_EXPR:
6219 r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
6220 overflow_p);
6221 break;
6222
6223 case CONSTRUCTOR:
6224 if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
6225 {
6226 /* Don't re-process a constant CONSTRUCTOR, but do fold it to
6227 VECTOR_CST if applicable. */
6228 verify_constructor_flags (t);
6229 if (TREE_CONSTANT (t))
6230 return fold (t);
6231 }
6232 r = cxx_eval_bare_aggregate (ctx, t, lval,
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. */
6242 r = cxx_eval_vec_init (ctx, t, lval,
6243 non_constant_p, overflow_p);
6244 break;
6245
6246 case VEC_PERM_EXPR:
6247 r = cxx_eval_trinary_expression (ctx, t, lval,
6248 non_constant_p, overflow_p);
6249 break;
6250
6251 case NOP_EXPR:
6252 if (REINTERPRET_CAST_P (t))
6253 {
6254 if (!ctx->quiet)
6255 error_at (loc,
6256 "%<reinterpret_cast%> is not a constant expression");
6257 *non_constant_p = true;
6258 return t;
6259 }
6260 /* FALLTHROUGH. */
6261 case CONVERT_EXPR:
6262 case VIEW_CONVERT_EXPR:
6263 case UNARY_PLUS_EXPR:
6264 {
6265 tree oldop = TREE_OPERAND (t, 0);
6266
6267 tree op = cxx_eval_constant_expression (ctx, oldop,
6268 lval,
6269 non_constant_p, overflow_p);
6270 if (*non_constant_p)
6271 return t;
6272 tree type = TREE_TYPE (t);
6273
6274 if (VOID_TYPE_P (type))
6275 return void_node;
6276
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
6289 if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
6290 op = cplus_expand_constant (op);
6291
6292 if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
6293 {
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);
6298 }
6299
6300 if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
6301 {
6302 if (integer_zerop (op))
6303 {
6304 if (TYPE_REF_P (type))
6305 {
6306 if (!ctx->quiet)
6307 error_at (loc,
6308 "dereferencing a null pointer");
6309 *non_constant_p = true;
6310 return t;
6311 }
6312 else if (TYPE_PTR_P (TREE_TYPE (op)))
6313 {
6314 tree from = TREE_TYPE (op);
6315
6316 if (!can_convert (type, from, tf_none))
6317 {
6318 if (!ctx->quiet)
6319 error_at (loc,
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)
6334 error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
6335 "a constant expression",
6336 type, op);
6337 *non_constant_p = true;
6338 return t;
6339 }
6340 }
6341
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
6370 if (op == oldop && tcode != UNARY_PLUS_EXPR)
6371 /* We didn't fold at the top so we could check for ptr-int
6372 conversion. */
6373 return fold (t);
6374
6375 tree sop;
6376
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;
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;
6385 else if (tcode == UNARY_PLUS_EXPR)
6386 r = fold_convert (TREE_TYPE (t), op);
6387 else
6388 r = fold_build1 (tcode, type, op);
6389
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
6403 case STATEMENT_LIST:
6404 new_ctx = *ctx;
6405 new_ctx.ctor = new_ctx.object = NULL_TREE;
6406 return cxx_eval_statement_list (&new_ctx, t,
6407 non_constant_p, overflow_p, jump_target);
6408
6409 case BIND_EXPR:
6410 return cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
6411 lval,
6412 non_constant_p, overflow_p,
6413 jump_target);
6414
6415 case PREINCREMENT_EXPR:
6416 case POSTINCREMENT_EXPR:
6417 case PREDECREMENT_EXPR:
6418 case POSTDECREMENT_EXPR:
6419 return cxx_eval_increment_expression (ctx, t,
6420 lval, non_constant_p, overflow_p);
6421
6422 case LAMBDA_EXPR:
6423 case NEW_EXPR:
6424 case VEC_NEW_EXPR:
6425 case DELETE_EXPR:
6426 case VEC_DELETE_EXPR:
6427 case THROW_EXPR:
6428 case MODOP_EXPR:
6429 /* GCC internal stuff. */
6430 case VA_ARG_EXPR:
6431 case NON_DEPENDENT_EXPR:
6432 case BASELINK:
6433 case OFFSET_REF:
6434 if (!ctx->quiet)
6435 error_at (loc, "expression %qE is not a constant expression", t);
6436 *non_constant_p = true;
6437 break;
6438
6439 case OBJ_TYPE_REF:
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);
6443
6444 case PLACEHOLDER_EXPR:
6445 /* Use of the value or address of the current object. */
6446 if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
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 }
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;
6459 break;
6460
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
6472 case GOTO_EXPR:
6473 *jump_target = TREE_OPERAND (t, 0);
6474 gcc_assert (breaks (jump_target) || continues (jump_target)
6475 /* Allow for jumping to a cdtor_label. */
6476 || returns (jump_target));
6477 break;
6478
6479 case LOOP_EXPR:
6480 case DO_STMT:
6481 case WHILE_STMT:
6482 case FOR_STMT:
6483 cxx_eval_loop_expr (ctx, t,
6484 non_constant_p, overflow_p, jump_target);
6485 break;
6486
6487 case SWITCH_EXPR:
6488 case SWITCH_STMT:
6489 cxx_eval_switch_expr (ctx, t,
6490 non_constant_p, overflow_p, jump_target);
6491 break;
6492
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)
6507 return satisfy_constraint_expression (t);
6508 else
6509 *non_constant_p = true;
6510 return t;
6511
6512 case ANNOTATE_EXPR:
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
6519 case USING_STMT:
6520 r = void_node;
6521 break;
6522
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. */
6527 tree id = unpack_concept_check (t);
6528 tree tmpl = TREE_OPERAND (id, 0);
6529 if (!concept_definition_p (tmpl))
6530 internal_error ("unexpected template-id %qE", t);
6531
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
6541 if (!processing_template_decl
6542 && !uid_sensitive_constexpr_evaluation_p ())
6543 r = evaluate_concept_check (t, tf_warning_or_error);
6544 else
6545 *non_constant_p = true;
6546
6547 break;
6548 }
6549
6550 case ASM_EXPR:
6551 if (!ctx->quiet)
6552 inline_asm_in_constexpr_error (loc);
6553 *non_constant_p = true;
6554 return t;
6555
6556 default:
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),
6564 "statement is not a constant expression");
6565 }
6566 else
6567 internal_error ("unexpected expression %qE of kind %s", t,
6568 get_tree_code_name (TREE_CODE (t)));
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
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
6590 static tree
6591 instantiate_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)
6596 && !trivial_fn_p (*tp)
6597 && DECL_TEMPLOID_INSTANTIATION (*tp)
6598 && !uid_sensitive_constexpr_evaluation_p ())
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 }
6616
6617 static void
6618 instantiate_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
6625 /* Look for heap variables in the expression *TP. */
6626
6627 static tree
6628 find_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
6641 /* Find immediate function decls in *TP if any. */
6642
6643 static tree
6644 find_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
6651 /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
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.
6655 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
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. */
6659
6660 static tree
6661 cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
6662 bool strict = true,
6663 bool manifestly_const_eval = false,
6664 bool constexpr_dtor = false,
6665 tree object = NULL_TREE)
6666 {
6667 auto_timevar time (TV_CONSTEXPR);
6668
6669 bool non_constant_p = false;
6670 bool overflow_p = false;
6671
6672 if (BRACE_ENCLOSED_INITIALIZER_P (t))
6673 {
6674 gcc_checking_assert (allow_non_constant);
6675 return t;
6676 }
6677
6678 constexpr_global_ctx global_ctx;
6679 constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
6680 allow_non_constant, strict,
6681 manifestly_const_eval || !allow_non_constant };
6682
6683 /* Turn off -frounding-math for manifestly constant evaluation. */
6684 warning_sentinel rm (flag_rounding_math, ctx.manifestly_const_eval);
6685 tree type = initialized_type (t);
6686 tree r = t;
6687 bool is_consteval = false;
6688 if (VOID_TYPE_P (type))
6689 {
6690 if (constexpr_dtor)
6691 /* Used for destructors of array elements. */
6692 type = TREE_TYPE (object);
6693 else
6694 {
6695 if (cxx_dialect < cxx20)
6696 return t;
6697 if (TREE_CODE (t) != CALL_EXPR && TREE_CODE (t) != AGGR_INIT_EXPR)
6698 return t;
6699 /* Calls to immediate functions returning void need to be
6700 evaluated. */
6701 tree fndecl = cp_get_callee_fndecl_nofold (t);
6702 if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
6703 return t;
6704 else
6705 is_consteval = true;
6706 }
6707 }
6708 else if (cxx_dialect >= cxx20
6709 && (TREE_CODE (t) == CALL_EXPR
6710 || TREE_CODE (t) == AGGR_INIT_EXPR
6711 || TREE_CODE (t) == TARGET_EXPR))
6712 {
6713 /* For non-concept checks, determine if it is consteval. */
6714 if (!concept_check_p (t))
6715 {
6716 tree x = t;
6717 if (TREE_CODE (x) == TARGET_EXPR)
6718 x = TARGET_EXPR_INITIAL (x);
6719 tree fndecl = cp_get_callee_fndecl_nofold (x);
6720 if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
6721 is_consteval = true;
6722 }
6723 }
6724 if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
6725 {
6726 /* In C++14 an NSDMI can participate in aggregate initialization,
6727 and can refer to the address of the object being initialized, so
6728 we need to pass in the relevant VAR_DECL if we want to do the
6729 evaluation in a single pass. The evaluation will dynamically
6730 update ctx.values for the VAR_DECL. We use the same strategy
6731 for C++11 constexpr constructors that refer to the object being
6732 initialized. */
6733 if (constexpr_dtor)
6734 {
6735 gcc_assert (object && VAR_P (object));
6736 gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
6737 gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
6738 if (error_operand_p (DECL_INITIAL (object)))
6739 return t;
6740 ctx.ctor = unshare_expr (DECL_INITIAL (object));
6741 TREE_READONLY (ctx.ctor) = false;
6742 /* Temporarily force decl_really_constant_value to return false
6743 for it, we want to use ctx.ctor for the current value instead. */
6744 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
6745 }
6746 else
6747 {
6748 ctx.ctor = build_constructor (type, NULL);
6749 CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
6750 }
6751 if (!object)
6752 {
6753 if (TREE_CODE (t) == TARGET_EXPR)
6754 object = TARGET_EXPR_SLOT (t);
6755 else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6756 object = AGGR_INIT_EXPR_SLOT (t);
6757 }
6758 ctx.object = object;
6759 if (object)
6760 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6761 (type, TREE_TYPE (object)));
6762 if (object && DECL_P (object))
6763 global_ctx.values.put (object, ctx.ctor);
6764 if (TREE_CODE (r) == TARGET_EXPR)
6765 /* Avoid creating another CONSTRUCTOR when we expand the
6766 TARGET_EXPR. */
6767 r = TARGET_EXPR_INITIAL (r);
6768 }
6769
6770 auto_vec<tree, 16> cleanups;
6771 global_ctx.cleanups = &cleanups;
6772
6773 instantiate_constexpr_fns (r);
6774 r = cxx_eval_constant_expression (&ctx, r,
6775 false, &non_constant_p, &overflow_p);
6776
6777 if (!constexpr_dtor)
6778 verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
6779 else
6780 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
6781
6782 unsigned int i;
6783 tree cleanup;
6784 /* Evaluate the cleanups. */
6785 FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
6786 cxx_eval_constant_expression (&ctx, cleanup, false,
6787 &non_constant_p, &overflow_p);
6788
6789 /* Mutable logic is a bit tricky: we want to allow initialization of
6790 constexpr variables with mutable members, but we can't copy those
6791 members to another constexpr variable. */
6792 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_MUTABLE_POISON (r))
6793 {
6794 if (!allow_non_constant)
6795 error ("%qE is not a constant expression because it refers to "
6796 "mutable subobjects of %qT", t, type);
6797 non_constant_p = true;
6798 }
6799
6800 if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
6801 {
6802 if (!allow_non_constant)
6803 error ("%qE is not a constant expression because it refers to "
6804 "an incompletely initialized variable", t);
6805 TREE_CONSTANT (r) = false;
6806 non_constant_p = true;
6807 }
6808
6809 if (!global_ctx.heap_vars.is_empty ())
6810 {
6811 tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
6812 NULL);
6813 unsigned int i;
6814 if (heap_var)
6815 {
6816 if (!allow_non_constant && !non_constant_p)
6817 error_at (DECL_SOURCE_LOCATION (heap_var),
6818 "%qE is not a constant expression because it refers to "
6819 "a result of %<operator new%>", t);
6820 r = t;
6821 non_constant_p = true;
6822 }
6823 FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
6824 if (DECL_NAME (heap_var) != heap_deleted_identifier)
6825 {
6826 if (!allow_non_constant && !non_constant_p)
6827 error_at (DECL_SOURCE_LOCATION (heap_var),
6828 "%qE is not a constant expression because allocated "
6829 "storage has not been deallocated", t);
6830 r = t;
6831 non_constant_p = true;
6832 }
6833 }
6834
6835 /* Check that immediate invocation does not return an expression referencing
6836 any immediate function decls. They need to be allowed while parsing
6837 immediate functions, but can't leak outside of them. */
6838 if (is_consteval
6839 && t != r
6840 && (current_function_decl == NULL_TREE
6841 || !DECL_IMMEDIATE_FUNCTION_P (current_function_decl)))
6842 if (tree immediate_fndecl
6843 = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
6844 NULL))
6845 {
6846 if (!allow_non_constant && !non_constant_p)
6847 error_at (cp_expr_loc_or_input_loc (t),
6848 "immediate evaluation returns address of immediate "
6849 "function %qD", immediate_fndecl);
6850 r = t;
6851 non_constant_p = true;
6852 }
6853
6854
6855 if (!non_constant_p && overflow_p)
6856 non_constant_p = true;
6857
6858 /* Unshare the result. */
6859 bool should_unshare = true;
6860 if (r == t || (TREE_CODE (t) == TARGET_EXPR
6861 && TARGET_EXPR_INITIAL (t) == r))
6862 should_unshare = false;
6863
6864 if (non_constant_p && !allow_non_constant)
6865 return error_mark_node;
6866 else if (constexpr_dtor)
6867 return r;
6868 else if (non_constant_p && TREE_CONSTANT (r))
6869 {
6870 /* If __builtin_is_constant_evaluated () was evaluated to true
6871 and the result is not a valid constant expression, we need to
6872 punt. */
6873 if (manifestly_const_eval)
6874 return cxx_eval_outermost_constant_expr (t, true, strict,
6875 false, false, object);
6876 /* This isn't actually constant, so unset TREE_CONSTANT.
6877 Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
6878 it to be set if it is invariant address, even when it is not
6879 a valid C++ constant expression. Wrap it with a NOP_EXPR
6880 instead. */
6881 if (EXPR_P (r) && TREE_CODE (r) != ADDR_EXPR)
6882 r = copy_node (r);
6883 else if (TREE_CODE (r) == CONSTRUCTOR)
6884 r = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (r), r);
6885 else
6886 r = build_nop (TREE_TYPE (r), r);
6887 TREE_CONSTANT (r) = false;
6888 }
6889 else if (non_constant_p)
6890 return t;
6891
6892 if (should_unshare)
6893 r = unshare_expr (r);
6894
6895 if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
6896 {
6897 r = adjust_temp_type (type, r);
6898 if (TREE_CODE (t) == TARGET_EXPR
6899 && TARGET_EXPR_INITIAL (t) == r)
6900 return t;
6901 else if (TREE_CODE (t) != CONSTRUCTOR)
6902 {
6903 r = get_target_expr_sfinae (r, tf_warning_or_error | tf_no_cleanup);
6904 TREE_CONSTANT (r) = true;
6905 }
6906 }
6907
6908 return r;
6909 }
6910
6911 /* If T represents a constant expression returns its reduced value.
6912 Otherwise return error_mark_node. If T is dependent, then
6913 return NULL. */
6914
6915 tree
6916 cxx_constant_value (tree t, tree decl)
6917 {
6918 return cxx_eval_outermost_constant_expr (t, false, true, true, false, decl);
6919 }
6920
6921 /* Like cxx_constant_value, but used for evaluation of constexpr destructors
6922 of constexpr variables. The actual initializer of DECL is not modified. */
6923
6924 void
6925 cxx_constant_dtor (tree t, tree decl)
6926 {
6927 cxx_eval_outermost_constant_expr (t, false, true, true, true, decl);
6928 }
6929
6930 /* Helper routine for fold_simple function. Either return simplified
6931 expression T, otherwise NULL_TREE.
6932 In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
6933 even if we are within template-declaration. So be careful on call, as in
6934 such case types can be undefined. */
6935
6936 static tree
6937 fold_simple_1 (tree t)
6938 {
6939 tree op1;
6940 enum tree_code code = TREE_CODE (t);
6941
6942 switch (code)
6943 {
6944 case INTEGER_CST:
6945 case REAL_CST:
6946 case VECTOR_CST:
6947 case FIXED_CST:
6948 case COMPLEX_CST:
6949 return t;
6950
6951 case SIZEOF_EXPR:
6952 return fold_sizeof_expr (t);
6953
6954 case ABS_EXPR:
6955 case ABSU_EXPR:
6956 case CONJ_EXPR:
6957 case REALPART_EXPR:
6958 case IMAGPART_EXPR:
6959 case NEGATE_EXPR:
6960 case BIT_NOT_EXPR:
6961 case TRUTH_NOT_EXPR:
6962 case NOP_EXPR:
6963 case VIEW_CONVERT_EXPR:
6964 case CONVERT_EXPR:
6965 case FLOAT_EXPR:
6966 case FIX_TRUNC_EXPR:
6967 case FIXED_CONVERT_EXPR:
6968 case ADDR_SPACE_CONVERT_EXPR:
6969
6970 op1 = TREE_OPERAND (t, 0);
6971
6972 t = const_unop (code, TREE_TYPE (t), op1);
6973 if (!t)
6974 return NULL_TREE;
6975
6976 if (CONVERT_EXPR_CODE_P (code)
6977 && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
6978 TREE_OVERFLOW (t) = false;
6979 return t;
6980
6981 default:
6982 return NULL_TREE;
6983 }
6984 }
6985
6986 /* If T is a simple constant expression, returns its simplified value.
6987 Otherwise returns T. In contrast to maybe_constant_value we
6988 simplify only few operations on constant-expressions, and we don't
6989 try to simplify constexpressions. */
6990
6991 tree
6992 fold_simple (tree t)
6993 {
6994 if (processing_template_decl)
6995 return t;
6996
6997 tree r = fold_simple_1 (t);
6998 if (r)
6999 return r;
7000
7001 return t;
7002 }
7003
7004 /* If T is a constant expression, returns its reduced value.
7005 Otherwise, if T does not have TREE_CONSTANT set, returns T.
7006 Otherwise, returns a version of T without TREE_CONSTANT.
7007 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
7008 as per P0595. */
7009
7010 static GTY((deletable)) hash_map<tree, tree> *cv_cache;
7011
7012 tree
7013 maybe_constant_value (tree t, tree decl, bool manifestly_const_eval)
7014 {
7015 tree r;
7016
7017 if (!is_nondependent_constant_expression (t))
7018 {
7019 if (TREE_OVERFLOW_P (t))
7020 {
7021 t = build_nop (TREE_TYPE (t), t);
7022 TREE_CONSTANT (t) = false;
7023 }
7024 return t;
7025 }
7026 else if (CONSTANT_CLASS_P (t))
7027 /* No caching or evaluation needed. */
7028 return t;
7029
7030 if (manifestly_const_eval)
7031 return cxx_eval_outermost_constant_expr (t, true, true, true, false, decl);
7032
7033 if (cv_cache == NULL)
7034 cv_cache = hash_map<tree, tree>::create_ggc (101);
7035 if (tree *cached = cv_cache->get (t))
7036 {
7037 r = *cached;
7038 if (r != t)
7039 {
7040 r = break_out_target_exprs (r, /*clear_loc*/true);
7041 protected_set_expr_location (r, EXPR_LOCATION (t));
7042 }
7043 return r;
7044 }
7045
7046 uid_sensitive_constexpr_evaluation_checker c;
7047 r = cxx_eval_outermost_constant_expr (t, true, true, false, false, decl);
7048 gcc_checking_assert (r == t
7049 || CONVERT_EXPR_P (t)
7050 || TREE_CODE (t) == VIEW_CONVERT_EXPR
7051 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
7052 || !cp_tree_equal (r, t));
7053 if (!c.evaluation_restricted_p ())
7054 cv_cache->put (t, r);
7055 return r;
7056 }
7057
7058 /* Dispose of the whole CV_CACHE. */
7059
7060 static void
7061 clear_cv_cache (void)
7062 {
7063 if (cv_cache != NULL)
7064 cv_cache->empty ();
7065 }
7066
7067 /* Dispose of the whole CV_CACHE, FOLD_CACHE, and satisfaction caches. */
7068
7069 void
7070 clear_cv_and_fold_caches (bool sat /*= true*/)
7071 {
7072 clear_cv_cache ();
7073 clear_fold_cache ();
7074 if (sat)
7075 clear_satisfaction_cache ();
7076 }
7077
7078 /* Internal function handling expressions in templates for
7079 fold_non_dependent_expr and fold_non_dependent_init.
7080
7081 If we're in a template, but T isn't value dependent, simplify
7082 it. We're supposed to treat:
7083
7084 template <typename T> void f(T[1 + 1]);
7085 template <typename T> void f(T[2]);
7086
7087 as two declarations of the same function, for example. */
7088
7089 static tree
7090 fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
7091 bool manifestly_const_eval,
7092 tree object)
7093 {
7094 gcc_assert (processing_template_decl);
7095
7096 if (is_nondependent_constant_expression (t))
7097 {
7098 processing_template_decl_sentinel s;
7099 t = instantiate_non_dependent_expr_internal (t, complain);
7100
7101 if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
7102 {
7103 if (TREE_OVERFLOW_P (t))
7104 {
7105 t = build_nop (TREE_TYPE (t), t);
7106 TREE_CONSTANT (t) = false;
7107 }
7108 return t;
7109 }
7110
7111 tree r = cxx_eval_outermost_constant_expr (t, true, true,
7112 manifestly_const_eval,
7113 false, object);
7114 /* cp_tree_equal looks through NOPs, so allow them. */
7115 gcc_checking_assert (r == t
7116 || CONVERT_EXPR_P (t)
7117 || TREE_CODE (t) == VIEW_CONVERT_EXPR
7118 || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
7119 || !cp_tree_equal (r, t));
7120 return r;
7121 }
7122 else if (TREE_OVERFLOW_P (t))
7123 {
7124 t = build_nop (TREE_TYPE (t), t);
7125 TREE_CONSTANT (t) = false;
7126 }
7127
7128 return t;
7129 }
7130
7131 /* Like maybe_constant_value but first fully instantiate the argument.
7132
7133 Note: this is equivalent to instantiate_non_dependent_expr_sfinae
7134 (t, complain) followed by maybe_constant_value but is more efficient,
7135 because it calls instantiation_dependent_expression_p and
7136 potential_constant_expression at most once.
7137 The manifestly_const_eval argument is passed to maybe_constant_value.
7138
7139 Callers should generally pass their active complain, or if they are in a
7140 non-template, diagnosing context, they can use the default of
7141 tf_warning_or_error. Callers that might be within a template context, don't
7142 have a complain parameter, and aren't going to remember the result for long
7143 (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
7144 appropriately. */
7145
7146 tree
7147 fold_non_dependent_expr (tree t,
7148 tsubst_flags_t complain /* = tf_warning_or_error */,
7149 bool manifestly_const_eval /* = false */,
7150 tree object /* = NULL_TREE */)
7151 {
7152 if (t == NULL_TREE)
7153 return NULL_TREE;
7154
7155 if (processing_template_decl)
7156 return fold_non_dependent_expr_template (t, complain,
7157 manifestly_const_eval, object);
7158
7159 return maybe_constant_value (t, object, manifestly_const_eval);
7160 }
7161
7162 /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
7163 return the original expression. */
7164
7165 tree
7166 maybe_fold_non_dependent_expr (tree expr,
7167 tsubst_flags_t complain/*=tf_warning_or_error*/)
7168 {
7169 tree t = fold_non_dependent_expr (expr, complain);
7170 if (t && TREE_CONSTANT (t))
7171 return t;
7172
7173 return expr;
7174 }
7175
7176 /* Like maybe_constant_init but first fully instantiate the argument. */
7177
7178 tree
7179 fold_non_dependent_init (tree t,
7180 tsubst_flags_t complain /*=tf_warning_or_error*/,
7181 bool manifestly_const_eval /*=false*/)
7182 {
7183 if (t == NULL_TREE)
7184 return NULL_TREE;
7185
7186 if (processing_template_decl)
7187 {
7188 t = fold_non_dependent_expr_template (t, complain,
7189 manifestly_const_eval, NULL_TREE);
7190 /* maybe_constant_init does this stripping, so do it here too. */
7191 if (TREE_CODE (t) == TARGET_EXPR)
7192 {
7193 tree init = TARGET_EXPR_INITIAL (t);
7194 if (TREE_CODE (init) == CONSTRUCTOR)
7195 t = init;
7196 }
7197 return t;
7198 }
7199
7200 return maybe_constant_init (t, NULL_TREE, manifestly_const_eval);
7201 }
7202
7203 /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
7204 than wrapped in a TARGET_EXPR.
7205 ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
7206 MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
7207 per P0595 even when ALLOW_NON_CONSTANT is true. */
7208
7209 static tree
7210 maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
7211 bool manifestly_const_eval)
7212 {
7213 if (!t)
7214 return t;
7215 if (TREE_CODE (t) == EXPR_STMT)
7216 t = TREE_OPERAND (t, 0);
7217 if (TREE_CODE (t) == CONVERT_EXPR
7218 && VOID_TYPE_P (TREE_TYPE (t)))
7219 t = TREE_OPERAND (t, 0);
7220 if (TREE_CODE (t) == INIT_EXPR)
7221 t = TREE_OPERAND (t, 1);
7222 if (TREE_CODE (t) == TARGET_EXPR)
7223 t = TARGET_EXPR_INITIAL (t);
7224 if (!is_nondependent_static_init_expression (t))
7225 /* Don't try to evaluate it. */;
7226 else if (CONSTANT_CLASS_P (t) && allow_non_constant)
7227 /* No evaluation needed. */;
7228 else
7229 t = cxx_eval_outermost_constant_expr (t, allow_non_constant,
7230 /*strict*/false,
7231 manifestly_const_eval, false, decl);
7232 if (TREE_CODE (t) == TARGET_EXPR)
7233 {
7234 tree init = TARGET_EXPR_INITIAL (t);
7235 if (TREE_CODE (init) == CONSTRUCTOR)
7236 t = init;
7237 }
7238 return t;
7239 }
7240
7241 /* Wrapper for maybe_constant_init_1 which permits non constants. */
7242
7243 tree
7244 maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
7245 {
7246 return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
7247 }
7248
7249 /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
7250
7251 tree
7252 cxx_constant_init (tree t, tree decl)
7253 {
7254 return maybe_constant_init_1 (t, decl, false, true);
7255 }
7256
7257 #if 0
7258 /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
7259 /* Return true if the object referred to by REF has automatic or thread
7260 local storage. */
7261
7262 enum { ck_ok, ck_bad, ck_unknown };
7263 static int
7264 check_automatic_or_tls (tree ref)
7265 {
7266 machine_mode mode;
7267 poly_int64 bitsize, bitpos;
7268 tree offset;
7269 int volatilep = 0, unsignedp = 0;
7270 tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
7271 &mode, &unsignedp, &volatilep, false);
7272 duration_kind dk;
7273
7274 /* If there isn't a decl in the middle, we don't know the linkage here,
7275 and this isn't a constant expression anyway. */
7276 if (!DECL_P (decl))
7277 return ck_unknown;
7278 dk = decl_storage_duration (decl);
7279 return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
7280 }
7281 #endif
7282
7283 /* Data structure for passing data from potential_constant_expression_1
7284 to check_for_return_continue via cp_walk_tree. */
7285 struct check_for_return_continue_data {
7286 hash_set<tree> *pset;
7287 tree continue_stmt;
7288 };
7289
7290 /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
7291 called through cp_walk_tree. Return the first RETURN_EXPR found, or note
7292 the first CONTINUE_STMT if RETURN_EXPR is not found. */
7293 static tree
7294 check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
7295 {
7296 tree t = *tp, s;
7297 check_for_return_continue_data *d = (check_for_return_continue_data *) data;
7298 switch (TREE_CODE (t))
7299 {
7300 case RETURN_EXPR:
7301 return t;
7302
7303 case CONTINUE_STMT:
7304 if (d->continue_stmt == NULL_TREE)
7305 d->continue_stmt = t;
7306 break;
7307
7308 #define RECUR(x) \
7309 if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
7310 d->pset)) \
7311 return r
7312
7313 /* For loops, walk subtrees manually, so that continue stmts found
7314 inside of the bodies of the loops are ignored. */
7315 case DO_STMT:
7316 *walk_subtrees = 0;
7317 RECUR (DO_COND (t));
7318 s = d->continue_stmt;
7319 RECUR (DO_BODY (t));
7320 d->continue_stmt = s;
7321 break;
7322
7323 case WHILE_STMT:
7324 *walk_subtrees = 0;
7325 RECUR (WHILE_COND (t));
7326 s = d->continue_stmt;
7327 RECUR (WHILE_BODY (t));
7328 d->continue_stmt = s;
7329 break;
7330
7331 case FOR_STMT:
7332 *walk_subtrees = 0;
7333 RECUR (FOR_INIT_STMT (t));
7334 RECUR (FOR_COND (t));
7335 RECUR (FOR_EXPR (t));
7336 s = d->continue_stmt;
7337 RECUR (FOR_BODY (t));
7338 d->continue_stmt = s;
7339 break;
7340
7341 case RANGE_FOR_STMT:
7342 *walk_subtrees = 0;
7343 RECUR (RANGE_FOR_EXPR (t));
7344 s = d->continue_stmt;
7345 RECUR (RANGE_FOR_BODY (t));
7346 d->continue_stmt = s;
7347 break;
7348 #undef RECUR
7349
7350 case STATEMENT_LIST:
7351 case CONSTRUCTOR:
7352 break;
7353
7354 default:
7355 if (!EXPR_P (t))
7356 *walk_subtrees = 0;
7357 break;
7358 }
7359
7360 return NULL_TREE;
7361 }
7362
7363 /* Return true if T denotes a potentially constant expression. Issue
7364 diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
7365 an lvalue-rvalue conversion is implied. If NOW is true, we want to
7366 consider the expression in the current context, independent of constexpr
7367 substitution.
7368
7369 C++0x [expr.const] used to say
7370
7371 6 An expression is a potential constant expression if it is
7372 a constant expression where all occurrences of function
7373 parameters are replaced by arbitrary constant expressions
7374 of the appropriate type.
7375
7376 2 A conditional expression is a constant expression unless it
7377 involves one of the following as a potentially evaluated
7378 subexpression (3.2), but subexpressions of logical AND (5.14),
7379 logical OR (5.15), and conditional (5.16) operations that are
7380 not evaluated are not considered. */
7381
7382 static bool
7383 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
7384 tsubst_flags_t flags, tree *jump_target)
7385 {
7386 #define RECUR(T,RV) \
7387 potential_constant_expression_1 ((T), (RV), strict, now, flags, jump_target)
7388
7389 enum { any = false, rval = true };
7390 int i;
7391 tree tmp;
7392
7393 if (t == error_mark_node)
7394 return false;
7395 if (t == NULL_TREE)
7396 return true;
7397 location_t loc = cp_expr_loc_or_input_loc (t);
7398
7399 if (*jump_target)
7400 /* If we are jumping, ignore everything. This is simpler than the
7401 cxx_eval_constant_expression handling because we only need to be
7402 conservatively correct, and we don't necessarily have a constant value
7403 available, so we don't bother with switch tracking. */
7404 return true;
7405
7406 if (TREE_THIS_VOLATILE (t) && want_rval)
7407 {
7408 if (flags & tf_error)
7409 error_at (loc, "lvalue-to-rvalue conversion of a volatile lvalue "
7410 "%qE with type %qT", t, TREE_TYPE (t));
7411 return false;
7412 }
7413 if (CONSTANT_CLASS_P (t))
7414 return true;
7415 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
7416 && TREE_TYPE (t) == error_mark_node)
7417 return false;
7418
7419 switch (TREE_CODE (t))
7420 {
7421 case FUNCTION_DECL:
7422 case BASELINK:
7423 case TEMPLATE_DECL:
7424 case OVERLOAD:
7425 case TEMPLATE_ID_EXPR:
7426 case LABEL_DECL:
7427 case LABEL_EXPR:
7428 case CASE_LABEL_EXPR:
7429 case PREDICT_EXPR:
7430 case CONST_DECL:
7431 case SIZEOF_EXPR:
7432 case ALIGNOF_EXPR:
7433 case OFFSETOF_EXPR:
7434 case NOEXCEPT_EXPR:
7435 case TEMPLATE_PARM_INDEX:
7436 case TRAIT_EXPR:
7437 case IDENTIFIER_NODE:
7438 case USERDEF_LITERAL:
7439 /* We can see a FIELD_DECL in a pointer-to-member expression. */
7440 case FIELD_DECL:
7441 case RESULT_DECL:
7442 case USING_DECL:
7443 case USING_STMT:
7444 case PLACEHOLDER_EXPR:
7445 case REQUIRES_EXPR:
7446 case STATIC_ASSERT:
7447 case DEBUG_BEGIN_STMT:
7448 return true;
7449
7450 case RETURN_EXPR:
7451 if (!RECUR (TREE_OPERAND (t, 0), any))
7452 return false;
7453 /* FALLTHROUGH */
7454
7455 case BREAK_STMT:
7456 case CONTINUE_STMT:
7457 *jump_target = t;
7458 return true;
7459
7460 case PARM_DECL:
7461 if (now && want_rval)
7462 {
7463 tree type = TREE_TYPE (t);
7464 if ((processing_template_decl && !COMPLETE_TYPE_P (type))
7465 || dependent_type_p (type)
7466 || is_really_empty_class (type, /*ignore_vptr*/false))
7467 /* An empty class has no data to read. */
7468 return true;
7469 if (flags & tf_error)
7470 error ("%qE is not a constant expression", t);
7471 return false;
7472 }
7473 return true;
7474
7475 case AGGR_INIT_EXPR:
7476 case CALL_EXPR:
7477 /* -- an invocation of a function other than a constexpr function
7478 or a constexpr constructor. */
7479 {
7480 tree fun = get_function_named_in_call (t);
7481 const int nargs = call_expr_nargs (t);
7482 i = 0;
7483
7484 if (fun == NULL_TREE)
7485 {
7486 /* Reset to allow the function to continue past the end
7487 of the block below. Otherwise return early. */
7488 bool bail = true;
7489
7490 if (TREE_CODE (t) == CALL_EXPR
7491 && CALL_EXPR_FN (t) == NULL_TREE)
7492 switch (CALL_EXPR_IFN (t))
7493 {
7494 /* These should be ignored, they are optimized away from
7495 constexpr functions. */
7496 case IFN_UBSAN_NULL:
7497 case IFN_UBSAN_BOUNDS:
7498 case IFN_UBSAN_VPTR:
7499 case IFN_FALLTHROUGH:
7500 return true;
7501
7502 case IFN_ADD_OVERFLOW:
7503 case IFN_SUB_OVERFLOW:
7504 case IFN_MUL_OVERFLOW:
7505 case IFN_LAUNDER:
7506 case IFN_VEC_CONVERT:
7507 bail = false;
7508 break;
7509
7510 default:
7511 break;
7512 }
7513
7514 if (bail)
7515 {
7516 /* fold_call_expr can't do anything with IFN calls. */
7517 if (flags & tf_error)
7518 error_at (loc, "call to internal function %qE", t);
7519 return false;
7520 }
7521 }
7522
7523 if (fun && is_overloaded_fn (fun))
7524 {
7525 if (TREE_CODE (fun) == FUNCTION_DECL)
7526 {
7527 if (builtin_valid_in_constant_expr_p (fun))
7528 return true;
7529 if (!DECL_DECLARED_CONSTEXPR_P (fun)
7530 /* Allow any built-in function; if the expansion
7531 isn't constant, we'll deal with that then. */
7532 && !fndecl_built_in_p (fun)
7533 /* In C++20, replaceable global allocation functions
7534 are constant expressions. */
7535 && (!cxx_replaceable_global_alloc_fn (fun)
7536 || TREE_CODE (t) != CALL_EXPR
7537 || (!CALL_FROM_NEW_OR_DELETE_P (t)
7538 && (current_function_decl == NULL_TREE
7539 || !is_std_allocator_allocate
7540 (current_function_decl))))
7541 /* Allow placement new in std::construct_at. */
7542 && (!cxx_placement_new_fn (fun)
7543 || TREE_CODE (t) != CALL_EXPR
7544 || current_function_decl == NULL_TREE
7545 || !is_std_construct_at (current_function_decl))
7546 && !cxx_dynamic_cast_fn_p (fun))
7547 {
7548 if (flags & tf_error)
7549 {
7550 error_at (loc, "call to non-%<constexpr%> function %qD",
7551 fun);
7552 explain_invalid_constexpr_fn (fun);
7553 }
7554 return false;
7555 }
7556 /* A call to a non-static member function takes the address
7557 of the object as the first argument. But in a constant
7558 expression the address will be folded away, so look
7559 through it now. */
7560 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fun)
7561 && !DECL_CONSTRUCTOR_P (fun))
7562 {
7563 tree x = get_nth_callarg (t, 0);
7564 if (is_this_parameter (x))
7565 return true;
7566 /* Don't require an immediately constant value, as
7567 constexpr substitution might not use the value. */
7568 bool sub_now = false;
7569 if (!potential_constant_expression_1 (x, rval, strict,
7570 sub_now, flags,
7571 jump_target))
7572 return false;
7573 i = 1;
7574 }
7575 }
7576 else
7577 {
7578 if (!RECUR (fun, true))
7579 return false;
7580 fun = get_first_fn (fun);
7581 }
7582 /* Skip initial arguments to base constructors. */
7583 if (DECL_BASE_CONSTRUCTOR_P (fun))
7584 i = num_artificial_parms_for (fun);
7585 fun = DECL_ORIGIN (fun);
7586 }
7587 else if (fun)
7588 {
7589 if (RECUR (fun, rval))
7590 /* Might end up being a constant function pointer. */;
7591 else
7592 return false;
7593 }
7594 for (; i < nargs; ++i)
7595 {
7596 tree x = get_nth_callarg (t, i);
7597 /* In a template, reference arguments haven't been converted to
7598 REFERENCE_TYPE and we might not even know if the parameter
7599 is a reference, so accept lvalue constants too. */
7600 bool rv = processing_template_decl ? any : rval;
7601 /* Don't require an immediately constant value, as constexpr
7602 substitution might not use the value of the argument. */
7603 bool sub_now = false;
7604 if (!potential_constant_expression_1 (x, rv, strict,
7605 sub_now, flags, jump_target))
7606 return false;
7607 }
7608 return true;
7609 }
7610
7611 case NON_LVALUE_EXPR:
7612 /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
7613 -- an lvalue of integral type that refers to a non-volatile
7614 const variable or static data member initialized with
7615 constant expressions, or
7616
7617 -- an lvalue of literal type that refers to non-volatile
7618 object defined with constexpr, or that refers to a
7619 sub-object of such an object; */
7620 return RECUR (TREE_OPERAND (t, 0), rval);
7621
7622 case VAR_DECL:
7623 if (DECL_HAS_VALUE_EXPR_P (t))
7624 {
7625 if (now && is_normal_capture_proxy (t))
7626 {
7627 /* -- in a lambda-expression, a reference to this or to a
7628 variable with automatic storage duration defined outside that
7629 lambda-expression, where the reference would be an
7630 odr-use. */
7631
7632 if (want_rval)
7633 /* Since we're doing an lvalue-rvalue conversion, this might
7634 not be an odr-use, so evaluate the variable directly. */
7635 return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
7636
7637 if (flags & tf_error)
7638 {
7639 tree cap = DECL_CAPTURED_VARIABLE (t);
7640 error ("lambda capture of %qE is not a constant expression",
7641 cap);
7642 if (decl_constant_var_p (cap))
7643 inform (input_location, "because it is used as a glvalue");
7644 }
7645 return false;
7646 }
7647 return RECUR (DECL_VALUE_EXPR (t), rval);
7648 }
7649 if (want_rval
7650 && !var_in_maybe_constexpr_fn (t)
7651 && !type_dependent_expression_p (t)
7652 && !decl_maybe_constant_var_p (t)
7653 && (strict
7654 || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
7655 || (DECL_INITIAL (t)
7656 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
7657 && COMPLETE_TYPE_P (TREE_TYPE (t))
7658 && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
7659 {
7660 if (flags & tf_error)
7661 non_const_var_error (loc, t);
7662 return false;
7663 }
7664 return true;
7665
7666 case NOP_EXPR:
7667 if (REINTERPRET_CAST_P (t))
7668 {
7669 if (flags & tf_error)
7670 error_at (loc, "%<reinterpret_cast%> is not a constant expression");
7671 return false;
7672 }
7673 /* FALLTHRU */
7674 case CONVERT_EXPR:
7675 case VIEW_CONVERT_EXPR:
7676 /* -- a reinterpret_cast. FIXME not implemented, and this rule
7677 may change to something more specific to type-punning (DR 1312). */
7678 {
7679 tree from = TREE_OPERAND (t, 0);
7680 if (location_wrapper_p (t))
7681 return (RECUR (from, want_rval));
7682 if (INDIRECT_TYPE_P (TREE_TYPE (t)))
7683 {
7684 STRIP_ANY_LOCATION_WRAPPER (from);
7685 if (TREE_CODE (from) == INTEGER_CST
7686 && !integer_zerop (from))
7687 {
7688 if (flags & tf_error)
7689 error_at (loc,
7690 "%<reinterpret_cast%> from integer to pointer");
7691 return false;
7692 }
7693 }
7694 return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
7695 }
7696
7697 case ADDRESSOF_EXPR:
7698 /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
7699 t = TREE_OPERAND (t, 0);
7700 goto handle_addr_expr;
7701
7702 case ADDR_EXPR:
7703 /* -- a unary operator & that is applied to an lvalue that
7704 designates an object with thread or automatic storage
7705 duration; */
7706 t = TREE_OPERAND (t, 0);
7707
7708 if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
7709 /* A pointer-to-member constant. */
7710 return true;
7711
7712 handle_addr_expr:
7713 #if 0
7714 /* FIXME adjust when issue 1197 is fully resolved. For now don't do
7715 any checking here, as we might dereference the pointer later. If
7716 we remove this code, also remove check_automatic_or_tls. */
7717 i = check_automatic_or_tls (t);
7718 if (i == ck_ok)
7719 return true;
7720 if (i == ck_bad)
7721 {
7722 if (flags & tf_error)
7723 error ("address-of an object %qE with thread local or "
7724 "automatic storage is not a constant expression", t);
7725 return false;
7726 }
7727 #endif
7728 return RECUR (t, any);
7729
7730 case COMPONENT_REF:
7731 case ARROW_EXPR:
7732 case OFFSET_REF:
7733 /* -- a class member access unless its postfix-expression is
7734 of literal type or of pointer to literal type. */
7735 /* This test would be redundant, as it follows from the
7736 postfix-expression being a potential constant expression. */
7737 if (type_unknown_p (t))
7738 return true;
7739 if (is_overloaded_fn (t))
7740 /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
7741 which uses ob as an lvalue. */
7742 want_rval = false;
7743 gcc_fallthrough ();
7744
7745 case REALPART_EXPR:
7746 case IMAGPART_EXPR:
7747 case BIT_FIELD_REF:
7748 return RECUR (TREE_OPERAND (t, 0), want_rval);
7749
7750 case EXPR_PACK_EXPANSION:
7751 return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
7752
7753 case INDIRECT_REF:
7754 {
7755 tree x = TREE_OPERAND (t, 0);
7756 STRIP_NOPS (x);
7757 if (is_this_parameter (x) && !is_capture_proxy (x))
7758 {
7759 if (!var_in_maybe_constexpr_fn (x))
7760 {
7761 if (flags & tf_error)
7762 error_at (loc, "use of %<this%> in a constant expression");
7763 return false;
7764 }
7765 return true;
7766 }
7767 return RECUR (x, rval);
7768 }
7769
7770 case STATEMENT_LIST:
7771 {
7772 tree_stmt_iterator i;
7773 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
7774 {
7775 if (!RECUR (tsi_stmt (i), any))
7776 return false;
7777 }
7778 return true;
7779 }
7780 break;
7781
7782 case MODIFY_EXPR:
7783 if (cxx_dialect < cxx14)
7784 goto fail;
7785 if (!RECUR (TREE_OPERAND (t, 0), any))
7786 return false;
7787 /* Just ignore clobbers. */
7788 if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
7789 return true;
7790 if (!RECUR (TREE_OPERAND (t, 1), rval))
7791 return false;
7792 return true;
7793
7794 case MODOP_EXPR:
7795 if (cxx_dialect < cxx14)
7796 goto fail;
7797 if (!RECUR (TREE_OPERAND (t, 0), rval))
7798 return false;
7799 if (!RECUR (TREE_OPERAND (t, 2), rval))
7800 return false;
7801 return true;
7802
7803 case DO_STMT:
7804 if (!RECUR (DO_COND (t), rval))
7805 return false;
7806 if (!RECUR (DO_BODY (t), any))
7807 return false;
7808 if (breaks (jump_target) || continues (jump_target))
7809 *jump_target = NULL_TREE;
7810 return true;
7811
7812 case FOR_STMT:
7813 if (!RECUR (FOR_INIT_STMT (t), any))
7814 return false;
7815 tmp = FOR_COND (t);
7816 if (!RECUR (tmp, rval))
7817 return false;
7818 if (tmp)
7819 {
7820 if (!processing_template_decl)
7821 tmp = cxx_eval_outermost_constant_expr (tmp, true);
7822 /* If we couldn't evaluate the condition, it might not ever be
7823 true. */
7824 if (!integer_onep (tmp))
7825 return true;
7826 }
7827 if (!RECUR (FOR_EXPR (t), any))
7828 return false;
7829 if (!RECUR (FOR_BODY (t), any))
7830 return false;
7831 if (breaks (jump_target) || continues (jump_target))
7832 *jump_target = NULL_TREE;
7833 return true;
7834
7835 case RANGE_FOR_STMT:
7836 if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
7837 return false;
7838 if (!RECUR (RANGE_FOR_EXPR (t), any))
7839 return false;
7840 if (!RECUR (RANGE_FOR_BODY (t), any))
7841 return false;
7842 if (breaks (jump_target) || continues (jump_target))
7843 *jump_target = NULL_TREE;
7844 return true;
7845
7846 case WHILE_STMT:
7847 tmp = WHILE_COND (t);
7848 if (!RECUR (tmp, rval))
7849 return false;
7850 if (!processing_template_decl)
7851 tmp = cxx_eval_outermost_constant_expr (tmp, true);
7852 /* If we couldn't evaluate the condition, it might not ever be true. */
7853 if (!integer_onep (tmp))
7854 return true;
7855 if (!RECUR (WHILE_BODY (t), any))
7856 return false;
7857 if (breaks (jump_target) || continues (jump_target))
7858 *jump_target = NULL_TREE;
7859 return true;
7860
7861 case SWITCH_STMT:
7862 if (!RECUR (SWITCH_STMT_COND (t), rval))
7863 return false;
7864 /* FIXME we don't check SWITCH_STMT_BODY currently, because even
7865 unreachable labels would be checked and it is enough if there is
7866 a single switch cond value for which it is a valid constant
7867 expression. We need to check if there are any RETURN_EXPRs
7868 or CONTINUE_STMTs inside of the body though, as in that case
7869 we need to set *jump_target. */
7870 else
7871 {
7872 hash_set<tree> pset;
7873 check_for_return_continue_data data = { &pset, NULL_TREE };
7874 if (tree ret_expr
7875 = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
7876 &data, &pset))
7877 /* The switch might return. */
7878 *jump_target = ret_expr;
7879 else if (data.continue_stmt)
7880 /* The switch can't return, but might continue. */
7881 *jump_target = data.continue_stmt;
7882 }
7883 return true;
7884
7885 case STMT_EXPR:
7886 return RECUR (STMT_EXPR_STMT (t), rval);
7887
7888 case LAMBDA_EXPR:
7889 if (cxx_dialect >= cxx17)
7890 /* In C++17 lambdas can be constexpr, don't give up yet. */
7891 return true;
7892 else if (flags & tf_error)
7893 error_at (loc, "lambda-expression is not a constant expression "
7894 "before C++17");
7895 return false;
7896
7897 case DYNAMIC_CAST_EXPR:
7898 case PSEUDO_DTOR_EXPR:
7899 case NEW_EXPR:
7900 case VEC_NEW_EXPR:
7901 case DELETE_EXPR:
7902 case VEC_DELETE_EXPR:
7903 case THROW_EXPR:
7904 case OMP_PARALLEL:
7905 case OMP_TASK:
7906 case OMP_FOR:
7907 case OMP_SIMD:
7908 case OMP_DISTRIBUTE:
7909 case OMP_TASKLOOP:
7910 case OMP_LOOP:
7911 case OMP_TEAMS:
7912 case OMP_TARGET_DATA:
7913 case OMP_TARGET:
7914 case OMP_SECTIONS:
7915 case OMP_ORDERED:
7916 case OMP_CRITICAL:
7917 case OMP_SINGLE:
7918 case OMP_SECTION:
7919 case OMP_MASTER:
7920 case OMP_TASKGROUP:
7921 case OMP_TARGET_UPDATE:
7922 case OMP_TARGET_ENTER_DATA:
7923 case OMP_TARGET_EXIT_DATA:
7924 case OMP_ATOMIC:
7925 case OMP_ATOMIC_READ:
7926 case OMP_ATOMIC_CAPTURE_OLD:
7927 case OMP_ATOMIC_CAPTURE_NEW:
7928 case OMP_DEPOBJ:
7929 case OACC_PARALLEL:
7930 case OACC_KERNELS:
7931 case OACC_SERIAL:
7932 case OACC_DATA:
7933 case OACC_HOST_DATA:
7934 case OACC_LOOP:
7935 case OACC_CACHE:
7936 case OACC_DECLARE:
7937 case OACC_ENTER_DATA:
7938 case OACC_EXIT_DATA:
7939 case OACC_UPDATE:
7940 /* GCC internal stuff. */
7941 case VA_ARG_EXPR:
7942 case TRANSACTION_EXPR:
7943 case AT_ENCODE_EXPR:
7944 fail:
7945 if (flags & tf_error)
7946 error_at (loc, "expression %qE is not a constant expression", t);
7947 return false;
7948
7949 case ASM_EXPR:
7950 if (flags & tf_error)
7951 inline_asm_in_constexpr_error (loc);
7952 return false;
7953
7954 case OBJ_TYPE_REF:
7955 if (cxx_dialect >= cxx20)
7956 /* In C++20 virtual calls can be constexpr, don't give up yet. */
7957 return true;
7958 else if (flags & tf_error)
7959 error_at (loc,
7960 "virtual functions cannot be %<constexpr%> before C++20");
7961 return false;
7962
7963 case TYPEID_EXPR:
7964 /* In C++20, a typeid expression whose operand is of polymorphic
7965 class type can be constexpr. */
7966 {
7967 tree e = TREE_OPERAND (t, 0);
7968 if (cxx_dialect < cxx20
7969 && strict
7970 && !TYPE_P (e)
7971 && !type_dependent_expression_p (e)
7972 && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
7973 {
7974 if (flags & tf_error)
7975 error_at (loc, "%<typeid%> is not a constant expression "
7976 "because %qE is of polymorphic type", e);
7977 return false;
7978 }
7979 return true;
7980 }
7981
7982 case POINTER_DIFF_EXPR:
7983 case MINUS_EXPR:
7984 want_rval = true;
7985 goto binary;
7986
7987 case LT_EXPR:
7988 case LE_EXPR:
7989 case GT_EXPR:
7990 case GE_EXPR:
7991 case EQ_EXPR:
7992 case NE_EXPR:
7993 case SPACESHIP_EXPR:
7994 want_rval = true;
7995 goto binary;
7996
7997 case PREINCREMENT_EXPR:
7998 case POSTINCREMENT_EXPR:
7999 case PREDECREMENT_EXPR:
8000 case POSTDECREMENT_EXPR:
8001 if (cxx_dialect < cxx14)
8002 goto fail;
8003 goto unary;
8004
8005 case BIT_NOT_EXPR:
8006 /* A destructor. */
8007 if (TYPE_P (TREE_OPERAND (t, 0)))
8008 return true;
8009 /* fall through. */
8010
8011 case CONJ_EXPR:
8012 case SAVE_EXPR:
8013 case FIX_TRUNC_EXPR:
8014 case FLOAT_EXPR:
8015 case NEGATE_EXPR:
8016 case ABS_EXPR:
8017 case ABSU_EXPR:
8018 case TRUTH_NOT_EXPR:
8019 case FIXED_CONVERT_EXPR:
8020 case UNARY_PLUS_EXPR:
8021 case UNARY_LEFT_FOLD_EXPR:
8022 case UNARY_RIGHT_FOLD_EXPR:
8023 unary:
8024 return RECUR (TREE_OPERAND (t, 0), rval);
8025
8026 case CAST_EXPR:
8027 case CONST_CAST_EXPR:
8028 case STATIC_CAST_EXPR:
8029 case REINTERPRET_CAST_EXPR:
8030 case IMPLICIT_CONV_EXPR:
8031 if (cxx_dialect < cxx11
8032 && !dependent_type_p (TREE_TYPE (t))
8033 && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t)))
8034 /* In C++98, a conversion to non-integral type can't be part of a
8035 constant expression. */
8036 {
8037 if (flags & tf_error)
8038 error_at (loc,
8039 "cast to non-integral type %qT in a constant expression",
8040 TREE_TYPE (t));
8041 return false;
8042 }
8043 /* This might be a conversion from a class to a (potentially) literal
8044 type. Let's consider it potentially constant since the conversion
8045 might be a constexpr user-defined conversion. */
8046 else if (cxx_dialect >= cxx11
8047 && (dependent_type_p (TREE_TYPE (t))
8048 || !COMPLETE_TYPE_P (TREE_TYPE (t))
8049 || literal_type_p (TREE_TYPE (t)))
8050 && TREE_OPERAND (t, 0))
8051 {
8052 tree type = TREE_TYPE (TREE_OPERAND (t, 0));
8053 /* If this is a dependent type, it could end up being a class
8054 with conversions. */
8055 if (type == NULL_TREE || WILDCARD_TYPE_P (type))
8056 return true;
8057 /* Or a non-dependent class which has conversions. */
8058 else if (CLASS_TYPE_P (type)
8059 && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
8060 return true;
8061 }
8062
8063 return (RECUR (TREE_OPERAND (t, 0),
8064 !TYPE_REF_P (TREE_TYPE (t))));
8065
8066 case BIND_EXPR:
8067 return RECUR (BIND_EXPR_BODY (t), want_rval);
8068
8069 case CLEANUP_POINT_EXPR:
8070 case MUST_NOT_THROW_EXPR:
8071 case TRY_CATCH_EXPR:
8072 case TRY_BLOCK:
8073 case EH_SPEC_BLOCK:
8074 case EXPR_STMT:
8075 case PAREN_EXPR:
8076 case NON_DEPENDENT_EXPR:
8077 /* For convenience. */
8078 case LOOP_EXPR:
8079 case EXIT_EXPR:
8080 return RECUR (TREE_OPERAND (t, 0), want_rval);
8081
8082 case DECL_EXPR:
8083 tmp = DECL_EXPR_DECL (t);
8084 if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp))
8085 {
8086 if (TREE_STATIC (tmp))
8087 {
8088 if (flags & tf_error)
8089 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
8090 "%<static%> in %<constexpr%> context", tmp);
8091 return false;
8092 }
8093 else if (CP_DECL_THREAD_LOCAL_P (tmp))
8094 {
8095 if (flags & tf_error)
8096 error_at (DECL_SOURCE_LOCATION (tmp), "%qD declared "
8097 "%<thread_local%> in %<constexpr%> context", tmp);
8098 return false;
8099 }
8100 else if (!check_for_uninitialized_const_var
8101 (tmp, /*constexpr_context_p=*/true, flags))
8102 return false;
8103 }
8104 return RECUR (tmp, want_rval);
8105
8106 case TRY_FINALLY_EXPR:
8107 return (RECUR (TREE_OPERAND (t, 0), want_rval)
8108 && RECUR (TREE_OPERAND (t, 1), any));
8109
8110 case SCOPE_REF:
8111 return RECUR (TREE_OPERAND (t, 1), want_rval);
8112
8113 case TARGET_EXPR:
8114 if (!TARGET_EXPR_DIRECT_INIT_P (t)
8115 && !literal_type_p (TREE_TYPE (t)))
8116 {
8117 if (flags & tf_error)
8118 {
8119 auto_diagnostic_group d;
8120 error_at (loc, "temporary of non-literal type %qT in a "
8121 "constant expression", TREE_TYPE (t));
8122 explain_non_literal_class (TREE_TYPE (t));
8123 }
8124 return false;
8125 }
8126 /* FALLTHRU */
8127 case INIT_EXPR:
8128 return RECUR (TREE_OPERAND (t, 1), rval);
8129
8130 case CONSTRUCTOR:
8131 {
8132 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
8133 constructor_elt *ce;
8134 for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
8135 if (!RECUR (ce->value, want_rval))
8136 return false;
8137 return true;
8138 }
8139
8140 case TREE_LIST:
8141 {
8142 gcc_assert (TREE_PURPOSE (t) == NULL_TREE
8143 || DECL_P (TREE_PURPOSE (t)));
8144 if (!RECUR (TREE_VALUE (t), want_rval))
8145 return false;
8146 if (TREE_CHAIN (t) == NULL_TREE)
8147 return true;
8148 return RECUR (TREE_CHAIN (t), want_rval);
8149 }
8150
8151 case TRUNC_DIV_EXPR:
8152 case CEIL_DIV_EXPR:
8153 case FLOOR_DIV_EXPR:
8154 case ROUND_DIV_EXPR:
8155 case TRUNC_MOD_EXPR:
8156 case CEIL_MOD_EXPR:
8157 case ROUND_MOD_EXPR:
8158 {
8159 tree denom = TREE_OPERAND (t, 1);
8160 if (!RECUR (denom, rval))
8161 return false;
8162 /* We can't call cxx_eval_outermost_constant_expr on an expression
8163 that hasn't been through instantiate_non_dependent_expr yet. */
8164 if (!processing_template_decl)
8165 denom = cxx_eval_outermost_constant_expr (denom, true);
8166 if (integer_zerop (denom))
8167 {
8168 if (flags & tf_error)
8169 error ("division by zero is not a constant expression");
8170 return false;
8171 }
8172 else
8173 {
8174 want_rval = true;
8175 return RECUR (TREE_OPERAND (t, 0), want_rval);
8176 }
8177 }
8178
8179 case COMPOUND_EXPR:
8180 {
8181 /* check_return_expr sometimes wraps a TARGET_EXPR in a
8182 COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
8183 introduced by build_call_a. */
8184 tree op0 = TREE_OPERAND (t, 0);
8185 tree op1 = TREE_OPERAND (t, 1);
8186 STRIP_NOPS (op1);
8187 if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
8188 || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
8189 return RECUR (op0, want_rval);
8190 else
8191 goto binary;
8192 }
8193
8194 /* If the first operand is the non-short-circuit constant, look at
8195 the second operand; otherwise we only care about the first one for
8196 potentiality. */
8197 case TRUTH_AND_EXPR:
8198 case TRUTH_ANDIF_EXPR:
8199 tmp = boolean_true_node;
8200 goto truth;
8201 case TRUTH_OR_EXPR:
8202 case TRUTH_ORIF_EXPR:
8203 tmp = boolean_false_node;
8204 truth:
8205 {
8206 tree op = TREE_OPERAND (t, 0);
8207 if (!RECUR (op, rval))
8208 return false;
8209 if (!processing_template_decl)
8210 op = cxx_eval_outermost_constant_expr (op, true);
8211 if (tree_int_cst_equal (op, tmp))
8212 return RECUR (TREE_OPERAND (t, 1), rval);
8213 else
8214 return true;
8215 }
8216
8217 case PLUS_EXPR:
8218 case MULT_EXPR:
8219 case POINTER_PLUS_EXPR:
8220 case RDIV_EXPR:
8221 case EXACT_DIV_EXPR:
8222 case MIN_EXPR:
8223 case MAX_EXPR:
8224 case LSHIFT_EXPR:
8225 case RSHIFT_EXPR:
8226 case LROTATE_EXPR:
8227 case RROTATE_EXPR:
8228 case BIT_IOR_EXPR:
8229 case BIT_XOR_EXPR:
8230 case BIT_AND_EXPR:
8231 case TRUTH_XOR_EXPR:
8232 case UNORDERED_EXPR:
8233 case ORDERED_EXPR:
8234 case UNLT_EXPR:
8235 case UNLE_EXPR:
8236 case UNGT_EXPR:
8237 case UNGE_EXPR:
8238 case UNEQ_EXPR:
8239 case LTGT_EXPR:
8240 case RANGE_EXPR:
8241 case COMPLEX_EXPR:
8242 want_rval = true;
8243 /* Fall through. */
8244 case ARRAY_REF:
8245 case ARRAY_RANGE_REF:
8246 case MEMBER_REF:
8247 case DOTSTAR_EXPR:
8248 case MEM_REF:
8249 case BINARY_LEFT_FOLD_EXPR:
8250 case BINARY_RIGHT_FOLD_EXPR:
8251 binary:
8252 for (i = 0; i < 2; ++i)
8253 if (!RECUR (TREE_OPERAND (t, i), want_rval))
8254 return false;
8255 return true;
8256
8257 case VEC_PERM_EXPR:
8258 for (i = 0; i < 3; ++i)
8259 if (!RECUR (TREE_OPERAND (t, i), true))
8260 return false;
8261 return true;
8262
8263 case COND_EXPR:
8264 if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
8265 {
8266 if (flags & tf_error)
8267 error_at (loc, "%<delete[]%> is not a constant expression");
8268 return false;
8269 }
8270 /* Fall through. */
8271 case IF_STMT:
8272 case VEC_COND_EXPR:
8273 /* If the condition is a known constant, we know which of the legs we
8274 care about; otherwise we only require that the condition and
8275 either of the legs be potentially constant. */
8276 tmp = TREE_OPERAND (t, 0);
8277 if (!RECUR (tmp, rval))
8278 return false;
8279 if (!processing_template_decl)
8280 tmp = cxx_eval_outermost_constant_expr (tmp, true);
8281 if (integer_zerop (tmp))
8282 return RECUR (TREE_OPERAND (t, 2), want_rval);
8283 else if (TREE_CODE (tmp) == INTEGER_CST)
8284 return RECUR (TREE_OPERAND (t, 1), want_rval);
8285 for (i = 1; i < 3; ++i)
8286 if (potential_constant_expression_1 (TREE_OPERAND (t, i),
8287 want_rval, strict, now,
8288 tf_none, jump_target))
8289 return true;
8290 if (flags & tf_error)
8291 error_at (loc, "expression %qE is not a constant expression", t);
8292 return false;
8293
8294 case VEC_INIT_EXPR:
8295 if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
8296 return true;
8297 if (flags & tf_error)
8298 {
8299 error_at (loc, "non-constant array initialization");
8300 diagnose_non_constexpr_vec_init (t);
8301 }
8302 return false;
8303
8304 case TYPE_DECL:
8305 case TAG_DEFN:
8306 /* We can see these in statement-expressions. */
8307 return true;
8308
8309 case CLEANUP_STMT:
8310 if (!RECUR (CLEANUP_BODY (t), any))
8311 return false;
8312 if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
8313 return false;
8314 return true;
8315
8316 case EMPTY_CLASS_EXPR:
8317 return false;
8318
8319 case GOTO_EXPR:
8320 {
8321 tree *target = &TREE_OPERAND (t, 0);
8322 /* Gotos representing break and continue are OK. */
8323 if (breaks (target) || continues (target))
8324 {
8325 *jump_target = *target;
8326 return true;
8327 }
8328 if (flags & tf_error)
8329 error_at (loc, "%<goto%> is not a constant expression");
8330 return false;
8331 }
8332
8333 case ANNOTATE_EXPR:
8334 return RECUR (TREE_OPERAND (t, 0), rval);
8335
8336 /* Coroutine await, yield and return expressions are not. */
8337 case CO_AWAIT_EXPR:
8338 case CO_YIELD_EXPR:
8339 case CO_RETURN_EXPR:
8340 return false;
8341
8342 default:
8343 if (objc_is_property_ref (t))
8344 return false;
8345
8346 sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
8347 gcc_unreachable ();
8348 return false;
8349 }
8350 #undef RECUR
8351 }
8352
8353 bool
8354 potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
8355 tsubst_flags_t flags)
8356 {
8357 tree target = NULL_TREE;
8358 return potential_constant_expression_1 (t, want_rval, strict, now,
8359 flags, &target);
8360 }
8361
8362 /* The main entry point to the above. */
8363
8364 bool
8365 potential_constant_expression (tree t)
8366 {
8367 return potential_constant_expression_1 (t, false, true, false, tf_none);
8368 }
8369
8370 /* As above, but require a constant rvalue. */
8371
8372 bool
8373 potential_rvalue_constant_expression (tree t)
8374 {
8375 return potential_constant_expression_1 (t, true, true, false, tf_none);
8376 }
8377
8378 /* Like above, but complain about non-constant expressions. */
8379
8380 bool
8381 require_potential_constant_expression (tree t)
8382 {
8383 return potential_constant_expression_1 (t, false, true, false,
8384 tf_warning_or_error);
8385 }
8386
8387 /* Cross product of the above. */
8388
8389 bool
8390 require_potential_rvalue_constant_expression (tree t)
8391 {
8392 return potential_constant_expression_1 (t, true, true, false,
8393 tf_warning_or_error);
8394 }
8395
8396 /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
8397
8398 bool
8399 require_rvalue_constant_expression (tree t)
8400 {
8401 return potential_constant_expression_1 (t, true, true, true,
8402 tf_warning_or_error);
8403 }
8404
8405 /* Like potential_constant_expression, but don't consider possible constexpr
8406 substitution of the current function. That is, PARM_DECL qualifies under
8407 potential_constant_expression, but not here.
8408
8409 This is basically what you can check when any actual constant values might
8410 be value-dependent. */
8411
8412 bool
8413 is_constant_expression (tree t)
8414 {
8415 return potential_constant_expression_1 (t, false, true, true, tf_none);
8416 }
8417
8418 /* As above, but expect an rvalue. */
8419
8420 bool
8421 is_rvalue_constant_expression (tree t)
8422 {
8423 return potential_constant_expression_1 (t, true, true, true, tf_none);
8424 }
8425
8426 /* Like above, but complain about non-constant expressions. */
8427
8428 bool
8429 require_constant_expression (tree t)
8430 {
8431 return potential_constant_expression_1 (t, false, true, true,
8432 tf_warning_or_error);
8433 }
8434
8435 /* Like is_constant_expression, but allow const variables that are not allowed
8436 under constexpr rules. */
8437
8438 bool
8439 is_static_init_expression (tree t)
8440 {
8441 return potential_constant_expression_1 (t, false, false, true, tf_none);
8442 }
8443
8444 /* Returns true if T is a potential constant expression that is not
8445 instantiation-dependent, and therefore a candidate for constant folding even
8446 in a template. */
8447
8448 bool
8449 is_nondependent_constant_expression (tree t)
8450 {
8451 return (!type_unknown_p (t)
8452 && is_constant_expression (t)
8453 && !instantiation_dependent_expression_p (t));
8454 }
8455
8456 /* Returns true if T is a potential static initializer expression that is not
8457 instantiation-dependent. */
8458
8459 bool
8460 is_nondependent_static_init_expression (tree t)
8461 {
8462 return (!type_unknown_p (t)
8463 && is_static_init_expression (t)
8464 && !instantiation_dependent_expression_p (t));
8465 }
8466
8467 /* Finalize constexpr processing after parsing. */
8468
8469 void
8470 fini_constexpr (void)
8471 {
8472 /* The contexpr call and fundef copies tables are no longer needed. */
8473 constexpr_call_table = NULL;
8474 fundef_copies_table = NULL;
8475 }
8476
8477 #include "gt-cp-constexpr.h"