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