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