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