Line data Source code
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-2026 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 "timevar.h"
35 : #include "fold-const-call.h"
36 : #include "stor-layout.h"
37 : #include "cgraph.h"
38 : #include "opts.h"
39 : #include "stringpool.h"
40 : #include "attribs.h"
41 : #include "fold-const.h"
42 : #include "intl.h"
43 : #include "toplev.h"
44 : #include "contracts.h"
45 :
46 : static bool verify_constant (tree, bool, bool *, bool *);
47 : #define VERIFY_CONSTANT(X) \
48 : do { \
49 : if (verify_constant ((X), ctx->quiet, non_constant_p, overflow_p)) \
50 : return t; \
51 : } while (0)
52 :
53 : static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex,
54 : bool insert = false);
55 : static int array_index_cmp (tree key, tree index);
56 : static bool check_for_failed_contracts (const constexpr_ctx *ctx);
57 :
58 : /* Returns true iff FUN is an instantiation of a constexpr function
59 : template or a defaulted constexpr function. */
60 :
61 : bool
62 193523552 : is_instantiation_of_constexpr (tree fun)
63 : {
64 268011906 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
65 119534048 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
66 287703010 : || (DECL_DEFAULTED_FN (fun)
67 410 : && DECL_DECLARED_CONSTEXPR_P (fun)));
68 : }
69 :
70 : /* Return true if T is a literal type. */
71 :
72 : bool
73 192965823 : literal_type_p (tree t)
74 : {
75 194601917 : if (SCALAR_TYPE_P (t)
76 91713268 : || VECTOR_TYPE_P (t)
77 91681615 : || TYPE_REF_P (t)
78 76090781 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
79 : return true;
80 72839879 : if (CLASS_TYPE_P (t))
81 : {
82 71203573 : t = complete_type (t);
83 71203573 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
84 71203573 : return CLASSTYPE_LITERAL_P (t);
85 : }
86 1636306 : if (TREE_CODE (t) == ARRAY_TYPE)
87 1636094 : return literal_type_p (strip_array_types (t));
88 : return false;
89 : }
90 :
91 : /* If DECL is a variable declared `constexpr', require its type
92 : be literal. Return error_mark_node if we give an error, the
93 : DECL otherwise. */
94 :
95 : tree
96 336076266 : ensure_literal_type_for_constexpr_object (tree decl)
97 : {
98 336076266 : tree type = TREE_TYPE (decl);
99 336076266 : if (VAR_P (decl)
100 126950232 : && (DECL_DECLARED_CONSTEXPR_P (decl)
101 88593032 : || var_in_constexpr_fn (decl))
102 393895329 : && !processing_template_decl)
103 : {
104 37308020 : tree stype = strip_array_types (type);
105 37308020 : if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
106 : /* Don't complain here, we'll complain about incompleteness
107 : when we try to initialize the variable. */;
108 37308014 : else if (!literal_type_p (type))
109 : {
110 24115 : if (DECL_DECLARED_CONSTEXPR_P (decl))
111 : {
112 53 : auto_diagnostic_group d;
113 53 : error_at (DECL_SOURCE_LOCATION (decl),
114 : "the type %qT of %<constexpr%> variable %qD "
115 : "is not literal", type, decl);
116 53 : explain_non_literal_class (type);
117 53 : decl = error_mark_node;
118 53 : }
119 24062 : else if (cxx_dialect < cxx23)
120 : {
121 18240 : if (!is_instantiation_of_constexpr (current_function_decl))
122 : {
123 8 : auto_diagnostic_group d;
124 8 : error_at (DECL_SOURCE_LOCATION (decl),
125 : "variable %qD of non-literal type %qT in "
126 : "%<constexpr%> function only available with "
127 : "%<-std=c++23%> or %<-std=gnu++23%>", decl, type);
128 8 : explain_non_literal_class (type);
129 8 : decl = error_mark_node;
130 8 : }
131 18240 : cp_function_chain->invalid_constexpr = true;
132 : }
133 : }
134 37283899 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
135 37283899 : && variably_modified_type_p (type, NULL_TREE))
136 : {
137 4 : error_at (DECL_SOURCE_LOCATION (decl),
138 : "%<constexpr%> variable %qD has variably-modified "
139 : "type %qT", decl, type);
140 4 : decl = error_mark_node;
141 : }
142 : }
143 336076266 : return decl;
144 : }
145 :
146 : /* Issue a diagnostic with text GMSGID for constructs that are invalid in
147 : constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking
148 : a constexpr function body; if so, don't report hard errors and issue
149 : a pedwarn pre-C++23, or a warning in C++23, if requested by
150 : -Winvalid-constexpr. Otherwise, we're not in the context where we are
151 : checking if a function can be marked 'constexpr', so give a hard error. */
152 :
153 : ATTRIBUTE_GCC_DIAG(3,4)
154 : static bool
155 1070 : constexpr_error (location_t location, bool constexpr_fundef_p,
156 : const char *gmsgid, ...)
157 : {
158 1070 : diagnostics::diagnostic_info diagnostic;
159 1070 : va_list ap;
160 1070 : rich_location richloc (line_table, location);
161 1070 : va_start (ap, gmsgid);
162 1070 : bool ret;
163 1070 : if (!constexpr_fundef_p)
164 : {
165 : /* Report an error that cannot be suppressed. */
166 764 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
167 : diagnostics::kind::error);
168 764 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
169 : }
170 306 : else if (warn_invalid_constexpr)
171 : {
172 163 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
173 163 : (cxx_dialect < cxx23
174 : ? diagnostics::kind::pedwarn
175 : : diagnostics::kind::warning));
176 163 : diagnostic.m_option_id = OPT_Winvalid_constexpr;
177 163 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
178 : }
179 : else
180 : ret = false;
181 1070 : va_end (ap);
182 2140 : return ret;
183 1070 : }
184 :
185 : struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
186 : {
187 : static hashval_t hash (const constexpr_fundef *);
188 : static bool equal (const constexpr_fundef *, const constexpr_fundef *);
189 : };
190 :
191 : /* This table holds all constexpr function definitions seen in
192 : the current translation unit. */
193 :
194 : static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
195 :
196 : /* Utility function used for managing the constexpr function table.
197 : Return true if the entries pointed to by P and Q are for the
198 : same constexpr function. */
199 :
200 : inline bool
201 903154297 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
202 : const constexpr_fundef *rhs)
203 : {
204 875488751 : return lhs->decl == rhs->decl;
205 : }
206 :
207 : /* Utility function used for managing the constexpr function table.
208 : Return a hash value for the entry pointed to by Q. */
209 :
210 : inline hashval_t
211 868827498 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
212 : {
213 868827498 : return DECL_UID (fundef->decl);
214 : }
215 :
216 : /* Return a previously saved definition of function FUN. */
217 :
218 : constexpr_fundef *
219 110561378 : retrieve_constexpr_fundef (tree fun)
220 : {
221 110561378 : if (constexpr_fundef_table == NULL)
222 : return NULL;
223 :
224 110556930 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
225 110556930 : return constexpr_fundef_table->find (&fundef);
226 : }
227 :
228 : /* Check whether the parameter and return types of FUN are valid for a
229 : constexpr function, and complain if COMPLAIN. */
230 :
231 : bool
232 31705234 : is_valid_constexpr_fn (tree fun, bool complain)
233 : {
234 31705234 : bool ret = true;
235 :
236 63410468 : if (DECL_INHERITED_CTOR (fun)
237 3679921 : && TREE_CODE (fun) == TEMPLATE_DECL)
238 : {
239 0 : ret = false;
240 0 : if (complain)
241 0 : error ("inherited constructor %qD is not %<constexpr%>",
242 0 : DECL_INHERITED_CTOR (fun));
243 : }
244 : else
245 : {
246 31705234 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
247 63896792 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
248 32191558 : if (!literal_type_p (TREE_TYPE (parm)))
249 : {
250 13318 : ret = false;
251 13318 : if (complain)
252 : {
253 23 : auto_diagnostic_group d;
254 23 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
255 : "invalid type for parameter %d of "
256 : "%<constexpr%> function %q+#D",
257 23 : DECL_PARM_INDEX (parm), fun))
258 15 : explain_non_literal_class (TREE_TYPE (parm));
259 23 : }
260 : }
261 : }
262 :
263 52455643 : if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
264 : {
265 4 : ret = false;
266 4 : if (complain)
267 3 : inform (DECL_SOURCE_LOCATION (fun),
268 : "lambdas are implicitly %<constexpr%> only in C++17 and later");
269 : }
270 63410460 : else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20)
271 : {
272 0 : ret = false;
273 0 : if (complain)
274 0 : error_at (DECL_SOURCE_LOCATION (fun),
275 : "%<constexpr%> destructors only available with "
276 : "%<-std=c++20%> or %<-std=gnu++20%>");
277 : }
278 31705230 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
279 : {
280 27356140 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
281 27356140 : if (!literal_type_p (rettype))
282 : {
283 50685 : ret = false;
284 50685 : if (complain)
285 : {
286 21 : auto_diagnostic_group d;
287 21 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
288 : "invalid return type %qT of %<constexpr%> "
289 : "function %q+D", rettype, fun))
290 10 : explain_non_literal_class (rettype);
291 21 : }
292 : }
293 :
294 : /* C++14 DR 1684 removed this restriction. */
295 27356140 : if (cxx_dialect < cxx14
296 33089 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
297 27357271 : && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
298 : {
299 1 : ret = false;
300 1 : if (complain)
301 : {
302 1 : auto_diagnostic_group d;
303 1 : if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
304 : "enclosing class of %<constexpr%> non-static"
305 : " member function %q+#D is not a literal type",
306 : fun))
307 1 : explain_non_literal_class (DECL_CONTEXT (fun));
308 1 : }
309 : }
310 : }
311 4349090 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)) && cxx_dialect < cxx26)
312 : {
313 69 : ret = false;
314 69 : if (complain)
315 : {
316 10 : if (DECL_CONSTRUCTOR_P (fun))
317 8 : error ("%<constexpr%> constructor in %q#T that has "
318 : "virtual base classes only available with "
319 8 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
320 : else
321 2 : error ("%<constexpr%> destructor in %q#T that has "
322 : "virtual base classes only available with "
323 2 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
324 : }
325 : }
326 :
327 31705234 : return ret;
328 : }
329 :
330 : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
331 : for a member of an anonymous aggregate, INIT is the initializer for that
332 : member, and VEC_OUTER is the vector of constructor elements for the class
333 : whose constructor we are processing. Add the initializer to the vector
334 : and return true to indicate success. */
335 :
336 : static bool
337 4637 : build_anon_member_initialization (tree member, tree init,
338 : vec<constructor_elt, va_gc> **vec_outer)
339 : {
340 : /* MEMBER presents the relevant fields from the inside out, but we need
341 : to build up the initializer from the outside in so that we can reuse
342 : previously built CONSTRUCTORs if this is, say, the second field in an
343 : anonymous struct. So we use a vec as a stack. */
344 4637 : auto_vec<tree, 2> fields;
345 9370 : do
346 : {
347 9370 : fields.safe_push (TREE_OPERAND (member, 1));
348 9370 : member = TREE_OPERAND (member, 0);
349 : }
350 9370 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
351 18746 : && TREE_CODE (member) == COMPONENT_REF);
352 :
353 : /* VEC has the constructor elements vector for the context of FIELD.
354 : If FIELD is an anonymous aggregate, we will push inside it. */
355 : vec<constructor_elt, va_gc> **vec = vec_outer;
356 : tree field;
357 9370 : while (field = fields.pop(),
358 9370 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
359 : {
360 4733 : tree ctor;
361 : /* If there is already an outer constructor entry for the anonymous
362 : aggregate FIELD, use it; otherwise, insert one. */
363 4733 : if (vec_safe_is_empty (*vec)
364 198 : || (*vec)->last().index != field)
365 : {
366 4631 : ctor = build_constructor (TREE_TYPE (field), NULL);
367 4631 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
368 : }
369 : else
370 102 : ctor = (*vec)->last().value;
371 4733 : vec = &CONSTRUCTOR_ELTS (ctor);
372 : }
373 :
374 : /* Now we're at the innermost field, the one that isn't an anonymous
375 : aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
376 4637 : gcc_assert (fields.is_empty());
377 4637 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
378 :
379 4637 : return true;
380 4637 : }
381 :
382 : /* Subroutine of build_constexpr_constructor_member_initializers.
383 : The expression tree T represents a data member initialization
384 : in a (constexpr) constructor definition. Build a pairing of
385 : the data member with its initializer, and prepend that pair
386 : to the existing initialization pair INITS. */
387 :
388 : static bool
389 6075044 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
390 : {
391 6781568 : tree member, init;
392 6781568 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
393 3091472 : t = TREE_OPERAND (t, 0);
394 6781568 : if (TREE_CODE (t) == EXPR_STMT)
395 3091408 : t = TREE_OPERAND (t, 0);
396 6781568 : if (t == error_mark_node)
397 : return false;
398 6781559 : if (TREE_CODE (t) == STATEMENT_LIST)
399 : {
400 1601619 : for (tree stmt : tsi_range (t))
401 898874 : if (! build_data_member_initialization (stmt, vec))
402 6075044 : return false;
403 : return true;
404 : }
405 6078814 : if (TREE_CODE (t) == CLEANUP_STMT)
406 : {
407 : /* We can't see a CLEANUP_STMT in a constructor for a literal class,
408 : but we can in a constexpr constructor for a non-literal class. Just
409 : ignore it; either all the initialization will be constant, in which
410 : case the cleanup can't run, or it can't be constexpr.
411 : Still recurse into CLEANUP_BODY. */
412 672593 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
413 : }
414 5406221 : if (TREE_CODE (t) == CONVERT_EXPR)
415 3018767 : t = TREE_OPERAND (t, 0);
416 5406221 : if (TREE_CODE (t) == INIT_EXPR)
417 : {
418 2870656 : member = TREE_OPERAND (t, 0);
419 2870656 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
420 : }
421 2535565 : else if (TREE_CODE (t) == CALL_EXPR)
422 : {
423 2059063 : tree fn = get_callee_fndecl (t);
424 4118126 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
425 : /* We're only interested in calls to subobject constructors. */
426 : return true;
427 1653159 : member = CALL_EXPR_ARG (t, 0);
428 : /* We don't use build_cplus_new here because it complains about
429 : abstract bases. Leaving the call unwrapped means that it has the
430 : wrong type, but cxx_eval_constant_expression doesn't care. */
431 1653159 : init = break_out_target_exprs (t);
432 : }
433 476502 : else if (TREE_CODE (t) == BIND_EXPR)
434 33931 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
435 : else
436 : /* Don't add anything else to the CONSTRUCTOR. */
437 : return true;
438 4523815 : if (INDIRECT_REF_P (member))
439 62289 : member = TREE_OPERAND (member, 0);
440 4523815 : if (TREE_CODE (member) == NOP_EXPR)
441 : {
442 556494 : tree op = member;
443 556494 : STRIP_NOPS (op);
444 556494 : if (TREE_CODE (op) == ADDR_EXPR)
445 : {
446 658 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
447 : (TREE_TYPE (TREE_TYPE (op)),
448 : TREE_TYPE (TREE_TYPE (member))));
449 : /* Initializing a cv-qualified member; we need to look through
450 : the const_cast. */
451 : member = op;
452 : }
453 555836 : else if (op == current_class_ptr
454 1111515 : && (same_type_ignoring_top_level_qualifiers_p
455 555679 : (TREE_TYPE (TREE_TYPE (member)),
456 : current_class_type)))
457 : /* Delegating constructor. */
458 : member = op;
459 : else
460 : {
461 : /* This is an initializer for an empty base; keep it for now so
462 : we can check it in cxx_eval_bare_aggregate. */
463 477019 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
464 : }
465 : }
466 4523815 : if (TREE_CODE (member) == ADDR_EXPR)
467 1159612 : member = TREE_OPERAND (member, 0);
468 4523815 : if (TREE_CODE (member) == COMPONENT_REF)
469 : {
470 3942467 : tree aggr = TREE_OPERAND (member, 0);
471 3942467 : if (TREE_CODE (aggr) == VAR_DECL)
472 : /* Initializing a local variable, don't add anything. */
473 : return true;
474 3942465 : if (TREE_CODE (aggr) != COMPONENT_REF)
475 : /* Normal member initialization. */
476 3937825 : member = TREE_OPERAND (member, 1);
477 4640 : else if (VAR_P (get_base_address (aggr)))
478 : /* Initializing a local variable, don't add anything. */
479 : return true;
480 4637 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
481 : /* Initializing a member of an anonymous union. */
482 4637 : return build_anon_member_initialization (member, init, vec);
483 : else
484 : /* We're initializing a vtable pointer in a base. Leave it as
485 : COMPONENT_REF so we remember the path to get to the vfield. */
486 0 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
487 : }
488 :
489 : /* Value-initialization can produce multiple initializers for the
490 : same field; use the last one. */
491 5641353 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
492 1324 : (*vec)->last().value = init;
493 : else
494 4517849 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
495 : return true;
496 : }
497 :
498 : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
499 : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
500 : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
501 :
502 : static bool
503 3792 : check_constexpr_bind_expr_vars (tree t)
504 : {
505 3792 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
506 :
507 4630 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
508 850 : if (TREE_CODE (var) == TYPE_DECL
509 831 : && DECL_IMPLICIT_TYPEDEF_P (var)
510 872 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
511 : return false;
512 : return true;
513 : }
514 :
515 : /* Subroutine of check_constexpr_ctor_body. */
516 :
517 : static bool
518 8667 : check_constexpr_ctor_body_1 (tree last, tree list)
519 : {
520 8667 : switch (TREE_CODE (list))
521 : {
522 6 : case DECL_EXPR:
523 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
524 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
525 : return true;
526 : return false;
527 :
528 4 : case CLEANUP_POINT_EXPR:
529 4 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
530 4 : /*complain=*/false);
531 :
532 3449 : case BIND_EXPR:
533 3449 : if (!check_constexpr_bind_expr_vars (list)
534 3449 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
535 : /*complain=*/false))
536 : return false;
537 : return true;
538 :
539 : case USING_STMT:
540 : case STATIC_ASSERT:
541 : case DEBUG_BEGIN_STMT:
542 : return true;
543 :
544 : default:
545 : return false;
546 : }
547 : }
548 :
549 : /* Make sure that there are no statements after LAST in the constructor
550 : body represented by LIST. */
551 :
552 : bool
553 6019447 : check_constexpr_ctor_body (tree last, tree list, bool complain)
554 : {
555 : /* C++14 doesn't require a constexpr ctor to have an empty body. */
556 6019447 : if (cxx_dialect >= cxx14)
557 : return true;
558 :
559 15368 : bool ok = true;
560 15368 : if (TREE_CODE (list) == STATEMENT_LIST)
561 : {
562 13950 : tree_stmt_iterator i = tsi_last (list);
563 21128 : for (; !tsi_end_p (i); tsi_prev (&i))
564 : {
565 18967 : tree t = tsi_stmt (i);
566 18967 : if (t == last)
567 : break;
568 7249 : if (!check_constexpr_ctor_body_1 (last, t))
569 : {
570 : ok = false;
571 : break;
572 : }
573 : }
574 : }
575 1418 : else if (list != last
576 1418 : && !check_constexpr_ctor_body_1 (last, list))
577 : ok = false;
578 15368 : if (!ok && complain)
579 : {
580 50 : pedwarn (input_location, OPT_Wc__14_extensions,
581 : "%<constexpr%> constructor does not have empty body");
582 50 : ok = true;
583 : }
584 : return ok;
585 : }
586 :
587 : /* V is a vector of constructor elements built up for the base and member
588 : initializers of a constructor for TYPE. They need to be in increasing
589 : offset order, which they might not be yet if TYPE has a primary base
590 : which is not first in the base-clause or a vptr and at least one base
591 : all of which are non-primary. */
592 :
593 : static vec<constructor_elt, va_gc> *
594 3601431 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
595 : {
596 3601431 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
597 3601431 : tree field_type;
598 3601431 : unsigned i;
599 3601431 : constructor_elt *ce;
600 :
601 3601431 : if (pri)
602 91222 : field_type = BINFO_TYPE (pri);
603 3510209 : else if (TYPE_CONTAINS_VPTR_P (type))
604 37719 : field_type = vtbl_ptr_type_node;
605 : else
606 : return v;
607 :
608 : /* Find the element for the primary base or vptr and move it to the
609 : beginning of the vec. */
610 170604 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
611 132761 : if (TREE_TYPE (ce->index) == field_type)
612 : break;
613 :
614 151197 : if (i > 0 && i < vec_safe_length (v))
615 : {
616 34 : vec<constructor_elt, va_gc> &vref = *v;
617 34 : constructor_elt elt = vref[i];
618 68 : for (; i > 0; --i)
619 34 : vref[i] = vref[i-1];
620 34 : vref[0] = elt;
621 : }
622 :
623 : return v;
624 : }
625 :
626 : /* Build compile-time evalable representations of member-initializer list
627 : for a constexpr constructor. */
628 :
629 : static tree
630 3679049 : build_constexpr_constructor_member_initializers (tree type, tree body)
631 : {
632 3679049 : vec<constructor_elt, va_gc> *vec = NULL;
633 3679049 : bool ok = true;
634 5731922 : while (true)
635 5731922 : switch (TREE_CODE (body))
636 : {
637 2052794 : case MUST_NOT_THROW_EXPR:
638 2052794 : case EH_SPEC_BLOCK:
639 2052794 : case TRY_FINALLY_EXPR: // For C++26 postconditions.
640 2052794 : body = TREE_OPERAND (body, 0);
641 2052794 : break;
642 :
643 79 : case STATEMENT_LIST:
644 2053058 : for (tree stmt : tsi_range (body))
645 : {
646 161 : body = stmt;
647 161 : if (TREE_CODE (body) == BIND_EXPR)
648 : break;
649 : }
650 : break;
651 :
652 3679049 : case BIND_EXPR:
653 3679049 : body = BIND_EXPR_BODY (body);
654 3679049 : goto found;
655 :
656 0 : default:
657 0 : gcc_unreachable ();
658 : }
659 3679049 : found:
660 3679049 : if (TREE_CODE (body) == TRY_BLOCK)
661 : {
662 31 : body = TREE_OPERAND (body, 0);
663 31 : if (TREE_CODE (body) == BIND_EXPR)
664 31 : body = BIND_EXPR_BODY (body);
665 : }
666 3679049 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
667 : {
668 1986057 : body = TREE_OPERAND (body, 0);
669 1986057 : if (TREE_CODE (body) == EXPR_STMT)
670 1986021 : body = TREE_OPERAND (body, 0);
671 1986057 : if (TREE_CODE (body) == INIT_EXPR
672 1986057 : && (same_type_ignoring_top_level_qualifiers_p
673 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
674 : current_class_type)))
675 : {
676 : /* Trivial copy. */
677 0 : return TREE_OPERAND (body, 1);
678 : }
679 1986057 : ok = build_data_member_initialization (body, &vec);
680 : }
681 1692992 : else if (TREE_CODE (body) == STATEMENT_LIST)
682 : {
683 4871966 : for (tree stmt : tsi_range (body))
684 : {
685 3184544 : ok = build_data_member_initialization (stmt, &vec);
686 3184544 : if (!ok)
687 : break;
688 : }
689 : }
690 5569 : else if (EXPR_P (body))
691 5569 : ok = build_data_member_initialization (body, &vec);
692 : else
693 0 : gcc_assert (errorcount > 0);
694 3679049 : if (ok)
695 : {
696 3679040 : if (vec_safe_length (vec) > 0)
697 : {
698 : /* In a delegating constructor, return the target. */
699 3401504 : constructor_elt *ce = &(*vec)[0];
700 3401504 : if (ce->index == current_class_ptr)
701 : {
702 77609 : body = ce->value;
703 77609 : vec_free (vec);
704 77609 : return body;
705 : }
706 : }
707 3601431 : vec = sort_constexpr_mem_initializers (type, vec);
708 3601431 : return build_constructor (type, vec);
709 : }
710 : else
711 9 : return error_mark_node;
712 : }
713 :
714 : /* We have an expression tree T that represents a call, either CALL_EXPR
715 : or AGGR_INIT_EXPR. If the call is lexically to a named function,
716 : return the _DECL for that function. */
717 :
718 : static tree
719 562190195 : get_function_named_in_call (tree t)
720 : {
721 562190195 : tree callee = cp_get_callee (t);
722 562190195 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
723 562190195 : return fun ? fun : callee;
724 : }
725 :
726 : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
727 : declared to be constexpr, or a sub-statement thereof. Returns the
728 : return value if suitable, error_mark_node for a statement not allowed in
729 : a constexpr function, or NULL_TREE if no return value was found. */
730 :
731 : tree
732 70722 : constexpr_fn_retval (tree body)
733 : {
734 77565 : switch (TREE_CODE (body))
735 : {
736 18785 : case STATEMENT_LIST:
737 18785 : {
738 18785 : tree expr = NULL_TREE;
739 56285 : for (tree stmt : tsi_range (body))
740 : {
741 37529 : tree s = constexpr_fn_retval (stmt);
742 37529 : if (s == error_mark_node)
743 : return error_mark_node;
744 37502 : else if (s == NULL_TREE)
745 : /* Keep iterating. */;
746 18750 : else if (expr)
747 : /* Multiple return statements. */
748 : return error_mark_node;
749 : else
750 : expr = s;
751 : }
752 : return expr;
753 : }
754 :
755 33143 : case RETURN_EXPR:
756 33143 : return break_out_target_exprs (TREE_OPERAND (body, 0));
757 :
758 20 : case DECL_EXPR:
759 20 : {
760 20 : tree decl = DECL_EXPR_DECL (body);
761 20 : if (TREE_CODE (decl) == USING_DECL
762 : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
763 20 : || DECL_ARTIFICIAL (decl))
764 : return NULL_TREE;
765 6 : return error_mark_node;
766 : }
767 :
768 6506 : case CLEANUP_POINT_EXPR:
769 6506 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
770 :
771 343 : case BIND_EXPR:
772 343 : if (!check_constexpr_bind_expr_vars (body))
773 6 : return error_mark_node;
774 337 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
775 :
776 : case USING_STMT:
777 : case DEBUG_BEGIN_STMT:
778 : return NULL_TREE;
779 :
780 0 : case CALL_EXPR:
781 0 : {
782 0 : tree fun = get_function_named_in_call (body);
783 0 : if (fun != NULL_TREE
784 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
785 : return NULL_TREE;
786 : }
787 : /* Fallthru. */
788 :
789 30 : default:
790 30 : return error_mark_node;
791 : }
792 : }
793 :
794 : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
795 : FUN; do the necessary transformations to turn it into a single expression
796 : that we can store in the hash table. */
797 :
798 : static tree
799 30992684 : massage_constexpr_body (tree fun, tree body)
800 : {
801 61985368 : if (DECL_CONSTRUCTOR_P (fun))
802 3679049 : body = build_constexpr_constructor_member_initializers
803 3679049 : (DECL_CONTEXT (fun), body);
804 27313635 : else if (cxx_dialect < cxx14)
805 : {
806 32958 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
807 1 : body = EH_SPEC_STMTS (body);
808 32958 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
809 21517 : body = TREE_OPERAND (body, 0);
810 32958 : body = constexpr_fn_retval (body);
811 : }
812 30992684 : return body;
813 : }
814 :
815 : /* CTYPE is a type constructed from BODY. Return true if some
816 : bases/fields are uninitialized, and complain if COMPLAIN. */
817 :
818 : static bool
819 3331443 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
820 : {
821 : /* We allow uninitialized bases/fields in C++20. */
822 3331443 : if (cxx_dialect >= cxx20)
823 : return false;
824 :
825 13780 : unsigned nelts = 0;
826 :
827 13780 : if (body)
828 : {
829 13776 : if (TREE_CODE (body) != CONSTRUCTOR)
830 : return false;
831 13723 : nelts = CONSTRUCTOR_NELTS (body);
832 : }
833 13727 : tree field = TYPE_FIELDS (ctype);
834 :
835 13727 : if (TREE_CODE (ctype) == UNION_TYPE)
836 : {
837 110 : if (nelts == 0 && next_aggregate_field (field))
838 : {
839 2 : if (complain)
840 2 : error ("%<constexpr%> constructor for union %qT must "
841 : "initialize exactly one non-static data member", ctype);
842 : return true;
843 : }
844 : return false;
845 : }
846 :
847 : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
848 : need an explicit initialization. */
849 : bool bad = false;
850 28892 : for (unsigned i = 0; i <= nelts; ++i)
851 : {
852 28892 : tree index = NULL_TREE;
853 28892 : if (i < nelts)
854 : {
855 15275 : index = CONSTRUCTOR_ELT (body, i)->index;
856 : /* Skip vptr adjustment represented with a COMPONENT_REF. */
857 15275 : if (TREE_CODE (index) != FIELD_DECL)
858 628 : continue;
859 : }
860 :
861 498897 : for (; field != index; field = DECL_CHAIN (field))
862 : {
863 470639 : tree ftype;
864 470639 : if (TREE_CODE (field) != FIELD_DECL)
865 470611 : continue;
866 1789 : if (DECL_UNNAMED_BIT_FIELD (field))
867 10 : continue;
868 : /* Artificial fields can be ignored unless they're bases. */
869 1779 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
870 6 : continue;
871 1773 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
872 : {
873 : /* Recurse to check the anonymous aggregate member. */
874 8 : bad |= cx_check_missing_mem_inits
875 4 : (TREE_TYPE (field), NULL_TREE, complain);
876 4 : if (bad && !complain)
877 6 : return true;
878 4 : continue;
879 : }
880 1769 : ftype = TREE_TYPE (field);
881 1769 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
882 : /* A flexible array can't be initialized here, so don't complain
883 : that it isn't. */
884 1 : continue;
885 1768 : if (is_empty_field (field))
886 : /* An empty field doesn't need an initializer. */
887 1733 : continue;
888 35 : ftype = strip_array_types (ftype);
889 35 : if (type_has_constexpr_default_constructor (ftype))
890 : {
891 : /* It's OK to skip a member with a trivial constexpr ctor.
892 : A constexpr ctor that isn't trivial should have been
893 : added in by now. */
894 7 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
895 : || errorcount != 0);
896 7 : continue;
897 : }
898 28 : if (!complain)
899 : return true;
900 22 : auto_diagnostic_group d;
901 22 : error ("member %qD must be initialized by mem-initializer "
902 : "in %<constexpr%> constructor", field);
903 22 : inform (DECL_SOURCE_LOCATION (field), "declared here");
904 22 : bad = true;
905 22 : }
906 28258 : if (field == NULL_TREE)
907 : break;
908 :
909 14647 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
910 : {
911 : /* Check the anonymous aggregate initializer is valid. */
912 38 : bad |= cx_check_missing_mem_inits
913 38 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
914 38 : if (bad && !complain)
915 : return true;
916 : }
917 14647 : field = DECL_CHAIN (field);
918 : }
919 :
920 : return bad;
921 : }
922 :
923 : /* We are processing the definition of the constexpr function FUN.
924 : Check that its body fulfills the appropriate requirements and
925 : enter it in the constexpr function definition table. */
926 :
927 : void
928 166967232 : maybe_save_constexpr_fundef (tree fun)
929 : {
930 166967232 : if (processing_template_decl
931 68967044 : || cp_function_chain->invalid_constexpr
932 235916143 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
933 135974842 : return;
934 :
935 : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
936 : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
937 52748463 : bool implicit = false;
938 52748463 : if (flag_implicit_constexpr)
939 : {
940 40 : if (DECL_DELETING_DESTRUCTOR_P (fun)
941 40 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
942 : /* Don't inherit implicit constexpr from the non-deleting
943 : destructor. */
944 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
945 :
946 40 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
947 27 : && DECL_DECLARED_INLINE_P (fun)
948 63 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
949 : implicit = true;
950 : }
951 :
952 52748463 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
953 : return;
954 :
955 31048038 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
956 :
957 31048038 : if (!is_valid_constexpr_fn (fun, complain))
958 : return;
959 :
960 30992604 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
961 30992604 : if (massaged == NULL_TREE || massaged == error_mark_node)
962 : {
963 72 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
964 22 : error ("body of %<constexpr%> function %qD not a return-statement",
965 : fun);
966 : return;
967 : }
968 :
969 30992568 : bool potential = potential_rvalue_constant_expression (massaged);
970 30992568 : if (!potential && complain)
971 264 : require_potential_rvalue_constant_expression_fncheck (massaged);
972 :
973 34671594 : if (DECL_CONSTRUCTOR_P (fun) && potential
974 34667625 : && !DECL_DEFAULTED_FN (fun))
975 : {
976 3331387 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
977 : massaged, complain))
978 : potential = false;
979 3331361 : else if (cxx_dialect > cxx11)
980 : {
981 : /* What we got from massage_constexpr_body is pretty much just the
982 : ctor-initializer, also check the body. */
983 3326492 : massaged = DECL_SAVED_TREE (fun);
984 3326492 : potential = potential_rvalue_constant_expression (massaged);
985 3326492 : if (!potential && complain)
986 20 : require_potential_rvalue_constant_expression_fncheck (massaged);
987 : }
988 : }
989 :
990 30992568 : if (!potential && complain
991 : /* If -Wno-invalid-constexpr was specified, we haven't complained
992 : about non-constant expressions yet. Register the function and
993 : complain in explain_invalid_constexpr_fn if the function is
994 : called. */
995 304 : && warn_invalid_constexpr != 0)
996 : return;
997 :
998 30992401 : if (implicit)
999 : {
1000 23 : if (potential)
1001 : {
1002 12 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
1003 12 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
1004 24 : if (DECL_CONSTRUCTOR_P (fun))
1005 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
1006 : }
1007 : else
1008 : /* Don't bother keeping the pre-generic body of unsuitable functions
1009 : not explicitly declared constexpr. */
1010 : return;
1011 : }
1012 :
1013 30992390 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1014 30992390 : bool clear_ctx = false;
1015 30992390 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1016 : {
1017 30992390 : clear_ctx = true;
1018 30992390 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1019 : }
1020 30992390 : tree saved_fn = current_function_decl;
1021 30992390 : current_function_decl = fun;
1022 30992390 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1023 30992390 : current_function_decl = saved_fn;
1024 30992390 : if (clear_ctx)
1025 30992390 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1026 30992390 : if (!potential)
1027 : /* For a template instantiation, we want to remember the pre-generic body
1028 : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1029 : that it doesn't need to bother trying to expand the function. */
1030 60540 : entry.result = error_mark_node;
1031 :
1032 30992390 : register_constexpr_fundef (entry);
1033 : }
1034 :
1035 : /* BODY is a validated and massaged definition of a constexpr
1036 : function. Register it in the hash table. */
1037 :
1038 : void
1039 31031072 : register_constexpr_fundef (const constexpr_fundef &value)
1040 : {
1041 : /* Create the constexpr function table if necessary. */
1042 31031072 : if (constexpr_fundef_table == NULL)
1043 27655 : constexpr_fundef_table
1044 27655 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1045 :
1046 31031072 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1047 31031072 : (const_cast<constexpr_fundef *> (&value), INSERT);
1048 :
1049 31031072 : gcc_assert (*slot == NULL);
1050 31031072 : *slot = ggc_alloc<constexpr_fundef> ();
1051 31031072 : **slot = value;
1052 31031072 : }
1053 :
1054 : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1055 : function called in a context that requires a constant expression).
1056 : If it comes from a constexpr template, explain why the instantiation
1057 : isn't constexpr. Otherwise, explain why the function cannot be used
1058 : in a constexpr context. */
1059 :
1060 : void
1061 1069 : explain_invalid_constexpr_fn (tree fun)
1062 : {
1063 1069 : static hash_set<tree> *diagnosed;
1064 1069 : tree body;
1065 :
1066 : /* Don't try to explain a function we already complained about. */
1067 1069 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1068 859 : if (f->language->erroneous)
1069 1069 : return;
1070 :
1071 : /* In C++23, a function marked 'constexpr' may not actually be a constant
1072 : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1073 : wasn't enabled. The function was called, so diagnose why it cannot be
1074 : used in a constant expression. */
1075 1020 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1076 : /* Go on. */;
1077 : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1078 984 : else if (!DECL_DEFAULTED_FN (fun)
1079 1236 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1080 947 : && !(flag_implicit_constexpr
1081 9 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1082 9 : && DECL_DECLARED_INLINE_P (fun))
1083 1925 : && !is_instantiation_of_constexpr (fun))
1084 : {
1085 923 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1086 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1087 926 : && decl_defined_p (fun))
1088 3 : inform (DECL_SOURCE_LOCATION (fun),
1089 : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1090 : return;
1091 : }
1092 97 : if (diagnosed == NULL)
1093 74 : diagnosed = new hash_set<tree>;
1094 97 : if (diagnosed->add (fun))
1095 : /* Already explained. */
1096 : return;
1097 :
1098 96 : iloc_sentinel ils = input_location;
1099 96 : if (!lambda_static_thunk_p (fun))
1100 : {
1101 : /* Diagnostics should completely ignore the static thunk, so leave
1102 : input_location set to our caller's location. */
1103 84 : input_location = DECL_SOURCE_LOCATION (fun);
1104 84 : inform (input_location,
1105 : "%qD is not usable as a %<constexpr%> function because:", fun);
1106 : }
1107 : /* First check the declaration. */
1108 96 : if (is_valid_constexpr_fn (fun, true))
1109 : {
1110 : /* Then if it's OK, the body. */
1111 90 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1112 90 : && DECL_DEFAULTED_FN (fun))
1113 10 : explain_implicit_non_constexpr (fun);
1114 : else
1115 : {
1116 80 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1117 54 : body = fd->body;
1118 : else
1119 26 : body = DECL_SAVED_TREE (fun);
1120 80 : tree massaged = massage_constexpr_body (fun, body);
1121 80 : require_potential_rvalue_constant_expression (massaged);
1122 160 : if (DECL_CONSTRUCTOR_P (fun))
1123 : {
1124 14 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1125 14 : if (cxx_dialect > cxx11)
1126 : /* Also check the body, not just the ctor-initializer. */
1127 12 : require_potential_rvalue_constant_expression (body);
1128 : }
1129 66 : else if (massaged == NULL_TREE || massaged == error_mark_node)
1130 1 : error ("body of %<constexpr%> function %qD not a return-statement",
1131 : fun);
1132 : }
1133 : }
1134 96 : }
1135 :
1136 : /* Objects of this type represent calls to constexpr functions
1137 : along with the bindings of parameters to their arguments, for
1138 : the purpose of compile time evaluation. */
1139 :
1140 : struct GTY((for_user)) constexpr_call {
1141 : /* Description of the constexpr function definition. */
1142 : constexpr_fundef *fundef = nullptr;
1143 : /* Parameter bindings environment. A TREE_VEC of arguments. */
1144 : tree bindings = NULL_TREE;
1145 : /* Result of the call, indexed by the value of
1146 : constexpr_ctx::manifestly_const_eval.
1147 : unknown_type_node means the call is being evaluated.
1148 : error_mark_node means that the evaluation was erroneous or otherwise
1149 : uncacheable (e.g. because it depends on the caller).
1150 : Otherwise, the actual value of the call. */
1151 : tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1152 : /* The hash of this call; we remember it here to avoid having to
1153 : recalculate it when expanding the hash table. */
1154 : hashval_t hash = 0;
1155 :
1156 : /* The result slot corresponding to the given mce_value. */
1157 64541630 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1158 : };
1159 :
1160 : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1161 : {
1162 : static hashval_t hash (constexpr_call *);
1163 : static bool equal (constexpr_call *, constexpr_call *);
1164 : };
1165 :
1166 : enum constexpr_switch_state {
1167 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1168 : and default: label for that switch has not been seen yet. */
1169 : css_default_not_seen,
1170 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1171 : and default: label for that switch has been seen already. */
1172 : css_default_seen,
1173 : /* Used when processing a switch for the second time by
1174 : cxx_eval_switch_expr, where default: label should match. */
1175 : css_default_processing
1176 : };
1177 :
1178 : /* The constexpr expansion context part which needs one instance per
1179 : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1180 : variables initialized within the expression. */
1181 :
1182 : class constexpr_global_ctx {
1183 : /* Values for any temporaries or local variables within the
1184 : constant-expression. Objects outside their lifetime have
1185 : value 'void_node' or 'void_list_node', the former if they are outside
1186 : of lifetime but still before storage has been deallocated. */
1187 : hash_map<tree,tree> values;
1188 : public:
1189 : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1190 : on simple constants or location wrappers) encountered during current
1191 : cxx_eval_outermost_constant_expr call. */
1192 : HOST_WIDE_INT constexpr_ops_count;
1193 : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1194 : expression. */
1195 : auto_vec<tree, 16> heap_vars;
1196 : /* Vector of caught exceptions, including exceptions still not active at
1197 : the start of a handler (those are immediately followed up by HANDLER_TYPE
1198 : until __cxa_begin_catch finishes). If __cxa_begin_catch or
1199 : __cxa_get_exception_ptr need to create temporaries, the VAR_DECL of the
1200 : exception object is wrapped in the vector into a TREE_LIST where
1201 : TREE_VALUE of it is the VAR_DECL of the exception object and TREE_PURPOSE
1202 : one of the temporaries, others chained through DECL_CHAIN. */
1203 : auto_vec<tree, 2> caught_exceptions;
1204 : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1205 : vec<tree> *cleanups;
1206 : /* If non-null, only allow modification of existing values of the variables
1207 : in this set. Set by modifiable_tracker, below. */
1208 : hash_set<tree> *modifiable;
1209 : /* If cxx_eval_outermost_constant_expr is called on the consteval block
1210 : operator (), this is the FUNCTION_DECL of that operator (). */
1211 : tree consteval_block;
1212 : /* Number of heap VAR_DECL deallocations. */
1213 : unsigned heap_dealloc_count;
1214 : /* Number of uncaught exceptions. */
1215 : unsigned uncaught_exceptions;
1216 : /* A contract statement that failed or was not constant, we only store the
1217 : first one that fails. */
1218 : tree contract_statement;
1219 : /* [basic.contract.eval]/7.3 if this expression would otherwise be constant
1220 : then a non-const contract makes the program ill-formed. */
1221 : bool contract_condition_non_const;
1222 : /* Some metafunctions aren't dependent just on their arguments, but also
1223 : on various other dependencies, e.g. has_identifier on a function parameter
1224 : reflection can change depending on further declarations of corresponding
1225 : function, is_complete_type depends on type definitions and template
1226 : specializations in between the calls, define_aggregate even defines
1227 : class types, etc. Thus, we need to arrange for calls which call
1228 : at least some metafunctions to be non-cacheable, because their behavior
1229 : might not be the same. Until we figure out which exact metafunctions
1230 : need this and which don't, do it for all of them.
1231 : Also used for some cases in constexpr EH, e.g. __cxa_rethrow,
1232 : __builtin_uncaught_exceptions and __builtin_current_exception, which can
1233 : be also dependent on some state (pending uncaught or caught exceptions)
1234 : not tracked in the constexpr call caching. */
1235 : bool state_dependent;
1236 :
1237 : /* Constructor. */
1238 573704284 : constexpr_global_ctx ()
1239 573704284 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1240 573704284 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1241 573704284 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1242 573704284 : contract_condition_non_const (false), state_dependent (false) {}
1243 :
1244 463427616 : bool is_outside_lifetime (tree t)
1245 : {
1246 463427616 : if (tree *p = values.get (t))
1247 209768483 : if (*p == void_node || *p == void_list_node)
1248 268 : return true;
1249 : return false;
1250 : }
1251 : /* This differs from !is_outside_lifetime in that it returns false if
1252 : T is not in the map. */
1253 108 : bool in_eval_lifetime_p (tree t)
1254 : {
1255 108 : if (tree *p = values.get (t))
1256 40 : if (*p != void_node && *p != void_list_node)
1257 40 : return true;
1258 : return false;
1259 : }
1260 587284531 : tree get_value (tree t)
1261 : {
1262 587284531 : if (tree *p = values.get (t))
1263 308969778 : if (*p != void_node && *p != void_list_node)
1264 300806924 : return *p;
1265 : return NULL_TREE;
1266 : }
1267 5124 : tree *get_raw_value_ptr (tree t)
1268 : {
1269 5124 : return values.get (t);
1270 : }
1271 125661493 : tree *get_value_ptr (tree t, bool initializing)
1272 : {
1273 125661493 : if (modifiable && !modifiable->contains (t))
1274 : return nullptr;
1275 125661470 : if (tree *p = values.get (t))
1276 : {
1277 125651958 : if (*p != void_node && *p != void_list_node)
1278 : return p;
1279 63 : else if (initializing)
1280 : {
1281 21 : *p = NULL_TREE;
1282 21 : return p;
1283 : }
1284 : }
1285 : return nullptr;
1286 : }
1287 302339937 : void put_value (tree t, tree v)
1288 : {
1289 : /* If modifiable is not null, any key that was not previously in the
1290 : map, or that has been destroyed and freed (as indicated by
1291 : void_list_node), is tracked in modifiable. If the object is there,
1292 : or its storage remains (as indicated by void_node), then we don't
1293 : track it as modifiable. */
1294 302339937 : bool already_in_map;
1295 302339937 : tree &slot = values.get_or_insert (t, &already_in_map);
1296 302339937 : bool newval = !already_in_map || slot == void_list_node;
1297 302339937 : slot = v;
1298 302339937 : if (newval && modifiable)
1299 76 : modifiable->add (t);
1300 302339937 : }
1301 242447111 : void destroy_value (tree t, bool past_storage_end = true)
1302 : {
1303 242447111 : if (TREE_CODE (t) == VAR_DECL
1304 242447111 : || TREE_CODE (t) == PARM_DECL
1305 : || TREE_CODE (t) == RESULT_DECL)
1306 242738486 : values.put (t, past_storage_end ? void_list_node : void_node);
1307 : else
1308 9269 : values.remove (t);
1309 242447111 : }
1310 19299204 : void clear_value (tree t)
1311 : {
1312 38598408 : values.remove (t);
1313 : }
1314 : };
1315 :
1316 : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1317 : side-effects from evaluation of a particular subexpression of a
1318 : constant-expression. In such cases we use modifiable_tracker to prevent
1319 : modification of variables created outside of that subexpression. Keep and
1320 : then restore the original value of global->modifiable so that tracker
1321 : instantiations can nest.
1322 :
1323 : ??? We could change the hash_set to a hash_map, allow and track external
1324 : modifications, and roll them back in the destructor. It's not clear to me
1325 : that this would be worthwhile. */
1326 :
1327 : class modifiable_tracker
1328 : {
1329 : hash_set<tree> set;
1330 : constexpr_global_ctx *global;
1331 : hash_set<tree> *previous_set;
1332 : public:
1333 173645 : modifiable_tracker (constexpr_global_ctx *g, bool push = true)
1334 173645 : : global (g), previous_set (g->modifiable)
1335 : {
1336 173645 : if (push)
1337 173618 : global->modifiable = &set;
1338 173645 : }
1339 173645 : ~modifiable_tracker ()
1340 : {
1341 173721 : for (tree t: set)
1342 76 : global->clear_value (t);
1343 173645 : global->modifiable = previous_set;
1344 173645 : }
1345 : };
1346 :
1347 : /* The constexpr expansion context. CALL is the current function
1348 : expansion, CTOR is the current aggregate initializer, OBJECT is the
1349 : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1350 :
1351 : struct constexpr_ctx {
1352 : /* The part of the context that needs to be unique to the whole
1353 : cxx_eval_outermost_constant_expr invocation. */
1354 : constexpr_global_ctx *global;
1355 : /* The innermost call we're evaluating. */
1356 : constexpr_call *call;
1357 : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1358 : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1359 : vec<tree> *save_exprs;
1360 : /* The CONSTRUCTOR we're currently building up for an aggregate
1361 : initializer. */
1362 : tree ctor;
1363 : /* The object we're building the CONSTRUCTOR for. */
1364 : tree object;
1365 : /* If inside SWITCH_EXPR. */
1366 : constexpr_switch_state *css_state;
1367 : /* The aggregate initialization context inside which this one is nested. This
1368 : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1369 : const constexpr_ctx *parent;
1370 :
1371 : /* Whether we should error on a non-constant expression or fail quietly.
1372 : This flag needs to be here, but some of the others could move to global
1373 : if they get larger than a word. */
1374 : bool quiet;
1375 : /* Whether we are strictly conforming to constant expression rules or
1376 : trying harder to get a constant value. */
1377 : bool strict;
1378 : /* Whether __builtin_is_constant_evaluated () should be true. */
1379 : mce_value manifestly_const_eval;
1380 : };
1381 :
1382 : /* Return ctx->quiet. For use in reflect.cc. */
1383 :
1384 : bool
1385 1400 : cxx_constexpr_quiet_p (const constexpr_ctx *ctx)
1386 : {
1387 1400 : return ctx->quiet;
1388 : }
1389 :
1390 : /* Return ctx->manifestly_const_eval. For use in reflect.cc. */
1391 :
1392 : mce_value
1393 388 : cxx_constexpr_manifestly_const_eval (const constexpr_ctx *ctx)
1394 : {
1395 388 : return ctx->manifestly_const_eval;
1396 : }
1397 :
1398 : /* Return ctx->call->fundef->decl or NULL_TREE. For use in
1399 : reflect.cc. */
1400 :
1401 : tree
1402 2136 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1403 : {
1404 2136 : if (ctx->call)
1405 958 : return ctx->call->fundef->decl;
1406 : else
1407 : return NULL_TREE;
1408 : }
1409 :
1410 : /* Return ctx->global->consteval_block. For use in reflect.cc. */
1411 :
1412 : tree
1413 159 : cxx_constexpr_consteval_block (const constexpr_ctx *ctx)
1414 : {
1415 159 : return ctx->global->consteval_block;
1416 : }
1417 :
1418 : /* Return true if T's lifetime began within the core constant expression
1419 : currently under evaluation. */
1420 :
1421 : bool
1422 108 : cxx_constexpr_in_eval_lifetime_p (const constexpr_ctx *ctx, tree t)
1423 : {
1424 108 : return ctx->global->in_eval_lifetime_p (t);
1425 : }
1426 :
1427 : /* Predicates for the meaning of *jump_target. */
1428 :
1429 : static bool
1430 111188405 : returns (tree *jump_target)
1431 : {
1432 19407878 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1433 : }
1434 :
1435 : static bool
1436 105588018 : breaks (tree *jump_target)
1437 : {
1438 105588018 : return (*jump_target
1439 105588018 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1440 2157 : && LABEL_DECL_BREAK (*jump_target))
1441 1481270 : || TREE_CODE (*jump_target) == BREAK_STMT
1442 1336032 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1443 : }
1444 :
1445 : static bool
1446 142189009 : continues (tree *jump_target)
1447 : {
1448 142189009 : return (*jump_target
1449 142189009 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1450 4049 : && LABEL_DECL_CONTINUE (*jump_target))
1451 1352855 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1452 : }
1453 :
1454 : static bool
1455 12989471 : switches (tree *jump_target)
1456 : {
1457 109791 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1458 : }
1459 :
1460 : static bool
1461 1909689355 : throws (tree *jump_target)
1462 : {
1463 : /* void_node is for use in potential_constant_expression_1, otherwise
1464 : it should an artificial VAR_DECL created by constant evaluation
1465 : of __cxa_allocate_exception (). */
1466 213515798 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1467 : }
1468 :
1469 : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1470 : references and objects are in effect. */
1471 :
1472 : static bool
1473 268182940 : p2280_active_p (const constexpr_ctx *ctx)
1474 : {
1475 34311828 : if (ctx->manifestly_const_eval != mce_true)
1476 : /* Disable these relaxations during speculative constexpr folding,
1477 : as it can significantly increase compile time/memory use
1478 : (PR119387). */
1479 : return false;
1480 :
1481 : /* P2280R4 was accepted as a DR against C++11. */
1482 0 : return cxx_dialect >= cxx11;
1483 : }
1484 :
1485 : /* Remove T from the global values map, checking for attempts to destroy
1486 : a value that has already finished its lifetime. */
1487 :
1488 : static void
1489 242063916 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1490 : {
1491 242063916 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1492 : return;
1493 :
1494 : /* Don't error again here if we've already reported a problem. */
1495 242063697 : if (!*non_constant_p
1496 210975216 : && DECL_P (t)
1497 : /* Non-trivial destructors have their lifetimes ended explicitly
1498 : with a clobber, so don't worry about it here. */
1499 210966153 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1500 : /* ...except parameters are remapped in cxx_eval_call_expression,
1501 : and the destructor call during cleanup won't be able to tell that
1502 : this value has already been destroyed, so complain now. This is
1503 : not quite unobservable, but is extremely unlikely to crop up in
1504 : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1505 765325 : || TREE_CODE (t) == PARM_DECL)
1506 452265976 : && ctx->global->is_outside_lifetime (t))
1507 : {
1508 54 : if (!ctx->quiet)
1509 : {
1510 18 : auto_diagnostic_group d;
1511 18 : error ("destroying %qE outside its lifetime", t);
1512 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1513 18 : }
1514 54 : *non_constant_p = true;
1515 : }
1516 242063697 : ctx->global->destroy_value (t);
1517 : }
1518 :
1519 : /* This internal flag controls whether we should avoid doing anything during
1520 : constexpr evaluation that would cause extra DECL_UID generation, such as
1521 : template instantiation and function body copying. */
1522 :
1523 : static bool uid_sensitive_constexpr_evaluation_value;
1524 :
1525 : /* An internal counter that keeps track of the number of times
1526 : uid_sensitive_constexpr_evaluation_p returned true. */
1527 :
1528 : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1529 :
1530 : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1531 : increments the corresponding counter. */
1532 :
1533 : static bool
1534 9014058 : uid_sensitive_constexpr_evaluation_p ()
1535 : {
1536 8926636 : if (uid_sensitive_constexpr_evaluation_value)
1537 : {
1538 240509 : ++uid_sensitive_constexpr_evaluation_true_counter;
1539 240500 : return true;
1540 : }
1541 : else
1542 : return false;
1543 : }
1544 :
1545 : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1546 : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1547 : during the lifetime of the sentinel object. Upon its destruction, the
1548 : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1549 :
1550 80600571 : uid_sensitive_constexpr_evaluation_sentinel
1551 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1552 80600571 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1553 : {
1554 80600571 : }
1555 :
1556 : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1557 : records the current number of times that uid_sensitive_constexpr_evaluation_p
1558 : has been called and returned true. */
1559 :
1560 3837498421 : uid_sensitive_constexpr_evaluation_checker
1561 : ::uid_sensitive_constexpr_evaluation_checker ()
1562 3837498421 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1563 : {
1564 3837498421 : }
1565 :
1566 : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1567 : some constexpr evaluation was restricted due to u_s_c_e_p being called
1568 : and returning true during the lifetime of this checker object. */
1569 :
1570 : bool
1571 2545450264 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1572 : {
1573 2545450264 : return (uid_sensitive_constexpr_evaluation_value
1574 2545450264 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1575 : }
1576 :
1577 :
1578 : /* A table of all constexpr calls that have been evaluated by the
1579 : compiler in this translation unit. */
1580 :
1581 : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1582 :
1583 : /* Compute a hash value for a constexpr call representation. */
1584 :
1585 : inline hashval_t
1586 240813837 : constexpr_call_hasher::hash (constexpr_call *info)
1587 : {
1588 240813837 : return info->hash;
1589 : }
1590 :
1591 : /* Return true if the objects pointed to by P and Q represent calls
1592 : to the same constexpr function with the same arguments.
1593 : Otherwise, return false. */
1594 :
1595 : bool
1596 248791559 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1597 : {
1598 248791559 : if (lhs == rhs)
1599 : return true;
1600 248791559 : if (lhs->hash != rhs->hash)
1601 : return false;
1602 27665546 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1603 : return false;
1604 27665546 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1605 : }
1606 :
1607 : /* Initialize the constexpr call table, if needed. */
1608 :
1609 : static void
1610 36824097 : maybe_initialize_constexpr_call_table (void)
1611 : {
1612 36824097 : if (constexpr_call_table == NULL)
1613 18383 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1614 36824097 : }
1615 :
1616 : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1617 : a function happens to get called recursively, we unshare the callee
1618 : function's body and evaluate this unshared copy instead of evaluating the
1619 : original body.
1620 :
1621 : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1622 : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1623 : that's keyed off of the original FUNCTION_DECL and whose value is a
1624 : TREE_LIST of this function's unused copies awaiting reuse.
1625 :
1626 : This is not GC-deletable to avoid GC affecting UID generation. */
1627 :
1628 : static GTY(()) decl_tree_map *fundef_copies_table;
1629 :
1630 : /* Reuse a copy or create a new unshared copy of the function FUN.
1631 : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1632 : is parms, TYPE is result. */
1633 :
1634 : static tree
1635 86755854 : get_fundef_copy (constexpr_fundef *fundef)
1636 : {
1637 86755854 : tree copy;
1638 86755854 : bool existed;
1639 86755854 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1640 86755854 : (fundef_copies_table, fundef->decl, &existed, 127));
1641 :
1642 86755854 : if (!existed)
1643 : {
1644 : /* There is no cached function available, or in use. We can use
1645 : the function directly. That the slot is now created records
1646 : that this function is now in use. */
1647 7553088 : copy = build_tree_list (fundef->body, fundef->parms);
1648 7553088 : TREE_TYPE (copy) = fundef->result;
1649 : }
1650 79202766 : else if (*slot == NULL_TREE)
1651 : {
1652 4443 : if (uid_sensitive_constexpr_evaluation_p ())
1653 9 : return NULL_TREE;
1654 :
1655 : /* We've already used the function itself, so make a copy. */
1656 4434 : copy = build_tree_list (NULL, NULL);
1657 4434 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1658 4434 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1659 4434 : tree saved_result = DECL_RESULT (fundef->decl);
1660 4434 : tree saved_fn = current_function_decl;
1661 4434 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1662 4434 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1663 4434 : DECL_RESULT (fundef->decl) = fundef->result;
1664 4434 : current_function_decl = fundef->decl;
1665 4434 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1666 4434 : TREE_TYPE (copy));
1667 4434 : current_function_decl = saved_fn;
1668 4434 : DECL_RESULT (fundef->decl) = saved_result;
1669 4434 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1670 4434 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1671 : }
1672 : else
1673 : {
1674 : /* We have a cached function available. */
1675 79198323 : copy = *slot;
1676 79198323 : *slot = TREE_CHAIN (copy);
1677 : }
1678 :
1679 : return copy;
1680 : }
1681 :
1682 : /* Save the copy COPY of function FUN for later reuse by
1683 : get_fundef_copy(). By construction, there will always be an entry
1684 : to find. */
1685 :
1686 : static void
1687 86755841 : save_fundef_copy (tree fun, tree copy)
1688 : {
1689 86755841 : tree *slot = fundef_copies_table->get (fun);
1690 86755841 : TREE_CHAIN (copy) = *slot;
1691 86755841 : *slot = copy;
1692 86755841 : }
1693 :
1694 : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1695 : value_cat, bool *, bool *, tree *);
1696 : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1697 : bool *, tree *);
1698 : static tree find_heap_var_refs (tree *, int *, void *);
1699 :
1700 : /* For exception object EXC if it has class type and usable what () method
1701 : which returns cv char * return the xmalloced string literal which it returns
1702 : if possible, otherwise return NULL. */
1703 :
1704 : static char *
1705 418 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1706 : {
1707 418 : tree type = strip_array_types (TREE_TYPE (exc));
1708 418 : if (!CLASS_TYPE_P (type))
1709 : return NULL;
1710 362 : tree std_exception = lookup_qualified_name (std_node, "exception",
1711 : LOOK_want::NORMAL, false);
1712 362 : if (TREE_CODE (std_exception) != TYPE_DECL)
1713 : return NULL;
1714 356 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1715 : return NULL;
1716 356 : base_kind b_kind;
1717 356 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1718 : tf_none);
1719 356 : if (binfo == NULL_TREE || binfo == error_mark_node)
1720 : return NULL;
1721 348 : if (type != TREE_TYPE (exc))
1722 310 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1723 348 : tree call
1724 348 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1725 : tf_none);
1726 348 : if (call == error_mark_node)
1727 : return NULL;
1728 348 : releasing_vec what_args;
1729 348 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1730 348 : if (call == error_mark_node)
1731 : return NULL;
1732 348 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1733 348 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1734 348 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1735 348 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1736 348 : TYPE_SIZE_UNIT (char_type_node))
1737 696 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1738 : return NULL;
1739 348 : if (!potential_constant_expression (call))
1740 : return NULL;
1741 348 : bool non_constant_p = false, overflow_p = false;
1742 348 : tree jmp_target = NULL;
1743 348 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1744 : &non_constant_p, &overflow_p,
1745 : &jmp_target);
1746 696 : if (throws (&jmp_target) || non_constant_p)
1747 : return NULL;
1748 348 : if (reduced_constant_expression_p (ptr))
1749 42 : if (const char *msg = c_getstr (ptr))
1750 42 : return xstrdup (msg);
1751 306 : auto_vec <char, 32> v;
1752 12318 : for (unsigned i = 0; i < INT_MAX; ++i)
1753 : {
1754 12318 : tree t = call;
1755 12318 : if (i)
1756 12012 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1757 12318 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1758 12318 : non_constant_p = false;
1759 12318 : overflow_p = false;
1760 12318 : jmp_target = NULL;
1761 12318 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1762 : &non_constant_p, &overflow_p,
1763 : &jmp_target);
1764 12318 : if (throws (&jmp_target)
1765 12318 : || non_constant_p
1766 12318 : || !tree_fits_shwi_p (t2))
1767 0 : return NULL;
1768 12318 : char c = tree_to_shwi (t2);
1769 12318 : v.safe_push (c);
1770 12318 : if (c == '\0')
1771 : break;
1772 : }
1773 612 : return xstrdup (v.address ());
1774 654 : }
1775 :
1776 : /* Diagnose constant expression evaluation encountering call to
1777 : std::terminate due to exception EXC. */
1778 :
1779 : static void
1780 22 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1781 : {
1782 22 : tree type = strip_array_types (TREE_TYPE (exc));
1783 22 : if (char *str = exception_what_str (ctx, exc))
1784 : {
1785 4 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1786 : "%<what()%>: %qs", "std::terminate", type, str);
1787 4 : free (str);
1788 : }
1789 : else
1790 : {
1791 18 : if (type != TREE_TYPE (exc))
1792 18 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1793 18 : bool non_constant_p = false, overflow_p = false;
1794 18 : tree jmp_target = NULL;
1795 18 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1796 : &non_constant_p, &overflow_p,
1797 : &jmp_target);
1798 18 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1799 18 : if (reduced_constant_expression_p (val))
1800 18 : error_at (loc, "%qs called after throwing an exception %qE",
1801 : "std::terminate", val);
1802 : else
1803 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1804 : "std::terminate", type);
1805 : }
1806 22 : }
1807 :
1808 : /* Diagnose constant expression evaluation encountering call to
1809 : uncaught exception EXC. */
1810 :
1811 : static void
1812 396 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1813 : {
1814 396 : tree type = strip_array_types (TREE_TYPE (exc));
1815 396 : if (char *str = exception_what_str (ctx, exc))
1816 : {
1817 344 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1818 344 : free (str);
1819 : }
1820 : else
1821 : {
1822 52 : if (type != TREE_TYPE (exc))
1823 52 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1824 52 : bool non_constant_p = false, overflow_p = false;
1825 52 : tree jmp_target = NULL;
1826 52 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1827 : &non_constant_p, &overflow_p,
1828 : &jmp_target);
1829 52 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1830 52 : if (reduced_constant_expression_p (val))
1831 50 : error_at (loc, "uncaught exception %qE", val);
1832 : else
1833 2 : error_at (loc, "uncaught exception of type %qT", type);
1834 : }
1835 396 : }
1836 :
1837 : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1838 : magic constexpr functions for C++26. */
1839 :
1840 : enum cxa_builtin {
1841 : CXA_NONE = 0,
1842 : CXA_ALLOCATE_EXCEPTION = 1,
1843 : CXA_FREE_EXCEPTION = 2,
1844 : CXA_THROW = 3,
1845 : CXA_BEGIN_CATCH = 4,
1846 : CXA_END_CATCH = 5,
1847 : CXA_RETHROW = 6,
1848 : CXA_GET_EXCEPTION_PTR = 7,
1849 : CXA_BAD_CAST = 8,
1850 : CXA_BAD_TYPEID = 9,
1851 : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1852 : STD_RETHROW_EXCEPTION = 11,
1853 : BUILTIN_EH_PTR_ADJUST_REF = 12,
1854 : BUILTIN_UNCAUGHT_EXCEPTIONS = 13,
1855 : BUILTIN_CURRENT_EXCEPTION = 14
1856 : };
1857 :
1858 : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1859 : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1860 :
1861 : static enum cxa_builtin
1862 36511882 : cxx_cxa_builtin_fn_p (tree fndecl)
1863 : {
1864 36511882 : if (cxx_dialect < cxx26)
1865 : return CXA_NONE;
1866 3740524 : if (DECL_LANGUAGE (fndecl) != lang_c)
1867 : {
1868 3116079 : if (!decl_in_std_namespace_p (fndecl))
1869 : return CXA_NONE;
1870 1315724 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1871 : return STD_RETHROW_EXCEPTION;
1872 1315682 : return CXA_NONE;
1873 : }
1874 624445 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1875 : return CXA_NONE;
1876 85632 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1877 : return CXA_ALLOCATE_EXCEPTION;
1878 28340 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1879 : return CXA_FREE_EXCEPTION;
1880 28338 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1881 : return CXA_THROW;
1882 18642 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1883 : return CXA_BEGIN_CATCH;
1884 4857 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1885 : return CXA_END_CATCH;
1886 1092 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1887 : return CXA_RETHROW;
1888 986 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1889 : return CXA_GET_EXCEPTION_PTR;
1890 942 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1891 : return CXA_BAD_CAST;
1892 646 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1893 : return CXA_BAD_TYPEID;
1894 633 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1895 33 : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1896 : return CXA_NONE;
1897 : }
1898 :
1899 : /* Helper function for cxx_eval_cxa_builtin_fn.
1900 : Check if ARG is a valid first argument of __cxa_throw or
1901 : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1902 : not, otherwise return the artificial __cxa_allocate_exception allocated
1903 : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1904 :
1905 : static tree
1906 2116 : cxa_check_throw_arg (tree arg, bool free_exc)
1907 : {
1908 2116 : STRIP_NOPS (arg);
1909 2116 : if (TREE_CODE (arg) != ADDR_EXPR)
1910 : return NULL_TREE;
1911 2116 : arg = TREE_OPERAND (arg, 0);
1912 2116 : if (!VAR_P (arg)
1913 2116 : || !DECL_ARTIFICIAL (arg)
1914 2116 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1915 2116 : && DECL_NAME (arg) != heap_identifier)
1916 4232 : || !DECL_LANG_SPECIFIC (arg))
1917 0 : return NULL_TREE;
1918 : return arg;
1919 : }
1920 :
1921 : /* Helper function for cxx_eval_cxa_builtin_fn.
1922 : "Allocate" on the constexpr heap an exception object of TYPE
1923 : with REFCOUNT. */
1924 :
1925 : static tree
1926 18432 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1927 : tree refcount)
1928 : {
1929 18432 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1930 18432 : DECL_ARTIFICIAL (var) = 1;
1931 18432 : retrofit_lang_decl (var);
1932 18432 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1933 18432 : ctx->global->heap_vars.safe_push (var);
1934 18432 : return var;
1935 : }
1936 :
1937 : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1938 : C++26 constexpr exception support (P3068R5). */
1939 :
1940 : static tree
1941 32336 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1942 : enum cxa_builtin kind, tree fndecl,
1943 : bool *non_constant_p, bool *overflow_p,
1944 : tree *jump_target)
1945 : {
1946 32336 : int nargs = call_expr_nargs (call);
1947 32336 : location_t loc = cp_expr_loc_or_input_loc (call);
1948 32336 : tree args[4], arg;
1949 32336 : if (nargs > 4)
1950 : {
1951 0 : invalid_nargs:
1952 0 : if (!ctx->quiet)
1953 0 : error_at (loc, "call to %qD function with incorrect "
1954 : "number of arguments", fndecl);
1955 0 : *non_constant_p = true;
1956 0 : return call;
1957 : }
1958 32336 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1959 8813 : && nargs == 1
1960 8813 : && (arg = CALL_EXPR_ARG (call, 0))
1961 8813 : && TREE_CODE (arg) == CALL_EXPR
1962 8813 : && call_expr_nargs (arg) == 1
1963 41149 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1964 8813 : if (tree fun = get_function_named_in_call (arg))
1965 8813 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1966 : {
1967 8813 : if (ctx->global->caught_exceptions.length () < 2)
1968 : {
1969 5016 : no_caught_exceptions:
1970 5016 : if (!ctx->quiet)
1971 0 : error_at (loc, "%qD called with no caught exceptions pending",
1972 : fndecl);
1973 5016 : *non_constant_p = true;
1974 5016 : return call;
1975 : }
1976 : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1977 : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1978 : ctx->global->caught_exceptions vector to end with
1979 : __cxa_allocate_exception created artificial VAR_DECL (the
1980 : exception object) followed by handler type, pushed by TRY_BLOCK
1981 : evaluation. The only difference between the functions is that
1982 : __cxa_begin_catch pops the handler type from the vector and keeps
1983 : the VAR_DECL last and decreases uncaught_exceptions. The
1984 : VAR_DECL after __cxa_begin_catch serves as the current exception
1985 : and is then popped in __cxa_end_catch evaluation. */
1986 3797 : tree handler_type = ctx->global->caught_exceptions.last ();
1987 3797 : if (handler_type && (VAR_P (handler_type)
1988 3715 : || TREE_CODE (handler_type) == TREE_LIST))
1989 0 : goto no_caught_exceptions;
1990 3797 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1991 3797 : arg = ctx->global->caught_exceptions[idx];
1992 3797 : if (TREE_CODE (arg) == TREE_LIST)
1993 0 : arg = TREE_VALUE (arg);
1994 3797 : gcc_assert (VAR_P (arg));
1995 3797 : if (kind == CXA_BEGIN_CATCH)
1996 : {
1997 3769 : ctx->global->caught_exceptions.pop ();
1998 3769 : --ctx->global->uncaught_exceptions;
1999 : }
2000 3797 : if (handler_type == NULL_TREE)
2001 : /* Used for catch (...). Just return void. */
2002 82 : return void_node;
2003 3715 : else if (POINTER_TYPE_P (handler_type))
2004 : {
2005 : /* Used for catch of a pointer. */
2006 66 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
2007 66 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
2008 : size_zero_node, NULL_TREE, NULL_TREE);
2009 66 : arg = cp_convert (handler_type, arg,
2010 66 : ctx->quiet ? tf_none : tf_warning_or_error);
2011 66 : if (arg == error_mark_node)
2012 : {
2013 0 : *non_constant_p = true;
2014 0 : return call;
2015 : }
2016 : }
2017 : else
2018 : {
2019 : /* Used for catch of a non-pointer type. */
2020 3649 : tree exc_type = strip_array_types (TREE_TYPE (arg));
2021 3629 : if (TYPE_PTRMEM_P (handler_type)
2022 3695 : && !same_type_ignoring_top_level_qualifiers_p
2023 46 : (handler_type, exc_type))
2024 : {
2025 46 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
2026 46 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
2027 : size_zero_node, NULL_TREE, NULL_TREE);
2028 46 : arg = cp_convert (handler_type, arg,
2029 46 : ctx->quiet ? tf_none
2030 : : tf_warning_or_error);
2031 46 : if (arg == error_mark_node)
2032 : {
2033 0 : *non_constant_p = true;
2034 0 : return call;
2035 : }
2036 46 : tree var = build_decl (loc, VAR_DECL, heap_identifier,
2037 46 : handler_type);
2038 46 : DECL_ARTIFICIAL (var) = 1;
2039 46 : ctx->global->heap_vars.safe_push (var);
2040 46 : ctx->global->put_value (var, NULL_TREE);
2041 46 : tree init = build2_loc (loc, INIT_EXPR, handler_type,
2042 : var, arg);
2043 46 : arg = ctx->global->caught_exceptions[idx];
2044 46 : if (TREE_CODE (arg) == TREE_LIST)
2045 : {
2046 0 : DECL_CHAIN (var) = TREE_PURPOSE (arg);
2047 0 : TREE_PURPOSE (arg) = var;
2048 : }
2049 : else
2050 46 : ctx->global->caught_exceptions[idx]
2051 92 : = build_tree_list (var, arg);
2052 46 : arg = cp_build_addr_expr (var, tf_none);
2053 46 : arg = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (arg),
2054 : init, arg);
2055 : }
2056 : else
2057 : {
2058 3603 : tree exc_ptr_type = build_pointer_type (exc_type);
2059 3603 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
2060 3603 : if (CLASS_TYPE_P (handler_type))
2061 : {
2062 3513 : tree ptr_type = build_pointer_type (handler_type);
2063 3513 : arg = cp_convert (ptr_type, arg,
2064 3513 : ctx->quiet ? tf_none
2065 : : tf_warning_or_error);
2066 3513 : if (arg == error_mark_node)
2067 : {
2068 0 : *non_constant_p = true;
2069 0 : return call;
2070 : }
2071 : }
2072 : }
2073 : }
2074 3715 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2075 : non_constant_p, overflow_p,
2076 3715 : jump_target);
2077 : }
2078 44710 : for (int i = 0; i < nargs; ++i)
2079 : {
2080 21187 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
2081 : vc_prvalue, non_constant_p,
2082 : overflow_p, jump_target);
2083 21187 : if (*non_constant_p)
2084 : return call;
2085 21187 : if (*jump_target)
2086 : return NULL_TREE;
2087 : }
2088 23523 : switch (kind)
2089 : {
2090 15049 : case CXA_ALLOCATE_EXCEPTION:
2091 15049 : if (nargs != 1)
2092 0 : goto invalid_nargs;
2093 15049 : if (!tree_fits_uhwi_p (args[0]))
2094 : {
2095 0 : if (!ctx->quiet)
2096 0 : error_at (loc, "cannot allocate exception: size not constant");
2097 0 : *non_constant_p = true;
2098 0 : return call;
2099 : }
2100 : else
2101 : {
2102 30098 : tree type = build_array_type_nelts (char_type_node,
2103 15049 : tree_to_uhwi (args[0]));
2104 15049 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2105 15049 : ctx->global->put_value (var, NULL_TREE);
2106 15049 : return fold_convert (ptr_type_node, build_address (var));
2107 : }
2108 2 : case CXA_FREE_EXCEPTION:
2109 2 : if (nargs != 1)
2110 0 : goto invalid_nargs;
2111 2 : arg = cxa_check_throw_arg (args[0], true);
2112 2 : if (arg == NULL_TREE)
2113 : {
2114 0 : invalid_ptr:
2115 0 : if (!ctx->quiet)
2116 0 : error_at (loc, "first argument to %qD function not result of "
2117 : "%<__cxa_allocate_exception%>", fndecl);
2118 0 : *non_constant_p = true;
2119 0 : return call;
2120 : }
2121 2 : DECL_NAME (arg) = heap_deleted_identifier;
2122 2 : ctx->global->destroy_value (arg);
2123 2 : ctx->global->heap_dealloc_count++;
2124 2 : return void_node;
2125 1948 : case CXA_THROW:
2126 1948 : if (nargs != 3)
2127 0 : goto invalid_nargs;
2128 1948 : arg = cxa_check_throw_arg (args[0], false);
2129 1948 : if (arg == NULL_TREE)
2130 0 : goto invalid_ptr;
2131 1948 : DECL_EXCEPTION_REFCOUNT (arg)
2132 1948 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2133 : size_one_node);
2134 1948 : ++ctx->global->uncaught_exceptions;
2135 1948 : *jump_target = arg;
2136 1948 : return void_node;
2137 0 : case CXA_BEGIN_CATCH:
2138 0 : case CXA_GET_EXCEPTION_PTR:
2139 0 : goto invalid_nargs;
2140 3765 : case CXA_END_CATCH:
2141 3765 : if (nargs != 0)
2142 0 : goto invalid_nargs;
2143 3765 : if (ctx->global->caught_exceptions.is_empty ())
2144 : {
2145 0 : no_active_exc:
2146 0 : if (!ctx->quiet)
2147 0 : error_at (loc, "%qD called with no caught exceptions active",
2148 : fndecl);
2149 0 : *non_constant_p = true;
2150 0 : return call;
2151 : }
2152 : else
2153 : {
2154 3765 : arg = ctx->global->caught_exceptions.pop ();
2155 3765 : if (arg == NULL_TREE
2156 3765 : || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2157 0 : goto no_active_exc;
2158 3765 : if (TREE_CODE (arg) == TREE_LIST)
2159 : {
2160 92 : for (tree aux = TREE_PURPOSE (arg); aux; aux = DECL_CHAIN (aux))
2161 : {
2162 46 : DECL_NAME (aux) = heap_deleted_identifier;
2163 46 : ctx->global->destroy_value (aux);
2164 46 : ctx->global->heap_dealloc_count++;
2165 : }
2166 46 : arg = TREE_VALUE (arg);
2167 : }
2168 3765 : DECL_CHAIN (arg) = NULL_TREE;
2169 3843 : free_except:
2170 3843 : DECL_EXCEPTION_REFCOUNT (arg)
2171 3843 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2172 : size_one_node);
2173 3843 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2174 : {
2175 3633 : if (type_build_dtor_call (TREE_TYPE (arg)))
2176 : {
2177 : /* So that we don't complain about out-of-consteval use. */
2178 3415 : temp_override<tree> ovr (current_function_decl);
2179 3415 : if (ctx->call && ctx->call->fundef)
2180 3415 : current_function_decl = ctx->call->fundef->decl;
2181 3415 : tree cleanup
2182 3447 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2183 : : tf_warning_or_error));
2184 3415 : if (cleanup == error_mark_node)
2185 0 : *non_constant_p = true;
2186 3415 : tree jmp_target = NULL_TREE;
2187 3415 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2188 : non_constant_p, overflow_p,
2189 : &jmp_target);
2190 3415 : if (throws (&jmp_target))
2191 0 : *jump_target = jmp_target;
2192 3415 : }
2193 3633 : DECL_NAME (arg) = heap_deleted_identifier;
2194 3633 : ctx->global->destroy_value (arg);
2195 3633 : ctx->global->heap_dealloc_count++;
2196 : }
2197 : }
2198 3843 : return void_node;
2199 98 : case CXA_RETHROW:
2200 98 : if (nargs != 0)
2201 0 : goto invalid_nargs;
2202 98 : unsigned idx;
2203 196 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2204 82 : if (arg == NULL_TREE || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2205 0 : --idx;
2206 : else
2207 : break;
2208 98 : if (arg == NULL_TREE)
2209 : {
2210 16 : if (!ctx->quiet)
2211 4 : error_at (loc, "%qD called with no caught exceptions active",
2212 : fndecl);
2213 16 : *non_constant_p = true;
2214 16 : return call;
2215 : }
2216 82 : if (TREE_CODE (arg) == TREE_LIST)
2217 0 : arg = TREE_VALUE (arg);
2218 82 : DECL_EXCEPTION_REFCOUNT (arg)
2219 82 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2220 82 : ++ctx->global->uncaught_exceptions;
2221 : /* Don't cache calls which rethrow, they depend on the current
2222 : exception which might be caught in the caller. */
2223 82 : ctx->global->state_dependent = true;
2224 82 : *jump_target = arg;
2225 82 : return void_node;
2226 163 : case CXA_BAD_CAST:
2227 163 : case CXA_BAD_TYPEID:
2228 163 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2229 163 : if (nargs != 0)
2230 0 : goto invalid_nargs;
2231 : else
2232 : {
2233 163 : tree name;
2234 163 : switch (kind)
2235 : {
2236 123 : case CXA_BAD_CAST:
2237 123 : name = get_identifier ("bad_cast");
2238 123 : break;
2239 7 : case CXA_BAD_TYPEID:
2240 7 : name = get_identifier ("bad_typeid");
2241 7 : break;
2242 33 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2243 33 : name = get_identifier ("bad_array_new_length");
2244 33 : break;
2245 : default:
2246 : gcc_unreachable ();
2247 : }
2248 163 : tree decl = lookup_qualified_name (std_node, name);
2249 163 : if (TREE_CODE (decl) != TYPE_DECL
2250 135 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2251 298 : || !type_build_ctor_call (TREE_TYPE (decl)))
2252 : {
2253 28 : if (!ctx->quiet)
2254 8 : error_at (loc, "%qD called without %<std::%D%> being defined",
2255 : fndecl, name);
2256 28 : *non_constant_p = true;
2257 28 : return call;
2258 : }
2259 135 : tree type = TREE_TYPE (decl);
2260 135 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2261 135 : tree ctor
2262 135 : = build_special_member_call (var, complete_ctor_identifier,
2263 : NULL, type, LOOKUP_NORMAL,
2264 135 : ctx->quiet ? tf_none
2265 : : tf_warning_or_error);
2266 135 : if (ctor == error_mark_node)
2267 : {
2268 0 : *non_constant_p = true;
2269 0 : return call;
2270 : }
2271 135 : if (TREE_CONSTANT (ctor))
2272 0 : ctx->global->put_value (var, ctor);
2273 : else
2274 : {
2275 135 : ctx->global->put_value (var, NULL_TREE);
2276 135 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2277 : non_constant_p, overflow_p,
2278 : jump_target);
2279 135 : if (*non_constant_p)
2280 : return call;
2281 135 : if (throws (jump_target))
2282 : return NULL_TREE;
2283 : }
2284 135 : ++ctx->global->uncaught_exceptions;
2285 135 : *jump_target = var;
2286 : }
2287 135 : return void_node;
2288 82 : case BUILTIN_UNCAUGHT_EXCEPTIONS:
2289 82 : if (nargs != 0)
2290 0 : goto invalid_nargs;
2291 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2292 : want to give a definite answer during mce_unknown evaluation,
2293 : because that might prevent evaluation later on when some
2294 : exceptions might be uncaught. But unlike that, we don't
2295 : want to constant fold it even during cp_fold, because at runtime
2296 : std::uncaught_exceptions () might still be non-zero. */
2297 82 : if (ctx->manifestly_const_eval != mce_true)
2298 : {
2299 46 : *non_constant_p = true;
2300 46 : return call;
2301 : }
2302 : /* Don't cache calls which call __builtin_uncaught_exceptions (),
2303 : they depend on the current uncaught exceptions which might
2304 : be the state from their caller. */
2305 36 : ctx->global->state_dependent = true;
2306 36 : return build_int_cst (integer_type_node,
2307 36 : ctx->global->uncaught_exceptions);
2308 2250 : case BUILTIN_CURRENT_EXCEPTION:
2309 2250 : if (nargs != 0)
2310 0 : goto invalid_nargs;
2311 : else
2312 : {
2313 2250 : tree name = get_identifier ("exception_ptr");
2314 2250 : tree decl = lookup_qualified_name (std_node, name);
2315 2250 : tree fld;
2316 2250 : if (TREE_CODE (decl) != TYPE_DECL
2317 2250 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2318 2250 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2319 2250 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2320 2250 : || DECL_ARTIFICIAL (fld)
2321 2250 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2322 2250 : || next_aggregate_field (DECL_CHAIN (fld))
2323 4500 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2324 2250 : TYPE_SIZE (TREE_TYPE (fld))))
2325 : {
2326 0 : if (!ctx->quiet)
2327 0 : error_at (loc, "%qD called without supportable %qs",
2328 : fndecl, "std::exception_ptr");
2329 0 : *non_constant_p = true;
2330 0 : return call;
2331 : }
2332 4500 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2333 42 : if (arg == NULL_TREE
2334 42 : || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2335 0 : --idx;
2336 : else
2337 : break;
2338 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2339 : want to give a definite answer during mce_unknown evaluation,
2340 : because that might prevent evaluation later on when some
2341 : exceptions might be current. But unlike that, we don't
2342 : want to constant fold it to null even during cp_fold, because
2343 : at runtime std::current_exception () might still be non-null. */
2344 2250 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2345 : {
2346 2196 : *non_constant_p = true;
2347 2196 : return call;
2348 : }
2349 54 : if (arg == NULL_TREE)
2350 12 : arg = build_zero_cst (TREE_TYPE (fld));
2351 : else
2352 : {
2353 42 : if (TREE_CODE (arg) == TREE_LIST)
2354 0 : arg = TREE_VALUE (arg);
2355 42 : DECL_EXCEPTION_REFCOUNT (arg)
2356 42 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2357 : size_one_node);
2358 42 : arg = fold_convert (ptr_type_node, build_address (arg));
2359 : }
2360 : /* Don't cache calls which call __builtin_current_exception (),
2361 : they depend on the current exception which might be caught
2362 : in the caller. */
2363 54 : ctx->global->state_dependent = true;
2364 54 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2365 : }
2366 40 : case STD_RETHROW_EXCEPTION:
2367 40 : if (nargs != 1)
2368 0 : goto invalid_nargs;
2369 40 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2370 : {
2371 40 : arg = args[0];
2372 40 : STRIP_NOPS (arg);
2373 40 : if (TREE_CODE (arg) == ADDR_EXPR)
2374 : {
2375 40 : args[0]
2376 40 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2377 : vc_prvalue, non_constant_p,
2378 : overflow_p, jump_target);
2379 40 : if (*non_constant_p)
2380 : return call;
2381 40 : if (*jump_target)
2382 : return NULL_TREE;
2383 : }
2384 : }
2385 40 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2386 40 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2387 : {
2388 0 : invalid_std_rethrow:
2389 0 : if (!ctx->quiet)
2390 0 : error_at (loc, "%qD called with unexpected %qs argument",
2391 : fndecl, "std::exception_ptr");
2392 0 : *non_constant_p = true;
2393 0 : return void_node;
2394 : }
2395 40 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2396 40 : if (arg == NULL_TREE)
2397 0 : goto invalid_std_rethrow;
2398 40 : DECL_EXCEPTION_REFCOUNT (arg)
2399 40 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2400 40 : ++ctx->global->uncaught_exceptions;
2401 40 : *jump_target = arg;
2402 40 : return void_node;
2403 126 : case BUILTIN_EH_PTR_ADJUST_REF:
2404 126 : if (nargs != 2)
2405 0 : goto invalid_nargs;
2406 126 : arg = cxa_check_throw_arg (args[0], false);
2407 126 : if (arg == NULL_TREE)
2408 0 : goto invalid_ptr;
2409 126 : if (integer_onep (args[1]))
2410 48 : DECL_EXCEPTION_REFCOUNT (arg)
2411 96 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2412 : size_one_node);
2413 78 : else if (integer_minus_onep (args[1]))
2414 78 : goto free_except;
2415 : else
2416 : {
2417 0 : if (!ctx->quiet)
2418 0 : error_at (loc, "%qD called with second argument "
2419 : "other than 1 or -1", fndecl);
2420 0 : *non_constant_p = true;
2421 : }
2422 48 : return void_node;
2423 0 : default:
2424 0 : gcc_unreachable ();
2425 : }
2426 : }
2427 :
2428 : /* Variables and functions to manage constexpr call expansion context.
2429 : These do not need to be marked for PCH or GC. */
2430 :
2431 : /* FIXME remember and print actual constant arguments. */
2432 : static vec<tree> call_stack;
2433 : static int call_stack_tick;
2434 : static int last_cx_error_tick;
2435 :
2436 : /* Attempt to evaluate T which represents a call to __builtin_constexpr_diag.
2437 : The arguments should be an integer (0 for inform, 1 for warning, 2 for
2438 : error) optionally with 16 ored in if it should use caller's caller location
2439 : instead of caller's location and 2 messages which are either a pointer to
2440 : a STRING_CST or class with data () and size () member functions like
2441 : string_view or u8string_view. The first message is a tag, with "" passed
2442 : for no tag, data () should return const char *, the tag should only contain
2443 : alphanumeric letters or underscores. The second message is the diagnostic
2444 : message, data () can be either const char * or const char8_t *. size ()
2445 : should return the corresponding length of the strings in bytes as an
2446 : integer. */
2447 :
2448 : static tree
2449 210 : cxx_eval_constexpr_diag (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
2450 : bool *overflow_p, tree *jump_target)
2451 : {
2452 210 : location_t loc = EXPR_LOCATION (t);
2453 210 : if (call_expr_nargs (t) != 3)
2454 : {
2455 12 : if (!ctx->quiet)
2456 12 : error_at (loc, "wrong number of arguments to %qs call",
2457 : "__builtin_constexpr_diag");
2458 12 : *non_constant_p = true;
2459 12 : return t;
2460 : }
2461 : tree args[3];
2462 792 : for (int i = 0; i < 3; ++i)
2463 : {
2464 594 : tree arg = convert_from_reference (CALL_EXPR_ARG (t, i));
2465 594 : arg = cxx_eval_constant_expression (ctx, arg,
2466 : (i == 0
2467 534 : || POINTER_TYPE_P (TREE_TYPE (arg)))
2468 990 : ? vc_prvalue : vc_glvalue,
2469 : non_constant_p, overflow_p,
2470 : jump_target);
2471 594 : if (*jump_target)
2472 : return NULL_TREE;
2473 594 : if (*non_constant_p)
2474 : return t;
2475 594 : args[i] = arg;
2476 : }
2477 396 : if (TREE_CODE (args[0]) != INTEGER_CST
2478 198 : || wi::to_widest (args[0]) < 0
2479 194 : || wi::to_widest (args[0]) > 18
2480 396 : || (wi::to_widest (args[0]) & 15) > 2)
2481 : {
2482 8 : if (!ctx->quiet)
2483 8 : error_at (loc, "first %qs call argument should be 0, 1, 2, 16, 17 or "
2484 : "18", "__builtin_constexpr_diag");
2485 8 : *non_constant_p = true;
2486 8 : return t;
2487 : }
2488 190 : const char *msgs[2] = {};
2489 190 : int lens[3] = {};
2490 950 : cexpr_str cstrs[2];
2491 478 : diagnostics::kind kind = diagnostics::kind::error;
2492 478 : for (int i = 1; i < 3; ++i)
2493 : {
2494 344 : tree arg = args[i];
2495 344 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2496 : {
2497 206 : tree str = arg;
2498 206 : STRIP_NOPS (str);
2499 206 : if (TREE_CODE (str) == ADDR_EXPR
2500 206 : && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST)
2501 : {
2502 206 : str = TREE_OPERAND (str, 0);
2503 206 : tree eltype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)));
2504 206 : if (eltype == char_type_node
2505 24 : || (i == 2 && eltype == char8_type_node))
2506 344 : arg = str;
2507 : }
2508 : }
2509 344 : cstrs[i - 1].message = arg;
2510 344 : if (!cstrs[i - 1].type_check (loc, i == 2))
2511 : {
2512 40 : *non_constant_p = true;
2513 40 : return t;
2514 : }
2515 304 : if (!cstrs[i - 1].extract (loc, msgs[i - 1], lens[i - 1], ctx,
2516 : non_constant_p, overflow_p, jump_target))
2517 : {
2518 16 : if (*jump_target)
2519 : return NULL_TREE;
2520 16 : *non_constant_p = true;
2521 16 : return t;
2522 : }
2523 : }
2524 134 : if (msgs[0])
2525 : {
2526 726 : for (int i = 0; i < lens[0]; ++i)
2527 596 : if (!ISALNUM (msgs[0][i]) && msgs[0][i] != '_')
2528 : {
2529 4 : if (!ctx->quiet)
2530 4 : error_at (loc, "%qs tag string contains %qc character other than"
2531 : " letters, digits or %<_%>",
2532 : "__builtin_constexpr_diag", msgs[0][i]);
2533 4 : *non_constant_p = true;
2534 4 : return t;
2535 : }
2536 : }
2537 130 : if (ctx->manifestly_const_eval == mce_unknown)
2538 : {
2539 0 : *non_constant_p = true;
2540 0 : return t;
2541 : }
2542 130 : int arg0 = tree_to_uhwi (args[0]);
2543 130 : if (arg0 & 16)
2544 : {
2545 32 : arg0 &= 15;
2546 32 : if (!call_stack.is_empty ())
2547 : {
2548 32 : tree call = call_stack.last ();
2549 32 : if (EXPR_HAS_LOCATION (call))
2550 32 : loc = EXPR_LOCATION (call);
2551 : }
2552 : }
2553 130 : if (arg0 == 0)
2554 : kind = diagnostics::kind::note;
2555 82 : else if (arg0 == 1)
2556 36 : kind = diagnostics::kind::warning;
2557 130 : if (lens[0])
2558 : {
2559 88 : const char *color = "error";
2560 88 : if (kind == diagnostics::kind::note)
2561 : color = "note";
2562 60 : else if (kind == diagnostics::kind::warning)
2563 32 : color = "warning";
2564 88 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s [%r%.*s%R]",
2565 : lens[1], msgs[1], color, lens[0], msgs[0]);
2566 : }
2567 : else
2568 42 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s",
2569 : lens[1], msgs[1]);
2570 130 : return void_node;
2571 570 : }
2572 :
2573 : static tree eval_and_check_array_index (const constexpr_ctx *, tree, bool,
2574 : bool *, bool *, tree *);
2575 :
2576 : /* Helper function for cxx_eval_is_within_lifetime and
2577 : cxx_eval_start_lifetime. Check if ARG is within lifetime. */
2578 :
2579 : static tree
2580 5166 : is_within_lifetime (location_t loc, const constexpr_ctx *ctx, tree t, tree fun,
2581 : tree arg, bool *non_constant_p, bool *overflow_p,
2582 : tree *jump_target)
2583 : {
2584 5166 : auto_vec <tree, 4> refs;
2585 5166 : tree obj;
2586 12281 : for (obj = arg; ; obj = TREE_OPERAND (obj, 0))
2587 : {
2588 12281 : switch (TREE_CODE (obj))
2589 : {
2590 7115 : case COMPONENT_REF:
2591 7115 : case ARRAY_REF:
2592 7115 : case REALPART_EXPR:
2593 7115 : case IMAGPART_EXPR:
2594 7115 : refs.safe_push (obj);
2595 7115 : continue;
2596 5166 : default:
2597 5166 : break;
2598 : }
2599 5166 : break;
2600 : }
2601 5166 : tree *valp = nullptr;
2602 5166 : bool check_mutable = false;
2603 5166 : if (DECL_P (obj))
2604 : {
2605 5124 : valp = ctx->global->get_raw_value_ptr (obj);
2606 5124 : if (!valp
2607 1170 : && VAR_P (obj)
2608 1170 : && TREE_STATIC (obj)
2609 5548 : && decl_constant_var_p (obj))
2610 : {
2611 410 : valp = &DECL_INITIAL (obj);
2612 410 : check_mutable = true;
2613 : }
2614 : }
2615 5124 : if (!valp || *valp == void_list_node)
2616 : {
2617 988 : if (!ctx->quiet)
2618 : {
2619 61 : auto_diagnostic_group d;
2620 61 : if (DECL_P (obj) && DECL_NAME (obj) == heap_deleted_identifier)
2621 : {
2622 3 : error_at (loc, "%qE on allocated storage after deallocation "
2623 3 : "is not a constant expression", DECL_NAME (fun));
2624 3 : inform (DECL_SOURCE_LOCATION (obj), "allocated here");
2625 : }
2626 58 : else if (DECL_P (obj) && ctx->global->is_outside_lifetime (obj))
2627 : {
2628 45 : error_at (loc, "%qE on %qE after its storage has been released "
2629 45 : "is not a constant expression", DECL_NAME (fun), obj);
2630 45 : inform (DECL_SOURCE_LOCATION (obj), "declared here");
2631 : }
2632 : else
2633 : {
2634 13 : error_at (loc, "%qE on %qE from outside current evaluation "
2635 13 : "is not a constant expression", DECL_NAME (fun), obj);
2636 13 : if (DECL_P (obj) && DECL_SOURCE_LOCATION (obj))
2637 10 : inform (DECL_SOURCE_LOCATION (obj), "from here");
2638 : }
2639 61 : }
2640 988 : *non_constant_p = true;
2641 988 : return t;
2642 : }
2643 4178 : tree val = *valp;
2644 4178 : if (val == void_node)
2645 15 : return boolean_false_node;
2646 4163 : unsigned i;
2647 4163 : tree ref;
2648 : /* Magic value used for val when inside the following loop when
2649 : inside of an omitted part of initializer due to it being
2650 : uninitialized. In this case return boolean_true_node unless
2651 : there is some union member access, in unitialized union
2652 : no member is within lifetime. */
2653 4163 : const tree val_uninit = global_namespace;
2654 : /* Magic value used for val when inside the following loop when
2655 : inside of an omitted part of initializer due to it being
2656 : zero initialized. In this case return boolean_true_node unless
2657 : there is some union member access except for the first member. */
2658 4163 : const tree val_zero_init = std_node;
2659 4163 : if (val == NULL_TREE)
2660 141 : val = val_uninit;
2661 12811 : FOR_EACH_VEC_ELT_REVERSE (refs, i, ref)
2662 5273 : switch (TREE_CODE (ref))
2663 : {
2664 18 : case REALPART_EXPR:
2665 18 : case IMAGPART_EXPR:
2666 18 : if (TREE_CODE (val) == COMPLEX_EXPR
2667 18 : && (TREE_OPERAND (val, TREE_CODE (ref) == IMAGPART_EXPR)
2668 12 : == void_node))
2669 6 : return boolean_false_node;
2670 12 : return boolean_true_node;
2671 4231 : case COMPONENT_REF:
2672 4231 : if (check_mutable && DECL_MUTABLE_P (TREE_OPERAND (ref, 1)))
2673 : {
2674 12 : if (!ctx->quiet)
2675 3 : error_at (loc, "%qE on %<mutable%> sub-object %qD",
2676 3 : DECL_NAME (fun), TREE_OPERAND (ref, 1));
2677 12 : *non_constant_p = true;
2678 12 : return t;
2679 : }
2680 4219 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == UNION_TYPE)
2681 : {
2682 1280 : tree union_type = TREE_TYPE (TREE_OPERAND (ref, 0));
2683 1280 : if (val == val_zero_init)
2684 : {
2685 48 : if (TREE_OPERAND (ref, 1)
2686 24 : != next_aggregate_field (TYPE_FIELDS (union_type)))
2687 15 : return boolean_false_node;
2688 9 : continue;
2689 : }
2690 1256 : else if (val == val_uninit)
2691 102 : return boolean_false_node;
2692 1154 : else if (val == error_mark_node)
2693 : return val;
2694 : else
2695 : {
2696 1154 : gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
2697 1154 : if (CONSTRUCTOR_NELTS (val) == 0)
2698 : {
2699 147 : if (CONSTRUCTOR_NO_CLEARING (val))
2700 135 : return boolean_false_node;
2701 12 : tree first
2702 12 : = next_aggregate_field (TYPE_FIELDS (union_type));
2703 12 : if (first != TREE_OPERAND (ref, 1))
2704 3 : return boolean_false_node;
2705 9 : val = val_zero_init;
2706 9 : continue;
2707 9 : }
2708 : else
2709 : {
2710 1007 : if (CONSTRUCTOR_ELT (val, 0)->index
2711 1007 : != TREE_OPERAND (ref, 1))
2712 287 : return boolean_false_node;
2713 720 : val = CONSTRUCTOR_ELT (val, 0)->value;
2714 720 : if (val == void_node)
2715 0 : return boolean_false_node;
2716 720 : continue;
2717 : }
2718 : }
2719 : }
2720 2939 : if (val == val_zero_init || val == val_uninit)
2721 80 : continue;
2722 2859 : if (val == error_mark_node)
2723 : return val;
2724 2847 : gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
2725 : unsigned int j;
2726 : tree field, value;
2727 4362 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), j, field, value)
2728 2708 : if (field == TREE_OPERAND (ref, 1))
2729 : {
2730 1193 : if (value == void_node)
2731 6 : return boolean_false_node;
2732 : val = value;
2733 : ref = NULL_TREE;
2734 : break;
2735 : }
2736 2841 : if (ref == NULL_TREE)
2737 1187 : continue;
2738 1654 : if (CONSTRUCTOR_NO_CLEARING (val))
2739 : {
2740 1537 : if (CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (val))
2741 75 : return boolean_false_node;
2742 : val = val_uninit;
2743 : }
2744 : else
2745 : val = val_zero_init;
2746 1579 : continue;
2747 1024 : case ARRAY_REF:
2748 1024 : field = eval_and_check_array_index (ctx, ref, false,
2749 : non_constant_p, overflow_p,
2750 : jump_target);
2751 1024 : if (*jump_target)
2752 : return NULL_TREE;
2753 1024 : if (*non_constant_p)
2754 : return t;
2755 1012 : if (val == val_zero_init || val == val_uninit)
2756 33 : continue;
2757 979 : if (val == error_mark_node)
2758 : return val;
2759 979 : if (TREE_CODE (val) == STRING_CST)
2760 0 : return boolean_true_node;
2761 979 : gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
2762 979 : HOST_WIDE_INT idx;
2763 979 : idx = find_array_ctor_elt (val, field, false);
2764 979 : if (idx != -1)
2765 : {
2766 595 : val = CONSTRUCTOR_ELT (val, idx)->value;
2767 595 : if (val == void_node)
2768 72 : return boolean_false_node;
2769 523 : continue;
2770 : }
2771 384 : if (CONSTRUCTOR_NO_CLEARING (val))
2772 : {
2773 159 : if (CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (val))
2774 39 : return boolean_false_node;
2775 : val = val_uninit;
2776 : }
2777 : else
2778 : val = val_zero_init;
2779 345 : continue;
2780 0 : default:
2781 0 : gcc_unreachable ();
2782 1924 : }
2783 3375 : return boolean_true_node;
2784 5166 : }
2785 :
2786 : /* Attempt to evaluate T which represents a call to
2787 : __builtin_is_within_lifetime. */
2788 :
2789 : static tree
2790 3952 : cxx_eval_is_within_lifetime (const constexpr_ctx *ctx, tree t, tree fun,
2791 : bool *non_constant_p, bool *overflow_p,
2792 : tree *jump_target)
2793 : {
2794 3952 : location_t loc = EXPR_LOCATION (t);
2795 3952 : tree arg = CALL_EXPR_ARG (t, 0);
2796 3952 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2797 : non_constant_p, overflow_p,
2798 : jump_target);
2799 3952 : if (*jump_target)
2800 : return NULL_TREE;
2801 3952 : if (*non_constant_p)
2802 : return t;
2803 : /* Need to strip casts to pointers to void, but should preserve
2804 : pointer casts within the innermost pointer to void cast if
2805 : any, so that e.g. pointers to heap allocations are handled
2806 : correctly. */
2807 : tree p = arg;
2808 6290 : while (CONVERT_EXPR_P (p) || TREE_CODE (p) == NON_LVALUE_EXPR)
2809 : {
2810 2754 : if (!TYPE_PTR_P (TREE_TYPE (p)))
2811 : break;
2812 2754 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (p))))
2813 174 : arg = TREE_OPERAND (p, 0);
2814 2754 : p = TREE_OPERAND (p, 0);
2815 : }
2816 3536 : if (integer_zerop (arg))
2817 : {
2818 24 : if (!ctx->quiet)
2819 6 : error_at (loc, "%qE called with a null pointer",
2820 6 : DECL_NAME (fun));
2821 24 : *non_constant_p = true;
2822 24 : return t;
2823 : }
2824 3512 : if (POINTER_TYPE_P (TREE_TYPE (arg))
2825 3512 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == FUNCTION_TYPE
2826 3491 : || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == METHOD_TYPE))
2827 : {
2828 21 : if (!ctx->quiet)
2829 6 : error_at (loc, "%qE called with pointer to function",
2830 6 : DECL_NAME (fun));
2831 21 : *non_constant_p = true;
2832 21 : return t;
2833 : }
2834 3491 : arg = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (arg)), arg);
2835 3491 : arg = cxx_eval_constant_expression (ctx, arg, vc_glvalue,
2836 : non_constant_p, overflow_p,
2837 : jump_target);
2838 3491 : if (*jump_target)
2839 : return NULL_TREE;
2840 3491 : if (*non_constant_p)
2841 : return t;
2842 3491 : return is_within_lifetime (loc, ctx, t, fun, arg,
2843 3491 : non_constant_p, overflow_p, jump_target);
2844 : }
2845 :
2846 : static tree cxx_eval_store_expression (const constexpr_ctx *, tree,
2847 : value_cat, bool *, bool *,
2848 : tree *);
2849 :
2850 : /* Attempt to evaluate T which represents a call to
2851 : __builtin_start_lifetime. */
2852 :
2853 : static tree
2854 1681 : cxx_eval_start_lifetime (const constexpr_ctx *ctx, tree t, tree fun,
2855 : bool *non_constant_p, bool *overflow_p,
2856 : tree *jump_target)
2857 : {
2858 1681 : location_t loc = EXPR_LOCATION (t);
2859 1681 : tree arg = CALL_EXPR_ARG (t, 0), within_lifetime;
2860 1681 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2861 : non_constant_p, overflow_p,
2862 : jump_target);
2863 1681 : if (*jump_target)
2864 : return NULL_TREE;
2865 1681 : if (*non_constant_p)
2866 : return t;
2867 1681 : arg = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (arg)), arg);
2868 1681 : arg = cxx_eval_constant_expression (ctx, arg, vc_glvalue,
2869 : non_constant_p, overflow_p,
2870 : jump_target);
2871 1681 : if (*jump_target)
2872 : return NULL_TREE;
2873 1681 : if (*non_constant_p)
2874 : return t;
2875 1681 : switch (TREE_CODE (arg))
2876 : {
2877 1675 : case COMPONENT_REF:
2878 1675 : case ARRAY_REF:
2879 1675 : case REALPART_EXPR:
2880 1675 : case IMAGPART_EXPR:
2881 1675 : within_lifetime
2882 1675 : = is_within_lifetime (loc, ctx, t, fun, TREE_OPERAND (arg, 0),
2883 : non_constant_p, overflow_p, jump_target);
2884 1675 : if (*jump_target)
2885 : return NULL_TREE;
2886 1675 : if (*non_constant_p)
2887 : return t;
2888 1667 : if (within_lifetime != boolean_true_node)
2889 : {
2890 90 : if (!ctx->quiet)
2891 30 : error_at (loc, "%qE with containing object not within lifetime",
2892 30 : DECL_NAME (fun));
2893 90 : *non_constant_p = true;
2894 90 : return t;
2895 : }
2896 : break;
2897 : default:
2898 : break;
2899 : }
2900 1583 : tree ctor = build_constructor (TREE_TYPE (arg), NULL);
2901 1583 : CONSTRUCTOR_NO_CLEARING (ctor) = 1;
2902 1583 : CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (ctor) = 1;
2903 1583 : tree modopexpr
2904 1583 : = build3_loc (loc, MODOP_EXPR, TREE_TYPE (arg), arg, ctor, NULL_TREE);
2905 1583 : arg = cxx_eval_store_expression (ctx, modopexpr, vc_discard, non_constant_p,
2906 : overflow_p, jump_target);
2907 1583 : ggc_free (modopexpr);
2908 1583 : if (*jump_target)
2909 : return NULL_TREE;
2910 1583 : if (*non_constant_p)
2911 : return t;
2912 1579 : return void_node;
2913 : }
2914 :
2915 : /* Attempt to evaluate T which represents a call to a builtin function.
2916 : We assume here that all builtin functions evaluate to scalar types
2917 : represented by _CST nodes. */
2918 :
2919 : static tree
2920 20504732 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2921 : value_cat lval,
2922 : bool *non_constant_p, bool *overflow_p,
2923 : tree *jump_target)
2924 : {
2925 20504732 : const int nargs = call_expr_nargs (t);
2926 20504732 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2927 20504732 : tree new_call;
2928 20504732 : int i;
2929 :
2930 : /* Don't fold __builtin_constant_p within a constexpr function. */
2931 20504732 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2932 :
2933 : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2934 : in a constexpr function until we have values for the parameters. */
2935 3526848 : if (bi_const_p
2936 3526848 : && ctx->manifestly_const_eval != mce_true
2937 1708700 : && current_function_decl
2938 1707571 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2939 : {
2940 1369846 : *non_constant_p = true;
2941 1369846 : return t;
2942 : }
2943 :
2944 19134886 : if (fndecl_built_in_p (fun, BUILT_IN_FRONTEND))
2945 3914121 : switch (DECL_FE_FUNCTION_CODE (fun))
2946 : {
2947 3889623 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
2948 : /* For __builtin_is_constant_evaluated, defer it if not
2949 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2950 : without manifestly_const_eval even expressions or parts thereof
2951 : which will later be manifestly const_eval evaluated), otherwise fold
2952 : it to true. */
2953 3889623 : if (ctx->manifestly_const_eval == mce_unknown)
2954 : {
2955 3842453 : *non_constant_p = true;
2956 3842453 : return t;
2957 : }
2958 47170 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2959 47170 : boolean_type_node);
2960 :
2961 11009 : case CP_BUILT_IN_SOURCE_LOCATION:
2962 11009 : {
2963 11009 : temp_override<tree> ovr (current_function_decl);
2964 11009 : if (ctx->call && ctx->call->fundef)
2965 2779 : current_function_decl = ctx->call->fundef->decl;
2966 11009 : return fold_builtin_source_location (t);
2967 11009 : }
2968 :
2969 126 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
2970 126 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2971 : fun, non_constant_p, overflow_p,
2972 126 : jump_target);
2973 :
2974 2250 : case CP_BUILT_IN_CURRENT_EXCEPTION:
2975 2250 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_CURRENT_EXCEPTION,
2976 : fun, non_constant_p, overflow_p,
2977 2250 : jump_target);
2978 :
2979 82 : case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
2980 82 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_UNCAUGHT_EXCEPTIONS,
2981 : fun, non_constant_p, overflow_p,
2982 82 : jump_target);
2983 :
2984 210 : case CP_BUILT_IN_CONSTEXPR_DIAG:
2985 210 : return cxx_eval_constexpr_diag (ctx, t, non_constant_p, overflow_p,
2986 210 : jump_target);
2987 :
2988 3952 : case CP_BUILT_IN_IS_WITHIN_LIFETIME:
2989 3952 : return cxx_eval_is_within_lifetime (ctx, t, fun, non_constant_p,
2990 3952 : overflow_p, jump_target);
2991 :
2992 1681 : case CP_BUILT_IN_START_LIFETIME:
2993 1681 : return cxx_eval_start_lifetime (ctx, t, fun, non_constant_p,
2994 1681 : overflow_p, jump_target);
2995 :
2996 : default:
2997 : break;
2998 : }
2999 :
3000 15225953 : int strops = 0;
3001 15225953 : int strret = 0;
3002 15225953 : bool bos = false;
3003 15225953 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
3004 13878993 : switch (DECL_FUNCTION_CODE (fun))
3005 : {
3006 : case BUILT_IN_STRLEN:
3007 : case BUILT_IN_STRNLEN:
3008 15225802 : strops = 1;
3009 : break;
3010 198103 : case BUILT_IN_MEMCHR:
3011 198103 : case BUILT_IN_STRCHR:
3012 198103 : case BUILT_IN_STRRCHR:
3013 198103 : strops = 1;
3014 198103 : strret = 1;
3015 198103 : break;
3016 109404 : case BUILT_IN_MEMCMP:
3017 109404 : case BUILT_IN_STRCMP:
3018 109404 : strops = 2;
3019 109404 : break;
3020 49461 : case BUILT_IN_STRSTR:
3021 49461 : strops = 2;
3022 49461 : strret = 1;
3023 49461 : break;
3024 66 : case BUILT_IN_ASAN_POINTER_COMPARE:
3025 66 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
3026 66 : case BUILT_IN_OBSERVABLE_CHKPT:
3027 : /* These builtins shall be ignored during constant expression
3028 : evaluation. */
3029 66 : return void_node;
3030 85 : case BUILT_IN_UNREACHABLE:
3031 85 : case BUILT_IN_TRAP:
3032 85 : if (!*non_constant_p && !ctx->quiet)
3033 : {
3034 : /* Do not allow__builtin_unreachable in constexpr function.
3035 : The __builtin_unreachable call with BUILTINS_LOCATION
3036 : comes from cp_maybe_instrument_return. */
3037 25 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
3038 0 : error ("%<constexpr%> call flows off the end of the function");
3039 : else
3040 25 : error ("%q+E is not a constant expression", t);
3041 : }
3042 85 : *non_constant_p = true;
3043 85 : return t;
3044 2881 : case BUILT_IN_OBJECT_SIZE:
3045 2881 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
3046 2881 : bos = ctx->manifestly_const_eval == mce_true;
3047 2881 : break;
3048 : default:
3049 : break;
3050 : }
3051 :
3052 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
3053 : return constant false for a non-constant argument. */
3054 15225802 : constexpr_ctx new_ctx = *ctx;
3055 15225802 : new_ctx.quiet = true;
3056 41353829 : for (i = 0; i < nargs; ++i)
3057 : {
3058 26128029 : tree arg = CALL_EXPR_ARG (t, i);
3059 26128029 : tree oarg = arg;
3060 :
3061 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
3062 : expand_builtin doesn't know how to look in the values table. */
3063 26128029 : bool strop = i < strops;
3064 26128029 : if (strop)
3065 : {
3066 670971 : STRIP_NOPS (arg);
3067 670971 : if (TREE_CODE (arg) == ADDR_EXPR)
3068 8242 : arg = TREE_OPERAND (arg, 0);
3069 : else
3070 : strop = false;
3071 : }
3072 :
3073 : /* If builtin_valid_in_constant_expr_p is true,
3074 : potential_constant_expression_1 has not recursed into the arguments
3075 : of the builtin, verify it here. */
3076 26128029 : if (!builtin_valid_in_constant_expr_p (fun)
3077 26128029 : || potential_constant_expression (arg))
3078 : {
3079 26127892 : bool dummy1 = false, dummy2 = false;
3080 26127892 : tree jmp_target = NULL_TREE;
3081 26127892 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3082 : &dummy1, &dummy2, &jmp_target);
3083 26127890 : if (jmp_target)
3084 : {
3085 0 : *jump_target = jmp_target;
3086 0 : return NULL_TREE;
3087 : }
3088 : }
3089 :
3090 26128027 : if (bi_const_p)
3091 : /* For __builtin_constant_p, fold all expressions with constant values
3092 : even if they aren't C++ constant-expressions. */
3093 2157000 : arg = cp_fold_rvalue (arg);
3094 23971027 : else if (strop)
3095 : {
3096 8242 : if (TREE_CODE (arg) == CONSTRUCTOR)
3097 76 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
3098 8242 : if (TREE_CODE (arg) == STRING_CST)
3099 3013 : arg = build_address (arg);
3100 : else
3101 : arg = oarg;
3102 : }
3103 :
3104 : /* If the argument STRRET refers to didn't end up as the address of a
3105 : STRING_CST, the folded result is already relative to it and must not
3106 : be re-based on the original argument below. */
3107 26128027 : if (i == strret - 1
3108 26128027 : && (TREE_CODE (arg) != ADDR_EXPR
3109 110 : || TREE_CODE (TREE_OPERAND (arg, 0)) != STRING_CST))
3110 : strret = 0;
3111 :
3112 26128027 : args[i] = arg;
3113 : }
3114 15225800 : if (bos)
3115 : {
3116 102 : tree arg = args[0];
3117 102 : STRIP_NOPS (arg);
3118 102 : if (TREE_CODE (arg) == ADDR_EXPR)
3119 102 : args[0] = arg;
3120 : }
3121 :
3122 15225800 : bool save_ffbcp = force_folding_builtin_constant_p;
3123 15225800 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
3124 15225800 : tree save_cur_fn = current_function_decl;
3125 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
3126 15225800 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
3127 37 : && ctx->call
3128 15225804 : && ctx->call->fundef)
3129 4 : current_function_decl = ctx->call->fundef->decl;
3130 15225800 : if (fndecl_built_in_p (fun,
3131 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
3132 : BUILT_IN_FRONTEND))
3133 : {
3134 336 : location_t loc = EXPR_LOCATION (t);
3135 336 : if (nargs >= 1)
3136 333 : VERIFY_CONSTANT (args[0]);
3137 136 : new_call
3138 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
3139 : args);
3140 : }
3141 15225464 : else if (fndecl_built_in_p (fun,
3142 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
3143 : BUILT_IN_FRONTEND))
3144 : {
3145 471 : location_t loc = EXPR_LOCATION (t);
3146 471 : if (nargs >= 2)
3147 : {
3148 465 : VERIFY_CONSTANT (args[0]);
3149 237 : VERIFY_CONSTANT (args[1]);
3150 : }
3151 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
3152 : }
3153 15224993 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
3154 : BUILT_IN_FRONTEND))
3155 : {
3156 4381 : location_t loc = EXPR_LOCATION (t);
3157 4381 : if (nargs >= 1)
3158 : {
3159 4381 : tree arg = CALL_EXPR_ARG (t, 0);
3160 4381 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3161 : non_constant_p, overflow_p,
3162 : jump_target);
3163 4381 : if (*jump_target)
3164 : return NULL_TREE;
3165 4381 : args[0] = arg;
3166 : }
3167 4381 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
3168 : }
3169 : else
3170 30441224 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
3171 15220612 : CALL_EXPR_FN (t), nargs, args);
3172 15225372 : current_function_decl = save_cur_fn;
3173 15225372 : force_folding_builtin_constant_p = save_ffbcp;
3174 15225372 : if (new_call == NULL)
3175 : {
3176 10344579 : if (!*non_constant_p && !ctx->quiet)
3177 : {
3178 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
3179 0 : CALL_EXPR_FN (t), nargs, args);
3180 0 : error ("%q+E is not a constant expression", new_call);
3181 : }
3182 10344579 : *non_constant_p = true;
3183 10344579 : return t;
3184 : }
3185 :
3186 4880793 : if (!potential_constant_expression (new_call))
3187 : {
3188 861 : if (!*non_constant_p && !ctx->quiet)
3189 4 : error ("%q+E is not a constant expression", new_call);
3190 861 : *non_constant_p = true;
3191 861 : return t;
3192 : }
3193 :
3194 4879932 : if (strret)
3195 : {
3196 : /* memchr returns a pointer into the first argument, but we replaced the
3197 : argument above with a STRING_CST; put it back it now. */
3198 18 : tree op = CALL_EXPR_ARG (t, strret-1);
3199 18 : STRIP_NOPS (new_call);
3200 18 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
3201 15 : TREE_OPERAND (new_call, 0) = op;
3202 3 : else if (TREE_CODE (new_call) == ADDR_EXPR)
3203 4879932 : new_call = op;
3204 : }
3205 :
3206 4879932 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
3207 : non_constant_p, overflow_p,
3208 4879932 : jump_target);
3209 : }
3210 :
3211 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
3212 : the type of the value to match. */
3213 :
3214 : static tree
3215 136485679 : adjust_temp_type (tree type, tree temp)
3216 : {
3217 136485679 : if (same_type_p (TREE_TYPE (temp), type))
3218 : return temp;
3219 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
3220 62867643 : if (TREE_CODE (temp) == CONSTRUCTOR)
3221 : {
3222 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
3223 3791565 : tree t = copy_node (temp);
3224 3791565 : TREE_TYPE (t) = type;
3225 3791565 : return t;
3226 : }
3227 59076078 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
3228 0 : return build0 (EMPTY_CLASS_EXPR, type);
3229 59076078 : gcc_assert (scalarish_type_p (type));
3230 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
3231 : type is cv-unqualified. */
3232 59076078 : return cp_fold_convert (cv_unqualified (type), temp);
3233 : }
3234 :
3235 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
3236 : sub-CONSTRUCTORs. Otherwise return T.
3237 :
3238 : We use this whenever we initialize an object as a whole, whether it's a
3239 : parameter, a local variable, or a subobject, so that subsequent
3240 : modifications don't affect other places where it was used. */
3241 :
3242 : tree
3243 123706865 : unshare_constructor (tree t MEM_STAT_DECL)
3244 : {
3245 123706865 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
3246 : return t;
3247 18590214 : auto_vec <tree*, 4> ptrs;
3248 18590214 : ptrs.safe_push (&t);
3249 18590214 : while (!ptrs.is_empty ())
3250 : {
3251 24475287 : tree *p = ptrs.pop ();
3252 24475287 : tree n = copy_node (*p PASS_MEM_STAT);
3253 40630977 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
3254 24475287 : *p = n;
3255 24475287 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
3256 24475287 : constructor_elt *ce;
3257 94053689 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
3258 : {
3259 26512901 : if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3260 784 : ce->index = copy_node (ce->index PASS_MEM_STAT);
3261 26512901 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
3262 5885073 : ptrs.safe_push (&ce->value);
3263 : }
3264 : }
3265 18590214 : return t;
3266 18590214 : }
3267 :
3268 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
3269 :
3270 : static void
3271 872458 : free_constructor (tree t)
3272 : {
3273 872458 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
3274 0 : return;
3275 872458 : releasing_vec ctors;
3276 872458 : vec_safe_push (ctors, t);
3277 2656600 : while (!ctors->is_empty ())
3278 : {
3279 911684 : tree c = ctors->pop ();
3280 911684 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
3281 : {
3282 : constructor_elt *ce;
3283 393001 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3284 254492 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
3285 39226 : vec_safe_push (ctors, ce->value);
3286 138509 : ggc_free (elts);
3287 : }
3288 911684 : ggc_free (c);
3289 : }
3290 872458 : }
3291 :
3292 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
3293 : if *TP is address of a static variable (or part of it) currently being
3294 : constructed or of a heap artificial variable. */
3295 :
3296 : static tree
3297 48866226 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
3298 : {
3299 48866226 : if (TREE_CODE (*tp) == ADDR_EXPR)
3300 9065437 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
3301 9065437 : if (VAR_P (var) && TREE_STATIC (var))
3302 : {
3303 5674797 : if (heap_var_p (var))
3304 : return var;
3305 :
3306 5674797 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
3307 5674797 : if (global->get_value (var))
3308 : return var;
3309 : }
3310 48423860 : if (TYPE_P (*tp))
3311 2503 : *walk_subtrees = false;
3312 : return NULL_TREE;
3313 : }
3314 :
3315 : /* Subroutine of cxx_eval_call_expression.
3316 : We are processing a call expression (either CALL_EXPR or
3317 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
3318 : all arguments and bind their values to correspondings
3319 : parameters, making up the NEW_CALL context. */
3320 :
3321 : static tree
3322 183482341 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
3323 : tree orig_fun, bool *non_constant_p,
3324 : bool *overflow_p, bool *non_constant_args,
3325 : tree *jump_target)
3326 : {
3327 183482341 : int nargs = call_expr_nargs (t);
3328 183482341 : tree parms = DECL_ARGUMENTS (fun);
3329 183482341 : int i, j = 0;
3330 183482341 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
3331 1523 : ++nargs;
3332 183482341 : if (DECL_HAS_VTT_PARM_P (fun)
3333 1525 : && fun != orig_fun
3334 183483864 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
3335 1082 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
3336 463 : ++nargs;
3337 : /* We don't record ellipsis args below. */
3338 183482341 : int nparms = list_length (parms);
3339 183482341 : int nbinds = nargs < nparms ? nargs : nparms;
3340 183482341 : tree binds = make_tree_vec (nbinds);
3341 :
3342 : /* The call is not a constant expression if it involves the cdtor for a type
3343 : with virtual bases before C++26. */
3344 183482341 : if (cxx_dialect < cxx26
3345 183482341 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
3346 : {
3347 17 : if (!ctx->quiet)
3348 : {
3349 3 : error_at (cp_expr_loc_or_input_loc (t),
3350 : "call to non-%<constexpr%> function %qD", fun);
3351 3 : explain_invalid_constexpr_fn (fun);
3352 : }
3353 17 : *non_constant_p = true;
3354 17 : return binds;
3355 : }
3356 :
3357 330781764 : for (i = 0; i < nargs; ++i)
3358 : {
3359 220838289 : tree x, arg;
3360 220838289 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
3361 220838289 : if (parms && DECL_BY_REFERENCE (parms))
3362 27391 : type = TREE_TYPE (type);
3363 220838289 : if (i == 1
3364 220838289 : && j == 0
3365 47431136 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
3366 220839680 : && orig_fun != fun)
3367 : {
3368 1391 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
3369 1391 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
3370 428 : x = boolean_true_node;
3371 : else
3372 963 : x = boolean_false_node;
3373 : j = -1;
3374 : }
3375 220836898 : else if (i == 2
3376 220836898 : && j == -1
3377 1391 : && DECL_HAS_VTT_PARM_P (fun)
3378 1391 : && orig_fun != fun
3379 220838289 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
3380 985 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
3381 : {
3382 428 : x = build_zero_cst (type);
3383 428 : j = -2;
3384 : }
3385 : else
3386 220836470 : x = get_nth_callarg (t, i + j);
3387 : /* For member function, the first argument is a pointer to the implied
3388 : object. For a constructor, it might still be a dummy object, in
3389 : which case we get the real argument from ctx. */
3390 323475514 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
3391 243645177 : && is_dummy_object (x))
3392 : {
3393 11691674 : x = ctx->object;
3394 11691674 : x = build_address (x);
3395 : }
3396 220838289 : if (TREE_ADDRESSABLE (type))
3397 : {
3398 : /* Undo convert_for_arg_passing work here. */
3399 39453 : x = convert_from_reference (x);
3400 39453 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
3401 : non_constant_p, overflow_p,
3402 : jump_target);
3403 : }
3404 : else
3405 : /* Normally we would strip a TARGET_EXPR in an initialization context
3406 : such as this, but here we do the elision differently: we keep the
3407 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
3408 220798836 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
3409 : non_constant_p, overflow_p,
3410 : jump_target);
3411 220838289 : if (*jump_target)
3412 : break;
3413 : /* Check we aren't dereferencing a null pointer when calling a non-static
3414 : member function, which is undefined behaviour. */
3415 161737702 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
3416 91974375 : && integer_zerop (arg)
3417 : /* But ignore calls from within compiler-generated code, to handle
3418 : cases like lambda function pointer conversion operator thunks
3419 : which pass NULL as the 'this' pointer. */
3420 221012214 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
3421 : {
3422 527 : if (!ctx->quiet)
3423 6 : error_at (cp_expr_loc_or_input_loc (x),
3424 : "dereferencing a null pointer");
3425 527 : *non_constant_p = true;
3426 : }
3427 : /* Don't VERIFY_CONSTANT here. */
3428 220838205 : if (*non_constant_p && ctx->quiet)
3429 : break;
3430 : /* Just discard ellipsis args after checking their constantitude. */
3431 147299440 : if (!parms)
3432 400 : continue;
3433 :
3434 147299040 : if (!*non_constant_p)
3435 : {
3436 : /* Make sure the binding has the same type as the parm. But
3437 : only for constant args. */
3438 147298258 : if (TREE_ADDRESSABLE (type))
3439 : {
3440 26288 : if (!same_type_p (type, TREE_TYPE (arg)))
3441 : {
3442 9 : arg = build_fold_addr_expr (arg);
3443 9 : arg = cp_fold_convert (build_reference_type (type), arg);
3444 9 : arg = convert_from_reference (arg);
3445 : }
3446 : }
3447 147271970 : else if (!TYPE_REF_P (type))
3448 105190832 : arg = adjust_temp_type (type, arg);
3449 147298258 : if (!TREE_CONSTANT (arg))
3450 96970130 : *non_constant_args = true;
3451 50328128 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3452 : /* The destructor needs to see any modifications the callee makes
3453 : to the argument. */
3454 0 : *non_constant_args = true;
3455 : /* If arg is or contains address of a heap artificial variable or
3456 : of a static variable being constructed, avoid caching the
3457 : function call, as those variables might be modified by the
3458 : function, or might be modified by the callers in between
3459 : the cached function and just read by the function. */
3460 50328128 : else if (!*non_constant_args
3461 50328128 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
3462 : NULL))
3463 442366 : *non_constant_args = true;
3464 :
3465 : /* For virtual calls, adjust the this argument, so that it is
3466 : the object on which the method is called, rather than
3467 : one of its bases. */
3468 147298258 : if (i == 0 && DECL_VIRTUAL_P (fun))
3469 : {
3470 27569 : tree addr = arg;
3471 27569 : STRIP_NOPS (addr);
3472 27569 : if (TREE_CODE (addr) == ADDR_EXPR)
3473 : {
3474 27532 : tree obj = TREE_OPERAND (addr, 0);
3475 27532 : while (TREE_CODE (obj) == COMPONENT_REF
3476 11871 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
3477 39481 : && !same_type_ignoring_top_level_qualifiers_p
3478 11778 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
3479 171 : obj = TREE_OPERAND (obj, 0);
3480 27532 : if (obj != TREE_OPERAND (addr, 0))
3481 168 : arg = build_fold_addr_expr_with_type (obj,
3482 : TREE_TYPE (arg));
3483 : }
3484 : }
3485 147298258 : TREE_VEC_ELT (binds, i) = arg;
3486 : }
3487 147299040 : parms = TREE_CHAIN (parms);
3488 : }
3489 :
3490 : return binds;
3491 : }
3492 :
3493 : static int
3494 109356930 : push_cx_call_context (tree call)
3495 : {
3496 109356930 : ++call_stack_tick;
3497 109356930 : if (!EXPR_HAS_LOCATION (call))
3498 75397 : SET_EXPR_LOCATION (call, input_location);
3499 109356930 : call_stack.safe_push (call);
3500 109356930 : int len = call_stack.length ();
3501 109356930 : if (len > max_constexpr_depth)
3502 33 : return false;
3503 : return len;
3504 : }
3505 :
3506 : static void
3507 109356926 : pop_cx_call_context (void)
3508 : {
3509 109356926 : ++call_stack_tick;
3510 109356926 : call_stack.pop ();
3511 109356926 : }
3512 :
3513 : vec<tree>
3514 264295 : cx_error_context (void)
3515 : {
3516 264295 : vec<tree> r = vNULL;
3517 264295 : if (call_stack_tick != last_cx_error_tick
3518 264295 : && !call_stack.is_empty ())
3519 : r = call_stack;
3520 264295 : last_cx_error_tick = call_stack_tick;
3521 264295 : return r;
3522 : }
3523 :
3524 : /* E is an operand of a failed assertion, fold it either with or without
3525 : constexpr context. */
3526 :
3527 : static tree
3528 829 : fold_operand (tree e, const constexpr_ctx *ctx)
3529 : {
3530 829 : if (ctx)
3531 : {
3532 32 : bool new_non_constant_p = false, new_overflow_p = false;
3533 32 : tree jmp_target = NULL_TREE;
3534 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3535 : &new_non_constant_p,
3536 : &new_overflow_p, &jmp_target);
3537 : }
3538 : else
3539 797 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3540 829 : return e;
3541 : }
3542 :
3543 : /* If we have a condition in conjunctive normal form (CNF), find the first
3544 : failing clause. In other words, given an expression like
3545 :
3546 : true && true && false && true && false
3547 :
3548 : return the first 'false'. EXPR is the expression. */
3549 :
3550 : static tree
3551 332 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3552 : {
3553 437 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3554 : {
3555 : /* First check the left side... */
3556 192 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3557 192 : if (e == NULL_TREE)
3558 : /* ...if we didn't find a false clause, check the right side. */
3559 105 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3560 : return e;
3561 : }
3562 245 : tree e = contextual_conv_bool (expr, tf_none);
3563 245 : e = fold_operand (e, ctx);
3564 245 : if (integer_zerop (e))
3565 : /* This is the failing clause. */
3566 140 : return expr;
3567 : return NULL_TREE;
3568 : }
3569 :
3570 : /* Wrapper for find_failing_clause_r. */
3571 :
3572 : tree
3573 1986 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3574 : {
3575 1986 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3576 140 : if (tree e = find_failing_clause_r (ctx, expr))
3577 1986 : expr = e;
3578 1986 : return expr;
3579 : }
3580 :
3581 : /* Emit additional diagnostics for failing condition BAD.
3582 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3583 : If SHOW_EXPR_P is true, print the condition (because it was
3584 : instantiation-dependent). */
3585 :
3586 : void
3587 2010 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3588 : const constexpr_ctx *ctx /* = nullptr */)
3589 : {
3590 : /* Nobody wants to see the artificial (bool) cast. */
3591 2010 : bad = tree_strip_nop_conversions (bad);
3592 2010 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3593 3 : bad = TREE_OPERAND (bad, 0);
3594 :
3595 2010 : auto_diagnostic_nesting_level sentinel;
3596 :
3597 : /* Actually explain the failure if this is a concept check or a
3598 : requires-expression. */
3599 2010 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3600 334 : diagnose_constraints (cloc, bad, NULL_TREE);
3601 : /* Similarly if this is a standard trait. */
3602 1676 : else if (maybe_diagnose_standard_trait (cloc, bad))
3603 : ;
3604 1321 : else if (COMPARISON_CLASS_P (bad)
3605 1321 : && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))
3606 76 : || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))))
3607 : {
3608 292 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3609 292 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3610 292 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3611 292 : inform (cloc, "the comparison reduces to %qE", cond);
3612 : }
3613 1029 : else if (show_expr_p)
3614 779 : inform (cloc, "%qE evaluates to false", bad);
3615 2010 : }
3616 :
3617 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3618 : do it without changing the current evaluation state. If it evaluates to
3619 : false, complain and return false; otherwise, return true. */
3620 :
3621 : static bool
3622 173602 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3623 : location_t loc, bool evaluated,
3624 : bool *non_constant_p, bool *overflow_p)
3625 : {
3626 173602 : if (*non_constant_p)
3627 : return true;
3628 :
3629 173602 : tree eval, jmp_target = NULL_TREE;
3630 173602 : if (!evaluated)
3631 : {
3632 173602 : if (!potential_rvalue_constant_expression (arg))
3633 16 : return true;
3634 :
3635 173586 : constexpr_ctx new_ctx = *ctx;
3636 173586 : new_ctx.quiet = true;
3637 173586 : bool new_non_constant_p = false, new_overflow_p = false;
3638 : /* Avoid modification of existing values. */
3639 173586 : modifiable_tracker ms (new_ctx.global);
3640 173586 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3641 : &new_non_constant_p,
3642 : &new_overflow_p, &jmp_target);
3643 173586 : }
3644 : else
3645 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3646 : non_constant_p,
3647 : overflow_p, &jmp_target);
3648 173586 : if (jmp_target)
3649 : return true;
3650 :
3651 173586 : if (!*non_constant_p && integer_zerop (eval))
3652 : {
3653 42 : if (!ctx->quiet)
3654 : {
3655 : /* See if we can find which clause was failing
3656 : (for logical AND). */
3657 13 : tree bad = find_failing_clause (ctx, arg);
3658 : /* If not, or its location is unusable, fall back to the
3659 : previous location. */
3660 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3661 :
3662 : /* Report the error. */
3663 13 : auto_diagnostic_group d;
3664 13 : error_at (cloc, msg);
3665 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3666 13 : return bad;
3667 13 : }
3668 29 : *non_constant_p = true;
3669 29 : return false;
3670 : }
3671 :
3672 : return true;
3673 : }
3674 :
3675 : /* Evaluate a call T to a GCC internal function when possible and return
3676 : the evaluated result or, under the control of CTX, give an error, set
3677 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3678 :
3679 : static tree
3680 385288 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3681 : value_cat lval,
3682 : bool *non_constant_p, bool *overflow_p,
3683 : tree *jump_target)
3684 : {
3685 385288 : enum tree_code opcode = ERROR_MARK;
3686 :
3687 385288 : switch (CALL_EXPR_IFN (t))
3688 : {
3689 166 : case IFN_UBSAN_NULL:
3690 166 : case IFN_UBSAN_BOUNDS:
3691 166 : case IFN_UBSAN_VPTR:
3692 166 : case IFN_FALLTHROUGH:
3693 166 : return void_node;
3694 :
3695 173602 : case IFN_ASSUME:
3696 173602 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3697 : G_("failed %<assume%> attribute assumption"),
3698 173602 : EXPR_LOCATION (t), /*eval*/false,
3699 : non_constant_p, overflow_p))
3700 : return t;
3701 173573 : return void_node;
3702 :
3703 : case IFN_ADD_OVERFLOW:
3704 : opcode = PLUS_EXPR;
3705 : break;
3706 385 : case IFN_SUB_OVERFLOW:
3707 385 : opcode = MINUS_EXPR;
3708 385 : break;
3709 201747 : case IFN_MUL_OVERFLOW:
3710 201747 : opcode = MULT_EXPR;
3711 201747 : break;
3712 :
3713 96 : case IFN_LAUNDER:
3714 96 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3715 : vc_prvalue, non_constant_p,
3716 96 : overflow_p, jump_target);
3717 :
3718 0 : case IFN_DEFERRED_INIT:
3719 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3720 :
3721 176 : case IFN_BSWAP:
3722 176 : case IFN_BITREVERSE:
3723 176 : {
3724 176 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3725 : vc_prvalue, non_constant_p,
3726 : overflow_p, jump_target);
3727 176 : if (*jump_target)
3728 : return NULL_TREE;
3729 176 : if (*non_constant_p)
3730 : return t;
3731 104 : location_t loc = cp_expr_loc_or_input_loc (t);
3732 104 : if (TREE_CODE (arg) != INTEGER_CST
3733 104 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg))
3734 104 : || !TYPE_UNSIGNED (TREE_TYPE (arg))
3735 208 : || (CALL_EXPR_IFN (t) == IFN_BSWAP
3736 30 : && (TYPE_PRECISION (TREE_TYPE (arg)) % 8) != 0))
3737 : {
3738 0 : if (!ctx->quiet)
3739 0 : error_at (loc, "call to internal function %qE", t);
3740 0 : *non_constant_p = true;
3741 0 : return t;
3742 : }
3743 104 : return fold_build_builtin_bswapg_bitreverseg (loc, CALL_EXPR_IFN (t),
3744 104 : arg);
3745 : }
3746 :
3747 344 : case IFN_CLMUL:
3748 344 : {
3749 344 : tree args[3] = {};
3750 616 : for (int i = 0; i < call_expr_nargs (t); ++i)
3751 : {
3752 510 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
3753 : vc_prvalue, non_constant_p,
3754 : overflow_p, jump_target);
3755 510 : if (*jump_target)
3756 : return NULL_TREE;
3757 510 : if (*non_constant_p)
3758 : return t;
3759 : }
3760 106 : location_t loc = cp_expr_loc_or_input_loc (t);
3761 106 : if (TREE_CODE (args[0]) != INTEGER_CST
3762 106 : || TREE_CODE (args[1]) != INTEGER_CST)
3763 : {
3764 0 : if (!ctx->quiet)
3765 0 : error_at (loc, "call to internal function %qE", t);
3766 0 : *non_constant_p = true;
3767 0 : return t;
3768 : }
3769 106 : unsigned int prec = TYPE_PRECISION (TREE_TYPE (args[0]));
3770 106 : wide_int res = wi::clmul (wi::to_wide (args[0]),
3771 106 : wi::to_wide (args[1]),
3772 298 : prec * (args[2] ? 2 : 1));
3773 106 : if (args[2] == NULL_TREE)
3774 86 : return wide_int_to_tree (TREE_TYPE (t), res);
3775 20 : wide_int lo = wide_int::from (res, prec, UNSIGNED);
3776 20 : wide_int hi = wide_int::from (wi::rshift (res, prec, UNSIGNED),
3777 40 : prec, UNSIGNED);
3778 40 : tree mod = build2_loc (loc, MODIFY_EXPR, void_type_node,
3779 : build_fold_indirect_ref_loc (loc, args[2]),
3780 20 : wide_int_to_tree (TREE_TYPE (t), hi));
3781 20 : cxx_eval_constant_expression (ctx, mod, vc_prvalue, non_constant_p,
3782 : overflow_p, jump_target);
3783 20 : if (*jump_target)
3784 : return NULL_TREE;
3785 20 : if (*non_constant_p)
3786 : return t;
3787 20 : return wide_int_to_tree (TREE_TYPE (t), lo);
3788 126 : }
3789 :
3790 1638 : case IFN_BITCOMPRESS:
3791 1638 : case IFN_BITEXPAND:
3792 1638 : {
3793 1638 : tree args[2] = {};
3794 4722 : for (int i = 0; i < call_expr_nargs (t); ++i)
3795 : {
3796 3180 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
3797 : vc_prvalue, non_constant_p,
3798 : overflow_p, jump_target);
3799 3180 : if (*jump_target)
3800 : return NULL_TREE;
3801 3180 : if (*non_constant_p)
3802 : return t;
3803 : }
3804 1542 : location_t loc = cp_expr_loc_or_input_loc (t);
3805 1542 : if (TREE_CODE (args[0]) != INTEGER_CST
3806 1542 : || TREE_CODE (args[1]) != INTEGER_CST)
3807 : {
3808 0 : if (!ctx->quiet)
3809 0 : error_at (loc, "call to internal function %qE", t);
3810 0 : *non_constant_p = true;
3811 0 : return t;
3812 : }
3813 1542 : return fold_build_builtin_bitcompressg_bitexpandg (loc,
3814 1542 : CALL_EXPR_IFN (t),
3815 1542 : args[0], args[1]);
3816 : }
3817 :
3818 6634 : case IFN_VEC_CONVERT:
3819 6634 : {
3820 6634 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3821 : vc_prvalue, non_constant_p,
3822 : overflow_p, jump_target);
3823 6634 : if (*jump_target)
3824 : return NULL_TREE;
3825 6634 : if (TREE_CODE (arg) == VECTOR_CST)
3826 6237 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3827 : return r;
3828 : }
3829 : /* FALLTHRU */
3830 :
3831 402 : default:
3832 402 : if (!ctx->quiet)
3833 0 : error_at (cp_expr_loc_or_input_loc (t),
3834 : "call to internal function %qE", t);
3835 402 : *non_constant_p = true;
3836 402 : return t;
3837 : }
3838 :
3839 : /* Evaluate constant arguments using OPCODE and return a complex
3840 : number containing the result and the overflow bit. */
3841 202632 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3842 : non_constant_p, overflow_p,
3843 : jump_target);
3844 202632 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3845 : non_constant_p, overflow_p,
3846 : jump_target);
3847 202632 : if (*jump_target)
3848 : return NULL_TREE;
3849 202632 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3850 : {
3851 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3852 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3853 2 : tree result = fold_binary_loc (loc, opcode, type,
3854 : fold_convert_loc (loc, type, arg0),
3855 : fold_convert_loc (loc, type, arg1));
3856 2 : tree ovf
3857 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3858 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3859 2 : if (TREE_OVERFLOW (result))
3860 0 : TREE_OVERFLOW (result) = 0;
3861 :
3862 2 : return build_complex (TREE_TYPE (t), result, ovf);
3863 : }
3864 :
3865 202630 : *non_constant_p = true;
3866 202630 : return t;
3867 : }
3868 :
3869 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3870 :
3871 : static void
3872 5976015 : clear_no_implicit_zero (tree ctor)
3873 : {
3874 5976015 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3875 : {
3876 1748275 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3877 7400086 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3878 2324255 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3879 729892 : clear_no_implicit_zero (e.value);
3880 : }
3881 5976015 : }
3882 :
3883 : /* Complain about a const object OBJ being modified in a constant expression.
3884 : EXPR is the MODIFY_EXPR expression performing the modification. */
3885 :
3886 : static void
3887 65 : modifying_const_object_error (tree expr, tree obj)
3888 : {
3889 65 : location_t loc = cp_expr_loc_or_input_loc (expr);
3890 65 : auto_diagnostic_group d;
3891 130 : error_at (loc, "modifying a const object %qE is not allowed in "
3892 65 : "a constant expression", TREE_OPERAND (expr, 0));
3893 :
3894 : /* Find the underlying object that was declared as const. */
3895 65 : location_t decl_loc = UNKNOWN_LOCATION;
3896 207 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3897 77 : switch (TREE_CODE (probe))
3898 : {
3899 39 : case BIT_FIELD_REF:
3900 39 : case COMPONENT_REF:
3901 39 : {
3902 39 : tree elt = TREE_OPERAND (probe, 1);
3903 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3904 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3905 39 : probe = TREE_OPERAND (probe, 0);
3906 : }
3907 39 : break;
3908 :
3909 0 : case ARRAY_REF:
3910 0 : case REALPART_EXPR:
3911 0 : case IMAGPART_EXPR:
3912 0 : probe = TREE_OPERAND (probe, 0);
3913 0 : break;
3914 :
3915 38 : default:
3916 38 : decl_loc = location_of (probe);
3917 38 : break;
3918 : }
3919 65 : inform (decl_loc, "originally declared %<const%> here");
3920 65 : }
3921 :
3922 : /* Return true if FNDECL is a replaceable global allocation function that
3923 : should be usable during constant expression evaluation. */
3924 :
3925 : static inline bool
3926 38271813 : cxx_replaceable_global_alloc_fn (tree fndecl)
3927 : {
3928 38271813 : return (cxx_dialect >= cxx20
3929 36209185 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3930 2192109 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3931 40463525 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3932 906203 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3933 : }
3934 :
3935 : /* Return true if FNDECL is a placement new function that should be
3936 : usable during constant expression evaluation of std::construct_at. */
3937 :
3938 : static inline bool
3939 36819882 : cxx_placement_new_fn (tree fndecl)
3940 : {
3941 36819882 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3942 : }
3943 :
3944 : /* Return true if FNDECL is std::construct_at. */
3945 :
3946 : static inline bool
3947 2329860 : is_std_construct_at (tree fndecl)
3948 : {
3949 2329860 : if (!decl_in_std_namespace_p (fndecl))
3950 : return false;
3951 :
3952 2286496 : tree name = DECL_NAME (fndecl);
3953 2286496 : return name && id_equal (name, "construct_at");
3954 : }
3955 :
3956 : /* Overload for the above taking constexpr_call*. */
3957 :
3958 : static inline bool
3959 2094942 : is_std_construct_at (const constexpr_call *call)
3960 : {
3961 2094942 : return (call
3962 1938241 : && call->fundef
3963 4033183 : && is_std_construct_at (call->fundef->decl));
3964 : }
3965 :
3966 : /* True if CTX is an instance of std::NAME class. */
3967 :
3968 : bool
3969 12195931 : is_std_class (tree ctx, const char *name)
3970 : {
3971 12195931 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3972 : return false;
3973 :
3974 12195309 : tree decl = TYPE_MAIN_DECL (ctx);
3975 12195309 : tree dname = DECL_NAME (decl);
3976 12195309 : if (dname == NULL_TREE || !id_equal (dname, name))
3977 : return false;
3978 :
3979 864374 : return decl_in_std_namespace_p (decl);
3980 : }
3981 :
3982 : /* True if CTX is an instance of std::allocator. */
3983 :
3984 : bool
3985 703375 : is_std_allocator (tree ctx)
3986 : {
3987 703375 : return is_std_class (ctx, "allocator");
3988 : }
3989 :
3990 : /* Return true if FNDECL is std::allocator<T>::allocate. */
3991 :
3992 : static bool
3993 107152 : is_std_allocator_allocate (tree fndecl)
3994 : {
3995 107152 : tree name = DECL_NAME (fndecl);
3996 107152 : if (name == NULL_TREE || !id_equal (name, "allocate"))
3997 : return false;
3998 :
3999 98354 : return is_std_allocator (DECL_CONTEXT (fndecl));
4000 : }
4001 :
4002 : /* Overload for the above taking constexpr_call*. */
4003 :
4004 : static inline bool
4005 117539 : is_std_allocator_allocate (const constexpr_call *call)
4006 : {
4007 117539 : return (call
4008 107152 : && call->fundef
4009 224691 : && is_std_allocator_allocate (call->fundef->decl));
4010 : }
4011 :
4012 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
4013 :
4014 : static bool
4015 650552 : is_std_allocator_allocate_deallocate (tree fndecl)
4016 : {
4017 650552 : tree name = DECL_NAME (fndecl);
4018 650552 : if (name == NULL_TREE
4019 650552 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
4020 : return false;
4021 :
4022 603389 : return is_std_allocator (DECL_CONTEXT (fndecl));
4023 : }
4024 :
4025 : /* Overload for the above taking constexpr_call*. */
4026 :
4027 : static inline bool
4028 489900 : is_std_allocator_allocate_deallocate (const constexpr_call *call)
4029 : {
4030 489900 : return (call
4031 298210 : && call->fundef
4032 788110 : && is_std_allocator_allocate_deallocate (call->fundef->decl));
4033 : }
4034 :
4035 : /* Return true if FNDECL is std::source_location::current. */
4036 :
4037 : static inline bool
4038 58518 : is_std_source_location_current (tree fndecl)
4039 : {
4040 58518 : if (!decl_in_std_namespace_p (fndecl))
4041 : return false;
4042 :
4043 39331 : tree name = DECL_NAME (fndecl);
4044 39331 : if (name == NULL_TREE || !id_equal (name, "current"))
4045 : return false;
4046 :
4047 159 : tree ctx = DECL_CONTEXT (fndecl);
4048 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
4049 : return false;
4050 :
4051 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
4052 159 : return name && id_equal (name, "source_location");
4053 : }
4054 :
4055 : /* Overload for the above taking constexpr_call*. */
4056 :
4057 : static inline bool
4058 85220 : is_std_source_location_current (const constexpr_call *call)
4059 : {
4060 85220 : return (call
4061 58518 : && call->fundef
4062 143738 : && is_std_source_location_current (call->fundef->decl));
4063 : }
4064 :
4065 : /* Return true if FNDECL is __dynamic_cast. */
4066 :
4067 : static inline bool
4068 36518666 : cxx_dynamic_cast_fn_p (tree fndecl)
4069 : {
4070 36518666 : return (cxx_dialect >= cxx20
4071 34456033 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
4072 6788 : && CP_DECL_CONTEXT (fndecl) == abi_node
4073 : /* Only consider implementation-detail __dynamic_cast calls that
4074 : correspond to a dynamic_cast, and ignore direct calls to
4075 : abi::__dynamic_cast. */
4076 36525454 : && DECL_ARTIFICIAL (fndecl));
4077 : }
4078 :
4079 : /* Often, we have an expression in the form of address + offset, e.g.
4080 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
4081 :
4082 : static tree
4083 5840 : extract_obj_from_addr_offset (tree expr)
4084 : {
4085 5840 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
4086 2786 : expr = TREE_OPERAND (expr, 0);
4087 5840 : STRIP_NOPS (expr);
4088 5840 : if (TREE_CODE (expr) == ADDR_EXPR)
4089 5829 : expr = TREE_OPERAND (expr, 0);
4090 5840 : return expr;
4091 : }
4092 :
4093 : /* Given a PATH like
4094 :
4095 : g.D.2181.D.2154.D.2102.D.2093
4096 :
4097 : find a component with type TYPE. Return NULL_TREE if not found, and
4098 : error_mark_node if the component is not accessible. If STOP is non-null,
4099 : this function will return NULL_TREE if STOP is found before TYPE. */
4100 :
4101 : static tree
4102 2831 : get_component_with_type (tree path, tree type, tree stop)
4103 : {
4104 7921 : while (true)
4105 : {
4106 5376 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
4107 : /* Found it. */
4108 : return path;
4109 3761 : else if (stop
4110 3761 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
4111 : stop)))
4112 : return NULL_TREE;
4113 3716 : else if (TREE_CODE (path) == COMPONENT_REF
4114 3716 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
4115 : {
4116 : /* We need to check that the component we're accessing is in fact
4117 : accessible. */
4118 3716 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
4119 3716 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
4120 1171 : return error_mark_node;
4121 2545 : path = TREE_OPERAND (path, 0);
4122 : }
4123 : else
4124 : return NULL_TREE;
4125 : }
4126 : }
4127 :
4128 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
4129 :
4130 : The declaration of __dynamic_cast is:
4131 :
4132 : void* __dynamic_cast (const void* __src_ptr,
4133 : const __class_type_info* __src_type,
4134 : const __class_type_info* __dst_type,
4135 : ptrdiff_t __src2dst);
4136 :
4137 : where src2dst has the following possible values
4138 :
4139 : >-1: src_type is a unique public non-virtual base of dst_type
4140 : dst_ptr + src2dst == src_ptr
4141 : -1: unspecified relationship
4142 : -2: src_type is not a public base of dst_type
4143 : -3: src_type is a multiple public non-virtual base of dst_type */
4144 :
4145 : static tree
4146 3173 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
4147 : bool *non_constant_p, bool *overflow_p,
4148 : tree *jump_target)
4149 : {
4150 : /* T will be something like
4151 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
4152 : dismantle it. */
4153 3173 : gcc_assert (call_expr_nargs (call) == 4);
4154 3173 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
4155 3173 : tree obj = CALL_EXPR_ARG (call, 0);
4156 3173 : tree type = CALL_EXPR_ARG (call, 2);
4157 3173 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
4158 3173 : location_t loc = cp_expr_loc_or_input_loc (call);
4159 :
4160 : /* Get the target type of the dynamic_cast. */
4161 3173 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
4162 3173 : type = TREE_OPERAND (type, 0);
4163 3173 : type = TREE_TYPE (DECL_NAME (type));
4164 :
4165 : /* TYPE can only be either T* or T&. We can't know which of these it
4166 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
4167 : and something like "(T*)(T&)(T*) x" in the second case.
4168 : This is true for the reference cases in C++ < 26 or when exceptions
4169 : aren't enabled, in that case we should diagnose errors. For C++26
4170 : with exceptions we should silently evaluate to null pointer and
4171 : let the callers call __cxa_bad_cast () later to throw an exception. */
4172 3173 : bool fail_for_non_constant_p = false;
4173 12899 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
4174 : {
4175 9726 : if (cxx_dialect < cxx26 || !flag_exceptions)
4176 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
4177 9726 : obj = TREE_OPERAND (obj, 0);
4178 : }
4179 :
4180 : /* Evaluate the object so that we know its dynamic type. */
4181 3173 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
4182 : overflow_p, jump_target);
4183 3173 : if (*non_constant_p)
4184 : return call;
4185 3054 : if (*jump_target)
4186 : return NULL_TREE;
4187 :
4188 : /* For dynamic_cast from classes with virtual bases we can get something
4189 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
4190 : d.D.1234 using cxx_fold_indirect_ref. */
4191 3054 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
4192 : {
4193 1206 : tree objo = obj;
4194 1206 : STRIP_NOPS (objo);
4195 1206 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
4196 : {
4197 1152 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
4198 : obj, NULL, jump_target);
4199 1152 : if (objo)
4200 1152 : obj = build_fold_addr_expr (objo);
4201 : }
4202 : }
4203 :
4204 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
4205 : but when HINT is > 0, it can also be something like
4206 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
4207 3054 : obj = extract_obj_from_addr_offset (obj);
4208 3054 : const tree objtype = TREE_TYPE (obj);
4209 : /* If OBJ doesn't refer to a base field, we're done. */
4210 6029 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
4211 3054 : ? TREE_OPERAND (obj, 1) : obj))
4212 3054 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
4213 : {
4214 79 : if (fail_for_non_constant_p)
4215 : {
4216 55 : if (!ctx->quiet)
4217 : {
4218 2 : auto_diagnostic_group d;
4219 2 : error_at (loc, "reference %<dynamic_cast%> failed");
4220 2 : inform (loc, "dynamic type %qT of its operand does "
4221 : "not have a base class of type %qT",
4222 : objtype, type);
4223 2 : }
4224 55 : *non_constant_p = true;
4225 : }
4226 79 : return integer_zero_node;
4227 : }
4228 :
4229 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
4230 : or in a destructor ... if the operand of the dynamic_cast refers
4231 : to the object under construction or destruction, this object is
4232 : considered to be a most derived object that has the type of the
4233 : constructor or destructor's class. */
4234 2975 : tree vtable = build_vfield_ref (obj, objtype);
4235 2975 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
4236 : non_constant_p, overflow_p,
4237 : jump_target);
4238 2975 : if (*non_constant_p)
4239 : return call;
4240 2798 : if (*jump_target)
4241 : return NULL_TREE;
4242 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
4243 : so it's possible that we got a null pointer now. */
4244 2798 : if (integer_zerop (vtable))
4245 : {
4246 12 : if (!ctx->quiet)
4247 3 : error_at (loc, "virtual table pointer is used uninitialized");
4248 12 : *non_constant_p = true;
4249 12 : return integer_zero_node;
4250 : }
4251 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
4252 2786 : vtable = extract_obj_from_addr_offset (vtable);
4253 2786 : const tree mdtype = DECL_CONTEXT (vtable);
4254 :
4255 : /* Given dynamic_cast<T>(v),
4256 :
4257 : [expr.dynamic.cast] If C is the class type to which T points or refers,
4258 : the runtime check logically executes as follows:
4259 :
4260 : If, in the most derived object pointed (referred) to by v, v points
4261 : (refers) to a public base class subobject of a C object, and if only
4262 : one object of type C is derived from the subobject pointed (referred)
4263 : to by v the result points (refers) to that C object.
4264 :
4265 : In this case, HINT >= 0 or -3. */
4266 2786 : if (hint >= 0 || hint == -3)
4267 : {
4268 : /* Look for a component with type TYPE. */
4269 687 : tree t = get_component_with_type (obj, type, mdtype);
4270 : /* If not accessible, give an error. */
4271 687 : if (t == error_mark_node)
4272 : {
4273 198 : if (fail_for_non_constant_p)
4274 : {
4275 108 : if (!ctx->quiet)
4276 : {
4277 12 : auto_diagnostic_group d;
4278 12 : error_at (loc, "reference %<dynamic_cast%> failed");
4279 12 : inform (loc, "static type %qT of its operand is a "
4280 : "non-public base class of dynamic type %qT",
4281 : objtype, type);
4282 :
4283 12 : }
4284 108 : *non_constant_p = true;
4285 : }
4286 198 : return integer_zero_node;
4287 : }
4288 489 : else if (t)
4289 : /* The result points to the TYPE object. */
4290 444 : return cp_build_addr_expr (t, complain);
4291 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
4292 : Fall through to the normal processing. */
4293 : }
4294 :
4295 : /* Otherwise, if v points (refers) to a public base class subobject of the
4296 : most derived object, and the type of the most derived object has a base
4297 : class, of type C, that is unambiguous and public, the result points
4298 : (refers) to the C subobject of the most derived object.
4299 :
4300 : But it can also be an invalid case. */
4301 :
4302 : /* Get the most derived object. */
4303 2144 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
4304 2144 : if (obj == error_mark_node)
4305 : {
4306 973 : if (fail_for_non_constant_p)
4307 : {
4308 350 : if (!ctx->quiet)
4309 : {
4310 38 : auto_diagnostic_group d;
4311 38 : error_at (loc, "reference %<dynamic_cast%> failed");
4312 38 : inform (loc, "static type %qT of its operand is a non-public"
4313 : " base class of dynamic type %qT", objtype, mdtype);
4314 38 : }
4315 350 : *non_constant_p = true;
4316 : }
4317 973 : return integer_zero_node;
4318 : }
4319 : else
4320 1171 : gcc_assert (obj);
4321 :
4322 : /* Check that the type of the most derived object has a base class
4323 : of type TYPE that is unambiguous and public. */
4324 1171 : base_kind b_kind;
4325 1171 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
4326 1171 : if (!binfo || binfo == error_mark_node)
4327 : {
4328 441 : if (fail_for_non_constant_p)
4329 : {
4330 134 : if (!ctx->quiet)
4331 : {
4332 16 : auto_diagnostic_group d;
4333 16 : error_at (loc, "reference %<dynamic_cast%> failed");
4334 16 : if (b_kind == bk_ambig)
4335 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
4336 : "type %qT of its operand", type, mdtype);
4337 : else
4338 10 : inform (loc, "dynamic type %qT of its operand does not "
4339 : "have an unambiguous public base class %qT",
4340 : mdtype, type);
4341 16 : }
4342 134 : *non_constant_p = true;
4343 : }
4344 441 : return integer_zero_node;
4345 : }
4346 : /* If so, return the TYPE subobject of the most derived object. */
4347 730 : obj = convert_to_base_statically (obj, binfo);
4348 730 : return cp_build_addr_expr (obj, complain);
4349 : }
4350 :
4351 : /* Data structure used by replace_decl and replace_decl_r. */
4352 :
4353 : struct replace_decl_data
4354 : {
4355 : /* The _DECL we want to replace. */
4356 : tree decl;
4357 : /* The replacement for DECL. */
4358 : tree replacement;
4359 : /* Trees we've visited. */
4360 : hash_set<tree> *pset;
4361 : /* Whether we've performed any replacements. */
4362 : bool changed;
4363 : };
4364 :
4365 : /* Helper function for replace_decl, called through cp_walk_tree. */
4366 :
4367 : static tree
4368 17002207 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
4369 : {
4370 17002207 : replace_decl_data *d = (replace_decl_data *) data;
4371 :
4372 : /* We could be replacing
4373 : &<retval>.bar -> &foo.bar
4374 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
4375 : on the ADDR_EXPR around it. */
4376 17002207 : if (TREE_CODE (*tp) == ADDR_EXPR)
4377 : {
4378 1530623 : d->pset->add (*tp);
4379 1530623 : auto save_changed = d->changed;
4380 1530623 : d->changed = false;
4381 1530623 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
4382 1530623 : if (d->changed)
4383 : {
4384 1855 : cxx_mark_addressable (*tp);
4385 1855 : recompute_tree_invariant_for_addr_expr (*tp);
4386 : }
4387 : else
4388 1528768 : d->changed = save_changed;
4389 1530623 : *walk_subtrees = 0;
4390 : }
4391 15471584 : else if (*tp == d->decl)
4392 : {
4393 2095 : *tp = unshare_expr (d->replacement);
4394 2095 : d->changed = true;
4395 2095 : *walk_subtrees = 0;
4396 : }
4397 15469489 : else if (TYPE_P (*tp)
4398 15469489 : || d->pset->add (*tp))
4399 2818565 : *walk_subtrees = 0;
4400 :
4401 17002207 : return NULL_TREE;
4402 : }
4403 :
4404 : /* Replace every occurrence of DECL with (an unshared copy of)
4405 : REPLACEMENT within the expression *TP. Returns true iff a
4406 : replacement was performed. */
4407 :
4408 : bool
4409 2266663 : replace_decl (tree *tp, tree decl, tree replacement)
4410 : {
4411 2266663 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
4412 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
4413 2266663 : hash_set<tree> pset;
4414 2266663 : replace_decl_data data = { decl, replacement, &pset, false };
4415 2266663 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
4416 2266663 : return data.changed;
4417 2266663 : }
4418 :
4419 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
4420 :
4421 : static tree
4422 44 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
4423 : value_cat lval,
4424 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
4425 : {
4426 44 : tree function = THUNK_TARGET (thunk_fndecl);
4427 :
4428 44 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
4429 : {
4430 13 : if (!ctx->quiet)
4431 : {
4432 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
4433 : {
4434 0 : error ("call to non-%<constexpr%> function %qD", function);
4435 0 : explain_invalid_constexpr_fn (function);
4436 : }
4437 : else
4438 : /* virtual_offset is only set for virtual bases, which make the
4439 : class non-literal, so we don't need to handle it here. */
4440 3 : error ("calling constexpr member function %qD through virtual "
4441 : "base subobject", function);
4442 : }
4443 13 : *non_constant_p = true;
4444 13 : return t;
4445 : }
4446 :
4447 31 : tree new_call = copy_node (t);
4448 31 : CALL_EXPR_FN (new_call) = function;
4449 31 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
4450 :
4451 31 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
4452 :
4453 31 : if (DECL_THIS_THUNK_P (thunk_fndecl))
4454 : {
4455 : /* 'this'-adjusting thunk. */
4456 19 : tree this_arg = CALL_EXPR_ARG (t, 0);
4457 19 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
4458 : this_arg, offset);
4459 19 : CALL_EXPR_ARG (new_call, 0) = this_arg;
4460 : }
4461 : else
4462 : {
4463 : /* Return-adjusting thunk. As in expand_thunk, adjust a pointer only
4464 : if it is non-null. */
4465 12 : tree call = new_call;
4466 12 : if (TYPE_PTR_P (TREE_TYPE (call)))
4467 12 : call = save_expr (call);
4468 12 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (call), call, offset);
4469 12 : if (TYPE_PTR_P (TREE_TYPE (call)))
4470 12 : new_call = build_if_nonnull (call, new_call, tf_none);
4471 : }
4472 :
4473 31 : tree result = cxx_eval_constant_expression (ctx, new_call, lval,
4474 : non_constant_p, overflow_p,
4475 : jump_target);
4476 31 : if (!*non_constant_p
4477 27 : && !*overflow_p
4478 27 : && !*jump_target
4479 27 : && result != void_node
4480 27 : && INDIRECT_TYPE_P (TREE_TYPE (t))
4481 55 : && !(same_type_ignoring_top_level_qualifiers_p
4482 24 : (TREE_TYPE (result), TREE_TYPE (t))))
4483 15 : result = adjust_temp_type (TREE_TYPE (t), result);
4484 : return result;
4485 : }
4486 :
4487 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
4488 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
4489 : 'tors to detect modifying const objects in a constexpr context. */
4490 :
4491 : static void
4492 11049343 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
4493 : bool readonly_p, bool *non_constant_p,
4494 : bool *overflow_p, tree *jump_target)
4495 : {
4496 22098686 : if (CLASS_TYPE_P (TREE_TYPE (object))
4497 22098686 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
4498 : {
4499 : /* Subobjects might not be stored in ctx->global->values but we
4500 : can get its CONSTRUCTOR by evaluating *this. */
4501 1026281 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
4502 : non_constant_p, overflow_p,
4503 : jump_target);
4504 1026281 : if (!*non_constant_p
4505 12033384 : && !throws (jump_target)
4506 2010322 : && TREE_CODE (e) == CONSTRUCTOR)
4507 984041 : TREE_READONLY (e) = readonly_p;
4508 : }
4509 11049343 : }
4510 :
4511 : /* Allocate an exception for OBJECT and throw it. */
4512 :
4513 : tree
4514 3248 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
4515 : tree object)
4516 : {
4517 3248 : tree type = TREE_TYPE (object);
4518 : /* This simulates a call to __cxa_allocate_exception. We need
4519 : (struct exception *) &heap -- memory on the heap so that
4520 : it can survive the stack being unwound. */
4521 3248 : tree arr = build_array_of_n_type (type, 1);
4522 3248 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
4523 3248 : DECL_NAME (var) = heap_identifier;
4524 3248 : ctx->global->put_value (var, NULL_TREE);
4525 :
4526 : /* *(struct exception *) &heap = exc{ ... } */
4527 3248 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
4528 3248 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
4529 3248 : bool non_constant_p = false, overflow_p = false;
4530 3248 : tree jump_target = NULL_TREE;
4531 3248 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
4532 : &overflow_p, &jump_target);
4533 3248 : if (non_constant_p)
4534 : {
4535 0 : if (!ctx->quiet)
4536 0 : error_at (loc, "couldn%'t throw %qT", type);
4537 : return NULL_TREE;
4538 : }
4539 :
4540 : /* Now we can __cxa_throw. */
4541 3248 : DECL_EXCEPTION_REFCOUNT (var)
4542 3248 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
4543 3248 : ++ctx->global->uncaught_exceptions;
4544 :
4545 3248 : return var;
4546 : }
4547 :
4548 : /* Subroutine of cxx_eval_constant_expression.
4549 : Evaluate the call expression tree T in the context of OLD_CALL expression
4550 : evaluation. */
4551 :
4552 : static tree
4553 205683103 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
4554 : value_cat lval,
4555 : bool *non_constant_p, bool *overflow_p,
4556 : tree *jump_target)
4557 : {
4558 205683103 : location_t loc = cp_expr_loc_or_input_loc (t);
4559 205683103 : tree fun = get_function_named_in_call (t);
4560 :
4561 205683103 : if (fun == NULL_TREE)
4562 385288 : return cxx_eval_internal_function (ctx, t, lval,
4563 : non_constant_p, overflow_p,
4564 385288 : jump_target);
4565 :
4566 205297815 : if (TREE_CODE (fun) != FUNCTION_DECL)
4567 : {
4568 : /* Might be a constexpr function pointer. */
4569 410237 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
4570 : non_constant_p, overflow_p,
4571 : jump_target);
4572 410237 : if (*jump_target)
4573 : return NULL_TREE;
4574 410237 : STRIP_NOPS (fun);
4575 410237 : if (TREE_CODE (fun) == ADDR_EXPR)
4576 30606 : fun = TREE_OPERAND (fun, 0);
4577 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
4578 : indirection, the called expression is a pointer into the
4579 : virtual table which should contain FDESC_EXPR. Extract the
4580 : FUNCTION_DECL from there. */
4581 : else if (TARGET_VTABLE_USES_DESCRIPTORS
4582 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
4583 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
4584 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
4585 : {
4586 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
4587 : if (VAR_P (d)
4588 : && DECL_VTABLE_OR_VTT_P (d)
4589 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
4590 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
4591 : && DECL_INITIAL (d)
4592 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
4593 : {
4594 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
4595 : TYPE_SIZE_UNIT (vtable_entry_type));
4596 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
4597 : if (idx >= 0)
4598 : {
4599 : tree fdesc
4600 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
4601 : if (TREE_CODE (fdesc) == FDESC_EXPR
4602 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
4603 : fun = TREE_OPERAND (fdesc, 0);
4604 : }
4605 : }
4606 : }
4607 : }
4608 205297815 : if (TREE_CODE (fun) != FUNCTION_DECL)
4609 : {
4610 379631 : if (!ctx->quiet && !*non_constant_p)
4611 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
4612 : "function", fun);
4613 379631 : *non_constant_p = true;
4614 379631 : return t;
4615 : }
4616 204918184 : tree orig_fun = fun;
4617 204918184 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
4618 24274077 : fun = DECL_CLONED_FUNCTION (fun);
4619 :
4620 204918184 : if (is_ubsan_builtin_p (fun))
4621 58 : return void_node;
4622 :
4623 204918126 : if (fndecl_built_in_p (fun))
4624 20504732 : return cxx_eval_builtin_function_call (ctx, t, fun,
4625 : lval, non_constant_p, overflow_p,
4626 20504730 : jump_target);
4627 184413394 : if (DECL_THUNK_P (fun))
4628 44 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
4629 44 : jump_target);
4630 184413350 : if (metafunction_p (fun))
4631 : {
4632 : /* To be able to evaluate a metafunction, we may have to instantiate
4633 : constexpr functions. If we're not allowed to instantiate, leave
4634 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4635 : otherwise we might fold those prematurely. See
4636 : g++.dg/reflect/p2996-17.C. */
4637 82979 : if (uid_sensitive_constexpr_evaluation_p ()
4638 82407 : || ctx->manifestly_const_eval == mce_unknown)
4639 : {
4640 21807 : *non_constant_p = true;
4641 21807 : return t;
4642 : }
4643 61172 : ctx->global->state_dependent = true;
4644 61172 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4645 : jump_target);
4646 61172 : if (*jump_target)
4647 : return NULL_TREE;
4648 57912 : if (*non_constant_p)
4649 : return t;
4650 56810 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4651 : non_constant_p, overflow_p,
4652 : jump_target);
4653 56810 : if (*jump_target)
4654 : return NULL_TREE;
4655 56810 : if (*non_constant_p)
4656 : return t;
4657 56806 : return e;
4658 : }
4659 184330371 : bool non_constexpr_call = false;
4660 184330371 : if (!maybe_constexpr_fn (fun))
4661 : {
4662 889380 : if (TREE_CODE (t) == CALL_EXPR
4663 881675 : && cxx_replaceable_global_alloc_fn (fun)
4664 1541182 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4665 323907 : || is_std_allocator_allocate_deallocate (ctx->call)))
4666 : {
4667 486814 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4668 486814 : const int nargs = call_expr_nargs (t);
4669 486814 : tree arg0 = NULL_TREE;
4670 774222 : for (int i = 0; i < nargs; ++i)
4671 : {
4672 487883 : tree arg = CALL_EXPR_ARG (t, i);
4673 487883 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4674 : non_constant_p, overflow_p,
4675 : jump_target);
4676 487883 : if (*jump_target)
4677 : return NULL_TREE;
4678 : /* Deleting a non-constant pointer has a better error message
4679 : below. */
4680 487873 : if (new_op_p || i != 0)
4681 408742 : VERIFY_CONSTANT (arg);
4682 208277 : if (i == 0)
4683 : arg0 = arg;
4684 : }
4685 286339 : gcc_assert (arg0);
4686 286339 : if (new_op_p)
4687 : {
4688 207208 : if (!tree_fits_uhwi_p (arg0))
4689 : {
4690 : /* We should not get here; the VERIFY_CONSTANT above
4691 : should have already caught it. */
4692 0 : gcc_checking_assert (false);
4693 : if (!ctx->quiet)
4694 : error_at (loc, "cannot allocate array: size not constant");
4695 : *non_constant_p = true;
4696 : return t;
4697 : }
4698 414416 : tree type = build_array_type_nelts (char_type_node,
4699 207208 : tree_to_uhwi (arg0));
4700 207208 : tree var = build_decl (loc, VAR_DECL,
4701 207208 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4702 : & OVL_OP_FLAG_VEC)
4703 : ? heap_vec_uninit_identifier
4704 : : heap_uninit_identifier,
4705 207208 : type);
4706 207208 : DECL_ARTIFICIAL (var) = 1;
4707 207208 : ctx->global->heap_vars.safe_push (var);
4708 207208 : ctx->global->put_value (var, NULL_TREE);
4709 207208 : return fold_convert (ptr_type_node, build_address (var));
4710 : }
4711 : else
4712 : {
4713 79131 : STRIP_NOPS (arg0);
4714 79131 : if (TREE_CODE (arg0) == ADDR_EXPR
4715 79131 : && VAR_P (TREE_OPERAND (arg0, 0)))
4716 : {
4717 79131 : tree var = TREE_OPERAND (arg0, 0);
4718 79131 : if (DECL_NAME (var) == heap_uninit_identifier
4719 79131 : || DECL_NAME (var) == heap_identifier)
4720 : {
4721 78957 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4722 : & OVL_OP_FLAG_VEC)
4723 : {
4724 9 : if (!ctx->quiet)
4725 : {
4726 3 : auto_diagnostic_group d;
4727 3 : error_at (loc, "array deallocation of object "
4728 : "allocated with non-array "
4729 : "allocation");
4730 3 : inform (DECL_SOURCE_LOCATION (var),
4731 : "allocation performed here");
4732 3 : }
4733 9 : *non_constant_p = true;
4734 9 : return t;
4735 : }
4736 78948 : DECL_NAME (var) = heap_deleted_identifier;
4737 78948 : ctx->global->destroy_value (var);
4738 78948 : ctx->global->heap_dealloc_count++;
4739 78948 : return void_node;
4740 : }
4741 174 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4742 174 : || DECL_NAME (var) == heap_vec_identifier)
4743 : {
4744 150 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4745 : & OVL_OP_FLAG_VEC) == 0)
4746 : {
4747 9 : if (!ctx->quiet)
4748 : {
4749 3 : auto_diagnostic_group d;
4750 3 : error_at (loc, "non-array deallocation of "
4751 : "object allocated with array "
4752 : "allocation");
4753 3 : inform (DECL_SOURCE_LOCATION (var),
4754 : "allocation performed here");
4755 3 : }
4756 9 : *non_constant_p = true;
4757 9 : return t;
4758 : }
4759 141 : DECL_NAME (var) = heap_deleted_identifier;
4760 141 : ctx->global->destroy_value (var);
4761 141 : ctx->global->heap_dealloc_count++;
4762 141 : return void_node;
4763 : }
4764 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4765 : {
4766 15 : if (!ctx->quiet)
4767 6 : error_at (loc, "deallocation of already deallocated "
4768 : "storage");
4769 15 : *non_constant_p = true;
4770 15 : return t;
4771 : }
4772 : }
4773 9 : if (!ctx->quiet)
4774 3 : error_at (loc, "deallocation of storage that was "
4775 : "not previously allocated");
4776 9 : *non_constant_p = true;
4777 9 : return t;
4778 : }
4779 : }
4780 : /* Allow placement new in std::construct_at, just return the second
4781 : argument. */
4782 402566 : if (TREE_CODE (t) == CALL_EXPR
4783 394861 : && cxx_placement_new_fn (fun)
4784 578640 : && is_std_construct_at (ctx->call))
4785 : {
4786 46029 : const int nargs = call_expr_nargs (t);
4787 46029 : tree arg1 = NULL_TREE;
4788 138087 : for (int i = 0; i < nargs; ++i)
4789 : {
4790 92058 : tree arg = CALL_EXPR_ARG (t, i);
4791 92058 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4792 : non_constant_p, overflow_p,
4793 : jump_target);
4794 92058 : if (*jump_target)
4795 : return NULL_TREE;
4796 92058 : if (i == 1)
4797 : arg1 = arg;
4798 : else
4799 92058 : VERIFY_CONSTANT (arg);
4800 : }
4801 46029 : gcc_assert (arg1);
4802 : return arg1;
4803 : }
4804 356537 : else if (cxx_dynamic_cast_fn_p (fun))
4805 3173 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4806 3173 : jump_target);
4807 353364 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4808 29878 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4809 : non_constant_p, overflow_p,
4810 29878 : jump_target);
4811 :
4812 : /* Calls to non-constexpr functions can be diagnosed right away
4813 : before C++26, though in C++26 evaluation of the arguments might
4814 : throw and if caught it could be still constant expression.
4815 : So for C++26 this is diagnosed only after
4816 : cxx_bind_parameters_in_call. */
4817 323486 : if (cxx_dialect >= cxx26)
4818 : non_constexpr_call = true;
4819 : else
4820 : {
4821 282136 : if (!ctx->quiet)
4822 : {
4823 210 : if (!lambda_static_thunk_p (fun))
4824 210 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4825 210 : explain_invalid_constexpr_fn (fun);
4826 : }
4827 282136 : *non_constant_p = true;
4828 282136 : return t;
4829 : }
4830 : }
4831 :
4832 183482341 : constexpr_ctx new_ctx = *ctx;
4833 183482341 : ctx = &new_ctx;
4834 206289246 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4835 187419674 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4836 : {
4837 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4838 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4839 2777 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4840 2777 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4841 2777 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4842 2777 : ctx->global->put_value (new_ctx.object, ctor);
4843 : }
4844 : /* An immediate invocation is manifestly constant evaluated including the
4845 : arguments of the call, so use mce_true even for the argument
4846 : evaluation. */
4847 366964682 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4848 3446926 : new_ctx.manifestly_const_eval = mce_true;
4849 :
4850 : /* We used to shortcut trivial constructor/op= here, but nowadays
4851 : we can only get a trivial function here with -fno-elide-constructors. */
4852 183482477 : gcc_checking_assert (!trivial_fn_p (fun)
4853 : || !flag_elide_constructors
4854 : /* Or it's a call from maybe_thunk_body (111075). */
4855 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4856 : : AGGR_INIT_FROM_THUNK_P (t))
4857 : /* We don't elide constructors when processing
4858 : a noexcept-expression. */
4859 : || cp_noexcept_operand);
4860 :
4861 183482341 : bool non_constant_args = false;
4862 183482341 : constexpr_call new_call;
4863 183482341 : new_call.bindings
4864 183482341 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4865 : overflow_p, &non_constant_args,
4866 : jump_target);
4867 :
4868 : /* We build up the bindings list before we know whether we already have this
4869 : call cached. If we don't end up saving these bindings, ggc_free them when
4870 : this function exits. */
4871 183482341 : class free_bindings
4872 : {
4873 : tree *bindings;
4874 : public:
4875 183482341 : free_bindings (tree &b): bindings (&b) { }
4876 183482337 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4877 4605355 : void preserve () { bindings = NULL; }
4878 183482341 : } fb (new_call.bindings);
4879 :
4880 183482341 : if (*jump_target)
4881 : return NULL_TREE;
4882 183482257 : if (*non_constant_p)
4883 : return t;
4884 109942716 : if (non_constexpr_call)
4885 : {
4886 6460 : if (!ctx->quiet)
4887 : {
4888 440 : if (!lambda_static_thunk_p (fun))
4889 440 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4890 440 : explain_invalid_constexpr_fn (fun);
4891 : }
4892 6460 : *non_constant_p = true;
4893 6460 : return t;
4894 : }
4895 :
4896 : /* We can't defer instantiating the function any longer. */
4897 109936256 : if (!DECL_INITIAL (fun)
4898 3107871 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4899 109936256 : && !uid_sensitive_constexpr_evaluation_p ())
4900 : {
4901 2868578 : location_t save_loc = input_location;
4902 2868578 : input_location = loc;
4903 2868578 : ++function_depth;
4904 2868578 : if (ctx->manifestly_const_eval == mce_true)
4905 367968 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4906 2868578 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4907 2868564 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4908 : else
4909 14 : synthesize_method (fun);
4910 2868578 : --function_depth;
4911 2868578 : input_location = save_loc;
4912 : }
4913 :
4914 : /* If in direct recursive call, optimize definition search. */
4915 109936256 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4916 29287 : new_call.fundef = ctx->call->fundef;
4917 : else
4918 : {
4919 109906969 : new_call.fundef = retrieve_constexpr_fundef (fun);
4920 109906969 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4921 109343497 : || new_call.fundef->result == error_mark_node
4922 109327649 : || fun == current_function_decl)
4923 : {
4924 579326 : if (!ctx->quiet)
4925 : {
4926 : /* We need to check for current_function_decl here in case we're
4927 : being called during cp_fold_function, because at that point
4928 : DECL_INITIAL is set properly and we have a fundef but we
4929 : haven't lowered invisirefs yet (c++/70344). */
4930 187 : if (DECL_INITIAL (fun) == error_mark_node
4931 187 : || fun == current_function_decl)
4932 15 : error_at (loc, "%qD called in a constant expression before its "
4933 : "definition is complete", fun);
4934 172 : else if (DECL_INITIAL (fun))
4935 : {
4936 : /* The definition of fun was somehow unsuitable. But pretend
4937 : that lambda static thunks don't exist. */
4938 134 : if (!lambda_static_thunk_p (fun))
4939 122 : error_at (loc, "%qD called in a constant expression", fun);
4940 134 : explain_invalid_constexpr_fn (fun);
4941 : }
4942 : else
4943 38 : error_at (loc, "%qD used before its definition", fun);
4944 : }
4945 579326 : *non_constant_p = true;
4946 579326 : return t;
4947 : }
4948 : }
4949 :
4950 : /* Don't complain about problems evaluating an ill-formed function. */
4951 109356930 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4952 109356930 : if (f->language->erroneous)
4953 1005 : new_ctx.quiet = true;
4954 :
4955 109356930 : int depth_ok = push_cx_call_context (t);
4956 :
4957 : /* Remember the object we are constructing or destructing. */
4958 109356930 : tree new_obj = NULL_TREE;
4959 218713860 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4960 : {
4961 : /* In a cdtor, it should be the first `this' argument.
4962 : At this point it has already been evaluated in the call
4963 : to cxx_bind_parameters_in_call. */
4964 13624773 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4965 13624773 : bool empty_base = false;
4966 13624773 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4967 : &empty_base, jump_target);
4968 13624773 : if (*jump_target)
4969 0 : return NULL_TREE;
4970 : /* If we're initializing an empty class, don't set constness, because
4971 : cxx_fold_indirect_ref will return the wrong object to set constness
4972 : of. */
4973 13624773 : if (empty_base)
4974 : new_obj = NULL_TREE;
4975 6845203 : else if (ctx->call && ctx->call->fundef
4976 24797910 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4977 : {
4978 3570915 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4979 3570915 : STRIP_NOPS (cur_obj);
4980 3570915 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4981 3568426 : cur_obj = TREE_OPERAND (cur_obj, 0);
4982 3570915 : if (new_obj == cur_obj)
4983 : /* We're calling the target constructor of a delegating
4984 : constructor, or accessing a base subobject through a
4985 : NOP_EXPR as part of a call to a base constructor, so
4986 : there is no new (sub)object. */
4987 13624773 : new_obj = NULL_TREE;
4988 : }
4989 : }
4990 :
4991 109356930 : tree result = NULL_TREE;
4992 :
4993 109356930 : constexpr_call *entry = NULL;
4994 109356930 : if (depth_ok && !non_constant_args && ctx->strict)
4995 : {
4996 36824097 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4997 36824097 : new_call.hash
4998 36824097 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4999 :
5000 : /* If we have seen this call before, we are done. */
5001 36824097 : maybe_initialize_constexpr_call_table ();
5002 36824097 : bool insert = depth_ok < constexpr_cache_depth;
5003 36824097 : constexpr_call **slot
5004 50186768 : = constexpr_call_table->find_slot (&new_call,
5005 : insert ? INSERT : NO_INSERT);
5006 36824097 : entry = slot ? *slot : NULL;
5007 32270813 : if (entry == NULL)
5008 : {
5009 : /* Only cache up to constexpr_cache_depth to limit memory use. */
5010 9158639 : if (insert)
5011 : {
5012 : /* We need to keep a pointer to the entry, not just the slot, as
5013 : the slot can move during evaluation of the body. */
5014 4605355 : *slot = entry = ggc_alloc<constexpr_call> ();
5015 4605355 : *entry = new_call;
5016 4605355 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
5017 4605355 : fb.preserve ();
5018 :
5019 : /* Unshare args going into the hash table to separate them
5020 : from the caller's context, for better GC and to avoid
5021 : problems with verify_gimple. */
5022 4605355 : tree bound = new_call.bindings;
5023 7446722 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
5024 : {
5025 2841367 : tree &arg = TREE_VEC_ELT (bound, i);
5026 2841367 : arg = unshare_expr_without_location (arg);
5027 : }
5028 : }
5029 : }
5030 : /* Calls that are in progress have their result set to unknown_type_node,
5031 : so that we can detect circular dependencies. Now that we only cache
5032 : up to constexpr_cache_depth this won't catch circular dependencies that
5033 : start deeper, but they'll hit the recursion or ops limit. */
5034 27665458 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
5035 : {
5036 6 : if (!ctx->quiet)
5037 0 : error ("call has circular dependency");
5038 6 : *non_constant_p = true;
5039 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
5040 : }
5041 : else
5042 27665452 : result = entry->result (ctx->manifestly_const_eval);
5043 : }
5044 :
5045 109356897 : if (!depth_ok)
5046 : {
5047 33 : if (!ctx->quiet)
5048 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
5049 : "%<-fconstexpr-depth=%> to increase the maximum)",
5050 : max_constexpr_depth);
5051 33 : *non_constant_p = true;
5052 33 : result = error_mark_node;
5053 : }
5054 : else
5055 : {
5056 109356897 : bool cacheable = !!entry;
5057 109356897 : if (result && result != error_mark_node)
5058 : /* OK */;
5059 86755854 : else if (!DECL_SAVED_TREE (fun))
5060 : {
5061 : /* When at_eof >= 3, cgraph has started throwing away
5062 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
5063 : late code generation for VEC_INIT_EXPR, which needs to be
5064 : completely reconsidered. */
5065 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
5066 0 : *non_constant_p = true;
5067 : }
5068 86755854 : else if (tree copy = get_fundef_copy (new_call.fundef))
5069 : {
5070 86755845 : tree body, parms, res;
5071 86755845 : releasing_vec ctors;
5072 :
5073 : /* Reuse or create a new unshared copy of this function's body. */
5074 86755845 : body = TREE_PURPOSE (copy);
5075 86755845 : parms = TREE_VALUE (copy);
5076 86755845 : res = TREE_TYPE (copy);
5077 :
5078 : /* Associate the bindings with the remapped parms. */
5079 86755845 : tree bound = new_call.bindings;
5080 86755845 : tree remapped = parms;
5081 212666625 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
5082 : {
5083 125910780 : tree arg = TREE_VEC_ELT (bound, i);
5084 125910780 : if (entry)
5085 : {
5086 : /* Unshare again so the callee doesn't change the
5087 : argument values in the hash table. XXX Could we unshare
5088 : lazily in cxx_eval_store_expression? */
5089 3081714 : arg = unshare_constructor (arg);
5090 3081714 : if (TREE_CODE (arg) == CONSTRUCTOR)
5091 871958 : vec_safe_push (ctors, arg);
5092 : }
5093 125910780 : ctx->global->put_value (remapped, arg);
5094 125910780 : remapped = DECL_CHAIN (remapped);
5095 : }
5096 86755845 : if (remapped)
5097 : {
5098 : /* We shouldn't have any parms without args, but fail gracefully
5099 : in error recovery. */
5100 6 : gcc_checking_assert (seen_error ());
5101 6 : *non_constant_p = true;
5102 : }
5103 : /* Add the RESULT_DECL to the values map, too. */
5104 86755845 : gcc_assert (!DECL_BY_REFERENCE (res));
5105 86755845 : ctx->global->put_value (res, NULL_TREE);
5106 :
5107 : /* Remember the current call we're evaluating. */
5108 86755845 : constexpr_ctx call_ctx = *ctx;
5109 86755845 : call_ctx.call = &new_call;
5110 86755845 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
5111 86755845 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
5112 86755845 : bool save_state_dependent = ctx->global->state_dependent;
5113 :
5114 : /* Make sure we fold std::is_constant_evaluated to true in an
5115 : immediate function. */
5116 173511690 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
5117 1437371 : call_ctx.manifestly_const_eval = mce_true;
5118 :
5119 : /* If this is a constexpr destructor, the object's const and volatile
5120 : semantics are no longer in effect; see [class.dtor]p5. */
5121 97805188 : if (new_obj && DECL_DESTRUCTOR_P (fun))
5122 1235549 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
5123 : non_constant_p, overflow_p, jump_target);
5124 :
5125 : /* If this is a constructor, we are beginning the lifetime of the
5126 : object we are initializing. */
5127 86755845 : if (new_obj
5128 22098686 : && DECL_CONSTRUCTOR_P (fun)
5129 9813794 : && TREE_CODE (new_obj) == COMPONENT_REF
5130 89566803 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
5131 : {
5132 8980 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
5133 8980 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
5134 8980 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
5135 : new_obj, ctor);
5136 8980 : cxx_eval_constant_expression (ctx, activate,
5137 : lval, non_constant_p, overflow_p,
5138 : jump_target);
5139 8980 : ggc_free (activate);
5140 8980 : if (*jump_target)
5141 0 : return NULL_TREE;
5142 : }
5143 :
5144 86755845 : ctx->global->state_dependent = false;
5145 :
5146 86755845 : tree jmp_target = NULL_TREE;
5147 86755845 : cxx_eval_constant_expression (&call_ctx, body,
5148 : vc_discard, non_constant_p, overflow_p,
5149 : &jmp_target);
5150 :
5151 86755841 : if (!*non_constant_p && throws (&jmp_target))
5152 : {
5153 4701 : result = NULL_TREE;
5154 4701 : cacheable = false;
5155 4701 : *jump_target = jmp_target;
5156 : }
5157 86751140 : else if (!*non_constant_p && TREE_THIS_VOLATILE (fun))
5158 : {
5159 : /* Return from a [[noreturn]] function. */
5160 11 : if (!ctx->quiet)
5161 5 : error ("%<[[noreturn]]%> call returns");
5162 11 : *non_constant_p = true;
5163 : }
5164 173502258 : else if (DECL_CONSTRUCTOR_P (fun))
5165 : /* This can be null for a subobject constructor call, in
5166 : which case what we care about is the initialization
5167 : side-effects rather than the value. We could get at the
5168 : value by evaluating *this, but we don't bother; there's
5169 : no need to put such a call in the hash table. */
5170 12221528 : result = lval ? ctx->object : ctx->ctor;
5171 74529601 : else if (VOID_TYPE_P (TREE_TYPE (res)))
5172 7299961 : result = void_node;
5173 : else
5174 : {
5175 67229640 : result = ctx->global->get_value (res);
5176 10878437 : if (result == NULL_TREE && !*non_constant_p
5177 67229860 : && !DECL_DESTRUCTOR_P (fun))
5178 : {
5179 110 : if (!ctx->quiet)
5180 6 : error ("%<constexpr%> call flows off the end "
5181 : "of the function");
5182 110 : *non_constant_p = true;
5183 : }
5184 : }
5185 :
5186 86755841 : if (ctx->global->state_dependent)
5187 59949 : cacheable = false;
5188 86755841 : ctx->global->state_dependent |= save_state_dependent;
5189 :
5190 : /* At this point, the object's constructor will have run, so
5191 : the object is no longer under construction, and its possible
5192 : 'const' semantics now apply. Make a note of this fact by
5193 : marking the CONSTRUCTOR TREE_READONLY. */
5194 97805184 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
5195 9813794 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
5196 : non_constant_p, overflow_p, jump_target);
5197 :
5198 : /* Remove the parms/result from the values map. */
5199 86755841 : destroy_value_checked (ctx, res, non_constant_p);
5200 299422466 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
5201 125910784 : destroy_value_checked (ctx, parm, non_constant_p);
5202 :
5203 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
5204 87627799 : while (!ctors->is_empty ())
5205 : {
5206 871958 : tree c = ctors->pop ();
5207 871958 : if (c != result)
5208 871958 : free_constructor (c);
5209 : }
5210 :
5211 : /* Make the unshared function copy we used available for re-use. */
5212 86755841 : save_fundef_copy (fun, copy);
5213 :
5214 : /* If the call allocated some heap object that hasn't been
5215 : deallocated during the call, or if it deallocated some heap
5216 : object it has not allocated, the call isn't really stateless
5217 : for the constexpr evaluation and should not be cached.
5218 : It is fine if the call allocates something and deallocates it
5219 : too. */
5220 86755841 : if (cacheable
5221 96420219 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
5222 9659877 : || (save_heap_dealloc_count
5223 9659877 : != ctx->global->heap_dealloc_count)))
5224 : {
5225 4501 : tree heap_var;
5226 4501 : unsigned int i;
5227 4501 : if ((ctx->global->heap_vars.length ()
5228 4501 : - ctx->global->heap_dealloc_count)
5229 4501 : != save_heap_alloc_count - save_heap_dealloc_count)
5230 : cacheable = false;
5231 : else
5232 139075 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
5233 : save_heap_alloc_count)
5234 136450 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
5235 : {
5236 : cacheable = false;
5237 : break;
5238 : }
5239 : }
5240 :
5241 : /* Rewrite all occurrences of the function's RESULT_DECL with the
5242 : current object under construction. */
5243 86755841 : if (!*non_constant_p
5244 72228545 : && ctx->object
5245 17124165 : && CLASS_TYPE_P (TREE_TYPE (res))
5246 89939303 : && !is_empty_class (TREE_TYPE (res)))
5247 : {
5248 2266517 : if (!same_type_ignoring_top_level_qualifiers_p
5249 2266517 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
5250 0 : *non_constant_p = true;
5251 2266517 : else if (replace_decl (&result, res, ctx->object))
5252 : {
5253 1791 : cacheable = false;
5254 1791 : result = cxx_eval_constant_expression (ctx, result, lval,
5255 : non_constant_p,
5256 : overflow_p,
5257 : jump_target);
5258 1791 : if (*jump_target)
5259 : {
5260 0 : cacheable = false;
5261 0 : result = NULL_TREE;
5262 : }
5263 : }
5264 : }
5265 :
5266 : /* Only cache a permitted result of a constant expression. */
5267 86754050 : if (cacheable && !reduced_constant_expression_p (result))
5268 : cacheable = false;
5269 :
5270 : /* Only cache a result without contract violations. */
5271 81782764 : if (cacheable && ctx->global->contract_statement)
5272 82066905 : cacheable = false;
5273 86755841 : }
5274 : else
5275 : /* Couldn't get a function copy to evaluate. */
5276 9 : *non_constant_p = true;
5277 :
5278 109356893 : if (result == error_mark_node)
5279 6 : *non_constant_p = true;
5280 109356893 : if (*non_constant_p || *overflow_p)
5281 14527461 : result = error_mark_node;
5282 94829432 : else if (!result)
5283 3895511 : result = void_node;
5284 109356893 : if (entry)
5285 : {
5286 64541622 : entry->result (ctx->manifestly_const_eval)
5287 32270811 : = cacheable ? result : error_mark_node;
5288 :
5289 32270811 : if (result != error_mark_node
5290 27363860 : && ctx->manifestly_const_eval == mce_unknown)
5291 : {
5292 : /* Evaluation succeeded and was independent of whether we're in a
5293 : manifestly constant-evaluated context, so we can also reuse
5294 : this result when evaluating this call with a fixed context. */
5295 1901644 : if (!entry->result (mce_true))
5296 702399 : entry->result (mce_true) = entry->result (mce_unknown);
5297 1901644 : if (!entry->result (mce_false))
5298 669506 : entry->result (mce_false) = entry->result (mce_unknown);
5299 : }
5300 : }
5301 : }
5302 :
5303 : /* The result of a constexpr function must be completely initialized.
5304 :
5305 : However, in C++20, a constexpr constructor doesn't necessarily have
5306 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
5307 : in order to detect reading an uninitialized object in constexpr instead
5308 : of value-initializing it. (reduced_constant_expression_p is expected to
5309 : take care of clearing the flag.) */
5310 109356926 : if (TREE_CODE (result) == CONSTRUCTOR
5311 109356926 : && (cxx_dialect < cxx20
5312 21298986 : || !DECL_CONSTRUCTOR_P (fun)))
5313 5246123 : clear_no_implicit_zero (result);
5314 :
5315 109356926 : pop_cx_call_context ();
5316 :
5317 : /* A virtual call with a zero-offset covariant return needs no thunk, so
5318 : constant evaluation can evaluate the final overrider directly. The
5319 : result then has the overrider's return type rather than the static type
5320 : of the call. Adjust after caching so a direct call can reuse the result
5321 : with its original type. */
5322 109356926 : if (!*non_constant_p
5323 94829588 : && !*overflow_p
5324 94829432 : && !*jump_target
5325 94824731 : && DECL_VIRTUAL_P (fun)
5326 26625 : && result != void_node
5327 2217 : && INDIRECT_TYPE_P (TREE_TYPE (t))
5328 109357979 : && !(same_type_ignoring_top_level_qualifiers_p
5329 1053 : (TREE_TYPE (result), TREE_TYPE (t))))
5330 11 : result = adjust_temp_type (TREE_TYPE (t), result);
5331 :
5332 109356926 : return result;
5333 183482337 : }
5334 :
5335 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
5336 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
5337 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
5338 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
5339 : UNION_ELEMENTAL_SUBOBJECT is true when recursing on union elemental
5340 : subobjects.
5341 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
5342 :
5343 : bool
5344 1241222995 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */,
5345 : bool union_elemental_subobject /* = false */)
5346 : {
5347 1241222995 : if (t == NULL_TREE)
5348 : return false;
5349 :
5350 1236320942 : switch (TREE_CODE (t))
5351 : {
5352 : case PTRMEM_CST:
5353 : case REFLECT_EXPR:
5354 : /* Even if we can't lower this yet, it's constant. */
5355 : return true;
5356 :
5357 : case OMP_DECLARE_MAPPER:
5358 : return true;
5359 :
5360 64401413 : case CONSTRUCTOR:
5361 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
5362 64401413 : tree field;
5363 64401413 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
5364 : /* A constant vector would be folded to VECTOR_CST.
5365 : A CONSTRUCTOR of scalar type means uninitialized. */
5366 : return false;
5367 64384794 : if (CONSTRUCTOR_NO_CLEARING (t))
5368 : {
5369 3452438 : if (union_elemental_subobject
5370 5344 : && cxx_dialect >= cxx26
5371 2037 : && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
5372 3453720 : && CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (t))
5373 : field = NULL_TREE;
5374 3451990 : else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5375 : {
5376 : /* There must be a valid constant initializer at every array
5377 : index. */
5378 6128 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
5379 6128 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
5380 6128 : tree cursor = min;
5381 97686 : for (auto &e: CONSTRUCTOR_ELTS (t))
5382 : {
5383 79521 : if (!reduced_constant_expression_p
5384 79521 : (e.value, NULL_TREE, union_elemental_subobject))
5385 : return false;
5386 79362 : if (array_index_cmp (cursor, e.index) != 0)
5387 : return false;
5388 79302 : if (TREE_CODE (e.index) == RANGE_EXPR)
5389 0 : cursor = TREE_OPERAND (e.index, 1);
5390 79302 : if (TREE_CODE (e.value) == RAW_DATA_CST)
5391 0 : cursor
5392 0 : = int_const_binop (PLUS_EXPR, cursor,
5393 0 : size_int (RAW_DATA_LENGTH (e.value)));
5394 : else
5395 79302 : cursor = int_const_binop (PLUS_EXPR, cursor,
5396 79302 : size_one_node);
5397 : }
5398 5909 : if (find_array_ctor_elt (t, max) == -1)
5399 : return false;
5400 5638 : goto ok;
5401 : }
5402 3445862 : else if (cxx_dialect >= cxx20
5403 3445862 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
5404 : {
5405 : /* A union can have at most one active member. A union with no
5406 : active member has no constituent values, so all constituent
5407 : values are constant. */
5408 : field = NULL_TREE;
5409 : union_elemental_subobject = true;
5410 : }
5411 : else
5412 : {
5413 3442286 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
5414 3442286 : union_elemental_subobject = false;
5415 : }
5416 : }
5417 : else
5418 : {
5419 60932356 : field = NULL_TREE;
5420 60932356 : if (TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
5421 : union_elemental_subobject = true;
5422 59980309 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
5423 57132736 : union_elemental_subobject = false;
5424 : }
5425 601952696 : for (auto &e: CONSTRUCTOR_ELTS (t))
5426 : {
5427 : /* If VAL is null, we're in the middle of initializing this
5428 : element. */
5429 469532988 : if (!reduced_constant_expression_p (e.value,
5430 469532988 : (e.index
5431 469532952 : && (TREE_CODE (e.index)
5432 : == FIELD_DECL))
5433 59602367 : ? DECL_SIZE (e.index)
5434 : : NULL_TREE,
5435 : union_elemental_subobject))
5436 : return false;
5437 : /* We want to remove initializers for empty fields in a struct to
5438 : avoid confusing output_constructor. */
5439 468955428 : if (is_empty_field (e.index)
5440 468955428 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
5441 : return false;
5442 : /* Check for non-empty fields between initialized fields when
5443 : CONSTRUCTOR_NO_CLEARING. */
5444 467842898 : for (; field && e.index != field;
5445 381838 : field = next_subobject_field (DECL_CHAIN (field)))
5446 386654 : if (!is_really_empty_class (TREE_TYPE (field),
5447 : /*ignore_vptr*/false))
5448 : return false;
5449 467456244 : if (field)
5450 3924109 : field = next_subobject_field (DECL_CHAIN (field));
5451 : }
5452 : /* There could be a non-empty field at the end. */
5453 62581302 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
5454 285678 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
5455 : {
5456 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
5457 : the parent FIELD_DECL (if any) for classes with virtual
5458 : bases. */
5459 6298 : if (cxx_dialect >= cxx26
5460 4353 : && sz
5461 6820 : && tree_int_cst_le (sz, bit_position (field)))
5462 : break;
5463 : return false;
5464 : }
5465 62295624 : ok:
5466 62301654 : if (CONSTRUCTOR_NO_CLEARING (t))
5467 : /* All the fields are initialized. */
5468 3318587 : CONSTRUCTOR_NO_CLEARING (t) = false;
5469 : return true;
5470 :
5471 1171650142 : default:
5472 : /* FIXME are we calling this too much? */
5473 1171650142 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
5474 : }
5475 : }
5476 :
5477 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
5478 : why and suggest what could be done about it. */
5479 :
5480 : static tree
5481 1608 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
5482 : {
5483 1608 : bool ref_p = false;
5484 :
5485 : /* No need to look into types or unevaluated operands. */
5486 1608 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
5487 : {
5488 0 : *walk_subtrees = false;
5489 0 : return NULL_TREE;
5490 : }
5491 :
5492 1608 : switch (TREE_CODE (*tp))
5493 : {
5494 106 : CASE_CONVERT:
5495 106 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
5496 : break;
5497 87 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
5498 87 : *tp = TREE_OPERAND (*tp, 0);
5499 219 : gcc_fallthrough ();
5500 219 : case ADDR_EXPR:
5501 219 : {
5502 219 : tree op = TREE_OPERAND (*tp, 0);
5503 219 : if (VAR_P (op)
5504 118 : && DECL_DECLARED_CONSTEXPR_P (op)
5505 41 : && !TREE_STATIC (op)
5506 : /* ??? We should also say something about temporaries. */
5507 257 : && !DECL_ARTIFICIAL (op))
5508 : {
5509 33 : if (ref_p)
5510 12 : inform (location_of (*tp), "reference to %qD is not a constant "
5511 : "expression", op);
5512 : else
5513 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
5514 : "expression", op);
5515 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
5516 33 : rich_location richloc (line_table, op_loc);
5517 33 : richloc.add_fixit_insert_before (op_loc, "static ");
5518 33 : inform (&richloc,
5519 : "address of non-static constexpr variable %qD may differ on "
5520 : "each invocation of the enclosing function; add %<static%> "
5521 : "to give it a constant address", op);
5522 33 : }
5523 : break;
5524 : }
5525 : default:
5526 : break;
5527 : }
5528 :
5529 : return NULL_TREE;
5530 : }
5531 :
5532 : /* Some expressions may have constant operands but are not constant
5533 : themselves, such as 1/0. Call this function to check for that
5534 : condition.
5535 :
5536 : We only call this in places that require an arithmetic constant, not in
5537 : places where we might have a non-constant expression that can be a
5538 : component of a constant expression, such as the address of a constexpr
5539 : variable that might be dereferenced later. */
5540 :
5541 : static bool
5542 671175830 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
5543 : bool *overflow_p)
5544 : {
5545 658268699 : if (!*non_constant_p && !reduced_constant_expression_p (t)
5546 674097787 : && t != void_node)
5547 : {
5548 2921505 : if (!allow_non_constant)
5549 : {
5550 424 : auto_diagnostic_group d;
5551 581 : error_at (cp_expr_loc_or_input_loc (t),
5552 : "%q+E is not a constant expression", t);
5553 424 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
5554 : nullptr);
5555 424 : }
5556 2921505 : *non_constant_p = true;
5557 : }
5558 671175830 : if (TREE_OVERFLOW_P (t))
5559 : {
5560 678 : if (!allow_non_constant)
5561 : {
5562 155 : permerror (input_location, "overflow in constant expression");
5563 : /* If we're being permissive (and are in an enforcing
5564 : context), ignore the overflow. */
5565 155 : if (flag_permissive)
5566 75 : return *non_constant_p;
5567 : }
5568 603 : *overflow_p = true;
5569 : }
5570 671175755 : return *non_constant_p;
5571 : }
5572 :
5573 : /* Check whether the shift operation with code CODE and type TYPE on LHS
5574 : and RHS is undefined. If it is, give an error with an explanation,
5575 : and return true; return false otherwise. */
5576 :
5577 : static bool
5578 79858326 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
5579 : enum tree_code code, tree type, tree lhs, tree rhs)
5580 : {
5581 79858326 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
5582 5472725 : || TREE_CODE (lhs) != INTEGER_CST
5583 5472449 : || TREE_CODE (rhs) != INTEGER_CST)
5584 : return false;
5585 :
5586 5472449 : tree lhstype = TREE_TYPE (lhs);
5587 5472449 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
5588 :
5589 : /* [expr.shift] The behavior is undefined if the right operand
5590 : is negative, or greater than or equal to the length in bits
5591 : of the promoted left operand. */
5592 5472449 : if (tree_int_cst_sgn (rhs) == -1)
5593 : {
5594 126 : if (!ctx->quiet)
5595 35 : permerror (loc, "right operand of shift expression %q+E is negative",
5596 : build2_loc (loc, code, type, lhs, rhs));
5597 126 : return (!flag_permissive || ctx->quiet);
5598 : }
5599 5472323 : if (compare_tree_int (rhs, uprec) >= 0)
5600 : {
5601 200 : if (!ctx->quiet)
5602 34 : permerror (loc, "right operand of shift expression %q+E is greater "
5603 : "than or equal to the precision %wu of the left operand",
5604 : build2_loc (loc, code, type, lhs, rhs), uprec);
5605 200 : return (!flag_permissive || ctx->quiet);
5606 : }
5607 :
5608 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
5609 : if E1 has a signed type and non-negative value, and E1x2^E2 is
5610 : representable in the corresponding unsigned type of the result type,
5611 : then that value, converted to the result type, is the resulting value;
5612 : otherwise, the behavior is undefined.
5613 : For C++20:
5614 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
5615 : 2^N, where N is the range exponent of the type of the result. */
5616 5472123 : if (code == LSHIFT_EXPR
5617 4561286 : && !TYPE_OVERFLOW_WRAPS (lhstype)
5618 2140936 : && cxx_dialect >= cxx11
5619 7596742 : && cxx_dialect < cxx20)
5620 : {
5621 54060 : if (tree_int_cst_sgn (lhs) == -1)
5622 : {
5623 74 : if (!ctx->quiet)
5624 18 : permerror (loc,
5625 : "left operand of shift expression %q+E is negative",
5626 : build2_loc (loc, code, type, lhs, rhs));
5627 74 : return (!flag_permissive || ctx->quiet);
5628 : }
5629 : /* For signed x << y the following:
5630 : (unsigned) x >> ((prec (lhs) - 1) - y)
5631 : if > 1, is undefined. The right-hand side of this formula
5632 : is the highest bit of the LHS that can be set (starting from 0),
5633 : so that the shift doesn't overflow. We then right-shift the LHS
5634 : to see whether any other bit is set making the original shift
5635 : undefined -- the result is not representable in the corresponding
5636 : unsigned type. */
5637 53986 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
5638 53986 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
5639 53986 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
5640 53986 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
5641 53986 : if (tree_int_cst_lt (integer_one_node, t))
5642 : {
5643 41 : if (!ctx->quiet)
5644 7 : permerror (loc, "shift expression %q+E overflows",
5645 : build2_loc (loc, code, type, lhs, rhs));
5646 41 : return (!flag_permissive || ctx->quiet);
5647 : }
5648 : }
5649 : return false;
5650 : }
5651 :
5652 : /* Subroutine of cxx_eval_constant_expression.
5653 : Attempt to reduce the unary expression tree T to a compile time value.
5654 : If successful, return the value. Otherwise issue a diagnostic
5655 : and return error_mark_node. */
5656 :
5657 : static tree
5658 25796650 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
5659 : bool /*lval*/,
5660 : bool *non_constant_p, bool *overflow_p,
5661 : tree *jump_target)
5662 : {
5663 25796650 : tree r;
5664 25796650 : tree orig_arg = TREE_OPERAND (t, 0);
5665 25796650 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
5666 : non_constant_p, overflow_p,
5667 : jump_target);
5668 25796648 : if (*jump_target)
5669 : return NULL_TREE;
5670 25796646 : VERIFY_CONSTANT (arg);
5671 20572339 : location_t loc = EXPR_LOCATION (t);
5672 20572339 : enum tree_code code = TREE_CODE (t);
5673 20572339 : tree type = TREE_TYPE (t);
5674 20572339 : r = fold_unary_loc (loc, code, type, arg);
5675 20572339 : if (r == NULL_TREE)
5676 : {
5677 17 : if (arg == orig_arg)
5678 : r = t;
5679 : else
5680 13 : r = build1_loc (loc, code, type, arg);
5681 : }
5682 20572339 : VERIFY_CONSTANT (r);
5683 : return r;
5684 : }
5685 :
5686 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5687 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5688 : generic folding should be used. */
5689 :
5690 : static tree
5691 10936975 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5692 : tree lhs, tree rhs, bool *non_constant_p,
5693 : bool *overflow_p, tree *jump_target)
5694 : {
5695 10936975 : STRIP_NOPS (lhs);
5696 10936975 : if (TREE_CODE (lhs) != ADDR_EXPR)
5697 : return NULL_TREE;
5698 :
5699 10068760 : lhs = TREE_OPERAND (lhs, 0);
5700 :
5701 : /* &A[i] p+ j => &A[i + j] */
5702 10068760 : if (TREE_CODE (lhs) == ARRAY_REF
5703 211129 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5704 211129 : && TREE_CODE (rhs) == INTEGER_CST
5705 211129 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5706 10279889 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5707 : {
5708 211129 : tree orig_type = TREE_TYPE (t);
5709 211129 : location_t loc = EXPR_LOCATION (t);
5710 211129 : tree type = TREE_TYPE (lhs);
5711 :
5712 211129 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5713 211129 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5714 211129 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5715 : non_constant_p, overflow_p,
5716 : jump_target);
5717 211129 : if (*non_constant_p)
5718 : return NULL_TREE;
5719 211117 : if (*jump_target)
5720 : return NULL_TREE;
5721 : /* Don't fold an out-of-bound access. */
5722 211117 : if (!tree_int_cst_le (t, nelts))
5723 : return NULL_TREE;
5724 211117 : rhs = cp_fold_convert (ssizetype, rhs);
5725 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5726 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5727 211117 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5728 211117 : rhs, TYPE_SIZE_UNIT (type))))
5729 : return NULL_TREE;
5730 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5731 : as signed. */
5732 211085 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5733 211085 : TYPE_SIZE_UNIT (type));
5734 211085 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5735 211085 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5736 : t, NULL_TREE, NULL_TREE);
5737 211085 : t = cp_build_addr_expr (t, tf_warning_or_error);
5738 211085 : t = cp_fold_convert (orig_type, t);
5739 211085 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5740 : non_constant_p, overflow_p,
5741 211085 : jump_target);
5742 : }
5743 :
5744 : return NULL_TREE;
5745 : }
5746 :
5747 : /* Try to fold expressions like
5748 : (struct S *) (&a[0].D.2378 + 12)
5749 : into
5750 : &MEM <struct T> [(void *)&a + 12B]
5751 : This is something normally done by gimple_fold_stmt_to_constant_1
5752 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5753 : dereference the address because some details are lost.
5754 : For pointer comparisons we want such folding though so that
5755 : match.pd address_compare optimization works. */
5756 :
5757 : static tree
5758 12259792 : cxx_maybe_fold_addr_pointer_plus (tree t)
5759 : {
5760 24519584 : while (CONVERT_EXPR_P (t)
5761 12965482 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5762 705690 : t = TREE_OPERAND (t, 0);
5763 12259792 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5764 : return NULL_TREE;
5765 10565450 : tree op0 = TREE_OPERAND (t, 0);
5766 10565450 : tree op1 = TREE_OPERAND (t, 1);
5767 10565450 : if (TREE_CODE (op1) != INTEGER_CST)
5768 : return NULL_TREE;
5769 10565450 : while (CONVERT_EXPR_P (op0)
5770 21127352 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5771 10561902 : op0 = TREE_OPERAND (op0, 0);
5772 10565450 : if (TREE_CODE (op0) != ADDR_EXPR)
5773 : return NULL_TREE;
5774 10565450 : op1 = fold_convert (ptr_type_node, op1);
5775 10565450 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5776 10565450 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5777 : }
5778 :
5779 : /* Subroutine of cxx_eval_constant_expression.
5780 : Like cxx_eval_unary_expression, except for binary expressions. */
5781 :
5782 : static tree
5783 111722010 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5784 : value_cat lval,
5785 : bool *non_constant_p, bool *overflow_p,
5786 : tree *jump_target)
5787 : {
5788 111722010 : tree r = NULL_TREE;
5789 111722010 : tree orig_lhs = TREE_OPERAND (t, 0);
5790 111722010 : tree orig_rhs = TREE_OPERAND (t, 1);
5791 111722010 : tree lhs, rhs;
5792 :
5793 111722010 : if (TREE_CODE (t) == POINTER_PLUS_EXPR
5794 12675718 : && CONVERT_EXPR_P (orig_lhs)
5795 6713985 : && (lhs = TREE_OPERAND (orig_lhs, 0))
5796 6713985 : && INDIRECT_TYPE_P (TREE_TYPE (lhs))
5797 6713985 : && (TREE_CODE (orig_lhs) != NOP_EXPR || !REINTERPRET_CAST_P (orig_lhs))
5798 118435966 : && is_properly_derived_from (TREE_TYPE (TREE_TYPE (orig_lhs)),
5799 6713956 : TREE_TYPE (TREE_TYPE (lhs))))
5800 : /* fold_unary_loc and match.pd can optimize
5801 : (type *) (ptr p+ off) into ((type *) ptr) p+ off, which can break
5802 : static_cast constexpr diagnostics, because ptr before the p+
5803 : cast to type * can be invalid while the original valid.
5804 : Evaluate it as (type *) (ptr p+ off) instead. */
5805 : {
5806 42 : tree cast = copy_node (orig_lhs);
5807 42 : tree pplus = copy_node (t);
5808 42 : TREE_OPERAND (cast, 0) = pplus;
5809 42 : TREE_OPERAND (pplus, 0) = lhs;
5810 42 : TREE_TYPE (pplus) = TREE_TYPE (lhs);
5811 42 : lhs = cxx_eval_constant_expression (ctx, cast, vc_prvalue,
5812 : non_constant_p, overflow_p,
5813 : jump_target);
5814 42 : if (lhs != cast)
5815 : {
5816 24 : ggc_free (cast);
5817 24 : ggc_free (pplus);
5818 : }
5819 42 : if (*non_constant_p)
5820 : return t;
5821 42 : return lhs;
5822 : }
5823 :
5824 111721968 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5825 : non_constant_p, overflow_p,
5826 : jump_target);
5827 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5828 : subtraction. */
5829 111721966 : if (*non_constant_p)
5830 : return t;
5831 90111617 : if (*jump_target)
5832 : return NULL_TREE;
5833 90111522 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5834 : non_constant_p, overflow_p,
5835 : jump_target);
5836 90111522 : if (*non_constant_p)
5837 : return t;
5838 88949138 : if (*jump_target)
5839 : return NULL_TREE;
5840 :
5841 88949132 : location_t loc = EXPR_LOCATION (t);
5842 88949132 : enum tree_code code = TREE_CODE (t);
5843 88949132 : tree type = TREE_TYPE (t);
5844 :
5845 88949132 : if (code == EQ_EXPR || code == NE_EXPR)
5846 : {
5847 18486967 : bool is_code_eq = (code == EQ_EXPR);
5848 :
5849 18486967 : if (TREE_CODE (lhs) == PTRMEM_CST
5850 217 : && TREE_CODE (rhs) == PTRMEM_CST)
5851 : {
5852 76 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5853 76 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5854 76 : bool eq;
5855 76 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5856 76 : && TREE_CODE (lmem) == FIELD_DECL
5857 76 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5858 103 : && same_type_p (DECL_CONTEXT (lmem),
5859 : DECL_CONTEXT (rmem)))
5860 : /* If both refer to (possibly different) members of the same union
5861 : (12.3), they compare equal. */
5862 : eq = true;
5863 : else
5864 49 : eq = cp_tree_equal (lhs, rhs);
5865 76 : r = constant_boolean_node (eq == is_code_eq, type);
5866 76 : }
5867 18486891 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5868 18486750 : || TREE_CODE (rhs) == PTRMEM_CST)
5869 18486891 : && (null_member_pointer_value_p (lhs)
5870 141 : || null_member_pointer_value_p (rhs)))
5871 141 : r = constant_boolean_node (!is_code_eq, type);
5872 18486750 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5873 0 : lhs = cplus_expand_constant (lhs);
5874 18486750 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5875 0 : rhs = cplus_expand_constant (rhs);
5876 36973500 : else if (REFLECTION_TYPE_P (TREE_TYPE (lhs))
5877 18490706 : && REFLECTION_TYPE_P (TREE_TYPE (rhs)))
5878 : {
5879 3956 : const bool eq = compare_reflections (lhs, rhs);
5880 3956 : r = constant_boolean_node (eq == is_code_eq, type);
5881 : }
5882 : }
5883 0 : if (r == NULL_TREE
5884 88944959 : && TREE_CODE_CLASS (code) == tcc_comparison
5885 39219714 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5886 : {
5887 6129896 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5888 5136569 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5889 6129896 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5890 5428881 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5891 : }
5892 10937130 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5893 99886262 : && integer_zerop (lhs) && !integer_zerop (rhs))
5894 : {
5895 155 : if (!ctx->quiet)
5896 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5897 155 : *non_constant_p = true;
5898 155 : return t;
5899 : }
5900 88948977 : else if (code == POINTER_PLUS_EXPR)
5901 : {
5902 10936975 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5903 : overflow_p, jump_target);
5904 10936975 : if (*jump_target)
5905 : return NULL_TREE;
5906 : }
5907 78012002 : else if (code == SPACESHIP_EXPR)
5908 : {
5909 27234 : r = genericize_spaceship (loc, type, lhs, rhs);
5910 27234 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5911 27234 : overflow_p, jump_target);
5912 : }
5913 :
5914 88921743 : if (r == NULL_TREE)
5915 : {
5916 88706485 : if (ctx->manifestly_const_eval == mce_true
5917 68123933 : && (flag_constexpr_fp_except
5918 68123930 : || TREE_CODE (type) != REAL_TYPE))
5919 : {
5920 68071733 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5921 68071733 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5922 68071733 : }
5923 : else
5924 20634752 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5925 : }
5926 :
5927 88706485 : if (r == NULL_TREE
5928 9063515 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5929 100 : && TREE_CODE (lhs) == INTEGER_CST
5930 98 : && TREE_CODE (rhs) == INTEGER_CST
5931 97985258 : && wi::neg_p (wi::to_wide (rhs)))
5932 : {
5933 : /* For diagnostics and -fpermissive emulate previous behavior of
5934 : handling shifts by negative amount. */
5935 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5936 98 : if (nrhs)
5937 119 : r = fold_binary_loc (loc,
5938 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5939 : type, lhs, nrhs);
5940 : }
5941 :
5942 88921743 : if (r == NULL_TREE)
5943 : {
5944 9063417 : if (lhs == orig_lhs && rhs == orig_rhs)
5945 : r = t;
5946 : else
5947 3146183 : r = build2_loc (loc, code, type, lhs, rhs);
5948 : }
5949 79858326 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5950 433 : *non_constant_p = true;
5951 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5952 : a local array in a constexpr function. */
5953 88921743 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5954 71174568 : if (!ptr)
5955 71174568 : VERIFY_CONSTANT (r);
5956 : return r;
5957 : }
5958 :
5959 : /* Subroutine of cxx_eval_constant_expression.
5960 : Attempt to evaluate condition expressions. */
5961 :
5962 : static tree
5963 39373261 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5964 : value_cat lval,
5965 : bool *non_constant_p, bool *overflow_p,
5966 : tree *jump_target)
5967 : {
5968 39373261 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5969 : vc_prvalue,
5970 : non_constant_p, overflow_p,
5971 : jump_target);
5972 39373261 : if (*jump_target)
5973 : return NULL_TREE;
5974 39373237 : VERIFY_CONSTANT (val);
5975 37087947 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5976 : {
5977 : /* Evaluate the condition as if it was
5978 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5979 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5980 : without manifestly_const_eval even expressions or parts thereof which
5981 : will later be manifestly const_eval evaluated), otherwise fold it to
5982 : true. */
5983 1085789 : if (ctx->manifestly_const_eval == mce_unknown)
5984 : {
5985 983052 : *non_constant_p = true;
5986 983052 : return t;
5987 : }
5988 102737 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5989 : boolean_type_node);
5990 : }
5991 : /* Don't VERIFY_CONSTANT the other operands. */
5992 36104895 : const bool zero_p = integer_zerop (val);
5993 36104895 : if (zero_p)
5994 22751522 : val = TREE_OPERAND (t, 2);
5995 : else
5996 13353373 : val = TREE_OPERAND (t, 1);
5997 36104895 : if (TREE_CODE (t) == IF_STMT && !val)
5998 9916418 : val = void_node;
5999 :
6000 : /* P2564: If we aren't in immediate function context (including a manifestly
6001 : constant-evaluated expression), check any uses of immediate functions in
6002 : the arm we're discarding. But don't do this inside a call; we already
6003 : checked when parsing the function. */
6004 36104895 : if (ctx->manifestly_const_eval != mce_true
6005 5037689 : && !in_immediate_context ()
6006 4939982 : && !ctx->call
6007 36897029 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
6008 792134 : ctx->manifestly_const_eval))
6009 : {
6010 7 : *non_constant_p = true;
6011 7 : return t;
6012 : }
6013 :
6014 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
6015 : serve as the initializer for the same object as the outer TARGET_EXPR,
6016 : as in
6017 : A a = true ? A{} : A{};
6018 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
6019 36104888 : if (TREE_CODE (val) == TARGET_EXPR)
6020 5342 : val = TARGET_EXPR_INITIAL (val);
6021 36104888 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6022 36104888 : overflow_p, jump_target);
6023 : }
6024 :
6025 : /* Subroutine of cxx_eval_constant_expression.
6026 : Attempt to evaluate vector condition expressions. Unlike
6027 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
6028 : ternary arithmetics operation, where all 3 arguments have to be
6029 : evaluated as constants and then folding computes the result from
6030 : them. */
6031 :
6032 : static tree
6033 11755 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
6034 : bool *non_constant_p, bool *overflow_p,
6035 : tree *jump_target)
6036 : {
6037 11755 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6038 : vc_prvalue,
6039 : non_constant_p, overflow_p,
6040 : jump_target);
6041 11755 : if (*jump_target)
6042 : return NULL_TREE;
6043 11755 : VERIFY_CONSTANT (arg1);
6044 8227 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6045 : vc_prvalue,
6046 : non_constant_p, overflow_p,
6047 : jump_target);
6048 8227 : if (*jump_target)
6049 : return NULL_TREE;
6050 8227 : VERIFY_CONSTANT (arg2);
6051 7097 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
6052 : vc_prvalue,
6053 : non_constant_p, overflow_p,
6054 : jump_target);
6055 7097 : if (*jump_target)
6056 : return NULL_TREE;
6057 7097 : VERIFY_CONSTANT (arg3);
6058 7097 : location_t loc = EXPR_LOCATION (t);
6059 7097 : tree type = TREE_TYPE (t);
6060 7097 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
6061 7097 : if (r == NULL_TREE)
6062 : {
6063 0 : if (arg1 == TREE_OPERAND (t, 0)
6064 0 : && arg2 == TREE_OPERAND (t, 1)
6065 0 : && arg3 == TREE_OPERAND (t, 2))
6066 : r = t;
6067 : else
6068 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
6069 : }
6070 7097 : VERIFY_CONSTANT (r);
6071 : return r;
6072 : }
6073 :
6074 : /* Returns less than, equal to, or greater than zero if KEY is found to be
6075 : less than, to match, or to be greater than the constructor_elt's INDEX. */
6076 :
6077 : static int
6078 441080 : array_index_cmp (tree key, tree index)
6079 : {
6080 441080 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
6081 :
6082 441080 : switch (TREE_CODE (index))
6083 : {
6084 438136 : case INTEGER_CST:
6085 438136 : return tree_int_cst_compare (key, index);
6086 2944 : case RANGE_EXPR:
6087 2944 : {
6088 2944 : tree lo = TREE_OPERAND (index, 0);
6089 2944 : tree hi = TREE_OPERAND (index, 1);
6090 2944 : if (tree_int_cst_lt (key, lo))
6091 : return -1;
6092 2648 : else if (tree_int_cst_lt (hi, key))
6093 : return 1;
6094 : else
6095 2648 : return 0;
6096 : }
6097 0 : default:
6098 0 : gcc_unreachable ();
6099 : }
6100 : }
6101 :
6102 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
6103 : relative index OFF. */
6104 :
6105 : static tree
6106 29559 : raw_data_cst_elt (tree raw_data, unsigned int off)
6107 : {
6108 29559 : return build_int_cst (TREE_TYPE (raw_data),
6109 29559 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
6110 59118 : ? (HOST_WIDE_INT)
6111 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
6112 : : (HOST_WIDE_INT)
6113 486 : RAW_DATA_SCHAR_ELT (raw_data, off));
6114 : }
6115 :
6116 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
6117 : if none. If INSERT is true, insert a matching element rather than fail. */
6118 :
6119 : static HOST_WIDE_INT
6120 12531612 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
6121 : {
6122 12531612 : if (tree_int_cst_sgn (dindex) < 0)
6123 : return -1;
6124 :
6125 12531612 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
6126 12531612 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
6127 12531612 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
6128 :
6129 16778964 : unsigned HOST_WIDE_INT end = len;
6130 16778964 : unsigned HOST_WIDE_INT begin = 0;
6131 :
6132 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
6133 : that the same is true of the other elements and index directly. */
6134 11976615 : if (end > 0)
6135 : {
6136 11747636 : tree cindex = (*elts)[end - 1].index;
6137 11747636 : if (cindex == NULL_TREE)
6138 : {
6139 : /* Verify that if the last index is missing, all indexes
6140 : are missing and there is no RAW_DATA_CST. */
6141 21915 : if (flag_checking)
6142 220661 : for (unsigned int j = 0; j < len - 1; ++j)
6143 198746 : gcc_assert ((*elts)[j].index == NULL_TREE
6144 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
6145 21915 : if (i < end)
6146 21915 : return i;
6147 : else
6148 : {
6149 0 : begin = end;
6150 0 : if (i == end)
6151 : /* If the element is to be added right at the end,
6152 : make sure it is added with cleared index too. */
6153 4802349 : dindex = NULL_TREE;
6154 0 : else if (insert)
6155 : /* Otherwise, in order not to break the assumption
6156 : that CONSTRUCTOR either has all indexes or none,
6157 : we need to add indexes to all elements. */
6158 0 : for (unsigned int j = 0; j < len; ++j)
6159 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
6160 : }
6161 : }
6162 11725721 : else if (TREE_CODE (cindex) == INTEGER_CST
6163 11725721 : && compare_tree_int (cindex, end - 1) == 0)
6164 : {
6165 11514630 : tree value = (*elts)[end - 1].value;
6166 11514630 : if (i < end)
6167 : {
6168 7707438 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
6169 : begin = end - 1;
6170 : else
6171 7707348 : return i;
6172 : }
6173 3807192 : else if (TREE_CODE (value) == RAW_DATA_CST
6174 3839348 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
6175 32156 : + RAW_DATA_LENGTH (value)))
6176 16078 : begin = end - 1;
6177 : else
6178 3791114 : begin = end;
6179 : }
6180 : }
6181 :
6182 : /* Otherwise, find a matching index by means of a binary search. */
6183 5007131 : while (begin != end)
6184 : {
6185 361715 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
6186 361715 : constructor_elt &elt = (*elts)[middle];
6187 361715 : tree idx = elt.index;
6188 :
6189 361715 : int cmp = array_index_cmp (dindex, idx);
6190 361715 : if (cmp > 0
6191 66760 : && TREE_CODE (elt.value) == RAW_DATA_CST
6192 445523 : && wi::to_offset (dindex) < (wi::to_offset (idx)
6193 83808 : + RAW_DATA_LENGTH (elt.value)))
6194 29417 : cmp = 0;
6195 361715 : if (cmp < 0)
6196 : end = middle;
6197 194276 : else if (cmp > 0)
6198 37343 : begin = middle + 1;
6199 : else
6200 : {
6201 156933 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
6202 : {
6203 : /* We need to split the RAW_DATA_CST elt. */
6204 122 : constructor_elt e;
6205 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
6206 244 : unsigned int off = (wi::to_offset (dindex)
6207 244 : - wi::to_offset (idx)).to_uhwi ();
6208 122 : tree value = elt.value;
6209 122 : unsigned int len = RAW_DATA_LENGTH (value);
6210 122 : if (off > 1 && len >= off + 3)
6211 22 : value = copy_node (elt.value);
6212 122 : if (off)
6213 : {
6214 33 : if (off > 1)
6215 33 : RAW_DATA_LENGTH (elt.value) = off;
6216 : else
6217 0 : elt.value = raw_data_cst_elt (elt.value, 0);
6218 33 : e.index = size_binop (PLUS_EXPR, elt.index,
6219 : build_int_cst (TREE_TYPE (elt.index),
6220 : off));
6221 33 : e.value = NULL_TREE;
6222 33 : ++middle;
6223 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
6224 : }
6225 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
6226 122 : if (len >= off + 2)
6227 : {
6228 111 : e.index = (*elts)[middle].index;
6229 111 : e.index = size_binop (PLUS_EXPR, e.index,
6230 : build_one_cst (TREE_TYPE (e.index)));
6231 111 : if (len >= off + 3)
6232 : {
6233 111 : RAW_DATA_LENGTH (value) -= off + 1;
6234 111 : RAW_DATA_POINTER (value) += off + 1;
6235 111 : e.value = value;
6236 : }
6237 : else
6238 0 : e.value = raw_data_cst_elt (value, off + 1);
6239 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
6240 : }
6241 122 : return middle;
6242 : }
6243 35919 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
6244 : {
6245 : /* We need to split the range. */
6246 361 : constructor_elt e;
6247 361 : tree lo = TREE_OPERAND (idx, 0);
6248 361 : tree hi = TREE_OPERAND (idx, 1);
6249 361 : tree value = elt.value;
6250 361 : dindex = fold_convert (sizetype, dindex);
6251 361 : if (tree_int_cst_lt (lo, dindex))
6252 : {
6253 : /* There are still some lower elts; shorten the range. */
6254 194 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
6255 97 : size_one_node);
6256 97 : if (tree_int_cst_equal (lo, new_hi))
6257 : /* Only one element left, no longer a range. */
6258 42 : elt.index = lo;
6259 : else
6260 55 : TREE_OPERAND (idx, 1) = new_hi;
6261 : /* Append the element we want to insert. */
6262 97 : ++middle;
6263 97 : e.index = dindex;
6264 97 : e.value = unshare_constructor (value);
6265 97 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
6266 : }
6267 : else
6268 : /* No lower elts, the range elt is now ours. */
6269 264 : elt.index = dindex;
6270 :
6271 361 : if (tree_int_cst_lt (dindex, hi))
6272 : {
6273 : /* There are still some higher elts; append a range. */
6274 506 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
6275 253 : size_one_node);
6276 253 : if (tree_int_cst_equal (new_lo, hi))
6277 : e.index = hi;
6278 : else
6279 153 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
6280 253 : e.value = unshare_constructor (value);
6281 253 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
6282 : }
6283 : }
6284 156811 : return middle;
6285 : }
6286 : }
6287 :
6288 4645416 : if (insert)
6289 : {
6290 4514750 : constructor_elt e = { dindex, NULL_TREE };
6291 4514750 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
6292 4514750 : return end;
6293 : }
6294 :
6295 : return -1;
6296 : }
6297 :
6298 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
6299 : matching constructor_elt exists, then add one to CTOR.
6300 :
6301 : As an optimization, if POS_HINT is non-negative then it is used as a guess
6302 : for the (integer) index of the matching constructor_elt within CTOR.
6303 :
6304 : If POS_HINT is -2, it means do not insert. */
6305 :
6306 : static constructor_elt *
6307 42463301 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
6308 : {
6309 : /* Check the hint first. */
6310 4633213 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
6311 47096514 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
6312 : return CONSTRUCTOR_ELT (ctor, pos_hint);
6313 :
6314 37830100 : bool insertp = (pos_hint != -2);
6315 37830100 : tree type = TREE_TYPE (ctor);
6316 37830100 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
6317 : {
6318 27296 : gcc_assert (insertp);
6319 27296 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
6320 27296 : return &CONSTRUCTOR_ELTS (ctor)->last();
6321 : }
6322 37802804 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
6323 : {
6324 6195344 : if (TREE_CODE (index) == RANGE_EXPR)
6325 : {
6326 : /* Support for RANGE_EXPR index lookups is currently limited to
6327 : accessing an existing element via POS_HINT, or appending a new
6328 : element to the end of CTOR. ??? Support for other access
6329 : patterns may also be needed. */
6330 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
6331 3 : if (vec_safe_length (elts))
6332 : {
6333 3 : tree lo = TREE_OPERAND (index, 0);
6334 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
6335 : }
6336 3 : gcc_assert (insertp);
6337 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
6338 3 : return &elts->last();
6339 : }
6340 :
6341 6195341 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
6342 6195341 : if (i < 0)
6343 : {
6344 2711 : gcc_assert (!insertp);
6345 : return nullptr;
6346 : }
6347 6192630 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
6348 6192630 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
6349 : {
6350 : /* Split a range even if we aren't inserting new entries. */
6351 0 : gcc_assert (!insertp);
6352 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
6353 0 : gcc_assert (i >= 0);
6354 0 : cep = CONSTRUCTOR_ELT (ctor, i);
6355 : }
6356 6192630 : gcc_assert (cep->index == NULL_TREE
6357 : || TREE_CODE (cep->index) != RANGE_EXPR);
6358 : return cep;
6359 : }
6360 : else
6361 : {
6362 31607460 : gcc_assert (TREE_CODE (index) == FIELD_DECL
6363 : && (same_type_ignoring_top_level_qualifiers_p
6364 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
6365 :
6366 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
6367 : Usually we meet initializers in that order, but it is
6368 : possible for base types to be placed not in program
6369 : order. */
6370 31607460 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
6371 31607460 : unsigned HOST_WIDE_INT idx = 0;
6372 31607460 : constructor_elt *cep = NULL;
6373 :
6374 : /* Check if we're changing the active member of a union. */
6375 582944 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
6376 31987591 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
6377 12486 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
6378 : /* If the bit offset of INDEX is larger than that of the last
6379 : constructor_elt, then we can just immediately append a new
6380 : constructor_elt to the end of CTOR. */
6381 48166385 : else if (CONSTRUCTOR_NELTS (ctor)
6382 44366286 : && tree_int_cst_compare (bit_position (index),
6383 43399882 : bit_position (CONSTRUCTOR_ELTS (ctor)
6384 21699941 : ->last().index)) > 0)
6385 : {
6386 6107420 : idx = CONSTRUCTOR_NELTS (ctor);
6387 6107420 : goto insert;
6388 : }
6389 :
6390 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
6391 : appropriately. */
6392 :
6393 30289417 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
6394 4789377 : idx++, fields = DECL_CHAIN (fields))
6395 : {
6396 20381898 : if (index == cep->index)
6397 15550248 : goto found;
6398 :
6399 : /* The field we're initializing must be on the field
6400 : list. Look to see if it is present before the
6401 : field the current ELT initializes. */
6402 51188425 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
6403 46399048 : if (index == fields)
6404 42273 : goto insert;
6405 : }
6406 : /* We fell off the end of the CONSTRUCTOR, so insert a new
6407 : entry at the end. */
6408 :
6409 16057212 : insert:
6410 16057212 : if (!insertp)
6411 : return nullptr;
6412 :
6413 16057209 : {
6414 16057209 : constructor_elt ce = { index, NULL_TREE };
6415 :
6416 16057209 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
6417 16057209 : cep = CONSTRUCTOR_ELT (ctor, idx);
6418 : }
6419 42463301 : found:;
6420 :
6421 : return cep;
6422 : }
6423 : }
6424 :
6425 : /* Under the control of CTX, issue a detailed diagnostic for
6426 : an out-of-bounds subscript INDEX into the expression ARRAY. */
6427 :
6428 : static void
6429 601 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
6430 : {
6431 601 : if (!ctx->quiet)
6432 : {
6433 120 : tree arraytype = TREE_TYPE (array);
6434 :
6435 : /* Convert the unsigned array subscript to a signed integer to avoid
6436 : printing huge numbers for small negative values. */
6437 120 : tree sidx = fold_convert (ssizetype, index);
6438 120 : STRIP_ANY_LOCATION_WRAPPER (array);
6439 120 : if (DECL_P (array))
6440 : {
6441 79 : auto_diagnostic_group d;
6442 79 : if (TYPE_DOMAIN (arraytype))
6443 73 : error_at (loc, "array subscript value %qE is outside the bounds "
6444 : "of array %qD of type %qT", sidx, array, arraytype);
6445 : else
6446 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
6447 : "type %qT with unknown bounds", sidx, array, arraytype);
6448 79 : inform (DECL_SOURCE_LOCATION (array), "declared here");
6449 79 : }
6450 41 : else if (TYPE_DOMAIN (arraytype))
6451 38 : error_at (loc, "array subscript value %qE is outside the bounds "
6452 : "of array type %qT", sidx, arraytype);
6453 : else
6454 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
6455 : "with unknown bounds", sidx, arraytype);
6456 : }
6457 601 : }
6458 :
6459 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
6460 : a VECTOR_TYPE). */
6461 :
6462 : static tree
6463 21077349 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
6464 : bool *non_constant_p, bool *overflow_p,
6465 : tree *jump_target)
6466 : {
6467 21077349 : tree nelts;
6468 21077349 : if (TREE_CODE (type) == ARRAY_TYPE)
6469 : {
6470 21077349 : if (TYPE_DOMAIN (type))
6471 21077089 : nelts = array_type_nelts_top (type);
6472 : else
6473 260 : nelts = size_zero_node;
6474 : }
6475 0 : else if (VECTOR_TYPE_P (type))
6476 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
6477 : else
6478 0 : gcc_unreachable ();
6479 :
6480 : /* For VLAs, the number of elements won't be an integer constant. */
6481 21077349 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
6482 : non_constant_p, overflow_p,
6483 : jump_target);
6484 21077349 : return nelts;
6485 : }
6486 :
6487 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
6488 : STRING_CST STRING. */
6489 :
6490 : static tree
6491 1992134 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
6492 : {
6493 1992134 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
6494 1992134 : tree r;
6495 :
6496 1992134 : if (chars_per_elt == 1)
6497 1776317 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
6498 : else
6499 : {
6500 215817 : const unsigned char *ptr
6501 215817 : = ((const unsigned char *)TREE_STRING_POINTER (string)
6502 215817 : + index * chars_per_elt);
6503 215817 : r = native_interpret_expr (type, ptr, chars_per_elt);
6504 : }
6505 1992134 : return r;
6506 : }
6507 :
6508 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
6509 : subscript, diagnose any problems with it, and return the result. */
6510 :
6511 : static tree
6512 21277196 : eval_and_check_array_index (const constexpr_ctx *ctx,
6513 : tree t, bool allow_one_past,
6514 : bool *non_constant_p, bool *overflow_p,
6515 : tree *jump_target)
6516 : {
6517 21277196 : location_t loc = cp_expr_loc_or_input_loc (t);
6518 21277196 : tree ary = TREE_OPERAND (t, 0);
6519 21277196 : t = TREE_OPERAND (t, 1);
6520 21277196 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
6521 : non_constant_p, overflow_p,
6522 : jump_target);
6523 21277196 : if (*jump_target)
6524 : return NULL_TREE;
6525 21277196 : VERIFY_CONSTANT (index);
6526 :
6527 21076545 : if (!tree_fits_shwi_p (index)
6528 21076545 : || tree_int_cst_sgn (index) < 0)
6529 : {
6530 112 : diag_array_subscript (loc, ctx, ary, index);
6531 112 : *non_constant_p = true;
6532 112 : return t;
6533 : }
6534 :
6535 21076433 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
6536 : overflow_p, jump_target);
6537 21076433 : if (*jump_target)
6538 : return NULL_TREE;
6539 21076433 : VERIFY_CONSTANT (nelts);
6540 21076349 : if (allow_one_past
6541 21076349 : ? !tree_int_cst_le (index, nelts)
6542 14301834 : : !tree_int_cst_lt (index, nelts))
6543 : {
6544 489 : diag_array_subscript (loc, ctx, ary, index);
6545 489 : *non_constant_p = true;
6546 489 : return t;
6547 : }
6548 :
6549 : return index;
6550 : }
6551 :
6552 : /* Subroutine of cxx_eval_constant_expression.
6553 : Attempt to reduce a reference to an array slot. */
6554 :
6555 : static tree
6556 15673755 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
6557 : value_cat lval,
6558 : bool *non_constant_p, bool *overflow_p,
6559 : tree *jump_target)
6560 : {
6561 15673755 : tree oldary = TREE_OPERAND (t, 0);
6562 15673755 : tree ary = cxx_eval_constant_expression (ctx, oldary,
6563 : lval,
6564 : non_constant_p, overflow_p,
6565 : jump_target);
6566 15673755 : if (*non_constant_p)
6567 : return t;
6568 15430153 : if (*jump_target)
6569 : return NULL_TREE;
6570 15430153 : if (!lval
6571 8654191 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
6572 144212 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
6573 15574365 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
6574 144212 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
6575 144212 : ary = TREE_OPERAND (ary, 0);
6576 :
6577 15430153 : tree oldidx = TREE_OPERAND (t, 1);
6578 15430153 : tree index = eval_and_check_array_index (ctx, t, lval,
6579 : non_constant_p, overflow_p,
6580 : jump_target);
6581 15430153 : if (*non_constant_p)
6582 : return t;
6583 15228898 : if (*jump_target)
6584 : return NULL_TREE;
6585 :
6586 15228898 : if (lval && ary == oldary && index == oldidx)
6587 : return t;
6588 10227409 : else if (lval == vc_discard)
6589 : return t;
6590 10227409 : else if (lval)
6591 1772772 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
6592 :
6593 8454637 : unsigned len = 0, elem_nchars = 1;
6594 8454637 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
6595 8454637 : if (TREE_CODE (ary) == CONSTRUCTOR)
6596 6329383 : len = CONSTRUCTOR_NELTS (ary);
6597 2125254 : else if (TREE_CODE (ary) == STRING_CST)
6598 : {
6599 1981056 : elem_nchars = (TYPE_PRECISION (elem_type)
6600 1981056 : / TYPE_PRECISION (char_type_node));
6601 1981056 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
6602 : }
6603 144198 : else if (TREE_CODE (ary) == VECTOR_CST)
6604 : /* We don't create variable-length VECTOR_CSTs. */
6605 144198 : len = VECTOR_CST_NELTS (ary).to_constant ();
6606 : else
6607 : {
6608 : /* We can't do anything with other tree codes, so use
6609 : VERIFY_CONSTANT to complain and fail. */
6610 0 : VERIFY_CONSTANT (ary);
6611 0 : gcc_unreachable ();
6612 : }
6613 :
6614 8454637 : bool found;
6615 8454637 : HOST_WIDE_INT i = 0;
6616 8454637 : if (TREE_CODE (ary) == CONSTRUCTOR)
6617 : {
6618 6329383 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
6619 6329383 : found = (ix >= 0);
6620 6329383 : if (found)
6621 6202083 : i = ix;
6622 : }
6623 : else
6624 : {
6625 2125254 : i = tree_to_shwi (index);
6626 2125254 : found = (i < len);
6627 : }
6628 :
6629 8454637 : if (found)
6630 : {
6631 8326872 : tree r;
6632 8326872 : if (TREE_CODE (ary) == CONSTRUCTOR)
6633 : {
6634 6202083 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
6635 6202083 : if (TREE_CODE (r) == RAW_DATA_CST)
6636 : {
6637 29437 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
6638 29437 : gcc_checking_assert (ridx);
6639 29437 : unsigned int off
6640 29437 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
6641 29437 : r = raw_data_cst_elt (r, off);
6642 : }
6643 : }
6644 2124789 : else if (TREE_CODE (ary) == VECTOR_CST)
6645 144198 : r = VECTOR_CST_ELT (ary, i);
6646 : else
6647 1980591 : r = extract_string_elt (ary, elem_nchars, i);
6648 :
6649 2154226 : if (r)
6650 : /* Don't VERIFY_CONSTANT here. */
6651 : return r;
6652 :
6653 : /* Otherwise the element doesn't have a value yet. */
6654 : }
6655 :
6656 : /* Not found. */
6657 :
6658 127765 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
6659 90 : return build_constructor (elem_type, NULL);
6660 :
6661 127675 : if (TREE_CODE (ary) == CONSTRUCTOR
6662 127675 : && CONSTRUCTOR_NO_CLEARING (ary))
6663 : {
6664 : /* 'ary' is part of the aggregate initializer we're currently
6665 : building; if there's no initializer for this element yet,
6666 : that's an error. */
6667 51 : if (!ctx->quiet)
6668 16 : error ("accessing uninitialized array element");
6669 51 : *non_constant_p = true;
6670 51 : return t;
6671 : }
6672 :
6673 : /* If it's within the array bounds but doesn't have an explicit
6674 : initializer, it's initialized from {}. But use build_value_init
6675 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
6676 127624 : tree val;
6677 127624 : constexpr_ctx new_ctx;
6678 127624 : if (CP_AGGREGATE_TYPE_P (elem_type))
6679 : {
6680 504 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
6681 504 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
6682 : }
6683 : else
6684 127120 : val = build_value_init (elem_type, tf_warning_or_error);
6685 :
6686 : /* Create a new constructor only if we don't already have a suitable one. */
6687 127624 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
6688 128154 : && (!ctx->ctor
6689 45 : || !same_type_ignoring_top_level_qualifiers_p
6690 45 : (elem_type, TREE_TYPE (ctx->ctor))));
6691 : if (new_ctor)
6692 : {
6693 521 : new_ctx = *ctx;
6694 : /* We clear the object here. We used to replace it with T, but that
6695 : caused problems (101371, 108158); and anyway, T is the initializer,
6696 : not the target object. */
6697 521 : new_ctx.object = NULL_TREE;
6698 521 : new_ctx.ctor = build_constructor (elem_type, NULL);
6699 521 : ctx = &new_ctx;
6700 : }
6701 127624 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6702 : overflow_p, jump_target);
6703 127624 : if (new_ctor && t != ctx->ctor)
6704 500 : free_constructor (ctx->ctor);
6705 : return t;
6706 : }
6707 :
6708 : /* Subroutine of cxx_eval_constant_expression.
6709 : Attempt to reduce a field access of a value of class type. */
6710 :
6711 : static tree
6712 115868221 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
6713 : value_cat lval,
6714 : bool *non_constant_p, bool *overflow_p,
6715 : tree *jump_target)
6716 : {
6717 115868221 : unsigned HOST_WIDE_INT i;
6718 115868221 : tree field;
6719 115868221 : tree value;
6720 115868221 : tree part = TREE_OPERAND (t, 1);
6721 115868221 : tree orig_whole = TREE_OPERAND (t, 0);
6722 115868221 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6723 : lval,
6724 : non_constant_p, overflow_p,
6725 : jump_target);
6726 115868221 : if (*non_constant_p)
6727 : return t;
6728 90075842 : if (*jump_target)
6729 : return NULL_TREE;
6730 90075830 : if (INDIRECT_REF_P (whole)
6731 90075830 : && integer_zerop (TREE_OPERAND (whole, 0)))
6732 : {
6733 177 : if (!ctx->quiet)
6734 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6735 177 : *non_constant_p = true;
6736 177 : return t;
6737 : }
6738 :
6739 90075653 : if (TREE_CODE (whole) == PTRMEM_CST)
6740 1667 : whole = cplus_expand_constant (whole);
6741 90075653 : if (whole == orig_whole)
6742 : return t;
6743 61130173 : if (lval == vc_discard)
6744 : return t;
6745 61130125 : if (lval)
6746 21705926 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6747 : whole, part, NULL_TREE);
6748 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6749 : CONSTRUCTOR. */
6750 39424199 : if (TREE_CODE (whole) != CONSTRUCTOR)
6751 : {
6752 0 : if (!ctx->quiet)
6753 0 : error ("%qE is not a constant expression", orig_whole);
6754 0 : *non_constant_p = true;
6755 0 : return t;
6756 : }
6757 39422704 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6758 39425876 : && DECL_MUTABLE_P (part))
6759 : {
6760 144 : if (!ctx->quiet)
6761 16 : error ("mutable %qD is not usable in a constant expression", part);
6762 144 : *non_constant_p = true;
6763 144 : return t;
6764 : }
6765 :
6766 39424055 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6767 59240675 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6768 : {
6769 : /* Use name match for PMF fields, as a variant will have a
6770 : different FIELD_DECL with a different type. */
6771 59014640 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6772 : : field == part)
6773 : {
6774 39198020 : if (value == void_node)
6775 6 : goto uninit;
6776 39198014 : if (value)
6777 : {
6778 39197996 : STRIP_ANY_LOCATION_WRAPPER (value);
6779 39197996 : return value;
6780 : }
6781 : else
6782 : /* We're in the middle of initializing it. */
6783 : break;
6784 : }
6785 : }
6786 226053 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6787 : {
6788 112 : if (CONSTRUCTOR_NELTS (whole) > 0)
6789 : {
6790 : /* DR 1188 says we don't have to deal with this. */
6791 97 : if (!ctx->quiet)
6792 : {
6793 20 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6794 20 : if (cep->value == NULL_TREE)
6795 9 : error ("accessing uninitialized member %qD", part);
6796 : else
6797 11 : error ("accessing %qD member instead of active %qD member "
6798 : "in constant expression", part, cep->index);
6799 : }
6800 97 : *non_constant_p = true;
6801 97 : return t;
6802 : }
6803 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6804 : {
6805 : /* Value-initialized union, check if looking at the first member. */
6806 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6807 12 : if (first != part)
6808 : {
6809 9 : if (!ctx->quiet)
6810 3 : error ("accessing %qD member instead of initialized %qD "
6811 : "member in constant expression", part, first);
6812 9 : *non_constant_p = true;
6813 9 : return t;
6814 : }
6815 : }
6816 : }
6817 :
6818 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6819 : classes never get represented; throw together a value now. */
6820 225947 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6821 209353 : return build_constructor (TREE_TYPE (t), NULL);
6822 :
6823 16594 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6824 :
6825 16594 : if (CONSTRUCTOR_NO_CLEARING (whole))
6826 : {
6827 112 : uninit:
6828 : /* 'whole' is part of the aggregate initializer we're currently
6829 : building; if there's no initializer for this member yet, that's an
6830 : error. */
6831 118 : if (!ctx->quiet)
6832 23 : error ("accessing uninitialized member %qD", part);
6833 118 : *non_constant_p = true;
6834 118 : return t;
6835 : }
6836 :
6837 : /* If there's no explicit init for this field, it's value-initialized. */
6838 :
6839 16482 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6840 : {
6841 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6842 : and copy the flags. */
6843 15869 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6844 15869 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6845 15869 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6846 15869 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6847 15869 : return value;
6848 : }
6849 :
6850 613 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6851 613 : return cxx_eval_constant_expression (ctx, value,
6852 : lval,
6853 : non_constant_p, overflow_p,
6854 613 : jump_target);
6855 : }
6856 :
6857 : /* Subroutine of cxx_eval_constant_expression.
6858 : Attempt to reduce a field access of a value of class type that is
6859 : expressed as a BIT_FIELD_REF. */
6860 :
6861 : static tree
6862 40745 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6863 : value_cat lval,
6864 : bool *non_constant_p, bool *overflow_p,
6865 : tree *jump_target)
6866 : {
6867 40745 : tree orig_whole = TREE_OPERAND (t, 0);
6868 40745 : tree retval, fldval, utype, mask;
6869 40745 : bool fld_seen = false;
6870 40745 : HOST_WIDE_INT istart, isize;
6871 40745 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6872 : lval,
6873 : non_constant_p, overflow_p,
6874 : jump_target);
6875 40745 : tree start, field, value;
6876 40745 : unsigned HOST_WIDE_INT i;
6877 :
6878 40745 : if (*jump_target)
6879 : return NULL_TREE;
6880 40745 : if (whole == orig_whole)
6881 : return t;
6882 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6883 : CONSTRUCTOR. */
6884 40596 : if (!*non_constant_p
6885 40596 : && TREE_CODE (whole) != VECTOR_CST
6886 0 : && TREE_CODE (whole) != CONSTRUCTOR)
6887 : {
6888 0 : if (!ctx->quiet)
6889 0 : error ("%qE is not a constant expression", orig_whole);
6890 0 : *non_constant_p = true;
6891 : }
6892 40596 : if (*non_constant_p)
6893 : return t;
6894 :
6895 40596 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6896 : {
6897 40596 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6898 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6899 : return r;
6900 0 : if (!ctx->quiet)
6901 0 : error ("%qE is not a constant expression", orig_whole);
6902 0 : *non_constant_p = true;
6903 0 : return t;
6904 : }
6905 :
6906 0 : start = TREE_OPERAND (t, 2);
6907 0 : istart = tree_to_shwi (start);
6908 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6909 0 : utype = TREE_TYPE (t);
6910 0 : if (!TYPE_UNSIGNED (utype))
6911 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6912 0 : retval = build_int_cst (utype, 0);
6913 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6914 : {
6915 0 : tree bitpos = bit_position (field);
6916 0 : STRIP_ANY_LOCATION_WRAPPER (value);
6917 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6918 : return value;
6919 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6920 0 : && TREE_CODE (value) == INTEGER_CST
6921 0 : && tree_fits_shwi_p (bitpos)
6922 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
6923 : {
6924 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6925 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6926 0 : HOST_WIDE_INT shift;
6927 0 : if (bit >= istart && bit + sz <= istart + isize)
6928 : {
6929 0 : fldval = fold_convert (utype, value);
6930 0 : mask = build_int_cst_type (utype, -1);
6931 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6932 : size_int (TYPE_PRECISION (utype) - sz));
6933 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6934 : size_int (TYPE_PRECISION (utype) - sz));
6935 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6936 0 : shift = bit - istart;
6937 0 : if (BYTES_BIG_ENDIAN)
6938 : shift = TYPE_PRECISION (utype) - shift - sz;
6939 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6940 : size_int (shift));
6941 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6942 0 : fld_seen = true;
6943 : }
6944 : }
6945 : }
6946 0 : if (fld_seen)
6947 0 : return fold_convert (TREE_TYPE (t), retval);
6948 0 : gcc_unreachable ();
6949 : return error_mark_node;
6950 : }
6951 :
6952 : /* Helper for cxx_eval_bit_cast.
6953 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6954 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6955 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6956 : data members of reference or std::meta::info type. */
6957 :
6958 : static bool
6959 56312 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6960 : tree orig_type)
6961 : {
6962 57312 : if (TREE_CODE (type) == UNION_TYPE)
6963 : {
6964 63 : if (!ctx->quiet)
6965 : {
6966 21 : if (type == orig_type)
6967 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6968 : "a union type", "__builtin_bit_cast", type);
6969 : else
6970 15 : error_at (loc, "%qs is not a constant expression because %qT "
6971 : "contains a union type", "__builtin_bit_cast",
6972 : orig_type);
6973 : }
6974 : return true;
6975 : }
6976 : if (TREE_CODE (type) == POINTER_TYPE)
6977 : {
6978 57 : if (!ctx->quiet)
6979 : {
6980 18 : if (type == orig_type)
6981 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6982 : "a pointer type", "__builtin_bit_cast", type);
6983 : else
6984 12 : error_at (loc, "%qs is not a constant expression because %qT "
6985 : "contains a pointer type", "__builtin_bit_cast",
6986 : orig_type);
6987 : }
6988 : return true;
6989 : }
6990 : if (TREE_CODE (type) == REFERENCE_TYPE)
6991 : {
6992 0 : if (!ctx->quiet)
6993 : {
6994 0 : if (type == orig_type)
6995 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6996 : "a reference type", "__builtin_bit_cast", type);
6997 : else
6998 0 : error_at (loc, "%qs is not a constant expression because %qT "
6999 : "contains a reference type", "__builtin_bit_cast",
7000 : orig_type);
7001 : }
7002 : return true;
7003 : }
7004 39316 : if (TYPE_PTRMEM_P (type))
7005 : {
7006 36 : if (!ctx->quiet)
7007 : {
7008 12 : if (type == orig_type)
7009 12 : error_at (loc, "%qs is not a constant expression because %qT is "
7010 : "a pointer to member type", "__builtin_bit_cast",
7011 : type);
7012 : else
7013 0 : error_at (loc, "%qs is not a constant expression because %qT "
7014 : "contains a pointer to member type",
7015 : "__builtin_bit_cast", orig_type);
7016 : }
7017 : return true;
7018 : }
7019 57156 : if (TYPE_VOLATILE (type))
7020 : {
7021 0 : if (!ctx->quiet)
7022 : {
7023 0 : if (type == orig_type)
7024 0 : error_at (loc, "%qs is not a constant expression because %qT is "
7025 : "volatile", "__builtin_bit_cast", type);
7026 : else
7027 0 : error_at (loc, "%qs is not a constant expression because %qT "
7028 : "contains a volatile subobject",
7029 : "__builtin_bit_cast", orig_type);
7030 : }
7031 : return true;
7032 : }
7033 57156 : if (REFLECTION_TYPE_P (type))
7034 : {
7035 22 : if (!ctx->quiet)
7036 : {
7037 6 : if (type == orig_type)
7038 4 : error_at (loc, "%qs is not a constant expression because its "
7039 : "type is %qs", "__builtin_bit_cast", "std::meta::info");
7040 : else
7041 2 : error_at (loc, "%qs is not a constant expression because %qT "
7042 : "contains %qs type",
7043 : "__builtin_bit_cast", orig_type, "std::meta::info");
7044 : }
7045 : return true;
7046 : }
7047 57134 : if (TREE_CODE (type) == RECORD_TYPE)
7048 2294092 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7049 2254890 : if (TREE_CODE (field) == FIELD_DECL
7050 2254890 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
7051 : return true;
7052 57038 : if (TREE_CODE (type) == ARRAY_TYPE)
7053 1000 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
7054 : return false;
7055 : }
7056 :
7057 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
7058 : std::byte members of CONSTRUCTOR (recursively) if they contain
7059 : some indeterminate bits (as set in MASK), remove the ctor elts,
7060 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
7061 : bits in MASK. */
7062 :
7063 : static void
7064 9631 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
7065 : {
7066 9631 : if (TREE_CODE (t) != CONSTRUCTOR)
7067 : return;
7068 :
7069 : unsigned i, j = 0;
7070 : tree index, value;
7071 23564 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
7072 : {
7073 13933 : tree type = TREE_TYPE (value);
7074 13933 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7075 27427 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
7076 : {
7077 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
7078 : {
7079 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
7080 135 : gcc_assert (fldsz != 0);
7081 135 : HOST_WIDE_INT pos = int_byte_position (index);
7082 135 : HOST_WIDE_INT bpos
7083 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
7084 135 : bpos %= BITS_PER_UNIT;
7085 135 : HOST_WIDE_INT end
7086 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
7087 135 : gcc_assert (end == 1 || end == 2);
7088 135 : unsigned char *p = mask + pos;
7089 135 : unsigned char mask_save[2];
7090 135 : mask_save[0] = mask[pos];
7091 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
7092 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7093 : sorry_at (loc, "PDP11 bit-field handling unsupported"
7094 : " in %qs", "__builtin_bit_cast");
7095 135 : else if (BYTES_BIG_ENDIAN)
7096 : {
7097 : /* Big endian. */
7098 : if (bpos + fldsz <= BITS_PER_UNIT)
7099 : *p &= ~(((1 << fldsz) - 1)
7100 : << (BITS_PER_UNIT - bpos - fldsz));
7101 : else
7102 : {
7103 : gcc_assert (bpos);
7104 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
7105 : p++;
7106 : fldsz -= BITS_PER_UNIT - bpos;
7107 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
7108 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
7109 : }
7110 : }
7111 : else
7112 : {
7113 : /* Little endian. */
7114 135 : if (bpos + fldsz <= BITS_PER_UNIT)
7115 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
7116 : else
7117 : {
7118 0 : gcc_assert (bpos);
7119 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
7120 0 : p++;
7121 0 : fldsz -= BITS_PER_UNIT - bpos;
7122 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
7123 0 : *p &= ~((1 << fldsz) - 1);
7124 : }
7125 : }
7126 135 : if (mask_save[0] != mask[pos]
7127 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
7128 : {
7129 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
7130 36 : continue;
7131 : }
7132 : }
7133 : }
7134 13663 : else if (is_byte_access_type_not_plain_char (type))
7135 : {
7136 228 : HOST_WIDE_INT pos;
7137 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7138 132 : pos = tree_to_shwi (index);
7139 : else
7140 96 : pos = int_byte_position (index);
7141 228 : if (mask[pos])
7142 : {
7143 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
7144 48 : mask[pos] = 0;
7145 48 : continue;
7146 : }
7147 : }
7148 13849 : if (TREE_CODE (value) == CONSTRUCTOR)
7149 : {
7150 7292 : HOST_WIDE_INT pos;
7151 7292 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7152 144 : pos = tree_to_shwi (index)
7153 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
7154 : else
7155 7220 : pos = int_byte_position (index);
7156 7292 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
7157 : }
7158 13849 : if (i != j)
7159 : {
7160 180 : CONSTRUCTOR_ELT (t, j)->index = index;
7161 180 : CONSTRUCTOR_ELT (t, j)->value = value;
7162 : }
7163 13849 : ++j;
7164 : }
7165 19262 : if (CONSTRUCTOR_NELTS (t) != j)
7166 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
7167 : }
7168 :
7169 : /* Subroutine of cxx_eval_constant_expression.
7170 : Attempt to evaluate a BIT_CAST_EXPR. */
7171 :
7172 : static tree
7173 4047 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
7174 : bool *overflow_p, tree *jump_target)
7175 : {
7176 4047 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7177 4047 : TREE_TYPE (t))
7178 12767 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
7179 3950 : EXPR_LOCATION (t)),
7180 3950 : TREE_TYPE (TREE_OPERAND (t, 0)),
7181 3950 : TREE_TYPE (TREE_OPERAND (t, 0))))
7182 : {
7183 178 : *non_constant_p = true;
7184 178 : return t;
7185 : }
7186 :
7187 3869 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
7188 : non_constant_p, overflow_p,
7189 : jump_target);
7190 3869 : if (*non_constant_p)
7191 : return t;
7192 2820 : if (*jump_target)
7193 : return NULL_TREE;
7194 :
7195 2820 : location_t loc = EXPR_LOCATION (t);
7196 2820 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
7197 : {
7198 : if (!ctx->quiet)
7199 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
7200 : "__builtin_bit_cast");
7201 : *non_constant_p = true;
7202 : return t;
7203 : }
7204 :
7205 2820 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
7206 : {
7207 0 : if (!ctx->quiet)
7208 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7209 : "type is too large", "__builtin_bit_cast");
7210 0 : *non_constant_p = true;
7211 0 : return t;
7212 : }
7213 :
7214 2820 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
7215 2820 : if (len < 0 || (int) len != len)
7216 : {
7217 0 : if (!ctx->quiet)
7218 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7219 : "type is too large", "__builtin_bit_cast");
7220 0 : *non_constant_p = true;
7221 0 : return t;
7222 : }
7223 :
7224 2820 : unsigned char buf[64];
7225 2820 : unsigned char *ptr, *mask;
7226 2820 : size_t alen = (size_t) len * 2;
7227 2820 : if (alen <= sizeof (buf))
7228 : ptr = buf;
7229 : else
7230 329 : ptr = XNEWVEC (unsigned char, alen);
7231 2820 : mask = ptr + (size_t) len;
7232 : /* At the beginning consider everything indeterminate. */
7233 2820 : memset (mask, ~0, (size_t) len);
7234 :
7235 2820 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
7236 : {
7237 0 : if (!ctx->quiet)
7238 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7239 : "argument cannot be encoded", "__builtin_bit_cast");
7240 0 : *non_constant_p = true;
7241 0 : if (ptr != buf)
7242 0 : XDELETE (ptr);
7243 : return t;
7244 : }
7245 :
7246 2820 : tree r = NULL_TREE;
7247 2820 : if (can_native_interpret_type_p (TREE_TYPE (t)))
7248 : {
7249 481 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
7250 481 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
7251 : {
7252 52 : gcc_assert (len == 1);
7253 52 : if (mask[0])
7254 : {
7255 27 : memset (mask, 0, len);
7256 27 : r = build_constructor (TREE_TYPE (r), NULL);
7257 27 : CONSTRUCTOR_NO_CLEARING (r) = 1;
7258 : }
7259 : }
7260 : }
7261 2339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
7262 : {
7263 2339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
7264 2339 : if (r != NULL_TREE)
7265 : {
7266 2339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
7267 2339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
7268 2339 : if (CHECKING_P)
7269 : {
7270 2339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
7271 : non_constant_p, overflow_p,
7272 : jump_target);
7273 2339 : gcc_checking_assert (e == r && !*jump_target);
7274 : r = e;
7275 : }
7276 : }
7277 : }
7278 :
7279 2820 : if (r != NULL_TREE)
7280 : {
7281 92041 : for (int i = 0; i < len; i++)
7282 89311 : if (mask[i])
7283 : {
7284 90 : if (!ctx->quiet)
7285 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
7286 : "__builtin_bit_cast", i);
7287 90 : *non_constant_p = true;
7288 90 : r = t;
7289 90 : break;
7290 : }
7291 2820 : if (ptr != buf)
7292 329 : XDELETE (ptr);
7293 : return r;
7294 : }
7295 :
7296 0 : if (!ctx->quiet)
7297 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7298 : "argument cannot be interpreted", "__builtin_bit_cast");
7299 0 : *non_constant_p = true;
7300 0 : if (ptr != buf)
7301 0 : XDELETE (ptr);
7302 : return t;
7303 : }
7304 :
7305 : /* Subroutine of cxx_eval_constant_expression.
7306 : Evaluate a short-circuited logical expression T in the context
7307 : of a given constexpr CALL. BAILOUT_VALUE is the value for
7308 : early return. CONTINUE_VALUE is used here purely for
7309 : sanity check purposes. */
7310 :
7311 : static tree
7312 18580718 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
7313 : tree bailout_value, tree continue_value,
7314 : bool *non_constant_p, bool *overflow_p,
7315 : tree *jump_target)
7316 : {
7317 18580718 : tree r;
7318 18580718 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7319 : vc_prvalue, non_constant_p,
7320 : overflow_p, jump_target);
7321 18580716 : if (*jump_target)
7322 : return NULL_TREE;
7323 18580707 : VERIFY_CONSTANT (lhs);
7324 13840003 : if (tree_int_cst_equal (lhs, bailout_value))
7325 : return lhs;
7326 11783267 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
7327 11783267 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
7328 : vc_prvalue, non_constant_p,
7329 : overflow_p, jump_target);
7330 11783267 : if (*jump_target)
7331 : return NULL_TREE;
7332 11783267 : VERIFY_CONSTANT (r);
7333 : return r;
7334 : }
7335 :
7336 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
7337 : CONSTRUCTOR elements to initialize (part of) an object containing that
7338 : field. Return a pointer to the constructor_elt corresponding to the
7339 : initialization of the field. */
7340 :
7341 : static constructor_elt *
7342 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
7343 : {
7344 0 : tree aggr = TREE_OPERAND (ref, 0);
7345 0 : tree field = TREE_OPERAND (ref, 1);
7346 0 : HOST_WIDE_INT i;
7347 0 : constructor_elt *ce;
7348 :
7349 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
7350 :
7351 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
7352 : {
7353 0 : constructor_elt *base_ce
7354 0 : = base_field_constructor_elt (v, aggr);
7355 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
7356 : }
7357 :
7358 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7359 0 : if (ce->index == field)
7360 0 : return ce;
7361 :
7362 0 : gcc_unreachable ();
7363 : return NULL;
7364 : }
7365 :
7366 : /* Some of the expressions fed to the constexpr mechanism are calls to
7367 : constructors, which have type void. In that case, return the type being
7368 : initialized by the constructor. */
7369 :
7370 : static tree
7371 606143121 : initialized_type (tree t)
7372 : {
7373 607805318 : if (TYPE_P (t))
7374 : return t;
7375 607804854 : tree type = TREE_TYPE (t);
7376 607804854 : if (TREE_CODE (t) == CALL_EXPR)
7377 : {
7378 : /* A constructor call has void type, so we need to look deeper. */
7379 73254122 : tree fn = get_function_named_in_call (t);
7380 73246973 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
7381 146376837 : && DECL_CXX_CONSTRUCTOR_P (fn))
7382 4051537 : type = DECL_CONTEXT (fn);
7383 : }
7384 534550732 : else if (TREE_CODE (t) == COMPOUND_EXPR)
7385 1662197 : return initialized_type (TREE_OPERAND (t, 1));
7386 532888535 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
7387 873296 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
7388 606142657 : return cv_unqualified (type);
7389 : }
7390 :
7391 : /* We're about to initialize element INDEX of an array or class from VALUE.
7392 : Set up NEW_CTX appropriately by adjusting .object to refer to the
7393 : subobject and creating a new CONSTRUCTOR if the element is itself
7394 : a class or array. */
7395 :
7396 : static void
7397 4180752 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
7398 : tree index, tree &value)
7399 : {
7400 4180752 : new_ctx = *ctx;
7401 :
7402 4180752 : if (index && TREE_CODE (index) != INTEGER_CST
7403 3743430 : && TREE_CODE (index) != FIELD_DECL
7404 3 : && TREE_CODE (index) != RANGE_EXPR)
7405 : /* This won't have an element in the new CONSTRUCTOR. */
7406 : return;
7407 :
7408 4180752 : tree type = initialized_type (value);
7409 4180752 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
7410 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
7411 : return;
7412 :
7413 1682977 : tree ctxtype = NULL_TREE;
7414 1682977 : if (ctx->ctor)
7415 1682960 : ctxtype = TREE_TYPE (ctx->ctor);
7416 17 : else if (ctx->object)
7417 14 : ctxtype = TREE_TYPE (ctx->object);
7418 : else
7419 : {
7420 : /* This can happen if the enclosing object is also an empty subobject
7421 : (c++/125315). */
7422 3 : gcc_checking_assert (is_empty_field (index));
7423 : return;
7424 : }
7425 :
7426 1682974 : if (VECTOR_TYPE_P (type)
7427 7417 : && VECTOR_TYPE_P (ctxtype)
7428 2596 : && index == NULL_TREE)
7429 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
7430 : vector is constructed from smaller vectors, doesn't get its own
7431 : CONSTRUCTOR either. */
7432 : return;
7433 :
7434 : /* The sub-aggregate initializer might contain a placeholder;
7435 : update object to refer to the subobject and ctor to refer to
7436 : the (newly created) sub-initializer. */
7437 1680378 : if (ctx->object)
7438 : {
7439 1520093 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
7440 : /* There's no well-defined subobject for this index. */
7441 3 : new_ctx.object = NULL_TREE;
7442 : else
7443 1520090 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
7444 : }
7445 :
7446 1680378 : if (is_empty_field (index)
7447 1680378 : && TREE_CODE (ctxtype) != UNION_TYPE)
7448 : /* Leave ctor null for an empty subobject of a non-union class, they aren't
7449 : represented in the result of evaluation. */
7450 1503630 : new_ctx.ctor = NULL_TREE;
7451 : else
7452 : {
7453 176748 : tree elt = build_constructor (type, NULL);
7454 176748 : CONSTRUCTOR_NO_CLEARING (elt) = true;
7455 176748 : new_ctx.ctor = elt;
7456 : }
7457 :
7458 1680378 : if (TREE_CODE (value) == TARGET_EXPR)
7459 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
7460 121080 : value = TARGET_EXPR_INITIAL (value);
7461 : }
7462 :
7463 : /* We're about to process an initializer for a class or array TYPE. Make
7464 : sure that CTX is set up appropriately. */
7465 :
7466 : static void
7467 3372418 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
7468 : {
7469 : /* We don't bother building a ctor for an empty base subobject. */
7470 3372418 : if (is_empty_class (type))
7471 : return;
7472 :
7473 : /* We're in the middle of an initializer that might involve placeholders;
7474 : our caller should have created a CONSTRUCTOR for us to put the
7475 : initializer into. We will either return that constructor or T. */
7476 1886588 : gcc_assert (ctx->ctor);
7477 1886586 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7478 : (type, TREE_TYPE (ctx->ctor)));
7479 : /* We used to check that ctx->ctor was empty, but that isn't the case when
7480 : the object is zero-initialized before calling the constructor. */
7481 1886586 : if (ctx->object)
7482 : {
7483 1670863 : tree otype = TREE_TYPE (ctx->object);
7484 1670863 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
7485 : /* Handle flexible array members. */
7486 : || (TREE_CODE (otype) == ARRAY_TYPE
7487 : && TYPE_DOMAIN (otype) == NULL_TREE
7488 : && TREE_CODE (type) == ARRAY_TYPE
7489 : && (same_type_ignoring_top_level_qualifiers_p
7490 : (TREE_TYPE (type), TREE_TYPE (otype)))));
7491 : }
7492 1886586 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
7493 : || ctx->global->get_value (ctx->object) == ctx->ctor);
7494 : }
7495 :
7496 : /* Subroutine of cxx_eval_constant_expression.
7497 : The expression tree T denotes a C-style array or a C-style
7498 : aggregate. Reduce it to a constant expression. */
7499 :
7500 : static tree
7501 3371502 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
7502 : value_cat lval,
7503 : bool *non_constant_p, bool *overflow_p,
7504 : tree *jump_target)
7505 : {
7506 3371502 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
7507 3371502 : bool changed = false;
7508 3371502 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
7509 3371502 : tree type = TREE_TYPE (t);
7510 :
7511 3371502 : constexpr_ctx new_ctx;
7512 3371502 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
7513 : {
7514 : /* We don't really need the ctx->ctor business for a PMF or
7515 : vector, but it's simpler to use the same code. */
7516 56032 : new_ctx = *ctx;
7517 56032 : new_ctx.ctor = build_constructor (type, NULL);
7518 56032 : new_ctx.object = NULL_TREE;
7519 56032 : ctx = &new_ctx;
7520 3371502 : };
7521 3371502 : verify_ctor_sanity (ctx, type);
7522 3371500 : vec<constructor_elt, va_gc> **p = nullptr;
7523 3371500 : if (ctx->ctor)
7524 : {
7525 3371483 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
7526 6739495 : vec_alloc (*p, vec_safe_length (v));
7527 3371483 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
7528 16085 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
7529 : }
7530 :
7531 3371500 : unsigned i;
7532 3371500 : tree index, value;
7533 3371500 : bool constant_p = true;
7534 3371500 : bool side_effects_p = false;
7535 6823821 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
7536 : {
7537 4174303 : tree orig_value = value;
7538 4174303 : init_subob_ctx (ctx, new_ctx, index, value);
7539 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
7540 4174303 : bool no_slot = new_ctx.ctor == NULL_TREE;
7541 4174303 : int pos_hint = -1;
7542 4174303 : if (!ctx->ctor)
7543 : /* The enclosing object could be an empty subobject so we have no
7544 : CONSTRUCTOR (c++/125336). */
7545 17 : gcc_checking_assert (is_empty_class (type));
7546 4174286 : else if (new_ctx.ctor != ctx->ctor && !no_slot)
7547 : {
7548 : /* If we built a new CONSTRUCTOR, attach it now so that other
7549 : initializers can refer to it. */
7550 170608 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
7551 170608 : cep->value = new_ctx.ctor;
7552 170608 : pos_hint = cep - (*p)->begin();
7553 170608 : }
7554 4003678 : else if (TREE_CODE (type) == UNION_TYPE)
7555 : /* Otherwise if we're constructing a non-aggregate union member, set
7556 : the active union member now so that we can later detect and diagnose
7557 : if its initializer attempts to activate another member. */
7558 1093 : get_or_insert_ctor_field (ctx->ctor, index);
7559 4174303 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
7560 : lval,
7561 : non_constant_p, overflow_p,
7562 : jump_target);
7563 4174303 : if (*jump_target)
7564 : return NULL_TREE;
7565 : /* Don't VERIFY_CONSTANT here. */
7566 4174297 : if (ctx->quiet && *non_constant_p)
7567 : break;
7568 3452321 : if (elt != orig_value)
7569 1239625 : changed = true;
7570 :
7571 3452321 : if (!TREE_CONSTANT (elt))
7572 955258 : constant_p = false;
7573 3452321 : if (TREE_SIDE_EFFECTS (elt))
7574 283 : side_effects_p = true;
7575 3452321 : if (index && TREE_CODE (index) == COMPONENT_REF)
7576 : {
7577 : /* This is an initialization of a vfield inside a base
7578 : subaggregate that we already initialized; push this
7579 : initialization into the previous initialization. */
7580 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
7581 0 : inner->value = elt;
7582 0 : changed = true;
7583 0 : }
7584 3452321 : else if (no_slot)
7585 : /* This is an initializer for an empty field; now that we've
7586 : checked that it's constant, we can ignore it. */
7587 : changed = true;
7588 1949741 : else if (ctx->ctor)
7589 : {
7590 1949741 : if (TREE_CODE (type) == UNION_TYPE
7591 1949741 : && (*p)->last().index != index)
7592 : /* The initializer erroneously changed the active union member that
7593 : we're initializing. */
7594 7 : gcc_assert (*non_constant_p);
7595 : else
7596 : {
7597 : /* The initializer might have mutated the underlying CONSTRUCTOR,
7598 : so recompute the location of the target constructer_elt. */
7599 1949734 : constructor_elt *cep
7600 1949734 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
7601 1949734 : cep->value = elt;
7602 : }
7603 :
7604 : /* Adding or replacing an element might change the ctor's flags. */
7605 1949741 : TREE_CONSTANT (ctx->ctor) = constant_p;
7606 1949741 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
7607 : }
7608 : }
7609 3371494 : if (*non_constant_p)
7610 : return t;
7611 2649447 : if (!changed)
7612 : {
7613 268381 : if (VECTOR_TYPE_P (type))
7614 14431 : t = fold (t);
7615 : return t;
7616 : }
7617 2381066 : t = ctx->ctor;
7618 2381066 : if (!t)
7619 8 : t = build_constructor (type, NULL);
7620 : /* We're done building this CONSTRUCTOR, so now we can interpret an
7621 : element without an explicit initializer as value-initialized. */
7622 2381066 : CONSTRUCTOR_NO_CLEARING (t) = false;
7623 2381066 : TREE_CONSTANT (t) = constant_p;
7624 2381066 : TREE_SIDE_EFFECTS (t) = side_effects_p;
7625 2381066 : if (VECTOR_TYPE_P (type))
7626 11444 : t = fold (t);
7627 : return t;
7628 : }
7629 :
7630 : /* Subroutine of cxx_eval_constant_expression.
7631 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
7632 : initialization of a non-static data member of array type. Reduce it to a
7633 : CONSTRUCTOR.
7634 :
7635 : Note that apart from value-initialization (when VALUE_INIT is true),
7636 : this is only intended to support value-initialization and the
7637 : initializations done by defaulted constructors for classes with
7638 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
7639 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
7640 : for the copy/move constructor. */
7641 :
7642 : static tree
7643 916 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
7644 : bool value_init, value_cat lval,
7645 : bool *non_constant_p, bool *overflow_p,
7646 : tree *jump_target)
7647 : {
7648 916 : tree elttype = TREE_TYPE (atype);
7649 916 : verify_ctor_sanity (ctx, atype);
7650 916 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
7651 916 : bool pre_init = false;
7652 916 : unsigned HOST_WIDE_INT i;
7653 916 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7654 :
7655 916 : if (init && TREE_CODE (init) == CONSTRUCTOR)
7656 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
7657 0 : non_constant_p, overflow_p, jump_target);
7658 :
7659 : /* We already checked access when building the VEC_INIT_EXPR. */
7660 916 : deferring_access_check_sentinel acs (dk_deferred);
7661 :
7662 : /* For the default constructor, build up a call to the default
7663 : constructor of the element type. We only need to handle class types
7664 : here, as for a constructor to be constexpr, all members must be
7665 : initialized, which for a defaulted default constructor means they must
7666 : be of a class type with a constexpr default constructor. */
7667 916 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7668 : /* We only do this at the lowest level. */;
7669 862 : else if (value_init)
7670 : {
7671 346 : init = build_value_init (elttype, complain);
7672 346 : pre_init = true;
7673 : }
7674 516 : else if (!init)
7675 : {
7676 404 : releasing_vec argvec;
7677 404 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7678 : &argvec, elttype, LOOKUP_NORMAL,
7679 : complain);
7680 404 : init = build_aggr_init_expr (elttype, init);
7681 404 : pre_init = true;
7682 404 : }
7683 :
7684 916 : bool zeroed_out = false;
7685 916 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
7686 : {
7687 : /* We're initializing an array object that had been zero-initialized
7688 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
7689 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
7690 : initializers we append to it. */
7691 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
7692 156 : zeroed_out = true;
7693 156 : vec_safe_truncate (*p, 0);
7694 : }
7695 :
7696 916 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
7697 : overflow_p, jump_target);
7698 916 : if (*jump_target)
7699 : return NULL_TREE;
7700 916 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
7701 6686 : for (i = 0; i < max; ++i)
7702 : {
7703 6449 : tree idx = build_int_cst (size_type_node, i);
7704 6449 : tree eltinit;
7705 6449 : bool reuse = false;
7706 6449 : constexpr_ctx new_ctx;
7707 6913 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
7708 6449 : bool no_slot = new_ctx.ctor == NULL_TREE;
7709 6449 : if (new_ctx.ctor != ctx->ctor && !no_slot)
7710 : {
7711 6140 : if (zeroed_out)
7712 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
7713 6140 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
7714 : }
7715 6449 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7716 : {
7717 : /* A multidimensional array; recurse. */
7718 186 : if (value_init || init == NULL_TREE)
7719 : {
7720 176 : eltinit = NULL_TREE;
7721 176 : reuse = i == 0;
7722 : }
7723 : else
7724 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7725 186 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
7726 : value_init, lval, non_constant_p,
7727 : overflow_p, jump_target);
7728 : }
7729 6263 : else if (pre_init)
7730 : {
7731 : /* Initializing an element using value or default initialization
7732 : we just pre-built above. */
7733 5985 : if (init == void_node)
7734 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
7735 10 : return ctx->ctor;
7736 5975 : eltinit = init;
7737 5975 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7738 : /* Clarify what object is being initialized (118285). */
7739 5667 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7740 5975 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7741 : non_constant_p, overflow_p,
7742 : jump_target);
7743 5975 : reuse = i == 0;
7744 : }
7745 : else
7746 : {
7747 : /* Copying an element. */
7748 278 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7749 278 : if (!lvalue_p (init))
7750 232 : eltinit = move (eltinit);
7751 278 : eltinit = (perform_implicit_conversion_flags
7752 278 : (elttype, eltinit, complain,
7753 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7754 271 : if (CLASS_TYPE_P (elttype)
7755 271 : && new_ctx.object
7756 526 : && !error_operand_p (eltinit))
7757 : /* Clarify what object is being initialized (118285). */
7758 244 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7759 278 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7760 : non_constant_p, overflow_p,
7761 : jump_target);
7762 : }
7763 6439 : if (*jump_target)
7764 : return NULL_TREE;
7765 6439 : if (*non_constant_p)
7766 : break;
7767 6259 : if (no_slot)
7768 : {
7769 : /* This is an initializer for an empty subobject; now that we've
7770 : checked that it's constant, we can ignore it. */
7771 0 : gcc_checking_assert (i == 0);
7772 : break;
7773 : }
7774 6259 : else if (new_ctx.ctor != ctx->ctor)
7775 : {
7776 : /* We appended this element above; update the value. */
7777 5957 : gcc_assert ((*p)->last().index == idx);
7778 5957 : (*p)->last().value = eltinit;
7779 : }
7780 : else
7781 302 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7782 : /* Reuse the result of cxx_eval_constant_expression call
7783 : from the first iteration to all others if it is a constant
7784 : initializer that doesn't require relocations. */
7785 6259 : if (reuse
7786 6259 : && max > 1
7787 6259 : && (eltinit == NULL_TREE
7788 642 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7789 642 : == null_pointer_node)))
7790 : {
7791 489 : if (new_ctx.ctor != ctx->ctor)
7792 201 : eltinit = new_ctx.ctor;
7793 489 : tree range = build2 (RANGE_EXPR, size_type_node,
7794 : build_int_cst (size_type_node, 1),
7795 489 : build_int_cst (size_type_node, max - 1));
7796 489 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7797 489 : break;
7798 : }
7799 5770 : else if (i == 0)
7800 234 : vec_safe_reserve (*p, max);
7801 : }
7802 :
7803 906 : if (!*non_constant_p)
7804 : {
7805 726 : init = ctx->ctor;
7806 726 : CONSTRUCTOR_NO_CLEARING (init) = false;
7807 : }
7808 906 : return init;
7809 : }
7810 :
7811 : static tree
7812 808 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7813 : value_cat lval,
7814 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7815 : {
7816 808 : tree atype = TREE_TYPE (t);
7817 808 : tree init = VEC_INIT_EXPR_INIT (t);
7818 808 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7819 808 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7820 : ;
7821 89 : else if (CONSTRUCTOR_NELTS (init) == 0
7822 89 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7823 : {
7824 : /* Handle {} as value-init. */
7825 : init = NULL_TREE;
7826 : value_init = true;
7827 : }
7828 : else
7829 : {
7830 : /* This is a more complicated case, like needing to loop over trailing
7831 : elements; call build_vec_init and evaluate the result. */
7832 78 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7833 78 : constexpr_ctx new_ctx = *ctx;
7834 78 : if (!ctx->object)
7835 : {
7836 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7837 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7838 60 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7839 60 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7840 60 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7841 60 : ctx->global->put_value (new_ctx.object, ctor);
7842 60 : ctx = &new_ctx;
7843 : }
7844 78 : init = expand_vec_init_expr (ctx->object, t, complain);
7845 78 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7846 : overflow_p, jump_target);
7847 : }
7848 730 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7849 : lval, non_constant_p, overflow_p, jump_target);
7850 730 : if (*non_constant_p)
7851 : return t;
7852 : else
7853 576 : return r;
7854 : }
7855 :
7856 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7857 : where the desired type is an array of unknown bounds because the variable
7858 : has had its bounds deduced since the wrapping expression was created. */
7859 :
7860 : static bool
7861 448252263 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7862 : {
7863 448252263 : while (TREE_CODE (type1) == ARRAY_TYPE
7864 144762 : && TREE_CODE (type2) == ARRAY_TYPE
7865 448252267 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7866 : {
7867 2 : type1 = TREE_TYPE (type1);
7868 2 : type2 = TREE_TYPE (type2);
7869 : }
7870 448252263 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7871 : }
7872 :
7873 : /* Try to determine the currently active union member for an expression
7874 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7875 : otherwise return NULL_TREE. */
7876 :
7877 : static tree
7878 86 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7879 : {
7880 86 : constexpr_ctx new_ctx = *ctx;
7881 86 : new_ctx.quiet = true;
7882 86 : bool non_constant_p = false, overflow_p = false;
7883 86 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7884 : &non_constant_p,
7885 : &overflow_p, jump_target);
7886 86 : if (*jump_target)
7887 : return NULL_TREE;
7888 86 : if (TREE_CODE (ctor) == CONSTRUCTOR
7889 32 : && CONSTRUCTOR_NELTS (ctor) == 1
7890 20 : && CONSTRUCTOR_ELT (ctor, 0)->index
7891 106 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7892 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7893 : return NULL_TREE;
7894 : }
7895 :
7896 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7897 :
7898 : static tree
7899 15249830 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7900 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7901 : tree *jump_target)
7902 : {
7903 24822132 : tree optype = TREE_TYPE (op);
7904 24822132 : unsigned HOST_WIDE_INT const_nunits;
7905 24822132 : if (off == 0 && similar_type_p (optype, type))
7906 : return op;
7907 15245061 : else if (cxx_dialect >= cxx26
7908 9457058 : && VAR_P (op)
7909 5448680 : && DECL_VTABLE_OR_VTT_P (op)
7910 15699 : && same_type_ignoring_top_level_qualifiers_p (type,
7911 : ptrdiff_type_node)
7912 15246406 : && POINTER_TYPE_P (strip_array_types (optype)))
7913 : {
7914 : /* We often read some virtual table elements using ptrdiff_t rather
7915 : than pointer type. */
7916 1345 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7917 : strip_array_types (optype),
7918 : op, off, empty_base,
7919 : jump_target))
7920 1345 : return fold_convert (type, ret);
7921 : }
7922 15243716 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7923 15243716 : && similar_type_p (type, TREE_TYPE (optype)))
7924 : {
7925 : /* *(foo *)&complexfoo => __real__ complexfoo */
7926 0 : if (off == 0)
7927 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7928 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7929 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7930 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7931 : }
7932 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7933 15243716 : else if (VECTOR_TYPE_P (optype)
7934 0 : && similar_type_p (type, TREE_TYPE (optype))
7935 15243716 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7936 : {
7937 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7938 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7939 0 : if (off < max_offset && off % part_width == 0)
7940 : {
7941 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7942 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7943 0 : TYPE_SIZE (type), index);
7944 : }
7945 : }
7946 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7947 15243716 : else if (TREE_CODE (optype) == ARRAY_TYPE
7948 9572302 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7949 24816018 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7950 : {
7951 9572302 : tree type_domain = TYPE_DOMAIN (optype);
7952 9572302 : tree min_val = size_zero_node;
7953 9572302 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7954 9572073 : min_val = TYPE_MIN_VALUE (type_domain);
7955 9572302 : unsigned HOST_WIDE_INT el_sz
7956 9572302 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7957 9572302 : unsigned HOST_WIDE_INT idx = off / el_sz;
7958 9572302 : unsigned HOST_WIDE_INT rem = off % el_sz;
7959 9572302 : if (tree_fits_uhwi_p (min_val))
7960 : {
7961 9572302 : tree index = size_int (idx + tree_to_uhwi (min_val));
7962 9572302 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7963 : NULL_TREE, NULL_TREE);
7964 9572302 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7965 9572302 : empty_base, jump_target);
7966 : }
7967 : }
7968 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7969 5671414 : else if (TREE_CODE (optype) == RECORD_TYPE
7970 5671414 : || TREE_CODE (optype) == UNION_TYPE)
7971 : {
7972 5662840 : if (TREE_CODE (optype) == UNION_TYPE)
7973 : /* For unions prefer the currently active member. */
7974 86 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7975 : {
7976 20 : unsigned HOST_WIDE_INT el_sz
7977 20 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7978 20 : if (off < el_sz)
7979 : {
7980 20 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7981 : op, field, NULL_TREE);
7982 20 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7983 : off, empty_base,
7984 : jump_target))
7985 : return ret;
7986 : }
7987 : }
7988 :
7989 : /* Handle conversion to "as base" type. */
7990 5662826 : if (CLASS_TYPE_P (optype)
7991 11325488 : && CLASSTYPE_AS_BASE (optype) == type)
7992 : return op;
7993 :
7994 : /* Handle conversion to an empty base class, which is represented with a
7995 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7996 : which is likely to be a waste of time (109678). */
7997 5657355 : if (is_empty_class (type)
7998 5318550 : && CLASS_TYPE_P (optype)
7999 10975899 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
8000 : {
8001 2578867 : if (empty_base)
8002 2578862 : *empty_base = true;
8003 : return op;
8004 : }
8005 :
8006 3078488 : for (tree field = TYPE_FIELDS (optype);
8007 44136136 : field; field = DECL_CHAIN (field))
8008 44090236 : if (TREE_CODE (field) == FIELD_DECL
8009 3091020 : && TREE_TYPE (field) != error_mark_node
8010 47181256 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
8011 : {
8012 3091016 : tree pos = byte_position (field);
8013 3091016 : if (!tree_fits_uhwi_p (pos))
8014 0 : continue;
8015 3091016 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
8016 3091016 : unsigned HOST_WIDE_INT el_sz;
8017 3091016 : if (DECL_FIELD_IS_BASE (field)
8018 778793 : && CLASS_TYPE_P (optype)
8019 3869809 : && CLASSTYPE_VBASECLASSES (optype))
8020 7765 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
8021 : else
8022 3083251 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
8023 3091016 : if (upos <= off && off < upos + el_sz)
8024 : {
8025 3078289 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
8026 : op, field, NULL_TREE);
8027 3078289 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
8028 : off - upos,
8029 : empty_base,
8030 : jump_target))
8031 : return ret;
8032 : }
8033 : }
8034 : }
8035 :
8036 : return NULL_TREE;
8037 : }
8038 :
8039 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
8040 : match. We want to be less strict for simple *& folding; if we have a
8041 : non-const temporary that we access through a const pointer, that should
8042 : work. We handle this here rather than change fold_indirect_ref_1
8043 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
8044 : don't really make sense outside of constant expression evaluation. Also
8045 : we want to allow folding to COMPONENT_REF, which could cause trouble
8046 : with TBAA in fold_indirect_ref_1. */
8047 :
8048 : static tree
8049 196781354 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
8050 : tree op0, bool *empty_base, tree *jump_target)
8051 : {
8052 196781354 : tree sub = op0;
8053 196781354 : tree subtype;
8054 :
8055 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
8056 405308613 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
8057 528969651 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
8058 : {
8059 132995723 : if (TREE_CODE (sub) == NOP_EXPR
8060 132995723 : && REINTERPRET_CAST_P (sub))
8061 : return NULL_TREE;
8062 132995723 : sub = TREE_OPERAND (sub, 0);
8063 : }
8064 :
8065 196781354 : subtype = TREE_TYPE (sub);
8066 196781354 : if (!INDIRECT_TYPE_P (subtype))
8067 : return NULL_TREE;
8068 :
8069 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
8070 : the innermost component into the offset until it would make the
8071 : offset positive, so that cxx_fold_indirect_ref_1 can identify
8072 : more folding opportunities. */
8073 208951481 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
8074 12170176 : if (cxx_dialect >= cxx26)
8075 : {
8076 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
8077 : to access virtual base members. */
8078 7384628 : tree nobj = obj;
8079 7384628 : while (TREE_CODE (nobj) == COMPONENT_REF
8080 7420929 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
8081 36301 : nobj = TREE_OPERAND (nobj, 0);
8082 7384628 : if (nobj != obj
8083 28504 : && CLASS_TYPE_P (TREE_TYPE (nobj))
8084 7413132 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
8085 2537 : while (obj != nobj)
8086 : {
8087 1424 : tree field = TREE_OPERAND (obj, 1);
8088 1424 : tree pos = byte_position (field);
8089 1424 : off = int_const_binop (PLUS_EXPR, off, pos);
8090 1424 : obj = TREE_OPERAND (obj, 0);
8091 : }
8092 : }
8093 15238867 : while (TREE_CODE (obj) == COMPONENT_REF
8094 : /* We need to preserve union member accesses so that we can
8095 : later properly diagnose accessing the wrong member. */
8096 6368862 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
8097 21384745 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
8098 : {
8099 3253982 : tree field = TREE_OPERAND (obj, 1);
8100 3253982 : tree pos = byte_position (field);
8101 3253982 : if (integer_zerop (off) && integer_nonzerop (pos))
8102 : /* If the offset is already 0, keep going as long as the
8103 : component is at position 0. */
8104 : break;
8105 3068691 : off = int_const_binop (PLUS_EXPR, off, pos);
8106 3068691 : obj = TREE_OPERAND (obj, 0);
8107 : }
8108 12170176 : };
8109 :
8110 196781305 : if (TREE_CODE (sub) == ADDR_EXPR)
8111 : {
8112 87825966 : tree op = TREE_OPERAND (sub, 0);
8113 87825966 : tree optype = TREE_TYPE (op);
8114 :
8115 : /* *&CONST_DECL -> to the value of the const decl. */
8116 87825966 : if (TREE_CODE (op) == CONST_DECL)
8117 0 : return DECL_INITIAL (op);
8118 : /* *&p => p; make sure to handle *&"str"[cst] here. */
8119 87825966 : if (similar_type_p (optype, type))
8120 : {
8121 83974607 : tree fop = fold_read_from_constant_string (op);
8122 83974607 : if (fop)
8123 : return fop;
8124 : else
8125 83764234 : return op;
8126 : }
8127 : else
8128 : {
8129 3851359 : tree off = integer_zero_node;
8130 3851359 : canonicalize_obj_off (op, off);
8131 3851359 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
8132 : tree_to_uhwi (off), empty_base,
8133 : jump_target);
8134 : }
8135 : }
8136 108955339 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
8137 108955339 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
8138 : {
8139 8906736 : tree op00 = TREE_OPERAND (sub, 0);
8140 8906736 : tree off = TREE_OPERAND (sub, 1);
8141 :
8142 8906736 : STRIP_NOPS (op00);
8143 8906736 : if (TREE_CODE (op00) == ADDR_EXPR)
8144 : {
8145 8318817 : tree obj = TREE_OPERAND (op00, 0);
8146 8318817 : canonicalize_obj_off (obj, off);
8147 8318817 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
8148 : tree_to_uhwi (off), empty_base,
8149 : jump_target);
8150 : }
8151 : }
8152 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
8153 100048603 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
8154 100048603 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
8155 : {
8156 0 : tree type_domain;
8157 0 : tree min_val = size_zero_node;
8158 0 : tree newsub
8159 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
8160 : jump_target);
8161 0 : if (*jump_target)
8162 : return NULL_TREE;
8163 0 : if (newsub)
8164 : sub = newsub;
8165 : else
8166 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
8167 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
8168 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
8169 0 : min_val = TYPE_MIN_VALUE (type_domain);
8170 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
8171 0 : NULL_TREE);
8172 : }
8173 :
8174 : return NULL_TREE;
8175 : }
8176 :
8177 : static tree
8178 103905419 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
8179 : value_cat lval,
8180 : bool *non_constant_p, bool *overflow_p,
8181 : tree *jump_target)
8182 : {
8183 103905419 : tree orig_op0 = TREE_OPERAND (t, 0);
8184 103905419 : bool empty_base = false;
8185 :
8186 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
8187 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
8188 :
8189 103905419 : if (TREE_CODE (t) == MEM_REF
8190 103905419 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
8191 : {
8192 9 : gcc_assert (ctx->quiet);
8193 9 : *non_constant_p = true;
8194 9 : return t;
8195 : }
8196 :
8197 : /* First try to simplify it directly. */
8198 103905410 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
8199 : orig_op0, &empty_base, jump_target);
8200 103905410 : if (*jump_target)
8201 : return NULL_TREE;
8202 103905410 : if (!r)
8203 : {
8204 : /* If that didn't work, evaluate the operand first. */
8205 99956266 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
8206 : vc_prvalue, non_constant_p,
8207 : overflow_p, jump_target);
8208 99956266 : if (*jump_target)
8209 : return NULL_TREE;
8210 : /* Don't VERIFY_CONSTANT here. */
8211 99956212 : if (*non_constant_p)
8212 : return t;
8213 :
8214 79211331 : if (!lval && integer_zerop (op0))
8215 : {
8216 73 : if (!ctx->quiet)
8217 12 : error ("dereferencing a null pointer");
8218 73 : *non_constant_p = true;
8219 73 : return t;
8220 : }
8221 :
8222 79211258 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
8223 : &empty_base, jump_target);
8224 79211258 : if (*jump_target)
8225 : return NULL_TREE;
8226 79211258 : if (r == NULL_TREE)
8227 : {
8228 : /* We couldn't fold to a constant value. Make sure it's not
8229 : something we should have been able to fold. */
8230 688741 : tree sub = op0;
8231 688741 : STRIP_NOPS (sub);
8232 688741 : if (TREE_CODE (sub) == ADDR_EXPR)
8233 : {
8234 4135 : gcc_assert (!similar_type_p
8235 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
8236 : /* DR 1188 says we don't have to deal with this. */
8237 4135 : if (!ctx->quiet)
8238 : {
8239 9 : auto_diagnostic_group d;
8240 15 : error_at (cp_expr_loc_or_input_loc (t),
8241 : "accessing value of %qT object through a %qT "
8242 : "glvalue in a constant expression",
8243 9 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
8244 9 : tree ob = build_fold_indirect_ref (sub);
8245 9 : if (DECL_P (ob))
8246 : {
8247 9 : if (DECL_ARTIFICIAL (ob))
8248 2 : inform (DECL_SOURCE_LOCATION (ob),
8249 2 : "%qT object created here", TREE_TYPE (ob));
8250 : else
8251 7 : inform (DECL_SOURCE_LOCATION (ob),
8252 : "%q#D declared here", ob);
8253 : }
8254 9 : }
8255 4135 : *non_constant_p = true;
8256 4135 : return t;
8257 : }
8258 :
8259 684606 : if (lval == vc_glvalue && op0 != orig_op0)
8260 80610 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
8261 603996 : if (!lval)
8262 493328 : VERIFY_CONSTANT (t);
8263 : return t;
8264 : }
8265 : }
8266 :
8267 82471661 : r = cxx_eval_constant_expression (ctx, r,
8268 : lval, non_constant_p, overflow_p,
8269 : jump_target);
8270 82471659 : if (*jump_target)
8271 : return NULL_TREE;
8272 82471659 : if (*non_constant_p)
8273 : return t;
8274 :
8275 : /* If we're pulling out the value of an empty base, just return an empty
8276 : CONSTRUCTOR. */
8277 73493761 : if (empty_base && !lval)
8278 : {
8279 18763 : r = build_constructor (TREE_TYPE (t), NULL);
8280 18763 : TREE_CONSTANT (r) = true;
8281 : }
8282 :
8283 : return r;
8284 : }
8285 :
8286 : /* Complain about R, a DECL that is accessed outside its lifetime. */
8287 :
8288 : static void
8289 45 : outside_lifetime_error (location_t loc, tree r)
8290 : {
8291 45 : auto_diagnostic_group d;
8292 45 : if (DECL_NAME (r) == heap_deleted_identifier)
8293 : {
8294 : /* Provide a more accurate message for deleted variables. */
8295 14 : error_at (loc, "use of allocated storage after deallocation "
8296 : "in a constant expression");
8297 14 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
8298 : }
8299 : else
8300 : {
8301 31 : error_at (loc, "accessing %qE outside its lifetime", r);
8302 31 : inform (DECL_SOURCE_LOCATION (r), "declared here");
8303 : }
8304 45 : }
8305 :
8306 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
8307 : FUNDEF_P is true if we're checking a constexpr function body.
8308 : Shared between potential_constant_expression and
8309 : cxx_eval_constant_expression. */
8310 :
8311 : static void
8312 362 : non_const_var_error (location_t loc, tree r, bool fundef_p)
8313 : {
8314 362 : auto_diagnostic_group d;
8315 362 : tree type = TREE_TYPE (r);
8316 362 : if (heap_var_p (r))
8317 : {
8318 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
8319 : "storage is not usable in a constant expression"))
8320 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
8321 : return;
8322 : }
8323 359 : if (DECL_NAME (r) == heap_deleted_identifier)
8324 : {
8325 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
8326 : "deallocation in a constant expression"))
8327 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
8328 : return;
8329 : }
8330 359 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
8331 : "a constant expression", r))
8332 : return;
8333 : /* Avoid error cascade. */
8334 352 : if (DECL_INITIAL (r) == error_mark_node)
8335 : return;
8336 336 : if (DECL_DECLARED_CONSTEXPR_P (r))
8337 3 : inform (DECL_SOURCE_LOCATION (r),
8338 : "%qD used in its own initializer", r);
8339 333 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8340 : {
8341 233 : if (!CP_TYPE_CONST_P (type))
8342 204 : inform (DECL_SOURCE_LOCATION (r),
8343 : "%q#D is not const", r);
8344 29 : else if (CP_TYPE_VOLATILE_P (type))
8345 0 : inform (DECL_SOURCE_LOCATION (r),
8346 : "%q#D is volatile", r);
8347 29 : else if (!DECL_INITIAL (r)
8348 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
8349 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
8350 29 : inform (DECL_SOURCE_LOCATION (r),
8351 : "%qD was not initialized with a constant "
8352 : "expression", r);
8353 : else
8354 0 : gcc_unreachable ();
8355 : }
8356 100 : else if (TYPE_REF_P (type))
8357 9 : inform (DECL_SOURCE_LOCATION (r),
8358 : "%qD was not initialized with a constant "
8359 : "expression", r);
8360 : else
8361 : {
8362 91 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
8363 91 : inform (DECL_SOURCE_LOCATION (r),
8364 : "%qD was not declared %<constexpr%>", r);
8365 : else
8366 0 : inform (DECL_SOURCE_LOCATION (r),
8367 : "%qD does not have integral or enumeration type",
8368 : r);
8369 : }
8370 362 : }
8371 :
8372 : /* Subroutine of cxx_eval_constant_expression.
8373 : Like cxx_eval_unary_expression, except for trinary expressions. */
8374 :
8375 : static tree
8376 43640 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
8377 : value_cat lval,
8378 : bool *non_constant_p, bool *overflow_p,
8379 : tree *jump_target)
8380 : {
8381 43640 : int i;
8382 43640 : tree args[3];
8383 43640 : tree val;
8384 :
8385 168053 : for (i = 0; i < 3; i++)
8386 : {
8387 126582 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
8388 : lval,
8389 : non_constant_p, overflow_p,
8390 : jump_target);
8391 126582 : if (*jump_target)
8392 : return NULL_TREE;
8393 126582 : VERIFY_CONSTANT (args[i]);
8394 : }
8395 :
8396 41471 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
8397 : args[0], args[1], args[2]);
8398 41471 : if (val == NULL_TREE)
8399 : return t;
8400 41471 : VERIFY_CONSTANT (val);
8401 : return val;
8402 : }
8403 :
8404 : /* True if T was declared in a function declared to be constexpr, and
8405 : therefore potentially constant in C++14. */
8406 :
8407 : bool
8408 88717124 : var_in_constexpr_fn (tree t)
8409 : {
8410 88717124 : tree ctx = DECL_CONTEXT (t);
8411 88717124 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
8412 169070787 : && DECL_DECLARED_CONSTEXPR_P (ctx));
8413 : }
8414 :
8415 : /* True if a function might be constexpr: either a function that was
8416 : declared constexpr, or a C++17 lambda op(). */
8417 :
8418 : bool
8419 729733997 : maybe_constexpr_fn (tree t)
8420 : {
8421 729733997 : return (DECL_DECLARED_CONSTEXPR_P (t)
8422 223653925 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
8423 944831113 : || (flag_implicit_constexpr
8424 138 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
8425 : }
8426 :
8427 : /* True if T was declared in a function that might be constexpr: either a
8428 : function that was declared constexpr, or a C++17 lambda op(). */
8429 :
8430 : bool
8431 66303722 : var_in_maybe_constexpr_fn (tree t)
8432 : {
8433 132606793 : return (DECL_FUNCTION_SCOPE_P (t)
8434 121184470 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
8435 : }
8436 :
8437 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
8438 : build_over_call we implement trivial copy of a class with tail padding using
8439 : assignment of character arrays, which is valid in normal code, but not in
8440 : constexpr evaluation. We don't need to worry about clobbering tail padding
8441 : in constexpr evaluation, so strip the type punning. */
8442 :
8443 : static void
8444 103698805 : maybe_simplify_trivial_copy (tree &target, tree &init)
8445 : {
8446 103698805 : if (TREE_CODE (target) == MEM_REF
8447 36160 : && TREE_CODE (init) == MEM_REF
8448 35992 : && TREE_TYPE (target) == TREE_TYPE (init)
8449 35992 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
8450 103734797 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
8451 : {
8452 35992 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
8453 35992 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
8454 : }
8455 103698805 : }
8456 :
8457 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
8458 : of constant type. This does not check for 'mutable', so the
8459 : caller is expected to be mindful of that. */
8460 :
8461 : static bool
8462 419 : cref_has_const_field (tree ref)
8463 : {
8464 488 : while (TREE_CODE (ref) == COMPONENT_REF)
8465 : {
8466 479 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
8467 : return true;
8468 69 : ref = TREE_OPERAND (ref, 0);
8469 : }
8470 : return false;
8471 : }
8472 :
8473 : /* Return true if we are modifying something that is const during constant
8474 : expression evaluation. CODE is the code of the statement, OBJ is the
8475 : object in question, MUTABLE_P is true if one of the subobjects were
8476 : declared mutable. */
8477 :
8478 : static bool
8479 107475418 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
8480 : {
8481 : /* If this is initialization, there's no problem. */
8482 107475418 : if (code == INIT_EXPR)
8483 : return false;
8484 :
8485 : /* [basic.type.qualifier] "A const object is an object of type
8486 : const T or a non-mutable subobject of a const object." */
8487 30012766 : if (mutable_p)
8488 : return false;
8489 :
8490 30012015 : if (TREE_READONLY (obj))
8491 : return true;
8492 :
8493 29957212 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
8494 : {
8495 : /* Although a COMPONENT_REF may have a const type, we should
8496 : only consider it modifying a const object when any of the
8497 : field components is const. This can happen when using
8498 : constructs such as const_cast<const T &>(m), making something
8499 : const even though it wasn't declared const. */
8500 34283 : if (TREE_CODE (obj) == COMPONENT_REF)
8501 419 : return cref_has_const_field (obj);
8502 : else
8503 : return true;
8504 : }
8505 :
8506 : return false;
8507 : }
8508 :
8509 : /* Evaluate an INIT_EXPR or MODIFY_EXPR or MODOP_EXPR. MODOP_EXPR means
8510 : we are evaluating __builtin_start_lifetime (init is in the second
8511 : operand rather than third, like for INIT_EXPR and MODIFY_EXPR). */
8512 :
8513 : static tree
8514 103698805 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
8515 : value_cat lval,
8516 : bool *non_constant_p, bool *overflow_p,
8517 : tree *jump_target)
8518 : {
8519 103698805 : constexpr_ctx new_ctx = *ctx;
8520 :
8521 103698805 : tree init = TREE_OPERAND (t, 1);
8522 :
8523 : /* First we figure out where we're storing to. */
8524 103698805 : tree target = TREE_OPERAND (t, 0);
8525 :
8526 103698805 : maybe_simplify_trivial_copy (target, init);
8527 :
8528 103698805 : tree type = TREE_TYPE (target);
8529 103698805 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) != INIT_EXPR;
8530 72649425 : if (preeval && !TREE_CLOBBER_P (init) && TREE_CODE (t) != MODOP_EXPR)
8531 : {
8532 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
8533 71610110 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
8534 42182279 : && TREE_CODE (init) == CALL_EXPR
8535 6954884 : && CALL_EXPR_FN (init) == NULL_TREE
8536 71610409 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
8537 8 : return void_node;
8538 :
8539 : /* Evaluate the value to be stored without knowing what object it will be
8540 : stored in, so that any side-effects happen first. */
8541 71610102 : if (!SCALAR_TYPE_P (type))
8542 203814 : new_ctx.ctor = new_ctx.object = NULL_TREE;
8543 71610102 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8544 : non_constant_p, overflow_p,
8545 : jump_target);
8546 71610098 : if (*jump_target)
8547 : return NULL_TREE;
8548 71609202 : if (*non_constant_p)
8549 : return t;
8550 : }
8551 :
8552 94636819 : bool evaluated = false;
8553 94636819 : if (lval == vc_glvalue)
8554 : {
8555 : /* If we want to return a reference to the target, we need to evaluate it
8556 : as a whole; otherwise, only evaluate the innermost piece to avoid
8557 : building up unnecessary *_REFs. */
8558 0 : target = cxx_eval_constant_expression (ctx, target, lval,
8559 : non_constant_p, overflow_p,
8560 : jump_target);
8561 0 : evaluated = true;
8562 0 : if (*jump_target)
8563 : return NULL_TREE;
8564 0 : if (*non_constant_p)
8565 : return t;
8566 : }
8567 :
8568 : /* Find the underlying variable. */
8569 94636819 : releasing_vec refs;
8570 94636819 : tree object = NULL_TREE;
8571 : /* If we're modifying a const object, save it. */
8572 94636819 : tree const_object_being_modified = NULL_TREE;
8573 94636819 : bool mutable_p = false;
8574 : /* If we see a union, we can't ignore clobbers. */
8575 94636819 : int seen_union = 0;
8576 320068883 : for (tree probe = target; object == NULL_TREE; )
8577 : {
8578 225432529 : switch (TREE_CODE (probe))
8579 : {
8580 36159227 : case BIT_FIELD_REF:
8581 36159227 : case COMPONENT_REF:
8582 36159227 : case ARRAY_REF:
8583 36159227 : {
8584 36159227 : tree ob = TREE_OPERAND (probe, 0);
8585 36159227 : tree elt = TREE_OPERAND (probe, 1);
8586 36159227 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
8587 : mutable_p = true;
8588 36159227 : if (TREE_CODE (probe) == ARRAY_REF)
8589 : {
8590 5846019 : elt = eval_and_check_array_index (ctx, probe, false,
8591 : non_constant_p, overflow_p,
8592 : jump_target);
8593 5846019 : if (*jump_target)
8594 69 : return NULL_TREE;
8595 5846019 : if (*non_constant_p)
8596 : return t;
8597 : }
8598 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
8599 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
8600 : the array isn't const. Instead, check "a" in the next iteration;
8601 : that will detect modifying "const int a[10]". */
8602 30313208 : else if (evaluated
8603 12839064 : && modifying_const_object_p (TREE_CODE (t), probe,
8604 : mutable_p)
8605 30313822 : && const_object_being_modified == NULL_TREE)
8606 : const_object_being_modified = probe;
8607 :
8608 : /* Track named member accesses for unions to validate modifications
8609 : that change active member. */
8610 36159158 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
8611 17474144 : vec_safe_push (refs, probe);
8612 : else
8613 18685014 : vec_safe_push (refs, NULL_TREE);
8614 :
8615 36159158 : vec_safe_push (refs, elt);
8616 36159158 : vec_safe_push (refs, TREE_TYPE (probe));
8617 36159158 : probe = ob;
8618 36159158 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
8619 584642 : ++seen_union;
8620 : }
8621 36159158 : break;
8622 :
8623 30 : case REALPART_EXPR:
8624 30 : gcc_assert (refs->is_empty ());
8625 30 : vec_safe_push (refs, NULL_TREE);
8626 30 : vec_safe_push (refs, probe);
8627 30 : vec_safe_push (refs, TREE_TYPE (probe));
8628 30 : probe = TREE_OPERAND (probe, 0);
8629 30 : break;
8630 :
8631 31 : case IMAGPART_EXPR:
8632 31 : gcc_assert (refs->is_empty ());
8633 31 : vec_safe_push (refs, NULL_TREE);
8634 31 : vec_safe_push (refs, probe);
8635 31 : vec_safe_push (refs, TREE_TYPE (probe));
8636 31 : probe = TREE_OPERAND (probe, 0);
8637 31 : break;
8638 :
8639 189273241 : default:
8640 189273241 : if (evaluated)
8641 : object = probe;
8642 : else
8643 : {
8644 94636887 : tree pvar = tree_strip_any_location_wrapper (probe);
8645 94636887 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
8646 : {
8647 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
8648 : active union member. */
8649 101 : probe = DECL_VALUE_EXPR (pvar);
8650 101 : break;
8651 : }
8652 94636786 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
8653 : non_constant_p, overflow_p,
8654 : jump_target);
8655 94636786 : evaluated = true;
8656 94636786 : if (*jump_target)
8657 : return NULL_TREE;
8658 94636786 : if (*non_constant_p)
8659 : return t;
8660 : }
8661 : break;
8662 : }
8663 : }
8664 :
8665 94636354 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
8666 94636354 : && const_object_being_modified == NULL_TREE)
8667 94636354 : const_object_being_modified = object;
8668 :
8669 94636354 : if (DECL_P (object)
8670 94634288 : && TREE_CLOBBER_P (init)
8671 95698038 : && DECL_NAME (object) == heap_deleted_identifier)
8672 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
8673 : message later when operator delete is called. */
8674 15 : return void_node;
8675 :
8676 : /* And then find/build up our initializer for the path to the subobject
8677 : we're initializing. */
8678 94636339 : tree *valp;
8679 94636339 : if (DECL_P (object))
8680 94634273 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8681 : else
8682 : valp = NULL;
8683 94636339 : if (!valp)
8684 : {
8685 : /* A constant-expression cannot modify objects from outside the
8686 : constant-expression. */
8687 11643 : if (!ctx->quiet)
8688 : {
8689 31 : auto_diagnostic_group d;
8690 31 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
8691 : {
8692 0 : error ("modification of allocated storage after deallocation "
8693 : "is not a constant expression");
8694 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
8695 : }
8696 31 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
8697 : {
8698 14 : if (TREE_CLOBBER_P (init))
8699 6 : error ("destroying %qE outside its lifetime", object);
8700 : else
8701 8 : error ("modification of %qE outside its lifetime "
8702 : "is not a constant expression", object);
8703 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
8704 : }
8705 : else
8706 : {
8707 17 : if (TREE_CLOBBER_P (init))
8708 6 : error ("destroying %qE from outside current evaluation "
8709 : "is not a constant expression", object);
8710 : else
8711 11 : error ("modification of %qE from outside current evaluation "
8712 : "is not a constant expression", object);
8713 17 : if (DECL_P (object) && DECL_SOURCE_LOCATION (object))
8714 14 : inform (DECL_SOURCE_LOCATION (object), "from here");
8715 : }
8716 31 : }
8717 11643 : *non_constant_p = true;
8718 11643 : return t;
8719 : }
8720 :
8721 : /* Handle explicit end-of-lifetime. */
8722 94624696 : if (TREE_CLOBBER_P (init))
8723 : {
8724 1061618 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
8725 1061618 : && refs->is_empty ())
8726 : {
8727 300644 : ctx->global->destroy_value (object, (CLOBBER_KIND (init)
8728 : > CLOBBER_OBJECT_END));
8729 300644 : return void_node;
8730 : }
8731 :
8732 747743 : if (!seen_union && !*valp
8733 771127 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
8734 10118 : return void_node;
8735 :
8736 : /* Ending the lifetime of a const object is OK. */
8737 : const_object_being_modified = NULL_TREE;
8738 : }
8739 :
8740 94313934 : type = TREE_TYPE (object);
8741 94313934 : bool no_zero_init = true;
8742 94313934 : bool zero_padding_bits = false;
8743 :
8744 188627868 : auto_vec<tree *> ctors;
8745 188627868 : releasing_vec indexes;
8746 188627868 : auto_vec<int> index_pos_hints;
8747 94313934 : bool activated_union_member_p = false;
8748 94313934 : bool empty_base = false;
8749 130156450 : while (!refs->is_empty ())
8750 : {
8751 36129380 : if (*valp == NULL_TREE || *valp == void_node)
8752 : {
8753 2913908 : *valp = build_constructor (type, NULL);
8754 2913908 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8755 2913908 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8756 : }
8757 33215472 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8758 33215472 : TREE_CODE (*valp) == STRING_CST)
8759 : {
8760 : /* An array was initialized with a string constant, and now
8761 : we're writing into one of its elements. Explode the
8762 : single initialization into a set of element
8763 : initializations. */
8764 11375 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8765 :
8766 11375 : tree string = *valp;
8767 11375 : tree elt_type = TREE_TYPE (type);
8768 11375 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8769 11375 : / TYPE_PRECISION (char_type_node));
8770 11375 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8771 11375 : tree ary_ctor = build_constructor (type, NULL);
8772 :
8773 11375 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8774 34293 : for (unsigned ix = 0; ix != num_elts; ix++)
8775 : {
8776 11543 : constructor_elt elt =
8777 : {
8778 11543 : build_int_cst (size_type_node, ix),
8779 11543 : extract_string_elt (string, chars_per_elt, ix)
8780 11543 : };
8781 11543 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8782 : }
8783 :
8784 11375 : *valp = ary_ctor;
8785 : }
8786 :
8787 36129380 : enum tree_code code = TREE_CODE (type);
8788 36129380 : tree reftype = refs->pop();
8789 36129380 : tree index = refs->pop();
8790 36129380 : bool is_access_expr = refs->pop() != NULL_TREE;
8791 :
8792 36129380 : if (code == COMPLEX_TYPE)
8793 : {
8794 61 : if (TREE_CODE (*valp) == COMPLEX_CST)
8795 51 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8796 51 : TREE_IMAGPART (*valp));
8797 10 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8798 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8799 12 : && CONSTRUCTOR_NO_CLEARING (*valp))
8800 : {
8801 2 : tree r = build_constructor (reftype, NULL);
8802 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8803 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8804 : }
8805 61 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8806 61 : ctors.safe_push (valp);
8807 61 : vec_safe_push (indexes, index);
8808 61 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8809 61 : gcc_checking_assert (refs->is_empty ());
8810 : type = reftype;
8811 282353 : break;
8812 : }
8813 :
8814 : /* If the value of object is already zero-initialized, any new ctors for
8815 : subobjects will also be zero-initialized. Similarly with zeroing of
8816 : padding bits. */
8817 36129319 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8818 36129319 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8819 :
8820 36129319 : if (code == RECORD_TYPE && is_empty_field (index))
8821 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8822 : have no data and might have an offset lower than previously declared
8823 : fields, which confuses the middle-end. The code below will notice
8824 : that we don't have a CONSTRUCTOR for our inner target and just
8825 : return init. */
8826 : {
8827 : empty_base = true;
8828 : break;
8829 : }
8830 :
8831 : /* If a union is zero-initialized, its first non-static named data member
8832 : is zero-initialized (and therefore active). */
8833 35847027 : if (code == UNION_TYPE
8834 35847027 : && !no_zero_init
8835 35847027 : && CONSTRUCTOR_NELTS (*valp) == 0)
8836 4079 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8837 4079 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8838 :
8839 : /* Check for implicit change of active member for a union. */
8840 :
8841 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8842 : now allow initializing an array element to activate the array. */
8843 35899343 : auto only_array_refs = [](const releasing_vec &refs)
8844 : {
8845 52321 : for (unsigned i = 1; i < refs->length(); i += 3)
8846 35 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8847 : return false;
8848 : return true;
8849 : };
8850 :
8851 35847027 : if (code == UNION_TYPE
8852 582598 : && (CONSTRUCTOR_NELTS (*valp) == 0
8853 381899 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8854 : /* An INIT_EXPR of the last member in an access chain is always OK,
8855 : but still check implicit change of members earlier on; see
8856 : cpp2a/constexpr-union6.C. */
8857 36060206 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8858 : {
8859 160893 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8860 160893 : tree inner = strip_array_types (reftype);
8861 :
8862 160893 : if (has_active_member && cxx_dialect < cxx20)
8863 : {
8864 58 : if (!ctx->quiet)
8865 19 : error_at (cp_expr_loc_or_input_loc (t),
8866 : "change of the active member of a union "
8867 : "from %qD to %qD is not a constant expression "
8868 : "before C++20",
8869 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8870 : index);
8871 58 : *non_constant_p = true;
8872 : }
8873 160835 : else if ((!is_access_expr
8874 47098 : || (TREE_CLOBBER_P (init)
8875 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8876 47098 : || (TREE_CODE (t) != INIT_EXPR
8877 47086 : && CLASS_TYPE_P (inner)
8878 482 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8879 160920 : && TREE_CODE (t) != MODOP_EXPR)
8880 : {
8881 : /* Diagnose changing active union member after initialization
8882 : without a valid member access expression, as described in
8883 : [class.union.general] p5. */
8884 113746 : if (!ctx->quiet)
8885 : {
8886 33 : auto_diagnostic_group d;
8887 33 : if (has_active_member)
8888 24 : error_at (cp_expr_loc_or_input_loc (t),
8889 : "accessing %qD member instead of initialized "
8890 : "%qD member in constant expression",
8891 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8892 : else
8893 9 : error_at (cp_expr_loc_or_input_loc (t),
8894 : "accessing uninitialized member %qD",
8895 : index);
8896 33 : if (is_access_expr)
8897 3 : inform (DECL_SOURCE_LOCATION (index),
8898 : "%qD does not implicitly begin its lifetime "
8899 : "because %qT does not have a non-deleted "
8900 : "trivial default constructor, use "
8901 : "%<std::construct_at%> instead",
8902 : index, inner);
8903 : else
8904 30 : inform (DECL_SOURCE_LOCATION (index),
8905 : "initializing %qD requires a member access "
8906 : "expression as the left operand of the assignment",
8907 : index);
8908 33 : }
8909 113746 : *non_constant_p = true;
8910 : }
8911 47089 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8912 : {
8913 : /* Diagnose changing the active union member while the union
8914 : is in the process of being initialized. */
8915 18 : if (!ctx->quiet)
8916 6 : error_at (cp_expr_loc_or_input_loc (t),
8917 : "change of the active member of a union "
8918 : "from %qD to %qD during initialization",
8919 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8920 : index);
8921 18 : *non_constant_p = true;
8922 : }
8923 : no_zero_init = true;
8924 : }
8925 :
8926 : /* Ending the lifetime of the active union member means the union no
8927 : longer has an active member. */
8928 582598 : if (code == UNION_TYPE && refs->is_empty ()
8929 100217 : && TREE_CLOBBER_P (init)
8930 35854900 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8931 : {
8932 1797 : vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
8933 4511 : return void_node;
8934 : }
8935 :
8936 35845230 : ctors.safe_push (valp);
8937 35845230 : vec_safe_push (indexes, index);
8938 :
8939 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8940 : uninitialized. */
8941 33937211 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8942 1744520 : && CONSTRUCTOR_NO_CLEARING (*valp)
8943 37399215 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8944 35845230 : constructor_elt *cep
8945 35845230 : = get_or_insert_ctor_field (*valp, index, pos);
8946 35845230 : if (cep == nullptr)
8947 2714 : return void_node;
8948 35842516 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8949 :
8950 35842516 : if (code == UNION_TYPE)
8951 : {
8952 580801 : activated_union_member_p = true;
8953 580801 : --seen_union;
8954 : }
8955 :
8956 35842516 : valp = &cep->value;
8957 35842516 : type = reftype;
8958 : }
8959 :
8960 : /* Change an "as-base" clobber to the real type;
8961 : we don't need to worry about padding in constexpr. */
8962 94309423 : tree itype = initialized_type (init);
8963 94309423 : if (IS_FAKE_BASE_TYPE (itype))
8964 4449 : itype = TYPE_CONTEXT (itype);
8965 :
8966 : /* For initialization of an empty base, the original target will be
8967 : *(base*)this, evaluation of which resolves to the object
8968 : argument, which has the derived type rather than the base type. */
8969 188336554 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8970 94027131 : (itype, type)))
8971 : {
8972 18785 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8973 : empty_base = true;
8974 : }
8975 :
8976 : /* For __builtin_start_lifetime, if it is already within lifetime,
8977 : don't do anything. */
8978 94309423 : if (TREE_CODE (t) == MODOP_EXPR && *valp != NULL_TREE && *valp != void_node)
8979 : return void_node;
8980 :
8981 : /* Detect modifying a constant object in constexpr evaluation.
8982 : We have found a const object that is being modified. Figure out
8983 : if we need to issue an error. Consider
8984 :
8985 : struct A {
8986 : int n;
8987 : constexpr A() : n(1) { n = 2; } // #1
8988 : };
8989 : struct B {
8990 : const A a;
8991 : constexpr B() { a.n = 3; } // #2
8992 : };
8993 : constexpr B b{};
8994 :
8995 : #1 is OK, since we're modifying an object under construction, but
8996 : #2 is wrong, since "a" is const and has been fully constructed.
8997 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8998 : which means that the object is read-only. For the example above, the
8999 : *ctors stack at the point of #2 will look like:
9000 :
9001 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
9002 : ctors[1] = {.n=2} TREE_READONLY = 1
9003 :
9004 : and we're modifying "b.a", so we search the stack and see if the
9005 : constructor for "b.a" has already run. */
9006 94309385 : if (const_object_being_modified)
9007 : {
9008 87542 : bool fail = false;
9009 87542 : tree const_objtype
9010 87542 : = strip_array_types (TREE_TYPE (const_object_being_modified));
9011 87542 : if (!CLASS_TYPE_P (const_objtype))
9012 : fail = true;
9013 : else
9014 : {
9015 : /* [class.ctor]p5 "A constructor can be invoked for a const,
9016 : volatile, or const volatile object. const and volatile
9017 : semantics are not applied on an object under construction.
9018 : They come into effect when the constructor for the most
9019 : derived object ends." */
9020 262825 : for (tree *elt : ctors)
9021 87951 : if (same_type_ignoring_top_level_qualifiers_p
9022 87951 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
9023 : {
9024 87437 : fail = TREE_READONLY (*elt);
9025 87437 : break;
9026 : }
9027 : }
9028 87437 : if (fail)
9029 : {
9030 200 : if (!ctx->quiet)
9031 65 : modifying_const_object_error (t, const_object_being_modified);
9032 200 : *non_constant_p = true;
9033 200 : return t;
9034 : }
9035 : }
9036 :
9037 94309185 : if (!preeval)
9038 : {
9039 : /* We're handling an INIT_EXPR of class type, so the value of the
9040 : initializer can depend on the object it's initializing. */
9041 :
9042 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
9043 : wants to modify it. */
9044 31027414 : if (*valp == NULL_TREE)
9045 : {
9046 29935062 : *valp = build_constructor (type, NULL);
9047 29935062 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
9048 29935062 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
9049 : }
9050 31027414 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
9051 31027414 : new_ctx.object = target;
9052 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
9053 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
9054 : expansion of those trees uses ctx instead. */
9055 31027414 : if (TREE_CODE (init) == TARGET_EXPR)
9056 3271412 : if (tree tinit = TARGET_EXPR_INITIAL (init))
9057 3271412 : init = tinit;
9058 31027414 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
9059 : non_constant_p, overflow_p,
9060 : jump_target);
9061 31027414 : if (*jump_target)
9062 : return NULL_TREE;
9063 : /* The hash table might have moved since the get earlier, and the
9064 : initializer might have mutated the underlying CONSTRUCTORs, so we must
9065 : recompute VALP. */
9066 31027220 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
9067 35507987 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
9068 : {
9069 4480767 : ctors[i] = valp;
9070 4480767 : constructor_elt *cep
9071 4480767 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
9072 4480767 : valp = &cep->value;
9073 : }
9074 : }
9075 :
9076 94308991 : if (*non_constant_p)
9077 : return t;
9078 :
9079 : /* Don't share a CONSTRUCTOR that might be changed later. */
9080 92459325 : init = unshare_constructor (init);
9081 :
9082 92459325 : gcc_checking_assert (!*valp
9083 : || *valp == void_node
9084 : || (same_type_ignoring_top_level_qualifiers_p
9085 : (TREE_TYPE (*valp), type)));
9086 92459325 : if (empty_base)
9087 : {
9088 : /* Just evaluate the initializer and return, since there's no actual data
9089 : to store, and we didn't build a CONSTRUCTOR. */
9090 289678 : if (!*valp)
9091 : {
9092 : /* But do make sure we have something in *valp. */
9093 0 : *valp = build_constructor (type, nullptr);
9094 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
9095 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
9096 : }
9097 : }
9098 92169647 : else if (TREE_CLOBBER_P (init))
9099 : {
9100 746345 : if (AGGREGATE_TYPE_P (type) && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
9101 : {
9102 11424 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
9103 6788 : CONSTRUCTOR_ELTS (*valp) = nullptr;
9104 : else
9105 4636 : *valp = build_constructor (type, nullptr);
9106 11424 : TREE_CONSTANT (*valp) = true;
9107 11424 : TREE_SIDE_EFFECTS (*valp) = false;
9108 11424 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
9109 11424 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
9110 : }
9111 : else
9112 734921 : *valp = void_node;
9113 : }
9114 91423302 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
9115 29172787 : && TREE_CODE (init) == CONSTRUCTOR)
9116 : {
9117 : /* An outer ctx->ctor might be pointing to *valp, so replace
9118 : its contents. */
9119 4735780 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
9120 4735780 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
9121 4735780 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
9122 14207340 : CONSTRUCTOR_NO_CLEARING (*valp)
9123 4735780 : = CONSTRUCTOR_NO_CLEARING (init);
9124 14207340 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
9125 4735780 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
9126 : }
9127 : else
9128 86687522 : *valp = init;
9129 :
9130 : /* After initialization, 'const' semantics apply to the value of the
9131 : object. Make a note of this fact by marking the CONSTRUCTOR
9132 : TREE_READONLY. */
9133 92459325 : if (TREE_CODE (t) == INIT_EXPR
9134 69913870 : && !empty_base
9135 69624192 : && TREE_CODE (*valp) == CONSTRUCTOR
9136 97024069 : && TYPE_READONLY (type))
9137 : {
9138 36628 : tree target_type = TREE_TYPE (target);
9139 36628 : if (IS_FAKE_BASE_TYPE (target_type))
9140 0 : target_type = TYPE_CONTEXT (target_type);
9141 36628 : if (INDIRECT_REF_P (target)
9142 52551 : && (is_this_parameter
9143 15923 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
9144 : /* We've just initialized '*this' (perhaps via the target
9145 : constructor of a delegating constructor). Leave it up to the
9146 : caller that set 'this' to set TREE_READONLY appropriately. */
9147 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
9148 : (target_type, type) || empty_base);
9149 : else
9150 36605 : TREE_READONLY (*valp) = true;
9151 : }
9152 :
9153 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
9154 : CONSTRUCTORs, if any. */
9155 92459325 : bool c = TREE_CONSTANT (init);
9156 92459325 : bool s = TREE_SIDE_EFFECTS (init);
9157 92459325 : if (!indexes->is_empty ())
9158 : {
9159 20310614 : tree last = indexes->last ();
9160 20310614 : if (TREE_CODE (last) == REALPART_EXPR
9161 20310614 : || TREE_CODE (last) == IMAGPART_EXPR)
9162 : {
9163 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
9164 : possible. */
9165 61 : tree *cexpr = ctors.last ();
9166 61 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
9167 61 : TREE_OPERAND (*cexpr, 0),
9168 61 : TREE_OPERAND (*cexpr, 1)))
9169 53 : *cexpr = c;
9170 : else
9171 : {
9172 16 : TREE_CONSTANT (*cexpr)
9173 8 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
9174 8 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
9175 16 : TREE_SIDE_EFFECTS (*cexpr)
9176 16 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
9177 8 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
9178 : }
9179 61 : c = TREE_CONSTANT (*cexpr);
9180 61 : s = TREE_SIDE_EFFECTS (*cexpr);
9181 : }
9182 : }
9183 92459325 : if (!c || s || activated_union_member_p)
9184 63650429 : for (tree *elt : ctors)
9185 : {
9186 12495764 : if (TREE_CODE (*elt) != CONSTRUCTOR)
9187 0 : continue;
9188 12495764 : if (!c)
9189 10598266 : TREE_CONSTANT (*elt) = false;
9190 12495764 : if (s)
9191 0 : TREE_SIDE_EFFECTS (*elt) = true;
9192 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
9193 : this union. */
9194 12495764 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
9195 466193 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
9196 : }
9197 :
9198 92459325 : if (lval)
9199 : return target;
9200 : else
9201 587121 : return init;
9202 94636819 : }
9203 :
9204 : /* Evaluate a ++ or -- expression. */
9205 :
9206 : static tree
9207 8826007 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
9208 : value_cat lval,
9209 : bool *non_constant_p, bool *overflow_p,
9210 : tree *jump_target)
9211 : {
9212 8826007 : enum tree_code code = TREE_CODE (t);
9213 8826007 : tree type = TREE_TYPE (t);
9214 8826007 : tree op = TREE_OPERAND (t, 0);
9215 8826007 : tree offset = TREE_OPERAND (t, 1);
9216 8826007 : gcc_assert (TREE_CONSTANT (offset));
9217 :
9218 : /* OFFSET is constant, but perhaps not constant enough. We need to
9219 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
9220 8826007 : offset = fold_simple (offset);
9221 :
9222 : /* The operand as an lvalue. */
9223 8826007 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
9224 : non_constant_p, overflow_p,
9225 : jump_target);
9226 8826007 : if (*jump_target)
9227 : return NULL_TREE;
9228 :
9229 : /* The operand as an rvalue. */
9230 8826007 : tree val
9231 8826007 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
9232 : non_constant_p, overflow_p,
9233 : jump_target);
9234 8826007 : if (*jump_target)
9235 : return NULL_TREE;
9236 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
9237 : a local array in a constexpr function. */
9238 8826007 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
9239 3532613 : if (!ptr)
9240 3532613 : VERIFY_CONSTANT (val);
9241 :
9242 : /* The modified value. */
9243 8767013 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
9244 8767013 : tree mod;
9245 8767013 : if (INDIRECT_TYPE_P (type))
9246 : {
9247 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
9248 5293394 : offset = convert_to_ptrofftype (offset);
9249 5293394 : if (!inc)
9250 80926 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
9251 5293394 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
9252 : }
9253 3473619 : else if (c_promoting_integer_type_p (type)
9254 39613 : && !TYPE_UNSIGNED (type)
9255 3473831 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9256 : {
9257 212 : offset = fold_convert (integer_type_node, offset);
9258 212 : mod = fold_convert (integer_type_node, val);
9259 245 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
9260 : mod, offset);
9261 212 : mod = fold_convert (type, t);
9262 212 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
9263 9 : TREE_OVERFLOW (mod) = false;
9264 : }
9265 : else
9266 3916306 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
9267 8767013 : if (!ptr)
9268 3473619 : VERIFY_CONSTANT (mod);
9269 :
9270 : /* Storing the modified value. */
9271 14736918 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
9272 : MODIFY_EXPR, type, op, mod);
9273 8767013 : mod = cxx_eval_constant_expression (ctx, store, lval,
9274 : non_constant_p, overflow_p,
9275 : jump_target);
9276 8767013 : ggc_free (store);
9277 8767013 : if (*jump_target)
9278 : return NULL_TREE;
9279 8767013 : if (*non_constant_p)
9280 : return t;
9281 :
9282 : /* And the value of the expression. */
9283 8692754 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
9284 : /* Prefix ops are lvalues, but the caller might want an rvalue;
9285 : lval has already been taken into account in the store above. */
9286 : return mod;
9287 : else
9288 : /* Postfix ops are rvalues. */
9289 420635 : return val;
9290 : }
9291 :
9292 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
9293 : STMT matches *jump_target. If we're looking for a case label and we see
9294 : the default label, note it in ctx->css_state. */
9295 :
9296 : static bool
9297 552334 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
9298 : {
9299 552334 : switch (TREE_CODE (*jump_target))
9300 : {
9301 3341 : case LABEL_DECL:
9302 3341 : if (TREE_CODE (stmt) == LABEL_EXPR
9303 3341 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
9304 430 : return true;
9305 : break;
9306 :
9307 545844 : case INTEGER_CST:
9308 545844 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
9309 : {
9310 540501 : gcc_assert (ctx->css_state != NULL);
9311 540501 : if (!CASE_LOW (stmt))
9312 : {
9313 : /* default: should appear just once in a SWITCH_EXPR
9314 : body (excluding nested SWITCH_EXPR). */
9315 80758 : gcc_assert (*ctx->css_state != css_default_seen);
9316 : /* When evaluating SWITCH_EXPR body for the second time,
9317 : return true for the default: label. */
9318 80758 : if (*ctx->css_state == css_default_processing)
9319 : return true;
9320 41255 : *ctx->css_state = css_default_seen;
9321 : }
9322 459743 : else if (CASE_HIGH (stmt))
9323 : {
9324 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
9325 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
9326 : return true;
9327 : }
9328 459723 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
9329 : return true;
9330 : }
9331 : break;
9332 :
9333 : case BREAK_STMT:
9334 : case CONTINUE_STMT:
9335 : /* These two are handled directly in cxx_eval_loop_expr by testing
9336 : breaks (jump_target) or continues (jump_target). */
9337 : break;
9338 :
9339 : case VAR_DECL:
9340 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
9341 : and other places by testing throws (jump_target). */
9342 : break;
9343 :
9344 0 : default:
9345 0 : gcc_unreachable ();
9346 : }
9347 : return false;
9348 : }
9349 :
9350 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
9351 : semantics, for switch, break, continue, and return. */
9352 :
9353 : static tree
9354 48079145 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
9355 : bool *non_constant_p, bool *overflow_p,
9356 : tree *jump_target)
9357 : {
9358 : /* In a statement-expression we want to return the last value.
9359 : For empty statement expression return void_node. */
9360 48079145 : tree r = void_node;
9361 126622092 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
9362 : {
9363 104152942 : tree stmt = *i;
9364 :
9365 : /* We've found a continue, so skip everything until we reach
9366 : the label its jumping to. */
9367 104152942 : if (continues (jump_target))
9368 : {
9369 6490 : if (label_matches (ctx, jump_target, stmt))
9370 : /* Found it. */
9371 430 : *jump_target = NULL_TREE;
9372 : else
9373 6060 : continue;
9374 : }
9375 104146882 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
9376 11171016 : continue;
9377 :
9378 92975866 : value_cat lval = vc_discard;
9379 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
9380 130730235 : if (tsi_one_before_end_p (i)
9381 37755637 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
9382 : lval = vc_prvalue;
9383 :
9384 92975866 : r = cxx_eval_constant_expression (ctx, stmt, lval,
9385 : non_constant_p, overflow_p,
9386 : jump_target);
9387 92975862 : if (*non_constant_p)
9388 : break;
9389 83097131 : if (returns (jump_target)
9390 67459430 : || breaks (jump_target)
9391 78542947 : || throws (jump_target))
9392 : break;
9393 : }
9394 48079141 : return r;
9395 : }
9396 :
9397 : static tree merge_jump_target (location_t, const constexpr_ctx *, tree,
9398 : bool *, tree *, tree);
9399 :
9400 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
9401 : semantics; continue semantics are covered by cxx_eval_statement_list. */
9402 :
9403 : static tree
9404 6650427 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
9405 : bool *non_constant_p, bool *overflow_p,
9406 : tree *jump_target)
9407 : {
9408 6650427 : tree body, cond = NULL_TREE, expr = NULL_TREE;
9409 6650427 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
9410 6650427 : tree name = NULL_TREE;
9411 6650427 : unsigned cond_cleanup_depth = 0;
9412 6650427 : int count = 0;
9413 6650427 : switch (TREE_CODE (t))
9414 : {
9415 3425 : case LOOP_EXPR:
9416 3425 : body = LOOP_EXPR_BODY (t);
9417 3425 : break;
9418 5352805 : case DO_STMT:
9419 5352805 : body = DO_BODY (t);
9420 5352805 : cond = DO_COND (t);
9421 5352805 : name = DO_NAME (t);
9422 5352805 : break;
9423 365533 : case WHILE_STMT:
9424 365533 : body = WHILE_BODY (t);
9425 365533 : cond = WHILE_COND (t);
9426 365533 : cond_prep = WHILE_COND_PREP (t);
9427 365533 : cond_cleanup = WHILE_COND_CLEANUP (t);
9428 365533 : name = WHILE_NAME (t);
9429 365533 : count = -1;
9430 365533 : break;
9431 928664 : case FOR_STMT:
9432 928664 : if (FOR_INIT_STMT (t))
9433 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
9434 : non_constant_p, overflow_p, jump_target);
9435 928664 : if (*non_constant_p)
9436 : return NULL_TREE;
9437 928664 : body = FOR_BODY (t);
9438 928664 : cond = FOR_COND (t);
9439 928664 : expr = FOR_EXPR (t);
9440 928664 : cond_prep = FOR_COND_PREP (t);
9441 928664 : cond_cleanup = FOR_COND_CLEANUP (t);
9442 928664 : name = FOR_NAME (t);
9443 928664 : count = -1;
9444 928664 : break;
9445 0 : default:
9446 0 : gcc_unreachable ();
9447 : }
9448 6650427 : if (cond_prep)
9449 15 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
9450 31501300 : auto cleanup_cond = [&] {
9451 : /* Clean up the condition variable after each iteration. */
9452 24850873 : if (cond_cleanup_depth && !*non_constant_p)
9453 : {
9454 108 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
9455 108 : tree s = BIND_EXPR_BODY (cond_prep);
9456 108 : unsigned i;
9457 216 : for (i = cond_cleanup_depth; i; --i)
9458 : {
9459 108 : tree_stmt_iterator iter = tsi_last (s);
9460 108 : s = tsi_stmt (iter);
9461 108 : cleanups.quick_push (CLEANUP_EXPR (s));
9462 108 : s = CLEANUP_BODY (s);
9463 : }
9464 108 : tree c;
9465 108 : tree jmp_target = NULL_TREE;
9466 324 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
9467 108 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
9468 : overflow_p, &jmp_target);
9469 108 : merge_jump_target (cp_expr_loc_or_input_loc (t), ctx, NULL_TREE,
9470 : non_constant_p, jump_target, jmp_target);
9471 108 : }
9472 24850873 : if (cond_prep)
9473 114 : for (tree decl = BIND_EXPR_VARS (cond_prep);
9474 228 : decl; decl = DECL_CHAIN (decl))
9475 114 : destroy_value_checked (ctx, decl, non_constant_p);
9476 6650427 : };
9477 19530885 : do
9478 : {
9479 19530885 : if (count != -1)
9480 : {
9481 18236688 : if (body)
9482 18236688 : cxx_eval_constant_expression (ctx, body, vc_discard,
9483 : non_constant_p, overflow_p,
9484 : jump_target);
9485 18236688 : if (breaks (jump_target))
9486 : {
9487 36016 : if (TREE_CODE (*jump_target) != BREAK_STMT
9488 32567 : || BREAK_NAME (*jump_target) == NULL_TREE
9489 36320 : || BREAK_NAME (*jump_target) == name)
9490 35843 : *jump_target = NULL_TREE;
9491 : break;
9492 : }
9493 :
9494 18200672 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
9495 : {
9496 980 : if (TREE_CODE (*jump_target) != LABEL_DECL
9497 980 : && (TREE_CODE (*jump_target) != CONTINUE_STMT
9498 844 : || CONTINUE_NAME (*jump_target) == NULL_TREE
9499 184 : || CONTINUE_NAME (*jump_target) == name))
9500 754 : *jump_target = NULL_TREE;
9501 : else
9502 : break;
9503 : }
9504 :
9505 18200446 : if (expr)
9506 5418020 : cxx_eval_constant_expression (ctx, expr, vc_discard,
9507 : non_constant_p, overflow_p,
9508 : jump_target);
9509 18200446 : cleanup_cond ();
9510 : }
9511 :
9512 19494643 : if (cond_prep)
9513 : {
9514 114 : for (tree decl = BIND_EXPR_VARS (cond_prep);
9515 228 : decl; decl = DECL_CHAIN (decl))
9516 114 : ctx->global->clear_value (decl);
9517 114 : if (cond_cleanup)
9518 : {
9519 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
9520 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
9521 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
9522 : at the end of those STATEMENT_LISTs. */
9523 114 : cond_cleanup_depth = 0;
9524 114 : tree s = BIND_EXPR_BODY (cond_prep);
9525 114 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
9526 228 : depth; --depth)
9527 : {
9528 114 : for (tree_stmt_iterator i = tsi_start (s);
9529 330 : !tsi_end_p (i); ++i)
9530 : {
9531 330 : tree stmt = *i;
9532 330 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
9533 0 : continue;
9534 330 : if (tsi_one_before_end_p (i))
9535 : {
9536 : /* The last statement in the STATEMENT_LIST
9537 : has to be a CLEANUP_STMT (verified in
9538 : finish_loop_cond_prep). We want to
9539 : evaluate just its CLEANUP_BODY part but not
9540 : CLEANUP_EXPR part just yet. */
9541 108 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
9542 : /* If the CLEANUP_STMT is not actually to be
9543 : evaluated, don't increment cond_cleanup_depth
9544 : so that we don't evaluate the CLEANUP_EXPR
9545 : for it later either. */
9546 108 : if (*jump_target)
9547 : {
9548 : depth = 1;
9549 : break;
9550 : }
9551 108 : ++cond_cleanup_depth;
9552 : /* If not in the innermost one, next iteration
9553 : will handle CLEANUP_BODY similarly. */
9554 108 : if (depth > 1)
9555 : {
9556 0 : s = CLEANUP_BODY (stmt);
9557 0 : break;
9558 : }
9559 : /* The innermost one can be evaluated normally. */
9560 108 : cxx_eval_constant_expression (ctx,
9561 108 : CLEANUP_BODY (stmt),
9562 : vc_discard,
9563 : non_constant_p,
9564 : overflow_p,
9565 : jump_target);
9566 108 : break;
9567 : }
9568 : /* And so should be evaluated statements which aren't
9569 : last in the STATEMENT_LIST. */
9570 222 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
9571 : non_constant_p, overflow_p,
9572 : jump_target);
9573 222 : if (*non_constant_p
9574 216 : || returns (jump_target)
9575 216 : || breaks (jump_target)
9576 216 : || continues (jump_target)
9577 546 : || throws (jump_target))
9578 : {
9579 : depth = 1;
9580 : break;
9581 : }
9582 : }
9583 : }
9584 : }
9585 : else
9586 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
9587 : vc_discard, non_constant_p,
9588 : overflow_p, jump_target);
9589 : }
9590 :
9591 19494643 : if (cond)
9592 : {
9593 19487103 : tree res
9594 19487103 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9595 : non_constant_p, overflow_p,
9596 : jump_target);
9597 19487103 : if (res)
9598 : {
9599 19427299 : if (verify_constant (res, ctx->quiet, non_constant_p,
9600 : overflow_p))
9601 : break;
9602 19240763 : if (integer_zerop (res))
9603 : break;
9604 : }
9605 : else
9606 59804 : gcc_assert (*jump_target);
9607 : }
9608 :
9609 12940767 : if (++count >= constexpr_loop_limit)
9610 : {
9611 27 : if (!ctx->quiet)
9612 9 : error_at (cp_expr_loc_or_input_loc (t),
9613 : "%<constexpr%> loop iteration count exceeds limit of %d "
9614 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
9615 : constexpr_loop_limit);
9616 27 : *non_constant_p = true;
9617 27 : break;
9618 : }
9619 : }
9620 38765075 : while (!returns (jump_target)
9621 12883595 : && !breaks (jump_target)
9622 12883595 : && !continues (jump_target)
9623 12887966 : && (!switches (jump_target) || count == 0)
9624 12880667 : && !throws (jump_target)
9625 25881635 : && !*non_constant_p);
9626 :
9627 6650427 : cleanup_cond ();
9628 :
9629 6650427 : return NULL_TREE;
9630 : }
9631 :
9632 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
9633 : semantics. */
9634 :
9635 : static tree
9636 53231 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
9637 : bool *non_constant_p, bool *overflow_p,
9638 : tree *jump_target)
9639 : {
9640 53231 : tree cond
9641 53231 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
9642 53231 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9643 : non_constant_p, overflow_p,
9644 : jump_target);
9645 53231 : if (*jump_target)
9646 : return NULL_TREE;
9647 53231 : VERIFY_CONSTANT (cond);
9648 52938 : if (TREE_CODE (cond) != INTEGER_CST)
9649 : {
9650 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
9651 : switch condition even if it's constant enough for other things
9652 : (c++/113545). */
9653 0 : gcc_checking_assert (ctx->quiet);
9654 0 : *non_constant_p = true;
9655 0 : return t;
9656 : }
9657 :
9658 52938 : *jump_target = cond;
9659 :
9660 52938 : tree body
9661 52938 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
9662 52938 : tree name
9663 52938 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_NAME (t) : NULL_TREE;
9664 52938 : constexpr_ctx new_ctx = *ctx;
9665 52938 : constexpr_switch_state css = css_default_not_seen;
9666 52938 : new_ctx.css_state = &css;
9667 52938 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
9668 : non_constant_p, overflow_p, jump_target);
9669 92567 : if (switches (jump_target) && css == css_default_seen)
9670 : {
9671 : /* If the SWITCH_EXPR body has default: label, process it once again,
9672 : this time instructing label_matches to return true for default:
9673 : label on switches (jump_target). */
9674 39503 : css = css_default_processing;
9675 39503 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
9676 : non_constant_p, overflow_p, jump_target);
9677 : }
9678 52938 : if (switches (jump_target))
9679 126 : *jump_target = NULL_TREE;
9680 52812 : else if (breaks (jump_target))
9681 : {
9682 24111 : if (TREE_CODE (*jump_target) != BREAK_STMT
9683 24111 : || BREAK_NAME (*jump_target) == NULL_TREE
9684 24255 : || BREAK_NAME (*jump_target) == name)
9685 23989 : *jump_target = NULL_TREE;
9686 : }
9687 : return NULL_TREE;
9688 : }
9689 :
9690 : /* Find the object of TYPE under initialization in CTX. */
9691 :
9692 : static tree
9693 16716 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
9694 : {
9695 16716 : if (!ctx)
9696 : return NULL_TREE;
9697 :
9698 : /* Prefer the outermost matching object, but don't cross
9699 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
9700 16320 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
9701 574 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
9702 : return outer_ob;
9703 :
9704 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
9705 : can is a minor optimization. */
9706 16142 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
9707 302 : return ctx->ctor;
9708 :
9709 15840 : if (!ctx->object)
9710 : return NULL_TREE;
9711 :
9712 : /* Since an object cannot have a field of its own type, we can search outward
9713 : from ctx->object to find the unique containing object of TYPE. */
9714 : tree ob = ctx->object;
9715 15816 : while (ob)
9716 : {
9717 15816 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
9718 : break;
9719 219 : if (handled_component_p (ob))
9720 217 : ob = TREE_OPERAND (ob, 0);
9721 : else
9722 : ob = NULL_TREE;
9723 : }
9724 :
9725 : /* Like in replace_placeholders_r. */
9726 15599 : return unshare_expr (ob);
9727 : }
9728 :
9729 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
9730 : true, we're checking a constexpr function body. */
9731 :
9732 : static void
9733 35 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
9734 : {
9735 35 : auto_diagnostic_group d;
9736 35 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
9737 : "constant expression"))
9738 35 : inform (loc, "only unevaluated inline assembly is allowed in a "
9739 : "%<constexpr%> function in C++20");
9740 35 : }
9741 :
9742 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
9743 : context; maybe complain about that. */
9744 :
9745 : static void
9746 92321730 : maybe_warn_about_constant_value (location_t loc, tree decl)
9747 : {
9748 92321730 : static bool explained = false;
9749 92321730 : auto_diagnostic_group d;
9750 92321730 : if (cxx_dialect >= cxx17
9751 92159470 : && warn_interference_size
9752 92159470 : && !OPTION_SET_P (param_destruct_interfere_size)
9753 92159470 : && DECL_CONTEXT (decl) == std_node
9754 18862592 : && DECL_NAME (decl)
9755 18862586 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
9756 16 : && (LOCATION_FILE (input_location) != main_input_filename
9757 10 : || module_exporting_p ())
9758 92321733 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
9759 92321739 : && !explained)
9760 : {
9761 6 : explained = true;
9762 6 : inform (loc, "its value can vary between compiler versions or "
9763 : "with different %<-mtune%> or %<-mcpu%> flags");
9764 6 : inform (loc, "if this use is part of a public ABI, change it to "
9765 : "instead use a constant variable you define");
9766 6 : inform (loc, "the default value for the current CPU tuning "
9767 : "is %d bytes", param_destruct_interfere_size);
9768 6 : inform (loc, "you can stabilize this value with %<--param "
9769 : "hardware_destructive_interference_size=%d%>, or disable "
9770 : "this warning with %<-Wno-interference-size%>",
9771 : param_destruct_interfere_size);
9772 : }
9773 92321730 : }
9774 :
9775 : /* For element type ELT_TYPE, return the appropriate type of the heap object
9776 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
9777 : in bytes. If COOKIE_SIZE is NULL, return array type
9778 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
9779 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
9780 : where N is computed such that the size of the struct fits into FULL_SIZE.
9781 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
9782 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
9783 : will be also 0 and so it is not possible to determine the actual array
9784 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
9785 : expression evaluation of subexpressions of ARG_SIZE. */
9786 :
9787 : static tree
9788 117539 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9789 : tree cookie_size, tree full_size, tree arg_size,
9790 : bool *non_constant_p, bool *overflow_p,
9791 : tree *jump_target)
9792 : {
9793 117539 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9794 117539 : gcc_assert (tree_fits_uhwi_p (full_size));
9795 117539 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9796 117539 : if (arg_size)
9797 : {
9798 9 : STRIP_NOPS (arg_size);
9799 9 : if (cookie_size)
9800 : {
9801 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9802 : arg_size = NULL_TREE;
9803 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9804 0 : && tree_int_cst_equal (cookie_size,
9805 0 : TREE_OPERAND (arg_size, 0)))
9806 : {
9807 0 : arg_size = TREE_OPERAND (arg_size, 1);
9808 0 : STRIP_NOPS (arg_size);
9809 : }
9810 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9811 0 : && tree_int_cst_equal (cookie_size,
9812 0 : TREE_OPERAND (arg_size, 1)))
9813 : {
9814 0 : arg_size = TREE_OPERAND (arg_size, 0);
9815 0 : STRIP_NOPS (arg_size);
9816 : }
9817 : else
9818 : arg_size = NULL_TREE;
9819 : }
9820 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9821 : {
9822 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9823 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9824 9 : if (integer_zerop (op0))
9825 9 : arg_size
9826 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9827 : non_constant_p, overflow_p,
9828 : jump_target);
9829 0 : else if (integer_zerop (op1))
9830 0 : arg_size
9831 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9832 : non_constant_p, overflow_p,
9833 : jump_target);
9834 : else
9835 : arg_size = NULL_TREE;
9836 9 : if (*jump_target)
9837 : return NULL_TREE;
9838 : }
9839 : else
9840 : arg_size = NULL_TREE;
9841 : }
9842 :
9843 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9844 117539 : if (!arg_size)
9845 : {
9846 117530 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9847 117530 : gcc_assert (fsz >= csz);
9848 117530 : fsz -= csz;
9849 117530 : if (esz)
9850 117530 : fsz /= esz;
9851 : }
9852 117539 : tree itype2 = build_index_type (size_int (fsz - 1));
9853 117539 : if (!cookie_size)
9854 117530 : return build_cplus_array_type (elt_type, itype2);
9855 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9856 : }
9857 :
9858 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9859 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9860 : the expression which needed the cleanup threw. If both threw, diagnose
9861 : it and return NULL, otherwise return R. If only the cleanup threw, set
9862 : *JUMP_TARGET to the exception object from the cleanup. */
9863 :
9864 : static tree
9865 1743991 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9866 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9867 : {
9868 1743991 : if (!throws (&jmp_target))
9869 : return r;
9870 14 : if (throws (jump_target))
9871 : {
9872 : /* [except.throw]/9 - If the exception handling mechanism
9873 : handling an uncaught exception directly invokes a function
9874 : that exits via an exception, the function std::terminate is
9875 : invoked. */
9876 12 : if (!ctx->quiet)
9877 : {
9878 4 : auto_diagnostic_group d;
9879 4 : diagnose_std_terminate (loc, ctx, *jump_target);
9880 4 : inform (loc, "destructor exited with an exception");
9881 4 : }
9882 12 : *non_constant_p = true;
9883 12 : *jump_target = NULL_TREE;
9884 12 : return NULL_TREE;
9885 : }
9886 2 : *jump_target = jmp_target;
9887 2 : return r;
9888 : }
9889 :
9890 : /* Attempt to reduce the expression T to a constant value.
9891 : On failure, issue diagnostic and return error_mark_node. */
9892 : /* FIXME unify with c_fully_fold */
9893 : /* FIXME overflow_p is too global */
9894 :
9895 : tree
9896 3013152765 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9897 : value_cat lval,
9898 : bool *non_constant_p, bool *overflow_p,
9899 : tree *jump_target)
9900 : {
9901 3013152765 : if (*jump_target)
9902 : {
9903 : /* If we are jumping, ignore all statements/expressions except those
9904 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9905 1781254 : switch (TREE_CODE (t))
9906 : {
9907 : case BIND_EXPR:
9908 : case STATEMENT_LIST:
9909 : case LOOP_EXPR:
9910 : case COND_EXPR:
9911 : case IF_STMT:
9912 : case DO_STMT:
9913 : case WHILE_STMT:
9914 : case FOR_STMT:
9915 : break;
9916 545844 : case LABEL_EXPR:
9917 545844 : case CASE_LABEL_EXPR:
9918 545844 : if (label_matches (ctx, jump_target, t))
9919 : /* Found it. */
9920 52812 : *jump_target = NULL_TREE;
9921 : return NULL_TREE;
9922 : default:
9923 : return NULL_TREE;
9924 : }
9925 : }
9926 3011662191 : if (error_operand_p (t))
9927 : {
9928 188 : *non_constant_p = true;
9929 188 : return t;
9930 : }
9931 :
9932 : /* Change the input location to the currently processed expression for
9933 : better error messages when a subexpression has no location. */
9934 3011662003 : location_t loc = cp_expr_loc_or_input_loc (t);
9935 6023318572 : iloc_sentinel sentinel (loc);
9936 :
9937 3011662003 : STRIP_ANY_LOCATION_WRAPPER (t);
9938 :
9939 3011662003 : if (CONSTANT_CLASS_P (t))
9940 : {
9941 458543180 : if (TREE_OVERFLOW (t))
9942 : {
9943 62 : if (!ctx->quiet)
9944 15 : permerror (input_location, "overflow in constant expression");
9945 62 : if (!flag_permissive || ctx->quiet)
9946 59 : *overflow_p = true;
9947 : }
9948 :
9949 458543180 : if (TREE_CODE (t) == INTEGER_CST
9950 436270241 : && TYPE_PTR_P (TREE_TYPE (t))
9951 : /* INTEGER_CST with pointer-to-method type is only used
9952 : for a virtual method in a pointer to member function.
9953 : Don't reject those. */
9954 2097522 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9955 460640375 : && !integer_zerop (t))
9956 : {
9957 6 : if (!ctx->quiet)
9958 0 : error ("value %qE of type %qT is not a constant expression",
9959 0 : t, TREE_TYPE (t));
9960 6 : *non_constant_p = true;
9961 : }
9962 :
9963 : return t;
9964 : }
9965 :
9966 : /* Avoid excessively long constexpr evaluations. */
9967 2553118823 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9968 : {
9969 9 : if (!ctx->quiet)
9970 3 : error_at (loc,
9971 : "%<constexpr%> evaluation operation count exceeds limit of "
9972 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9973 : constexpr_ops_limit);
9974 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9975 9 : *non_constant_p = true;
9976 9 : return t;
9977 : }
9978 :
9979 2553118814 : constexpr_ctx new_ctx;
9980 2553118814 : tree r = t;
9981 :
9982 2553118814 : tree_code tcode = TREE_CODE (t);
9983 2553118814 : switch (tcode)
9984 : {
9985 59747996 : case RESULT_DECL:
9986 59747996 : if (lval)
9987 : return t;
9988 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9989 : through an invisible reference, or in named return value
9990 : optimization. */
9991 147212 : if (tree v = ctx->global->get_value (t))
9992 : return v;
9993 : else
9994 : {
9995 3 : if (!ctx->quiet)
9996 0 : error ("%qE is not a constant expression", t);
9997 3 : *non_constant_p = true;
9998 : }
9999 3 : break;
10000 :
10001 298185497 : case VAR_DECL:
10002 298185497 : if (DECL_HAS_VALUE_EXPR_P (t))
10003 : {
10004 9263070 : if (is_normal_capture_proxy (t)
10005 9263070 : && current_function_decl == DECL_CONTEXT (t))
10006 : {
10007 : /* Function parms aren't constexpr within the function
10008 : definition, so don't try to look at the closure. But if the
10009 : captured variable is constant, try to evaluate it directly. */
10010 633089 : r = DECL_CAPTURED_VARIABLE (t);
10011 : }
10012 : else
10013 8629981 : r = DECL_VALUE_EXPR (t);
10014 :
10015 9263070 : tree type = TREE_TYPE (t);
10016 9263070 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
10017 : {
10018 : /* Adjust r to match the reference-ness of t. */
10019 260426 : if (TYPE_REF_P (type))
10020 256235 : r = build_address (r);
10021 : else
10022 4191 : r = convert_from_reference (r);
10023 : }
10024 9263070 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
10025 9263070 : overflow_p, jump_target);
10026 : }
10027 : /* fall through */
10028 299521491 : case CONST_DECL:
10029 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
10030 : CONST_DECL for aggregate constants. */
10031 299521491 : if (lval)
10032 : return t;
10033 212805339 : else if (t == ctx->object)
10034 1009192 : return ctx->ctor;
10035 211796147 : if (VAR_P (t))
10036 : {
10037 201197089 : if (tree v = ctx->global->get_value (t))
10038 : {
10039 : r = v;
10040 : break;
10041 : }
10042 136111630 : if (ctx->global->is_outside_lifetime (t))
10043 : {
10044 137 : if (!ctx->quiet)
10045 39 : outside_lifetime_error (loc, t);
10046 137 : *non_constant_p = true;
10047 137 : break;
10048 : }
10049 : }
10050 146710551 : if (ctx->manifestly_const_eval == mce_true)
10051 92321730 : maybe_warn_about_constant_value (loc, t);
10052 146710551 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
10053 146710551 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
10054 : {
10055 : /* If the class is empty, we aren't actually loading anything. */
10056 252817 : r = build_constructor (TREE_TYPE (t), NULL);
10057 252817 : TREE_CONSTANT (r) = true;
10058 : }
10059 146457734 : else if (ctx->strict)
10060 146116367 : r = decl_really_constant_value (t, /*unshare_p=*/false);
10061 : else
10062 341367 : r = decl_constant_value (t, /*unshare_p=*/false);
10063 146707854 : if (TREE_CODE (r) == TARGET_EXPR
10064 146707854 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
10065 0 : r = TARGET_EXPR_INITIAL (r);
10066 146707854 : if (DECL_P (r)
10067 : /* P2280 allows references to unknown. */
10068 146993541 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
10069 : {
10070 34308808 : if (!ctx->quiet)
10071 187 : non_const_var_error (loc, r, /*fundef_p*/false);
10072 34308808 : *non_constant_p = true;
10073 : }
10074 : break;
10075 :
10076 : case DEBUG_BEGIN_STMT:
10077 : /* ??? It might be nice to retain this information somehow, so
10078 : as to be able to step into a constexpr function call. */
10079 : /* Fall through. */
10080 :
10081 : case FUNCTION_DECL:
10082 : case TEMPLATE_DECL:
10083 : case LABEL_DECL:
10084 : case LABEL_EXPR:
10085 : case CASE_LABEL_EXPR:
10086 : case PREDICT_EXPR:
10087 : case OMP_DECLARE_MAPPER:
10088 : case REFLECT_EXPR:
10089 : return t;
10090 :
10091 305218326 : case PARM_DECL:
10092 305218326 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
10093 : {
10094 : /* glvalue use. */
10095 21792660 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
10096 220877 : if (tree v = ctx->global->get_value (t))
10097 2055527929 : r = v;
10098 : }
10099 283425666 : else if (tree v = ctx->global->get_value (t))
10100 : {
10101 166312042 : r = v;
10102 166312042 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
10103 11305 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
10104 : non_constant_p, overflow_p,
10105 : jump_target);
10106 166312042 : if (*jump_target)
10107 : return NULL_TREE;
10108 : }
10109 117113624 : else if (lval)
10110 : /* Defer in case this is only used for its type. */;
10111 117113624 : else if (ctx->global->is_outside_lifetime (t))
10112 : {
10113 18 : if (!ctx->quiet)
10114 6 : outside_lifetime_error (loc, t);
10115 18 : *non_constant_p = true;
10116 18 : break;
10117 : }
10118 117113606 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
10119 117113606 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
10120 : {
10121 : /* If the class is empty, we aren't actually loading anything. */
10122 41287 : r = build_constructor (TREE_TYPE (t), NULL);
10123 41287 : TREE_CONSTANT (r) = true;
10124 : }
10125 117072319 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
10126 : /* P2280 allows references to unknown... */;
10127 116798793 : else if (p2280_active_p (ctx) && is_this_parameter (t))
10128 : /* ...as well as the this pointer. */;
10129 : else
10130 : {
10131 116350671 : if (!ctx->quiet)
10132 199 : error ("%qE is not a constant expression", t);
10133 116350671 : *non_constant_p = true;
10134 : }
10135 : break;
10136 :
10137 205683103 : case CALL_EXPR:
10138 205683103 : case AGGR_INIT_EXPR:
10139 205683103 : r = cxx_eval_call_expression (ctx, t, lval,
10140 : non_constant_p, overflow_p, jump_target);
10141 205683103 : break;
10142 :
10143 21509301 : case DECL_EXPR:
10144 21509301 : {
10145 21509301 : r = DECL_EXPR_DECL (t);
10146 21509301 : if (TREE_CODE (r) == USING_DECL)
10147 : {
10148 9355 : r = void_node;
10149 9355 : break;
10150 : }
10151 :
10152 21499946 : if (VAR_P (r)
10153 21499946 : && (TREE_STATIC (r)
10154 21336648 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
10155 : /* Allow __FUNCTION__ etc. */
10156 163298 : && !DECL_ARTIFICIAL (r)
10157 21500843 : && !decl_constant_var_p (r))
10158 : {
10159 7 : if (!ctx->quiet)
10160 : {
10161 2 : if (CP_DECL_THREAD_LOCAL_P (r))
10162 1 : error_at (loc, "control passes through definition of %qD "
10163 : "with thread storage duration", r);
10164 : else
10165 1 : error_at (loc, "control passes through definition of %qD "
10166 : "with static storage duration", r);
10167 : }
10168 7 : *non_constant_p = true;
10169 7 : break;
10170 : }
10171 :
10172 : /* make_rtl_for_nonlocal_decl could have deferred emission of
10173 : a local static var, but if it appears in a statement expression
10174 : which is constant expression evaluated to e.g. just the address
10175 : of the variable, its DECL_EXPR will never be seen during
10176 : gimple lowering's record_vars_into as the statement expression
10177 : will not be in the IL at all. */
10178 21499939 : if (VAR_P (r)
10179 21499939 : && TREE_STATIC (r)
10180 163291 : && !DECL_REALLY_EXTERN (r)
10181 163269 : && DECL_FUNCTION_SCOPE_P (r)
10182 163269 : && !var_in_maybe_constexpr_fn (r)
10183 21499942 : && decl_constant_var_p (r))
10184 : {
10185 3 : varpool_node *node = varpool_node::get (r);
10186 3 : if (node == NULL || !node->definition)
10187 3 : rest_of_decl_compilation (r, 0, at_eof);
10188 : }
10189 :
10190 42756548 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
10191 40090624 : || VECTOR_TYPE_P (TREE_TYPE (r)))
10192 : {
10193 2912218 : new_ctx = *ctx;
10194 2912218 : new_ctx.object = r;
10195 2912218 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
10196 2912218 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
10197 2912218 : ctx->global->put_value (r, new_ctx.ctor);
10198 2912218 : ctx = &new_ctx;
10199 : }
10200 :
10201 21499939 : if (tree init = DECL_INITIAL (r))
10202 : {
10203 11197651 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
10204 : non_constant_p, overflow_p,
10205 : jump_target);
10206 11197651 : if (*jump_target)
10207 : return NULL_TREE;
10208 : /* Don't share a CONSTRUCTOR that might be changed. */
10209 11197651 : init = unshare_constructor (init);
10210 : /* Remember that a constant object's constructor has already
10211 : run. */
10212 22395302 : if (CLASS_TYPE_P (TREE_TYPE (r))
10213 11747158 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
10214 71541 : TREE_READONLY (init) = true;
10215 11197651 : ctx->global->put_value (r, init);
10216 : }
10217 10302288 : else if (ctx == &new_ctx)
10218 : /* We gave it a CONSTRUCTOR above. */;
10219 : else
10220 8017010 : ctx->global->put_value (r, NULL_TREE);
10221 : }
10222 : break;
10223 :
10224 26920009 : case TARGET_EXPR:
10225 26920009 : {
10226 26920009 : tree type = TREE_TYPE (t);
10227 :
10228 26920009 : if (!literal_type_p (type))
10229 : {
10230 7138 : if (!ctx->quiet)
10231 : {
10232 0 : auto_diagnostic_group d;
10233 0 : error ("temporary of non-literal type %qT in a "
10234 : "constant expression", type);
10235 0 : explain_non_literal_class (type);
10236 0 : }
10237 7138 : *non_constant_p = true;
10238 11716749 : break;
10239 : }
10240 26912871 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
10241 : /* Avoid evaluating a TARGET_EXPR more than once. */
10242 26912871 : tree slot = TARGET_EXPR_SLOT (t);
10243 26912871 : if (tree v = ctx->global->get_value (slot))
10244 : {
10245 2166 : if (lval)
10246 10901946 : return slot;
10247 : r = v;
10248 : break;
10249 : }
10250 26910705 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
10251 : {
10252 : /* We're being expanded without an explicit target, so start
10253 : initializing a new object; expansion with an explicit target
10254 : strips the TARGET_EXPR before we get here. */
10255 20400503 : new_ctx = *ctx;
10256 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
10257 : any PLACEHOLDER_EXPR within the initializer that refers to the
10258 : former object under construction. */
10259 20400503 : new_ctx.parent = ctx;
10260 20400503 : new_ctx.ctor = build_constructor (type, NULL);
10261 20400503 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
10262 20400503 : new_ctx.object = slot;
10263 20400503 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
10264 20400503 : ctx = &new_ctx;
10265 : }
10266 :
10267 : /* If the initializer is complex, evaluate it to initialize slot. */
10268 26910705 : bool is_complex = target_expr_needs_replace (t);
10269 26910705 : if (is_complex)
10270 : /* In case no initialization actually happens, clear out any
10271 : void_node from a previous evaluation. */
10272 493 : ctx->global->put_value (slot, NULL_TREE);
10273 :
10274 : /* Pass vc_prvalue because this indicates
10275 : initialization of a temporary. */
10276 26910705 : r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t),
10277 : vc_prvalue, non_constant_p,
10278 : overflow_p, jump_target);
10279 26910705 : if (*non_constant_p)
10280 : break;
10281 15202204 : if (ctx->save_exprs)
10282 10088906 : ctx->save_exprs->safe_push (slot);
10283 15202204 : if (*jump_target)
10284 : {
10285 669 : if (!is_complex
10286 669 : && !(AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
10287 : /* If TARGET_EXPR_INITIAL throws exception and slot's value
10288 : has not been changed yet, CLEANUP_POINT_EXPR handling
10289 : could see there void_node from a previous evaluation
10290 : and complain. */
10291 6 : ctx->global->put_value (slot, NULL_TREE);
10292 : return NULL_TREE;
10293 : }
10294 15201535 : if (!is_complex)
10295 : {
10296 15201122 : r = unshare_constructor (r);
10297 : /* Adjust the type of the result to the type of the temporary. */
10298 15201122 : r = adjust_temp_type (type, r);
10299 15201122 : ctx->global->put_value (slot, r);
10300 : }
10301 15201535 : if (TARGET_EXPR_CLEANUP (t)
10302 15201535 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
10303 : {
10304 1437361 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
10305 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
10306 : them. */
10307 1437361 : if (CLEANUP_EH_ONLY (t))
10308 2364 : ctx->global->cleanups->safe_push (NULL_TREE);
10309 : }
10310 15201535 : if (lval)
10311 : return slot;
10312 4301314 : if (is_complex)
10313 0 : r = ctx->global->get_value (slot);
10314 : }
10315 4301314 : break;
10316 :
10317 103697222 : case INIT_EXPR:
10318 103697222 : case MODIFY_EXPR:
10319 103697222 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
10320 103697222 : r = cxx_eval_store_expression (ctx, t, lval,
10321 : non_constant_p, overflow_p, jump_target);
10322 103697222 : break;
10323 :
10324 0 : case SCOPE_REF:
10325 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
10326 : lval,
10327 : non_constant_p, overflow_p,
10328 : jump_target);
10329 0 : break;
10330 :
10331 64631270 : case RETURN_EXPR:
10332 64631270 : if (TREE_OPERAND (t, 0) != NULL_TREE)
10333 64305278 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10334 : lval,
10335 : non_constant_p, overflow_p,
10336 : jump_target);
10337 64631266 : if (!throws (jump_target))
10338 64630584 : *jump_target = t;
10339 : break;
10340 57161 : case BREAK_STMT:
10341 57161 : case CONTINUE_STMT:
10342 57161 : *jump_target = t;
10343 57161 : break;
10344 :
10345 966568 : case SAVE_EXPR:
10346 : /* Avoid evaluating a SAVE_EXPR more than once. */
10347 966568 : if (tree v = ctx->global->get_value (t))
10348 : r = v;
10349 : else
10350 : {
10351 944539 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10352 : vc_prvalue, non_constant_p,
10353 : overflow_p, jump_target);
10354 944539 : if (*non_constant_p || *jump_target)
10355 : break;
10356 12799 : ctx->global->put_value (t, r);
10357 12799 : if (ctx->save_exprs)
10358 9259 : ctx->save_exprs->safe_push (t);
10359 : }
10360 : break;
10361 :
10362 62837050 : case NON_LVALUE_EXPR:
10363 62837050 : case EXPR_STMT:
10364 62837050 : case EH_SPEC_BLOCK:
10365 62837050 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10366 : lval,
10367 : non_constant_p, overflow_p,
10368 : jump_target);
10369 62837050 : break;
10370 :
10371 9434 : case TRY_BLOCK:
10372 9434 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
10373 : non_constant_p, overflow_p,
10374 : jump_target);
10375 9434 : if (!*non_constant_p && throws (jump_target))
10376 3809 : if (tree h = TRY_HANDLERS (t))
10377 : {
10378 3809 : tree type = strip_array_types (TREE_TYPE (*jump_target));
10379 3809 : if (TREE_CODE (h) == STATEMENT_LIST)
10380 : {
10381 938 : for (tree stmt : tsi_range (h))
10382 904 : if (TREE_CODE (stmt) == HANDLER
10383 904 : && handler_match_for_exception_type (stmt, type))
10384 : {
10385 : h = stmt;
10386 : break;
10387 : }
10388 462 : if (TREE_CODE (h) == STATEMENT_LIST)
10389 : h = NULL_TREE;
10390 : }
10391 3347 : else if (TREE_CODE (h) != HANDLER
10392 3347 : || !handler_match_for_exception_type (h, type))
10393 : h = NULL_TREE;
10394 : if (h)
10395 : {
10396 3775 : gcc_assert (VAR_P (*jump_target));
10397 3775 : ctx->global->caught_exceptions.safe_push (*jump_target);
10398 3775 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
10399 3775 : *jump_target = NULL_TREE;
10400 3775 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
10401 : vc_discard, non_constant_p,
10402 : overflow_p, jump_target);
10403 : }
10404 : }
10405 : break;
10406 :
10407 91871596 : case CLEANUP_POINT_EXPR:
10408 91871596 : {
10409 91871596 : auto_vec<tree, 2> cleanups;
10410 91871596 : vec<tree> *prev_cleanups = ctx->global->cleanups;
10411 91871596 : ctx->global->cleanups = &cleanups;
10412 :
10413 91871596 : auto_vec<tree, 10> save_exprs;
10414 91871596 : constexpr_ctx new_ctx = *ctx;
10415 91871596 : new_ctx.save_exprs = &save_exprs;
10416 :
10417 91871596 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
10418 : lval,
10419 : non_constant_p, overflow_p,
10420 : jump_target);
10421 :
10422 91871594 : ctx->global->cleanups = prev_cleanups;
10423 91871594 : unsigned int i;
10424 91871594 : tree cleanup, jmp_target = NULL_TREE;
10425 91871594 : bool eh = throws (jump_target);
10426 : /* Evaluate the cleanups. */
10427 185082740 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
10428 1339552 : if (cleanup == NULL_TREE)
10429 : {
10430 : /* NULL_TREE cleanup is a marker that before it is
10431 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
10432 : if the body didn't throw. */
10433 2341 : if (!eh)
10434 2339 : --i;
10435 : }
10436 : else
10437 1337211 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
10438 : non_constant_p, overflow_p,
10439 : &jmp_target);
10440 :
10441 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
10442 : full-expression. */
10443 285712947 : for (tree save_expr : save_exprs)
10444 10098165 : destroy_value_checked (ctx, save_expr, non_constant_p);
10445 91871594 : if (throws (&jmp_target))
10446 0 : *jump_target = jmp_target;
10447 91871594 : }
10448 91871594 : break;
10449 :
10450 58901218 : case MUST_NOT_THROW_EXPR:
10451 58901218 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10452 : lval,
10453 : non_constant_p, overflow_p,
10454 : jump_target);
10455 58901216 : if (throws (jump_target))
10456 : {
10457 : /* [except.handle]/7 - If the search for a handler exits the
10458 : function body of a function with a non-throwing exception
10459 : specification, the function std::terminate is invoked. */
10460 54 : if (!ctx->quiet)
10461 : {
10462 18 : auto_diagnostic_group d;
10463 18 : diagnose_std_terminate (loc, ctx, *jump_target);
10464 18 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
10465 14 : && ctx->call
10466 32 : && ctx->call->fundef)
10467 14 : inform (loc, "uncaught exception exited from %<noexcept%> "
10468 : "function %qD",
10469 : ctx->call->fundef->decl);
10470 4 : else if (MUST_NOT_THROW_THROW_P (t))
10471 2 : inform (loc, "destructor exited with an exception after "
10472 : "initializing the exception object");
10473 2 : else if (MUST_NOT_THROW_CATCH_P (t))
10474 2 : inform (loc, "constructor exited with another exception while "
10475 : "entering handler");
10476 18 : }
10477 54 : *non_constant_p = true;
10478 54 : *jump_target = NULL_TREE;
10479 54 : r = NULL_TREE;
10480 : }
10481 : break;
10482 :
10483 0 : case TRY_CATCH_EXPR:
10484 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
10485 : {
10486 0 : r = void_node;
10487 0 : break;
10488 : }
10489 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10490 : non_constant_p, overflow_p,
10491 : jump_target);
10492 0 : if (!*non_constant_p && throws (jump_target))
10493 : {
10494 0 : tree jmp_target = NULL_TREE;
10495 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
10496 : non_constant_p, overflow_p,
10497 : &jmp_target);
10498 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
10499 : jmp_target);
10500 : }
10501 : break;
10502 :
10503 842 : case TRY_FINALLY_EXPR:
10504 842 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10505 : non_constant_p, overflow_p,
10506 : jump_target);
10507 842 : if (!*non_constant_p)
10508 : {
10509 827 : tree jmp_target = NULL_TREE;
10510 : /* Also evaluate the cleanup. */
10511 827 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
10512 827 : && throws (jump_target))
10513 0 : cxx_eval_constant_expression (ctx,
10514 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
10515 : 1), vc_discard,
10516 : non_constant_p, overflow_p,
10517 : &jmp_target);
10518 : else
10519 827 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
10520 : non_constant_p, overflow_p,
10521 : &jmp_target);
10522 827 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
10523 : jmp_target);
10524 : }
10525 : break;
10526 :
10527 0 : case EH_ELSE_EXPR:
10528 : /* Evaluate any cleanup that applies to non-EH exits. */
10529 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
10530 : non_constant_p, overflow_p,
10531 : jump_target);
10532 :
10533 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
10534 0 : break;
10535 :
10536 3887578 : case CLEANUP_STMT:
10537 3887578 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
10538 : non_constant_p, overflow_p,
10539 : jump_target);
10540 3887592 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
10541 : {
10542 1743056 : iloc_sentinel ils (loc);
10543 1743056 : tree jmp_target = NULL_TREE;
10544 : /* Also evaluate the cleanup. */
10545 1743056 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
10546 : non_constant_p, overflow_p,
10547 : &jmp_target);
10548 1743056 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
10549 : jmp_target);
10550 1743056 : }
10551 : break;
10552 :
10553 : /* These differ from cxx_eval_unary_expression in that this doesn't
10554 : check for a constant operand or result; an address can be
10555 : constant without its operand being, and vice versa. */
10556 103905419 : case MEM_REF:
10557 103905419 : case INDIRECT_REF:
10558 103905419 : r = cxx_eval_indirect_ref (ctx, t, lval,
10559 : non_constant_p, overflow_p,
10560 : jump_target);
10561 103905419 : break;
10562 :
10563 101185986 : case ADDR_EXPR:
10564 101185986 : {
10565 101185986 : tree oldop = TREE_OPERAND (t, 0);
10566 101185986 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
10567 : non_constant_p, overflow_p,
10568 : jump_target);
10569 101185986 : if (*jump_target)
10570 : return NULL_TREE;
10571 : /* Don't VERIFY_CONSTANT here. */
10572 101185981 : if (*non_constant_p)
10573 : return t;
10574 81721114 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
10575 : /* This function does more aggressive folding than fold itself. */
10576 81721114 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
10577 81721114 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
10578 : {
10579 56588403 : ggc_free (r);
10580 56588403 : return t;
10581 : }
10582 : break;
10583 : }
10584 :
10585 857219 : case REALPART_EXPR:
10586 857219 : case IMAGPART_EXPR:
10587 857219 : if (lval)
10588 : {
10589 1482 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10590 : non_constant_p, overflow_p,
10591 : jump_target);
10592 1482 : if (*jump_target)
10593 : return NULL_TREE;
10594 1482 : if (r == error_mark_node)
10595 : ;
10596 1482 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
10597 : r = t;
10598 : else
10599 1372 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
10600 : break;
10601 : }
10602 : /* FALLTHRU */
10603 25796650 : case CONJ_EXPR:
10604 25796650 : case FIX_TRUNC_EXPR:
10605 25796650 : case FLOAT_EXPR:
10606 25796650 : case NEGATE_EXPR:
10607 25796650 : case ABS_EXPR:
10608 25796650 : case ABSU_EXPR:
10609 25796650 : case BIT_NOT_EXPR:
10610 25796650 : case TRUTH_NOT_EXPR:
10611 25796650 : case FIXED_CONVERT_EXPR:
10612 25796650 : case VEC_DUPLICATE_EXPR:
10613 25796650 : r = cxx_eval_unary_expression (ctx, t, lval,
10614 : non_constant_p, overflow_p,
10615 : jump_target);
10616 25796650 : break;
10617 :
10618 9796747 : case SIZEOF_EXPR:
10619 9796747 : r = fold_sizeof_expr (t);
10620 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
10621 : which could lead to an infinite recursion. */
10622 9796747 : if (TREE_CODE (r) != SIZEOF_EXPR)
10623 9796747 : r = cxx_eval_constant_expression (ctx, r, lval,
10624 : non_constant_p, overflow_p,
10625 : jump_target);
10626 : else
10627 : {
10628 0 : *non_constant_p = true;
10629 0 : gcc_assert (ctx->quiet);
10630 : }
10631 :
10632 : break;
10633 :
10634 13674177 : case COMPOUND_EXPR:
10635 13674177 : {
10636 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
10637 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10638 : introduced by build_call_a. */
10639 13674177 : tree op0 = TREE_OPERAND (t, 0);
10640 13674177 : tree op1 = TREE_OPERAND (t, 1);
10641 13674177 : STRIP_NOPS (op1);
10642 5558760 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10643 16284300 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10644 6819862 : r = cxx_eval_constant_expression (ctx, op0,
10645 : lval, non_constant_p, overflow_p,
10646 : jump_target);
10647 : else
10648 : {
10649 : /* Check that the LHS is constant and then discard it. */
10650 6854315 : cxx_eval_constant_expression (ctx, op0, vc_discard,
10651 : non_constant_p, overflow_p,
10652 : jump_target);
10653 6854315 : if (*jump_target)
10654 : return NULL_TREE;
10655 6847060 : if (*non_constant_p)
10656 : return t;
10657 6572762 : op1 = TREE_OPERAND (t, 1);
10658 6572762 : r = cxx_eval_constant_expression (ctx, op1,
10659 : lval, non_constant_p, overflow_p,
10660 : jump_target);
10661 : }
10662 : }
10663 : break;
10664 :
10665 111722010 : case POINTER_PLUS_EXPR:
10666 111722010 : case POINTER_DIFF_EXPR:
10667 111722010 : case PLUS_EXPR:
10668 111722010 : case MINUS_EXPR:
10669 111722010 : case MULT_EXPR:
10670 111722010 : case TRUNC_DIV_EXPR:
10671 111722010 : case CEIL_DIV_EXPR:
10672 111722010 : case FLOOR_DIV_EXPR:
10673 111722010 : case ROUND_DIV_EXPR:
10674 111722010 : case TRUNC_MOD_EXPR:
10675 111722010 : case CEIL_MOD_EXPR:
10676 111722010 : case ROUND_MOD_EXPR:
10677 111722010 : case RDIV_EXPR:
10678 111722010 : case EXACT_DIV_EXPR:
10679 111722010 : case MIN_EXPR:
10680 111722010 : case MAX_EXPR:
10681 111722010 : case LSHIFT_EXPR:
10682 111722010 : case RSHIFT_EXPR:
10683 111722010 : case LROTATE_EXPR:
10684 111722010 : case RROTATE_EXPR:
10685 111722010 : case BIT_IOR_EXPR:
10686 111722010 : case BIT_XOR_EXPR:
10687 111722010 : case BIT_AND_EXPR:
10688 111722010 : case TRUTH_XOR_EXPR:
10689 111722010 : case LT_EXPR:
10690 111722010 : case LE_EXPR:
10691 111722010 : case GT_EXPR:
10692 111722010 : case GE_EXPR:
10693 111722010 : case EQ_EXPR:
10694 111722010 : case NE_EXPR:
10695 111722010 : case SPACESHIP_EXPR:
10696 111722010 : case UNORDERED_EXPR:
10697 111722010 : case ORDERED_EXPR:
10698 111722010 : case UNLT_EXPR:
10699 111722010 : case UNLE_EXPR:
10700 111722010 : case UNGT_EXPR:
10701 111722010 : case UNGE_EXPR:
10702 111722010 : case UNEQ_EXPR:
10703 111722010 : case LTGT_EXPR:
10704 111722010 : case RANGE_EXPR:
10705 111722010 : case COMPLEX_EXPR:
10706 111722010 : r = cxx_eval_binary_expression (ctx, t, lval,
10707 : non_constant_p, overflow_p,
10708 : jump_target);
10709 111722010 : break;
10710 :
10711 : /* fold can introduce non-IF versions of these; still treat them as
10712 : short-circuiting. */
10713 15507370 : case TRUTH_AND_EXPR:
10714 15507370 : case TRUTH_ANDIF_EXPR:
10715 15507370 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
10716 : boolean_true_node,
10717 : non_constant_p, overflow_p,
10718 : jump_target);
10719 15507370 : break;
10720 :
10721 3073348 : case TRUTH_OR_EXPR:
10722 3073348 : case TRUTH_ORIF_EXPR:
10723 3073348 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
10724 : boolean_false_node,
10725 : non_constant_p, overflow_p,
10726 : jump_target);
10727 3073348 : break;
10728 :
10729 15673755 : case ARRAY_REF:
10730 15673755 : r = cxx_eval_array_reference (ctx, t, lval,
10731 : non_constant_p, overflow_p,
10732 : jump_target);
10733 15673755 : break;
10734 :
10735 115868227 : case COMPONENT_REF:
10736 115868227 : if (is_overloaded_fn (t))
10737 : {
10738 : /* We can only get here in checking mode via
10739 : build_non_dependent_expr, because any expression that
10740 : calls or takes the address of the function will have
10741 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
10742 6 : gcc_checking_assert (ctx->quiet || errorcount);
10743 6 : *non_constant_p = true;
10744 6 : return t;
10745 : }
10746 115868221 : r = cxx_eval_component_reference (ctx, t, lval,
10747 : non_constant_p, overflow_p,
10748 : jump_target);
10749 115868221 : break;
10750 :
10751 40745 : case BIT_FIELD_REF:
10752 40745 : r = cxx_eval_bit_field_ref (ctx, t, lval,
10753 : non_constant_p, overflow_p,
10754 : jump_target);
10755 40745 : break;
10756 :
10757 39549653 : case COND_EXPR:
10758 39549653 : case IF_STMT:
10759 39549653 : if (*jump_target)
10760 : {
10761 176392 : tree orig_jump = *jump_target;
10762 176392 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
10763 352784 : ? TREE_OPERAND (t, 1) : void_node);
10764 : /* When jumping to a label, the label might be either in the
10765 : then or else blocks, so process then block first in skipping
10766 : mode first, and if we are still in the skipping mode at its end,
10767 : process the else block too. */
10768 176392 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10769 : overflow_p, jump_target);
10770 : /* It's possible that we found the label in the then block. But
10771 : it could have been followed by another jumping statement, e.g.
10772 : say we're looking for case 1:
10773 : if (cond)
10774 : {
10775 : // skipped statements
10776 : case 1:; // clears up *jump_target
10777 : return 1; // and sets it to a RETURN_EXPR
10778 : }
10779 : else { ... }
10780 : in which case we need not go looking to the else block.
10781 : (goto is not allowed in a constexpr function.) */
10782 176392 : if (*jump_target == orig_jump)
10783 : {
10784 192319 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
10785 192319 : ? TREE_OPERAND (t, 2) : void_node);
10786 176354 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10787 : overflow_p, jump_target);
10788 : }
10789 : break;
10790 : }
10791 39373261 : r = cxx_eval_conditional_expression (ctx, t, lval,
10792 : non_constant_p, overflow_p,
10793 : jump_target);
10794 39373261 : break;
10795 11755 : case VEC_COND_EXPR:
10796 11755 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10797 : overflow_p, jump_target);
10798 11755 : break;
10799 :
10800 23479226 : case CONSTRUCTOR:
10801 23479226 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10802 : {
10803 : /* Don't re-process a constant CONSTRUCTOR. */
10804 20109821 : verify_constructor_flags (t);
10805 20109821 : if (TREE_CONSTANT (t))
10806 : return t;
10807 : }
10808 3369405 : if (TREE_CLOBBER_P (t))
10809 : {
10810 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10811 : a clobber by itself isn't a constant-expression. */
10812 242 : gcc_assert (ctx->quiet);
10813 242 : *non_constant_p = true;
10814 242 : break;
10815 : }
10816 3369163 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10817 : non_constant_p, overflow_p, jump_target);
10818 3369163 : break;
10819 :
10820 808 : case VEC_INIT_EXPR:
10821 : /* We can get this in a defaulted constructor for a class with a
10822 : non-static data member of array type. Either the initializer will
10823 : be NULL, meaning default-initialization, or it will be an lvalue
10824 : or xvalue of the same type, meaning direct-initialization from the
10825 : corresponding member. */
10826 808 : r = cxx_eval_vec_init (ctx, t, lval,
10827 : non_constant_p, overflow_p, jump_target);
10828 808 : break;
10829 :
10830 43640 : case VEC_PERM_EXPR:
10831 43640 : r = cxx_eval_trinary_expression (ctx, t, lval,
10832 : non_constant_p, overflow_p,
10833 : jump_target);
10834 43640 : break;
10835 :
10836 7 : case PAREN_EXPR:
10837 7 : gcc_assert (!REF_PARENTHESIZED_P (t));
10838 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10839 : constant expressions since it's unaffected by -fassociative-math. */
10840 7 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10841 : non_constant_p, overflow_p,
10842 : jump_target);
10843 7 : break;
10844 :
10845 441071029 : case NOP_EXPR:
10846 441071029 : if (REINTERPRET_CAST_P (t))
10847 : {
10848 145 : if (!ctx->quiet)
10849 6 : error_at (loc,
10850 : "%<reinterpret_cast%> is not a constant expression");
10851 145 : *non_constant_p = true;
10852 145 : return t;
10853 : }
10854 : /* FALLTHROUGH. */
10855 552989992 : case CONVERT_EXPR:
10856 552989992 : case VIEW_CONVERT_EXPR:
10857 552989992 : case UNARY_PLUS_EXPR:
10858 552989992 : {
10859 552989992 : tree oldop = TREE_OPERAND (t, 0);
10860 :
10861 552989992 : tree op = cxx_eval_constant_expression (ctx, oldop,
10862 552989992 : VOID_TYPE_P (TREE_TYPE (t))
10863 : ? vc_discard
10864 : : tcode == VIEW_CONVERT_EXPR
10865 519229000 : ? lval : vc_prvalue,
10866 : non_constant_p, overflow_p,
10867 : jump_target);
10868 552987293 : if (*jump_target)
10869 : return NULL_TREE;
10870 552982853 : if (*non_constant_p)
10871 : return t;
10872 446461320 : tree type = TREE_TYPE (t);
10873 :
10874 446461320 : if (VOID_TYPE_P (type))
10875 32568186 : return void_node;
10876 :
10877 413893134 : if (TREE_CODE (t) == CONVERT_EXPR
10878 60799512 : && ARITHMETIC_TYPE_P (type)
10879 5036429 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10880 413916897 : && ctx->strict)
10881 : {
10882 23668 : if (!ctx->quiet)
10883 54 : error_at (loc,
10884 : "conversion from pointer type %qT to arithmetic type "
10885 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10886 23668 : *non_constant_p = true;
10887 23668 : return t;
10888 : }
10889 :
10890 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10891 : type cannot be part of a core constant expression as a resolution to
10892 : DR 1312. */
10893 153724673 : if (TYPE_PTROB_P (type)
10894 150355284 : && TYPE_PTR_P (TREE_TYPE (op))
10895 94470061 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10896 : /* Inside a call to std::construct_at,
10897 : std::allocator<T>::{,de}allocate, or
10898 : std::source_location::current, we permit casting from void*
10899 : because that is compiler-generated code. */
10900 1918868 : && !is_std_construct_at (ctx->call)
10901 165993 : && !is_std_allocator_allocate_deallocate (ctx->call)
10902 413954686 : && !is_std_source_location_current (ctx->call))
10903 : {
10904 : /* Likewise, don't error when casting from void* when OP is
10905 : &heap uninit and similar. */
10906 85061 : tree sop = tree_strip_nop_conversions (op);
10907 85061 : tree decl = NULL_TREE;
10908 85061 : if (TREE_CODE (sop) == ADDR_EXPR)
10909 62856 : decl = TREE_OPERAND (sop, 0);
10910 62856 : if (decl
10911 62856 : && VAR_P (decl)
10912 61428 : && DECL_ARTIFICIAL (decl)
10913 123850 : && heap_var_p (decl))
10914 : /* OK */;
10915 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10916 : cv void" to a pointer-to-object type T unless P is a null
10917 : pointer value or points to an object whose type is similar to
10918 : T. */
10919 24478 : else if (cxx_dialect > cxx23)
10920 : {
10921 24288 : if (integer_zerop (sop))
10922 30 : return build_int_cst (type, 0);
10923 24258 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10924 : NULL, jump_target);
10925 24258 : if (*jump_target)
10926 : return NULL_TREE;
10927 24258 : if (r)
10928 : {
10929 24211 : r = build1 (ADDR_EXPR, type, r);
10930 24211 : break;
10931 : }
10932 47 : if (!ctx->quiet)
10933 : {
10934 5 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10935 5 : auto_diagnostic_group d;
10936 5 : error_at (loc, "cast from %qT is not allowed in a "
10937 : "constant expression because "
10938 : "pointed-to type %qT is not similar to %qT",
10939 5 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10940 5 : TREE_TYPE (type));
10941 5 : tree obj = build_fold_indirect_ref (sop);
10942 5 : if (TREE_CODE (obj) == COMPONENT_REF)
10943 4 : obj = TREE_OPERAND (obj, 1);
10944 5 : if (DECL_P (obj))
10945 5 : inform (DECL_SOURCE_LOCATION (obj),
10946 : "pointed-to object declared here");
10947 5 : }
10948 47 : *non_constant_p = true;
10949 47 : return t;
10950 : }
10951 : else
10952 : {
10953 190 : if (!ctx->quiet)
10954 26 : error_at (loc, "cast from %qT is not allowed in a "
10955 : "constant expression before C++26",
10956 26 : TREE_TYPE (op));
10957 190 : *non_constant_p = true;
10958 190 : return t;
10959 : }
10960 : }
10961 :
10962 : /* [expr.static.cast]/10: A prvalue of type "pointer to cv1 B", where
10963 : B is a class type, can be converted to a prvalue of type
10964 : "pointer to cv2 D", where D is a complete class derived from B, ...
10965 : If the prvalue of type "pointer to cv1 B" points to a B that is
10966 : actually a base class subobject of an object of type D, the
10967 : resulting pointer points to the enclosing object of type D.
10968 : Otherwise, the behavior is undefined.
10969 : Similarly [expr.static.cast]/2 for references. */
10970 413844988 : if (INDIRECT_TYPE_P (type)
10971 257613824 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10972 257479000 : && COMPLETE_TYPE_P (TREE_TYPE (type))
10973 254291928 : && !integer_zerop (op)
10974 667413982 : && is_properly_derived_from (TREE_TYPE (type),
10975 253568994 : TREE_TYPE (TREE_TYPE (op))))
10976 : {
10977 14503 : tree sop = tree_strip_nop_conversions (op);
10978 14503 : if (cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10979 : NULL, jump_target) == NULL_TREE)
10980 : {
10981 284 : tree dyntype = NULL_TREE;
10982 284 : if (!ctx->quiet && TREE_CODE (sop) == ADDR_EXPR)
10983 : {
10984 44 : sop = TREE_OPERAND (sop, 0);
10985 44 : while (TREE_CODE (sop) == COMPONENT_REF
10986 59 : && DECL_FIELD_IS_BASE (TREE_OPERAND (sop, 1)))
10987 15 : sop = TREE_OPERAND (sop, 0);
10988 44 : dyntype = strip_array_types (TREE_TYPE (sop));
10989 44 : if (same_type_p (dyntype, TREE_TYPE (TREE_TYPE (op))))
10990 261 : dyntype = NULL_TREE;
10991 : }
10992 284 : if (!ctx->quiet)
10993 : {
10994 53 : if (dyntype)
10995 23 : error_at (loc, "%qT operand (of dynamic type %qT) is "
10996 : "not a base class subobject of a %qT object",
10997 23 : TREE_TYPE (TREE_TYPE (op)), dyntype,
10998 23 : TREE_TYPE (type));
10999 : else
11000 30 : error_at (loc, "%qT operand is not a base class "
11001 : "subobject of a %qT object",
11002 30 : TREE_TYPE (TREE_TYPE (op)), TREE_TYPE (type));
11003 : }
11004 284 : *non_constant_p = true;
11005 284 : return t;
11006 : }
11007 : }
11008 :
11009 413844704 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
11010 : {
11011 1064 : op = cplus_expand_constant (op);
11012 1064 : if (TREE_CODE (op) == PTRMEM_CST)
11013 : {
11014 21 : if (!ctx->quiet)
11015 3 : error_at (loc, "%qE is not a constant expression when the "
11016 : "class %qT is still incomplete", op,
11017 3 : PTRMEM_CST_CLASS (op));
11018 21 : *non_constant_p = true;
11019 21 : return t;
11020 : }
11021 : }
11022 :
11023 413844683 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
11024 : {
11025 657 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
11026 657 : && !can_convert_qual (type, op))
11027 6 : op = cplus_expand_constant (op);
11028 657 : return cp_fold_convert (type, op);
11029 : }
11030 :
11031 413844026 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
11032 : {
11033 833188 : if (integer_zerop (op))
11034 : {
11035 776654 : if (TYPE_REF_P (type))
11036 : {
11037 36 : if (!ctx->quiet)
11038 4 : error_at (loc, "dereferencing a null pointer");
11039 36 : *non_constant_p = true;
11040 36 : return t;
11041 : }
11042 : }
11043 56534 : else if (TYPE_PTR_P (type)
11044 56534 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
11045 : /* INTEGER_CST with pointer-to-method type is only used
11046 : for a virtual method in a pointer to member function.
11047 : Don't reject those. */
11048 : ;
11049 : else
11050 : {
11051 : /* This detects for example:
11052 : reinterpret_cast<void*>(sizeof 0)
11053 : */
11054 56531 : if (!ctx->quiet)
11055 6 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
11056 : "a constant expression",
11057 : type, op);
11058 56531 : *non_constant_p = true;
11059 56531 : return t;
11060 : }
11061 : }
11062 :
11063 413787459 : if (INDIRECT_TYPE_P (type)
11064 257556973 : && TREE_CODE (op) == NOP_EXPR
11065 123297299 : && TREE_TYPE (op) == ptr_type_node
11066 517241 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
11067 515234 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
11068 414190609 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
11069 403150 : 0)) == heap_uninit_identifier
11070 287578 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
11071 287578 : 0)) == heap_vec_uninit_identifier))
11072 : {
11073 117539 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
11074 117539 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
11075 117539 : tree elt_type = TREE_TYPE (type);
11076 117539 : tree cookie_size = NULL_TREE;
11077 117539 : tree arg_size = NULL_TREE;
11078 117539 : if (TREE_CODE (elt_type) == RECORD_TYPE
11079 117539 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
11080 : {
11081 9 : tree fld1 = TYPE_FIELDS (elt_type);
11082 9 : tree fld2 = DECL_CHAIN (fld1);
11083 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
11084 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
11085 : }
11086 117539 : DECL_NAME (var)
11087 117539 : = (DECL_NAME (var) == heap_uninit_identifier
11088 117539 : ? heap_identifier : heap_vec_identifier);
11089 : /* For zero sized elt_type, try to recover how many outer_nelts
11090 : it should have. */
11091 117539 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
11092 117530 : : integer_zerop (var_size))
11093 109 : && !int_size_in_bytes (elt_type)
11094 9 : && TREE_CODE (oldop) == CALL_EXPR
11095 117557 : && call_expr_nargs (oldop) >= 1)
11096 9 : if (tree fun = get_function_named_in_call (oldop))
11097 9 : if (cxx_replaceable_global_alloc_fn (fun)
11098 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
11099 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
11100 117539 : tree new_type
11101 117539 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
11102 : var_size, arg_size,
11103 : non_constant_p, overflow_p,
11104 : jump_target);
11105 117539 : if (*jump_target)
11106 : return NULL_TREE;
11107 117539 : TREE_TYPE (var) = new_type;
11108 117539 : TREE_TYPE (TREE_OPERAND (op, 0))
11109 117539 : = build_pointer_type (TREE_TYPE (var));
11110 117539 : if (is_std_allocator_allocate (ctx->call))
11111 : {
11112 : /* [allocator.members]/5: This function starts the lifetime
11113 : of the array object, but not that of any of the array
11114 : elements. */
11115 80773 : tree ctor = build_constructor (new_type, NULL);
11116 80773 : CONSTRUCTOR_NO_CLEARING (ctor) = 1;
11117 80773 : CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (ctor) = 1;
11118 80773 : ctx->global->put_value (var, ctor);
11119 : }
11120 : }
11121 :
11122 : /* This can happen for std::meta::info(^^int) where the cast has no
11123 : meaning. */
11124 413787459 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
11125 : {
11126 : r = op;
11127 : break;
11128 : }
11129 :
11130 413697009 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
11131 : /* We didn't fold at the top so we could check for ptr-int
11132 : conversion. */
11133 32613523 : return fold (t);
11134 :
11135 381083486 : tree sop;
11136 :
11137 : /* Handle an array's bounds having been deduced after we built
11138 : the wrapping expression. */
11139 381083486 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
11140 : r = op;
11141 167138805 : else if (sop = tree_strip_nop_conversions (op),
11142 234307582 : sop != op && (same_type_ignoring_tlq_and_bounds_p
11143 67168777 : (type, TREE_TYPE (sop))))
11144 : r = sop;
11145 138057183 : else if (tcode == UNARY_PLUS_EXPR)
11146 0 : r = fold_convert (TREE_TYPE (t), op);
11147 : else
11148 138057183 : r = fold_build1 (tcode, type, op);
11149 :
11150 : /* Conversion of an out-of-range value has implementation-defined
11151 : behavior; the language considers it different from arithmetic
11152 : overflow, which is undefined. */
11153 381083486 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
11154 12845 : TREE_OVERFLOW (r) = false;
11155 : }
11156 : break;
11157 :
11158 9965 : case EXCESS_PRECISION_EXPR:
11159 9965 : {
11160 9965 : tree oldop = TREE_OPERAND (t, 0);
11161 :
11162 9965 : tree op = cxx_eval_constant_expression (ctx, oldop,
11163 : lval,
11164 : non_constant_p, overflow_p,
11165 : jump_target);
11166 9965 : if (*jump_target)
11167 : return NULL_TREE;
11168 9965 : if (*non_constant_p)
11169 : return t;
11170 9959 : r = fold_convert (TREE_TYPE (t), op);
11171 9959 : break;
11172 : }
11173 :
11174 66845 : case EMPTY_CLASS_EXPR:
11175 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
11176 : it to an appropriate CONSTRUCTOR. */
11177 66845 : return build_constructor (TREE_TYPE (t), NULL);
11178 :
11179 48079145 : case STATEMENT_LIST:
11180 48079145 : new_ctx = *ctx;
11181 48079145 : new_ctx.ctor = new_ctx.object = NULL_TREE;
11182 48079145 : return cxx_eval_statement_list (&new_ctx, t,
11183 48079141 : non_constant_p, overflow_p, jump_target);
11184 :
11185 33475591 : case BIND_EXPR:
11186 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
11187 : map, so that when checking whether they're already destroyed later we
11188 : don't get confused by remnants of previous calls. */
11189 52774605 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
11190 19299014 : ctx->global->clear_value (decl);
11191 33475591 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
11192 : lval,
11193 : non_constant_p, overflow_p,
11194 : jump_target);
11195 52774601 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
11196 19299012 : destroy_value_checked (ctx, decl, non_constant_p);
11197 : break;
11198 :
11199 8826007 : case PREINCREMENT_EXPR:
11200 8826007 : case POSTINCREMENT_EXPR:
11201 8826007 : case PREDECREMENT_EXPR:
11202 8826007 : case POSTDECREMENT_EXPR:
11203 8826007 : return cxx_eval_increment_expression (ctx, t,
11204 : lval, non_constant_p, overflow_p,
11205 8826007 : jump_target);
11206 :
11207 2382 : case THROW_EXPR:
11208 2382 : if (cxx_dialect >= cxx26)
11209 2055 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
11210 : non_constant_p, overflow_p,
11211 2055 : jump_target);
11212 : /* FALLTHROUGH */
11213 335 : case LAMBDA_EXPR:
11214 335 : case NEW_EXPR:
11215 335 : case VEC_NEW_EXPR:
11216 335 : case DELETE_EXPR:
11217 335 : case VEC_DELETE_EXPR:
11218 335 : case MODOP_EXPR:
11219 : /* GCC internal stuff. */
11220 335 : case VA_ARG_EXPR:
11221 335 : case BASELINK:
11222 335 : case OFFSET_REF:
11223 335 : if (!ctx->quiet)
11224 86 : error_at (loc, "expression %qE is not a constant expression", t);
11225 335 : *non_constant_p = true;
11226 335 : break;
11227 :
11228 397690 : case OBJ_TYPE_REF:
11229 : /* Virtual function lookup. We don't need to do anything fancy. */
11230 397690 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
11231 : lval, non_constant_p, overflow_p,
11232 397690 : jump_target);
11233 :
11234 16142 : case PLACEHOLDER_EXPR:
11235 : /* Use of the value or address of the current object. */
11236 16142 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
11237 : {
11238 15899 : if (TREE_CODE (ctor) == CONSTRUCTOR)
11239 : return ctor;
11240 : else
11241 15597 : return cxx_eval_constant_expression (ctx, ctor, lval,
11242 : non_constant_p, overflow_p,
11243 15597 : jump_target);
11244 : }
11245 : /* A placeholder without a referent. We can get here when
11246 : checking whether NSDMIs are noexcept, or in massage_init_elt;
11247 : just say it's non-constant for now. */
11248 243 : gcc_assert (ctx->quiet);
11249 243 : *non_constant_p = true;
11250 243 : break;
11251 :
11252 6965 : case EXIT_EXPR:
11253 6965 : {
11254 6965 : tree cond = TREE_OPERAND (t, 0);
11255 6965 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
11256 : non_constant_p, overflow_p,
11257 : jump_target);
11258 6965 : if (*jump_target)
11259 : return NULL_TREE;
11260 6965 : VERIFY_CONSTANT (cond);
11261 6965 : if (integer_nonzerop (cond))
11262 3407 : *jump_target = t;
11263 : }
11264 : break;
11265 :
11266 491 : case GOTO_EXPR:
11267 491 : if (breaks (&TREE_OPERAND (t, 0))
11268 491 : || continues (&TREE_OPERAND (t, 0)))
11269 472 : *jump_target = TREE_OPERAND (t, 0);
11270 : else
11271 : {
11272 19 : if (!ctx->quiet)
11273 1 : error_at (loc, "%<goto%> is not a constant expression");
11274 19 : *non_constant_p = true;
11275 : }
11276 : break;
11277 :
11278 6650427 : case LOOP_EXPR:
11279 6650427 : case DO_STMT:
11280 6650427 : case WHILE_STMT:
11281 6650427 : case FOR_STMT:
11282 6650427 : cxx_eval_loop_expr (ctx, t,
11283 : non_constant_p, overflow_p, jump_target);
11284 6650427 : break;
11285 :
11286 53231 : case SWITCH_EXPR:
11287 53231 : case SWITCH_STMT:
11288 53231 : cxx_eval_switch_expr (ctx, t,
11289 : non_constant_p, overflow_p, jump_target);
11290 53231 : break;
11291 :
11292 215 : case REQUIRES_EXPR:
11293 : /* It's possible to get a requires-expression in a constant
11294 : expression. For example:
11295 :
11296 : template<typename T> concept bool C() {
11297 : return requires (T t) { t; };
11298 : }
11299 :
11300 : template<typename T> requires !C<T>() void f(T);
11301 :
11302 : Normalization leaves f with the associated constraint
11303 : '!requires (T t) { ... }' which is not transformed into
11304 : a constraint. */
11305 215 : if (!processing_template_decl)
11306 215 : return evaluate_requires_expr (t);
11307 : else
11308 0 : *non_constant_p = true;
11309 0 : return t;
11310 :
11311 1395235 : case ANNOTATE_EXPR:
11312 1395235 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
11313 : lval,
11314 : non_constant_p, overflow_p,
11315 : jump_target);
11316 1395235 : break;
11317 :
11318 9366 : case USING_STMT:
11319 9366 : r = void_node;
11320 9366 : break;
11321 :
11322 68 : case ASSERTION_STMT:
11323 68 : case PRECONDITION_STMT:
11324 68 : case POSTCONDITION_STMT:
11325 68 : {
11326 68 : r = void_node;
11327 : /* Only record the first fail, and do not go further is the semantic
11328 : is 'ignore'. */
11329 68 : if (*non_constant_p
11330 68 : || (ctx->global->contract_statement
11331 0 : && contract_terminating_p (ctx->global->contract_statement))
11332 136 : || contract_ignored_p (t))
11333 : break;
11334 :
11335 68 : tree cond = CONTRACT_CONDITION (t);
11336 68 : bool ctrct_non_const_p = !potential_rvalue_constant_expression (cond);
11337 68 : tree eval = boolean_true_node;
11338 68 : if (!ctrct_non_const_p)
11339 : {
11340 : /* We need to evaluate and stash the result of this here, since
11341 : whether it needs to be reported (and how) depends on whether the
11342 : containing expression is otherwise const. */
11343 59 : bool ctrct_overflow_p = false;
11344 59 : tree jmp_target = NULL_TREE;
11345 59 : constexpr_ctx new_ctx = *ctx;
11346 59 : new_ctx.quiet = true;
11347 : /* The standard says a tentative evaluation uses the ignore
11348 : semantic; we go ahead and check conditions during evaluation to
11349 : avoid needing to do a second evaluation of anything involving
11350 : contracts, but we need to avoid any modifications to objects
11351 : from outside the predicate. If we run into any such
11352 : modifications, we set contract_condition_non_const; if the
11353 : evaluation is otherwise constant, we'll try a non-tentative
11354 : evaluation in _outermost_, which might succeed.
11355 :
11356 : During non-tentative evaluation, modification is OK. */
11357 59 : modifiable_tracker ms (new_ctx.global, /*push*/ctx->quiet);
11358 59 : eval = (cxx_eval_constant_expression
11359 59 : (&new_ctx, cond, vc_prvalue, &ctrct_non_const_p,
11360 : &ctrct_overflow_p, &jmp_target));
11361 59 : }
11362 :
11363 : /* Not a constant or check failed. */
11364 68 : if (ctrct_non_const_p || integer_zerop (eval))
11365 : {
11366 63 : ctx->global->contract_statement = t;
11367 63 : ctx->global->contract_condition_non_const = ctrct_non_const_p;
11368 : }
11369 :
11370 : /* If diagnosing, do it while we have the cx_call_context. */
11371 68 : if (!ctx->quiet
11372 68 : && check_for_failed_contracts (ctx))
11373 : /* And for a terminating semantic, stop evaluating. */
11374 24 : *non_constant_p = true;
11375 : }
11376 68 : break;
11377 :
11378 2755321 : case TEMPLATE_ID_EXPR:
11379 2755321 : {
11380 : /* We can evaluate template-id that refers to a concept only if
11381 : the template arguments are non-dependent. */
11382 2755321 : gcc_assert (concept_check_p (t));
11383 :
11384 2755321 : if (!value_dependent_expression_p (t)
11385 2755321 : && !uid_sensitive_constexpr_evaluation_p ())
11386 2754398 : r = evaluate_concept_check (t);
11387 : else
11388 923 : *non_constant_p = true;
11389 :
11390 : break;
11391 : }
11392 :
11393 51 : case ASM_EXPR:
11394 51 : if (!ctx->quiet)
11395 29 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
11396 51 : *non_constant_p = true;
11397 51 : return t;
11398 :
11399 4047 : case BIT_CAST_EXPR:
11400 4047 : if (lval)
11401 : {
11402 0 : if (!ctx->quiet)
11403 0 : error_at (EXPR_LOCATION (t),
11404 : "address of a call to %qs is not a constant expression",
11405 : "__builtin_bit_cast");
11406 0 : *non_constant_p = true;
11407 0 : return t;
11408 : }
11409 4047 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
11410 4047 : break;
11411 :
11412 0 : case OMP_PARALLEL:
11413 0 : case OMP_TASK:
11414 0 : case OMP_FOR:
11415 0 : case OMP_SIMD:
11416 0 : case OMP_DISTRIBUTE:
11417 0 : case OMP_TASKLOOP:
11418 0 : case OMP_LOOP:
11419 0 : case OMP_TEAMS:
11420 0 : case OMP_TARGET_DATA:
11421 0 : case OMP_TARGET:
11422 0 : case OMP_SECTIONS:
11423 0 : case OMP_ORDERED:
11424 0 : case OMP_CRITICAL:
11425 0 : case OMP_SINGLE:
11426 0 : case OMP_SCAN:
11427 0 : case OMP_SCOPE:
11428 0 : case OMP_SECTION:
11429 0 : case OMP_STRUCTURED_BLOCK:
11430 0 : case OMP_MASTER:
11431 0 : case OMP_MASKED:
11432 0 : case OMP_TASKGROUP:
11433 0 : case OMP_TARGET_UPDATE:
11434 0 : case OMP_TARGET_ENTER_DATA:
11435 0 : case OMP_TARGET_EXIT_DATA:
11436 0 : case OMP_ATOMIC:
11437 0 : case OMP_ATOMIC_READ:
11438 0 : case OMP_ATOMIC_CAPTURE_OLD:
11439 0 : case OMP_ATOMIC_CAPTURE_NEW:
11440 0 : case OMP_DEPOBJ:
11441 0 : case OMP_ALLOCATE:
11442 0 : case OACC_PARALLEL:
11443 0 : case OACC_KERNELS:
11444 0 : case OACC_SERIAL:
11445 0 : case OACC_DATA:
11446 0 : case OACC_HOST_DATA:
11447 0 : case OACC_LOOP:
11448 0 : case OACC_CACHE:
11449 0 : case OACC_DECLARE:
11450 0 : case OACC_ENTER_DATA:
11451 0 : case OACC_EXIT_DATA:
11452 0 : case OACC_UPDATE:
11453 0 : if (!ctx->quiet)
11454 0 : error_at (EXPR_LOCATION (t),
11455 : "statement is not a constant expression");
11456 0 : *non_constant_p = true;
11457 0 : break;
11458 :
11459 0 : default:
11460 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
11461 : {
11462 : /* This function doesn't know how to deal with pre-genericize
11463 : statements; this can only happen with statement-expressions,
11464 : so for now just fail. */
11465 0 : if (!ctx->quiet)
11466 0 : error_at (EXPR_LOCATION (t),
11467 : "statement is not a constant expression");
11468 : }
11469 0 : else if (flag_checking)
11470 0 : internal_error ("unexpected expression %qE of kind %s", t,
11471 : get_tree_code_name (TREE_CODE (t)));
11472 0 : *non_constant_p = true;
11473 0 : break;
11474 : }
11475 :
11476 2055527929 : if (r == error_mark_node)
11477 14528812 : *non_constant_p = true;
11478 :
11479 135081028 : if (r == void_node && lval != vc_discard && !*jump_target
11480 2055545824 : && !VOID_TYPE_P (TREE_TYPE (t)))
11481 : {
11482 : /* For diagnostic quality we should have handled this sooner, where we
11483 : can be more specific about the out-of-lifetime object. But here we
11484 : can still be correct. */
11485 2 : gcc_checking_assert (false);
11486 : if (!ctx->quiet)
11487 : error_at (EXPR_LOCATION (t),
11488 : "%qE accesses an object outside its lifetime", t);
11489 : *non_constant_p = true;
11490 : }
11491 :
11492 2055527927 : if (*non_constant_p)
11493 : return t;
11494 : else
11495 1636430789 : return r;
11496 : }
11497 :
11498 : /* P0859: A function is needed for constant evaluation if it is a constexpr
11499 : function that is named by an expression ([basic.def.odr]) that is
11500 : potentially constant evaluated.
11501 :
11502 : So we need to instantiate any constexpr functions mentioned by the
11503 : expression even if the definition isn't needed for evaluating the
11504 : expression. */
11505 :
11506 : static tree
11507 589506163 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
11508 : {
11509 589506163 : if (TREE_CODE (*tp) == FUNCTION_DECL
11510 12147325 : && DECL_DECLARED_CONSTEXPR_P (*tp)
11511 12011515 : && !DECL_INITIAL (*tp)
11512 3094509 : && !trivial_fn_p (*tp)
11513 3094360 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
11514 589506163 : && !uid_sensitive_constexpr_evaluation_p ())
11515 : {
11516 3063732 : ++function_depth;
11517 3063732 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
11518 3063700 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
11519 : else
11520 32 : synthesize_method (*tp);
11521 3063732 : --function_depth;
11522 : }
11523 586442431 : else if (TREE_CODE (*tp) == CALL_EXPR
11524 574776522 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
11525 : {
11526 12215789 : if (EXPR_HAS_LOCATION (*tp))
11527 12208839 : input_location = EXPR_LOCATION (*tp);
11528 : }
11529 :
11530 589506163 : if (!EXPR_P (*tp))
11531 336695477 : *walk_subtrees = 0;
11532 :
11533 589506163 : return NULL_TREE;
11534 : }
11535 :
11536 : static void
11537 287380625 : instantiate_constexpr_fns (tree t)
11538 : {
11539 287380625 : location_t loc = input_location;
11540 287380625 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
11541 287380625 : input_location = loc;
11542 287380625 : }
11543 :
11544 : /* Look for heap variables in the expression *TP. */
11545 :
11546 : static tree
11547 375562 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
11548 : {
11549 375562 : if (VAR_P (*tp) && heap_var_p (*tp, /*include_deleted_p=*/true))
11550 113029 : return *tp;
11551 :
11552 262533 : if (TYPE_P (*tp))
11553 66 : *walk_subtrees = 0;
11554 : return NULL_TREE;
11555 : }
11556 :
11557 : /* Find immediate function decls in *TP if any. */
11558 :
11559 : static tree
11560 620513326 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
11561 : {
11562 624543726 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
11563 : return *tp;
11564 620506684 : if (TREE_CODE (*tp) == PTRMEM_CST
11565 29657 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
11566 620535524 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
11567 : return PTRMEM_CST_MEMBER (*tp);
11568 620506571 : if (REFLECT_EXPR_P (*tp))
11569 86789 : *walk_subtrees = 0;
11570 : return NULL_TREE;
11571 : }
11572 :
11573 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
11574 : expression. Return a version of T that has TREE_CONSTANT cleared. */
11575 :
11576 : static tree
11577 147384 : mark_non_constant (tree t)
11578 : {
11579 147384 : gcc_checking_assert (TREE_CONSTANT (t));
11580 :
11581 : /* This isn't actually constant, so unset TREE_CONSTANT.
11582 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
11583 : it to be set if it is invariant address, even when it is not
11584 : a valid C++ constant expression. Wrap it with a NOP_EXPR
11585 : instead. */
11586 147384 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
11587 145403 : t = copy_node (t);
11588 1981 : else if (TREE_CODE (t) == CONSTRUCTOR)
11589 480 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
11590 : else
11591 1501 : t = build_nop (TREE_TYPE (t), t);
11592 147384 : TREE_CONSTANT (t) = false;
11593 147384 : return t;
11594 : }
11595 :
11596 : /* If we have a successful constant evaluation, now check whether there is
11597 : a failed or non-constant contract that would invalidate this. */
11598 :
11599 : static bool
11600 33 : check_for_failed_contracts (const constexpr_ctx *ctx)
11601 : {
11602 33 : constexpr_global_ctx *const global_ctx = ctx->global;
11603 33 : if (!flag_contracts || !global_ctx->contract_statement)
11604 : return false;
11605 :
11606 30 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
11607 30 : tree cond = CONTRACT_CONDITION (global_ctx->contract_statement);
11608 30 : loc = cp_expr_loc_or_loc (cond, loc);
11609 30 : enum diagnostics::kind kind;
11610 30 : bool error = false;
11611 : /* [intro.compliance.general]/2.3.4. */
11612 : /* [basic.contract.eval]/8. */
11613 30 : if (ctx->manifestly_const_eval != mce_true)
11614 : {
11615 : /* When !MCE, silently return not constant. */
11616 : return true;
11617 : }
11618 30 : else if (contract_terminating_p (global_ctx->contract_statement))
11619 : {
11620 : kind = diagnostics::kind::error;
11621 : error = true;
11622 : }
11623 : else
11624 6 : kind = diagnostics::kind::warning;
11625 :
11626 30 : auto_diagnostic_group d;
11627 :
11628 : /* [basic.contract.eval]/7.3 */
11629 30 : if (global_ctx->contract_condition_non_const)
11630 : {
11631 6 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
11632 6 : if (error)
11633 : {
11634 6 : bool nc = false, ov = false; tree jmp = NULL_TREE;
11635 6 : cxx_eval_constant_expression (ctx, cond, vc_prvalue, &nc, &ov, &jmp);
11636 6 : gcc_checking_assert (nc||ov||jmp);
11637 : }
11638 : return error;
11639 : }
11640 :
11641 : /* Otherwise, the evaluation was const, but determined to be false. */
11642 24 : emit_diagnostic (kind, loc, 0,
11643 : "contract predicate is false in constant expression");
11644 : /* Workaround: Avoid redundant "in expansion of macro". */
11645 24 : loc = linemap_resolve_location (line_table, loc,
11646 : LRK_SPELLING_LOCATION, NULL);
11647 24 : diagnose_failing_condition (cond, loc, /*show_expr*/true, ctx);
11648 24 : return error;
11649 30 : }
11650 :
11651 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11652 : STRICT has the same sense as for constant_value_1: true if we only allow
11653 : conforming C++ constant expressions, or false if we want a constant value
11654 : even if it doesn't conform.
11655 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11656 : per P0595 even when ALLOW_NON_CONSTANT is true.
11657 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
11658 : OBJECT must be non-NULL in that case. */
11659 :
11660 : static tree
11661 573704284 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
11662 : bool strict = true,
11663 : mce_value manifestly_const_eval = mce_unknown,
11664 : bool constexpr_dtor = false,
11665 : tree object = NULL_TREE)
11666 : {
11667 573704284 : auto_timevar time (TV_CONSTEXPR);
11668 :
11669 573704284 : bool non_constant_p = false;
11670 573704284 : bool overflow_p = false;
11671 :
11672 573704284 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
11673 : {
11674 0 : gcc_checking_assert (allow_non_constant);
11675 : return t;
11676 : }
11677 :
11678 573704284 : constexpr_global_ctx global_ctx;
11679 573704284 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
11680 : allow_non_constant, strict,
11681 573704284 : !allow_non_constant ? mce_true : manifestly_const_eval };
11682 :
11683 : /* Turn off -frounding-math for manifestly constant evaluation. */
11684 573704284 : warning_sentinel rm (flag_rounding_math,
11685 573704284 : ctx.manifestly_const_eval == mce_true);
11686 573704284 : tree type = (object
11687 573704284 : ? cv_unqualified (TREE_TYPE (object))
11688 507652946 : : initialized_type (t));
11689 573704284 : tree r = t;
11690 573704284 : bool is_consteval = false;
11691 573704284 : if (VOID_TYPE_P (type))
11692 : {
11693 9912945 : if (!constexpr_dtor)
11694 : {
11695 9912945 : if (cxx_dialect < cxx20)
11696 : return t;
11697 : /* We could have a COMPOUND_EXPR here coming from
11698 : keep_unused_object_arg. */
11699 9901891 : tree x = extract_call_expr (t);
11700 9901891 : if (x == NULL_TREE || x == error_mark_node)
11701 : return t;
11702 : /* Calls to immediate functions returning void need to be
11703 : evaluated. */
11704 9901849 : tree fndecl = cp_get_callee_fndecl_nofold (x);
11705 19803698 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
11706 : return t;
11707 : else
11708 652 : is_consteval = true;
11709 652 : tree lam;
11710 652 : if (manifestly_const_eval == mce_true
11711 1198 : && LAMBDA_FUNCTION_P (fndecl)
11712 540 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
11713 1192 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
11714 533 : global_ctx.consteval_block = fndecl;
11715 : }
11716 : }
11717 563791339 : else if (cxx_dialect >= cxx20
11718 554900834 : && (TREE_CODE (t) == CALL_EXPR
11719 476875727 : || TREE_CODE (t) == AGGR_INIT_EXPR
11720 471829302 : || TREE_CODE (t) == TARGET_EXPR))
11721 : {
11722 88651221 : tree x = t;
11723 88651221 : if (TREE_CODE (x) == TARGET_EXPR)
11724 5579689 : x = TARGET_EXPR_INITIAL (x);
11725 88651221 : tree fndecl = cp_get_callee_fndecl_nofold (x);
11726 174386688 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
11727 : is_consteval = true;
11728 : /* Don't try to evaluate a std::vector constructor taking an integer, it
11729 : will fail in the 'if (heap_var)' block below after doing all the work
11730 : (c++/113835). This will need adjustment if P3554 is accepted. Note
11731 : that evaluation of e.g. the vector default constructor can succeed, so
11732 : we don't shortcut all vector constructors. */
11733 171470934 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
11734 11379185 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
11735 88675809 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
11736 : return t;
11737 : }
11738 563789934 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
11739 : {
11740 : /* In C++14 an NSDMI can participate in aggregate initialization,
11741 : and can refer to the address of the object being initialized, so
11742 : we need to pass in the relevant VAR_DECL if we want to do the
11743 : evaluation in a single pass. The evaluation will dynamically
11744 : update ctx.values for the VAR_DECL. We use the same strategy
11745 : for C++11 constexpr constructors that refer to the object being
11746 : initialized. */
11747 49479665 : if (constexpr_dtor)
11748 : {
11749 202 : gcc_assert (object && VAR_P (object));
11750 202 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
11751 202 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
11752 202 : if (error_operand_p (DECL_INITIAL (object)))
11753 : return t;
11754 185 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
11755 185 : TREE_READONLY (ctx.ctor) = false;
11756 : /* Temporarily force decl_really_constant_value to return false
11757 : for it, we want to use ctx.ctor for the current value instead. */
11758 185 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
11759 : }
11760 : else
11761 : {
11762 49479463 : ctx.ctor = build_constructor (type, NULL);
11763 49479463 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
11764 : }
11765 49479648 : if (!object)
11766 : {
11767 21961703 : if (TREE_CODE (t) == CALL_EXPR)
11768 : {
11769 : /* If T is calling a constructor to initialize an object, reframe
11770 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
11771 : from outside the constant evaluation, which will fail even if
11772 : the value is actually constant (is_constant_evaluated3.C). */
11773 9884025 : tree fn = cp_get_callee_fndecl_nofold (t);
11774 19767842 : if (fn && DECL_CONSTRUCTOR_P (fn))
11775 : {
11776 4050786 : object = CALL_EXPR_ARG (t, 0);
11777 4050786 : object = build_fold_indirect_ref (object);
11778 4050786 : r = build_aggr_init_expr (type, r);
11779 : }
11780 : }
11781 12077678 : else if (TREE_CODE (t) == TARGET_EXPR)
11782 5442701 : object = TARGET_EXPR_SLOT (t);
11783 6634977 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
11784 697797 : object = AGGR_INIT_EXPR_SLOT (t);
11785 : }
11786 49479648 : ctx.object = object;
11787 49479648 : if (object)
11788 37709229 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
11789 : (type, TREE_TYPE (object)));
11790 37709229 : if (object && DECL_P (object))
11791 31622214 : global_ctx.put_value (object, ctx.ctor);
11792 49479648 : if (TREE_CODE (r) == TARGET_EXPR)
11793 : /* Avoid creating another CONSTRUCTOR when we expand the
11794 : TARGET_EXPR. */
11795 5618691 : r = TARGET_EXPR_INITIAL (r);
11796 : }
11797 :
11798 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
11799 : constexpr evaluation to avoid unnecessary template instantiation, and is
11800 : always done with mce_unknown. But due to gaps in the restriction logic
11801 : we may still end up taking an evaluation path that in turn requires
11802 : manifestly constant evaluation, and such evaluation must not be
11803 : restricted since it likely has semantic consequences.
11804 : TODO: Remove/replace the mechanism in GCC 17. */
11805 563789917 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
11806 563789917 : if (ctx.manifestly_const_eval == mce_true)
11807 287380631 : uid_sensitive_constexpr_evaluation_value = false;
11808 :
11809 563789917 : auto_vec<tree, 16> cleanups;
11810 563789917 : global_ctx.cleanups = &cleanups;
11811 :
11812 563789917 : if (manifestly_const_eval == mce_true)
11813 287380625 : instantiate_constexpr_fns (r);
11814 563789917 : tree jmp_target = NULL_TREE;
11815 563789917 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
11816 : &non_constant_p, &overflow_p,
11817 : &jmp_target);
11818 563788814 : if (throws (&jmp_target) && !non_constant_p)
11819 : {
11820 1598 : if (!ctx.quiet)
11821 396 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11822 1598 : non_constant_p = true;
11823 1598 : jmp_target = NULL_TREE;
11824 1598 : r = t;
11825 : }
11826 563785618 : else if (!non_constant_p && jmp_target)
11827 : {
11828 24 : non_constant_p = true;
11829 24 : if (!ctx.quiet)
11830 : {
11831 3 : if (breaks (&jmp_target))
11832 3 : error ("%<break%> outside of a loop or %<switch%>");
11833 0 : else if (continues (&jmp_target))
11834 0 : error ("%<continue%> outside of a loop");
11835 0 : else if (returns (&jmp_target))
11836 0 : error ("%<return%> in a statement expression");
11837 : else
11838 0 : gcc_unreachable ();
11839 : }
11840 24 : r = t;
11841 : }
11842 :
11843 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
11844 : of statements, and the result ought to be stored in ctx.ctor. */
11845 563787216 : if (r == void_node && !constexpr_dtor && ctx.ctor)
11846 0 : r = ctx.ctor;
11847 :
11848 563787216 : unsigned int i;
11849 563787216 : tree cleanup;
11850 563787216 : jmp_target = NULL_TREE;
11851 : /* Evaluate the cleanups. */
11852 1127672243 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
11853 97811 : if (cleanup == NULL_TREE)
11854 : /* NULL_TREE cleanup is a marker that before it is
11855 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
11856 23 : --i;
11857 : else
11858 97788 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
11859 : &non_constant_p, &overflow_p,
11860 : &jmp_target);
11861 563787216 : if (throws (&jmp_target) && !non_constant_p)
11862 : {
11863 0 : if (!ctx.quiet)
11864 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11865 0 : non_constant_p = true;
11866 0 : r = t;
11867 : }
11868 :
11869 : /* Mutable logic is a bit tricky: we want to allow initialization of
11870 : constexpr variables with mutable members, but we can't copy those
11871 : members to another constexpr variable. */
11872 563787216 : if (!non_constant_p
11873 414016471 : && TREE_CODE (r) == CONSTRUCTOR
11874 580882817 : && CONSTRUCTOR_MUTABLE_POISON (r))
11875 : {
11876 93 : if (!allow_non_constant)
11877 6 : error ("%qE is not a constant expression because it refers to "
11878 : "mutable subobjects of %qT", t, type);
11879 93 : non_constant_p = true;
11880 : }
11881 :
11882 414016378 : if (!non_constant_p && cxx_dialect >= cxx20
11883 977803594 : && !global_ctx.heap_vars.is_empty ())
11884 : {
11885 122075 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11886 : NULL);
11887 122075 : unsigned int i;
11888 122075 : if (heap_var)
11889 : {
11890 113029 : if (!allow_non_constant && !non_constant_p)
11891 : {
11892 17 : auto_diagnostic_group d;
11893 17 : if (DECL_LANG_SPECIFIC (heap_var))
11894 4 : error ("%qE is not a constant expression because it refers to "
11895 : "exception object allocated with "
11896 : "%<__cxa_allocate_exception%>", t);
11897 : else
11898 13 : error ("%qE is not a constant expression because it refers to "
11899 : "a result of %<operator new%>", t);
11900 17 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11901 17 : }
11902 113029 : r = t;
11903 113029 : non_constant_p = true;
11904 : }
11905 317452 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11906 : {
11907 195377 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11908 : {
11909 113601 : if (!allow_non_constant && !non_constant_p)
11910 : {
11911 16 : auto_diagnostic_group d;
11912 16 : error ("%qE is not a constant expression because allocated "
11913 : "storage has not been deallocated", t);
11914 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11915 16 : }
11916 113601 : r = t;
11917 113601 : non_constant_p = true;
11918 : }
11919 : }
11920 : }
11921 :
11922 : /* Check that immediate invocation does not return an expression referencing
11923 : any immediate function decls. */
11924 563787216 : if (!non_constant_p && cxx_dialect >= cxx20)
11925 816415208 : if (tree immediate_fndecl
11926 408207604 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11927 : NULL))
11928 : {
11929 6755 : if (!allow_non_constant && !non_constant_p)
11930 : {
11931 78 : if (is_consteval)
11932 36 : error_at (cp_expr_loc_or_input_loc (t),
11933 : "immediate evaluation returns address of immediate "
11934 : "function %qD", immediate_fndecl);
11935 : else
11936 64 : error_at (cp_expr_loc_or_input_loc (t),
11937 : "constant evaluation returns address of immediate "
11938 : "function %qD", immediate_fndecl);
11939 : }
11940 6755 : r = t;
11941 6755 : non_constant_p = true;
11942 : }
11943 :
11944 563787216 : if (!non_constant_p && !constexpr_dtor)
11945 413896347 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11946 :
11947 : /* After verify_constant because reduced_constant_expression_p can unset
11948 : CONSTRUCTOR_NO_CLEARING. */
11949 563787216 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11950 : {
11951 70503 : if (!allow_non_constant)
11952 45 : error ("%qE is not a constant expression because it refers to "
11953 : "an incompletely initialized variable", t);
11954 70503 : TREE_CONSTANT (r) = false;
11955 70503 : non_constant_p = true;
11956 : }
11957 :
11958 563787216 : if (non_constant_p)
11959 : /* If we saw something bad, go back to our argument. The wrapping below is
11960 : only for the cases of TREE_CONSTANT argument or overflow. */
11961 152316731 : r = t;
11962 :
11963 563787216 : if (!non_constant_p && overflow_p)
11964 215 : non_constant_p = true;
11965 :
11966 : /* If allow_non_constant, we delay diagnosing contracts until we know
11967 : that the expression is otherwise constant.
11968 :
11969 : Various constexpr contract violation scenarios:
11970 : * MCE, non-SFINAE, terminating: diagnose and mark non-constant
11971 : during evaluation
11972 : * MCE, non-SFINAE, non-terminating: diagnose during evaluation
11973 : * MCE, SFINAE, terminating: diagnose in repeated eval, return
11974 : error_mark_node for SFINAE
11975 : * MCE, SFINAE, non-terminating: diagnose in repeated eval
11976 : * non-MCE: diagnose in repeated eval, set non-constant so run-time
11977 : violation handler also sees the violation
11978 :
11979 : For MCE+terminating, the necessary semantics are just a hard error; what
11980 : happens after that is error-recovery.
11981 :
11982 : In the non-SFINAE (!allow_non_constant) case I think it's clearly
11983 : preferable to stop evaluation after the violation, which we do by marking
11984 : the evaluation non-constant. We could work harder to return
11985 : error_mark_node instead like the SFINAE case below, but that seems
11986 : unnecessary.
11987 :
11988 : The allow_non_constant case is trickier, because that expresses both
11989 : actual SFINAE and cases like finish_static_assert where we do one trial
11990 : evaluation to see if it's constant, and then another to get the actual
11991 : error if needed. So if we set non_constant_p like for the
11992 : !allow_non_constant case, we get SFINAE consistent with
11993 : e.g. _GLIBCXX_ASSERT_FAIL, but we get redundant errors with static_assert.
11994 : We can finesse it a bit more by returning error_mark_node, which
11995 : finish_static_assert distinguishes from non-constant.
11996 :
11997 : ??? cp_fold_immediate_r should change to follow the finish_static_assert
11998 : pattern (using maybe_constant_value instead of cxx_constant_value for the
11999 : trial evaluation) once the consteval-only values overhaul goes in. */
12000 :
12001 563787216 : if (!non_constant_p && allow_non_constant
12002 275229035 : && global_ctx.contract_statement)
12003 : {
12004 : /* Repeat evaluation to get cx_call_context for the diagnostic or
12005 : perform any modifications we didn't allow the first time. */
12006 24 : r = cxx_eval_outermost_constant_expr (t, /*allow*/false, strict,
12007 : manifestly_const_eval,
12008 : constexpr_dtor, object);
12009 24 : bool term = contract_terminating_p (global_ctx.contract_statement);
12010 24 : if (!term && ctx.manifestly_const_eval != mce_true)
12011 : /* We're observing and not MCE, treat the evaluation as non-constant so
12012 : we observe it again at runtime. */
12013 0 : non_constant_p = true;
12014 18 : else if (term && !TREE_CONSTANT (r))
12015 : /* Substitution failure (see discussion above). We check TREE_CONSTANT
12016 : to handle the case where in the trial evaluation we set
12017 : contract_condition_non_const due to modifications we needed to
12018 : ignore, but on reevaluation the contract is satisfied. */
12019 15 : return error_mark_node;
12020 : else
12021 9 : return r;
12022 : }
12023 :
12024 : /* Unshare the result. */
12025 563787192 : bool should_unshare = true;
12026 563787192 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
12027 1662523 : && TARGET_EXPR_INITIAL (t) == r))
12028 : should_unshare = false;
12029 :
12030 563787192 : if (non_constant_p && !allow_non_constant)
12031 4194 : return error_mark_node;
12032 563782998 : else if (non_constant_p && TREE_CONSTANT (r))
12033 142882 : r = mark_non_constant (r);
12034 563640116 : else if (non_constant_p)
12035 : return t;
12036 :
12037 411613128 : if (constexpr_dtor)
12038 : {
12039 167 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
12040 167 : return r;
12041 : }
12042 :
12043 : /* Check we are not trying to return the wrong type. */
12044 411612961 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
12045 : {
12046 : /* If so, this is not a constant expression. */
12047 151 : if (!allow_non_constant)
12048 0 : error ("%qE is not a constant expression because it initializes "
12049 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
12050 : return t;
12051 : }
12052 :
12053 411612810 : if (should_unshare)
12054 231119671 : r = unshare_expr (r);
12055 :
12056 411612810 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
12057 : {
12058 16093699 : r = adjust_temp_type (type, r);
12059 16093699 : if (TREE_CODE (t) == TARGET_EXPR
12060 16093699 : && TARGET_EXPR_INITIAL (t) == r)
12061 : return t;
12062 14976036 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
12063 2478589 : || TREE_CODE (t) == AGGR_INIT_EXPR)
12064 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
12065 826387 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
12066 782 : r = get_target_expr (r);
12067 825605 : else if (processing_template_decl)
12068 : /* Don't insert TARGET_EXPR in template trees. */
12069 : return r;
12070 : else
12071 : {
12072 825602 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
12073 825602 : TREE_CONSTANT (r) = true;
12074 : }
12075 : }
12076 :
12077 410495144 : if (TREE_CODE (t) == TARGET_EXPR
12078 544860 : && TREE_CODE (r) == TARGET_EXPR)
12079 : {
12080 : /* Preserve this flag for potential_constant_expression, and the others
12081 : for good measure. */
12082 544746 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
12083 544746 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
12084 544746 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
12085 544746 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
12086 : }
12087 :
12088 : /* Remember the original location if that wouldn't need a wrapper. */
12089 410495144 : if (location_t loc = EXPR_LOCATION (t))
12090 187949265 : protected_set_expr_location (r, loc);
12091 :
12092 410495144 : return r;
12093 573701583 : }
12094 :
12095 : /* If T represents a constant expression returns its reduced value.
12096 : Otherwise return error_mark_node. */
12097 :
12098 : tree
12099 157464453 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
12100 : tsubst_flags_t complain /* = tf_error */)
12101 : {
12102 157464453 : bool sfinae = !(complain & tf_error);
12103 157464453 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
12104 157464453 : if (sfinae && !TREE_CONSTANT (r))
12105 3021 : r = error_mark_node;
12106 157464453 : return r;
12107 : }
12108 :
12109 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
12110 : of constexpr variables. The actual initializer of DECL is not modified. */
12111 :
12112 : void
12113 202 : cxx_constant_dtor (tree t, tree decl)
12114 : {
12115 202 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
12116 202 : }
12117 :
12118 : /* Helper routine for fold_simple function. Either return simplified
12119 : expression T, otherwise NULL_TREE.
12120 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
12121 : even if we are within template-declaration. So be careful on call, as in
12122 : such case types can be undefined. */
12123 :
12124 : static tree
12125 169359787 : fold_simple_1 (tree t)
12126 : {
12127 169359787 : tree op1;
12128 169359787 : enum tree_code code = TREE_CODE (t);
12129 :
12130 169359787 : switch (code)
12131 : {
12132 : case INTEGER_CST:
12133 : case REAL_CST:
12134 : case VECTOR_CST:
12135 : case FIXED_CST:
12136 : case COMPLEX_CST:
12137 : return t;
12138 :
12139 1800961 : case SIZEOF_EXPR:
12140 1800961 : return fold_sizeof_expr (t);
12141 :
12142 40147735 : case ABS_EXPR:
12143 40147735 : case ABSU_EXPR:
12144 40147735 : case CONJ_EXPR:
12145 40147735 : case REALPART_EXPR:
12146 40147735 : case IMAGPART_EXPR:
12147 40147735 : case NEGATE_EXPR:
12148 40147735 : case BIT_NOT_EXPR:
12149 40147735 : case TRUTH_NOT_EXPR:
12150 40147735 : case VIEW_CONVERT_EXPR:
12151 40147735 : CASE_CONVERT:
12152 40147735 : case FLOAT_EXPR:
12153 40147735 : case FIX_TRUNC_EXPR:
12154 40147735 : case FIXED_CONVERT_EXPR:
12155 40147735 : case ADDR_SPACE_CONVERT_EXPR:
12156 :
12157 40147735 : op1 = TREE_OPERAND (t, 0);
12158 :
12159 40147735 : t = const_unop (code, TREE_TYPE (t), op1);
12160 40147735 : if (!t)
12161 : return NULL_TREE;
12162 :
12163 6219308 : if (CONVERT_EXPR_CODE_P (code)
12164 6219308 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
12165 0 : TREE_OVERFLOW (t) = false;
12166 : return t;
12167 :
12168 : default:
12169 : return NULL_TREE;
12170 : }
12171 : }
12172 :
12173 : /* If T is a simple constant expression, returns its simplified value.
12174 : Otherwise returns T. In contrast to maybe_constant_value we
12175 : simplify only few operations on constant-expressions, and we don't
12176 : try to simplify constexpressions. */
12177 :
12178 : tree
12179 170159315 : fold_simple (tree t)
12180 : {
12181 170159315 : if (processing_template_decl)
12182 : return t;
12183 :
12184 169359787 : tree r = fold_simple_1 (t);
12185 169359787 : if (r)
12186 119069698 : return r;
12187 :
12188 : return t;
12189 : }
12190 :
12191 : /* Try folding the expression T to a simple constant.
12192 : Returns that constant, otherwise returns T. */
12193 :
12194 : tree
12195 1649013 : fold_to_constant (tree t)
12196 : {
12197 1649013 : if (processing_template_decl)
12198 : return t;
12199 :
12200 1554344 : tree r = fold (t);
12201 1554344 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
12202 : return r;
12203 : else
12204 139759 : return t;
12205 : }
12206 :
12207 : /* If T is a constant expression, returns its reduced value.
12208 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
12209 : Otherwise, returns a version of T without TREE_CONSTANT.
12210 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
12211 : as per P0595. */
12212 :
12213 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
12214 :
12215 : tree
12216 862866777 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
12217 : mce_value manifestly_const_eval /* = mce_unknown */)
12218 : {
12219 862866777 : tree orig_t = t;
12220 862866777 : tree r;
12221 :
12222 862866777 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
12223 : {
12224 : /* Look up each operand in the cv_cache first to see if we've already
12225 : reduced it, and reuse that result to avoid quadratic behavior if
12226 : we're called when building up a large expression. */
12227 250048022 : int n = cp_tree_operand_length (t);
12228 250048022 : tree *ops = XALLOCAVEC (tree, n);
12229 250048022 : bool rebuild = false;
12230 699849574 : for (int i = 0; i < n; ++i)
12231 : {
12232 449801552 : ops[i] = TREE_OPERAND (t, i);
12233 930002779 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
12234 31009530 : if (*cached != ops[i])
12235 : {
12236 9636928 : ops[i] = *cached;
12237 9636928 : rebuild = true;
12238 : }
12239 : }
12240 250048022 : if (rebuild)
12241 : {
12242 8090084 : t = copy_node (t);
12243 26360646 : for (int i = 0; i < n; ++i)
12244 18270562 : TREE_OPERAND (t, i) = ops[i];
12245 : }
12246 : }
12247 :
12248 862866777 : if (!is_nondependent_constant_expression (t))
12249 : {
12250 0 : if (TREE_OVERFLOW_P (t)
12251 127870600 : || (!processing_template_decl && TREE_CONSTANT (t)))
12252 4502 : t = mark_non_constant (t);
12253 127870600 : return t;
12254 : }
12255 734996177 : else if (CONSTANT_CLASS_P (t))
12256 : /* No caching or evaluation needed. */
12257 : return t;
12258 :
12259 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
12260 : but at least try folding it to a simple constant. */
12261 336097847 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
12262 500508 : return fold_to_constant (t);
12263 :
12264 319196092 : if (manifestly_const_eval != mce_unknown)
12265 : /* TODO: Extend the cache to be mce_value aware. And if we have a
12266 : previously cached mce_unknown result that's TREE_CONSTANT, it means
12267 : the reduced value is independent of mce_value and so we should
12268 : be able to reuse it in the mce_true/false case. */
12269 171818198 : return cxx_eval_outermost_constant_expr (t, true, true,
12270 171815499 : manifestly_const_eval, false, decl);
12271 :
12272 163779141 : if (cv_cache == NULL)
12273 149964 : cv_cache = hash_map<tree, tree>::create_ggc (101);
12274 163779141 : if (tree *cached = cv_cache->get (t))
12275 : {
12276 13301876 : r = *cached;
12277 13301876 : if (r != t)
12278 : {
12279 : /* Clear processing_template_decl for sake of break_out_target_exprs;
12280 : entries in the cv_cache are non-templated. */
12281 4483944 : processing_template_decl_sentinel ptds;
12282 :
12283 4483944 : r = break_out_target_exprs (r, /*clear_loc*/true);
12284 4483944 : protected_set_expr_location (r, EXPR_LOCATION (t));
12285 4483944 : }
12286 13301876 : return r;
12287 : }
12288 :
12289 150477265 : uid_sensitive_constexpr_evaluation_checker c;
12290 150477265 : r = cxx_eval_outermost_constant_expr (t, true, true,
12291 : manifestly_const_eval, false, decl);
12292 150477265 : gcc_checking_assert (r == t
12293 : || CONVERT_EXPR_P (t)
12294 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
12295 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
12296 : || !cp_tree_equal (r, t));
12297 150477265 : if (!c.evaluation_restricted_p ())
12298 150237003 : cv_cache->put (orig_t, r);
12299 : return r;
12300 : }
12301 :
12302 : /* Dispose of the whole CV_CACHE. */
12303 :
12304 : static void
12305 40663383 : clear_cv_cache (void)
12306 : {
12307 40663383 : if (cv_cache != NULL)
12308 40328297 : cv_cache->empty ();
12309 40663383 : }
12310 :
12311 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
12312 :
12313 : void
12314 40663383 : clear_cv_and_fold_caches ()
12315 : {
12316 40663383 : clear_cv_cache ();
12317 40663383 : clear_fold_cache ();
12318 40663383 : }
12319 :
12320 : /* Internal function handling expressions in templates for
12321 : fold_non_dependent_expr and fold_non_dependent_init.
12322 :
12323 : If we're in a template, but T isn't value dependent, simplify
12324 : it. We're supposed to treat:
12325 :
12326 : template <typename T> void f(T[1 + 1]);
12327 : template <typename T> void f(T[2]);
12328 :
12329 : as two declarations of the same function, for example. */
12330 :
12331 : static tree
12332 31560489 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
12333 : bool manifestly_const_eval,
12334 : tree object)
12335 : {
12336 31560489 : gcc_assert (processing_template_decl);
12337 :
12338 31560489 : if (is_nondependent_constant_expression (t))
12339 : {
12340 17562261 : processing_template_decl_sentinel s;
12341 17562261 : t = instantiate_non_dependent_expr_internal (t, complain);
12342 :
12343 17562261 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
12344 : {
12345 0 : if (TREE_OVERFLOW_P (t))
12346 : {
12347 0 : t = build_nop (TREE_TYPE (t), t);
12348 0 : TREE_CONSTANT (t) = false;
12349 : }
12350 : return t;
12351 : }
12352 17562261 : else if (CONSTANT_CLASS_P (t))
12353 : /* No evaluation needed. */
12354 : return t;
12355 :
12356 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
12357 : but at least try folding it to a simple constant. */
12358 4648039 : if (cp_unevaluated_operand && !manifestly_const_eval)
12359 89 : return fold_to_constant (t);
12360 :
12361 4647950 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
12362 : mce_value (manifestly_const_eval),
12363 : false, object);
12364 : /* cp_tree_equal looks through NOPs, so allow them. */
12365 4647950 : gcc_checking_assert (r == t
12366 : || CONVERT_EXPR_P (t)
12367 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
12368 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
12369 : || !cp_tree_equal (r, t));
12370 : return r;
12371 17562261 : }
12372 13998228 : else if (TREE_OVERFLOW_P (t))
12373 : {
12374 0 : t = build_nop (TREE_TYPE (t), t);
12375 0 : TREE_CONSTANT (t) = false;
12376 : }
12377 :
12378 : return t;
12379 : }
12380 :
12381 : /* Like maybe_constant_value but first fully instantiate the argument.
12382 :
12383 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
12384 : followed by maybe_constant_value but is more efficient,
12385 : because it calls instantiation_dependent_expression_p and
12386 : potential_constant_expression at most once.
12387 : The manifestly_const_eval argument is passed to maybe_constant_value.
12388 :
12389 : Callers should generally pass their active complain, or if they are in a
12390 : non-template, diagnosing context, they can use the default of
12391 : tf_warning_or_error. Callers that might be within a template context, don't
12392 : have a complain parameter, and aren't going to remember the result for long
12393 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
12394 : appropriately. */
12395 :
12396 : tree
12397 264774890 : fold_non_dependent_expr (tree t,
12398 : tsubst_flags_t complain /* = tf_warning_or_error */,
12399 : bool manifestly_const_eval /* = false */,
12400 : tree object /* = NULL_TREE */)
12401 : {
12402 264774890 : if (t == NULL_TREE)
12403 : return NULL_TREE;
12404 :
12405 263666880 : if (processing_template_decl)
12406 30436393 : return fold_non_dependent_expr_template (t, complain,
12407 30436393 : manifestly_const_eval, object);
12408 :
12409 233230487 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
12410 : }
12411 :
12412 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
12413 : return the original expression. */
12414 :
12415 : tree
12416 2358417 : maybe_fold_non_dependent_expr (tree expr,
12417 : tsubst_flags_t complain/*=tf_warning_or_error*/)
12418 : {
12419 2358417 : tree t = fold_non_dependent_expr (expr, complain);
12420 2358417 : if (t && TREE_CONSTANT (t))
12421 1123558 : return t;
12422 :
12423 : return expr;
12424 : }
12425 :
12426 : /* Like maybe_constant_init but first fully instantiate the argument. */
12427 :
12428 : tree
12429 62792886 : fold_non_dependent_init (tree t,
12430 : tsubst_flags_t complain /*=tf_warning_or_error*/,
12431 : bool manifestly_const_eval /*=false*/,
12432 : tree object /* = NULL_TREE */)
12433 : {
12434 62792886 : if (t == NULL_TREE)
12435 : return NULL_TREE;
12436 :
12437 62792886 : if (processing_template_decl)
12438 : {
12439 1124096 : t = fold_non_dependent_expr_template (t, complain,
12440 : manifestly_const_eval, object);
12441 : /* maybe_constant_init does this stripping, so do it here too. */
12442 1124096 : if (TREE_CODE (t) == TARGET_EXPR)
12443 : {
12444 2107 : tree init = TARGET_EXPR_INITIAL (t);
12445 2107 : if (TREE_CODE (init) == CONSTRUCTOR)
12446 62792886 : t = init;
12447 : }
12448 : return t;
12449 : }
12450 :
12451 61668790 : return maybe_constant_init (t, object, manifestly_const_eval);
12452 : }
12453 :
12454 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
12455 : than wrapped in a TARGET_EXPR.
12456 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
12457 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
12458 : per P0595 even when ALLOW_NON_CONSTANT is true. */
12459 :
12460 : static tree
12461 174148693 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
12462 : mce_value manifestly_const_eval)
12463 : {
12464 174148693 : if (!t)
12465 : return t;
12466 174148693 : if (TREE_CODE (t) == EXPR_STMT)
12467 270 : t = TREE_OPERAND (t, 0);
12468 174148693 : if (TREE_CODE (t) == CONVERT_EXPR
12469 174148693 : && VOID_TYPE_P (TREE_TYPE (t)))
12470 2872 : t = TREE_OPERAND (t, 0);
12471 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
12472 : DECL and losing that information would cause mischief later. */
12473 174148693 : if (TREE_CODE (t) == INIT_EXPR
12474 174148693 : && (!decl
12475 2620 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
12476 2620 : TREE_TYPE (t))))
12477 1706 : t = TREE_OPERAND (t, 1);
12478 174148693 : if (TREE_CODE (t) == TARGET_EXPR)
12479 7949253 : t = TARGET_EXPR_INITIAL (t);
12480 174148693 : if (!is_nondependent_static_init_expression (t))
12481 : /* Don't try to evaluate it. */;
12482 140858845 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
12483 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
12484 : else
12485 : {
12486 : /* [basic.start.static] allows constant-initialization of variables with
12487 : static or thread storage duration even if it isn't required, but we
12488 : shouldn't bend the rules the same way for automatic variables.
12489 :
12490 : But still enforce the requirements of constexpr/constinit.
12491 : [dcl.constinit] "If a variable declared with the constinit specifier
12492 : has dynamic initialization, the program is ill-formed, even if the
12493 : implementation would perform that initialization as a static
12494 : initialization." */
12495 43006221 : bool is_static = (decl && DECL_P (decl)
12496 102778244 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
12497 : bool strict = (!is_static
12498 : || (decl && DECL_P (decl)
12499 3431655 : && (DECL_DECLARED_CONSTEXPR_P (decl)
12500 977796 : || DECL_DECLARED_CONSTINIT_P (decl))));
12501 : if (is_static)
12502 : manifestly_const_eval = mce_true;
12503 :
12504 65482827 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
12505 88394 : return fold_to_constant (t);
12506 :
12507 65394433 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
12508 : manifestly_const_eval,
12509 : false, decl);
12510 : }
12511 174060297 : if (TREE_CODE (t) == TARGET_EXPR)
12512 : {
12513 281077 : tree init = TARGET_EXPR_INITIAL (t);
12514 281077 : if (TREE_CODE (init) == CONSTRUCTOR)
12515 174148691 : t = init;
12516 : }
12517 : return t;
12518 : }
12519 :
12520 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
12521 :
12522 : tree
12523 95564749 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
12524 : {
12525 95564749 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
12526 : }
12527 :
12528 : tree
12529 78581687 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
12530 : {
12531 78581687 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
12532 : }
12533 :
12534 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
12535 :
12536 : tree
12537 2257 : cxx_constant_init (tree t, tree decl)
12538 : {
12539 2257 : return maybe_constant_init_1 (t, decl, false, mce_true);
12540 : }
12541 :
12542 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
12543 :
12544 : static bool
12545 229449819 : callee_might_throw (tree t)
12546 : {
12547 229449819 : if (cxx_dialect < cxx26 || !flag_exceptions)
12548 : return false;
12549 37407544 : tree callee = cp_get_callee (t);
12550 37407544 : if (callee == NULL_TREE)
12551 : return false;
12552 37348828 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
12553 37348828 : return (!flag_enforce_eh_specs
12554 37348828 : || type_dependent_expression_p (callee)
12555 34242967 : || !POINTER_TYPE_P (TREE_TYPE (callee))
12556 69356126 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
12557 15008921 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
12558 : }
12559 :
12560 : #if 0
12561 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
12562 : /* Return true if the object referred to by REF has automatic or thread
12563 : local storage. */
12564 :
12565 : enum { ck_ok, ck_bad, ck_unknown };
12566 : static int
12567 : check_automatic_or_tls (tree ref)
12568 : {
12569 : machine_mode mode;
12570 : poly_int64 bitsize, bitpos;
12571 : tree offset;
12572 : int volatilep = 0, unsignedp = 0;
12573 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
12574 : &mode, &unsignedp, &volatilep, false);
12575 : duration_kind dk;
12576 :
12577 : /* If there isn't a decl in the middle, we don't know the linkage here,
12578 : and this isn't a constant expression anyway. */
12579 : if (!DECL_P (decl))
12580 : return ck_unknown;
12581 : dk = decl_storage_duration (decl);
12582 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
12583 : }
12584 : #endif
12585 :
12586 : /* Data structure for passing data from potential_constant_expression_1
12587 : to check_for_return_continue via cp_walk_tree. */
12588 : struct check_for_return_continue_data {
12589 : hash_set<tree> *pset;
12590 : tree continue_stmt;
12591 : tree break_stmt;
12592 : bool could_throw;
12593 : };
12594 :
12595 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
12596 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
12597 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
12598 : For C++26 also note presence of possibly throwing calls. */
12599 : static tree
12600 51265067 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
12601 : {
12602 51265067 : tree t = *tp, s, b;
12603 51265067 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
12604 51265067 : switch (TREE_CODE (t))
12605 : {
12606 : case RETURN_EXPR:
12607 : return t;
12608 :
12609 28 : case CONTINUE_STMT:
12610 28 : if (CONTINUE_NAME (t))
12611 : {
12612 : /* For CONTINUE_STMT with CONTINUE_NAME set could_throw. While
12613 : those cannot throw, there could be possibly many such statements
12614 : and we don't track precisely in potential_constant_expression_1
12615 : which named loops or switches are more outer, so we wouldn't
12616 : know what to propagate to the caller as the outermost
12617 : break or continue. */
12618 0 : d->could_throw = true;
12619 0 : break;
12620 : }
12621 28 : if (d->continue_stmt == NULL_TREE)
12622 28 : d->continue_stmt = t;
12623 : break;
12624 :
12625 3283 : case BREAK_STMT:
12626 3283 : if (BREAK_NAME (t))
12627 : {
12628 : /* For BREAK_STMT with BREAK_NAME set could_throw. See above
12629 : comment about CONTINUE_STMT. */
12630 7 : d->could_throw = true;
12631 7 : break;
12632 : }
12633 3276 : if (d->break_stmt == NULL_TREE)
12634 1353 : d->break_stmt = t;
12635 : break;
12636 :
12637 : #define RECUR(x) \
12638 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
12639 : d->pset)) \
12640 : return r
12641 :
12642 : /* For loops, walk subtrees manually, so that continue stmts found
12643 : inside of the bodies of the loops are ignored. */
12644 42105 : case DO_STMT:
12645 42105 : *walk_subtrees = 0;
12646 42105 : RECUR (DO_COND (t));
12647 42105 : s = d->continue_stmt;
12648 42105 : b = d->break_stmt;
12649 42105 : RECUR (DO_BODY (t));
12650 42099 : d->continue_stmt = s;
12651 42099 : d->break_stmt = b;
12652 42099 : break;
12653 :
12654 127 : case WHILE_STMT:
12655 127 : *walk_subtrees = 0;
12656 127 : RECUR (WHILE_COND_PREP (t));
12657 127 : RECUR (WHILE_COND (t));
12658 127 : s = d->continue_stmt;
12659 127 : b = d->break_stmt;
12660 127 : RECUR (WHILE_BODY (t));
12661 113 : d->continue_stmt = s;
12662 113 : d->break_stmt = b;
12663 113 : break;
12664 :
12665 402 : case FOR_STMT:
12666 402 : *walk_subtrees = 0;
12667 402 : RECUR (FOR_INIT_STMT (t));
12668 402 : RECUR (FOR_COND_PREP (t));
12669 402 : RECUR (FOR_COND (t));
12670 402 : RECUR (FOR_EXPR (t));
12671 402 : s = d->continue_stmt;
12672 402 : b = d->break_stmt;
12673 402 : RECUR (FOR_BODY (t));
12674 348 : d->continue_stmt = s;
12675 348 : d->break_stmt = b;
12676 348 : break;
12677 :
12678 0 : case RANGE_FOR_STMT:
12679 0 : *walk_subtrees = 0;
12680 0 : RECUR (RANGE_FOR_EXPR (t));
12681 0 : s = d->continue_stmt;
12682 0 : b = d->break_stmt;
12683 0 : RECUR (RANGE_FOR_BODY (t));
12684 0 : d->continue_stmt = s;
12685 0 : d->break_stmt = b;
12686 0 : break;
12687 :
12688 221 : case SWITCH_STMT:
12689 221 : *walk_subtrees = 0;
12690 221 : RECUR (SWITCH_STMT_COND (t));
12691 221 : b = d->break_stmt;
12692 221 : RECUR (SWITCH_STMT_BODY (t));
12693 189 : d->break_stmt = b;
12694 189 : break;
12695 : #undef RECUR
12696 :
12697 : case STATEMENT_LIST:
12698 : case CONSTRUCTOR:
12699 : break;
12700 :
12701 2617357 : case AGGR_INIT_EXPR:
12702 2617357 : case CALL_EXPR:
12703 : /* In C++26 a function could throw. */
12704 2617357 : if (callee_might_throw (t))
12705 128921 : d->could_throw = true;
12706 : break;
12707 :
12708 47212118 : default:
12709 47212118 : if (!EXPR_P (t))
12710 14878993 : *walk_subtrees = 0;
12711 : break;
12712 : }
12713 :
12714 : return NULL_TREE;
12715 : }
12716 :
12717 : /* Return true if T denotes a potentially constant expression. Issue
12718 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
12719 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
12720 : consider the expression in the current context, independent of constexpr
12721 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
12722 : and hard errors should not be reported by constexpr_error.
12723 :
12724 : C++0x [expr.const] used to say
12725 :
12726 : 6 An expression is a potential constant expression if it is
12727 : a constant expression where all occurrences of function
12728 : parameters are replaced by arbitrary constant expressions
12729 : of the appropriate type.
12730 :
12731 : 2 A conditional expression is a constant expression unless it
12732 : involves one of the following as a potentially evaluated
12733 : subexpression (3.2), but subexpressions of logical AND (5.14),
12734 : logical OR (5.15), and conditional (5.16) operations that are
12735 : not evaluated are not considered. */
12736 :
12737 : static bool
12738 4933015760 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
12739 : bool fundef_p, tsubst_flags_t flags,
12740 : tree *jump_target)
12741 : {
12742 : #define RECUR(T,RV) \
12743 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
12744 : jump_target)
12745 :
12746 4933015760 : enum { any = false, rval = true };
12747 4933015760 : int i;
12748 4933015760 : tree tmp;
12749 :
12750 4933015760 : if (t == error_mark_node)
12751 : return false;
12752 4933003098 : if (t == NULL_TREE)
12753 : return true;
12754 4924887037 : location_t loc = cp_expr_loc_or_input_loc (t);
12755 4924887037 : iloc_sentinel ils = loc;
12756 :
12757 4924887037 : if (*jump_target)
12758 : /* If we are jumping, ignore everything. This is simpler than the
12759 : cxx_eval_constant_expression handling because we only need to be
12760 : conservatively correct, and we don't necessarily have a constant value
12761 : available, so we don't bother with switch tracking. */
12762 : return true;
12763 :
12764 2224116 : if (TREE_THIS_VOLATILE (t) && want_rval
12765 1254209 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
12766 4915421328 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
12767 : {
12768 79495 : if (TREE_CLOBBER_P (t))
12769 : {
12770 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
12771 0 : gcc_checking_assert (false);
12772 : return true;
12773 : }
12774 :
12775 79495 : if (flags & tf_error)
12776 25 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
12777 : "a volatile lvalue %qE with type %qT", t,
12778 25 : TREE_TYPE (t));
12779 : return false;
12780 : }
12781 4915262311 : if (CONSTANT_CLASS_P (t))
12782 : return true;
12783 3584276377 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
12784 3584276377 : && TREE_TYPE (t) == error_mark_node)
12785 : return false;
12786 :
12787 3584276334 : switch (TREE_CODE (t))
12788 : {
12789 : case FUNCTION_DECL:
12790 : case BASELINK:
12791 : case TEMPLATE_DECL:
12792 : case OVERLOAD:
12793 : case TEMPLATE_ID_EXPR:
12794 : case LABEL_DECL:
12795 : case CASE_LABEL_EXPR:
12796 : case PREDICT_EXPR:
12797 : case CONST_DECL:
12798 : case SIZEOF_EXPR:
12799 : case ALIGNOF_EXPR:
12800 : case OFFSETOF_EXPR:
12801 : case NOEXCEPT_EXPR:
12802 : case TEMPLATE_PARM_INDEX:
12803 : case TRAIT_EXPR:
12804 : case IDENTIFIER_NODE:
12805 : case USERDEF_LITERAL:
12806 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
12807 : case FIELD_DECL:
12808 : case RESULT_DECL:
12809 : case USING_DECL:
12810 : case USING_STMT:
12811 : case PLACEHOLDER_EXPR:
12812 : case REQUIRES_EXPR:
12813 : case STATIC_ASSERT:
12814 : case DEBUG_BEGIN_STMT:
12815 : case REFLECT_EXPR:
12816 : return true;
12817 :
12818 24779778 : case RETURN_EXPR:
12819 24779778 : if (!RECUR (TREE_OPERAND (t, 0), any))
12820 : return false;
12821 : /* FALLTHROUGH */
12822 :
12823 24612015 : case BREAK_STMT:
12824 24612015 : case CONTINUE_STMT:
12825 24612015 : *jump_target = t;
12826 24612015 : return true;
12827 :
12828 390790864 : case PARM_DECL:
12829 390790864 : if (now && want_rval)
12830 : {
12831 100403518 : tree type = TREE_TYPE (t);
12832 100403518 : if (dependent_type_p (type)
12833 71666977 : || !COMPLETE_TYPE_P (processing_template_decl
12834 : ? type : complete_type (type))
12835 172070495 : || is_really_empty_class (type, /*ignore_vptr*/false))
12836 : /* An empty class has no data to read. */
12837 : return true;
12838 71666269 : if (flags & tf_error)
12839 24 : constexpr_error (input_location, fundef_p,
12840 : "%qE is not a constant expression", t);
12841 : return false;
12842 : }
12843 : return true;
12844 :
12845 283244148 : case AGGR_INIT_EXPR:
12846 283244148 : case CALL_EXPR:
12847 : /* -- an invocation of a function other than a constexpr function
12848 : or a constexpr constructor. */
12849 283244148 : {
12850 283244148 : tree fun = get_function_named_in_call (t);
12851 283244148 : const int nargs = call_expr_nargs (t);
12852 283244148 : i = 0;
12853 :
12854 283244148 : if (fun == NULL_TREE)
12855 : {
12856 : /* Reset to allow the function to continue past the end
12857 : of the block below. Otherwise return early. */
12858 418953 : bool bail = true;
12859 :
12860 418953 : if (TREE_CODE (t) == CALL_EXPR
12861 418953 : && CALL_EXPR_FN (t) == NULL_TREE)
12862 418953 : switch (CALL_EXPR_IFN (t))
12863 : {
12864 : /* These should be ignored, they are optimized away from
12865 : constexpr functions. */
12866 : case IFN_UBSAN_NULL:
12867 : case IFN_UBSAN_BOUNDS:
12868 : case IFN_UBSAN_VPTR:
12869 : case IFN_FALLTHROUGH:
12870 : case IFN_ASSUME:
12871 : return true;
12872 :
12873 : case IFN_ADD_OVERFLOW:
12874 : case IFN_SUB_OVERFLOW:
12875 : case IFN_MUL_OVERFLOW:
12876 : case IFN_LAUNDER:
12877 : case IFN_VEC_CONVERT:
12878 : case IFN_BSWAP:
12879 : case IFN_BITREVERSE:
12880 : case IFN_CLMUL:
12881 : case IFN_BITCOMPRESS:
12882 : case IFN_BITEXPAND:
12883 : bail = false;
12884 : break;
12885 :
12886 : default:
12887 : break;
12888 : }
12889 :
12890 : if (bail)
12891 : {
12892 : /* fold_call_expr can't do anything with IFN calls. */
12893 172 : if (flags & tf_error)
12894 0 : constexpr_error (loc, fundef_p,
12895 : "call to internal function %qE", t);
12896 : return false;
12897 : }
12898 : }
12899 :
12900 282825195 : if (fun && is_overloaded_fn (fun))
12901 : {
12902 264131864 : if (!RECUR (fun, true))
12903 : return false;
12904 263716456 : fun = get_fns (fun);
12905 :
12906 263716456 : if (TREE_CODE (fun) == FUNCTION_DECL)
12907 : {
12908 245858461 : if (builtin_valid_in_constant_expr_p (fun))
12909 : return true;
12910 244070601 : if (!maybe_constexpr_fn (fun)
12911 : /* Allow any built-in function; if the expansion
12912 : isn't constant, we'll deal with that then. */
12913 54900619 : && !fndecl_built_in_p (fun)
12914 : /* In C++20, replaceable global allocation functions
12915 : are constant expressions. */
12916 37390129 : && (!cxx_replaceable_global_alloc_fn (fun)
12917 972081 : || TREE_CODE (t) != CALL_EXPR
12918 972081 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
12919 352342 : && (current_function_decl == NULL_TREE
12920 352342 : || !is_std_allocator_allocate_deallocate
12921 352342 : (current_function_decl))))
12922 : /* Allow placement new in std::construct_at. */
12923 36425021 : && (!cxx_placement_new_fn (fun)
12924 391631 : || TREE_CODE (t) != CALL_EXPR
12925 391631 : || current_function_decl == NULL_TREE
12926 391619 : || !is_std_construct_at (current_function_decl))
12927 36162129 : && !cxx_dynamic_cast_fn_p (fun)
12928 280229119 : && !cxx_cxa_builtin_fn_p (fun))
12929 : {
12930 : /* In C++26 evaluation of the function arguments might
12931 : throw and in that case it is irrelevant whether
12932 : fun is constexpr or not. */
12933 36103322 : if (cxx_dialect >= cxx26)
12934 8227748 : for (; i < nargs; ++i)
12935 : {
12936 4936323 : tree x = get_nth_callarg (t, i);
12937 4936323 : bool rv = processing_template_decl ? any : rval;
12938 4936323 : bool sub_now = false;
12939 4936323 : if (!potential_constant_expression_1 (x, rv, strict,
12940 : sub_now,
12941 : fundef_p,
12942 : flags,
12943 : jump_target))
12944 : return false;
12945 4643325 : if (throws (jump_target))
12946 : return true;
12947 : }
12948 35780647 : if ((flags & tf_error)
12949 35780647 : && constexpr_error (loc, fundef_p,
12950 : "call to non-%<constexpr%> "
12951 : "function %qD", fun))
12952 272 : explain_invalid_constexpr_fn (fun);
12953 : return false;
12954 : }
12955 : }
12956 :
12957 225825274 : fun = OVL_FIRST (fun);
12958 : /* Skip initial arguments to base constructors. */
12959 225825274 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12960 4135238 : i = num_artificial_parms_for (fun);
12961 : }
12962 18693331 : else if (fun)
12963 : {
12964 18693331 : if (TREE_TYPE (fun)
12965 18693331 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12966 : want_rval = rval;
12967 : else
12968 : want_rval = any;
12969 18693331 : if (RECUR (fun, want_rval))
12970 : /* Might end up being a constant function pointer. But it
12971 : could also be a function object with constexpr op(), so
12972 : we pass 'any' so that the underlying VAR_DECL is deemed
12973 : as potentially-constant even though it wasn't declared
12974 : constexpr. */;
12975 : else
12976 : return false;
12977 : }
12978 542614561 : for (; i < nargs; ++i)
12979 : {
12980 315725110 : tree x = get_nth_callarg (t, i);
12981 : /* In a template, reference arguments haven't been converted to
12982 : REFERENCE_TYPE and we might not even know if the parameter
12983 : is a reference, so accept lvalue constants too. */
12984 315725110 : bool rv = processing_template_decl ? any : rval;
12985 : /* Don't require an immediately constant value, as constexpr
12986 : substitution might not use the value of the argument. */
12987 315725110 : bool sub_now = false;
12988 315725110 : if (!potential_constant_expression_1 (x, rv, strict,
12989 : sub_now, fundef_p, flags,
12990 : jump_target))
12991 : return false;
12992 3210383577 : if (throws (jump_target))
12993 : return true;
12994 : }
12995 : /* In C++26 a function could throw. */
12996 226889451 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12997 11881602 : *jump_target = void_node;
12998 : return true;
12999 : }
13000 :
13001 202208391 : case NON_LVALUE_EXPR:
13002 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
13003 : -- an lvalue of integral type that refers to a non-volatile
13004 : const variable or static data member initialized with
13005 : constant expressions, or
13006 :
13007 : -- an lvalue of literal type that refers to non-volatile
13008 : object defined with constexpr, or that refers to a
13009 : sub-object of such an object; */
13010 202208391 : return RECUR (TREE_OPERAND (t, 0), rval);
13011 :
13012 73023 : case EXCESS_PRECISION_EXPR:
13013 73023 : return RECUR (TREE_OPERAND (t, 0), rval);
13014 :
13015 313702857 : case VAR_DECL:
13016 313702857 : if (DECL_HAS_VALUE_EXPR_P (t))
13017 : {
13018 5280807 : if (now && is_normal_capture_proxy (t))
13019 : {
13020 : /* -- in a lambda-expression, a reference to this or to a
13021 : variable with automatic storage duration defined outside that
13022 : lambda-expression, where the reference would be an
13023 : odr-use. */
13024 :
13025 475733 : if (want_rval)
13026 : /* Since we're doing an lvalue-rvalue conversion, this might
13027 : not be an odr-use, so evaluate the variable directly. */
13028 468680 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
13029 :
13030 7053 : if (flags & tf_error)
13031 : {
13032 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
13033 3 : auto_diagnostic_group d;
13034 3 : if (constexpr_error (input_location, fundef_p,
13035 : "lambda capture of %qE is not a "
13036 : "constant expression", cap)
13037 3 : && decl_constant_var_p (cap))
13038 3 : inform (input_location, "because it is used as a glvalue");
13039 3 : }
13040 : return false;
13041 : }
13042 4805074 : tree ve = DECL_VALUE_EXPR (t);
13043 : /* Treat __PRETTY_FUNCTION__ inside a template function as
13044 : potentially-constant. */
13045 4805074 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
13046 : return true;
13047 4805069 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
13048 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
13049 4802333 : return RECUR (ve, rval);
13050 : }
13051 308422050 : if (want_rval
13052 218048960 : && (now || !var_in_maybe_constexpr_fn (t))
13053 198822497 : && !type_dependent_expression_p (t)
13054 167151064 : && !decl_maybe_constant_var_p (t)
13055 54697810 : && !is_local_temp (t)
13056 54123095 : && (strict
13057 4073396 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
13058 1614367 : || (DECL_INITIAL (t)
13059 1148352 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
13060 53618263 : && COMPLETE_TYPE_P (TREE_TYPE (t))
13061 362040159 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
13062 : {
13063 53613261 : if (flags & tf_error)
13064 175 : non_const_var_error (loc, t, fundef_p);
13065 : return false;
13066 : }
13067 : return true;
13068 :
13069 445249407 : case NOP_EXPR:
13070 445249407 : if (REINTERPRET_CAST_P (t))
13071 : {
13072 159762 : if (flags & tf_error)
13073 49 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
13074 : "constant expression");
13075 : return false;
13076 : }
13077 : /* FALLTHRU */
13078 897020028 : case CONVERT_EXPR:
13079 897020028 : case VIEW_CONVERT_EXPR:
13080 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
13081 : may change to something more specific to type-punning (DR 1312). */
13082 897020028 : {
13083 897020028 : tree from = TREE_OPERAND (t, 0);
13084 897020028 : if (location_wrapper_p (t))
13085 368127085 : return (RECUR (from, want_rval));
13086 528892943 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
13087 : {
13088 300832071 : STRIP_ANY_LOCATION_WRAPPER (from);
13089 300832071 : if (TREE_CODE (from) == INTEGER_CST
13090 300832071 : && !integer_zerop (from))
13091 : {
13092 1689 : if (flags & tf_error)
13093 37 : constexpr_error (loc, fundef_p,
13094 : "%<reinterpret_cast%> from integer to "
13095 : "pointer");
13096 : return false;
13097 : }
13098 : }
13099 : /* convert_to_void used to fold these away to void_node, because they
13100 : are a discarded-value expression. */
13101 528891254 : if (TREE_CODE (t) == CONVERT_EXPR && VOID_TYPE_P (TREE_TYPE (t)))
13102 8677323 : return RECUR (from, /*want_rval=*/false);
13103 520213931 : return RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR);
13104 : }
13105 :
13106 12574 : case ADDRESSOF_EXPR:
13107 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
13108 12574 : t = TREE_OPERAND (t, 0);
13109 12574 : goto handle_addr_expr;
13110 :
13111 99922562 : case ADDR_EXPR:
13112 : /* -- a unary operator & that is applied to an lvalue that
13113 : designates an object with thread or automatic storage
13114 : duration; */
13115 99922562 : t = TREE_OPERAND (t, 0);
13116 :
13117 99922562 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
13118 : /* A pointer-to-member constant. */
13119 : return true;
13120 :
13121 99934870 : handle_addr_expr:
13122 : #if 0
13123 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
13124 : any checking here, as we might dereference the pointer later. If
13125 : we remove this code, also remove check_automatic_or_tls. */
13126 : i = check_automatic_or_tls (t);
13127 : if (i == ck_ok)
13128 : return true;
13129 : if (i == ck_bad)
13130 : {
13131 : if (flags & tf_error)
13132 : error ("address-of an object %qE with thread local or "
13133 : "automatic storage is not a constant expression", t);
13134 : return false;
13135 : }
13136 : #endif
13137 99934870 : return RECUR (t, any);
13138 :
13139 106701628 : case COMPONENT_REF:
13140 106701628 : case ARROW_EXPR:
13141 106701628 : case OFFSET_REF:
13142 : /* -- a class member access unless its postfix-expression is
13143 : of literal type or of pointer to literal type. */
13144 : /* This test would be redundant, as it follows from the
13145 : postfix-expression being a potential constant expression. */
13146 106701628 : if (type_unknown_p (t))
13147 : return true;
13148 98821702 : if (is_overloaded_fn (t))
13149 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
13150 : which uses ob as an lvalue. */
13151 101341302 : want_rval = false;
13152 101341302 : gcc_fallthrough ();
13153 :
13154 101341302 : case REALPART_EXPR:
13155 101341302 : case IMAGPART_EXPR:
13156 101341302 : case BIT_FIELD_REF:
13157 101341302 : return RECUR (TREE_OPERAND (t, 0), want_rval);
13158 :
13159 292700 : case EXPR_PACK_EXPANSION:
13160 292700 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
13161 :
13162 : case PACK_INDEX_EXPR:
13163 : return true;
13164 :
13165 112059001 : case INDIRECT_REF:
13166 112059001 : return RECUR (TREE_OPERAND (t, 0), rval);
13167 :
13168 25249686 : case STATEMENT_LIST:
13169 5024390629 : for (tree stmt : tsi_range (t))
13170 74773583 : if (!RECUR (stmt, any))
13171 4924887037 : return false;
13172 : return true;
13173 :
13174 5095348 : case MODIFY_EXPR:
13175 5095348 : if (cxx_dialect < cxx14)
13176 3897 : goto fail;
13177 5091451 : if (!RECUR (TREE_OPERAND (t, 0), any))
13178 : return false;
13179 : /* Just ignore clobbers. */
13180 4246451 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
13181 : return true;
13182 3775446 : if (!RECUR (TREE_OPERAND (t, 1), rval))
13183 : return false;
13184 : return true;
13185 :
13186 901343 : case MODOP_EXPR:
13187 901343 : if (cxx_dialect < cxx14)
13188 98 : goto fail;
13189 901245 : if (!RECUR (TREE_OPERAND (t, 0), rval))
13190 : return false;
13191 890705 : if (!RECUR (TREE_OPERAND (t, 2), rval))
13192 : return false;
13193 : return true;
13194 :
13195 547460 : case DO_STMT:
13196 547460 : if (!RECUR (DO_COND (t), rval))
13197 : return false;
13198 547460 : if (!RECUR (DO_BODY (t), any))
13199 : return false;
13200 546832 : if ((breaks (jump_target)
13201 3 : && (TREE_CODE (*jump_target) != BREAK_STMT
13202 0 : || BREAK_NAME (*jump_target) == NULL_TREE
13203 0 : || BREAK_NAME (*jump_target) == DO_NAME (t)))
13204 546832 : || (continues (jump_target)
13205 2 : && (TREE_CODE (*jump_target) != CONTINUE_STMT
13206 2 : || CONTINUE_NAME (*jump_target) == NULL_TREE
13207 0 : || CONTINUE_NAME (*jump_target) == DO_NAME (t))))
13208 5 : *jump_target = NULL_TREE;
13209 : return true;
13210 :
13211 443444 : case FOR_STMT:
13212 443444 : if (!RECUR (FOR_INIT_STMT (t), any))
13213 : return false;
13214 443444 : if (!RECUR (FOR_COND_PREP (t), any))
13215 : return false;
13216 443444 : tmp = FOR_COND (t);
13217 443444 : if (!RECUR (tmp, rval))
13218 : return false;
13219 443365 : if (tmp)
13220 : {
13221 396018 : if (!processing_template_decl)
13222 391398 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
13223 : /* If we couldn't evaluate the condition, it might not ever be
13224 : true. */
13225 396018 : if (!integer_onep (tmp))
13226 : {
13227 : /* Before returning true, check if the for body can contain
13228 : a return. */
13229 396018 : hash_set<tree> pset;
13230 396018 : check_for_return_continue_data data = { &pset, NULL_TREE,
13231 396018 : NULL_TREE, false };
13232 396018 : if (tree ret_expr
13233 396018 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
13234 : &data, &pset))
13235 136979 : *jump_target = ret_expr;
13236 396018 : if (data.could_throw)
13237 18348 : *jump_target = void_node;
13238 396018 : return true;
13239 396018 : }
13240 : }
13241 47347 : if (!RECUR (FOR_EXPR (t), any))
13242 : return false;
13243 47347 : if (!RECUR (FOR_BODY (t), any))
13244 : return false;
13245 47347 : if (!RECUR (FOR_COND_CLEANUP (t), any))
13246 : return false;
13247 47347 : if ((breaks (jump_target)
13248 12 : && (TREE_CODE (*jump_target) != BREAK_STMT
13249 12 : || BREAK_NAME (*jump_target) == NULL_TREE
13250 0 : || BREAK_NAME (*jump_target) == FOR_NAME (t)))
13251 47347 : || (continues (jump_target)
13252 0 : && (TREE_CODE (*jump_target) != CONTINUE_STMT
13253 0 : || CONTINUE_NAME (*jump_target) == NULL_TREE
13254 0 : || CONTINUE_NAME (*jump_target) == FOR_NAME (t))))
13255 12 : *jump_target = NULL_TREE;
13256 : return true;
13257 :
13258 486 : case RANGE_FOR_STMT:
13259 486 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
13260 : return false;
13261 486 : if (!RECUR (RANGE_FOR_EXPR (t), any))
13262 : return false;
13263 486 : if (!RECUR (RANGE_FOR_BODY (t), any))
13264 : return false;
13265 484 : if ((breaks (jump_target)
13266 0 : && (TREE_CODE (*jump_target) != BREAK_STMT
13267 0 : || BREAK_NAME (*jump_target) == NULL_TREE
13268 0 : || BREAK_NAME (*jump_target) == RANGE_FOR_NAME (t)))
13269 484 : || (continues (jump_target)
13270 0 : && (TREE_CODE (*jump_target) != CONTINUE_STMT
13271 0 : || CONTINUE_NAME (*jump_target) == NULL_TREE
13272 0 : || CONTINUE_NAME (*jump_target) == RANGE_FOR_NAME (t))))
13273 0 : *jump_target = NULL_TREE;
13274 : return true;
13275 :
13276 166393 : case WHILE_STMT:
13277 166393 : if (!RECUR (WHILE_COND_PREP (t), any))
13278 : return false;
13279 166393 : tmp = WHILE_COND (t);
13280 166393 : if (!RECUR (tmp, rval))
13281 : return false;
13282 166325 : if (!processing_template_decl)
13283 166288 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
13284 : /* If we couldn't evaluate the condition, it might not ever be true. */
13285 166325 : if (!integer_onep (tmp))
13286 : {
13287 : /* Before returning true, check if the while body can contain
13288 : a return. */
13289 156191 : hash_set<tree> pset;
13290 156191 : check_for_return_continue_data data = { &pset, NULL_TREE,
13291 156191 : NULL_TREE, false };
13292 156191 : if (tree ret_expr
13293 156191 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
13294 : &data, &pset))
13295 476 : *jump_target = ret_expr;
13296 156191 : if (data.could_throw)
13297 4672 : *jump_target = void_node;
13298 156191 : return true;
13299 156191 : }
13300 10134 : if (!RECUR (WHILE_BODY (t), any))
13301 : return false;
13302 10123 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
13303 : return false;
13304 10123 : if ((breaks (jump_target)
13305 27 : && (TREE_CODE (*jump_target) != BREAK_STMT
13306 27 : || BREAK_NAME (*jump_target) == NULL_TREE
13307 0 : || BREAK_NAME (*jump_target) == WHILE_NAME (t)))
13308 10123 : || (continues (jump_target)
13309 0 : && (TREE_CODE (*jump_target) != CONTINUE_STMT
13310 0 : || CONTINUE_NAME (*jump_target) == NULL_TREE
13311 0 : || CONTINUE_NAME (*jump_target) == WHILE_NAME (t))))
13312 27 : *jump_target = NULL_TREE;
13313 : return true;
13314 :
13315 43089 : case SWITCH_STMT:
13316 43089 : if (!RECUR (SWITCH_STMT_COND (t), rval))
13317 : return false;
13318 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
13319 : unreachable labels would be checked and it is enough if there is
13320 : a single switch cond value for which it is a valid constant
13321 : expression. We need to check if there are any RETURN_EXPRs
13322 : or CONTINUE_STMTs inside of the body though, as in that case
13323 : we need to set *jump_target. */
13324 : else
13325 : {
13326 43079 : hash_set<tree> pset;
13327 43079 : check_for_return_continue_data data = { &pset, NULL_TREE,
13328 43079 : NULL_TREE, false };
13329 43079 : if (tree ret_expr
13330 43079 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
13331 : &data, &pset))
13332 : /* The switch might return. */
13333 42684 : *jump_target = ret_expr;
13334 395 : else if (data.continue_stmt)
13335 : /* The switch can't return, but might continue. */
13336 3 : *jump_target = data.continue_stmt;
13337 43079 : if (data.could_throw)
13338 1889 : *jump_target = void_node;
13339 43079 : }
13340 43079 : return true;
13341 :
13342 50 : case STMT_EXPR:
13343 50 : return RECUR (STMT_EXPR_STMT (t), rval);
13344 :
13345 623821 : case LAMBDA_EXPR:
13346 623821 : if (cxx_dialect >= cxx17)
13347 : /* In C++17 lambdas can be constexpr, don't give up yet. */
13348 : return true;
13349 505 : else if (flags & tf_error)
13350 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
13351 : "constant expression before C++17");
13352 : return false;
13353 :
13354 111830 : case NEW_EXPR:
13355 111830 : case VEC_NEW_EXPR:
13356 111830 : case DELETE_EXPR:
13357 111830 : case VEC_DELETE_EXPR:
13358 111830 : if (cxx_dialect >= cxx20)
13359 : /* In C++20, new-expressions are potentially constant. */
13360 : return true;
13361 1078 : else if (flags & tf_error)
13362 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
13363 : "constant expression before C++20");
13364 : return false;
13365 :
13366 84301 : case DYNAMIC_CAST_EXPR:
13367 84301 : case PSEUDO_DTOR_EXPR:
13368 84301 : case OMP_PARALLEL:
13369 84301 : case OMP_TASK:
13370 84301 : case OMP_FOR:
13371 84301 : case OMP_SIMD:
13372 84301 : case OMP_DISTRIBUTE:
13373 84301 : case OMP_TASKLOOP:
13374 84301 : case OMP_LOOP:
13375 84301 : case OMP_TEAMS:
13376 84301 : case OMP_TARGET_DATA:
13377 84301 : case OMP_TARGET:
13378 84301 : case OMP_SECTIONS:
13379 84301 : case OMP_ORDERED:
13380 84301 : case OMP_CRITICAL:
13381 84301 : case OMP_SINGLE:
13382 84301 : case OMP_SCAN:
13383 84301 : case OMP_SCOPE:
13384 84301 : case OMP_SECTION:
13385 84301 : case OMP_MASTER:
13386 84301 : case OMP_MASKED:
13387 84301 : case OMP_TASKGROUP:
13388 84301 : case OMP_TARGET_UPDATE:
13389 84301 : case OMP_TARGET_ENTER_DATA:
13390 84301 : case OMP_TARGET_EXIT_DATA:
13391 84301 : case OMP_ATOMIC:
13392 84301 : case OMP_ATOMIC_READ:
13393 84301 : case OMP_ATOMIC_CAPTURE_OLD:
13394 84301 : case OMP_ATOMIC_CAPTURE_NEW:
13395 84301 : case OMP_DEPOBJ:
13396 84301 : case OMP_ALLOCATE:
13397 84301 : case OACC_PARALLEL:
13398 84301 : case OACC_KERNELS:
13399 84301 : case OACC_SERIAL:
13400 84301 : case OACC_DATA:
13401 84301 : case OACC_HOST_DATA:
13402 84301 : case OACC_LOOP:
13403 84301 : case OACC_CACHE:
13404 84301 : case OACC_DECLARE:
13405 84301 : case OACC_ENTER_DATA:
13406 84301 : case OACC_EXIT_DATA:
13407 84301 : case OACC_UPDATE:
13408 84301 : case OMP_ARRAY_SECTION:
13409 : /* GCC internal stuff. */
13410 84301 : case VA_ARG_EXPR:
13411 84301 : case TRANSACTION_EXPR:
13412 84301 : case AT_ENCODE_EXPR:
13413 84301 : fail:
13414 84301 : if (flags & tf_error)
13415 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
13416 : "expression", t);
13417 : return false;
13418 :
13419 : case OMP_DECLARE_MAPPER:
13420 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
13421 : constant. */
13422 : return true;
13423 :
13424 27340 : case THROW_EXPR:
13425 27340 : if (cxx_dialect < cxx26)
13426 291 : goto fail;
13427 27049 : return RECUR (TREE_OPERAND (t, 0), rval);
13428 :
13429 531 : case ASM_EXPR:
13430 531 : if (flags & tf_error)
13431 6 : inline_asm_in_constexpr_error (loc, fundef_p);
13432 : return false;
13433 :
13434 530882 : case OBJ_TYPE_REF:
13435 530882 : if (cxx_dialect >= cxx20)
13436 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
13437 : return true;
13438 9062 : else if (flags & tf_error)
13439 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
13440 : "%<constexpr%> before C++20");
13441 : return false;
13442 :
13443 6729 : case TYPEID_EXPR:
13444 : /* In C++20, a typeid expression whose operand is of polymorphic
13445 : class type can be constexpr. */
13446 6729 : {
13447 6729 : tree e = TREE_OPERAND (t, 0);
13448 6729 : if (cxx_dialect < cxx20
13449 26 : && strict
13450 26 : && !TYPE_P (e)
13451 19 : && !type_dependent_expression_p (e)
13452 7 : && CLASS_TYPE_P (TREE_TYPE (e))
13453 6734 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
13454 : {
13455 4 : if (flags & tf_error)
13456 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
13457 : "constant expression because %qE is "
13458 : "of polymorphic type", e);
13459 : return false;
13460 : }
13461 : return true;
13462 : }
13463 :
13464 31133939 : case POINTER_DIFF_EXPR:
13465 31133939 : case MINUS_EXPR:
13466 31133939 : want_rval = true;
13467 31133939 : goto binary;
13468 :
13469 84877641 : case LT_EXPR:
13470 84877641 : case LE_EXPR:
13471 84877641 : case GT_EXPR:
13472 84877641 : case GE_EXPR:
13473 84877641 : case EQ_EXPR:
13474 84877641 : case NE_EXPR:
13475 84877641 : case SPACESHIP_EXPR:
13476 84877641 : want_rval = true;
13477 84877641 : goto binary;
13478 :
13479 1975577 : case PREINCREMENT_EXPR:
13480 1975577 : case POSTINCREMENT_EXPR:
13481 1975577 : case PREDECREMENT_EXPR:
13482 1975577 : case POSTDECREMENT_EXPR:
13483 1975577 : if (cxx_dialect < cxx14)
13484 8825 : goto fail;
13485 1966752 : goto unary;
13486 :
13487 1267158 : case BIT_NOT_EXPR:
13488 : /* A destructor. */
13489 1267158 : if (TYPE_P (TREE_OPERAND (t, 0)))
13490 : return true;
13491 : /* fall through. */
13492 :
13493 48269110 : case CONJ_EXPR:
13494 48269110 : case SAVE_EXPR:
13495 48269110 : case FIX_TRUNC_EXPR:
13496 48269110 : case FLOAT_EXPR:
13497 48269110 : case NEGATE_EXPR:
13498 48269110 : case ABS_EXPR:
13499 48269110 : case ABSU_EXPR:
13500 48269110 : case TRUTH_NOT_EXPR:
13501 48269110 : case FIXED_CONVERT_EXPR:
13502 48269110 : case UNARY_PLUS_EXPR:
13503 48269110 : case UNARY_LEFT_FOLD_EXPR:
13504 48269110 : case UNARY_RIGHT_FOLD_EXPR:
13505 48269110 : case VEC_DUPLICATE_EXPR:
13506 1267158 : unary:
13507 48269110 : return RECUR (TREE_OPERAND (t, 0), rval);
13508 :
13509 31523257 : case CAST_EXPR:
13510 31523257 : case CONST_CAST_EXPR:
13511 31523257 : case STATIC_CAST_EXPR:
13512 31523257 : case REINTERPRET_CAST_EXPR:
13513 31523257 : case IMPLICIT_CONV_EXPR:
13514 31523257 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
13515 : /* In C++98, a conversion to non-integral type can't be part of a
13516 : constant expression. */
13517 : {
13518 284 : if (flags & tf_error)
13519 0 : constexpr_error (loc, fundef_p,
13520 : "cast to non-integral type %qT in a constant "
13521 0 : "expression", TREE_TYPE (t));
13522 : return false;
13523 : }
13524 : /* This might be a conversion from a class to a (potentially) literal
13525 : type. Let's consider it potentially constant since the conversion
13526 : might be a constexpr user-defined conversion. */
13527 31522973 : else if (cxx_dialect >= cxx11
13528 31503039 : && (dependent_type_p (TREE_TYPE (t))
13529 11120385 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
13530 11106035 : || literal_type_p (TREE_TYPE (t)))
13531 31465073 : && TREE_OPERAND (t, 0)
13532 62746225 : && (TREE_CODE (t) != CAST_EXPR
13533 23640572 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
13534 : {
13535 31180354 : tree from = TREE_OPERAND (t, 0);
13536 31180354 : if (TREE_CODE (t) == CAST_EXPR)
13537 23597674 : from = TREE_VALUE (from);
13538 31180354 : tree type = TREE_TYPE (from);
13539 : /* If this is a dependent type, it could end up being a class
13540 : with conversions. */
13541 31180354 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
13542 : return true;
13543 : /* Or a non-dependent class which has conversions. */
13544 651930 : else if (CLASS_TYPE_P (type)
13545 651930 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
13546 : return true;
13547 : }
13548 :
13549 25008228 : return (RECUR (TREE_OPERAND (t, 0),
13550 25008228 : !TYPE_REF_P (TREE_TYPE (t))));
13551 :
13552 14473207 : case BIND_EXPR:
13553 14473207 : return RECUR (BIND_EXPR_BODY (t), want_rval);
13554 :
13555 70569624 : case CLEANUP_POINT_EXPR:
13556 70569624 : case MUST_NOT_THROW_EXPR:
13557 70569624 : case TRY_CATCH_EXPR:
13558 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
13559 : body could throw, even with catch (...) among handlers we'd need
13560 : to analyze them in detail if they couldn't rethrow it. More
13561 : importantly though, throws (jump_target) is just conservative,
13562 : and there could be e.g.
13563 : try
13564 : {
13565 : possibly_throwing_fn (args);
13566 : break;
13567 : }
13568 : catch (...)
13569 : {
13570 : }
13571 : or continue or return instead of break. So, clearing *jump_target
13572 : because we see catch (...) handler might mean we missed break
13573 : etc. */
13574 70569624 : case TRY_BLOCK:
13575 70569624 : case EH_SPEC_BLOCK:
13576 70569624 : case EXPR_STMT:
13577 70569624 : case PAREN_EXPR:
13578 : /* For convenience. */
13579 70569624 : case LOOP_EXPR:
13580 70569624 : case EXIT_EXPR:
13581 70569624 : return RECUR (TREE_OPERAND (t, 0), want_rval);
13582 :
13583 12917307 : case DECL_EXPR:
13584 12917307 : tmp = DECL_EXPR_DECL (t);
13585 8576325 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
13586 18920512 : && (processing_template_decl
13587 5518976 : ? !decl_maybe_constant_var_p (tmp)
13588 5034747 : : !decl_constant_var_p (tmp)))
13589 : {
13590 4959100 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
13591 : {
13592 8 : if (flags & tf_error)
13593 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
13594 : "%qD defined %<thread_local%> in "
13595 : "%<constexpr%> context", tmp);
13596 : return false;
13597 : }
13598 4959092 : else if (TREE_STATIC (tmp))
13599 : {
13600 160 : if (flags & tf_error)
13601 36 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
13602 : "%qD defined %<static%> in %<constexpr%> "
13603 : "context", tmp);
13604 : return false;
13605 : }
13606 4958932 : else if (!check_for_uninitialized_const_var
13607 4958932 : (tmp, /*constexpr_context_p=*/true, flags))
13608 : return false;
13609 : }
13610 12912188 : if (VAR_P (tmp))
13611 8571206 : return RECUR (DECL_INITIAL (tmp), want_rval);
13612 : return true;
13613 :
13614 44712 : case TRY_FINALLY_EXPR:
13615 44712 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
13616 44712 : && RECUR (TREE_OPERAND (t, 1), any));
13617 :
13618 0 : case EH_ELSE_EXPR:
13619 : /* maybe_apply_function_contracts uses this to check postconditions only
13620 : on normal return. */
13621 0 : return (RECUR (TREE_OPERAND (t, 1), any)
13622 0 : || RECUR (TREE_OPERAND (t, 0), any));
13623 :
13624 24422657 : case SCOPE_REF:
13625 24422657 : return RECUR (TREE_OPERAND (t, 1), want_rval);
13626 :
13627 49569048 : case TARGET_EXPR:
13628 49569048 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
13629 49360298 : && !TARGET_EXPR_ELIDING_P (t)
13630 89139812 : && !literal_type_p (TREE_TYPE (t)))
13631 : {
13632 900323 : if (flags & tf_error)
13633 : {
13634 25 : auto_diagnostic_group d;
13635 25 : if (constexpr_error (loc, fundef_p,
13636 : "temporary of non-literal type %qT in a "
13637 25 : "constant expression", TREE_TYPE (t)))
13638 25 : explain_non_literal_class (TREE_TYPE (t));
13639 25 : }
13640 : return false;
13641 : }
13642 : /* FALLTHRU */
13643 77217420 : case INIT_EXPR:
13644 77217420 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
13645 : return true;
13646 77098156 : return RECUR (TREE_OPERAND (t, 1), rval);
13647 :
13648 40935617 : case CONSTRUCTOR:
13649 40935617 : {
13650 40935617 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
13651 40935617 : constructor_elt *ce;
13652 5200364864 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
13653 235827662 : if (!RECUR (ce->value, want_rval))
13654 : return false;
13655 : return true;
13656 : }
13657 :
13658 21185696 : case TREE_LIST:
13659 21185696 : {
13660 21185696 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
13661 : || DECL_P (TREE_PURPOSE (t)));
13662 21185696 : if (!RECUR (TREE_VALUE (t), want_rval))
13663 : return false;
13664 18985047 : if (TREE_CHAIN (t) == NULL_TREE)
13665 : return true;
13666 55001 : return RECUR (TREE_CHAIN (t), want_rval);
13667 : }
13668 :
13669 15360827 : case TRUNC_DIV_EXPR:
13670 15360827 : case CEIL_DIV_EXPR:
13671 15360827 : case FLOOR_DIV_EXPR:
13672 15360827 : case ROUND_DIV_EXPR:
13673 15360827 : case TRUNC_MOD_EXPR:
13674 15360827 : case CEIL_MOD_EXPR:
13675 15360827 : case ROUND_MOD_EXPR:
13676 15360827 : {
13677 15360827 : tree denom = TREE_OPERAND (t, 1);
13678 15360827 : if (!RECUR (denom, rval))
13679 : return false;
13680 : /* We can't call cxx_eval_outermost_constant_expr on an expression
13681 : that hasn't been through instantiate_non_dependent_expr yet. */
13682 14841184 : if (!processing_template_decl)
13683 7869068 : denom = cxx_eval_outermost_constant_expr (denom, true);
13684 14841184 : if (integer_zerop (denom))
13685 : {
13686 378 : if (flags & tf_error)
13687 38 : constexpr_error (input_location, fundef_p,
13688 : "division by zero is not a constant expression");
13689 : return false;
13690 : }
13691 : else
13692 : {
13693 14840806 : want_rval = true;
13694 14840806 : return RECUR (TREE_OPERAND (t, 0), want_rval);
13695 : }
13696 : }
13697 :
13698 9635591 : case COMPOUND_EXPR:
13699 9635591 : {
13700 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
13701 : COMPOUND_EXPR; don't get confused. */
13702 9635591 : tree op0 = TREE_OPERAND (t, 0);
13703 9635591 : tree op1 = TREE_OPERAND (t, 1);
13704 9635591 : STRIP_NOPS (op1);
13705 9635591 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
13706 2706787 : return RECUR (op0, want_rval);
13707 : else
13708 6928804 : goto binary;
13709 : }
13710 :
13711 : /* If the first operand is the non-short-circuit constant, look at
13712 : the second operand; otherwise we only care about the first one for
13713 : potentiality. */
13714 18757495 : case TRUTH_AND_EXPR:
13715 18757495 : case TRUTH_ANDIF_EXPR:
13716 18757495 : tmp = boolean_true_node;
13717 18757495 : goto truth;
13718 7609988 : case TRUTH_OR_EXPR:
13719 7609988 : case TRUTH_ORIF_EXPR:
13720 7609988 : tmp = boolean_false_node;
13721 26367483 : truth:
13722 26367483 : {
13723 26367483 : tree op0 = TREE_OPERAND (t, 0);
13724 26367483 : tree op1 = TREE_OPERAND (t, 1);
13725 26367483 : if (!RECUR (op0, rval))
13726 : return false;
13727 18180041 : if (!(flags & tf_error) && RECUR (op1, rval))
13728 : /* When quiet, try to avoid expensive trial evaluation by first
13729 : checking potentiality of the second operand. */
13730 : return true;
13731 1824509 : if (!processing_template_decl)
13732 1425263 : op0 = cxx_eval_outermost_constant_expr (op0, true);
13733 1824509 : if (tree_int_cst_equal (op0, tmp))
13734 9132 : return (flags & tf_error) ? RECUR (op1, rval) : false;
13735 : else
13736 : return true;
13737 : }
13738 :
13739 : case PLUS_EXPR:
13740 : case MULT_EXPR:
13741 : case POINTER_PLUS_EXPR:
13742 : case RDIV_EXPR:
13743 : case EXACT_DIV_EXPR:
13744 : case MIN_EXPR:
13745 : case MAX_EXPR:
13746 : case LSHIFT_EXPR:
13747 : case RSHIFT_EXPR:
13748 : case LROTATE_EXPR:
13749 : case RROTATE_EXPR:
13750 : case BIT_IOR_EXPR:
13751 : case BIT_XOR_EXPR:
13752 : case BIT_AND_EXPR:
13753 : case TRUTH_XOR_EXPR:
13754 : case UNORDERED_EXPR:
13755 : case ORDERED_EXPR:
13756 : case UNLT_EXPR:
13757 : case UNLE_EXPR:
13758 : case UNGT_EXPR:
13759 : case UNGE_EXPR:
13760 : case UNEQ_EXPR:
13761 : case LTGT_EXPR:
13762 : case RANGE_EXPR:
13763 : case COMPLEX_EXPR:
13764 237280798 : want_rval = true;
13765 : /* Fall through. */
13766 249523439 : case ARRAY_REF:
13767 249523439 : case ARRAY_RANGE_REF:
13768 249523439 : case MEMBER_REF:
13769 249523439 : case DOTSTAR_EXPR:
13770 249523439 : case MEM_REF:
13771 249523439 : case BINARY_LEFT_FOLD_EXPR:
13772 249523439 : case BINARY_RIGHT_FOLD_EXPR:
13773 249523439 : binary:
13774 549802107 : for (i = 0; i < 2; ++i)
13775 406871956 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
13776 : return false;
13777 : return true;
13778 :
13779 : case VEC_PERM_EXPR:
13780 121551 : for (i = 0; i < 3; ++i)
13781 120601 : if (!RECUR (TREE_OPERAND (t, i), true))
13782 : return false;
13783 : return true;
13784 :
13785 7813197 : case COND_EXPR:
13786 7813197 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
13787 : {
13788 8 : if (flags & tf_error)
13789 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
13790 : "constant expression");
13791 : return false;
13792 : }
13793 : /* Fall through. */
13794 18681058 : case IF_STMT:
13795 18681058 : case VEC_COND_EXPR:
13796 : /* If the condition is a known constant, we know which of the legs we
13797 : care about; otherwise we only require that the condition and
13798 : either of the legs be potentially constant. */
13799 18681058 : tmp = TREE_OPERAND (t, 0);
13800 18681058 : if (!RECUR (tmp, rval))
13801 : return false;
13802 16390962 : if (!processing_template_decl)
13803 14049742 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
13804 : /* potential_constant_expression* isn't told if it is called for
13805 : manifestly_const_eval or not, so for consteval if always
13806 : process both branches as if the condition is not a known
13807 : constant. */
13808 16390962 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
13809 : {
13810 16312415 : if (integer_zerop (tmp))
13811 3526121 : return RECUR (TREE_OPERAND (t, 2), want_rval);
13812 12786294 : else if (TREE_CODE (tmp) == INTEGER_CST)
13813 3918445 : return RECUR (TREE_OPERAND (t, 1), want_rval);
13814 : }
13815 8946396 : tmp = *jump_target;
13816 10276838 : for (i = 1; i < 3; ++i)
13817 : {
13818 10130871 : tree this_jump_target = tmp;
13819 10130871 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
13820 : want_rval, strict, now, fundef_p,
13821 : tf_none, &this_jump_target))
13822 : {
13823 9104831 : if (returns (&this_jump_target) || throws (&this_jump_target))
13824 2450560 : *jump_target = this_jump_target;
13825 6349869 : else if (!returns (jump_target) && !throws (jump_target))
13826 : {
13827 6349869 : if (breaks (&this_jump_target)
13828 6349869 : || continues (&this_jump_target))
13829 45 : *jump_target = this_jump_target;
13830 6349869 : if (i == 1)
13831 : {
13832 : /* If the then branch is potentially constant, but
13833 : does not return, check if the else branch
13834 : couldn't return, break or continue. */
13835 5312035 : hash_set<tree> pset;
13836 5312035 : check_for_return_continue_data data = { &pset, NULL_TREE,
13837 : NULL_TREE,
13838 5312035 : false };
13839 10624070 : if (tree ret_expr
13840 5312035 : = cp_walk_tree (&TREE_OPERAND (t, 2),
13841 : check_for_return_continue, &data,
13842 : &pset))
13843 79527 : *jump_target = ret_expr;
13844 5232508 : else if (*jump_target == NULL_TREE)
13845 : {
13846 5232466 : if (data.continue_stmt)
13847 0 : *jump_target = data.continue_stmt;
13848 5232466 : else if (data.break_stmt)
13849 13 : *jump_target = data.break_stmt;
13850 : }
13851 5312035 : if (data.could_throw)
13852 55653 : *jump_target = void_node;
13853 5312035 : }
13854 : }
13855 8800429 : return true;
13856 : }
13857 : }
13858 145967 : if (flags & tf_error)
13859 : {
13860 3 : if (TREE_CODE (t) == IF_STMT)
13861 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
13862 : "constant expression");
13863 : else
13864 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
13865 : "constant expression", t);
13866 : }
13867 : return false;
13868 :
13869 1770 : case VEC_INIT_EXPR:
13870 1770 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
13871 : return true;
13872 871 : if (flags & tf_error)
13873 : {
13874 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
13875 : "initialization"))
13876 2 : diagnose_non_constexpr_vec_init (t);
13877 : }
13878 : return false;
13879 :
13880 : case TYPE_DECL:
13881 : case TAG_DEFN:
13882 : /* We can see these in statement-expressions. */
13883 : return true;
13884 :
13885 1469326 : case CLEANUP_STMT:
13886 1469326 : if (!RECUR (CLEANUP_BODY (t), any))
13887 : return false;
13888 2453347 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
13889 : return false;
13890 : return true;
13891 :
13892 : case EMPTY_CLASS_EXPR:
13893 : return true;
13894 :
13895 128 : case GOTO_EXPR:
13896 128 : {
13897 128 : tree *target = &TREE_OPERAND (t, 0);
13898 : /* Gotos representing break, continue and cdtor return are OK. */
13899 128 : if (breaks (target) || continues (target) || returns (target))
13900 : {
13901 108 : *jump_target = *target;
13902 108 : return true;
13903 : }
13904 20 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
13905 : /* The user didn't write this goto, this isn't the problem. */
13906 : return true;
13907 16 : if (flags & tf_error)
13908 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
13909 : "expression");
13910 : return false;
13911 : }
13912 :
13913 : case ASSERTION_STMT:
13914 : case PRECONDITION_STMT:
13915 : case POSTCONDITION_STMT:
13916 : /* Contracts are not supposed to alter this; we have to check that this
13917 : is not violated at a later time. */
13918 : return true;
13919 :
13920 266 : case LABEL_EXPR:
13921 266 : t = LABEL_EXPR_LABEL (t);
13922 266 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
13923 : return true;
13924 25 : else if (flags & tf_error)
13925 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
13926 : "function only available with %<-std=c++23%> or "
13927 : "%<-std=gnu++23%>");
13928 : return false;
13929 :
13930 10192 : case ANNOTATE_EXPR:
13931 10192 : return RECUR (TREE_OPERAND (t, 0), rval);
13932 :
13933 185694 : case BIT_CAST_EXPR:
13934 185694 : return RECUR (TREE_OPERAND (t, 0), rval);
13935 :
13936 : /* Coroutine await, yield and return expressions are not. */
13937 768 : case CO_AWAIT_EXPR:
13938 768 : case CO_YIELD_EXPR:
13939 768 : case CO_RETURN_EXPR:
13940 768 : case TEMPLATE_FOR_STMT:
13941 768 : if (flags & tf_error)
13942 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
13943 : "%qE is not a constant expression", t);
13944 : return false;
13945 :
13946 : /* Assume a TU-local entity is not constant, we'll error later when
13947 : instantiating. */
13948 : case TU_LOCAL_ENTITY:
13949 : return false;
13950 :
13951 : /* A splice expression is dependent, but will be constant after
13952 : substitution. */
13953 : case SPLICE_EXPR:
13954 : return true;
13955 :
13956 30 : case NONTYPE_ARGUMENT_PACK:
13957 30 : {
13958 30 : tree args = ARGUMENT_PACK_ARGS (t);
13959 30 : int len = TREE_VEC_LENGTH (args);
13960 90 : for (int i = 0; i < len; ++i)
13961 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
13962 : return false;
13963 : return true;
13964 : }
13965 :
13966 0 : default:
13967 0 : if (objc_non_constant_expr_p (t))
13968 : return false;
13969 :
13970 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13971 0 : gcc_unreachable ();
13972 : return false;
13973 : }
13974 : #undef RECUR
13975 4924887037 : }
13976 :
13977 : bool
13978 1760644649 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13979 : bool fundef_p, tsubst_flags_t flags)
13980 : {
13981 1760644649 : if (flags & tf_error)
13982 : {
13983 : /* Check potentiality quietly first, as that could be performed more
13984 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13985 : that fails, replay the check noisily to give errors. */
13986 11445971 : flags &= ~tf_error;
13987 11445971 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13988 : flags))
13989 : return true;
13990 1103 : flags |= tf_error;
13991 : }
13992 :
13993 1749199781 : tree target = NULL_TREE;
13994 1749199781 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13995 1749199781 : flags, &target);
13996 : }
13997 :
13998 : /* The main entry point to the above. */
13999 :
14000 : bool
14001 445509934 : potential_constant_expression (tree t)
14002 : {
14003 445509934 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
14004 : /*now*/false, /*fundef_p*/false,
14005 445509934 : tf_none);
14006 : }
14007 :
14008 : /* As above, but require a constant rvalue. */
14009 :
14010 : bool
14011 35406166 : potential_rvalue_constant_expression (tree t)
14012 : {
14013 35406166 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
14014 : /*now*/false, /*fundef_p*/false,
14015 35406166 : tf_none);
14016 : }
14017 :
14018 : /* Like above, but complain about non-constant expressions. */
14019 :
14020 : bool
14021 88 : require_potential_constant_expression (tree t)
14022 : {
14023 88 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
14024 : /*now*/false, /*fundef_p*/false,
14025 88 : tf_warning_or_error);
14026 : }
14027 :
14028 : /* Cross product of the above. */
14029 :
14030 : bool
14031 131686 : require_potential_rvalue_constant_expression (tree t)
14032 : {
14033 131686 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
14034 : /*now*/false, /*fundef_p*/false,
14035 131686 : tf_warning_or_error);
14036 : }
14037 :
14038 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
14039 :
14040 : bool
14041 296 : require_potential_rvalue_constant_expression_fncheck (tree t)
14042 : {
14043 296 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
14044 : /*now*/false, /*fundef_p*/true,
14045 296 : tf_warning_or_error);
14046 : }
14047 :
14048 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
14049 :
14050 : bool
14051 1149 : require_rvalue_constant_expression (tree t)
14052 : {
14053 1149 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
14054 : /*now*/true, /*fundef_p*/false,
14055 1149 : tf_warning_or_error);
14056 : }
14057 :
14058 : /* Like potential_constant_expression, but don't consider possible constexpr
14059 : substitution of the current function. That is, PARM_DECL qualifies under
14060 : potential_constant_expression, but not here.
14061 :
14062 : This is basically what you can check when any actual constant values might
14063 : be value-dependent. */
14064 :
14065 : bool
14066 926298770 : is_constant_expression (tree t)
14067 : {
14068 926298770 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
14069 : /*now*/true, /*fundef_p*/false,
14070 926298770 : tf_none);
14071 : }
14072 :
14073 : /* As above, but expect an rvalue. */
14074 :
14075 : bool
14076 156389144 : is_rvalue_constant_expression (tree t)
14077 : {
14078 156389144 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
14079 : /*now*/true, /*fundef_p*/false,
14080 156389144 : tf_none);
14081 : }
14082 :
14083 : /* Like above, but complain about non-constant expressions. */
14084 :
14085 : bool
14086 11312752 : require_constant_expression (tree t)
14087 : {
14088 11312752 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
14089 : /*now*/true, /*fundef_p*/false,
14090 11312752 : tf_warning_or_error);
14091 : }
14092 :
14093 : /* Like is_constant_expression, but allow const variables that are not allowed
14094 : under constexpr rules. */
14095 :
14096 : bool
14097 174148693 : is_static_init_expression (tree t)
14098 : {
14099 174148693 : return potential_constant_expression_1 (t, /*want_rval*/false,
14100 : /*strict*/false, /*now*/true,
14101 174148693 : /*fundef_p*/false, tf_none);
14102 : }
14103 :
14104 : /* Returns true if T is a potential constant expression that is not
14105 : instantiation-dependent, and therefore a candidate for constant folding even
14106 : in a template. */
14107 :
14108 : bool
14109 894810338 : is_nondependent_constant_expression (tree t)
14110 : {
14111 894810338 : return (!type_unknown_p (t)
14112 894810288 : && is_constant_expression (t)
14113 1679269213 : && !instantiation_dependent_expression_p (t));
14114 : }
14115 :
14116 : /* Returns true if T is a potential static initializer expression that is not
14117 : instantiation-dependent. */
14118 :
14119 : bool
14120 174148693 : is_nondependent_static_init_expression (tree t)
14121 : {
14122 174148693 : return (!type_unknown_p (t)
14123 174148693 : && is_static_init_expression (t)
14124 315007544 : && !instantiation_dependent_expression_p (t));
14125 : }
14126 :
14127 : /* True iff FN is an implicitly constexpr function. */
14128 :
14129 : bool
14130 474518 : decl_implicit_constexpr_p (tree fn)
14131 : {
14132 474518 : if (!(flag_implicit_constexpr
14133 18 : && TREE_CODE (fn) == FUNCTION_DECL
14134 18 : && DECL_DECLARED_CONSTEXPR_P (fn)))
14135 : return false;
14136 :
14137 18 : if (DECL_CLONED_FUNCTION_P (fn))
14138 0 : fn = DECL_CLONED_FUNCTION (fn);
14139 :
14140 18 : return (DECL_LANG_SPECIFIC (fn)
14141 18 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
14142 : }
14143 :
14144 : /* Finalize constexpr processing after parsing. */
14145 :
14146 : void
14147 100997 : fini_constexpr (void)
14148 : {
14149 : /* The contexpr call and fundef copies tables are no longer needed. */
14150 100997 : constexpr_call_table = NULL;
14151 100997 : fundef_copies_table = NULL;
14152 100997 : }
14153 :
14154 : /* Return true if T is one of the heap vars. Include "heap deleted" if
14155 : INCLUDE_DELETED_P. */
14156 :
14157 : bool
14158 5849750 : heap_var_p (tree t, bool include_deleted_p/*=false*/)
14159 : {
14160 5849750 : return (DECL_NAME (t) == heap_uninit_identifier
14161 5710910 : || DECL_NAME (t) == heap_identifier
14162 5681978 : || DECL_NAME (t) == heap_vec_uninit_identifier
14163 5679373 : || DECL_NAME (t) == heap_vec_identifier
14164 11525902 : || (include_deleted_p && DECL_NAME (t) == heap_deleted_identifier));
14165 : }
14166 :
14167 : #include "gt-cp-constexpr.h"
|