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 193677105 : is_instantiation_of_constexpr (tree fun)
63 : {
64 268255828 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
65 119595598 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
66 287969680 : || (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 192971953 : literal_type_p (tree t)
74 : {
75 194607792 : if (SCALAR_TYPE_P (t)
76 91662059 : || VECTOR_TYPE_P (t)
77 91630406 : || TYPE_REF_P (t)
78 76029979 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
79 : return true;
80 72778370 : if (CLASS_TYPE_P (t))
81 : {
82 71142319 : t = complete_type (t);
83 71142319 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
84 71142319 : return CLASSTYPE_LITERAL_P (t);
85 : }
86 1636051 : if (TREE_CODE (t) == ARRAY_TYPE)
87 1635839 : 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 336120032 : ensure_literal_type_for_constexpr_object (tree decl)
97 : {
98 336120032 : tree type = TREE_TYPE (decl);
99 336120032 : if (VAR_P (decl)
100 126827746 : && (DECL_DECLARED_CONSTEXPR_P (decl)
101 88688572 : || var_in_constexpr_fn (decl))
102 393750693 : && !processing_template_decl)
103 : {
104 37318637 : tree stype = strip_array_types (type);
105 37318637 : 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 37318631 : else if (!literal_type_p (type))
109 : {
110 24131 : 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 24078 : else if (cxx_dialect < cxx23)
120 : {
121 18264 : 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 18264 : cp_function_chain->invalid_constexpr = true;
132 : }
133 : }
134 37294500 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
135 37294500 : && 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 336120032 : 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 997037071 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
202 : const constexpr_fundef *rhs)
203 : {
204 969362867 : 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 956874098 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
212 : {
213 956874098 : return DECL_UID (fundef->decl);
214 : }
215 :
216 : /* Return a previously saved definition of function FUN. */
217 :
218 : constexpr_fundef *
219 110573658 : retrieve_constexpr_fundef (tree fun)
220 : {
221 110573658 : if (constexpr_fundef_table == NULL)
222 : return NULL;
223 :
224 110569210 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
225 110569210 : 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 31720614 : is_valid_constexpr_fn (tree fun, bool complain)
233 : {
234 31720614 : bool ret = true;
235 :
236 63441228 : if (DECL_INHERITED_CTOR (fun)
237 3681857 : && 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 31720614 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
247 63930216 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
248 32209602 : if (!literal_type_p (TREE_TYPE (parm)))
249 : {
250 13314 : ret = false;
251 13314 : 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 52484113 : 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 63441220 : 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 31720610 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
279 : {
280 27369573 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
281 27369573 : if (!literal_type_p (rettype))
282 : {
283 50745 : ret = false;
284 50745 : 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 27369573 : if (cxx_dialect < cxx14
296 33089 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
297 27370704 : && !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 4351037 : 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 31720614 : 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 4613 : 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 4613 : auto_vec<tree, 2> fields;
345 9322 : do
346 : {
347 9322 : fields.safe_push (TREE_OPERAND (member, 1));
348 9322 : member = TREE_OPERAND (member, 0);
349 : }
350 9322 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
351 18650 : && 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 9322 : while (field = fields.pop(),
358 9322 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
359 : {
360 4709 : tree ctor;
361 : /* If there is already an outer constructor entry for the anonymous
362 : aggregate FIELD, use it; otherwise, insert one. */
363 4709 : if (vec_safe_is_empty (*vec)
364 198 : || (*vec)->last().index != field)
365 : {
366 4607 : ctor = build_constructor (TREE_TYPE (field), NULL);
367 4607 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
368 : }
369 : else
370 102 : ctor = (*vec)->last().value;
371 4709 : 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 4613 : gcc_assert (fields.is_empty());
377 4613 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
378 :
379 4613 : return true;
380 4613 : }
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 6077118 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
390 : {
391 6783746 : tree member, init;
392 6783746 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
393 3092064 : t = TREE_OPERAND (t, 0);
394 6783746 : if (TREE_CODE (t) == EXPR_STMT)
395 3092000 : t = TREE_OPERAND (t, 0);
396 6783746 : if (t == error_mark_node)
397 : return false;
398 6783737 : if (TREE_CODE (t) == STATEMENT_LIST)
399 : {
400 1601943 : for (tree stmt : tsi_range (t))
401 899090 : if (! build_data_member_initialization (stmt, vec))
402 6077118 : return false;
403 : return true;
404 : }
405 6080884 : 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 672692 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
413 : }
414 5408192 : if (TREE_CODE (t) == CONVERT_EXPR)
415 3019893 : t = TREE_OPERAND (t, 0);
416 5408192 : if (TREE_CODE (t) == INIT_EXPR)
417 : {
418 2872369 : member = TREE_OPERAND (t, 0);
419 2872369 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
420 : }
421 2535823 : else if (TREE_CODE (t) == CALL_EXPR)
422 : {
423 2059857 : tree fn = get_callee_fndecl (t);
424 4119714 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
425 : /* We're only interested in calls to subobject constructors. */
426 : return true;
427 1653466 : 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 1653466 : init = break_out_target_exprs (t);
432 : }
433 475966 : else if (TREE_CODE (t) == BIND_EXPR)
434 33936 : 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 4525835 : if (INDIRECT_REF_P (member))
439 62335 : member = TREE_OPERAND (member, 0);
440 4525835 : if (TREE_CODE (member) == NOP_EXPR)
441 : {
442 557161 : tree op = member;
443 557161 : STRIP_NOPS (op);
444 557161 : if (TREE_CODE (op) == ADDR_EXPR)
445 : {
446 664 : 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 556497 : else if (op == current_class_ptr
454 1112843 : && (same_type_ignoring_top_level_qualifiers_p
455 556346 : (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 477636 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
464 : }
465 : }
466 4525835 : if (TREE_CODE (member) == ADDR_EXPR)
467 1159304 : member = TREE_OPERAND (member, 0);
468 4525835 : if (TREE_CODE (member) == COMPONENT_REF)
469 : {
470 3943813 : tree aggr = TREE_OPERAND (member, 0);
471 3943813 : if (TREE_CODE (aggr) == VAR_DECL)
472 : /* Initializing a local variable, don't add anything. */
473 : return true;
474 3943811 : if (TREE_CODE (aggr) != COMPONENT_REF)
475 : /* Normal member initialization. */
476 3939195 : member = TREE_OPERAND (member, 1);
477 4616 : else if (VAR_P (get_base_address (aggr)))
478 : /* Initializing a local variable, don't add anything. */
479 : return true;
480 4613 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
481 : /* Initializing a member of an anonymous union. */
482 4613 : 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 5643715 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
492 1324 : (*vec)->last().value = init;
493 : else
494 4519893 : 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 6021996 : 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 6021996 : 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 3603323 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
595 : {
596 3603323 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
597 3603323 : tree field_type;
598 3603323 : unsigned i;
599 3603323 : constructor_elt *ce;
600 :
601 3603323 : if (pri)
602 90719 : field_type = BINFO_TYPE (pri);
603 3512604 : else if (TYPE_CONTAINS_VPTR_P (type))
604 37712 : 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 170106 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
611 132270 : if (TREE_TYPE (ce->index) == field_type)
612 : break;
613 :
614 150687 : 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 3680985 : build_constexpr_constructor_member_initializers (tree type, tree body)
631 : {
632 3680985 : vec<constructor_elt, va_gc> *vec = NULL;
633 3680985 : bool ok = true;
634 5735354 : while (true)
635 5735354 : switch (TREE_CODE (body))
636 : {
637 2054290 : case MUST_NOT_THROW_EXPR:
638 2054290 : case EH_SPEC_BLOCK:
639 2054290 : case TRY_FINALLY_EXPR: // For C++26 postconditions.
640 2054290 : body = TREE_OPERAND (body, 0);
641 2054290 : break;
642 :
643 79 : case STATEMENT_LIST:
644 2054554 : 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 3680985 : case BIND_EXPR:
653 3680985 : body = BIND_EXPR_BODY (body);
654 3680985 : goto found;
655 :
656 0 : default:
657 0 : gcc_unreachable ();
658 : }
659 3680985 : found:
660 3680985 : 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 3680985 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
667 : {
668 1987393 : body = TREE_OPERAND (body, 0);
669 1987393 : if (TREE_CODE (body) == EXPR_STMT)
670 1987357 : body = TREE_OPERAND (body, 0);
671 1987393 : if (TREE_CODE (body) == INIT_EXPR
672 1987393 : && (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 1987393 : ok = build_data_member_initialization (body, &vec);
680 : }
681 1693592 : else if (TREE_CODE (body) == STATEMENT_LIST)
682 : {
683 4873192 : for (tree stmt : tsi_range (body))
684 : {
685 3185118 : ok = build_data_member_initialization (stmt, &vec);
686 3185118 : if (!ok)
687 : break;
688 : }
689 : }
690 5517 : else if (EXPR_P (body))
691 5517 : ok = build_data_member_initialization (body, &vec);
692 : else
693 0 : gcc_assert (errorcount > 0);
694 3680985 : if (ok)
695 : {
696 3680976 : if (vec_safe_length (vec) > 0)
697 : {
698 : /* In a delegating constructor, return the target. */
699 3403206 : constructor_elt *ce = &(*vec)[0];
700 3403206 : if (ce->index == current_class_ptr)
701 : {
702 77653 : body = ce->value;
703 77653 : vec_free (vec);
704 77653 : return body;
705 : }
706 : }
707 3603323 : vec = sort_constexpr_mem_initializers (type, vec);
708 3603323 : 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 562447458 : get_function_named_in_call (tree t)
720 : {
721 562447458 : tree callee = cp_get_callee (t);
722 562447458 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
723 562447458 : 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 31008574 : massage_constexpr_body (tree fun, tree body)
800 : {
801 62017148 : if (DECL_CONSTRUCTOR_P (fun))
802 3680985 : body = build_constexpr_constructor_member_initializers
803 3680985 : (DECL_CONTEXT (fun), body);
804 27327589 : 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 31008574 : 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 3333536 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
820 : {
821 : /* We allow uninitialized bases/fields in C++20. */
822 3333536 : 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 167100604 : maybe_save_constexpr_fundef (tree fun)
929 : {
930 167100604 : if (processing_template_decl
931 69018106 : || cp_function_chain->invalid_constexpr
932 236100553 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
933 136092324 : 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 52784239 : bool implicit = false;
938 52784239 : 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 52784239 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
953 : return;
954 :
955 31063984 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
956 :
957 31063984 : if (!is_valid_constexpr_fn (fun, complain))
958 : return;
959 :
960 31008494 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
961 31008494 : 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 31008458 : bool potential = potential_rvalue_constant_expression (massaged);
970 31008458 : if (!potential && complain)
971 264 : require_potential_rvalue_constant_expression_fncheck (massaged);
972 :
973 34689420 : if (DECL_CONSTRUCTOR_P (fun) && potential
974 34685445 : && !DECL_DEFAULTED_FN (fun))
975 : {
976 3333480 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
977 : massaged, complain))
978 : potential = false;
979 3333454 : 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 3328585 : massaged = DECL_SAVED_TREE (fun);
984 3328585 : potential = potential_rvalue_constant_expression (massaged);
985 3328585 : if (!potential && complain)
986 20 : require_potential_rvalue_constant_expression_fncheck (massaged);
987 : }
988 : }
989 :
990 31008458 : 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 31008291 : 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 31008280 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1014 31008280 : bool clear_ctx = false;
1015 31008280 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1016 : {
1017 31008280 : clear_ctx = true;
1018 31008280 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1019 : }
1020 31008280 : tree saved_fn = current_function_decl;
1021 31008280 : current_function_decl = fun;
1022 31008280 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1023 31008280 : current_function_decl = saved_fn;
1024 31008280 : if (clear_ctx)
1025 31008280 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1026 31008280 : 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 60592 : entry.result = error_mark_node;
1031 :
1032 31008280 : 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 31046962 : register_constexpr_fundef (const constexpr_fundef &value)
1040 : {
1041 : /* Create the constexpr function table if necessary. */
1042 31046962 : if (constexpr_fundef_table == NULL)
1043 27554 : constexpr_fundef_table
1044 27554 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1045 :
1046 31046962 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1047 31046962 : (const_cast<constexpr_fundef *> (&value), INSERT);
1048 :
1049 31046962 : gcc_assert (*slot == NULL);
1050 31046962 : *slot = ggc_alloc<constexpr_fundef> ();
1051 31046962 : **slot = value;
1052 31046962 : }
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 64562928 : 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 573863461 : constexpr_global_ctx ()
1239 573863461 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1240 573863461 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1241 573863461 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1242 573863461 : contract_condition_non_const (false), state_dependent (false) {}
1243 :
1244 463532064 : bool is_outside_lifetime (tree t)
1245 : {
1246 463532064 : if (tree *p = values.get (t))
1247 209775510 : 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 587445909 : tree get_value (tree t)
1261 : {
1262 587445909 : if (tree *p = values.get (t))
1263 309018208 : if (*p != void_node && *p != void_list_node)
1264 300857468 : return *p;
1265 : return NULL_TREE;
1266 : }
1267 5116 : tree *get_raw_value_ptr (tree t)
1268 : {
1269 5116 : return values.get (t);
1270 : }
1271 125670192 : tree *get_value_ptr (tree t, bool initializing)
1272 : {
1273 125670192 : if (modifiable && !modifiable->contains (t))
1274 : return nullptr;
1275 125670175 : if (tree *p = values.get (t))
1276 : {
1277 125660657 : 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 302311832 : 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 302311832 : bool already_in_map;
1295 302311832 : tree &slot = values.get_or_insert (t, &already_in_map);
1296 302311832 : bool newval = !already_in_map || slot == void_list_node;
1297 302311832 : slot = v;
1298 302311832 : if (newval && modifiable)
1299 64 : modifiable->add (t);
1300 302311832 : }
1301 242453924 : void destroy_value (tree t, bool past_storage_end = true)
1302 : {
1303 242453924 : if (TREE_CODE (t) == VAR_DECL
1304 242453924 : || TREE_CODE (t) == PARM_DECL
1305 : || TREE_CODE (t) == RESULT_DECL)
1306 242745264 : values.put (t, past_storage_end ? void_list_node : void_node);
1307 : else
1308 9269 : values.remove (t);
1309 242453924 : }
1310 19291154 : void clear_value (tree t)
1311 : {
1312 38582308 : 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 173636 : modifiable_tracker (constexpr_global_ctx *g)
1334 173636 : : global (g), previous_set (g->modifiable)
1335 : {
1336 173636 : global->modifiable = &set;
1337 173636 : }
1338 173636 : ~modifiable_tracker ()
1339 : {
1340 173700 : for (tree t: set)
1341 64 : global->clear_value (t);
1342 173636 : global->modifiable = previous_set;
1343 173636 : }
1344 : };
1345 :
1346 : /* The constexpr expansion context. CALL is the current function
1347 : expansion, CTOR is the current aggregate initializer, OBJECT is the
1348 : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1349 :
1350 : struct constexpr_ctx {
1351 : /* The part of the context that needs to be unique to the whole
1352 : cxx_eval_outermost_constant_expr invocation. */
1353 : constexpr_global_ctx *global;
1354 : /* The innermost call we're evaluating. */
1355 : constexpr_call *call;
1356 : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1357 : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1358 : vec<tree> *save_exprs;
1359 : /* The CONSTRUCTOR we're currently building up for an aggregate
1360 : initializer. */
1361 : tree ctor;
1362 : /* The object we're building the CONSTRUCTOR for. */
1363 : tree object;
1364 : /* If inside SWITCH_EXPR. */
1365 : constexpr_switch_state *css_state;
1366 : /* The aggregate initialization context inside which this one is nested. This
1367 : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1368 : const constexpr_ctx *parent;
1369 :
1370 : /* Whether we should error on a non-constant expression or fail quietly.
1371 : This flag needs to be here, but some of the others could move to global
1372 : if they get larger than a word. */
1373 : bool quiet;
1374 : /* Whether we are strictly conforming to constant expression rules or
1375 : trying harder to get a constant value. */
1376 : bool strict;
1377 : /* Whether __builtin_is_constant_evaluated () should be true. */
1378 : mce_value manifestly_const_eval;
1379 : };
1380 :
1381 : /* Return ctx->quiet. For use in reflect.cc. */
1382 :
1383 : bool
1384 1376 : cxx_constexpr_quiet_p (const constexpr_ctx *ctx)
1385 : {
1386 1376 : return ctx->quiet;
1387 : }
1388 :
1389 : /* Return ctx->manifestly_const_eval. For use in reflect.cc. */
1390 :
1391 : mce_value
1392 388 : cxx_constexpr_manifestly_const_eval (const constexpr_ctx *ctx)
1393 : {
1394 388 : return ctx->manifestly_const_eval;
1395 : }
1396 :
1397 : /* Return ctx->call->fundef->decl or NULL_TREE. For use in
1398 : reflect.cc. */
1399 :
1400 : tree
1401 2120 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1402 : {
1403 2120 : if (ctx->call)
1404 958 : return ctx->call->fundef->decl;
1405 : else
1406 : return NULL_TREE;
1407 : }
1408 :
1409 : /* Return ctx->global->consteval_block. For use in reflect.cc. */
1410 :
1411 : tree
1412 159 : cxx_constexpr_consteval_block (const constexpr_ctx *ctx)
1413 : {
1414 159 : return ctx->global->consteval_block;
1415 : }
1416 :
1417 : /* Return true if T's lifetime began within the core constant expression
1418 : currently under evaluation. */
1419 :
1420 : bool
1421 108 : cxx_constexpr_in_eval_lifetime_p (const constexpr_ctx *ctx, tree t)
1422 : {
1423 108 : return ctx->global->in_eval_lifetime_p (t);
1424 : }
1425 :
1426 : /* Predicates for the meaning of *jump_target. */
1427 :
1428 : static bool
1429 110517127 : returns (tree *jump_target)
1430 : {
1431 19312580 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1432 : }
1433 :
1434 : static bool
1435 105207755 : breaks (tree *jump_target)
1436 : {
1437 105207755 : return (*jump_target
1438 105207755 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1439 339 : && LABEL_DECL_BREAK (*jump_target))
1440 1377992 : || TREE_CODE (*jump_target) == BREAK_STMT
1441 1239329 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1442 : }
1443 :
1444 : static bool
1445 141831214 : continues (tree *jump_target)
1446 : {
1447 141831214 : return (*jump_target
1448 141831214 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1449 339 : && LABEL_DECL_CONTINUE (*jump_target))
1450 1254118 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1451 : }
1452 :
1453 : static bool
1454 12964111 : switches (tree *jump_target)
1455 : {
1456 78173 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1457 : }
1458 :
1459 : static bool
1460 1909499845 : throws (tree *jump_target)
1461 : {
1462 : /* void_node is for use in potential_constant_expression_1, otherwise
1463 : it should an artificial VAR_DECL created by constant evaluation
1464 : of __cxa_allocate_exception (). */
1465 213423118 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1466 : }
1467 :
1468 : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1469 : references and objects are in effect. */
1470 :
1471 : static bool
1472 268318197 : p2280_active_p (const constexpr_ctx *ctx)
1473 : {
1474 34317096 : if (ctx->manifestly_const_eval != mce_true)
1475 : /* Disable these relaxations during speculative constexpr folding,
1476 : as it can significantly increase compile time/memory use
1477 : (PR119387). */
1478 : return false;
1479 :
1480 : /* P2280R4 was accepted as a DR against C++11. */
1481 0 : return cxx_dialect >= cxx11;
1482 : }
1483 :
1484 : /* Remove T from the global values map, checking for attempts to destroy
1485 : a value that has already finished its lifetime. */
1486 :
1487 : static void
1488 242070622 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1489 : {
1490 242070622 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1491 : return;
1492 :
1493 : /* Don't error again here if we've already reported a problem. */
1494 242070561 : if (!*non_constant_p
1495 210971287 : && DECL_P (t)
1496 : /* Non-trivial destructors have their lifetimes ended explicitly
1497 : with a clobber, so don't worry about it here. */
1498 210962224 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1499 : /* ...except parameters are remapped in cxx_eval_call_expression,
1500 : and the destructor call during cleanup won't be able to tell that
1501 : this value has already been destroyed, so complain now. This is
1502 : not quite unobservable, but is extremely unlikely to crop up in
1503 : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1504 765367 : || TREE_CODE (t) == PARM_DECL)
1505 452268869 : && ctx->global->is_outside_lifetime (t))
1506 : {
1507 54 : if (!ctx->quiet)
1508 : {
1509 18 : auto_diagnostic_group d;
1510 18 : error ("destroying %qE outside its lifetime", t);
1511 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1512 18 : }
1513 54 : *non_constant_p = true;
1514 : }
1515 242070561 : ctx->global->destroy_value (t);
1516 : }
1517 :
1518 : /* This internal flag controls whether we should avoid doing anything during
1519 : constexpr evaluation that would cause extra DECL_UID generation, such as
1520 : template instantiation and function body copying. */
1521 :
1522 : static bool uid_sensitive_constexpr_evaluation_value;
1523 :
1524 : /* An internal counter that keeps track of the number of times
1525 : uid_sensitive_constexpr_evaluation_p returned true. */
1526 :
1527 : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1528 :
1529 : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1530 : increments the corresponding counter. */
1531 :
1532 : static bool
1533 9012853 : uid_sensitive_constexpr_evaluation_p ()
1534 : {
1535 8926633 : if (uid_sensitive_constexpr_evaluation_value)
1536 : {
1537 240570 : ++uid_sensitive_constexpr_evaluation_true_counter;
1538 240561 : return true;
1539 : }
1540 : else
1541 : return false;
1542 : }
1543 :
1544 : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1545 : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1546 : during the lifetime of the sentinel object. Upon its destruction, the
1547 : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1548 :
1549 80661305 : uid_sensitive_constexpr_evaluation_sentinel
1550 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1551 80661305 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1552 : {
1553 80661305 : }
1554 :
1555 : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1556 : records the current number of times that uid_sensitive_constexpr_evaluation_p
1557 : has been called and returned true. */
1558 :
1559 3839376633 : uid_sensitive_constexpr_evaluation_checker
1560 : ::uid_sensitive_constexpr_evaluation_checker ()
1561 3839376633 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1562 : {
1563 3839376633 : }
1564 :
1565 : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1566 : some constexpr evaluation was restricted due to u_s_c_e_p being called
1567 : and returning true during the lifetime of this checker object. */
1568 :
1569 : bool
1570 2546553406 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1571 : {
1572 2546553406 : return (uid_sensitive_constexpr_evaluation_value
1573 2546553406 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1574 : }
1575 :
1576 :
1577 : /* A table of all constexpr calls that have been evaluated by the
1578 : compiler in this translation unit. */
1579 :
1580 : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1581 :
1582 : /* Compute a hash value for a constexpr call representation. */
1583 :
1584 : inline hashval_t
1585 260291600 : constexpr_call_hasher::hash (constexpr_call *info)
1586 : {
1587 260291600 : return info->hash;
1588 : }
1589 :
1590 : /* Return true if the objects pointed to by P and Q represent calls
1591 : to the same constexpr function with the same arguments.
1592 : Otherwise, return false. */
1593 :
1594 : bool
1595 267227942 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1596 : {
1597 267227942 : if (lhs == rhs)
1598 : return true;
1599 267227942 : if (lhs->hash != rhs->hash)
1600 : return false;
1601 27674204 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1602 : return false;
1603 27674204 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1604 : }
1605 :
1606 : /* Initialize the constexpr call table, if needed. */
1607 :
1608 : static void
1609 36834424 : maybe_initialize_constexpr_call_table (void)
1610 : {
1611 36834424 : if (constexpr_call_table == NULL)
1612 18385 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1613 36834424 : }
1614 :
1615 : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1616 : a function happens to get called recursively, we unshare the callee
1617 : function's body and evaluate this unshared copy instead of evaluating the
1618 : original body.
1619 :
1620 : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1621 : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1622 : that's keyed off of the original FUNCTION_DECL and whose value is a
1623 : TREE_LIST of this function's unused copies awaiting reuse.
1624 :
1625 : This is not GC-deletable to avoid GC affecting UID generation. */
1626 :
1627 : static GTY(()) decl_tree_map *fundef_copies_table;
1628 :
1629 : /* Reuse a copy or create a new unshared copy of the function FUN.
1630 : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1631 : is parms, TYPE is result. */
1632 :
1633 : static tree
1634 86763389 : get_fundef_copy (constexpr_fundef *fundef)
1635 : {
1636 86763389 : tree copy;
1637 86763389 : bool existed;
1638 86763389 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1639 86763389 : (fundef_copies_table, fundef->decl, &existed, 127));
1640 :
1641 86763389 : if (!existed)
1642 : {
1643 : /* There is no cached function available, or in use. We can use
1644 : the function directly. That the slot is now created records
1645 : that this function is now in use. */
1646 7554991 : copy = build_tree_list (fundef->body, fundef->parms);
1647 7554991 : TREE_TYPE (copy) = fundef->result;
1648 : }
1649 79208398 : else if (*slot == NULL_TREE)
1650 : {
1651 4443 : if (uid_sensitive_constexpr_evaluation_p ())
1652 9 : return NULL_TREE;
1653 :
1654 : /* We've already used the function itself, so make a copy. */
1655 4434 : copy = build_tree_list (NULL, NULL);
1656 4434 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1657 4434 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1658 4434 : tree saved_result = DECL_RESULT (fundef->decl);
1659 4434 : tree saved_fn = current_function_decl;
1660 4434 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1661 4434 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1662 4434 : DECL_RESULT (fundef->decl) = fundef->result;
1663 4434 : current_function_decl = fundef->decl;
1664 4434 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1665 4434 : TREE_TYPE (copy));
1666 4434 : current_function_decl = saved_fn;
1667 4434 : DECL_RESULT (fundef->decl) = saved_result;
1668 4434 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1669 4434 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1670 : }
1671 : else
1672 : {
1673 : /* We have a cached function available. */
1674 79203955 : copy = *slot;
1675 79203955 : *slot = TREE_CHAIN (copy);
1676 : }
1677 :
1678 : return copy;
1679 : }
1680 :
1681 : /* Save the copy COPY of function FUN for later reuse by
1682 : get_fundef_copy(). By construction, there will always be an entry
1683 : to find. */
1684 :
1685 : static void
1686 86763376 : save_fundef_copy (tree fun, tree copy)
1687 : {
1688 86763376 : tree *slot = fundef_copies_table->get (fun);
1689 86763376 : TREE_CHAIN (copy) = *slot;
1690 86763376 : *slot = copy;
1691 86763376 : }
1692 :
1693 : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1694 : value_cat, bool *, bool *, tree *);
1695 : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1696 : bool *, tree *);
1697 : static tree find_heap_var_refs (tree *, int *, void *);
1698 :
1699 : /* For exception object EXC if it has class type and usable what () method
1700 : which returns cv char * return the xmalloced string literal which it returns
1701 : if possible, otherwise return NULL. */
1702 :
1703 : static char *
1704 418 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1705 : {
1706 418 : tree type = strip_array_types (TREE_TYPE (exc));
1707 418 : if (!CLASS_TYPE_P (type))
1708 : return NULL;
1709 362 : tree std_exception = lookup_qualified_name (std_node, "exception",
1710 : LOOK_want::NORMAL, false);
1711 362 : if (TREE_CODE (std_exception) != TYPE_DECL)
1712 : return NULL;
1713 356 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1714 : return NULL;
1715 356 : base_kind b_kind;
1716 356 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1717 : tf_none);
1718 356 : if (binfo == NULL_TREE || binfo == error_mark_node)
1719 : return NULL;
1720 348 : if (type != TREE_TYPE (exc))
1721 310 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1722 348 : tree call
1723 348 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1724 : tf_none);
1725 348 : if (call == error_mark_node)
1726 : return NULL;
1727 348 : releasing_vec what_args;
1728 348 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1729 348 : if (call == error_mark_node)
1730 : return NULL;
1731 348 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1732 348 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1733 348 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1734 348 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1735 348 : TYPE_SIZE_UNIT (char_type_node))
1736 696 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1737 : return NULL;
1738 348 : if (!potential_constant_expression (call))
1739 : return NULL;
1740 348 : bool non_constant_p = false, overflow_p = false;
1741 348 : tree jmp_target = NULL;
1742 348 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1743 : &non_constant_p, &overflow_p,
1744 : &jmp_target);
1745 696 : if (throws (&jmp_target) || non_constant_p)
1746 : return NULL;
1747 348 : if (reduced_constant_expression_p (ptr))
1748 42 : if (const char *msg = c_getstr (ptr))
1749 42 : return xstrdup (msg);
1750 306 : auto_vec <char, 32> v;
1751 12318 : for (unsigned i = 0; i < INT_MAX; ++i)
1752 : {
1753 12318 : tree t = call;
1754 12318 : if (i)
1755 12012 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1756 12318 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1757 12318 : non_constant_p = false;
1758 12318 : overflow_p = false;
1759 12318 : jmp_target = NULL;
1760 12318 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1761 : &non_constant_p, &overflow_p,
1762 : &jmp_target);
1763 12318 : if (throws (&jmp_target)
1764 12318 : || non_constant_p
1765 12318 : || !tree_fits_shwi_p (t2))
1766 0 : return NULL;
1767 12318 : char c = tree_to_shwi (t2);
1768 12318 : v.safe_push (c);
1769 12318 : if (c == '\0')
1770 : break;
1771 : }
1772 612 : return xstrdup (v.address ());
1773 654 : }
1774 :
1775 : /* Diagnose constant expression evaluation encountering call to
1776 : std::terminate due to exception EXC. */
1777 :
1778 : static void
1779 22 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1780 : {
1781 22 : tree type = strip_array_types (TREE_TYPE (exc));
1782 22 : if (char *str = exception_what_str (ctx, exc))
1783 : {
1784 4 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1785 : "%<what()%>: %qs", "std::terminate", type, str);
1786 4 : free (str);
1787 : }
1788 : else
1789 : {
1790 18 : if (type != TREE_TYPE (exc))
1791 18 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1792 18 : bool non_constant_p = false, overflow_p = false;
1793 18 : tree jmp_target = NULL;
1794 18 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1795 : &non_constant_p, &overflow_p,
1796 : &jmp_target);
1797 18 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1798 18 : if (reduced_constant_expression_p (val))
1799 18 : error_at (loc, "%qs called after throwing an exception %qE",
1800 : "std::terminate", val);
1801 : else
1802 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1803 : "std::terminate", type);
1804 : }
1805 22 : }
1806 :
1807 : /* Diagnose constant expression evaluation encountering call to
1808 : uncaught exception EXC. */
1809 :
1810 : static void
1811 396 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1812 : {
1813 396 : tree type = strip_array_types (TREE_TYPE (exc));
1814 396 : if (char *str = exception_what_str (ctx, exc))
1815 : {
1816 344 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1817 344 : free (str);
1818 : }
1819 : else
1820 : {
1821 52 : if (type != TREE_TYPE (exc))
1822 52 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1823 52 : bool non_constant_p = false, overflow_p = false;
1824 52 : tree jmp_target = NULL;
1825 52 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1826 : &non_constant_p, &overflow_p,
1827 : &jmp_target);
1828 52 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1829 52 : if (reduced_constant_expression_p (val))
1830 50 : error_at (loc, "uncaught exception %qE", val);
1831 : else
1832 2 : error_at (loc, "uncaught exception of type %qT", type);
1833 : }
1834 396 : }
1835 :
1836 : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1837 : magic constexpr functions for C++26. */
1838 :
1839 : enum cxa_builtin {
1840 : CXA_NONE = 0,
1841 : CXA_ALLOCATE_EXCEPTION = 1,
1842 : CXA_FREE_EXCEPTION = 2,
1843 : CXA_THROW = 3,
1844 : CXA_BEGIN_CATCH = 4,
1845 : CXA_END_CATCH = 5,
1846 : CXA_RETHROW = 6,
1847 : CXA_GET_EXCEPTION_PTR = 7,
1848 : CXA_BAD_CAST = 8,
1849 : CXA_BAD_TYPEID = 9,
1850 : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1851 : STD_RETHROW_EXCEPTION = 11,
1852 : BUILTIN_EH_PTR_ADJUST_REF = 12,
1853 : BUILTIN_UNCAUGHT_EXCEPTIONS = 13,
1854 : BUILTIN_CURRENT_EXCEPTION = 14
1855 : };
1856 :
1857 : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1858 : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1859 :
1860 : static enum cxa_builtin
1861 36546481 : cxx_cxa_builtin_fn_p (tree fndecl)
1862 : {
1863 36546481 : if (cxx_dialect < cxx26)
1864 : return CXA_NONE;
1865 3718801 : if (DECL_LANGUAGE (fndecl) != lang_c)
1866 : {
1867 3099049 : if (!decl_in_std_namespace_p (fndecl))
1868 : return CXA_NONE;
1869 1308131 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1870 : return STD_RETHROW_EXCEPTION;
1871 1308089 : return CXA_NONE;
1872 : }
1873 619752 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1874 : return CXA_NONE;
1875 85188 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1876 : return CXA_ALLOCATE_EXCEPTION;
1877 28256 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1878 : return CXA_FREE_EXCEPTION;
1879 28254 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1880 : return CXA_THROW;
1881 18610 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1882 : return CXA_BEGIN_CATCH;
1883 4851 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1884 : return CXA_END_CATCH;
1885 1092 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1886 : return CXA_RETHROW;
1887 986 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1888 : return CXA_GET_EXCEPTION_PTR;
1889 942 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1890 : return CXA_BAD_CAST;
1891 646 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1892 : return CXA_BAD_TYPEID;
1893 633 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1894 33 : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1895 : return CXA_NONE;
1896 : }
1897 :
1898 : /* Helper function for cxx_eval_cxa_builtin_fn.
1899 : Check if ARG is a valid first argument of __cxa_throw or
1900 : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1901 : not, otherwise return the artificial __cxa_allocate_exception allocated
1902 : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1903 :
1904 : static tree
1905 2116 : cxa_check_throw_arg (tree arg, bool free_exc)
1906 : {
1907 2116 : STRIP_NOPS (arg);
1908 2116 : if (TREE_CODE (arg) != ADDR_EXPR)
1909 : return NULL_TREE;
1910 2116 : arg = TREE_OPERAND (arg, 0);
1911 2116 : if (!VAR_P (arg)
1912 2116 : || !DECL_ARTIFICIAL (arg)
1913 2116 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1914 2116 : && DECL_NAME (arg) != heap_identifier)
1915 4232 : || !DECL_LANG_SPECIFIC (arg))
1916 0 : return NULL_TREE;
1917 : return arg;
1918 : }
1919 :
1920 : /* Helper function for cxx_eval_cxa_builtin_fn.
1921 : "Allocate" on the constexpr heap an exception object of TYPE
1922 : with REFCOUNT. */
1923 :
1924 : static tree
1925 18360 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1926 : tree refcount)
1927 : {
1928 18360 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1929 18360 : DECL_ARTIFICIAL (var) = 1;
1930 18360 : retrofit_lang_decl (var);
1931 18360 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1932 18360 : ctx->global->heap_vars.safe_push (var);
1933 18360 : return var;
1934 : }
1935 :
1936 : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1937 : C++26 constexpr exception support (P3068R5). */
1938 :
1939 : static tree
1940 32236 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1941 : enum cxa_builtin kind, tree fndecl,
1942 : bool *non_constant_p, bool *overflow_p,
1943 : tree *jump_target)
1944 : {
1945 32236 : int nargs = call_expr_nargs (call);
1946 32236 : location_t loc = cp_expr_loc_or_input_loc (call);
1947 32236 : tree args[4], arg;
1948 32236 : if (nargs > 4)
1949 : {
1950 0 : invalid_nargs:
1951 0 : if (!ctx->quiet)
1952 0 : error_at (loc, "call to %qD function with incorrect "
1953 : "number of arguments", fndecl);
1954 0 : *non_constant_p = true;
1955 0 : return call;
1956 : }
1957 32236 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1958 8797 : && nargs == 1
1959 8797 : && (arg = CALL_EXPR_ARG (call, 0))
1960 8797 : && TREE_CODE (arg) == CALL_EXPR
1961 8797 : && call_expr_nargs (arg) == 1
1962 41033 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1963 8797 : if (tree fun = get_function_named_in_call (arg))
1964 8797 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1965 : {
1966 8797 : if (ctx->global->caught_exceptions.length () < 2)
1967 : {
1968 5006 : no_caught_exceptions:
1969 5006 : if (!ctx->quiet)
1970 0 : error_at (loc, "%qD called with no caught exceptions pending",
1971 : fndecl);
1972 5006 : *non_constant_p = true;
1973 5006 : return call;
1974 : }
1975 : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1976 : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1977 : ctx->global->caught_exceptions vector to end with
1978 : __cxa_allocate_exception created artificial VAR_DECL (the
1979 : exception object) followed by handler type, pushed by TRY_BLOCK
1980 : evaluation. The only difference between the functions is that
1981 : __cxa_begin_catch pops the handler type from the vector and keeps
1982 : the VAR_DECL last and decreases uncaught_exceptions. The
1983 : VAR_DECL after __cxa_begin_catch serves as the current exception
1984 : and is then popped in __cxa_end_catch evaluation. */
1985 3791 : tree handler_type = ctx->global->caught_exceptions.last ();
1986 3791 : if (handler_type && (VAR_P (handler_type)
1987 3709 : || TREE_CODE (handler_type) == TREE_LIST))
1988 0 : goto no_caught_exceptions;
1989 3791 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1990 3791 : arg = ctx->global->caught_exceptions[idx];
1991 3791 : if (TREE_CODE (arg) == TREE_LIST)
1992 0 : arg = TREE_VALUE (arg);
1993 3791 : gcc_assert (VAR_P (arg));
1994 3791 : if (kind == CXA_BEGIN_CATCH)
1995 : {
1996 3763 : ctx->global->caught_exceptions.pop ();
1997 3763 : --ctx->global->uncaught_exceptions;
1998 : }
1999 3791 : if (handler_type == NULL_TREE)
2000 : /* Used for catch (...). Just return void. */
2001 82 : return void_node;
2002 3709 : else if (POINTER_TYPE_P (handler_type))
2003 : {
2004 : /* Used for catch of a pointer. */
2005 66 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
2006 66 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
2007 : size_zero_node, NULL_TREE, NULL_TREE);
2008 66 : arg = cp_convert (handler_type, arg,
2009 66 : ctx->quiet ? tf_none : tf_warning_or_error);
2010 66 : if (arg == error_mark_node)
2011 : {
2012 0 : *non_constant_p = true;
2013 0 : return call;
2014 : }
2015 : }
2016 : else
2017 : {
2018 : /* Used for catch of a non-pointer type. */
2019 3643 : tree exc_type = strip_array_types (TREE_TYPE (arg));
2020 3623 : if (TYPE_PTRMEM_P (handler_type)
2021 3689 : && !same_type_ignoring_top_level_qualifiers_p
2022 46 : (handler_type, exc_type))
2023 : {
2024 46 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
2025 46 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
2026 : size_zero_node, NULL_TREE, NULL_TREE);
2027 46 : arg = cp_convert (handler_type, arg,
2028 46 : ctx->quiet ? tf_none
2029 : : tf_warning_or_error);
2030 46 : if (arg == error_mark_node)
2031 : {
2032 0 : *non_constant_p = true;
2033 0 : return call;
2034 : }
2035 46 : tree var = build_decl (loc, VAR_DECL, heap_identifier,
2036 46 : handler_type);
2037 46 : DECL_ARTIFICIAL (var) = 1;
2038 46 : ctx->global->heap_vars.safe_push (var);
2039 46 : ctx->global->put_value (var, NULL_TREE);
2040 46 : tree init = build2_loc (loc, INIT_EXPR, handler_type,
2041 : var, arg);
2042 46 : arg = ctx->global->caught_exceptions[idx];
2043 46 : if (TREE_CODE (arg) == TREE_LIST)
2044 : {
2045 0 : DECL_CHAIN (var) = TREE_PURPOSE (arg);
2046 0 : TREE_PURPOSE (arg) = var;
2047 : }
2048 : else
2049 46 : ctx->global->caught_exceptions[idx]
2050 92 : = build_tree_list (var, arg);
2051 46 : arg = cp_build_addr_expr (var, tf_none);
2052 46 : arg = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (arg),
2053 : init, arg);
2054 : }
2055 : else
2056 : {
2057 3597 : tree exc_ptr_type = build_pointer_type (exc_type);
2058 3597 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
2059 3597 : if (CLASS_TYPE_P (handler_type))
2060 : {
2061 3507 : tree ptr_type = build_pointer_type (handler_type);
2062 3507 : arg = cp_convert (ptr_type, arg,
2063 3507 : ctx->quiet ? tf_none
2064 : : tf_warning_or_error);
2065 3507 : if (arg == error_mark_node)
2066 : {
2067 0 : *non_constant_p = true;
2068 0 : return call;
2069 : }
2070 : }
2071 : }
2072 : }
2073 3709 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2074 : non_constant_p, overflow_p,
2075 3709 : jump_target);
2076 : }
2077 44560 : for (int i = 0; i < nargs; ++i)
2078 : {
2079 21121 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
2080 : vc_prvalue, non_constant_p,
2081 : overflow_p, jump_target);
2082 21121 : if (*non_constant_p)
2083 : return call;
2084 21121 : if (*jump_target)
2085 : return NULL_TREE;
2086 : }
2087 23439 : switch (kind)
2088 : {
2089 14983 : case CXA_ALLOCATE_EXCEPTION:
2090 14983 : if (nargs != 1)
2091 0 : goto invalid_nargs;
2092 14983 : if (!tree_fits_uhwi_p (args[0]))
2093 : {
2094 0 : if (!ctx->quiet)
2095 0 : error_at (loc, "cannot allocate exception: size not constant");
2096 0 : *non_constant_p = true;
2097 0 : return call;
2098 : }
2099 : else
2100 : {
2101 29966 : tree type = build_array_type_nelts (char_type_node,
2102 14983 : tree_to_uhwi (args[0]));
2103 14983 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2104 14983 : ctx->global->put_value (var, NULL_TREE);
2105 14983 : return fold_convert (ptr_type_node, build_address (var));
2106 : }
2107 2 : case CXA_FREE_EXCEPTION:
2108 2 : if (nargs != 1)
2109 0 : goto invalid_nargs;
2110 2 : arg = cxa_check_throw_arg (args[0], true);
2111 2 : if (arg == NULL_TREE)
2112 : {
2113 0 : invalid_ptr:
2114 0 : if (!ctx->quiet)
2115 0 : error_at (loc, "first argument to %qD function not result of "
2116 : "%<__cxa_allocate_exception%>", fndecl);
2117 0 : *non_constant_p = true;
2118 0 : return call;
2119 : }
2120 2 : DECL_NAME (arg) = heap_deleted_identifier;
2121 2 : ctx->global->destroy_value (arg);
2122 2 : ctx->global->heap_dealloc_count++;
2123 2 : return void_node;
2124 1948 : case CXA_THROW:
2125 1948 : if (nargs != 3)
2126 0 : goto invalid_nargs;
2127 1948 : arg = cxa_check_throw_arg (args[0], false);
2128 1948 : if (arg == NULL_TREE)
2129 0 : goto invalid_ptr;
2130 1948 : DECL_EXCEPTION_REFCOUNT (arg)
2131 1948 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2132 : size_one_node);
2133 1948 : ++ctx->global->uncaught_exceptions;
2134 1948 : *jump_target = arg;
2135 1948 : return void_node;
2136 0 : case CXA_BEGIN_CATCH:
2137 0 : case CXA_GET_EXCEPTION_PTR:
2138 0 : goto invalid_nargs;
2139 3759 : case CXA_END_CATCH:
2140 3759 : if (nargs != 0)
2141 0 : goto invalid_nargs;
2142 3759 : if (ctx->global->caught_exceptions.is_empty ())
2143 : {
2144 0 : no_active_exc:
2145 0 : if (!ctx->quiet)
2146 0 : error_at (loc, "%qD called with no caught exceptions active",
2147 : fndecl);
2148 0 : *non_constant_p = true;
2149 0 : return call;
2150 : }
2151 : else
2152 : {
2153 3759 : arg = ctx->global->caught_exceptions.pop ();
2154 3759 : if (arg == NULL_TREE
2155 3759 : || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2156 0 : goto no_active_exc;
2157 3759 : if (TREE_CODE (arg) == TREE_LIST)
2158 : {
2159 92 : for (tree aux = TREE_PURPOSE (arg); aux; aux = DECL_CHAIN (aux))
2160 : {
2161 46 : DECL_NAME (aux) = heap_deleted_identifier;
2162 46 : ctx->global->destroy_value (aux);
2163 46 : ctx->global->heap_dealloc_count++;
2164 : }
2165 46 : arg = TREE_VALUE (arg);
2166 : }
2167 3759 : DECL_CHAIN (arg) = NULL_TREE;
2168 3837 : free_except:
2169 3837 : DECL_EXCEPTION_REFCOUNT (arg)
2170 3837 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2171 : size_one_node);
2172 3837 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2173 : {
2174 3627 : if (type_build_dtor_call (TREE_TYPE (arg)))
2175 : {
2176 : /* So that we don't complain about out-of-consteval use. */
2177 3409 : temp_override<tree> ovr (current_function_decl);
2178 3409 : if (ctx->call && ctx->call->fundef)
2179 3409 : current_function_decl = ctx->call->fundef->decl;
2180 3409 : tree cleanup
2181 3435 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2182 : : tf_warning_or_error));
2183 3409 : if (cleanup == error_mark_node)
2184 0 : *non_constant_p = true;
2185 3409 : tree jmp_target = NULL_TREE;
2186 3409 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2187 : non_constant_p, overflow_p,
2188 : &jmp_target);
2189 3409 : if (throws (&jmp_target))
2190 0 : *jump_target = jmp_target;
2191 3409 : }
2192 3627 : DECL_NAME (arg) = heap_deleted_identifier;
2193 3627 : ctx->global->destroy_value (arg);
2194 3627 : ctx->global->heap_dealloc_count++;
2195 : }
2196 : }
2197 3837 : return void_node;
2198 98 : case CXA_RETHROW:
2199 98 : if (nargs != 0)
2200 0 : goto invalid_nargs;
2201 98 : unsigned idx;
2202 196 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2203 82 : if (arg == NULL_TREE || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2204 0 : --idx;
2205 : else
2206 : break;
2207 98 : if (arg == NULL_TREE)
2208 : {
2209 16 : if (!ctx->quiet)
2210 4 : error_at (loc, "%qD called with no caught exceptions active",
2211 : fndecl);
2212 16 : *non_constant_p = true;
2213 16 : return call;
2214 : }
2215 82 : if (TREE_CODE (arg) == TREE_LIST)
2216 0 : arg = TREE_VALUE (arg);
2217 82 : DECL_EXCEPTION_REFCOUNT (arg)
2218 82 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2219 82 : ++ctx->global->uncaught_exceptions;
2220 : /* Don't cache calls which rethrow, they depend on the current
2221 : exception which might be caught in the caller. */
2222 82 : ctx->global->state_dependent = true;
2223 82 : *jump_target = arg;
2224 82 : return void_node;
2225 163 : case CXA_BAD_CAST:
2226 163 : case CXA_BAD_TYPEID:
2227 163 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2228 163 : if (nargs != 0)
2229 0 : goto invalid_nargs;
2230 : else
2231 : {
2232 163 : tree name;
2233 163 : switch (kind)
2234 : {
2235 123 : case CXA_BAD_CAST:
2236 123 : name = get_identifier ("bad_cast");
2237 123 : break;
2238 7 : case CXA_BAD_TYPEID:
2239 7 : name = get_identifier ("bad_typeid");
2240 7 : break;
2241 33 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2242 33 : name = get_identifier ("bad_array_new_length");
2243 33 : break;
2244 : default:
2245 : gcc_unreachable ();
2246 : }
2247 163 : tree decl = lookup_qualified_name (std_node, name);
2248 163 : if (TREE_CODE (decl) != TYPE_DECL
2249 135 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2250 298 : || !type_build_ctor_call (TREE_TYPE (decl)))
2251 : {
2252 28 : if (!ctx->quiet)
2253 8 : error_at (loc, "%qD called without %<std::%D%> being defined",
2254 : fndecl, name);
2255 28 : *non_constant_p = true;
2256 28 : return call;
2257 : }
2258 135 : tree type = TREE_TYPE (decl);
2259 135 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2260 135 : tree ctor
2261 135 : = build_special_member_call (var, complete_ctor_identifier,
2262 : NULL, type, LOOKUP_NORMAL,
2263 135 : ctx->quiet ? tf_none
2264 : : tf_warning_or_error);
2265 135 : if (ctor == error_mark_node)
2266 : {
2267 0 : *non_constant_p = true;
2268 0 : return call;
2269 : }
2270 135 : if (TREE_CONSTANT (ctor))
2271 0 : ctx->global->put_value (var, ctor);
2272 : else
2273 : {
2274 135 : ctx->global->put_value (var, NULL_TREE);
2275 135 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2276 : non_constant_p, overflow_p,
2277 : jump_target);
2278 135 : if (*non_constant_p)
2279 : return call;
2280 135 : if (throws (jump_target))
2281 : return NULL_TREE;
2282 : }
2283 135 : ++ctx->global->uncaught_exceptions;
2284 135 : *jump_target = var;
2285 : }
2286 135 : return void_node;
2287 82 : case BUILTIN_UNCAUGHT_EXCEPTIONS:
2288 82 : if (nargs != 0)
2289 0 : goto invalid_nargs;
2290 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2291 : want to give a definite answer during mce_unknown evaluation,
2292 : because that might prevent evaluation later on when some
2293 : exceptions might be uncaught. But unlike that, we don't
2294 : want to constant fold it even during cp_fold, because at runtime
2295 : std::uncaught_exceptions () might still be non-zero. */
2296 82 : if (ctx->manifestly_const_eval != mce_true)
2297 : {
2298 46 : *non_constant_p = true;
2299 46 : return call;
2300 : }
2301 : /* Don't cache calls which call __builtin_uncaught_exceptions (),
2302 : they depend on the current uncaught exceptions which might
2303 : be the state from their caller. */
2304 36 : ctx->global->state_dependent = true;
2305 36 : return build_int_cst (integer_type_node,
2306 36 : ctx->global->uncaught_exceptions);
2307 2238 : case BUILTIN_CURRENT_EXCEPTION:
2308 2238 : if (nargs != 0)
2309 0 : goto invalid_nargs;
2310 : else
2311 : {
2312 2238 : tree name = get_identifier ("exception_ptr");
2313 2238 : tree decl = lookup_qualified_name (std_node, name);
2314 2238 : tree fld;
2315 2238 : if (TREE_CODE (decl) != TYPE_DECL
2316 2238 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2317 2238 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2318 2238 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2319 2238 : || DECL_ARTIFICIAL (fld)
2320 2238 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2321 2238 : || next_aggregate_field (DECL_CHAIN (fld))
2322 4476 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2323 2238 : TYPE_SIZE (TREE_TYPE (fld))))
2324 : {
2325 0 : if (!ctx->quiet)
2326 0 : error_at (loc, "%qD called without supportable %qs",
2327 : fndecl, "std::exception_ptr");
2328 0 : *non_constant_p = true;
2329 0 : return call;
2330 : }
2331 4476 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2332 42 : if (arg == NULL_TREE
2333 42 : || (!VAR_P (arg) && TREE_CODE (arg) != TREE_LIST))
2334 0 : --idx;
2335 : else
2336 : break;
2337 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2338 : want to give a definite answer during mce_unknown evaluation,
2339 : because that might prevent evaluation later on when some
2340 : exceptions might be current. But unlike that, we don't
2341 : want to constant fold it to null even during cp_fold, because
2342 : at runtime std::current_exception () might still be non-null. */
2343 2238 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2344 : {
2345 2184 : *non_constant_p = true;
2346 2184 : return call;
2347 : }
2348 54 : if (arg == NULL_TREE)
2349 12 : arg = build_zero_cst (TREE_TYPE (fld));
2350 : else
2351 : {
2352 42 : if (TREE_CODE (arg) == TREE_LIST)
2353 0 : arg = TREE_VALUE (arg);
2354 42 : DECL_EXCEPTION_REFCOUNT (arg)
2355 42 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2356 : size_one_node);
2357 42 : arg = fold_convert (ptr_type_node, build_address (arg));
2358 : }
2359 : /* Don't cache calls which call __builtin_current_exception (),
2360 : they depend on the current exception which might be caught
2361 : in the caller. */
2362 54 : ctx->global->state_dependent = true;
2363 54 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2364 : }
2365 40 : case STD_RETHROW_EXCEPTION:
2366 40 : if (nargs != 1)
2367 0 : goto invalid_nargs;
2368 40 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2369 : {
2370 40 : arg = args[0];
2371 40 : STRIP_NOPS (arg);
2372 40 : if (TREE_CODE (arg) == ADDR_EXPR)
2373 : {
2374 40 : args[0]
2375 40 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2376 : vc_prvalue, non_constant_p,
2377 : overflow_p, jump_target);
2378 40 : if (*non_constant_p)
2379 : return call;
2380 40 : if (*jump_target)
2381 : return NULL_TREE;
2382 : }
2383 : }
2384 40 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2385 40 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2386 : {
2387 0 : invalid_std_rethrow:
2388 0 : if (!ctx->quiet)
2389 0 : error_at (loc, "%qD called with unexpected %qs argument",
2390 : fndecl, "std::exception_ptr");
2391 0 : *non_constant_p = true;
2392 0 : return void_node;
2393 : }
2394 40 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2395 40 : if (arg == NULL_TREE)
2396 0 : goto invalid_std_rethrow;
2397 40 : DECL_EXCEPTION_REFCOUNT (arg)
2398 40 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2399 40 : ++ctx->global->uncaught_exceptions;
2400 40 : *jump_target = arg;
2401 40 : return void_node;
2402 126 : case BUILTIN_EH_PTR_ADJUST_REF:
2403 126 : if (nargs != 2)
2404 0 : goto invalid_nargs;
2405 126 : arg = cxa_check_throw_arg (args[0], false);
2406 126 : if (arg == NULL_TREE)
2407 0 : goto invalid_ptr;
2408 126 : if (integer_onep (args[1]))
2409 48 : DECL_EXCEPTION_REFCOUNT (arg)
2410 96 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2411 : size_one_node);
2412 78 : else if (integer_minus_onep (args[1]))
2413 78 : goto free_except;
2414 : else
2415 : {
2416 0 : if (!ctx->quiet)
2417 0 : error_at (loc, "%qD called with second argument "
2418 : "other than 1 or -1", fndecl);
2419 0 : *non_constant_p = true;
2420 : }
2421 48 : return void_node;
2422 0 : default:
2423 0 : gcc_unreachable ();
2424 : }
2425 : }
2426 :
2427 : /* Variables and functions to manage constexpr call expansion context.
2428 : These do not need to be marked for PCH or GC. */
2429 :
2430 : /* FIXME remember and print actual constant arguments. */
2431 : static vec<tree> call_stack;
2432 : static int call_stack_tick;
2433 : static int last_cx_error_tick;
2434 :
2435 : /* Attempt to evaluate T which represents a call to __builtin_constexpr_diag.
2436 : The arguments should be an integer (0 for inform, 1 for warning, 2 for
2437 : error) optionally with 16 ored in if it should use caller's caller location
2438 : instead of caller's location and 2 messages which are either a pointer to
2439 : a STRING_CST or class with data () and size () member functions like
2440 : string_view or u8string_view. The first message is a tag, with "" passed
2441 : for no tag, data () should return const char *, the tag should only contain
2442 : alphanumeric letters or underscores. The second message is the diagnostic
2443 : message, data () can be either const char * or const char8_t *. size ()
2444 : should return the corresponding length of the strings in bytes as an
2445 : integer. */
2446 :
2447 : static tree
2448 210 : cxx_eval_constexpr_diag (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
2449 : bool *overflow_p, tree *jump_target)
2450 : {
2451 210 : location_t loc = EXPR_LOCATION (t);
2452 210 : if (call_expr_nargs (t) != 3)
2453 : {
2454 12 : if (!ctx->quiet)
2455 12 : error_at (loc, "wrong number of arguments to %qs call",
2456 : "__builtin_constexpr_diag");
2457 12 : *non_constant_p = true;
2458 12 : return t;
2459 : }
2460 : tree args[3];
2461 792 : for (int i = 0; i < 3; ++i)
2462 : {
2463 594 : tree arg = convert_from_reference (CALL_EXPR_ARG (t, i));
2464 594 : arg = cxx_eval_constant_expression (ctx, arg,
2465 : (i == 0
2466 534 : || POINTER_TYPE_P (TREE_TYPE (arg)))
2467 990 : ? vc_prvalue : vc_glvalue,
2468 : non_constant_p, overflow_p,
2469 : jump_target);
2470 594 : if (*jump_target)
2471 : return NULL_TREE;
2472 594 : if (*non_constant_p)
2473 : return t;
2474 594 : args[i] = arg;
2475 : }
2476 396 : if (TREE_CODE (args[0]) != INTEGER_CST
2477 198 : || wi::to_widest (args[0]) < 0
2478 194 : || wi::to_widest (args[0]) > 18
2479 396 : || (wi::to_widest (args[0]) & 15) > 2)
2480 : {
2481 8 : if (!ctx->quiet)
2482 8 : error_at (loc, "first %qs call argument should be 0, 1, 2, 16, 17 or "
2483 : "18", "__builtin_constexpr_diag");
2484 8 : *non_constant_p = true;
2485 8 : return t;
2486 : }
2487 190 : const char *msgs[2] = {};
2488 190 : int lens[3] = {};
2489 950 : cexpr_str cstrs[2];
2490 478 : diagnostics::kind kind = diagnostics::kind::error;
2491 478 : for (int i = 1; i < 3; ++i)
2492 : {
2493 344 : tree arg = args[i];
2494 344 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2495 : {
2496 206 : tree str = arg;
2497 206 : STRIP_NOPS (str);
2498 206 : if (TREE_CODE (str) == ADDR_EXPR
2499 206 : && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST)
2500 : {
2501 206 : str = TREE_OPERAND (str, 0);
2502 206 : tree eltype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)));
2503 206 : if (eltype == char_type_node
2504 24 : || (i == 2 && eltype == char8_type_node))
2505 344 : arg = str;
2506 : }
2507 : }
2508 344 : cstrs[i - 1].message = arg;
2509 344 : if (!cstrs[i - 1].type_check (loc, i == 2))
2510 : {
2511 40 : *non_constant_p = true;
2512 40 : return t;
2513 : }
2514 304 : if (!cstrs[i - 1].extract (loc, msgs[i - 1], lens[i - 1], ctx,
2515 : non_constant_p, overflow_p, jump_target))
2516 : {
2517 16 : if (*jump_target)
2518 : return NULL_TREE;
2519 16 : *non_constant_p = true;
2520 16 : return t;
2521 : }
2522 : }
2523 134 : if (msgs[0])
2524 : {
2525 726 : for (int i = 0; i < lens[0]; ++i)
2526 596 : if (!ISALNUM (msgs[0][i]) && msgs[0][i] != '_')
2527 : {
2528 4 : if (!ctx->quiet)
2529 4 : error_at (loc, "%qs tag string contains %qc character other than"
2530 : " letters, digits or %<_%>",
2531 : "__builtin_constexpr_diag", msgs[0][i]);
2532 4 : *non_constant_p = true;
2533 4 : return t;
2534 : }
2535 : }
2536 130 : if (ctx->manifestly_const_eval == mce_unknown)
2537 : {
2538 0 : *non_constant_p = true;
2539 0 : return t;
2540 : }
2541 130 : int arg0 = tree_to_uhwi (args[0]);
2542 130 : if (arg0 & 16)
2543 : {
2544 32 : arg0 &= 15;
2545 32 : if (!call_stack.is_empty ())
2546 : {
2547 32 : tree call = call_stack.last ();
2548 32 : if (EXPR_HAS_LOCATION (call))
2549 32 : loc = EXPR_LOCATION (call);
2550 : }
2551 : }
2552 130 : if (arg0 == 0)
2553 : kind = diagnostics::kind::note;
2554 82 : else if (arg0 == 1)
2555 36 : kind = diagnostics::kind::warning;
2556 130 : if (lens[0])
2557 : {
2558 88 : const char *color = "error";
2559 88 : if (kind == diagnostics::kind::note)
2560 : color = "note";
2561 60 : else if (kind == diagnostics::kind::warning)
2562 32 : color = "warning";
2563 88 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s [%r%.*s%R]",
2564 : lens[1], msgs[1], color, lens[0], msgs[0]);
2565 : }
2566 : else
2567 42 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s",
2568 : lens[1], msgs[1]);
2569 130 : return void_node;
2570 570 : }
2571 :
2572 : static tree eval_and_check_array_index (const constexpr_ctx *, tree, bool,
2573 : bool *, bool *, tree *);
2574 :
2575 : /* Helper function for cxx_eval_is_within_lifetime and
2576 : cxx_eval_start_lifetime. Check if ARG is within lifetime. */
2577 :
2578 : static tree
2579 5158 : is_within_lifetime (location_t loc, const constexpr_ctx *ctx, tree t, tree fun,
2580 : tree arg, bool *non_constant_p, bool *overflow_p,
2581 : tree *jump_target)
2582 : {
2583 5158 : auto_vec <tree, 4> refs;
2584 5158 : tree obj;
2585 12273 : for (obj = arg; ; obj = TREE_OPERAND (obj, 0))
2586 : {
2587 12273 : switch (TREE_CODE (obj))
2588 : {
2589 7115 : case COMPONENT_REF:
2590 7115 : case ARRAY_REF:
2591 7115 : case REALPART_EXPR:
2592 7115 : case IMAGPART_EXPR:
2593 7115 : refs.safe_push (obj);
2594 7115 : continue;
2595 5158 : default:
2596 5158 : break;
2597 : }
2598 5158 : break;
2599 : }
2600 5158 : tree *valp = nullptr;
2601 5158 : bool check_mutable = false;
2602 5158 : if (DECL_P (obj))
2603 : {
2604 5116 : valp = ctx->global->get_raw_value_ptr (obj);
2605 5116 : if (!valp
2606 1162 : && VAR_P (obj)
2607 1162 : && TREE_STATIC (obj)
2608 5538 : && decl_constant_var_p (obj))
2609 : {
2610 410 : valp = &DECL_INITIAL (obj);
2611 410 : check_mutable = true;
2612 : }
2613 : }
2614 5116 : if (!valp || *valp == void_list_node)
2615 : {
2616 980 : if (!ctx->quiet)
2617 : {
2618 57 : auto_diagnostic_group d;
2619 57 : if (DECL_P (obj) && DECL_NAME (obj) == heap_deleted_identifier)
2620 : {
2621 3 : error_at (loc, "%qE on allocated storage after deallocation "
2622 3 : "is not a constant expression", DECL_NAME (fun));
2623 3 : inform (DECL_SOURCE_LOCATION (obj), "allocated here");
2624 : }
2625 54 : else if (DECL_P (obj) && ctx->global->is_outside_lifetime (obj))
2626 : {
2627 45 : error_at (loc, "%qE on %qE after its storage has been released "
2628 45 : "is not a constant expression", DECL_NAME (fun), obj);
2629 45 : inform (DECL_SOURCE_LOCATION (obj), "declared here");
2630 : }
2631 : else
2632 9 : error_at (loc, "%qE on %qE from outside current evaluation "
2633 9 : "is not a constant expression", DECL_NAME (fun), obj);
2634 57 : }
2635 980 : *non_constant_p = true;
2636 980 : return t;
2637 : }
2638 4178 : tree val = *valp;
2639 4178 : if (val == void_node)
2640 15 : return boolean_false_node;
2641 4163 : unsigned i;
2642 4163 : tree ref;
2643 : /* Magic value used for val when inside the following loop when
2644 : inside of an omitted part of initializer due to it being
2645 : uninitialized. In this case return boolean_true_node unless
2646 : there is some union member access, in unitialized union
2647 : no member is within lifetime. */
2648 4163 : const tree val_uninit = global_namespace;
2649 : /* Magic value used for val when inside the following loop when
2650 : inside of an omitted part of initializer due to it being
2651 : zero initialized. In this case return boolean_true_node unless
2652 : there is some union member access except for the first member. */
2653 4163 : const tree val_zero_init = std_node;
2654 4163 : if (val == NULL_TREE)
2655 141 : val = val_uninit;
2656 12811 : FOR_EACH_VEC_ELT_REVERSE (refs, i, ref)
2657 5273 : switch (TREE_CODE (ref))
2658 : {
2659 18 : case REALPART_EXPR:
2660 18 : case IMAGPART_EXPR:
2661 18 : if (TREE_CODE (val) == COMPLEX_EXPR
2662 18 : && (TREE_OPERAND (val, TREE_CODE (ref) == IMAGPART_EXPR)
2663 12 : == void_node))
2664 6 : return boolean_false_node;
2665 12 : return boolean_true_node;
2666 4231 : case COMPONENT_REF:
2667 4231 : if (check_mutable && DECL_MUTABLE_P (TREE_OPERAND (ref, 1)))
2668 : {
2669 12 : if (!ctx->quiet)
2670 3 : error_at (loc, "%qE on %<mutable%> sub-object %qD",
2671 3 : DECL_NAME (fun), TREE_OPERAND (ref, 1));
2672 12 : *non_constant_p = true;
2673 12 : return t;
2674 : }
2675 4219 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == UNION_TYPE)
2676 : {
2677 1280 : tree union_type = TREE_TYPE (TREE_OPERAND (ref, 0));
2678 1280 : if (val == val_zero_init)
2679 : {
2680 48 : if (TREE_OPERAND (ref, 1)
2681 24 : != next_aggregate_field (TYPE_FIELDS (union_type)))
2682 15 : return boolean_false_node;
2683 9 : continue;
2684 : }
2685 1256 : else if (val == val_uninit)
2686 102 : return boolean_false_node;
2687 1154 : else if (val == error_mark_node)
2688 : return val;
2689 : else
2690 : {
2691 1154 : gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
2692 1154 : if (CONSTRUCTOR_NELTS (val) == 0)
2693 : {
2694 147 : if (CONSTRUCTOR_NO_CLEARING (val))
2695 135 : return boolean_false_node;
2696 12 : tree first
2697 12 : = next_aggregate_field (TYPE_FIELDS (union_type));
2698 12 : if (first != TREE_OPERAND (ref, 1))
2699 3 : return boolean_false_node;
2700 9 : val = val_zero_init;
2701 9 : continue;
2702 9 : }
2703 : else
2704 : {
2705 1007 : if (CONSTRUCTOR_ELT (val, 0)->index
2706 1007 : != TREE_OPERAND (ref, 1))
2707 287 : return boolean_false_node;
2708 720 : val = CONSTRUCTOR_ELT (val, 0)->value;
2709 720 : if (val == void_node)
2710 0 : return boolean_false_node;
2711 720 : continue;
2712 : }
2713 : }
2714 : }
2715 2939 : if (val == val_zero_init || val == val_uninit)
2716 80 : continue;
2717 2859 : if (val == error_mark_node)
2718 : return val;
2719 2847 : gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
2720 : unsigned int j;
2721 : tree field, value;
2722 4362 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), j, field, value)
2723 2708 : if (field == TREE_OPERAND (ref, 1))
2724 : {
2725 1193 : if (value == void_node)
2726 6 : return boolean_false_node;
2727 : val = value;
2728 : ref = NULL_TREE;
2729 : break;
2730 : }
2731 2841 : if (ref == NULL_TREE)
2732 1187 : continue;
2733 1654 : if (CONSTRUCTOR_NO_CLEARING (val))
2734 : {
2735 1537 : if (CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (val))
2736 75 : return boolean_false_node;
2737 : val = val_uninit;
2738 : }
2739 : else
2740 : val = val_zero_init;
2741 1579 : continue;
2742 1024 : case ARRAY_REF:
2743 1024 : field = eval_and_check_array_index (ctx, ref, false,
2744 : non_constant_p, overflow_p,
2745 : jump_target);
2746 1024 : if (*jump_target)
2747 : return NULL_TREE;
2748 1024 : if (*non_constant_p)
2749 : return t;
2750 1012 : if (val == val_zero_init || val == val_uninit)
2751 33 : continue;
2752 979 : if (val == error_mark_node)
2753 : return val;
2754 979 : if (TREE_CODE (val) == STRING_CST)
2755 0 : return boolean_true_node;
2756 979 : gcc_assert (TREE_CODE (val) == CONSTRUCTOR);
2757 979 : HOST_WIDE_INT idx;
2758 979 : idx = find_array_ctor_elt (val, field, false);
2759 979 : if (idx != -1)
2760 : {
2761 595 : val = CONSTRUCTOR_ELT (val, idx)->value;
2762 595 : if (val == void_node)
2763 72 : return boolean_false_node;
2764 523 : continue;
2765 : }
2766 384 : if (CONSTRUCTOR_NO_CLEARING (val))
2767 : {
2768 159 : if (CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (val))
2769 39 : return boolean_false_node;
2770 : val = val_uninit;
2771 : }
2772 : else
2773 : val = val_zero_init;
2774 345 : continue;
2775 0 : default:
2776 0 : gcc_unreachable ();
2777 1924 : }
2778 3375 : return boolean_true_node;
2779 5158 : }
2780 :
2781 : /* Attempt to evaluate T which represents a call to
2782 : __builtin_is_within_lifetime. */
2783 :
2784 : static tree
2785 3952 : cxx_eval_is_within_lifetime (const constexpr_ctx *ctx, tree t, tree fun,
2786 : bool *non_constant_p, bool *overflow_p,
2787 : tree *jump_target)
2788 : {
2789 3952 : location_t loc = EXPR_LOCATION (t);
2790 3952 : tree arg = CALL_EXPR_ARG (t, 0);
2791 3952 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2792 : non_constant_p, overflow_p,
2793 : jump_target);
2794 3952 : if (*jump_target)
2795 : return NULL_TREE;
2796 3952 : if (*non_constant_p)
2797 : return t;
2798 : /* Need to strip casts to pointers to void, but should preserve
2799 : pointer casts within the innermost pointer to void cast if
2800 : any, so that e.g. pointers to heap allocations are handled
2801 : correctly. */
2802 : tree p = arg;
2803 6290 : while (CONVERT_EXPR_P (p) || TREE_CODE (p) == NON_LVALUE_EXPR)
2804 : {
2805 2754 : if (!TYPE_PTR_P (TREE_TYPE (p)))
2806 : break;
2807 2754 : if (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (p))))
2808 174 : arg = TREE_OPERAND (p, 0);
2809 2754 : p = TREE_OPERAND (p, 0);
2810 : }
2811 3536 : if (integer_zerop (arg))
2812 : {
2813 24 : if (!ctx->quiet)
2814 6 : error_at (loc, "%qE called with a null pointer",
2815 6 : DECL_NAME (fun));
2816 24 : *non_constant_p = true;
2817 24 : return t;
2818 : }
2819 3512 : if (POINTER_TYPE_P (TREE_TYPE (arg))
2820 3512 : && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == FUNCTION_TYPE
2821 3491 : || TREE_CODE (TREE_TYPE (TREE_TYPE (arg))) == METHOD_TYPE))
2822 : {
2823 21 : if (!ctx->quiet)
2824 6 : error_at (loc, "%qE called with pointer to function",
2825 6 : DECL_NAME (fun));
2826 21 : *non_constant_p = true;
2827 21 : return t;
2828 : }
2829 3491 : arg = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (arg)), arg);
2830 3491 : arg = cxx_eval_constant_expression (ctx, arg, vc_glvalue,
2831 : non_constant_p, overflow_p,
2832 : jump_target);
2833 3491 : if (*jump_target)
2834 : return NULL_TREE;
2835 3491 : if (*non_constant_p)
2836 : return t;
2837 3491 : return is_within_lifetime (loc, ctx, t, fun, arg,
2838 3491 : non_constant_p, overflow_p, jump_target);
2839 : }
2840 :
2841 : static tree cxx_eval_store_expression (const constexpr_ctx *, tree,
2842 : value_cat, bool *, bool *,
2843 : tree *);
2844 :
2845 : /* Attempt to evaluate T which represents a call to
2846 : __builtin_start_lifetime. */
2847 :
2848 : static tree
2849 1673 : cxx_eval_start_lifetime (const constexpr_ctx *ctx, tree t, tree fun,
2850 : bool *non_constant_p, bool *overflow_p,
2851 : tree *jump_target)
2852 : {
2853 1673 : location_t loc = EXPR_LOCATION (t);
2854 1673 : tree arg = CALL_EXPR_ARG (t, 0), within_lifetime;
2855 1673 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2856 : non_constant_p, overflow_p,
2857 : jump_target);
2858 1673 : if (*jump_target)
2859 : return NULL_TREE;
2860 1673 : if (*non_constant_p)
2861 : return t;
2862 1673 : arg = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (arg)), arg);
2863 1673 : arg = cxx_eval_constant_expression (ctx, arg, vc_glvalue,
2864 : non_constant_p, overflow_p,
2865 : jump_target);
2866 1673 : if (*jump_target)
2867 : return NULL_TREE;
2868 1673 : if (*non_constant_p)
2869 : return t;
2870 1673 : switch (TREE_CODE (arg))
2871 : {
2872 1667 : case COMPONENT_REF:
2873 1667 : case ARRAY_REF:
2874 1667 : case REALPART_EXPR:
2875 1667 : case IMAGPART_EXPR:
2876 1667 : within_lifetime
2877 1667 : = is_within_lifetime (loc, ctx, t, fun, TREE_OPERAND (arg, 0),
2878 : non_constant_p, overflow_p, jump_target);
2879 1667 : if (*jump_target)
2880 : return NULL_TREE;
2881 1667 : if (*non_constant_p)
2882 : return t;
2883 1667 : if (within_lifetime != boolean_true_node)
2884 : {
2885 90 : if (!ctx->quiet)
2886 30 : error_at (loc, "%qE with containing object not within lifetime",
2887 30 : DECL_NAME (fun));
2888 90 : *non_constant_p = true;
2889 90 : return t;
2890 : }
2891 : break;
2892 : default:
2893 : break;
2894 : }
2895 1583 : tree ctor = build_constructor (TREE_TYPE (arg), NULL);
2896 1583 : CONSTRUCTOR_NO_CLEARING (ctor) = 1;
2897 1583 : CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (ctor) = 1;
2898 1583 : tree modopexpr
2899 1583 : = build3_loc (loc, MODOP_EXPR, TREE_TYPE (arg), arg, ctor, NULL_TREE);
2900 1583 : arg = cxx_eval_store_expression (ctx, modopexpr, vc_discard, non_constant_p,
2901 : overflow_p, jump_target);
2902 1583 : ggc_free (modopexpr);
2903 1583 : if (*jump_target)
2904 : return NULL_TREE;
2905 1583 : if (*non_constant_p)
2906 : return t;
2907 1579 : return void_node;
2908 : }
2909 :
2910 : /* Attempt to evaluate T which represents a call to a builtin function.
2911 : We assume here that all builtin functions evaluate to scalar types
2912 : represented by _CST nodes. */
2913 :
2914 : static tree
2915 20514983 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2916 : value_cat lval,
2917 : bool *non_constant_p, bool *overflow_p,
2918 : tree *jump_target)
2919 : {
2920 20514983 : const int nargs = call_expr_nargs (t);
2921 20514983 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2922 20514983 : tree new_call;
2923 20514983 : int i;
2924 :
2925 : /* Don't fold __builtin_constant_p within a constexpr function. */
2926 20514983 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2927 :
2928 : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2929 : in a constexpr function until we have values for the parameters. */
2930 3529489 : if (bi_const_p
2931 3529489 : && ctx->manifestly_const_eval != mce_true
2932 1711341 : && current_function_decl
2933 1710212 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2934 : {
2935 1371385 : *non_constant_p = true;
2936 1371385 : return t;
2937 : }
2938 :
2939 19143598 : if (fndecl_built_in_p (fun, BUILT_IN_FRONTEND))
2940 3922866 : switch (DECL_FE_FUNCTION_CODE (fun))
2941 : {
2942 3898424 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
2943 : /* For __builtin_is_constant_evaluated, defer it if not
2944 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2945 : without manifestly_const_eval even expressions or parts thereof
2946 : which will later be manifestly const_eval evaluated), otherwise fold
2947 : it to true. */
2948 3898424 : if (ctx->manifestly_const_eval == mce_unknown)
2949 : {
2950 3851162 : *non_constant_p = true;
2951 3851162 : return t;
2952 : }
2953 47262 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2954 47262 : boolean_type_node);
2955 :
2956 11003 : case CP_BUILT_IN_SOURCE_LOCATION:
2957 11003 : {
2958 11003 : temp_override<tree> ovr (current_function_decl);
2959 11003 : if (ctx->call && ctx->call->fundef)
2960 2773 : current_function_decl = ctx->call->fundef->decl;
2961 11003 : return fold_builtin_source_location (t);
2962 11003 : }
2963 :
2964 126 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
2965 126 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2966 : fun, non_constant_p, overflow_p,
2967 126 : jump_target);
2968 :
2969 2238 : case CP_BUILT_IN_CURRENT_EXCEPTION:
2970 2238 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_CURRENT_EXCEPTION,
2971 : fun, non_constant_p, overflow_p,
2972 2238 : jump_target);
2973 :
2974 82 : case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
2975 82 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_UNCAUGHT_EXCEPTIONS,
2976 : fun, non_constant_p, overflow_p,
2977 82 : jump_target);
2978 :
2979 210 : case CP_BUILT_IN_CONSTEXPR_DIAG:
2980 210 : return cxx_eval_constexpr_diag (ctx, t, non_constant_p, overflow_p,
2981 210 : jump_target);
2982 :
2983 3952 : case CP_BUILT_IN_IS_WITHIN_LIFETIME:
2984 3952 : return cxx_eval_is_within_lifetime (ctx, t, fun, non_constant_p,
2985 3952 : overflow_p, jump_target);
2986 :
2987 1673 : case CP_BUILT_IN_START_LIFETIME:
2988 1673 : return cxx_eval_start_lifetime (ctx, t, fun, non_constant_p,
2989 1673 : overflow_p, jump_target);
2990 :
2991 : default:
2992 : break;
2993 : }
2994 :
2995 15225890 : int strops = 0;
2996 15225890 : int strret = 0;
2997 15225890 : bool bos = false;
2998 15225890 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2999 13879006 : switch (DECL_FUNCTION_CODE (fun))
3000 : {
3001 : case BUILT_IN_STRLEN:
3002 : case BUILT_IN_STRNLEN:
3003 15225739 : strops = 1;
3004 : break;
3005 198142 : case BUILT_IN_MEMCHR:
3006 198142 : case BUILT_IN_STRCHR:
3007 198142 : case BUILT_IN_STRRCHR:
3008 198142 : strops = 1;
3009 198142 : strret = 1;
3010 198142 : break;
3011 109567 : case BUILT_IN_MEMCMP:
3012 109567 : case BUILT_IN_STRCMP:
3013 109567 : strops = 2;
3014 109567 : break;
3015 49436 : case BUILT_IN_STRSTR:
3016 49436 : strops = 2;
3017 49436 : strret = 1;
3018 49436 : break;
3019 66 : case BUILT_IN_ASAN_POINTER_COMPARE:
3020 66 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
3021 66 : case BUILT_IN_OBSERVABLE_CHKPT:
3022 : /* These builtins shall be ignored during constant expression
3023 : evaluation. */
3024 66 : return void_node;
3025 85 : case BUILT_IN_UNREACHABLE:
3026 85 : case BUILT_IN_TRAP:
3027 85 : if (!*non_constant_p && !ctx->quiet)
3028 : {
3029 : /* Do not allow__builtin_unreachable in constexpr function.
3030 : The __builtin_unreachable call with BUILTINS_LOCATION
3031 : comes from cp_maybe_instrument_return. */
3032 25 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
3033 0 : error ("%<constexpr%> call flows off the end of the function");
3034 : else
3035 25 : error ("%q+E is not a constant expression", t);
3036 : }
3037 85 : *non_constant_p = true;
3038 85 : return t;
3039 2881 : case BUILT_IN_OBJECT_SIZE:
3040 2881 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
3041 2881 : bos = ctx->manifestly_const_eval == mce_true;
3042 2881 : break;
3043 : default:
3044 : break;
3045 : }
3046 :
3047 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
3048 : return constant false for a non-constant argument. */
3049 15225739 : constexpr_ctx new_ctx = *ctx;
3050 15225739 : new_ctx.quiet = true;
3051 41357620 : for (i = 0; i < nargs; ++i)
3052 : {
3053 26131883 : tree arg = CALL_EXPR_ARG (t, i);
3054 26131883 : tree oarg = arg;
3055 :
3056 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
3057 : expand_builtin doesn't know how to look in the values table. */
3058 26131883 : bool strop = i < strops;
3059 26131883 : if (strop)
3060 : {
3061 671876 : STRIP_NOPS (arg);
3062 671876 : if (TREE_CODE (arg) == ADDR_EXPR)
3063 8131 : arg = TREE_OPERAND (arg, 0);
3064 : else
3065 : strop = false;
3066 : }
3067 :
3068 : /* If builtin_valid_in_constant_expr_p is true,
3069 : potential_constant_expression_1 has not recursed into the arguments
3070 : of the builtin, verify it here. */
3071 26131883 : if (!builtin_valid_in_constant_expr_p (fun)
3072 26131883 : || potential_constant_expression (arg))
3073 : {
3074 26131746 : bool dummy1 = false, dummy2 = false;
3075 26131746 : tree jmp_target = NULL_TREE;
3076 26131746 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3077 : &dummy1, &dummy2, &jmp_target);
3078 26131744 : if (jmp_target)
3079 : {
3080 0 : *jump_target = jmp_target;
3081 0 : return NULL_TREE;
3082 : }
3083 : }
3084 :
3085 26131881 : if (bi_const_p)
3086 : /* For __builtin_constant_p, fold all expressions with constant values
3087 : even if they aren't C++ constant-expressions. */
3088 2158102 : arg = cp_fold_rvalue (arg);
3089 23973779 : else if (strop)
3090 : {
3091 8131 : if (TREE_CODE (arg) == CONSTRUCTOR)
3092 76 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
3093 8131 : if (TREE_CODE (arg) == STRING_CST)
3094 3019 : arg = build_address (arg);
3095 : else
3096 : arg = oarg;
3097 : }
3098 :
3099 : /* If the argument STRRET refers to didn't end up as the address of a
3100 : STRING_CST, the folded result is already relative to it and must not
3101 : be re-based on the original argument below. */
3102 26131881 : if (i == strret - 1
3103 26131881 : && (TREE_CODE (arg) != ADDR_EXPR
3104 110 : || TREE_CODE (TREE_OPERAND (arg, 0)) != STRING_CST))
3105 : strret = 0;
3106 :
3107 26131881 : args[i] = arg;
3108 : }
3109 15225737 : if (bos)
3110 : {
3111 102 : tree arg = args[0];
3112 102 : STRIP_NOPS (arg);
3113 102 : if (TREE_CODE (arg) == ADDR_EXPR)
3114 102 : args[0] = arg;
3115 : }
3116 :
3117 15225737 : bool save_ffbcp = force_folding_builtin_constant_p;
3118 15225737 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
3119 15225737 : tree save_cur_fn = current_function_decl;
3120 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
3121 15225737 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
3122 37 : && ctx->call
3123 15225741 : && ctx->call->fundef)
3124 4 : current_function_decl = ctx->call->fundef->decl;
3125 15225737 : if (fndecl_built_in_p (fun,
3126 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
3127 : BUILT_IN_FRONTEND))
3128 : {
3129 336 : location_t loc = EXPR_LOCATION (t);
3130 336 : if (nargs >= 1)
3131 333 : VERIFY_CONSTANT (args[0]);
3132 136 : new_call
3133 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
3134 : args);
3135 : }
3136 15225401 : else if (fndecl_built_in_p (fun,
3137 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
3138 : BUILT_IN_FRONTEND))
3139 : {
3140 471 : location_t loc = EXPR_LOCATION (t);
3141 471 : if (nargs >= 2)
3142 : {
3143 465 : VERIFY_CONSTANT (args[0]);
3144 237 : VERIFY_CONSTANT (args[1]);
3145 : }
3146 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
3147 : }
3148 15224930 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
3149 : BUILT_IN_FRONTEND))
3150 : {
3151 4351 : location_t loc = EXPR_LOCATION (t);
3152 4351 : if (nargs >= 1)
3153 : {
3154 4351 : tree arg = CALL_EXPR_ARG (t, 0);
3155 4351 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3156 : non_constant_p, overflow_p,
3157 : jump_target);
3158 4351 : if (*jump_target)
3159 : return NULL_TREE;
3160 4351 : args[0] = arg;
3161 : }
3162 4351 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
3163 : }
3164 : else
3165 30441158 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
3166 15220579 : CALL_EXPR_FN (t), nargs, args);
3167 15225309 : current_function_decl = save_cur_fn;
3168 15225309 : force_folding_builtin_constant_p = save_ffbcp;
3169 15225309 : if (new_call == NULL)
3170 : {
3171 10345943 : if (!*non_constant_p && !ctx->quiet)
3172 : {
3173 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
3174 0 : CALL_EXPR_FN (t), nargs, args);
3175 0 : error ("%q+E is not a constant expression", new_call);
3176 : }
3177 10345943 : *non_constant_p = true;
3178 10345943 : return t;
3179 : }
3180 :
3181 4879366 : if (!potential_constant_expression (new_call))
3182 : {
3183 861 : if (!*non_constant_p && !ctx->quiet)
3184 4 : error ("%q+E is not a constant expression", new_call);
3185 861 : *non_constant_p = true;
3186 861 : return t;
3187 : }
3188 :
3189 4878505 : if (strret)
3190 : {
3191 : /* memchr returns a pointer into the first argument, but we replaced the
3192 : argument above with a STRING_CST; put it back it now. */
3193 18 : tree op = CALL_EXPR_ARG (t, strret-1);
3194 18 : STRIP_NOPS (new_call);
3195 18 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
3196 15 : TREE_OPERAND (new_call, 0) = op;
3197 3 : else if (TREE_CODE (new_call) == ADDR_EXPR)
3198 4878505 : new_call = op;
3199 : }
3200 :
3201 4878505 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
3202 : non_constant_p, overflow_p,
3203 4878505 : jump_target);
3204 : }
3205 :
3206 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
3207 : the type of the value to match. */
3208 :
3209 : static tree
3210 136430322 : adjust_temp_type (tree type, tree temp)
3211 : {
3212 136430322 : if (same_type_p (TREE_TYPE (temp), type))
3213 : return temp;
3214 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
3215 62871957 : if (TREE_CODE (temp) == CONSTRUCTOR)
3216 : {
3217 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
3218 3787581 : tree t = copy_node (temp);
3219 3787581 : TREE_TYPE (t) = type;
3220 3787581 : return t;
3221 : }
3222 59084376 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
3223 0 : return build0 (EMPTY_CLASS_EXPR, type);
3224 59084376 : gcc_assert (scalarish_type_p (type));
3225 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
3226 : type is cv-unqualified. */
3227 59084376 : return cp_fold_convert (cv_unqualified (type), temp);
3228 : }
3229 :
3230 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
3231 : sub-CONSTRUCTORs. Otherwise return T.
3232 :
3233 : We use this whenever we initialize an object as a whole, whether it's a
3234 : parameter, a local variable, or a subobject, so that subsequent
3235 : modifications don't affect other places where it was used. */
3236 :
3237 : tree
3238 123710891 : unshare_constructor (tree t MEM_STAT_DECL)
3239 : {
3240 123710891 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
3241 : return t;
3242 18571196 : auto_vec <tree*, 4> ptrs;
3243 18571196 : ptrs.safe_push (&t);
3244 18571196 : while (!ptrs.is_empty ())
3245 : {
3246 24455281 : tree *p = ptrs.pop ();
3247 24455281 : tree n = copy_node (*p PASS_MEM_STAT);
3248 40608089 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
3249 24455281 : *p = n;
3250 24455281 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
3251 24455281 : constructor_elt *ce;
3252 93981903 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
3253 : {
3254 26500145 : if (ce->index && TREE_CODE (ce->index) == RANGE_EXPR)
3255 780 : ce->index = copy_node (ce->index PASS_MEM_STAT);
3256 26500145 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
3257 5884085 : ptrs.safe_push (&ce->value);
3258 : }
3259 : }
3260 18571196 : return t;
3261 18571196 : }
3262 :
3263 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
3264 :
3265 : static void
3266 872115 : free_constructor (tree t)
3267 : {
3268 872115 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
3269 0 : return;
3270 872115 : releasing_vec ctors;
3271 872115 : vec_safe_push (ctors, t);
3272 2655571 : while (!ctors->is_empty ())
3273 : {
3274 911341 : tree c = ctors->pop ();
3275 911341 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
3276 : {
3277 : constructor_elt *ce;
3278 393053 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
3279 254526 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
3280 39226 : vec_safe_push (ctors, ce->value);
3281 138527 : ggc_free (elts);
3282 : }
3283 911341 : ggc_free (c);
3284 : }
3285 872115 : }
3286 :
3287 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
3288 : if *TP is address of a static variable (or part of it) currently being
3289 : constructed or of a heap artificial variable. */
3290 :
3291 : static tree
3292 48867547 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
3293 : {
3294 48867547 : if (TREE_CODE (*tp) == ADDR_EXPR)
3295 9065898 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
3296 9065898 : if (VAR_P (var) && TREE_STATIC (var))
3297 : {
3298 5674970 : if (heap_var_p (var))
3299 : return var;
3300 :
3301 5674970 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
3302 5674970 : if (global->get_value (var))
3303 : return var;
3304 : }
3305 48424736 : if (TYPE_P (*tp))
3306 2503 : *walk_subtrees = false;
3307 : return NULL_TREE;
3308 : }
3309 :
3310 : /* Subroutine of cxx_eval_call_expression.
3311 : We are processing a call expression (either CALL_EXPR or
3312 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
3313 : all arguments and bind their values to correspondings
3314 : parameters, making up the NEW_CALL context. */
3315 :
3316 : static tree
3317 183547306 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
3318 : tree orig_fun, bool *non_constant_p,
3319 : bool *overflow_p, bool *non_constant_args,
3320 : tree *jump_target)
3321 : {
3322 183547306 : int nargs = call_expr_nargs (t);
3323 183547306 : tree parms = DECL_ARGUMENTS (fun);
3324 183547306 : int i, j = 0;
3325 183547306 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
3326 1523 : ++nargs;
3327 183547306 : if (DECL_HAS_VTT_PARM_P (fun)
3328 1525 : && fun != orig_fun
3329 183548829 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
3330 1082 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
3331 463 : ++nargs;
3332 : /* We don't record ellipsis args below. */
3333 183547306 : int nparms = list_length (parms);
3334 183547306 : int nbinds = nargs < nparms ? nargs : nparms;
3335 183547306 : tree binds = make_tree_vec (nbinds);
3336 :
3337 : /* The call is not a constant expression if it involves the cdtor for a type
3338 : with virtual bases before C++26. */
3339 183547306 : if (cxx_dialect < cxx26
3340 183547306 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
3341 : {
3342 17 : if (!ctx->quiet)
3343 : {
3344 3 : error_at (cp_expr_loc_or_input_loc (t),
3345 : "call to non-%<constexpr%> function %qD", fun);
3346 3 : explain_invalid_constexpr_fn (fun);
3347 : }
3348 17 : *non_constant_p = true;
3349 17 : return binds;
3350 : }
3351 :
3352 330863353 : for (i = 0; i < nargs; ++i)
3353 : {
3354 220907528 : tree x, arg;
3355 220907528 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
3356 220907528 : if (parms && DECL_BY_REFERENCE (parms))
3357 27161 : type = TREE_TYPE (type);
3358 220907528 : if (i == 1
3359 220907528 : && j == 0
3360 47443897 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
3361 220908919 : && orig_fun != fun)
3362 : {
3363 1391 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
3364 1391 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
3365 428 : x = boolean_true_node;
3366 : else
3367 963 : x = boolean_false_node;
3368 : j = -1;
3369 : }
3370 220906137 : else if (i == 2
3371 220906137 : && j == -1
3372 1391 : && DECL_HAS_VTT_PARM_P (fun)
3373 1391 : && orig_fun != fun
3374 220907528 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
3375 985 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
3376 : {
3377 428 : x = build_zero_cst (type);
3378 428 : j = -2;
3379 : }
3380 : else
3381 220905709 : x = get_nth_callarg (t, i + j);
3382 : /* For member function, the first argument is a pointer to the implied
3383 : object. For a constructor, it might still be a dummy object, in
3384 : which case we get the real argument from ctx. */
3385 323583958 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
3386 243729951 : && is_dummy_object (x))
3387 : {
3388 11700483 : x = ctx->object;
3389 11700483 : x = build_address (x);
3390 : }
3391 220907528 : if (TREE_ADDRESSABLE (type))
3392 : {
3393 : /* Undo convert_for_arg_passing work here. */
3394 39203 : x = convert_from_reference (x);
3395 39203 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
3396 : non_constant_p, overflow_p,
3397 : jump_target);
3398 : }
3399 : else
3400 : /* Normally we would strip a TARGET_EXPR in an initialization context
3401 : such as this, but here we do the elision differently: we keep the
3402 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
3403 220868325 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
3404 : non_constant_p, overflow_p,
3405 : jump_target);
3406 220907528 : if (*jump_target)
3407 : break;
3408 : /* Check we aren't dereferencing a null pointer when calling a non-static
3409 : member function, which is undefined behaviour. */
3410 161791924 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
3411 92010395 : && integer_zerop (arg)
3412 : /* But ignore calls from within compiler-generated code, to handle
3413 : cases like lambda function pointer conversion operator thunks
3414 : which pass NULL as the 'this' pointer. */
3415 221081445 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
3416 : {
3417 527 : if (!ctx->quiet)
3418 6 : error_at (cp_expr_loc_or_input_loc (x),
3419 : "dereferencing a null pointer");
3420 527 : *non_constant_p = true;
3421 : }
3422 : /* Don't VERIFY_CONSTANT here. */
3423 220907444 : if (*non_constant_p && ctx->quiet)
3424 : break;
3425 : /* Just discard ellipsis args after checking their constantitude. */
3426 147316064 : if (!parms)
3427 400 : continue;
3428 :
3429 147315664 : if (!*non_constant_p)
3430 : {
3431 : /* Make sure the binding has the same type as the parm. But
3432 : only for constant args. */
3433 147314890 : if (TREE_ADDRESSABLE (type))
3434 : {
3435 26088 : if (!same_type_p (type, TREE_TYPE (arg)))
3436 : {
3437 9 : arg = build_fold_addr_expr (arg);
3438 9 : arg = cp_fold_convert (build_reference_type (type), arg);
3439 9 : arg = convert_from_reference (arg);
3440 : }
3441 : }
3442 147288802 : else if (!TYPE_REF_P (type))
3443 105208346 : arg = adjust_temp_type (type, arg);
3444 147314890 : if (!TREE_CONSTANT (arg))
3445 96980897 : *non_constant_args = true;
3446 50333993 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3447 : /* The destructor needs to see any modifications the callee makes
3448 : to the argument. */
3449 0 : *non_constant_args = true;
3450 : /* If arg is or contains address of a heap artificial variable or
3451 : of a static variable being constructed, avoid caching the
3452 : function call, as those variables might be modified by the
3453 : function, or might be modified by the callers in between
3454 : the cached function and just read by the function. */
3455 50333993 : else if (!*non_constant_args
3456 50333993 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
3457 : NULL))
3458 442811 : *non_constant_args = true;
3459 :
3460 : /* For virtual calls, adjust the this argument, so that it is
3461 : the object on which the method is called, rather than
3462 : one of its bases. */
3463 147314890 : if (i == 0 && DECL_VIRTUAL_P (fun))
3464 : {
3465 27557 : tree addr = arg;
3466 27557 : STRIP_NOPS (addr);
3467 27557 : if (TREE_CODE (addr) == ADDR_EXPR)
3468 : {
3469 27520 : tree obj = TREE_OPERAND (addr, 0);
3470 27520 : while (TREE_CODE (obj) == COMPONENT_REF
3471 11865 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
3472 39463 : && !same_type_ignoring_top_level_qualifiers_p
3473 11772 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
3474 171 : obj = TREE_OPERAND (obj, 0);
3475 27520 : if (obj != TREE_OPERAND (addr, 0))
3476 168 : arg = build_fold_addr_expr_with_type (obj,
3477 : TREE_TYPE (arg));
3478 : }
3479 : }
3480 147314890 : TREE_VEC_ELT (binds, i) = arg;
3481 : }
3482 147315664 : parms = TREE_CHAIN (parms);
3483 : }
3484 :
3485 : return binds;
3486 : }
3487 :
3488 : static int
3489 109368954 : push_cx_call_context (tree call)
3490 : {
3491 109368954 : ++call_stack_tick;
3492 109368954 : if (!EXPR_HAS_LOCATION (call))
3493 75425 : SET_EXPR_LOCATION (call, input_location);
3494 109368954 : call_stack.safe_push (call);
3495 109368954 : int len = call_stack.length ();
3496 109368954 : if (len > max_constexpr_depth)
3497 33 : return false;
3498 : return len;
3499 : }
3500 :
3501 : static void
3502 109368950 : pop_cx_call_context (void)
3503 : {
3504 109368950 : ++call_stack_tick;
3505 109368950 : call_stack.pop ();
3506 109368950 : }
3507 :
3508 : vec<tree>
3509 261545 : cx_error_context (void)
3510 : {
3511 261545 : vec<tree> r = vNULL;
3512 261545 : if (call_stack_tick != last_cx_error_tick
3513 261545 : && !call_stack.is_empty ())
3514 : r = call_stack;
3515 261545 : last_cx_error_tick = call_stack_tick;
3516 261545 : return r;
3517 : }
3518 :
3519 : /* E is an operand of a failed assertion, fold it either with or without
3520 : constexpr context. */
3521 :
3522 : static tree
3523 829 : fold_operand (tree e, const constexpr_ctx *ctx)
3524 : {
3525 829 : if (ctx)
3526 : {
3527 32 : bool new_non_constant_p = false, new_overflow_p = false;
3528 32 : tree jmp_target = NULL_TREE;
3529 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3530 : &new_non_constant_p,
3531 : &new_overflow_p, &jmp_target);
3532 : }
3533 : else
3534 797 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3535 829 : return e;
3536 : }
3537 :
3538 : /* If we have a condition in conjunctive normal form (CNF), find the first
3539 : failing clause. In other words, given an expression like
3540 :
3541 : true && true && false && true && false
3542 :
3543 : return the first 'false'. EXPR is the expression. */
3544 :
3545 : static tree
3546 332 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3547 : {
3548 437 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3549 : {
3550 : /* First check the left side... */
3551 192 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3552 192 : if (e == NULL_TREE)
3553 : /* ...if we didn't find a false clause, check the right side. */
3554 105 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3555 : return e;
3556 : }
3557 245 : tree e = contextual_conv_bool (expr, tf_none);
3558 245 : e = fold_operand (e, ctx);
3559 245 : if (integer_zerop (e))
3560 : /* This is the failing clause. */
3561 140 : return expr;
3562 : return NULL_TREE;
3563 : }
3564 :
3565 : /* Wrapper for find_failing_clause_r. */
3566 :
3567 : tree
3568 1986 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3569 : {
3570 1986 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3571 140 : if (tree e = find_failing_clause_r (ctx, expr))
3572 1986 : expr = e;
3573 1986 : return expr;
3574 : }
3575 :
3576 : /* Emit additional diagnostics for failing condition BAD.
3577 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3578 : If SHOW_EXPR_P is true, print the condition (because it was
3579 : instantiation-dependent). */
3580 :
3581 : void
3582 2010 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3583 : const constexpr_ctx *ctx /* = nullptr */)
3584 : {
3585 : /* Nobody wants to see the artificial (bool) cast. */
3586 2010 : bad = tree_strip_nop_conversions (bad);
3587 2010 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3588 3 : bad = TREE_OPERAND (bad, 0);
3589 :
3590 2010 : auto_diagnostic_nesting_level sentinel;
3591 :
3592 : /* Actually explain the failure if this is a concept check or a
3593 : requires-expression. */
3594 2010 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3595 334 : diagnose_constraints (cloc, bad, NULL_TREE);
3596 : /* Similarly if this is a standard trait. */
3597 1676 : else if (maybe_diagnose_standard_trait (cloc, bad))
3598 : ;
3599 1321 : else if (COMPARISON_CLASS_P (bad)
3600 1321 : && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))
3601 76 : || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))))
3602 : {
3603 292 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3604 292 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3605 292 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3606 292 : inform (cloc, "the comparison reduces to %qE", cond);
3607 : }
3608 1029 : else if (show_expr_p)
3609 779 : inform (cloc, "%qE evaluates to false", bad);
3610 2010 : }
3611 :
3612 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3613 : do it without changing the current evaluation state. If it evaluates to
3614 : false, complain and return false; otherwise, return true. */
3615 :
3616 : static bool
3617 173602 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3618 : location_t loc, bool evaluated,
3619 : bool *non_constant_p, bool *overflow_p)
3620 : {
3621 173602 : if (*non_constant_p)
3622 : return true;
3623 :
3624 173602 : tree eval, jmp_target = NULL_TREE;
3625 173602 : if (!evaluated)
3626 : {
3627 173602 : if (!potential_rvalue_constant_expression (arg))
3628 16 : return true;
3629 :
3630 173586 : constexpr_ctx new_ctx = *ctx;
3631 173586 : new_ctx.quiet = true;
3632 173586 : bool new_non_constant_p = false, new_overflow_p = false;
3633 : /* Avoid modification of existing values. */
3634 173586 : modifiable_tracker ms (new_ctx.global);
3635 173586 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3636 : &new_non_constant_p,
3637 : &new_overflow_p, &jmp_target);
3638 173586 : }
3639 : else
3640 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3641 : non_constant_p,
3642 : overflow_p, &jmp_target);
3643 173586 : if (jmp_target)
3644 : return true;
3645 :
3646 173586 : if (!*non_constant_p && integer_zerop (eval))
3647 : {
3648 42 : if (!ctx->quiet)
3649 : {
3650 : /* See if we can find which clause was failing
3651 : (for logical AND). */
3652 13 : tree bad = find_failing_clause (ctx, arg);
3653 : /* If not, or its location is unusable, fall back to the
3654 : previous location. */
3655 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3656 :
3657 : /* Report the error. */
3658 13 : auto_diagnostic_group d;
3659 13 : error_at (cloc, msg);
3660 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3661 13 : return bad;
3662 13 : }
3663 29 : *non_constant_p = true;
3664 29 : return false;
3665 : }
3666 :
3667 : return true;
3668 : }
3669 :
3670 : /* Evaluate a call T to a GCC internal function when possible and return
3671 : the evaluated result or, under the control of CTX, give an error, set
3672 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3673 :
3674 : static tree
3675 383909 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3676 : value_cat lval,
3677 : bool *non_constant_p, bool *overflow_p,
3678 : tree *jump_target)
3679 : {
3680 383909 : enum tree_code opcode = ERROR_MARK;
3681 :
3682 383909 : switch (CALL_EXPR_IFN (t))
3683 : {
3684 166 : case IFN_UBSAN_NULL:
3685 166 : case IFN_UBSAN_BOUNDS:
3686 166 : case IFN_UBSAN_VPTR:
3687 166 : case IFN_FALLTHROUGH:
3688 166 : return void_node;
3689 :
3690 173602 : case IFN_ASSUME:
3691 173602 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3692 : G_("failed %<assume%> attribute assumption"),
3693 173602 : EXPR_LOCATION (t), /*eval*/false,
3694 : non_constant_p, overflow_p))
3695 : return t;
3696 173573 : return void_node;
3697 :
3698 : case IFN_ADD_OVERFLOW:
3699 : opcode = PLUS_EXPR;
3700 : break;
3701 385 : case IFN_SUB_OVERFLOW:
3702 385 : opcode = MINUS_EXPR;
3703 385 : break;
3704 202071 : case IFN_MUL_OVERFLOW:
3705 202071 : opcode = MULT_EXPR;
3706 202071 : break;
3707 :
3708 96 : case IFN_LAUNDER:
3709 96 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3710 : vc_prvalue, non_constant_p,
3711 96 : overflow_p, jump_target);
3712 :
3713 0 : case IFN_DEFERRED_INIT:
3714 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3715 :
3716 176 : case IFN_BSWAP:
3717 176 : case IFN_BITREVERSE:
3718 176 : {
3719 176 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3720 : vc_prvalue, non_constant_p,
3721 : overflow_p, jump_target);
3722 176 : if (*jump_target)
3723 : return NULL_TREE;
3724 176 : if (*non_constant_p)
3725 : return t;
3726 104 : location_t loc = cp_expr_loc_or_input_loc (t);
3727 104 : if (TREE_CODE (arg) != INTEGER_CST
3728 104 : || !INTEGRAL_TYPE_P (TREE_TYPE (arg))
3729 104 : || !TYPE_UNSIGNED (TREE_TYPE (arg))
3730 208 : || (CALL_EXPR_IFN (t) == IFN_BSWAP
3731 30 : && (TYPE_PRECISION (TREE_TYPE (arg)) % 8) != 0))
3732 : {
3733 0 : if (!ctx->quiet)
3734 0 : error_at (loc, "call to internal function %qE", t);
3735 0 : *non_constant_p = true;
3736 0 : return t;
3737 : }
3738 104 : return fold_build_builtin_bswapg_bitreverseg (loc, CALL_EXPR_IFN (t),
3739 104 : arg);
3740 : }
3741 :
3742 279 : case IFN_CLMUL:
3743 279 : {
3744 279 : tree args[3] = {};
3745 551 : for (int i = 0; i < call_expr_nargs (t); ++i)
3746 : {
3747 445 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, i),
3748 : vc_prvalue, non_constant_p,
3749 : overflow_p, jump_target);
3750 445 : if (*jump_target)
3751 : return NULL_TREE;
3752 445 : if (*non_constant_p)
3753 : return t;
3754 : }
3755 106 : location_t loc = cp_expr_loc_or_input_loc (t);
3756 106 : if (TREE_CODE (args[0]) != INTEGER_CST
3757 106 : || TREE_CODE (args[1]) != INTEGER_CST)
3758 : {
3759 0 : if (!ctx->quiet)
3760 0 : error_at (loc, "call to internal function %qE", t);
3761 0 : *non_constant_p = true;
3762 0 : return t;
3763 : }
3764 106 : unsigned int prec = TYPE_PRECISION (TREE_TYPE (args[0]));
3765 106 : wide_int res = wi::clmul (wi::to_wide (args[0]),
3766 106 : wi::to_wide (args[1]),
3767 298 : prec * (args[2] ? 2 : 1));
3768 106 : if (args[2] == NULL_TREE)
3769 86 : return wide_int_to_tree (TREE_TYPE (t), res);
3770 20 : wide_int lo = wide_int::from (res, prec, UNSIGNED);
3771 20 : wide_int hi = wide_int::from (wi::rshift (res, prec, UNSIGNED),
3772 40 : prec, UNSIGNED);
3773 40 : tree mod = build2_loc (loc, MODIFY_EXPR, void_type_node,
3774 : build_fold_indirect_ref_loc (loc, args[2]),
3775 20 : wide_int_to_tree (TREE_TYPE (t), hi));
3776 20 : cxx_eval_constant_expression (ctx, mod, vc_prvalue, non_constant_p,
3777 : overflow_p, jump_target);
3778 20 : if (*jump_target)
3779 : return NULL_TREE;
3780 20 : if (*non_constant_p)
3781 : return t;
3782 20 : return wide_int_to_tree (TREE_TYPE (t), lo);
3783 126 : }
3784 :
3785 6634 : case IFN_VEC_CONVERT:
3786 6634 : {
3787 6634 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3788 : vc_prvalue, non_constant_p,
3789 : overflow_p, jump_target);
3790 6634 : if (*jump_target)
3791 : return NULL_TREE;
3792 6634 : if (TREE_CODE (arg) == VECTOR_CST)
3793 6237 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3794 : return r;
3795 : }
3796 : /* FALLTHRU */
3797 :
3798 402 : default:
3799 402 : if (!ctx->quiet)
3800 0 : error_at (cp_expr_loc_or_input_loc (t),
3801 : "call to internal function %qE", t);
3802 402 : *non_constant_p = true;
3803 402 : return t;
3804 : }
3805 :
3806 : /* Evaluate constant arguments using OPCODE and return a complex
3807 : number containing the result and the overflow bit. */
3808 202956 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3809 : non_constant_p, overflow_p,
3810 : jump_target);
3811 202956 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3812 : non_constant_p, overflow_p,
3813 : jump_target);
3814 202956 : if (*jump_target)
3815 : return NULL_TREE;
3816 202956 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3817 : {
3818 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3819 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3820 2 : tree result = fold_binary_loc (loc, opcode, type,
3821 : fold_convert_loc (loc, type, arg0),
3822 : fold_convert_loc (loc, type, arg1));
3823 2 : tree ovf
3824 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3825 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3826 2 : if (TREE_OVERFLOW (result))
3827 0 : TREE_OVERFLOW (result) = 0;
3828 :
3829 2 : return build_complex (TREE_TYPE (t), result, ovf);
3830 : }
3831 :
3832 202954 : *non_constant_p = true;
3833 202954 : return t;
3834 : }
3835 :
3836 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3837 :
3838 : static void
3839 5970847 : clear_no_implicit_zero (tree ctor)
3840 : {
3841 5970847 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3842 : {
3843 1747221 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3844 7396810 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3845 2323341 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3846 729791 : clear_no_implicit_zero (e.value);
3847 : }
3848 5970847 : }
3849 :
3850 : /* Complain about a const object OBJ being modified in a constant expression.
3851 : EXPR is the MODIFY_EXPR expression performing the modification. */
3852 :
3853 : static void
3854 65 : modifying_const_object_error (tree expr, tree obj)
3855 : {
3856 65 : location_t loc = cp_expr_loc_or_input_loc (expr);
3857 65 : auto_diagnostic_group d;
3858 130 : error_at (loc, "modifying a const object %qE is not allowed in "
3859 65 : "a constant expression", TREE_OPERAND (expr, 0));
3860 :
3861 : /* Find the underlying object that was declared as const. */
3862 65 : location_t decl_loc = UNKNOWN_LOCATION;
3863 207 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3864 77 : switch (TREE_CODE (probe))
3865 : {
3866 39 : case BIT_FIELD_REF:
3867 39 : case COMPONENT_REF:
3868 39 : {
3869 39 : tree elt = TREE_OPERAND (probe, 1);
3870 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3871 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3872 39 : probe = TREE_OPERAND (probe, 0);
3873 : }
3874 39 : break;
3875 :
3876 0 : case ARRAY_REF:
3877 0 : case REALPART_EXPR:
3878 0 : case IMAGPART_EXPR:
3879 0 : probe = TREE_OPERAND (probe, 0);
3880 0 : break;
3881 :
3882 38 : default:
3883 38 : decl_loc = location_of (probe);
3884 38 : break;
3885 : }
3886 65 : inform (decl_loc, "originally declared %<const%> here");
3887 65 : }
3888 :
3889 : /* Return true if FNDECL is a replaceable global allocation function that
3890 : should be usable during constant expression evaluation. */
3891 :
3892 : static inline bool
3893 38309093 : cxx_replaceable_global_alloc_fn (tree fndecl)
3894 : {
3895 38309093 : return (cxx_dialect >= cxx20
3896 36246252 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3897 2195766 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3898 40504462 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3899 907772 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3900 : }
3901 :
3902 : /* Return true if FNDECL is a placement new function that should be
3903 : usable during constant expression evaluation of std::construct_at. */
3904 :
3905 : static inline bool
3906 36855415 : cxx_placement_new_fn (tree fndecl)
3907 : {
3908 36855415 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3909 : }
3910 :
3911 : /* Return true if FNDECL is std::construct_at. */
3912 :
3913 : static inline bool
3914 2330025 : is_std_construct_at (tree fndecl)
3915 : {
3916 2330025 : if (!decl_in_std_namespace_p (fndecl))
3917 : return false;
3918 :
3919 2286595 : tree name = DECL_NAME (fndecl);
3920 2286595 : return name && id_equal (name, "construct_at");
3921 : }
3922 :
3923 : /* Overload for the above taking constexpr_call*. */
3924 :
3925 : static inline bool
3926 2094447 : is_std_construct_at (const constexpr_call *call)
3927 : {
3928 2094447 : return (call
3929 1937303 : && call->fundef
3930 4031750 : && is_std_construct_at (call->fundef->decl));
3931 : }
3932 :
3933 : /* True if CTX is an instance of std::NAME class. */
3934 :
3935 : bool
3936 12206486 : is_std_class (tree ctx, const char *name)
3937 : {
3938 12206486 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3939 : return false;
3940 :
3941 12205864 : tree decl = TYPE_MAIN_DECL (ctx);
3942 12205864 : tree dname = DECL_NAME (decl);
3943 12205864 : if (dname == NULL_TREE || !id_equal (dname, name))
3944 : return false;
3945 :
3946 865367 : return decl_in_std_namespace_p (decl);
3947 : }
3948 :
3949 : /* True if CTX is an instance of std::allocator. */
3950 :
3951 : bool
3952 704233 : is_std_allocator (tree ctx)
3953 : {
3954 704233 : return is_std_class (ctx, "allocator");
3955 : }
3956 :
3957 : /* Return true if FNDECL is std::allocator<T>::allocate. */
3958 :
3959 : static bool
3960 107194 : is_std_allocator_allocate (tree fndecl)
3961 : {
3962 107194 : tree name = DECL_NAME (fndecl);
3963 107194 : if (name == NULL_TREE || !id_equal (name, "allocate"))
3964 : return false;
3965 :
3966 98404 : return is_std_allocator (DECL_CONTEXT (fndecl));
3967 : }
3968 :
3969 : /* Overload for the above taking constexpr_call*. */
3970 :
3971 : static inline bool
3972 117585 : is_std_allocator_allocate (const constexpr_call *call)
3973 : {
3974 117585 : return (call
3975 107194 : && call->fundef
3976 224779 : && is_std_allocator_allocate (call->fundef->decl));
3977 : }
3978 :
3979 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3980 :
3981 : static bool
3982 651354 : is_std_allocator_allocate_deallocate (tree fndecl)
3983 : {
3984 651354 : tree name = DECL_NAME (fndecl);
3985 651354 : if (name == NULL_TREE
3986 651354 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3987 : return false;
3988 :
3989 604197 : return is_std_allocator (DECL_CONTEXT (fndecl));
3990 : }
3991 :
3992 : /* Overload for the above taking constexpr_call*. */
3993 :
3994 : static inline bool
3995 490264 : is_std_allocator_allocate_deallocate (const constexpr_call *call)
3996 : {
3997 490264 : return (call
3998 298220 : && call->fundef
3999 788484 : && is_std_allocator_allocate_deallocate (call->fundef->decl));
4000 : }
4001 :
4002 : /* Return true if FNDECL is std::source_location::current. */
4003 :
4004 : static inline bool
4005 58582 : is_std_source_location_current (tree fndecl)
4006 : {
4007 58582 : if (!decl_in_std_namespace_p (fndecl))
4008 : return false;
4009 :
4010 39395 : tree name = DECL_NAME (fndecl);
4011 39395 : if (name == NULL_TREE || !id_equal (name, "current"))
4012 : return false;
4013 :
4014 159 : tree ctx = DECL_CONTEXT (fndecl);
4015 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
4016 : return false;
4017 :
4018 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
4019 159 : return name && id_equal (name, "source_location");
4020 : }
4021 :
4022 : /* Overload for the above taking constexpr_call*. */
4023 :
4024 : static inline bool
4025 85280 : is_std_source_location_current (const constexpr_call *call)
4026 : {
4027 85280 : return (call
4028 58582 : && call->fundef
4029 143862 : && is_std_source_location_current (call->fundef->decl));
4030 : }
4031 :
4032 : /* Return true if FNDECL is __dynamic_cast. */
4033 :
4034 : static inline bool
4035 36553265 : cxx_dynamic_cast_fn_p (tree fndecl)
4036 : {
4037 36553265 : return (cxx_dialect >= cxx20
4038 34490419 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
4039 6788 : && CP_DECL_CONTEXT (fndecl) == abi_node
4040 : /* Only consider implementation-detail __dynamic_cast calls that
4041 : correspond to a dynamic_cast, and ignore direct calls to
4042 : abi::__dynamic_cast. */
4043 36560053 : && DECL_ARTIFICIAL (fndecl));
4044 : }
4045 :
4046 : /* Often, we have an expression in the form of address + offset, e.g.
4047 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
4048 :
4049 : static tree
4050 5840 : extract_obj_from_addr_offset (tree expr)
4051 : {
4052 5840 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
4053 2786 : expr = TREE_OPERAND (expr, 0);
4054 5840 : STRIP_NOPS (expr);
4055 5840 : if (TREE_CODE (expr) == ADDR_EXPR)
4056 5829 : expr = TREE_OPERAND (expr, 0);
4057 5840 : return expr;
4058 : }
4059 :
4060 : /* Given a PATH like
4061 :
4062 : g.D.2181.D.2154.D.2102.D.2093
4063 :
4064 : find a component with type TYPE. Return NULL_TREE if not found, and
4065 : error_mark_node if the component is not accessible. If STOP is non-null,
4066 : this function will return NULL_TREE if STOP is found before TYPE. */
4067 :
4068 : static tree
4069 2831 : get_component_with_type (tree path, tree type, tree stop)
4070 : {
4071 7921 : while (true)
4072 : {
4073 5376 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
4074 : /* Found it. */
4075 : return path;
4076 3761 : else if (stop
4077 3761 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
4078 : stop)))
4079 : return NULL_TREE;
4080 3716 : else if (TREE_CODE (path) == COMPONENT_REF
4081 3716 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
4082 : {
4083 : /* We need to check that the component we're accessing is in fact
4084 : accessible. */
4085 3716 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
4086 3716 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
4087 1171 : return error_mark_node;
4088 2545 : path = TREE_OPERAND (path, 0);
4089 : }
4090 : else
4091 : return NULL_TREE;
4092 : }
4093 : }
4094 :
4095 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
4096 :
4097 : The declaration of __dynamic_cast is:
4098 :
4099 : void* __dynamic_cast (const void* __src_ptr,
4100 : const __class_type_info* __src_type,
4101 : const __class_type_info* __dst_type,
4102 : ptrdiff_t __src2dst);
4103 :
4104 : where src2dst has the following possible values
4105 :
4106 : >-1: src_type is a unique public non-virtual base of dst_type
4107 : dst_ptr + src2dst == src_ptr
4108 : -1: unspecified relationship
4109 : -2: src_type is not a public base of dst_type
4110 : -3: src_type is a multiple public non-virtual base of dst_type */
4111 :
4112 : static tree
4113 3173 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
4114 : bool *non_constant_p, bool *overflow_p,
4115 : tree *jump_target)
4116 : {
4117 : /* T will be something like
4118 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
4119 : dismantle it. */
4120 3173 : gcc_assert (call_expr_nargs (call) == 4);
4121 3173 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
4122 3173 : tree obj = CALL_EXPR_ARG (call, 0);
4123 3173 : tree type = CALL_EXPR_ARG (call, 2);
4124 3173 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
4125 3173 : location_t loc = cp_expr_loc_or_input_loc (call);
4126 :
4127 : /* Get the target type of the dynamic_cast. */
4128 3173 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
4129 3173 : type = TREE_OPERAND (type, 0);
4130 3173 : type = TREE_TYPE (DECL_NAME (type));
4131 :
4132 : /* TYPE can only be either T* or T&. We can't know which of these it
4133 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
4134 : and something like "(T*)(T&)(T*) x" in the second case.
4135 : This is true for the reference cases in C++ < 26 or when exceptions
4136 : aren't enabled, in that case we should diagnose errors. For C++26
4137 : with exceptions we should silently evaluate to null pointer and
4138 : let the callers call __cxa_bad_cast () later to throw an exception. */
4139 3173 : bool fail_for_non_constant_p = false;
4140 12899 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
4141 : {
4142 9726 : if (cxx_dialect < cxx26 || !flag_exceptions)
4143 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
4144 9726 : obj = TREE_OPERAND (obj, 0);
4145 : }
4146 :
4147 : /* Evaluate the object so that we know its dynamic type. */
4148 3173 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
4149 : overflow_p, jump_target);
4150 3173 : if (*non_constant_p)
4151 : return call;
4152 3054 : if (*jump_target)
4153 : return NULL_TREE;
4154 :
4155 : /* For dynamic_cast from classes with virtual bases we can get something
4156 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
4157 : d.D.1234 using cxx_fold_indirect_ref. */
4158 3054 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
4159 : {
4160 1206 : tree objo = obj;
4161 1206 : STRIP_NOPS (objo);
4162 1206 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
4163 : {
4164 1152 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
4165 : obj, NULL, jump_target);
4166 1152 : if (objo)
4167 1152 : obj = build_fold_addr_expr (objo);
4168 : }
4169 : }
4170 :
4171 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
4172 : but when HINT is > 0, it can also be something like
4173 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
4174 3054 : obj = extract_obj_from_addr_offset (obj);
4175 3054 : const tree objtype = TREE_TYPE (obj);
4176 : /* If OBJ doesn't refer to a base field, we're done. */
4177 6029 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
4178 3054 : ? TREE_OPERAND (obj, 1) : obj))
4179 3054 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
4180 : {
4181 79 : if (fail_for_non_constant_p)
4182 : {
4183 55 : if (!ctx->quiet)
4184 : {
4185 2 : auto_diagnostic_group d;
4186 2 : error_at (loc, "reference %<dynamic_cast%> failed");
4187 2 : inform (loc, "dynamic type %qT of its operand does "
4188 : "not have a base class of type %qT",
4189 : objtype, type);
4190 2 : }
4191 55 : *non_constant_p = true;
4192 : }
4193 79 : return integer_zero_node;
4194 : }
4195 :
4196 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
4197 : or in a destructor ... if the operand of the dynamic_cast refers
4198 : to the object under construction or destruction, this object is
4199 : considered to be a most derived object that has the type of the
4200 : constructor or destructor's class. */
4201 2975 : tree vtable = build_vfield_ref (obj, objtype);
4202 2975 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
4203 : non_constant_p, overflow_p,
4204 : jump_target);
4205 2975 : if (*non_constant_p)
4206 : return call;
4207 2798 : if (*jump_target)
4208 : return NULL_TREE;
4209 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
4210 : so it's possible that we got a null pointer now. */
4211 2798 : if (integer_zerop (vtable))
4212 : {
4213 12 : if (!ctx->quiet)
4214 3 : error_at (loc, "virtual table pointer is used uninitialized");
4215 12 : *non_constant_p = true;
4216 12 : return integer_zero_node;
4217 : }
4218 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
4219 2786 : vtable = extract_obj_from_addr_offset (vtable);
4220 2786 : const tree mdtype = DECL_CONTEXT (vtable);
4221 :
4222 : /* Given dynamic_cast<T>(v),
4223 :
4224 : [expr.dynamic.cast] If C is the class type to which T points or refers,
4225 : the runtime check logically executes as follows:
4226 :
4227 : If, in the most derived object pointed (referred) to by v, v points
4228 : (refers) to a public base class subobject of a C object, and if only
4229 : one object of type C is derived from the subobject pointed (referred)
4230 : to by v the result points (refers) to that C object.
4231 :
4232 : In this case, HINT >= 0 or -3. */
4233 2786 : if (hint >= 0 || hint == -3)
4234 : {
4235 : /* Look for a component with type TYPE. */
4236 687 : tree t = get_component_with_type (obj, type, mdtype);
4237 : /* If not accessible, give an error. */
4238 687 : if (t == error_mark_node)
4239 : {
4240 198 : if (fail_for_non_constant_p)
4241 : {
4242 108 : if (!ctx->quiet)
4243 : {
4244 12 : auto_diagnostic_group d;
4245 12 : error_at (loc, "reference %<dynamic_cast%> failed");
4246 12 : inform (loc, "static type %qT of its operand is a "
4247 : "non-public base class of dynamic type %qT",
4248 : objtype, type);
4249 :
4250 12 : }
4251 108 : *non_constant_p = true;
4252 : }
4253 198 : return integer_zero_node;
4254 : }
4255 489 : else if (t)
4256 : /* The result points to the TYPE object. */
4257 444 : return cp_build_addr_expr (t, complain);
4258 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
4259 : Fall through to the normal processing. */
4260 : }
4261 :
4262 : /* Otherwise, if v points (refers) to a public base class subobject of the
4263 : most derived object, and the type of the most derived object has a base
4264 : class, of type C, that is unambiguous and public, the result points
4265 : (refers) to the C subobject of the most derived object.
4266 :
4267 : But it can also be an invalid case. */
4268 :
4269 : /* Get the most derived object. */
4270 2144 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
4271 2144 : if (obj == error_mark_node)
4272 : {
4273 973 : if (fail_for_non_constant_p)
4274 : {
4275 350 : if (!ctx->quiet)
4276 : {
4277 38 : auto_diagnostic_group d;
4278 38 : error_at (loc, "reference %<dynamic_cast%> failed");
4279 38 : inform (loc, "static type %qT of its operand is a non-public"
4280 : " base class of dynamic type %qT", objtype, mdtype);
4281 38 : }
4282 350 : *non_constant_p = true;
4283 : }
4284 973 : return integer_zero_node;
4285 : }
4286 : else
4287 1171 : gcc_assert (obj);
4288 :
4289 : /* Check that the type of the most derived object has a base class
4290 : of type TYPE that is unambiguous and public. */
4291 1171 : base_kind b_kind;
4292 1171 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
4293 1171 : if (!binfo || binfo == error_mark_node)
4294 : {
4295 441 : if (fail_for_non_constant_p)
4296 : {
4297 134 : if (!ctx->quiet)
4298 : {
4299 16 : auto_diagnostic_group d;
4300 16 : error_at (loc, "reference %<dynamic_cast%> failed");
4301 16 : if (b_kind == bk_ambig)
4302 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
4303 : "type %qT of its operand", type, mdtype);
4304 : else
4305 10 : inform (loc, "dynamic type %qT of its operand does not "
4306 : "have an unambiguous public base class %qT",
4307 : mdtype, type);
4308 16 : }
4309 134 : *non_constant_p = true;
4310 : }
4311 441 : return integer_zero_node;
4312 : }
4313 : /* If so, return the TYPE subobject of the most derived object. */
4314 730 : obj = convert_to_base_statically (obj, binfo);
4315 730 : return cp_build_addr_expr (obj, complain);
4316 : }
4317 :
4318 : /* Data structure used by replace_decl and replace_decl_r. */
4319 :
4320 : struct replace_decl_data
4321 : {
4322 : /* The _DECL we want to replace. */
4323 : tree decl;
4324 : /* The replacement for DECL. */
4325 : tree replacement;
4326 : /* Trees we've visited. */
4327 : hash_set<tree> *pset;
4328 : /* Whether we've performed any replacements. */
4329 : bool changed;
4330 : };
4331 :
4332 : /* Helper function for replace_decl, called through cp_walk_tree. */
4333 :
4334 : static tree
4335 16999156 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
4336 : {
4337 16999156 : replace_decl_data *d = (replace_decl_data *) data;
4338 :
4339 : /* We could be replacing
4340 : &<retval>.bar -> &foo.bar
4341 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
4342 : on the ADDR_EXPR around it. */
4343 16999156 : if (TREE_CODE (*tp) == ADDR_EXPR)
4344 : {
4345 1530123 : d->pset->add (*tp);
4346 1530123 : auto save_changed = d->changed;
4347 1530123 : d->changed = false;
4348 1530123 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
4349 1530123 : if (d->changed)
4350 : {
4351 1855 : cxx_mark_addressable (*tp);
4352 1855 : recompute_tree_invariant_for_addr_expr (*tp);
4353 : }
4354 : else
4355 1528268 : d->changed = save_changed;
4356 1530123 : *walk_subtrees = 0;
4357 : }
4358 15469033 : else if (*tp == d->decl)
4359 : {
4360 2095 : *tp = unshare_expr (d->replacement);
4361 2095 : d->changed = true;
4362 2095 : *walk_subtrees = 0;
4363 : }
4364 15466938 : else if (TYPE_P (*tp)
4365 15466938 : || d->pset->add (*tp))
4366 2819441 : *walk_subtrees = 0;
4367 :
4368 16999156 : return NULL_TREE;
4369 : }
4370 :
4371 : /* Replace every occurrence of DECL with (an unshared copy of)
4372 : REPLACEMENT within the expression *TP. Returns true iff a
4373 : replacement was performed. */
4374 :
4375 : bool
4376 2265988 : replace_decl (tree *tp, tree decl, tree replacement)
4377 : {
4378 2265988 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
4379 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
4380 2265988 : hash_set<tree> pset;
4381 2265988 : replace_decl_data data = { decl, replacement, &pset, false };
4382 2265988 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
4383 2265988 : return data.changed;
4384 2265988 : }
4385 :
4386 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
4387 :
4388 : static tree
4389 44 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
4390 : value_cat lval,
4391 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
4392 : {
4393 44 : tree function = THUNK_TARGET (thunk_fndecl);
4394 :
4395 44 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
4396 : {
4397 13 : if (!ctx->quiet)
4398 : {
4399 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
4400 : {
4401 0 : error ("call to non-%<constexpr%> function %qD", function);
4402 0 : explain_invalid_constexpr_fn (function);
4403 : }
4404 : else
4405 : /* virtual_offset is only set for virtual bases, which make the
4406 : class non-literal, so we don't need to handle it here. */
4407 3 : error ("calling constexpr member function %qD through virtual "
4408 : "base subobject", function);
4409 : }
4410 13 : *non_constant_p = true;
4411 13 : return t;
4412 : }
4413 :
4414 31 : tree new_call = copy_node (t);
4415 31 : CALL_EXPR_FN (new_call) = function;
4416 31 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
4417 :
4418 31 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
4419 :
4420 31 : if (DECL_THIS_THUNK_P (thunk_fndecl))
4421 : {
4422 : /* 'this'-adjusting thunk. */
4423 19 : tree this_arg = CALL_EXPR_ARG (t, 0);
4424 19 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
4425 : this_arg, offset);
4426 19 : CALL_EXPR_ARG (new_call, 0) = this_arg;
4427 : }
4428 : else
4429 : {
4430 : /* Return-adjusting thunk. As in expand_thunk, adjust a pointer only
4431 : if it is non-null. */
4432 12 : tree call = new_call;
4433 12 : if (TYPE_PTR_P (TREE_TYPE (call)))
4434 12 : call = save_expr (call);
4435 12 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (call), call, offset);
4436 12 : if (TYPE_PTR_P (TREE_TYPE (call)))
4437 12 : new_call = build_if_nonnull (call, new_call, tf_none);
4438 : }
4439 :
4440 31 : tree result = cxx_eval_constant_expression (ctx, new_call, lval,
4441 : non_constant_p, overflow_p,
4442 : jump_target);
4443 31 : if (!*non_constant_p
4444 27 : && !*overflow_p
4445 27 : && !*jump_target
4446 27 : && result != void_node
4447 27 : && INDIRECT_TYPE_P (TREE_TYPE (t))
4448 55 : && !(same_type_ignoring_top_level_qualifiers_p
4449 24 : (TREE_TYPE (result), TREE_TYPE (t))))
4450 15 : result = adjust_temp_type (TREE_TYPE (t), result);
4451 : return result;
4452 : }
4453 :
4454 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
4455 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
4456 : 'tors to detect modifying const objects in a constexpr context. */
4457 :
4458 : static void
4459 11053988 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
4460 : bool readonly_p, bool *non_constant_p,
4461 : bool *overflow_p, tree *jump_target)
4462 : {
4463 22107976 : if (CLASS_TYPE_P (TREE_TYPE (object))
4464 22107976 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
4465 : {
4466 : /* Subobjects might not be stored in ctx->global->values but we
4467 : can get its CONSTRUCTOR by evaluating *this. */
4468 1027496 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
4469 : non_constant_p, overflow_p,
4470 : jump_target);
4471 1027496 : if (!*non_constant_p
4472 12039212 : && !throws (jump_target)
4473 2012720 : && TREE_CODE (e) == CONSTRUCTOR)
4474 985224 : TREE_READONLY (e) = readonly_p;
4475 : }
4476 11053988 : }
4477 :
4478 : /* Allocate an exception for OBJECT and throw it. */
4479 :
4480 : tree
4481 3242 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
4482 : tree object)
4483 : {
4484 3242 : tree type = TREE_TYPE (object);
4485 : /* This simulates a call to __cxa_allocate_exception. We need
4486 : (struct exception *) &heap -- memory on the heap so that
4487 : it can survive the stack being unwound. */
4488 3242 : tree arr = build_array_of_n_type (type, 1);
4489 3242 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
4490 3242 : DECL_NAME (var) = heap_identifier;
4491 3242 : ctx->global->put_value (var, NULL_TREE);
4492 :
4493 : /* *(struct exception *) &heap = exc{ ... } */
4494 3242 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
4495 3242 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
4496 3242 : bool non_constant_p = false, overflow_p = false;
4497 3242 : tree jump_target = NULL_TREE;
4498 3242 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
4499 : &overflow_p, &jump_target);
4500 3242 : if (non_constant_p)
4501 : {
4502 0 : if (!ctx->quiet)
4503 0 : error_at (loc, "couldn%'t throw %qT", type);
4504 : return NULL_TREE;
4505 : }
4506 :
4507 : /* Now we can __cxa_throw. */
4508 3242 : DECL_EXCEPTION_REFCOUNT (var)
4509 3242 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
4510 3242 : ++ctx->global->uncaught_exceptions;
4511 :
4512 3242 : return var;
4513 : }
4514 :
4515 : /* Subroutine of cxx_eval_constant_expression.
4516 : Evaluate the call expression tree T in the context of OLD_CALL expression
4517 : evaluation. */
4518 :
4519 : static tree
4520 205757673 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
4521 : value_cat lval,
4522 : bool *non_constant_p, bool *overflow_p,
4523 : tree *jump_target)
4524 : {
4525 205757673 : location_t loc = cp_expr_loc_or_input_loc (t);
4526 205757673 : tree fun = get_function_named_in_call (t);
4527 :
4528 205757673 : if (fun == NULL_TREE)
4529 383909 : return cxx_eval_internal_function (ctx, t, lval,
4530 : non_constant_p, overflow_p,
4531 383909 : jump_target);
4532 :
4533 205373764 : if (TREE_CODE (fun) != FUNCTION_DECL)
4534 : {
4535 : /* Might be a constexpr function pointer. */
4536 410839 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
4537 : non_constant_p, overflow_p,
4538 : jump_target);
4539 410839 : if (*jump_target)
4540 : return NULL_TREE;
4541 410839 : STRIP_NOPS (fun);
4542 410839 : if (TREE_CODE (fun) == ADDR_EXPR)
4543 30596 : fun = TREE_OPERAND (fun, 0);
4544 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
4545 : indirection, the called expression is a pointer into the
4546 : virtual table which should contain FDESC_EXPR. Extract the
4547 : FUNCTION_DECL from there. */
4548 : else if (TARGET_VTABLE_USES_DESCRIPTORS
4549 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
4550 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
4551 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
4552 : {
4553 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
4554 : if (VAR_P (d)
4555 : && DECL_VTABLE_OR_VTT_P (d)
4556 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
4557 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
4558 : && DECL_INITIAL (d)
4559 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
4560 : {
4561 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
4562 : TYPE_SIZE_UNIT (vtable_entry_type));
4563 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
4564 : if (idx >= 0)
4565 : {
4566 : tree fdesc
4567 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
4568 : if (TREE_CODE (fdesc) == FDESC_EXPR
4569 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
4570 : fun = TREE_OPERAND (fdesc, 0);
4571 : }
4572 : }
4573 : }
4574 : }
4575 205373764 : if (TREE_CODE (fun) != FUNCTION_DECL)
4576 : {
4577 380243 : if (!ctx->quiet && !*non_constant_p)
4578 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
4579 : "function", fun);
4580 380243 : *non_constant_p = true;
4581 380243 : return t;
4582 : }
4583 204993521 : tree orig_fun = fun;
4584 204993521 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
4585 24290042 : fun = DECL_CLONED_FUNCTION (fun);
4586 :
4587 204993521 : if (is_ubsan_builtin_p (fun))
4588 58 : return void_node;
4589 :
4590 204993463 : if (fndecl_built_in_p (fun))
4591 20514983 : return cxx_eval_builtin_function_call (ctx, t, fun,
4592 : lval, non_constant_p, overflow_p,
4593 20514981 : jump_target);
4594 184478480 : if (DECL_THUNK_P (fun))
4595 44 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
4596 44 : jump_target);
4597 184478436 : if (metafunction_p (fun))
4598 : {
4599 : /* To be able to evaluate a metafunction, we may have to instantiate
4600 : constexpr functions. If we're not allowed to instantiate, leave
4601 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4602 : otherwise we might fold those prematurely. See
4603 : g++.dg/reflect/p2996-17.C. */
4604 81777 : if (uid_sensitive_constexpr_evaluation_p ()
4605 81205 : || ctx->manifestly_const_eval == mce_unknown)
4606 : {
4607 21777 : *non_constant_p = true;
4608 21777 : return t;
4609 : }
4610 60000 : ctx->global->state_dependent = true;
4611 60000 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4612 : jump_target);
4613 60000 : if (*jump_target)
4614 : return NULL_TREE;
4615 56746 : if (*non_constant_p)
4616 : return t;
4617 55676 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4618 : non_constant_p, overflow_p,
4619 : jump_target);
4620 55676 : if (*jump_target)
4621 : return NULL_TREE;
4622 55676 : if (*non_constant_p)
4623 : return t;
4624 55666 : return e;
4625 : }
4626 184396659 : bool non_constexpr_call = false;
4627 184396659 : if (!maybe_constexpr_fn (fun))
4628 : {
4629 890511 : if (TREE_CODE (t) == CALL_EXPR
4630 882846 : && cxx_replaceable_global_alloc_fn (fun)
4631 1543145 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4632 324233 : || is_std_allocator_allocate_deallocate (ctx->call)))
4633 : {
4634 487288 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4635 487288 : const int nargs = call_expr_nargs (t);
4636 487288 : tree arg0 = NULL_TREE;
4637 774840 : for (int i = 0; i < nargs; ++i)
4638 : {
4639 488357 : tree arg = CALL_EXPR_ARG (t, i);
4640 488357 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4641 : non_constant_p, overflow_p,
4642 : jump_target);
4643 488357 : if (*jump_target)
4644 : return NULL_TREE;
4645 : /* Deleting a non-constant pointer has a better error message
4646 : below. */
4647 488347 : if (new_op_p || i != 0)
4648 409226 : VERIFY_CONSTANT (arg);
4649 208431 : if (i == 0)
4650 : arg0 = arg;
4651 : }
4652 286483 : gcc_assert (arg0);
4653 286483 : if (new_op_p)
4654 : {
4655 207362 : if (!tree_fits_uhwi_p (arg0))
4656 : {
4657 : /* We should not get here; the VERIFY_CONSTANT above
4658 : should have already caught it. */
4659 0 : gcc_checking_assert (false);
4660 : if (!ctx->quiet)
4661 : error_at (loc, "cannot allocate array: size not constant");
4662 : *non_constant_p = true;
4663 : return t;
4664 : }
4665 414724 : tree type = build_array_type_nelts (char_type_node,
4666 207362 : tree_to_uhwi (arg0));
4667 207362 : tree var = build_decl (loc, VAR_DECL,
4668 207362 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4669 : & OVL_OP_FLAG_VEC)
4670 : ? heap_vec_uninit_identifier
4671 : : heap_uninit_identifier,
4672 207362 : type);
4673 207362 : DECL_ARTIFICIAL (var) = 1;
4674 207362 : ctx->global->heap_vars.safe_push (var);
4675 207362 : ctx->global->put_value (var, NULL_TREE);
4676 207362 : return fold_convert (ptr_type_node, build_address (var));
4677 : }
4678 : else
4679 : {
4680 79121 : STRIP_NOPS (arg0);
4681 79121 : if (TREE_CODE (arg0) == ADDR_EXPR
4682 79121 : && VAR_P (TREE_OPERAND (arg0, 0)))
4683 : {
4684 79121 : tree var = TREE_OPERAND (arg0, 0);
4685 79121 : if (DECL_NAME (var) == heap_uninit_identifier
4686 79121 : || DECL_NAME (var) == heap_identifier)
4687 : {
4688 78947 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4689 : & OVL_OP_FLAG_VEC)
4690 : {
4691 9 : if (!ctx->quiet)
4692 : {
4693 3 : auto_diagnostic_group d;
4694 3 : error_at (loc, "array deallocation of object "
4695 : "allocated with non-array "
4696 : "allocation");
4697 3 : inform (DECL_SOURCE_LOCATION (var),
4698 : "allocation performed here");
4699 3 : }
4700 9 : *non_constant_p = true;
4701 9 : return t;
4702 : }
4703 78938 : DECL_NAME (var) = heap_deleted_identifier;
4704 78938 : ctx->global->destroy_value (var);
4705 78938 : ctx->global->heap_dealloc_count++;
4706 78938 : return void_node;
4707 : }
4708 174 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4709 174 : || DECL_NAME (var) == heap_vec_identifier)
4710 : {
4711 150 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4712 : & OVL_OP_FLAG_VEC) == 0)
4713 : {
4714 9 : if (!ctx->quiet)
4715 : {
4716 3 : auto_diagnostic_group d;
4717 3 : error_at (loc, "non-array deallocation of "
4718 : "object allocated with array "
4719 : "allocation");
4720 3 : inform (DECL_SOURCE_LOCATION (var),
4721 : "allocation performed here");
4722 3 : }
4723 9 : *non_constant_p = true;
4724 9 : return t;
4725 : }
4726 141 : DECL_NAME (var) = heap_deleted_identifier;
4727 141 : ctx->global->destroy_value (var);
4728 141 : ctx->global->heap_dealloc_count++;
4729 141 : return void_node;
4730 : }
4731 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4732 : {
4733 15 : if (!ctx->quiet)
4734 6 : error_at (loc, "deallocation of already deallocated "
4735 : "storage");
4736 15 : *non_constant_p = true;
4737 15 : return t;
4738 : }
4739 : }
4740 9 : if (!ctx->quiet)
4741 3 : error_at (loc, "deallocation of storage that was "
4742 : "not previously allocated");
4743 9 : *non_constant_p = true;
4744 9 : return t;
4745 : }
4746 : }
4747 : /* Allow placement new in std::construct_at, just return the second
4748 : argument. */
4749 403223 : if (TREE_CODE (t) == CALL_EXPR
4750 395558 : && cxx_placement_new_fn (fun)
4751 579744 : && is_std_construct_at (ctx->call))
4752 : {
4753 46029 : const int nargs = call_expr_nargs (t);
4754 46029 : tree arg1 = NULL_TREE;
4755 138087 : for (int i = 0; i < nargs; ++i)
4756 : {
4757 92058 : tree arg = CALL_EXPR_ARG (t, i);
4758 92058 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4759 : non_constant_p, overflow_p,
4760 : jump_target);
4761 92058 : if (*jump_target)
4762 : return NULL_TREE;
4763 92058 : if (i == 1)
4764 : arg1 = arg;
4765 : else
4766 92058 : VERIFY_CONSTANT (arg);
4767 : }
4768 46029 : gcc_assert (arg1);
4769 : return arg1;
4770 : }
4771 357194 : else if (cxx_dynamic_cast_fn_p (fun))
4772 3173 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4773 3173 : jump_target);
4774 354021 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4775 29790 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4776 : non_constant_p, overflow_p,
4777 29790 : jump_target);
4778 :
4779 : /* Calls to non-constexpr functions can be diagnosed right away
4780 : before C++26, though in C++26 evaluation of the arguments might
4781 : throw and if caught it could be still constant expression.
4782 : So for C++26 this is diagnosed only after
4783 : cxx_bind_parameters_in_call. */
4784 324231 : if (cxx_dialect >= cxx26)
4785 : non_constexpr_call = true;
4786 : else
4787 : {
4788 283073 : if (!ctx->quiet)
4789 : {
4790 210 : if (!lambda_static_thunk_p (fun))
4791 210 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4792 210 : explain_invalid_constexpr_fn (fun);
4793 : }
4794 283073 : *non_constant_p = true;
4795 283073 : return t;
4796 : }
4797 : }
4798 :
4799 183547306 : constexpr_ctx new_ctx = *ctx;
4800 183547306 : ctx = &new_ctx;
4801 206369746 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4802 187486968 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4803 : {
4804 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4805 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4806 2783 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4807 2783 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4808 2783 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4809 2783 : ctx->global->put_value (new_ctx.object, ctor);
4810 : }
4811 : /* An immediate invocation is manifestly constant evaluated including the
4812 : arguments of the call, so use mce_true even for the argument
4813 : evaluation. */
4814 367094612 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4815 3446122 : new_ctx.manifestly_const_eval = mce_true;
4816 :
4817 : /* We used to shortcut trivial constructor/op= here, but nowadays
4818 : we can only get a trivial function here with -fno-elide-constructors. */
4819 183547442 : gcc_checking_assert (!trivial_fn_p (fun)
4820 : || !flag_elide_constructors
4821 : /* Or it's a call from maybe_thunk_body (111075). */
4822 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4823 : : AGGR_INIT_FROM_THUNK_P (t))
4824 : /* We don't elide constructors when processing
4825 : a noexcept-expression. */
4826 : || cp_noexcept_operand);
4827 :
4828 183547306 : bool non_constant_args = false;
4829 183547306 : constexpr_call new_call;
4830 183547306 : new_call.bindings
4831 183547306 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4832 : overflow_p, &non_constant_args,
4833 : jump_target);
4834 :
4835 : /* We build up the bindings list before we know whether we already have this
4836 : call cached. If we don't end up saving these bindings, ggc_free them when
4837 : this function exits. */
4838 183547306 : class free_bindings
4839 : {
4840 : tree *bindings;
4841 : public:
4842 183547306 : free_bindings (tree &b): bindings (&b) { }
4843 183547302 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4844 4607346 : void preserve () { bindings = NULL; }
4845 183547306 : } fb (new_call.bindings);
4846 :
4847 183547306 : if (*jump_target)
4848 : return NULL_TREE;
4849 183547222 : if (*non_constant_p)
4850 : return t;
4851 109955074 : if (non_constexpr_call)
4852 : {
4853 6444 : if (!ctx->quiet)
4854 : {
4855 440 : if (!lambda_static_thunk_p (fun))
4856 440 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4857 440 : explain_invalid_constexpr_fn (fun);
4858 : }
4859 6444 : *non_constant_p = true;
4860 6444 : return t;
4861 : }
4862 :
4863 : /* We can't defer instantiating the function any longer. */
4864 109948630 : if (!DECL_INITIAL (fun)
4865 3108482 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4866 109948630 : && !uid_sensitive_constexpr_evaluation_p ())
4867 : {
4868 2869128 : location_t save_loc = input_location;
4869 2869128 : input_location = loc;
4870 2869128 : ++function_depth;
4871 2869128 : if (ctx->manifestly_const_eval == mce_true)
4872 367397 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4873 2869128 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4874 2869114 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4875 : else
4876 14 : synthesize_method (fun);
4877 2869128 : --function_depth;
4878 2869128 : input_location = save_loc;
4879 : }
4880 :
4881 : /* If in direct recursive call, optimize definition search. */
4882 109948630 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4883 29287 : new_call.fundef = ctx->call->fundef;
4884 : else
4885 : {
4886 109919343 : new_call.fundef = retrieve_constexpr_fundef (fun);
4887 109919343 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4888 109355581 : || new_call.fundef->result == error_mark_node
4889 109339673 : || fun == current_function_decl)
4890 : {
4891 579676 : if (!ctx->quiet)
4892 : {
4893 : /* We need to check for current_function_decl here in case we're
4894 : being called during cp_fold_function, because at that point
4895 : DECL_INITIAL is set properly and we have a fundef but we
4896 : haven't lowered invisirefs yet (c++/70344). */
4897 187 : if (DECL_INITIAL (fun) == error_mark_node
4898 187 : || fun == current_function_decl)
4899 15 : error_at (loc, "%qD called in a constant expression before its "
4900 : "definition is complete", fun);
4901 172 : else if (DECL_INITIAL (fun))
4902 : {
4903 : /* The definition of fun was somehow unsuitable. But pretend
4904 : that lambda static thunks don't exist. */
4905 134 : if (!lambda_static_thunk_p (fun))
4906 122 : error_at (loc, "%qD called in a constant expression", fun);
4907 134 : explain_invalid_constexpr_fn (fun);
4908 : }
4909 : else
4910 38 : error_at (loc, "%qD used before its definition", fun);
4911 : }
4912 579676 : *non_constant_p = true;
4913 579676 : return t;
4914 : }
4915 : }
4916 :
4917 : /* Don't complain about problems evaluating an ill-formed function. */
4918 109368954 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4919 109368954 : if (f->language->erroneous)
4920 1005 : new_ctx.quiet = true;
4921 :
4922 109368954 : int depth_ok = push_cx_call_context (t);
4923 :
4924 : /* Remember the object we are constructing or destructing. */
4925 109368954 : tree new_obj = NULL_TREE;
4926 218737908 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4927 : {
4928 : /* In a cdtor, it should be the first `this' argument.
4929 : At this point it has already been evaluated in the call
4930 : to cxx_bind_parameters_in_call. */
4931 13632663 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4932 13632663 : bool empty_base = false;
4933 13632663 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4934 : &empty_base, jump_target);
4935 13632663 : if (*jump_target)
4936 0 : return NULL_TREE;
4937 : /* If we're initializing an empty class, don't set constness, because
4938 : cxx_fold_indirect_ref will return the wrong object to set constness
4939 : of. */
4940 13632663 : if (empty_base)
4941 : new_obj = NULL_TREE;
4942 6844683 : else if (ctx->call && ctx->call->fundef
4943 24801493 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4944 : {
4945 3570968 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4946 3570968 : STRIP_NOPS (cur_obj);
4947 3570968 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4948 3568485 : cur_obj = TREE_OPERAND (cur_obj, 0);
4949 3570968 : if (new_obj == cur_obj)
4950 : /* We're calling the target constructor of a delegating
4951 : constructor, or accessing a base subobject through a
4952 : NOP_EXPR as part of a call to a base constructor, so
4953 : there is no new (sub)object. */
4954 13632663 : new_obj = NULL_TREE;
4955 : }
4956 : }
4957 :
4958 109368954 : tree result = NULL_TREE;
4959 :
4960 109368954 : constexpr_call *entry = NULL;
4961 109368954 : if (depth_ok && !non_constant_args && ctx->strict)
4962 : {
4963 36834424 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4964 36834424 : new_call.hash
4965 36834424 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4966 :
4967 : /* If we have seen this call before, we are done. */
4968 36834424 : maybe_initialize_constexpr_call_table ();
4969 36834424 : bool insert = depth_ok < constexpr_cache_depth;
4970 36834424 : constexpr_call **slot
4971 50196687 : = constexpr_call_table->find_slot (&new_call,
4972 : insert ? INSERT : NO_INSERT);
4973 36834424 : entry = slot ? *slot : NULL;
4974 32281462 : if (entry == NULL)
4975 : {
4976 : /* Only cache up to constexpr_cache_depth to limit memory use. */
4977 9160308 : if (insert)
4978 : {
4979 : /* We need to keep a pointer to the entry, not just the slot, as
4980 : the slot can move during evaluation of the body. */
4981 4607346 : *slot = entry = ggc_alloc<constexpr_call> ();
4982 4607346 : *entry = new_call;
4983 4607346 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4984 4607346 : fb.preserve ();
4985 :
4986 : /* Unshare args going into the hash table to separate them
4987 : from the caller's context, for better GC and to avoid
4988 : problems with verify_gimple. */
4989 4607346 : tree bound = new_call.bindings;
4990 7450493 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4991 : {
4992 2843147 : tree &arg = TREE_VEC_ELT (bound, i);
4993 2843147 : arg = unshare_expr_without_location (arg);
4994 : }
4995 : }
4996 : }
4997 : /* Calls that are in progress have their result set to unknown_type_node,
4998 : so that we can detect circular dependencies. Now that we only cache
4999 : up to constexpr_cache_depth this won't catch circular dependencies that
5000 : start deeper, but they'll hit the recursion or ops limit. */
5001 27674116 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
5002 : {
5003 6 : if (!ctx->quiet)
5004 0 : error ("call has circular dependency");
5005 6 : *non_constant_p = true;
5006 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
5007 : }
5008 : else
5009 27674110 : result = entry->result (ctx->manifestly_const_eval);
5010 : }
5011 :
5012 109368921 : if (!depth_ok)
5013 : {
5014 33 : if (!ctx->quiet)
5015 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
5016 : "%<-fconstexpr-depth=%> to increase the maximum)",
5017 : max_constexpr_depth);
5018 33 : *non_constant_p = true;
5019 33 : result = error_mark_node;
5020 : }
5021 : else
5022 : {
5023 109368921 : bool cacheable = !!entry;
5024 109368921 : if (result && result != error_mark_node)
5025 : /* OK */;
5026 86763389 : else if (!DECL_SAVED_TREE (fun))
5027 : {
5028 : /* When at_eof >= 3, cgraph has started throwing away
5029 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
5030 : late code generation for VEC_INIT_EXPR, which needs to be
5031 : completely reconsidered. */
5032 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
5033 0 : *non_constant_p = true;
5034 : }
5035 86763389 : else if (tree copy = get_fundef_copy (new_call.fundef))
5036 : {
5037 86763380 : tree body, parms, res;
5038 86763380 : releasing_vec ctors;
5039 :
5040 : /* Reuse or create a new unshared copy of this function's body. */
5041 86763380 : body = TREE_PURPOSE (copy);
5042 86763380 : parms = TREE_VALUE (copy);
5043 86763380 : res = TREE_TYPE (copy);
5044 :
5045 : /* Associate the bindings with the remapped parms. */
5046 86763380 : tree bound = new_call.bindings;
5047 86763380 : tree remapped = parms;
5048 212684765 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
5049 : {
5050 125921385 : tree arg = TREE_VEC_ELT (bound, i);
5051 125921385 : if (entry)
5052 : {
5053 : /* Unshare again so the callee doesn't change the
5054 : argument values in the hash table. XXX Could we unshare
5055 : lazily in cxx_eval_store_expression? */
5056 3083556 : arg = unshare_constructor (arg);
5057 3083556 : if (TREE_CODE (arg) == CONSTRUCTOR)
5058 871615 : vec_safe_push (ctors, arg);
5059 : }
5060 125921385 : ctx->global->put_value (remapped, arg);
5061 125921385 : remapped = DECL_CHAIN (remapped);
5062 : }
5063 86763380 : if (remapped)
5064 : {
5065 : /* We shouldn't have any parms without args, but fail gracefully
5066 : in error recovery. */
5067 6 : gcc_checking_assert (seen_error ());
5068 6 : *non_constant_p = true;
5069 : }
5070 : /* Add the RESULT_DECL to the values map, too. */
5071 86763380 : gcc_assert (!DECL_BY_REFERENCE (res));
5072 86763380 : ctx->global->put_value (res, NULL_TREE);
5073 :
5074 : /* Remember the current call we're evaluating. */
5075 86763380 : constexpr_ctx call_ctx = *ctx;
5076 86763380 : call_ctx.call = &new_call;
5077 86763380 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
5078 86763380 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
5079 86763380 : bool save_state_dependent = ctx->global->state_dependent;
5080 :
5081 : /* Make sure we fold std::is_constant_evaluated to true in an
5082 : immediate function. */
5083 173526760 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
5084 1437442 : call_ctx.manifestly_const_eval = mce_true;
5085 :
5086 : /* If this is a constexpr destructor, the object's const and volatile
5087 : semantics are no longer in effect; see [class.dtor]p5. */
5088 97817368 : if (new_obj && DECL_DESTRUCTOR_P (fun))
5089 1235545 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
5090 : non_constant_p, overflow_p, jump_target);
5091 :
5092 : /* If this is a constructor, we are beginning the lifetime of the
5093 : object we are initializing. */
5094 86763380 : if (new_obj
5095 22107976 : && DECL_CONSTRUCTOR_P (fun)
5096 9818443 : && TREE_CODE (new_obj) == COMPONENT_REF
5097 89574339 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
5098 : {
5099 8916 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
5100 8916 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
5101 8916 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
5102 : new_obj, ctor);
5103 8916 : cxx_eval_constant_expression (ctx, activate,
5104 : lval, non_constant_p, overflow_p,
5105 : jump_target);
5106 8916 : ggc_free (activate);
5107 8916 : if (*jump_target)
5108 0 : return NULL_TREE;
5109 : }
5110 :
5111 86763380 : ctx->global->state_dependent = false;
5112 :
5113 86763380 : tree jmp_target = NULL_TREE;
5114 86763380 : cxx_eval_constant_expression (&call_ctx, body,
5115 : vc_discard, non_constant_p, overflow_p,
5116 : &jmp_target);
5117 :
5118 86763376 : if (!*non_constant_p && throws (&jmp_target))
5119 : {
5120 4701 : result = NULL_TREE;
5121 4701 : cacheable = false;
5122 4701 : *jump_target = jmp_target;
5123 : }
5124 86758675 : else if (!*non_constant_p && TREE_THIS_VOLATILE (fun))
5125 : {
5126 : /* Return from a [[noreturn]] function. */
5127 11 : if (!ctx->quiet)
5128 5 : error ("%<[[noreturn]]%> call returns");
5129 11 : *non_constant_p = true;
5130 : }
5131 173517328 : else if (DECL_CONSTRUCTOR_P (fun))
5132 : /* This can be null for a subobject constructor call, in
5133 : which case what we care about is the initialization
5134 : side-effects rather than the value. We could get at the
5135 : value by evaluating *this, but we don't bother; there's
5136 : no need to put such a call in the hash table. */
5137 12229428 : result = lval ? ctx->object : ctx->ctor;
5138 74529236 : else if (VOID_TYPE_P (TREE_TYPE (res)))
5139 7301656 : result = void_node;
5140 : else
5141 : {
5142 67227580 : result = ctx->global->get_value (res);
5143 10879804 : if (result == NULL_TREE && !*non_constant_p
5144 67227800 : && !DECL_DESTRUCTOR_P (fun))
5145 : {
5146 110 : if (!ctx->quiet)
5147 6 : error ("%<constexpr%> call flows off the end "
5148 : "of the function");
5149 110 : *non_constant_p = true;
5150 : }
5151 : }
5152 :
5153 86763376 : if (ctx->global->state_dependent)
5154 59939 : cacheable = false;
5155 86763376 : ctx->global->state_dependent |= save_state_dependent;
5156 :
5157 : /* At this point, the object's constructor will have run, so
5158 : the object is no longer under construction, and its possible
5159 : 'const' semantics now apply. Make a note of this fact by
5160 : marking the CONSTRUCTOR TREE_READONLY. */
5161 97817364 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
5162 9818443 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
5163 : non_constant_p, overflow_p, jump_target);
5164 :
5165 : /* Remove the parms/result from the values map. */
5166 86763376 : destroy_value_checked (ctx, res, non_constant_p);
5167 299448141 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
5168 125921389 : destroy_value_checked (ctx, parm, non_constant_p);
5169 :
5170 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
5171 87634991 : while (!ctors->is_empty ())
5172 : {
5173 871615 : tree c = ctors->pop ();
5174 871615 : if (c != result)
5175 871615 : free_constructor (c);
5176 : }
5177 :
5178 : /* Make the unshared function copy we used available for re-use. */
5179 86763376 : save_fundef_copy (fun, copy);
5180 :
5181 : /* If the call allocated some heap object that hasn't been
5182 : deallocated during the call, or if it deallocated some heap
5183 : object it has not allocated, the call isn't really stateless
5184 : for the constexpr evaluation and should not be cached.
5185 : It is fine if the call allocates something and deallocates it
5186 : too. */
5187 86763376 : if (cacheable
5188 96433918 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
5189 9666041 : || (save_heap_dealloc_count
5190 9666041 : != ctx->global->heap_dealloc_count)))
5191 : {
5192 4501 : tree heap_var;
5193 4501 : unsigned int i;
5194 4501 : if ((ctx->global->heap_vars.length ()
5195 4501 : - ctx->global->heap_dealloc_count)
5196 4501 : != save_heap_alloc_count - save_heap_dealloc_count)
5197 : cacheable = false;
5198 : else
5199 139075 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
5200 : save_heap_alloc_count)
5201 136450 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
5202 : {
5203 : cacheable = false;
5204 : break;
5205 : }
5206 : }
5207 :
5208 : /* Rewrite all occurrences of the function's RESULT_DECL with the
5209 : current object under construction. */
5210 86763376 : if (!*non_constant_p
5211 72231865 : && ctx->object
5212 17122690 : && CLASS_TYPE_P (TREE_TYPE (res))
5213 89944823 : && !is_empty_class (TREE_TYPE (res)))
5214 : {
5215 2265842 : if (!same_type_ignoring_top_level_qualifiers_p
5216 2265842 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
5217 0 : *non_constant_p = true;
5218 2265842 : else if (replace_decl (&result, res, ctx->object))
5219 : {
5220 1791 : cacheable = false;
5221 1791 : result = cxx_eval_constant_expression (ctx, result, lval,
5222 : non_constant_p,
5223 : overflow_p,
5224 : jump_target);
5225 1791 : if (*jump_target)
5226 : {
5227 0 : cacheable = false;
5228 0 : result = NULL_TREE;
5229 : }
5230 : }
5231 : }
5232 :
5233 : /* Only cache a permitted result of a constant expression. */
5234 86761585 : if (cacheable && !reduced_constant_expression_p (result))
5235 : cacheable = false;
5236 :
5237 : /* Only cache a result without contract violations. */
5238 81786317 : if (cacheable && ctx->global->contract_statement)
5239 82072255 : cacheable = false;
5240 86763376 : }
5241 : else
5242 : /* Couldn't get a function copy to evaluate. */
5243 9 : *non_constant_p = true;
5244 :
5245 109368917 : if (result == error_mark_node)
5246 6 : *non_constant_p = true;
5247 109368917 : if (*non_constant_p || *overflow_p)
5248 14531676 : result = error_mark_node;
5249 94837241 : else if (!result)
5250 3898087 : result = void_node;
5251 109368917 : if (entry)
5252 : {
5253 64562920 : entry->result (ctx->manifestly_const_eval)
5254 32281460 : = cacheable ? result : error_mark_node;
5255 :
5256 32281460 : if (result != error_mark_node
5257 27370375 : && ctx->manifestly_const_eval == mce_unknown)
5258 : {
5259 : /* Evaluation succeeded and was independent of whether we're in a
5260 : manifestly constant-evaluated context, so we can also reuse
5261 : this result when evaluating this call with a fixed context. */
5262 1902819 : if (!entry->result (mce_true))
5263 702886 : entry->result (mce_true) = entry->result (mce_unknown);
5264 1902819 : if (!entry->result (mce_false))
5265 669993 : entry->result (mce_false) = entry->result (mce_unknown);
5266 : }
5267 : }
5268 : }
5269 :
5270 : /* The result of a constexpr function must be completely initialized.
5271 :
5272 : However, in C++20, a constexpr constructor doesn't necessarily have
5273 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
5274 : in order to detect reading an uninitialized object in constexpr instead
5275 : of value-initializing it. (reduced_constant_expression_p is expected to
5276 : take care of clearing the flag.) */
5277 109368950 : if (TREE_CODE (result) == CONSTRUCTOR
5278 109368950 : && (cxx_dialect < cxx20
5279 21294574 : || !DECL_CONSTRUCTOR_P (fun)))
5280 5241056 : clear_no_implicit_zero (result);
5281 :
5282 109368950 : pop_cx_call_context ();
5283 :
5284 : /* A virtual call with a zero-offset covariant return needs no thunk, so
5285 : constant evaluation can evaluate the final overrider directly. The
5286 : result then has the overrider's return type rather than the static type
5287 : of the call. Adjust after caching so a direct call can reuse the result
5288 : with its original type. */
5289 109368950 : if (!*non_constant_p
5290 94837397 : && !*overflow_p
5291 94837241 : && !*jump_target
5292 94832540 : && DECL_VIRTUAL_P (fun)
5293 26613 : && result != void_node
5294 2217 : && INDIRECT_TYPE_P (TREE_TYPE (t))
5295 109370003 : && !(same_type_ignoring_top_level_qualifiers_p
5296 1053 : (TREE_TYPE (result), TREE_TYPE (t))))
5297 11 : result = adjust_temp_type (TREE_TYPE (t), result);
5298 :
5299 109368950 : return result;
5300 183547302 : }
5301 :
5302 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
5303 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
5304 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
5305 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
5306 : UNION_ELEMENTAL_SUBOBJECT is true when recursing on union elemental
5307 : subobjects.
5308 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
5309 :
5310 : bool
5311 1241210530 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */,
5312 : bool union_elemental_subobject /* = false */)
5313 : {
5314 1241210530 : if (t == NULL_TREE)
5315 : return false;
5316 :
5317 1236304391 : switch (TREE_CODE (t))
5318 : {
5319 : case PTRMEM_CST:
5320 : case REFLECT_EXPR:
5321 : /* Even if we can't lower this yet, it's constant. */
5322 : return true;
5323 :
5324 : case OMP_DECLARE_MAPPER:
5325 : return true;
5326 :
5327 64172255 : case CONSTRUCTOR:
5328 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
5329 64172255 : tree field;
5330 64172255 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
5331 : /* A constant vector would be folded to VECTOR_CST.
5332 : A CONSTRUCTOR of scalar type means uninitialized. */
5333 : return false;
5334 64155673 : if (CONSTRUCTOR_NO_CLEARING (t))
5335 : {
5336 3455817 : if (union_elemental_subobject
5337 5342 : && cxx_dialect >= cxx26
5338 2035 : && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE
5339 3457099 : && CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (t))
5340 : field = NULL_TREE;
5341 3455369 : else if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
5342 : {
5343 : /* There must be a valid constant initializer at every array
5344 : index. */
5345 6128 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
5346 6128 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
5347 6128 : tree cursor = min;
5348 97686 : for (auto &e: CONSTRUCTOR_ELTS (t))
5349 : {
5350 79521 : if (!reduced_constant_expression_p
5351 79521 : (e.value, NULL_TREE, union_elemental_subobject))
5352 : return false;
5353 79362 : if (array_index_cmp (cursor, e.index) != 0)
5354 : return false;
5355 79302 : if (TREE_CODE (e.index) == RANGE_EXPR)
5356 0 : cursor = TREE_OPERAND (e.index, 1);
5357 79302 : if (TREE_CODE (e.value) == RAW_DATA_CST)
5358 0 : cursor
5359 0 : = int_const_binop (PLUS_EXPR, cursor,
5360 0 : size_int (RAW_DATA_LENGTH (e.value)));
5361 : else
5362 79302 : cursor = int_const_binop (PLUS_EXPR, cursor,
5363 79302 : size_one_node);
5364 : }
5365 5909 : if (find_array_ctor_elt (t, max) == -1)
5366 : return false;
5367 5638 : goto ok;
5368 : }
5369 3449241 : else if (cxx_dialect >= cxx20
5370 3449241 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
5371 : {
5372 : /* A union can have at most one active member. A union with no
5373 : active member has no constituent values, so all constituent
5374 : values are constant. */
5375 : field = NULL_TREE;
5376 : union_elemental_subobject = true;
5377 : }
5378 : else
5379 : {
5380 3445665 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
5381 3445665 : union_elemental_subobject = false;
5382 : }
5383 : }
5384 : else
5385 : {
5386 60699856 : field = NULL_TREE;
5387 60699856 : if (TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
5388 : union_elemental_subobject = true;
5389 59747112 : else if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
5390 56899206 : union_elemental_subobject = false;
5391 : }
5392 601590294 : for (auto &e: CONSTRUCTOR_ELTS (t))
5393 : {
5394 : /* If VAL is null, we're in the middle of initializing this
5395 : element. */
5396 469510946 : if (!reduced_constant_expression_p (e.value,
5397 469510946 : (e.index
5398 469510910 : && (TREE_CODE (e.index)
5399 : == FIELD_DECL))
5400 59477040 : ? DECL_SIZE (e.index)
5401 : : NULL_TREE,
5402 : union_elemental_subobject))
5403 : return false;
5404 : /* We want to remove initializers for empty fields in a struct to
5405 : avoid confusing output_constructor. */
5406 468933883 : if (is_empty_field (e.index)
5407 468933883 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
5408 : return false;
5409 : /* Check for non-empty fields between initialized fields when
5410 : CONSTRUCTOR_NO_CLEARING. */
5411 467820278 : for (; field && e.index != field;
5412 382151 : field = next_subobject_field (DECL_CHAIN (field)))
5413 386947 : if (!is_really_empty_class (TREE_TYPE (field),
5414 : /*ignore_vptr*/false))
5415 : return false;
5416 467433331 : if (field)
5417 3927770 : field = next_subobject_field (DECL_CHAIN (field));
5418 : }
5419 : /* There could be a non-empty field at the end. */
5420 62351822 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
5421 286232 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
5422 : {
5423 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
5424 : the parent FIELD_DECL (if any) for classes with virtual
5425 : bases. */
5426 6340 : if (cxx_dialect >= cxx26
5427 4341 : && sz
5428 6862 : && tree_int_cst_le (sz, bit_position (field)))
5429 : break;
5430 : return false;
5431 : }
5432 62065590 : ok:
5433 62071620 : if (CONSTRUCTOR_NO_CLEARING (t))
5434 : /* All the fields are initialized. */
5435 3322163 : CONSTRUCTOR_NO_CLEARING (t) = false;
5436 : return true;
5437 :
5438 1171862993 : default:
5439 : /* FIXME are we calling this too much? */
5440 1171862993 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
5441 : }
5442 : }
5443 :
5444 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
5445 : why and suggest what could be done about it. */
5446 :
5447 : static tree
5448 1608 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
5449 : {
5450 1608 : bool ref_p = false;
5451 :
5452 : /* No need to look into types or unevaluated operands. */
5453 1608 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
5454 : {
5455 0 : *walk_subtrees = false;
5456 0 : return NULL_TREE;
5457 : }
5458 :
5459 1608 : switch (TREE_CODE (*tp))
5460 : {
5461 106 : CASE_CONVERT:
5462 106 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
5463 : break;
5464 87 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
5465 87 : *tp = TREE_OPERAND (*tp, 0);
5466 219 : gcc_fallthrough ();
5467 219 : case ADDR_EXPR:
5468 219 : {
5469 219 : tree op = TREE_OPERAND (*tp, 0);
5470 219 : if (VAR_P (op)
5471 118 : && DECL_DECLARED_CONSTEXPR_P (op)
5472 41 : && !TREE_STATIC (op)
5473 : /* ??? We should also say something about temporaries. */
5474 257 : && !DECL_ARTIFICIAL (op))
5475 : {
5476 33 : if (ref_p)
5477 12 : inform (location_of (*tp), "reference to %qD is not a constant "
5478 : "expression", op);
5479 : else
5480 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
5481 : "expression", op);
5482 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
5483 33 : rich_location richloc (line_table, op_loc);
5484 33 : richloc.add_fixit_insert_before (op_loc, "static ");
5485 33 : inform (&richloc,
5486 : "address of non-static constexpr variable %qD may differ on "
5487 : "each invocation of the enclosing function; add %<static%> "
5488 : "to give it a constant address", op);
5489 33 : }
5490 : break;
5491 : }
5492 : default:
5493 : break;
5494 : }
5495 :
5496 : return NULL_TREE;
5497 : }
5498 :
5499 : /* Some expressions may have constant operands but are not constant
5500 : themselves, such as 1/0. Call this function to check for that
5501 : condition.
5502 :
5503 : We only call this in places that require an arithmetic constant, not in
5504 : places where we might have a non-constant expression that can be a
5505 : component of a constant expression, such as the address of a constexpr
5506 : variable that might be dereferenced later. */
5507 :
5508 : static bool
5509 671272705 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
5510 : bool *overflow_p)
5511 : {
5512 658367256 : if (!*non_constant_p && !reduced_constant_expression_p (t)
5513 674193415 : && t != void_node)
5514 : {
5515 2920260 : if (!allow_non_constant)
5516 : {
5517 424 : auto_diagnostic_group d;
5518 581 : error_at (cp_expr_loc_or_input_loc (t),
5519 : "%q+E is not a constant expression", t);
5520 424 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
5521 : nullptr);
5522 424 : }
5523 2920260 : *non_constant_p = true;
5524 : }
5525 671272705 : if (TREE_OVERFLOW_P (t))
5526 : {
5527 678 : if (!allow_non_constant)
5528 : {
5529 155 : permerror (input_location, "overflow in constant expression");
5530 : /* If we're being permissive (and are in an enforcing
5531 : context), ignore the overflow. */
5532 155 : if (flag_permissive)
5533 75 : return *non_constant_p;
5534 : }
5535 603 : *overflow_p = true;
5536 : }
5537 671272630 : return *non_constant_p;
5538 : }
5539 :
5540 : /* Check whether the shift operation with code CODE and type TYPE on LHS
5541 : and RHS is undefined. If it is, give an error with an explanation,
5542 : and return true; return false otherwise. */
5543 :
5544 : static bool
5545 79887877 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
5546 : enum tree_code code, tree type, tree lhs, tree rhs)
5547 : {
5548 79887877 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
5549 5495459 : || TREE_CODE (lhs) != INTEGER_CST
5550 5495183 : || TREE_CODE (rhs) != INTEGER_CST)
5551 : return false;
5552 :
5553 5495183 : tree lhstype = TREE_TYPE (lhs);
5554 5495183 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
5555 :
5556 : /* [expr.shift] The behavior is undefined if the right operand
5557 : is negative, or greater than or equal to the length in bits
5558 : of the promoted left operand. */
5559 5495183 : if (tree_int_cst_sgn (rhs) == -1)
5560 : {
5561 126 : if (!ctx->quiet)
5562 35 : permerror (loc, "right operand of shift expression %q+E is negative",
5563 : build2_loc (loc, code, type, lhs, rhs));
5564 126 : return (!flag_permissive || ctx->quiet);
5565 : }
5566 5495057 : if (compare_tree_int (rhs, uprec) >= 0)
5567 : {
5568 200 : if (!ctx->quiet)
5569 34 : permerror (loc, "right operand of shift expression %q+E is greater "
5570 : "than or equal to the precision %wu of the left operand",
5571 : build2_loc (loc, code, type, lhs, rhs), uprec);
5572 200 : return (!flag_permissive || ctx->quiet);
5573 : }
5574 :
5575 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
5576 : if E1 has a signed type and non-negative value, and E1x2^E2 is
5577 : representable in the corresponding unsigned type of the result type,
5578 : then that value, converted to the result type, is the resulting value;
5579 : otherwise, the behavior is undefined.
5580 : For C++20:
5581 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
5582 : 2^N, where N is the range exponent of the type of the result. */
5583 5494857 : if (code == LSHIFT_EXPR
5584 4582171 : && !TYPE_OVERFLOW_WRAPS (lhstype)
5585 2152249 : && cxx_dialect >= cxx11
5586 7630457 : && cxx_dialect < cxx20)
5587 : {
5588 54050 : if (tree_int_cst_sgn (lhs) == -1)
5589 : {
5590 74 : if (!ctx->quiet)
5591 18 : permerror (loc,
5592 : "left operand of shift expression %q+E is negative",
5593 : build2_loc (loc, code, type, lhs, rhs));
5594 74 : return (!flag_permissive || ctx->quiet);
5595 : }
5596 : /* For signed x << y the following:
5597 : (unsigned) x >> ((prec (lhs) - 1) - y)
5598 : if > 1, is undefined. The right-hand side of this formula
5599 : is the highest bit of the LHS that can be set (starting from 0),
5600 : so that the shift doesn't overflow. We then right-shift the LHS
5601 : to see whether any other bit is set making the original shift
5602 : undefined -- the result is not representable in the corresponding
5603 : unsigned type. */
5604 53976 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
5605 53976 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
5606 53976 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
5607 53976 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
5608 53976 : if (tree_int_cst_lt (integer_one_node, t))
5609 : {
5610 41 : if (!ctx->quiet)
5611 7 : permerror (loc, "shift expression %q+E overflows",
5612 : build2_loc (loc, code, type, lhs, rhs));
5613 41 : return (!flag_permissive || ctx->quiet);
5614 : }
5615 : }
5616 : return false;
5617 : }
5618 :
5619 : /* Subroutine of cxx_eval_constant_expression.
5620 : Attempt to reduce the unary expression tree T to a compile time value.
5621 : If successful, return the value. Otherwise issue a diagnostic
5622 : and return error_mark_node. */
5623 :
5624 : static tree
5625 25801376 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
5626 : bool /*lval*/,
5627 : bool *non_constant_p, bool *overflow_p,
5628 : tree *jump_target)
5629 : {
5630 25801376 : tree r;
5631 25801376 : tree orig_arg = TREE_OPERAND (t, 0);
5632 25801376 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
5633 : non_constant_p, overflow_p,
5634 : jump_target);
5635 25801374 : if (*jump_target)
5636 : return NULL_TREE;
5637 25801372 : VERIFY_CONSTANT (arg);
5638 20580102 : location_t loc = EXPR_LOCATION (t);
5639 20580102 : enum tree_code code = TREE_CODE (t);
5640 20580102 : tree type = TREE_TYPE (t);
5641 20580102 : r = fold_unary_loc (loc, code, type, arg);
5642 20580102 : if (r == NULL_TREE)
5643 : {
5644 17 : if (arg == orig_arg)
5645 : r = t;
5646 : else
5647 13 : r = build1_loc (loc, code, type, arg);
5648 : }
5649 20580102 : VERIFY_CONSTANT (r);
5650 : return r;
5651 : }
5652 :
5653 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5654 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5655 : generic folding should be used. */
5656 :
5657 : static tree
5658 10936563 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5659 : tree lhs, tree rhs, bool *non_constant_p,
5660 : bool *overflow_p, tree *jump_target)
5661 : {
5662 10936563 : STRIP_NOPS (lhs);
5663 10936563 : if (TREE_CODE (lhs) != ADDR_EXPR)
5664 : return NULL_TREE;
5665 :
5666 10068590 : lhs = TREE_OPERAND (lhs, 0);
5667 :
5668 : /* &A[i] p+ j => &A[i + j] */
5669 10068590 : if (TREE_CODE (lhs) == ARRAY_REF
5670 210833 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5671 210833 : && TREE_CODE (rhs) == INTEGER_CST
5672 210833 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5673 10279423 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5674 : {
5675 210833 : tree orig_type = TREE_TYPE (t);
5676 210833 : location_t loc = EXPR_LOCATION (t);
5677 210833 : tree type = TREE_TYPE (lhs);
5678 :
5679 210833 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5680 210833 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5681 210833 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5682 : non_constant_p, overflow_p,
5683 : jump_target);
5684 210833 : if (*non_constant_p)
5685 : return NULL_TREE;
5686 210821 : if (*jump_target)
5687 : return NULL_TREE;
5688 : /* Don't fold an out-of-bound access. */
5689 210821 : if (!tree_int_cst_le (t, nelts))
5690 : return NULL_TREE;
5691 210821 : rhs = cp_fold_convert (ssizetype, rhs);
5692 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5693 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5694 210821 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5695 210821 : rhs, TYPE_SIZE_UNIT (type))))
5696 : return NULL_TREE;
5697 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5698 : as signed. */
5699 210789 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5700 210789 : TYPE_SIZE_UNIT (type));
5701 210789 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5702 210789 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5703 : t, NULL_TREE, NULL_TREE);
5704 210789 : t = cp_build_addr_expr (t, tf_warning_or_error);
5705 210789 : t = cp_fold_convert (orig_type, t);
5706 210789 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5707 : non_constant_p, overflow_p,
5708 210789 : jump_target);
5709 : }
5710 :
5711 : return NULL_TREE;
5712 : }
5713 :
5714 : /* Try to fold expressions like
5715 : (struct S *) (&a[0].D.2378 + 12)
5716 : into
5717 : &MEM <struct T> [(void *)&a + 12B]
5718 : This is something normally done by gimple_fold_stmt_to_constant_1
5719 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5720 : dereference the address because some details are lost.
5721 : For pointer comparisons we want such folding though so that
5722 : match.pd address_compare optimization works. */
5723 :
5724 : static tree
5725 12262496 : cxx_maybe_fold_addr_pointer_plus (tree t)
5726 : {
5727 24524992 : while (CONVERT_EXPR_P (t)
5728 12968598 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5729 706102 : t = TREE_OPERAND (t, 0);
5730 12262496 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5731 : return NULL_TREE;
5732 10567386 : tree op0 = TREE_OPERAND (t, 0);
5733 10567386 : tree op1 = TREE_OPERAND (t, 1);
5734 10567386 : if (TREE_CODE (op1) != INTEGER_CST)
5735 : return NULL_TREE;
5736 10567386 : while (CONVERT_EXPR_P (op0)
5737 21131224 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5738 10563838 : op0 = TREE_OPERAND (op0, 0);
5739 10567386 : if (TREE_CODE (op0) != ADDR_EXPR)
5740 : return NULL_TREE;
5741 10567386 : op1 = fold_convert (ptr_type_node, op1);
5742 10567386 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5743 10567386 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5744 : }
5745 :
5746 : /* Subroutine of cxx_eval_constant_expression.
5747 : Like cxx_eval_unary_expression, except for binary expressions. */
5748 :
5749 : static tree
5750 111760920 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5751 : value_cat lval,
5752 : bool *non_constant_p, bool *overflow_p,
5753 : tree *jump_target)
5754 : {
5755 111760920 : tree r = NULL_TREE;
5756 111760920 : tree orig_lhs = TREE_OPERAND (t, 0);
5757 111760920 : tree orig_rhs = TREE_OPERAND (t, 1);
5758 111760920 : tree lhs, rhs;
5759 :
5760 111760920 : if (TREE_CODE (t) == POINTER_PLUS_EXPR
5761 12677979 : && CONVERT_EXPR_P (orig_lhs)
5762 6714444 : && (lhs = TREE_OPERAND (orig_lhs, 0))
5763 6714444 : && INDIRECT_TYPE_P (TREE_TYPE (lhs))
5764 6714444 : && (TREE_CODE (orig_lhs) != NOP_EXPR || !REINTERPRET_CAST_P (orig_lhs))
5765 118475335 : && is_properly_derived_from (TREE_TYPE (TREE_TYPE (orig_lhs)),
5766 6714415 : TREE_TYPE (TREE_TYPE (lhs))))
5767 : /* fold_unary_loc and match.pd can optimize
5768 : (type *) (ptr p+ off) into ((type *) ptr) p+ off, which can break
5769 : static_cast constexpr diagnostics, because ptr before the p+
5770 : cast to type * can be invalid while the original valid.
5771 : Evaluate it as (type *) (ptr p+ off) instead. */
5772 : {
5773 42 : tree cast = copy_node (orig_lhs);
5774 42 : tree pplus = copy_node (t);
5775 42 : TREE_OPERAND (cast, 0) = pplus;
5776 42 : TREE_OPERAND (pplus, 0) = lhs;
5777 42 : TREE_TYPE (pplus) = TREE_TYPE (lhs);
5778 42 : lhs = cxx_eval_constant_expression (ctx, cast, vc_prvalue,
5779 : non_constant_p, overflow_p,
5780 : jump_target);
5781 42 : if (lhs != cast)
5782 : {
5783 24 : ggc_free (cast);
5784 24 : ggc_free (pplus);
5785 : }
5786 42 : if (*non_constant_p)
5787 : return t;
5788 42 : return lhs;
5789 : }
5790 :
5791 111760878 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5792 : non_constant_p, overflow_p,
5793 : jump_target);
5794 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5795 : subtraction. */
5796 111760876 : if (*non_constant_p)
5797 : return t;
5798 90140437 : if (*jump_target)
5799 : return NULL_TREE;
5800 90140342 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5801 : non_constant_p, overflow_p,
5802 : jump_target);
5803 90140342 : if (*non_constant_p)
5804 : return t;
5805 88978864 : if (*jump_target)
5806 : return NULL_TREE;
5807 :
5808 88978858 : location_t loc = EXPR_LOCATION (t);
5809 88978858 : enum tree_code code = TREE_CODE (t);
5810 88978858 : tree type = TREE_TYPE (t);
5811 :
5812 88978858 : if (code == EQ_EXPR || code == NE_EXPR)
5813 : {
5814 18475893 : bool is_code_eq = (code == EQ_EXPR);
5815 :
5816 18475893 : if (TREE_CODE (lhs) == PTRMEM_CST
5817 217 : && TREE_CODE (rhs) == PTRMEM_CST)
5818 : {
5819 76 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5820 76 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5821 76 : bool eq;
5822 76 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5823 76 : && TREE_CODE (lmem) == FIELD_DECL
5824 76 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5825 103 : && same_type_p (DECL_CONTEXT (lmem),
5826 : DECL_CONTEXT (rmem)))
5827 : /* If both refer to (possibly different) members of the same union
5828 : (12.3), they compare equal. */
5829 : eq = true;
5830 : else
5831 49 : eq = cp_tree_equal (lhs, rhs);
5832 76 : r = constant_boolean_node (eq == is_code_eq, type);
5833 76 : }
5834 18475817 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5835 18475676 : || TREE_CODE (rhs) == PTRMEM_CST)
5836 18475817 : && (null_member_pointer_value_p (lhs)
5837 141 : || null_member_pointer_value_p (rhs)))
5838 141 : r = constant_boolean_node (!is_code_eq, type);
5839 18475676 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5840 0 : lhs = cplus_expand_constant (lhs);
5841 18475676 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5842 0 : rhs = cplus_expand_constant (rhs);
5843 36951352 : else if (REFLECTION_TYPE_P (TREE_TYPE (lhs))
5844 18479624 : && REFLECTION_TYPE_P (TREE_TYPE (rhs)))
5845 : {
5846 3948 : const bool eq = compare_reflections (lhs, rhs);
5847 3948 : r = constant_boolean_node (eq == is_code_eq, type);
5848 : }
5849 : }
5850 0 : if (r == NULL_TREE
5851 88974693 : && TREE_CODE_CLASS (code) == tcc_comparison
5852 39225309 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5853 : {
5854 6131248 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5855 5137529 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5856 6131248 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5857 5429857 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5858 : }
5859 10936718 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5860 99915576 : && integer_zerop (lhs) && !integer_zerop (rhs))
5861 : {
5862 155 : if (!ctx->quiet)
5863 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5864 155 : *non_constant_p = true;
5865 155 : return t;
5866 : }
5867 88978703 : else if (code == POINTER_PLUS_EXPR)
5868 : {
5869 10936563 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5870 : overflow_p, jump_target);
5871 10936563 : if (*jump_target)
5872 : return NULL_TREE;
5873 : }
5874 78042140 : else if (code == SPACESHIP_EXPR)
5875 : {
5876 27236 : r = genericize_spaceship (loc, type, lhs, rhs);
5877 27236 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5878 27236 : overflow_p, jump_target);
5879 : }
5880 :
5881 88951467 : if (r == NULL_TREE)
5882 : {
5883 88736513 : if (ctx->manifestly_const_eval == mce_true
5884 68160365 : && (flag_constexpr_fp_except
5885 68160362 : || TREE_CODE (type) != REAL_TYPE))
5886 : {
5887 68108085 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5888 68108085 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5889 68108085 : }
5890 : else
5891 20628428 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5892 : }
5893 :
5894 88736513 : if (r == NULL_TREE
5895 9063688 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5896 100 : && TREE_CODE (lhs) == INTEGER_CST
5897 98 : && TREE_CODE (rhs) == INTEGER_CST
5898 98015155 : && wi::neg_p (wi::to_wide (rhs)))
5899 : {
5900 : /* For diagnostics and -fpermissive emulate previous behavior of
5901 : handling shifts by negative amount. */
5902 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5903 98 : if (nrhs)
5904 119 : r = fold_binary_loc (loc,
5905 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5906 : type, lhs, nrhs);
5907 : }
5908 :
5909 88951467 : if (r == NULL_TREE)
5910 : {
5911 9063590 : if (lhs == orig_lhs && rhs == orig_rhs)
5912 : r = t;
5913 : else
5914 3145253 : r = build2_loc (loc, code, type, lhs, rhs);
5915 : }
5916 79887877 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5917 433 : *non_constant_p = true;
5918 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5919 : a local array in a constexpr function. */
5920 88951467 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5921 71202506 : if (!ptr)
5922 71202506 : VERIFY_CONSTANT (r);
5923 : return r;
5924 : }
5925 :
5926 : /* Subroutine of cxx_eval_constant_expression.
5927 : Attempt to evaluate condition expressions. */
5928 :
5929 : static tree
5930 39363117 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5931 : value_cat lval,
5932 : bool *non_constant_p, bool *overflow_p,
5933 : tree *jump_target)
5934 : {
5935 39363117 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5936 : vc_prvalue,
5937 : non_constant_p, overflow_p,
5938 : jump_target);
5939 39363117 : if (*jump_target)
5940 : return NULL_TREE;
5941 39363093 : VERIFY_CONSTANT (val);
5942 37077977 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5943 : {
5944 : /* Evaluate the condition as if it was
5945 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5946 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5947 : without manifestly_const_eval even expressions or parts thereof which
5948 : will later be manifestly const_eval evaluated), otherwise fold it to
5949 : true. */
5950 1081089 : if (ctx->manifestly_const_eval == mce_unknown)
5951 : {
5952 978436 : *non_constant_p = true;
5953 978436 : return t;
5954 : }
5955 102653 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5956 : boolean_type_node);
5957 : }
5958 : /* Don't VERIFY_CONSTANT the other operands. */
5959 36099541 : const bool zero_p = integer_zerop (val);
5960 36099541 : if (zero_p)
5961 22744358 : val = TREE_OPERAND (t, 2);
5962 : else
5963 13355183 : val = TREE_OPERAND (t, 1);
5964 36099541 : if (TREE_CODE (t) == IF_STMT && !val)
5965 9916409 : val = void_node;
5966 :
5967 : /* P2564: If we aren't in immediate function context (including a manifestly
5968 : constant-evaluated expression), check any uses of immediate functions in
5969 : the arm we're discarding. But don't do this inside a call; we already
5970 : checked when parsing the function. */
5971 36099541 : if (ctx->manifestly_const_eval != mce_true
5972 5043412 : && !in_immediate_context ()
5973 4945795 : && !ctx->call
5974 36892530 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5975 792989 : ctx->manifestly_const_eval))
5976 : {
5977 7 : *non_constant_p = true;
5978 7 : return t;
5979 : }
5980 :
5981 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5982 : serve as the initializer for the same object as the outer TARGET_EXPR,
5983 : as in
5984 : A a = true ? A{} : A{};
5985 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5986 36099534 : if (TREE_CODE (val) == TARGET_EXPR)
5987 5342 : val = TARGET_EXPR_INITIAL (val);
5988 36099534 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5989 36099534 : overflow_p, jump_target);
5990 : }
5991 :
5992 : /* Subroutine of cxx_eval_constant_expression.
5993 : Attempt to evaluate vector condition expressions. Unlike
5994 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5995 : ternary arithmetics operation, where all 3 arguments have to be
5996 : evaluated as constants and then folding computes the result from
5997 : them. */
5998 :
5999 : static tree
6000 11755 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
6001 : bool *non_constant_p, bool *overflow_p,
6002 : tree *jump_target)
6003 : {
6004 11755 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6005 : vc_prvalue,
6006 : non_constant_p, overflow_p,
6007 : jump_target);
6008 11755 : if (*jump_target)
6009 : return NULL_TREE;
6010 11755 : VERIFY_CONSTANT (arg1);
6011 8227 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6012 : vc_prvalue,
6013 : non_constant_p, overflow_p,
6014 : jump_target);
6015 8227 : if (*jump_target)
6016 : return NULL_TREE;
6017 8227 : VERIFY_CONSTANT (arg2);
6018 7097 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
6019 : vc_prvalue,
6020 : non_constant_p, overflow_p,
6021 : jump_target);
6022 7097 : if (*jump_target)
6023 : return NULL_TREE;
6024 7097 : VERIFY_CONSTANT (arg3);
6025 7097 : location_t loc = EXPR_LOCATION (t);
6026 7097 : tree type = TREE_TYPE (t);
6027 7097 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
6028 7097 : if (r == NULL_TREE)
6029 : {
6030 0 : if (arg1 == TREE_OPERAND (t, 0)
6031 0 : && arg2 == TREE_OPERAND (t, 1)
6032 0 : && arg3 == TREE_OPERAND (t, 2))
6033 : r = t;
6034 : else
6035 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
6036 : }
6037 7097 : VERIFY_CONSTANT (r);
6038 : return r;
6039 : }
6040 :
6041 : /* Returns less than, equal to, or greater than zero if KEY is found to be
6042 : less than, to match, or to be greater than the constructor_elt's INDEX. */
6043 :
6044 : static int
6045 441080 : array_index_cmp (tree key, tree index)
6046 : {
6047 441080 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
6048 :
6049 441080 : switch (TREE_CODE (index))
6050 : {
6051 438136 : case INTEGER_CST:
6052 438136 : return tree_int_cst_compare (key, index);
6053 2944 : case RANGE_EXPR:
6054 2944 : {
6055 2944 : tree lo = TREE_OPERAND (index, 0);
6056 2944 : tree hi = TREE_OPERAND (index, 1);
6057 2944 : if (tree_int_cst_lt (key, lo))
6058 : return -1;
6059 2648 : else if (tree_int_cst_lt (hi, key))
6060 : return 1;
6061 : else
6062 2648 : return 0;
6063 : }
6064 0 : default:
6065 0 : gcc_unreachable ();
6066 : }
6067 : }
6068 :
6069 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
6070 : relative index OFF. */
6071 :
6072 : static tree
6073 29559 : raw_data_cst_elt (tree raw_data, unsigned int off)
6074 : {
6075 29559 : return build_int_cst (TREE_TYPE (raw_data),
6076 29559 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
6077 59118 : ? (HOST_WIDE_INT)
6078 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
6079 : : (HOST_WIDE_INT)
6080 486 : RAW_DATA_SCHAR_ELT (raw_data, off));
6081 : }
6082 :
6083 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
6084 : if none. If INSERT is true, insert a matching element rather than fail. */
6085 :
6086 : static HOST_WIDE_INT
6087 12532375 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
6088 : {
6089 12532375 : if (tree_int_cst_sgn (dindex) < 0)
6090 : return -1;
6091 :
6092 12532375 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
6093 12532375 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
6094 12532375 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
6095 :
6096 16780402 : unsigned HOST_WIDE_INT end = len;
6097 16780402 : unsigned HOST_WIDE_INT begin = 0;
6098 :
6099 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
6100 : that the same is true of the other elements and index directly. */
6101 11977280 : if (end > 0)
6102 : {
6103 11748317 : tree cindex = (*elts)[end - 1].index;
6104 11748317 : if (cindex == NULL_TREE)
6105 : {
6106 : /* Verify that if the last index is missing, all indexes
6107 : are missing and there is no RAW_DATA_CST. */
6108 21915 : if (flag_checking)
6109 220661 : for (unsigned int j = 0; j < len - 1; ++j)
6110 198746 : gcc_assert ((*elts)[j].index == NULL_TREE
6111 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
6112 21915 : if (i < end)
6113 21915 : return i;
6114 : else
6115 : {
6116 0 : begin = end;
6117 0 : if (i == end)
6118 : /* If the element is to be added right at the end,
6119 : make sure it is added with cleared index too. */
6120 4803122 : dindex = NULL_TREE;
6121 0 : else if (insert)
6122 : /* Otherwise, in order not to break the assumption
6123 : that CONSTRUCTOR either has all indexes or none,
6124 : we need to add indexes to all elements. */
6125 0 : for (unsigned int j = 0; j < len; ++j)
6126 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
6127 : }
6128 : }
6129 11726402 : else if (TREE_CODE (cindex) == INTEGER_CST
6130 11726402 : && compare_tree_int (cindex, end - 1) == 0)
6131 : {
6132 11515311 : tree value = (*elts)[end - 1].value;
6133 11515311 : if (i < end)
6134 : {
6135 7707428 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
6136 : begin = end - 1;
6137 : else
6138 7707338 : return i;
6139 : }
6140 3807883 : else if (TREE_CODE (value) == RAW_DATA_CST
6141 3840039 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
6142 32156 : + RAW_DATA_LENGTH (value)))
6143 16078 : begin = end - 1;
6144 : else
6145 3791805 : begin = end;
6146 : }
6147 : }
6148 :
6149 : /* Otherwise, find a matching index by means of a binary search. */
6150 5007904 : while (begin != end)
6151 : {
6152 361715 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
6153 361715 : constructor_elt &elt = (*elts)[middle];
6154 361715 : tree idx = elt.index;
6155 :
6156 361715 : int cmp = array_index_cmp (dindex, idx);
6157 361715 : if (cmp > 0
6158 66760 : && TREE_CODE (elt.value) == RAW_DATA_CST
6159 445523 : && wi::to_offset (dindex) < (wi::to_offset (idx)
6160 83808 : + RAW_DATA_LENGTH (elt.value)))
6161 29417 : cmp = 0;
6162 361715 : if (cmp < 0)
6163 : end = middle;
6164 194276 : else if (cmp > 0)
6165 37343 : begin = middle + 1;
6166 : else
6167 : {
6168 156933 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
6169 : {
6170 : /* We need to split the RAW_DATA_CST elt. */
6171 122 : constructor_elt e;
6172 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
6173 244 : unsigned int off = (wi::to_offset (dindex)
6174 244 : - wi::to_offset (idx)).to_uhwi ();
6175 122 : tree value = elt.value;
6176 122 : unsigned int len = RAW_DATA_LENGTH (value);
6177 122 : if (off > 1 && len >= off + 3)
6178 22 : value = copy_node (elt.value);
6179 122 : if (off)
6180 : {
6181 33 : if (off > 1)
6182 33 : RAW_DATA_LENGTH (elt.value) = off;
6183 : else
6184 0 : elt.value = raw_data_cst_elt (elt.value, 0);
6185 33 : e.index = size_binop (PLUS_EXPR, elt.index,
6186 : build_int_cst (TREE_TYPE (elt.index),
6187 : off));
6188 33 : e.value = NULL_TREE;
6189 33 : ++middle;
6190 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
6191 : }
6192 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
6193 122 : if (len >= off + 2)
6194 : {
6195 111 : e.index = (*elts)[middle].index;
6196 111 : e.index = size_binop (PLUS_EXPR, e.index,
6197 : build_one_cst (TREE_TYPE (e.index)));
6198 111 : if (len >= off + 3)
6199 : {
6200 111 : RAW_DATA_LENGTH (value) -= off + 1;
6201 111 : RAW_DATA_POINTER (value) += off + 1;
6202 111 : e.value = value;
6203 : }
6204 : else
6205 0 : e.value = raw_data_cst_elt (value, off + 1);
6206 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
6207 : }
6208 122 : return middle;
6209 : }
6210 35919 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
6211 : {
6212 : /* We need to split the range. */
6213 361 : constructor_elt e;
6214 361 : tree lo = TREE_OPERAND (idx, 0);
6215 361 : tree hi = TREE_OPERAND (idx, 1);
6216 361 : tree value = elt.value;
6217 361 : dindex = fold_convert (sizetype, dindex);
6218 361 : if (tree_int_cst_lt (lo, dindex))
6219 : {
6220 : /* There are still some lower elts; shorten the range. */
6221 194 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
6222 97 : size_one_node);
6223 97 : if (tree_int_cst_equal (lo, new_hi))
6224 : /* Only one element left, no longer a range. */
6225 42 : elt.index = lo;
6226 : else
6227 55 : TREE_OPERAND (idx, 1) = new_hi;
6228 : /* Append the element we want to insert. */
6229 97 : ++middle;
6230 97 : e.index = dindex;
6231 97 : e.value = unshare_constructor (value);
6232 97 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
6233 : }
6234 : else
6235 : /* No lower elts, the range elt is now ours. */
6236 264 : elt.index = dindex;
6237 :
6238 361 : if (tree_int_cst_lt (dindex, hi))
6239 : {
6240 : /* There are still some higher elts; append a range. */
6241 506 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
6242 253 : size_one_node);
6243 253 : if (tree_int_cst_equal (new_lo, hi))
6244 : e.index = hi;
6245 : else
6246 153 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
6247 253 : e.value = unshare_constructor (value);
6248 253 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
6249 : }
6250 : }
6251 156811 : return middle;
6252 : }
6253 : }
6254 :
6255 4646189 : if (insert)
6256 : {
6257 4515523 : constructor_elt e = { dindex, NULL_TREE };
6258 4515523 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
6259 4515523 : return end;
6260 : }
6261 :
6262 : return -1;
6263 : }
6264 :
6265 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
6266 : matching constructor_elt exists, then add one to CTOR.
6267 :
6268 : As an optimization, if POS_HINT is non-negative then it is used as a guess
6269 : for the (integer) index of the matching constructor_elt within CTOR.
6270 :
6271 : If POS_HINT is -2, it means do not insert. */
6272 :
6273 : static constructor_elt *
6274 42461953 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
6275 : {
6276 : /* Check the hint first. */
6277 4631868 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
6278 47093821 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
6279 : return CONSTRUCTOR_ELT (ctor, pos_hint);
6280 :
6281 37830097 : bool insertp = (pos_hint != -2);
6282 37830097 : tree type = TREE_TYPE (ctor);
6283 37830097 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
6284 : {
6285 27296 : gcc_assert (insertp);
6286 27296 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
6287 27296 : return &CONSTRUCTOR_ELTS (ctor)->last();
6288 : }
6289 37802801 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
6290 : {
6291 6196223 : if (TREE_CODE (index) == RANGE_EXPR)
6292 : {
6293 : /* Support for RANGE_EXPR index lookups is currently limited to
6294 : accessing an existing element via POS_HINT, or appending a new
6295 : element to the end of CTOR. ??? Support for other access
6296 : patterns may also be needed. */
6297 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
6298 3 : if (vec_safe_length (elts))
6299 : {
6300 3 : tree lo = TREE_OPERAND (index, 0);
6301 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
6302 : }
6303 3 : gcc_assert (insertp);
6304 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
6305 3 : return &elts->last();
6306 : }
6307 :
6308 6196220 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
6309 6196220 : if (i < 0)
6310 : {
6311 2711 : gcc_assert (!insertp);
6312 : return nullptr;
6313 : }
6314 6193509 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
6315 6193509 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
6316 : {
6317 : /* Split a range even if we aren't inserting new entries. */
6318 0 : gcc_assert (!insertp);
6319 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
6320 0 : gcc_assert (i >= 0);
6321 0 : cep = CONSTRUCTOR_ELT (ctor, i);
6322 : }
6323 6193509 : gcc_assert (cep->index == NULL_TREE
6324 : || TREE_CODE (cep->index) != RANGE_EXPR);
6325 : return cep;
6326 : }
6327 : else
6328 : {
6329 31606578 : gcc_assert (TREE_CODE (index) == FIELD_DECL
6330 : && (same_type_ignoring_top_level_qualifiers_p
6331 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
6332 :
6333 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
6334 : Usually we meet initializers in that order, but it is
6335 : possible for base types to be placed not in program
6336 : order. */
6337 31606578 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
6338 31606578 : unsigned HOST_WIDE_INT idx = 0;
6339 31606578 : constructor_elt *cep = NULL;
6340 :
6341 : /* Check if we're changing the active member of a union. */
6342 582099 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
6343 31985829 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
6344 12486 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
6345 : /* If the bit offset of INDEX is larger than that of the last
6346 : constructor_elt, then we can just immediately append a new
6347 : constructor_elt to the end of CTOR. */
6348 48161045 : else if (CONSTRUCTOR_NELTS (ctor)
6349 44358036 : && tree_int_cst_compare (bit_position (index),
6350 43391948 : bit_position (CONSTRUCTOR_ELTS (ctor)
6351 21695974 : ->last().index)) > 0)
6352 : {
6353 6107595 : idx = CONSTRUCTOR_NELTS (ctor);
6354 6107595 : goto insert;
6355 : }
6356 :
6357 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
6358 : appropriately. */
6359 :
6360 30286740 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
6361 4787757 : idx++, fields = DECL_CHAIN (fields))
6362 : {
6363 20376136 : if (index == cep->index)
6364 15546162 : goto found;
6365 :
6366 : /* The field we're initializing must be on the field
6367 : list. Look to see if it is present before the
6368 : field the current ELT initializes. */
6369 51176905 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
6370 46389148 : if (index == fields)
6371 42217 : goto insert;
6372 : }
6373 : /* We fell off the end of the CONSTRUCTOR, so insert a new
6374 : entry at the end. */
6375 :
6376 16060416 : insert:
6377 16060416 : if (!insertp)
6378 : return nullptr;
6379 :
6380 16060413 : {
6381 16060413 : constructor_elt ce = { index, NULL_TREE };
6382 :
6383 16060413 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
6384 16060413 : cep = CONSTRUCTOR_ELT (ctor, idx);
6385 : }
6386 42461953 : found:;
6387 :
6388 : return cep;
6389 : }
6390 : }
6391 :
6392 : /* Under the control of CTX, issue a detailed diagnostic for
6393 : an out-of-bounds subscript INDEX into the expression ARRAY. */
6394 :
6395 : static void
6396 601 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
6397 : {
6398 601 : if (!ctx->quiet)
6399 : {
6400 120 : tree arraytype = TREE_TYPE (array);
6401 :
6402 : /* Convert the unsigned array subscript to a signed integer to avoid
6403 : printing huge numbers for small negative values. */
6404 120 : tree sidx = fold_convert (ssizetype, index);
6405 120 : STRIP_ANY_LOCATION_WRAPPER (array);
6406 120 : if (DECL_P (array))
6407 : {
6408 79 : auto_diagnostic_group d;
6409 79 : if (TYPE_DOMAIN (arraytype))
6410 73 : error_at (loc, "array subscript value %qE is outside the bounds "
6411 : "of array %qD of type %qT", sidx, array, arraytype);
6412 : else
6413 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
6414 : "type %qT with unknown bounds", sidx, array, arraytype);
6415 79 : inform (DECL_SOURCE_LOCATION (array), "declared here");
6416 79 : }
6417 41 : else if (TYPE_DOMAIN (arraytype))
6418 38 : error_at (loc, "array subscript value %qE is outside the bounds "
6419 : "of array type %qT", sidx, arraytype);
6420 : else
6421 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
6422 : "with unknown bounds", sidx, arraytype);
6423 : }
6424 601 : }
6425 :
6426 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
6427 : a VECTOR_TYPE). */
6428 :
6429 : static tree
6430 21076069 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
6431 : bool *non_constant_p, bool *overflow_p,
6432 : tree *jump_target)
6433 : {
6434 21076069 : tree nelts;
6435 21076069 : if (TREE_CODE (type) == ARRAY_TYPE)
6436 : {
6437 21076069 : if (TYPE_DOMAIN (type))
6438 21075809 : nelts = array_type_nelts_top (type);
6439 : else
6440 260 : nelts = size_zero_node;
6441 : }
6442 0 : else if (VECTOR_TYPE_P (type))
6443 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
6444 : else
6445 0 : gcc_unreachable ();
6446 :
6447 : /* For VLAs, the number of elements won't be an integer constant. */
6448 21076069 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
6449 : non_constant_p, overflow_p,
6450 : jump_target);
6451 21076069 : return nelts;
6452 : }
6453 :
6454 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
6455 : STRING_CST STRING. */
6456 :
6457 : static tree
6458 1991520 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
6459 : {
6460 1991520 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
6461 1991520 : tree r;
6462 :
6463 1991520 : if (chars_per_elt == 1)
6464 1776099 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
6465 : else
6466 : {
6467 215421 : const unsigned char *ptr
6468 215421 : = ((const unsigned char *)TREE_STRING_POINTER (string)
6469 215421 : + index * chars_per_elt);
6470 215421 : r = native_interpret_expr (type, ptr, chars_per_elt);
6471 : }
6472 1991520 : return r;
6473 : }
6474 :
6475 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
6476 : subscript, diagnose any problems with it, and return the result. */
6477 :
6478 : static tree
6479 21275869 : eval_and_check_array_index (const constexpr_ctx *ctx,
6480 : tree t, bool allow_one_past,
6481 : bool *non_constant_p, bool *overflow_p,
6482 : tree *jump_target)
6483 : {
6484 21275869 : location_t loc = cp_expr_loc_or_input_loc (t);
6485 21275869 : tree ary = TREE_OPERAND (t, 0);
6486 21275869 : t = TREE_OPERAND (t, 1);
6487 21275869 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
6488 : non_constant_p, overflow_p,
6489 : jump_target);
6490 21275869 : if (*jump_target)
6491 : return NULL_TREE;
6492 21275869 : VERIFY_CONSTANT (index);
6493 :
6494 21075273 : if (!tree_fits_shwi_p (index)
6495 21075273 : || tree_int_cst_sgn (index) < 0)
6496 : {
6497 112 : diag_array_subscript (loc, ctx, ary, index);
6498 112 : *non_constant_p = true;
6499 112 : return t;
6500 : }
6501 :
6502 21075161 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
6503 : overflow_p, jump_target);
6504 21075161 : if (*jump_target)
6505 : return NULL_TREE;
6506 21075161 : VERIFY_CONSTANT (nelts);
6507 21075077 : if (allow_one_past
6508 21075077 : ? !tree_int_cst_le (index, nelts)
6509 14302023 : : !tree_int_cst_lt (index, nelts))
6510 : {
6511 489 : diag_array_subscript (loc, ctx, ary, index);
6512 489 : *non_constant_p = true;
6513 489 : return t;
6514 : }
6515 :
6516 : return index;
6517 : }
6518 :
6519 : /* Subroutine of cxx_eval_constant_expression.
6520 : Attempt to reduce a reference to an array slot. */
6521 :
6522 : static tree
6523 15671074 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
6524 : value_cat lval,
6525 : bool *non_constant_p, bool *overflow_p,
6526 : tree *jump_target)
6527 : {
6528 15671074 : tree oldary = TREE_OPERAND (t, 0);
6529 15671074 : tree ary = cxx_eval_constant_expression (ctx, oldary,
6530 : lval,
6531 : non_constant_p, overflow_p,
6532 : jump_target);
6533 15671074 : if (*non_constant_p)
6534 : return t;
6535 15427903 : if (*jump_target)
6536 : return NULL_TREE;
6537 15427903 : if (!lval
6538 8653402 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
6539 144212 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
6540 15572115 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
6541 144212 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
6542 144212 : ary = TREE_OPERAND (ary, 0);
6543 :
6544 15427903 : tree oldidx = TREE_OPERAND (t, 1);
6545 15427903 : tree index = eval_and_check_array_index (ctx, t, lval,
6546 : non_constant_p, overflow_p,
6547 : jump_target);
6548 15427903 : if (*non_constant_p)
6549 : return t;
6550 15226703 : if (*jump_target)
6551 : return NULL_TREE;
6552 :
6553 15226703 : if (lval && ary == oldary && index == oldidx)
6554 : return t;
6555 10225547 : else if (lval == vc_discard)
6556 : return t;
6557 10225547 : else if (lval)
6558 1771644 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
6559 :
6560 8453903 : unsigned len = 0, elem_nchars = 1;
6561 8453903 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
6562 8453903 : if (TREE_CODE (ary) == CONSTRUCTOR)
6563 6329267 : len = CONSTRUCTOR_NELTS (ary);
6564 2124636 : else if (TREE_CODE (ary) == STRING_CST)
6565 : {
6566 1980438 : elem_nchars = (TYPE_PRECISION (elem_type)
6567 1980438 : / TYPE_PRECISION (char_type_node));
6568 1980438 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
6569 : }
6570 144198 : else if (TREE_CODE (ary) == VECTOR_CST)
6571 : /* We don't create variable-length VECTOR_CSTs. */
6572 144198 : len = VECTOR_CST_NELTS (ary).to_constant ();
6573 : else
6574 : {
6575 : /* We can't do anything with other tree codes, so use
6576 : VERIFY_CONSTANT to complain and fail. */
6577 0 : VERIFY_CONSTANT (ary);
6578 0 : gcc_unreachable ();
6579 : }
6580 :
6581 8453903 : bool found;
6582 8453903 : HOST_WIDE_INT i = 0;
6583 8453903 : if (TREE_CODE (ary) == CONSTRUCTOR)
6584 : {
6585 6329267 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
6586 6329267 : found = (ix >= 0);
6587 6329267 : if (found)
6588 6201967 : i = ix;
6589 : }
6590 : else
6591 : {
6592 2124636 : i = tree_to_shwi (index);
6593 2124636 : found = (i < len);
6594 : }
6595 :
6596 8453903 : if (found)
6597 : {
6598 8326138 : tree r;
6599 8326138 : if (TREE_CODE (ary) == CONSTRUCTOR)
6600 : {
6601 6201967 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
6602 6201967 : if (TREE_CODE (r) == RAW_DATA_CST)
6603 : {
6604 29437 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
6605 29437 : gcc_checking_assert (ridx);
6606 29437 : unsigned int off
6607 29437 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
6608 29437 : r = raw_data_cst_elt (r, off);
6609 : }
6610 : }
6611 2124171 : else if (TREE_CODE (ary) == VECTOR_CST)
6612 144198 : r = VECTOR_CST_ELT (ary, i);
6613 : else
6614 1979973 : r = extract_string_elt (ary, elem_nchars, i);
6615 :
6616 2153608 : if (r)
6617 : /* Don't VERIFY_CONSTANT here. */
6618 : return r;
6619 :
6620 : /* Otherwise the element doesn't have a value yet. */
6621 : }
6622 :
6623 : /* Not found. */
6624 :
6625 127765 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
6626 90 : return build_constructor (elem_type, NULL);
6627 :
6628 127675 : if (TREE_CODE (ary) == CONSTRUCTOR
6629 127675 : && CONSTRUCTOR_NO_CLEARING (ary))
6630 : {
6631 : /* 'ary' is part of the aggregate initializer we're currently
6632 : building; if there's no initializer for this element yet,
6633 : that's an error. */
6634 51 : if (!ctx->quiet)
6635 16 : error ("accessing uninitialized array element");
6636 51 : *non_constant_p = true;
6637 51 : return t;
6638 : }
6639 :
6640 : /* If it's within the array bounds but doesn't have an explicit
6641 : initializer, it's initialized from {}. But use build_value_init
6642 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
6643 127624 : tree val;
6644 127624 : constexpr_ctx new_ctx;
6645 127624 : if (CP_AGGREGATE_TYPE_P (elem_type))
6646 : {
6647 504 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
6648 504 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
6649 : }
6650 : else
6651 127120 : val = build_value_init (elem_type, tf_warning_or_error);
6652 :
6653 : /* Create a new constructor only if we don't already have a suitable one. */
6654 127624 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
6655 128154 : && (!ctx->ctor
6656 45 : || !same_type_ignoring_top_level_qualifiers_p
6657 45 : (elem_type, TREE_TYPE (ctx->ctor))));
6658 : if (new_ctor)
6659 : {
6660 521 : new_ctx = *ctx;
6661 : /* We clear the object here. We used to replace it with T, but that
6662 : caused problems (101371, 108158); and anyway, T is the initializer,
6663 : not the target object. */
6664 521 : new_ctx.object = NULL_TREE;
6665 521 : new_ctx.ctor = build_constructor (elem_type, NULL);
6666 521 : ctx = &new_ctx;
6667 : }
6668 127624 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6669 : overflow_p, jump_target);
6670 127624 : if (new_ctor && t != ctx->ctor)
6671 500 : free_constructor (ctx->ctor);
6672 : return t;
6673 : }
6674 :
6675 : /* Subroutine of cxx_eval_constant_expression.
6676 : Attempt to reduce a field access of a value of class type. */
6677 :
6678 : static tree
6679 115867082 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
6680 : value_cat lval,
6681 : bool *non_constant_p, bool *overflow_p,
6682 : tree *jump_target)
6683 : {
6684 115867082 : unsigned HOST_WIDE_INT i;
6685 115867082 : tree field;
6686 115867082 : tree value;
6687 115867082 : tree part = TREE_OPERAND (t, 1);
6688 115867082 : tree orig_whole = TREE_OPERAND (t, 0);
6689 115867082 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6690 : lval,
6691 : non_constant_p, overflow_p,
6692 : jump_target);
6693 115867082 : if (*non_constant_p)
6694 : return t;
6695 90065566 : if (*jump_target)
6696 : return NULL_TREE;
6697 90065554 : if (INDIRECT_REF_P (whole)
6698 90065554 : && integer_zerop (TREE_OPERAND (whole, 0)))
6699 : {
6700 177 : if (!ctx->quiet)
6701 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6702 177 : *non_constant_p = true;
6703 177 : return t;
6704 : }
6705 :
6706 90065377 : if (TREE_CODE (whole) == PTRMEM_CST)
6707 1651 : whole = cplus_expand_constant (whole);
6708 90065377 : if (whole == orig_whole)
6709 : return t;
6710 61126744 : if (lval == vc_discard)
6711 : return t;
6712 61126696 : if (lval)
6713 21704964 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6714 : whole, part, NULL_TREE);
6715 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6716 : CONSTRUCTOR. */
6717 39421732 : if (TREE_CODE (whole) != CONSTRUCTOR)
6718 : {
6719 0 : if (!ctx->quiet)
6720 0 : error ("%qE is not a constant expression", orig_whole);
6721 0 : *non_constant_p = true;
6722 0 : return t;
6723 : }
6724 39420237 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6725 39423409 : && DECL_MUTABLE_P (part))
6726 : {
6727 144 : if (!ctx->quiet)
6728 16 : error ("mutable %qD is not usable in a constant expression", part);
6729 144 : *non_constant_p = true;
6730 144 : return t;
6731 : }
6732 :
6733 39421588 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6734 59237041 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6735 : {
6736 : /* Use name match for PMF fields, as a variant will have a
6737 : different FIELD_DECL with a different type. */
6738 59011188 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6739 : : field == part)
6740 : {
6741 39195735 : if (value == void_node)
6742 6 : goto uninit;
6743 39195729 : if (value)
6744 : {
6745 39195711 : STRIP_ANY_LOCATION_WRAPPER (value);
6746 39195711 : return value;
6747 : }
6748 : else
6749 : /* We're in the middle of initializing it. */
6750 : break;
6751 : }
6752 : }
6753 225871 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6754 : {
6755 112 : if (CONSTRUCTOR_NELTS (whole) > 0)
6756 : {
6757 : /* DR 1188 says we don't have to deal with this. */
6758 97 : if (!ctx->quiet)
6759 : {
6760 20 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6761 20 : if (cep->value == NULL_TREE)
6762 9 : error ("accessing uninitialized member %qD", part);
6763 : else
6764 11 : error ("accessing %qD member instead of active %qD member "
6765 : "in constant expression", part, cep->index);
6766 : }
6767 97 : *non_constant_p = true;
6768 97 : return t;
6769 : }
6770 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6771 : {
6772 : /* Value-initialized union, check if looking at the first member. */
6773 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6774 12 : if (first != part)
6775 : {
6776 9 : if (!ctx->quiet)
6777 3 : error ("accessing %qD member instead of initialized %qD "
6778 : "member in constant expression", part, first);
6779 9 : *non_constant_p = true;
6780 9 : return t;
6781 : }
6782 : }
6783 : }
6784 :
6785 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6786 : classes never get represented; throw together a value now. */
6787 225765 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6788 209177 : return build_constructor (TREE_TYPE (t), NULL);
6789 :
6790 16588 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6791 :
6792 16588 : if (CONSTRUCTOR_NO_CLEARING (whole))
6793 : {
6794 112 : uninit:
6795 : /* 'whole' is part of the aggregate initializer we're currently
6796 : building; if there's no initializer for this member yet, that's an
6797 : error. */
6798 118 : if (!ctx->quiet)
6799 23 : error ("accessing uninitialized member %qD", part);
6800 118 : *non_constant_p = true;
6801 118 : return t;
6802 : }
6803 :
6804 : /* If there's no explicit init for this field, it's value-initialized. */
6805 :
6806 16476 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6807 : {
6808 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6809 : and copy the flags. */
6810 15869 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6811 15869 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6812 15869 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6813 15869 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6814 15869 : return value;
6815 : }
6816 :
6817 607 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6818 607 : return cxx_eval_constant_expression (ctx, value,
6819 : lval,
6820 : non_constant_p, overflow_p,
6821 607 : jump_target);
6822 : }
6823 :
6824 : /* Subroutine of cxx_eval_constant_expression.
6825 : Attempt to reduce a field access of a value of class type that is
6826 : expressed as a BIT_FIELD_REF. */
6827 :
6828 : static tree
6829 40745 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6830 : value_cat lval,
6831 : bool *non_constant_p, bool *overflow_p,
6832 : tree *jump_target)
6833 : {
6834 40745 : tree orig_whole = TREE_OPERAND (t, 0);
6835 40745 : tree retval, fldval, utype, mask;
6836 40745 : bool fld_seen = false;
6837 40745 : HOST_WIDE_INT istart, isize;
6838 40745 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6839 : lval,
6840 : non_constant_p, overflow_p,
6841 : jump_target);
6842 40745 : tree start, field, value;
6843 40745 : unsigned HOST_WIDE_INT i;
6844 :
6845 40745 : if (*jump_target)
6846 : return NULL_TREE;
6847 40745 : if (whole == orig_whole)
6848 : return t;
6849 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6850 : CONSTRUCTOR. */
6851 40596 : if (!*non_constant_p
6852 40596 : && TREE_CODE (whole) != VECTOR_CST
6853 0 : && TREE_CODE (whole) != CONSTRUCTOR)
6854 : {
6855 0 : if (!ctx->quiet)
6856 0 : error ("%qE is not a constant expression", orig_whole);
6857 0 : *non_constant_p = true;
6858 : }
6859 40596 : if (*non_constant_p)
6860 : return t;
6861 :
6862 40596 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6863 : {
6864 40596 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6865 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6866 : return r;
6867 0 : if (!ctx->quiet)
6868 0 : error ("%qE is not a constant expression", orig_whole);
6869 0 : *non_constant_p = true;
6870 0 : return t;
6871 : }
6872 :
6873 0 : start = TREE_OPERAND (t, 2);
6874 0 : istart = tree_to_shwi (start);
6875 0 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6876 0 : utype = TREE_TYPE (t);
6877 0 : if (!TYPE_UNSIGNED (utype))
6878 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6879 0 : retval = build_int_cst (utype, 0);
6880 0 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6881 : {
6882 0 : tree bitpos = bit_position (field);
6883 0 : STRIP_ANY_LOCATION_WRAPPER (value);
6884 0 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6885 : return value;
6886 0 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6887 0 : && TREE_CODE (value) == INTEGER_CST
6888 0 : && tree_fits_shwi_p (bitpos)
6889 0 : && tree_fits_shwi_p (DECL_SIZE (field)))
6890 : {
6891 0 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6892 0 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6893 0 : HOST_WIDE_INT shift;
6894 0 : if (bit >= istart && bit + sz <= istart + isize)
6895 : {
6896 0 : fldval = fold_convert (utype, value);
6897 0 : mask = build_int_cst_type (utype, -1);
6898 0 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6899 : size_int (TYPE_PRECISION (utype) - sz));
6900 0 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6901 : size_int (TYPE_PRECISION (utype) - sz));
6902 0 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6903 0 : shift = bit - istart;
6904 0 : if (BYTES_BIG_ENDIAN)
6905 : shift = TYPE_PRECISION (utype) - shift - sz;
6906 0 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6907 : size_int (shift));
6908 0 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6909 0 : fld_seen = true;
6910 : }
6911 : }
6912 : }
6913 0 : if (fld_seen)
6914 0 : return fold_convert (TREE_TYPE (t), retval);
6915 0 : gcc_unreachable ();
6916 : return error_mark_node;
6917 : }
6918 :
6919 : /* Helper for cxx_eval_bit_cast.
6920 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6921 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6922 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6923 : data members of reference or std::meta::info type. */
6924 :
6925 : static bool
6926 56312 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6927 : tree orig_type)
6928 : {
6929 57312 : if (TREE_CODE (type) == UNION_TYPE)
6930 : {
6931 63 : if (!ctx->quiet)
6932 : {
6933 21 : if (type == orig_type)
6934 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6935 : "a union type", "__builtin_bit_cast", type);
6936 : else
6937 15 : error_at (loc, "%qs is not a constant expression because %qT "
6938 : "contains a union type", "__builtin_bit_cast",
6939 : orig_type);
6940 : }
6941 : return true;
6942 : }
6943 : if (TREE_CODE (type) == POINTER_TYPE)
6944 : {
6945 57 : if (!ctx->quiet)
6946 : {
6947 18 : if (type == orig_type)
6948 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6949 : "a pointer type", "__builtin_bit_cast", type);
6950 : else
6951 12 : error_at (loc, "%qs is not a constant expression because %qT "
6952 : "contains a pointer type", "__builtin_bit_cast",
6953 : orig_type);
6954 : }
6955 : return true;
6956 : }
6957 : if (TREE_CODE (type) == REFERENCE_TYPE)
6958 : {
6959 0 : if (!ctx->quiet)
6960 : {
6961 0 : if (type == orig_type)
6962 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6963 : "a reference type", "__builtin_bit_cast", type);
6964 : else
6965 0 : error_at (loc, "%qs is not a constant expression because %qT "
6966 : "contains a reference type", "__builtin_bit_cast",
6967 : orig_type);
6968 : }
6969 : return true;
6970 : }
6971 39316 : if (TYPE_PTRMEM_P (type))
6972 : {
6973 36 : if (!ctx->quiet)
6974 : {
6975 12 : if (type == orig_type)
6976 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6977 : "a pointer to member type", "__builtin_bit_cast",
6978 : type);
6979 : else
6980 0 : error_at (loc, "%qs is not a constant expression because %qT "
6981 : "contains a pointer to member type",
6982 : "__builtin_bit_cast", orig_type);
6983 : }
6984 : return true;
6985 : }
6986 57156 : if (TYPE_VOLATILE (type))
6987 : {
6988 0 : if (!ctx->quiet)
6989 : {
6990 0 : if (type == orig_type)
6991 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6992 : "volatile", "__builtin_bit_cast", type);
6993 : else
6994 0 : error_at (loc, "%qs is not a constant expression because %qT "
6995 : "contains a volatile subobject",
6996 : "__builtin_bit_cast", orig_type);
6997 : }
6998 : return true;
6999 : }
7000 57156 : if (REFLECTION_TYPE_P (type))
7001 : {
7002 22 : if (!ctx->quiet)
7003 : {
7004 6 : if (type == orig_type)
7005 4 : error_at (loc, "%qs is not a constant expression because its "
7006 : "type is %qs", "__builtin_bit_cast", "std::meta::info");
7007 : else
7008 2 : error_at (loc, "%qs is not a constant expression because %qT "
7009 : "contains %qs type",
7010 : "__builtin_bit_cast", orig_type, "std::meta::info");
7011 : }
7012 : return true;
7013 : }
7014 57134 : if (TREE_CODE (type) == RECORD_TYPE)
7015 2294092 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
7016 2254890 : if (TREE_CODE (field) == FIELD_DECL
7017 2254890 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
7018 : return true;
7019 57038 : if (TREE_CODE (type) == ARRAY_TYPE)
7020 1000 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
7021 : return false;
7022 : }
7023 :
7024 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
7025 : std::byte members of CONSTRUCTOR (recursively) if they contain
7026 : some indeterminate bits (as set in MASK), remove the ctor elts,
7027 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
7028 : bits in MASK. */
7029 :
7030 : static void
7031 9631 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
7032 : {
7033 9631 : if (TREE_CODE (t) != CONSTRUCTOR)
7034 : return;
7035 :
7036 : unsigned i, j = 0;
7037 : tree index, value;
7038 23564 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
7039 : {
7040 13933 : tree type = TREE_TYPE (value);
7041 13933 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
7042 27427 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
7043 : {
7044 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
7045 : {
7046 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
7047 135 : gcc_assert (fldsz != 0);
7048 135 : HOST_WIDE_INT pos = int_byte_position (index);
7049 135 : HOST_WIDE_INT bpos
7050 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
7051 135 : bpos %= BITS_PER_UNIT;
7052 135 : HOST_WIDE_INT end
7053 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
7054 135 : gcc_assert (end == 1 || end == 2);
7055 135 : unsigned char *p = mask + pos;
7056 135 : unsigned char mask_save[2];
7057 135 : mask_save[0] = mask[pos];
7058 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
7059 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7060 : sorry_at (loc, "PDP11 bit-field handling unsupported"
7061 : " in %qs", "__builtin_bit_cast");
7062 135 : else if (BYTES_BIG_ENDIAN)
7063 : {
7064 : /* Big endian. */
7065 : if (bpos + fldsz <= BITS_PER_UNIT)
7066 : *p &= ~(((1 << fldsz) - 1)
7067 : << (BITS_PER_UNIT - bpos - fldsz));
7068 : else
7069 : {
7070 : gcc_assert (bpos);
7071 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
7072 : p++;
7073 : fldsz -= BITS_PER_UNIT - bpos;
7074 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
7075 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
7076 : }
7077 : }
7078 : else
7079 : {
7080 : /* Little endian. */
7081 135 : if (bpos + fldsz <= BITS_PER_UNIT)
7082 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
7083 : else
7084 : {
7085 0 : gcc_assert (bpos);
7086 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
7087 0 : p++;
7088 0 : fldsz -= BITS_PER_UNIT - bpos;
7089 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
7090 0 : *p &= ~((1 << fldsz) - 1);
7091 : }
7092 : }
7093 135 : if (mask_save[0] != mask[pos]
7094 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
7095 : {
7096 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
7097 36 : continue;
7098 : }
7099 : }
7100 : }
7101 13663 : else if (is_byte_access_type_not_plain_char (type))
7102 : {
7103 228 : HOST_WIDE_INT pos;
7104 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7105 132 : pos = tree_to_shwi (index);
7106 : else
7107 96 : pos = int_byte_position (index);
7108 228 : if (mask[pos])
7109 : {
7110 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
7111 48 : mask[pos] = 0;
7112 48 : continue;
7113 : }
7114 : }
7115 13849 : if (TREE_CODE (value) == CONSTRUCTOR)
7116 : {
7117 7292 : HOST_WIDE_INT pos;
7118 7292 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
7119 144 : pos = tree_to_shwi (index)
7120 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
7121 : else
7122 7220 : pos = int_byte_position (index);
7123 7292 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
7124 : }
7125 13849 : if (i != j)
7126 : {
7127 180 : CONSTRUCTOR_ELT (t, j)->index = index;
7128 180 : CONSTRUCTOR_ELT (t, j)->value = value;
7129 : }
7130 13849 : ++j;
7131 : }
7132 19262 : if (CONSTRUCTOR_NELTS (t) != j)
7133 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
7134 : }
7135 :
7136 : /* Subroutine of cxx_eval_constant_expression.
7137 : Attempt to evaluate a BIT_CAST_EXPR. */
7138 :
7139 : static tree
7140 4047 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
7141 : bool *overflow_p, tree *jump_target)
7142 : {
7143 4047 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7144 4047 : TREE_TYPE (t))
7145 12767 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
7146 3950 : EXPR_LOCATION (t)),
7147 3950 : TREE_TYPE (TREE_OPERAND (t, 0)),
7148 3950 : TREE_TYPE (TREE_OPERAND (t, 0))))
7149 : {
7150 178 : *non_constant_p = true;
7151 178 : return t;
7152 : }
7153 :
7154 3869 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
7155 : non_constant_p, overflow_p,
7156 : jump_target);
7157 3869 : if (*non_constant_p)
7158 : return t;
7159 2820 : if (*jump_target)
7160 : return NULL_TREE;
7161 :
7162 2820 : location_t loc = EXPR_LOCATION (t);
7163 2820 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
7164 : {
7165 : if (!ctx->quiet)
7166 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
7167 : "__builtin_bit_cast");
7168 : *non_constant_p = true;
7169 : return t;
7170 : }
7171 :
7172 2820 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
7173 : {
7174 0 : if (!ctx->quiet)
7175 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7176 : "type is too large", "__builtin_bit_cast");
7177 0 : *non_constant_p = true;
7178 0 : return t;
7179 : }
7180 :
7181 2820 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
7182 2820 : if (len < 0 || (int) len != len)
7183 : {
7184 0 : if (!ctx->quiet)
7185 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7186 : "type is too large", "__builtin_bit_cast");
7187 0 : *non_constant_p = true;
7188 0 : return t;
7189 : }
7190 :
7191 2820 : unsigned char buf[64];
7192 2820 : unsigned char *ptr, *mask;
7193 2820 : size_t alen = (size_t) len * 2;
7194 2820 : if (alen <= sizeof (buf))
7195 : ptr = buf;
7196 : else
7197 329 : ptr = XNEWVEC (unsigned char, alen);
7198 2820 : mask = ptr + (size_t) len;
7199 : /* At the beginning consider everything indeterminate. */
7200 2820 : memset (mask, ~0, (size_t) len);
7201 :
7202 2820 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
7203 : {
7204 0 : if (!ctx->quiet)
7205 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7206 : "argument cannot be encoded", "__builtin_bit_cast");
7207 0 : *non_constant_p = true;
7208 0 : if (ptr != buf)
7209 0 : XDELETE (ptr);
7210 : return t;
7211 : }
7212 :
7213 2820 : tree r = NULL_TREE;
7214 2820 : if (can_native_interpret_type_p (TREE_TYPE (t)))
7215 : {
7216 481 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
7217 481 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
7218 : {
7219 52 : gcc_assert (len == 1);
7220 52 : if (mask[0])
7221 : {
7222 27 : memset (mask, 0, len);
7223 27 : r = build_constructor (TREE_TYPE (r), NULL);
7224 27 : CONSTRUCTOR_NO_CLEARING (r) = 1;
7225 : }
7226 : }
7227 : }
7228 2339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
7229 : {
7230 2339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
7231 2339 : if (r != NULL_TREE)
7232 : {
7233 2339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
7234 2339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
7235 2339 : if (CHECKING_P)
7236 : {
7237 2339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
7238 : non_constant_p, overflow_p,
7239 : jump_target);
7240 2339 : gcc_checking_assert (e == r && !*jump_target);
7241 : r = e;
7242 : }
7243 : }
7244 : }
7245 :
7246 2820 : if (r != NULL_TREE)
7247 : {
7248 92041 : for (int i = 0; i < len; i++)
7249 89311 : if (mask[i])
7250 : {
7251 90 : if (!ctx->quiet)
7252 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
7253 : "__builtin_bit_cast", i);
7254 90 : *non_constant_p = true;
7255 90 : r = t;
7256 90 : break;
7257 : }
7258 2820 : if (ptr != buf)
7259 329 : XDELETE (ptr);
7260 : return r;
7261 : }
7262 :
7263 0 : if (!ctx->quiet)
7264 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
7265 : "argument cannot be interpreted", "__builtin_bit_cast");
7266 0 : *non_constant_p = true;
7267 0 : if (ptr != buf)
7268 0 : XDELETE (ptr);
7269 : return t;
7270 : }
7271 :
7272 : /* Subroutine of cxx_eval_constant_expression.
7273 : Evaluate a short-circuited logical expression T in the context
7274 : of a given constexpr CALL. BAILOUT_VALUE is the value for
7275 : early return. CONTINUE_VALUE is used here purely for
7276 : sanity check purposes. */
7277 :
7278 : static tree
7279 18581932 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
7280 : tree bailout_value, tree continue_value,
7281 : bool *non_constant_p, bool *overflow_p,
7282 : tree *jump_target)
7283 : {
7284 18581932 : tree r;
7285 18581932 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
7286 : vc_prvalue, non_constant_p,
7287 : overflow_p, jump_target);
7288 18581930 : if (*jump_target)
7289 : return NULL_TREE;
7290 18581921 : VERIFY_CONSTANT (lhs);
7291 13839701 : if (tree_int_cst_equal (lhs, bailout_value))
7292 : return lhs;
7293 11783038 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
7294 11783038 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
7295 : vc_prvalue, non_constant_p,
7296 : overflow_p, jump_target);
7297 11783038 : if (*jump_target)
7298 : return NULL_TREE;
7299 11783038 : VERIFY_CONSTANT (r);
7300 : return r;
7301 : }
7302 :
7303 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
7304 : CONSTRUCTOR elements to initialize (part of) an object containing that
7305 : field. Return a pointer to the constructor_elt corresponding to the
7306 : initialization of the field. */
7307 :
7308 : static constructor_elt *
7309 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
7310 : {
7311 0 : tree aggr = TREE_OPERAND (ref, 0);
7312 0 : tree field = TREE_OPERAND (ref, 1);
7313 0 : HOST_WIDE_INT i;
7314 0 : constructor_elt *ce;
7315 :
7316 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
7317 :
7318 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
7319 : {
7320 0 : constructor_elt *base_ce
7321 0 : = base_field_constructor_elt (v, aggr);
7322 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
7323 : }
7324 :
7325 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
7326 0 : if (ce->index == field)
7327 0 : return ce;
7328 :
7329 0 : gcc_unreachable ();
7330 : return NULL;
7331 : }
7332 :
7333 : /* Some of the expressions fed to the constexpr mechanism are calls to
7334 : constructors, which have type void. In that case, return the type being
7335 : initialized by the constructor. */
7336 :
7337 : static tree
7338 606342129 : initialized_type (tree t)
7339 : {
7340 608003880 : if (TYPE_P (t))
7341 : return t;
7342 608003416 : tree type = TREE_TYPE (t);
7343 608003416 : if (TREE_CODE (t) == CALL_EXPR)
7344 : {
7345 : /* A constructor call has void type, so we need to look deeper. */
7346 73302067 : tree fn = get_function_named_in_call (t);
7347 73295472 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
7348 146473145 : && DECL_CXX_CONSTRUCTOR_P (fn))
7349 4054357 : type = DECL_CONTEXT (fn);
7350 : }
7351 534701349 : else if (TREE_CODE (t) == COMPOUND_EXPR)
7352 1661751 : return initialized_type (TREE_OPERAND (t, 1));
7353 533039598 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
7354 873890 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
7355 606341665 : return cv_unqualified (type);
7356 : }
7357 :
7358 : /* We're about to initialize element INDEX of an array or class from VALUE.
7359 : Set up NEW_CTX appropriately by adjusting .object to refer to the
7360 : subobject and creating a new CONSTRUCTOR if the element is itself
7361 : a class or array. */
7362 :
7363 : static void
7364 4181045 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
7365 : tree index, tree &value)
7366 : {
7367 4181045 : new_ctx = *ctx;
7368 :
7369 4181045 : if (index && TREE_CODE (index) != INTEGER_CST
7370 3743792 : && TREE_CODE (index) != FIELD_DECL
7371 3 : && TREE_CODE (index) != RANGE_EXPR)
7372 : /* This won't have an element in the new CONSTRUCTOR. */
7373 : return;
7374 :
7375 4181045 : tree type = initialized_type (value);
7376 4181045 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
7377 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
7378 : return;
7379 :
7380 1684335 : tree ctxtype = NULL_TREE;
7381 1684335 : if (ctx->ctor)
7382 1684318 : ctxtype = TREE_TYPE (ctx->ctor);
7383 17 : else if (ctx->object)
7384 14 : ctxtype = TREE_TYPE (ctx->object);
7385 : else
7386 : {
7387 : /* This can happen if the enclosing object is also an empty subobject
7388 : (c++/125315). */
7389 3 : gcc_checking_assert (is_empty_field (index));
7390 : return;
7391 : }
7392 :
7393 1684332 : if (VECTOR_TYPE_P (type)
7394 7417 : && VECTOR_TYPE_P (ctxtype)
7395 2596 : && index == NULL_TREE)
7396 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
7397 : vector is constructed from smaller vectors, doesn't get its own
7398 : CONSTRUCTOR either. */
7399 : return;
7400 :
7401 : /* The sub-aggregate initializer might contain a placeholder;
7402 : update object to refer to the subobject and ctor to refer to
7403 : the (newly created) sub-initializer. */
7404 1681736 : if (ctx->object)
7405 : {
7406 1521279 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
7407 : /* There's no well-defined subobject for this index. */
7408 3 : new_ctx.object = NULL_TREE;
7409 : else
7410 1521276 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
7411 : }
7412 :
7413 1681736 : if (is_empty_field (index)
7414 1681736 : && TREE_CODE (ctxtype) != UNION_TYPE)
7415 : /* Leave ctor null for an empty subobject of a non-union class, they aren't
7416 : represented in the result of evaluation. */
7417 1505030 : new_ctx.ctor = NULL_TREE;
7418 : else
7419 : {
7420 176706 : tree elt = build_constructor (type, NULL);
7421 176706 : CONSTRUCTOR_NO_CLEARING (elt) = true;
7422 176706 : new_ctx.ctor = elt;
7423 : }
7424 :
7425 1681736 : if (TREE_CODE (value) == TARGET_EXPR)
7426 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
7427 121052 : value = TARGET_EXPR_INITIAL (value);
7428 : }
7429 :
7430 : /* We're about to process an initializer for a class or array TYPE. Make
7431 : sure that CTX is set up appropriately. */
7432 :
7433 : static void
7434 3373515 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
7435 : {
7436 : /* We don't bother building a ctor for an empty base subobject. */
7437 3373515 : if (is_empty_class (type))
7438 : return;
7439 :
7440 : /* We're in the middle of an initializer that might involve placeholders;
7441 : our caller should have created a CONSTRUCTOR for us to put the
7442 : initializer into. We will either return that constructor or T. */
7443 1886285 : gcc_assert (ctx->ctor);
7444 1886285 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
7445 : (type, TREE_TYPE (ctx->ctor)));
7446 : /* We used to check that ctx->ctor was empty, but that isn't the case when
7447 : the object is zero-initialized before calling the constructor. */
7448 1886285 : if (ctx->object)
7449 : {
7450 1670782 : tree otype = TREE_TYPE (ctx->object);
7451 1670782 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
7452 : /* Handle flexible array members. */
7453 : || (TREE_CODE (otype) == ARRAY_TYPE
7454 : && TYPE_DOMAIN (otype) == NULL_TREE
7455 : && TREE_CODE (type) == ARRAY_TYPE
7456 : && (same_type_ignoring_top_level_qualifiers_p
7457 : (TREE_TYPE (type), TREE_TYPE (otype)))));
7458 : }
7459 1886285 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
7460 : || ctx->global->get_value (ctx->object) == ctx->ctor);
7461 : }
7462 :
7463 : /* Subroutine of cxx_eval_constant_expression.
7464 : The expression tree T denotes a C-style array or a C-style
7465 : aggregate. Reduce it to a constant expression. */
7466 :
7467 : static tree
7468 3372607 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
7469 : value_cat lval,
7470 : bool *non_constant_p, bool *overflow_p,
7471 : tree *jump_target)
7472 : {
7473 3372607 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
7474 3372607 : bool changed = false;
7475 3372607 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
7476 3372607 : tree type = TREE_TYPE (t);
7477 :
7478 3372607 : constexpr_ctx new_ctx;
7479 3372607 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
7480 : {
7481 : /* We don't really need the ctx->ctor business for a PMF or
7482 : vector, but it's simpler to use the same code. */
7483 56004 : new_ctx = *ctx;
7484 56004 : new_ctx.ctor = build_constructor (type, NULL);
7485 56004 : new_ctx.object = NULL_TREE;
7486 56004 : ctx = &new_ctx;
7487 3372607 : };
7488 3372607 : verify_ctor_sanity (ctx, type);
7489 3372607 : vec<constructor_elt, va_gc> **p = nullptr;
7490 3372607 : if (ctx->ctor)
7491 : {
7492 3372590 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
7493 6741729 : vec_alloc (*p, vec_safe_length (v));
7494 3372590 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
7495 16085 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
7496 : }
7497 :
7498 3372607 : unsigned i;
7499 3372607 : tree index, value;
7500 3372607 : bool constant_p = true;
7501 3372607 : bool side_effects_p = false;
7502 6825258 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
7503 : {
7504 4174604 : tree orig_value = value;
7505 4174604 : init_subob_ctx (ctx, new_ctx, index, value);
7506 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
7507 4174604 : bool no_slot = new_ctx.ctor == NULL_TREE;
7508 4174604 : int pos_hint = -1;
7509 4174604 : if (!ctx->ctor)
7510 : /* The enclosing object could be an empty subobject so we have no
7511 : CONSTRUCTOR (c++/125336). */
7512 17 : gcc_checking_assert (is_empty_class (type));
7513 4174587 : else if (new_ctx.ctor != ctx->ctor && !no_slot)
7514 : {
7515 : /* If we built a new CONSTRUCTOR, attach it now so that other
7516 : initializers can refer to it. */
7517 170566 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
7518 170566 : cep->value = new_ctx.ctor;
7519 170566 : pos_hint = cep - (*p)->begin();
7520 170566 : }
7521 4004021 : else if (TREE_CODE (type) == UNION_TYPE)
7522 : /* Otherwise if we're constructing a non-aggregate union member, set
7523 : the active union member now so that we can later detect and diagnose
7524 : if its initializer attempts to activate another member. */
7525 1089 : get_or_insert_ctor_field (ctx->ctor, index);
7526 4174604 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
7527 : lval,
7528 : non_constant_p, overflow_p,
7529 : jump_target);
7530 4174604 : if (*jump_target)
7531 : return NULL_TREE;
7532 : /* Don't VERIFY_CONSTANT here. */
7533 4174598 : if (ctx->quiet && *non_constant_p)
7534 : break;
7535 3452651 : if (elt != orig_value)
7536 1239509 : changed = true;
7537 :
7538 3452651 : if (!TREE_CONSTANT (elt))
7539 954310 : constant_p = false;
7540 3452651 : if (TREE_SIDE_EFFECTS (elt))
7541 281 : side_effects_p = true;
7542 3452651 : if (index && TREE_CODE (index) == COMPONENT_REF)
7543 : {
7544 : /* This is an initialization of a vfield inside a base
7545 : subaggregate that we already initialized; push this
7546 : initialization into the previous initialization. */
7547 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
7548 0 : inner->value = elt;
7549 0 : changed = true;
7550 0 : }
7551 3452651 : else if (no_slot)
7552 : /* This is an initializer for an empty field; now that we've
7553 : checked that it's constant, we can ignore it. */
7554 : changed = true;
7555 1948683 : else if (ctx->ctor)
7556 : {
7557 1948683 : if (TREE_CODE (type) == UNION_TYPE
7558 1948683 : && (*p)->last().index != index)
7559 : /* The initializer erroneously changed the active union member that
7560 : we're initializing. */
7561 7 : gcc_assert (*non_constant_p);
7562 : else
7563 : {
7564 : /* The initializer might have mutated the underlying CONSTRUCTOR,
7565 : so recompute the location of the target constructer_elt. */
7566 1948676 : constructor_elt *cep
7567 1948676 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
7568 1948676 : cep->value = elt;
7569 : }
7570 :
7571 : /* Adding or replacing an element might change the ctor's flags. */
7572 1948683 : TREE_CONSTANT (ctx->ctor) = constant_p;
7573 1948683 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
7574 : }
7575 : }
7576 3372601 : if (*non_constant_p)
7577 : return t;
7578 2650587 : if (!changed)
7579 : {
7580 268175 : if (VECTOR_TYPE_P (type))
7581 14411 : t = fold (t);
7582 : return t;
7583 : }
7584 2382412 : t = ctx->ctor;
7585 2382412 : if (!t)
7586 8 : t = build_constructor (type, NULL);
7587 : /* We're done building this CONSTRUCTOR, so now we can interpret an
7588 : element without an explicit initializer as value-initialized. */
7589 2382412 : CONSTRUCTOR_NO_CLEARING (t) = false;
7590 2382412 : TREE_CONSTANT (t) = constant_p;
7591 2382412 : TREE_SIDE_EFFECTS (t) = side_effects_p;
7592 2382412 : if (VECTOR_TYPE_P (type))
7593 11444 : t = fold (t);
7594 : return t;
7595 : }
7596 :
7597 : /* Subroutine of cxx_eval_constant_expression.
7598 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
7599 : initialization of a non-static data member of array type. Reduce it to a
7600 : CONSTRUCTOR.
7601 :
7602 : Note that apart from value-initialization (when VALUE_INIT is true),
7603 : this is only intended to support value-initialization and the
7604 : initializations done by defaulted constructors for classes with
7605 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
7606 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
7607 : for the copy/move constructor. */
7608 :
7609 : static tree
7610 908 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
7611 : bool value_init, value_cat lval,
7612 : bool *non_constant_p, bool *overflow_p,
7613 : tree *jump_target)
7614 : {
7615 908 : tree elttype = TREE_TYPE (atype);
7616 908 : verify_ctor_sanity (ctx, atype);
7617 908 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
7618 908 : bool pre_init = false;
7619 908 : unsigned HOST_WIDE_INT i;
7620 908 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7621 :
7622 908 : if (init && TREE_CODE (init) == CONSTRUCTOR)
7623 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
7624 0 : non_constant_p, overflow_p, jump_target);
7625 :
7626 : /* We already checked access when building the VEC_INIT_EXPR. */
7627 908 : deferring_access_check_sentinel acs (dk_deferred);
7628 :
7629 : /* For the default constructor, build up a call to the default
7630 : constructor of the element type. We only need to handle class types
7631 : here, as for a constructor to be constexpr, all members must be
7632 : initialized, which for a defaulted default constructor means they must
7633 : be of a class type with a constexpr default constructor. */
7634 908 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7635 : /* We only do this at the lowest level. */;
7636 854 : else if (value_init)
7637 : {
7638 338 : init = build_value_init (elttype, complain);
7639 338 : pre_init = true;
7640 : }
7641 516 : else if (!init)
7642 : {
7643 404 : releasing_vec argvec;
7644 404 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
7645 : &argvec, elttype, LOOKUP_NORMAL,
7646 : complain);
7647 404 : init = build_aggr_init_expr (elttype, init);
7648 404 : pre_init = true;
7649 404 : }
7650 :
7651 908 : bool zeroed_out = false;
7652 908 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
7653 : {
7654 : /* We're initializing an array object that had been zero-initialized
7655 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
7656 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
7657 : initializers we append to it. */
7658 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
7659 156 : zeroed_out = true;
7660 156 : vec_safe_truncate (*p, 0);
7661 : }
7662 :
7663 908 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
7664 : overflow_p, jump_target);
7665 908 : if (*jump_target)
7666 : return NULL_TREE;
7667 908 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
7668 6678 : for (i = 0; i < max; ++i)
7669 : {
7670 6441 : tree idx = build_int_cst (size_type_node, i);
7671 6441 : tree eltinit;
7672 6441 : bool reuse = false;
7673 6441 : constexpr_ctx new_ctx;
7674 6905 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
7675 6441 : bool no_slot = new_ctx.ctor == NULL_TREE;
7676 6441 : if (new_ctx.ctor != ctx->ctor && !no_slot)
7677 : {
7678 6140 : if (zeroed_out)
7679 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
7680 6140 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
7681 : }
7682 6441 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7683 : {
7684 : /* A multidimensional array; recurse. */
7685 186 : if (value_init || init == NULL_TREE)
7686 : {
7687 176 : eltinit = NULL_TREE;
7688 176 : reuse = i == 0;
7689 : }
7690 : else
7691 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7692 186 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
7693 : value_init, lval, non_constant_p,
7694 : overflow_p, jump_target);
7695 : }
7696 6255 : else if (pre_init)
7697 : {
7698 : /* Initializing an element using value or default initialization
7699 : we just pre-built above. */
7700 5977 : if (init == void_node)
7701 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
7702 10 : return ctx->ctor;
7703 5967 : eltinit = init;
7704 5967 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7705 : /* Clarify what object is being initialized (118285). */
7706 5667 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7707 5967 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7708 : non_constant_p, overflow_p,
7709 : jump_target);
7710 5967 : reuse = i == 0;
7711 : }
7712 : else
7713 : {
7714 : /* Copying an element. */
7715 278 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7716 278 : if (!lvalue_p (init))
7717 232 : eltinit = move (eltinit);
7718 278 : eltinit = (perform_implicit_conversion_flags
7719 278 : (elttype, eltinit, complain,
7720 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7721 271 : if (CLASS_TYPE_P (elttype)
7722 271 : && new_ctx.object
7723 526 : && !error_operand_p (eltinit))
7724 : /* Clarify what object is being initialized (118285). */
7725 244 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7726 278 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7727 : non_constant_p, overflow_p,
7728 : jump_target);
7729 : }
7730 6431 : if (*jump_target)
7731 : return NULL_TREE;
7732 6431 : if (*non_constant_p)
7733 : break;
7734 6251 : if (no_slot)
7735 : {
7736 : /* This is an initializer for an empty subobject; now that we've
7737 : checked that it's constant, we can ignore it. */
7738 0 : gcc_checking_assert (i == 0);
7739 : break;
7740 : }
7741 6251 : else if (new_ctx.ctor != ctx->ctor)
7742 : {
7743 : /* We appended this element above; update the value. */
7744 5957 : gcc_assert ((*p)->last().index == idx);
7745 5957 : (*p)->last().value = eltinit;
7746 : }
7747 : else
7748 294 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7749 : /* Reuse the result of cxx_eval_constant_expression call
7750 : from the first iteration to all others if it is a constant
7751 : initializer that doesn't require relocations. */
7752 6251 : if (reuse
7753 6251 : && max > 1
7754 6251 : && (eltinit == NULL_TREE
7755 634 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7756 634 : == null_pointer_node)))
7757 : {
7758 481 : if (new_ctx.ctor != ctx->ctor)
7759 201 : eltinit = new_ctx.ctor;
7760 481 : tree range = build2 (RANGE_EXPR, size_type_node,
7761 : build_int_cst (size_type_node, 1),
7762 481 : build_int_cst (size_type_node, max - 1));
7763 481 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7764 481 : break;
7765 : }
7766 5770 : else if (i == 0)
7767 234 : vec_safe_reserve (*p, max);
7768 : }
7769 :
7770 898 : if (!*non_constant_p)
7771 : {
7772 718 : init = ctx->ctor;
7773 718 : CONSTRUCTOR_NO_CLEARING (init) = false;
7774 : }
7775 898 : return init;
7776 : }
7777 :
7778 : static tree
7779 800 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7780 : value_cat lval,
7781 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7782 : {
7783 800 : tree atype = TREE_TYPE (t);
7784 800 : tree init = VEC_INIT_EXPR_INIT (t);
7785 800 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7786 800 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7787 : ;
7788 89 : else if (CONSTRUCTOR_NELTS (init) == 0
7789 89 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7790 : {
7791 : /* Handle {} as value-init. */
7792 : init = NULL_TREE;
7793 : value_init = true;
7794 : }
7795 : else
7796 : {
7797 : /* This is a more complicated case, like needing to loop over trailing
7798 : elements; call build_vec_init and evaluate the result. */
7799 78 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7800 78 : constexpr_ctx new_ctx = *ctx;
7801 78 : if (!ctx->object)
7802 : {
7803 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7804 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7805 60 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7806 60 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7807 60 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7808 60 : ctx->global->put_value (new_ctx.object, ctor);
7809 60 : ctx = &new_ctx;
7810 : }
7811 78 : init = expand_vec_init_expr (ctx->object, t, complain);
7812 78 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7813 : overflow_p, jump_target);
7814 : }
7815 722 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7816 : lval, non_constant_p, overflow_p, jump_target);
7817 722 : if (*non_constant_p)
7818 : return t;
7819 : else
7820 568 : return r;
7821 : }
7822 :
7823 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7824 : where the desired type is an array of unknown bounds because the variable
7825 : has had its bounds deduced since the wrapping expression was created. */
7826 :
7827 : static bool
7828 448304117 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7829 : {
7830 448304117 : while (TREE_CODE (type1) == ARRAY_TYPE
7831 144752 : && TREE_CODE (type2) == ARRAY_TYPE
7832 448304121 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7833 : {
7834 2 : type1 = TREE_TYPE (type1);
7835 2 : type2 = TREE_TYPE (type2);
7836 : }
7837 448304117 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7838 : }
7839 :
7840 : /* Try to determine the currently active union member for an expression
7841 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7842 : otherwise return NULL_TREE. */
7843 :
7844 : static tree
7845 86 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7846 : {
7847 86 : constexpr_ctx new_ctx = *ctx;
7848 86 : new_ctx.quiet = true;
7849 86 : bool non_constant_p = false, overflow_p = false;
7850 86 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7851 : &non_constant_p,
7852 : &overflow_p, jump_target);
7853 86 : if (*jump_target)
7854 : return NULL_TREE;
7855 86 : if (TREE_CODE (ctor) == CONSTRUCTOR
7856 32 : && CONSTRUCTOR_NELTS (ctor) == 1
7857 20 : && CONSTRUCTOR_ELT (ctor, 0)->index
7858 106 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7859 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7860 : return NULL_TREE;
7861 : }
7862 :
7863 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7864 :
7865 : static tree
7866 15254577 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7867 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7868 : tree *jump_target)
7869 : {
7870 24826516 : tree optype = TREE_TYPE (op);
7871 24826516 : unsigned HOST_WIDE_INT const_nunits;
7872 24826516 : if (off == 0 && similar_type_p (optype, type))
7873 : return op;
7874 15249808 : else if (cxx_dialect >= cxx26
7875 9447381 : && VAR_P (op)
7876 5445416 : && DECL_VTABLE_OR_VTT_P (op)
7877 15699 : && same_type_ignoring_top_level_qualifiers_p (type,
7878 : ptrdiff_type_node)
7879 15251153 : && POINTER_TYPE_P (strip_array_types (optype)))
7880 : {
7881 : /* We often read some virtual table elements using ptrdiff_t rather
7882 : than pointer type. */
7883 1345 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7884 : strip_array_types (optype),
7885 : op, off, empty_base,
7886 : jump_target))
7887 1345 : return fold_convert (type, ret);
7888 : }
7889 15248463 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7890 15248463 : && similar_type_p (type, TREE_TYPE (optype)))
7891 : {
7892 : /* *(foo *)&complexfoo => __real__ complexfoo */
7893 0 : if (off == 0)
7894 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7895 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7896 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7897 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7898 : }
7899 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7900 15248463 : else if (VECTOR_TYPE_P (optype)
7901 0 : && similar_type_p (type, TREE_TYPE (optype))
7902 15248463 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7903 : {
7904 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7905 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7906 0 : if (off < max_offset && off % part_width == 0)
7907 : {
7908 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7909 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7910 0 : TYPE_SIZE (type), index);
7911 : }
7912 : }
7913 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7914 15248463 : else if (TREE_CODE (optype) == ARRAY_TYPE
7915 9571939 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7916 24820402 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7917 : {
7918 9571939 : tree type_domain = TYPE_DOMAIN (optype);
7919 9571939 : tree min_val = size_zero_node;
7920 9571939 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7921 9571710 : min_val = TYPE_MIN_VALUE (type_domain);
7922 9571939 : unsigned HOST_WIDE_INT el_sz
7923 9571939 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7924 9571939 : unsigned HOST_WIDE_INT idx = off / el_sz;
7925 9571939 : unsigned HOST_WIDE_INT rem = off % el_sz;
7926 9571939 : if (tree_fits_uhwi_p (min_val))
7927 : {
7928 9571939 : tree index = size_int (idx + tree_to_uhwi (min_val));
7929 9571939 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7930 : NULL_TREE, NULL_TREE);
7931 9571939 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7932 9571939 : empty_base, jump_target);
7933 : }
7934 : }
7935 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7936 5676524 : else if (TREE_CODE (optype) == RECORD_TYPE
7937 5676524 : || TREE_CODE (optype) == UNION_TYPE)
7938 : {
7939 5667950 : if (TREE_CODE (optype) == UNION_TYPE)
7940 : /* For unions prefer the currently active member. */
7941 86 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7942 : {
7943 20 : unsigned HOST_WIDE_INT el_sz
7944 20 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7945 20 : if (off < el_sz)
7946 : {
7947 20 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7948 : op, field, NULL_TREE);
7949 20 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7950 : off, empty_base,
7951 : jump_target))
7952 : return ret;
7953 : }
7954 : }
7955 :
7956 : /* Handle conversion to "as base" type. */
7957 5667936 : if (CLASS_TYPE_P (optype)
7958 11335708 : && CLASSTYPE_AS_BASE (optype) == type)
7959 : return op;
7960 :
7961 : /* Handle conversion to an empty base class, which is represented with a
7962 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7963 : which is likely to be a waste of time (109678). */
7964 5662459 : if (is_empty_class (type)
7965 5323626 : && CLASS_TYPE_P (optype)
7966 10986079 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
7967 : {
7968 2581981 : if (empty_base)
7969 2581976 : *empty_base = true;
7970 : return op;
7971 : }
7972 :
7973 3080478 : for (tree field = TYPE_FIELDS (optype);
7974 44155028 : field; field = DECL_CHAIN (field))
7975 44109128 : if (TREE_CODE (field) == FIELD_DECL
7976 3093010 : && TREE_TYPE (field) != error_mark_node
7977 47202138 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
7978 : {
7979 3093006 : tree pos = byte_position (field);
7980 3093006 : if (!tree_fits_uhwi_p (pos))
7981 0 : continue;
7982 3093006 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
7983 3093006 : unsigned HOST_WIDE_INT el_sz;
7984 3093006 : if (DECL_FIELD_IS_BASE (field)
7985 778847 : && CLASS_TYPE_P (optype)
7986 3871853 : && CLASSTYPE_VBASECLASSES (optype))
7987 7765 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
7988 : else
7989 3085241 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7990 3093006 : if (upos <= off && off < upos + el_sz)
7991 : {
7992 3080279 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7993 : op, field, NULL_TREE);
7994 3080279 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7995 : off - upos,
7996 : empty_base,
7997 : jump_target))
7998 : return ret;
7999 : }
8000 : }
8001 : }
8002 :
8003 : return NULL_TREE;
8004 : }
8005 :
8006 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
8007 : match. We want to be less strict for simple *& folding; if we have a
8008 : non-const temporary that we access through a const pointer, that should
8009 : work. We handle this here rather than change fold_indirect_ref_1
8010 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
8011 : don't really make sense outside of constant expression evaluation. Also
8012 : we want to allow folding to COMPONENT_REF, which could cause trouble
8013 : with TBAA in fold_indirect_ref_1. */
8014 :
8015 : static tree
8016 196796258 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
8017 : tree op0, bool *empty_base, tree *jump_target)
8018 : {
8019 196796258 : tree sub = op0;
8020 196796258 : tree subtype;
8021 :
8022 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
8023 405349800 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
8024 529030119 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
8025 : {
8026 133021913 : if (TREE_CODE (sub) == NOP_EXPR
8027 133021913 : && REINTERPRET_CAST_P (sub))
8028 : return NULL_TREE;
8029 133021913 : sub = TREE_OPERAND (sub, 0);
8030 : }
8031 :
8032 196796258 : subtype = TREE_TYPE (sub);
8033 196796258 : if (!INDIRECT_TYPE_P (subtype))
8034 : return NULL_TREE;
8035 :
8036 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
8037 : the innermost component into the offset until it would make the
8038 : offset positive, so that cxx_fold_indirect_ref_1 can identify
8039 : more folding opportunities. */
8040 208969142 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
8041 12172933 : if (cxx_dialect >= cxx26)
8042 : {
8043 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
8044 : to access virtual base members. */
8045 7377041 : tree nobj = obj;
8046 7377041 : while (TREE_CODE (nobj) == COMPONENT_REF
8047 7413042 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
8048 36001 : nobj = TREE_OPERAND (nobj, 0);
8049 7377041 : if (nobj != obj
8050 28296 : && CLASS_TYPE_P (TREE_TYPE (nobj))
8051 7405337 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
8052 2537 : while (obj != nobj)
8053 : {
8054 1424 : tree field = TREE_OPERAND (obj, 1);
8055 1424 : tree pos = byte_position (field);
8056 1424 : off = int_const_binop (PLUS_EXPR, off, pos);
8057 1424 : obj = TREE_OPERAND (obj, 0);
8058 : }
8059 : }
8060 15243337 : while (TREE_CODE (obj) == COMPONENT_REF
8061 : /* We need to preserve union member accesses so that we can
8062 : later properly diagnose accessing the wrong member. */
8063 6371346 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
8064 21391684 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
8065 : {
8066 3255461 : tree field = TREE_OPERAND (obj, 1);
8067 3255461 : tree pos = byte_position (field);
8068 3255461 : if (integer_zerop (off) && integer_nonzerop (pos))
8069 : /* If the offset is already 0, keep going as long as the
8070 : component is at position 0. */
8071 : break;
8072 3070404 : off = int_const_binop (PLUS_EXPR, off, pos);
8073 3070404 : obj = TREE_OPERAND (obj, 0);
8074 : }
8075 12172933 : };
8076 :
8077 196796209 : if (TREE_CODE (sub) == ADDR_EXPR)
8078 : {
8079 87833102 : tree op = TREE_OPERAND (sub, 0);
8080 87833102 : tree optype = TREE_TYPE (op);
8081 :
8082 : /* *&CONST_DECL -> to the value of the const decl. */
8083 87833102 : if (TREE_CODE (op) == CONST_DECL)
8084 0 : return DECL_INITIAL (op);
8085 : /* *&p => p; make sure to handle *&"str"[cst] here. */
8086 87833102 : if (similar_type_p (optype, type))
8087 : {
8088 83978680 : tree fop = fold_read_from_constant_string (op);
8089 83978680 : if (fop)
8090 : return fop;
8091 : else
8092 83768597 : return op;
8093 : }
8094 : else
8095 : {
8096 3854422 : tree off = integer_zero_node;
8097 3854422 : canonicalize_obj_off (op, off);
8098 3854422 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
8099 : tree_to_uhwi (off), empty_base,
8100 : jump_target);
8101 : }
8102 : }
8103 108963107 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
8104 108963107 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
8105 : {
8106 8907112 : tree op00 = TREE_OPERAND (sub, 0);
8107 8907112 : tree off = TREE_OPERAND (sub, 1);
8108 :
8109 8907112 : STRIP_NOPS (op00);
8110 8907112 : if (TREE_CODE (op00) == ADDR_EXPR)
8111 : {
8112 8318511 : tree obj = TREE_OPERAND (op00, 0);
8113 8318511 : canonicalize_obj_off (obj, off);
8114 8318511 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
8115 : tree_to_uhwi (off), empty_base,
8116 : jump_target);
8117 : }
8118 : }
8119 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
8120 100055995 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
8121 100055995 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
8122 : {
8123 0 : tree type_domain;
8124 0 : tree min_val = size_zero_node;
8125 0 : tree newsub
8126 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
8127 : jump_target);
8128 0 : if (*jump_target)
8129 : return NULL_TREE;
8130 0 : if (newsub)
8131 : sub = newsub;
8132 : else
8133 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
8134 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
8135 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
8136 0 : min_val = TYPE_MIN_VALUE (type_domain);
8137 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
8138 0 : NULL_TREE);
8139 : }
8140 :
8141 : return NULL_TREE;
8142 : }
8143 :
8144 : static tree
8145 103916642 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
8146 : value_cat lval,
8147 : bool *non_constant_p, bool *overflow_p,
8148 : tree *jump_target)
8149 : {
8150 103916642 : tree orig_op0 = TREE_OPERAND (t, 0);
8151 103916642 : bool empty_base = false;
8152 :
8153 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
8154 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
8155 :
8156 103916642 : if (TREE_CODE (t) == MEM_REF
8157 103916642 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
8158 : {
8159 9 : gcc_assert (ctx->quiet);
8160 9 : *non_constant_p = true;
8161 9 : return t;
8162 : }
8163 :
8164 : /* First try to simplify it directly. */
8165 103916633 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
8166 : orig_op0, &empty_base, jump_target);
8167 103916633 : if (*jump_target)
8168 : return NULL_TREE;
8169 103916633 : if (!r)
8170 : {
8171 : /* If that didn't work, evaluate the operand first. */
8172 99964472 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
8173 : vc_prvalue, non_constant_p,
8174 : overflow_p, jump_target);
8175 99964472 : if (*jump_target)
8176 : return NULL_TREE;
8177 : /* Don't VERIFY_CONSTANT here. */
8178 99964418 : if (*non_constant_p)
8179 : return t;
8180 :
8181 79207122 : if (!lval && integer_zerop (op0))
8182 : {
8183 73 : if (!ctx->quiet)
8184 12 : error ("dereferencing a null pointer");
8185 73 : *non_constant_p = true;
8186 73 : return t;
8187 : }
8188 :
8189 79207049 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
8190 : &empty_base, jump_target);
8191 79207049 : if (*jump_target)
8192 : return NULL_TREE;
8193 79207049 : if (r == NULL_TREE)
8194 : {
8195 : /* We couldn't fold to a constant value. Make sure it's not
8196 : something we should have been able to fold. */
8197 688609 : tree sub = op0;
8198 688609 : STRIP_NOPS (sub);
8199 688609 : if (TREE_CODE (sub) == ADDR_EXPR)
8200 : {
8201 4135 : gcc_assert (!similar_type_p
8202 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
8203 : /* DR 1188 says we don't have to deal with this. */
8204 4135 : if (!ctx->quiet)
8205 : {
8206 9 : auto_diagnostic_group d;
8207 15 : error_at (cp_expr_loc_or_input_loc (t),
8208 : "accessing value of %qT object through a %qT "
8209 : "glvalue in a constant expression",
8210 9 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
8211 9 : tree ob = build_fold_indirect_ref (sub);
8212 9 : if (DECL_P (ob))
8213 : {
8214 9 : if (DECL_ARTIFICIAL (ob))
8215 2 : inform (DECL_SOURCE_LOCATION (ob),
8216 2 : "%qT object created here", TREE_TYPE (ob));
8217 : else
8218 7 : inform (DECL_SOURCE_LOCATION (ob),
8219 : "%q#D declared here", ob);
8220 : }
8221 9 : }
8222 4135 : *non_constant_p = true;
8223 4135 : return t;
8224 : }
8225 :
8226 684474 : if (lval == vc_glvalue && op0 != orig_op0)
8227 80778 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
8228 603696 : if (!lval)
8229 493756 : VERIFY_CONSTANT (t);
8230 : return t;
8231 : }
8232 : }
8233 :
8234 82470601 : r = cxx_eval_constant_expression (ctx, r,
8235 : lval, non_constant_p, overflow_p,
8236 : jump_target);
8237 82470599 : if (*jump_target)
8238 : return NULL_TREE;
8239 82470599 : if (*non_constant_p)
8240 : return t;
8241 :
8242 : /* If we're pulling out the value of an empty base, just return an empty
8243 : CONSTRUCTOR. */
8244 73492190 : if (empty_base && !lval)
8245 : {
8246 18807 : r = build_constructor (TREE_TYPE (t), NULL);
8247 18807 : TREE_CONSTANT (r) = true;
8248 : }
8249 :
8250 : return r;
8251 : }
8252 :
8253 : /* Complain about R, a DECL that is accessed outside its lifetime. */
8254 :
8255 : static void
8256 45 : outside_lifetime_error (location_t loc, tree r)
8257 : {
8258 45 : auto_diagnostic_group d;
8259 45 : if (DECL_NAME (r) == heap_deleted_identifier)
8260 : {
8261 : /* Provide a more accurate message for deleted variables. */
8262 14 : error_at (loc, "use of allocated storage after deallocation "
8263 : "in a constant expression");
8264 14 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
8265 : }
8266 : else
8267 : {
8268 31 : error_at (loc, "accessing %qE outside its lifetime", r);
8269 31 : inform (DECL_SOURCE_LOCATION (r), "declared here");
8270 : }
8271 45 : }
8272 :
8273 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
8274 : FUNDEF_P is true if we're checking a constexpr function body.
8275 : Shared between potential_constant_expression and
8276 : cxx_eval_constant_expression. */
8277 :
8278 : static void
8279 362 : non_const_var_error (location_t loc, tree r, bool fundef_p)
8280 : {
8281 362 : auto_diagnostic_group d;
8282 362 : tree type = TREE_TYPE (r);
8283 362 : if (heap_var_p (r))
8284 : {
8285 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
8286 : "storage is not usable in a constant expression"))
8287 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
8288 : return;
8289 : }
8290 359 : if (DECL_NAME (r) == heap_deleted_identifier)
8291 : {
8292 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
8293 : "deallocation in a constant expression"))
8294 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
8295 : return;
8296 : }
8297 359 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
8298 : "a constant expression", r))
8299 : return;
8300 : /* Avoid error cascade. */
8301 352 : if (DECL_INITIAL (r) == error_mark_node)
8302 : return;
8303 336 : if (DECL_DECLARED_CONSTEXPR_P (r))
8304 3 : inform (DECL_SOURCE_LOCATION (r),
8305 : "%qD used in its own initializer", r);
8306 333 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
8307 : {
8308 233 : if (!CP_TYPE_CONST_P (type))
8309 204 : inform (DECL_SOURCE_LOCATION (r),
8310 : "%q#D is not const", r);
8311 29 : else if (CP_TYPE_VOLATILE_P (type))
8312 0 : inform (DECL_SOURCE_LOCATION (r),
8313 : "%q#D is volatile", r);
8314 29 : else if (!DECL_INITIAL (r)
8315 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
8316 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
8317 29 : inform (DECL_SOURCE_LOCATION (r),
8318 : "%qD was not initialized with a constant "
8319 : "expression", r);
8320 : else
8321 0 : gcc_unreachable ();
8322 : }
8323 100 : else if (TYPE_REF_P (type))
8324 9 : inform (DECL_SOURCE_LOCATION (r),
8325 : "%qD was not initialized with a constant "
8326 : "expression", r);
8327 : else
8328 : {
8329 91 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
8330 91 : inform (DECL_SOURCE_LOCATION (r),
8331 : "%qD was not declared %<constexpr%>", r);
8332 : else
8333 0 : inform (DECL_SOURCE_LOCATION (r),
8334 : "%qD does not have integral or enumeration type",
8335 : r);
8336 : }
8337 362 : }
8338 :
8339 : /* Subroutine of cxx_eval_constant_expression.
8340 : Like cxx_eval_unary_expression, except for trinary expressions. */
8341 :
8342 : static tree
8343 43640 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
8344 : value_cat lval,
8345 : bool *non_constant_p, bool *overflow_p,
8346 : tree *jump_target)
8347 : {
8348 43640 : int i;
8349 43640 : tree args[3];
8350 43640 : tree val;
8351 :
8352 168053 : for (i = 0; i < 3; i++)
8353 : {
8354 126582 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
8355 : lval,
8356 : non_constant_p, overflow_p,
8357 : jump_target);
8358 126582 : if (*jump_target)
8359 : return NULL_TREE;
8360 126582 : VERIFY_CONSTANT (args[i]);
8361 : }
8362 :
8363 41471 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
8364 : args[0], args[1], args[2]);
8365 41471 : if (val == NULL_TREE)
8366 : return t;
8367 41471 : VERIFY_CONSTANT (val);
8368 : return val;
8369 : }
8370 :
8371 : /* True if T was declared in a function declared to be constexpr, and
8372 : therefore potentially constant in C++14. */
8373 :
8374 : bool
8375 88812646 : var_in_constexpr_fn (tree t)
8376 : {
8377 88812646 : tree ctx = DECL_CONTEXT (t);
8378 88812646 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
8379 169250149 : && DECL_DECLARED_CONSTEXPR_P (ctx));
8380 : }
8381 :
8382 : /* True if a function might be constexpr: either a function that was
8383 : declared constexpr, or a C++17 lambda op(). */
8384 :
8385 : bool
8386 730149114 : maybe_constexpr_fn (tree t)
8387 : {
8388 730149114 : return (DECL_DECLARED_CONSTEXPR_P (t)
8389 223841761 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
8390 945427504 : || (flag_implicit_constexpr
8391 138 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
8392 : }
8393 :
8394 : /* True if T was declared in a function that might be constexpr: either a
8395 : function that was declared constexpr, or a C++17 lambda op(). */
8396 :
8397 : bool
8398 66336099 : var_in_maybe_constexpr_fn (tree t)
8399 : {
8400 132671551 : return (DECL_FUNCTION_SCOPE_P (t)
8401 121242001 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
8402 : }
8403 :
8404 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
8405 : build_over_call we implement trivial copy of a class with tail padding using
8406 : assignment of character arrays, which is valid in normal code, but not in
8407 : constexpr evaluation. We don't need to worry about clobbering tail padding
8408 : in constexpr evaluation, so strip the type punning. */
8409 :
8410 : static void
8411 103720799 : maybe_simplify_trivial_copy (tree &target, tree &init)
8412 : {
8413 103720799 : if (TREE_CODE (target) == MEM_REF
8414 36160 : && TREE_CODE (init) == MEM_REF
8415 35992 : && TREE_TYPE (target) == TREE_TYPE (init)
8416 35992 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
8417 103756791 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
8418 : {
8419 35992 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
8420 35992 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
8421 : }
8422 103720799 : }
8423 :
8424 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
8425 : of constant type. This does not check for 'mutable', so the
8426 : caller is expected to be mindful of that. */
8427 :
8428 : static bool
8429 419 : cref_has_const_field (tree ref)
8430 : {
8431 488 : while (TREE_CODE (ref) == COMPONENT_REF)
8432 : {
8433 479 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
8434 : return true;
8435 69 : ref = TREE_OPERAND (ref, 0);
8436 : }
8437 : return false;
8438 : }
8439 :
8440 : /* Return true if we are modifying something that is const during constant
8441 : expression evaluation. CODE is the code of the statement, OBJ is the
8442 : object in question, MUTABLE_P is true if one of the subobjects were
8443 : declared mutable. */
8444 :
8445 : static bool
8446 107488035 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
8447 : {
8448 : /* If this is initialization, there's no problem. */
8449 107488035 : if (code == INIT_EXPR)
8450 : return false;
8451 :
8452 : /* [basic.type.qualifier] "A const object is an object of type
8453 : const T or a non-mutable subobject of a const object." */
8454 30025153 : if (mutable_p)
8455 : return false;
8456 :
8457 30024411 : if (TREE_READONLY (obj))
8458 : return true;
8459 :
8460 29969608 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
8461 : {
8462 : /* Although a COMPONENT_REF may have a const type, we should
8463 : only consider it modifying a const object when any of the
8464 : field components is const. This can happen when using
8465 : constructs such as const_cast<const T &>(m), making something
8466 : const even though it wasn't declared const. */
8467 34291 : if (TREE_CODE (obj) == COMPONENT_REF)
8468 419 : return cref_has_const_field (obj);
8469 : else
8470 : return true;
8471 : }
8472 :
8473 : return false;
8474 : }
8475 :
8476 : /* Evaluate an INIT_EXPR or MODIFY_EXPR or MODOP_EXPR. MODOP_EXPR means
8477 : we are evaluating __builtin_start_lifetime (init is in the second
8478 : operand rather than third, like for INIT_EXPR and MODIFY_EXPR). */
8479 :
8480 : static tree
8481 103720799 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
8482 : value_cat lval,
8483 : bool *non_constant_p, bool *overflow_p,
8484 : tree *jump_target)
8485 : {
8486 103720799 : constexpr_ctx new_ctx = *ctx;
8487 :
8488 103720799 : tree init = TREE_OPERAND (t, 1);
8489 :
8490 : /* First we figure out where we're storing to. */
8491 103720799 : tree target = TREE_OPERAND (t, 0);
8492 :
8493 103720799 : maybe_simplify_trivial_copy (target, init);
8494 :
8495 103720799 : tree type = TREE_TYPE (target);
8496 103720799 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) != INIT_EXPR;
8497 72677640 : if (preeval && !TREE_CLOBBER_P (init) && TREE_CODE (t) != MODOP_EXPR)
8498 : {
8499 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
8500 71638366 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
8501 42160078 : && TREE_CODE (init) == CALL_EXPR
8502 6952491 : && CALL_EXPR_FN (init) == NULL_TREE
8503 71638443 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
8504 8 : return void_node;
8505 :
8506 : /* Evaluate the value to be stored without knowing what object it will be
8507 : stored in, so that any side-effects happen first. */
8508 71638358 : if (!SCALAR_TYPE_P (type))
8509 203804 : new_ctx.ctor = new_ctx.object = NULL_TREE;
8510 71638358 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8511 : non_constant_p, overflow_p,
8512 : jump_target);
8513 71638354 : if (*jump_target)
8514 : return NULL_TREE;
8515 71637458 : if (*non_constant_p)
8516 : return t;
8517 : }
8518 :
8519 94651639 : bool evaluated = false;
8520 94651639 : if (lval == vc_glvalue)
8521 : {
8522 : /* If we want to return a reference to the target, we need to evaluate it
8523 : as a whole; otherwise, only evaluate the innermost piece to avoid
8524 : building up unnecessary *_REFs. */
8525 0 : target = cxx_eval_constant_expression (ctx, target, lval,
8526 : non_constant_p, overflow_p,
8527 : jump_target);
8528 0 : evaluated = true;
8529 0 : if (*jump_target)
8530 : return NULL_TREE;
8531 0 : if (*non_constant_p)
8532 : return t;
8533 : }
8534 :
8535 : /* Find the underlying variable. */
8536 94651639 : releasing_vec refs;
8537 94651639 : tree object = NULL_TREE;
8538 : /* If we're modifying a const object, save it. */
8539 94651639 : tree const_object_being_modified = NULL_TREE;
8540 94651639 : bool mutable_p = false;
8541 : /* If we see a union, we can't ignore clobbers. */
8542 94651639 : int seen_union = 0;
8543 320113957 : for (tree probe = target; object == NULL_TREE; )
8544 : {
8545 225462783 : switch (TREE_CODE (probe))
8546 : {
8547 36159841 : case BIT_FIELD_REF:
8548 36159841 : case COMPONENT_REF:
8549 36159841 : case ARRAY_REF:
8550 36159841 : {
8551 36159841 : tree ob = TREE_OPERAND (probe, 0);
8552 36159841 : tree elt = TREE_OPERAND (probe, 1);
8553 36159841 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
8554 : mutable_p = true;
8555 36159841 : if (TREE_CODE (probe) == ARRAY_REF)
8556 : {
8557 5846942 : elt = eval_and_check_array_index (ctx, probe, false,
8558 : non_constant_p, overflow_p,
8559 : jump_target);
8560 5846942 : if (*jump_target)
8561 69 : return NULL_TREE;
8562 5846942 : if (*non_constant_p)
8563 : return t;
8564 : }
8565 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
8566 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
8567 : the array isn't const. Instead, check "a" in the next iteration;
8568 : that will detect modifying "const int a[10]". */
8569 30312899 : else if (evaluated
8570 12836861 : && modifying_const_object_p (TREE_CODE (t), probe,
8571 : mutable_p)
8572 30313513 : && const_object_being_modified == NULL_TREE)
8573 : const_object_being_modified = probe;
8574 :
8575 : /* Track named member accesses for unions to validate modifications
8576 : that change active member. */
8577 36159772 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
8578 17476038 : vec_safe_push (refs, probe);
8579 : else
8580 18683734 : vec_safe_push (refs, NULL_TREE);
8581 :
8582 36159772 : vec_safe_push (refs, elt);
8583 36159772 : vec_safe_push (refs, TREE_TYPE (probe));
8584 36159772 : probe = ob;
8585 36159772 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
8586 583751 : ++seen_union;
8587 : }
8588 36159772 : break;
8589 :
8590 30 : case REALPART_EXPR:
8591 30 : gcc_assert (refs->is_empty ());
8592 30 : vec_safe_push (refs, NULL_TREE);
8593 30 : vec_safe_push (refs, probe);
8594 30 : vec_safe_push (refs, TREE_TYPE (probe));
8595 30 : probe = TREE_OPERAND (probe, 0);
8596 30 : break;
8597 :
8598 31 : case IMAGPART_EXPR:
8599 31 : gcc_assert (refs->is_empty ());
8600 31 : vec_safe_push (refs, NULL_TREE);
8601 31 : vec_safe_push (refs, probe);
8602 31 : vec_safe_push (refs, TREE_TYPE (probe));
8603 31 : probe = TREE_OPERAND (probe, 0);
8604 31 : break;
8605 :
8606 189302881 : default:
8607 189302881 : if (evaluated)
8608 : object = probe;
8609 : else
8610 : {
8611 94651707 : tree pvar = tree_strip_any_location_wrapper (probe);
8612 94651707 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
8613 : {
8614 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
8615 : active union member. */
8616 101 : probe = DECL_VALUE_EXPR (pvar);
8617 101 : break;
8618 : }
8619 94651606 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
8620 : non_constant_p, overflow_p,
8621 : jump_target);
8622 94651606 : evaluated = true;
8623 94651606 : if (*jump_target)
8624 : return NULL_TREE;
8625 94651606 : if (*non_constant_p)
8626 : return t;
8627 : }
8628 : break;
8629 : }
8630 : }
8631 :
8632 94651174 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
8633 94651174 : && const_object_being_modified == NULL_TREE)
8634 94651174 : const_object_being_modified = object;
8635 :
8636 94651174 : if (DECL_P (object)
8637 94649160 : && TREE_CLOBBER_P (init)
8638 95712821 : && DECL_NAME (object) == heap_deleted_identifier)
8639 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
8640 : message later when operator delete is called. */
8641 15 : return void_node;
8642 :
8643 : /* And then find/build up our initializer for the path to the subobject
8644 : we're initializing. */
8645 94651159 : tree *valp;
8646 94651159 : if (DECL_P (object))
8647 94649145 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8648 : else
8649 : valp = NULL;
8650 94651159 : if (!valp)
8651 : {
8652 : /* A constant-expression cannot modify objects from outside the
8653 : constant-expression. */
8654 11591 : if (!ctx->quiet)
8655 : {
8656 31 : auto_diagnostic_group d;
8657 31 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
8658 : {
8659 0 : error ("modification of allocated storage after deallocation "
8660 : "is not a constant expression");
8661 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
8662 : }
8663 31 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
8664 : {
8665 14 : if (TREE_CLOBBER_P (init))
8666 6 : error ("destroying %qE outside its lifetime", object);
8667 : else
8668 8 : error ("modification of %qE outside its lifetime "
8669 : "is not a constant expression", object);
8670 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
8671 : }
8672 : else
8673 : {
8674 17 : if (TREE_CLOBBER_P (init))
8675 6 : error ("destroying %qE from outside current evaluation "
8676 : "is not a constant expression", object);
8677 : else
8678 11 : error ("modification of %qE from outside current evaluation "
8679 : "is not a constant expression", object);
8680 : }
8681 31 : }
8682 11591 : *non_constant_p = true;
8683 11591 : return t;
8684 : }
8685 :
8686 : /* Handle explicit end-of-lifetime. */
8687 94639568 : if (TREE_CLOBBER_P (init))
8688 : {
8689 1061581 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
8690 1061581 : && refs->is_empty ())
8691 : {
8692 300609 : ctx->global->destroy_value (object, (CLOBBER_KIND (init)
8693 : > CLOBBER_OBJECT_END));
8694 300609 : return void_node;
8695 : }
8696 :
8697 747731 : if (!seen_union && !*valp
8698 771129 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
8699 10122 : return void_node;
8700 :
8701 : /* Ending the lifetime of a const object is OK. */
8702 : const_object_being_modified = NULL_TREE;
8703 : }
8704 :
8705 94328837 : type = TREE_TYPE (object);
8706 94328837 : bool no_zero_init = true;
8707 94328837 : bool zero_padding_bits = false;
8708 :
8709 188657674 : auto_vec<tree *> ctors;
8710 188657674 : releasing_vec indexes;
8711 188657674 : auto_vec<int> index_pos_hints;
8712 94328837 : bool activated_union_member_p = false;
8713 94328837 : bool empty_base = false;
8714 130172412 : while (!refs->is_empty ())
8715 : {
8716 36130237 : if (*valp == NULL_TREE || *valp == void_node)
8717 : {
8718 2914548 : *valp = build_constructor (type, NULL);
8719 2914548 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8720 2914548 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8721 : }
8722 33215689 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8723 33215689 : TREE_CODE (*valp) == STRING_CST)
8724 : {
8725 : /* An array was initialized with a string constant, and now
8726 : we're writing into one of its elements. Explode the
8727 : single initialization into a set of element
8728 : initializations. */
8729 11379 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8730 :
8731 11379 : tree string = *valp;
8732 11379 : tree elt_type = TREE_TYPE (type);
8733 11379 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8734 11379 : / TYPE_PRECISION (char_type_node));
8735 11379 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8736 11379 : tree ary_ctor = build_constructor (type, NULL);
8737 :
8738 11379 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8739 34305 : for (unsigned ix = 0; ix != num_elts; ix++)
8740 : {
8741 11547 : constructor_elt elt =
8742 : {
8743 11547 : build_int_cst (size_type_node, ix),
8744 11547 : extract_string_elt (string, chars_per_elt, ix)
8745 11547 : };
8746 11547 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8747 : }
8748 :
8749 11379 : *valp = ary_ctor;
8750 : }
8751 :
8752 36130237 : enum tree_code code = TREE_CODE (type);
8753 36130237 : tree reftype = refs->pop();
8754 36130237 : tree index = refs->pop();
8755 36130237 : bool is_access_expr = refs->pop() != NULL_TREE;
8756 :
8757 36130237 : if (code == COMPLEX_TYPE)
8758 : {
8759 61 : if (TREE_CODE (*valp) == COMPLEX_CST)
8760 51 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8761 51 : TREE_IMAGPART (*valp));
8762 10 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8763 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8764 12 : && CONSTRUCTOR_NO_CLEARING (*valp))
8765 : {
8766 2 : tree r = build_constructor (reftype, NULL);
8767 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8768 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8769 : }
8770 61 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8771 61 : ctors.safe_push (valp);
8772 61 : vec_safe_push (indexes, index);
8773 61 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8774 61 : gcc_checking_assert (refs->is_empty ());
8775 : type = reftype;
8776 282149 : break;
8777 : }
8778 :
8779 : /* If the value of object is already zero-initialized, any new ctors for
8780 : subobjects will also be zero-initialized. Similarly with zeroing of
8781 : padding bits. */
8782 36130176 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8783 36130176 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8784 :
8785 36130176 : if (code == RECORD_TYPE && is_empty_field (index))
8786 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8787 : have no data and might have an offset lower than previously declared
8788 : fields, which confuses the middle-end. The code below will notice
8789 : that we don't have a CONSTRUCTOR for our inner target and just
8790 : return init. */
8791 : {
8792 : empty_base = true;
8793 : break;
8794 : }
8795 :
8796 : /* If a union is zero-initialized, its first non-static named data member
8797 : is zero-initialized (and therefore active). */
8798 35848088 : if (code == UNION_TYPE
8799 35848088 : && !no_zero_init
8800 35848088 : && CONSTRUCTOR_NELTS (*valp) == 0)
8801 4079 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8802 4079 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8803 :
8804 : /* Check for implicit change of active member for a union. */
8805 :
8806 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8807 : now allow initializing an array element to activate the array. */
8808 35900364 : auto only_array_refs = [](const releasing_vec &refs)
8809 : {
8810 52281 : for (unsigned i = 1; i < refs->length(); i += 3)
8811 35 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8812 : return false;
8813 : return true;
8814 : };
8815 :
8816 35848088 : if (code == UNION_TYPE
8817 581759 : && (CONSTRUCTOR_NELTS (*valp) == 0
8818 381021 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8819 : /* An INIT_EXPR of the last member in an access chain is always OK,
8820 : but still check implicit change of members earlier on; see
8821 : cpp2a/constexpr-union6.C. */
8822 36061306 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8823 : {
8824 160972 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8825 160972 : tree inner = strip_array_types (reftype);
8826 :
8827 160972 : if (has_active_member && cxx_dialect < cxx20)
8828 : {
8829 58 : if (!ctx->quiet)
8830 19 : error_at (cp_expr_loc_or_input_loc (t),
8831 : "change of the active member of a union "
8832 : "from %qD to %qD is not a constant expression "
8833 : "before C++20",
8834 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8835 : index);
8836 58 : *non_constant_p = true;
8837 : }
8838 160914 : else if ((!is_access_expr
8839 47126 : || (TREE_CLOBBER_P (init)
8840 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8841 47126 : || (TREE_CODE (t) != INIT_EXPR
8842 47114 : && CLASS_TYPE_P (inner)
8843 482 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8844 160999 : && TREE_CODE (t) != MODOP_EXPR)
8845 : {
8846 : /* Diagnose changing active union member after initialization
8847 : without a valid member access expression, as described in
8848 : [class.union.general] p5. */
8849 113797 : if (!ctx->quiet)
8850 : {
8851 33 : auto_diagnostic_group d;
8852 33 : if (has_active_member)
8853 24 : error_at (cp_expr_loc_or_input_loc (t),
8854 : "accessing %qD member instead of initialized "
8855 : "%qD member in constant expression",
8856 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8857 : else
8858 9 : error_at (cp_expr_loc_or_input_loc (t),
8859 : "accessing uninitialized member %qD",
8860 : index);
8861 33 : if (is_access_expr)
8862 3 : inform (DECL_SOURCE_LOCATION (index),
8863 : "%qD does not implicitly begin its lifetime "
8864 : "because %qT does not have a non-deleted "
8865 : "trivial default constructor, use "
8866 : "%<std::construct_at%> instead",
8867 : index, inner);
8868 : else
8869 30 : inform (DECL_SOURCE_LOCATION (index),
8870 : "initializing %qD requires a member access "
8871 : "expression as the left operand of the assignment",
8872 : index);
8873 33 : }
8874 113797 : *non_constant_p = true;
8875 : }
8876 47117 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8877 : {
8878 : /* Diagnose changing the active union member while the union
8879 : is in the process of being initialized. */
8880 18 : if (!ctx->quiet)
8881 6 : error_at (cp_expr_loc_or_input_loc (t),
8882 : "change of the active member of a union "
8883 : "from %qD to %qD during initialization",
8884 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8885 : index);
8886 18 : *non_constant_p = true;
8887 : }
8888 : no_zero_init = true;
8889 : }
8890 :
8891 : /* Ending the lifetime of the active union member means the union no
8892 : longer has an active member. */
8893 581759 : if (code == UNION_TYPE && refs->is_empty ()
8894 100231 : && TREE_CLOBBER_P (init)
8895 35855963 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8896 : {
8897 1799 : vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
8898 4513 : return void_node;
8899 : }
8900 :
8901 35846289 : ctors.safe_push (valp);
8902 35846289 : vec_safe_push (indexes, index);
8903 :
8904 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8905 : uninitialized. */
8906 33942902 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8907 1744510 : && CONSTRUCTOR_NO_CLEARING (*valp)
8908 37400246 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8909 35846289 : constructor_elt *cep
8910 35846289 : = get_or_insert_ctor_field (*valp, index, pos);
8911 35846289 : if (cep == nullptr)
8912 2714 : return void_node;
8913 35843575 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8914 :
8915 35843575 : if (code == UNION_TYPE)
8916 : {
8917 579960 : activated_union_member_p = true;
8918 579960 : --seen_union;
8919 : }
8920 :
8921 35843575 : valp = &cep->value;
8922 35843575 : type = reftype;
8923 : }
8924 :
8925 : /* Change an "as-base" clobber to the real type;
8926 : we don't need to worry about padding in constexpr. */
8927 94324324 : tree itype = initialized_type (init);
8928 94324324 : if (IS_FAKE_BASE_TYPE (itype))
8929 4455 : itype = TYPE_CONTEXT (itype);
8930 :
8931 : /* For initialization of an empty base, the original target will be
8932 : *(base*)this, evaluation of which resolves to the object
8933 : argument, which has the derived type rather than the base type. */
8934 188366560 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8935 94042236 : (itype, type)))
8936 : {
8937 18757 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8938 : empty_base = true;
8939 : }
8940 :
8941 : /* For __builtin_start_lifetime, if it is already within lifetime,
8942 : don't do anything. */
8943 94324324 : if (TREE_CODE (t) == MODOP_EXPR && *valp != NULL_TREE && *valp != void_node)
8944 : return void_node;
8945 :
8946 : /* Detect modifying a constant object in constexpr evaluation.
8947 : We have found a const object that is being modified. Figure out
8948 : if we need to issue an error. Consider
8949 :
8950 : struct A {
8951 : int n;
8952 : constexpr A() : n(1) { n = 2; } // #1
8953 : };
8954 : struct B {
8955 : const A a;
8956 : constexpr B() { a.n = 3; } // #2
8957 : };
8958 : constexpr B b{};
8959 :
8960 : #1 is OK, since we're modifying an object under construction, but
8961 : #2 is wrong, since "a" is const and has been fully constructed.
8962 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8963 : which means that the object is read-only. For the example above, the
8964 : *ctors stack at the point of #2 will look like:
8965 :
8966 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
8967 : ctors[1] = {.n=2} TREE_READONLY = 1
8968 :
8969 : and we're modifying "b.a", so we search the stack and see if the
8970 : constructor for "b.a" has already run. */
8971 94324286 : if (const_object_being_modified)
8972 : {
8973 87550 : bool fail = false;
8974 87550 : tree const_objtype
8975 87550 : = strip_array_types (TREE_TYPE (const_object_being_modified));
8976 87550 : if (!CLASS_TYPE_P (const_objtype))
8977 : fail = true;
8978 : else
8979 : {
8980 : /* [class.ctor]p5 "A constructor can be invoked for a const,
8981 : volatile, or const volatile object. const and volatile
8982 : semantics are not applied on an object under construction.
8983 : They come into effect when the constructor for the most
8984 : derived object ends." */
8985 262849 : for (tree *elt : ctors)
8986 87959 : if (same_type_ignoring_top_level_qualifiers_p
8987 87959 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
8988 : {
8989 87445 : fail = TREE_READONLY (*elt);
8990 87445 : break;
8991 : }
8992 : }
8993 87445 : if (fail)
8994 : {
8995 200 : if (!ctx->quiet)
8996 65 : modifying_const_object_error (t, const_object_being_modified);
8997 200 : *non_constant_p = true;
8998 200 : return t;
8999 : }
9000 : }
9001 :
9002 94324086 : if (!preeval)
9003 : {
9004 : /* We're handling an INIT_EXPR of class type, so the value of the
9005 : initializer can depend on the object it's initializing. */
9006 :
9007 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
9008 : wants to modify it. */
9009 31021241 : if (*valp == NULL_TREE)
9010 : {
9011 29929181 : *valp = build_constructor (type, NULL);
9012 29929181 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
9013 29929181 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
9014 : }
9015 31021241 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
9016 31021241 : new_ctx.object = target;
9017 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
9018 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
9019 : expansion of those trees uses ctx instead. */
9020 31021241 : if (TREE_CODE (init) == TARGET_EXPR)
9021 3269580 : if (tree tinit = TARGET_EXPR_INITIAL (init))
9022 3269580 : init = tinit;
9023 31021241 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
9024 : non_constant_p, overflow_p,
9025 : jump_target);
9026 31021241 : if (*jump_target)
9027 : return NULL_TREE;
9028 : /* The hash table might have moved since the get earlier, and the
9029 : initializer might have mutated the underlying CONSTRUCTORs, so we must
9030 : recompute VALP. */
9031 31021047 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
9032 35500511 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
9033 : {
9034 4479464 : ctors[i] = valp;
9035 4479464 : constructor_elt *cep
9036 4479464 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
9037 4479464 : valp = &cep->value;
9038 : }
9039 : }
9040 :
9041 94323892 : if (*non_constant_p)
9042 : return t;
9043 :
9044 : /* Don't share a CONSTRUCTOR that might be changed later. */
9045 92473884 : init = unshare_constructor (init);
9046 :
9047 92473884 : gcc_checking_assert (!*valp
9048 : || *valp == void_node
9049 : || (same_type_ignoring_top_level_qualifiers_p
9050 : (TREE_TYPE (*valp), type)));
9051 92473884 : if (empty_base)
9052 : {
9053 : /* Just evaluate the initializer and return, since there's no actual data
9054 : to store, and we didn't build a CONSTRUCTOR. */
9055 289446 : if (!*valp)
9056 : {
9057 : /* But do make sure we have something in *valp. */
9058 0 : *valp = build_constructor (type, nullptr);
9059 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
9060 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
9061 : }
9062 : }
9063 92184438 : else if (TREE_CLOBBER_P (init))
9064 : {
9065 746337 : if (AGGREGATE_TYPE_P (type) && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
9066 : {
9067 11424 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
9068 6788 : CONSTRUCTOR_ELTS (*valp) = nullptr;
9069 : else
9070 4636 : *valp = build_constructor (type, nullptr);
9071 11424 : TREE_CONSTANT (*valp) = true;
9072 11424 : TREE_SIDE_EFFECTS (*valp) = false;
9073 11424 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
9074 11424 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
9075 : }
9076 : else
9077 734913 : *valp = void_node;
9078 : }
9079 91438101 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
9080 29166545 : && TREE_CODE (init) == CONSTRUCTOR)
9081 : {
9082 : /* An outer ctx->ctor might be pointing to *valp, so replace
9083 : its contents. */
9084 4733120 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
9085 4733120 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
9086 4733120 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
9087 14199360 : CONSTRUCTOR_NO_CLEARING (*valp)
9088 4733120 : = CONSTRUCTOR_NO_CLEARING (init);
9089 14199360 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
9090 4733120 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
9091 : }
9092 : else
9093 86704981 : *valp = init;
9094 :
9095 : /* After initialization, 'const' semantics apply to the value of the
9096 : object. Make a note of this fact by marking the CONSTRUCTOR
9097 : TREE_READONLY. */
9098 92473884 : if (TREE_CODE (t) == INIT_EXPR
9099 69913586 : && !empty_base
9100 69624140 : && TREE_CODE (*valp) == CONSTRUCTOR
9101 97035978 : && TYPE_READONLY (type))
9102 : {
9103 36628 : tree target_type = TREE_TYPE (target);
9104 36628 : if (IS_FAKE_BASE_TYPE (target_type))
9105 0 : target_type = TYPE_CONTEXT (target_type);
9106 36628 : if (INDIRECT_REF_P (target)
9107 52551 : && (is_this_parameter
9108 15923 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
9109 : /* We've just initialized '*this' (perhaps via the target
9110 : constructor of a delegating constructor). Leave it up to the
9111 : caller that set 'this' to set TREE_READONLY appropriately. */
9112 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
9113 : (target_type, type) || empty_base);
9114 : else
9115 36605 : TREE_READONLY (*valp) = true;
9116 : }
9117 :
9118 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
9119 : CONSTRUCTORs, if any. */
9120 92473884 : bool c = TREE_CONSTANT (init);
9121 92473884 : bool s = TREE_SIDE_EFFECTS (init);
9122 92473884 : if (!indexes->is_empty ())
9123 : {
9124 20314911 : tree last = indexes->last ();
9125 20314911 : if (TREE_CODE (last) == REALPART_EXPR
9126 20314911 : || TREE_CODE (last) == IMAGPART_EXPR)
9127 : {
9128 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
9129 : possible. */
9130 61 : tree *cexpr = ctors.last ();
9131 61 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
9132 61 : TREE_OPERAND (*cexpr, 0),
9133 61 : TREE_OPERAND (*cexpr, 1)))
9134 53 : *cexpr = c;
9135 : else
9136 : {
9137 16 : TREE_CONSTANT (*cexpr)
9138 8 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
9139 8 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
9140 16 : TREE_SIDE_EFFECTS (*cexpr)
9141 16 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
9142 8 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
9143 : }
9144 61 : c = TREE_CONSTANT (*cexpr);
9145 61 : s = TREE_SIDE_EFFECTS (*cexpr);
9146 : }
9147 : }
9148 92473884 : if (!c || s || activated_union_member_p)
9149 63648138 : for (tree *elt : ctors)
9150 : {
9151 12491778 : if (TREE_CODE (*elt) != CONSTRUCTOR)
9152 0 : continue;
9153 12491778 : if (!c)
9154 10599308 : TREE_CONSTANT (*elt) = false;
9155 12491778 : if (s)
9156 0 : TREE_SIDE_EFFECTS (*elt) = true;
9157 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
9158 : this union. */
9159 12491778 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
9160 465301 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
9161 : }
9162 :
9163 92473884 : if (lval)
9164 : return target;
9165 : else
9166 587118 : return init;
9167 94651639 : }
9168 :
9169 : /* Evaluate a ++ or -- expression. */
9170 :
9171 : static tree
9172 8824003 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
9173 : value_cat lval,
9174 : bool *non_constant_p, bool *overflow_p,
9175 : tree *jump_target)
9176 : {
9177 8824003 : enum tree_code code = TREE_CODE (t);
9178 8824003 : tree type = TREE_TYPE (t);
9179 8824003 : tree op = TREE_OPERAND (t, 0);
9180 8824003 : tree offset = TREE_OPERAND (t, 1);
9181 8824003 : gcc_assert (TREE_CONSTANT (offset));
9182 :
9183 : /* OFFSET is constant, but perhaps not constant enough. We need to
9184 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
9185 8824003 : offset = fold_simple (offset);
9186 :
9187 : /* The operand as an lvalue. */
9188 8824003 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
9189 : non_constant_p, overflow_p,
9190 : jump_target);
9191 8824003 : if (*jump_target)
9192 : return NULL_TREE;
9193 :
9194 : /* The operand as an rvalue. */
9195 8824003 : tree val
9196 8824003 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
9197 : non_constant_p, overflow_p,
9198 : jump_target);
9199 8824003 : if (*jump_target)
9200 : return NULL_TREE;
9201 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
9202 : a local array in a constexpr function. */
9203 8824003 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
9204 3529649 : if (!ptr)
9205 3529649 : VERIFY_CONSTANT (val);
9206 :
9207 : /* The modified value. */
9208 8765009 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
9209 8765009 : tree mod;
9210 8765009 : if (INDIRECT_TYPE_P (type))
9211 : {
9212 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
9213 5294354 : offset = convert_to_ptrofftype (offset);
9214 5294354 : if (!inc)
9215 80926 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
9216 5294354 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
9217 : }
9218 3470655 : else if (c_promoting_integer_type_p (type)
9219 39613 : && !TYPE_UNSIGNED (type)
9220 3470867 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
9221 : {
9222 212 : offset = fold_convert (integer_type_node, offset);
9223 212 : mod = fold_convert (integer_type_node, val);
9224 245 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
9225 : mod, offset);
9226 212 : mod = fold_convert (type, t);
9227 212 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
9228 9 : TREE_OVERFLOW (mod) = false;
9229 : }
9230 : else
9231 3913953 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
9232 8765009 : if (!ptr)
9233 3470655 : VERIFY_CONSTANT (mod);
9234 :
9235 : /* Storing the modified value. */
9236 14732404 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
9237 : MODIFY_EXPR, type, op, mod);
9238 8765009 : mod = cxx_eval_constant_expression (ctx, store, lval,
9239 : non_constant_p, overflow_p,
9240 : jump_target);
9241 8765009 : ggc_free (store);
9242 8765009 : if (*jump_target)
9243 : return NULL_TREE;
9244 8765009 : if (*non_constant_p)
9245 : return t;
9246 :
9247 : /* And the value of the expression. */
9248 8690758 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
9249 : /* Prefix ops are lvalues, but the caller might want an rvalue;
9250 : lval has already been taken into account in the store above. */
9251 : return mod;
9252 : else
9253 : /* Postfix ops are rvalues. */
9254 421844 : return val;
9255 : }
9256 :
9257 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
9258 : STMT matches *jump_target. If we're looking for a case label and we see
9259 : the default label, note it in ctx->css_state. */
9260 :
9261 : static bool
9262 504254 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
9263 : {
9264 504254 : switch (TREE_CODE (*jump_target))
9265 : {
9266 145 : case LABEL_DECL:
9267 145 : if (TREE_CODE (stmt) == LABEL_EXPR
9268 145 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
9269 52 : return true;
9270 : break;
9271 :
9272 502192 : case INTEGER_CST:
9273 502192 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
9274 : {
9275 502165 : gcc_assert (ctx->css_state != NULL);
9276 502165 : if (!CASE_LOW (stmt))
9277 : {
9278 : /* default: should appear just once in a SWITCH_EXPR
9279 : body (excluding nested SWITCH_EXPR). */
9280 76286 : gcc_assert (*ctx->css_state != css_default_seen);
9281 : /* When evaluating SWITCH_EXPR body for the second time,
9282 : return true for the default: label. */
9283 76286 : if (*ctx->css_state == css_default_processing)
9284 : return true;
9285 39019 : *ctx->css_state = css_default_seen;
9286 : }
9287 425879 : else if (CASE_HIGH (stmt))
9288 : {
9289 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
9290 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
9291 : return true;
9292 : }
9293 425859 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
9294 : return true;
9295 : }
9296 : break;
9297 :
9298 : case BREAK_STMT:
9299 : case CONTINUE_STMT:
9300 : /* These two are handled directly in cxx_eval_loop_expr by testing
9301 : breaks (jump_target) or continues (jump_target). */
9302 : break;
9303 :
9304 : case VAR_DECL:
9305 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
9306 : and other places by testing throws (jump_target). */
9307 : break;
9308 :
9309 0 : default:
9310 0 : gcc_unreachable ();
9311 : }
9312 : return false;
9313 : }
9314 :
9315 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
9316 : semantics, for switch, break, continue, and return. */
9317 :
9318 : static tree
9319 48084592 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
9320 : bool *non_constant_p, bool *overflow_p,
9321 : tree *jump_target)
9322 : {
9323 : /* In a statement-expression we want to return the last value.
9324 : For empty statement expression return void_node. */
9325 48084592 : tree r = void_node;
9326 126549091 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
9327 : {
9328 104082904 : tree stmt = *i;
9329 :
9330 : /* We've found a continue, so skip everything until we reach
9331 : the label its jumping to. */
9332 104082904 : if (continues (jump_target))
9333 : {
9334 2062 : if (label_matches (ctx, jump_target, stmt))
9335 : /* Found it. */
9336 52 : *jump_target = NULL_TREE;
9337 : else
9338 2010 : continue;
9339 : }
9340 104080894 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
9341 11182147 : continue;
9342 :
9343 92898747 : value_cat lval = vc_discard;
9344 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
9345 130658019 : if (tsi_one_before_end_p (i)
9346 37760537 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
9347 : lval = vc_prvalue;
9348 :
9349 92898747 : r = cxx_eval_constant_expression (ctx, stmt, lval,
9350 : non_constant_p, overflow_p,
9351 : jump_target);
9352 92898743 : if (*non_constant_p)
9353 : break;
9354 83009788 : if (returns (jump_target)
9355 67370504 : || breaks (jump_target)
9356 78464499 : || throws (jump_target))
9357 : break;
9358 : }
9359 48084588 : return r;
9360 : }
9361 :
9362 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
9363 : semantics; continue semantics are covered by cxx_eval_statement_list. */
9364 :
9365 : static tree
9366 6646544 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
9367 : bool *non_constant_p, bool *overflow_p,
9368 : tree *jump_target)
9369 : {
9370 6646544 : tree body, cond = NULL_TREE, expr = NULL_TREE;
9371 6646544 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
9372 6646544 : unsigned cond_cleanup_depth = 0;
9373 6646544 : int count = 0;
9374 6646544 : switch (TREE_CODE (t))
9375 : {
9376 3419 : case LOOP_EXPR:
9377 3419 : body = LOOP_EXPR_BODY (t);
9378 3419 : break;
9379 5349924 : case DO_STMT:
9380 5349924 : body = DO_BODY (t);
9381 5349924 : cond = DO_COND (t);
9382 5349924 : break;
9383 364564 : case WHILE_STMT:
9384 364564 : body = WHILE_BODY (t);
9385 364564 : cond = WHILE_COND (t);
9386 364564 : cond_prep = WHILE_COND_PREP (t);
9387 364564 : cond_cleanup = WHILE_COND_CLEANUP (t);
9388 364564 : count = -1;
9389 364564 : break;
9390 928637 : case FOR_STMT:
9391 928637 : if (FOR_INIT_STMT (t))
9392 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
9393 : non_constant_p, overflow_p, jump_target);
9394 928637 : if (*non_constant_p)
9395 : return NULL_TREE;
9396 928637 : body = FOR_BODY (t);
9397 928637 : cond = FOR_COND (t);
9398 928637 : expr = FOR_EXPR (t);
9399 928637 : cond_prep = FOR_COND_PREP (t);
9400 928637 : cond_cleanup = FOR_COND_CLEANUP (t);
9401 928637 : count = -1;
9402 928637 : break;
9403 0 : default:
9404 0 : gcc_unreachable ();
9405 : }
9406 6646544 : if (cond_prep)
9407 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
9408 31496579 : auto cleanup_cond = [&] {
9409 : /* Clean up the condition variable after each iteration. */
9410 24850035 : if (cond_cleanup_depth && !*non_constant_p)
9411 : {
9412 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
9413 105 : tree s = BIND_EXPR_BODY (cond_prep);
9414 105 : unsigned i;
9415 210 : for (i = cond_cleanup_depth; i; --i)
9416 : {
9417 105 : tree_stmt_iterator iter = tsi_last (s);
9418 105 : s = tsi_stmt (iter);
9419 105 : cleanups.quick_push (CLEANUP_EXPR (s));
9420 105 : s = CLEANUP_BODY (s);
9421 : }
9422 105 : tree c;
9423 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
9424 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
9425 : overflow_p, jump_target);
9426 105 : }
9427 24850035 : if (cond_prep)
9428 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
9429 222 : decl; decl = DECL_CHAIN (decl))
9430 111 : destroy_value_checked (ctx, decl, non_constant_p);
9431 6646544 : };
9432 19532145 : do
9433 : {
9434 19532145 : if (count != -1)
9435 : {
9436 18238944 : if (body)
9437 18238944 : cxx_eval_constant_expression (ctx, body, vc_discard,
9438 : non_constant_p, overflow_p,
9439 : jump_target);
9440 18238944 : if (breaks (jump_target))
9441 : {
9442 35453 : *jump_target = NULL_TREE;
9443 35453 : break;
9444 : }
9445 :
9446 18203491 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
9447 570 : *jump_target = NULL_TREE;
9448 :
9449 18203491 : if (expr)
9450 5424830 : cxx_eval_constant_expression (ctx, expr, vc_discard,
9451 : non_constant_p, overflow_p,
9452 : jump_target);
9453 18203491 : cleanup_cond ();
9454 : }
9455 :
9456 19496692 : if (cond_prep)
9457 : {
9458 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
9459 222 : decl; decl = DECL_CHAIN (decl))
9460 111 : ctx->global->clear_value (decl);
9461 111 : if (cond_cleanup)
9462 : {
9463 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
9464 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
9465 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
9466 : at the end of those STATEMENT_LISTs. */
9467 111 : cond_cleanup_depth = 0;
9468 111 : tree s = BIND_EXPR_BODY (cond_prep);
9469 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
9470 222 : depth; --depth)
9471 : {
9472 111 : for (tree_stmt_iterator i = tsi_start (s);
9473 321 : !tsi_end_p (i); ++i)
9474 : {
9475 321 : tree stmt = *i;
9476 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
9477 0 : continue;
9478 321 : if (tsi_one_before_end_p (i))
9479 : {
9480 : /* The last statement in the STATEMENT_LIST
9481 : has to be a CLEANUP_STMT (verified in
9482 : finish_loop_cond_prep). We want to
9483 : evaluate just its CLEANUP_BODY part but not
9484 : CLEANUP_EXPR part just yet. */
9485 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
9486 : /* If the CLEANUP_STMT is not actually to be
9487 : evaluated, don't increment cond_cleanup_depth
9488 : so that we don't evaluate the CLEANUP_EXPR
9489 : for it later either. */
9490 105 : if (*jump_target)
9491 : {
9492 : depth = 1;
9493 : break;
9494 : }
9495 105 : ++cond_cleanup_depth;
9496 : /* If not in the innermost one, next iteration
9497 : will handle CLEANUP_BODY similarly. */
9498 105 : if (depth > 1)
9499 : {
9500 0 : s = CLEANUP_BODY (stmt);
9501 0 : break;
9502 : }
9503 : /* The innermost one can be evaluated normally. */
9504 105 : cxx_eval_constant_expression (ctx,
9505 105 : CLEANUP_BODY (stmt),
9506 : vc_discard,
9507 : non_constant_p,
9508 : overflow_p,
9509 : jump_target);
9510 105 : break;
9511 : }
9512 : /* And so should be evaluated statements which aren't
9513 : last in the STATEMENT_LIST. */
9514 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
9515 : non_constant_p, overflow_p,
9516 : jump_target);
9517 216 : if (*non_constant_p
9518 210 : || returns (jump_target)
9519 210 : || breaks (jump_target)
9520 210 : || continues (jump_target)
9521 531 : || throws (jump_target))
9522 : {
9523 : depth = 1;
9524 : break;
9525 : }
9526 : }
9527 : }
9528 : }
9529 : else
9530 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
9531 : vc_discard, non_constant_p,
9532 : overflow_p, jump_target);
9533 : }
9534 :
9535 19496692 : if (cond)
9536 : {
9537 19489158 : tree res
9538 19489158 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9539 : non_constant_p, overflow_p,
9540 : jump_target);
9541 19489158 : if (res)
9542 : {
9543 19433748 : if (verify_constant (res, ctx->quiet, non_constant_p,
9544 : overflow_p))
9545 : break;
9546 19247474 : if (integer_zerop (res))
9547 : break;
9548 : }
9549 : else
9550 55410 : gcc_assert (*jump_target);
9551 : }
9552 :
9553 12942896 : if (++count >= constexpr_loop_limit)
9554 : {
9555 27 : if (!ctx->quiet)
9556 9 : error_at (cp_expr_loc_or_input_loc (t),
9557 : "%<constexpr%> loop iteration count exceeds limit of %d "
9558 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
9559 : constexpr_loop_limit);
9560 27 : *non_constant_p = true;
9561 27 : break;
9562 : }
9563 : }
9564 38771578 : while (!returns (jump_target)
9565 12885840 : && !breaks (jump_target)
9566 12885840 : && !continues (jump_target)
9567 12885933 : && (!switches (jump_target) || count == 0)
9568 12885810 : && !throws (jump_target)
9569 25885893 : && !*non_constant_p);
9570 :
9571 6646544 : cleanup_cond ();
9572 :
9573 6646544 : return NULL_TREE;
9574 : }
9575 :
9576 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
9577 : semantics. */
9578 :
9579 : static tree
9580 50127 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
9581 : bool *non_constant_p, bool *overflow_p,
9582 : tree *jump_target)
9583 : {
9584 50127 : tree cond
9585 50127 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
9586 50127 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
9587 : non_constant_p, overflow_p,
9588 : jump_target);
9589 50127 : if (*jump_target)
9590 : return NULL_TREE;
9591 50127 : VERIFY_CONSTANT (cond);
9592 49834 : if (TREE_CODE (cond) != INTEGER_CST)
9593 : {
9594 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
9595 : switch condition even if it's constant enough for other things
9596 : (c++/113545). */
9597 0 : gcc_checking_assert (ctx->quiet);
9598 0 : *non_constant_p = true;
9599 0 : return t;
9600 : }
9601 :
9602 49834 : *jump_target = cond;
9603 :
9604 49834 : tree body
9605 49834 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
9606 49834 : constexpr_ctx new_ctx = *ctx;
9607 49834 : constexpr_switch_state css = css_default_not_seen;
9608 49834 : new_ctx.css_state = &css;
9609 49834 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
9610 : non_constant_p, overflow_p, jump_target);
9611 87143 : if (switches (jump_target) && css == css_default_seen)
9612 : {
9613 : /* If the SWITCH_EXPR body has default: label, process it once again,
9614 : this time instructing label_matches to return true for default:
9615 : label on switches (jump_target). */
9616 37267 : css = css_default_processing;
9617 37267 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
9618 : non_constant_p, overflow_p, jump_target);
9619 : }
9620 49834 : if (breaks (jump_target) || switches (jump_target))
9621 21439 : *jump_target = NULL_TREE;
9622 : return NULL_TREE;
9623 : }
9624 :
9625 : /* Find the object of TYPE under initialization in CTX. */
9626 :
9627 : static tree
9628 16716 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
9629 : {
9630 16716 : if (!ctx)
9631 : return NULL_TREE;
9632 :
9633 : /* Prefer the outermost matching object, but don't cross
9634 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
9635 16320 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
9636 574 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
9637 : return outer_ob;
9638 :
9639 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
9640 : can is a minor optimization. */
9641 16142 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
9642 302 : return ctx->ctor;
9643 :
9644 15840 : if (!ctx->object)
9645 : return NULL_TREE;
9646 :
9647 : /* Since an object cannot have a field of its own type, we can search outward
9648 : from ctx->object to find the unique containing object of TYPE. */
9649 : tree ob = ctx->object;
9650 15816 : while (ob)
9651 : {
9652 15816 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
9653 : break;
9654 219 : if (handled_component_p (ob))
9655 217 : ob = TREE_OPERAND (ob, 0);
9656 : else
9657 : ob = NULL_TREE;
9658 : }
9659 :
9660 : /* Like in replace_placeholders_r. */
9661 15599 : return unshare_expr (ob);
9662 : }
9663 :
9664 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
9665 : true, we're checking a constexpr function body. */
9666 :
9667 : static void
9668 35 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
9669 : {
9670 35 : auto_diagnostic_group d;
9671 35 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
9672 : "constant expression"))
9673 35 : inform (loc, "only unevaluated inline assembly is allowed in a "
9674 : "%<constexpr%> function in C++20");
9675 35 : }
9676 :
9677 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
9678 : context; maybe complain about that. */
9679 :
9680 : static void
9681 92351251 : maybe_warn_about_constant_value (location_t loc, tree decl)
9682 : {
9683 92351251 : static bool explained = false;
9684 92351251 : auto_diagnostic_group d;
9685 92351251 : if (cxx_dialect >= cxx17
9686 92188324 : && warn_interference_size
9687 92188324 : && !OPTION_SET_P (param_destruct_interfere_size)
9688 92188324 : && DECL_CONTEXT (decl) == std_node
9689 18859518 : && DECL_NAME (decl)
9690 18859512 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
9691 16 : && (LOCATION_FILE (input_location) != main_input_filename
9692 10 : || module_exporting_p ())
9693 92351254 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
9694 92351260 : && !explained)
9695 : {
9696 6 : explained = true;
9697 6 : inform (loc, "its value can vary between compiler versions or "
9698 : "with different %<-mtune%> or %<-mcpu%> flags");
9699 6 : inform (loc, "if this use is part of a public ABI, change it to "
9700 : "instead use a constant variable you define");
9701 6 : inform (loc, "the default value for the current CPU tuning "
9702 : "is %d bytes", param_destruct_interfere_size);
9703 6 : inform (loc, "you can stabilize this value with %<--param "
9704 : "hardware_destructive_interference_size=%d%>, or disable "
9705 : "this warning with %<-Wno-interference-size%>",
9706 : param_destruct_interfere_size);
9707 : }
9708 92351251 : }
9709 :
9710 : /* For element type ELT_TYPE, return the appropriate type of the heap object
9711 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
9712 : in bytes. If COOKIE_SIZE is NULL, return array type
9713 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
9714 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
9715 : where N is computed such that the size of the struct fits into FULL_SIZE.
9716 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
9717 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
9718 : will be also 0 and so it is not possible to determine the actual array
9719 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
9720 : expression evaluation of subexpressions of ARG_SIZE. */
9721 :
9722 : static tree
9723 117585 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9724 : tree cookie_size, tree full_size, tree arg_size,
9725 : bool *non_constant_p, bool *overflow_p,
9726 : tree *jump_target)
9727 : {
9728 117585 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9729 117585 : gcc_assert (tree_fits_uhwi_p (full_size));
9730 117585 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9731 117585 : if (arg_size)
9732 : {
9733 9 : STRIP_NOPS (arg_size);
9734 9 : if (cookie_size)
9735 : {
9736 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9737 : arg_size = NULL_TREE;
9738 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9739 0 : && tree_int_cst_equal (cookie_size,
9740 0 : TREE_OPERAND (arg_size, 0)))
9741 : {
9742 0 : arg_size = TREE_OPERAND (arg_size, 1);
9743 0 : STRIP_NOPS (arg_size);
9744 : }
9745 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9746 0 : && tree_int_cst_equal (cookie_size,
9747 0 : TREE_OPERAND (arg_size, 1)))
9748 : {
9749 0 : arg_size = TREE_OPERAND (arg_size, 0);
9750 0 : STRIP_NOPS (arg_size);
9751 : }
9752 : else
9753 : arg_size = NULL_TREE;
9754 : }
9755 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9756 : {
9757 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9758 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9759 9 : if (integer_zerop (op0))
9760 9 : arg_size
9761 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9762 : non_constant_p, overflow_p,
9763 : jump_target);
9764 0 : else if (integer_zerop (op1))
9765 0 : arg_size
9766 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9767 : non_constant_p, overflow_p,
9768 : jump_target);
9769 : else
9770 : arg_size = NULL_TREE;
9771 9 : if (*jump_target)
9772 : return NULL_TREE;
9773 : }
9774 : else
9775 : arg_size = NULL_TREE;
9776 : }
9777 :
9778 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9779 117585 : if (!arg_size)
9780 : {
9781 117576 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9782 117576 : gcc_assert (fsz >= csz);
9783 117576 : fsz -= csz;
9784 117576 : if (esz)
9785 117576 : fsz /= esz;
9786 : }
9787 117585 : tree itype2 = build_index_type (size_int (fsz - 1));
9788 117585 : if (!cookie_size)
9789 117576 : return build_cplus_array_type (elt_type, itype2);
9790 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9791 : }
9792 :
9793 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9794 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9795 : the expression which needed the cleanup threw. If both threw, diagnose
9796 : it and return NULL, otherwise return R. If only the cleanup threw, set
9797 : *JUMP_TARGET to the exception object from the cleanup. */
9798 :
9799 : static tree
9800 1743790 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9801 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9802 : {
9803 1743790 : if (!throws (&jmp_target))
9804 : return r;
9805 14 : if (throws (jump_target))
9806 : {
9807 : /* [except.throw]/9 - If the exception handling mechanism
9808 : handling an uncaught exception directly invokes a function
9809 : that exits via an exception, the function std::terminate is
9810 : invoked. */
9811 12 : if (!ctx->quiet)
9812 : {
9813 4 : auto_diagnostic_group d;
9814 4 : diagnose_std_terminate (loc, ctx, *jump_target);
9815 4 : inform (loc, "destructor exited with an exception");
9816 4 : }
9817 12 : *non_constant_p = true;
9818 12 : *jump_target = NULL_TREE;
9819 12 : return NULL_TREE;
9820 : }
9821 2 : *jump_target = jmp_target;
9822 2 : return r;
9823 : }
9824 :
9825 : /* Attempt to reduce the expression T to a constant value.
9826 : On failure, issue diagnostic and return error_mark_node. */
9827 : /* FIXME unify with c_fully_fold */
9828 : /* FIXME overflow_p is too global */
9829 :
9830 : tree
9831 3013530421 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9832 : value_cat lval,
9833 : bool *non_constant_p, bool *overflow_p,
9834 : tree *jump_target)
9835 : {
9836 3013530421 : if (*jump_target)
9837 : {
9838 : /* If we are jumping, ignore all statements/expressions except those
9839 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9840 1622890 : switch (TREE_CODE (t))
9841 : {
9842 : case BIND_EXPR:
9843 : case STATEMENT_LIST:
9844 : case LOOP_EXPR:
9845 : case COND_EXPR:
9846 : case IF_STMT:
9847 : case DO_STMT:
9848 : case WHILE_STMT:
9849 : case FOR_STMT:
9850 : break;
9851 502192 : case LABEL_EXPR:
9852 502192 : case CASE_LABEL_EXPR:
9853 502192 : if (label_matches (ctx, jump_target, t))
9854 : /* Found it. */
9855 49792 : *jump_target = NULL_TREE;
9856 : return NULL_TREE;
9857 : default:
9858 : return NULL_TREE;
9859 : }
9860 : }
9861 3012153379 : if (error_operand_p (t))
9862 : {
9863 194 : *non_constant_p = true;
9864 194 : return t;
9865 : }
9866 :
9867 : /* Change the input location to the currently processed expression for
9868 : better error messages when a subexpression has no location. */
9869 3012153185 : location_t loc = cp_expr_loc_or_input_loc (t);
9870 6024300938 : iloc_sentinel sentinel (loc);
9871 :
9872 3012153185 : STRIP_ANY_LOCATION_WRAPPER (t);
9873 :
9874 3012153185 : if (CONSTANT_CLASS_P (t))
9875 : {
9876 458628675 : if (TREE_OVERFLOW (t))
9877 : {
9878 62 : if (!ctx->quiet)
9879 15 : permerror (input_location, "overflow in constant expression");
9880 62 : if (!flag_permissive || ctx->quiet)
9881 59 : *overflow_p = true;
9882 : }
9883 :
9884 458628675 : if (TREE_CODE (t) == INTEGER_CST
9885 436363677 : && TYPE_PTR_P (TREE_TYPE (t))
9886 : /* INTEGER_CST with pointer-to-method type is only used
9887 : for a virtual method in a pointer to member function.
9888 : Don't reject those. */
9889 2098671 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9890 460727019 : && !integer_zerop (t))
9891 : {
9892 6 : if (!ctx->quiet)
9893 0 : error ("value %qE of type %qT is not a constant expression",
9894 0 : t, TREE_TYPE (t));
9895 6 : *non_constant_p = true;
9896 : }
9897 :
9898 : return t;
9899 : }
9900 :
9901 : /* Avoid excessively long constexpr evaluations. */
9902 2553524510 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9903 : {
9904 9 : if (!ctx->quiet)
9905 3 : error_at (loc,
9906 : "%<constexpr%> evaluation operation count exceeds limit of "
9907 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9908 : constexpr_ops_limit);
9909 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9910 9 : *non_constant_p = true;
9911 9 : return t;
9912 : }
9913 :
9914 2553524501 : constexpr_ctx new_ctx;
9915 2553524501 : tree r = t;
9916 :
9917 2553524501 : tree_code tcode = TREE_CODE (t);
9918 2553524501 : switch (tcode)
9919 : {
9920 59747106 : case RESULT_DECL:
9921 59747106 : if (lval)
9922 : return t;
9923 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9924 : through an invisible reference, or in named return value
9925 : optimization. */
9926 147224 : if (tree v = ctx->global->get_value (t))
9927 : return v;
9928 : else
9929 : {
9930 3 : if (!ctx->quiet)
9931 0 : error ("%qE is not a constant expression", t);
9932 3 : *non_constant_p = true;
9933 : }
9934 3 : break;
9935 :
9936 298285706 : case VAR_DECL:
9937 298285706 : if (DECL_HAS_VALUE_EXPR_P (t))
9938 : {
9939 9259869 : if (is_normal_capture_proxy (t)
9940 9259869 : && current_function_decl == DECL_CONTEXT (t))
9941 : {
9942 : /* Function parms aren't constexpr within the function
9943 : definition, so don't try to look at the closure. But if the
9944 : captured variable is constant, try to evaluate it directly. */
9945 632201 : r = DECL_CAPTURED_VARIABLE (t);
9946 : }
9947 : else
9948 8627668 : r = DECL_VALUE_EXPR (t);
9949 :
9950 9259869 : tree type = TREE_TYPE (t);
9951 9259869 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
9952 : {
9953 : /* Adjust r to match the reference-ness of t. */
9954 259756 : if (TYPE_REF_P (type))
9955 255565 : r = build_address (r);
9956 : else
9957 4191 : r = convert_from_reference (r);
9958 : }
9959 9259869 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
9960 9259869 : overflow_p, jump_target);
9961 : }
9962 : /* fall through */
9963 299632869 : case CONST_DECL:
9964 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
9965 : CONST_DECL for aggregate constants. */
9966 299632869 : if (lval)
9967 : return t;
9968 212901595 : else if (t == ctx->object)
9969 1010395 : return ctx->ctor;
9970 211891200 : if (VAR_P (t))
9971 : {
9972 201284174 : if (tree v = ctx->global->get_value (t))
9973 : {
9974 : r = v;
9975 : break;
9976 : }
9977 136154968 : if (ctx->global->is_outside_lifetime (t))
9978 : {
9979 137 : if (!ctx->quiet)
9980 39 : outside_lifetime_error (loc, t);
9981 137 : *non_constant_p = true;
9982 137 : break;
9983 : }
9984 : }
9985 146761857 : if (ctx->manifestly_const_eval == mce_true)
9986 92351251 : maybe_warn_about_constant_value (loc, t);
9987 146761857 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
9988 146761857 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9989 : {
9990 : /* If the class is empty, we aren't actually loading anything. */
9991 252660 : r = build_constructor (TREE_TYPE (t), NULL);
9992 252660 : TREE_CONSTANT (r) = true;
9993 : }
9994 146509197 : else if (ctx->strict)
9995 146167273 : r = decl_really_constant_value (t, /*unshare_p=*/false);
9996 : else
9997 341924 : r = decl_constant_value (t, /*unshare_p=*/false);
9998 146759160 : if (TREE_CODE (r) == TARGET_EXPR
9999 146759160 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
10000 0 : r = TARGET_EXPR_INITIAL (r);
10001 146759160 : if (DECL_P (r)
10002 : /* P2280 allows references to unknown. */
10003 147044691 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
10004 : {
10005 34314436 : if (!ctx->quiet)
10006 187 : non_const_var_error (loc, r, /*fundef_p*/false);
10007 34314436 : *non_constant_p = true;
10008 : }
10009 : break;
10010 :
10011 : case DEBUG_BEGIN_STMT:
10012 : /* ??? It might be nice to retain this information somehow, so
10013 : as to be able to step into a constexpr function call. */
10014 : /* Fall through. */
10015 :
10016 : case FUNCTION_DECL:
10017 : case TEMPLATE_DECL:
10018 : case LABEL_DECL:
10019 : case LABEL_EXPR:
10020 : case CASE_LABEL_EXPR:
10021 : case PREDICT_EXPR:
10022 : case OMP_DECLARE_MAPPER:
10023 : case REFLECT_EXPR:
10024 : return t;
10025 :
10026 305294920 : case PARM_DECL:
10027 305294920 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
10028 : {
10029 : /* glvalue use. */
10030 21795412 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
10031 220605 : if (tree v = ctx->global->get_value (t))
10032 2055912128 : r = v;
10033 : }
10034 283499508 : else if (tree v = ctx->global->get_value (t))
10035 : {
10036 166320799 : r = v;
10037 166320799 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
10038 11305 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
10039 : non_constant_p, overflow_p,
10040 : jump_target);
10041 166320799 : if (*jump_target)
10042 : return NULL_TREE;
10043 : }
10044 117178709 : else if (lval)
10045 : /* Defer in case this is only used for its type. */;
10046 117178709 : else if (ctx->global->is_outside_lifetime (t))
10047 : {
10048 18 : if (!ctx->quiet)
10049 6 : outside_lifetime_error (loc, t);
10050 18 : *non_constant_p = true;
10051 18 : break;
10052 : }
10053 117178691 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
10054 117178691 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
10055 : {
10056 : /* If the class is empty, we aren't actually loading anything. */
10057 41299 : r = build_constructor (TREE_TYPE (t), NULL);
10058 41299 : TREE_CONSTANT (r) = true;
10059 : }
10060 117137392 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
10061 : /* P2280 allows references to unknown... */;
10062 116863709 : else if (p2280_active_p (ctx) && is_this_parameter (t))
10063 : /* ...as well as the this pointer. */;
10064 : else
10065 : {
10066 116415572 : if (!ctx->quiet)
10067 199 : error ("%qE is not a constant expression", t);
10068 116415572 : *non_constant_p = true;
10069 : }
10070 : break;
10071 :
10072 205757673 : case CALL_EXPR:
10073 205757673 : case AGGR_INIT_EXPR:
10074 205757673 : r = cxx_eval_call_expression (ctx, t, lval,
10075 : non_constant_p, overflow_p, jump_target);
10076 205757673 : break;
10077 :
10078 21512907 : case DECL_EXPR:
10079 21512907 : {
10080 21512907 : r = DECL_EXPR_DECL (t);
10081 21512907 : if (TREE_CODE (r) == USING_DECL)
10082 : {
10083 9363 : r = void_node;
10084 9363 : break;
10085 : }
10086 :
10087 21503544 : if (VAR_P (r)
10088 21503544 : && (TREE_STATIC (r)
10089 21340491 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
10090 : /* Allow __FUNCTION__ etc. */
10091 163053 : && !DECL_ARTIFICIAL (r)
10092 21504441 : && !decl_constant_var_p (r))
10093 : {
10094 7 : if (!ctx->quiet)
10095 : {
10096 2 : if (CP_DECL_THREAD_LOCAL_P (r))
10097 1 : error_at (loc, "control passes through definition of %qD "
10098 : "with thread storage duration", r);
10099 : else
10100 1 : error_at (loc, "control passes through definition of %qD "
10101 : "with static storage duration", r);
10102 : }
10103 7 : *non_constant_p = true;
10104 7 : break;
10105 : }
10106 :
10107 : /* make_rtl_for_nonlocal_decl could have deferred emission of
10108 : a local static var, but if it appears in a statement expression
10109 : which is constant expression evaluated to e.g. just the address
10110 : of the variable, its DECL_EXPR will never be seen during
10111 : gimple lowering's record_vars_into as the statement expression
10112 : will not be in the IL at all. */
10113 21503537 : if (VAR_P (r)
10114 21503537 : && TREE_STATIC (r)
10115 163046 : && !DECL_REALLY_EXTERN (r)
10116 163024 : && DECL_FUNCTION_SCOPE_P (r)
10117 163024 : && !var_in_maybe_constexpr_fn (r)
10118 21503540 : && decl_constant_var_p (r))
10119 : {
10120 3 : varpool_node *node = varpool_node::get (r);
10121 3 : if (node == NULL || !node->definition)
10122 3 : rest_of_decl_compilation (r, 0, at_eof);
10123 : }
10124 :
10125 42764364 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
10126 40098306 : || VECTOR_TYPE_P (TREE_TYPE (r)))
10127 : {
10128 2911732 : new_ctx = *ctx;
10129 2911732 : new_ctx.object = r;
10130 2911732 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
10131 2911732 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
10132 2911732 : ctx->global->put_value (r, new_ctx.ctor);
10133 2911732 : ctx = &new_ctx;
10134 : }
10135 :
10136 21503537 : if (tree init = DECL_INITIAL (r))
10137 : {
10138 11202084 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
10139 : non_constant_p, overflow_p,
10140 : jump_target);
10141 11202084 : if (*jump_target)
10142 : return NULL_TREE;
10143 : /* Don't share a CONSTRUCTOR that might be changed. */
10144 11202084 : init = unshare_constructor (init);
10145 : /* Remember that a constant object's constructor has already
10146 : run. */
10147 22404168 : if (CLASS_TYPE_P (TREE_TYPE (r))
10148 11751510 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
10149 71521 : TREE_READONLY (init) = true;
10150 11202084 : ctx->global->put_value (r, init);
10151 : }
10152 10301453 : else if (ctx == &new_ctx)
10153 : /* We gave it a CONSTRUCTOR above. */;
10154 : else
10155 8016094 : ctx->global->put_value (r, NULL_TREE);
10156 : }
10157 : break;
10158 :
10159 26924340 : case TARGET_EXPR:
10160 26924340 : {
10161 26924340 : tree type = TREE_TYPE (t);
10162 :
10163 26924340 : if (!literal_type_p (type))
10164 : {
10165 7138 : if (!ctx->quiet)
10166 : {
10167 0 : auto_diagnostic_group d;
10168 0 : error ("temporary of non-literal type %qT in a "
10169 : "constant expression", type);
10170 0 : explain_non_literal_class (type);
10171 0 : }
10172 7138 : *non_constant_p = true;
10173 11724625 : break;
10174 : }
10175 26917202 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
10176 : /* Avoid evaluating a TARGET_EXPR more than once. */
10177 26917202 : tree slot = TARGET_EXPR_SLOT (t);
10178 26917202 : if (tree v = ctx->global->get_value (slot))
10179 : {
10180 2142 : if (lval)
10181 10898442 : return slot;
10182 : r = v;
10183 : break;
10184 : }
10185 26915060 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
10186 : {
10187 : /* We're being expanded without an explicit target, so start
10188 : initializing a new object; expansion with an explicit target
10189 : strips the TARGET_EXPR before we get here. */
10190 20405500 : new_ctx = *ctx;
10191 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
10192 : any PLACEHOLDER_EXPR within the initializer that refers to the
10193 : former object under construction. */
10194 20405500 : new_ctx.parent = ctx;
10195 20405500 : new_ctx.ctor = build_constructor (type, NULL);
10196 20405500 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
10197 20405500 : new_ctx.object = slot;
10198 20405500 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
10199 20405500 : ctx = &new_ctx;
10200 : }
10201 :
10202 : /* If the initializer is complex, evaluate it to initialize slot. */
10203 26915060 : bool is_complex = target_expr_needs_replace (t);
10204 26915060 : if (is_complex)
10205 : /* In case no initialization actually happens, clear out any
10206 : void_node from a previous evaluation. */
10207 493 : ctx->global->put_value (slot, NULL_TREE);
10208 :
10209 : /* Pass vc_prvalue because this indicates
10210 : initialization of a temporary. */
10211 26915060 : r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t),
10212 : vc_prvalue, non_constant_p,
10213 : overflow_p, jump_target);
10214 26915060 : if (*non_constant_p)
10215 : break;
10216 15198659 : if (ctx->save_exprs)
10217 10085510 : ctx->save_exprs->safe_push (slot);
10218 15198659 : if (*jump_target)
10219 : {
10220 669 : if (!is_complex
10221 669 : && !(AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
10222 : /* If TARGET_EXPR_INITIAL throws exception and slot's value
10223 : has not been changed yet, CLEANUP_POINT_EXPR handling
10224 : could see there void_node from a previous evaluation
10225 : and complain. */
10226 6 : ctx->global->put_value (slot, NULL_TREE);
10227 : return NULL_TREE;
10228 : }
10229 15197990 : if (!is_complex)
10230 : {
10231 15197577 : r = unshare_constructor (r);
10232 : /* Adjust the type of the result to the type of the temporary. */
10233 15197577 : r = adjust_temp_type (type, r);
10234 15197577 : ctx->global->put_value (slot, r);
10235 : }
10236 15197990 : if (TARGET_EXPR_CLEANUP (t)
10237 15197990 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
10238 : {
10239 1437332 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
10240 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
10241 : them. */
10242 1437332 : if (CLEANUP_EH_ONLY (t))
10243 2364 : ctx->global->cleanups->safe_push (NULL_TREE);
10244 : }
10245 15197990 : if (lval)
10246 : return slot;
10247 4301273 : if (is_complex)
10248 0 : r = ctx->global->get_value (slot);
10249 : }
10250 4301273 : break;
10251 :
10252 103719216 : case INIT_EXPR:
10253 103719216 : case MODIFY_EXPR:
10254 103719216 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
10255 103719216 : r = cxx_eval_store_expression (ctx, t, lval,
10256 : non_constant_p, overflow_p, jump_target);
10257 103719216 : break;
10258 :
10259 0 : case SCOPE_REF:
10260 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
10261 : lval,
10262 : non_constant_p, overflow_p,
10263 : jump_target);
10264 0 : break;
10265 :
10266 64634187 : case RETURN_EXPR:
10267 64634187 : if (TREE_OPERAND (t, 0) != NULL_TREE)
10268 64308215 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10269 : lval,
10270 : non_constant_p, overflow_p,
10271 : jump_target);
10272 64634183 : if (!throws (jump_target))
10273 64633501 : *jump_target = t;
10274 : break;
10275 54031 : case BREAK_STMT:
10276 54031 : case CONTINUE_STMT:
10277 54031 : *jump_target = t;
10278 54031 : break;
10279 :
10280 964968 : case SAVE_EXPR:
10281 : /* Avoid evaluating a SAVE_EXPR more than once. */
10282 964968 : if (tree v = ctx->global->get_value (t))
10283 : r = v;
10284 : else
10285 : {
10286 942939 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10287 : vc_prvalue, non_constant_p,
10288 : overflow_p, jump_target);
10289 942939 : if (*non_constant_p || *jump_target)
10290 : break;
10291 12799 : ctx->global->put_value (t, r);
10292 12799 : if (ctx->save_exprs)
10293 9259 : ctx->save_exprs->safe_push (t);
10294 : }
10295 : break;
10296 :
10297 62853071 : case NON_LVALUE_EXPR:
10298 62853071 : case EXPR_STMT:
10299 62853071 : case EH_SPEC_BLOCK:
10300 62853071 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10301 : lval,
10302 : non_constant_p, overflow_p,
10303 : jump_target);
10304 62853071 : break;
10305 :
10306 9428 : case TRY_BLOCK:
10307 9428 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
10308 : non_constant_p, overflow_p,
10309 : jump_target);
10310 9428 : if (!*non_constant_p && throws (jump_target))
10311 3803 : if (tree h = TRY_HANDLERS (t))
10312 : {
10313 3803 : tree type = strip_array_types (TREE_TYPE (*jump_target));
10314 3803 : if (TREE_CODE (h) == STATEMENT_LIST)
10315 : {
10316 938 : for (tree stmt : tsi_range (h))
10317 904 : if (TREE_CODE (stmt) == HANDLER
10318 904 : && handler_match_for_exception_type (stmt, type))
10319 : {
10320 : h = stmt;
10321 : break;
10322 : }
10323 462 : if (TREE_CODE (h) == STATEMENT_LIST)
10324 : h = NULL_TREE;
10325 : }
10326 3341 : else if (TREE_CODE (h) != HANDLER
10327 3341 : || !handler_match_for_exception_type (h, type))
10328 : h = NULL_TREE;
10329 : if (h)
10330 : {
10331 3769 : gcc_assert (VAR_P (*jump_target));
10332 3769 : ctx->global->caught_exceptions.safe_push (*jump_target);
10333 3769 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
10334 3769 : *jump_target = NULL_TREE;
10335 3769 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
10336 : vc_discard, non_constant_p,
10337 : overflow_p, jump_target);
10338 : }
10339 : }
10340 : break;
10341 :
10342 91899842 : case CLEANUP_POINT_EXPR:
10343 91899842 : {
10344 91899842 : auto_vec<tree, 2> cleanups;
10345 91899842 : vec<tree> *prev_cleanups = ctx->global->cleanups;
10346 91899842 : ctx->global->cleanups = &cleanups;
10347 :
10348 91899842 : auto_vec<tree, 10> save_exprs;
10349 91899842 : constexpr_ctx new_ctx = *ctx;
10350 91899842 : new_ctx.save_exprs = &save_exprs;
10351 :
10352 91899842 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
10353 : lval,
10354 : non_constant_p, overflow_p,
10355 : jump_target);
10356 :
10357 91899840 : ctx->global->cleanups = prev_cleanups;
10358 91899840 : unsigned int i;
10359 91899840 : tree cleanup, jmp_target = NULL_TREE;
10360 91899840 : bool eh = throws (jump_target);
10361 : /* Evaluate the cleanups. */
10362 185138786 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
10363 1339106 : if (cleanup == NULL_TREE)
10364 : {
10365 : /* NULL_TREE cleanup is a marker that before it is
10366 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
10367 : if the body didn't throw. */
10368 2341 : if (!eh)
10369 2339 : --i;
10370 : }
10371 : else
10372 1336765 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
10373 : non_constant_p, overflow_p,
10374 : &jmp_target);
10375 :
10376 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
10377 : full-expression. */
10378 285794289 : for (tree save_expr : save_exprs)
10379 10094769 : destroy_value_checked (ctx, save_expr, non_constant_p);
10380 91899840 : if (throws (&jmp_target))
10381 0 : *jump_target = jmp_target;
10382 91899840 : }
10383 91899840 : break;
10384 :
10385 58906411 : case MUST_NOT_THROW_EXPR:
10386 58906411 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10387 : lval,
10388 : non_constant_p, overflow_p,
10389 : jump_target);
10390 58906409 : if (throws (jump_target))
10391 : {
10392 : /* [except.handle]/7 - If the search for a handler exits the
10393 : function body of a function with a non-throwing exception
10394 : specification, the function std::terminate is invoked. */
10395 54 : if (!ctx->quiet)
10396 : {
10397 18 : auto_diagnostic_group d;
10398 18 : diagnose_std_terminate (loc, ctx, *jump_target);
10399 18 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
10400 14 : && ctx->call
10401 32 : && ctx->call->fundef)
10402 14 : inform (loc, "uncaught exception exited from %<noexcept%> "
10403 : "function %qD",
10404 : ctx->call->fundef->decl);
10405 4 : else if (MUST_NOT_THROW_THROW_P (t))
10406 2 : inform (loc, "destructor exited with an exception after "
10407 : "initializing the exception object");
10408 2 : else if (MUST_NOT_THROW_CATCH_P (t))
10409 2 : inform (loc, "constructor exited with another exception while "
10410 : "entering handler");
10411 18 : }
10412 54 : *non_constant_p = true;
10413 54 : *jump_target = NULL_TREE;
10414 54 : r = NULL_TREE;
10415 : }
10416 : break;
10417 :
10418 0 : case TRY_CATCH_EXPR:
10419 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
10420 : {
10421 0 : r = void_node;
10422 0 : break;
10423 : }
10424 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10425 : non_constant_p, overflow_p,
10426 : jump_target);
10427 0 : if (!*non_constant_p && throws (jump_target))
10428 : {
10429 0 : tree jmp_target = NULL_TREE;
10430 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
10431 : non_constant_p, overflow_p,
10432 : &jmp_target);
10433 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
10434 : jmp_target);
10435 : }
10436 : break;
10437 :
10438 842 : case TRY_FINALLY_EXPR:
10439 842 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10440 : non_constant_p, overflow_p,
10441 : jump_target);
10442 842 : if (!*non_constant_p)
10443 : {
10444 827 : tree jmp_target = NULL_TREE;
10445 : /* Also evaluate the cleanup. */
10446 827 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
10447 827 : && throws (jump_target))
10448 0 : cxx_eval_constant_expression (ctx,
10449 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
10450 : 1), vc_discard,
10451 : non_constant_p, overflow_p,
10452 : &jmp_target);
10453 : else
10454 827 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
10455 : non_constant_p, overflow_p,
10456 : &jmp_target);
10457 827 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
10458 : jmp_target);
10459 : }
10460 : break;
10461 :
10462 0 : case EH_ELSE_EXPR:
10463 : /* Evaluate any cleanup that applies to non-EH exits. */
10464 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
10465 : non_constant_p, overflow_p,
10466 : jump_target);
10467 :
10468 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
10469 0 : break;
10470 :
10471 3889788 : case CLEANUP_STMT:
10472 3889788 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
10473 : non_constant_p, overflow_p,
10474 : jump_target);
10475 3889802 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
10476 : {
10477 1742963 : iloc_sentinel ils (loc);
10478 1742963 : tree jmp_target = NULL_TREE;
10479 : /* Also evaluate the cleanup. */
10480 1742963 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
10481 : non_constant_p, overflow_p,
10482 : &jmp_target);
10483 1742963 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
10484 : jmp_target);
10485 1742963 : }
10486 : break;
10487 :
10488 : /* These differ from cxx_eval_unary_expression in that this doesn't
10489 : check for a constant operand or result; an address can be
10490 : constant without its operand being, and vice versa. */
10491 103916642 : case MEM_REF:
10492 103916642 : case INDIRECT_REF:
10493 103916642 : r = cxx_eval_indirect_ref (ctx, t, lval,
10494 : non_constant_p, overflow_p,
10495 : jump_target);
10496 103916642 : break;
10497 :
10498 101194455 : case ADDR_EXPR:
10499 101194455 : {
10500 101194455 : tree oldop = TREE_OPERAND (t, 0);
10501 101194455 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
10502 : non_constant_p, overflow_p,
10503 : jump_target);
10504 101194455 : if (*jump_target)
10505 : return NULL_TREE;
10506 : /* Don't VERIFY_CONSTANT here. */
10507 101194450 : if (*non_constant_p)
10508 : return t;
10509 81717172 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
10510 : /* This function does more aggressive folding than fold itself. */
10511 81717172 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
10512 81717172 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
10513 : {
10514 56589552 : ggc_free (r);
10515 56589552 : return t;
10516 : }
10517 : break;
10518 : }
10519 :
10520 855699 : case REALPART_EXPR:
10521 855699 : case IMAGPART_EXPR:
10522 855699 : if (lval)
10523 : {
10524 1386 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10525 : non_constant_p, overflow_p,
10526 : jump_target);
10527 1386 : if (*jump_target)
10528 : return NULL_TREE;
10529 1386 : if (r == error_mark_node)
10530 : ;
10531 1386 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
10532 : r = t;
10533 : else
10534 1276 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
10535 : break;
10536 : }
10537 : /* FALLTHRU */
10538 25801376 : case CONJ_EXPR:
10539 25801376 : case FIX_TRUNC_EXPR:
10540 25801376 : case FLOAT_EXPR:
10541 25801376 : case NEGATE_EXPR:
10542 25801376 : case ABS_EXPR:
10543 25801376 : case ABSU_EXPR:
10544 25801376 : case BIT_NOT_EXPR:
10545 25801376 : case TRUTH_NOT_EXPR:
10546 25801376 : case FIXED_CONVERT_EXPR:
10547 25801376 : case VEC_DUPLICATE_EXPR:
10548 25801376 : r = cxx_eval_unary_expression (ctx, t, lval,
10549 : non_constant_p, overflow_p,
10550 : jump_target);
10551 25801376 : break;
10552 :
10553 9801140 : case SIZEOF_EXPR:
10554 9801140 : r = fold_sizeof_expr (t);
10555 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
10556 : which could lead to an infinite recursion. */
10557 9801140 : if (TREE_CODE (r) != SIZEOF_EXPR)
10558 9801140 : r = cxx_eval_constant_expression (ctx, r, lval,
10559 : non_constant_p, overflow_p,
10560 : jump_target);
10561 : else
10562 : {
10563 0 : *non_constant_p = true;
10564 0 : gcc_assert (ctx->quiet);
10565 : }
10566 :
10567 : break;
10568 :
10569 13669623 : case COMPOUND_EXPR:
10570 13669623 : {
10571 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
10572 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
10573 : introduced by build_call_a. */
10574 13669623 : tree op0 = TREE_OPERAND (t, 0);
10575 13669623 : tree op1 = TREE_OPERAND (t, 1);
10576 13669623 : STRIP_NOPS (op1);
10577 5558659 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
10578 16278730 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
10579 6817418 : r = cxx_eval_constant_expression (ctx, op0,
10580 : lval, non_constant_p, overflow_p,
10581 : jump_target);
10582 : else
10583 : {
10584 : /* Check that the LHS is constant and then discard it. */
10585 6852205 : cxx_eval_constant_expression (ctx, op0, vc_discard,
10586 : non_constant_p, overflow_p,
10587 : jump_target);
10588 6852205 : if (*jump_target)
10589 : return NULL_TREE;
10590 6844962 : if (*non_constant_p)
10591 : return t;
10592 6570398 : op1 = TREE_OPERAND (t, 1);
10593 6570398 : r = cxx_eval_constant_expression (ctx, op1,
10594 : lval, non_constant_p, overflow_p,
10595 : jump_target);
10596 : }
10597 : }
10598 : break;
10599 :
10600 111760920 : case POINTER_PLUS_EXPR:
10601 111760920 : case POINTER_DIFF_EXPR:
10602 111760920 : case PLUS_EXPR:
10603 111760920 : case MINUS_EXPR:
10604 111760920 : case MULT_EXPR:
10605 111760920 : case TRUNC_DIV_EXPR:
10606 111760920 : case CEIL_DIV_EXPR:
10607 111760920 : case FLOOR_DIV_EXPR:
10608 111760920 : case ROUND_DIV_EXPR:
10609 111760920 : case TRUNC_MOD_EXPR:
10610 111760920 : case CEIL_MOD_EXPR:
10611 111760920 : case ROUND_MOD_EXPR:
10612 111760920 : case RDIV_EXPR:
10613 111760920 : case EXACT_DIV_EXPR:
10614 111760920 : case MIN_EXPR:
10615 111760920 : case MAX_EXPR:
10616 111760920 : case LSHIFT_EXPR:
10617 111760920 : case RSHIFT_EXPR:
10618 111760920 : case LROTATE_EXPR:
10619 111760920 : case RROTATE_EXPR:
10620 111760920 : case BIT_IOR_EXPR:
10621 111760920 : case BIT_XOR_EXPR:
10622 111760920 : case BIT_AND_EXPR:
10623 111760920 : case TRUTH_XOR_EXPR:
10624 111760920 : case LT_EXPR:
10625 111760920 : case LE_EXPR:
10626 111760920 : case GT_EXPR:
10627 111760920 : case GE_EXPR:
10628 111760920 : case EQ_EXPR:
10629 111760920 : case NE_EXPR:
10630 111760920 : case SPACESHIP_EXPR:
10631 111760920 : case UNORDERED_EXPR:
10632 111760920 : case ORDERED_EXPR:
10633 111760920 : case UNLT_EXPR:
10634 111760920 : case UNLE_EXPR:
10635 111760920 : case UNGT_EXPR:
10636 111760920 : case UNGE_EXPR:
10637 111760920 : case UNEQ_EXPR:
10638 111760920 : case LTGT_EXPR:
10639 111760920 : case RANGE_EXPR:
10640 111760920 : case COMPLEX_EXPR:
10641 111760920 : r = cxx_eval_binary_expression (ctx, t, lval,
10642 : non_constant_p, overflow_p,
10643 : jump_target);
10644 111760920 : break;
10645 :
10646 : /* fold can introduce non-IF versions of these; still treat them as
10647 : short-circuiting. */
10648 15509981 : case TRUTH_AND_EXPR:
10649 15509981 : case TRUTH_ANDIF_EXPR:
10650 15509981 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
10651 : boolean_true_node,
10652 : non_constant_p, overflow_p,
10653 : jump_target);
10654 15509981 : break;
10655 :
10656 3071951 : case TRUTH_OR_EXPR:
10657 3071951 : case TRUTH_ORIF_EXPR:
10658 3071951 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
10659 : boolean_false_node,
10660 : non_constant_p, overflow_p,
10661 : jump_target);
10662 3071951 : break;
10663 :
10664 15671074 : case ARRAY_REF:
10665 15671074 : r = cxx_eval_array_reference (ctx, t, lval,
10666 : non_constant_p, overflow_p,
10667 : jump_target);
10668 15671074 : break;
10669 :
10670 115867088 : case COMPONENT_REF:
10671 115867088 : if (is_overloaded_fn (t))
10672 : {
10673 : /* We can only get here in checking mode via
10674 : build_non_dependent_expr, because any expression that
10675 : calls or takes the address of the function will have
10676 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
10677 6 : gcc_checking_assert (ctx->quiet || errorcount);
10678 6 : *non_constant_p = true;
10679 6 : return t;
10680 : }
10681 115867082 : r = cxx_eval_component_reference (ctx, t, lval,
10682 : non_constant_p, overflow_p,
10683 : jump_target);
10684 115867082 : break;
10685 :
10686 40745 : case BIT_FIELD_REF:
10687 40745 : r = cxx_eval_bit_field_ref (ctx, t, lval,
10688 : non_constant_p, overflow_p,
10689 : jump_target);
10690 40745 : break;
10691 :
10692 39514669 : case COND_EXPR:
10693 39514669 : case IF_STMT:
10694 39514669 : if (*jump_target)
10695 : {
10696 151552 : tree orig_jump = *jump_target;
10697 151552 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
10698 303104 : ? TREE_OPERAND (t, 1) : void_node);
10699 : /* When jumping to a label, the label might be either in the
10700 : then or else blocks, so process then block first in skipping
10701 : mode first, and if we are still in the skipping mode at its end,
10702 : process the else block too. */
10703 151552 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10704 : overflow_p, jump_target);
10705 : /* It's possible that we found the label in the then block. But
10706 : it could have been followed by another jumping statement, e.g.
10707 : say we're looking for case 1:
10708 : if (cond)
10709 : {
10710 : // skipped statements
10711 : case 1:; // clears up *jump_target
10712 : return 1; // and sets it to a RETURN_EXPR
10713 : }
10714 : else { ... }
10715 : in which case we need not go looking to the else block.
10716 : (goto is not allowed in a constexpr function.) */
10717 151552 : if (*jump_target == orig_jump)
10718 : {
10719 151609 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
10720 151609 : ? TREE_OPERAND (t, 2) : void_node);
10721 151514 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10722 : overflow_p, jump_target);
10723 : }
10724 : break;
10725 : }
10726 39363117 : r = cxx_eval_conditional_expression (ctx, t, lval,
10727 : non_constant_p, overflow_p,
10728 : jump_target);
10729 39363117 : break;
10730 11755 : case VEC_COND_EXPR:
10731 11755 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10732 : overflow_p, jump_target);
10733 11755 : break;
10734 :
10735 23405690 : case CONSTRUCTOR:
10736 23405690 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10737 : {
10738 : /* Don't re-process a constant CONSTRUCTOR. */
10739 20035180 : verify_constructor_flags (t);
10740 20035180 : if (TREE_CONSTANT (t))
10741 : return t;
10742 : }
10743 3370510 : if (TREE_CLOBBER_P (t))
10744 : {
10745 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10746 : a clobber by itself isn't a constant-expression. */
10747 242 : gcc_assert (ctx->quiet);
10748 242 : *non_constant_p = true;
10749 242 : break;
10750 : }
10751 3370268 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10752 : non_constant_p, overflow_p, jump_target);
10753 3370268 : break;
10754 :
10755 800 : case VEC_INIT_EXPR:
10756 : /* We can get this in a defaulted constructor for a class with a
10757 : non-static data member of array type. Either the initializer will
10758 : be NULL, meaning default-initialization, or it will be an lvalue
10759 : or xvalue of the same type, meaning direct-initialization from the
10760 : corresponding member. */
10761 800 : r = cxx_eval_vec_init (ctx, t, lval,
10762 : non_constant_p, overflow_p, jump_target);
10763 800 : break;
10764 :
10765 43640 : case VEC_PERM_EXPR:
10766 43640 : r = cxx_eval_trinary_expression (ctx, t, lval,
10767 : non_constant_p, overflow_p,
10768 : jump_target);
10769 43640 : break;
10770 :
10771 7 : case PAREN_EXPR:
10772 7 : gcc_assert (!REF_PARENTHESIZED_P (t));
10773 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10774 : constant expressions since it's unaffected by -fassociative-math. */
10775 7 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10776 : non_constant_p, overflow_p,
10777 : jump_target);
10778 7 : break;
10779 :
10780 441162115 : case NOP_EXPR:
10781 441162115 : if (REINTERPRET_CAST_P (t))
10782 : {
10783 145 : if (!ctx->quiet)
10784 6 : error_at (loc,
10785 : "%<reinterpret_cast%> is not a constant expression");
10786 145 : *non_constant_p = true;
10787 145 : return t;
10788 : }
10789 : /* FALLTHROUGH. */
10790 553110356 : case CONVERT_EXPR:
10791 553110356 : case VIEW_CONVERT_EXPR:
10792 553110356 : case UNARY_PLUS_EXPR:
10793 553110356 : {
10794 553110356 : tree oldop = TREE_OPERAND (t, 0);
10795 :
10796 553110356 : tree op = cxx_eval_constant_expression (ctx, oldop,
10797 553110356 : VOID_TYPE_P (TREE_TYPE (t))
10798 : ? vc_discard
10799 : : tcode == VIEW_CONVERT_EXPR
10800 519329557 : ? lval : vc_prvalue,
10801 : non_constant_p, overflow_p,
10802 : jump_target);
10803 553107657 : if (*jump_target)
10804 : return NULL_TREE;
10805 553103223 : if (*non_constant_p)
10806 : return t;
10807 446521621 : tree type = TREE_TYPE (t);
10808 :
10809 446521621 : if (VOID_TYPE_P (type))
10810 32587069 : return void_node;
10811 :
10812 413934552 : if (TREE_CODE (t) == CONVERT_EXPR
10813 60796709 : && ARITHMETIC_TYPE_P (type)
10814 5036714 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10815 413958315 : && ctx->strict)
10816 : {
10817 23668 : if (!ctx->quiet)
10818 54 : error_at (loc,
10819 : "conversion from pointer type %qT to arithmetic type "
10820 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10821 23668 : *non_constant_p = true;
10822 23668 : return t;
10823 : }
10824 :
10825 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10826 : type cannot be part of a core constant expression as a resolution to
10827 : DR 1312. */
10828 153725230 : if (TYPE_PTROB_P (type)
10829 150355467 : && TYPE_PTR_P (TREE_TYPE (op))
10830 94473443 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10831 : /* Inside a call to std::construct_at,
10832 : std::allocator<T>::{,de}allocate, or
10833 : std::source_location::current, we permit casting from void*
10834 : because that is compiler-generated code. */
10835 1917926 : && !is_std_construct_at (ctx->call)
10836 166031 : && !is_std_allocator_allocate_deallocate (ctx->call)
10837 413996164 : && !is_std_source_location_current (ctx->call))
10838 : {
10839 : /* Likewise, don't error when casting from void* when OP is
10840 : &heap uninit and similar. */
10841 85121 : tree sop = tree_strip_nop_conversions (op);
10842 85121 : tree decl = NULL_TREE;
10843 85121 : if (TREE_CODE (sop) == ADDR_EXPR)
10844 62916 : decl = TREE_OPERAND (sop, 0);
10845 62916 : if (decl
10846 62916 : && VAR_P (decl)
10847 61488 : && DECL_ARTIFICIAL (decl)
10848 123970 : && heap_var_p (decl))
10849 : /* OK */;
10850 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10851 : cv void" to a pointer-to-object type T unless P is a null
10852 : pointer value or points to an object whose type is similar to
10853 : T. */
10854 24478 : else if (cxx_dialect > cxx23)
10855 : {
10856 24288 : if (integer_zerop (sop))
10857 30 : return build_int_cst (type, 0);
10858 24258 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10859 : NULL, jump_target);
10860 24258 : if (*jump_target)
10861 : return NULL_TREE;
10862 24258 : if (r)
10863 : {
10864 24211 : r = build1 (ADDR_EXPR, type, r);
10865 24211 : break;
10866 : }
10867 47 : if (!ctx->quiet)
10868 : {
10869 5 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10870 5 : auto_diagnostic_group d;
10871 5 : error_at (loc, "cast from %qT is not allowed in a "
10872 : "constant expression because "
10873 : "pointed-to type %qT is not similar to %qT",
10874 5 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10875 5 : TREE_TYPE (type));
10876 5 : tree obj = build_fold_indirect_ref (sop);
10877 5 : if (TREE_CODE (obj) == COMPONENT_REF)
10878 4 : obj = TREE_OPERAND (obj, 1);
10879 5 : if (DECL_P (obj))
10880 5 : inform (DECL_SOURCE_LOCATION (obj),
10881 : "pointed-to object declared here");
10882 5 : }
10883 47 : *non_constant_p = true;
10884 47 : return t;
10885 : }
10886 : else
10887 : {
10888 190 : if (!ctx->quiet)
10889 26 : error_at (loc, "cast from %qT is not allowed in a "
10890 : "constant expression before C++26",
10891 26 : TREE_TYPE (op));
10892 190 : *non_constant_p = true;
10893 190 : return t;
10894 : }
10895 : }
10896 :
10897 : /* [expr.static.cast]/10: A prvalue of type "pointer to cv1 B", where
10898 : B is a class type, can be converted to a prvalue of type
10899 : "pointer to cv2 D", where D is a complete class derived from B, ...
10900 : If the prvalue of type "pointer to cv1 B" points to a B that is
10901 : actually a base class subobject of an object of type D, the
10902 : resulting pointer points to the enclosing object of type D.
10903 : Otherwise, the behavior is undefined.
10904 : Similarly [expr.static.cast]/2 for references. */
10905 413886406 : if (INDIRECT_TYPE_P (type)
10906 257601345 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10907 257466475 : && COMPLETE_TYPE_P (TREE_TYPE (type))
10908 254279033 : && !integer_zerop (op)
10909 667442267 : && is_properly_derived_from (TREE_TYPE (type),
10910 253555861 : TREE_TYPE (TREE_TYPE (op))))
10911 : {
10912 14503 : tree sop = tree_strip_nop_conversions (op);
10913 14503 : if (cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10914 : NULL, jump_target) == NULL_TREE)
10915 : {
10916 284 : tree dyntype = NULL_TREE;
10917 284 : if (!ctx->quiet && TREE_CODE (sop) == ADDR_EXPR)
10918 : {
10919 44 : sop = TREE_OPERAND (sop, 0);
10920 44 : while (TREE_CODE (sop) == COMPONENT_REF
10921 59 : && DECL_FIELD_IS_BASE (TREE_OPERAND (sop, 1)))
10922 15 : sop = TREE_OPERAND (sop, 0);
10923 44 : dyntype = strip_array_types (TREE_TYPE (sop));
10924 44 : if (same_type_p (dyntype, TREE_TYPE (TREE_TYPE (op))))
10925 261 : dyntype = NULL_TREE;
10926 : }
10927 284 : if (!ctx->quiet)
10928 : {
10929 53 : if (dyntype)
10930 23 : error_at (loc, "%qT operand (of dynamic type %qT) is "
10931 : "not a base class subobject of a %qT object",
10932 23 : TREE_TYPE (TREE_TYPE (op)), dyntype,
10933 23 : TREE_TYPE (type));
10934 : else
10935 30 : error_at (loc, "%qT operand is not a base class "
10936 : "subobject of a %qT object",
10937 30 : TREE_TYPE (TREE_TYPE (op)), TREE_TYPE (type));
10938 : }
10939 284 : *non_constant_p = true;
10940 284 : return t;
10941 : }
10942 : }
10943 :
10944 413886122 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
10945 : {
10946 1064 : op = cplus_expand_constant (op);
10947 1064 : if (TREE_CODE (op) == PTRMEM_CST)
10948 : {
10949 21 : if (!ctx->quiet)
10950 3 : error_at (loc, "%qE is not a constant expression when the "
10951 : "class %qT is still incomplete", op,
10952 3 : PTRMEM_CST_CLASS (op));
10953 21 : *non_constant_p = true;
10954 21 : return t;
10955 : }
10956 : }
10957 :
10958 413886101 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
10959 : {
10960 657 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
10961 657 : && !can_convert_qual (type, op))
10962 6 : op = cplus_expand_constant (op);
10963 657 : return cp_fold_convert (type, op);
10964 : }
10965 :
10966 413885444 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
10967 : {
10968 833513 : if (integer_zerop (op))
10969 : {
10970 776931 : if (TYPE_REF_P (type))
10971 : {
10972 36 : if (!ctx->quiet)
10973 4 : error_at (loc, "dereferencing a null pointer");
10974 36 : *non_constant_p = true;
10975 36 : return t;
10976 : }
10977 : }
10978 56582 : else if (TYPE_PTR_P (type)
10979 56582 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10980 : /* INTEGER_CST with pointer-to-method type is only used
10981 : for a virtual method in a pointer to member function.
10982 : Don't reject those. */
10983 : ;
10984 : else
10985 : {
10986 : /* This detects for example:
10987 : reinterpret_cast<void*>(sizeof 0)
10988 : */
10989 56579 : if (!ctx->quiet)
10990 6 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
10991 : "a constant expression",
10992 : type, op);
10993 56579 : *non_constant_p = true;
10994 56579 : return t;
10995 : }
10996 : }
10997 :
10998 413828829 : if (INDIRECT_TYPE_P (type)
10999 257544446 : && TREE_CODE (op) == NOP_EXPR
11000 123306536 : && TREE_TYPE (op) == ptr_type_node
11001 517165 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
11002 515050 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
11003 414231849 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
11004 403020 : 0)) == heap_uninit_identifier
11005 287426 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
11006 287426 : 0)) == heap_vec_uninit_identifier))
11007 : {
11008 117585 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
11009 117585 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
11010 117585 : tree elt_type = TREE_TYPE (type);
11011 117585 : tree cookie_size = NULL_TREE;
11012 117585 : tree arg_size = NULL_TREE;
11013 117585 : if (TREE_CODE (elt_type) == RECORD_TYPE
11014 117585 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
11015 : {
11016 9 : tree fld1 = TYPE_FIELDS (elt_type);
11017 9 : tree fld2 = DECL_CHAIN (fld1);
11018 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
11019 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
11020 : }
11021 117585 : DECL_NAME (var)
11022 117585 : = (DECL_NAME (var) == heap_uninit_identifier
11023 117585 : ? heap_identifier : heap_vec_identifier);
11024 : /* For zero sized elt_type, try to recover how many outer_nelts
11025 : it should have. */
11026 117585 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
11027 117576 : : integer_zerop (var_size))
11028 109 : && !int_size_in_bytes (elt_type)
11029 9 : && TREE_CODE (oldop) == CALL_EXPR
11030 117603 : && call_expr_nargs (oldop) >= 1)
11031 9 : if (tree fun = get_function_named_in_call (oldop))
11032 9 : if (cxx_replaceable_global_alloc_fn (fun)
11033 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
11034 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
11035 117585 : tree new_type
11036 117585 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
11037 : var_size, arg_size,
11038 : non_constant_p, overflow_p,
11039 : jump_target);
11040 117585 : if (*jump_target)
11041 : return NULL_TREE;
11042 117585 : TREE_TYPE (var) = new_type;
11043 117585 : TREE_TYPE (TREE_OPERAND (op, 0))
11044 117585 : = build_pointer_type (TREE_TYPE (var));
11045 117585 : if (is_std_allocator_allocate (ctx->call))
11046 : {
11047 : /* [allocator.members]/5: This function starts the lifetime
11048 : of the array object, but not that of any of the array
11049 : elements. */
11050 80751 : tree ctor = build_constructor (new_type, NULL);
11051 80751 : CONSTRUCTOR_NO_CLEARING (ctor) = 1;
11052 80751 : CONSTRUCTOR_OMITTED_NOT_WITHIN_LIFETIME_P (ctor) = 1;
11053 80751 : ctx->global->put_value (var, ctor);
11054 : }
11055 : }
11056 :
11057 : /* This can happen for std::meta::info(^^int) where the cast has no
11058 : meaning. */
11059 413828829 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
11060 : {
11061 : r = op;
11062 : break;
11063 : }
11064 :
11065 413738413 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
11066 : /* We didn't fold at the top so we could check for ptr-int
11067 : conversion. */
11068 32607333 : return fold (t);
11069 :
11070 381131080 : tree sop;
11071 :
11072 : /* Handle an array's bounds having been deduced after we built
11073 : the wrapping expression. */
11074 381131080 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
11075 : r = op;
11076 167142838 : else if (sop = tree_strip_nop_conversions (op),
11077 234315875 : sop != op && (same_type_ignoring_tlq_and_bounds_p
11078 67173037 : (type, TREE_TYPE (sop))))
11079 : r = sop;
11080 138069644 : else if (tcode == UNARY_PLUS_EXPR)
11081 0 : r = fold_convert (TREE_TYPE (t), op);
11082 : else
11083 138069644 : r = fold_build1 (tcode, type, op);
11084 :
11085 : /* Conversion of an out-of-range value has implementation-defined
11086 : behavior; the language considers it different from arithmetic
11087 : overflow, which is undefined. */
11088 381131080 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
11089 12877 : TREE_OVERFLOW (r) = false;
11090 : }
11091 : break;
11092 :
11093 9897 : case EXCESS_PRECISION_EXPR:
11094 9897 : {
11095 9897 : tree oldop = TREE_OPERAND (t, 0);
11096 :
11097 9897 : tree op = cxx_eval_constant_expression (ctx, oldop,
11098 : lval,
11099 : non_constant_p, overflow_p,
11100 : jump_target);
11101 9897 : if (*jump_target)
11102 : return NULL_TREE;
11103 9897 : if (*non_constant_p)
11104 : return t;
11105 9891 : r = fold_convert (TREE_TYPE (t), op);
11106 9891 : break;
11107 : }
11108 :
11109 66711 : case EMPTY_CLASS_EXPR:
11110 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
11111 : it to an appropriate CONSTRUCTOR. */
11112 66711 : return build_constructor (TREE_TYPE (t), NULL);
11113 :
11114 48084592 : case STATEMENT_LIST:
11115 48084592 : new_ctx = *ctx;
11116 48084592 : new_ctx.ctor = new_ctx.object = NULL_TREE;
11117 48084592 : return cxx_eval_statement_list (&new_ctx, t,
11118 48084588 : non_constant_p, overflow_p, jump_target);
11119 :
11120 33479113 : case BIND_EXPR:
11121 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
11122 : map, so that when checking whether they're already destroyed later we
11123 : don't get confused by remnants of previous calls. */
11124 52770092 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
11125 19290979 : ctx->global->clear_value (decl);
11126 33479113 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
11127 : lval,
11128 : non_constant_p, overflow_p,
11129 : jump_target);
11130 52770088 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
11131 19290977 : destroy_value_checked (ctx, decl, non_constant_p);
11132 : break;
11133 :
11134 8824003 : case PREINCREMENT_EXPR:
11135 8824003 : case POSTINCREMENT_EXPR:
11136 8824003 : case PREDECREMENT_EXPR:
11137 8824003 : case POSTDECREMENT_EXPR:
11138 8824003 : return cxx_eval_increment_expression (ctx, t,
11139 : lval, non_constant_p, overflow_p,
11140 8824003 : jump_target);
11141 :
11142 2382 : case THROW_EXPR:
11143 2382 : if (cxx_dialect >= cxx26)
11144 2055 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
11145 : non_constant_p, overflow_p,
11146 2055 : jump_target);
11147 : /* FALLTHROUGH */
11148 335 : case LAMBDA_EXPR:
11149 335 : case NEW_EXPR:
11150 335 : case VEC_NEW_EXPR:
11151 335 : case DELETE_EXPR:
11152 335 : case VEC_DELETE_EXPR:
11153 335 : case MODOP_EXPR:
11154 : /* GCC internal stuff. */
11155 335 : case VA_ARG_EXPR:
11156 335 : case BASELINK:
11157 335 : case OFFSET_REF:
11158 335 : if (!ctx->quiet)
11159 86 : error_at (loc, "expression %qE is not a constant expression", t);
11160 335 : *non_constant_p = true;
11161 335 : break;
11162 :
11163 398302 : case OBJ_TYPE_REF:
11164 : /* Virtual function lookup. We don't need to do anything fancy. */
11165 398302 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
11166 : lval, non_constant_p, overflow_p,
11167 398302 : jump_target);
11168 :
11169 16142 : case PLACEHOLDER_EXPR:
11170 : /* Use of the value or address of the current object. */
11171 16142 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
11172 : {
11173 15899 : if (TREE_CODE (ctor) == CONSTRUCTOR)
11174 : return ctor;
11175 : else
11176 15597 : return cxx_eval_constant_expression (ctx, ctor, lval,
11177 : non_constant_p, overflow_p,
11178 15597 : jump_target);
11179 : }
11180 : /* A placeholder without a referent. We can get here when
11181 : checking whether NSDMIs are noexcept, or in massage_init_elt;
11182 : just say it's non-constant for now. */
11183 243 : gcc_assert (ctx->quiet);
11184 243 : *non_constant_p = true;
11185 243 : break;
11186 :
11187 6953 : case EXIT_EXPR:
11188 6953 : {
11189 6953 : tree cond = TREE_OPERAND (t, 0);
11190 6953 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
11191 : non_constant_p, overflow_p,
11192 : jump_target);
11193 6953 : if (*jump_target)
11194 : return NULL_TREE;
11195 6953 : VERIFY_CONSTANT (cond);
11196 6953 : if (integer_nonzerop (cond))
11197 3401 : *jump_target = t;
11198 : }
11199 : break;
11200 :
11201 83 : case GOTO_EXPR:
11202 83 : if (breaks (&TREE_OPERAND (t, 0))
11203 83 : || continues (&TREE_OPERAND (t, 0)))
11204 64 : *jump_target = TREE_OPERAND (t, 0);
11205 : else
11206 : {
11207 19 : if (!ctx->quiet)
11208 1 : error_at (loc, "%<goto%> is not a constant expression");
11209 19 : *non_constant_p = true;
11210 : }
11211 : break;
11212 :
11213 6646544 : case LOOP_EXPR:
11214 6646544 : case DO_STMT:
11215 6646544 : case WHILE_STMT:
11216 6646544 : case FOR_STMT:
11217 6646544 : cxx_eval_loop_expr (ctx, t,
11218 : non_constant_p, overflow_p, jump_target);
11219 6646544 : break;
11220 :
11221 50127 : case SWITCH_EXPR:
11222 50127 : case SWITCH_STMT:
11223 50127 : cxx_eval_switch_expr (ctx, t,
11224 : non_constant_p, overflow_p, jump_target);
11225 50127 : break;
11226 :
11227 215 : case REQUIRES_EXPR:
11228 : /* It's possible to get a requires-expression in a constant
11229 : expression. For example:
11230 :
11231 : template<typename T> concept bool C() {
11232 : return requires (T t) { t; };
11233 : }
11234 :
11235 : template<typename T> requires !C<T>() void f(T);
11236 :
11237 : Normalization leaves f with the associated constraint
11238 : '!requires (T t) { ... }' which is not transformed into
11239 : a constraint. */
11240 215 : if (!processing_template_decl)
11241 215 : return evaluate_requires_expr (t);
11242 : else
11243 0 : *non_constant_p = true;
11244 0 : return t;
11245 :
11246 1395245 : case ANNOTATE_EXPR:
11247 1395245 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
11248 : lval,
11249 : non_constant_p, overflow_p,
11250 : jump_target);
11251 1395245 : break;
11252 :
11253 9366 : case USING_STMT:
11254 9366 : r = void_node;
11255 9366 : break;
11256 :
11257 59 : case ASSERTION_STMT:
11258 59 : case PRECONDITION_STMT:
11259 59 : case POSTCONDITION_STMT:
11260 59 : {
11261 59 : r = void_node;
11262 : /* Only record the first fail, and do not go further is the semantic
11263 : is 'ignore'. */
11264 59 : if (*non_constant_p
11265 59 : || (ctx->global->contract_statement
11266 0 : && contract_terminating_p (ctx->global->contract_statement))
11267 118 : || contract_ignored_p (t))
11268 : break;
11269 :
11270 59 : tree cond = CONTRACT_CONDITION (t);
11271 59 : bool ctrct_non_const_p = !potential_rvalue_constant_expression (cond);
11272 59 : tree eval = boolean_true_node;
11273 59 : if (!ctrct_non_const_p)
11274 : {
11275 : /* We need to evaluate and stash the result of this here, since
11276 : whether it needs to be reported (and how) depends on whether the
11277 : containing expression is otherwise const. */
11278 50 : bool ctrct_overflow_p = false;
11279 50 : tree jmp_target = NULL_TREE;
11280 50 : constexpr_ctx new_ctx = *ctx;
11281 50 : new_ctx.quiet = true;
11282 : /* Avoid modification of existing values. */
11283 50 : modifiable_tracker ms (new_ctx.global);
11284 50 : eval = (cxx_eval_constant_expression
11285 50 : (&new_ctx, cond, vc_prvalue, &ctrct_non_const_p,
11286 : &ctrct_overflow_p, &jmp_target));
11287 50 : }
11288 :
11289 : /* Not a constant or check failed. */
11290 59 : if (ctrct_non_const_p || integer_zerop (eval))
11291 : {
11292 57 : ctx->global->contract_statement = t;
11293 57 : ctx->global->contract_condition_non_const = ctrct_non_const_p;
11294 : }
11295 :
11296 : /* If diagnosing, do it while we have the cx_call_context. */
11297 59 : if (!ctx->quiet
11298 59 : && check_for_failed_contracts (ctx))
11299 : /* And for a terminating semantic, stop evaluating. */
11300 24 : *non_constant_p = true;
11301 : }
11302 59 : break;
11303 :
11304 2753643 : case TEMPLATE_ID_EXPR:
11305 2753643 : {
11306 : /* We can evaluate template-id that refers to a concept only if
11307 : the template arguments are non-dependent. */
11308 2753643 : gcc_assert (concept_check_p (t));
11309 :
11310 2753643 : if (!value_dependent_expression_p (t)
11311 2753643 : && !uid_sensitive_constexpr_evaluation_p ())
11312 2752720 : r = evaluate_concept_check (t);
11313 : else
11314 923 : *non_constant_p = true;
11315 :
11316 : break;
11317 : }
11318 :
11319 51 : case ASM_EXPR:
11320 51 : if (!ctx->quiet)
11321 29 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
11322 51 : *non_constant_p = true;
11323 51 : return t;
11324 :
11325 4047 : case BIT_CAST_EXPR:
11326 4047 : if (lval)
11327 : {
11328 0 : if (!ctx->quiet)
11329 0 : error_at (EXPR_LOCATION (t),
11330 : "address of a call to %qs is not a constant expression",
11331 : "__builtin_bit_cast");
11332 0 : *non_constant_p = true;
11333 0 : return t;
11334 : }
11335 4047 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
11336 4047 : break;
11337 :
11338 0 : case OMP_PARALLEL:
11339 0 : case OMP_TASK:
11340 0 : case OMP_FOR:
11341 0 : case OMP_SIMD:
11342 0 : case OMP_DISTRIBUTE:
11343 0 : case OMP_TASKLOOP:
11344 0 : case OMP_LOOP:
11345 0 : case OMP_TEAMS:
11346 0 : case OMP_TARGET_DATA:
11347 0 : case OMP_TARGET:
11348 0 : case OMP_SECTIONS:
11349 0 : case OMP_ORDERED:
11350 0 : case OMP_CRITICAL:
11351 0 : case OMP_SINGLE:
11352 0 : case OMP_SCAN:
11353 0 : case OMP_SCOPE:
11354 0 : case OMP_SECTION:
11355 0 : case OMP_STRUCTURED_BLOCK:
11356 0 : case OMP_MASTER:
11357 0 : case OMP_MASKED:
11358 0 : case OMP_TASKGROUP:
11359 0 : case OMP_TARGET_UPDATE:
11360 0 : case OMP_TARGET_ENTER_DATA:
11361 0 : case OMP_TARGET_EXIT_DATA:
11362 0 : case OMP_ATOMIC:
11363 0 : case OMP_ATOMIC_READ:
11364 0 : case OMP_ATOMIC_CAPTURE_OLD:
11365 0 : case OMP_ATOMIC_CAPTURE_NEW:
11366 0 : case OMP_DEPOBJ:
11367 0 : case OMP_ALLOCATE:
11368 0 : case OACC_PARALLEL:
11369 0 : case OACC_KERNELS:
11370 0 : case OACC_SERIAL:
11371 0 : case OACC_DATA:
11372 0 : case OACC_HOST_DATA:
11373 0 : case OACC_LOOP:
11374 0 : case OACC_CACHE:
11375 0 : case OACC_DECLARE:
11376 0 : case OACC_ENTER_DATA:
11377 0 : case OACC_EXIT_DATA:
11378 0 : case OACC_UPDATE:
11379 0 : if (!ctx->quiet)
11380 0 : error_at (EXPR_LOCATION (t),
11381 : "statement is not a constant expression");
11382 0 : *non_constant_p = true;
11383 0 : break;
11384 :
11385 0 : default:
11386 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
11387 : {
11388 : /* This function doesn't know how to deal with pre-genericize
11389 : statements; this can only happen with statement-expressions,
11390 : so for now just fail. */
11391 0 : if (!ctx->quiet)
11392 0 : error_at (EXPR_LOCATION (t),
11393 : "statement is not a constant expression");
11394 : }
11395 0 : else if (flag_checking)
11396 0 : internal_error ("unexpected expression %qE of kind %s", t,
11397 : get_tree_code_name (TREE_CODE (t)));
11398 0 : *non_constant_p = true;
11399 0 : break;
11400 : }
11401 :
11402 2055912128 : if (r == error_mark_node)
11403 14533015 : *non_constant_p = true;
11404 :
11405 135125175 : if (r == void_node && lval != vc_discard && !*jump_target
11406 2055929943 : && !VOID_TYPE_P (TREE_TYPE (t)))
11407 : {
11408 : /* For diagnostic quality we should have handled this sooner, where we
11409 : can be more specific about the out-of-lifetime object. But here we
11410 : can still be correct. */
11411 2 : gcc_checking_assert (false);
11412 : if (!ctx->quiet)
11413 : error_at (EXPR_LOCATION (t),
11414 : "%qE accesses an object outside its lifetime", t);
11415 : *non_constant_p = true;
11416 : }
11417 :
11418 2055912126 : if (*non_constant_p)
11419 : return t;
11420 : else
11421 1636614118 : return r;
11422 : }
11423 :
11424 : /* P0859: A function is needed for constant evaluation if it is a constexpr
11425 : function that is named by an expression ([basic.def.odr]) that is
11426 : potentially constant evaluated.
11427 :
11428 : So we need to instantiate any constexpr functions mentioned by the
11429 : expression even if the definition isn't needed for evaluating the
11430 : expression. */
11431 :
11432 : static tree
11433 589634244 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
11434 : {
11435 589634244 : if (TREE_CODE (*tp) == FUNCTION_DECL
11436 12148300 : && DECL_DECLARED_CONSTEXPR_P (*tp)
11437 12012369 : && !DECL_INITIAL (*tp)
11438 3094421 : && !trivial_fn_p (*tp)
11439 3094272 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
11440 589634244 : && !uid_sensitive_constexpr_evaluation_p ())
11441 : {
11442 3064796 : ++function_depth;
11443 3064796 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
11444 3064764 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
11445 : else
11446 32 : synthesize_method (*tp);
11447 3064796 : --function_depth;
11448 : }
11449 586569448 : else if (TREE_CODE (*tp) == CALL_EXPR
11450 574907074 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
11451 : {
11452 12212816 : if (EXPR_HAS_LOCATION (*tp))
11453 12205875 : input_location = EXPR_LOCATION (*tp);
11454 : }
11455 :
11456 589634244 : if (!EXPR_P (*tp))
11457 336767230 : *walk_subtrees = 0;
11458 :
11459 589634244 : return NULL_TREE;
11460 : }
11461 :
11462 : static void
11463 287443431 : instantiate_constexpr_fns (tree t)
11464 : {
11465 287443431 : location_t loc = input_location;
11466 287443431 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
11467 287443431 : input_location = loc;
11468 287443431 : }
11469 :
11470 : /* Look for heap variables in the expression *TP. */
11471 :
11472 : static tree
11473 375734 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
11474 : {
11475 375734 : if (VAR_P (*tp) && heap_var_p (*tp, /*include_deleted_p=*/true))
11476 113099 : return *tp;
11477 :
11478 262635 : if (TYPE_P (*tp))
11479 68 : *walk_subtrees = 0;
11480 : return NULL_TREE;
11481 : }
11482 :
11483 : /* Find immediate function decls in *TP if any. */
11484 :
11485 : static tree
11486 620530261 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
11487 : {
11488 624562735 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
11489 : return *tp;
11490 620523633 : if (TREE_CODE (*tp) == PTRMEM_CST
11491 29679 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
11492 620552495 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
11493 : return PTRMEM_CST_MEMBER (*tp);
11494 620523520 : if (REFLECT_EXPR_P (*tp))
11495 86617 : *walk_subtrees = 0;
11496 : return NULL_TREE;
11497 : }
11498 :
11499 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
11500 : expression. Return a version of T that has TREE_CONSTANT cleared. */
11501 :
11502 : static tree
11503 147399 : mark_non_constant (tree t)
11504 : {
11505 147399 : gcc_checking_assert (TREE_CONSTANT (t));
11506 :
11507 : /* This isn't actually constant, so unset TREE_CONSTANT.
11508 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
11509 : it to be set if it is invariant address, even when it is not
11510 : a valid C++ constant expression. Wrap it with a NOP_EXPR
11511 : instead. */
11512 147399 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
11513 145423 : t = copy_node (t);
11514 1976 : else if (TREE_CODE (t) == CONSTRUCTOR)
11515 480 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
11516 : else
11517 1496 : t = build_nop (TREE_TYPE (t), t);
11518 147399 : TREE_CONSTANT (t) = false;
11519 147399 : return t;
11520 : }
11521 :
11522 : /* If we have a successful constant evaluation, now check whether there is
11523 : a failed or non-constant contract that would invalidate this. */
11524 :
11525 : static bool
11526 30 : check_for_failed_contracts (const constexpr_ctx *ctx)
11527 : {
11528 30 : constexpr_global_ctx *const global_ctx = ctx->global;
11529 30 : if (!flag_contracts || !global_ctx->contract_statement)
11530 : return false;
11531 :
11532 30 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
11533 30 : tree cond = CONTRACT_CONDITION (global_ctx->contract_statement);
11534 30 : loc = cp_expr_loc_or_loc (cond, loc);
11535 30 : enum diagnostics::kind kind;
11536 30 : bool error = false;
11537 : /* [intro.compliance.general]/2.3.4. */
11538 : /* [basic.contract.eval]/8. */
11539 30 : if (ctx->manifestly_const_eval != mce_true)
11540 : {
11541 : /* When !MCE, silently return not constant. */
11542 : return true;
11543 : }
11544 30 : else if (contract_terminating_p (global_ctx->contract_statement))
11545 : {
11546 : kind = diagnostics::kind::error;
11547 : error = true;
11548 : }
11549 : else
11550 6 : kind = diagnostics::kind::warning;
11551 :
11552 30 : auto_diagnostic_group d;
11553 :
11554 : /* [basic.contract.eval]/7.3 */
11555 30 : if (global_ctx->contract_condition_non_const)
11556 : {
11557 6 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
11558 6 : if (error)
11559 6 : cxx_constant_value (cond);
11560 : return error;
11561 : }
11562 :
11563 : /* Otherwise, the evaluation was const, but determined to be false. */
11564 24 : emit_diagnostic (kind, loc, 0,
11565 : "contract predicate is false in constant expression");
11566 : /* Workaround: Avoid redundant "in expansion of macro". */
11567 24 : loc = linemap_resolve_location (line_table, loc,
11568 : LRK_SPELLING_LOCATION, NULL);
11569 24 : diagnose_failing_condition (cond, loc, /*show_expr*/true, ctx);
11570 24 : return error;
11571 30 : }
11572 :
11573 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11574 : STRICT has the same sense as for constant_value_1: true if we only allow
11575 : conforming C++ constant expressions, or false if we want a constant value
11576 : even if it doesn't conform.
11577 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11578 : per P0595 even when ALLOW_NON_CONSTANT is true.
11579 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
11580 : OBJECT must be non-NULL in that case. */
11581 :
11582 : static tree
11583 573863461 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
11584 : bool strict = true,
11585 : mce_value manifestly_const_eval = mce_unknown,
11586 : bool constexpr_dtor = false,
11587 : tree object = NULL_TREE)
11588 : {
11589 573863461 : auto_timevar time (TV_CONSTEXPR);
11590 :
11591 573863461 : bool non_constant_p = false;
11592 573863461 : bool overflow_p = false;
11593 :
11594 573863461 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
11595 : {
11596 0 : gcc_checking_assert (allow_non_constant);
11597 : return t;
11598 : }
11599 :
11600 573863461 : constexpr_global_ctx global_ctx;
11601 573863461 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
11602 : allow_non_constant, strict,
11603 573863461 : !allow_non_constant ? mce_true : manifestly_const_eval };
11604 :
11605 : /* Turn off -frounding-math for manifestly constant evaluation. */
11606 573863461 : warning_sentinel rm (flag_rounding_math,
11607 573863461 : ctx.manifestly_const_eval == mce_true);
11608 573863461 : tree type = (object
11609 573863461 : ? cv_unqualified (TREE_TYPE (object))
11610 507836760 : : initialized_type (t));
11611 573863461 : tree r = t;
11612 573863461 : bool is_consteval = false;
11613 573863461 : if (VOID_TYPE_P (type))
11614 : {
11615 9923400 : if (!constexpr_dtor)
11616 : {
11617 9923400 : if (cxx_dialect < cxx20)
11618 : return t;
11619 : /* We could have a COMPOUND_EXPR here coming from
11620 : keep_unused_object_arg. */
11621 9912346 : tree x = extract_call_expr (t);
11622 9912346 : if (x == NULL_TREE || x == error_mark_node)
11623 : return t;
11624 : /* Calls to immediate functions returning void need to be
11625 : evaluated. */
11626 9912304 : tree fndecl = cp_get_callee_fndecl_nofold (x);
11627 19824608 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
11628 : return t;
11629 : else
11630 646 : is_consteval = true;
11631 646 : tree lam;
11632 646 : if (manifestly_const_eval == mce_true
11633 1186 : && LAMBDA_FUNCTION_P (fndecl)
11634 534 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
11635 1180 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
11636 527 : global_ctx.consteval_block = fndecl;
11637 : }
11638 : }
11639 563940061 : else if (cxx_dialect >= cxx20
11640 555054963 : && (TREE_CODE (t) == CALL_EXPR
11641 476983889 : || TREE_CODE (t) == AGGR_INIT_EXPR
11642 471931912 : || TREE_CODE (t) == TARGET_EXPR))
11643 : {
11644 88668894 : tree x = t;
11645 88668894 : if (TREE_CODE (x) == TARGET_EXPR)
11646 5545843 : x = TARGET_EXPR_INITIAL (x);
11647 88668894 : tree fndecl = cp_get_callee_fndecl_nofold (x);
11648 174454855 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
11649 : is_consteval = true;
11650 : /* Don't try to evaluate a std::vector constructor taking an integer, it
11651 : will fail in the 'if (heap_var)' block below after doing all the work
11652 : (c++/113835). This will need adjustment if P3554 is accepted. Note
11653 : that evaluation of e.g. the vector default constructor can succeed, so
11654 : we don't shortcut all vector constructors. */
11655 171571922 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
11656 11388678 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
11657 88693481 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
11658 : return t;
11659 : }
11660 563938650 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
11661 : {
11662 : /* In C++14 an NSDMI can participate in aggregate initialization,
11663 : and can refer to the address of the object being initialized, so
11664 : we need to pass in the relevant VAR_DECL if we want to do the
11665 : evaluation in a single pass. The evaluation will dynamically
11666 : update ctx.values for the VAR_DECL. We use the same strategy
11667 : for C++11 constexpr constructors that refer to the object being
11668 : initialized. */
11669 49426145 : if (constexpr_dtor)
11670 : {
11671 202 : gcc_assert (object && VAR_P (object));
11672 202 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
11673 202 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
11674 202 : if (error_operand_p (DECL_INITIAL (object)))
11675 : return t;
11676 185 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
11677 185 : TREE_READONLY (ctx.ctor) = false;
11678 : /* Temporarily force decl_really_constant_value to return false
11679 : for it, we want to use ctx.ctor for the current value instead. */
11680 185 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
11681 : }
11682 : else
11683 : {
11684 49425943 : ctx.ctor = build_constructor (type, NULL);
11685 49425943 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
11686 : }
11687 49426128 : if (!object)
11688 : {
11689 21958417 : if (TREE_CODE (t) == CALL_EXPR)
11690 : {
11691 : /* If T is calling a constructor to initialize an object, reframe
11692 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
11693 : from outside the constant evaluation, which will fail even if
11694 : the value is actually constant (is_constant_evaluated3.C). */
11695 9889227 : tree fn = cp_get_callee_fndecl_nofold (t);
11696 19778246 : if (fn && DECL_CONSTRUCTOR_P (fn))
11697 : {
11698 4053606 : object = CALL_EXPR_ARG (t, 0);
11699 4053606 : object = build_fold_indirect_ref (object);
11700 4053606 : r = build_aggr_init_expr (type, r);
11701 : }
11702 : }
11703 12069190 : else if (TREE_CODE (t) == TARGET_EXPR)
11704 5442541 : object = TARGET_EXPR_SLOT (t);
11705 6626649 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
11706 698491 : object = AGGR_INIT_EXPR_SLOT (t);
11707 : }
11708 49426128 : ctx.object = object;
11709 49426128 : if (object)
11710 37662349 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
11711 : (type, TREE_TYPE (object)));
11712 37662349 : if (object && DECL_P (object))
11713 31571420 : global_ctx.put_value (object, ctx.ctor);
11714 49426128 : if (TREE_CODE (r) == TARGET_EXPR)
11715 : /* Avoid creating another CONSTRUCTOR when we expand the
11716 : TARGET_EXPR. */
11717 5582620 : r = TARGET_EXPR_INITIAL (r);
11718 : }
11719 :
11720 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
11721 : constexpr evaluation to avoid unnecessary template instantiation, and is
11722 : always done with mce_unknown. But due to gaps in the restriction logic
11723 : we may still end up taking an evaluation path that in turn requires
11724 : manifestly constant evaluation, and such evaluation must not be
11725 : restricted since it likely has semantic consequences.
11726 : TODO: Remove/replace the mechanism in GCC 17. */
11727 563938633 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
11728 563938633 : if (ctx.manifestly_const_eval == mce_true)
11729 287443434 : uid_sensitive_constexpr_evaluation_value = false;
11730 :
11731 563938633 : auto_vec<tree, 16> cleanups;
11732 563938633 : global_ctx.cleanups = &cleanups;
11733 :
11734 563938633 : if (manifestly_const_eval == mce_true)
11735 287443431 : instantiate_constexpr_fns (r);
11736 563938633 : tree jmp_target = NULL_TREE;
11737 563938633 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
11738 : &non_constant_p, &overflow_p,
11739 : &jmp_target);
11740 563937532 : if (throws (&jmp_target) && !non_constant_p)
11741 : {
11742 1598 : if (!ctx.quiet)
11743 396 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11744 1598 : non_constant_p = true;
11745 1598 : jmp_target = NULL_TREE;
11746 1598 : r = t;
11747 : }
11748 563934336 : else if (!non_constant_p && jmp_target)
11749 : {
11750 24 : non_constant_p = true;
11751 24 : if (!ctx.quiet)
11752 : {
11753 3 : if (breaks (&jmp_target))
11754 3 : error ("%<break%> outside of a loop or %<switch%>");
11755 0 : else if (continues (&jmp_target))
11756 0 : error ("%<continue%> outside of a loop");
11757 0 : else if (returns (&jmp_target))
11758 0 : error ("%<return%> in a statement expression");
11759 : else
11760 0 : gcc_unreachable ();
11761 : }
11762 24 : r = t;
11763 : }
11764 :
11765 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
11766 : of statements, and the result ought to be stored in ctx.ctor. */
11767 563935934 : if (r == void_node && !constexpr_dtor && ctx.ctor)
11768 0 : r = ctx.ctor;
11769 :
11770 563935934 : unsigned int i;
11771 563935934 : tree cleanup;
11772 563935934 : jmp_target = NULL_TREE;
11773 : /* Evaluate the cleanups. */
11774 1127970096 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
11775 98228 : if (cleanup == NULL_TREE)
11776 : /* NULL_TREE cleanup is a marker that before it is
11777 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
11778 23 : --i;
11779 : else
11780 98205 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
11781 : &non_constant_p, &overflow_p,
11782 : &jmp_target);
11783 563935934 : if (throws (&jmp_target) && !non_constant_p)
11784 : {
11785 0 : if (!ctx.quiet)
11786 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11787 0 : non_constant_p = true;
11788 0 : r = t;
11789 : }
11790 :
11791 : /* Mutable logic is a bit tricky: we want to allow initialization of
11792 : constexpr variables with mutable members, but we can't copy those
11793 : members to another constexpr variable. */
11794 563935934 : if (!non_constant_p
11795 414086416 : && TREE_CODE (r) == CONSTRUCTOR
11796 580962176 : && CONSTRUCTOR_MUTABLE_POISON (r))
11797 : {
11798 93 : if (!allow_non_constant)
11799 6 : error ("%qE is not a constant expression because it refers to "
11800 : "mutable subobjects of %qT", t, type);
11801 93 : non_constant_p = true;
11802 : }
11803 :
11804 414086323 : if (!non_constant_p && cxx_dialect >= cxx20
11805 978022257 : && !global_ctx.heap_vars.is_empty ())
11806 : {
11807 122145 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11808 : NULL);
11809 122145 : unsigned int i;
11810 122145 : if (heap_var)
11811 : {
11812 113099 : if (!allow_non_constant && !non_constant_p)
11813 : {
11814 17 : auto_diagnostic_group d;
11815 17 : if (DECL_LANG_SPECIFIC (heap_var))
11816 4 : error ("%qE is not a constant expression because it refers to "
11817 : "exception object allocated with "
11818 : "%<__cxa_allocate_exception%>", t);
11819 : else
11820 13 : error ("%qE is not a constant expression because it refers to "
11821 : "a result of %<operator new%>", t);
11822 17 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11823 17 : }
11824 113099 : r = t;
11825 113099 : non_constant_p = true;
11826 : }
11827 317576 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11828 : {
11829 195431 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11830 : {
11831 113671 : if (!allow_non_constant && !non_constant_p)
11832 : {
11833 16 : auto_diagnostic_group d;
11834 16 : error ("%qE is not a constant expression because allocated "
11835 : "storage has not been deallocated", t);
11836 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11837 16 : }
11838 113671 : r = t;
11839 113671 : non_constant_p = true;
11840 : }
11841 : }
11842 : }
11843 :
11844 : /* Check that immediate invocation does not return an expression referencing
11845 : any immediate function decls. */
11846 563935934 : if (!non_constant_p && cxx_dialect >= cxx20)
11847 816563962 : if (tree immediate_fndecl
11848 408281981 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11849 : NULL))
11850 : {
11851 6741 : if (!allow_non_constant && !non_constant_p)
11852 : {
11853 78 : if (is_consteval)
11854 36 : error_at (cp_expr_loc_or_input_loc (t),
11855 : "immediate evaluation returns address of immediate "
11856 : "function %qD", immediate_fndecl);
11857 : else
11858 64 : error_at (cp_expr_loc_or_input_loc (t),
11859 : "constant evaluation returns address of immediate "
11860 : "function %qD", immediate_fndecl);
11861 : }
11862 6741 : r = t;
11863 6741 : non_constant_p = true;
11864 : }
11865 :
11866 563935934 : if (!non_constant_p && !constexpr_dtor)
11867 413966236 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11868 :
11869 : /* After verify_constant because reduced_constant_expression_p can unset
11870 : CONSTRUCTOR_NO_CLEARING. */
11871 563935934 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11872 : {
11873 70386 : if (!allow_non_constant)
11874 45 : error ("%qE is not a constant expression because it refers to "
11875 : "an incompletely initialized variable", t);
11876 70386 : TREE_CONSTANT (r) = false;
11877 70386 : non_constant_p = true;
11878 : }
11879 :
11880 563935934 : if (non_constant_p)
11881 : /* If we saw something bad, go back to our argument. The wrapping below is
11882 : only for the cases of TREE_CONSTANT argument or overflow. */
11883 152393887 : r = t;
11884 :
11885 563935934 : if (!non_constant_p && overflow_p)
11886 215 : non_constant_p = true;
11887 :
11888 : /* If allow_non_constant, we delay diagnosing contracts until we know
11889 : that the expression is otherwise constant.
11890 :
11891 : Various constexpr contract violation scenarios:
11892 : * MCE, non-SFINAE, terminating: diagnose and mark non-constant
11893 : during evaluation
11894 : * MCE, non-SFINAE, non-terminating: diagnose during evaluation
11895 : * MCE, SFINAE, terminating: diagnose in repeated eval, return
11896 : error_mark_node for SFINAE
11897 : * MCE, SFINAE, non-terminating: diagnose in repeated eval
11898 : * non-MCE: diagnose in repeated eval, set non-constant so run-time
11899 : violation handler also sees the violation
11900 :
11901 : For MCE+terminating, the necessary semantics are just a hard error; what
11902 : happens after that is error-recovery.
11903 :
11904 : In the non-SFINAE (!allow_non_constant) case I think it's clearly
11905 : preferable to stop evaluation after the violation, which we do by marking
11906 : the evaluation non-constant. We could work harder to return
11907 : error_mark_node instead like the SFINAE case below, but that seems
11908 : unnecessary.
11909 :
11910 : The allow_non_constant case is trickier, because that expresses both
11911 : actual SFINAE and cases like finish_static_assert where we do one trial
11912 : evaluation to see if it's constant, and then another to get the actual
11913 : error if needed. So if we set non_constant_p like for the
11914 : !allow_non_constant case, we get SFINAE consistent with
11915 : e.g. _GLIBCXX_ASSERT_FAIL, but we get redundant errors with static_assert.
11916 : We can finesse it a bit more by returning error_mark_node, which
11917 : finish_static_assert distinguishes from non-constant.
11918 :
11919 : ??? cp_fold_immediate_r should change to follow the finish_static_assert
11920 : pattern (using maybe_constant_value instead of cxx_constant_value for the
11921 : trial evaluation) once the consteval-only values overhaul goes in. */
11922 :
11923 563935934 : if (!non_constant_p && allow_non_constant
11924 275212504 : && global_ctx.contract_statement)
11925 : {
11926 : /* Repeat evaluation to get cx_call_context for the diagnostic. */
11927 21 : cxx_eval_outermost_constant_expr (t, /*allow*/false, strict,
11928 : manifestly_const_eval,
11929 : constexpr_dtor, object);
11930 21 : if (ctx.manifestly_const_eval != mce_true)
11931 : /* Treat it as non-constant so the violation is also detectable at
11932 : runtime (if the compile succeeds because we're observing). */
11933 3 : non_constant_p = true;
11934 18 : else if (contract_terminating_p (global_ctx.contract_statement))
11935 : /* Substitution failure (see discussion above). */
11936 12 : return error_mark_node;
11937 : }
11938 :
11939 : /* Unshare the result. */
11940 563935922 : bool should_unshare = true;
11941 563935922 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
11942 1626661 : && TARGET_EXPR_INITIAL (t) == r))
11943 : should_unshare = false;
11944 :
11945 563935922 : if (non_constant_p && !allow_non_constant)
11946 4186 : return error_mark_node;
11947 563931736 : else if (non_constant_p && TREE_CONSTANT (r))
11948 142897 : r = mark_non_constant (r);
11949 563788839 : else if (non_constant_p)
11950 : return t;
11951 :
11952 411684714 : if (constexpr_dtor)
11953 : {
11954 167 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
11955 167 : return r;
11956 : }
11957 :
11958 : /* Check we are not trying to return the wrong type. */
11959 411684547 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
11960 : {
11961 : /* If so, this is not a constant expression. */
11962 151 : if (!allow_non_constant)
11963 0 : error ("%qE is not a constant expression because it initializes "
11964 0 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
11965 : return t;
11966 : }
11967 :
11968 411684396 : if (should_unshare)
11969 231173236 : r = unshare_expr (r);
11970 :
11971 411684396 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
11972 : {
11973 16024373 : r = adjust_temp_type (type, r);
11974 16024373 : if (TREE_CODE (t) == TARGET_EXPR
11975 16024373 : && TARGET_EXPR_INITIAL (t) == r)
11976 : return t;
11977 14942306 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
11978 2481082 : || TREE_CODE (t) == AGGR_INIT_EXPR)
11979 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
11980 826383 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
11981 778 : r = get_target_expr (r);
11982 825605 : else if (processing_template_decl)
11983 : /* Don't insert TARGET_EXPR in template trees. */
11984 : return r;
11985 : else
11986 : {
11987 825602 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
11988 825602 : TREE_CONSTANT (r) = true;
11989 : }
11990 : }
11991 :
11992 410602326 : if (TREE_CODE (t) == TARGET_EXPR
11993 544594 : && TREE_CODE (r) == TARGET_EXPR)
11994 : {
11995 : /* Preserve this flag for potential_constant_expression, and the others
11996 : for good measure. */
11997 544482 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
11998 544482 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
11999 544482 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
12000 544482 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
12001 : }
12002 :
12003 : /* Remember the original location if that wouldn't need a wrapper. */
12004 410602326 : if (location_t loc = EXPR_LOCATION (t))
12005 187983614 : protected_set_expr_location (r, loc);
12006 :
12007 410602326 : return r;
12008 573860762 : }
12009 :
12010 : /* If T represents a constant expression returns its reduced value.
12011 : Otherwise return error_mark_node. */
12012 :
12013 : tree
12014 157551865 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
12015 : tsubst_flags_t complain /* = tf_error */)
12016 : {
12017 157551865 : bool sfinae = !(complain & tf_error);
12018 157551865 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
12019 157551865 : if (sfinae && !TREE_CONSTANT (r))
12020 2976 : r = error_mark_node;
12021 157551865 : return r;
12022 : }
12023 :
12024 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
12025 : of constexpr variables. The actual initializer of DECL is not modified. */
12026 :
12027 : void
12028 202 : cxx_constant_dtor (tree t, tree decl)
12029 : {
12030 202 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
12031 202 : }
12032 :
12033 : /* Helper routine for fold_simple function. Either return simplified
12034 : expression T, otherwise NULL_TREE.
12035 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
12036 : even if we are within template-declaration. So be careful on call, as in
12037 : such case types can be undefined. */
12038 :
12039 : static tree
12040 169399680 : fold_simple_1 (tree t)
12041 : {
12042 169399680 : tree op1;
12043 169399680 : enum tree_code code = TREE_CODE (t);
12044 :
12045 169399680 : switch (code)
12046 : {
12047 : case INTEGER_CST:
12048 : case REAL_CST:
12049 : case VECTOR_CST:
12050 : case FIXED_CST:
12051 : case COMPLEX_CST:
12052 : return t;
12053 :
12054 1801176 : case SIZEOF_EXPR:
12055 1801176 : return fold_sizeof_expr (t);
12056 :
12057 40149908 : case ABS_EXPR:
12058 40149908 : case ABSU_EXPR:
12059 40149908 : case CONJ_EXPR:
12060 40149908 : case REALPART_EXPR:
12061 40149908 : case IMAGPART_EXPR:
12062 40149908 : case NEGATE_EXPR:
12063 40149908 : case BIT_NOT_EXPR:
12064 40149908 : case TRUTH_NOT_EXPR:
12065 40149908 : case VIEW_CONVERT_EXPR:
12066 40149908 : CASE_CONVERT:
12067 40149908 : case FLOAT_EXPR:
12068 40149908 : case FIX_TRUNC_EXPR:
12069 40149908 : case FIXED_CONVERT_EXPR:
12070 40149908 : case ADDR_SPACE_CONVERT_EXPR:
12071 :
12072 40149908 : op1 = TREE_OPERAND (t, 0);
12073 :
12074 40149908 : t = const_unop (code, TREE_TYPE (t), op1);
12075 40149908 : if (!t)
12076 : return NULL_TREE;
12077 :
12078 6220362 : if (CONVERT_EXPR_CODE_P (code)
12079 6220362 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
12080 0 : TREE_OVERFLOW (t) = false;
12081 : return t;
12082 :
12083 : default:
12084 : return NULL_TREE;
12085 : }
12086 : }
12087 :
12088 : /* If T is a simple constant expression, returns its simplified value.
12089 : Otherwise returns T. In contrast to maybe_constant_value we
12090 : simplify only few operations on constant-expressions, and we don't
12091 : try to simplify constexpressions. */
12092 :
12093 : tree
12094 170200140 : fold_simple (tree t)
12095 : {
12096 170200140 : if (processing_template_decl)
12097 : return t;
12098 :
12099 169399680 : tree r = fold_simple_1 (t);
12100 169399680 : if (r)
12101 119138698 : return r;
12102 :
12103 : return t;
12104 : }
12105 :
12106 : /* Try folding the expression T to a simple constant.
12107 : Returns that constant, otherwise returns T. */
12108 :
12109 : tree
12110 1650992 : fold_to_constant (tree t)
12111 : {
12112 1650992 : if (processing_template_decl)
12113 : return t;
12114 :
12115 1556121 : tree r = fold (t);
12116 1556121 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
12117 : return r;
12118 : else
12119 139888 : return t;
12120 : }
12121 :
12122 : /* If T is a constant expression, returns its reduced value.
12123 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
12124 : Otherwise, returns a version of T without TREE_CONSTANT.
12125 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
12126 : as per P0595. */
12127 :
12128 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
12129 :
12130 : tree
12131 862878193 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
12132 : mce_value manifestly_const_eval /* = mce_unknown */)
12133 : {
12134 862878193 : tree orig_t = t;
12135 862878193 : tree r;
12136 :
12137 862878193 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
12138 : {
12139 : /* Look up each operand in the cv_cache first to see if we've already
12140 : reduced it, and reuse that result to avoid quadratic behavior if
12141 : we're called when building up a large expression. */
12142 250116451 : int n = cp_tree_operand_length (t);
12143 250116451 : tree *ops = XALLOCAVEC (tree, n);
12144 250116451 : bool rebuild = false;
12145 700044284 : for (int i = 0; i < n; ++i)
12146 : {
12147 449927833 : ops[i] = TREE_OPERAND (t, i);
12148 930151319 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
12149 31015712 : if (*cached != ops[i])
12150 : {
12151 9644653 : ops[i] = *cached;
12152 9644653 : rebuild = true;
12153 : }
12154 : }
12155 250116451 : if (rebuild)
12156 : {
12157 8096729 : t = copy_node (t);
12158 26380598 : for (int i = 0; i < n; ++i)
12159 18283869 : TREE_OPERAND (t, i) = ops[i];
12160 : }
12161 : }
12162 :
12163 862878193 : if (!is_nondependent_constant_expression (t))
12164 : {
12165 0 : if (TREE_OVERFLOW_P (t)
12166 127700562 : || (!processing_template_decl && TREE_CONSTANT (t)))
12167 4502 : t = mark_non_constant (t);
12168 127700562 : return t;
12169 : }
12170 735177631 : else if (CONSTANT_CLASS_P (t))
12171 : /* No caching or evaluation needed. */
12172 : return t;
12173 :
12174 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
12175 : but at least try folding it to a simple constant. */
12176 336193618 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
12177 501286 : return fold_to_constant (t);
12178 :
12179 319291276 : if (manifestly_const_eval != mce_unknown)
12180 : /* TODO: Extend the cache to be mce_value aware. And if we have a
12181 : previously cached mce_unknown result that's TREE_CONSTANT, it means
12182 : the reduced value is independent of mce_value and so we should
12183 : be able to reuse it in the mce_true/false case. */
12184 171884789 : return cxx_eval_outermost_constant_expr (t, true, true,
12185 171882090 : manifestly_const_eval, false, decl);
12186 :
12187 163807543 : if (cv_cache == NULL)
12188 148548 : cv_cache = hash_map<tree, tree>::create_ggc (101);
12189 163807543 : if (tree *cached = cv_cache->get (t))
12190 : {
12191 13307852 : r = *cached;
12192 13307852 : if (r != t)
12193 : {
12194 : /* Clear processing_template_decl for sake of break_out_target_exprs;
12195 : entries in the cv_cache are non-templated. */
12196 4486567 : processing_template_decl_sentinel ptds;
12197 :
12198 4486567 : r = break_out_target_exprs (r, /*clear_loc*/true);
12199 4486567 : protected_set_expr_location (r, EXPR_LOCATION (t));
12200 4486567 : }
12201 13307852 : return r;
12202 : }
12203 :
12204 150499691 : uid_sensitive_constexpr_evaluation_checker c;
12205 150499691 : r = cxx_eval_outermost_constant_expr (t, true, true,
12206 : manifestly_const_eval, false, decl);
12207 150499691 : gcc_checking_assert (r == t
12208 : || CONVERT_EXPR_P (t)
12209 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
12210 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
12211 : || !cp_tree_equal (r, t));
12212 150499691 : if (!c.evaluation_restricted_p ())
12213 150259368 : cv_cache->put (orig_t, r);
12214 : return r;
12215 : }
12216 :
12217 : /* Dispose of the whole CV_CACHE. */
12218 :
12219 : static void
12220 40626104 : clear_cv_cache (void)
12221 : {
12222 40626104 : if (cv_cache != NULL)
12223 40306448 : cv_cache->empty ();
12224 40626104 : }
12225 :
12226 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
12227 :
12228 : void
12229 40626104 : clear_cv_and_fold_caches ()
12230 : {
12231 40626104 : clear_cv_cache ();
12232 40626104 : clear_fold_cache ();
12233 40626104 : }
12234 :
12235 : /* Internal function handling expressions in templates for
12236 : fold_non_dependent_expr and fold_non_dependent_init.
12237 :
12238 : If we're in a template, but T isn't value dependent, simplify
12239 : it. We're supposed to treat:
12240 :
12241 : template <typename T> void f(T[1 + 1]);
12242 : template <typename T> void f(T[2]);
12243 :
12244 : as two declarations of the same function, for example. */
12245 :
12246 : static tree
12247 31446805 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
12248 : bool manifestly_const_eval,
12249 : tree object)
12250 : {
12251 31446805 : gcc_assert (processing_template_decl);
12252 :
12253 31446805 : if (is_nondependent_constant_expression (t))
12254 : {
12255 17526957 : processing_template_decl_sentinel s;
12256 17526957 : t = instantiate_non_dependent_expr_internal (t, complain);
12257 :
12258 17526957 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
12259 : {
12260 0 : if (TREE_OVERFLOW_P (t))
12261 : {
12262 0 : t = build_nop (TREE_TYPE (t), t);
12263 0 : TREE_CONSTANT (t) = false;
12264 : }
12265 : return t;
12266 : }
12267 17526957 : else if (CONSTANT_CLASS_P (t))
12268 : /* No evaluation needed. */
12269 : return t;
12270 :
12271 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
12272 : but at least try folding it to a simple constant. */
12273 4625643 : if (cp_unevaluated_operand && !manifestly_const_eval)
12274 89 : return fold_to_constant (t);
12275 :
12276 4625554 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
12277 : mce_value (manifestly_const_eval),
12278 : false, object);
12279 : /* cp_tree_equal looks through NOPs, so allow them. */
12280 4625554 : gcc_checking_assert (r == t
12281 : || CONVERT_EXPR_P (t)
12282 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
12283 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
12284 : || !cp_tree_equal (r, t));
12285 : return r;
12286 17526957 : }
12287 13919848 : else if (TREE_OVERFLOW_P (t))
12288 : {
12289 0 : t = build_nop (TREE_TYPE (t), t);
12290 0 : TREE_CONSTANT (t) = false;
12291 : }
12292 :
12293 : return t;
12294 : }
12295 :
12296 : /* Like maybe_constant_value but first fully instantiate the argument.
12297 :
12298 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
12299 : followed by maybe_constant_value but is more efficient,
12300 : because it calls instantiation_dependent_expression_p and
12301 : potential_constant_expression at most once.
12302 : The manifestly_const_eval argument is passed to maybe_constant_value.
12303 :
12304 : Callers should generally pass their active complain, or if they are in a
12305 : non-template, diagnosing context, they can use the default of
12306 : tf_warning_or_error. Callers that might be within a template context, don't
12307 : have a complain parameter, and aren't going to remember the result for long
12308 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
12309 : appropriately. */
12310 :
12311 : tree
12312 264712666 : fold_non_dependent_expr (tree t,
12313 : tsubst_flags_t complain /* = tf_warning_or_error */,
12314 : bool manifestly_const_eval /* = false */,
12315 : tree object /* = NULL_TREE */)
12316 : {
12317 264712666 : if (t == NULL_TREE)
12318 : return NULL_TREE;
12319 :
12320 263603620 : if (processing_template_decl)
12321 30322475 : return fold_non_dependent_expr_template (t, complain,
12322 30322475 : manifestly_const_eval, object);
12323 :
12324 233281145 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
12325 : }
12326 :
12327 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
12328 : return the original expression. */
12329 :
12330 : tree
12331 2358949 : maybe_fold_non_dependent_expr (tree expr,
12332 : tsubst_flags_t complain/*=tf_warning_or_error*/)
12333 : {
12334 2358949 : tree t = fold_non_dependent_expr (expr, complain);
12335 2358949 : if (t && TREE_CONSTANT (t))
12336 1124111 : return t;
12337 :
12338 : return expr;
12339 : }
12340 :
12341 : /* Like maybe_constant_init but first fully instantiate the argument. */
12342 :
12343 : tree
12344 62796567 : fold_non_dependent_init (tree t,
12345 : tsubst_flags_t complain /*=tf_warning_or_error*/,
12346 : bool manifestly_const_eval /*=false*/,
12347 : tree object /* = NULL_TREE */)
12348 : {
12349 62796567 : if (t == NULL_TREE)
12350 : return NULL_TREE;
12351 :
12352 62796567 : if (processing_template_decl)
12353 : {
12354 1124330 : t = fold_non_dependent_expr_template (t, complain,
12355 : manifestly_const_eval, object);
12356 : /* maybe_constant_init does this stripping, so do it here too. */
12357 1124330 : if (TREE_CODE (t) == TARGET_EXPR)
12358 : {
12359 2107 : tree init = TARGET_EXPR_INITIAL (t);
12360 2107 : if (TREE_CODE (init) == CONSTRUCTOR)
12361 62796567 : t = init;
12362 : }
12363 : return t;
12364 : }
12365 :
12366 61672237 : return maybe_constant_init (t, object, manifestly_const_eval);
12367 : }
12368 :
12369 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
12370 : than wrapped in a TARGET_EXPR.
12371 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
12372 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
12373 : per P0595 even when ALLOW_NON_CONSTANT is true. */
12374 :
12375 : static tree
12376 174192808 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
12377 : mce_value manifestly_const_eval)
12378 : {
12379 174192808 : if (!t)
12380 : return t;
12381 174192808 : if (TREE_CODE (t) == EXPR_STMT)
12382 270 : t = TREE_OPERAND (t, 0);
12383 174192808 : if (TREE_CODE (t) == CONVERT_EXPR
12384 174192808 : && VOID_TYPE_P (TREE_TYPE (t)))
12385 2864 : t = TREE_OPERAND (t, 0);
12386 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
12387 : DECL and losing that information would cause mischief later. */
12388 174192808 : if (TREE_CODE (t) == INIT_EXPR
12389 174192808 : && (!decl
12390 2612 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
12391 2612 : TREE_TYPE (t))))
12392 1704 : t = TREE_OPERAND (t, 1);
12393 174192808 : if (TREE_CODE (t) == TARGET_EXPR)
12394 7919581 : t = TARGET_EXPR_INITIAL (t);
12395 174192808 : if (!is_nondependent_static_init_expression (t))
12396 : /* Don't try to evaluate it. */;
12397 140866762 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
12398 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
12399 : else
12400 : {
12401 : /* [basic.start.static] allows constant-initialization of variables with
12402 : static or thread storage duration even if it isn't required, but we
12403 : shouldn't bend the rules the same way for automatic variables.
12404 :
12405 : But still enforce the requirements of constexpr/constinit.
12406 : [dcl.constinit] "If a variable declared with the constinit specifier
12407 : has dynamic initialization, the program is ill-formed, even if the
12408 : implementation would perform that initialization as a static
12409 : initialization." */
12410 43006062 : bool is_static = (decl && DECL_P (decl)
12411 102766256 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
12412 : bool strict = (!is_static
12413 : || (decl && DECL_P (decl)
12414 3399780 : && (DECL_DECLARED_CONSTEXPR_P (decl)
12415 978778 : || DECL_DECLARED_CONSTINIT_P (decl))));
12416 : if (is_static)
12417 : manifestly_const_eval = mce_true;
12418 :
12419 65475625 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
12420 88479 : return fold_to_constant (t);
12421 :
12422 65387146 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
12423 : manifestly_const_eval,
12424 : false, decl);
12425 : }
12426 174104329 : if (TREE_CODE (t) == TARGET_EXPR)
12427 : {
12428 281337 : tree init = TARGET_EXPR_INITIAL (t);
12429 281337 : if (TREE_CODE (init) == CONSTRUCTOR)
12430 174192808 : t = init;
12431 : }
12432 : return t;
12433 : }
12434 :
12435 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
12436 :
12437 : tree
12438 95526948 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
12439 : {
12440 95526948 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
12441 : }
12442 :
12443 : tree
12444 78663613 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
12445 : {
12446 78663613 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
12447 : }
12448 :
12449 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
12450 :
12451 : tree
12452 2247 : cxx_constant_init (tree t, tree decl)
12453 : {
12454 2247 : return maybe_constant_init_1 (t, decl, false, mce_true);
12455 : }
12456 :
12457 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
12458 :
12459 : static bool
12460 229550821 : callee_might_throw (tree t)
12461 : {
12462 229550821 : if (cxx_dialect < cxx26 || !flag_exceptions)
12463 : return false;
12464 37179101 : tree callee = cp_get_callee (t);
12465 37179101 : if (callee == NULL_TREE)
12466 : return false;
12467 37121355 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
12468 37121355 : return (!flag_enforce_eh_specs
12469 37121355 : || type_dependent_expression_p (callee)
12470 34044111 : || !POINTER_TYPE_P (TREE_TYPE (callee))
12471 68943552 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
12472 14924977 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
12473 : }
12474 :
12475 : #if 0
12476 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
12477 : /* Return true if the object referred to by REF has automatic or thread
12478 : local storage. */
12479 :
12480 : enum { ck_ok, ck_bad, ck_unknown };
12481 : static int
12482 : check_automatic_or_tls (tree ref)
12483 : {
12484 : machine_mode mode;
12485 : poly_int64 bitsize, bitpos;
12486 : tree offset;
12487 : int volatilep = 0, unsignedp = 0;
12488 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
12489 : &mode, &unsignedp, &volatilep, false);
12490 : duration_kind dk;
12491 :
12492 : /* If there isn't a decl in the middle, we don't know the linkage here,
12493 : and this isn't a constant expression anyway. */
12494 : if (!DECL_P (decl))
12495 : return ck_unknown;
12496 : dk = decl_storage_duration (decl);
12497 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
12498 : }
12499 : #endif
12500 :
12501 : /* Data structure for passing data from potential_constant_expression_1
12502 : to check_for_return_continue via cp_walk_tree. */
12503 : struct check_for_return_continue_data {
12504 : hash_set<tree> *pset;
12505 : tree continue_stmt;
12506 : tree break_stmt;
12507 : bool could_throw;
12508 : };
12509 :
12510 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
12511 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
12512 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
12513 : For C++26 also note presence of possibly throwing calls. */
12514 : static tree
12515 51018421 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
12516 : {
12517 51018421 : tree t = *tp, s, b;
12518 51018421 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
12519 51018421 : switch (TREE_CODE (t))
12520 : {
12521 : case RETURN_EXPR:
12522 : return t;
12523 :
12524 28 : case CONTINUE_STMT:
12525 28 : if (d->continue_stmt == NULL_TREE)
12526 28 : d->continue_stmt = t;
12527 : break;
12528 :
12529 3276 : case BREAK_STMT:
12530 3276 : if (d->break_stmt == NULL_TREE)
12531 1353 : d->break_stmt = t;
12532 : break;
12533 :
12534 : #define RECUR(x) \
12535 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
12536 : d->pset)) \
12537 : return r
12538 :
12539 : /* For loops, walk subtrees manually, so that continue stmts found
12540 : inside of the bodies of the loops are ignored. */
12541 42099 : case DO_STMT:
12542 42099 : *walk_subtrees = 0;
12543 42099 : RECUR (DO_COND (t));
12544 42099 : s = d->continue_stmt;
12545 42099 : b = d->break_stmt;
12546 42099 : RECUR (DO_BODY (t));
12547 42099 : d->continue_stmt = s;
12548 42099 : d->break_stmt = b;
12549 42099 : break;
12550 :
12551 127 : case WHILE_STMT:
12552 127 : *walk_subtrees = 0;
12553 127 : RECUR (WHILE_COND_PREP (t));
12554 127 : RECUR (WHILE_COND (t));
12555 127 : s = d->continue_stmt;
12556 127 : b = d->break_stmt;
12557 127 : RECUR (WHILE_BODY (t));
12558 113 : d->continue_stmt = s;
12559 113 : d->break_stmt = b;
12560 113 : break;
12561 :
12562 414 : case FOR_STMT:
12563 414 : *walk_subtrees = 0;
12564 414 : RECUR (FOR_INIT_STMT (t));
12565 414 : RECUR (FOR_COND_PREP (t));
12566 414 : RECUR (FOR_COND (t));
12567 414 : RECUR (FOR_EXPR (t));
12568 414 : s = d->continue_stmt;
12569 414 : b = d->break_stmt;
12570 414 : RECUR (FOR_BODY (t));
12571 360 : d->continue_stmt = s;
12572 360 : d->break_stmt = b;
12573 360 : break;
12574 :
12575 0 : case RANGE_FOR_STMT:
12576 0 : *walk_subtrees = 0;
12577 0 : RECUR (RANGE_FOR_EXPR (t));
12578 0 : s = d->continue_stmt;
12579 0 : b = d->break_stmt;
12580 0 : RECUR (RANGE_FOR_BODY (t));
12581 0 : d->continue_stmt = s;
12582 0 : d->break_stmt = b;
12583 0 : break;
12584 :
12585 209 : case SWITCH_STMT:
12586 209 : *walk_subtrees = 0;
12587 209 : RECUR (SWITCH_STMT_COND (t));
12588 209 : b = d->break_stmt;
12589 209 : RECUR (SWITCH_STMT_BODY (t));
12590 189 : d->break_stmt = b;
12591 189 : break;
12592 : #undef RECUR
12593 :
12594 : case STATEMENT_LIST:
12595 : case CONSTRUCTOR:
12596 : break;
12597 :
12598 2621257 : case AGGR_INIT_EXPR:
12599 2621257 : case CALL_EXPR:
12600 : /* In C++26 a function could throw. */
12601 2621257 : if (callee_might_throw (t))
12602 129441 : d->could_throw = true;
12603 : break;
12604 :
12605 46959970 : default:
12606 46959970 : if (!EXPR_P (t))
12607 14598290 : *walk_subtrees = 0;
12608 : break;
12609 : }
12610 :
12611 : return NULL_TREE;
12612 : }
12613 :
12614 : /* Return true if T denotes a potentially constant expression. Issue
12615 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
12616 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
12617 : consider the expression in the current context, independent of constexpr
12618 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
12619 : and hard errors should not be reported by constexpr_error.
12620 :
12621 : C++0x [expr.const] used to say
12622 :
12623 : 6 An expression is a potential constant expression if it is
12624 : a constant expression where all occurrences of function
12625 : parameters are replaced by arbitrary constant expressions
12626 : of the appropriate type.
12627 :
12628 : 2 A conditional expression is a constant expression unless it
12629 : involves one of the following as a potentially evaluated
12630 : subexpression (3.2), but subexpressions of logical AND (5.14),
12631 : logical OR (5.15), and conditional (5.16) operations that are
12632 : not evaluated are not considered. */
12633 :
12634 : static bool
12635 4932698202 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
12636 : bool fundef_p, tsubst_flags_t flags,
12637 : tree *jump_target)
12638 : {
12639 : #define RECUR(T,RV) \
12640 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
12641 : jump_target)
12642 :
12643 4932698202 : enum { any = false, rval = true };
12644 4932698202 : int i;
12645 4932698202 : tree tmp;
12646 :
12647 4932698202 : if (t == error_mark_node)
12648 : return false;
12649 4932685654 : if (t == NULL_TREE)
12650 : return true;
12651 4924567744 : location_t loc = cp_expr_loc_or_input_loc (t);
12652 4924567744 : iloc_sentinel ils = loc;
12653 :
12654 4924567744 : if (*jump_target)
12655 : /* If we are jumping, ignore everything. This is simpler than the
12656 : cxx_eval_constant_expression handling because we only need to be
12657 : conservatively correct, and we don't necessarily have a constant value
12658 : available, so we don't bother with switch tracking. */
12659 : return true;
12660 :
12661 2222679 : if (TREE_THIS_VOLATILE (t) && want_rval
12662 1252472 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
12663 4915116699 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
12664 : {
12665 78525 : if (TREE_CLOBBER_P (t))
12666 : {
12667 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
12668 0 : gcc_checking_assert (false);
12669 : return true;
12670 : }
12671 :
12672 78525 : if (flags & tf_error)
12673 25 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
12674 : "a volatile lvalue %qE with type %qT", t,
12675 25 : TREE_TYPE (t));
12676 : return false;
12677 : }
12678 4914959622 : if (CONSTANT_CLASS_P (t))
12679 : return true;
12680 3583790757 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
12681 3583790757 : && TREE_TYPE (t) == error_mark_node)
12682 : return false;
12683 :
12684 3583790714 : switch (TREE_CODE (t))
12685 : {
12686 : case FUNCTION_DECL:
12687 : case BASELINK:
12688 : case TEMPLATE_DECL:
12689 : case OVERLOAD:
12690 : case TEMPLATE_ID_EXPR:
12691 : case LABEL_DECL:
12692 : case CASE_LABEL_EXPR:
12693 : case PREDICT_EXPR:
12694 : case CONST_DECL:
12695 : case SIZEOF_EXPR:
12696 : case ALIGNOF_EXPR:
12697 : case OFFSETOF_EXPR:
12698 : case NOEXCEPT_EXPR:
12699 : case TEMPLATE_PARM_INDEX:
12700 : case TRAIT_EXPR:
12701 : case IDENTIFIER_NODE:
12702 : case USERDEF_LITERAL:
12703 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
12704 : case FIELD_DECL:
12705 : case RESULT_DECL:
12706 : case USING_DECL:
12707 : case USING_STMT:
12708 : case PLACEHOLDER_EXPR:
12709 : case REQUIRES_EXPR:
12710 : case STATIC_ASSERT:
12711 : case DEBUG_BEGIN_STMT:
12712 : case REFLECT_EXPR:
12713 : return true;
12714 :
12715 24794207 : case RETURN_EXPR:
12716 24794207 : if (!RECUR (TREE_OPERAND (t, 0), any))
12717 : return false;
12718 : /* FALLTHROUGH */
12719 :
12720 24626561 : case BREAK_STMT:
12721 24626561 : case CONTINUE_STMT:
12722 24626561 : *jump_target = t;
12723 24626561 : return true;
12724 :
12725 391039182 : case PARM_DECL:
12726 391039182 : if (now && want_rval)
12727 : {
12728 100495828 : tree type = TREE_TYPE (t);
12729 100495828 : if (dependent_type_p (type)
12730 71716965 : || !COMPLETE_TYPE_P (processing_template_decl
12731 : ? type : complete_type (type))
12732 172212793 : || is_really_empty_class (type, /*ignore_vptr*/false))
12733 : /* An empty class has no data to read. */
12734 : return true;
12735 71716259 : if (flags & tf_error)
12736 24 : constexpr_error (input_location, fundef_p,
12737 : "%qE is not a constant expression", t);
12738 : return false;
12739 : }
12740 : return true;
12741 :
12742 283378912 : case AGGR_INIT_EXPR:
12743 283378912 : case CALL_EXPR:
12744 : /* -- an invocation of a function other than a constexpr function
12745 : or a constexpr constructor. */
12746 283378912 : {
12747 283378912 : tree fun = get_function_named_in_call (t);
12748 283378912 : const int nargs = call_expr_nargs (t);
12749 283378912 : i = 0;
12750 :
12751 283378912 : if (fun == NULL_TREE)
12752 : {
12753 : /* Reset to allow the function to continue past the end
12754 : of the block below. Otherwise return early. */
12755 418044 : bool bail = true;
12756 :
12757 418044 : if (TREE_CODE (t) == CALL_EXPR
12758 418044 : && CALL_EXPR_FN (t) == NULL_TREE)
12759 418044 : switch (CALL_EXPR_IFN (t))
12760 : {
12761 : /* These should be ignored, they are optimized away from
12762 : constexpr functions. */
12763 : case IFN_UBSAN_NULL:
12764 : case IFN_UBSAN_BOUNDS:
12765 : case IFN_UBSAN_VPTR:
12766 : case IFN_FALLTHROUGH:
12767 : case IFN_ASSUME:
12768 : return true;
12769 :
12770 : case IFN_ADD_OVERFLOW:
12771 : case IFN_SUB_OVERFLOW:
12772 : case IFN_MUL_OVERFLOW:
12773 : case IFN_LAUNDER:
12774 : case IFN_VEC_CONVERT:
12775 : case IFN_BSWAP:
12776 : case IFN_BITREVERSE:
12777 : case IFN_CLMUL:
12778 : bail = false;
12779 : break;
12780 :
12781 : default:
12782 : break;
12783 : }
12784 :
12785 : if (bail)
12786 : {
12787 : /* fold_call_expr can't do anything with IFN calls. */
12788 172 : if (flags & tf_error)
12789 0 : constexpr_error (loc, fundef_p,
12790 : "call to internal function %qE", t);
12791 : return false;
12792 : }
12793 : }
12794 :
12795 282960868 : if (fun && is_overloaded_fn (fun))
12796 : {
12797 264248580 : if (!RECUR (fun, true))
12798 : return false;
12799 263832453 : fun = get_fns (fun);
12800 :
12801 263832453 : if (TREE_CODE (fun) == FUNCTION_DECL)
12802 : {
12803 246016593 : if (builtin_valid_in_constant_expr_p (fun))
12804 : return true;
12805 244226662 : if (!maybe_constexpr_fn (fun)
12806 : /* Allow any built-in function; if the expansion
12807 : isn't constant, we'll deal with that then. */
12808 54931920 : && !fndecl_built_in_p (fun)
12809 : /* In C++20, replaceable global allocation functions
12810 : are constant expressions. */
12811 37426238 : && (!cxx_replaceable_global_alloc_fn (fun)
12812 973356 : || TREE_CODE (t) != CALL_EXPR
12813 973356 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
12814 353134 : && (current_function_decl == NULL_TREE
12815 353134 : || !is_std_allocator_allocate_deallocate
12816 353134 : (current_function_decl))))
12817 : /* Allow placement new in std::construct_at. */
12818 36459857 : && (!cxx_placement_new_fn (fun)
12819 392734 : || TREE_CODE (t) != CALL_EXPR
12820 392734 : || current_function_decl == NULL_TREE
12821 392722 : || !is_std_construct_at (current_function_decl))
12822 36196071 : && !cxx_dynamic_cast_fn_p (fun)
12823 280419122 : && !cxx_cxa_builtin_fn_p (fun))
12824 : {
12825 : /* In C++26 evaluation of the function arguments might
12826 : throw and in that case it is irrelevant whether
12827 : fun is constexpr or not. */
12828 36137620 : if (cxx_dialect >= cxx26)
12829 8178988 : for (; i < nargs; ++i)
12830 : {
12831 4907080 : tree x = get_nth_callarg (t, i);
12832 4907080 : bool rv = processing_template_decl ? any : rval;
12833 4907080 : bool sub_now = false;
12834 4907080 : if (!potential_constant_expression_1 (x, rv, strict,
12835 : sub_now,
12836 : fundef_p,
12837 : flags,
12838 : jump_target))
12839 : return false;
12840 4615538 : if (throws (jump_target))
12841 : return true;
12842 : }
12843 35816515 : if ((flags & tf_error)
12844 35816515 : && constexpr_error (loc, fundef_p,
12845 : "call to non-%<constexpr%> "
12846 : "function %qD", fun))
12847 272 : explain_invalid_constexpr_fn (fun);
12848 : return false;
12849 : }
12850 : }
12851 :
12852 225904902 : fun = OVL_FIRST (fun);
12853 : /* Skip initial arguments to base constructors. */
12854 225904902 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12855 4137682 : i = num_artificial_parms_for (fun);
12856 : }
12857 18712288 : else if (fun)
12858 : {
12859 18712288 : if (TREE_TYPE (fun)
12860 18712288 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12861 : want_rval = rval;
12862 : else
12863 : want_rval = any;
12864 18712288 : if (RECUR (fun, want_rval))
12865 : /* Might end up being a constant function pointer. But it
12866 : could also be a function object with constexpr op(), so
12867 : we pass 'any' so that the underlying VAR_DECL is deemed
12868 : as potentially-constant even though it wasn't declared
12869 : constexpr. */;
12870 : else
12871 : return false;
12872 : }
12873 542859033 : for (; i < nargs; ++i)
12874 : {
12875 315872827 : tree x = get_nth_callarg (t, i);
12876 : /* In a template, reference arguments haven't been converted to
12877 : REFERENCE_TYPE and we might not even know if the parameter
12878 : is a reference, so accept lvalue constants too. */
12879 315872827 : bool rv = processing_template_decl ? any : rval;
12880 : /* Don't require an immediately constant value, as constexpr
12881 : substitution might not use the value of the argument. */
12882 315872827 : bool sub_now = false;
12883 315872827 : if (!potential_constant_expression_1 (x, rv, strict,
12884 : sub_now, fundef_p, flags,
12885 : jump_target))
12886 : return false;
12887 3209311602 : if (throws (jump_target))
12888 : return true;
12889 : }
12890 : /* In C++26 a function could throw. */
12891 226986206 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12892 11798036 : *jump_target = void_node;
12893 : return true;
12894 : }
12895 :
12896 202233603 : case NON_LVALUE_EXPR:
12897 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
12898 : -- an lvalue of integral type that refers to a non-volatile
12899 : const variable or static data member initialized with
12900 : constant expressions, or
12901 :
12902 : -- an lvalue of literal type that refers to non-volatile
12903 : object defined with constexpr, or that refers to a
12904 : sub-object of such an object; */
12905 202233603 : return RECUR (TREE_OPERAND (t, 0), rval);
12906 :
12907 72513 : case EXCESS_PRECISION_EXPR:
12908 72513 : return RECUR (TREE_OPERAND (t, 0), rval);
12909 :
12910 313841787 : case VAR_DECL:
12911 313841787 : if (DECL_HAS_VALUE_EXPR_P (t))
12912 : {
12913 5272061 : if (now && is_normal_capture_proxy (t))
12914 : {
12915 : /* -- in a lambda-expression, a reference to this or to a
12916 : variable with automatic storage duration defined outside that
12917 : lambda-expression, where the reference would be an
12918 : odr-use. */
12919 :
12920 474692 : if (want_rval)
12921 : /* Since we're doing an lvalue-rvalue conversion, this might
12922 : not be an odr-use, so evaluate the variable directly. */
12923 467659 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
12924 :
12925 7033 : if (flags & tf_error)
12926 : {
12927 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
12928 3 : auto_diagnostic_group d;
12929 3 : if (constexpr_error (input_location, fundef_p,
12930 : "lambda capture of %qE is not a "
12931 : "constant expression", cap)
12932 3 : && decl_constant_var_p (cap))
12933 3 : inform (input_location, "because it is used as a glvalue");
12934 3 : }
12935 : return false;
12936 : }
12937 4797369 : tree ve = DECL_VALUE_EXPR (t);
12938 : /* Treat __PRETTY_FUNCTION__ inside a template function as
12939 : potentially-constant. */
12940 4797369 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
12941 : return true;
12942 4797364 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
12943 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
12944 4794628 : return RECUR (ve, rval);
12945 : }
12946 308569726 : if (want_rval
12947 218165101 : && (now || !var_in_maybe_constexpr_fn (t))
12948 198929718 : && !type_dependent_expression_p (t)
12949 167223803 : && !decl_maybe_constant_var_p (t)
12950 54722554 : && !is_local_temp (t)
12951 54148599 : && (strict
12952 4075556 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
12953 1615879 : || (DECL_INITIAL (t)
12954 1149620 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
12955 53643499 : && COMPLETE_TYPE_P (TREE_TYPE (t))
12956 362213071 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
12957 : {
12958 53638497 : if (flags & tf_error)
12959 175 : non_const_var_error (loc, t, fundef_p);
12960 : return false;
12961 : }
12962 : return true;
12963 :
12964 445467190 : case NOP_EXPR:
12965 445467190 : if (REINTERPRET_CAST_P (t))
12966 : {
12967 160030 : if (flags & tf_error)
12968 49 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
12969 : "constant expression");
12970 : return false;
12971 : }
12972 : /* FALLTHRU */
12973 897472874 : case CONVERT_EXPR:
12974 897472874 : case VIEW_CONVERT_EXPR:
12975 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
12976 : may change to something more specific to type-punning (DR 1312). */
12977 897472874 : {
12978 897472874 : tree from = TREE_OPERAND (t, 0);
12979 897472874 : if (location_wrapper_p (t))
12980 368309675 : return (RECUR (from, want_rval));
12981 529163199 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
12982 : {
12983 301009336 : STRIP_ANY_LOCATION_WRAPPER (from);
12984 301009336 : if (TREE_CODE (from) == INTEGER_CST
12985 301009336 : && !integer_zerop (from))
12986 : {
12987 1653 : if (flags & tf_error)
12988 37 : constexpr_error (loc, fundef_p,
12989 : "%<reinterpret_cast%> from integer to "
12990 : "pointer");
12991 : return false;
12992 : }
12993 : }
12994 : /* convert_to_void used to fold these away to void_node, because they
12995 : are a discarded-value expression. */
12996 529161546 : if (TREE_CODE (t) == CONVERT_EXPR && VOID_TYPE_P (TREE_TYPE (t)))
12997 8682940 : return RECUR (from, /*want_rval=*/false);
12998 520478606 : return RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR);
12999 : }
13000 :
13001 12597 : case ADDRESSOF_EXPR:
13002 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
13003 12597 : t = TREE_OPERAND (t, 0);
13004 12597 : goto handle_addr_expr;
13005 :
13006 99917670 : case ADDR_EXPR:
13007 : /* -- a unary operator & that is applied to an lvalue that
13008 : designates an object with thread or automatic storage
13009 : duration; */
13010 99917670 : t = TREE_OPERAND (t, 0);
13011 :
13012 99917670 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
13013 : /* A pointer-to-member constant. */
13014 : return true;
13015 :
13016 99930001 : handle_addr_expr:
13017 : #if 0
13018 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
13019 : any checking here, as we might dereference the pointer later. If
13020 : we remove this code, also remove check_automatic_or_tls. */
13021 : i = check_automatic_or_tls (t);
13022 : if (i == ck_ok)
13023 : return true;
13024 : if (i == ck_bad)
13025 : {
13026 : if (flags & tf_error)
13027 : error ("address-of an object %qE with thread local or "
13028 : "automatic storage is not a constant expression", t);
13029 : return false;
13030 : }
13031 : #endif
13032 99930001 : return RECUR (t, any);
13033 :
13034 106723975 : case COMPONENT_REF:
13035 106723975 : case ARROW_EXPR:
13036 106723975 : case OFFSET_REF:
13037 : /* -- a class member access unless its postfix-expression is
13038 : of literal type or of pointer to literal type. */
13039 : /* This test would be redundant, as it follows from the
13040 : postfix-expression being a potential constant expression. */
13041 106723975 : if (type_unknown_p (t))
13042 : return true;
13043 98835726 : if (is_overloaded_fn (t))
13044 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
13045 : which uses ob as an lvalue. */
13046 101351280 : want_rval = false;
13047 101351280 : gcc_fallthrough ();
13048 :
13049 101351280 : case REALPART_EXPR:
13050 101351280 : case IMAGPART_EXPR:
13051 101351280 : case BIT_FIELD_REF:
13052 101351280 : return RECUR (TREE_OPERAND (t, 0), want_rval);
13053 :
13054 292839 : case EXPR_PACK_EXPANSION:
13055 292839 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
13056 :
13057 : case PACK_INDEX_EXPR:
13058 : return true;
13059 :
13060 112123932 : case INDIRECT_REF:
13061 112123932 : return RECUR (TREE_OPERAND (t, 0), rval);
13062 :
13063 25272856 : case STATEMENT_LIST:
13064 5024154445 : for (tree stmt : tsi_range (t))
13065 74834239 : if (!RECUR (stmt, any))
13066 4924567744 : return false;
13067 : return true;
13068 :
13069 5099419 : case MODIFY_EXPR:
13070 5099419 : if (cxx_dialect < cxx14)
13071 3895 : goto fail;
13072 5095524 : if (!RECUR (TREE_OPERAND (t, 0), any))
13073 : return false;
13074 : /* Just ignore clobbers. */
13075 4249478 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
13076 : return true;
13077 3777814 : if (!RECUR (TREE_OPERAND (t, 1), rval))
13078 : return false;
13079 : return true;
13080 :
13081 902047 : case MODOP_EXPR:
13082 902047 : if (cxx_dialect < cxx14)
13083 98 : goto fail;
13084 901949 : if (!RECUR (TREE_OPERAND (t, 0), rval))
13085 : return false;
13086 891407 : if (!RECUR (TREE_OPERAND (t, 2), rval))
13087 : return false;
13088 : return true;
13089 :
13090 547643 : case DO_STMT:
13091 547643 : if (!RECUR (DO_COND (t), rval))
13092 : return false;
13093 547643 : if (!RECUR (DO_BODY (t), any))
13094 : return false;
13095 547019 : if (breaks (jump_target) || continues (jump_target))
13096 5 : *jump_target = NULL_TREE;
13097 : return true;
13098 :
13099 444107 : case FOR_STMT:
13100 444107 : if (!RECUR (FOR_INIT_STMT (t), any))
13101 : return false;
13102 444107 : if (!RECUR (FOR_COND_PREP (t), any))
13103 : return false;
13104 444107 : tmp = FOR_COND (t);
13105 444107 : if (!RECUR (tmp, rval))
13106 : return false;
13107 444028 : if (tmp)
13108 : {
13109 396623 : if (!processing_template_decl)
13110 392019 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
13111 : /* If we couldn't evaluate the condition, it might not ever be
13112 : true. */
13113 396623 : if (!integer_onep (tmp))
13114 : {
13115 : /* Before returning true, check if the for body can contain
13116 : a return. */
13117 396623 : hash_set<tree> pset;
13118 396623 : check_for_return_continue_data data = { &pset, NULL_TREE,
13119 396623 : NULL_TREE, false };
13120 396623 : if (tree ret_expr
13121 396623 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
13122 : &data, &pset))
13123 137202 : *jump_target = ret_expr;
13124 396623 : if (data.could_throw)
13125 18237 : *jump_target = void_node;
13126 396623 : return true;
13127 396623 : }
13128 : }
13129 47405 : if (!RECUR (FOR_EXPR (t), any))
13130 : return false;
13131 47405 : if (!RECUR (FOR_BODY (t), any))
13132 : return false;
13133 47405 : if (!RECUR (FOR_COND_CLEANUP (t), any))
13134 : return false;
13135 47405 : if (breaks (jump_target) || continues (jump_target))
13136 12 : *jump_target = NULL_TREE;
13137 : return true;
13138 :
13139 486 : case RANGE_FOR_STMT:
13140 486 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
13141 : return false;
13142 486 : if (!RECUR (RANGE_FOR_EXPR (t), any))
13143 : return false;
13144 486 : if (!RECUR (RANGE_FOR_BODY (t), any))
13145 : return false;
13146 484 : if (breaks (jump_target) || continues (jump_target))
13147 0 : *jump_target = NULL_TREE;
13148 : return true;
13149 :
13150 166502 : case WHILE_STMT:
13151 166502 : if (!RECUR (WHILE_COND_PREP (t), any))
13152 : return false;
13153 166502 : tmp = WHILE_COND (t);
13154 166502 : if (!RECUR (tmp, rval))
13155 : return false;
13156 166434 : if (!processing_template_decl)
13157 166397 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
13158 : /* If we couldn't evaluate the condition, it might not ever be true. */
13159 166434 : if (!integer_onep (tmp))
13160 : {
13161 : /* Before returning true, check if the while body can contain
13162 : a return. */
13163 156302 : hash_set<tree> pset;
13164 156302 : check_for_return_continue_data data = { &pset, NULL_TREE,
13165 156302 : NULL_TREE, false };
13166 156302 : if (tree ret_expr
13167 156302 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
13168 : &data, &pset))
13169 476 : *jump_target = ret_expr;
13170 156302 : if (data.could_throw)
13171 4645 : *jump_target = void_node;
13172 156302 : return true;
13173 156302 : }
13174 10132 : if (!RECUR (WHILE_BODY (t), any))
13175 : return false;
13176 10121 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
13177 : return false;
13178 10121 : if (breaks (jump_target) || continues (jump_target))
13179 24 : *jump_target = NULL_TREE;
13180 : return true;
13181 :
13182 43027 : case SWITCH_STMT:
13183 43027 : if (!RECUR (SWITCH_STMT_COND (t), rval))
13184 : return false;
13185 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
13186 : unreachable labels would be checked and it is enough if there is
13187 : a single switch cond value for which it is a valid constant
13188 : expression. We need to check if there are any RETURN_EXPRs
13189 : or CONTINUE_STMTs inside of the body though, as in that case
13190 : we need to set *jump_target. */
13191 : else
13192 : {
13193 43017 : hash_set<tree> pset;
13194 43017 : check_for_return_continue_data data = { &pset, NULL_TREE,
13195 43017 : NULL_TREE, false };
13196 43017 : if (tree ret_expr
13197 43017 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
13198 : &data, &pset))
13199 : /* The switch might return. */
13200 42624 : *jump_target = ret_expr;
13201 393 : else if (data.continue_stmt)
13202 : /* The switch can't return, but might continue. */
13203 3 : *jump_target = data.continue_stmt;
13204 43017 : if (data.could_throw)
13205 1875 : *jump_target = void_node;
13206 43017 : }
13207 43017 : return true;
13208 :
13209 50 : case STMT_EXPR:
13210 50 : return RECUR (STMT_EXPR_STMT (t), rval);
13211 :
13212 623971 : case LAMBDA_EXPR:
13213 623971 : if (cxx_dialect >= cxx17)
13214 : /* In C++17 lambdas can be constexpr, don't give up yet. */
13215 : return true;
13216 505 : else if (flags & tf_error)
13217 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
13218 : "constant expression before C++17");
13219 : return false;
13220 :
13221 112004 : case NEW_EXPR:
13222 112004 : case VEC_NEW_EXPR:
13223 112004 : case DELETE_EXPR:
13224 112004 : case VEC_DELETE_EXPR:
13225 112004 : if (cxx_dialect >= cxx20)
13226 : /* In C++20, new-expressions are potentially constant. */
13227 : return true;
13228 1078 : else if (flags & tf_error)
13229 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
13230 : "constant expression before C++20");
13231 : return false;
13232 :
13233 84405 : case DYNAMIC_CAST_EXPR:
13234 84405 : case PSEUDO_DTOR_EXPR:
13235 84405 : case OMP_PARALLEL:
13236 84405 : case OMP_TASK:
13237 84405 : case OMP_FOR:
13238 84405 : case OMP_SIMD:
13239 84405 : case OMP_DISTRIBUTE:
13240 84405 : case OMP_TASKLOOP:
13241 84405 : case OMP_LOOP:
13242 84405 : case OMP_TEAMS:
13243 84405 : case OMP_TARGET_DATA:
13244 84405 : case OMP_TARGET:
13245 84405 : case OMP_SECTIONS:
13246 84405 : case OMP_ORDERED:
13247 84405 : case OMP_CRITICAL:
13248 84405 : case OMP_SINGLE:
13249 84405 : case OMP_SCAN:
13250 84405 : case OMP_SCOPE:
13251 84405 : case OMP_SECTION:
13252 84405 : case OMP_MASTER:
13253 84405 : case OMP_MASKED:
13254 84405 : case OMP_TASKGROUP:
13255 84405 : case OMP_TARGET_UPDATE:
13256 84405 : case OMP_TARGET_ENTER_DATA:
13257 84405 : case OMP_TARGET_EXIT_DATA:
13258 84405 : case OMP_ATOMIC:
13259 84405 : case OMP_ATOMIC_READ:
13260 84405 : case OMP_ATOMIC_CAPTURE_OLD:
13261 84405 : case OMP_ATOMIC_CAPTURE_NEW:
13262 84405 : case OMP_DEPOBJ:
13263 84405 : case OMP_ALLOCATE:
13264 84405 : case OACC_PARALLEL:
13265 84405 : case OACC_KERNELS:
13266 84405 : case OACC_SERIAL:
13267 84405 : case OACC_DATA:
13268 84405 : case OACC_HOST_DATA:
13269 84405 : case OACC_LOOP:
13270 84405 : case OACC_CACHE:
13271 84405 : case OACC_DECLARE:
13272 84405 : case OACC_ENTER_DATA:
13273 84405 : case OACC_EXIT_DATA:
13274 84405 : case OACC_UPDATE:
13275 84405 : case OMP_ARRAY_SECTION:
13276 : /* GCC internal stuff. */
13277 84405 : case VA_ARG_EXPR:
13278 84405 : case TRANSACTION_EXPR:
13279 84405 : case AT_ENCODE_EXPR:
13280 84405 : fail:
13281 84405 : if (flags & tf_error)
13282 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
13283 : "expression", t);
13284 : return false;
13285 :
13286 : case OMP_DECLARE_MAPPER:
13287 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
13288 : constant. */
13289 : return true;
13290 :
13291 27129 : case THROW_EXPR:
13292 27129 : if (cxx_dialect < cxx26)
13293 291 : goto fail;
13294 26838 : return RECUR (TREE_OPERAND (t, 0), rval);
13295 :
13296 531 : case ASM_EXPR:
13297 531 : if (flags & tf_error)
13298 6 : inline_asm_in_constexpr_error (loc, fundef_p);
13299 : return false;
13300 :
13301 531730 : case OBJ_TYPE_REF:
13302 531730 : if (cxx_dialect >= cxx20)
13303 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
13304 : return true;
13305 9068 : else if (flags & tf_error)
13306 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
13307 : "%<constexpr%> before C++20");
13308 : return false;
13309 :
13310 6687 : case TYPEID_EXPR:
13311 : /* In C++20, a typeid expression whose operand is of polymorphic
13312 : class type can be constexpr. */
13313 6687 : {
13314 6687 : tree e = TREE_OPERAND (t, 0);
13315 6687 : if (cxx_dialect < cxx20
13316 26 : && strict
13317 26 : && !TYPE_P (e)
13318 19 : && !type_dependent_expression_p (e)
13319 7 : && CLASS_TYPE_P (TREE_TYPE (e))
13320 6692 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
13321 : {
13322 4 : if (flags & tf_error)
13323 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
13324 : "constant expression because %qE is "
13325 : "of polymorphic type", e);
13326 : return false;
13327 : }
13328 : return true;
13329 : }
13330 :
13331 31155485 : case POINTER_DIFF_EXPR:
13332 31155485 : case MINUS_EXPR:
13333 31155485 : want_rval = true;
13334 31155485 : goto binary;
13335 :
13336 84921425 : case LT_EXPR:
13337 84921425 : case LE_EXPR:
13338 84921425 : case GT_EXPR:
13339 84921425 : case GE_EXPR:
13340 84921425 : case EQ_EXPR:
13341 84921425 : case NE_EXPR:
13342 84921425 : case SPACESHIP_EXPR:
13343 84921425 : want_rval = true;
13344 84921425 : goto binary;
13345 :
13346 1977224 : case PREINCREMENT_EXPR:
13347 1977224 : case POSTINCREMENT_EXPR:
13348 1977224 : case PREDECREMENT_EXPR:
13349 1977224 : case POSTDECREMENT_EXPR:
13350 1977224 : if (cxx_dialect < cxx14)
13351 8866 : goto fail;
13352 1968358 : goto unary;
13353 :
13354 1270060 : case BIT_NOT_EXPR:
13355 : /* A destructor. */
13356 1270060 : if (TYPE_P (TREE_OPERAND (t, 0)))
13357 : return true;
13358 : /* fall through. */
13359 :
13360 48287089 : case CONJ_EXPR:
13361 48287089 : case SAVE_EXPR:
13362 48287089 : case FIX_TRUNC_EXPR:
13363 48287089 : case FLOAT_EXPR:
13364 48287089 : case NEGATE_EXPR:
13365 48287089 : case ABS_EXPR:
13366 48287089 : case ABSU_EXPR:
13367 48287089 : case TRUTH_NOT_EXPR:
13368 48287089 : case FIXED_CONVERT_EXPR:
13369 48287089 : case UNARY_PLUS_EXPR:
13370 48287089 : case UNARY_LEFT_FOLD_EXPR:
13371 48287089 : case UNARY_RIGHT_FOLD_EXPR:
13372 48287089 : case VEC_DUPLICATE_EXPR:
13373 1270060 : unary:
13374 48287089 : return RECUR (TREE_OPERAND (t, 0), rval);
13375 :
13376 31485541 : case CAST_EXPR:
13377 31485541 : case CONST_CAST_EXPR:
13378 31485541 : case STATIC_CAST_EXPR:
13379 31485541 : case REINTERPRET_CAST_EXPR:
13380 31485541 : case IMPLICIT_CONV_EXPR:
13381 31485541 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
13382 : /* In C++98, a conversion to non-integral type can't be part of a
13383 : constant expression. */
13384 : {
13385 290 : if (flags & tf_error)
13386 0 : constexpr_error (loc, fundef_p,
13387 : "cast to non-integral type %qT in a constant "
13388 0 : "expression", TREE_TYPE (t));
13389 : return false;
13390 : }
13391 : /* This might be a conversion from a class to a (potentially) literal
13392 : type. Let's consider it potentially constant since the conversion
13393 : might be a constexpr user-defined conversion. */
13394 31485251 : else if (cxx_dialect >= cxx11
13395 31465166 : && (dependent_type_p (TREE_TYPE (t))
13396 11069201 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
13397 11054835 : || literal_type_p (TREE_TYPE (t)))
13398 31427174 : && TREE_OPERAND (t, 0)
13399 62670301 : && (TREE_CODE (t) != CAST_EXPR
13400 23656396 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
13401 : {
13402 31142124 : tree from = TREE_OPERAND (t, 0);
13403 31142124 : if (TREE_CODE (t) == CAST_EXPR)
13404 23613470 : from = TREE_VALUE (from);
13405 31142124 : tree type = TREE_TYPE (from);
13406 : /* If this is a dependent type, it could end up being a class
13407 : with conversions. */
13408 31142124 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
13409 : return true;
13410 : /* Or a non-dependent class which has conversions. */
13411 591800 : else if (CLASS_TYPE_P (type)
13412 591800 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
13413 : return true;
13414 : }
13415 :
13416 25025285 : return (RECUR (TREE_OPERAND (t, 0),
13417 25025285 : !TYPE_REF_P (TREE_TYPE (t))));
13418 :
13419 14472761 : case BIND_EXPR:
13420 14472761 : return RECUR (BIND_EXPR_BODY (t), want_rval);
13421 :
13422 70619500 : case CLEANUP_POINT_EXPR:
13423 70619500 : case MUST_NOT_THROW_EXPR:
13424 70619500 : case TRY_CATCH_EXPR:
13425 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
13426 : body could throw, even with catch (...) among handlers we'd need
13427 : to analyze them in detail if they couldn't rethrow it. More
13428 : importantly though, throws (jump_target) is just conservative,
13429 : and there could be e.g.
13430 : try
13431 : {
13432 : possibly_throwing_fn (args);
13433 : break;
13434 : }
13435 : catch (...)
13436 : {
13437 : }
13438 : or continue or return instead of break. So, clearing *jump_target
13439 : because we see catch (...) handler might mean we missed break
13440 : etc. */
13441 70619500 : case TRY_BLOCK:
13442 70619500 : case EH_SPEC_BLOCK:
13443 70619500 : case EXPR_STMT:
13444 70619500 : case PAREN_EXPR:
13445 : /* For convenience. */
13446 70619500 : case LOOP_EXPR:
13447 70619500 : case EXIT_EXPR:
13448 70619500 : return RECUR (TREE_OPERAND (t, 0), want_rval);
13449 :
13450 12919172 : case DECL_EXPR:
13451 12919172 : tmp = DECL_EXPR_DECL (t);
13452 8574584 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
13453 18923173 : && (processing_template_decl
13454 5519862 : ? !decl_maybe_constant_var_p (tmp)
13455 5035723 : : !decl_constant_var_p (tmp)))
13456 : {
13457 4961392 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
13458 : {
13459 8 : if (flags & tf_error)
13460 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
13461 : "%qD defined %<thread_local%> in "
13462 : "%<constexpr%> context", tmp);
13463 : return false;
13464 : }
13465 4961384 : else if (TREE_STATIC (tmp))
13466 : {
13467 160 : if (flags & tf_error)
13468 36 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
13469 : "%qD defined %<static%> in %<constexpr%> "
13470 : "context", tmp);
13471 : return false;
13472 : }
13473 4961224 : else if (!check_for_uninitialized_const_var
13474 4961224 : (tmp, /*constexpr_context_p=*/true, flags))
13475 : return false;
13476 : }
13477 12914053 : if (VAR_P (tmp))
13478 8569465 : return RECUR (DECL_INITIAL (tmp), want_rval);
13479 : return true;
13480 :
13481 44379 : case TRY_FINALLY_EXPR:
13482 44379 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
13483 44379 : && RECUR (TREE_OPERAND (t, 1), any));
13484 :
13485 0 : case EH_ELSE_EXPR:
13486 : /* maybe_apply_function_contracts uses this to check postconditions only
13487 : on normal return. */
13488 0 : return (RECUR (TREE_OPERAND (t, 1), any)
13489 0 : || RECUR (TREE_OPERAND (t, 0), any));
13490 :
13491 24499101 : case SCOPE_REF:
13492 24499101 : return RECUR (TREE_OPERAND (t, 1), want_rval);
13493 :
13494 49555375 : case TARGET_EXPR:
13495 49555375 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
13496 49346730 : && !TARGET_EXPR_ELIDING_P (t)
13497 89125032 : && !literal_type_p (TREE_TYPE (t)))
13498 : {
13499 901029 : if (flags & tf_error)
13500 : {
13501 25 : auto_diagnostic_group d;
13502 25 : if (constexpr_error (loc, fundef_p,
13503 : "temporary of non-literal type %qT in a "
13504 25 : "constant expression", TREE_TYPE (t)))
13505 25 : explain_non_literal_class (TREE_TYPE (t));
13506 25 : }
13507 : return false;
13508 : }
13509 : /* FALLTHRU */
13510 77219918 : case INIT_EXPR:
13511 77219918 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
13512 : return true;
13513 77100452 : return RECUR (TREE_OPERAND (t, 1), rval);
13514 :
13515 40309764 : case CONSTRUCTOR:
13516 40309764 : {
13517 40309764 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
13518 40309764 : constructor_elt *ce;
13519 5199404155 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
13520 235812275 : if (!RECUR (ce->value, want_rval))
13521 : return false;
13522 : return true;
13523 : }
13524 :
13525 21199655 : case TREE_LIST:
13526 21199655 : {
13527 21199655 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
13528 : || DECL_P (TREE_PURPOSE (t)));
13529 21199655 : if (!RECUR (TREE_VALUE (t), want_rval))
13530 : return false;
13531 18997889 : if (TREE_CHAIN (t) == NULL_TREE)
13532 : return true;
13533 55025 : return RECUR (TREE_CHAIN (t), want_rval);
13534 : }
13535 :
13536 15371263 : case TRUNC_DIV_EXPR:
13537 15371263 : case CEIL_DIV_EXPR:
13538 15371263 : case FLOOR_DIV_EXPR:
13539 15371263 : case ROUND_DIV_EXPR:
13540 15371263 : case TRUNC_MOD_EXPR:
13541 15371263 : case CEIL_MOD_EXPR:
13542 15371263 : case ROUND_MOD_EXPR:
13543 15371263 : {
13544 15371263 : tree denom = TREE_OPERAND (t, 1);
13545 15371263 : if (!RECUR (denom, rval))
13546 : return false;
13547 : /* We can't call cxx_eval_outermost_constant_expr on an expression
13548 : that hasn't been through instantiate_non_dependent_expr yet. */
13549 14851426 : if (!processing_template_decl)
13550 7874416 : denom = cxx_eval_outermost_constant_expr (denom, true);
13551 14851426 : if (integer_zerop (denom))
13552 : {
13553 378 : if (flags & tf_error)
13554 38 : constexpr_error (input_location, fundef_p,
13555 : "division by zero is not a constant expression");
13556 : return false;
13557 : }
13558 : else
13559 : {
13560 14851048 : want_rval = true;
13561 14851048 : return RECUR (TREE_OPERAND (t, 0), want_rval);
13562 : }
13563 : }
13564 :
13565 9640109 : case COMPOUND_EXPR:
13566 9640109 : {
13567 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
13568 : COMPOUND_EXPR; don't get confused. */
13569 9640109 : tree op0 = TREE_OPERAND (t, 0);
13570 9640109 : tree op1 = TREE_OPERAND (t, 1);
13571 9640109 : STRIP_NOPS (op1);
13572 9640109 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
13573 2709332 : return RECUR (op0, want_rval);
13574 : else
13575 6930777 : goto binary;
13576 : }
13577 :
13578 : /* If the first operand is the non-short-circuit constant, look at
13579 : the second operand; otherwise we only care about the first one for
13580 : potentiality. */
13581 18767208 : case TRUTH_AND_EXPR:
13582 18767208 : case TRUTH_ANDIF_EXPR:
13583 18767208 : tmp = boolean_true_node;
13584 18767208 : goto truth;
13585 7610898 : case TRUTH_OR_EXPR:
13586 7610898 : case TRUTH_ORIF_EXPR:
13587 7610898 : tmp = boolean_false_node;
13588 26378106 : truth:
13589 26378106 : {
13590 26378106 : tree op0 = TREE_OPERAND (t, 0);
13591 26378106 : tree op1 = TREE_OPERAND (t, 1);
13592 26378106 : if (!RECUR (op0, rval))
13593 : return false;
13594 18188430 : if (!(flags & tf_error) && RECUR (op1, rval))
13595 : /* When quiet, try to avoid expensive trial evaluation by first
13596 : checking potentiality of the second operand. */
13597 : return true;
13598 1826373 : if (!processing_template_decl)
13599 1426644 : op0 = cxx_eval_outermost_constant_expr (op0, true);
13600 1826373 : if (tree_int_cst_equal (op0, tmp))
13601 9132 : return (flags & tf_error) ? RECUR (op1, rval) : false;
13602 : else
13603 : return true;
13604 : }
13605 :
13606 : case PLUS_EXPR:
13607 : case MULT_EXPR:
13608 : case POINTER_PLUS_EXPR:
13609 : case RDIV_EXPR:
13610 : case EXACT_DIV_EXPR:
13611 : case MIN_EXPR:
13612 : case MAX_EXPR:
13613 : case LSHIFT_EXPR:
13614 : case RSHIFT_EXPR:
13615 : case LROTATE_EXPR:
13616 : case RROTATE_EXPR:
13617 : case BIT_IOR_EXPR:
13618 : case BIT_XOR_EXPR:
13619 : case BIT_AND_EXPR:
13620 : case TRUTH_XOR_EXPR:
13621 : case UNORDERED_EXPR:
13622 : case ORDERED_EXPR:
13623 : case UNLT_EXPR:
13624 : case UNLE_EXPR:
13625 : case UNGT_EXPR:
13626 : case UNGE_EXPR:
13627 : case UNEQ_EXPR:
13628 : case LTGT_EXPR:
13629 : case RANGE_EXPR:
13630 : case COMPLEX_EXPR:
13631 237371432 : want_rval = true;
13632 : /* Fall through. */
13633 249618437 : case ARRAY_REF:
13634 249618437 : case ARRAY_RANGE_REF:
13635 249618437 : case MEMBER_REF:
13636 249618437 : case DOTSTAR_EXPR:
13637 249618437 : case MEM_REF:
13638 249618437 : case BINARY_LEFT_FOLD_EXPR:
13639 249618437 : case BINARY_RIGHT_FOLD_EXPR:
13640 249618437 : binary:
13641 549999591 : for (i = 0; i < 2; ++i)
13642 407024011 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
13643 : return false;
13644 : return true;
13645 :
13646 : case VEC_PERM_EXPR:
13647 121529 : for (i = 0; i < 3; ++i)
13648 120583 : if (!RECUR (TREE_OPERAND (t, i), true))
13649 : return false;
13650 : return true;
13651 :
13652 7518740 : case COND_EXPR:
13653 7518740 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
13654 : {
13655 8 : if (flags & tf_error)
13656 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
13657 : "constant expression");
13658 : return false;
13659 : }
13660 : /* Fall through. */
13661 18388620 : case IF_STMT:
13662 18388620 : case VEC_COND_EXPR:
13663 : /* If the condition is a known constant, we know which of the legs we
13664 : care about; otherwise we only require that the condition and
13665 : either of the legs be potentially constant. */
13666 18388620 : tmp = TREE_OPERAND (t, 0);
13667 18388620 : if (!RECUR (tmp, rval))
13668 : return false;
13669 16097178 : if (!processing_template_decl)
13670 14054717 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
13671 : /* potential_constant_expression* isn't told if it is called for
13672 : manifestly_const_eval or not, so for consteval if always
13673 : process both branches as if the condition is not a known
13674 : constant. */
13675 16097178 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
13676 : {
13677 16019212 : if (integer_zerop (tmp))
13678 3524638 : return RECUR (TREE_OPERAND (t, 2), want_rval);
13679 12494574 : else if (TREE_CODE (tmp) == INTEGER_CST)
13680 3919335 : return RECUR (TREE_OPERAND (t, 1), want_rval);
13681 : }
13682 8653205 : tmp = *jump_target;
13683 9985736 : for (i = 1; i < 3; ++i)
13684 : {
13685 9839591 : tree this_jump_target = tmp;
13686 9839591 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
13687 : want_rval, strict, now, fundef_p,
13688 : tf_none, &this_jump_target))
13689 : {
13690 8809396 : if (returns (&this_jump_target) || throws (&this_jump_target))
13691 2449880 : *jump_target = this_jump_target;
13692 6057180 : else if (!returns (jump_target) && !throws (jump_target))
13693 : {
13694 6057180 : if (breaks (&this_jump_target)
13695 6057180 : || continues (&this_jump_target))
13696 45 : *jump_target = this_jump_target;
13697 6057180 : if (i == 1)
13698 : {
13699 : /* If the then branch is potentially constant, but
13700 : does not return, check if the else branch
13701 : couldn't return, break or continue. */
13702 5017613 : hash_set<tree> pset;
13703 5017613 : check_for_return_continue_data data = { &pset, NULL_TREE,
13704 : NULL_TREE,
13705 5017613 : false };
13706 10035226 : if (tree ret_expr
13707 5017613 : = cp_walk_tree (&TREE_OPERAND (t, 2),
13708 : check_for_return_continue, &data,
13709 : &pset))
13710 79513 : *jump_target = ret_expr;
13711 4938100 : else if (*jump_target == NULL_TREE)
13712 : {
13713 4938058 : if (data.continue_stmt)
13714 0 : *jump_target = data.continue_stmt;
13715 4938058 : else if (data.break_stmt)
13716 13 : *jump_target = data.break_stmt;
13717 : }
13718 5017613 : if (data.could_throw)
13719 56594 : *jump_target = void_node;
13720 5017613 : }
13721 : }
13722 8507060 : return true;
13723 : }
13724 : }
13725 146145 : if (flags & tf_error)
13726 : {
13727 3 : if (TREE_CODE (t) == IF_STMT)
13728 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
13729 : "constant expression");
13730 : else
13731 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
13732 : "constant expression", t);
13733 : }
13734 : return false;
13735 :
13736 1756 : case VEC_INIT_EXPR:
13737 1756 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
13738 : return true;
13739 869 : if (flags & tf_error)
13740 : {
13741 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
13742 : "initialization"))
13743 2 : diagnose_non_constexpr_vec_init (t);
13744 : }
13745 : return false;
13746 :
13747 : case TYPE_DECL:
13748 : case TAG_DEFN:
13749 : /* We can see these in statement-expressions. */
13750 : return true;
13751 :
13752 1470465 : case CLEANUP_STMT:
13753 1470465 : if (!RECUR (CLEANUP_BODY (t), any))
13754 : return false;
13755 2455046 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
13756 : return false;
13757 : return true;
13758 :
13759 : case EMPTY_CLASS_EXPR:
13760 : return true;
13761 :
13762 128 : case GOTO_EXPR:
13763 128 : {
13764 128 : tree *target = &TREE_OPERAND (t, 0);
13765 : /* Gotos representing break, continue and cdtor return are OK. */
13766 128 : if (breaks (target) || continues (target) || returns (target))
13767 : {
13768 108 : *jump_target = *target;
13769 108 : return true;
13770 : }
13771 20 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
13772 : /* The user didn't write this goto, this isn't the problem. */
13773 : return true;
13774 16 : if (flags & tf_error)
13775 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
13776 : "expression");
13777 : return false;
13778 : }
13779 :
13780 : case ASSERTION_STMT:
13781 : case PRECONDITION_STMT:
13782 : case POSTCONDITION_STMT:
13783 : /* Contracts are not supposed to alter this; we have to check that this
13784 : is not violated at a later time. */
13785 : return true;
13786 :
13787 228 : case LABEL_EXPR:
13788 228 : t = LABEL_EXPR_LABEL (t);
13789 228 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
13790 : return true;
13791 25 : else if (flags & tf_error)
13792 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
13793 : "function only available with %<-std=c++23%> or "
13794 : "%<-std=gnu++23%>");
13795 : return false;
13796 :
13797 10202 : case ANNOTATE_EXPR:
13798 10202 : return RECUR (TREE_OPERAND (t, 0), rval);
13799 :
13800 185906 : case BIT_CAST_EXPR:
13801 185906 : return RECUR (TREE_OPERAND (t, 0), rval);
13802 :
13803 : /* Coroutine await, yield and return expressions are not. */
13804 768 : case CO_AWAIT_EXPR:
13805 768 : case CO_YIELD_EXPR:
13806 768 : case CO_RETURN_EXPR:
13807 768 : case TEMPLATE_FOR_STMT:
13808 768 : if (flags & tf_error)
13809 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
13810 : "%qE is not a constant expression", t);
13811 : return false;
13812 :
13813 : /* Assume a TU-local entity is not constant, we'll error later when
13814 : instantiating. */
13815 : case TU_LOCAL_ENTITY:
13816 : return false;
13817 :
13818 : /* A splice expression is dependent, but will be constant after
13819 : substitution. */
13820 : case SPLICE_EXPR:
13821 : return true;
13822 :
13823 30 : case NONTYPE_ARGUMENT_PACK:
13824 30 : {
13825 30 : tree args = ARGUMENT_PACK_ARGS (t);
13826 30 : int len = TREE_VEC_LENGTH (args);
13827 90 : for (int i = 0; i < len; ++i)
13828 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
13829 : return false;
13830 : return true;
13831 : }
13832 :
13833 0 : default:
13834 0 : if (objc_non_constant_expr_p (t))
13835 : return false;
13836 :
13837 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13838 0 : gcc_unreachable ();
13839 : return false;
13840 : }
13841 : #undef RECUR
13842 4924567744 : }
13843 :
13844 : bool
13845 1759679433 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13846 : bool fundef_p, tsubst_flags_t flags)
13847 : {
13848 1759679433 : if (flags & tf_error)
13849 : {
13850 : /* Check potentiality quietly first, as that could be performed more
13851 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13852 : that fails, replay the check noisily to give errors. */
13853 11448180 : flags &= ~tf_error;
13854 11448180 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13855 : flags))
13856 : return true;
13857 1103 : flags |= tf_error;
13858 : }
13859 :
13860 1748232356 : tree target = NULL_TREE;
13861 1748232356 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13862 1748232356 : flags, &target);
13863 : }
13864 :
13865 : /* The main entry point to the above. */
13866 :
13867 : bool
13868 444747231 : potential_constant_expression (tree t)
13869 : {
13870 444747231 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13871 : /*now*/false, /*fundef_p*/false,
13872 444747231 : tf_none);
13873 : }
13874 :
13875 : /* As above, but require a constant rvalue. */
13876 :
13877 : bool
13878 35424656 : potential_rvalue_constant_expression (tree t)
13879 : {
13880 35424656 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13881 : /*now*/false, /*fundef_p*/false,
13882 35424656 : tf_none);
13883 : }
13884 :
13885 : /* Like above, but complain about non-constant expressions. */
13886 :
13887 : bool
13888 88 : require_potential_constant_expression (tree t)
13889 : {
13890 88 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13891 : /*now*/false, /*fundef_p*/false,
13892 88 : tf_warning_or_error);
13893 : }
13894 :
13895 : /* Cross product of the above. */
13896 :
13897 : bool
13898 131812 : require_potential_rvalue_constant_expression (tree t)
13899 : {
13900 131812 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13901 : /*now*/false, /*fundef_p*/false,
13902 131812 : tf_warning_or_error);
13903 : }
13904 :
13905 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
13906 :
13907 : bool
13908 296 : require_potential_rvalue_constant_expression_fncheck (tree t)
13909 : {
13910 296 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13911 : /*now*/false, /*fundef_p*/true,
13912 296 : tf_warning_or_error);
13913 : }
13914 :
13915 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
13916 :
13917 : bool
13918 1149 : require_rvalue_constant_expression (tree t)
13919 : {
13920 1149 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13921 : /*now*/true, /*fundef_p*/false,
13922 1149 : tf_warning_or_error);
13923 : }
13924 :
13925 : /* Like potential_constant_expression, but don't consider possible constexpr
13926 : substitution of the current function. That is, PARM_DECL qualifies under
13927 : potential_constant_expression, but not here.
13928 :
13929 : This is basically what you can check when any actual constant values might
13930 : be value-dependent. */
13931 :
13932 : bool
13933 926154412 : is_constant_expression (tree t)
13934 : {
13935 926154412 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13936 : /*now*/true, /*fundef_p*/false,
13937 926154412 : tf_none);
13938 : }
13939 :
13940 : /* As above, but expect an rvalue. */
13941 :
13942 : bool
13943 156263966 : is_rvalue_constant_expression (tree t)
13944 : {
13945 156263966 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13946 : /*now*/true, /*fundef_p*/false,
13947 156263966 : tf_none);
13948 : }
13949 :
13950 : /* Like above, but complain about non-constant expressions. */
13951 :
13952 : bool
13953 11314835 : require_constant_expression (tree t)
13954 : {
13955 11314835 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13956 : /*now*/true, /*fundef_p*/false,
13957 11314835 : tf_warning_or_error);
13958 : }
13959 :
13960 : /* Like is_constant_expression, but allow const variables that are not allowed
13961 : under constexpr rules. */
13962 :
13963 : bool
13964 174192808 : is_static_init_expression (tree t)
13965 : {
13966 174192808 : return potential_constant_expression_1 (t, /*want_rval*/false,
13967 : /*strict*/false, /*now*/true,
13968 174192808 : /*fundef_p*/false, tf_none);
13969 : }
13970 :
13971 : /* Returns true if T is a potential constant expression that is not
13972 : instantiation-dependent, and therefore a candidate for constant folding even
13973 : in a template. */
13974 :
13975 : bool
13976 894708014 : is_nondependent_constant_expression (tree t)
13977 : {
13978 894708014 : return (!type_unknown_p (t)
13979 894707964 : && is_constant_expression (t)
13980 1679008664 : && !instantiation_dependent_expression_p (t));
13981 : }
13982 :
13983 : /* Returns true if T is a potential static initializer expression that is not
13984 : instantiation-dependent. */
13985 :
13986 : bool
13987 174192808 : is_nondependent_static_init_expression (tree t)
13988 : {
13989 174192808 : return (!type_unknown_p (t)
13990 174192808 : && is_static_init_expression (t)
13991 315059576 : && !instantiation_dependent_expression_p (t));
13992 : }
13993 :
13994 : /* True iff FN is an implicitly constexpr function. */
13995 :
13996 : bool
13997 466939 : decl_implicit_constexpr_p (tree fn)
13998 : {
13999 466939 : if (!(flag_implicit_constexpr
14000 18 : && TREE_CODE (fn) == FUNCTION_DECL
14001 18 : && DECL_DECLARED_CONSTEXPR_P (fn)))
14002 : return false;
14003 :
14004 18 : if (DECL_CLONED_FUNCTION_P (fn))
14005 0 : fn = DECL_CLONED_FUNCTION (fn);
14006 :
14007 18 : return (DECL_LANG_SPECIFIC (fn)
14008 18 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
14009 : }
14010 :
14011 : /* Finalize constexpr processing after parsing. */
14012 :
14013 : void
14014 99305 : fini_constexpr (void)
14015 : {
14016 : /* The contexpr call and fundef copies tables are no longer needed. */
14017 99305 : constexpr_call_table = NULL;
14018 99305 : fundef_copies_table = NULL;
14019 99305 : }
14020 :
14021 : /* Return true if T is one of the heap vars. Include "heap deleted" if
14022 : INCLUDE_DELETED_P. */
14023 :
14024 : bool
14025 5850053 : heap_var_p (tree t, bool include_deleted_p/*=false*/)
14026 : {
14027 5850053 : return (DECL_NAME (t) == heap_uninit_identifier
14028 5711139 : || DECL_NAME (t) == heap_identifier
14029 5682235 : || DECL_NAME (t) == heap_vec_uninit_identifier
14030 5679594 : || DECL_NAME (t) == heap_vec_identifier
14031 11526378 : || (include_deleted_p && DECL_NAME (t) == heap_deleted_identifier));
14032 : }
14033 :
14034 : #include "gt-cp-constexpr.h"
|