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 :
57 : /* Returns true iff FUN is an instantiation of a constexpr function
58 : template or a defaulted constexpr function. */
59 :
60 : bool
61 181203684 : is_instantiation_of_constexpr (tree fun)
62 : {
63 252422172 : return ((DECL_TEMPLOID_INSTANTIATION (fun)
64 110483253 : && DECL_DECLARED_CONSTEXPR_P (DECL_TI_TEMPLATE (fun)))
65 272062223 : || (DECL_DEFAULTED_FN (fun)
66 0 : && DECL_DECLARED_CONSTEXPR_P (fun)));
67 : }
68 :
69 : /* Return true if T is a literal type. */
70 :
71 : bool
72 166834893 : literal_type_p (tree t)
73 : {
74 163752988 : if (SCALAR_TYPE_P (t)
75 75730484 : || VECTOR_TYPE_P (t)
76 75709959 : || TYPE_REF_P (t)
77 229203699 : || (VOID_TYPE_P (t) && cxx_dialect >= cxx14))
78 : return true;
79 58095496 : if (CLASS_TYPE_P (t))
80 : {
81 56643147 : t = complete_type (t);
82 56643147 : gcc_assert (COMPLETE_TYPE_P (t) || errorcount);
83 56643147 : return CLASSTYPE_LITERAL_P (t);
84 : }
85 1452349 : if (TREE_CODE (t) == ARRAY_TYPE)
86 1452155 : return literal_type_p (strip_array_types (t));
87 : return false;
88 : }
89 :
90 : /* If DECL is a variable declared `constexpr', require its type
91 : be literal. Return error_mark_node if we give an error, the
92 : DECL otherwise. */
93 :
94 : tree
95 318619161 : ensure_literal_type_for_constexpr_object (tree decl)
96 : {
97 318619161 : tree type = TREE_TYPE (decl);
98 318619161 : if (VAR_P (decl)
99 119887573 : && (DECL_DECLARED_CONSTEXPR_P (decl)
100 83969720 : || var_in_constexpr_fn (decl))
101 371687974 : && !processing_template_decl)
102 : {
103 34688324 : tree stype = strip_array_types (type);
104 34688324 : if (CLASS_TYPE_P (stype) && !COMPLETE_TYPE_P (complete_type (stype)))
105 : /* Don't complain here, we'll complain about incompleteness
106 : when we try to initialize the variable. */;
107 34688318 : else if (!literal_type_p (type))
108 : {
109 23944 : if (DECL_DECLARED_CONSTEXPR_P (decl))
110 : {
111 53 : auto_diagnostic_group d;
112 53 : error_at (DECL_SOURCE_LOCATION (decl),
113 : "the type %qT of %<constexpr%> variable %qD "
114 : "is not literal", type, decl);
115 53 : explain_non_literal_class (type);
116 53 : decl = error_mark_node;
117 53 : }
118 23891 : else if (cxx_dialect < cxx23)
119 : {
120 18145 : if (!is_instantiation_of_constexpr (current_function_decl))
121 : {
122 8 : auto_diagnostic_group d;
123 8 : error_at (DECL_SOURCE_LOCATION (decl),
124 : "variable %qD of non-literal type %qT in "
125 : "%<constexpr%> function only available with "
126 : "%<-std=c++23%> or %<-std=gnu++23%>", decl, type);
127 8 : explain_non_literal_class (type);
128 8 : decl = error_mark_node;
129 8 : }
130 18145 : cp_function_chain->invalid_constexpr = true;
131 : }
132 : }
133 34664374 : else if (DECL_DECLARED_CONSTEXPR_P (decl)
134 34664374 : && variably_modified_type_p (type, NULL_TREE))
135 : {
136 4 : error_at (DECL_SOURCE_LOCATION (decl),
137 : "%<constexpr%> variable %qD has variably-modified "
138 : "type %qT", decl, type);
139 4 : decl = error_mark_node;
140 : }
141 : }
142 318619161 : return decl;
143 : }
144 :
145 : /* Issue a diagnostic with text GMSGID for constructs that are invalid in
146 : constexpr functions. CONSTEXPR_FUNDEF_P is true if we're checking
147 : a constexpr function body; if so, don't report hard errors and issue
148 : a pedwarn pre-C++23, or a warning in C++23, if requested by
149 : -Winvalid-constexpr. Otherwise, we're not in the context where we are
150 : checking if a function can be marked 'constexpr', so give a hard error. */
151 :
152 : ATTRIBUTE_GCC_DIAG(3,4)
153 : static bool
154 970 : constexpr_error (location_t location, bool constexpr_fundef_p,
155 : const char *gmsgid, ...)
156 : {
157 970 : diagnostics::diagnostic_info diagnostic;
158 970 : va_list ap;
159 970 : rich_location richloc (line_table, location);
160 970 : va_start (ap, gmsgid);
161 970 : bool ret;
162 970 : if (!constexpr_fundef_p)
163 : {
164 : /* Report an error that cannot be suppressed. */
165 707 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
166 : diagnostics::kind::error);
167 707 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
168 : }
169 263 : else if (warn_invalid_constexpr)
170 : {
171 148 : diagnostic_set_info (&diagnostic, gmsgid, &ap, &richloc,
172 148 : (cxx_dialect < cxx23
173 : ? diagnostics::kind::pedwarn
174 : : diagnostics::kind::warning));
175 148 : diagnostic.m_option_id = OPT_Winvalid_constexpr;
176 148 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
177 : }
178 : else
179 : ret = false;
180 970 : va_end (ap);
181 1940 : return ret;
182 970 : }
183 :
184 : struct constexpr_fundef_hasher : ggc_ptr_hash<constexpr_fundef>
185 : {
186 : static hashval_t hash (const constexpr_fundef *);
187 : static bool equal (const constexpr_fundef *, const constexpr_fundef *);
188 : };
189 :
190 : /* This table holds all constexpr function definitions seen in
191 : the current translation unit. */
192 :
193 : static GTY (()) hash_table<constexpr_fundef_hasher> *constexpr_fundef_table;
194 :
195 : /* Utility function used for managing the constexpr function table.
196 : Return true if the entries pointed to by P and Q are for the
197 : same constexpr function. */
198 :
199 : inline bool
200 592881204 : constexpr_fundef_hasher::equal (const constexpr_fundef *lhs,
201 : const constexpr_fundef *rhs)
202 : {
203 572643236 : return lhs->decl == rhs->decl;
204 : }
205 :
206 : /* Utility function used for managing the constexpr function table.
207 : Return a hash value for the entry pointed to by Q. */
208 :
209 : inline hashval_t
210 578700089 : constexpr_fundef_hasher::hash (const constexpr_fundef *fundef)
211 : {
212 578700089 : return DECL_UID (fundef->decl);
213 : }
214 :
215 : /* Return a previously saved definition of function FUN. */
216 :
217 : constexpr_fundef *
218 65801761 : retrieve_constexpr_fundef (tree fun)
219 : {
220 65801761 : if (constexpr_fundef_table == NULL)
221 : return NULL;
222 :
223 65797813 : constexpr_fundef fundef = { fun, NULL_TREE, NULL_TREE, NULL_TREE };
224 65797813 : return constexpr_fundef_table->find (&fundef);
225 : }
226 :
227 : /* Check whether the parameter and return types of FUN are valid for a
228 : constexpr function, and complain if COMPLAIN. */
229 :
230 : bool
231 29184028 : is_valid_constexpr_fn (tree fun, bool complain)
232 : {
233 29184028 : bool ret = true;
234 :
235 58368056 : if (DECL_INHERITED_CTOR (fun)
236 3410297 : && TREE_CODE (fun) == TEMPLATE_DECL)
237 : {
238 0 : ret = false;
239 0 : if (complain)
240 0 : error ("inherited constructor %qD is not %<constexpr%>",
241 0 : DECL_INHERITED_CTOR (fun));
242 : }
243 : else
244 : {
245 29184028 : for (tree parm = FUNCTION_FIRST_USER_PARM (fun);
246 58910033 : parm != NULL_TREE; parm = TREE_CHAIN (parm))
247 29726005 : if (!literal_type_p (TREE_TYPE (parm)))
248 : {
249 13337 : ret = false;
250 13337 : if (complain)
251 : {
252 22 : auto_diagnostic_group d;
253 22 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
254 : "invalid type for parameter %d of "
255 : "%<constexpr%> function %q+#D",
256 22 : DECL_PARM_INDEX (parm), fun))
257 14 : explain_non_literal_class (TREE_TYPE (parm));
258 22 : }
259 : }
260 : }
261 :
262 48184892 : if (LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun)) && cxx_dialect < cxx17)
263 : {
264 3 : ret = false;
265 3 : if (complain)
266 3 : inform (DECL_SOURCE_LOCATION (fun),
267 : "lambdas are implicitly %<constexpr%> only in C++17 and later");
268 : }
269 58368050 : else if (DECL_DESTRUCTOR_P (fun) && cxx_dialect < cxx20)
270 : {
271 0 : ret = false;
272 0 : if (complain)
273 0 : error_at (DECL_SOURCE_LOCATION (fun),
274 : "%<constexpr%> destructors only available with "
275 : "%<-std=c++20%> or %<-std=gnu++20%>");
276 : }
277 29184025 : else if (!DECL_CONSTRUCTOR_P (fun) && !DECL_DESTRUCTOR_P (fun))
278 : {
279 25184013 : tree rettype = TREE_TYPE (TREE_TYPE (fun));
280 25184013 : if (!literal_type_p (rettype))
281 : {
282 50598 : ret = false;
283 50598 : if (complain)
284 : {
285 16 : auto_diagnostic_group d;
286 16 : if (constexpr_error (input_location, /*constexpr_fundef_p*/true,
287 : "invalid return type %qT of %<constexpr%> "
288 : "function %q+D", rettype, fun))
289 9 : explain_non_literal_class (rettype);
290 16 : }
291 : }
292 :
293 : /* C++14 DR 1684 removed this restriction. */
294 25184013 : if (cxx_dialect < cxx14
295 32158 : && DECL_IOBJ_MEMBER_FUNCTION_P (fun)
296 25185129 : && !CLASSTYPE_LITERAL_P (DECL_CONTEXT (fun)))
297 : {
298 1 : ret = false;
299 1 : if (complain)
300 : {
301 1 : auto_diagnostic_group d;
302 1 : if (pedwarn (DECL_SOURCE_LOCATION (fun), OPT_Wpedantic,
303 : "enclosing class of %<constexpr%> non-static"
304 : " member function %q+#D is not a literal type",
305 : fun))
306 1 : explain_non_literal_class (DECL_CONTEXT (fun));
307 1 : }
308 : }
309 : }
310 4000012 : else if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fun)) && cxx_dialect < cxx26)
311 : {
312 69 : ret = false;
313 69 : if (complain)
314 : {
315 10 : if (DECL_CONSTRUCTOR_P (fun))
316 8 : error ("%<constexpr%> constructor in %q#T that has "
317 : "virtual base classes only available with "
318 8 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
319 : else
320 2 : error ("%<constexpr%> destructor in %q#T that has "
321 : "virtual base classes only available with "
322 2 : "%<-std=c++2c%> or %<-std=gnu++2c%>", DECL_CONTEXT (fun));
323 : }
324 : }
325 :
326 29184028 : return ret;
327 : }
328 :
329 : /* Subroutine of build_data_member_initialization. MEMBER is a COMPONENT_REF
330 : for a member of an anonymous aggregate, INIT is the initializer for that
331 : member, and VEC_OUTER is the vector of constructor elements for the class
332 : whose constructor we are processing. Add the initializer to the vector
333 : and return true to indicate success. */
334 :
335 : static bool
336 901 : build_anon_member_initialization (tree member, tree init,
337 : vec<constructor_elt, va_gc> **vec_outer)
338 : {
339 : /* MEMBER presents the relevant fields from the inside out, but we need
340 : to build up the initializer from the outside in so that we can reuse
341 : previously built CONSTRUCTORs if this is, say, the second field in an
342 : anonymous struct. So we use a vec as a stack. */
343 901 : auto_vec<tree, 2> fields;
344 1898 : do
345 : {
346 1898 : fields.safe_push (TREE_OPERAND (member, 1));
347 1898 : member = TREE_OPERAND (member, 0);
348 : }
349 1898 : while (ANON_AGGR_TYPE_P (TREE_TYPE (member))
350 3802 : && TREE_CODE (member) == COMPONENT_REF);
351 :
352 : /* VEC has the constructor elements vector for the context of FIELD.
353 : If FIELD is an anonymous aggregate, we will push inside it. */
354 : vec<constructor_elt, va_gc> **vec = vec_outer;
355 : tree field;
356 1898 : while (field = fields.pop(),
357 1898 : ANON_AGGR_TYPE_P (TREE_TYPE (field)))
358 : {
359 997 : tree ctor;
360 : /* If there is already an outer constructor entry for the anonymous
361 : aggregate FIELD, use it; otherwise, insert one. */
362 997 : if (vec_safe_is_empty (*vec)
363 198 : || (*vec)->last().index != field)
364 : {
365 895 : ctor = build_constructor (TREE_TYPE (field), NULL);
366 895 : CONSTRUCTOR_APPEND_ELT (*vec, field, ctor);
367 : }
368 : else
369 102 : ctor = (*vec)->last().value;
370 997 : vec = &CONSTRUCTOR_ELTS (ctor);
371 : }
372 :
373 : /* Now we're at the innermost field, the one that isn't an anonymous
374 : aggregate. Add its initializer to the CONSTRUCTOR and we're done. */
375 901 : gcc_assert (fields.is_empty());
376 901 : CONSTRUCTOR_APPEND_ELT (*vec, field, init);
377 :
378 901 : return true;
379 901 : }
380 :
381 : /* Subroutine of build_constexpr_constructor_member_initializers.
382 : The expression tree T represents a data member initialization
383 : in a (constexpr) constructor definition. Build a pairing of
384 : the data member with its initializer, and prepend that pair
385 : to the existing initialization pair INITS. */
386 :
387 : static bool
388 5565172 : build_data_member_initialization (tree t, vec<constructor_elt, va_gc> **vec)
389 : {
390 6195862 : tree member, init;
391 6195862 : if (TREE_CODE (t) == CLEANUP_POINT_EXPR)
392 2801460 : t = TREE_OPERAND (t, 0);
393 6195862 : if (TREE_CODE (t) == EXPR_STMT)
394 2800565 : t = TREE_OPERAND (t, 0);
395 6195862 : if (t == error_mark_node)
396 : return false;
397 6195853 : if (TREE_CODE (t) == STATEMENT_LIST)
398 : {
399 6995153 : for (tree stmt : tsi_range (t))
400 803120 : if (! build_data_member_initialization (stmt, vec))
401 9 : return false;
402 : return true;
403 : }
404 5568983 : if (TREE_CODE (t) == CLEANUP_STMT)
405 : {
406 : /* We can't see a CLEANUP_STMT in a constructor for a literal class,
407 : but we can in a constexpr constructor for a non-literal class. Just
408 : ignore it; either all the initialization will be constant, in which
409 : case the cleanup can't run, or it can't be constexpr.
410 : Still recurse into CLEANUP_BODY. */
411 600806 : return build_data_member_initialization (CLEANUP_BODY (t), vec);
412 : }
413 4968177 : if (TREE_CODE (t) == CONVERT_EXPR)
414 2795805 : t = TREE_OPERAND (t, 0);
415 4968177 : if (TREE_CODE (t) == INIT_EXPR)
416 : {
417 2687029 : member = TREE_OPERAND (t, 0);
418 2687029 : init = break_out_target_exprs (TREE_OPERAND (t, 1));
419 : }
420 2281148 : else if (TREE_CODE (t) == CALL_EXPR)
421 : {
422 1876606 : tree fn = get_callee_fndecl (t);
423 3753212 : if (!fn || !DECL_CONSTRUCTOR_P (fn))
424 : /* We're only interested in calls to subobject constructors. */
425 : return true;
426 1492764 : member = CALL_EXPR_ARG (t, 0);
427 : /* We don't use build_cplus_new here because it complains about
428 : abstract bases. Leaving the call unwrapped means that it has the
429 : wrong type, but cxx_eval_constant_expression doesn't care. */
430 1492764 : init = break_out_target_exprs (t);
431 : }
432 404542 : else if (TREE_CODE (t) == BIND_EXPR)
433 29884 : return build_data_member_initialization (BIND_EXPR_BODY (t), vec);
434 : else
435 : /* Don't add anything else to the CONSTRUCTOR. */
436 : return true;
437 4179793 : if (INDIRECT_REF_P (member))
438 58671 : member = TREE_OPERAND (member, 0);
439 4179793 : if (TREE_CODE (member) == NOP_EXPR)
440 : {
441 527247 : tree op = member;
442 527247 : STRIP_NOPS (op);
443 527247 : if (TREE_CODE (op) == ADDR_EXPR)
444 : {
445 587 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
446 : (TREE_TYPE (TREE_TYPE (op)),
447 : TREE_TYPE (TREE_TYPE (member))));
448 : /* Initializing a cv-qualified member; we need to look through
449 : the const_cast. */
450 : member = op;
451 : }
452 526660 : else if (op == current_class_ptr
453 1053189 : && (same_type_ignoring_top_level_qualifiers_p
454 526529 : (TREE_TYPE (TREE_TYPE (member)),
455 : current_class_type)))
456 : /* Delegating constructor. */
457 : member = op;
458 : else
459 : {
460 : /* This is an initializer for an empty base; keep it for now so
461 : we can check it in cxx_eval_bare_aggregate. */
462 455051 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (member))));
463 : }
464 : }
465 4179793 : if (TREE_CODE (member) == ADDR_EXPR)
466 1024775 : member = TREE_OPERAND (member, 0);
467 4179793 : if (TREE_CODE (member) == COMPONENT_REF)
468 : {
469 3629393 : tree aggr = TREE_OPERAND (member, 0);
470 3629393 : if (TREE_CODE (aggr) == VAR_DECL)
471 : /* Initializing a local variable, don't add anything. */
472 : return true;
473 3629391 : if (TREE_CODE (aggr) != COMPONENT_REF)
474 : /* Normal member initialization. */
475 3628487 : member = TREE_OPERAND (member, 1);
476 904 : else if (VAR_P (get_base_address (aggr)))
477 : /* Initializing a local variable, don't add anything. */
478 : return true;
479 901 : else if (ANON_AGGR_TYPE_P (TREE_TYPE (aggr)))
480 : /* Initializing a member of an anonymous union. */
481 901 : return build_anon_member_initialization (member, init, vec);
482 : else
483 : /* We're initializing a vtable pointer in a base. Leave it as
484 : COMPONENT_REF so we remember the path to get to the vfield. */
485 0 : gcc_assert (TREE_TYPE (member) == vtbl_ptr_type_node);
486 : }
487 :
488 : /* Value-initialization can produce multiple initializers for the
489 : same field; use the last one. */
490 5199257 : if (!vec_safe_is_empty (*vec) && (*vec)->last().index == member)
491 56 : (*vec)->last().value = init;
492 : else
493 4178831 : CONSTRUCTOR_APPEND_ELT (*vec, member, init);
494 : return true;
495 : }
496 :
497 : /* Subroutine of check_constexpr_ctor_body_1 and constexpr_fn_retval.
498 : In C++11 mode checks that the TYPE_DECLs in the BIND_EXPR_VARS of a
499 : BIND_EXPR conform to 7.1.5/3/4 on typedef and alias declarations. */
500 :
501 : static bool
502 2401 : check_constexpr_bind_expr_vars (tree t)
503 : {
504 2401 : gcc_assert (TREE_CODE (t) == BIND_EXPR);
505 :
506 3239 : for (tree var = BIND_EXPR_VARS (t); var; var = DECL_CHAIN (var))
507 850 : if (TREE_CODE (var) == TYPE_DECL
508 831 : && DECL_IMPLICIT_TYPEDEF_P (var)
509 872 : && !LAMBDA_TYPE_P (TREE_TYPE (var)))
510 : return false;
511 : return true;
512 : }
513 :
514 : /* Subroutine of check_constexpr_ctor_body. */
515 :
516 : static bool
517 4135 : check_constexpr_ctor_body_1 (tree last, tree list)
518 : {
519 4135 : switch (TREE_CODE (list))
520 : {
521 6 : case DECL_EXPR:
522 6 : if (TREE_CODE (DECL_EXPR_DECL (list)) == USING_DECL
523 6 : || TREE_CODE (DECL_EXPR_DECL (list)) == TYPE_DECL)
524 : return true;
525 : return false;
526 :
527 4 : case CLEANUP_POINT_EXPR:
528 4 : return check_constexpr_ctor_body (last, TREE_OPERAND (list, 0),
529 4 : /*complain=*/false);
530 :
531 2058 : case BIND_EXPR:
532 2058 : if (!check_constexpr_bind_expr_vars (list)
533 2058 : || !check_constexpr_ctor_body (last, BIND_EXPR_BODY (list),
534 : /*complain=*/false))
535 43 : return false;
536 : return true;
537 :
538 : case USING_STMT:
539 : case STATIC_ASSERT:
540 : case DEBUG_BEGIN_STMT:
541 : return true;
542 :
543 : default:
544 : return false;
545 : }
546 : }
547 :
548 : /* Make sure that there are no statements after LAST in the constructor
549 : body represented by LIST. */
550 :
551 : bool
552 5492697 : check_constexpr_ctor_body (tree last, tree list, bool complain)
553 : {
554 : /* C++14 doesn't require a constexpr ctor to have an empty body. */
555 5492697 : if (cxx_dialect >= cxx14)
556 : return true;
557 :
558 13772 : bool ok = true;
559 13772 : if (TREE_CODE (list) == STATEMENT_LIST)
560 : {
561 13745 : tree_stmt_iterator i = tsi_last (list);
562 17783 : for (; !tsi_end_p (i); tsi_prev (&i))
563 : {
564 15656 : tree t = tsi_stmt (i);
565 15656 : if (t == last)
566 : break;
567 4108 : if (!check_constexpr_ctor_body_1 (last, t))
568 : {
569 : ok = false;
570 : break;
571 : }
572 : }
573 : }
574 27 : else if (list != last
575 27 : && !check_constexpr_ctor_body_1 (last, list))
576 : ok = false;
577 13772 : if (!ok && complain)
578 : {
579 49 : pedwarn (input_location, OPT_Wc__14_extensions,
580 : "%<constexpr%> constructor does not have empty body");
581 49 : ok = true;
582 : }
583 : return ok;
584 : }
585 :
586 : /* V is a vector of constructor elements built up for the base and member
587 : initializers of a constructor for TYPE. They need to be in increasing
588 : offset order, which they might not be yet if TYPE has a primary base
589 : which is not first in the base-clause or a vptr and at least one base
590 : all of which are non-primary. */
591 :
592 : static vec<constructor_elt, va_gc> *
593 3337843 : sort_constexpr_mem_initializers (tree type, vec<constructor_elt, va_gc> *v)
594 : {
595 3337843 : tree pri = CLASSTYPE_PRIMARY_BINFO (type);
596 3337843 : tree field_type;
597 3337843 : unsigned i;
598 3337843 : constructor_elt *ce;
599 :
600 3337843 : if (pri)
601 60355 : field_type = BINFO_TYPE (pri);
602 3277488 : else if (TYPE_CONTAINS_VPTR_P (type))
603 34555 : field_type = vtbl_ptr_type_node;
604 : else
605 : return v;
606 :
607 : /* Find the element for the primary base or vptr and move it to the
608 : beginning of the vec. */
609 134128 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
610 99453 : if (TREE_TYPE (ce->index) == field_type)
611 : break;
612 :
613 115552 : if (i > 0 && i < vec_safe_length (v))
614 : {
615 22 : vec<constructor_elt, va_gc> &vref = *v;
616 22 : constructor_elt elt = vref[i];
617 44 : for (; i > 0; --i)
618 22 : vref[i] = vref[i-1];
619 22 : vref[0] = elt;
620 : }
621 :
622 : return v;
623 : }
624 :
625 : /* Build compile-time evalable representations of member-initializer list
626 : for a constexpr constructor. */
627 :
628 : static tree
629 3409433 : build_constexpr_constructor_member_initializers (tree type, tree body)
630 : {
631 3409433 : vec<constructor_elt, va_gc> *vec = NULL;
632 3409433 : bool ok = true;
633 5346577 : while (true)
634 5346577 : switch (TREE_CODE (body))
635 : {
636 1937073 : case MUST_NOT_THROW_EXPR:
637 1937073 : case EH_SPEC_BLOCK:
638 1937073 : case TRY_FINALLY_EXPR: // For C++26 postconditions.
639 1937073 : body = TREE_OPERAND (body, 0);
640 1937073 : break;
641 :
642 71 : case STATEMENT_LIST:
643 5346673 : for (tree stmt : tsi_range (body))
644 : {
645 145 : body = stmt;
646 145 : if (TREE_CODE (body) == BIND_EXPR)
647 : break;
648 : }
649 : break;
650 :
651 3409433 : case BIND_EXPR:
652 3409433 : body = BIND_EXPR_BODY (body);
653 3409433 : goto found;
654 :
655 0 : default:
656 0 : gcc_unreachable ();
657 : }
658 3409433 : found:
659 3409433 : if (TREE_CODE (body) == TRY_BLOCK)
660 : {
661 29 : body = TREE_OPERAND (body, 0);
662 29 : if (TREE_CODE (body) == BIND_EXPR)
663 29 : body = BIND_EXPR_BODY (body);
664 : }
665 3409433 : if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
666 : {
667 1871916 : body = TREE_OPERAND (body, 0);
668 1871916 : if (TREE_CODE (body) == EXPR_STMT)
669 1871906 : body = TREE_OPERAND (body, 0);
670 1871916 : if (TREE_CODE (body) == INIT_EXPR
671 1871916 : && (same_type_ignoring_top_level_qualifiers_p
672 0 : (TREE_TYPE (TREE_OPERAND (body, 0)),
673 : current_class_type)))
674 : {
675 : /* Trivial copy. */
676 0 : return TREE_OPERAND (body, 1);
677 : }
678 1871916 : ok = build_data_member_initialization (body, &vec);
679 : }
680 1537517 : else if (TREE_CODE (body) == STATEMENT_LIST)
681 : {
682 4421138 : for (tree stmt : tsi_range (body))
683 : {
684 2886879 : ok = build_data_member_initialization (stmt, &vec);
685 2886879 : if (!ok)
686 : break;
687 : }
688 : }
689 3257 : else if (EXPR_P (body))
690 3257 : ok = build_data_member_initialization (body, &vec);
691 : else
692 0 : gcc_assert (errorcount > 0);
693 3409433 : if (ok)
694 : {
695 3409424 : if (vec_safe_length (vec) > 0)
696 : {
697 : /* In a delegating constructor, return the target. */
698 3159292 : constructor_elt *ce = &(*vec)[0];
699 3159292 : if (ce->index == current_class_ptr)
700 : {
701 71581 : body = ce->value;
702 71581 : vec_free (vec);
703 71581 : return body;
704 : }
705 : }
706 3337843 : vec = sort_constexpr_mem_initializers (type, vec);
707 3337843 : return build_constructor (type, vec);
708 : }
709 : else
710 9 : return error_mark_node;
711 : }
712 :
713 : /* We have an expression tree T that represents a call, either CALL_EXPR
714 : or AGGR_INIT_EXPR. If the call is lexically to a named function,
715 : return the _DECL for that function. */
716 :
717 : static tree
718 443997596 : get_function_named_in_call (tree t)
719 : {
720 443997596 : tree callee = cp_get_callee (t);
721 443997596 : tree fun = cp_get_fndecl_from_callee (callee, /*fold*/false);
722 443997596 : return fun ? fun : callee;
723 : }
724 :
725 : /* Subroutine of check_constexpr_fundef. BODY is the body of a function
726 : declared to be constexpr, or a sub-statement thereof. Returns the
727 : return value if suitable, error_mark_node for a statement not allowed in
728 : a constexpr function, or NULL_TREE if no return value was found. */
729 :
730 : tree
731 69257 : constexpr_fn_retval (tree body)
732 : {
733 75969 : switch (TREE_CODE (body))
734 : {
735 18518 : case STATEMENT_LIST:
736 18518 : {
737 18518 : tree expr = NULL_TREE;
738 55484 : for (tree stmt : tsi_range (body))
739 : {
740 36995 : tree s = constexpr_fn_retval (stmt);
741 36995 : if (s == error_mark_node)
742 : return error_mark_node;
743 36968 : else if (s == NULL_TREE)
744 : /* Keep iterating. */;
745 18483 : else if (expr)
746 : /* Multiple return statements. */
747 : return error_mark_node;
748 : else
749 : expr = s;
750 : }
751 : return expr;
752 : }
753 :
754 32212 : case RETURN_EXPR:
755 32212 : return break_out_target_exprs (TREE_OPERAND (body, 0));
756 :
757 20 : case DECL_EXPR:
758 20 : {
759 20 : tree decl = DECL_EXPR_DECL (body);
760 20 : if (TREE_CODE (decl) == USING_DECL
761 : /* Accept __func__, __FUNCTION__, and __PRETTY_FUNCTION__. */
762 20 : || DECL_ARTIFICIAL (decl))
763 : return NULL_TREE;
764 6 : return error_mark_node;
765 : }
766 :
767 6375 : case CLEANUP_POINT_EXPR:
768 6375 : return constexpr_fn_retval (TREE_OPERAND (body, 0));
769 :
770 343 : case BIND_EXPR:
771 343 : if (!check_constexpr_bind_expr_vars (body))
772 6 : return error_mark_node;
773 337 : return constexpr_fn_retval (BIND_EXPR_BODY (body));
774 :
775 : case USING_STMT:
776 : case DEBUG_BEGIN_STMT:
777 : return NULL_TREE;
778 :
779 0 : case CALL_EXPR:
780 0 : {
781 0 : tree fun = get_function_named_in_call (body);
782 0 : if (fun != NULL_TREE
783 0 : && fndecl_built_in_p (fun, BUILT_IN_UNREACHABLE))
784 : return NULL_TREE;
785 : }
786 : /* Fallthru. */
787 :
788 30 : default:
789 30 : return error_mark_node;
790 : }
791 : }
792 :
793 : /* Subroutine of check_constexpr_fundef. BODY is the DECL_SAVED_TREE of
794 : FUN; do the necessary transformations to turn it into a single expression
795 : that we can store in the hash table. */
796 :
797 : static tree
798 28584474 : massage_constexpr_body (tree fun, tree body)
799 : {
800 57168948 : if (DECL_CONSTRUCTOR_P (fun))
801 3409433 : body = build_constexpr_constructor_member_initializers
802 3409433 : (DECL_CONTEXT (fun), body);
803 25175041 : else if (cxx_dialect < cxx14)
804 : {
805 32027 : if (TREE_CODE (body) == EH_SPEC_BLOCK)
806 1 : body = EH_SPEC_STMTS (body);
807 32027 : if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
808 20673 : body = TREE_OPERAND (body, 0);
809 32027 : body = constexpr_fn_retval (body);
810 : }
811 28584474 : return body;
812 : }
813 :
814 : /* CTYPE is a type constructed from BODY. Return true if some
815 : bases/fields are uninitialized, and complain if COMPLAIN. */
816 :
817 : static bool
818 3099758 : cx_check_missing_mem_inits (tree ctype, tree body, bool complain)
819 : {
820 : /* We allow uninitialized bases/fields in C++20. */
821 3099758 : if (cxx_dialect >= cxx20)
822 : return false;
823 :
824 13613 : unsigned nelts = 0;
825 :
826 13613 : if (body)
827 : {
828 13609 : if (TREE_CODE (body) != CONSTRUCTOR)
829 : return false;
830 13558 : nelts = CONSTRUCTOR_NELTS (body);
831 : }
832 13562 : tree field = TYPE_FIELDS (ctype);
833 :
834 13562 : if (TREE_CODE (ctype) == UNION_TYPE)
835 : {
836 110 : if (nelts == 0 && next_aggregate_field (field))
837 : {
838 2 : if (complain)
839 2 : error ("%<constexpr%> constructor for union %qT must "
840 : "initialize exactly one non-static data member", ctype);
841 2 : return true;
842 : }
843 108 : return false;
844 : }
845 :
846 : /* Iterate over the CONSTRUCTOR, checking any missing fields don't
847 : need an explicit initialization. */
848 : bool bad = false;
849 28616 : for (unsigned i = 0; i <= nelts; ++i)
850 : {
851 28616 : tree index = NULL_TREE;
852 28616 : if (i < nelts)
853 : {
854 15164 : index = CONSTRUCTOR_ELT (body, i)->index;
855 : /* Skip vptr adjustment represented with a COMPONENT_REF. */
856 15164 : if (TREE_CODE (index) != FIELD_DECL)
857 627 : continue;
858 : }
859 :
860 494177 : for (; field != index; field = DECL_CHAIN (field))
861 : {
862 466194 : tree ftype;
863 466194 : if (TREE_CODE (field) != FIELD_DECL)
864 930571 : continue;
865 1789 : if (DECL_UNNAMED_BIT_FIELD (field))
866 10 : continue;
867 : /* Artificial fields can be ignored unless they're bases. */
868 1779 : if (DECL_ARTIFICIAL (field) && !DECL_FIELD_IS_BASE (field))
869 6 : continue;
870 1773 : if (ANON_AGGR_TYPE_P (TREE_TYPE (field)))
871 : {
872 : /* Recurse to check the anonymous aggregate member. */
873 8 : bad |= cx_check_missing_mem_inits
874 4 : (TREE_TYPE (field), NULL_TREE, complain);
875 4 : if (bad && !complain)
876 6 : return true;
877 4 : continue;
878 : }
879 1769 : ftype = TREE_TYPE (field);
880 1769 : if (!ftype || !TYPE_P (ftype) || !COMPLETE_TYPE_P (ftype))
881 : /* A flexible array can't be intialized here, so don't complain
882 : that it isn't. */
883 1 : continue;
884 1768 : if (is_empty_field (field))
885 : /* An empty field doesn't need an initializer. */
886 1733 : continue;
887 35 : ftype = strip_array_types (ftype);
888 35 : if (type_has_constexpr_default_constructor (ftype))
889 : {
890 : /* It's OK to skip a member with a trivial constexpr ctor.
891 : A constexpr ctor that isn't trivial should have been
892 : added in by now. */
893 7 : gcc_checking_assert (!TYPE_HAS_COMPLEX_DFLT (ftype)
894 : || errorcount != 0);
895 7 : continue;
896 : }
897 28 : if (!complain)
898 : return true;
899 22 : auto_diagnostic_group d;
900 22 : error ("member %qD must be initialized by mem-initializer "
901 : "in %<constexpr%> constructor", field);
902 22 : inform (DECL_SOURCE_LOCATION (field), "declared here");
903 22 : bad = true;
904 22 : }
905 27983 : if (field == NULL_TREE)
906 : break;
907 :
908 14537 : if (ANON_AGGR_TYPE_P (TREE_TYPE (index)))
909 : {
910 : /* Check the anonymous aggregate initializer is valid. */
911 76 : bad |= cx_check_missing_mem_inits
912 38 : (TREE_TYPE (index), CONSTRUCTOR_ELT (body, i)->value, complain);
913 38 : if (bad && !complain)
914 : return true;
915 : }
916 14537 : field = DECL_CHAIN (field);
917 : }
918 :
919 : return bad;
920 : }
921 :
922 : /* We are processing the definition of the constexpr function FUN.
923 : Check that its body fulfills the apropriate requirements and
924 : enter it in the constexpr function definition table. */
925 :
926 : void
927 157955035 : maybe_save_constexpr_fundef (tree fun)
928 : {
929 157955035 : if (processing_template_decl
930 65153651 : || cp_function_chain->invalid_constexpr
931 223090601 : || (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun)))
932 129370826 : return;
933 :
934 : /* With -fimplicit-constexpr, try to make inlines constexpr. We'll
935 : actually set DECL_DECLARED_CONSTEXPR_P below if the checks pass. */
936 49766630 : bool implicit = false;
937 49766630 : if (flag_implicit_constexpr)
938 : {
939 19 : if (DECL_DELETING_DESTRUCTOR_P (fun)
940 19 : && decl_implicit_constexpr_p (DECL_CLONED_FUNCTION (fun)))
941 : /* Don't inherit implicit constexpr from the non-deleting
942 : destructor. */
943 0 : DECL_DECLARED_CONSTEXPR_P (fun) = false;
944 :
945 19 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
946 16 : && DECL_DECLARED_INLINE_P (fun)
947 31 : && !lookup_attribute ("noinline", DECL_ATTRIBUTES (fun)))
948 : implicit = true;
949 : }
950 :
951 49766630 : if (!DECL_DECLARED_CONSTEXPR_P (fun) && !implicit)
952 : return;
953 :
954 28639614 : bool complain = !DECL_GENERATED_P (fun) && !implicit;
955 :
956 28639614 : if (!is_valid_constexpr_fn (fun, complain))
957 : return;
958 :
959 28584409 : tree massaged = massage_constexpr_body (fun, DECL_SAVED_TREE (fun));
960 28584409 : if (massaged == NULL_TREE || massaged == error_mark_node)
961 : {
962 72 : if (!DECL_CONSTRUCTOR_P (fun) && complain)
963 22 : error ("body of %<constexpr%> function %qD not a return-statement",
964 : fun);
965 36 : return;
966 : }
967 :
968 28584373 : bool potential = potential_rvalue_constant_expression (massaged);
969 28584373 : if (!potential && complain)
970 224 : require_potential_rvalue_constant_expression_fncheck (massaged);
971 :
972 31993785 : if (DECL_CONSTRUCTOR_P (fun) && potential
973 31991714 : && !DECL_DEFAULTED_FN (fun))
974 : {
975 3099704 : if (cx_check_missing_mem_inits (DECL_CONTEXT (fun),
976 : massaged, complain))
977 : potential = false;
978 3099678 : else if (cxx_dialect > cxx11)
979 : {
980 : /* What we got from massage_constexpr_body is pretty much just the
981 : ctor-initializer, also check the body. */
982 3094881 : massaged = DECL_SAVED_TREE (fun);
983 3094881 : potential = potential_rvalue_constant_expression (massaged);
984 3094881 : if (!potential && complain)
985 16 : require_potential_rvalue_constant_expression_fncheck (massaged);
986 : }
987 : }
988 :
989 28584373 : if (!potential && complain
990 : /* If -Wno-invalid-constexpr was specified, we haven't complained
991 : about non-constant expressions yet. Register the function and
992 : complain in explain_invalid_constexpr_fn if the function is
993 : called. */
994 260 : && warn_invalid_constexpr != 0)
995 : return;
996 :
997 28584219 : if (implicit)
998 : {
999 12 : if (potential)
1000 : {
1001 2 : DECL_DECLARED_CONSTEXPR_P (fun) = true;
1002 2 : DECL_LANG_SPECIFIC (fun)->u.fn.implicit_constexpr = true;
1003 4 : if (DECL_CONSTRUCTOR_P (fun))
1004 0 : TYPE_HAS_CONSTEXPR_CTOR (DECL_CONTEXT (fun)) = true;
1005 : }
1006 : else
1007 : /* Don't bother keeping the pre-generic body of unsuitable functions
1008 : not explicitly declared constexpr. */
1009 : return;
1010 : }
1011 :
1012 28584209 : constexpr_fundef entry = {fun, NULL_TREE, NULL_TREE, NULL_TREE};
1013 28584209 : bool clear_ctx = false;
1014 28584209 : if (DECL_RESULT (fun) && DECL_CONTEXT (DECL_RESULT (fun)) == NULL_TREE)
1015 : {
1016 28584209 : clear_ctx = true;
1017 28584209 : DECL_CONTEXT (DECL_RESULT (fun)) = fun;
1018 : }
1019 28584209 : tree saved_fn = current_function_decl;
1020 28584209 : current_function_decl = fun;
1021 28584209 : entry.body = copy_fn (entry.decl, entry.parms, entry.result);
1022 28584209 : current_function_decl = saved_fn;
1023 28584209 : if (clear_ctx)
1024 28584209 : DECL_CONTEXT (DECL_RESULT (entry.decl)) = NULL_TREE;
1025 28584209 : if (!potential)
1026 : /* For a template instantiation, we want to remember the pre-generic body
1027 : for explain_invalid_constexpr_fn, but do tell cxx_eval_call_expression
1028 : that it doesn't need to bother trying to expand the function. */
1029 59401 : entry.result = error_mark_node;
1030 :
1031 28584209 : register_constexpr_fundef (entry);
1032 : }
1033 :
1034 : /* BODY is a validated and massaged definition of a constexpr
1035 : function. Register it in the hash table. */
1036 :
1037 : void
1038 28619836 : register_constexpr_fundef (const constexpr_fundef &value)
1039 : {
1040 : /* Create the constexpr function table if necessary. */
1041 28619836 : if (constexpr_fundef_table == NULL)
1042 26165 : constexpr_fundef_table
1043 26165 : = hash_table<constexpr_fundef_hasher>::create_ggc (101);
1044 :
1045 28619836 : constexpr_fundef **slot = constexpr_fundef_table->find_slot
1046 28619836 : (const_cast<constexpr_fundef *> (&value), INSERT);
1047 :
1048 28619836 : gcc_assert (*slot == NULL);
1049 28619836 : *slot = ggc_alloc<constexpr_fundef> ();
1050 28619836 : **slot = value;
1051 28619836 : }
1052 :
1053 : /* FUN is a non-constexpr (or, with -Wno-invalid-constexpr, a constexpr
1054 : function called in a context that requires a constant expression).
1055 : If it comes from a constexpr template, explain why the instantiation
1056 : isn't constexpr. Otherwise, explain why the function cannot be used
1057 : in a constexpr context. */
1058 :
1059 : void
1060 793 : explain_invalid_constexpr_fn (tree fun)
1061 : {
1062 793 : static hash_set<tree> *diagnosed;
1063 793 : tree body;
1064 :
1065 : /* Don't try to explain a function we already complained about. */
1066 793 : if (function *f = DECL_STRUCT_FUNCTION (fun))
1067 606 : if (f->language->erroneous)
1068 793 : return;
1069 :
1070 : /* In C++23, a function marked 'constexpr' may not actually be a constant
1071 : expression. We haven't diagnosed the problem yet: -Winvalid-constexpr
1072 : wasn't enabled. The function was called, so diagnose why it cannot be
1073 : used in a constant expression. */
1074 744 : if (warn_invalid_constexpr == 0 && DECL_DECLARED_CONSTEXPR_P (fun))
1075 : /* Go on. */;
1076 : /* Only diagnose defaulted functions, lambdas, or instantiations. */
1077 716 : else if (!DECL_DEFAULTED_FN (fun)
1078 948 : && !LAMBDA_TYPE_P (CP_DECL_CONTEXT (fun))
1079 685 : && !(flag_implicit_constexpr
1080 8 : && !DECL_DECLARED_CONSTEXPR_P (fun)
1081 8 : && DECL_DECLARED_INLINE_P (fun))
1082 1396 : && !is_instantiation_of_constexpr (fun))
1083 : {
1084 662 : inform (DECL_SOURCE_LOCATION (fun), "%qD declared here", fun);
1085 3 : if (flag_implicit_constexpr && !maybe_constexpr_fn (fun)
1086 665 : && decl_defined_p (fun))
1087 3 : inform (DECL_SOURCE_LOCATION (fun),
1088 : "%<-fimplicit-constexpr%> only affects %<inline%> functions");
1089 662 : return;
1090 : }
1091 82 : if (diagnosed == NULL)
1092 65 : diagnosed = new hash_set<tree>;
1093 82 : if (diagnosed->add (fun))
1094 : /* Already explained. */
1095 : return;
1096 :
1097 81 : iloc_sentinel ils = input_location;
1098 81 : if (!lambda_static_thunk_p (fun))
1099 : {
1100 : /* Diagnostics should completely ignore the static thunk, so leave
1101 : input_location set to our caller's location. */
1102 75 : input_location = DECL_SOURCE_LOCATION (fun);
1103 75 : inform (input_location,
1104 : "%qD is not usable as a %<constexpr%> function because:", fun);
1105 : }
1106 : /* First check the declaration. */
1107 81 : if (is_valid_constexpr_fn (fun, true))
1108 : {
1109 : /* Then if it's OK, the body. */
1110 75 : if (!DECL_DECLARED_CONSTEXPR_P (fun)
1111 75 : && DECL_DEFAULTED_FN (fun))
1112 10 : explain_implicit_non_constexpr (fun);
1113 : else
1114 : {
1115 65 : if (constexpr_fundef *fd = retrieve_constexpr_fundef (fun))
1116 46 : body = fd->body;
1117 : else
1118 19 : body = DECL_SAVED_TREE (fun);
1119 65 : tree massaged = massage_constexpr_body (fun, body);
1120 65 : require_potential_rvalue_constant_expression (massaged);
1121 130 : if (DECL_CONSTRUCTOR_P (fun))
1122 : {
1123 12 : cx_check_missing_mem_inits (DECL_CONTEXT (fun), massaged, true);
1124 12 : if (cxx_dialect > cxx11)
1125 : /* Also check the body, not just the ctor-initializer. */
1126 10 : require_potential_rvalue_constant_expression (body);
1127 : }
1128 53 : else if (massaged == NULL_TREE || massaged == error_mark_node)
1129 1 : error ("body of %<constexpr%> function %qD not a return-statement",
1130 : fun);
1131 : }
1132 : }
1133 81 : }
1134 :
1135 : /* Objects of this type represent calls to constexpr functions
1136 : along with the bindings of parameters to their arguments, for
1137 : the purpose of compile time evaluation. */
1138 :
1139 : struct GTY((for_user)) constexpr_call {
1140 : /* Description of the constexpr function definition. */
1141 : constexpr_fundef *fundef = nullptr;
1142 : /* Parameter bindings environment. A TREE_VEC of arguments. */
1143 : tree bindings = NULL_TREE;
1144 : /* Result of the call, indexed by the value of
1145 : constexpr_ctx::manifestly_const_eval.
1146 : unknown_type_node means the call is being evaluated.
1147 : error_mark_node means that the evaluation was erroneous or otherwise
1148 : uncacheable (e.g. because it depends on the caller).
1149 : Otherwise, the actual value of the call. */
1150 : tree results[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
1151 : /* The hash of this call; we remember it here to avoid having to
1152 : recalculate it when expanding the hash table. */
1153 : hashval_t hash = 0;
1154 :
1155 : /* The result slot corresponding to the given mce_value. */
1156 48968029 : tree& result (mce_value mce) { return results[1 + int(mce)]; }
1157 : };
1158 :
1159 : struct constexpr_call_hasher : ggc_ptr_hash<constexpr_call>
1160 : {
1161 : static hashval_t hash (constexpr_call *);
1162 : static bool equal (constexpr_call *, constexpr_call *);
1163 : };
1164 :
1165 : enum constexpr_switch_state {
1166 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1167 : and default: label for that switch has not been seen yet. */
1168 : css_default_not_seen,
1169 : /* Used when processing a switch for the first time by cxx_eval_switch_expr
1170 : and default: label for that switch has been seen already. */
1171 : css_default_seen,
1172 : /* Used when processing a switch for the second time by
1173 : cxx_eval_switch_expr, where default: label should match. */
1174 : css_default_processing
1175 : };
1176 :
1177 : /* The constexpr expansion context part which needs one instance per
1178 : cxx_eval_outermost_constant_expr invocation. VALUES is a map of values of
1179 : variables initialized within the expression. */
1180 :
1181 : class constexpr_global_ctx {
1182 : /* Values for any temporaries or local variables within the
1183 : constant-expression. Objects outside their lifetime have
1184 : value 'void_node'. */
1185 : hash_map<tree,tree> values;
1186 : public:
1187 : /* Number of cxx_eval_constant_expression calls (except skipped ones,
1188 : on simple constants or location wrappers) encountered during current
1189 : cxx_eval_outermost_constant_expr call. */
1190 : HOST_WIDE_INT constexpr_ops_count;
1191 : /* Heap VAR_DECLs created during the evaluation of the outermost constant
1192 : expression. */
1193 : auto_vec<tree, 16> heap_vars;
1194 : /* Vector of caught exceptions, including exceptions still not active at
1195 : the start of a handler (those are immediately followed up by HANDLER_TYPE
1196 : until __cxa_begin_catch finishes). */
1197 : auto_vec<tree, 2> caught_exceptions;
1198 : /* Cleanups that need to be evaluated at the end of CLEANUP_POINT_EXPR. */
1199 : vec<tree> *cleanups;
1200 : /* If non-null, only allow modification of existing values of the variables
1201 : in this set. Set by modifiable_tracker, below. */
1202 : hash_set<tree> *modifiable;
1203 : /* If cxx_eval_outermost_constant_expr is called on the consteval block
1204 : operator (), this is the FUNCTION_DECL of that operator (). */
1205 : tree consteval_block;
1206 : /* Number of heap VAR_DECL deallocations. */
1207 : unsigned heap_dealloc_count;
1208 : /* Number of uncaught exceptions. */
1209 : unsigned uncaught_exceptions;
1210 : /* A contract statement that failed or was not constant, we only store the
1211 : first one that fails. */
1212 : tree contract_statement;
1213 : /* [basic.contract.eval]/7.3 if this expression would otherwise be constant
1214 : then a non-const contract makes the program ill-formed. */
1215 : bool contract_condition_non_const;
1216 : /* Some metafunctions aren't dependent just on their arguments, but also
1217 : on various other dependencies, e.g. has_identifier on a function parameter
1218 : reflection can change depending on further declarations of corresponding
1219 : function, is_complete_type depends on type definitions and template
1220 : specializations in between the calls, define_aggregate even defines
1221 : class types, etc. Thus, we need to arrange for calls which call
1222 : at least some metafunctions to be non-cacheable, because their behavior
1223 : might not be the same. Until we figure out which exact metafunctions
1224 : need this and which don't, do it for all of them. */
1225 : bool metafns_called;
1226 :
1227 : /* Constructor. */
1228 509355384 : constexpr_global_ctx ()
1229 1018710768 : : constexpr_ops_count (0), cleanups (NULL), modifiable (nullptr),
1230 509355384 : consteval_block (NULL_TREE), heap_dealloc_count (0),
1231 509355384 : uncaught_exceptions (0), contract_statement (NULL_TREE),
1232 509355384 : contract_condition_non_const (false), metafns_called (false) {}
1233 :
1234 314488462 : bool is_outside_lifetime (tree t)
1235 : {
1236 314488462 : if (tree *p = values.get (t))
1237 101243089 : if (*p == void_node)
1238 189 : return true;
1239 : return false;
1240 : }
1241 381732969 : tree get_value (tree t)
1242 : {
1243 381732969 : if (tree *p = values.get (t))
1244 152546165 : if (*p != void_node)
1245 149294174 : return *p;
1246 : return NULL_TREE;
1247 : }
1248 64818430 : tree *get_value_ptr (tree t, bool initializing)
1249 : {
1250 64818430 : if (modifiable && !modifiable->contains (t))
1251 : return nullptr;
1252 64818419 : if (tree *p = values.get (t))
1253 : {
1254 64813179 : if (*p != void_node)
1255 : return p;
1256 48 : else if (initializing)
1257 : {
1258 6 : *p = NULL_TREE;
1259 6 : return p;
1260 : }
1261 : }
1262 : return nullptr;
1263 : }
1264 171166902 : void put_value (tree t, tree v)
1265 : {
1266 171166902 : bool already_in_map = values.put (t, v);
1267 171166902 : if (!already_in_map && modifiable)
1268 30 : modifiable->add (t);
1269 171166902 : }
1270 126635551 : void destroy_value (tree t)
1271 : {
1272 126635551 : if (TREE_CODE (t) == VAR_DECL
1273 126635551 : || TREE_CODE (t) == PARM_DECL
1274 : || TREE_CODE (t) == RESULT_DECL)
1275 126629253 : values.put (t, void_node);
1276 : else
1277 6298 : values.remove (t);
1278 126635551 : }
1279 8571936 : void clear_value (tree t)
1280 : {
1281 17143872 : values.remove (t);
1282 : }
1283 : };
1284 :
1285 : /* Helper class for constexpr_global_ctx. In some cases we want to avoid
1286 : side-effects from evaluation of a particular subexpression of a
1287 : constant-expression. In such cases we use modifiable_tracker to prevent
1288 : modification of variables created outside of that subexpression.
1289 :
1290 : ??? We could change the hash_set to a hash_map, allow and track external
1291 : modifications, and roll them back in the destructor. It's not clear to me
1292 : that this would be worthwhile. */
1293 :
1294 : class modifiable_tracker
1295 : {
1296 : hash_set<tree> set;
1297 : constexpr_global_ctx *global;
1298 : public:
1299 173449 : modifiable_tracker (constexpr_global_ctx *g): global(g)
1300 : {
1301 173449 : global->modifiable = &set;
1302 173449 : }
1303 173449 : ~modifiable_tracker ()
1304 : {
1305 173479 : for (tree t: set)
1306 30 : global->clear_value (t);
1307 173449 : global->modifiable = nullptr;
1308 173449 : }
1309 : };
1310 :
1311 : /* The constexpr expansion context. CALL is the current function
1312 : expansion, CTOR is the current aggregate initializer, OBJECT is the
1313 : object being initialized by CTOR, either a VAR_DECL or a _REF. */
1314 :
1315 : struct constexpr_ctx {
1316 : /* The part of the context that needs to be unique to the whole
1317 : cxx_eval_outermost_constant_expr invocation. */
1318 : constexpr_global_ctx *global;
1319 : /* The innermost call we're evaluating. */
1320 : constexpr_call *call;
1321 : /* SAVE_EXPRs and TARGET_EXPR_SLOT vars of TARGET_EXPRs that we've seen
1322 : within the current LOOP_EXPR. NULL if we aren't inside a loop. */
1323 : vec<tree> *save_exprs;
1324 : /* The CONSTRUCTOR we're currently building up for an aggregate
1325 : initializer. */
1326 : tree ctor;
1327 : /* The object we're building the CONSTRUCTOR for. */
1328 : tree object;
1329 : /* If inside SWITCH_EXPR. */
1330 : constexpr_switch_state *css_state;
1331 : /* The aggregate initialization context inside which this one is nested. This
1332 : is used by lookup_placeholder to resolve PLACEHOLDER_EXPRs. */
1333 : const constexpr_ctx *parent;
1334 :
1335 : /* Whether we should error on a non-constant expression or fail quietly.
1336 : This flag needs to be here, but some of the others could move to global
1337 : if they get larger than a word. */
1338 : bool quiet;
1339 : /* Whether we are strictly conforming to constant expression rules or
1340 : trying harder to get a constant value. */
1341 : bool strict;
1342 : /* Whether __builtin_is_constant_evaluated () should be true. */
1343 : mce_value manifestly_const_eval;
1344 : };
1345 :
1346 : /* Return ctx->quiet. For use in reflect.cc. */
1347 :
1348 : bool
1349 294 : cxx_constexpr_quiet_p (const constexpr_ctx *ctx)
1350 : {
1351 294 : return ctx->quiet;
1352 : }
1353 :
1354 : /* Return ctx->manifestly_const_eval. For use in reflect.cc. */
1355 :
1356 : mce_value
1357 198 : cxx_constexpr_manifestly_const_eval (const constexpr_ctx *ctx)
1358 : {
1359 198 : return ctx->manifestly_const_eval;
1360 : }
1361 :
1362 : /* Return ctx->call->fundef->decl or NULL_TREE. For use in
1363 : reflect.cc. */
1364 :
1365 : tree
1366 1046 : cxx_constexpr_caller (const constexpr_ctx *ctx)
1367 : {
1368 1046 : if (ctx->call)
1369 468 : return ctx->call->fundef->decl;
1370 : else
1371 : return NULL_TREE;
1372 : }
1373 :
1374 : /* Return ctx->global->consteval_block. For use in reflect.cc. */
1375 :
1376 : tree
1377 79 : cxx_constexpr_consteval_block (const constexpr_ctx *ctx)
1378 : {
1379 79 : return ctx->global->consteval_block;
1380 : }
1381 :
1382 : /* Predicates for the meaning of *jump_target. */
1383 :
1384 : static bool
1385 65302814 : returns (tree *jump_target)
1386 : {
1387 11351628 : return *jump_target && TREE_CODE (*jump_target) == RETURN_EXPR;
1388 : }
1389 :
1390 : static bool
1391 61602949 : breaks (tree *jump_target)
1392 : {
1393 61602949 : return (*jump_target
1394 61602949 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1395 55 : && LABEL_DECL_BREAK (*jump_target))
1396 916179 : || TREE_CODE (*jump_target) == BREAK_STMT
1397 875964 : || TREE_CODE (*jump_target) == EXIT_EXPR));
1398 : }
1399 :
1400 : static bool
1401 86656831 : continues (tree *jump_target)
1402 : {
1403 86656831 : return (*jump_target
1404 86656831 : && ((TREE_CODE (*jump_target) == LABEL_DECL
1405 91 : && LABEL_DECL_CONTINUE (*jump_target))
1406 893921 : || TREE_CODE (*jump_target) == CONTINUE_STMT));
1407 : }
1408 :
1409 : static bool
1410 9105840 : switches (tree *jump_target)
1411 : {
1412 41978 : return *jump_target && TREE_CODE (*jump_target) == INTEGER_CST;
1413 : }
1414 :
1415 : static bool
1416 1517987569 : throws (tree *jump_target)
1417 : {
1418 : /* void_node is for use in potential_constant_expression_1, otherwise
1419 : it should an artificial VAR_DECL created by constant evaluation
1420 : of __cxa_allocate_exception (). */
1421 111183248 : return (*jump_target && (VAR_P (*jump_target) || *jump_target == void_node));
1422 : }
1423 :
1424 : /* True if the constexpr relaxations afforded by P2280R4 for unknown
1425 : references and objects are in effect. */
1426 :
1427 : static bool
1428 207397808 : p2280_active_p (const constexpr_ctx *ctx)
1429 : {
1430 28427422 : if (ctx->manifestly_const_eval != mce_true)
1431 : /* Disable these relaxations during speculative constexpr folding,
1432 : as it can significantly increase compile time/memory use
1433 : (PR119387). */
1434 : return false;
1435 :
1436 : /* P2280R4 was accepted as a DR against C++11. */
1437 0 : return cxx_dialect >= cxx11;
1438 : }
1439 :
1440 : /* Remove T from the global values map, checking for attempts to destroy
1441 : a value that has already finished its lifetime. */
1442 :
1443 : static void
1444 126478209 : destroy_value_checked (const constexpr_ctx* ctx, tree t, bool *non_constant_p)
1445 : {
1446 126478209 : if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
1447 : return;
1448 :
1449 : /* Don't error again here if we've already reported a problem. */
1450 126478192 : if (!*non_constant_p
1451 101742842 : && DECL_P (t)
1452 : /* Non-trivial destructors have their lifetimes ended explicitly
1453 : with a clobber, so don't worry about it here. */
1454 101736728 : && (!TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (t))
1455 : /* ...except parameters are remapped in cxx_eval_call_expression,
1456 : and the destructor call during cleanup won't be able to tell that
1457 : this value has already been destroyed, so complain now. This is
1458 : not quite unobservable, but is extremely unlikely to crop up in
1459 : practice; see g++.dg/cpp2a/constexpr-lifetime2.C. */
1460 411776 : || TREE_CODE (t) == PARM_DECL)
1461 227803816 : && ctx->global->is_outside_lifetime (t))
1462 : {
1463 54 : if (!ctx->quiet)
1464 : {
1465 18 : auto_diagnostic_group d;
1466 18 : error ("destroying %qE outside its lifetime", t);
1467 18 : inform (DECL_SOURCE_LOCATION (t), "declared here");
1468 18 : }
1469 54 : *non_constant_p = true;
1470 : }
1471 126478192 : ctx->global->destroy_value (t);
1472 : }
1473 :
1474 : /* This internal flag controls whether we should avoid doing anything during
1475 : constexpr evaluation that would cause extra DECL_UID generation, such as
1476 : template instantiation and function body copying. */
1477 :
1478 : static bool uid_sensitive_constexpr_evaluation_value;
1479 :
1480 : /* An internal counter that keeps track of the number of times
1481 : uid_sensitive_constexpr_evaluation_p returned true. */
1482 :
1483 : static unsigned uid_sensitive_constexpr_evaluation_true_counter;
1484 :
1485 : /* The accessor for uid_sensitive_constexpr_evaluation_value which also
1486 : increments the corresponding counter. */
1487 :
1488 : static bool
1489 8201116 : uid_sensitive_constexpr_evaluation_p ()
1490 : {
1491 8160131 : if (uid_sensitive_constexpr_evaluation_value)
1492 : {
1493 251808 : ++uid_sensitive_constexpr_evaluation_true_counter;
1494 251802 : return true;
1495 : }
1496 : else
1497 : return false;
1498 : }
1499 :
1500 : /* The default constructor for uid_sensitive_constexpr_evaluation_sentinel
1501 : enables the internal flag for uid_sensitive_constexpr_evaluation_p
1502 : during the lifetime of the sentinel object. Upon its destruction, the
1503 : previous value of uid_sensitive_constexpr_evaluation_p is restored. */
1504 :
1505 76406324 : uid_sensitive_constexpr_evaluation_sentinel
1506 76406324 : ::uid_sensitive_constexpr_evaluation_sentinel ()
1507 76406324 : : ovr (uid_sensitive_constexpr_evaluation_value, true)
1508 : {
1509 76406324 : }
1510 :
1511 : /* The default constructor for uid_sensitive_constexpr_evaluation_checker
1512 : records the current number of times that uid_sensitive_constexpr_evaluation_p
1513 : has been called and returned true. */
1514 :
1515 3601774591 : uid_sensitive_constexpr_evaluation_checker
1516 3601774591 : ::uid_sensitive_constexpr_evaluation_checker ()
1517 3601774591 : : saved_counter (uid_sensitive_constexpr_evaluation_true_counter)
1518 : {
1519 3601774591 : }
1520 :
1521 : /* Returns true iff uid_sensitive_constexpr_evaluation_p is true, and
1522 : some constexpr evaluation was restricted due to u_s_c_e_p being called
1523 : and returning true during the lifetime of this checker object. */
1524 :
1525 : bool
1526 2388640765 : uid_sensitive_constexpr_evaluation_checker::evaluation_restricted_p () const
1527 : {
1528 2388640765 : return (uid_sensitive_constexpr_evaluation_value
1529 2388640765 : && saved_counter != uid_sensitive_constexpr_evaluation_true_counter);
1530 : }
1531 :
1532 :
1533 : /* A table of all constexpr calls that have been evaluated by the
1534 : compiler in this translation unit. */
1535 :
1536 : static GTY (()) hash_table<constexpr_call_hasher> *constexpr_call_table;
1537 :
1538 : /* Compute a hash value for a constexpr call representation. */
1539 :
1540 : inline hashval_t
1541 149040350 : constexpr_call_hasher::hash (constexpr_call *info)
1542 : {
1543 149040350 : return info->hash;
1544 : }
1545 :
1546 : /* Return true if the objects pointed to by P and Q represent calls
1547 : to the same constexpr function with the same arguments.
1548 : Otherwise, return false. */
1549 :
1550 : bool
1551 151040182 : constexpr_call_hasher::equal (constexpr_call *lhs, constexpr_call *rhs)
1552 : {
1553 151040182 : if (lhs == rhs)
1554 : return true;
1555 151040182 : if (lhs->hash != rhs->hash)
1556 : return false;
1557 20237968 : if (!constexpr_fundef_hasher::equal (lhs->fundef, rhs->fundef))
1558 : return false;
1559 20237968 : return cp_tree_equal (lhs->bindings, rhs->bindings);
1560 : }
1561 :
1562 : /* Initialize the constexpr call table, if needed. */
1563 :
1564 : static void
1565 25500785 : maybe_initialize_constexpr_call_table (void)
1566 : {
1567 25500785 : if (constexpr_call_table == NULL)
1568 17242 : constexpr_call_table = hash_table<constexpr_call_hasher>::create_ggc (101);
1569 25500785 : }
1570 :
1571 : /* During constexpr CALL_EXPR evaluation, to avoid issues with sharing when
1572 : a function happens to get called recursively, we unshare the callee
1573 : function's body and evaluate this unshared copy instead of evaluating the
1574 : original body.
1575 :
1576 : FUNDEF_COPIES_TABLE is a per-function freelist of these unshared function
1577 : copies. The underlying data structure of FUNDEF_COPIES_TABLE is a hash_map
1578 : that's keyed off of the original FUNCTION_DECL and whose value is a
1579 : TREE_LIST of this function's unused copies awaiting reuse.
1580 :
1581 : This is not GC-deletable to avoid GC affecting UID generation. */
1582 :
1583 : static GTY(()) decl_tree_map *fundef_copies_table;
1584 :
1585 : /* Reuse a copy or create a new unshared copy of the function FUN.
1586 : Return this copy. We use a TREE_LIST whose PURPOSE is body, VALUE
1587 : is parms, TYPE is result. */
1588 :
1589 : static tree
1590 49196528 : get_fundef_copy (constexpr_fundef *fundef)
1591 : {
1592 49196528 : tree copy;
1593 49196528 : bool existed;
1594 49196528 : tree *slot = &(hash_map_safe_get_or_insert<hm_ggc>
1595 49196528 : (fundef_copies_table, fundef->decl, &existed, 127));
1596 :
1597 49196528 : if (!existed)
1598 : {
1599 : /* There is no cached function available, or in use. We can use
1600 : the function directly. That the slot is now created records
1601 : that this function is now in use. */
1602 6971638 : copy = build_tree_list (fundef->body, fundef->parms);
1603 6971638 : TREE_TYPE (copy) = fundef->result;
1604 : }
1605 42224890 : else if (*slot == NULL_TREE)
1606 : {
1607 4355 : if (uid_sensitive_constexpr_evaluation_p ())
1608 6 : return NULL_TREE;
1609 :
1610 : /* We've already used the function itself, so make a copy. */
1611 4349 : copy = build_tree_list (NULL, NULL);
1612 4349 : tree saved_body = DECL_SAVED_TREE (fundef->decl);
1613 4349 : tree saved_parms = DECL_ARGUMENTS (fundef->decl);
1614 4349 : tree saved_result = DECL_RESULT (fundef->decl);
1615 4349 : tree saved_fn = current_function_decl;
1616 4349 : DECL_SAVED_TREE (fundef->decl) = fundef->body;
1617 4349 : DECL_ARGUMENTS (fundef->decl) = fundef->parms;
1618 4349 : DECL_RESULT (fundef->decl) = fundef->result;
1619 4349 : current_function_decl = fundef->decl;
1620 4349 : TREE_PURPOSE (copy) = copy_fn (fundef->decl, TREE_VALUE (copy),
1621 4349 : TREE_TYPE (copy));
1622 4349 : current_function_decl = saved_fn;
1623 4349 : DECL_RESULT (fundef->decl) = saved_result;
1624 4349 : DECL_ARGUMENTS (fundef->decl) = saved_parms;
1625 4349 : DECL_SAVED_TREE (fundef->decl) = saved_body;
1626 : }
1627 : else
1628 : {
1629 : /* We have a cached function available. */
1630 42220535 : copy = *slot;
1631 42220535 : *slot = TREE_CHAIN (copy);
1632 : }
1633 :
1634 : return copy;
1635 : }
1636 :
1637 : /* Save the copy COPY of function FUN for later reuse by
1638 : get_fundef_copy(). By construction, there will always be an entry
1639 : to find. */
1640 :
1641 : static void
1642 49196520 : save_fundef_copy (tree fun, tree copy)
1643 : {
1644 49196520 : tree *slot = fundef_copies_table->get (fun);
1645 49196520 : TREE_CHAIN (copy) = *slot;
1646 49196520 : *slot = copy;
1647 49196520 : }
1648 :
1649 : static tree cxx_eval_bare_aggregate (const constexpr_ctx *, tree,
1650 : value_cat, bool *, bool *, tree *);
1651 : static tree cxx_fold_indirect_ref (const constexpr_ctx *, location_t, tree, tree,
1652 : bool *, tree *);
1653 : static tree find_heap_var_refs (tree *, int *, void *);
1654 :
1655 : /* For exception object EXC if it has class type and usable what () method
1656 : which returns cv char * return the xmalloced string literal which it returns
1657 : if possible, otherwise return NULL. */
1658 :
1659 : static char *
1660 227 : exception_what_str (const constexpr_ctx *ctx, tree exc)
1661 : {
1662 227 : tree type = strip_array_types (TREE_TYPE (exc));
1663 227 : if (!CLASS_TYPE_P (type))
1664 : return NULL;
1665 186 : tree std_exception = lookup_qualified_name (std_node, "exception",
1666 : LOOK_want::NORMAL, false);
1667 186 : if (TREE_CODE (std_exception) != TYPE_DECL)
1668 : return NULL;
1669 183 : if (!CLASS_TYPE_P (TREE_TYPE (std_exception)))
1670 : return NULL;
1671 183 : base_kind b_kind;
1672 183 : tree binfo = lookup_base (type, TREE_TYPE (std_exception), ba_check, &b_kind,
1673 : tf_none);
1674 183 : if (binfo == NULL_TREE || binfo == error_mark_node)
1675 : return NULL;
1676 179 : if (type != TREE_TYPE (exc))
1677 141 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1678 179 : tree call
1679 179 : = finish_class_member_access_expr (exc, get_identifier ("what"), false,
1680 : tf_none);
1681 179 : if (call == error_mark_node)
1682 : return NULL;
1683 179 : releasing_vec what_args;
1684 179 : call = finish_call_expr (call, &what_args, false, false, tf_none);
1685 179 : if (call == error_mark_node)
1686 : return NULL;
1687 179 : if (TREE_CODE (TREE_TYPE (call)) != POINTER_TYPE
1688 179 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1689 179 : || !COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (call)))
1690 179 : || !tree_int_cst_equal (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (call))),
1691 179 : TYPE_SIZE_UNIT (char_type_node))
1692 358 : || TYPE_PRECISION (TREE_TYPE (TREE_TYPE (call))) != BITS_PER_UNIT)
1693 0 : return NULL;
1694 179 : if (!potential_constant_expression (call))
1695 : return NULL;
1696 179 : bool non_constant_p = false, overflow_p = false;
1697 179 : tree jmp_target = NULL;
1698 179 : tree ptr = cxx_eval_constant_expression (ctx, call, vc_prvalue,
1699 : &non_constant_p, &overflow_p,
1700 : &jmp_target);
1701 179 : if (throws (&jmp_target) || non_constant_p)
1702 : return NULL;
1703 179 : if (reduced_constant_expression_p (ptr))
1704 40 : if (const char *msg = c_getstr (ptr))
1705 40 : return xstrdup (msg);
1706 139 : auto_vec <char, 32> v;
1707 5769 : for (unsigned i = 0; i < INT_MAX; ++i)
1708 : {
1709 5769 : tree t = call;
1710 5769 : if (i)
1711 5630 : t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, size_int (i));
1712 5769 : t = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
1713 5769 : non_constant_p = false;
1714 5769 : overflow_p = false;
1715 5769 : jmp_target = NULL;
1716 5769 : tree t2 = cxx_eval_constant_expression (ctx, t, vc_prvalue,
1717 : &non_constant_p, &overflow_p,
1718 : &jmp_target);
1719 5769 : if (throws (&jmp_target)
1720 5769 : || non_constant_p
1721 5769 : || !tree_fits_shwi_p (t2))
1722 0 : return NULL;
1723 5769 : char c = tree_to_shwi (t2);
1724 5769 : v.safe_push (c);
1725 5769 : if (c == '\0')
1726 : break;
1727 : }
1728 278 : return xstrdup (v.address ());
1729 318 : }
1730 :
1731 : /* Diagnose constant expression evaluation encountering call to
1732 : std::terminate due to exception EXC. */
1733 :
1734 : static void
1735 11 : diagnose_std_terminate (location_t loc, const constexpr_ctx *ctx, tree exc)
1736 : {
1737 11 : tree type = strip_array_types (TREE_TYPE (exc));
1738 11 : if (char *str = exception_what_str (ctx, exc))
1739 : {
1740 2 : error_at (loc, "%qs called after throwing an exception of type %qT; "
1741 : "%<what()%>: %qs", "std::terminate", type, str);
1742 2 : free (str);
1743 : }
1744 : else
1745 : {
1746 9 : if (type != TREE_TYPE (exc))
1747 9 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1748 9 : bool non_constant_p = false, overflow_p = false;
1749 9 : tree jmp_target = NULL;
1750 9 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1751 : &non_constant_p, &overflow_p,
1752 : &jmp_target);
1753 9 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1754 9 : if (reduced_constant_expression_p (val))
1755 9 : error_at (loc, "%qs called after throwing an exception %qE",
1756 : "std::terminate", val);
1757 : else
1758 0 : error_at (loc, "%qs called after throwing an exception of type %qT",
1759 : "std::terminate", type);
1760 : }
1761 11 : }
1762 :
1763 : /* Diagnose constant expression evaluation encountering call to
1764 : uncaught exception EXC. */
1765 :
1766 : static void
1767 216 : diagnose_uncaught_exception (location_t loc, const constexpr_ctx *ctx, tree exc)
1768 : {
1769 216 : tree type = strip_array_types (TREE_TYPE (exc));
1770 216 : if (char *str = exception_what_str (ctx, exc))
1771 : {
1772 177 : error_at (loc, "uncaught exception of type %qT; %<what()%>: %qs", type, str);
1773 177 : free (str);
1774 : }
1775 : else
1776 : {
1777 39 : if (type != TREE_TYPE (exc))
1778 39 : exc = build4 (ARRAY_REF, type, exc, size_zero_node, NULL, NULL);
1779 39 : bool non_constant_p = false, overflow_p = false;
1780 39 : tree jmp_target = NULL;
1781 39 : tree val = cxx_eval_constant_expression (ctx, exc, vc_prvalue,
1782 : &non_constant_p, &overflow_p,
1783 : &jmp_target);
1784 39 : gcc_assert (!throws (&jmp_target) && !non_constant_p);
1785 39 : if (reduced_constant_expression_p (val))
1786 38 : error_at (loc, "uncaught exception %qE", val);
1787 : else
1788 1 : error_at (loc, "uncaught exception of type %qT", type);
1789 : }
1790 216 : }
1791 :
1792 : /* Kinds of __cxa_* functions (and a few other EH related ones) we handle as
1793 : magic constexpr functions for C++26. */
1794 :
1795 : enum cxa_builtin {
1796 : CXA_NONE = 0,
1797 : CXA_ALLOCATE_EXCEPTION = 1,
1798 : CXA_FREE_EXCEPTION = 2,
1799 : CXA_THROW = 3,
1800 : CXA_BEGIN_CATCH = 4,
1801 : CXA_END_CATCH = 5,
1802 : CXA_RETHROW = 6,
1803 : CXA_GET_EXCEPTION_PTR = 7,
1804 : CXA_BAD_CAST = 8,
1805 : CXA_BAD_TYPEID = 9,
1806 : CXA_THROW_BAD_ARRAY_NEW_LENGTH = 10,
1807 : STD_RETHROW_EXCEPTION = 11,
1808 : BUILTIN_EH_PTR_ADJUST_REF = 12,
1809 : BUILTIN_UNCAUGHT_EXCEPTIONS = 13,
1810 : BUILTIN_CURRENT_EXCEPTION = 14
1811 : };
1812 :
1813 : /* Return cxa_builtin if FNDECL is a __cxa_* function handled as
1814 : magic constexpr function for C++26. Return CXA_NONE otherwise. */
1815 :
1816 : static enum cxa_builtin
1817 26498802 : cxx_cxa_builtin_fn_p (tree fndecl)
1818 : {
1819 26498802 : if (cxx_dialect < cxx26)
1820 : return CXA_NONE;
1821 2320647 : if (DECL_LANGUAGE (fndecl) != lang_c)
1822 : {
1823 1998288 : if (!decl_in_std_namespace_p (fndecl))
1824 : return CXA_NONE;
1825 774947 : if (id_equal (DECL_NAME (fndecl), "rethrow_exception"))
1826 : return STD_RETHROW_EXCEPTION;
1827 : return CXA_NONE;
1828 : }
1829 322359 : if (!startswith (IDENTIFIER_POINTER (DECL_NAME (fndecl)), "__cxa_"))
1830 : return CXA_NONE;
1831 45110 : if (id_equal (DECL_NAME (fndecl), "__cxa_allocate_exception"))
1832 : return CXA_ALLOCATE_EXCEPTION;
1833 12612 : if (id_equal (DECL_NAME (fndecl), "__cxa_free_exception"))
1834 : return CXA_FREE_EXCEPTION;
1835 12611 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw"))
1836 : return CXA_THROW;
1837 7434 : if (id_equal (DECL_NAME (fndecl), "__cxa_begin_catch"))
1838 : return CXA_BEGIN_CATCH;
1839 2569 : if (id_equal (DECL_NAME (fndecl), "__cxa_end_catch"))
1840 : return CXA_END_CATCH;
1841 858 : if (id_equal (DECL_NAME (fndecl), "__cxa_rethrow"))
1842 : return CXA_RETHROW;
1843 804 : if (id_equal (DECL_NAME (fndecl), "__cxa_get_exception_ptr"))
1844 : return CXA_GET_EXCEPTION_PTR;
1845 782 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_cast"))
1846 : return CXA_BAD_CAST;
1847 498 : if (id_equal (DECL_NAME (fndecl), "__cxa_bad_typeid"))
1848 : return CXA_BAD_TYPEID;
1849 490 : if (id_equal (DECL_NAME (fndecl), "__cxa_throw_bad_array_new_length"))
1850 : return CXA_THROW_BAD_ARRAY_NEW_LENGTH;
1851 : return CXA_NONE;
1852 : }
1853 :
1854 : /* Helper function for cxx_eval_cxa_builtin_fn.
1855 : Check if ARG is a valid first argument of __cxa_throw or
1856 : __cxa_free_exception or __builtin_eh_ptr_adjust_ref. Return NULL_TREE if
1857 : not, otherwise return the artificial __cxa_allocate_exception allocated
1858 : VAR_DECL. FREE_EXC is true for __cxa_free_exception, false otherwise. */
1859 :
1860 : static tree
1861 807 : cxa_check_throw_arg (tree arg, bool free_exc)
1862 : {
1863 807 : STRIP_NOPS (arg);
1864 807 : if (TREE_CODE (arg) != ADDR_EXPR)
1865 : return NULL_TREE;
1866 807 : arg = TREE_OPERAND (arg, 0);
1867 807 : if (!VAR_P (arg)
1868 807 : || !DECL_ARTIFICIAL (arg)
1869 807 : || ((!free_exc || DECL_NAME (arg) != heap_uninit_identifier)
1870 807 : && DECL_NAME (arg) != heap_identifier)
1871 1614 : || !DECL_LANG_SPECIFIC (arg))
1872 0 : return NULL_TREE;
1873 : return arg;
1874 : }
1875 :
1876 : /* Helper function for cxx_eval_cxa_builtin_fn.
1877 : "Allocate" on the constexpr heap an exception object of TYPE
1878 : with REFCOUNT. */
1879 :
1880 : static tree
1881 9945 : cxa_allocate_exception (location_t loc, const constexpr_ctx *ctx, tree type,
1882 : tree refcount)
1883 : {
1884 9945 : tree var = build_decl (loc, VAR_DECL, heap_uninit_identifier, type);
1885 9945 : DECL_ARTIFICIAL (var) = 1;
1886 9945 : retrofit_lang_decl (var);
1887 9945 : DECL_EXCEPTION_REFCOUNT (var) = refcount;
1888 9945 : ctx->global->heap_vars.safe_push (var);
1889 9945 : return var;
1890 : }
1891 :
1892 : /* Evaluate various __cxa_* calls as magic constexpr builtins for
1893 : C++26 constexpr exception support (P3068R5). */
1894 :
1895 : static tree
1896 15193 : cxx_eval_cxa_builtin_fn (const constexpr_ctx *ctx, tree call,
1897 : enum cxa_builtin kind, tree fndecl,
1898 : bool *non_constant_p, bool *overflow_p,
1899 : tree *jump_target)
1900 : {
1901 15193 : int nargs = call_expr_nargs (call);
1902 15193 : location_t loc = cp_expr_loc_or_input_loc (call);
1903 15193 : tree args[4], arg;
1904 15193 : if (nargs > 4)
1905 : {
1906 0 : invalid_nargs:
1907 0 : if (!ctx->quiet)
1908 0 : error_at (loc, "call to %qD function with incorrect "
1909 : "number of arguments", fndecl);
1910 0 : *non_constant_p = true;
1911 0 : return call;
1912 : }
1913 15193 : if ((kind == CXA_BEGIN_CATCH || kind == CXA_GET_EXCEPTION_PTR)
1914 3307 : && nargs == 1
1915 3307 : && (arg = CALL_EXPR_ARG (call, 0))
1916 3307 : && TREE_CODE (arg) == CALL_EXPR
1917 3307 : && call_expr_nargs (arg) == 1
1918 18500 : && integer_zerop (CALL_EXPR_ARG (arg, 0)))
1919 3307 : if (tree fun = get_function_named_in_call (arg))
1920 3307 : if (fndecl_built_in_p (fun, BUILT_IN_EH_POINTER))
1921 : {
1922 3307 : if (ctx->global->caught_exceptions.length () < 2)
1923 : {
1924 1580 : no_caught_exceptions:
1925 1580 : if (!ctx->quiet)
1926 0 : error_at (loc, "%qD called with no caught exceptions pending",
1927 : fndecl);
1928 1580 : *non_constant_p = true;
1929 1580 : return call;
1930 : }
1931 : /* Both __cxa_get_exception_ptr (__builtin_eh_pointer (0))
1932 : and __cxa_begin_catch (__builtin_eh_pointer (0)) calls expect
1933 : ctx->global->caught_exceptions vector to end with
1934 : __cxa_allocate_exception created artificial VAR_DECL (the
1935 : exception object) followed by handler type, pushed by TRY_BLOCK
1936 : evaluation. The only difference between the functions is that
1937 : __cxa_begin_catch pops the handler type from the vector and keeps
1938 : the VAR_DECL last and decreases uncaught_exceptions. The
1939 : VAR_DECL after __cxa_begin_catch serves as the current exception
1940 : and is then popped in __cxa_end_catch evaluation. */
1941 1727 : tree handler_type = ctx->global->caught_exceptions.last ();
1942 1727 : if (handler_type && VAR_P (handler_type))
1943 0 : goto no_caught_exceptions;
1944 1727 : unsigned idx = ctx->global->caught_exceptions.length () - 2;
1945 1727 : arg = ctx->global->caught_exceptions[idx];
1946 1727 : gcc_assert (VAR_P (arg));
1947 1727 : if (kind == CXA_BEGIN_CATCH)
1948 : {
1949 1713 : ctx->global->caught_exceptions.pop ();
1950 1713 : --ctx->global->uncaught_exceptions;
1951 : }
1952 1727 : if (handler_type == NULL_TREE)
1953 : /* Used for catch (...). Just return void. */
1954 33 : return void_node;
1955 1694 : else if (POINTER_TYPE_P (handler_type))
1956 : {
1957 : /* Used for catch of a pointer. */
1958 33 : if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE)
1959 33 : arg = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg)), arg,
1960 : size_zero_node, NULL_TREE, NULL_TREE);
1961 33 : arg = cp_convert (handler_type, arg,
1962 33 : ctx->quiet ? tf_none : tf_warning_or_error);
1963 33 : if (arg == error_mark_node)
1964 : {
1965 0 : *non_constant_p = true;
1966 0 : return call;
1967 : }
1968 : }
1969 : else
1970 : {
1971 : /* Used for catch of a non-pointer type. */
1972 1661 : tree exc_type = strip_array_types (TREE_TYPE (arg));
1973 1661 : tree exc_ptr_type = build_pointer_type (exc_type);
1974 1661 : arg = build_fold_addr_expr_with_type (arg, exc_ptr_type);
1975 1661 : if (CLASS_TYPE_P (handler_type))
1976 : {
1977 1621 : tree ptr_type = build_pointer_type (handler_type);
1978 1621 : arg = cp_convert (ptr_type, arg,
1979 1621 : ctx->quiet ? tf_none
1980 : : tf_warning_or_error);
1981 1621 : if (arg == error_mark_node)
1982 : {
1983 0 : *non_constant_p = true;
1984 0 : return call;
1985 : }
1986 : }
1987 : }
1988 1694 : return cxx_eval_constant_expression (ctx, arg, vc_prvalue,
1989 : non_constant_p, overflow_p,
1990 1694 : jump_target);
1991 : }
1992 22460 : for (int i = 0; i < nargs; ++i)
1993 : {
1994 10574 : args[i] = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (call, i),
1995 : vc_prvalue, non_constant_p,
1996 : overflow_p, jump_target);
1997 10574 : if (*non_constant_p)
1998 : return call;
1999 10574 : if (*jump_target)
2000 : return NULL_TREE;
2001 : }
2002 11886 : switch (kind)
2003 : {
2004 8244 : case CXA_ALLOCATE_EXCEPTION:
2005 8244 : if (nargs != 1)
2006 0 : goto invalid_nargs;
2007 8244 : if (!tree_fits_uhwi_p (args[0]))
2008 : {
2009 0 : if (!ctx->quiet)
2010 0 : error_at (loc, "cannot allocate exception: size not constant");
2011 0 : *non_constant_p = true;
2012 0 : return call;
2013 : }
2014 : else
2015 : {
2016 16488 : tree type = build_array_type_nelts (char_type_node,
2017 8244 : tree_to_uhwi (args[0]));
2018 8244 : tree var = cxa_allocate_exception (loc, ctx, type, size_zero_node);
2019 8244 : ctx->global->put_value (var, NULL_TREE);
2020 8244 : return fold_convert (ptr_type_node, build_address (var));
2021 : }
2022 1 : case CXA_FREE_EXCEPTION:
2023 1 : if (nargs != 1)
2024 0 : goto invalid_nargs;
2025 1 : arg = cxa_check_throw_arg (args[0], true);
2026 1 : if (arg == NULL_TREE)
2027 : {
2028 0 : invalid_ptr:
2029 0 : if (!ctx->quiet)
2030 0 : error_at (loc, "first argument to %qD function not result of "
2031 : "%<__cxa_allocate_exception%>", fndecl);
2032 0 : *non_constant_p = true;
2033 0 : return call;
2034 : }
2035 1 : DECL_NAME (arg) = heap_deleted_identifier;
2036 1 : ctx->global->destroy_value (arg);
2037 1 : ctx->global->heap_dealloc_count++;
2038 1 : return void_node;
2039 736 : case CXA_THROW:
2040 736 : if (nargs != 3)
2041 0 : goto invalid_nargs;
2042 736 : arg = cxa_check_throw_arg (args[0], false);
2043 736 : if (arg == NULL_TREE)
2044 0 : goto invalid_ptr;
2045 736 : DECL_EXCEPTION_REFCOUNT (arg)
2046 736 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2047 : size_one_node);
2048 736 : ++ctx->global->uncaught_exceptions;
2049 736 : *jump_target = arg;
2050 736 : return void_node;
2051 0 : case CXA_BEGIN_CATCH:
2052 0 : case CXA_GET_EXCEPTION_PTR:
2053 0 : goto invalid_nargs;
2054 1711 : case CXA_END_CATCH:
2055 1711 : if (nargs != 0)
2056 0 : goto invalid_nargs;
2057 1711 : if (ctx->global->caught_exceptions.is_empty ())
2058 : {
2059 0 : no_active_exc:
2060 0 : if (!ctx->quiet)
2061 0 : error_at (loc, "%qD called with no caught exceptions active",
2062 : fndecl);
2063 0 : *non_constant_p = true;
2064 0 : return call;
2065 : }
2066 : else
2067 : {
2068 1711 : arg = ctx->global->caught_exceptions.pop ();
2069 1711 : if (arg == NULL_TREE || !VAR_P (arg))
2070 0 : goto no_active_exc;
2071 1711 : free_except:
2072 1742 : DECL_EXCEPTION_REFCOUNT (arg)
2073 1742 : = size_binop (MINUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2074 : size_one_node);
2075 1742 : if (integer_zerop (DECL_EXCEPTION_REFCOUNT (arg)))
2076 : {
2077 1653 : if (type_build_dtor_call (TREE_TYPE (arg)))
2078 : {
2079 : /* So that we don't complain about out-of-consteval use. */
2080 1578 : temp_override<tree> ovr (current_function_decl);
2081 1578 : if (ctx->call && ctx->call->fundef)
2082 1578 : current_function_decl = ctx->call->fundef->decl;
2083 1578 : tree cleanup
2084 1589 : = cxx_maybe_build_cleanup (arg, (ctx->quiet ? tf_none
2085 : : tf_warning_or_error));
2086 1578 : if (cleanup == error_mark_node)
2087 0 : *non_constant_p = true;
2088 1578 : tree jmp_target = NULL_TREE;
2089 1578 : cxx_eval_constant_expression (ctx, cleanup, vc_discard,
2090 : non_constant_p, overflow_p,
2091 : &jmp_target);
2092 1578 : if (throws (&jmp_target))
2093 0 : *jump_target = jmp_target;
2094 1578 : }
2095 1653 : DECL_NAME (arg) = heap_deleted_identifier;
2096 1653 : ctx->global->destroy_value (arg);
2097 1653 : ctx->global->heap_dealloc_count++;
2098 : }
2099 : }
2100 1742 : return void_node;
2101 48 : case CXA_RETHROW:
2102 48 : if (nargs != 0)
2103 0 : goto invalid_nargs;
2104 48 : unsigned idx;
2105 96 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2106 34 : if (arg == NULL_TREE || !VAR_P (arg))
2107 0 : --idx;
2108 : else
2109 : break;
2110 48 : if (arg == NULL_TREE)
2111 : {
2112 14 : if (!ctx->quiet)
2113 4 : error_at (loc, "%qD called with no caught exceptions active",
2114 : fndecl);
2115 14 : *non_constant_p = true;
2116 14 : return call;
2117 : }
2118 34 : DECL_EXCEPTION_REFCOUNT (arg)
2119 34 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2120 34 : ++ctx->global->uncaught_exceptions;
2121 34 : *jump_target = arg;
2122 34 : return void_node;
2123 143 : case CXA_BAD_CAST:
2124 143 : case CXA_BAD_TYPEID:
2125 143 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2126 143 : if (nargs != 0)
2127 0 : goto invalid_nargs;
2128 : else
2129 : {
2130 143 : tree name;
2131 143 : switch (kind)
2132 : {
2133 117 : case CXA_BAD_CAST:
2134 117 : name = get_identifier ("bad_cast");
2135 117 : break;
2136 5 : case CXA_BAD_TYPEID:
2137 5 : name = get_identifier ("bad_typeid");
2138 5 : break;
2139 21 : case CXA_THROW_BAD_ARRAY_NEW_LENGTH:
2140 21 : name = get_identifier ("bad_array_new_length");
2141 21 : break;
2142 : default:
2143 : gcc_unreachable ();
2144 : }
2145 143 : tree decl = lookup_qualified_name (std_node, name);
2146 143 : if (TREE_CODE (decl) != TYPE_DECL
2147 128 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2148 271 : || !type_build_ctor_call (TREE_TYPE (decl)))
2149 : {
2150 15 : if (!ctx->quiet)
2151 4 : error_at (loc, "%qD called without %<std::%D%> being defined",
2152 : fndecl, name);
2153 15 : *non_constant_p = true;
2154 15 : return call;
2155 : }
2156 128 : tree type = TREE_TYPE (decl);
2157 128 : tree var = cxa_allocate_exception (loc, ctx, type, size_one_node);
2158 128 : tree ctor
2159 128 : = build_special_member_call (var, complete_ctor_identifier,
2160 : NULL, type, LOOKUP_NORMAL,
2161 128 : ctx->quiet ? tf_none
2162 : : tf_warning_or_error);
2163 128 : if (ctor == error_mark_node)
2164 : {
2165 0 : *non_constant_p = true;
2166 0 : return call;
2167 : }
2168 128 : if (TREE_CONSTANT (ctor))
2169 0 : ctx->global->put_value (var, ctor);
2170 : else
2171 : {
2172 128 : ctx->global->put_value (var, NULL_TREE);
2173 128 : cxx_eval_constant_expression (ctx, ctor, vc_discard,
2174 : non_constant_p, overflow_p,
2175 : jump_target);
2176 128 : if (*non_constant_p)
2177 : return call;
2178 128 : if (throws (jump_target))
2179 : return NULL_TREE;
2180 : }
2181 128 : ++ctx->global->uncaught_exceptions;
2182 128 : *jump_target = var;
2183 : }
2184 128 : return void_node;
2185 30 : case BUILTIN_UNCAUGHT_EXCEPTIONS:
2186 30 : if (nargs != 0)
2187 0 : goto invalid_nargs;
2188 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2189 : want to give a definite answer during mce_unknown evaluation,
2190 : because that might prevent evaluation later on when some
2191 : exceptions might be uncaught. But unlike that, we don't
2192 : want to constant fold it even during cp_fold, because at runtime
2193 : std::uncaught_exceptions () might still be non-zero. */
2194 30 : if (ctx->manifestly_const_eval != mce_true)
2195 : {
2196 17 : *non_constant_p = true;
2197 17 : return call;
2198 : }
2199 13 : return build_int_cst (integer_type_node,
2200 13 : ctx->global->uncaught_exceptions);
2201 903 : case BUILTIN_CURRENT_EXCEPTION:
2202 903 : if (nargs != 0)
2203 0 : goto invalid_nargs;
2204 : else
2205 : {
2206 903 : tree name = get_identifier ("exception_ptr");
2207 903 : tree decl = lookup_qualified_name (std_node, name);
2208 903 : tree fld;
2209 903 : if (TREE_CODE (decl) != TYPE_DECL
2210 903 : || !CLASS_TYPE_P (TREE_TYPE (decl))
2211 903 : || !COMPLETE_TYPE_P (TREE_TYPE (decl))
2212 903 : || !(fld = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (decl))))
2213 903 : || DECL_ARTIFICIAL (fld)
2214 903 : || TREE_CODE (TREE_TYPE (fld)) != POINTER_TYPE
2215 903 : || next_aggregate_field (DECL_CHAIN (fld))
2216 1806 : || !tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (decl)),
2217 903 : TYPE_SIZE (TREE_TYPE (fld))))
2218 : {
2219 0 : if (!ctx->quiet)
2220 0 : error_at (loc, "%qD called without supportable %qs",
2221 : fndecl, "std::exception_ptr");
2222 0 : *non_constant_p = true;
2223 0 : return call;
2224 : }
2225 1806 : FOR_EACH_VEC_ELT_REVERSE (ctx->global->caught_exceptions, idx, arg)
2226 17 : if (arg == NULL_TREE || !VAR_P (arg))
2227 0 : --idx;
2228 : else
2229 : break;
2230 : /* Similarly to __builtin_is_constant_evaluated (), we don't
2231 : want to give a definite answer during mce_unknown evaluation,
2232 : because that might prevent evaluation later on when some
2233 : exceptions might be current. But unlike that, we don't
2234 : want to constant fold it to null even during cp_fold, because
2235 : at runtime std::current_exception () might still be non-null. */
2236 903 : if (ctx->manifestly_const_eval != mce_true && arg == NULL_TREE)
2237 : {
2238 883 : *non_constant_p = true;
2239 883 : return call;
2240 : }
2241 20 : if (arg == NULL_TREE)
2242 3 : arg = build_zero_cst (TREE_TYPE (fld));
2243 : else
2244 : {
2245 17 : DECL_EXCEPTION_REFCOUNT (arg)
2246 17 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2247 : size_one_node);
2248 17 : arg = fold_convert (ptr_type_node, build_address (arg));
2249 : }
2250 20 : return build_constructor_single (TREE_TYPE (decl), fld, arg);
2251 : }
2252 19 : case STD_RETHROW_EXCEPTION:
2253 19 : if (nargs != 1)
2254 0 : goto invalid_nargs;
2255 19 : if (TYPE_REF_P (TREE_TYPE (args[0])))
2256 : {
2257 19 : arg = args[0];
2258 19 : STRIP_NOPS (arg);
2259 19 : if (TREE_CODE (arg) == ADDR_EXPR)
2260 : {
2261 19 : args[0]
2262 19 : = cxx_eval_constant_expression (ctx, TREE_OPERAND (arg, 0),
2263 : vc_prvalue, non_constant_p,
2264 : overflow_p, jump_target);
2265 19 : if (*non_constant_p)
2266 : return call;
2267 19 : if (*jump_target)
2268 : return NULL_TREE;
2269 : }
2270 : }
2271 19 : if (TREE_CODE (args[0]) != CONSTRUCTOR
2272 19 : || CONSTRUCTOR_NELTS (args[0]) != 1)
2273 : {
2274 0 : invalid_std_rethrow:
2275 0 : if (!ctx->quiet)
2276 0 : error_at (loc, "%qD called with unexpected %qs argument",
2277 : fndecl, "std::exception_ptr");
2278 0 : *non_constant_p = true;
2279 0 : return void_node;
2280 : }
2281 19 : arg = cxa_check_throw_arg (CONSTRUCTOR_ELT (args[0], 0)->value, false);
2282 19 : if (arg == NULL_TREE)
2283 0 : goto invalid_std_rethrow;
2284 19 : DECL_EXCEPTION_REFCOUNT (arg)
2285 19 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg), size_one_node);
2286 19 : ++ctx->global->uncaught_exceptions;
2287 19 : *jump_target = arg;
2288 19 : return void_node;
2289 51 : case BUILTIN_EH_PTR_ADJUST_REF:
2290 51 : if (nargs != 2)
2291 0 : goto invalid_nargs;
2292 51 : arg = cxa_check_throw_arg (args[0], false);
2293 51 : if (arg == NULL_TREE)
2294 0 : goto invalid_ptr;
2295 51 : if (integer_onep (args[1]))
2296 20 : DECL_EXCEPTION_REFCOUNT (arg)
2297 40 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (arg),
2298 : size_one_node);
2299 31 : else if (integer_minus_onep (args[1]))
2300 31 : goto free_except;
2301 : else
2302 : {
2303 0 : if (!ctx->quiet)
2304 0 : error_at (loc, "%qD called with second argument "
2305 : "other than 1 or -1", fndecl);
2306 0 : *non_constant_p = true;
2307 : }
2308 20 : return void_node;
2309 0 : default:
2310 0 : gcc_unreachable ();
2311 : }
2312 : }
2313 :
2314 : /* Variables and functions to manage constexpr call expansion context.
2315 : These do not need to be marked for PCH or GC. */
2316 :
2317 : /* FIXME remember and print actual constant arguments. */
2318 : static vec<tree> call_stack;
2319 : static int call_stack_tick;
2320 : static int last_cx_error_tick;
2321 :
2322 : /* Attempt to evaluate T which represents a call to __builtin_constexpr_diag.
2323 : The arguments should be an integer (0 for inform, 1 for warning, 2 for
2324 : error) optionally with 16 ored in if it should use caller's caller location
2325 : instead of caller's location and 2 messages which are either a pointer to
2326 : a STRING_CST or class with data () and size () member functions like
2327 : string_view or u8string_view. The first message is a tag, with "" passed
2328 : for no tag, data () should return const char *, the tag should only contain
2329 : alphanumeric letters or underscores. The second message is the diagnostic
2330 : message, data () can be either const char * or const char8_t *. size ()
2331 : should return the corresponding length of the strings in bytes as an
2332 : integer. */
2333 :
2334 : static tree
2335 103 : cxx_eval_constexpr_diag (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
2336 : bool *overflow_p, tree *jump_target)
2337 : {
2338 103 : location_t loc = EXPR_LOCATION (t);
2339 103 : if (call_expr_nargs (t) != 3)
2340 : {
2341 6 : if (!ctx->quiet)
2342 6 : error_at (loc, "wrong number of arguments to %qs call",
2343 : "__builtin_constexpr_diag");
2344 6 : *non_constant_p = true;
2345 6 : return t;
2346 : }
2347 : tree args[3];
2348 388 : for (int i = 0; i < 3; ++i)
2349 : {
2350 291 : tree arg = convert_from_reference (CALL_EXPR_ARG (t, i));
2351 291 : arg = cxx_eval_constant_expression (ctx, arg,
2352 : (i == 0
2353 263 : || POINTER_TYPE_P (TREE_TYPE (arg)))
2354 485 : ? vc_prvalue : vc_glvalue,
2355 : non_constant_p, overflow_p,
2356 : jump_target);
2357 291 : if (*jump_target)
2358 : return NULL_TREE;
2359 291 : if (*non_constant_p)
2360 : return t;
2361 291 : args[i] = arg;
2362 : }
2363 194 : if (TREE_CODE (args[0]) != INTEGER_CST
2364 97 : || wi::to_widest (args[0]) < 0
2365 95 : || wi::to_widest (args[0]) > 18
2366 194 : || (wi::to_widest (args[0]) & 15) > 2)
2367 : {
2368 4 : if (!ctx->quiet)
2369 4 : error_at (loc, "first %qs call argument should be 0, 1, 2, 16, 17 or "
2370 : "18", "__builtin_constexpr_diag");
2371 4 : *non_constant_p = true;
2372 4 : return t;
2373 : }
2374 93 : const char *msgs[2] = {};
2375 93 : int lens[3] = {};
2376 558 : cexpr_str cstrs[2];
2377 233 : diagnostics::kind kind = diagnostics::kind::error;
2378 233 : for (int i = 1; i < 3; ++i)
2379 : {
2380 168 : tree arg = args[i];
2381 168 : if (POINTER_TYPE_P (TREE_TYPE (arg)))
2382 : {
2383 99 : tree str = arg;
2384 99 : STRIP_NOPS (str);
2385 99 : if (TREE_CODE (str) == ADDR_EXPR
2386 99 : && TREE_CODE (TREE_OPERAND (str, 0)) == STRING_CST)
2387 : {
2388 99 : str = TREE_OPERAND (str, 0);
2389 99 : tree eltype = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (str)));
2390 99 : if (eltype == char_type_node
2391 12 : || (i == 2 && eltype == char8_type_node))
2392 168 : arg = str;
2393 : }
2394 : }
2395 168 : cstrs[i - 1].message = arg;
2396 168 : if (!cstrs[i - 1].type_check (loc, i == 2))
2397 : {
2398 20 : *non_constant_p = true;
2399 20 : return t;
2400 : }
2401 148 : if (!cstrs[i - 1].extract (loc, msgs[i - 1], lens[i - 1], ctx,
2402 : non_constant_p, overflow_p, jump_target))
2403 : {
2404 8 : if (*jump_target)
2405 : return NULL_TREE;
2406 8 : *non_constant_p = true;
2407 8 : return t;
2408 : }
2409 : }
2410 65 : if (msgs[0])
2411 : {
2412 361 : for (int i = 0; i < lens[0]; ++i)
2413 298 : if (!ISALNUM (msgs[0][i]) && msgs[0][i] != '_')
2414 : {
2415 2 : if (!ctx->quiet)
2416 2 : error_at (loc, "%qs tag string contains %qc character other than"
2417 : " letters, digits or %<_%>",
2418 : "__builtin_constexpr_diag", msgs[0][i]);
2419 2 : *non_constant_p = true;
2420 2 : return t;
2421 : }
2422 : }
2423 63 : if (ctx->manifestly_const_eval == mce_unknown)
2424 : {
2425 0 : *non_constant_p = true;
2426 0 : return t;
2427 : }
2428 63 : int arg0 = tree_to_uhwi (args[0]);
2429 63 : if (arg0 & 16)
2430 : {
2431 16 : arg0 &= 15;
2432 16 : if (!call_stack.is_empty ())
2433 : {
2434 16 : tree call = call_stack.last ();
2435 16 : if (EXPR_HAS_LOCATION (call))
2436 16 : loc = EXPR_LOCATION (call);
2437 : }
2438 : }
2439 63 : if (arg0 == 0)
2440 : kind = diagnostics::kind::note;
2441 39 : else if (arg0 == 1)
2442 18 : kind = diagnostics::kind::warning;
2443 63 : if (lens[0])
2444 : {
2445 44 : const char *color = "error";
2446 44 : if (kind == diagnostics::kind::note)
2447 : color = "note";
2448 30 : else if (kind == diagnostics::kind::warning)
2449 16 : color = "warning";
2450 44 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s [%r%.*s%R]",
2451 : lens[1], msgs[1], color, lens[0], msgs[0]);
2452 : }
2453 : else
2454 19 : emit_diagnostic (kind, loc, 0, "constexpr message: %.*s",
2455 : lens[1], msgs[1]);
2456 63 : return void_node;
2457 279 : }
2458 :
2459 : /* Attempt to evaluate T which represents a call to a builtin function.
2460 : We assume here that all builtin functions evaluate to scalar types
2461 : represented by _CST nodes. */
2462 :
2463 : static tree
2464 14094979 : cxx_eval_builtin_function_call (const constexpr_ctx *ctx, tree t, tree fun,
2465 : value_cat lval,
2466 : bool *non_constant_p, bool *overflow_p,
2467 : tree *jump_target)
2468 : {
2469 14094979 : const int nargs = call_expr_nargs (t);
2470 14094979 : tree *args = (tree *) alloca (nargs * sizeof (tree));
2471 14094979 : tree new_call;
2472 14094979 : int i;
2473 :
2474 : /* Don't fold __builtin_constant_p within a constexpr function. */
2475 14094979 : bool bi_const_p = DECL_IS_BUILTIN_CONSTANT_P (fun);
2476 :
2477 : /* If we aren't requiring a constant expression, defer __builtin_constant_p
2478 : in a constexpr function until we have values for the parameters. */
2479 1801569 : if (bi_const_p
2480 1801569 : && ctx->manifestly_const_eval != mce_true
2481 1691977 : && current_function_decl
2482 1690819 : && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
2483 : {
2484 1402103 : *non_constant_p = true;
2485 1402103 : return t;
2486 : }
2487 :
2488 12692876 : if (fndecl_built_in_p (fun, BUILT_IN_FRONTEND))
2489 3897848 : switch (DECL_FE_FUNCTION_CODE (fun))
2490 : {
2491 3889406 : case CP_BUILT_IN_IS_CONSTANT_EVALUATED:
2492 : /* For __builtin_is_constant_evaluated, defer it if not
2493 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
2494 : without manifestly_const_eval even expressions or parts thereof
2495 : which will later be manifestly const_eval evaluated), otherwise fold
2496 : it to true. */
2497 3889406 : if (ctx->manifestly_const_eval == mce_unknown)
2498 : {
2499 3855301 : *non_constant_p = true;
2500 3855301 : return t;
2501 : }
2502 34105 : return constant_boolean_node (ctx->manifestly_const_eval == mce_true,
2503 34105 : boolean_type_node);
2504 :
2505 4565 : case CP_BUILT_IN_SOURCE_LOCATION:
2506 4565 : {
2507 4565 : temp_override<tree> ovr (current_function_decl);
2508 4565 : if (ctx->call && ctx->call->fundef)
2509 1443 : current_function_decl = ctx->call->fundef->decl;
2510 4565 : return fold_builtin_source_location (t);
2511 4565 : }
2512 :
2513 51 : case CP_BUILT_IN_EH_PTR_ADJUST_REF:
2514 51 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_EH_PTR_ADJUST_REF,
2515 : fun, non_constant_p, overflow_p,
2516 51 : jump_target);
2517 :
2518 903 : case CP_BUILT_IN_CURRENT_EXCEPTION:
2519 903 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_CURRENT_EXCEPTION,
2520 : fun, non_constant_p, overflow_p,
2521 903 : jump_target);
2522 :
2523 30 : case CP_BUILT_IN_UNCAUGHT_EXCEPTIONS:
2524 30 : return cxx_eval_cxa_builtin_fn (ctx, t, BUILTIN_UNCAUGHT_EXCEPTIONS,
2525 : fun, non_constant_p, overflow_p,
2526 30 : jump_target);
2527 :
2528 103 : case CP_BUILT_IN_CONSTEXPR_DIAG:
2529 103 : return cxx_eval_constexpr_diag (ctx, t, non_constant_p, overflow_p,
2530 103 : jump_target);
2531 :
2532 : default:
2533 : break;
2534 : }
2535 :
2536 8797818 : int strops = 0;
2537 8797818 : int strret = 0;
2538 8797818 : bool bos = false;
2539 8797818 : if (fndecl_built_in_p (fun, BUILT_IN_NORMAL))
2540 8089342 : switch (DECL_FUNCTION_CODE (fun))
2541 : {
2542 : case BUILT_IN_STRLEN:
2543 : case BUILT_IN_STRNLEN:
2544 8797728 : strops = 1;
2545 : break;
2546 111403 : case BUILT_IN_MEMCHR:
2547 111403 : case BUILT_IN_STRCHR:
2548 111403 : case BUILT_IN_STRRCHR:
2549 111403 : strops = 1;
2550 111403 : strret = 1;
2551 111403 : break;
2552 72164 : case BUILT_IN_MEMCMP:
2553 72164 : case BUILT_IN_STRCMP:
2554 72164 : strops = 2;
2555 72164 : break;
2556 28713 : case BUILT_IN_STRSTR:
2557 28713 : strops = 2;
2558 28713 : strret = 1;
2559 28713 : break;
2560 42 : case BUILT_IN_ASAN_POINTER_COMPARE:
2561 42 : case BUILT_IN_ASAN_POINTER_SUBTRACT:
2562 42 : case BUILT_IN_OBSERVABLE_CHKPT:
2563 : /* These builtins shall be ignored during constant expression
2564 : evaluation. */
2565 42 : return void_node;
2566 48 : case BUILT_IN_UNREACHABLE:
2567 48 : case BUILT_IN_TRAP:
2568 48 : if (!*non_constant_p && !ctx->quiet)
2569 : {
2570 : /* Do not allow__builtin_unreachable in constexpr function.
2571 : The __builtin_unreachable call with BUILTINS_LOCATION
2572 : comes from cp_maybe_instrument_return. */
2573 13 : if (EXPR_LOCATION (t) == BUILTINS_LOCATION)
2574 0 : error ("%<constexpr%> call flows off the end of the function");
2575 : else
2576 13 : error ("%q+E is not a constant expression", t);
2577 : }
2578 48 : *non_constant_p = true;
2579 48 : return t;
2580 2667 : case BUILT_IN_OBJECT_SIZE:
2581 2667 : case BUILT_IN_DYNAMIC_OBJECT_SIZE:
2582 2667 : bos = ctx->manifestly_const_eval == mce_true;
2583 2667 : break;
2584 : default:
2585 : break;
2586 : }
2587 :
2588 : /* Be permissive for arguments to built-ins; __builtin_constant_p should
2589 : return constant false for a non-constant argument. */
2590 8797728 : constexpr_ctx new_ctx = *ctx;
2591 8797728 : new_ctx.quiet = true;
2592 24712628 : for (i = 0; i < nargs; ++i)
2593 : {
2594 15914901 : tree arg = CALL_EXPR_ARG (t, i);
2595 15914901 : tree oarg = arg;
2596 :
2597 : /* To handle string built-ins we need to pass ADDR_EXPR<STRING_CST> since
2598 : expand_builtin doesn't know how to look in the values table. */
2599 15914901 : bool strop = i < strops;
2600 15914901 : if (strop)
2601 : {
2602 443114 : STRIP_NOPS (arg);
2603 443114 : if (TREE_CODE (arg) == ADDR_EXPR)
2604 7899 : arg = TREE_OPERAND (arg, 0);
2605 : else
2606 : strop = false;
2607 : }
2608 :
2609 : /* If builtin_valid_in_constant_expr_p is true,
2610 : potential_constant_expression_1 has not recursed into the arguments
2611 : of the builtin, verify it here. */
2612 15914901 : if (!builtin_valid_in_constant_expr_p (fun)
2613 15914901 : || potential_constant_expression (arg))
2614 : {
2615 15914770 : bool dummy1 = false, dummy2 = false;
2616 15914770 : tree jmp_target = NULL_TREE;
2617 15914770 : arg = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
2618 : &dummy1, &dummy2, &jmp_target);
2619 15914769 : if (jmp_target)
2620 : {
2621 0 : *jump_target = jmp_target;
2622 0 : return NULL_TREE;
2623 : }
2624 : }
2625 :
2626 15914900 : if (bi_const_p)
2627 : /* For __builtin_constant_p, fold all expressions with constant values
2628 : even if they aren't C++ constant-expressions. */
2629 399465 : arg = cp_fold_rvalue (arg);
2630 15515435 : else if (strop)
2631 : {
2632 7899 : if (TREE_CODE (arg) == CONSTRUCTOR)
2633 100 : arg = braced_lists_to_strings (TREE_TYPE (arg), arg);
2634 7899 : if (TREE_CODE (arg) == STRING_CST)
2635 2962 : arg = build_address (arg);
2636 : else
2637 : arg = oarg;
2638 : }
2639 :
2640 15914900 : args[i] = arg;
2641 : }
2642 8797727 : if (bos)
2643 : {
2644 90 : tree arg = args[0];
2645 90 : STRIP_NOPS (arg);
2646 90 : if (TREE_CODE (arg) == ADDR_EXPR)
2647 90 : args[0] = arg;
2648 : }
2649 :
2650 8797727 : bool save_ffbcp = force_folding_builtin_constant_p;
2651 8797727 : force_folding_builtin_constant_p |= ctx->manifestly_const_eval == mce_true;
2652 8797727 : tree save_cur_fn = current_function_decl;
2653 : /* Return name of ctx->call->fundef->decl for __builtin_FUNCTION (). */
2654 8797727 : if (fndecl_built_in_p (fun, BUILT_IN_FUNCTION)
2655 37 : && ctx->call
2656 8797731 : && ctx->call->fundef)
2657 4 : current_function_decl = ctx->call->fundef->decl;
2658 8797727 : if (fndecl_built_in_p (fun,
2659 : CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS,
2660 : BUILT_IN_FRONTEND))
2661 : {
2662 336 : location_t loc = EXPR_LOCATION (t);
2663 336 : if (nargs >= 1)
2664 333 : VERIFY_CONSTANT (args[0]);
2665 136 : new_call
2666 136 : = fold_builtin_is_pointer_inverconvertible_with_class (loc, nargs,
2667 : args);
2668 : }
2669 8797391 : else if (fndecl_built_in_p (fun,
2670 : CP_BUILT_IN_IS_CORRESPONDING_MEMBER,
2671 : BUILT_IN_FRONTEND))
2672 : {
2673 471 : location_t loc = EXPR_LOCATION (t);
2674 471 : if (nargs >= 2)
2675 : {
2676 465 : VERIFY_CONSTANT (args[0]);
2677 237 : VERIFY_CONSTANT (args[1]);
2678 : }
2679 243 : new_call = fold_builtin_is_corresponding_member (loc, nargs, args);
2680 : }
2681 8796920 : else if (fndecl_built_in_p (fun, CP_BUILT_IN_IS_STRING_LITERAL,
2682 : BUILT_IN_FRONTEND))
2683 : {
2684 1983 : location_t loc = EXPR_LOCATION (t);
2685 1983 : if (nargs >= 1)
2686 : {
2687 1983 : tree arg = CALL_EXPR_ARG (t, 0);
2688 1983 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
2689 : non_constant_p, overflow_p,
2690 : jump_target);
2691 1983 : if (*jump_target)
2692 : return NULL_TREE;
2693 1983 : args[0] = arg;
2694 : }
2695 1983 : new_call = fold_builtin_is_string_literal (loc, nargs, args);
2696 : }
2697 : else
2698 17589874 : new_call = fold_builtin_call_array (EXPR_LOCATION (t), TREE_TYPE (t),
2699 8794937 : CALL_EXPR_FN (t), nargs, args);
2700 8797299 : current_function_decl = save_cur_fn;
2701 8797299 : force_folding_builtin_constant_p = save_ffbcp;
2702 8797299 : if (new_call == NULL)
2703 : {
2704 6308904 : if (!*non_constant_p && !ctx->quiet)
2705 : {
2706 0 : new_call = build_call_array_loc (EXPR_LOCATION (t), TREE_TYPE (t),
2707 0 : CALL_EXPR_FN (t), nargs, args);
2708 0 : error ("%q+E is not a constant expression", new_call);
2709 : }
2710 6308904 : *non_constant_p = true;
2711 6308904 : return t;
2712 : }
2713 :
2714 2488395 : if (!potential_constant_expression (new_call))
2715 : {
2716 732 : if (!*non_constant_p && !ctx->quiet)
2717 4 : error ("%q+E is not a constant expression", new_call);
2718 732 : *non_constant_p = true;
2719 732 : return t;
2720 : }
2721 :
2722 2487663 : if (strret)
2723 : {
2724 : /* memchr returns a pointer into the first argument, but we replaced the
2725 : argument above with a STRING_CST; put it back it now. */
2726 119 : tree op = CALL_EXPR_ARG (t, strret-1);
2727 119 : STRIP_NOPS (new_call);
2728 119 : if (TREE_CODE (new_call) == POINTER_PLUS_EXPR)
2729 60 : TREE_OPERAND (new_call, 0) = op;
2730 59 : else if (TREE_CODE (new_call) == ADDR_EXPR)
2731 2487663 : new_call = op;
2732 : }
2733 :
2734 2487663 : return cxx_eval_constant_expression (&new_ctx, new_call, lval,
2735 : non_constant_p, overflow_p,
2736 2487663 : jump_target);
2737 : }
2738 :
2739 : /* TEMP is the constant value of a temporary object of type TYPE. Adjust
2740 : the type of the value to match. */
2741 :
2742 : static tree
2743 84526690 : adjust_temp_type (tree type, tree temp)
2744 : {
2745 84526690 : if (same_type_p (TREE_TYPE (temp), type))
2746 : return temp;
2747 : /* Avoid wrapping an aggregate value in a NOP_EXPR. */
2748 38441150 : if (TREE_CODE (temp) == CONSTRUCTOR)
2749 : {
2750 : /* build_constructor wouldn't retain various CONSTRUCTOR flags. */
2751 3168614 : tree t = copy_node (temp);
2752 3168614 : TREE_TYPE (t) = type;
2753 3168614 : return t;
2754 : }
2755 35272536 : if (TREE_CODE (temp) == EMPTY_CLASS_EXPR)
2756 0 : return build0 (EMPTY_CLASS_EXPR, type);
2757 35272536 : gcc_assert (scalarish_type_p (type));
2758 : /* Now we know we're dealing with a scalar, and a prvalue of non-class
2759 : type is cv-unqualified. */
2760 35272536 : return cp_fold_convert (cv_unqualified (type), temp);
2761 : }
2762 :
2763 : /* If T is a CONSTRUCTOR, return an unshared copy of T and any
2764 : sub-CONSTRUCTORs. Otherwise return T.
2765 :
2766 : We use this whenever we initialize an object as a whole, whether it's a
2767 : parameter, a local variable, or a subobject, so that subsequent
2768 : modifications don't affect other places where it was used. */
2769 :
2770 : tree
2771 68431526 : unshare_constructor (tree t MEM_STAT_DECL)
2772 : {
2773 68431526 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2774 : return t;
2775 12003936 : auto_vec <tree*, 4> ptrs;
2776 12003936 : ptrs.safe_push (&t);
2777 12003936 : while (!ptrs.is_empty ())
2778 : {
2779 14834862 : tree *p = ptrs.pop ();
2780 14834862 : tree n = copy_node (*p PASS_MEM_STAT);
2781 22737266 : CONSTRUCTOR_ELTS (n) = vec_safe_copy (CONSTRUCTOR_ELTS (*p) PASS_MEM_STAT);
2782 14834862 : *p = n;
2783 14834862 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (n);
2784 14834862 : constructor_elt *ce;
2785 55762421 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (v, i, &ce); ++i)
2786 14088761 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2787 2830926 : ptrs.safe_push (&ce->value);
2788 : }
2789 12003936 : return t;
2790 12003936 : }
2791 :
2792 : /* If T is a CONSTRUCTOR, ggc_free T and any sub-CONSTRUCTORs. */
2793 :
2794 : static void
2795 809302 : free_constructor (tree t)
2796 : {
2797 809302 : if (!t || TREE_CODE (t) != CONSTRUCTOR)
2798 0 : return;
2799 809302 : releasing_vec ctors;
2800 809302 : vec_safe_push (ctors, t);
2801 1656414 : while (!ctors->is_empty ())
2802 : {
2803 847112 : tree c = ctors->pop ();
2804 847112 : if (vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (c))
2805 : {
2806 : constructor_elt *ce;
2807 316072 : for (HOST_WIDE_INT i = 0; vec_safe_iterate (elts, i, &ce); ++i)
2808 203846 : if (ce->value && TREE_CODE (ce->value) == CONSTRUCTOR)
2809 37810 : vec_safe_push (ctors, ce->value);
2810 112226 : ggc_free (elts);
2811 : }
2812 847112 : ggc_free (c);
2813 : }
2814 809302 : }
2815 :
2816 : /* Helper function of cxx_bind_parameters_in_call. Return non-NULL
2817 : if *TP is address of a static variable (or part of it) currently being
2818 : constructed or of a heap artificial variable. */
2819 :
2820 : static tree
2821 23709106 : addr_of_non_const_var (tree *tp, int *walk_subtrees, void *data)
2822 : {
2823 23709106 : if (TREE_CODE (*tp) == ADDR_EXPR)
2824 3641384 : if (tree var = get_base_address (TREE_OPERAND (*tp, 0)))
2825 3641384 : if (VAR_P (var) && TREE_STATIC (var))
2826 : {
2827 1709511 : if (DECL_NAME (var) == heap_uninit_identifier
2828 1709511 : || DECL_NAME (var) == heap_identifier
2829 1709511 : || DECL_NAME (var) == heap_vec_uninit_identifier
2830 3419022 : || DECL_NAME (var) == heap_vec_identifier)
2831 : return var;
2832 :
2833 1709511 : constexpr_global_ctx *global = (constexpr_global_ctx *) data;
2834 1709511 : if (global->get_value (var))
2835 : return var;
2836 : }
2837 23356466 : if (TYPE_P (*tp))
2838 1839 : *walk_subtrees = false;
2839 : return NULL_TREE;
2840 : }
2841 :
2842 : /* Subroutine of cxx_eval_call_expression.
2843 : We are processing a call expression (either CALL_EXPR or
2844 : AGGR_INIT_EXPR) in the context of CTX. Evaluate
2845 : all arguments and bind their values to correspondings
2846 : parameters, making up the NEW_CALL context. */
2847 :
2848 : static tree
2849 123776589 : cxx_bind_parameters_in_call (const constexpr_ctx *ctx, tree t, tree fun,
2850 : tree orig_fun, bool *non_constant_p,
2851 : bool *overflow_p, bool *non_constant_args,
2852 : tree *jump_target)
2853 : {
2854 123776589 : int nargs = call_expr_nargs (t);
2855 123776589 : tree parms = DECL_ARGUMENTS (fun);
2856 123776589 : int i, j = 0;
2857 123776589 : if (DECL_HAS_IN_CHARGE_PARM_P (fun) && fun != orig_fun)
2858 1236 : ++nargs;
2859 123776589 : if (DECL_HAS_VTT_PARM_P (fun)
2860 1238 : && fun != orig_fun
2861 123777825 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2862 902 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2863 345 : ++nargs;
2864 : /* We don't record ellipsis args below. */
2865 123776589 : int nparms = list_length (parms);
2866 123776589 : int nbinds = nargs < nparms ? nargs : nparms;
2867 123776589 : tree binds = make_tree_vec (nbinds);
2868 :
2869 : /* The call is not a constant expression if it involves the cdtor for a type
2870 : with virtual bases before C++26. */
2871 123776589 : if (cxx_dialect < cxx26
2872 123776589 : && (DECL_HAS_IN_CHARGE_PARM_P (fun) || DECL_HAS_VTT_PARM_P (fun)))
2873 : {
2874 17 : if (!ctx->quiet)
2875 : {
2876 3 : error_at (cp_expr_loc_or_input_loc (t),
2877 : "call to non-%<constexpr%> function %qD", fun);
2878 3 : explain_invalid_constexpr_fn (fun);
2879 : }
2880 17 : *non_constant_p = true;
2881 17 : return binds;
2882 : }
2883 :
2884 203701416 : for (i = 0; i < nargs; ++i)
2885 : {
2886 138440136 : tree x, arg;
2887 138440136 : tree type = parms ? TREE_TYPE (parms) : void_type_node;
2888 138440136 : if (parms && DECL_BY_REFERENCE (parms))
2889 8146 : type = TREE_TYPE (type);
2890 138440136 : if (i == 1
2891 138440136 : && j == 0
2892 26712823 : && DECL_HAS_IN_CHARGE_PARM_P (fun)
2893 138441261 : && orig_fun != fun)
2894 : {
2895 1125 : if (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2896 1125 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun))
2897 325 : x = boolean_true_node;
2898 : else
2899 800 : x = boolean_false_node;
2900 : j = -1;
2901 : }
2902 138439011 : else if (i == 2
2903 138439011 : && j == -1
2904 1125 : && DECL_HAS_VTT_PARM_P (fun)
2905 1125 : && orig_fun != fun
2906 138440136 : && (DECL_COMPLETE_CONSTRUCTOR_P (orig_fun)
2907 811 : || DECL_COMPLETE_DESTRUCTOR_P (orig_fun)))
2908 : {
2909 325 : x = build_zero_cst (type);
2910 325 : j = -2;
2911 : }
2912 : else
2913 138438686 : x = get_nth_callarg (t, i + j);
2914 : /* For member function, the first argument is a pointer to the implied
2915 : object. For a constructor, it might still be a dummy object, in
2916 : which case we get the real argument from ctx. */
2917 214193044 : if (i == 0 && DECL_CONSTRUCTOR_P (fun)
2918 155360411 : && is_dummy_object (x))
2919 : {
2920 8055907 : x = ctx->object;
2921 8055907 : x = build_address (x);
2922 : }
2923 138440136 : if (TREE_ADDRESSABLE (type))
2924 : {
2925 : /* Undo convert_for_arg_passing work here. */
2926 12892 : x = convert_from_reference (x);
2927 12892 : arg = cxx_eval_constant_expression (ctx, x, vc_glvalue,
2928 : non_constant_p, overflow_p,
2929 : jump_target);
2930 : }
2931 : else
2932 : /* Normally we would strip a TARGET_EXPR in an initialization context
2933 : such as this, but here we do the elision differently: we keep the
2934 : TARGET_EXPR, and use its CONSTRUCTOR as the value of the parm. */
2935 138427244 : arg = cxx_eval_constant_expression (ctx, x, vc_prvalue,
2936 : non_constant_p, overflow_p,
2937 : jump_target);
2938 138440136 : if (*jump_target)
2939 : break;
2940 : /* Check we aren't dereferencing a null pointer when calling a non-static
2941 : member function, which is undefined behaviour. */
2942 107096493 : if (i == 0 && DECL_OBJECT_MEMBER_FUNCTION_P (fun)
2943 62788299 : && integer_zerop (arg)
2944 : /* But ignore calls from within compiler-generated code, to handle
2945 : cases like lambda function pointer conversion operator thunks
2946 : which pass NULL as the 'this' pointer. */
2947 138553974 : && !(TREE_CODE (t) == CALL_EXPR && CALL_FROM_THUNK_P (t)))
2948 : {
2949 312 : if (!ctx->quiet)
2950 6 : error_at (cp_expr_loc_or_input_loc (x),
2951 : "dereferencing a null pointer");
2952 312 : *non_constant_p = true;
2953 : }
2954 : /* Don't VERIFY_CONSTANT here. */
2955 138440095 : if (*non_constant_p && ctx->quiet)
2956 : break;
2957 : /* Just discard ellipsis args after checking their constantitude. */
2958 79924844 : if (!parms)
2959 283 : continue;
2960 :
2961 79924561 : if (!*non_constant_p)
2962 : {
2963 : /* Make sure the binding has the same type as the parm. But
2964 : only for constant args. */
2965 79923917 : if (TREE_ADDRESSABLE (type))
2966 : {
2967 8941 : if (!same_type_p (type, TREE_TYPE (arg)))
2968 : {
2969 9 : arg = build_fold_addr_expr (arg);
2970 9 : arg = cp_fold_convert (build_reference_type (type), arg);
2971 9 : arg = convert_from_reference (arg);
2972 : }
2973 : }
2974 79914976 : else if (!TYPE_REF_P (type))
2975 61302935 : arg = adjust_temp_type (type, arg);
2976 79923917 : if (!TREE_CONSTANT (arg))
2977 53807801 : *non_constant_args = true;
2978 26116116 : else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2979 : /* The destructor needs to see any modifications the callee makes
2980 : to the argument. */
2981 0 : *non_constant_args = true;
2982 : /* If arg is or contains address of a heap artificial variable or
2983 : of a static variable being constructed, avoid caching the
2984 : function call, as those variables might be modified by the
2985 : function, or might be modified by the callers in between
2986 : the cached function and just read by the function. */
2987 26116116 : else if (!*non_constant_args
2988 26116116 : && cp_walk_tree (&arg, addr_of_non_const_var, ctx->global,
2989 : NULL))
2990 352640 : *non_constant_args = true;
2991 :
2992 : /* For virtual calls, adjust the this argument, so that it is
2993 : the object on which the method is called, rather than
2994 : one of its bases. */
2995 79923917 : if (i == 0 && DECL_VIRTUAL_P (fun))
2996 : {
2997 16677 : tree addr = arg;
2998 16677 : STRIP_NOPS (addr);
2999 16677 : if (TREE_CODE (addr) == ADDR_EXPR)
3000 : {
3001 16657 : tree obj = TREE_OPERAND (addr, 0);
3002 16657 : while (TREE_CODE (obj) == COMPONENT_REF
3003 7830 : && DECL_FIELD_IS_BASE (TREE_OPERAND (obj, 1))
3004 24489 : && !same_type_ignoring_top_level_qualifiers_p
3005 7778 : (TREE_TYPE (obj), DECL_CONTEXT (fun)))
3006 54 : obj = TREE_OPERAND (obj, 0);
3007 16657 : if (obj != TREE_OPERAND (addr, 0))
3008 51 : arg = build_fold_addr_expr_with_type (obj,
3009 : TREE_TYPE (arg));
3010 : }
3011 : }
3012 79923917 : TREE_VEC_ELT (binds, i) = arg;
3013 : }
3014 79924561 : parms = TREE_CHAIN (parms);
3015 : }
3016 :
3017 : return binds;
3018 : }
3019 :
3020 : static int
3021 64753238 : push_cx_call_context (tree call)
3022 : {
3023 64753238 : ++call_stack_tick;
3024 64753238 : if (!EXPR_HAS_LOCATION (call))
3025 55294 : SET_EXPR_LOCATION (call, input_location);
3026 64753238 : call_stack.safe_push (call);
3027 64753238 : int len = call_stack.length ();
3028 64753238 : if (len > max_constexpr_depth)
3029 30 : return false;
3030 : return len;
3031 : }
3032 :
3033 : static void
3034 64753236 : pop_cx_call_context (void)
3035 : {
3036 64753236 : ++call_stack_tick;
3037 64753236 : call_stack.pop ();
3038 64753236 : }
3039 :
3040 : vec<tree>
3041 243717 : cx_error_context (void)
3042 : {
3043 243717 : vec<tree> r = vNULL;
3044 243717 : if (call_stack_tick != last_cx_error_tick
3045 243717 : && !call_stack.is_empty ())
3046 : r = call_stack;
3047 243717 : last_cx_error_tick = call_stack_tick;
3048 243717 : return r;
3049 : }
3050 :
3051 : /* E is an operand of a failed assertion, fold it either with or without
3052 : constexpr context. */
3053 :
3054 : static tree
3055 591 : fold_operand (tree e, const constexpr_ctx *ctx)
3056 : {
3057 591 : if (ctx)
3058 : {
3059 32 : bool new_non_constant_p = false, new_overflow_p = false;
3060 32 : tree jmp_target = NULL_TREE;
3061 32 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
3062 : &new_non_constant_p,
3063 : &new_overflow_p, &jmp_target);
3064 : }
3065 : else
3066 559 : e = fold_non_dependent_expr (e, tf_none, /*manifestly_const_eval=*/true);
3067 591 : return e;
3068 : }
3069 :
3070 : /* If we have a condition in conjunctive normal form (CNF), find the first
3071 : failing clause. In other words, given an expression like
3072 :
3073 : true && true && false && true && false
3074 :
3075 : return the first 'false'. EXPR is the expression. */
3076 :
3077 : static tree
3078 320 : find_failing_clause_r (const constexpr_ctx *ctx, tree expr)
3079 : {
3080 421 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3081 : {
3082 : /* First check the left side... */
3083 186 : tree e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 0));
3084 186 : if (e == NULL_TREE)
3085 : /* ...if we didn't find a false clause, check the right side. */
3086 101 : e = find_failing_clause_r (ctx, TREE_OPERAND (expr, 1));
3087 : return e;
3088 : }
3089 235 : tree e = contextual_conv_bool (expr, tf_none);
3090 235 : e = fold_operand (e, ctx);
3091 235 : if (integer_zerop (e))
3092 : /* This is the failing clause. */
3093 134 : return expr;
3094 : return NULL_TREE;
3095 : }
3096 :
3097 : /* Wrapper for find_failing_clause_r. */
3098 :
3099 : tree
3100 1611 : find_failing_clause (const constexpr_ctx *ctx, tree expr)
3101 : {
3102 1611 : if (TREE_CODE (expr) == TRUTH_ANDIF_EXPR)
3103 134 : if (tree e = find_failing_clause_r (ctx, expr))
3104 1611 : expr = e;
3105 1611 : return expr;
3106 : }
3107 :
3108 : /* Emit additional diagnostics for failing condition BAD.
3109 : Used by finish_static_assert and IFN_ASSUME constexpr diagnostics.
3110 : If SHOW_EXPR_P is true, print the condition (because it was
3111 : instantiation-dependent). */
3112 :
3113 : void
3114 1611 : diagnose_failing_condition (tree bad, location_t cloc, bool show_expr_p,
3115 : const constexpr_ctx *ctx /* = nullptr */)
3116 : {
3117 : /* Nobody wants to see the artificial (bool) cast. */
3118 1611 : bad = tree_strip_nop_conversions (bad);
3119 1611 : if (TREE_CODE (bad) == CLEANUP_POINT_EXPR)
3120 3 : bad = TREE_OPERAND (bad, 0);
3121 :
3122 1611 : auto_diagnostic_nesting_level sentinel;
3123 :
3124 : /* Actually explain the failure if this is a concept check or a
3125 : requires-expression. */
3126 1611 : if (concept_check_p (bad) || TREE_CODE (bad) == REQUIRES_EXPR)
3127 287 : diagnose_constraints (cloc, bad, NULL_TREE);
3128 : /* Similarly if this is a standard trait. */
3129 1324 : else if (maybe_diagnose_standard_trait (cloc, bad))
3130 : ;
3131 996 : else if (COMPARISON_CLASS_P (bad)
3132 996 : && (ARITHMETIC_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))
3133 30 : || REFLECTION_TYPE_P (TREE_TYPE (TREE_OPERAND (bad, 0)))))
3134 : {
3135 178 : tree op0 = fold_operand (TREE_OPERAND (bad, 0), ctx);
3136 178 : tree op1 = fold_operand (TREE_OPERAND (bad, 1), ctx);
3137 178 : tree cond = build2 (TREE_CODE (bad), boolean_type_node, op0, op1);
3138 178 : inform (cloc, "the comparison reduces to %qE", cond);
3139 : }
3140 818 : else if (show_expr_p)
3141 576 : inform (cloc, "%qE evaluates to false", bad);
3142 1611 : }
3143 :
3144 : /* Process an assert/assume of ORIG_ARG. If it's not supposed to be evaluated,
3145 : do it without changing the current evaluation state. If it evaluates to
3146 : false, complain and return false; otherwise, return true. */
3147 :
3148 : static bool
3149 173449 : cxx_eval_assert (const constexpr_ctx *ctx, tree arg, const char *msg,
3150 : location_t loc, bool evaluated,
3151 : bool *non_constant_p, bool *overflow_p)
3152 : {
3153 173449 : if (*non_constant_p)
3154 : return true;
3155 :
3156 173449 : tree eval, jmp_target = NULL_TREE;
3157 173449 : if (!evaluated)
3158 : {
3159 173449 : if (!potential_rvalue_constant_expression (arg))
3160 16 : return true;
3161 :
3162 173433 : constexpr_ctx new_ctx = *ctx;
3163 173433 : new_ctx.quiet = true;
3164 173433 : bool new_non_constant_p = false, new_overflow_p = false;
3165 : /* Avoid modification of existing values. */
3166 173433 : modifiable_tracker ms (new_ctx.global);
3167 173433 : eval = cxx_eval_constant_expression (&new_ctx, arg, vc_prvalue,
3168 : &new_non_constant_p,
3169 : &new_overflow_p, &jmp_target);
3170 173433 : }
3171 : else
3172 0 : eval = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
3173 : non_constant_p,
3174 : overflow_p, &jmp_target);
3175 173433 : if (jmp_target)
3176 : return true;
3177 :
3178 173433 : if (!*non_constant_p && integer_zerop (eval))
3179 : {
3180 42 : if (!ctx->quiet)
3181 : {
3182 : /* See if we can find which clause was failing
3183 : (for logical AND). */
3184 13 : tree bad = find_failing_clause (ctx, arg);
3185 : /* If not, or its location is unusable, fall back to the
3186 : previous location. */
3187 13 : location_t cloc = cp_expr_loc_or_loc (bad, loc);
3188 :
3189 : /* Report the error. */
3190 13 : auto_diagnostic_group d;
3191 13 : error_at (cloc, msg);
3192 13 : diagnose_failing_condition (bad, cloc, true, ctx);
3193 13 : return bad;
3194 13 : }
3195 29 : *non_constant_p = true;
3196 29 : return false;
3197 : }
3198 :
3199 : return true;
3200 : }
3201 :
3202 : /* Evaluate a call T to a GCC internal function when possible and return
3203 : the evaluated result or, under the control of CTX, give an error, set
3204 : NON_CONSTANT_P, and return the unevaluated call T otherwise. */
3205 :
3206 : static tree
3207 368290 : cxx_eval_internal_function (const constexpr_ctx *ctx, tree t,
3208 : value_cat lval,
3209 : bool *non_constant_p, bool *overflow_p,
3210 : tree *jump_target)
3211 : {
3212 368290 : enum tree_code opcode = ERROR_MARK;
3213 :
3214 368290 : switch (CALL_EXPR_IFN (t))
3215 : {
3216 159 : case IFN_UBSAN_NULL:
3217 159 : case IFN_UBSAN_BOUNDS:
3218 159 : case IFN_UBSAN_VPTR:
3219 159 : case IFN_FALLTHROUGH:
3220 159 : return void_node;
3221 :
3222 173449 : case IFN_ASSUME:
3223 173449 : if (!cxx_eval_assert (ctx, CALL_EXPR_ARG (t, 0),
3224 : G_("failed %<assume%> attribute assumption"),
3225 173449 : EXPR_LOCATION (t), /*eval*/false,
3226 : non_constant_p, overflow_p))
3227 : return t;
3228 173420 : return void_node;
3229 :
3230 : case IFN_ADD_OVERFLOW:
3231 : opcode = PLUS_EXPR;
3232 : break;
3233 355 : case IFN_SUB_OVERFLOW:
3234 355 : opcode = MINUS_EXPR;
3235 355 : break;
3236 191775 : case IFN_MUL_OVERFLOW:
3237 191775 : opcode = MULT_EXPR;
3238 191775 : break;
3239 :
3240 74 : case IFN_LAUNDER:
3241 74 : return cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3242 : vc_prvalue, non_constant_p,
3243 74 : overflow_p, jump_target);
3244 :
3245 0 : case IFN_DEFERRED_INIT:
3246 0 : return build_clobber (TREE_TYPE (t), CLOBBER_OBJECT_BEGIN);
3247 :
3248 2011 : case IFN_VEC_CONVERT:
3249 2011 : {
3250 2011 : tree arg = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0),
3251 : vc_prvalue, non_constant_p,
3252 : overflow_p, jump_target);
3253 2011 : if (*jump_target)
3254 : return NULL_TREE;
3255 2011 : if (TREE_CODE (arg) == VECTOR_CST)
3256 1912 : if (tree r = fold_const_call (CFN_VEC_CONVERT, TREE_TYPE (t), arg))
3257 : return r;
3258 : }
3259 : /* FALLTHRU */
3260 :
3261 104 : default:
3262 104 : if (!ctx->quiet)
3263 0 : error_at (cp_expr_loc_or_input_loc (t),
3264 : "call to internal function %qE", t);
3265 104 : *non_constant_p = true;
3266 104 : return t;
3267 : }
3268 :
3269 : /* Evaluate constant arguments using OPCODE and return a complex
3270 : number containing the result and the overflow bit. */
3271 192597 : tree arg0 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 0), lval,
3272 : non_constant_p, overflow_p,
3273 : jump_target);
3274 192597 : tree arg1 = cxx_eval_constant_expression (ctx, CALL_EXPR_ARG (t, 1), lval,
3275 : non_constant_p, overflow_p,
3276 : jump_target);
3277 192597 : if (*jump_target)
3278 : return NULL_TREE;
3279 192597 : if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3280 : {
3281 2 : location_t loc = cp_expr_loc_or_input_loc (t);
3282 2 : tree type = TREE_TYPE (TREE_TYPE (t));
3283 2 : tree result = fold_binary_loc (loc, opcode, type,
3284 : fold_convert_loc (loc, type, arg0),
3285 : fold_convert_loc (loc, type, arg1));
3286 2 : tree ovf
3287 2 : = build_int_cst (type, arith_overflowed_p (opcode, type, arg0, arg1));
3288 : /* Reset TREE_OVERFLOW to avoid warnings for the overflow. */
3289 2 : if (TREE_OVERFLOW (result))
3290 0 : TREE_OVERFLOW (result) = 0;
3291 :
3292 2 : return build_complex (TREE_TYPE (t), result, ovf);
3293 : }
3294 :
3295 192595 : *non_constant_p = true;
3296 192595 : return t;
3297 : }
3298 :
3299 : /* Clean CONSTRUCTOR_NO_CLEARING from CTOR and its sub-aggregates. */
3300 :
3301 : static void
3302 4212152 : clear_no_implicit_zero (tree ctor)
3303 : {
3304 4212152 : if (CONSTRUCTOR_NO_CLEARING (ctor))
3305 : {
3306 925271 : CONSTRUCTOR_NO_CLEARING (ctor) = false;
3307 3933707 : for (auto &e: CONSTRUCTOR_ELTS (ctor))
3308 1239468 : if (TREE_CODE (e.value) == CONSTRUCTOR)
3309 395383 : clear_no_implicit_zero (e.value);
3310 : }
3311 4212152 : }
3312 :
3313 : /* Complain about a const object OBJ being modified in a constant expression.
3314 : EXPR is the MODIFY_EXPR expression performing the modification. */
3315 :
3316 : static void
3317 63 : modifying_const_object_error (tree expr, tree obj)
3318 : {
3319 63 : location_t loc = cp_expr_loc_or_input_loc (expr);
3320 63 : auto_diagnostic_group d;
3321 126 : error_at (loc, "modifying a const object %qE is not allowed in "
3322 63 : "a constant expression", TREE_OPERAND (expr, 0));
3323 :
3324 : /* Find the underlying object that was declared as const. */
3325 63 : location_t decl_loc = UNKNOWN_LOCATION;
3326 138 : for (tree probe = obj; decl_loc == UNKNOWN_LOCATION; )
3327 75 : switch (TREE_CODE (probe))
3328 : {
3329 39 : case BIT_FIELD_REF:
3330 39 : case COMPONENT_REF:
3331 39 : {
3332 39 : tree elt = TREE_OPERAND (probe, 1);
3333 39 : if (CP_TYPE_CONST_P (TREE_TYPE (elt)))
3334 27 : decl_loc = DECL_SOURCE_LOCATION (elt);
3335 39 : probe = TREE_OPERAND (probe, 0);
3336 : }
3337 39 : break;
3338 :
3339 0 : case ARRAY_REF:
3340 0 : case REALPART_EXPR:
3341 0 : case IMAGPART_EXPR:
3342 0 : probe = TREE_OPERAND (probe, 0);
3343 0 : break;
3344 :
3345 36 : default:
3346 36 : decl_loc = location_of (probe);
3347 36 : break;
3348 : }
3349 63 : inform (decl_loc, "originally declared %<const%> here");
3350 63 : }
3351 :
3352 : /* Return true if FNDECL is a replaceable global allocation function that
3353 : should be useable during constant expression evaluation. */
3354 :
3355 : static inline bool
3356 27773180 : cxx_replaceable_global_alloc_fn (tree fndecl)
3357 : {
3358 27773180 : return (cxx_dialect >= cxx20
3359 26262200 : && IDENTIFIER_NEWDEL_OP_P (DECL_NAME (fndecl))
3360 1607488 : && CP_DECL_CONTEXT (fndecl) == global_namespace
3361 29380354 : && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fndecl)
3362 815135 : || DECL_IS_OPERATOR_DELETE_P (fndecl)));
3363 : }
3364 :
3365 : /* Return true if FNDECL is a placement new function that should be
3366 : useable during constant expression evaluation of std::construct_at. */
3367 :
3368 : static inline bool
3369 26795644 : cxx_placement_new_fn (tree fndecl)
3370 : {
3371 26795644 : return (cxx_dialect >= cxx20 && std_placement_new_fn_p (fndecl));
3372 : }
3373 :
3374 : /* Return true if FNDECL is std::construct_at. */
3375 :
3376 : static inline bool
3377 1152632 : is_std_construct_at (tree fndecl)
3378 : {
3379 1152632 : if (!decl_in_std_namespace_p (fndecl))
3380 : return false;
3381 :
3382 1129872 : tree name = DECL_NAME (fndecl);
3383 1129872 : return name && id_equal (name, "construct_at");
3384 : }
3385 :
3386 : /* Overload for the above taking constexpr_call*. */
3387 :
3388 : static inline bool
3389 909498 : is_std_construct_at (const constexpr_call *call)
3390 : {
3391 909498 : return (call
3392 770671 : && call->fundef
3393 1680169 : && is_std_construct_at (call->fundef->decl));
3394 : }
3395 :
3396 : /* True if CTX is an instance of std::NAME class. */
3397 :
3398 : bool
3399 10535952 : is_std_class (tree ctx, const char *name)
3400 : {
3401 10535952 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3402 : return false;
3403 :
3404 10535027 : tree decl = TYPE_MAIN_DECL (ctx);
3405 10535027 : tree dname = DECL_NAME (decl);
3406 10535027 : if (dname == NULL_TREE || !id_equal (dname, name))
3407 : return false;
3408 :
3409 548148 : return decl_in_std_namespace_p (decl);
3410 : }
3411 :
3412 : /* True if CTX is an instance of std::allocator. */
3413 :
3414 : bool
3415 400951 : is_std_allocator (tree ctx)
3416 : {
3417 400951 : return is_std_class (ctx, "allocator");
3418 : }
3419 :
3420 : /* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
3421 :
3422 : static bool
3423 415740 : is_std_allocator_allocate (tree fndecl)
3424 : {
3425 415740 : tree name = DECL_NAME (fndecl);
3426 415740 : if (name == NULL_TREE
3427 415740 : || !(id_equal (name, "allocate") || id_equal (name, "deallocate")))
3428 : return false;
3429 :
3430 396311 : return is_std_allocator (DECL_CONTEXT (fndecl));
3431 : }
3432 :
3433 : /* Overload for the above taking constexpr_call*. */
3434 :
3435 : static inline bool
3436 244152 : is_std_allocator_allocate (const constexpr_call *call)
3437 : {
3438 244152 : return (call
3439 153741 : && call->fundef
3440 397893 : && is_std_allocator_allocate (call->fundef->decl));
3441 : }
3442 :
3443 : /* Return true if FNDECL is std::source_location::current. */
3444 :
3445 : static inline bool
3446 33223 : is_std_source_location_current (tree fndecl)
3447 : {
3448 33223 : if (!decl_in_std_namespace_p (fndecl))
3449 : return false;
3450 :
3451 24439 : tree name = DECL_NAME (fndecl);
3452 24439 : if (name == NULL_TREE || !id_equal (name, "current"))
3453 : return false;
3454 :
3455 159 : tree ctx = DECL_CONTEXT (fndecl);
3456 159 : if (ctx == NULL_TREE || !CLASS_TYPE_P (ctx) || !TYPE_MAIN_DECL (ctx))
3457 : return false;
3458 :
3459 159 : name = DECL_NAME (TYPE_MAIN_DECL (ctx));
3460 159 : return name && id_equal (name, "source_location");
3461 : }
3462 :
3463 : /* Overload for the above taking constexpr_call*. */
3464 :
3465 : static inline bool
3466 41578 : is_std_source_location_current (const constexpr_call *call)
3467 : {
3468 41578 : return (call
3469 33223 : && call->fundef
3470 74801 : && is_std_source_location_current (call->fundef->decl));
3471 : }
3472 :
3473 : /* Return true if FNDECL is __dynamic_cast. */
3474 :
3475 : static inline bool
3476 26504159 : cxx_dynamic_cast_fn_p (tree fndecl)
3477 : {
3478 26504159 : return (cxx_dialect >= cxx20
3479 24993174 : && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
3480 5361 : && CP_DECL_CONTEXT (fndecl) == abi_node
3481 : /* Only consider implementation-detail __dynamic_cast calls that
3482 : correspond to a dynamic_cast, and ignore direct calls to
3483 : abi::__dynamic_cast. */
3484 26509520 : && DECL_ARTIFICIAL (fndecl));
3485 : }
3486 :
3487 : /* Often, we have an expression in the form of address + offset, e.g.
3488 : "&_ZTV1A + 16". Extract the object from it, i.e. "_ZTV1A". */
3489 :
3490 : static tree
3491 4608 : extract_obj_from_addr_offset (tree expr)
3492 : {
3493 4608 : if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
3494 2173 : expr = TREE_OPERAND (expr, 0);
3495 4608 : STRIP_NOPS (expr);
3496 4608 : if (TREE_CODE (expr) == ADDR_EXPR)
3497 4597 : expr = TREE_OPERAND (expr, 0);
3498 4608 : return expr;
3499 : }
3500 :
3501 : /* Given a PATH like
3502 :
3503 : g.D.2181.D.2154.D.2102.D.2093
3504 :
3505 : find a component with type TYPE. Return NULL_TREE if not found, and
3506 : error_mark_node if the component is not accessible. If STOP is non-null,
3507 : this function will return NULL_TREE if STOP is found before TYPE. */
3508 :
3509 : static tree
3510 2218 : get_component_with_type (tree path, tree type, tree stop)
3511 : {
3512 6416 : while (true)
3513 : {
3514 4317 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path), type))
3515 : /* Found it. */
3516 : return path;
3517 3148 : else if (stop
3518 3148 : && (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (path),
3519 : stop)))
3520 : return NULL_TREE;
3521 3103 : else if (TREE_CODE (path) == COMPONENT_REF
3522 3103 : && DECL_FIELD_IS_BASE (TREE_OPERAND (path, 1)))
3523 : {
3524 : /* We need to check that the component we're accessing is in fact
3525 : accessible. */
3526 3103 : if (TREE_PRIVATE (TREE_OPERAND (path, 1))
3527 3103 : || TREE_PROTECTED (TREE_OPERAND (path, 1)))
3528 1004 : return error_mark_node;
3529 2099 : path = TREE_OPERAND (path, 0);
3530 : }
3531 : else
3532 : return NULL_TREE;
3533 : }
3534 : }
3535 :
3536 : /* Evaluate a call to __dynamic_cast (permitted by P1327R1).
3537 :
3538 : The declaration of __dynamic_cast is:
3539 :
3540 : void* __dynamic_cast (const void* __src_ptr,
3541 : const __class_type_info* __src_type,
3542 : const __class_type_info* __dst_type,
3543 : ptrdiff_t __src2dst);
3544 :
3545 : where src2dst has the following possible values
3546 :
3547 : >-1: src_type is a unique public non-virtual base of dst_type
3548 : dst_ptr + src2dst == src_ptr
3549 : -1: unspecified relationship
3550 : -2: src_type is not a public base of dst_type
3551 : -3: src_type is a multiple public non-virtual base of dst_type */
3552 :
3553 : static tree
3554 2554 : cxx_eval_dynamic_cast_fn (const constexpr_ctx *ctx, tree call,
3555 : bool *non_constant_p, bool *overflow_p,
3556 : tree *jump_target)
3557 : {
3558 : /* T will be something like
3559 : __dynamic_cast ((B*) b, &_ZTI1B, &_ZTI1D, 8)
3560 : dismantle it. */
3561 2554 : gcc_assert (call_expr_nargs (call) == 4);
3562 2554 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
3563 2554 : tree obj = CALL_EXPR_ARG (call, 0);
3564 2554 : tree type = CALL_EXPR_ARG (call, 2);
3565 2554 : HOST_WIDE_INT hint = int_cst_value (CALL_EXPR_ARG (call, 3));
3566 2554 : location_t loc = cp_expr_loc_or_input_loc (call);
3567 :
3568 : /* Get the target type of the dynamic_cast. */
3569 2554 : gcc_assert (TREE_CODE (type) == ADDR_EXPR);
3570 2554 : type = TREE_OPERAND (type, 0);
3571 2554 : type = TREE_TYPE (DECL_NAME (type));
3572 :
3573 : /* TYPE can only be either T* or T&. We can't know which of these it
3574 : is by looking at TYPE, but OBJ will be "(T*) x" in the first case,
3575 : and something like "(T*)(T&)(T*) x" in the second case.
3576 : This is true for the reference cases in C++ < 26 or when exceptions
3577 : aren't enabled, in that case we should diagnose errors. For C++26
3578 : with exceptions we should silently evaluate to null pointer and
3579 : let the callers call __cxa_bad_cast () later to throw an exception. */
3580 2554 : bool fail_for_non_constant_p = false;
3581 11597 : while (CONVERT_EXPR_P (obj) || TREE_CODE (obj) == SAVE_EXPR)
3582 : {
3583 9043 : if (cxx_dialect < cxx26 || !flag_exceptions)
3584 5557 : fail_for_non_constant_p |= TYPE_REF_P (TREE_TYPE (obj));
3585 9043 : obj = TREE_OPERAND (obj, 0);
3586 : }
3587 :
3588 : /* Evaluate the object so that we know its dynamic type. */
3589 2554 : obj = cxx_eval_constant_expression (ctx, obj, vc_prvalue, non_constant_p,
3590 : overflow_p, jump_target);
3591 2554 : if (*non_constant_p)
3592 : return call;
3593 2435 : if (*jump_target)
3594 : return NULL_TREE;
3595 :
3596 : /* For dynamic_cast from classes with virtual bases we can get something
3597 : like (virt_base *)(&d + 16) as OBJ. Try to convert that into
3598 : d.D.1234 using cxx_fold_indirect_ref. */
3599 2435 : if (cxx_dialect >= cxx26 && CONVERT_EXPR_P (obj))
3600 : {
3601 607 : tree objo = obj;
3602 607 : STRIP_NOPS (objo);
3603 607 : if (TREE_CODE (objo) == POINTER_PLUS_EXPR)
3604 : {
3605 576 : objo = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (TREE_TYPE (obj)),
3606 : obj, NULL, jump_target);
3607 576 : if (objo)
3608 576 : obj = build_fold_addr_expr (objo);
3609 : }
3610 : }
3611 :
3612 : /* We expect OBJ to be in form of &d.D.2102 when HINT == 0,
3613 : but when HINT is > 0, it can also be something like
3614 : &d.D.2102 + 18446744073709551608, which includes the BINFO_OFFSET. */
3615 2435 : obj = extract_obj_from_addr_offset (obj);
3616 2435 : const tree objtype = TREE_TYPE (obj);
3617 : /* If OBJ doesn't refer to a base field, we're done. */
3618 4797 : if (tree t = (TREE_CODE (obj) == COMPONENT_REF
3619 2435 : ? TREE_OPERAND (obj, 1) : obj))
3620 2435 : if (TREE_CODE (t) != FIELD_DECL || !DECL_FIELD_IS_BASE (t))
3621 : {
3622 73 : if (fail_for_non_constant_p)
3623 : {
3624 55 : if (!ctx->quiet)
3625 : {
3626 2 : auto_diagnostic_group d;
3627 2 : error_at (loc, "reference %<dynamic_cast%> failed");
3628 2 : inform (loc, "dynamic type %qT of its operand does "
3629 : "not have a base class of type %qT",
3630 : objtype, type);
3631 2 : }
3632 55 : *non_constant_p = true;
3633 : }
3634 73 : return integer_zero_node;
3635 : }
3636 :
3637 : /* [class.cdtor] When a dynamic_cast is used in a constructor ...
3638 : or in a destructor ... if the operand of the dynamic_cast refers
3639 : to the object under construction or destruction, this object is
3640 : considered to be a most derived object that has the type of the
3641 : constructor or destructor's class. */
3642 2362 : tree vtable = build_vfield_ref (obj, objtype);
3643 2362 : vtable = cxx_eval_constant_expression (ctx, vtable, vc_prvalue,
3644 : non_constant_p, overflow_p,
3645 : jump_target);
3646 2362 : if (*non_constant_p)
3647 : return call;
3648 2185 : if (*jump_target)
3649 : return NULL_TREE;
3650 : /* With -fsanitize=vptr, we initialize all vtable pointers to null,
3651 : so it's possible that we got a null pointer now. */
3652 2185 : if (integer_zerop (vtable))
3653 : {
3654 12 : if (!ctx->quiet)
3655 3 : error_at (loc, "virtual table pointer is used uninitialized");
3656 12 : *non_constant_p = true;
3657 12 : return integer_zero_node;
3658 : }
3659 : /* VTABLE will be &_ZTV1A + 16 or similar, get _ZTV1A. */
3660 2173 : vtable = extract_obj_from_addr_offset (vtable);
3661 2173 : const tree mdtype = DECL_CONTEXT (vtable);
3662 :
3663 : /* Given dynamic_cast<T>(v),
3664 :
3665 : [expr.dynamic.cast] If C is the class type to which T points or refers,
3666 : the runtime check logically executes as follows:
3667 :
3668 : If, in the most derived object pointed (referred) to by v, v points
3669 : (refers) to a public base class subobject of a C object, and if only
3670 : one object of type C is derived from the subobject pointed (referred)
3671 : to by v the result points (refers) to that C object.
3672 :
3673 : In this case, HINT >= 0 or -3. */
3674 2173 : if (hint >= 0 || hint == -3)
3675 : {
3676 : /* Look for a component with type TYPE. */
3677 664 : tree t = get_component_with_type (obj, type, mdtype);
3678 : /* If not accessible, give an error. */
3679 664 : if (t == error_mark_node)
3680 : {
3681 198 : if (fail_for_non_constant_p)
3682 : {
3683 108 : if (!ctx->quiet)
3684 : {
3685 12 : auto_diagnostic_group d;
3686 12 : error_at (loc, "reference %<dynamic_cast%> failed");
3687 12 : inform (loc, "static type %qT of its operand is a "
3688 : "non-public base class of dynamic type %qT",
3689 : objtype, type);
3690 :
3691 12 : }
3692 108 : *non_constant_p = true;
3693 : }
3694 198 : return integer_zero_node;
3695 : }
3696 466 : else if (t)
3697 : /* The result points to the TYPE object. */
3698 421 : return cp_build_addr_expr (t, complain);
3699 : /* Else, TYPE was not found, because the HINT turned out to be wrong.
3700 : Fall through to the normal processing. */
3701 : }
3702 :
3703 : /* Otherwise, if v points (refers) to a public base class subobject of the
3704 : most derived object, and the type of the most derived object has a base
3705 : class, of type C, that is unambiguous and public, the result points
3706 : (refers) to the C subobject of the most derived object.
3707 :
3708 : But it can also be an invalid case. */
3709 :
3710 : /* Get the most derived object. */
3711 1554 : obj = get_component_with_type (obj, mdtype, NULL_TREE);
3712 1554 : if (obj == error_mark_node)
3713 : {
3714 806 : if (fail_for_non_constant_p)
3715 : {
3716 350 : if (!ctx->quiet)
3717 : {
3718 38 : auto_diagnostic_group d;
3719 38 : error_at (loc, "reference %<dynamic_cast%> failed");
3720 38 : inform (loc, "static type %qT of its operand is a non-public"
3721 : " base class of dynamic type %qT", objtype, mdtype);
3722 38 : }
3723 350 : *non_constant_p = true;
3724 : }
3725 806 : return integer_zero_node;
3726 : }
3727 : else
3728 748 : gcc_assert (obj);
3729 :
3730 : /* Check that the type of the most derived object has a base class
3731 : of type TYPE that is unambiguous and public. */
3732 748 : base_kind b_kind;
3733 748 : tree binfo = lookup_base (mdtype, type, ba_check, &b_kind, tf_none);
3734 748 : if (!binfo || binfo == error_mark_node)
3735 : {
3736 339 : if (fail_for_non_constant_p)
3737 : {
3738 134 : if (!ctx->quiet)
3739 : {
3740 16 : auto_diagnostic_group d;
3741 16 : error_at (loc, "reference %<dynamic_cast%> failed");
3742 16 : if (b_kind == bk_ambig)
3743 6 : inform (loc, "%qT is an ambiguous base class of dynamic "
3744 : "type %qT of its operand", type, mdtype);
3745 : else
3746 10 : inform (loc, "dynamic type %qT of its operand does not "
3747 : "have an unambiguous public base class %qT",
3748 : mdtype, type);
3749 16 : }
3750 134 : *non_constant_p = true;
3751 : }
3752 339 : return integer_zero_node;
3753 : }
3754 : /* If so, return the TYPE subobject of the most derived object. */
3755 409 : obj = convert_to_base_statically (obj, binfo);
3756 409 : return cp_build_addr_expr (obj, complain);
3757 : }
3758 :
3759 : /* Data structure used by replace_decl and replace_decl_r. */
3760 :
3761 : struct replace_decl_data
3762 : {
3763 : /* The _DECL we want to replace. */
3764 : tree decl;
3765 : /* The replacement for DECL. */
3766 : tree replacement;
3767 : /* Trees we've visited. */
3768 : hash_set<tree> *pset;
3769 : /* Whether we've performed any replacements. */
3770 : bool changed;
3771 : };
3772 :
3773 : /* Helper function for replace_decl, called through cp_walk_tree. */
3774 :
3775 : static tree
3776 9088482 : replace_decl_r (tree *tp, int *walk_subtrees, void *data)
3777 : {
3778 9088482 : replace_decl_data *d = (replace_decl_data *) data;
3779 :
3780 : /* We could be replacing
3781 : &<retval>.bar -> &foo.bar
3782 : where foo is a static VAR_DECL, so we need to recompute TREE_CONSTANT
3783 : on the ADDR_EXPR around it. */
3784 9088482 : if (TREE_CODE (*tp) == ADDR_EXPR)
3785 : {
3786 616696 : d->pset->add (*tp);
3787 616696 : auto save_changed = d->changed;
3788 616696 : d->changed = false;
3789 616696 : cp_walk_tree (&TREE_OPERAND (*tp, 0), replace_decl_r, d, nullptr);
3790 616696 : if (d->changed)
3791 : {
3792 290 : cxx_mark_addressable (*tp);
3793 290 : recompute_tree_invariant_for_addr_expr (*tp);
3794 : }
3795 : else
3796 616406 : d->changed = save_changed;
3797 616696 : *walk_subtrees = 0;
3798 : }
3799 8471786 : else if (*tp == d->decl)
3800 : {
3801 513 : *tp = unshare_expr (d->replacement);
3802 513 : d->changed = true;
3803 513 : *walk_subtrees = 0;
3804 : }
3805 8471273 : else if (TYPE_P (*tp)
3806 8471273 : || d->pset->add (*tp))
3807 2341772 : *walk_subtrees = 0;
3808 :
3809 9088482 : return NULL_TREE;
3810 : }
3811 :
3812 : /* Replace every occurrence of DECL with (an unshared copy of)
3813 : REPLACEMENT within the expression *TP. Returns true iff a
3814 : replacement was performed. */
3815 :
3816 : bool
3817 1114663 : replace_decl (tree *tp, tree decl, tree replacement)
3818 : {
3819 1114663 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
3820 : (TREE_TYPE (decl), TREE_TYPE (replacement)));
3821 1114663 : hash_set<tree> pset;
3822 1114663 : replace_decl_data data = { decl, replacement, &pset, false };
3823 1114663 : cp_walk_tree (tp, replace_decl_r, &data, NULL);
3824 1114663 : return data.changed;
3825 1114663 : }
3826 :
3827 : /* Evaluate the call T to virtual function thunk THUNK_FNDECL. */
3828 :
3829 : static tree
3830 30 : cxx_eval_thunk_call (const constexpr_ctx *ctx, tree t, tree thunk_fndecl,
3831 : value_cat lval,
3832 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
3833 : {
3834 30 : tree function = THUNK_TARGET (thunk_fndecl);
3835 :
3836 30 : if (THUNK_VIRTUAL_OFFSET (thunk_fndecl))
3837 : {
3838 13 : if (!ctx->quiet)
3839 : {
3840 3 : if (!DECL_DECLARED_CONSTEXPR_P (function))
3841 : {
3842 0 : error ("call to non-%<constexpr%> function %qD", function);
3843 0 : explain_invalid_constexpr_fn (function);
3844 : }
3845 : else
3846 : /* virtual_offset is only set for virtual bases, which make the
3847 : class non-literal, so we don't need to handle it here. */
3848 3 : error ("calling constexpr member function %qD through virtual "
3849 : "base subobject", function);
3850 : }
3851 13 : *non_constant_p = true;
3852 13 : return t;
3853 : }
3854 :
3855 17 : tree new_call = copy_node (t);
3856 17 : CALL_EXPR_FN (new_call) = function;
3857 17 : TREE_TYPE (new_call) = TREE_TYPE (TREE_TYPE (function));
3858 :
3859 17 : tree offset = size_int (THUNK_FIXED_OFFSET (thunk_fndecl));
3860 :
3861 17 : if (DECL_THIS_THUNK_P (thunk_fndecl))
3862 : {
3863 : /* 'this'-adjusting thunk. */
3864 11 : tree this_arg = CALL_EXPR_ARG (t, 0);
3865 11 : this_arg = build2 (POINTER_PLUS_EXPR, TREE_TYPE (this_arg),
3866 : this_arg, offset);
3867 11 : CALL_EXPR_ARG (new_call, 0) = this_arg;
3868 : }
3869 : else
3870 : /* Return-adjusting thunk. */
3871 6 : new_call = build2 (POINTER_PLUS_EXPR, TREE_TYPE (new_call),
3872 : new_call, offset);
3873 :
3874 17 : return cxx_eval_constant_expression (ctx, new_call, lval,
3875 : non_constant_p, overflow_p,
3876 17 : jump_target);
3877 : }
3878 :
3879 : /* If OBJECT is of const class type, evaluate it to a CONSTRUCTOR and set
3880 : its TREE_READONLY flag according to READONLY_P. Used for constexpr
3881 : 'tors to detect modifying const objects in a constexpr context. */
3882 :
3883 : static void
3884 7859944 : cxx_set_object_constness (const constexpr_ctx *ctx, tree object,
3885 : bool readonly_p, bool *non_constant_p,
3886 : bool *overflow_p, tree *jump_target)
3887 : {
3888 15719888 : if (CLASS_TYPE_P (TREE_TYPE (object))
3889 15719888 : && CP_TYPE_CONST_P (TREE_TYPE (object)))
3890 : {
3891 : /* Subobjects might not be stored in ctx->global->values but we
3892 : can get its CONSTRUCTOR by evaluating *this. */
3893 952008 : tree e = cxx_eval_constant_expression (ctx, object, vc_prvalue,
3894 : non_constant_p, overflow_p,
3895 : jump_target);
3896 952008 : if (!*non_constant_p
3897 8776890 : && !throws (jump_target)
3898 1868954 : && TREE_CODE (e) == CONSTRUCTOR)
3899 916946 : TREE_READONLY (e) = readonly_p;
3900 : }
3901 7859944 : }
3902 :
3903 : /* Allocate an exception for OBJECT and throw it. */
3904 :
3905 : tree
3906 1573 : cxa_allocate_and_throw_exception (location_t loc, const constexpr_ctx *ctx,
3907 : tree object)
3908 : {
3909 1573 : tree type = TREE_TYPE (object);
3910 : /* This simulates a call to __cxa_allocate_exception. We need
3911 : (struct exception *) &heap -- memory on the heap so that
3912 : it can survive the stack being unwound. */
3913 1573 : tree arr = build_array_of_n_type (type, 1);
3914 1573 : tree var = cxa_allocate_exception (loc, ctx, arr, size_zero_node);
3915 1573 : DECL_NAME (var) = heap_identifier;
3916 1573 : ctx->global->put_value (var, NULL_TREE);
3917 :
3918 : /* *(struct exception *) &heap = exc{ ... } */
3919 1573 : tree ptr = build_nop (build_pointer_type (type), build_address (var));
3920 1573 : object = cp_build_init_expr (cp_build_fold_indirect_ref (ptr), object);
3921 1573 : bool non_constant_p = false, overflow_p = false;
3922 1573 : tree jump_target = NULL_TREE;
3923 1573 : cxx_eval_constant_expression (ctx, object, vc_prvalue, &non_constant_p,
3924 : &overflow_p, &jump_target);
3925 1573 : if (non_constant_p)
3926 : {
3927 0 : if (!ctx->quiet)
3928 0 : error_at (loc, "couldn%'t throw %qT", type);
3929 0 : return NULL_TREE;
3930 : }
3931 :
3932 : /* Now we can __cxa_throw. */
3933 1573 : DECL_EXCEPTION_REFCOUNT (var)
3934 1573 : = size_binop (PLUS_EXPR, DECL_EXCEPTION_REFCOUNT (var), size_one_node);
3935 1573 : ++ctx->global->uncaught_exceptions;
3936 :
3937 1573 : return var;
3938 : }
3939 :
3940 : /* Subroutine of cxx_eval_constant_expression.
3941 : Evaluate the call expression tree T in the context of OLD_CALL expression
3942 : evaluation. */
3943 :
3944 : static tree
3945 139021591 : cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
3946 : value_cat lval,
3947 : bool *non_constant_p, bool *overflow_p,
3948 : tree *jump_target)
3949 : {
3950 139021591 : location_t loc = cp_expr_loc_or_input_loc (t);
3951 139021591 : tree fun = get_function_named_in_call (t);
3952 :
3953 139021591 : if (fun == NULL_TREE)
3954 368290 : return cxx_eval_internal_function (ctx, t, lval,
3955 : non_constant_p, overflow_p,
3956 368290 : jump_target);
3957 :
3958 138653301 : if (TREE_CODE (fun) != FUNCTION_DECL)
3959 : {
3960 : /* Might be a constexpr function pointer. */
3961 214709 : fun = cxx_eval_constant_expression (ctx, fun, vc_prvalue,
3962 : non_constant_p, overflow_p,
3963 : jump_target);
3964 214709 : if (*jump_target)
3965 : return NULL_TREE;
3966 214709 : STRIP_NOPS (fun);
3967 214709 : if (TREE_CODE (fun) == ADDR_EXPR)
3968 20198 : fun = TREE_OPERAND (fun, 0);
3969 : /* For TARGET_VTABLE_USES_DESCRIPTORS targets, there is no
3970 : indirection, the called expression is a pointer into the
3971 : virtual table which should contain FDESC_EXPR. Extract the
3972 : FUNCTION_DECL from there. */
3973 : else if (TARGET_VTABLE_USES_DESCRIPTORS
3974 : && TREE_CODE (fun) == POINTER_PLUS_EXPR
3975 : && TREE_CODE (TREE_OPERAND (fun, 0)) == ADDR_EXPR
3976 : && TREE_CODE (TREE_OPERAND (fun, 1)) == INTEGER_CST)
3977 : {
3978 : tree d = TREE_OPERAND (TREE_OPERAND (fun, 0), 0);
3979 : if (VAR_P (d)
3980 : && DECL_VTABLE_OR_VTT_P (d)
3981 : && TREE_CODE (TREE_TYPE (d)) == ARRAY_TYPE
3982 : && TREE_TYPE (TREE_TYPE (d)) == vtable_entry_type
3983 : && DECL_INITIAL (d)
3984 : && TREE_CODE (DECL_INITIAL (d)) == CONSTRUCTOR)
3985 : {
3986 : tree i = int_const_binop (TRUNC_DIV_EXPR, TREE_OPERAND (fun, 1),
3987 : TYPE_SIZE_UNIT (vtable_entry_type));
3988 : HOST_WIDE_INT idx = find_array_ctor_elt (DECL_INITIAL (d), i);
3989 : if (idx >= 0)
3990 : {
3991 : tree fdesc
3992 : = (*CONSTRUCTOR_ELTS (DECL_INITIAL (d)))[idx].value;
3993 : if (TREE_CODE (fdesc) == FDESC_EXPR
3994 : && integer_zerop (TREE_OPERAND (fdesc, 1)))
3995 : fun = TREE_OPERAND (fdesc, 0);
3996 : }
3997 : }
3998 : }
3999 : }
4000 138653301 : if (TREE_CODE (fun) != FUNCTION_DECL)
4001 : {
4002 194511 : if (!ctx->quiet && !*non_constant_p)
4003 0 : error_at (loc, "expression %qE does not designate a %<constexpr%> "
4004 : "function", fun);
4005 194511 : *non_constant_p = true;
4006 194511 : return t;
4007 : }
4008 138458790 : tree orig_fun = fun;
4009 138458790 : if (DECL_CLONED_FUNCTION_P (fun) && !DECL_DELETING_DESTRUCTOR_P (fun))
4010 17718678 : fun = DECL_CLONED_FUNCTION (fun);
4011 :
4012 138458790 : if (is_ubsan_builtin_p (fun))
4013 58 : return void_node;
4014 :
4015 138458732 : if (fndecl_built_in_p (fun))
4016 14094979 : return cxx_eval_builtin_function_call (ctx, t, fun,
4017 : lval, non_constant_p, overflow_p,
4018 14094978 : jump_target);
4019 124363753 : if (DECL_THUNK_P (fun))
4020 30 : return cxx_eval_thunk_call (ctx, t, fun, lval, non_constant_p, overflow_p,
4021 30 : jump_target);
4022 124363723 : if (metafunction_p (fun))
4023 : {
4024 : /* To be able to evaluate a metafunction, we may have to instantiate
4025 : constexpr functions. If we're not allowed to instantiate, leave
4026 : this for later. Don't evaluate metafunctions at all when mce_unknown,
4027 : otherwise we might fold those prematurely. See
4028 : g++.dg/reflect/p2996-17.C. */
4029 36630 : if (uid_sensitive_constexpr_evaluation_p ()
4030 36352 : || ctx->manifestly_const_eval == mce_unknown)
4031 : {
4032 9907 : *non_constant_p = true;
4033 9907 : return t;
4034 : }
4035 26723 : ctx->global->metafns_called = true;
4036 26723 : tree e = process_metafunction (ctx, fun, t, non_constant_p, overflow_p,
4037 : jump_target);
4038 26723 : if (*jump_target)
4039 : return NULL_TREE;
4040 25144 : if (*non_constant_p)
4041 : return t;
4042 24996 : e = cxx_eval_constant_expression (ctx, e, vc_prvalue,
4043 : non_constant_p, overflow_p,
4044 : jump_target);
4045 24996 : if (*jump_target)
4046 : return NULL_TREE;
4047 24996 : if (*non_constant_p)
4048 : return t;
4049 : return e;
4050 : }
4051 124327093 : bool non_constexpr_call = false;
4052 124327093 : if (!maybe_constexpr_fn (fun))
4053 : {
4054 589175 : if (TREE_CODE (t) == CALL_EXPR
4055 585983 : && cxx_replaceable_global_alloc_fn (fun)
4056 963326 : && (CALL_FROM_NEW_OR_DELETE_P (t)
4057 161994 : || is_std_allocator_allocate (ctx->call)))
4058 : {
4059 292095 : const bool new_op_p = IDENTIFIER_NEW_OP_P (DECL_NAME (fun));
4060 292095 : const int nargs = call_expr_nargs (t);
4061 292095 : tree arg0 = NULL_TREE;
4062 484173 : for (int i = 0; i < nargs; ++i)
4063 : {
4064 292851 : tree arg = CALL_EXPR_ARG (t, i);
4065 292851 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4066 : non_constant_p, overflow_p,
4067 : jump_target);
4068 292851 : if (*jump_target)
4069 : return NULL_TREE;
4070 : /* Deleting a non-constant pointer has a better error message
4071 : below. */
4072 292843 : if (new_op_p || i != 0)
4073 252731 : VERIFY_CONSTANT (arg);
4074 151966 : if (i == 0)
4075 : arg0 = arg;
4076 : }
4077 191322 : gcc_assert (arg0);
4078 191322 : if (new_op_p)
4079 : {
4080 151210 : if (!tree_fits_uhwi_p (arg0))
4081 : {
4082 : /* We should not get here; the VERIFY_CONSTANT above
4083 : should have already caught it. */
4084 0 : gcc_checking_assert (false);
4085 : if (!ctx->quiet)
4086 : error_at (loc, "cannot allocate array: size not constant");
4087 : *non_constant_p = true;
4088 : return t;
4089 : }
4090 302420 : tree type = build_array_type_nelts (char_type_node,
4091 151210 : tree_to_uhwi (arg0));
4092 151210 : tree var = build_decl (loc, VAR_DECL,
4093 151210 : (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4094 : & OVL_OP_FLAG_VEC)
4095 : ? heap_vec_uninit_identifier
4096 : : heap_uninit_identifier,
4097 151210 : type);
4098 151210 : DECL_ARTIFICIAL (var) = 1;
4099 151210 : ctx->global->heap_vars.safe_push (var);
4100 151210 : ctx->global->put_value (var, NULL_TREE);
4101 151210 : return fold_convert (ptr_type_node, build_address (var));
4102 : }
4103 : else
4104 : {
4105 40112 : STRIP_NOPS (arg0);
4106 40112 : if (TREE_CODE (arg0) == ADDR_EXPR
4107 40112 : && VAR_P (TREE_OPERAND (arg0, 0)))
4108 : {
4109 40112 : tree var = TREE_OPERAND (arg0, 0);
4110 40112 : if (DECL_NAME (var) == heap_uninit_identifier
4111 40112 : || DECL_NAME (var) == heap_identifier)
4112 : {
4113 39940 : if (IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4114 : & OVL_OP_FLAG_VEC)
4115 : {
4116 9 : if (!ctx->quiet)
4117 : {
4118 3 : auto_diagnostic_group d;
4119 3 : error_at (loc, "array deallocation of object "
4120 : "allocated with non-array "
4121 : "allocation");
4122 3 : inform (DECL_SOURCE_LOCATION (var),
4123 : "allocation performed here");
4124 3 : }
4125 9 : *non_constant_p = true;
4126 9 : return t;
4127 : }
4128 39931 : DECL_NAME (var) = heap_deleted_identifier;
4129 39931 : ctx->global->destroy_value (var);
4130 39931 : ctx->global->heap_dealloc_count++;
4131 39931 : return void_node;
4132 : }
4133 172 : else if (DECL_NAME (var) == heap_vec_uninit_identifier
4134 172 : || DECL_NAME (var) == heap_vec_identifier)
4135 : {
4136 148 : if ((IDENTIFIER_OVL_OP_FLAGS (DECL_NAME (fun))
4137 : & OVL_OP_FLAG_VEC) == 0)
4138 : {
4139 9 : if (!ctx->quiet)
4140 : {
4141 3 : auto_diagnostic_group d;
4142 3 : error_at (loc, "non-array deallocation of "
4143 : "object allocated with array "
4144 : "allocation");
4145 3 : inform (DECL_SOURCE_LOCATION (var),
4146 : "allocation performed here");
4147 3 : }
4148 9 : *non_constant_p = true;
4149 9 : return t;
4150 : }
4151 139 : DECL_NAME (var) = heap_deleted_identifier;
4152 139 : ctx->global->destroy_value (var);
4153 139 : ctx->global->heap_dealloc_count++;
4154 139 : return void_node;
4155 : }
4156 24 : else if (DECL_NAME (var) == heap_deleted_identifier)
4157 : {
4158 15 : if (!ctx->quiet)
4159 6 : error_at (loc, "deallocation of already deallocated "
4160 : "storage");
4161 15 : *non_constant_p = true;
4162 15 : return t;
4163 : }
4164 : }
4165 9 : if (!ctx->quiet)
4166 3 : error_at (loc, "deallocation of storage that was "
4167 : "not previously allocated");
4168 9 : *non_constant_p = true;
4169 9 : return t;
4170 : }
4171 : }
4172 : /* Allow placement new in std::construct_at, just return the second
4173 : argument. */
4174 297080 : if (TREE_CODE (t) == CALL_EXPR
4175 293888 : && cxx_placement_new_fn (fun)
4176 458536 : && is_std_construct_at (ctx->call))
4177 : {
4178 30945 : const int nargs = call_expr_nargs (t);
4179 30945 : tree arg1 = NULL_TREE;
4180 92835 : for (int i = 0; i < nargs; ++i)
4181 : {
4182 61890 : tree arg = CALL_EXPR_ARG (t, i);
4183 61890 : arg = cxx_eval_constant_expression (ctx, arg, vc_prvalue,
4184 : non_constant_p, overflow_p,
4185 : jump_target);
4186 61890 : if (*jump_target)
4187 : return NULL_TREE;
4188 61890 : if (i == 1)
4189 : arg1 = arg;
4190 : else
4191 61890 : VERIFY_CONSTANT (arg);
4192 : }
4193 30945 : gcc_assert (arg1);
4194 : return arg1;
4195 : }
4196 266135 : else if (cxx_dynamic_cast_fn_p (fun))
4197 2554 : return cxx_eval_dynamic_cast_fn (ctx, t, non_constant_p, overflow_p,
4198 2554 : jump_target);
4199 263581 : else if (enum cxa_builtin kind = cxx_cxa_builtin_fn_p (fun))
4200 14209 : return cxx_eval_cxa_builtin_fn (ctx, t, kind, fun,
4201 : non_constant_p, overflow_p,
4202 14209 : jump_target);
4203 :
4204 : /* Calls to non-constexpr functions can be diagnosed right away
4205 : before C++26, though in C++26 evaluation of the arguments might
4206 : throw and if caught it could be still constant expression.
4207 : So for C++26 this is diagnosed only after
4208 : cxx_bind_parameters_in_call. */
4209 249372 : if (cxx_dialect >= cxx26)
4210 : non_constexpr_call = true;
4211 : else
4212 : {
4213 210701 : if (!ctx->quiet)
4214 : {
4215 193 : if (!lambda_static_thunk_p (fun))
4216 193 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4217 193 : explain_invalid_constexpr_fn (fun);
4218 : }
4219 210701 : *non_constant_p = true;
4220 210701 : return t;
4221 : }
4222 : }
4223 :
4224 123776589 : constexpr_ctx new_ctx = *ctx;
4225 123776589 : ctx = &new_ctx;
4226 140696881 : if (DECL_CONSTRUCTOR_P (fun) && !ctx->object
4227 126218528 : && TREE_CODE (t) == AGGR_INIT_EXPR)
4228 : {
4229 : /* We want to have an initialization target for an AGGR_INIT_EXPR.
4230 : If we don't already have one in CTX, use the AGGR_INIT_EXPR_SLOT. */
4231 1864 : new_ctx.object = AGGR_INIT_EXPR_SLOT (t);
4232 1864 : tree ctor = new_ctx.ctor = build_constructor (DECL_CONTEXT (fun), NULL);
4233 1864 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4234 1864 : ctx->global->put_value (new_ctx.object, ctor);
4235 : }
4236 : /* An immediate invocation is manifestly constant evaluated including the
4237 : arguments of the call, so use mce_true even for the argument
4238 : evaluation. */
4239 247553178 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4240 2933330 : new_ctx.manifestly_const_eval = mce_true;
4241 :
4242 : /* We used to shortcut trivial constructor/op= here, but nowadays
4243 : we can only get a trivial function here with -fno-elide-constructors. */
4244 123776629 : gcc_checking_assert (!trivial_fn_p (fun)
4245 : || !flag_elide_constructors
4246 : /* Or it's a call from maybe_thunk_body (111075). */
4247 : || (TREE_CODE (t) == CALL_EXPR ? CALL_FROM_THUNK_P (t)
4248 : : AGGR_INIT_FROM_THUNK_P (t))
4249 : /* We don't elide constructors when processing
4250 : a noexcept-expression. */
4251 : || cp_noexcept_operand);
4252 :
4253 123776589 : bool non_constant_args = false;
4254 123776589 : constexpr_call new_call;
4255 123776589 : new_call.bindings
4256 123776589 : = cxx_bind_parameters_in_call (ctx, t, fun, orig_fun, non_constant_p,
4257 : overflow_p, &non_constant_args,
4258 : jump_target);
4259 :
4260 : /* We build up the bindings list before we know whether we already have this
4261 : call cached. If we don't end up saving these bindings, ggc_free them when
4262 : this function exits. */
4263 123776589 : class free_bindings
4264 : {
4265 : tree *bindings;
4266 : public:
4267 123776589 : free_bindings (tree &b): bindings (&b) { }
4268 123776587 : ~free_bindings () { if (bindings) ggc_free (*bindings); }
4269 4246086 : void preserve () { bindings = NULL; }
4270 123776589 : } fb (new_call.bindings);
4271 :
4272 123776589 : if (*jump_target)
4273 : return NULL_TREE;
4274 123776548 : if (*non_constant_p)
4275 : return t;
4276 65260645 : if (non_constexpr_call)
4277 : {
4278 3000 : if (!ctx->quiet)
4279 : {
4280 219 : if (!lambda_static_thunk_p (fun))
4281 219 : error_at (loc, "call to non-%<constexpr%> function %qD", fun);
4282 219 : explain_invalid_constexpr_fn (fun);
4283 : }
4284 3000 : *non_constant_p = true;
4285 3000 : return t;
4286 : }
4287 :
4288 : /* We can't defer instantiating the function any longer. */
4289 65257645 : if (!DECL_INITIAL (fun)
4290 2876919 : && (DECL_TEMPLOID_INSTANTIATION (fun) || DECL_DEFAULTED_FN (fun))
4291 65257645 : && !uid_sensitive_constexpr_evaluation_p ())
4292 : {
4293 2625603 : location_t save_loc = input_location;
4294 2625603 : input_location = loc;
4295 2625603 : ++function_depth;
4296 2625603 : if (ctx->manifestly_const_eval == mce_true)
4297 311427 : FNDECL_MANIFESTLY_CONST_EVALUATED (fun) = true;
4298 2625603 : if (DECL_TEMPLOID_INSTANTIATION (fun))
4299 2625596 : instantiate_decl (fun, /*defer_ok*/false, /*expl_inst*/false);
4300 : else
4301 7 : synthesize_method (fun);
4302 2625603 : --function_depth;
4303 2625603 : input_location = save_loc;
4304 : }
4305 :
4306 : /* If in direct recursive call, optimize definition search. */
4307 65257645 : if (ctx && ctx->call && ctx->call->fundef && ctx->call->fundef->decl == fun)
4308 27560 : new_call.fundef = ctx->call->fundef;
4309 : else
4310 : {
4311 65230085 : new_call.fundef = retrieve_constexpr_fundef (fun);
4312 65230085 : if (new_call.fundef == NULL || new_call.fundef->body == NULL
4313 64740950 : || new_call.fundef->result == error_mark_node
4314 64725681 : || fun == current_function_decl)
4315 : {
4316 504407 : if (!ctx->quiet)
4317 : {
4318 : /* We need to check for current_function_decl here in case we're
4319 : being called during cp_fold_function, because at that point
4320 : DECL_INITIAL is set properly and we have a fundef but we
4321 : haven't lowered invisirefs yet (c++/70344). */
4322 170 : if (DECL_INITIAL (fun) == error_mark_node
4323 170 : || fun == current_function_decl)
4324 15 : error_at (loc, "%qD called in a constant expression before its "
4325 : "definition is complete", fun);
4326 155 : else if (DECL_INITIAL (fun))
4327 : {
4328 : /* The definition of fun was somehow unsuitable. But pretend
4329 : that lambda static thunks don't exist. */
4330 118 : if (!lambda_static_thunk_p (fun))
4331 112 : error_at (loc, "%qD called in a constant expression", fun);
4332 118 : explain_invalid_constexpr_fn (fun);
4333 : }
4334 : else
4335 37 : error_at (loc, "%qD used before its definition", fun);
4336 : }
4337 504407 : *non_constant_p = true;
4338 504407 : return t;
4339 : }
4340 : }
4341 :
4342 : /* Don't complain about problems evaluating an ill-formed function. */
4343 64753238 : if (function *f = DECL_STRUCT_FUNCTION (fun))
4344 64753238 : if (f->language->erroneous)
4345 593 : new_ctx.quiet = true;
4346 :
4347 64753238 : int depth_ok = push_cx_call_context (t);
4348 :
4349 : /* Remember the object we are constructing or destructing. */
4350 64753238 : tree new_obj = NULL_TREE;
4351 129506476 : if (DECL_CONSTRUCTOR_P (fun) || DECL_DESTRUCTOR_P (fun))
4352 : {
4353 : /* In a cdtor, it should be the first `this' argument.
4354 : At this point it has already been evaluated in the call
4355 : to cxx_bind_parameters_in_call. */
4356 9623041 : new_obj = TREE_VEC_ELT (new_call.bindings, 0);
4357 9623041 : bool empty_base = false;
4358 9623041 : new_obj = cxx_fold_indirect_ref (ctx, loc, DECL_CONTEXT (fun), new_obj,
4359 : &empty_base, jump_target);
4360 9623041 : if (*jump_target)
4361 0 : return NULL_TREE;
4362 : /* If we're initializing an empty class, don't set constness, because
4363 : cxx_fold_indirect_ref will return the wrong object to set constness
4364 : of. */
4365 9623041 : if (empty_base)
4366 : new_obj = NULL_TREE;
4367 4207147 : else if (ctx->call && ctx->call->fundef
4368 16313837 : && DECL_CONSTRUCTOR_P (ctx->call->fundef->decl))
4369 : {
4370 2337671 : tree cur_obj = TREE_VEC_ELT (ctx->call->bindings, 0);
4371 2337671 : STRIP_NOPS (cur_obj);
4372 2337671 : if (TREE_CODE (cur_obj) == ADDR_EXPR)
4373 2336417 : cur_obj = TREE_OPERAND (cur_obj, 0);
4374 2337671 : if (new_obj == cur_obj)
4375 : /* We're calling the target constructor of a delegating
4376 : constructor, or accessing a base subobject through a
4377 : NOP_EXPR as part of a call to a base constructor, so
4378 : there is no new (sub)object. */
4379 1763083 : new_obj = NULL_TREE;
4380 : }
4381 : }
4382 :
4383 64753238 : tree result = NULL_TREE;
4384 :
4385 64753238 : constexpr_call *entry = NULL;
4386 64753238 : if (depth_ok && !non_constant_args && ctx->strict)
4387 : {
4388 25500785 : new_call.hash = constexpr_fundef_hasher::hash (new_call.fundef);
4389 25500785 : new_call.hash
4390 25500785 : = iterative_hash_template_arg (new_call.bindings, new_call.hash);
4391 :
4392 : /* If we have seen this call before, we are done. */
4393 25500785 : maybe_initialize_constexpr_call_table ();
4394 25500785 : bool insert = depth_ok < constexpr_cache_depth;
4395 25500785 : constexpr_call **slot
4396 30584490 : = constexpr_call_table->find_slot (&new_call,
4397 : insert ? INSERT : NO_INSERT);
4398 25500785 : entry = slot ? *slot : NULL;
4399 24484012 : if (entry == NULL)
4400 : {
4401 : /* Only cache up to constexpr_cache_depth to limit memory use. */
4402 5262859 : if (insert)
4403 : {
4404 : /* We need to keep a pointer to the entry, not just the slot, as
4405 : the slot can move during evaluation of the body. */
4406 4246086 : *slot = entry = ggc_alloc<constexpr_call> ();
4407 4246086 : *entry = new_call;
4408 4246086 : entry->result (ctx->manifestly_const_eval) = unknown_type_node;
4409 4246086 : fb.preserve ();
4410 :
4411 : /* Unshare args going into the hash table to separate them
4412 : from the caller's context, for better GC and to avoid
4413 : problems with verify_gimple. */
4414 4246086 : tree bound = new_call.bindings;
4415 6899358 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4416 : {
4417 2653272 : tree &arg = TREE_VEC_ELT (bound, i);
4418 2653272 : arg = unshare_expr_without_location (arg);
4419 : }
4420 : }
4421 : }
4422 : /* Calls that are in progress have their result set to unknown_type_node,
4423 : so that we can detect circular dependencies. Now that we only cache
4424 : up to constexpr_cache_depth this won't catch circular dependencies that
4425 : start deeper, but they'll hit the recursion or ops limit. */
4426 20237926 : else if (entry->result (ctx->manifestly_const_eval) == unknown_type_node)
4427 : {
4428 6 : if (!ctx->quiet)
4429 0 : error ("call has circular dependency");
4430 6 : *non_constant_p = true;
4431 6 : entry->result (ctx->manifestly_const_eval) = result = error_mark_node;
4432 : }
4433 : else
4434 20237920 : result = entry->result (ctx->manifestly_const_eval);
4435 : }
4436 :
4437 64753238 : if (!depth_ok)
4438 : {
4439 30 : if (!ctx->quiet)
4440 3 : error ("%<constexpr%> evaluation depth exceeds maximum of %d (use "
4441 : "%<-fconstexpr-depth=%> to increase the maximum)",
4442 : max_constexpr_depth);
4443 30 : *non_constant_p = true;
4444 30 : result = error_mark_node;
4445 : }
4446 : else
4447 : {
4448 64753208 : bool cacheable = !!entry;
4449 64753208 : if (result && result != error_mark_node)
4450 : /* OK */;
4451 49196528 : else if (!DECL_SAVED_TREE (fun))
4452 : {
4453 : /* When at_eof >= 3, cgraph has started throwing away
4454 : DECL_SAVED_TREE, so fail quietly. FIXME we get here because of
4455 : late code generation for VEC_INIT_EXPR, which needs to be
4456 : completely reconsidered. */
4457 0 : gcc_assert (at_eof >= 3 && ctx->quiet);
4458 0 : *non_constant_p = true;
4459 : }
4460 49196528 : else if (tree copy = get_fundef_copy (new_call.fundef))
4461 : {
4462 49196522 : tree body, parms, res;
4463 49196522 : releasing_vec ctors;
4464 :
4465 : /* Reuse or create a new unshared copy of this function's body. */
4466 49196522 : body = TREE_PURPOSE (copy);
4467 49196522 : parms = TREE_VALUE (copy);
4468 49196522 : res = TREE_TYPE (copy);
4469 :
4470 : /* Associate the bindings with the remapped parms. */
4471 49196522 : tree bound = new_call.bindings;
4472 49196522 : tree remapped = parms;
4473 113315980 : for (int i = 0; i < TREE_VEC_LENGTH (bound); ++i)
4474 : {
4475 64119458 : tree arg = TREE_VEC_ELT (bound, i);
4476 64119458 : if (entry)
4477 : {
4478 : /* Unshare again so the callee doesn't change the
4479 : argument values in the hash table. XXX Could we unshare
4480 : lazily in cxx_eval_store_expression? */
4481 2812767 : arg = unshare_constructor (arg);
4482 2812767 : if (TREE_CODE (arg) == CONSTRUCTOR)
4483 808806 : vec_safe_push (ctors, arg);
4484 : }
4485 64119458 : ctx->global->put_value (remapped, arg);
4486 64119458 : remapped = DECL_CHAIN (remapped);
4487 : }
4488 49196522 : if (remapped)
4489 : {
4490 : /* We shouldn't have any parms without args, but fail gracefully
4491 : in error recovery. */
4492 6 : gcc_checking_assert (seen_error ());
4493 6 : *non_constant_p = true;
4494 : }
4495 : /* Add the RESULT_DECL to the values map, too. */
4496 49196522 : gcc_assert (!DECL_BY_REFERENCE (res));
4497 49196522 : ctx->global->put_value (res, NULL_TREE);
4498 :
4499 : /* Remember the current call we're evaluating. */
4500 49196522 : constexpr_ctx call_ctx = *ctx;
4501 49196522 : call_ctx.call = &new_call;
4502 49196522 : unsigned save_heap_alloc_count = ctx->global->heap_vars.length ();
4503 49196522 : unsigned save_heap_dealloc_count = ctx->global->heap_dealloc_count;
4504 49196522 : bool save_metafns_called = ctx->global->metafns_called;
4505 :
4506 : /* Make sure we fold std::is_constant_evaluated to true in an
4507 : immediate function. */
4508 98393044 : if (DECL_IMMEDIATE_FUNCTION_P (fun))
4509 1897029 : call_ctx.manifestly_const_eval = mce_true;
4510 :
4511 : /* If this is a constexpr destructor, the object's const and volatile
4512 : semantics are no longer in effect; see [class.dtor]p5. */
4513 57056466 : if (new_obj && DECL_DESTRUCTOR_P (fun))
4514 655075 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
4515 : non_constant_p, overflow_p, jump_target);
4516 :
4517 : /* If this is a constructor, we are beginning the lifetime of the
4518 : object we are initializing. */
4519 49196522 : if (new_obj
4520 15719888 : && DECL_CONSTRUCTOR_P (fun)
4521 7204869 : && TREE_CODE (new_obj) == COMPONENT_REF
4522 51004839 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
4523 : {
4524 4730 : tree ctor = build_constructor (TREE_TYPE (new_obj), NULL);
4525 4730 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
4526 4730 : tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
4527 : new_obj, ctor);
4528 4730 : cxx_eval_constant_expression (ctx, activate,
4529 : lval, non_constant_p, overflow_p,
4530 : jump_target);
4531 4730 : ggc_free (activate);
4532 4730 : if (*jump_target)
4533 0 : return NULL_TREE;
4534 : }
4535 :
4536 49196522 : ctx->global->metafns_called = false;
4537 :
4538 49196522 : tree jmp_target = NULL_TREE;
4539 49196522 : cxx_eval_constant_expression (&call_ctx, body,
4540 : vc_discard, non_constant_p, overflow_p,
4541 : &jmp_target);
4542 :
4543 49196520 : if (!*non_constant_p && throws (&jmp_target))
4544 : {
4545 1312 : result = NULL_TREE;
4546 1312 : cacheable = false;
4547 1312 : *jump_target = jmp_target;
4548 : }
4549 98390416 : else if (DECL_CONSTRUCTOR_P (fun))
4550 : /* This can be null for a subobject constructor call, in
4551 : which case what we care about is the initialization
4552 : side-effects rather than the value. We could get at the
4553 : value by evaluating *this, but we don't bother; there's
4554 : no need to put such a call in the hash table. */
4555 8884266 : result = lval ? ctx->object : ctx->ctor;
4556 40310942 : else if (VOID_TYPE_P (TREE_TYPE (res)))
4557 3131093 : result = void_node;
4558 : else
4559 : {
4560 37179849 : result = ctx->global->get_value (res);
4561 9102439 : if (result == NULL_TREE && !*non_constant_p
4562 37180053 : && !DECL_DESTRUCTOR_P (fun))
4563 : {
4564 102 : if (!ctx->quiet)
4565 6 : error ("%<constexpr%> call flows off the end "
4566 : "of the function");
4567 102 : *non_constant_p = true;
4568 : }
4569 : }
4570 :
4571 49196520 : if (ctx->global->metafns_called)
4572 29424 : cacheable = false;
4573 49196520 : ctx->global->metafns_called |= save_metafns_called;
4574 :
4575 : /* At this point, the object's constructor will have run, so
4576 : the object is no longer under construction, and its possible
4577 : 'const' semantics now apply. Make a note of this fact by
4578 : marking the CONSTRUCTOR TREE_READONLY. */
4579 57056464 : if (new_obj && DECL_CONSTRUCTOR_P (fun))
4580 7204869 : cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/true,
4581 : non_constant_p, overflow_p, jump_target);
4582 :
4583 : /* Remove the parms/result from the values map. */
4584 49196520 : destroy_value_checked (ctx, res, non_constant_p);
4585 113315983 : for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
4586 64119463 : destroy_value_checked (ctx, parm, non_constant_p);
4587 :
4588 : /* Free any parameter CONSTRUCTORs we aren't returning directly. */
4589 50005326 : while (!ctors->is_empty ())
4590 : {
4591 808806 : tree c = ctors->pop ();
4592 808806 : if (c != result)
4593 808806 : free_constructor (c);
4594 : }
4595 :
4596 : /* Make the unshared function copy we used available for re-use. */
4597 49196520 : save_fundef_copy (fun, copy);
4598 :
4599 : /* If the call allocated some heap object that hasn't been
4600 : deallocated during the call, or if it deallocated some heap
4601 : object it has not allocated, the call isn't really stateless
4602 : for the constexpr evaluation and should not be cached.
4603 : It is fine if the call allocates something and deallocates it
4604 : too. */
4605 49196520 : if (cacheable
4606 58121571 : && (save_heap_alloc_count != ctx->global->heap_vars.length ()
4607 8922440 : || (save_heap_dealloc_count
4608 8922440 : != ctx->global->heap_dealloc_count)))
4609 : {
4610 2611 : tree heap_var;
4611 2611 : unsigned int i;
4612 2611 : if ((ctx->global->heap_vars.length ()
4613 2611 : - ctx->global->heap_dealloc_count)
4614 2611 : != save_heap_alloc_count - save_heap_dealloc_count)
4615 : cacheable = false;
4616 : else
4617 70017 : FOR_EACH_VEC_ELT_FROM (ctx->global->heap_vars, i, heap_var,
4618 : save_heap_alloc_count)
4619 68438 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
4620 : {
4621 : cacheable = false;
4622 : break;
4623 : }
4624 : }
4625 :
4626 : /* Rewrite all occurrences of the function's RESULT_DECL with the
4627 : current object under construction. */
4628 49196520 : if (!*non_constant_p
4629 37171646 : && ctx->object
4630 11510603 : && CLASS_TYPE_P (TREE_TYPE (res))
4631 51107071 : && !is_empty_class (TREE_TYPE (res)))
4632 : {
4633 1114537 : if (!same_type_ignoring_top_level_qualifiers_p
4634 1114537 : (TREE_TYPE (res), TREE_TYPE (ctx->object)))
4635 0 : *non_constant_p = true;
4636 1114537 : else if (replace_decl (&result, res, ctx->object))
4637 : {
4638 238 : cacheable = false;
4639 238 : result = cxx_eval_constant_expression (ctx, result, lval,
4640 : non_constant_p,
4641 : overflow_p,
4642 : jump_target);
4643 238 : if (*jump_target)
4644 : {
4645 0 : cacheable = false;
4646 0 : result = NULL_TREE;
4647 : }
4648 : }
4649 : }
4650 :
4651 : /* Only cache a permitted result of a constant expression. */
4652 49196282 : if (cacheable && !reduced_constant_expression_p (result))
4653 : cacheable = false;
4654 :
4655 : /* Only cache a result without contract violations. */
4656 44597794 : if (cacheable && ctx->global->contract_statement)
4657 44871441 : cacheable = false;
4658 49196520 : }
4659 : else
4660 : /* Couldn't get a function copy to evaluate. */
4661 6 : *non_constant_p = true;
4662 :
4663 64753206 : if (result == error_mark_node)
4664 0 : *non_constant_p = true;
4665 64753206 : if (*non_constant_p || *overflow_p)
4666 12025036 : result = error_mark_node;
4667 52728170 : else if (!result)
4668 2426139 : result = void_node;
4669 64753206 : if (entry)
4670 : {
4671 48968022 : entry->result (ctx->manifestly_const_eval)
4672 24484011 : = cacheable ? result : error_mark_node;
4673 :
4674 24484011 : if (result != error_mark_node
4675 19921426 : && ctx->manifestly_const_eval == mce_unknown)
4676 : {
4677 : /* Evaluation succeeded and was independent of whether we're in a
4678 : manifestly constant-evaluated context, so we can also reuse
4679 : this result when evaluating this call with a fixed context. */
4680 1509540 : if (!entry->result (mce_true))
4681 574648 : entry->result (mce_true) = entry->result (mce_unknown);
4682 1509540 : if (!entry->result (mce_false))
4683 545481 : entry->result (mce_false) = entry->result (mce_unknown);
4684 : }
4685 : }
4686 : }
4687 :
4688 : /* The result of a constexpr function must be completely initialized.
4689 :
4690 : However, in C++20, a constexpr constructor doesn't necessarily have
4691 : to initialize all the fields, so we don't clear CONSTRUCTOR_NO_CLEARING
4692 : in order to detect reading an unitialized object in constexpr instead
4693 : of value-initializing it. (reduced_constant_expression_p is expected to
4694 : take care of clearing the flag.) */
4695 64753236 : if (TREE_CODE (result) == CONSTRUCTOR
4696 64753236 : && (cxx_dialect < cxx20
4697 16080820 : || !DECL_CONSTRUCTOR_P (fun)))
4698 3816769 : clear_no_implicit_zero (result);
4699 :
4700 64753236 : pop_cx_call_context ();
4701 64753236 : return result;
4702 123776587 : }
4703 :
4704 : /* Return true if T is a valid constant initializer. If a CONSTRUCTOR
4705 : initializes all the members, the CONSTRUCTOR_NO_CLEARING flag will be
4706 : cleared. If called recursively on a FIELD_DECL's CONSTRUCTOR, SZ
4707 : is DECL_SIZE of the FIELD_DECL, otherwise NULL.
4708 : FIXME speed this up, it's taking 16% of compile time on sieve testcase. */
4709 :
4710 : bool
4711 1070507955 : reduced_constant_expression_p (tree t, tree sz /* = NULL_TREE */)
4712 : {
4713 1070507955 : if (t == NULL_TREE)
4714 : return false;
4715 :
4716 1065948400 : switch (TREE_CODE (t))
4717 : {
4718 : case PTRMEM_CST:
4719 : case REFLECT_EXPR:
4720 : /* Even if we can't lower this yet, it's constant. */
4721 : return true;
4722 :
4723 : case OMP_DECLARE_MAPPER:
4724 : return true;
4725 :
4726 51171213 : case CONSTRUCTOR:
4727 : /* And we need to handle PTRMEM_CST wrapped in a CONSTRUCTOR. */
4728 51171213 : tree field;
4729 51171213 : if (!AGGREGATE_TYPE_P (TREE_TYPE (t)))
4730 : /* A constant vector would be folded to VECTOR_CST.
4731 : A CONSTRUCTOR of scalar type means uninitialized. */
4732 : return false;
4733 51155236 : if (CONSTRUCTOR_NO_CLEARING (t))
4734 : {
4735 3230481 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
4736 : {
4737 : /* There must be a valid constant initializer at every array
4738 : index. */
4739 1838 : tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4740 1838 : tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t)));
4741 1838 : tree cursor = min;
4742 26897 : for (auto &e: CONSTRUCTOR_ELTS (t))
4743 : {
4744 21493 : if (!reduced_constant_expression_p (e.value))
4745 : return false;
4746 21443 : if (array_index_cmp (cursor, e.index) != 0)
4747 : return false;
4748 21383 : if (TREE_CODE (e.index) == RANGE_EXPR)
4749 0 : cursor = TREE_OPERAND (e.index, 1);
4750 21383 : if (TREE_CODE (e.value) == RAW_DATA_CST)
4751 0 : cursor
4752 0 : = int_const_binop (PLUS_EXPR, cursor,
4753 0 : size_int (RAW_DATA_LENGTH (e.value)));
4754 : else
4755 21383 : cursor = int_const_binop (PLUS_EXPR, cursor,
4756 21383 : size_one_node);
4757 : }
4758 1728 : if (find_array_ctor_elt (t, max) == -1)
4759 : return false;
4760 1640 : goto ok;
4761 : }
4762 3228643 : else if (cxx_dialect >= cxx20
4763 3228643 : && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE)
4764 : {
4765 : /* A union can have at most one active member. A union with no
4766 : active member has no constituent values, so all constituent
4767 : values are constant. */
4768 : field = NULL_TREE;
4769 : }
4770 : else
4771 3226683 : field = next_subobject_field (TYPE_FIELDS (TREE_TYPE (t)));
4772 : }
4773 : else
4774 : field = NULL_TREE;
4775 536703708 : for (auto &e: CONSTRUCTOR_ELTS (t))
4776 : {
4777 : /* If VAL is null, we're in the middle of initializing this
4778 : element. */
4779 433060999 : if (!reduced_constant_expression_p (e.value,
4780 433060999 : (e.index
4781 433060981 : && (TREE_CODE (e.index)
4782 : == FIELD_DECL))
4783 44465684 : ? DECL_SIZE (e.index)
4784 : : NULL_TREE))
4785 : return false;
4786 : /* We want to remove initializers for empty fields in a struct to
4787 : avoid confusing output_constructor. */
4788 432557229 : if (is_empty_field (e.index)
4789 432557229 : && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
4790 : return false;
4791 : /* Check for non-empty fields between initialized fields when
4792 : CONSTRUCTOR_NO_CLEARING. */
4793 432259427 : for (; field && e.index != field;
4794 359737 : field = next_subobject_field (DECL_CHAIN (field)))
4795 363201 : if (!is_really_empty_class (TREE_TYPE (field),
4796 : /*ignore_vptr*/false))
4797 : return false;
4798 431896226 : if (field)
4799 3647190 : field = next_subobject_field (DECL_CHAIN (field));
4800 : }
4801 : /* There could be a non-empty field at the end. */
4802 50256103 : for (; field; field = next_subobject_field (DECL_CHAIN (field)))
4803 271763 : if (!is_really_empty_class (TREE_TYPE (field), /*ignore_vptr*/false))
4804 : {
4805 : /* Ignore FIELD_DECLs with bit positions beyond DECL_SIZE of
4806 : the parent FIELD_DECL (if any) for classes with virtual
4807 : bases. */
4808 4285 : if (cxx_dialect >= cxx26
4809 2332 : && sz
4810 4659 : && tree_int_cst_le (sz, bit_position (field)))
4811 : break;
4812 4033 : return false;
4813 : }
4814 49984340 : ok:
4815 49986232 : if (CONSTRUCTOR_NO_CLEARING (t))
4816 : /* All the fields are initialized. */
4817 3121543 : CONSTRUCTOR_NO_CLEARING (t) = false;
4818 : return true;
4819 :
4820 1014609316 : default:
4821 : /* FIXME are we calling this too much? */
4822 1014609316 : return initializer_constant_valid_p (t, TREE_TYPE (t)) != NULL_TREE;
4823 : }
4824 : }
4825 :
4826 : /* *TP was not deemed constant by reduced_constant_expression_p. Explain
4827 : why and suggest what could be done about it. */
4828 :
4829 : static tree
4830 1010 : verify_constant_explain_r (tree *tp, int *walk_subtrees, void *)
4831 : {
4832 1010 : bool ref_p = false;
4833 :
4834 : /* No need to look into types or unevaluated operands. */
4835 1010 : if (TYPE_P (*tp) || unevaluated_p (TREE_CODE (*tp)))
4836 : {
4837 0 : *walk_subtrees = false;
4838 0 : return NULL_TREE;
4839 : }
4840 :
4841 1010 : switch (TREE_CODE (*tp))
4842 : {
4843 85 : CASE_CONVERT:
4844 85 : if (TREE_CODE (TREE_OPERAND (*tp, 0)) != ADDR_EXPR)
4845 : break;
4846 66 : ref_p = TYPE_REF_P (TREE_TYPE (*tp));
4847 66 : *tp = TREE_OPERAND (*tp, 0);
4848 198 : gcc_fallthrough ();
4849 198 : case ADDR_EXPR:
4850 198 : {
4851 198 : tree op = TREE_OPERAND (*tp, 0);
4852 198 : if (VAR_P (op)
4853 113 : && DECL_DECLARED_CONSTEXPR_P (op)
4854 39 : && !TREE_STATIC (op)
4855 : /* ??? We should also say something about temporaries. */
4856 234 : && !DECL_ARTIFICIAL (op))
4857 : {
4858 33 : if (ref_p)
4859 12 : inform (location_of (*tp), "reference to %qD is not a constant "
4860 : "expression", op);
4861 : else
4862 21 : inform (location_of (*tp), "pointer to %qD is not a constant "
4863 : "expression", op);
4864 33 : const location_t op_loc = DECL_SOURCE_LOCATION (op);
4865 33 : rich_location richloc (line_table, op_loc);
4866 33 : richloc.add_fixit_insert_before (op_loc, "static ");
4867 33 : inform (&richloc,
4868 : "address of non-static constexpr variable %qD may differ on "
4869 : "each invocation of the enclosing function; add %<static%> "
4870 : "to give it a constant address", op);
4871 33 : }
4872 : break;
4873 : }
4874 : default:
4875 : break;
4876 : }
4877 :
4878 : return NULL_TREE;
4879 : }
4880 :
4881 : /* Some expressions may have constant operands but are not constant
4882 : themselves, such as 1/0. Call this function to check for that
4883 : condition.
4884 :
4885 : We only call this in places that require an arithmetic constant, not in
4886 : places where we might have a non-constant expression that can be a
4887 : component of a constant expression, such as the address of a constexpr
4888 : variable that might be dereferenced later. */
4889 :
4890 : static bool
4891 543519855 : verify_constant (tree t, bool allow_non_constant, bool *non_constant_p,
4892 : bool *overflow_p)
4893 : {
4894 534000395 : if (!*non_constant_p && !reduced_constant_expression_p (t)
4895 545961172 : && t != void_node)
4896 : {
4897 2441094 : if (!allow_non_constant)
4898 : {
4899 262 : auto_diagnostic_group d;
4900 377 : error_at (cp_expr_loc_or_input_loc (t),
4901 : "%q+E is not a constant expression", t);
4902 262 : cp_walk_tree_without_duplicates (&t, verify_constant_explain_r,
4903 : nullptr);
4904 262 : }
4905 2441094 : *non_constant_p = true;
4906 : }
4907 543519855 : if (TREE_OVERFLOW_P (t))
4908 : {
4909 678 : if (!allow_non_constant)
4910 : {
4911 155 : permerror (input_location, "overflow in constant expression");
4912 : /* If we're being permissive (and are in an enforcing
4913 : context), ignore the overflow. */
4914 155 : if (flag_permissive)
4915 75 : return *non_constant_p;
4916 : }
4917 603 : *overflow_p = true;
4918 : }
4919 543519780 : return *non_constant_p;
4920 : }
4921 :
4922 : /* Check whether the shift operation with code CODE and type TYPE on LHS
4923 : and RHS is undefined. If it is, give an error with an explanation,
4924 : and return true; return false otherwise. */
4925 :
4926 : static bool
4927 53121852 : cxx_eval_check_shift_p (location_t loc, const constexpr_ctx *ctx,
4928 : enum tree_code code, tree type, tree lhs, tree rhs)
4929 : {
4930 53121852 : if ((code != LSHIFT_EXPR && code != RSHIFT_EXPR)
4931 3638964 : || TREE_CODE (lhs) != INTEGER_CST
4932 3638852 : || TREE_CODE (rhs) != INTEGER_CST)
4933 : return false;
4934 :
4935 3638852 : tree lhstype = TREE_TYPE (lhs);
4936 3638852 : unsigned HOST_WIDE_INT uprec = TYPE_PRECISION (TREE_TYPE (lhs));
4937 :
4938 : /* [expr.shift] The behavior is undefined if the right operand
4939 : is negative, or greater than or equal to the length in bits
4940 : of the promoted left operand. */
4941 3638852 : if (tree_int_cst_sgn (rhs) == -1)
4942 : {
4943 126 : if (!ctx->quiet)
4944 35 : permerror (loc, "right operand of shift expression %q+E is negative",
4945 : build2_loc (loc, code, type, lhs, rhs));
4946 126 : return (!flag_permissive || ctx->quiet);
4947 : }
4948 3638726 : if (compare_tree_int (rhs, uprec) >= 0)
4949 : {
4950 200 : if (!ctx->quiet)
4951 34 : permerror (loc, "right operand of shift expression %q+E is greater "
4952 : "than or equal to the precision %wu of the left operand",
4953 : build2_loc (loc, code, type, lhs, rhs), uprec);
4954 200 : return (!flag_permissive || ctx->quiet);
4955 : }
4956 :
4957 : /* The value of E1 << E2 is E1 left-shifted E2 bit positions; [...]
4958 : if E1 has a signed type and non-negative value, and E1x2^E2 is
4959 : representable in the corresponding unsigned type of the result type,
4960 : then that value, converted to the result type, is the resulting value;
4961 : otherwise, the behavior is undefined.
4962 : For C++20:
4963 : The value of E1 << E2 is the unique value congruent to E1 x 2^E2 modulo
4964 : 2^N, where N is the range exponent of the type of the result. */
4965 3638526 : if (code == LSHIFT_EXPR
4966 3465198 : && !TYPE_OVERFLOW_WRAPS (lhstype)
4967 2031931 : && cxx_dialect >= cxx11
4968 5651931 : && cxx_dialect < cxx20)
4969 : {
4970 53848 : if (tree_int_cst_sgn (lhs) == -1)
4971 : {
4972 74 : if (!ctx->quiet)
4973 18 : permerror (loc,
4974 : "left operand of shift expression %q+E is negative",
4975 : build2_loc (loc, code, type, lhs, rhs));
4976 74 : return (!flag_permissive || ctx->quiet);
4977 : }
4978 : /* For signed x << y the following:
4979 : (unsigned) x >> ((prec (lhs) - 1) - y)
4980 : if > 1, is undefined. The right-hand side of this formula
4981 : is the highest bit of the LHS that can be set (starting from 0),
4982 : so that the shift doesn't overflow. We then right-shift the LHS
4983 : to see whether any other bit is set making the original shift
4984 : undefined -- the result is not representable in the corresponding
4985 : unsigned type. */
4986 53774 : tree t = build_int_cst (unsigned_type_node, uprec - 1);
4987 53774 : t = fold_build2 (MINUS_EXPR, unsigned_type_node, t, rhs);
4988 53774 : tree ulhs = fold_convert (unsigned_type_for (lhstype), lhs);
4989 53774 : t = fold_build2 (RSHIFT_EXPR, TREE_TYPE (ulhs), ulhs, t);
4990 53774 : if (tree_int_cst_lt (integer_one_node, t))
4991 : {
4992 41 : if (!ctx->quiet)
4993 7 : permerror (loc, "shift expression %q+E overflows",
4994 : build2_loc (loc, code, type, lhs, rhs));
4995 41 : return (!flag_permissive || ctx->quiet);
4996 : }
4997 : }
4998 : return false;
4999 : }
5000 :
5001 : /* Subroutine of cxx_eval_constant_expression.
5002 : Attempt to reduce the unary expression tree T to a compile time value.
5003 : If successful, return the value. Otherwise issue a diagnostic
5004 : and return error_mark_node. */
5005 :
5006 : static tree
5007 21089671 : cxx_eval_unary_expression (const constexpr_ctx *ctx, tree t,
5008 : bool /*lval*/,
5009 : bool *non_constant_p, bool *overflow_p,
5010 : tree *jump_target)
5011 : {
5012 21089671 : tree r;
5013 21089671 : tree orig_arg = TREE_OPERAND (t, 0);
5014 21089671 : tree arg = cxx_eval_constant_expression (ctx, orig_arg, vc_prvalue,
5015 : non_constant_p, overflow_p,
5016 : jump_target);
5017 21089670 : if (*jump_target)
5018 : return NULL_TREE;
5019 21089669 : VERIFY_CONSTANT (arg);
5020 17520176 : location_t loc = EXPR_LOCATION (t);
5021 17520176 : enum tree_code code = TREE_CODE (t);
5022 17520176 : tree type = TREE_TYPE (t);
5023 17520176 : r = fold_unary_loc (loc, code, type, arg);
5024 17520176 : if (r == NULL_TREE)
5025 : {
5026 17 : if (arg == orig_arg)
5027 : r = t;
5028 : else
5029 13 : r = build1_loc (loc, code, type, arg);
5030 : }
5031 17520176 : VERIFY_CONSTANT (r);
5032 : return r;
5033 : }
5034 :
5035 : /* Helper function for cxx_eval_binary_expression. Try to optimize
5036 : original POINTER_PLUS_EXPR T, LHS p+ RHS, return NULL_TREE if the
5037 : generic folding should be used. */
5038 :
5039 : static tree
5040 5803224 : cxx_fold_pointer_plus_expression (const constexpr_ctx *ctx, tree t,
5041 : tree lhs, tree rhs, bool *non_constant_p,
5042 : bool *overflow_p, tree *jump_target)
5043 : {
5044 5803224 : STRIP_NOPS (lhs);
5045 5803224 : if (TREE_CODE (lhs) != ADDR_EXPR)
5046 : return NULL_TREE;
5047 :
5048 5413299 : lhs = TREE_OPERAND (lhs, 0);
5049 :
5050 : /* &A[i] p+ j => &A[i + j] */
5051 5413299 : if (TREE_CODE (lhs) == ARRAY_REF
5052 100347 : && TREE_CODE (TREE_OPERAND (lhs, 1)) == INTEGER_CST
5053 100347 : && TREE_CODE (rhs) == INTEGER_CST
5054 100347 : && TYPE_SIZE_UNIT (TREE_TYPE (lhs))
5055 5513646 : && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (lhs))) == INTEGER_CST)
5056 : {
5057 100347 : tree orig_type = TREE_TYPE (t);
5058 100347 : location_t loc = EXPR_LOCATION (t);
5059 100347 : tree type = TREE_TYPE (lhs);
5060 :
5061 100347 : t = fold_convert_loc (loc, ssizetype, TREE_OPERAND (lhs, 1));
5062 100347 : tree nelts = array_type_nelts_top (TREE_TYPE (TREE_OPERAND (lhs, 0)));
5063 100347 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5064 : non_constant_p, overflow_p,
5065 : jump_target);
5066 100347 : if (*non_constant_p)
5067 : return NULL_TREE;
5068 100335 : if (*jump_target)
5069 : return NULL_TREE;
5070 : /* Don't fold an out-of-bound access. */
5071 100335 : if (!tree_int_cst_le (t, nelts))
5072 : return NULL_TREE;
5073 100335 : rhs = cp_fold_convert (ssizetype, rhs);
5074 : /* Don't fold if rhs can't be divided exactly by TYPE_SIZE_UNIT.
5075 : constexpr int A[1]; ... (char *)&A[0] + 1 */
5076 100335 : if (!integer_zerop (fold_build2_loc (loc, TRUNC_MOD_EXPR, sizetype,
5077 100335 : rhs, TYPE_SIZE_UNIT (type))))
5078 : return NULL_TREE;
5079 : /* Make sure to treat the second operand of POINTER_PLUS_EXPR
5080 : as signed. */
5081 100303 : rhs = fold_build2_loc (loc, EXACT_DIV_EXPR, ssizetype, rhs,
5082 100303 : TYPE_SIZE_UNIT (type));
5083 100303 : t = size_binop_loc (loc, PLUS_EXPR, rhs, t);
5084 100303 : t = build4_loc (loc, ARRAY_REF, type, TREE_OPERAND (lhs, 0),
5085 : t, NULL_TREE, NULL_TREE);
5086 100303 : t = cp_build_addr_expr (t, tf_warning_or_error);
5087 100303 : t = cp_fold_convert (orig_type, t);
5088 100303 : return cxx_eval_constant_expression (ctx, t, vc_prvalue,
5089 : non_constant_p, overflow_p,
5090 100303 : jump_target);
5091 : }
5092 :
5093 : return NULL_TREE;
5094 : }
5095 :
5096 : /* Try to fold expressions like
5097 : (struct S *) (&a[0].D.2378 + 12)
5098 : into
5099 : &MEM <struct T> [(void *)&a + 12B]
5100 : This is something normally done by gimple_fold_stmt_to_constant_1
5101 : on GIMPLE, but is undesirable on GENERIC if we are e.g. going to
5102 : dereference the address because some details are lost.
5103 : For pointer comparisons we want such folding though so that
5104 : match.pd address_compare optimization works. */
5105 :
5106 : static tree
5107 6978936 : cxx_maybe_fold_addr_pointer_plus (tree t)
5108 : {
5109 13957875 : while (CONVERT_EXPR_P (t)
5110 7364416 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (t, 0))))
5111 385477 : t = TREE_OPERAND (t, 0);
5112 6978936 : if (TREE_CODE (t) != POINTER_PLUS_EXPR)
5113 : return NULL_TREE;
5114 6006613 : tree op0 = TREE_OPERAND (t, 0);
5115 6006613 : tree op1 = TREE_OPERAND (t, 1);
5116 6006613 : if (TREE_CODE (op1) != INTEGER_CST)
5117 : return NULL_TREE;
5118 6006613 : while (CONVERT_EXPR_P (op0)
5119 12011194 : && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
5120 6004581 : op0 = TREE_OPERAND (op0, 0);
5121 6006613 : if (TREE_CODE (op0) != ADDR_EXPR)
5122 : return NULL_TREE;
5123 6006613 : op1 = fold_convert (ptr_type_node, op1);
5124 6006613 : tree r = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)), op0, op1);
5125 6006613 : return build1_loc (EXPR_LOCATION (t), ADDR_EXPR, TREE_TYPE (op0), r);
5126 : }
5127 :
5128 : /* Subroutine of cxx_eval_constant_expression.
5129 : Like cxx_eval_unary_expression, except for binary expressions. */
5130 :
5131 : static tree
5132 76327947 : cxx_eval_binary_expression (const constexpr_ctx *ctx, tree t,
5133 : value_cat lval,
5134 : bool *non_constant_p, bool *overflow_p,
5135 : tree *jump_target)
5136 : {
5137 76327947 : tree r = NULL_TREE;
5138 76327947 : tree orig_lhs = TREE_OPERAND (t, 0);
5139 76327947 : tree orig_rhs = TREE_OPERAND (t, 1);
5140 76327947 : tree lhs, rhs;
5141 76327947 : lhs = cxx_eval_constant_expression (ctx, orig_lhs, vc_prvalue,
5142 : non_constant_p, overflow_p,
5143 : jump_target);
5144 : /* Don't VERIFY_CONSTANT here, it's unnecessary and will break pointer
5145 : subtraction. */
5146 76327946 : if (*non_constant_p)
5147 : return t;
5148 58871737 : if (*jump_target)
5149 : return NULL_TREE;
5150 58871667 : rhs = cxx_eval_constant_expression (ctx, orig_rhs, vc_prvalue,
5151 : non_constant_p, overflow_p,
5152 : jump_target);
5153 58871667 : if (*non_constant_p)
5154 : return t;
5155 57973219 : if (*jump_target)
5156 : return NULL_TREE;
5157 :
5158 57973214 : location_t loc = EXPR_LOCATION (t);
5159 57973214 : enum tree_code code = TREE_CODE (t);
5160 57973214 : tree type = TREE_TYPE (t);
5161 :
5162 57973214 : if (code == EQ_EXPR || code == NE_EXPR)
5163 : {
5164 8930198 : bool is_code_eq = (code == EQ_EXPR);
5165 :
5166 8930198 : if (TREE_CODE (lhs) == PTRMEM_CST
5167 192 : && TREE_CODE (rhs) == PTRMEM_CST)
5168 : {
5169 61 : tree lmem = PTRMEM_CST_MEMBER (lhs);
5170 61 : tree rmem = PTRMEM_CST_MEMBER (rhs);
5171 61 : bool eq;
5172 61 : if (TREE_CODE (lmem) == TREE_CODE (rmem)
5173 61 : && TREE_CODE (lmem) == FIELD_DECL
5174 61 : && TREE_CODE (DECL_CONTEXT (lmem)) == UNION_TYPE
5175 88 : && same_type_p (DECL_CONTEXT (lmem),
5176 : DECL_CONTEXT (rmem)))
5177 : /* If both refer to (possibly different) members of the same union
5178 : (12.3), they compare equal. */
5179 : eq = true;
5180 : else
5181 34 : eq = cp_tree_equal (lhs, rhs);
5182 61 : r = constant_boolean_node (eq == is_code_eq, type);
5183 61 : }
5184 8930137 : else if ((TREE_CODE (lhs) == PTRMEM_CST
5185 8930006 : || TREE_CODE (rhs) == PTRMEM_CST)
5186 8930137 : && (null_member_pointer_value_p (lhs)
5187 131 : || null_member_pointer_value_p (rhs)))
5188 131 : r = constant_boolean_node (!is_code_eq, type);
5189 8930006 : else if (TREE_CODE (lhs) == PTRMEM_CST)
5190 0 : lhs = cplus_expand_constant (lhs);
5191 8930006 : else if (TREE_CODE (rhs) == PTRMEM_CST)
5192 0 : rhs = cplus_expand_constant (rhs);
5193 8930006 : else if (REFLECT_EXPR_P (lhs) && REFLECT_EXPR_P (rhs))
5194 : {
5195 1942 : const bool eq = compare_reflections (lhs, rhs);
5196 1942 : r = constant_boolean_node (eq == is_code_eq, type);
5197 : }
5198 : }
5199 0 : if (r == NULL_TREE
5200 57971080 : && TREE_CODE_CLASS (code) == tcc_comparison
5201 22033016 : && POINTER_TYPE_P (TREE_TYPE (lhs)))
5202 : {
5203 3489468 : if (tree lhso = cxx_maybe_fold_addr_pointer_plus (lhs))
5204 2936847 : lhs = fold_convert (TREE_TYPE (lhs), lhso);
5205 3489468 : if (tree rhso = cxx_maybe_fold_addr_pointer_plus (rhs))
5206 3069766 : rhs = fold_convert (TREE_TYPE (rhs), rhso);
5207 : }
5208 5803379 : if (code == POINTER_PLUS_EXPR && !*non_constant_p
5209 63776593 : && integer_zerop (lhs) && !integer_zerop (rhs))
5210 : {
5211 155 : if (!ctx->quiet)
5212 45 : error ("arithmetic involving a null pointer in %qE", lhs);
5213 155 : *non_constant_p = true;
5214 155 : return t;
5215 : }
5216 57973059 : else if (code == POINTER_PLUS_EXPR)
5217 : {
5218 5803224 : r = cxx_fold_pointer_plus_expression (ctx, t, lhs, rhs, non_constant_p,
5219 : overflow_p, jump_target);
5220 5803224 : if (*jump_target)
5221 : return NULL_TREE;
5222 : }
5223 52169835 : else if (code == SPACESHIP_EXPR)
5224 : {
5225 23508 : r = genericize_spaceship (loc, type, lhs, rhs);
5226 23508 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
5227 23508 : overflow_p, jump_target);
5228 : }
5229 :
5230 57949551 : if (r == NULL_TREE)
5231 : {
5232 57847114 : if (ctx->manifestly_const_eval == mce_true
5233 40269466 : && (flag_constexpr_fp_except
5234 40269463 : || TREE_CODE (type) != REAL_TYPE))
5235 : {
5236 40220905 : auto ofcc = make_temp_override (folding_cxx_constexpr, true);
5237 40220905 : r = fold_binary_initializer_loc (loc, code, type, lhs, rhs);
5238 40220905 : }
5239 : else
5240 17626209 : r = fold_binary_loc (loc, code, type, lhs, rhs);
5241 : }
5242 :
5243 57847114 : if (r == NULL_TREE
5244 4827797 : && (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
5245 100 : && TREE_CODE (lhs) == INTEGER_CST
5246 98 : && TREE_CODE (rhs) == INTEGER_CST
5247 62777348 : && wi::neg_p (wi::to_wide (rhs)))
5248 : {
5249 : /* For diagnostics and -fpermissive emulate previous behavior of
5250 : handling shifts by negative amount. */
5251 98 : tree nrhs = const_unop (NEGATE_EXPR, TREE_TYPE (rhs), rhs);
5252 98 : if (nrhs)
5253 119 : r = fold_binary_loc (loc,
5254 : code == LSHIFT_EXPR ? RSHIFT_EXPR : LSHIFT_EXPR,
5255 : type, lhs, nrhs);
5256 : }
5257 :
5258 57949551 : if (r == NULL_TREE)
5259 : {
5260 4827699 : if (lhs == orig_lhs && rhs == orig_rhs)
5261 : r = t;
5262 : else
5263 1286748 : r = build2_loc (loc, code, type, lhs, rhs);
5264 : }
5265 53121852 : else if (cxx_eval_check_shift_p (loc, ctx, code, type, lhs, rhs))
5266 433 : *non_constant_p = true;
5267 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
5268 : a local array in a constexpr function. */
5269 57949551 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (lhs));
5270 48278739 : if (!ptr)
5271 48278739 : VERIFY_CONSTANT (r);
5272 : return r;
5273 : }
5274 :
5275 : /* Subroutine of cxx_eval_constant_expression.
5276 : Attempt to evaluate condition expressions. */
5277 :
5278 : static tree
5279 18323530 : cxx_eval_conditional_expression (const constexpr_ctx *ctx, tree t,
5280 : value_cat lval,
5281 : bool *non_constant_p, bool *overflow_p,
5282 : tree *jump_target)
5283 : {
5284 18323530 : tree val = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5285 : vc_prvalue,
5286 : non_constant_p, overflow_p,
5287 : jump_target);
5288 18323530 : if (*jump_target)
5289 : return NULL_TREE;
5290 18323514 : VERIFY_CONSTANT (val);
5291 16835743 : if (TREE_CODE (t) == IF_STMT && IF_STMT_CONSTEVAL_P (t))
5292 : {
5293 : /* Evaluate the condition as if it was
5294 : if (__builtin_is_constant_evaluated ()), i.e. defer it if not
5295 : ctx->manifestly_const_eval (as sometimes we try to constant evaluate
5296 : without manifestly_const_eval even expressions or parts thereof which
5297 : will later be manifestly const_eval evaluated), otherwise fold it to
5298 : true. */
5299 662502 : if (ctx->manifestly_const_eval == mce_unknown)
5300 : {
5301 653705 : *non_constant_p = true;
5302 653705 : return t;
5303 : }
5304 8797 : val = constant_boolean_node (ctx->manifestly_const_eval == mce_true,
5305 : boolean_type_node);
5306 : }
5307 : /* Don't VERIFY_CONSTANT the other operands. */
5308 16182038 : const bool zero_p = integer_zerop (val);
5309 16182038 : if (zero_p)
5310 10214552 : val = TREE_OPERAND (t, 2);
5311 : else
5312 5967486 : val = TREE_OPERAND (t, 1);
5313 16182038 : if (TREE_CODE (t) == IF_STMT && !val)
5314 4880018 : val = void_node;
5315 :
5316 : /* P2564: If we aren't in immediate function context (including a manifestly
5317 : constant-evaluated expression), check any uses of immediate functions in
5318 : the arm we're discarding. But don't do this inside a call; we already
5319 : checked when parsing the function. */
5320 16182038 : if (ctx->manifestly_const_eval != mce_true
5321 3450280 : && !in_immediate_context ()
5322 3370094 : && !ctx->call
5323 16934733 : && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
5324 752695 : ctx->manifestly_const_eval))
5325 : {
5326 7 : *non_constant_p = true;
5327 7 : return t;
5328 : }
5329 :
5330 : /* A TARGET_EXPR may be nested inside another TARGET_EXPR, but still
5331 : serve as the initializer for the same object as the outer TARGET_EXPR,
5332 : as in
5333 : A a = true ? A{} : A{};
5334 : so strip the inner TARGET_EXPR so we don't materialize a temporary. */
5335 16182031 : if (TREE_CODE (val) == TARGET_EXPR)
5336 2235 : val = TARGET_EXPR_INITIAL (val);
5337 16182031 : return cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
5338 16182031 : overflow_p, jump_target);
5339 : }
5340 :
5341 : /* Subroutine of cxx_eval_constant_expression.
5342 : Attempt to evaluate vector condition expressions. Unlike
5343 : cxx_eval_conditional_expression, VEC_COND_EXPR acts like a normal
5344 : ternary arithmetics operation, where all 3 arguments have to be
5345 : evaluated as constants and then folding computes the result from
5346 : them. */
5347 :
5348 : static tree
5349 4582 : cxx_eval_vector_conditional_expression (const constexpr_ctx *ctx, tree t,
5350 : bool *non_constant_p, bool *overflow_p,
5351 : tree *jump_target)
5352 : {
5353 4582 : tree arg1 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
5354 : vc_prvalue,
5355 : non_constant_p, overflow_p,
5356 : jump_target);
5357 4582 : if (*jump_target)
5358 : return NULL_TREE;
5359 4582 : VERIFY_CONSTANT (arg1);
5360 3234 : tree arg2 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
5361 : vc_prvalue,
5362 : non_constant_p, overflow_p,
5363 : jump_target);
5364 3234 : if (*jump_target)
5365 : return NULL_TREE;
5366 3234 : VERIFY_CONSTANT (arg2);
5367 3167 : tree arg3 = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 2),
5368 : vc_prvalue,
5369 : non_constant_p, overflow_p,
5370 : jump_target);
5371 3167 : if (*jump_target)
5372 : return NULL_TREE;
5373 3167 : VERIFY_CONSTANT (arg3);
5374 3167 : location_t loc = EXPR_LOCATION (t);
5375 3167 : tree type = TREE_TYPE (t);
5376 3167 : tree r = fold_ternary_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5377 3167 : if (r == NULL_TREE)
5378 : {
5379 0 : if (arg1 == TREE_OPERAND (t, 0)
5380 0 : && arg2 == TREE_OPERAND (t, 1)
5381 0 : && arg3 == TREE_OPERAND (t, 2))
5382 : r = t;
5383 : else
5384 0 : r = build3_loc (loc, VEC_COND_EXPR, type, arg1, arg2, arg3);
5385 : }
5386 3167 : VERIFY_CONSTANT (r);
5387 : return r;
5388 : }
5389 :
5390 : /* Returns less than, equal to, or greater than zero if KEY is found to be
5391 : less than, to match, or to be greater than the constructor_elt's INDEX. */
5392 :
5393 : static int
5394 253369 : array_index_cmp (tree key, tree index)
5395 : {
5396 253369 : gcc_assert (TREE_CODE (key) == INTEGER_CST);
5397 :
5398 253369 : switch (TREE_CODE (index))
5399 : {
5400 250492 : case INTEGER_CST:
5401 250492 : return tree_int_cst_compare (key, index);
5402 2877 : case RANGE_EXPR:
5403 2877 : {
5404 2877 : tree lo = TREE_OPERAND (index, 0);
5405 2877 : tree hi = TREE_OPERAND (index, 1);
5406 2877 : if (tree_int_cst_lt (key, lo))
5407 : return -1;
5408 2591 : else if (tree_int_cst_lt (hi, key))
5409 : return 1;
5410 : else
5411 2591 : return 0;
5412 : }
5413 0 : default:
5414 0 : gcc_unreachable ();
5415 : }
5416 : }
5417 :
5418 : /* Extract a single INTEGER_CST from RAW_DATA_CST RAW_DATA at
5419 : relative index OFF. */
5420 :
5421 : static tree
5422 29316 : raw_data_cst_elt (tree raw_data, unsigned int off)
5423 : {
5424 29316 : return build_int_cst (TREE_TYPE (raw_data),
5425 29316 : TYPE_UNSIGNED (TREE_TYPE (raw_data))
5426 58632 : ? (HOST_WIDE_INT)
5427 29073 : RAW_DATA_UCHAR_ELT (raw_data, off)
5428 : : (HOST_WIDE_INT)
5429 243 : RAW_DATA_SCHAR_ELT (raw_data, off));
5430 : }
5431 :
5432 : /* Returns the index of the constructor_elt of ARY which matches DINDEX, or -1
5433 : if none. If INSERT is true, insert a matching element rather than fail. */
5434 :
5435 : static HOST_WIDE_INT
5436 6340737 : find_array_ctor_elt (tree ary, tree dindex, bool insert)
5437 : {
5438 6340737 : if (tree_int_cst_sgn (dindex) < 0)
5439 : return -1;
5440 :
5441 6340737 : unsigned HOST_WIDE_INT i = tree_to_uhwi (dindex);
5442 6340737 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ary);
5443 6340737 : unsigned HOST_WIDE_INT len = vec_safe_length (elts);
5444 :
5445 9562014 : unsigned HOST_WIDE_INT end = len;
5446 9562014 : unsigned HOST_WIDE_INT begin = 0;
5447 :
5448 : /* If the last element of the CONSTRUCTOR has its own index, we can assume
5449 : that the same is true of the other elements and index directly. */
5450 6030403 : if (end > 0)
5451 : {
5452 5910052 : tree cindex = (*elts)[end - 1].index;
5453 5910052 : if (cindex == NULL_TREE)
5454 : {
5455 : /* Verify that if the last index is missing, all indexes
5456 : are missing and there is no RAW_DATA_CST. */
5457 15536 : if (flag_checking)
5458 188282 : for (unsigned int j = 0; j < len - 1; ++j)
5459 172746 : gcc_assert ((*elts)[j].index == NULL_TREE
5460 : && TREE_CODE ((*elts)[j].value) != RAW_DATA_CST);
5461 15536 : if (i < end)
5462 15536 : return i;
5463 : else
5464 : {
5465 0 : begin = end;
5466 0 : if (i == end)
5467 : /* If the element is to be added right at the end,
5468 : make sure it is added with cleared index too. */
5469 3531611 : dindex = NULL_TREE;
5470 0 : else if (insert)
5471 : /* Otherwise, in order not to break the assumption
5472 : that CONSTRUCTOR either has all indexes or none,
5473 : we need to add indexes to all elements. */
5474 0 : for (unsigned int j = 0; j < len; ++j)
5475 0 : (*elts)[j].index = build_int_cst (TREE_TYPE (dindex), j);
5476 : }
5477 : }
5478 5894516 : else if (TREE_CODE (cindex) == INTEGER_CST
5479 5894516 : && compare_tree_int (cindex, end - 1) == 0)
5480 : {
5481 5769023 : tree value = (*elts)[end - 1].value;
5482 5769023 : if (i < end)
5483 : {
5484 2793680 : if (i == end - 1 && TREE_CODE (value) == RAW_DATA_CST)
5485 : begin = end - 1;
5486 : else
5487 2793590 : return i;
5488 : }
5489 2975343 : else if (TREE_CODE (value) == RAW_DATA_CST
5490 3007499 : && wi::to_offset (dindex) < (wi::to_offset (cindex)
5491 32156 : + RAW_DATA_LENGTH (value)))
5492 16078 : begin = end - 1;
5493 : else
5494 2959265 : begin = end;
5495 : }
5496 : }
5497 :
5498 : /* Otherwise, find a matching index by means of a binary search. */
5499 3664754 : while (begin != end)
5500 : {
5501 231923 : unsigned HOST_WIDE_INT middle = (begin + end) / 2;
5502 231923 : constructor_elt &elt = (*elts)[middle];
5503 231923 : tree idx = elt.index;
5504 :
5505 231923 : int cmp = array_index_cmp (dindex, idx);
5506 231923 : if (cmp > 0
5507 61023 : && TREE_CODE (elt.value) == RAW_DATA_CST
5508 315253 : && wi::to_offset (dindex) < (wi::to_offset (idx)
5509 83330 : + RAW_DATA_LENGTH (elt.value)))
5510 29178 : cmp = 0;
5511 231923 : if (cmp < 0)
5512 : end = middle;
5513 130625 : else if (cmp > 0)
5514 31845 : begin = middle + 1;
5515 : else
5516 : {
5517 98780 : if (insert && TREE_CODE (elt.value) == RAW_DATA_CST)
5518 : {
5519 : /* We need to split the RAW_DATA_CST elt. */
5520 122 : constructor_elt e;
5521 122 : gcc_checking_assert (TREE_CODE (idx) != RANGE_EXPR);
5522 244 : unsigned int off = (wi::to_offset (dindex)
5523 244 : - wi::to_offset (idx)).to_uhwi ();
5524 122 : tree value = elt.value;
5525 122 : unsigned int len = RAW_DATA_LENGTH (value);
5526 122 : if (off > 1 && len >= off + 3)
5527 22 : value = copy_node (elt.value);
5528 122 : if (off)
5529 : {
5530 33 : if (off > 1)
5531 33 : RAW_DATA_LENGTH (elt.value) = off;
5532 : else
5533 0 : elt.value = raw_data_cst_elt (elt.value, 0);
5534 33 : e.index = size_binop (PLUS_EXPR, elt.index,
5535 : build_int_cst (TREE_TYPE (elt.index),
5536 : off));
5537 33 : e.value = NULL_TREE;
5538 33 : ++middle;
5539 33 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5540 : }
5541 122 : (*elts)[middle].value = raw_data_cst_elt (value, off);
5542 122 : if (len >= off + 2)
5543 : {
5544 111 : e.index = (*elts)[middle].index;
5545 111 : e.index = size_binop (PLUS_EXPR, e.index,
5546 : build_one_cst (TREE_TYPE (e.index)));
5547 111 : if (len >= off + 3)
5548 : {
5549 111 : RAW_DATA_LENGTH (value) -= off + 1;
5550 111 : RAW_DATA_POINTER (value) += off + 1;
5551 111 : e.value = value;
5552 : }
5553 : else
5554 0 : e.value = raw_data_cst_elt (value, off + 1);
5555 111 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5556 : }
5557 122 : return middle;
5558 : }
5559 18467 : if (insert && TREE_CODE (idx) == RANGE_EXPR)
5560 : {
5561 : /* We need to split the range. */
5562 343 : constructor_elt e;
5563 343 : tree lo = TREE_OPERAND (idx, 0);
5564 343 : tree hi = TREE_OPERAND (idx, 1);
5565 343 : tree value = elt.value;
5566 343 : dindex = fold_convert (sizetype, dindex);
5567 343 : if (tree_int_cst_lt (lo, dindex))
5568 : {
5569 : /* There are still some lower elts; shorten the range. */
5570 170 : tree new_hi = int_const_binop (MINUS_EXPR, dindex,
5571 85 : size_one_node);
5572 85 : if (tree_int_cst_equal (lo, new_hi))
5573 : /* Only one element left, no longer a range. */
5574 33 : elt.index = lo;
5575 : else
5576 52 : TREE_OPERAND (idx, 1) = new_hi;
5577 : /* Append the element we want to insert. */
5578 85 : ++middle;
5579 85 : e.index = dindex;
5580 85 : e.value = unshare_constructor (value);
5581 85 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle, e);
5582 : }
5583 : else
5584 : /* No lower elts, the range elt is now ours. */
5585 258 : elt.index = dindex;
5586 :
5587 343 : if (tree_int_cst_lt (dindex, hi))
5588 : {
5589 : /* There are still some higher elts; append a range. */
5590 478 : tree new_lo = int_const_binop (PLUS_EXPR, dindex,
5591 239 : size_one_node);
5592 239 : if (tree_int_cst_equal (new_lo, hi))
5593 98 : e.index = hi;
5594 : else
5595 141 : e.index = build2 (RANGE_EXPR, sizetype, new_lo, hi);
5596 239 : e.value = unshare_constructor (value);
5597 239 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), middle + 1, e);
5598 : }
5599 : }
5600 98658 : return middle;
5601 : }
5602 : }
5603 :
5604 3432831 : if (insert)
5605 : {
5606 3356486 : constructor_elt e = { dindex, NULL_TREE };
5607 3356486 : vec_safe_insert (CONSTRUCTOR_ELTS (ary), end, e);
5608 3356486 : return end;
5609 : }
5610 :
5611 : return -1;
5612 : }
5613 :
5614 : /* Return a pointer to the constructor_elt of CTOR which matches INDEX. If no
5615 : matching constructor_elt exists, then add one to CTOR.
5616 :
5617 : As an optimization, if POS_HINT is non-negative then it is used as a guess
5618 : for the (integer) index of the matching constructor_elt within CTOR.
5619 :
5620 : If POS_HINT is -2, it means do not insert. */
5621 :
5622 : static constructor_elt *
5623 25270964 : get_or_insert_ctor_field (tree ctor, tree index, int pos_hint = -1)
5624 : {
5625 : /* Check the hint first. */
5626 2235458 : if (pos_hint >= 0 && (unsigned)pos_hint < CONSTRUCTOR_NELTS (ctor)
5627 27506422 : && CONSTRUCTOR_ELT (ctor, pos_hint)->index == index)
5628 : return CONSTRUCTOR_ELT (ctor, pos_hint);
5629 :
5630 23035555 : bool insertp = (pos_hint != -2);
5631 23035555 : tree type = TREE_TYPE (ctor);
5632 23035555 : if (TREE_CODE (type) == VECTOR_TYPE && index == NULL_TREE)
5633 : {
5634 11226 : gcc_assert (insertp);
5635 11226 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (ctor), index, NULL_TREE);
5636 11226 : return &CONSTRUCTOR_ELTS (ctor)->last();
5637 : }
5638 23024329 : else if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
5639 : {
5640 4412844 : if (TREE_CODE (index) == RANGE_EXPR)
5641 : {
5642 : /* Support for RANGE_EXPR index lookups is currently limited to
5643 : accessing an existing element via POS_HINT, or appending a new
5644 : element to the end of CTOR. ??? Support for other access
5645 : patterns may also be needed. */
5646 3 : vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (ctor);
5647 3 : if (vec_safe_length (elts))
5648 : {
5649 3 : tree lo = TREE_OPERAND (index, 0);
5650 3 : gcc_assert (array_index_cmp (elts->last().index, lo) < 0);
5651 : }
5652 3 : gcc_assert (insertp);
5653 3 : CONSTRUCTOR_APPEND_ELT (elts, index, NULL_TREE);
5654 3 : return &elts->last();
5655 : }
5656 :
5657 4412841 : HOST_WIDE_INT i = find_array_ctor_elt (ctor, index, insertp);
5658 4412841 : if (i < 0)
5659 : {
5660 918 : gcc_assert (!insertp);
5661 : return nullptr;
5662 : }
5663 4411923 : constructor_elt *cep = CONSTRUCTOR_ELT (ctor, i);
5664 4411923 : if (!insertp && cep->index && TREE_CODE (cep->index) == RANGE_EXPR)
5665 : {
5666 : /* Split a range even if we aren't inserting new entries. */
5667 0 : gcc_assert (!insertp);
5668 0 : i = find_array_ctor_elt (ctor, index, /*insert*/true);
5669 0 : gcc_assert (i >= 0);
5670 0 : cep = CONSTRUCTOR_ELT (ctor, i);
5671 : }
5672 4411923 : gcc_assert (cep->index == NULL_TREE
5673 : || TREE_CODE (cep->index) != RANGE_EXPR);
5674 : return cep;
5675 : }
5676 : else
5677 : {
5678 18611485 : gcc_assert (TREE_CODE (index) == FIELD_DECL
5679 : && (same_type_ignoring_top_level_qualifiers_p
5680 : (DECL_CONTEXT (index), TREE_TYPE (ctor))));
5681 :
5682 : /* We must keep the CONSTRUCTOR's ELTS in FIELD order.
5683 : Usually we meet initializers in that order, but it is
5684 : possible for base types to be placed not in program
5685 : order. */
5686 18611485 : tree fields = TYPE_FIELDS (DECL_CONTEXT (index));
5687 18611485 : unsigned HOST_WIDE_INT idx = 0;
5688 18611485 : constructor_elt *cep = NULL;
5689 :
5690 : /* Check if we're changing the active member of a union. */
5691 294906 : if (TREE_CODE (type) == UNION_TYPE && CONSTRUCTOR_NELTS (ctor)
5692 18759664 : && CONSTRUCTOR_ELT (ctor, 0)->index != index)
5693 4498 : vec_safe_truncate (CONSTRUCTOR_ELTS (ctor), 0);
5694 : /* If the bit offset of INDEX is larger than that of the last
5695 : constructor_elt, then we can just immediately append a new
5696 : constructor_elt to the end of CTOR. */
5697 18606987 : else if (CONSTRUCTOR_NELTS (ctor)
5698 24838381 : && tree_int_cst_compare (bit_position (index),
5699 24400952 : bit_position (CONSTRUCTOR_ELTS (ctor)
5700 12200476 : ->last().index)) > 0)
5701 : {
5702 3531033 : idx = CONSTRUCTOR_NELTS (ctor);
5703 3531033 : goto insert;
5704 : }
5705 :
5706 : /* Otherwise, we need to iterate over CTOR to find or insert INDEX
5707 : appropriately. */
5708 :
5709 16577715 : for (; vec_safe_iterate (CONSTRUCTOR_ELTS (ctor), idx, &cep);
5710 1497263 : idx++, fields = DECL_CHAIN (fields))
5711 : {
5712 10166706 : if (index == cep->index)
5713 8646179 : goto found;
5714 :
5715 : /* The field we're initializing must be on the field
5716 : list. Look to see if it is present before the
5717 : field the current ELT initializes. */
5718 16150619 : for (; fields != cep->index; fields = DECL_CHAIN (fields))
5719 14653356 : if (index == fields)
5720 23264 : goto insert;
5721 : }
5722 : /* We fell off the end of the CONSTRUCTOR, so insert a new
5723 : entry at the end. */
5724 :
5725 9965306 : insert:
5726 9965306 : if (!insertp)
5727 : return nullptr;
5728 :
5729 9965303 : {
5730 9965303 : constructor_elt ce = { index, NULL_TREE };
5731 :
5732 9965303 : vec_safe_insert (CONSTRUCTOR_ELTS (ctor), idx, ce);
5733 9965303 : cep = CONSTRUCTOR_ELT (ctor, idx);
5734 : }
5735 18611482 : found:;
5736 :
5737 18611482 : return cep;
5738 : }
5739 : }
5740 :
5741 : /* Under the control of CTX, issue a detailed diagnostic for
5742 : an out-of-bounds subscript INDEX into the expression ARRAY. */
5743 :
5744 : static void
5745 566 : diag_array_subscript (location_t loc, const constexpr_ctx *ctx, tree array, tree index)
5746 : {
5747 566 : if (!ctx->quiet)
5748 : {
5749 116 : tree arraytype = TREE_TYPE (array);
5750 :
5751 : /* Convert the unsigned array subscript to a signed integer to avoid
5752 : printing huge numbers for small negative values. */
5753 116 : tree sidx = fold_convert (ssizetype, index);
5754 116 : STRIP_ANY_LOCATION_WRAPPER (array);
5755 116 : if (DECL_P (array))
5756 : {
5757 75 : auto_diagnostic_group d;
5758 75 : if (TYPE_DOMAIN (arraytype))
5759 69 : error_at (loc, "array subscript value %qE is outside the bounds "
5760 : "of array %qD of type %qT", sidx, array, arraytype);
5761 : else
5762 6 : error_at (loc, "nonzero array subscript %qE is used with array %qD of "
5763 : "type %qT with unknown bounds", sidx, array, arraytype);
5764 75 : inform (DECL_SOURCE_LOCATION (array), "declared here");
5765 75 : }
5766 41 : else if (TYPE_DOMAIN (arraytype))
5767 38 : error_at (loc, "array subscript value %qE is outside the bounds "
5768 : "of array type %qT", sidx, arraytype);
5769 : else
5770 3 : error_at (loc, "nonzero array subscript %qE is used with array of type %qT "
5771 : "with unknown bounds", sidx, arraytype);
5772 : }
5773 566 : }
5774 :
5775 : /* Return the number of elements for TYPE (which is an ARRAY_TYPE or
5776 : a VECTOR_TYPE). */
5777 :
5778 : static tree
5779 12078553 : get_array_or_vector_nelts (const constexpr_ctx *ctx, tree type,
5780 : bool *non_constant_p, bool *overflow_p,
5781 : tree *jump_target)
5782 : {
5783 12078553 : tree nelts;
5784 12078553 : if (TREE_CODE (type) == ARRAY_TYPE)
5785 : {
5786 12078553 : if (TYPE_DOMAIN (type))
5787 12078305 : nelts = array_type_nelts_top (type);
5788 : else
5789 248 : nelts = size_zero_node;
5790 : }
5791 0 : else if (VECTOR_TYPE_P (type))
5792 0 : nelts = size_int (TYPE_VECTOR_SUBPARTS (type));
5793 : else
5794 0 : gcc_unreachable ();
5795 :
5796 : /* For VLAs, the number of elements won't be an integer constant. */
5797 12078553 : nelts = cxx_eval_constant_expression (ctx, nelts, vc_prvalue,
5798 : non_constant_p, overflow_p,
5799 : jump_target);
5800 12078553 : return nelts;
5801 : }
5802 :
5803 : /* Extract element INDEX consisting of CHARS_PER_ELT chars from
5804 : STRING_CST STRING. */
5805 :
5806 : static tree
5807 1587286 : extract_string_elt (tree string, unsigned chars_per_elt, unsigned index)
5808 : {
5809 1587286 : tree type = cv_unqualified (TREE_TYPE (TREE_TYPE (string)));
5810 1587286 : tree r;
5811 :
5812 1587286 : if (chars_per_elt == 1)
5813 1445397 : r = build_int_cst (type, TREE_STRING_POINTER (string)[index]);
5814 : else
5815 : {
5816 141889 : const unsigned char *ptr
5817 141889 : = ((const unsigned char *)TREE_STRING_POINTER (string)
5818 141889 : + index * chars_per_elt);
5819 141889 : r = native_interpret_expr (type, ptr, chars_per_elt);
5820 : }
5821 1587286 : return r;
5822 : }
5823 :
5824 : /* Subroutine of cxx_eval_array_reference. T is an ARRAY_REF; evaluate the
5825 : subscript, diagnose any problems with it, and return the result. */
5826 :
5827 : static tree
5828 12264206 : eval_and_check_array_index (const constexpr_ctx *ctx,
5829 : tree t, bool allow_one_past,
5830 : bool *non_constant_p, bool *overflow_p,
5831 : tree *jump_target)
5832 : {
5833 12264206 : location_t loc = cp_expr_loc_or_input_loc (t);
5834 12264206 : tree ary = TREE_OPERAND (t, 0);
5835 12264206 : t = TREE_OPERAND (t, 1);
5836 12264206 : tree index = cxx_eval_constant_expression (ctx, t, vc_prvalue,
5837 : non_constant_p, overflow_p,
5838 : jump_target);
5839 12264206 : if (*jump_target)
5840 : return NULL_TREE;
5841 12264206 : VERIFY_CONSTANT (index);
5842 :
5843 12078005 : if (!tree_fits_shwi_p (index)
5844 12078005 : || tree_int_cst_sgn (index) < 0)
5845 : {
5846 108 : diag_array_subscript (loc, ctx, ary, index);
5847 108 : *non_constant_p = true;
5848 108 : return t;
5849 : }
5850 :
5851 12077897 : tree nelts = get_array_or_vector_nelts (ctx, TREE_TYPE (ary), non_constant_p,
5852 : overflow_p, jump_target);
5853 12077897 : if (*jump_target)
5854 : return NULL_TREE;
5855 12077897 : VERIFY_CONSTANT (nelts);
5856 12077834 : if (allow_one_past
5857 12077834 : ? !tree_int_cst_le (index, nelts)
5858 7754851 : : !tree_int_cst_lt (index, nelts))
5859 : {
5860 458 : diag_array_subscript (loc, ctx, ary, index);
5861 458 : *non_constant_p = true;
5862 458 : return t;
5863 : }
5864 :
5865 : return index;
5866 : }
5867 :
5868 : /* Subroutine of cxx_eval_constant_expression.
5869 : Attempt to reduce a reference to an array slot. */
5870 :
5871 : static tree
5872 8263818 : cxx_eval_array_reference (const constexpr_ctx *ctx, tree t,
5873 : value_cat lval,
5874 : bool *non_constant_p, bool *overflow_p,
5875 : tree *jump_target)
5876 : {
5877 8263818 : tree oldary = TREE_OPERAND (t, 0);
5878 8263818 : tree ary = cxx_eval_constant_expression (ctx, oldary,
5879 : lval,
5880 : non_constant_p, overflow_p,
5881 : jump_target);
5882 8263818 : if (*non_constant_p)
5883 : return t;
5884 8066807 : if (*jump_target)
5885 : return NULL_TREE;
5886 8066807 : if (!lval
5887 3742581 : && TREE_CODE (ary) == VIEW_CONVERT_EXPR
5888 54095 : && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (ary, 0)))
5889 8120902 : && (TYPE_MAIN_VARIANT (TREE_TYPE (t))
5890 54095 : == TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_OPERAND (ary, 0))))))
5891 54095 : ary = TREE_OPERAND (ary, 0);
5892 :
5893 8066807 : tree oldidx = TREE_OPERAND (t, 1);
5894 8066807 : tree index = eval_and_check_array_index (ctx, t, lval,
5895 : non_constant_p, overflow_p,
5896 : jump_target);
5897 8066807 : if (*non_constant_p)
5898 : return t;
5899 7880043 : if (*jump_target)
5900 : return NULL_TREE;
5901 :
5902 7880043 : if (lval && ary == oldary && index == oldidx)
5903 : return t;
5904 4357452 : else if (lval == vc_discard)
5905 : return t;
5906 4357452 : else if (lval)
5907 800152 : return build4 (ARRAY_REF, TREE_TYPE (t), ary, index, NULL, NULL);
5908 :
5909 3557300 : unsigned len = 0, elem_nchars = 1;
5910 3557300 : tree elem_type = TREE_TYPE (TREE_TYPE (ary));
5911 3557300 : if (TREE_CODE (ary) == CONSTRUCTOR)
5912 1926168 : len = CONSTRUCTOR_NELTS (ary);
5913 1631132 : else if (TREE_CODE (ary) == STRING_CST)
5914 : {
5915 1577045 : elem_nchars = (TYPE_PRECISION (elem_type)
5916 1577045 : / TYPE_PRECISION (char_type_node));
5917 1577045 : len = (unsigned) TREE_STRING_LENGTH (ary) / elem_nchars;
5918 : }
5919 54087 : else if (TREE_CODE (ary) == VECTOR_CST)
5920 : /* We don't create variable-length VECTOR_CSTs. */
5921 54087 : len = VECTOR_CST_NELTS (ary).to_constant ();
5922 : else
5923 : {
5924 : /* We can't do anything with other tree codes, so use
5925 : VERIFY_CONSTANT to complain and fail. */
5926 0 : VERIFY_CONSTANT (ary);
5927 0 : gcc_unreachable ();
5928 : }
5929 :
5930 3557300 : bool found;
5931 3557300 : HOST_WIDE_INT i = 0;
5932 3557300 : if (TREE_CODE (ary) == CONSTRUCTOR)
5933 : {
5934 1926168 : HOST_WIDE_INT ix = find_array_ctor_elt (ary, index);
5935 1926168 : found = (ix >= 0);
5936 1926168 : if (found)
5937 1850829 : i = ix;
5938 : }
5939 : else
5940 : {
5941 1631132 : i = tree_to_shwi (index);
5942 1631132 : found = (i < len);
5943 : }
5944 :
5945 3557300 : if (found)
5946 : {
5947 3481510 : tree r;
5948 3481510 : if (TREE_CODE (ary) == CONSTRUCTOR)
5949 : {
5950 1850829 : r = (*CONSTRUCTOR_ELTS (ary))[i].value;
5951 1850829 : if (TREE_CODE (r) == RAW_DATA_CST)
5952 : {
5953 29194 : tree ridx = (*CONSTRUCTOR_ELTS (ary))[i].index;
5954 29194 : gcc_checking_assert (ridx);
5955 29194 : unsigned int off
5956 29194 : = (wi::to_offset (index) - wi::to_offset (ridx)).to_uhwi ();
5957 29194 : r = raw_data_cst_elt (r, off);
5958 : }
5959 : }
5960 1630681 : else if (TREE_CODE (ary) == VECTOR_CST)
5961 54087 : r = VECTOR_CST_ELT (ary, i);
5962 : else
5963 1576594 : r = extract_string_elt (ary, elem_nchars, i);
5964 :
5965 1659875 : if (r)
5966 : /* Don't VERIFY_CONSTANT here. */
5967 3481510 : return r;
5968 :
5969 : /* Otherwise the element doesn't have a value yet. */
5970 : }
5971 :
5972 : /* Not found. */
5973 :
5974 75790 : if (is_really_empty_class (elem_type, /*ignore_vptr*/false))
5975 63 : return build_constructor (elem_type, NULL);
5976 :
5977 75727 : if (TREE_CODE (ary) == CONSTRUCTOR
5978 75727 : && CONSTRUCTOR_NO_CLEARING (ary))
5979 : {
5980 : /* 'ary' is part of the aggregate initializer we're currently
5981 : building; if there's no initializer for this element yet,
5982 : that's an error. */
5983 51 : if (!ctx->quiet)
5984 16 : error ("accessing uninitialized array element");
5985 51 : *non_constant_p = true;
5986 51 : return t;
5987 : }
5988 :
5989 : /* If it's within the array bounds but doesn't have an explicit
5990 : initializer, it's initialized from {}. But use build_value_init
5991 : directly for non-aggregates to avoid creating a garbage CONSTRUCTOR. */
5992 75676 : tree val;
5993 75676 : constexpr_ctx new_ctx;
5994 75676 : if (CP_AGGREGATE_TYPE_P (elem_type))
5995 : {
5996 494 : tree empty_ctor = build_constructor (init_list_type_node, NULL);
5997 494 : val = digest_init (elem_type, empty_ctor, tf_warning_or_error);
5998 : }
5999 : else
6000 75182 : val = build_value_init (elem_type, tf_warning_or_error);
6001 :
6002 : /* Create a new constructor only if we don't already have a suitable one. */
6003 75676 : const bool new_ctor = (!SCALAR_TYPE_P (elem_type)
6004 76202 : && (!ctx->ctor
6005 45 : || !same_type_ignoring_top_level_qualifiers_p
6006 45 : (elem_type, TREE_TYPE (ctx->ctor))));
6007 517 : if (new_ctor)
6008 : {
6009 517 : new_ctx = *ctx;
6010 : /* We clear the object here. We used to replace it with T, but that
6011 : caused problems (101371, 108158); and anyway, T is the initializer,
6012 : not the target object. */
6013 517 : new_ctx.object = NULL_TREE;
6014 517 : new_ctx.ctor = build_constructor (elem_type, NULL);
6015 517 : ctx = &new_ctx;
6016 : }
6017 75676 : t = cxx_eval_constant_expression (ctx, val, lval, non_constant_p,
6018 : overflow_p, jump_target);
6019 75676 : if (new_ctor && t != ctx->ctor)
6020 496 : free_constructor (ctx->ctor);
6021 : return t;
6022 : }
6023 :
6024 : /* Subroutine of cxx_eval_constant_expression.
6025 : Attempt to reduce a field access of a value of class type. */
6026 :
6027 : static tree
6028 65336754 : cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
6029 : value_cat lval,
6030 : bool *non_constant_p, bool *overflow_p,
6031 : tree *jump_target)
6032 : {
6033 65336754 : unsigned HOST_WIDE_INT i;
6034 65336754 : tree field;
6035 65336754 : tree value;
6036 65336754 : tree part = TREE_OPERAND (t, 1);
6037 65336754 : tree orig_whole = TREE_OPERAND (t, 0);
6038 65336754 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6039 : lval,
6040 : non_constant_p, overflow_p,
6041 : jump_target);
6042 65336754 : if (*non_constant_p)
6043 : return t;
6044 45441862 : if (*jump_target)
6045 : return NULL_TREE;
6046 45441859 : if (INDIRECT_REF_P (whole)
6047 45441859 : && integer_zerop (TREE_OPERAND (whole, 0)))
6048 : {
6049 177 : if (!ctx->quiet)
6050 39 : error ("dereferencing a null pointer in %qE", orig_whole);
6051 177 : *non_constant_p = true;
6052 177 : return t;
6053 : }
6054 :
6055 45441682 : if (TREE_CODE (whole) == PTRMEM_CST)
6056 1486 : whole = cplus_expand_constant (whole);
6057 45441682 : if (whole == orig_whole)
6058 : return t;
6059 28425027 : if (lval == vc_discard)
6060 : return t;
6061 28425003 : if (lval)
6062 10253328 : return fold_build3 (COMPONENT_REF, TREE_TYPE (t),
6063 : whole, part, NULL_TREE);
6064 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6065 : CONSTRUCTOR. */
6066 18171675 : if (TREE_CODE (whole) != CONSTRUCTOR)
6067 : {
6068 0 : if (!ctx->quiet)
6069 0 : error ("%qE is not a constant expression", orig_whole);
6070 0 : *non_constant_p = true;
6071 0 : return t;
6072 : }
6073 18170183 : if ((cxx_dialect < cxx14 || CONSTRUCTOR_MUTABLE_POISON (whole))
6074 18173349 : && DECL_MUTABLE_P (part))
6075 : {
6076 144 : if (!ctx->quiet)
6077 16 : error ("mutable %qD is not usable in a constant expression", part);
6078 144 : *non_constant_p = true;
6079 144 : return t;
6080 : }
6081 :
6082 18171531 : bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
6083 26150020 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6084 : {
6085 : /* Use name match for PMF fields, as a variant will have a
6086 : different FIELD_DECL with a different type. */
6087 26051477 : if (pmf ? DECL_NAME (field) == DECL_NAME (part)
6088 : : field == part)
6089 : {
6090 18072988 : if (value == void_node)
6091 3 : goto uninit;
6092 18072985 : if (value)
6093 : {
6094 18072967 : STRIP_ANY_LOCATION_WRAPPER (value);
6095 18072967 : return value;
6096 : }
6097 : else
6098 : /* We're in the middle of initializing it. */
6099 : break;
6100 : }
6101 : }
6102 98561 : if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
6103 : {
6104 106 : if (CONSTRUCTOR_NELTS (whole) > 0)
6105 : {
6106 : /* DR 1188 says we don't have to deal with this. */
6107 91 : if (!ctx->quiet)
6108 : {
6109 19 : constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
6110 19 : if (cep->value == NULL_TREE)
6111 9 : error ("accessing uninitialized member %qD", part);
6112 : else
6113 10 : error ("accessing %qD member instead of active %qD member "
6114 : "in constant expression", part, cep->index);
6115 : }
6116 91 : *non_constant_p = true;
6117 91 : return t;
6118 : }
6119 15 : else if (!CONSTRUCTOR_NO_CLEARING (whole))
6120 : {
6121 : /* Value-initialized union, check if looking at the first member. */
6122 12 : tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
6123 12 : if (first != part)
6124 : {
6125 9 : if (!ctx->quiet)
6126 3 : error ("accessing %qD member instead of initialized %qD "
6127 : "member in constant expression", part, first);
6128 9 : *non_constant_p = true;
6129 9 : return t;
6130 : }
6131 : }
6132 : }
6133 :
6134 : /* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
6135 : classes never get represented; throw together a value now. */
6136 98461 : if (is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
6137 89864 : return build_constructor (TREE_TYPE (t), NULL);
6138 :
6139 8597 : gcc_assert (DECL_CONTEXT (part) == TYPE_MAIN_VARIANT (TREE_TYPE (whole)));
6140 :
6141 8597 : if (CONSTRUCTOR_NO_CLEARING (whole))
6142 : {
6143 101 : uninit:
6144 : /* 'whole' is part of the aggregate initializer we're currently
6145 : building; if there's no initializer for this member yet, that's an
6146 : error. */
6147 104 : if (!ctx->quiet)
6148 22 : error ("accessing uninitialized member %qD", part);
6149 104 : *non_constant_p = true;
6150 104 : return t;
6151 : }
6152 :
6153 : /* If there's no explicit init for this field, it's value-initialized. */
6154 :
6155 8496 : if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
6156 : {
6157 : /* As in cxx_eval_store_expression, insert an empty CONSTRUCTOR
6158 : and copy the flags. */
6159 7988 : constructor_elt *e = get_or_insert_ctor_field (whole, part);
6160 7988 : e->value = value = build_constructor (TREE_TYPE (part), NULL);
6161 7988 : CONSTRUCTOR_ZERO_PADDING_BITS (value)
6162 7988 : = CONSTRUCTOR_ZERO_PADDING_BITS (whole);
6163 7988 : return value;
6164 : }
6165 :
6166 508 : value = build_value_init (TREE_TYPE (t), tf_warning_or_error);
6167 508 : return cxx_eval_constant_expression (ctx, value,
6168 : lval,
6169 : non_constant_p, overflow_p,
6170 508 : jump_target);
6171 : }
6172 :
6173 : /* Subroutine of cxx_eval_constant_expression.
6174 : Attempt to reduce a field access of a value of class type that is
6175 : expressed as a BIT_FIELD_REF. */
6176 :
6177 : static tree
6178 15766 : cxx_eval_bit_field_ref (const constexpr_ctx *ctx, tree t,
6179 : value_cat lval,
6180 : bool *non_constant_p, bool *overflow_p,
6181 : tree *jump_target)
6182 : {
6183 15766 : tree orig_whole = TREE_OPERAND (t, 0);
6184 15766 : tree retval, fldval, utype, mask;
6185 15766 : bool fld_seen = false;
6186 15766 : HOST_WIDE_INT istart, isize;
6187 15766 : tree whole = cxx_eval_constant_expression (ctx, orig_whole,
6188 : lval,
6189 : non_constant_p, overflow_p,
6190 : jump_target);
6191 15766 : tree start, field, value;
6192 15766 : unsigned HOST_WIDE_INT i;
6193 :
6194 15766 : if (*jump_target)
6195 : return NULL_TREE;
6196 15766 : if (whole == orig_whole)
6197 : return t;
6198 : /* Don't VERIFY_CONSTANT here; we only want to check that we got a
6199 : CONSTRUCTOR. */
6200 15574 : if (!*non_constant_p
6201 15574 : && TREE_CODE (whole) != VECTOR_CST
6202 1495 : && TREE_CODE (whole) != CONSTRUCTOR)
6203 : {
6204 0 : if (!ctx->quiet)
6205 0 : error ("%qE is not a constant expression", orig_whole);
6206 0 : *non_constant_p = true;
6207 : }
6208 15574 : if (*non_constant_p)
6209 : return t;
6210 :
6211 15574 : if (TREE_CODE (whole) == VECTOR_CST || !INTEGRAL_TYPE_P (TREE_TYPE (t)))
6212 : {
6213 14079 : if (tree r = fold_ternary (BIT_FIELD_REF, TREE_TYPE (t), whole,
6214 : TREE_OPERAND (t, 1), TREE_OPERAND (t, 2)))
6215 : return r;
6216 0 : if (!ctx->quiet)
6217 0 : error ("%qE is not a constant expression", orig_whole);
6218 0 : *non_constant_p = true;
6219 0 : return t;
6220 : }
6221 :
6222 1495 : start = TREE_OPERAND (t, 2);
6223 1495 : istart = tree_to_shwi (start);
6224 1495 : isize = tree_to_shwi (TREE_OPERAND (t, 1));
6225 1495 : utype = TREE_TYPE (t);
6226 1495 : if (!TYPE_UNSIGNED (utype))
6227 0 : utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1);
6228 1495 : retval = build_int_cst (utype, 0);
6229 20930 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
6230 : {
6231 19435 : tree bitpos = bit_position (field);
6232 19435 : STRIP_ANY_LOCATION_WRAPPER (value);
6233 19435 : if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1))
6234 : return value;
6235 19435 : if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE
6236 19435 : && TREE_CODE (value) == INTEGER_CST
6237 19435 : && tree_fits_shwi_p (bitpos)
6238 38870 : && tree_fits_shwi_p (DECL_SIZE (field)))
6239 : {
6240 19435 : HOST_WIDE_INT bit = tree_to_shwi (bitpos);
6241 19435 : HOST_WIDE_INT sz = tree_to_shwi (DECL_SIZE (field));
6242 19435 : HOST_WIDE_INT shift;
6243 19435 : if (bit >= istart && bit + sz <= istart + isize)
6244 : {
6245 4485 : fldval = fold_convert (utype, value);
6246 4485 : mask = build_int_cst_type (utype, -1);
6247 4485 : mask = fold_build2 (LSHIFT_EXPR, utype, mask,
6248 : size_int (TYPE_PRECISION (utype) - sz));
6249 4485 : mask = fold_build2 (RSHIFT_EXPR, utype, mask,
6250 : size_int (TYPE_PRECISION (utype) - sz));
6251 4485 : fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask);
6252 4485 : shift = bit - istart;
6253 4485 : if (BYTES_BIG_ENDIAN)
6254 : shift = TYPE_PRECISION (utype) - shift - sz;
6255 4485 : fldval = fold_build2 (LSHIFT_EXPR, utype, fldval,
6256 : size_int (shift));
6257 4485 : retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval);
6258 4485 : fld_seen = true;
6259 : }
6260 : }
6261 : }
6262 1495 : if (fld_seen)
6263 1495 : return fold_convert (TREE_TYPE (t), retval);
6264 0 : gcc_unreachable ();
6265 : return error_mark_node;
6266 : }
6267 :
6268 : /* Helper for cxx_eval_bit_cast.
6269 : Check [bit.cast]/3 rules, bit_cast is constexpr only if the To and From
6270 : types and types of all subobjects have is_union_v<T>, is_pointer_v<T>,
6271 : is_member_pointer_v<T>, is_volatile_v<T> false and has no non-static
6272 : data members of reference type. */
6273 :
6274 : static bool
6275 5538 : check_bit_cast_type (const constexpr_ctx *ctx, location_t loc, tree type,
6276 : tree orig_type)
6277 : {
6278 6061 : if (TREE_CODE (type) == UNION_TYPE)
6279 : {
6280 63 : if (!ctx->quiet)
6281 : {
6282 21 : if (type == orig_type)
6283 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6284 : "a union type", "__builtin_bit_cast", type);
6285 : else
6286 15 : error_at (loc, "%qs is not a constant expression because %qT "
6287 : "contains a union type", "__builtin_bit_cast",
6288 : orig_type);
6289 : }
6290 63 : return true;
6291 : }
6292 : if (TREE_CODE (type) == POINTER_TYPE)
6293 : {
6294 57 : if (!ctx->quiet)
6295 : {
6296 18 : if (type == orig_type)
6297 6 : error_at (loc, "%qs is not a constant expression because %qT is "
6298 : "a pointer type", "__builtin_bit_cast", type);
6299 : else
6300 12 : error_at (loc, "%qs is not a constant expression because %qT "
6301 : "contains a pointer type", "__builtin_bit_cast",
6302 : orig_type);
6303 : }
6304 57 : return true;
6305 : }
6306 : if (TREE_CODE (type) == REFERENCE_TYPE)
6307 : {
6308 0 : if (!ctx->quiet)
6309 : {
6310 0 : if (type == orig_type)
6311 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6312 : "a reference type", "__builtin_bit_cast", type);
6313 : else
6314 0 : error_at (loc, "%qs is not a constant expression because %qT "
6315 : "contains a reference type", "__builtin_bit_cast",
6316 : orig_type);
6317 : }
6318 0 : return true;
6319 : }
6320 1887 : if (TYPE_PTRMEM_P (type))
6321 : {
6322 36 : if (!ctx->quiet)
6323 : {
6324 12 : if (type == orig_type)
6325 12 : error_at (loc, "%qs is not a constant expression because %qT is "
6326 : "a pointer to member type", "__builtin_bit_cast",
6327 : type);
6328 : else
6329 0 : error_at (loc, "%qs is not a constant expression because %qT "
6330 : "contains a pointer to member type",
6331 : "__builtin_bit_cast", orig_type);
6332 : }
6333 36 : return true;
6334 : }
6335 5905 : if (TYPE_VOLATILE (type))
6336 : {
6337 0 : if (!ctx->quiet)
6338 : {
6339 0 : if (type == orig_type)
6340 0 : error_at (loc, "%qs is not a constant expression because %qT is "
6341 : "volatile", "__builtin_bit_cast", type);
6342 : else
6343 0 : error_at (loc, "%qs is not a constant expression because %qT "
6344 : "contains a volatile subobject",
6345 : "__builtin_bit_cast", orig_type);
6346 : }
6347 0 : return true;
6348 : }
6349 5905 : if (TREE_CODE (type) == RECORD_TYPE)
6350 58406 : for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6351 56627 : if (TREE_CODE (field) == FIELD_DECL
6352 56627 : && check_bit_cast_type (ctx, loc, TREE_TYPE (field), orig_type))
6353 : return true;
6354 5815 : if (TREE_CODE (type) == ARRAY_TYPE)
6355 523 : return check_bit_cast_type (ctx, loc, TREE_TYPE (type), orig_type);
6356 : return false;
6357 : }
6358 :
6359 : /* Helper function for cxx_eval_bit_cast. For unsigned char or
6360 : std::byte members of CONSTRUCTOR (recursively) if they contain
6361 : some indeterminate bits (as set in MASK), remove the ctor elts,
6362 : mark the CONSTRUCTOR as CONSTRUCTOR_NO_CLEARING and clear the
6363 : bits in MASK. */
6364 :
6365 : static void
6366 707 : clear_uchar_or_std_byte_in_mask (location_t loc, tree t, unsigned char *mask)
6367 : {
6368 707 : if (TREE_CODE (t) != CONSTRUCTOR)
6369 : return;
6370 :
6371 : unsigned i, j = 0;
6372 : tree index, value;
6373 2284 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, index, value)
6374 : {
6375 1577 : tree type = TREE_TYPE (value);
6376 1577 : if (TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE
6377 2715 : && DECL_BIT_FIELD_TYPE (index) != NULL_TREE)
6378 : {
6379 270 : if (is_byte_access_type_not_plain_char (DECL_BIT_FIELD_TYPE (index)))
6380 : {
6381 135 : HOST_WIDE_INT fldsz = TYPE_PRECISION (TREE_TYPE (index));
6382 135 : gcc_assert (fldsz != 0);
6383 135 : HOST_WIDE_INT pos = int_byte_position (index);
6384 135 : HOST_WIDE_INT bpos
6385 135 : = tree_to_uhwi (DECL_FIELD_BIT_OFFSET (index));
6386 135 : bpos %= BITS_PER_UNIT;
6387 135 : HOST_WIDE_INT end
6388 135 : = ROUND_UP (bpos + fldsz, BITS_PER_UNIT) / BITS_PER_UNIT;
6389 135 : gcc_assert (end == 1 || end == 2);
6390 135 : unsigned char *p = mask + pos;
6391 135 : unsigned char mask_save[2];
6392 135 : mask_save[0] = mask[pos];
6393 135 : mask_save[1] = end == 2 ? mask[pos + 1] : 0;
6394 135 : if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
6395 : sorry_at (loc, "PDP11 bit-field handling unsupported"
6396 : " in %qs", "__builtin_bit_cast");
6397 135 : else if (BYTES_BIG_ENDIAN)
6398 : {
6399 : /* Big endian. */
6400 : if (bpos + fldsz <= BITS_PER_UNIT)
6401 : *p &= ~(((1 << fldsz) - 1)
6402 : << (BITS_PER_UNIT - bpos - fldsz));
6403 : else
6404 : {
6405 : gcc_assert (bpos);
6406 : *p &= ~(((1U << BITS_PER_UNIT) - 1) >> bpos);
6407 : p++;
6408 : fldsz -= BITS_PER_UNIT - bpos;
6409 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6410 : *p &= ((1U << BITS_PER_UNIT) - 1) >> fldsz;
6411 : }
6412 : }
6413 : else
6414 : {
6415 : /* Little endian. */
6416 135 : if (bpos + fldsz <= BITS_PER_UNIT)
6417 135 : *p &= ~(((1 << fldsz) - 1) << bpos);
6418 : else
6419 : {
6420 0 : gcc_assert (bpos);
6421 0 : *p &= ~(((1 << BITS_PER_UNIT) - 1) << bpos);
6422 0 : p++;
6423 0 : fldsz -= BITS_PER_UNIT - bpos;
6424 0 : gcc_assert (fldsz && fldsz < BITS_PER_UNIT);
6425 0 : *p &= ~((1 << fldsz) - 1);
6426 : }
6427 : }
6428 135 : if (mask_save[0] != mask[pos]
6429 99 : || (end == 2 && mask_save[1] != mask[pos + 1]))
6430 : {
6431 36 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6432 36 : continue;
6433 : }
6434 : }
6435 : }
6436 1307 : else if (is_byte_access_type_not_plain_char (type))
6437 : {
6438 228 : HOST_WIDE_INT pos;
6439 228 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6440 132 : pos = tree_to_shwi (index);
6441 : else
6442 96 : pos = int_byte_position (index);
6443 228 : if (mask[pos])
6444 : {
6445 48 : CONSTRUCTOR_NO_CLEARING (t) = 1;
6446 48 : mask[pos] = 0;
6447 48 : continue;
6448 : }
6449 : }
6450 1493 : if (TREE_CODE (value) == CONSTRUCTOR)
6451 : {
6452 368 : HOST_WIDE_INT pos;
6453 368 : if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
6454 144 : pos = tree_to_shwi (index)
6455 72 : * tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))));
6456 : else
6457 296 : pos = int_byte_position (index);
6458 368 : clear_uchar_or_std_byte_in_mask (loc, value, mask + pos);
6459 : }
6460 1493 : if (i != j)
6461 : {
6462 180 : CONSTRUCTOR_ELT (t, j)->index = index;
6463 180 : CONSTRUCTOR_ELT (t, j)->value = value;
6464 : }
6465 1493 : ++j;
6466 : }
6467 1414 : if (CONSTRUCTOR_NELTS (t) != j)
6468 84 : vec_safe_truncate (CONSTRUCTOR_ELTS (t), j);
6469 : }
6470 :
6471 : /* Subroutine of cxx_eval_constant_expression.
6472 : Attempt to evaluate a BIT_CAST_EXPR. */
6473 :
6474 : static tree
6475 1125 : cxx_eval_bit_cast (const constexpr_ctx *ctx, tree t, bool *non_constant_p,
6476 : bool *overflow_p, tree *jump_target)
6477 : {
6478 1125 : if (check_bit_cast_type (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
6479 1125 : TREE_TYPE (t))
6480 3652 : || check_bit_cast_type (ctx, cp_expr_loc_or_loc (TREE_OPERAND (t, 0),
6481 1044 : EXPR_LOCATION (t)),
6482 1044 : TREE_TYPE (TREE_OPERAND (t, 0)),
6483 1044 : TREE_TYPE (TREE_OPERAND (t, 0))))
6484 : {
6485 156 : *non_constant_p = true;
6486 156 : return t;
6487 : }
6488 :
6489 969 : tree op = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_prvalue,
6490 : non_constant_p, overflow_p,
6491 : jump_target);
6492 969 : if (*non_constant_p)
6493 : return t;
6494 708 : if (*jump_target)
6495 : return NULL_TREE;
6496 :
6497 708 : location_t loc = EXPR_LOCATION (t);
6498 708 : if (BITS_PER_UNIT != 8 || CHAR_BIT != 8)
6499 : {
6500 : if (!ctx->quiet)
6501 : sorry_at (loc, "%qs cannot be constant evaluated on the target",
6502 : "__builtin_bit_cast");
6503 : *non_constant_p = true;
6504 : return t;
6505 : }
6506 :
6507 708 : if (!tree_fits_shwi_p (TYPE_SIZE_UNIT (TREE_TYPE (t))))
6508 : {
6509 0 : if (!ctx->quiet)
6510 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6511 : "type is too large", "__builtin_bit_cast");
6512 0 : *non_constant_p = true;
6513 0 : return t;
6514 : }
6515 :
6516 708 : HOST_WIDE_INT len = tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (t)));
6517 708 : if (len < 0 || (int) len != len)
6518 : {
6519 0 : if (!ctx->quiet)
6520 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6521 : "type is too large", "__builtin_bit_cast");
6522 0 : *non_constant_p = true;
6523 0 : return t;
6524 : }
6525 :
6526 708 : unsigned char buf[64];
6527 708 : unsigned char *ptr, *mask;
6528 708 : size_t alen = (size_t) len * 2;
6529 708 : if (alen <= sizeof (buf))
6530 : ptr = buf;
6531 : else
6532 3 : ptr = XNEWVEC (unsigned char, alen);
6533 708 : mask = ptr + (size_t) len;
6534 : /* At the beginning consider everything indeterminate. */
6535 708 : memset (mask, ~0, (size_t) len);
6536 :
6537 708 : if (native_encode_initializer (op, ptr, len, 0, mask) != len)
6538 : {
6539 0 : if (!ctx->quiet)
6540 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6541 : "argument cannot be encoded", "__builtin_bit_cast");
6542 0 : *non_constant_p = true;
6543 0 : if (ptr != buf)
6544 0 : XDELETE (ptr);
6545 0 : return t;
6546 : }
6547 :
6548 708 : tree r = NULL_TREE;
6549 708 : if (can_native_interpret_type_p (TREE_TYPE (t)))
6550 : {
6551 369 : r = native_interpret_expr (TREE_TYPE (t), ptr, len);
6552 369 : if (is_byte_access_type_not_plain_char (TREE_TYPE (t)))
6553 : {
6554 46 : gcc_assert (len == 1);
6555 46 : if (mask[0])
6556 : {
6557 24 : memset (mask, 0, len);
6558 24 : r = build_constructor (TREE_TYPE (r), NULL);
6559 24 : CONSTRUCTOR_NO_CLEARING (r) = 1;
6560 : }
6561 : }
6562 : }
6563 339 : else if (TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE)
6564 : {
6565 339 : r = native_interpret_aggregate (TREE_TYPE (t), ptr, 0, len);
6566 339 : if (r != NULL_TREE)
6567 : {
6568 339 : clear_type_padding_in_mask (TREE_TYPE (t), mask);
6569 339 : clear_uchar_or_std_byte_in_mask (loc, r, mask);
6570 339 : if (CHECKING_P)
6571 : {
6572 339 : tree e = cxx_eval_bare_aggregate (ctx, r, vc_prvalue,
6573 : non_constant_p, overflow_p,
6574 : jump_target);
6575 339 : gcc_checking_assert (e == r && !*jump_target);
6576 : r = e;
6577 : }
6578 : }
6579 : }
6580 :
6581 708 : if (r != NULL_TREE)
6582 : {
6583 5935 : for (int i = 0; i < len; i++)
6584 5317 : if (mask[i])
6585 : {
6586 90 : if (!ctx->quiet)
6587 30 : error_at (loc, "%qs accessing uninitialized byte at offset %d",
6588 : "__builtin_bit_cast", i);
6589 90 : *non_constant_p = true;
6590 90 : r = t;
6591 90 : break;
6592 : }
6593 708 : if (ptr != buf)
6594 3 : XDELETE (ptr);
6595 708 : return r;
6596 : }
6597 :
6598 0 : if (!ctx->quiet)
6599 0 : sorry_at (loc, "%qs cannot be constant evaluated because the "
6600 : "argument cannot be interpreted", "__builtin_bit_cast");
6601 0 : *non_constant_p = true;
6602 0 : if (ptr != buf)
6603 0 : XDELETE (ptr);
6604 : return t;
6605 : }
6606 :
6607 : /* Subroutine of cxx_eval_constant_expression.
6608 : Evaluate a short-circuited logical expression T in the context
6609 : of a given constexpr CALL. BAILOUT_VALUE is the value for
6610 : early return. CONTINUE_VALUE is used here purely for
6611 : sanity check purposes. */
6612 :
6613 : static tree
6614 10693159 : cxx_eval_logical_expression (const constexpr_ctx *ctx, tree t,
6615 : tree bailout_value, tree continue_value,
6616 : bool *non_constant_p, bool *overflow_p,
6617 : tree *jump_target)
6618 : {
6619 10693159 : tree r;
6620 10693159 : tree lhs = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
6621 : vc_prvalue, non_constant_p,
6622 : overflow_p, jump_target);
6623 10693158 : if (*jump_target)
6624 : return NULL_TREE;
6625 10693149 : VERIFY_CONSTANT (lhs);
6626 6700129 : if (tree_int_cst_equal (lhs, bailout_value))
6627 : return lhs;
6628 5457908 : gcc_assert (tree_int_cst_equal (lhs, continue_value));
6629 5457908 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
6630 : vc_prvalue, non_constant_p,
6631 : overflow_p, jump_target);
6632 5457908 : if (*jump_target)
6633 : return NULL_TREE;
6634 5457908 : VERIFY_CONSTANT (r);
6635 : return r;
6636 : }
6637 :
6638 : /* REF is a COMPONENT_REF designating a particular field. V is a vector of
6639 : CONSTRUCTOR elements to initialize (part of) an object containing that
6640 : field. Return a pointer to the constructor_elt corresponding to the
6641 : initialization of the field. */
6642 :
6643 : static constructor_elt *
6644 0 : base_field_constructor_elt (vec<constructor_elt, va_gc> *v, tree ref)
6645 : {
6646 0 : tree aggr = TREE_OPERAND (ref, 0);
6647 0 : tree field = TREE_OPERAND (ref, 1);
6648 0 : HOST_WIDE_INT i;
6649 0 : constructor_elt *ce;
6650 :
6651 0 : gcc_assert (TREE_CODE (ref) == COMPONENT_REF);
6652 :
6653 0 : if (TREE_CODE (aggr) == COMPONENT_REF)
6654 : {
6655 0 : constructor_elt *base_ce
6656 0 : = base_field_constructor_elt (v, aggr);
6657 0 : v = CONSTRUCTOR_ELTS (base_ce->value);
6658 : }
6659 :
6660 0 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
6661 0 : if (ce->index == field)
6662 0 : return ce;
6663 :
6664 0 : gcc_unreachable ();
6665 : return NULL;
6666 : }
6667 :
6668 : /* Some of the expressions fed to the constexpr mechanism are calls to
6669 : constructors, which have type void. In that case, return the type being
6670 : initialized by the constructor. */
6671 :
6672 : static tree
6673 521030011 : initialized_type (tree t)
6674 : {
6675 522180195 : if (TYPE_P (t))
6676 : return t;
6677 522179756 : tree type = TREE_TYPE (t);
6678 522179756 : if (TREE_CODE (t) == CALL_EXPR)
6679 : {
6680 : /* A constructor call has void type, so we need to look deeper. */
6681 67808421 : tree fn = get_function_named_in_call (t);
6682 67806427 : if (fn && TREE_CODE (fn) == FUNCTION_DECL
6683 135496443 : && DECL_CXX_CONSTRUCTOR_P (fn))
6684 3759343 : type = DECL_CONTEXT (fn);
6685 : }
6686 454371335 : else if (TREE_CODE (t) == COMPOUND_EXPR)
6687 1150184 : return initialized_type (TREE_OPERAND (t, 1));
6688 453221151 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
6689 775143 : type = TREE_TYPE (AGGR_INIT_EXPR_SLOT (t));
6690 521029572 : return cv_unqualified (type);
6691 : }
6692 :
6693 : /* We're about to initialize element INDEX of an array or class from VALUE.
6694 : Set up NEW_CTX appropriately by adjusting .object to refer to the
6695 : subobject and creating a new CONSTRUCTOR if the element is itself
6696 : a class or array. */
6697 :
6698 : static void
6699 1893654 : init_subob_ctx (const constexpr_ctx *ctx, constexpr_ctx &new_ctx,
6700 : tree index, tree &value)
6701 : {
6702 1893654 : new_ctx = *ctx;
6703 :
6704 1893654 : if (index && TREE_CODE (index) != INTEGER_CST
6705 1634106 : && TREE_CODE (index) != FIELD_DECL
6706 3 : && TREE_CODE (index) != RANGE_EXPR)
6707 : /* This won't have an element in the new CONSTRUCTOR. */
6708 : return;
6709 :
6710 1893654 : tree type = initialized_type (value);
6711 1893654 : if (!AGGREGATE_TYPE_P (type) && !VECTOR_TYPE_P (type))
6712 : /* A non-aggregate member doesn't get its own CONSTRUCTOR. */
6713 : return;
6714 :
6715 740002 : tree ctxtype = NULL_TREE;
6716 740002 : if (ctx->ctor)
6717 739994 : ctxtype = TREE_TYPE (ctx->ctor);
6718 8 : else if (ctx->object)
6719 8 : ctxtype = TREE_TYPE (ctx->object);
6720 : else
6721 0 : gcc_unreachable ();
6722 :
6723 740002 : if (VECTOR_TYPE_P (type)
6724 2950 : && VECTOR_TYPE_P (ctxtype)
6725 922 : && index == NULL_TREE)
6726 : /* A vector inside of a vector CONSTRUCTOR, e.g. when a larger
6727 : vector is constructed from smaller vectors, doesn't get its own
6728 : CONSTRUCTOR either. */
6729 : return;
6730 :
6731 : /* The sub-aggregate initializer might contain a placeholder;
6732 : update object to refer to the subobject and ctor to refer to
6733 : the (newly created) sub-initializer. */
6734 739080 : if (ctx->object)
6735 : {
6736 583275 : if (index == NULL_TREE || TREE_CODE (index) == RANGE_EXPR)
6737 : /* There's no well-defined subobject for this index. */
6738 3 : new_ctx.object = NULL_TREE;
6739 : else
6740 583272 : new_ctx.object = build_ctor_subob_ref (index, type, ctx->object);
6741 : }
6742 :
6743 739080 : if (is_empty_class (type)
6744 739080 : && TREE_CODE (ctxtype) != UNION_TYPE)
6745 : /* Leave ctor null for an empty subobject of a non-union class, they aren't
6746 : represented in the result of evaluation. */
6747 663064 : new_ctx.ctor = NULL_TREE;
6748 : else
6749 : {
6750 76016 : tree elt = build_constructor (type, NULL);
6751 76016 : CONSTRUCTOR_NO_CLEARING (elt) = true;
6752 76016 : new_ctx.ctor = elt;
6753 : }
6754 :
6755 739080 : if (TREE_CODE (value) == TARGET_EXPR)
6756 : /* Avoid creating another CONSTRUCTOR when we expand the TARGET_EXPR. */
6757 53870 : value = TARGET_EXPR_INITIAL (value);
6758 : }
6759 :
6760 : /* We're about to process an initializer for a class or array TYPE. Make
6761 : sure that CTX is set up appropriately. */
6762 :
6763 : static void
6764 1508447 : verify_ctor_sanity (const constexpr_ctx *ctx, tree type)
6765 : {
6766 : /* We don't bother building a ctor for an empty base subobject. */
6767 1508447 : if (is_empty_class (type))
6768 : return;
6769 :
6770 : /* We're in the middle of an initializer that might involve placeholders;
6771 : our caller should have created a CONSTRUCTOR for us to put the
6772 : initializer into. We will either return that constructor or T. */
6773 852232 : gcc_assert (ctx->ctor);
6774 852232 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
6775 : (type, TREE_TYPE (ctx->ctor)));
6776 : /* We used to check that ctx->ctor was empty, but that isn't the case when
6777 : the object is zero-initialized before calling the constructor. */
6778 852232 : if (ctx->object)
6779 : {
6780 711274 : tree otype = TREE_TYPE (ctx->object);
6781 711274 : gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, otype)
6782 : /* Handle flexible array members. */
6783 : || (TREE_CODE (otype) == ARRAY_TYPE
6784 : && TYPE_DOMAIN (otype) == NULL_TREE
6785 : && TREE_CODE (type) == ARRAY_TYPE
6786 : && (same_type_ignoring_top_level_qualifiers_p
6787 : (TREE_TYPE (type), TREE_TYPE (otype)))));
6788 : }
6789 852232 : gcc_assert (!ctx->object || !DECL_P (ctx->object)
6790 : || ctx->global->get_value (ctx->object) == ctx->ctor);
6791 : }
6792 :
6793 : /* Subroutine of cxx_eval_constant_expression.
6794 : The expression tree T denotes a C-style array or a C-style
6795 : aggregate. Reduce it to a constant expression. */
6796 :
6797 : static tree
6798 1507791 : cxx_eval_bare_aggregate (const constexpr_ctx *ctx, tree t,
6799 : value_cat lval,
6800 : bool *non_constant_p, bool *overflow_p,
6801 : tree *jump_target)
6802 : {
6803 1507791 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
6804 1507791 : bool changed = false;
6805 1507791 : gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (t));
6806 1507791 : tree type = TREE_TYPE (t);
6807 :
6808 1507791 : constexpr_ctx new_ctx;
6809 1507791 : if (TYPE_PTRMEMFUNC_P (type) || VECTOR_TYPE_P (type))
6810 : {
6811 : /* We don't really need the ctx->ctor business for a PMF or
6812 : vector, but it's simpler to use the same code. */
6813 32140 : new_ctx = *ctx;
6814 32140 : new_ctx.ctor = build_constructor (type, NULL);
6815 32140 : new_ctx.object = NULL_TREE;
6816 32140 : ctx = &new_ctx;
6817 1507791 : };
6818 1507791 : verify_ctor_sanity (ctx, type);
6819 1507791 : vec<constructor_elt, va_gc> **p = nullptr;
6820 1507791 : if (ctx->ctor)
6821 : {
6822 1507783 : p = &CONSTRUCTOR_ELTS (ctx->ctor);
6823 3013363 : vec_alloc (*p, vec_safe_length (v));
6824 1507783 : if (CONSTRUCTOR_PLACEHOLDER_BOUNDARY (t))
6825 16053 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor) = 1;
6826 : }
6827 :
6828 1507791 : unsigned i;
6829 1507791 : tree index, value;
6830 1507791 : bool constant_p = true;
6831 1507791 : bool side_effects_p = false;
6832 3089477 : FOR_EACH_CONSTRUCTOR_ELT (v, i, index, value)
6833 : {
6834 1887483 : tree orig_value = value;
6835 1887483 : init_subob_ctx (ctx, new_ctx, index, value);
6836 : /* Like in cxx_eval_store_expression, omit entries for empty fields. */
6837 1887483 : bool no_slot = new_ctx.ctor == NULL_TREE;
6838 1887483 : int pos_hint = -1;
6839 1887483 : if (new_ctx.ctor != ctx->ctor && !no_slot)
6840 : {
6841 : /* If we built a new CONSTRUCTOR, attach it now so that other
6842 : initializers can refer to it. */
6843 70024 : constructor_elt *cep = get_or_insert_ctor_field (ctx->ctor, index);
6844 70024 : cep->value = new_ctx.ctor;
6845 70024 : pos_hint = cep - (*p)->begin();
6846 70024 : }
6847 1817459 : else if (TREE_CODE (type) == UNION_TYPE)
6848 : /* Otherwise if we're constructing a non-aggregate union member, set
6849 : the active union member now so that we can later detect and diagnose
6850 : if its initializer attempts to activate another member. */
6851 1095 : get_or_insert_ctor_field (ctx->ctor, index);
6852 1887483 : tree elt = cxx_eval_constant_expression (&new_ctx, value,
6853 : lval,
6854 : non_constant_p, overflow_p,
6855 : jump_target);
6856 1887483 : if (*jump_target)
6857 : return NULL_TREE;
6858 : /* Don't VERIFY_CONSTANT here. */
6859 1887480 : if (ctx->quiet && *non_constant_p)
6860 : break;
6861 1581686 : if (elt != orig_value)
6862 468947 : changed = true;
6863 :
6864 1581686 : if (!TREE_CONSTANT (elt))
6865 446172 : constant_p = false;
6866 1581686 : if (TREE_SIDE_EFFECTS (elt))
6867 191 : side_effects_p = true;
6868 1581686 : if (index && TREE_CODE (index) == COMPONENT_REF)
6869 : {
6870 : /* This is an initialization of a vfield inside a base
6871 : subaggregate that we already initialized; push this
6872 : initialization into the previous initialization. */
6873 0 : constructor_elt *inner = base_field_constructor_elt (*p, index);
6874 0 : inner->value = elt;
6875 0 : changed = true;
6876 0 : }
6877 1581686 : else if (no_slot)
6878 : /* This is an initializer for an empty field; now that we've
6879 : checked that it's constant, we can ignore it. */
6880 : changed = true;
6881 919239 : else if (index
6882 908013 : && (TREE_CODE (index) == NOP_EXPR
6883 908013 : || TREE_CODE (index) == POINTER_PLUS_EXPR))
6884 : {
6885 : /* Old representation of empty bases. FIXME remove. */
6886 0 : gcc_checking_assert (false);
6887 : gcc_assert (is_empty_class (TREE_TYPE (TREE_TYPE (index))));
6888 : changed = true;
6889 : }
6890 : else
6891 : {
6892 919239 : if (TREE_CODE (type) == UNION_TYPE
6893 919239 : && (*p)->last().index != index)
6894 : /* The initializer erroneously changed the active union member that
6895 : we're initializing. */
6896 7 : gcc_assert (*non_constant_p);
6897 : else
6898 : {
6899 : /* The initializer might have mutated the underlying CONSTRUCTOR,
6900 : so recompute the location of the target constructer_elt. */
6901 919232 : constructor_elt *cep
6902 919232 : = get_or_insert_ctor_field (ctx->ctor, index, pos_hint);
6903 919232 : cep->value = elt;
6904 : }
6905 :
6906 : /* Adding or replacing an element might change the ctor's flags. */
6907 919239 : TREE_CONSTANT (ctx->ctor) = constant_p;
6908 919239 : TREE_SIDE_EFFECTS (ctx->ctor) = side_effects_p;
6909 : }
6910 : }
6911 1507788 : if (*non_constant_p)
6912 : return t;
6913 1201943 : if (!changed)
6914 : {
6915 183843 : if (VECTOR_TYPE_P (type))
6916 14357 : t = fold (t);
6917 183843 : return t;
6918 : }
6919 1018100 : t = ctx->ctor;
6920 1018100 : if (!t)
6921 8 : t = build_constructor (type, NULL);
6922 : /* We're done building this CONSTRUCTOR, so now we can interpret an
6923 : element without an explicit initializer as value-initialized. */
6924 1018100 : CONSTRUCTOR_NO_CLEARING (t) = false;
6925 1018100 : TREE_CONSTANT (t) = constant_p;
6926 1018100 : TREE_SIDE_EFFECTS (t) = side_effects_p;
6927 1018100 : if (VECTOR_TYPE_P (type))
6928 4832 : t = fold (t);
6929 : return t;
6930 : }
6931 :
6932 : /* Subroutine of cxx_eval_constant_expression.
6933 : The expression tree T is a VEC_INIT_EXPR which denotes the desired
6934 : initialization of a non-static data member of array type. Reduce it to a
6935 : CONSTRUCTOR.
6936 :
6937 : Note that apart from value-initialization (when VALUE_INIT is true),
6938 : this is only intended to support value-initialization and the
6939 : initializations done by defaulted constructors for classes with
6940 : non-static data members of array type. In this case, VEC_INIT_EXPR_INIT
6941 : will either be NULL_TREE for the default constructor, or a COMPONENT_REF
6942 : for the copy/move constructor. */
6943 :
6944 : static tree
6945 656 : cxx_eval_vec_init_1 (const constexpr_ctx *ctx, tree atype, tree init,
6946 : bool value_init, value_cat lval,
6947 : bool *non_constant_p, bool *overflow_p,
6948 : tree *jump_target)
6949 : {
6950 656 : tree elttype = TREE_TYPE (atype);
6951 656 : verify_ctor_sanity (ctx, atype);
6952 656 : vec<constructor_elt, va_gc> **p = &CONSTRUCTOR_ELTS (ctx->ctor);
6953 656 : bool pre_init = false;
6954 656 : unsigned HOST_WIDE_INT i;
6955 656 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
6956 :
6957 656 : if (init && TREE_CODE (init) == CONSTRUCTOR)
6958 0 : return cxx_eval_bare_aggregate (ctx, init, lval,
6959 0 : non_constant_p, overflow_p, jump_target);
6960 :
6961 : /* We already checked access when building the VEC_INIT_EXPR. */
6962 656 : deferring_access_check_sentinel acs (dk_deferred);
6963 :
6964 : /* For the default constructor, build up a call to the default
6965 : constructor of the element type. We only need to handle class types
6966 : here, as for a constructor to be constexpr, all members must be
6967 : initialized, which for a defaulted default constructor means they must
6968 : be of a class type with a constexpr default constructor. */
6969 656 : if (TREE_CODE (elttype) == ARRAY_TYPE)
6970 : /* We only do this at the lowest level. */;
6971 607 : else if (value_init)
6972 : {
6973 174 : init = build_value_init (elttype, complain);
6974 174 : pre_init = true;
6975 : }
6976 433 : else if (!init)
6977 : {
6978 323 : releasing_vec argvec;
6979 323 : init = build_special_member_call (NULL_TREE, complete_ctor_identifier,
6980 : &argvec, elttype, LOOKUP_NORMAL,
6981 : complain);
6982 323 : init = build_aggr_init_expr (elttype, init);
6983 323 : pre_init = true;
6984 323 : }
6985 :
6986 656 : bool zeroed_out = false;
6987 656 : if (!CONSTRUCTOR_NO_CLEARING (ctx->ctor))
6988 : {
6989 : /* We're initializing an array object that had been zero-initialized
6990 : earlier. Truncate ctx->ctor, and propagate its zeroed state by
6991 : clearing CONSTRUCTOR_NO_CLEARING on each of the aggregate element
6992 : initializers we append to it. */
6993 156 : gcc_checking_assert (initializer_zerop (ctx->ctor));
6994 156 : zeroed_out = true;
6995 156 : vec_safe_truncate (*p, 0);
6996 : }
6997 :
6998 656 : tree nelts = get_array_or_vector_nelts (ctx, atype, non_constant_p,
6999 : overflow_p, jump_target);
7000 656 : if (*jump_target)
7001 : return NULL_TREE;
7002 656 : unsigned HOST_WIDE_INT max = tree_to_uhwi (nelts);
7003 6388 : for (i = 0; i < max; ++i)
7004 : {
7005 6171 : tree idx = build_int_cst (size_type_node, i);
7006 6171 : tree eltinit;
7007 6171 : bool reuse = false;
7008 6171 : constexpr_ctx new_ctx;
7009 6610 : init_subob_ctx (ctx, new_ctx, idx, pre_init ? init : elttype);
7010 6171 : bool no_slot = new_ctx.ctor == NULL_TREE;
7011 6171 : if (new_ctx.ctor != ctx->ctor && !no_slot)
7012 : {
7013 5992 : if (zeroed_out)
7014 5445 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = false;
7015 5992 : CONSTRUCTOR_APPEND_ELT (*p, idx, new_ctx.ctor);
7016 : }
7017 6171 : if (TREE_CODE (elttype) == ARRAY_TYPE)
7018 : {
7019 : /* A multidimensional array; recurse. */
7020 181 : if (value_init || init == NULL_TREE)
7021 : {
7022 171 : eltinit = NULL_TREE;
7023 171 : reuse = i == 0;
7024 : }
7025 : else
7026 10 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7027 181 : eltinit = cxx_eval_vec_init_1 (&new_ctx, elttype, eltinit,
7028 : value_init, lval, non_constant_p,
7029 : overflow_p, jump_target);
7030 : }
7031 5990 : else if (pre_init)
7032 : {
7033 : /* Initializing an element using value or default initialization
7034 : we just pre-built above. */
7035 5732 : if (init == void_node)
7036 : /* Trivial default-init, don't do anything to the CONSTRUCTOR. */
7037 3 : return ctx->ctor;
7038 5729 : eltinit = init;
7039 5729 : if (CLASS_TYPE_P (elttype) && new_ctx.object)
7040 : /* Clarify what object is being initialized (118285). */
7041 5593 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7042 5729 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7043 : non_constant_p, overflow_p,
7044 : jump_target);
7045 5729 : reuse = i == 0;
7046 : }
7047 : else
7048 : {
7049 : /* Copying an element. */
7050 258 : eltinit = cp_build_array_ref (input_location, init, idx, complain);
7051 258 : if (!lvalue_p (init))
7052 229 : eltinit = move (eltinit);
7053 258 : eltinit = (perform_implicit_conversion_flags
7054 258 : (elttype, eltinit, complain,
7055 : LOOKUP_IMPLICIT|LOOKUP_NO_NARROWING));
7056 251 : if (CLASS_TYPE_P (elttype)
7057 251 : && new_ctx.object
7058 498 : && !error_operand_p (eltinit))
7059 : /* Clarify what object is being initialized (118285). */
7060 238 : eltinit = build2 (INIT_EXPR, elttype, new_ctx.object, eltinit);
7061 258 : eltinit = cxx_eval_constant_expression (&new_ctx, eltinit, lval,
7062 : non_constant_p, overflow_p,
7063 : jump_target);
7064 : }
7065 6168 : if (*jump_target)
7066 : return NULL_TREE;
7067 6168 : if (*non_constant_p)
7068 : break;
7069 6043 : if (no_slot)
7070 : {
7071 : /* This is an initializer for an empty subobject; now that we've
7072 : checked that it's constant, we can ignore it. */
7073 18 : gcc_checking_assert (i == 0);
7074 : break;
7075 : }
7076 6025 : else if (new_ctx.ctor != ctx->ctor)
7077 : {
7078 : /* We appended this element above; update the value. */
7079 5895 : gcc_assert ((*p)->last().index == idx);
7080 5895 : (*p)->last().value = eltinit;
7081 : }
7082 : else
7083 130 : CONSTRUCTOR_APPEND_ELT (*p, idx, eltinit);
7084 : /* Reuse the result of cxx_eval_constant_expression call
7085 : from the first iteration to all others if it is a constant
7086 : initializer that doesn't require relocations. */
7087 6025 : if (reuse
7088 6025 : && max > 1
7089 6025 : && (eltinit == NULL_TREE
7090 446 : || (initializer_constant_valid_p (eltinit, TREE_TYPE (eltinit))
7091 446 : == null_pointer_node)))
7092 : {
7093 293 : if (new_ctx.ctor != ctx->ctor)
7094 169 : eltinit = new_ctx.ctor;
7095 293 : tree range = build2 (RANGE_EXPR, size_type_node,
7096 : build_int_cst (size_type_node, 1),
7097 293 : build_int_cst (size_type_node, max - 1));
7098 293 : CONSTRUCTOR_APPEND_ELT (*p, range, unshare_constructor (eltinit));
7099 293 : break;
7100 : }
7101 5732 : else if (i == 0)
7102 214 : vec_safe_reserve (*p, max);
7103 : }
7104 :
7105 653 : if (!*non_constant_p)
7106 : {
7107 528 : init = ctx->ctor;
7108 528 : CONSTRUCTOR_NO_CLEARING (init) = false;
7109 : }
7110 653 : return init;
7111 : }
7112 :
7113 : static tree
7114 538 : cxx_eval_vec_init (const constexpr_ctx *ctx, tree t,
7115 : value_cat lval,
7116 : bool *non_constant_p, bool *overflow_p, tree *jump_target)
7117 : {
7118 538 : tree atype = TREE_TYPE (t);
7119 538 : tree init = VEC_INIT_EXPR_INIT (t);
7120 538 : bool value_init = VEC_INIT_EXPR_VALUE_INIT (t);
7121 538 : if (!init || !BRACE_ENCLOSED_INITIALIZER_P (init))
7122 : ;
7123 74 : else if (CONSTRUCTOR_NELTS (init) == 0
7124 74 : && !CP_AGGREGATE_TYPE_P (strip_array_types (atype)))
7125 : {
7126 : /* Handle {} as value-init. */
7127 : init = NULL_TREE;
7128 : value_init = true;
7129 : }
7130 : else
7131 : {
7132 : /* This is a more complicated case, like needing to loop over trailing
7133 : elements; call build_vec_init and evaluate the result. */
7134 63 : tsubst_flags_t complain = ctx->quiet ? tf_none : tf_warning_or_error;
7135 63 : constexpr_ctx new_ctx = *ctx;
7136 63 : if (!ctx->object)
7137 : {
7138 : /* We want to have an initialization target for an VEC_INIT_EXPR.
7139 : If we don't already have one in CTX, use the VEC_INIT_EXPR_SLOT. */
7140 51 : new_ctx.object = VEC_INIT_EXPR_SLOT (t);
7141 51 : tree ctor = new_ctx.ctor = build_constructor (atype, NULL);
7142 51 : CONSTRUCTOR_NO_CLEARING (ctor) = true;
7143 51 : ctx->global->put_value (new_ctx.object, ctor);
7144 51 : ctx = &new_ctx;
7145 : }
7146 63 : init = expand_vec_init_expr (ctx->object, t, complain);
7147 63 : return cxx_eval_constant_expression (ctx, init, lval, non_constant_p,
7148 : overflow_p, jump_target);
7149 : }
7150 475 : tree r = cxx_eval_vec_init_1 (ctx, atype, init, value_init,
7151 : lval, non_constant_p, overflow_p, jump_target);
7152 475 : if (*non_constant_p)
7153 : return t;
7154 : else
7155 373 : return r;
7156 : }
7157 :
7158 : /* Like same_type_ignoring_top_level_qualifiers_p, but also handle the case
7159 : where the desired type is an array of unknown bounds because the variable
7160 : has had its bounds deduced since the wrapping expression was created. */
7161 :
7162 : static bool
7163 257360573 : same_type_ignoring_tlq_and_bounds_p (tree type1, tree type2)
7164 : {
7165 257360573 : while (TREE_CODE (type1) == ARRAY_TYPE
7166 54418 : && TREE_CODE (type2) == ARRAY_TYPE
7167 257360577 : && (!TYPE_DOMAIN (type1) || !TYPE_DOMAIN (type2)))
7168 : {
7169 2 : type1 = TREE_TYPE (type1);
7170 2 : type2 = TREE_TYPE (type2);
7171 : }
7172 257360573 : return same_type_ignoring_top_level_qualifiers_p (type1, type2);
7173 : }
7174 :
7175 : /* Try to determine the currently active union member for an expression
7176 : with UNION_TYPE. If it can be determined, return the FIELD_DECL,
7177 : otherwise return NULL_TREE. */
7178 :
7179 : static tree
7180 76 : cxx_union_active_member (const constexpr_ctx *ctx, tree t, tree *jump_target)
7181 : {
7182 76 : constexpr_ctx new_ctx = *ctx;
7183 76 : new_ctx.quiet = true;
7184 76 : bool non_constant_p = false, overflow_p = false;
7185 76 : tree ctor = cxx_eval_constant_expression (&new_ctx, t, vc_prvalue,
7186 : &non_constant_p,
7187 : &overflow_p, jump_target);
7188 76 : if (*jump_target)
7189 : return NULL_TREE;
7190 76 : if (TREE_CODE (ctor) == CONSTRUCTOR
7191 28 : && CONSTRUCTOR_NELTS (ctor) == 1
7192 16 : && CONSTRUCTOR_ELT (ctor, 0)->index
7193 92 : && TREE_CODE (CONSTRUCTOR_ELT (ctor, 0)->index) == FIELD_DECL)
7194 : return CONSTRUCTOR_ELT (ctor, 0)->index;
7195 : return NULL_TREE;
7196 : }
7197 :
7198 : /* Helper function for cxx_fold_indirect_ref_1, called recursively. */
7199 :
7200 : static tree
7201 8594987 : cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type,
7202 : tree op, unsigned HOST_WIDE_INT off, bool *empty_base,
7203 : tree *jump_target)
7204 : {
7205 13621656 : tree optype = TREE_TYPE (op);
7206 13621656 : unsigned HOST_WIDE_INT const_nunits;
7207 13621656 : if (off == 0 && similar_type_p (optype, type))
7208 : return op;
7209 8592458 : else if (cxx_dialect >= cxx26
7210 3308224 : && VAR_P (op)
7211 1410960 : && DECL_VTABLE_OR_VTT_P (op)
7212 8827 : && same_type_ignoring_top_level_qualifiers_p (type,
7213 : ptrdiff_type_node)
7214 8593440 : && POINTER_TYPE_P (strip_array_types (optype)))
7215 : {
7216 : /* We often read some virtual table elements using ptrdiff_t rather
7217 : than pointer type. */
7218 982 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc,
7219 : strip_array_types (optype),
7220 : op, off, empty_base,
7221 : jump_target))
7222 982 : return fold_convert (type, ret);
7223 : }
7224 8591476 : else if (TREE_CODE (optype) == COMPLEX_TYPE
7225 8591476 : && similar_type_p (type, TREE_TYPE (optype)))
7226 : {
7227 : /* *(foo *)&complexfoo => __real__ complexfoo */
7228 0 : if (off == 0)
7229 0 : return build1_loc (loc, REALPART_EXPR, type, op);
7230 : /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
7231 0 : else if (tree_to_uhwi (TYPE_SIZE_UNIT (type)) == off)
7232 0 : return build1_loc (loc, IMAGPART_EXPR, type, op);
7233 : }
7234 : /* ((foo*)&vectorfoo)[x] => BIT_FIELD_REF<vectorfoo,...> */
7235 8591476 : else if (VECTOR_TYPE_P (optype)
7236 0 : && similar_type_p (type, TREE_TYPE (optype))
7237 8591476 : && TYPE_VECTOR_SUBPARTS (optype).is_constant (&const_nunits))
7238 : {
7239 0 : unsigned HOST_WIDE_INT part_width = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7240 0 : unsigned HOST_WIDE_INT max_offset = part_width * const_nunits;
7241 0 : if (off < max_offset && off % part_width == 0)
7242 : {
7243 0 : tree index = bitsize_int (off * BITS_PER_UNIT);
7244 0 : return build3_loc (loc, BIT_FIELD_REF, type, op,
7245 0 : TYPE_SIZE (type), index);
7246 : }
7247 : }
7248 : /* ((foo *)&fooarray)[x] => fooarray[x] */
7249 8591476 : else if (TREE_CODE (optype) == ARRAY_TYPE
7250 5026669 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (optype)))
7251 13618145 : && !integer_zerop (TYPE_SIZE_UNIT (TREE_TYPE (optype))))
7252 : {
7253 5026669 : tree type_domain = TYPE_DOMAIN (optype);
7254 5026669 : tree min_val = size_zero_node;
7255 5026669 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7256 5026530 : min_val = TYPE_MIN_VALUE (type_domain);
7257 5026669 : unsigned HOST_WIDE_INT el_sz
7258 5026669 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (optype)));
7259 5026669 : unsigned HOST_WIDE_INT idx = off / el_sz;
7260 5026669 : unsigned HOST_WIDE_INT rem = off % el_sz;
7261 5026669 : if (tree_fits_uhwi_p (min_val))
7262 : {
7263 5026669 : tree index = size_int (idx + tree_to_uhwi (min_val));
7264 5026669 : op = build4_loc (loc, ARRAY_REF, TREE_TYPE (optype), op, index,
7265 : NULL_TREE, NULL_TREE);
7266 5026669 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op, rem,
7267 5026669 : empty_base, jump_target);
7268 : }
7269 : }
7270 : /* ((foo *)&struct_with_foo_field)[x] => COMPONENT_REF */
7271 3564807 : else if (TREE_CODE (optype) == RECORD_TYPE
7272 3564807 : || TREE_CODE (optype) == UNION_TYPE)
7273 : {
7274 3561392 : if (TREE_CODE (optype) == UNION_TYPE)
7275 : /* For unions prefer the currently active member. */
7276 76 : if (tree field = cxx_union_active_member (ctx, op, jump_target))
7277 : {
7278 16 : unsigned HOST_WIDE_INT el_sz
7279 16 : = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7280 16 : if (off < el_sz)
7281 : {
7282 16 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7283 : op, field, NULL_TREE);
7284 16 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7285 : off, empty_base,
7286 : jump_target))
7287 : return ret;
7288 : }
7289 : }
7290 :
7291 : /* Handle conversion to "as base" type. */
7292 3561382 : if (CLASS_TYPE_P (optype)
7293 7122600 : && CLASSTYPE_AS_BASE (optype) == type)
7294 : return op;
7295 :
7296 : /* Handle conversion to an empty base class, which is represented with a
7297 : NOP_EXPR. Do this before spelunking into the non-empty subobjects,
7298 : which is likely to be a waste of time (109678). */
7299 3558805 : if (is_empty_class (type)
7300 3388966 : && CLASS_TYPE_P (optype)
7301 6947765 : && lookup_base (optype, type, ba_any, NULL, tf_none, off))
7302 : {
7303 1761888 : if (empty_base)
7304 1761888 : *empty_base = true;
7305 1761888 : return op;
7306 : }
7307 :
7308 1796917 : for (tree field = TYPE_FIELDS (optype);
7309 23137764 : field; field = DECL_CHAIN (field))
7310 23122821 : if (TREE_CODE (field) == FIELD_DECL
7311 1802701 : && TREE_TYPE (field) != error_mark_node
7312 24925522 : && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (field))))
7313 : {
7314 1802697 : tree pos = byte_position (field);
7315 1802697 : if (!tree_fits_uhwi_p (pos))
7316 0 : continue;
7317 1802697 : unsigned HOST_WIDE_INT upos = tree_to_uhwi (pos);
7318 1802697 : unsigned HOST_WIDE_INT el_sz;
7319 1802697 : if (DECL_FIELD_IS_BASE (field)
7320 391431 : && CLASS_TYPE_P (optype)
7321 2194128 : && CLASSTYPE_VBASECLASSES (optype))
7322 6189 : el_sz = tree_to_uhwi (DECL_SIZE_UNIT (field));
7323 : else
7324 1796508 : el_sz = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (field)));
7325 1802697 : if (upos <= off && off < upos + el_sz)
7326 : {
7327 1796814 : tree cop = build3 (COMPONENT_REF, TREE_TYPE (field),
7328 : op, field, NULL_TREE);
7329 1796814 : if (tree ret = cxx_fold_indirect_ref_1 (ctx, loc, type, cop,
7330 : off - upos,
7331 : empty_base,
7332 : jump_target))
7333 : return ret;
7334 : }
7335 : }
7336 : }
7337 :
7338 : return NULL_TREE;
7339 : }
7340 :
7341 : /* A less strict version of fold_indirect_ref_1, which requires cv-quals to
7342 : match. We want to be less strict for simple *& folding; if we have a
7343 : non-const temporary that we access through a const pointer, that should
7344 : work. We handle this here rather than change fold_indirect_ref_1
7345 : because we're dealing with things like ADDR_EXPR of INTEGER_CST which
7346 : don't really make sense outside of constant expression evaluation. Also
7347 : we want to allow folding to COMPONENT_REF, which could cause trouble
7348 : with TBAA in fold_indirect_ref_1. */
7349 :
7350 : static tree
7351 109314966 : cxx_fold_indirect_ref (const constexpr_ctx *ctx, location_t loc, tree type,
7352 : tree op0, bool *empty_base, tree *jump_target)
7353 : {
7354 109314966 : tree sub = op0;
7355 109314966 : tree subtype;
7356 :
7357 : /* STRIP_NOPS, but stop if REINTERPRET_CAST_P. */
7358 226339088 : while (CONVERT_EXPR_P (sub) || TREE_CODE (sub) == NON_LVALUE_EXPR
7359 298454040 : || TREE_CODE (sub) == VIEW_CONVERT_EXPR)
7360 : {
7361 77906102 : if (TREE_CODE (sub) == NOP_EXPR
7362 77906102 : && REINTERPRET_CAST_P (sub))
7363 : return NULL_TREE;
7364 77906102 : sub = TREE_OPERAND (sub, 0);
7365 : }
7366 :
7367 109314966 : subtype = TREE_TYPE (sub);
7368 109314966 : if (!INDIRECT_TYPE_P (subtype))
7369 : return NULL_TREE;
7370 :
7371 : /* Canonicalizes the given OBJ/OFF pair by iteratively absorbing
7372 : the innermost component into the offset until it would make the
7373 : offset positive, so that cxx_fold_indirect_ref_1 can identify
7374 : more folding opportunities. */
7375 116112089 : auto canonicalize_obj_off = [] (tree& obj, tree& off) {
7376 6797175 : if (cxx_dialect >= cxx26)
7377 : {
7378 : /* For C++26, we need to fold *(B *)(&x.D.1234 + 32) used
7379 : to access virtual base members. */
7380 2299188 : tree nobj = obj;
7381 2299188 : while (TREE_CODE (nobj) == COMPONENT_REF
7382 2318793 : && DECL_FIELD_IS_BASE (TREE_OPERAND (nobj, 1)))
7383 19605 : nobj = TREE_OPERAND (nobj, 0);
7384 2299188 : if (nobj != obj
7385 15653 : && CLASS_TYPE_P (TREE_TYPE (nobj))
7386 2314841 : && CLASSTYPE_VBASECLASSES (TREE_TYPE (nobj)))
7387 2039 : while (obj != nobj)
7388 : {
7389 1174 : tree field = TREE_OPERAND (obj, 1);
7390 1174 : tree pos = byte_position (field);
7391 1174 : off = int_const_binop (PLUS_EXPR, off, pos);
7392 1174 : obj = TREE_OPERAND (obj, 0);
7393 : }
7394 : }
7395 8602133 : while (TREE_CODE (obj) == COMPONENT_REF
7396 : /* We need to preserve union member accesses so that we can
7397 : later properly diagnose accessing the wrong member. */
7398 4527016 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (obj, 0))) == RECORD_TYPE
7399 13003710 : && (tree_int_cst_sign_bit (off) || integer_zerop (off)))
7400 : {
7401 1881281 : tree field = TREE_OPERAND (obj, 1);
7402 1881281 : tree pos = byte_position (field);
7403 1881281 : if (integer_zerop (off) && integer_nonzerop (pos))
7404 : /* If the offset is already 0, keep going as long as the
7405 : component is at position 0. */
7406 : break;
7407 1804958 : off = int_const_binop (PLUS_EXPR, off, pos);
7408 1804958 : obj = TREE_OPERAND (obj, 0);
7409 : }
7410 6797175 : };
7411 :
7412 109314914 : if (TREE_CODE (sub) == ADDR_EXPR)
7413 : {
7414 47491744 : tree op = TREE_OPERAND (sub, 0);
7415 47491744 : tree optype = TREE_TYPE (op);
7416 :
7417 : /* *&CONST_DECL -> to the value of the const decl. */
7418 47491744 : if (TREE_CODE (op) == CONST_DECL)
7419 0 : return DECL_INITIAL (op);
7420 : /* *&p => p; make sure to handle *&"str"[cst] here. */
7421 47491744 : if (similar_type_p (optype, type))
7422 : {
7423 44962452 : tree fop = fold_read_from_constant_string (op);
7424 44962452 : if (fop)
7425 : return fop;
7426 : else
7427 44860843 : return op;
7428 : }
7429 : else
7430 : {
7431 2529292 : tree off = integer_zero_node;
7432 2529292 : canonicalize_obj_off (op, off);
7433 2529292 : return cxx_fold_indirect_ref_1 (ctx, loc, type, op,
7434 : tree_to_uhwi (off), empty_base,
7435 : jump_target);
7436 : }
7437 : }
7438 61823170 : else if (TREE_CODE (sub) == POINTER_PLUS_EXPR
7439 61823170 : && tree_fits_uhwi_p (TREE_OPERAND (sub, 1)))
7440 : {
7441 4593858 : tree op00 = TREE_OPERAND (sub, 0);
7442 4593858 : tree off = TREE_OPERAND (sub, 1);
7443 :
7444 4593858 : STRIP_NOPS (op00);
7445 4593858 : if (TREE_CODE (op00) == ADDR_EXPR)
7446 : {
7447 4267883 : tree obj = TREE_OPERAND (op00, 0);
7448 4267883 : canonicalize_obj_off (obj, off);
7449 4267883 : return cxx_fold_indirect_ref_1 (ctx, loc, type, obj,
7450 : tree_to_uhwi (off), empty_base,
7451 : jump_target);
7452 : }
7453 : }
7454 : /* *(foo *)fooarrptr => (*fooarrptr)[0] */
7455 57229312 : else if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
7456 57229312 : && similar_type_p (type, TREE_TYPE (TREE_TYPE (subtype))))
7457 : {
7458 0 : tree type_domain;
7459 0 : tree min_val = size_zero_node;
7460 0 : tree newsub
7461 0 : = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (subtype), sub, NULL,
7462 : jump_target);
7463 0 : if (*jump_target)
7464 : return NULL_TREE;
7465 0 : if (newsub)
7466 : sub = newsub;
7467 : else
7468 0 : sub = build1_loc (loc, INDIRECT_REF, TREE_TYPE (subtype), sub);
7469 0 : type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
7470 0 : if (type_domain && TYPE_MIN_VALUE (type_domain))
7471 0 : min_val = TYPE_MIN_VALUE (type_domain);
7472 0 : return build4_loc (loc, ARRAY_REF, type, sub, min_val, NULL_TREE,
7473 0 : NULL_TREE);
7474 : }
7475 :
7476 : return NULL_TREE;
7477 : }
7478 :
7479 : static tree
7480 59084870 : cxx_eval_indirect_ref (const constexpr_ctx *ctx, tree t,
7481 : value_cat lval,
7482 : bool *non_constant_p, bool *overflow_p,
7483 : tree *jump_target)
7484 : {
7485 59084870 : tree orig_op0 = TREE_OPERAND (t, 0);
7486 59084870 : bool empty_base = false;
7487 :
7488 : /* We can handle a MEM_REF like an INDIRECT_REF, if MEM_REF's second
7489 : operand is an integer-zero. Otherwise reject the MEM_REF for now. */
7490 :
7491 59084870 : if (TREE_CODE (t) == MEM_REF
7492 59084870 : && (!TREE_OPERAND (t, 1) || !integer_zerop (TREE_OPERAND (t, 1))))
7493 : {
7494 9 : gcc_assert (ctx->quiet);
7495 9 : *non_constant_p = true;
7496 9 : return t;
7497 : }
7498 :
7499 : /* First try to simplify it directly. */
7500 59084861 : tree r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t),
7501 : orig_op0, &empty_base, jump_target);
7502 59084861 : if (*jump_target)
7503 : return NULL_TREE;
7504 59084861 : if (!r)
7505 : {
7506 : /* If that didn't work, evaluate the operand first. */
7507 56928791 : tree op0 = cxx_eval_constant_expression (ctx, orig_op0,
7508 : vc_prvalue, non_constant_p,
7509 : overflow_p, jump_target);
7510 56928791 : if (*jump_target)
7511 : return NULL_TREE;
7512 : /* Don't VERIFY_CONSTANT here. */
7513 56928782 : if (*non_constant_p)
7514 : return t;
7515 :
7516 40596916 : if (!lval && integer_zerop (op0))
7517 : {
7518 70 : if (!ctx->quiet)
7519 12 : error ("dereferencing a null pointer");
7520 70 : *non_constant_p = true;
7521 70 : return t;
7522 : }
7523 :
7524 40596846 : r = cxx_fold_indirect_ref (ctx, EXPR_LOCATION (t), TREE_TYPE (t), op0,
7525 : &empty_base, jump_target);
7526 40596846 : if (*jump_target)
7527 : return NULL_TREE;
7528 40596846 : if (r == NULL_TREE)
7529 : {
7530 : /* We couldn't fold to a constant value. Make sure it's not
7531 : something we should have been able to fold. */
7532 630011 : tree sub = op0;
7533 630011 : STRIP_NOPS (sub);
7534 630011 : if (TREE_CODE (sub) == ADDR_EXPR)
7535 : {
7536 1675 : gcc_assert (!similar_type_p
7537 : (TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t)));
7538 : /* DR 1188 says we don't have to deal with this. */
7539 1675 : if (!ctx->quiet)
7540 : {
7541 8 : auto_diagnostic_group d;
7542 13 : error_at (cp_expr_loc_or_input_loc (t),
7543 : "accessing value of %qT object through a %qT "
7544 : "glvalue in a constant expression",
7545 8 : TREE_TYPE (TREE_TYPE (sub)), TREE_TYPE (t));
7546 8 : tree ob = build_fold_indirect_ref (sub);
7547 8 : if (DECL_P (ob))
7548 : {
7549 8 : if (DECL_ARTIFICIAL (ob))
7550 1 : inform (DECL_SOURCE_LOCATION (ob),
7551 1 : "%qT object created here", TREE_TYPE (ob));
7552 : else
7553 7 : inform (DECL_SOURCE_LOCATION (ob),
7554 : "%q#D declared here", ob);
7555 : }
7556 8 : }
7557 1675 : *non_constant_p = true;
7558 1675 : return t;
7559 : }
7560 :
7561 628336 : if (lval == vc_glvalue && op0 != orig_op0)
7562 73418 : return build1 (INDIRECT_REF, TREE_TYPE (t), op0);
7563 554918 : if (!lval)
7564 466669 : VERIFY_CONSTANT (t);
7565 554918 : return t;
7566 : }
7567 : }
7568 :
7569 42122905 : r = cxx_eval_constant_expression (ctx, r,
7570 : lval, non_constant_p, overflow_p,
7571 : jump_target);
7572 42122904 : if (*jump_target)
7573 : return NULL_TREE;
7574 42122904 : if (*non_constant_p)
7575 : return t;
7576 :
7577 : /* If we're pulling out the value of an empty base, just return an empty
7578 : CONSTRUCTOR. */
7579 35797918 : if (empty_base && !lval)
7580 : {
7581 16448 : r = build_constructor (TREE_TYPE (t), NULL);
7582 16448 : TREE_CONSTANT (r) = true;
7583 : }
7584 :
7585 : return r;
7586 : }
7587 :
7588 : /* Complain about R, a DECL that is accessed outside its lifetime. */
7589 :
7590 : static void
7591 36 : outside_lifetime_error (location_t loc, tree r)
7592 : {
7593 36 : auto_diagnostic_group d;
7594 36 : if (DECL_NAME (r) == heap_deleted_identifier)
7595 : {
7596 : /* Provide a more accurate message for deleted variables. */
7597 6 : error_at (loc, "use of allocated storage after deallocation "
7598 : "in a constant expression");
7599 6 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7600 : }
7601 : else
7602 : {
7603 30 : error_at (loc, "accessing %qE outside its lifetime", r);
7604 30 : inform (DECL_SOURCE_LOCATION (r), "declared here");
7605 : }
7606 36 : }
7607 :
7608 : /* Complain about R, a VAR_DECL, not being usable in a constant expression.
7609 : FUNDEF_P is true if we're checking a constexpr function body.
7610 : Shared between potential_constant_expression and
7611 : cxx_eval_constant_expression. */
7612 :
7613 : static void
7614 343 : non_const_var_error (location_t loc, tree r, bool fundef_p)
7615 : {
7616 343 : auto_diagnostic_group d;
7617 343 : tree type = TREE_TYPE (r);
7618 343 : if (DECL_NAME (r) == heap_uninit_identifier
7619 343 : || DECL_NAME (r) == heap_identifier
7620 340 : || DECL_NAME (r) == heap_vec_uninit_identifier
7621 683 : || DECL_NAME (r) == heap_vec_identifier)
7622 : {
7623 3 : if (constexpr_error (loc, fundef_p, "the content of uninitialized "
7624 : "storage is not usable in a constant expression"))
7625 3 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7626 3 : return;
7627 : }
7628 340 : if (DECL_NAME (r) == heap_deleted_identifier)
7629 : {
7630 0 : if (constexpr_error (loc, fundef_p, "use of allocated storage after "
7631 : "deallocation in a constant expression"))
7632 0 : inform (DECL_SOURCE_LOCATION (r), "allocated here");
7633 0 : return;
7634 : }
7635 340 : if (!constexpr_error (loc, fundef_p, "the value of %qD is not usable in "
7636 : "a constant expression", r))
7637 : return;
7638 : /* Avoid error cascade. */
7639 337 : if (DECL_INITIAL (r) == error_mark_node)
7640 : return;
7641 323 : if (DECL_DECLARED_CONSTEXPR_P (r))
7642 3 : inform (DECL_SOURCE_LOCATION (r),
7643 : "%qD used in its own initializer", r);
7644 320 : else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
7645 : {
7646 219 : if (!CP_TYPE_CONST_P (type))
7647 190 : inform (DECL_SOURCE_LOCATION (r),
7648 : "%q#D is not const", r);
7649 29 : else if (CP_TYPE_VOLATILE_P (type))
7650 0 : inform (DECL_SOURCE_LOCATION (r),
7651 : "%q#D is volatile", r);
7652 29 : else if (!DECL_INITIAL (r)
7653 9 : || !TREE_CONSTANT (DECL_INITIAL (r))
7654 37 : || !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r))
7655 29 : inform (DECL_SOURCE_LOCATION (r),
7656 : "%qD was not initialized with a constant "
7657 : "expression", r);
7658 : else
7659 0 : gcc_unreachable ();
7660 : }
7661 101 : else if (TYPE_REF_P (type))
7662 9 : inform (DECL_SOURCE_LOCATION (r),
7663 : "%qD was not initialized with a constant "
7664 : "expression", r);
7665 : else
7666 : {
7667 92 : if (cxx_dialect >= cxx11 && !DECL_DECLARED_CONSTEXPR_P (r))
7668 92 : inform (DECL_SOURCE_LOCATION (r),
7669 : "%qD was not declared %<constexpr%>", r);
7670 : else
7671 0 : inform (DECL_SOURCE_LOCATION (r),
7672 : "%qD does not have integral or enumeration type",
7673 : r);
7674 : }
7675 343 : }
7676 :
7677 : /* Subroutine of cxx_eval_constant_expression.
7678 : Like cxx_eval_unary_expression, except for trinary expressions. */
7679 :
7680 : static tree
7681 14887 : cxx_eval_trinary_expression (const constexpr_ctx *ctx, tree t,
7682 : value_cat lval,
7683 : bool *non_constant_p, bool *overflow_p,
7684 : tree *jump_target)
7685 : {
7686 14887 : int i;
7687 14887 : tree args[3];
7688 14887 : tree val;
7689 :
7690 58078 : for (i = 0; i < 3; i++)
7691 : {
7692 43681 : args[i] = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, i),
7693 : lval,
7694 : non_constant_p, overflow_p,
7695 : jump_target);
7696 43681 : if (*jump_target)
7697 : return NULL_TREE;
7698 43681 : VERIFY_CONSTANT (args[i]);
7699 : }
7700 :
7701 14397 : val = fold_ternary_loc (EXPR_LOCATION (t), TREE_CODE (t), TREE_TYPE (t),
7702 : args[0], args[1], args[2]);
7703 14397 : if (val == NULL_TREE)
7704 : return t;
7705 14397 : VERIFY_CONSTANT (val);
7706 : return val;
7707 : }
7708 :
7709 : /* True if T was declared in a function declared to be constexpr, and
7710 : therefore potentially constant in C++14. */
7711 :
7712 : bool
7713 84092844 : var_in_constexpr_fn (tree t)
7714 : {
7715 84092844 : tree ctx = DECL_CONTEXT (t);
7716 84092844 : return (ctx && TREE_CODE (ctx) == FUNCTION_DECL
7717 160136503 : && DECL_DECLARED_CONSTEXPR_P (ctx));
7718 : }
7719 :
7720 : /* True if a function might be constexpr: either a function that was
7721 : declared constexpr, or a C++17 lambda op(). */
7722 :
7723 : bool
7724 585938527 : maybe_constexpr_fn (tree t)
7725 : {
7726 585938527 : return (DECL_DECLARED_CONSTEXPR_P (t)
7727 185187749 : || (cxx_dialect >= cxx17 && LAMBDA_FUNCTION_P (t))
7728 763417050 : || (flag_implicit_constexpr
7729 86 : && DECL_DECLARED_INLINE_P (STRIP_TEMPLATE (t))));
7730 : }
7731 :
7732 : /* True if T was declared in a function that might be constexpr: either a
7733 : function that was declared constexpr, or a C++17 lambda op(). */
7734 :
7735 : bool
7736 59814662 : var_in_maybe_constexpr_fn (tree t)
7737 : {
7738 119628714 : return (DECL_FUNCTION_SCOPE_P (t)
7739 108713524 : && maybe_constexpr_fn (DECL_CONTEXT (t)));
7740 : }
7741 :
7742 : /* We're assigning INIT to TARGET. In do_build_copy_constructor and
7743 : build_over_call we implement trivial copy of a class with tail padding using
7744 : assignment of character arrays, which is valid in normal code, but not in
7745 : constexpr evaluation. We don't need to worry about clobbering tail padding
7746 : in constexpr evaluation, so strip the type punning. */
7747 :
7748 : static void
7749 58379331 : maybe_simplify_trivial_copy (tree &target, tree &init)
7750 : {
7751 58379331 : if (TREE_CODE (target) == MEM_REF
7752 3706 : && TREE_CODE (init) == MEM_REF
7753 3542 : && TREE_TYPE (target) == TREE_TYPE (init)
7754 3542 : && TREE_CODE (TREE_TYPE (target)) == ARRAY_TYPE
7755 58382873 : && TREE_TYPE (TREE_TYPE (target)) == unsigned_char_type_node)
7756 : {
7757 3542 : target = build_fold_indirect_ref (TREE_OPERAND (target, 0));
7758 3542 : init = build_fold_indirect_ref (TREE_OPERAND (init, 0));
7759 : }
7760 58379331 : }
7761 :
7762 : /* Returns true if REF, which is a COMPONENT_REF, has any fields
7763 : of constant type. This does not check for 'mutable', so the
7764 : caller is expected to be mindful of that. */
7765 :
7766 : static bool
7767 419 : cref_has_const_field (tree ref)
7768 : {
7769 488 : while (TREE_CODE (ref) == COMPONENT_REF)
7770 : {
7771 479 : if (CP_TYPE_CONST_P (TREE_TYPE (TREE_OPERAND (ref, 1))))
7772 : return true;
7773 69 : ref = TREE_OPERAND (ref, 0);
7774 : }
7775 : return false;
7776 : }
7777 :
7778 : /* Return true if we are modifying something that is const during constant
7779 : expression evaluation. CODE is the code of the statement, OBJ is the
7780 : object in question, MUTABLE_P is true if one of the subobjects were
7781 : declared mutable. */
7782 :
7783 : static bool
7784 58338184 : modifying_const_object_p (tree_code code, tree obj, bool mutable_p)
7785 : {
7786 : /* If this is initialization, there's no problem. */
7787 58338184 : if (code != MODIFY_EXPR)
7788 : return false;
7789 :
7790 : /* [basic.type.qualifier] "A const object is an object of type
7791 : const T or a non-mutable subobject of a const object." */
7792 17904117 : if (mutable_p)
7793 : return false;
7794 :
7795 17903381 : if (TREE_READONLY (obj))
7796 : return true;
7797 :
7798 17899441 : if (CP_TYPE_CONST_P (TREE_TYPE (obj)))
7799 : {
7800 : /* Although a COMPONENT_REF may have a const type, we should
7801 : only consider it modifying a const object when any of the
7802 : field components is const. This can happen when using
7803 : constructs such as const_cast<const T &>(m), making something
7804 : const even though it wasn't declared const. */
7805 30510 : if (TREE_CODE (obj) == COMPONENT_REF)
7806 419 : return cref_has_const_field (obj);
7807 : else
7808 : return true;
7809 : }
7810 :
7811 : return false;
7812 : }
7813 :
7814 : /* Evaluate an INIT_EXPR or MODIFY_EXPR. */
7815 :
7816 : static tree
7817 58379331 : cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
7818 : value_cat lval,
7819 : bool *non_constant_p, bool *overflow_p,
7820 : tree *jump_target)
7821 : {
7822 58379331 : constexpr_ctx new_ctx = *ctx;
7823 :
7824 58379331 : tree init = TREE_OPERAND (t, 1);
7825 :
7826 : /* First we figure out where we're storing to. */
7827 58379331 : tree target = TREE_OPERAND (t, 0);
7828 :
7829 58379331 : maybe_simplify_trivial_copy (target, init);
7830 :
7831 58379331 : tree type = TREE_TYPE (target);
7832 58379331 : bool preeval = SCALAR_TYPE_P (type) || TREE_CODE (t) == MODIFY_EXPR;
7833 44040581 : if (preeval && !TREE_CLOBBER_P (init))
7834 : {
7835 : /* Ignore var = .DEFERRED_INIT (); for now, until PR121965 is fixed. */
7836 43549614 : if (flag_auto_var_init > AUTO_INIT_UNINITIALIZED
7837 15306583 : && TREE_CODE (init) == CALL_EXPR
7838 2120938 : && CALL_EXPR_FN (init) == NULL_TREE
7839 43549618 : && CALL_EXPR_IFN (init) == IFN_DEFERRED_INIT)
7840 4 : return void_node;
7841 :
7842 : /* Evaluate the value to be stored without knowing what object it will be
7843 : stored in, so that any side-effects happen first. */
7844 43549610 : if (!SCALAR_TYPE_P (type))
7845 53731 : new_ctx.ctor = new_ctx.object = NULL_TREE;
7846 43549610 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
7847 : non_constant_p, overflow_p,
7848 : jump_target);
7849 43549608 : if (*jump_target)
7850 : return NULL_TREE;
7851 43549485 : if (*non_constant_p)
7852 : return t;
7853 : }
7854 :
7855 50489268 : bool evaluated = false;
7856 50489268 : if (lval == vc_glvalue)
7857 : {
7858 : /* If we want to return a reference to the target, we need to evaluate it
7859 : as a whole; otherwise, only evaluate the innermost piece to avoid
7860 : building up unnecessary *_REFs. */
7861 0 : target = cxx_eval_constant_expression (ctx, target, lval,
7862 : non_constant_p, overflow_p,
7863 : jump_target);
7864 0 : evaluated = true;
7865 0 : if (*jump_target)
7866 : return NULL_TREE;
7867 0 : if (*non_constant_p)
7868 : return t;
7869 : }
7870 :
7871 : /* Find the underlying variable. */
7872 50489268 : releasing_vec refs;
7873 50489268 : tree object = NULL_TREE;
7874 : /* If we're modifying a const object, save it. */
7875 50489268 : tree const_object_being_modified = NULL_TREE;
7876 50489268 : bool mutable_p = false;
7877 : /* If we see a union, we can't ignore clobbers. */
7878 50489268 : int seen_union = 0;
7879 173702218 : for (tree probe = target; object == NULL_TREE; )
7880 : {
7881 123213282 : switch (TREE_CODE (probe))
7882 : {
7883 22235023 : case BIT_FIELD_REF:
7884 22235023 : case COMPONENT_REF:
7885 22235023 : case ARRAY_REF:
7886 22235023 : {
7887 22235023 : tree ob = TREE_OPERAND (probe, 0);
7888 22235023 : tree elt = TREE_OPERAND (probe, 1);
7889 22235023 : if (TREE_CODE (elt) == FIELD_DECL && DECL_MUTABLE_P (elt))
7890 : mutable_p = true;
7891 22235023 : if (TREE_CODE (probe) == ARRAY_REF)
7892 : {
7893 4197399 : elt = eval_and_check_array_index (ctx, probe, false,
7894 : non_constant_p, overflow_p,
7895 : jump_target);
7896 4197399 : if (*jump_target)
7897 66 : return NULL_TREE;
7898 4197399 : if (*non_constant_p)
7899 : return t;
7900 : }
7901 : /* We don't check modifying_const_object_p for ARRAY_REFs. Given
7902 : "int a[10]", an ARRAY_REF "a[2]" can be "const int", even though
7903 : the array isn't const. Instead, check "a" in the next iteration;
7904 : that will detect modifying "const int a[10]". */
7905 18037624 : else if (evaluated
7906 7849248 : && modifying_const_object_p (TREE_CODE (t), probe,
7907 : mutable_p)
7908 18038136 : && const_object_being_modified == NULL_TREE)
7909 : const_object_being_modified = probe;
7910 :
7911 : /* Track named member accesses for unions to validate modifications
7912 : that change active member. */
7913 22234957 : if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
7914 10188376 : vec_safe_push (refs, probe);
7915 : else
7916 12046581 : vec_safe_push (refs, NULL_TREE);
7917 :
7918 22234957 : vec_safe_push (refs, elt);
7919 22234957 : vec_safe_push (refs, TREE_TYPE (probe));
7920 22234957 : probe = ob;
7921 22234957 : if (TREE_CODE (TREE_TYPE (ob)) == UNION_TYPE)
7922 294753 : ++seen_union;
7923 : }
7924 22234957 : break;
7925 :
7926 16 : case REALPART_EXPR:
7927 16 : gcc_assert (refs->is_empty ());
7928 16 : vec_safe_push (refs, NULL_TREE);
7929 16 : vec_safe_push (refs, probe);
7930 16 : vec_safe_push (refs, TREE_TYPE (probe));
7931 16 : probe = TREE_OPERAND (probe, 0);
7932 16 : break;
7933 :
7934 17 : case IMAGPART_EXPR:
7935 17 : gcc_assert (refs->is_empty ());
7936 17 : vec_safe_push (refs, NULL_TREE);
7937 17 : vec_safe_push (refs, probe);
7938 17 : vec_safe_push (refs, TREE_TYPE (probe));
7939 17 : probe = TREE_OPERAND (probe, 0);
7940 17 : break;
7941 :
7942 100978226 : default:
7943 100978226 : if (evaluated)
7944 : object = probe;
7945 : else
7946 : {
7947 50489290 : tree pvar = tree_strip_any_location_wrapper (probe);
7948 50489290 : if (VAR_P (pvar) && DECL_ANON_UNION_VAR_P (pvar))
7949 : {
7950 : /* Stores to DECL_ANON_UNION_VAR_P var are allowed to change
7951 : active union member. */
7952 55 : probe = DECL_VALUE_EXPR (pvar);
7953 55 : break;
7954 : }
7955 50489235 : probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue,
7956 : non_constant_p, overflow_p,
7957 : jump_target);
7958 50489235 : evaluated = true;
7959 50489235 : if (*jump_target)
7960 : return NULL_TREE;
7961 50489235 : if (*non_constant_p)
7962 : return t;
7963 : }
7964 : break;
7965 : }
7966 : }
7967 :
7968 50488936 : if (modifying_const_object_p (TREE_CODE (t), object, mutable_p)
7969 50488936 : && const_object_being_modified == NULL_TREE)
7970 50488936 : const_object_being_modified = object;
7971 :
7972 50488936 : if (DECL_P (object)
7973 50487977 : && TREE_CLOBBER_P (init)
7974 50987580 : && DECL_NAME (object) == heap_deleted_identifier)
7975 : /* Ignore clobbers of deleted allocations for now; we'll get a better error
7976 : message later when operator delete is called. */
7977 15 : return void_node;
7978 :
7979 : /* And then find/build up our initializer for the path to the subobject
7980 : we're initializing. */
7981 50488921 : tree *valp;
7982 50488921 : if (DECL_P (object))
7983 50487962 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
7984 : else
7985 959 : valp = NULL;
7986 50488921 : if (!valp)
7987 : {
7988 : /* A constant-expression cannot modify objects from outside the
7989 : constant-expression. */
7990 6252 : if (!ctx->quiet)
7991 : {
7992 29 : auto_diagnostic_group d;
7993 29 : if (DECL_P (object) && DECL_NAME (object) == heap_deleted_identifier)
7994 : {
7995 0 : error ("modification of allocated storage after deallocation "
7996 : "is not a constant expression");
7997 0 : inform (DECL_SOURCE_LOCATION (object), "allocated here");
7998 : }
7999 29 : else if (DECL_P (object) && ctx->global->is_outside_lifetime (object))
8000 : {
8001 14 : if (TREE_CLOBBER_P (init))
8002 6 : error ("destroying %qE outside its lifetime", object);
8003 : else
8004 8 : error ("modification of %qE outside its lifetime "
8005 : "is not a constant expression", object);
8006 14 : inform (DECL_SOURCE_LOCATION (object), "declared here");
8007 : }
8008 : else
8009 : {
8010 15 : if (TREE_CLOBBER_P (init))
8011 6 : error ("destroying %qE from outside current evaluation "
8012 : "is not a constant expression", object);
8013 : else
8014 9 : error ("modification of %qE from outside current evaluation "
8015 : "is not a constant expression", object);
8016 : }
8017 29 : }
8018 6252 : *non_constant_p = true;
8019 6252 : return t;
8020 : }
8021 :
8022 : /* Handle explicit end-of-lifetime. */
8023 50482669 : if (TREE_CLOBBER_P (init))
8024 : {
8025 498579 : if (CLOBBER_KIND (init) >= CLOBBER_OBJECT_END
8026 498579 : && refs->is_empty ())
8027 : {
8028 115635 : ctx->global->destroy_value (object);
8029 115635 : return void_node;
8030 : }
8031 :
8032 378038 : if (!seen_union && !*valp
8033 386235 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END)
8034 3257 : return void_node;
8035 :
8036 : /* Ending the lifetime of a const object is OK. */
8037 : const_object_being_modified = NULL_TREE;
8038 : }
8039 :
8040 50363777 : type = TREE_TYPE (object);
8041 50363777 : bool no_zero_init = true;
8042 50363777 : bool zero_padding_bits = false;
8043 :
8044 100727554 : auto_vec<tree *> ctors;
8045 100727554 : releasing_vec indexes;
8046 100727554 : auto_vec<int> index_pos_hints;
8047 50363777 : bool activated_union_member_p = false;
8048 50363777 : bool empty_base = false;
8049 72461624 : while (!refs->is_empty ())
8050 : {
8051 22220364 : if (*valp == NULL_TREE)
8052 : {
8053 1928923 : *valp = build_constructor (type, NULL);
8054 1928923 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8055 1928923 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8056 : }
8057 20291441 : else if (STRIP_ANY_LOCATION_WRAPPER (*valp),
8058 20291441 : TREE_CODE (*valp) == STRING_CST)
8059 : {
8060 : /* An array was initialized with a string constant, and now
8061 : we're writing into one of its elements. Explode the
8062 : single initialization into a set of element
8063 : initializations. */
8064 10538 : gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
8065 :
8066 10538 : tree string = *valp;
8067 10538 : tree elt_type = TREE_TYPE (type);
8068 10538 : unsigned chars_per_elt = (TYPE_PRECISION (elt_type)
8069 10538 : / TYPE_PRECISION (char_type_node));
8070 10538 : unsigned num_elts = TREE_STRING_LENGTH (string) / chars_per_elt;
8071 10538 : tree ary_ctor = build_constructor (type, NULL);
8072 :
8073 10538 : vec_safe_reserve (CONSTRUCTOR_ELTS (ary_ctor), num_elts);
8074 21230 : for (unsigned ix = 0; ix != num_elts; ix++)
8075 : {
8076 10692 : constructor_elt elt =
8077 : {
8078 10692 : build_int_cst (size_type_node, ix),
8079 10692 : extract_string_elt (string, chars_per_elt, ix)
8080 10692 : };
8081 10692 : CONSTRUCTOR_ELTS (ary_ctor)->quick_push (elt);
8082 : }
8083 :
8084 10538 : *valp = ary_ctor;
8085 : }
8086 :
8087 22220364 : enum tree_code code = TREE_CODE (type);
8088 22220364 : tree reftype = refs->pop();
8089 22220364 : tree index = refs->pop();
8090 22220364 : bool is_access_expr = refs->pop() != NULL_TREE;
8091 :
8092 22220364 : if (code == COMPLEX_TYPE)
8093 : {
8094 33 : if (TREE_CODE (*valp) == COMPLEX_CST)
8095 29 : *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
8096 29 : TREE_IMAGPART (*valp));
8097 4 : else if (TREE_CODE (*valp) == CONSTRUCTOR
8098 2 : && CONSTRUCTOR_NELTS (*valp) == 0
8099 6 : && CONSTRUCTOR_NO_CLEARING (*valp))
8100 : {
8101 2 : tree r = build_constructor (reftype, NULL);
8102 2 : CONSTRUCTOR_NO_CLEARING (r) = 1;
8103 2 : *valp = build2 (COMPLEX_EXPR, type, r, r);
8104 : }
8105 33 : gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
8106 33 : ctors.safe_push (valp);
8107 33 : vec_safe_push (indexes, index);
8108 33 : valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
8109 33 : gcc_checking_assert (refs->is_empty ());
8110 : type = reftype;
8111 120392 : break;
8112 : }
8113 :
8114 : /* If the value of object is already zero-initialized, any new ctors for
8115 : subobjects will also be zero-initialized. Similarly with zeroing of
8116 : padding bits. */
8117 22220331 : no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
8118 22220331 : zero_padding_bits = CONSTRUCTOR_ZERO_PADDING_BITS (*valp);
8119 :
8120 22220331 : if (code == RECORD_TYPE && is_empty_field (index))
8121 : /* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
8122 : have no data and might have an offset lower than previously declared
8123 : fields, which confuses the middle-end. The code below will notice
8124 : that we don't have a CONSTRUCTOR for our inner target and just
8125 : return init. */
8126 : {
8127 : empty_base = true;
8128 : break;
8129 : }
8130 :
8131 : /* If a union is zero-initialized, its first non-static named data member
8132 : is zero-initialized (and therefore active). */
8133 22099972 : if (code == UNION_TYPE
8134 22099972 : && !no_zero_init
8135 22099972 : && CONSTRUCTOR_NELTS (*valp) == 0)
8136 2281 : if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
8137 2281 : CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);
8138 :
8139 : /* Check for implicit change of active member for a union. */
8140 :
8141 : /* LWG3436, CWG2675, c++/121068: The array object model is confused. For
8142 : now allow initializing an array element to activate the array. */
8143 22135488 : auto only_array_refs = [](const releasing_vec &refs)
8144 : {
8145 35520 : for (unsigned i = 1; i < refs->length(); i += 3)
8146 34 : if (TREE_CODE ((*refs)[i]) != INTEGER_CST)
8147 : return false;
8148 : return true;
8149 : };
8150 :
8151 22099972 : if (code == UNION_TYPE
8152 293983 : && (CONSTRUCTOR_NELTS (*valp) == 0
8153 149354 : || CONSTRUCTOR_ELT (*valp, 0)->index != index)
8154 : /* An INIT_EXPR of the last member in an access chain is always OK,
8155 : but still check implicit change of members earlier on; see
8156 : cpp2a/constexpr-union6.C. */
8157 22249093 : && !(TREE_CODE (t) == INIT_EXPR && only_array_refs (refs)))
8158 : {
8159 113635 : bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
8160 113635 : tree inner = strip_array_types (reftype);
8161 :
8162 113635 : if (has_active_member && cxx_dialect < cxx20)
8163 : {
8164 58 : if (!ctx->quiet)
8165 19 : error_at (cp_expr_loc_or_input_loc (t),
8166 : "change of the active member of a union "
8167 : "from %qD to %qD is not a constant expression "
8168 : "before C++20",
8169 19 : CONSTRUCTOR_ELT (*valp, 0)->index,
8170 : index);
8171 58 : *non_constant_p = true;
8172 : }
8173 113577 : else if (!is_access_expr
8174 26015 : || (TREE_CLOBBER_P (init)
8175 0 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8176 139592 : || (TREE_CODE (t) == MODIFY_EXPR
8177 26003 : && CLASS_TYPE_P (inner)
8178 17 : && !type_has_non_deleted_trivial_default_ctor (inner)))
8179 : {
8180 : /* Diagnose changing active union member after initialization
8181 : without a valid member access expression, as described in
8182 : [class.union.general] p5. */
8183 87571 : if (!ctx->quiet)
8184 : {
8185 33 : auto_diagnostic_group d;
8186 33 : if (has_active_member)
8187 24 : error_at (cp_expr_loc_or_input_loc (t),
8188 : "accessing %qD member instead of initialized "
8189 : "%qD member in constant expression",
8190 24 : index, CONSTRUCTOR_ELT (*valp, 0)->index);
8191 : else
8192 9 : error_at (cp_expr_loc_or_input_loc (t),
8193 : "accessing uninitialized member %qD",
8194 : index);
8195 33 : if (is_access_expr)
8196 3 : inform (DECL_SOURCE_LOCATION (index),
8197 : "%qD does not implicitly begin its lifetime "
8198 : "because %qT does not have a non-deleted "
8199 : "trivial default constructor, use "
8200 : "%<std::construct_at%> instead",
8201 : index, inner);
8202 : else
8203 30 : inform (DECL_SOURCE_LOCATION (index),
8204 : "initializing %qD requires a member access "
8205 : "expression as the left operand of the assignment",
8206 : index);
8207 33 : }
8208 87571 : *non_constant_p = true;
8209 : }
8210 26006 : else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
8211 : {
8212 : /* Diagnose changing the active union member while the union
8213 : is in the process of being initialized. */
8214 18 : if (!ctx->quiet)
8215 6 : error_at (cp_expr_loc_or_input_loc (t),
8216 : "change of the active member of a union "
8217 : "from %qD to %qD during initialization",
8218 6 : CONSTRUCTOR_ELT (*valp, 0)->index,
8219 : index);
8220 18 : *non_constant_p = true;
8221 : }
8222 : no_zero_init = true;
8223 : }
8224 :
8225 : /* Ending the lifetime of the active union member means the union no
8226 : longer has an active member. */
8227 293983 : if (code == UNION_TYPE && refs->is_empty ()
8228 62101 : && TREE_CLOBBER_P (init)
8229 22102504 : && CLOBBER_KIND (init) >= CLOBBER_OBJECT_END)
8230 : {
8231 1204 : vec_safe_truncate (CONSTRUCTOR_ELTS (*valp), 0);
8232 2125 : return void_node;
8233 : }
8234 :
8235 22098768 : ctors.safe_push (valp);
8236 22098768 : vec_safe_push (indexes, index);
8237 :
8238 : /* Avoid adding an _elt for a clobber when the whole CONSTRUCTOR is
8239 : uninitialized. */
8240 21153456 : int pos = (!seen_union && TREE_CLOBBER_P (init)
8241 879098 : && CONSTRUCTOR_NO_CLEARING (*valp)
8242 22882208 : && CLOBBER_KIND (init) < CLOBBER_OBJECT_END) ? -2 : -1;
8243 22098768 : constructor_elt *cep
8244 22098768 : = get_or_insert_ctor_field (*valp, index, pos);
8245 22098768 : if (cep == nullptr)
8246 921 : return void_node;
8247 22097847 : index_pos_hints.safe_push (cep - CONSTRUCTOR_ELTS (*valp)->begin());
8248 :
8249 22097847 : if (code == UNION_TYPE)
8250 : {
8251 292779 : activated_union_member_p = true;
8252 292779 : --seen_union;
8253 : }
8254 :
8255 22097847 : valp = &cep->value;
8256 22097847 : type = reftype;
8257 : }
8258 :
8259 : /* Change an "as-base" clobber to the real type;
8260 : we don't need to worry about padding in constexpr. */
8261 50361652 : tree itype = initialized_type (init);
8262 50361652 : if (IS_FAKE_BASE_TYPE (itype))
8263 2204 : itype = TYPE_CONTEXT (itype);
8264 :
8265 : /* For initialization of an empty base, the original target will be
8266 : *(base*)this, evaluation of which resolves to the object
8267 : argument, which has the derived type rather than the base type. */
8268 100602945 : if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p
8269 50241293 : (itype, type)))
8270 : {
8271 9799 : gcc_assert (is_empty_class (TREE_TYPE (target)));
8272 : empty_base = true;
8273 : }
8274 :
8275 : /* Detect modifying a constant object in constexpr evaluation.
8276 : We have found a const object that is being modified. Figure out
8277 : if we need to issue an error. Consider
8278 :
8279 : struct A {
8280 : int n;
8281 : constexpr A() : n(1) { n = 2; } // #1
8282 : };
8283 : struct B {
8284 : const A a;
8285 : constexpr B() { a.n = 3; } // #2
8286 : };
8287 : constexpr B b{};
8288 :
8289 : #1 is OK, since we're modifying an object under construction, but
8290 : #2 is wrong, since "a" is const and has been fully constructed.
8291 : To track it, we use the TREE_READONLY bit in the object's CONSTRUCTOR
8292 : which means that the object is read-only. For the example above, the
8293 : *ctors stack at the point of #2 will look like:
8294 :
8295 : ctors[0] = {.a={.n=2}} TREE_READONLY = 0
8296 : ctors[1] = {.n=2} TREE_READONLY = 1
8297 :
8298 : and we're modifying "b.a", so we search the stack and see if the
8299 : constructor for "b.a" has already run. */
8300 50361652 : if (const_object_being_modified)
8301 : {
8302 33289 : bool fail = false;
8303 33289 : tree const_objtype
8304 33289 : = strip_array_types (TREE_TYPE (const_object_being_modified));
8305 33289 : if (!CLASS_TYPE_P (const_objtype))
8306 : fail = true;
8307 : else
8308 : {
8309 : /* [class.ctor]p5 "A constructor can be invoked for a const,
8310 : volatile, or const volatile object. const and volatile
8311 : semantics are not applied on an object under construction.
8312 : They come into effect when the constructor for the most
8313 : derived object ends." */
8314 99964 : for (tree *elt : ctors)
8315 33596 : if (same_type_ignoring_top_level_qualifiers_p
8316 33596 : (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
8317 : {
8318 33184 : fail = TREE_READONLY (*elt);
8319 33184 : break;
8320 : }
8321 : }
8322 33184 : if (fail)
8323 : {
8324 198 : if (!ctx->quiet)
8325 63 : modifying_const_object_error (t, const_object_being_modified);
8326 198 : *non_constant_p = true;
8327 198 : return t;
8328 : }
8329 : }
8330 :
8331 50361454 : if (!preeval)
8332 : {
8333 : /* We're handling an INIT_EXPR of class type, so the value of the
8334 : initializer can depend on the object it's initializing. */
8335 :
8336 : /* Create a new CONSTRUCTOR in case evaluation of the initializer
8337 : wants to modify it. */
8338 14330532 : if (*valp == NULL_TREE)
8339 : {
8340 13859699 : *valp = build_constructor (type, NULL);
8341 13859699 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8342 13859699 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8343 : }
8344 14330532 : new_ctx.ctor = empty_base ? NULL_TREE : *valp;
8345 14330532 : new_ctx.object = target;
8346 : /* Avoid temporary materialization when initializing from a TARGET_EXPR.
8347 : We don't need to mess with AGGR_EXPR_SLOT/VEC_INIT_EXPR_SLOT because
8348 : expansion of those trees uses ctx instead. */
8349 14330532 : if (TREE_CODE (init) == TARGET_EXPR)
8350 2178198 : if (tree tinit = TARGET_EXPR_INITIAL (init))
8351 2178198 : init = tinit;
8352 14330532 : init = cxx_eval_constant_expression (&new_ctx, init, vc_prvalue,
8353 : non_constant_p, overflow_p,
8354 : jump_target);
8355 14330532 : if (*jump_target)
8356 : return NULL_TREE;
8357 : /* The hash table might have moved since the get earlier, and the
8358 : initializer might have mutated the underlying CONSTRUCTORs, so we must
8359 : recompute VALP. */
8360 14330468 : valp = ctx->global->get_value_ptr (object, TREE_CODE (t) == INIT_EXPR);
8361 16504325 : for (unsigned i = 0; i < vec_safe_length (indexes); i++)
8362 : {
8363 2173857 : ctors[i] = valp;
8364 2173857 : constructor_elt *cep
8365 2173857 : = get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
8366 2173857 : valp = &cep->value;
8367 : }
8368 : }
8369 :
8370 50361390 : if (*non_constant_p)
8371 : return t;
8372 :
8373 : /* Don't share a CONSTRUCTOR that might be changed later. */
8374 48762601 : init = unshare_constructor (init);
8375 :
8376 48762601 : gcc_checking_assert (!*valp
8377 : || *valp == void_node
8378 : || (same_type_ignoring_top_level_qualifiers_p
8379 : (TREE_TYPE (*valp), type)));
8380 48762601 : if (empty_base)
8381 : {
8382 : /* Just evaluate the initializer and return, since there's no actual data
8383 : to store, and we didn't build a CONSTRUCTOR. */
8384 124978 : if (!*valp)
8385 : {
8386 : /* But do make sure we have something in *valp. */
8387 0 : *valp = build_constructor (type, nullptr);
8388 0 : CONSTRUCTOR_NO_CLEARING (*valp) = no_zero_init;
8389 0 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8390 : }
8391 : }
8392 48637623 : else if (TREE_CLOBBER_P (init))
8393 : {
8394 377562 : if (AGGREGATE_TYPE_P (type))
8395 : {
8396 265174 : if (*valp && TREE_CODE (*valp) == CONSTRUCTOR)
8397 265151 : CONSTRUCTOR_ELTS (*valp) = nullptr;
8398 : else
8399 23 : *valp = build_constructor (type, nullptr);
8400 265174 : TREE_CONSTANT (*valp) = true;
8401 265174 : TREE_SIDE_EFFECTS (*valp) = false;
8402 265174 : CONSTRUCTOR_NO_CLEARING (*valp) = true;
8403 265174 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp) = zero_padding_bits;
8404 : }
8405 : else
8406 112388 : *valp = void_node;
8407 : }
8408 48260061 : else if (*valp && TREE_CODE (*valp) == CONSTRUCTOR
8409 12737845 : && TREE_CODE (init) == CONSTRUCTOR)
8410 : {
8411 : /* An outer ctx->ctor might be pointing to *valp, so replace
8412 : its contents. */
8413 2691085 : CONSTRUCTOR_ELTS (*valp) = CONSTRUCTOR_ELTS (init);
8414 2691085 : TREE_CONSTANT (*valp) = TREE_CONSTANT (init);
8415 2691085 : TREE_SIDE_EFFECTS (*valp) = TREE_SIDE_EFFECTS (init);
8416 8073255 : CONSTRUCTOR_NO_CLEARING (*valp)
8417 2691085 : = CONSTRUCTOR_NO_CLEARING (init);
8418 8073255 : CONSTRUCTOR_ZERO_PADDING_BITS (*valp)
8419 2691085 : = CONSTRUCTOR_ZERO_PADDING_BITS (init);
8420 : }
8421 : else
8422 45568976 : *valp = init;
8423 :
8424 : /* After initialization, 'const' semantics apply to the value of the
8425 : object. Make a note of this fact by marking the CONSTRUCTOR
8426 : TREE_READONLY. */
8427 48762601 : if (TREE_CODE (t) == INIT_EXPR
8428 35511949 : && !empty_base
8429 35386971 : && TREE_CODE (*valp) == CONSTRUCTOR
8430 51410205 : && TYPE_READONLY (type))
8431 : {
8432 20806 : tree target_type = TREE_TYPE (target);
8433 20806 : if (IS_FAKE_BASE_TYPE (target_type))
8434 0 : target_type = TYPE_CONTEXT (target_type);
8435 20806 : if (INDIRECT_REF_P (target)
8436 35869 : && (is_this_parameter
8437 15063 : (tree_strip_nop_conversions (TREE_OPERAND (target, 0)))))
8438 : /* We've just initialized '*this' (perhaps via the target
8439 : constructor of a delegating constructor). Leave it up to the
8440 : caller that set 'this' to set TREE_READONLY appropriately. */
8441 23 : gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
8442 : (target_type, type) || empty_base);
8443 : else
8444 20783 : TREE_READONLY (*valp) = true;
8445 : }
8446 :
8447 : /* Update TREE_CONSTANT and TREE_SIDE_EFFECTS on enclosing
8448 : CONSTRUCTORs, if any. */
8449 48762601 : bool c = TREE_CONSTANT (init);
8450 48762601 : bool s = TREE_SIDE_EFFECTS (init);
8451 48762601 : if (!indexes->is_empty ())
8452 : {
8453 12661197 : tree last = indexes->last ();
8454 12661197 : if (TREE_CODE (last) == REALPART_EXPR
8455 12661197 : || TREE_CODE (last) == IMAGPART_EXPR)
8456 : {
8457 : /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
8458 : possible. */
8459 33 : tree *cexpr = ctors.last ();
8460 33 : if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
8461 33 : TREE_OPERAND (*cexpr, 0),
8462 33 : TREE_OPERAND (*cexpr, 1)))
8463 31 : *cexpr = c;
8464 : else
8465 : {
8466 4 : TREE_CONSTANT (*cexpr)
8467 2 : = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
8468 2 : & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
8469 4 : TREE_SIDE_EFFECTS (*cexpr)
8470 4 : = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
8471 2 : | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
8472 : }
8473 33 : c = TREE_CONSTANT (*cexpr);
8474 33 : s = TREE_SIDE_EFFECTS (*cexpr);
8475 : }
8476 : }
8477 48762601 : if (!c || s || activated_union_member_p)
8478 30705236 : for (tree *elt : ctors)
8479 : {
8480 5784597 : if (TREE_CODE (*elt) != CONSTRUCTOR)
8481 0 : continue;
8482 5784597 : if (!c)
8483 4935681 : TREE_CONSTANT (*elt) = false;
8484 5784597 : if (s)
8485 0 : TREE_SIDE_EFFECTS (*elt) = true;
8486 : /* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
8487 : this union. */
8488 5784597 : if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
8489 204953 : CONSTRUCTOR_NO_CLEARING (*elt) = false;
8490 : }
8491 :
8492 48762601 : if (lval)
8493 : return target;
8494 : else
8495 274027 : return init;
8496 50489268 : }
8497 :
8498 : /* Evaluate a ++ or -- expression. */
8499 :
8500 : static tree
8501 4961017 : cxx_eval_increment_expression (const constexpr_ctx *ctx, tree t,
8502 : value_cat lval,
8503 : bool *non_constant_p, bool *overflow_p,
8504 : tree *jump_target)
8505 : {
8506 4961017 : enum tree_code code = TREE_CODE (t);
8507 4961017 : tree type = TREE_TYPE (t);
8508 4961017 : tree op = TREE_OPERAND (t, 0);
8509 4961017 : tree offset = TREE_OPERAND (t, 1);
8510 4961017 : gcc_assert (TREE_CONSTANT (offset));
8511 :
8512 : /* OFFSET is constant, but perhaps not constant enough. We need to
8513 : e.g. bash FLOAT_EXPRs to REAL_CSTs. */
8514 4961017 : offset = fold_simple (offset);
8515 :
8516 : /* The operand as an lvalue. */
8517 4961017 : op = cxx_eval_constant_expression (ctx, op, vc_glvalue,
8518 : non_constant_p, overflow_p,
8519 : jump_target);
8520 4961017 : if (*jump_target)
8521 : return NULL_TREE;
8522 :
8523 : /* The operand as an rvalue. */
8524 4961017 : tree val
8525 4961017 : = cxx_eval_constant_expression (ctx, op, vc_prvalue,
8526 : non_constant_p, overflow_p,
8527 : jump_target);
8528 4961017 : if (*jump_target)
8529 : return NULL_TREE;
8530 : /* Don't VERIFY_CONSTANT if this might be dealing with a pointer to
8531 : a local array in a constexpr function. */
8532 4961017 : bool ptr = INDIRECT_TYPE_P (TREE_TYPE (val));
8533 1910932 : if (!ptr)
8534 1910932 : VERIFY_CONSTANT (val);
8535 :
8536 : /* The modified value. */
8537 4909026 : bool inc = (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR);
8538 4909026 : tree mod;
8539 4909026 : if (INDIRECT_TYPE_P (type))
8540 : {
8541 : /* The middle end requires pointers to use POINTER_PLUS_EXPR. */
8542 3050085 : offset = convert_to_ptrofftype (offset);
8543 3050085 : if (!inc)
8544 52532 : offset = fold_build1 (NEGATE_EXPR, TREE_TYPE (offset), offset);
8545 3050085 : mod = fold_build2 (POINTER_PLUS_EXPR, type, val, offset);
8546 : }
8547 1858941 : else if (c_promoting_integer_type_p (type)
8548 5085 : && !TYPE_UNSIGNED (type)
8549 1859065 : && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
8550 : {
8551 124 : offset = fold_convert (integer_type_node, offset);
8552 124 : mod = fold_convert (integer_type_node, val);
8553 153 : tree t = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, integer_type_node,
8554 : mod, offset);
8555 124 : mod = fold_convert (type, t);
8556 124 : if (TREE_OVERFLOW_P (mod) && !TREE_OVERFLOW_P (t))
8557 9 : TREE_OVERFLOW (mod) = false;
8558 : }
8559 : else
8560 2102414 : mod = fold_build2 (inc ? PLUS_EXPR : MINUS_EXPR, type, val, offset);
8561 4909026 : if (!ptr)
8562 1858941 : VERIFY_CONSTANT (mod);
8563 :
8564 : /* Storing the modified value. */
8565 7196394 : tree store = build2_loc (cp_expr_loc_or_loc (t, input_location),
8566 : MODIFY_EXPR, type, op, mod);
8567 4909026 : mod = cxx_eval_constant_expression (ctx, store, lval,
8568 : non_constant_p, overflow_p,
8569 : jump_target);
8570 4909026 : ggc_free (store);
8571 4909026 : if (*jump_target)
8572 : return NULL_TREE;
8573 4909026 : if (*non_constant_p)
8574 : return t;
8575 :
8576 : /* And the value of the expression. */
8577 4847326 : if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
8578 : /* Prefix ops are lvalues, but the caller might want an rvalue;
8579 : lval has already been taken into account in the store above. */
8580 : return mod;
8581 : else
8582 : /* Postfix ops are rvalues. */
8583 275630 : return val;
8584 : }
8585 :
8586 : /* Subroutine of cxx_eval_statement_list. Determine whether the statement
8587 : STMT matches *jump_target. If we're looking for a case label and we see
8588 : the default label, note it in ctx->css_state. */
8589 :
8590 : static bool
8591 308510 : label_matches (const constexpr_ctx *ctx, tree *jump_target, tree stmt)
8592 : {
8593 308510 : switch (TREE_CODE (*jump_target))
8594 : {
8595 45 : case LABEL_DECL:
8596 45 : if (TREE_CODE (stmt) == LABEL_EXPR
8597 45 : && LABEL_EXPR_LABEL (stmt) == *jump_target)
8598 : return true;
8599 : break;
8600 :
8601 306548 : case INTEGER_CST:
8602 306548 : if (TREE_CODE (stmt) == CASE_LABEL_EXPR)
8603 : {
8604 306532 : gcc_assert (ctx->css_state != NULL);
8605 306532 : if (!CASE_LOW (stmt))
8606 : {
8607 : /* default: should appear just once in a SWITCH_EXPR
8608 : body (excluding nested SWITCH_EXPR). */
8609 50432 : gcc_assert (*ctx->css_state != css_default_seen);
8610 : /* When evaluating SWITCH_EXPR body for the second time,
8611 : return true for the default: label. */
8612 50432 : if (*ctx->css_state == css_default_processing)
8613 : return true;
8614 25228 : *ctx->css_state = css_default_seen;
8615 : }
8616 256100 : else if (CASE_HIGH (stmt))
8617 : {
8618 20 : if (tree_int_cst_le (CASE_LOW (stmt), *jump_target)
8619 31 : && tree_int_cst_le (*jump_target, CASE_HIGH (stmt)))
8620 : return true;
8621 : }
8622 256080 : else if (tree_int_cst_equal (*jump_target, CASE_LOW (stmt)))
8623 : return true;
8624 : }
8625 : break;
8626 :
8627 : case BREAK_STMT:
8628 : case CONTINUE_STMT:
8629 : /* These two are handled directly in cxx_eval_loop_expr by testing
8630 : breaks (jump_target) or continues (jump_target). */
8631 : break;
8632 :
8633 : case VAR_DECL:
8634 : /* Uncaught exception. This is handled by TRY_BLOCK evaluation
8635 : and other places by testing throws (jump_target). */
8636 : break;
8637 :
8638 0 : default:
8639 0 : gcc_unreachable ();
8640 : }
8641 : return false;
8642 : }
8643 :
8644 : /* Evaluate a STATEMENT_LIST for side-effects. Handles various jump
8645 : semantics, for switch, break, continue, and return. */
8646 :
8647 : static tree
8648 30801256 : cxx_eval_statement_list (const constexpr_ctx *ctx, tree t,
8649 : bool *non_constant_p, bool *overflow_p,
8650 : tree *jump_target)
8651 : {
8652 : /* In a statement-expression we want to return the last value.
8653 : For empty statement expression return void_node. */
8654 30801256 : tree r = void_node;
8655 74016929 : for (tree_stmt_iterator i = tsi_start (t); !tsi_end_p (i); ++i)
8656 : {
8657 59845701 : tree stmt = *i;
8658 :
8659 : /* We've found a continue, so skip everything until we reach
8660 : the label its jumping to. */
8661 59845701 : if (continues (jump_target))
8662 : {
8663 1962 : if (label_matches (ctx, jump_target, stmt))
8664 : /* Found it. */
8665 9 : *jump_target = NULL_TREE;
8666 : else
8667 1953 : continue;
8668 : }
8669 59843748 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8670 8477996 : continue;
8671 :
8672 51365752 : value_cat lval = vc_discard;
8673 : /* The result of a statement-expression is not wrapped in EXPR_STMT. */
8674 74198379 : if (tsi_one_before_end_p (i)
8675 22833836 : && !VOID_TYPE_P (TREE_TYPE (stmt)))
8676 : lval = vc_prvalue;
8677 :
8678 51365752 : r = cxx_eval_constant_expression (ctx, stmt, lval,
8679 : non_constant_p, overflow_p,
8680 : jump_target);
8681 51365750 : if (*non_constant_p)
8682 : break;
8683 43086621 : if (returns (jump_target)
8684 34757249 : || breaks (jump_target)
8685 43215673 : || throws (jump_target))
8686 : break;
8687 : }
8688 30801254 : return r;
8689 : }
8690 :
8691 : /* Evaluate a LOOP_EXPR for side-effects. Handles break and return
8692 : semantics; continue semantics are covered by cxx_eval_statement_list. */
8693 :
8694 : static tree
8695 3296727 : cxx_eval_loop_expr (const constexpr_ctx *ctx, tree t,
8696 : bool *non_constant_p, bool *overflow_p,
8697 : tree *jump_target)
8698 : {
8699 3296727 : tree body, cond = NULL_TREE, expr = NULL_TREE;
8700 3296727 : tree cond_prep = NULL_TREE, cond_cleanup = NULL_TREE;
8701 3296727 : unsigned cond_cleanup_depth = 0;
8702 3296727 : int count = 0;
8703 3296727 : switch (TREE_CODE (t))
8704 : {
8705 1618 : case LOOP_EXPR:
8706 1618 : body = LOOP_EXPR_BODY (t);
8707 1618 : break;
8708 2666537 : case DO_STMT:
8709 2666537 : body = DO_BODY (t);
8710 2666537 : cond = DO_COND (t);
8711 2666537 : break;
8712 130933 : case WHILE_STMT:
8713 130933 : body = WHILE_BODY (t);
8714 130933 : cond = WHILE_COND (t);
8715 130933 : cond_prep = WHILE_COND_PREP (t);
8716 130933 : cond_cleanup = WHILE_COND_CLEANUP (t);
8717 130933 : count = -1;
8718 130933 : break;
8719 497639 : case FOR_STMT:
8720 497639 : if (FOR_INIT_STMT (t))
8721 0 : cxx_eval_constant_expression (ctx, FOR_INIT_STMT (t), vc_discard,
8722 : non_constant_p, overflow_p, jump_target);
8723 497639 : if (*non_constant_p)
8724 : return NULL_TREE;
8725 497639 : body = FOR_BODY (t);
8726 497639 : cond = FOR_COND (t);
8727 497639 : expr = FOR_EXPR (t);
8728 497639 : cond_prep = FOR_COND_PREP (t);
8729 497639 : cond_cleanup = FOR_COND_CLEANUP (t);
8730 497639 : count = -1;
8731 497639 : break;
8732 0 : default:
8733 0 : gcc_unreachable ();
8734 : }
8735 3296727 : if (cond_prep)
8736 12 : gcc_assert (TREE_CODE (cond_prep) == BIND_EXPR);
8737 18323319 : auto cleanup_cond = [&] {
8738 : /* Clean up the condition variable after each iteration. */
8739 15026592 : if (cond_cleanup_depth && !*non_constant_p)
8740 : {
8741 105 : auto_vec<tree, 4> cleanups (cond_cleanup_depth);
8742 105 : tree s = BIND_EXPR_BODY (cond_prep);
8743 105 : unsigned i;
8744 210 : for (i = cond_cleanup_depth; i; --i)
8745 : {
8746 105 : tree_stmt_iterator iter = tsi_last (s);
8747 105 : s = tsi_stmt (iter);
8748 105 : cleanups.quick_push (CLEANUP_EXPR (s));
8749 105 : s = CLEANUP_BODY (s);
8750 : }
8751 105 : tree c;
8752 420 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, c)
8753 105 : cxx_eval_constant_expression (ctx, c, vc_discard, non_constant_p,
8754 : overflow_p, jump_target);
8755 105 : }
8756 15026592 : if (cond_prep)
8757 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8758 222 : decl; decl = DECL_CHAIN (decl))
8759 111 : destroy_value_checked (ctx, decl, non_constant_p);
8760 3296727 : };
8761 12360473 : do
8762 : {
8763 12360473 : if (count != -1)
8764 : {
8765 11731901 : if (body)
8766 11731901 : cxx_eval_constant_expression (ctx, body, vc_discard,
8767 : non_constant_p, overflow_p,
8768 : jump_target);
8769 11731901 : if (breaks (jump_target))
8770 : {
8771 2036 : *jump_target = NULL_TREE;
8772 2036 : break;
8773 : }
8774 :
8775 11729865 : if (TREE_CODE (t) != LOOP_EXPR && continues (jump_target))
8776 570 : *jump_target = NULL_TREE;
8777 :
8778 11729865 : if (expr)
8779 3948377 : cxx_eval_constant_expression (ctx, expr, vc_discard,
8780 : non_constant_p, overflow_p,
8781 : jump_target);
8782 11729865 : cleanup_cond ();
8783 : }
8784 :
8785 12358437 : if (cond_prep)
8786 : {
8787 111 : for (tree decl = BIND_EXPR_VARS (cond_prep);
8788 222 : decl; decl = DECL_CHAIN (decl))
8789 111 : ctx->global->clear_value (decl);
8790 111 : if (cond_cleanup)
8791 : {
8792 : /* If COND_CLEANUP is non-NULL, we need to evaluate DEPTH
8793 : nested STATEMENT_LISTs from inside of BIND_EXPR_BODY,
8794 : but defer the evaluation of CLEANUP_EXPRs of CLEANUP_STMT
8795 : at the end of those STATEMENT_LISTs. */
8796 111 : cond_cleanup_depth = 0;
8797 111 : tree s = BIND_EXPR_BODY (cond_prep);
8798 111 : for (unsigned depth = tree_to_uhwi (cond_cleanup);
8799 222 : depth; --depth)
8800 : {
8801 111 : for (tree_stmt_iterator i = tsi_start (s);
8802 321 : !tsi_end_p (i); ++i)
8803 : {
8804 321 : tree stmt = *i;
8805 321 : if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT)
8806 0 : continue;
8807 321 : if (tsi_one_before_end_p (i))
8808 : {
8809 : /* The last statement in the STATEMENT_LIST
8810 : has to be a CLEANUP_STMT (verified in
8811 : finish_loop_cond_prep). We want to
8812 : evaluate just its CLEANUP_BODY part but not
8813 : CLEANUP_EXPR part just yet. */
8814 105 : gcc_assert (TREE_CODE (stmt) == CLEANUP_STMT);
8815 : /* If the CLEANUP_STMT is not actually to be
8816 : evaluated, don't increment cond_cleanup_depth
8817 : so that we don't evaluate the CLEANUP_EXPR
8818 : for it later either. */
8819 105 : if (*jump_target)
8820 : {
8821 : depth = 1;
8822 : break;
8823 : }
8824 105 : ++cond_cleanup_depth;
8825 : /* If not in the innermost one, next iteration
8826 : will handle CLEANUP_BODY similarly. */
8827 105 : if (depth > 1)
8828 : {
8829 0 : s = CLEANUP_BODY (stmt);
8830 0 : break;
8831 : }
8832 : /* The innermost one can be evaluated normally. */
8833 105 : cxx_eval_constant_expression (ctx,
8834 105 : CLEANUP_BODY (stmt),
8835 : vc_discard,
8836 : non_constant_p,
8837 : overflow_p,
8838 : jump_target);
8839 105 : break;
8840 : }
8841 : /* And so should be evaluated statements which aren't
8842 : last in the STATEMENT_LIST. */
8843 216 : cxx_eval_constant_expression (ctx, stmt, vc_discard,
8844 : non_constant_p, overflow_p,
8845 : jump_target);
8846 216 : if (*non_constant_p
8847 210 : || returns (jump_target)
8848 210 : || breaks (jump_target)
8849 210 : || continues (jump_target)
8850 531 : || throws (jump_target))
8851 : {
8852 : depth = 1;
8853 : break;
8854 : }
8855 : }
8856 : }
8857 : }
8858 : else
8859 0 : cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (cond_prep),
8860 : vc_discard, non_constant_p,
8861 : overflow_p, jump_target);
8862 : }
8863 :
8864 12358437 : if (cond)
8865 : {
8866 12356048 : tree res
8867 12356048 : = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8868 : non_constant_p, overflow_p,
8869 : jump_target);
8870 12356048 : if (res)
8871 : {
8872 12312332 : if (verify_constant (res, ctx->quiet, non_constant_p,
8873 : overflow_p))
8874 : break;
8875 12186449 : if (integer_zerop (res))
8876 : break;
8877 : }
8878 : else
8879 43716 : gcc_assert (*jump_target);
8880 : }
8881 :
8882 9107706 : if (++count >= constexpr_loop_limit)
8883 : {
8884 27 : if (!ctx->quiet)
8885 9 : error_at (cp_expr_loc_or_input_loc (t),
8886 : "%<constexpr%> loop iteration count exceeds limit of %d "
8887 : "(use %<-fconstexpr-loop-limit=%> to increase the limit)",
8888 : constexpr_loop_limit);
8889 27 : *non_constant_p = true;
8890 27 : break;
8891 : }
8892 : }
8893 27279292 : while (!returns (jump_target)
8894 9063934 : && !breaks (jump_target)
8895 9063934 : && !continues (jump_target)
8896 9064027 : && (!switches (jump_target) || count == 0)
8897 9063904 : && !throws (jump_target)
8898 18215509 : && !*non_constant_p);
8899 :
8900 3296727 : cleanup_cond ();
8901 :
8902 3296727 : return NULL_TREE;
8903 : }
8904 :
8905 : /* Evaluate a SWITCH_EXPR for side-effects. Handles switch and break jump
8906 : semantics. */
8907 :
8908 : static tree
8909 30977 : cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
8910 : bool *non_constant_p, bool *overflow_p,
8911 : tree *jump_target)
8912 : {
8913 30977 : tree cond
8914 30977 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_COND (t) : SWITCH_COND (t);
8915 30977 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
8916 : non_constant_p, overflow_p,
8917 : jump_target);
8918 30977 : if (*jump_target)
8919 : return NULL_TREE;
8920 30977 : VERIFY_CONSTANT (cond);
8921 30734 : if (TREE_CODE (cond) != INTEGER_CST)
8922 : {
8923 : /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
8924 : switch condition even if it's constant enough for other things
8925 : (c++/113545). */
8926 0 : gcc_checking_assert (ctx->quiet);
8927 0 : *non_constant_p = true;
8928 0 : return t;
8929 : }
8930 :
8931 30734 : *jump_target = cond;
8932 :
8933 30734 : tree body
8934 30734 : = TREE_CODE (t) == SWITCH_STMT ? SWITCH_STMT_BODY (t) : SWITCH_BODY (t);
8935 30734 : constexpr_ctx new_ctx = *ctx;
8936 30734 : constexpr_switch_state css = css_default_not_seen;
8937 30734 : new_ctx.css_state = &css;
8938 30734 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8939 : non_constant_p, overflow_p, jump_target);
8940 55972 : if (switches (jump_target) && css == css_default_seen)
8941 : {
8942 : /* If the SWITCH_EXPR body has default: label, process it once again,
8943 : this time instructing label_matches to return true for default:
8944 : label on switches (jump_target). */
8945 25204 : css = css_default_processing;
8946 25204 : cxx_eval_constant_expression (&new_ctx, body, vc_discard,
8947 : non_constant_p, overflow_p, jump_target);
8948 : }
8949 30734 : if (breaks (jump_target) || switches (jump_target))
8950 19596 : *jump_target = NULL_TREE;
8951 : return NULL_TREE;
8952 : }
8953 :
8954 : /* Find the object of TYPE under initialization in CTX. */
8955 :
8956 : static tree
8957 16621 : lookup_placeholder (const constexpr_ctx *ctx, value_cat lval, tree type)
8958 : {
8959 16621 : if (!ctx)
8960 : return NULL_TREE;
8961 :
8962 : /* Prefer the outermost matching object, but don't cross
8963 : CONSTRUCTOR_PLACEHOLDER_BOUNDARY constructors. */
8964 16294 : if (ctx->ctor && !CONSTRUCTOR_PLACEHOLDER_BOUNDARY (ctx->ctor))
8965 505 : if (tree outer_ob = lookup_placeholder (ctx->parent, lval, type))
8966 : return outer_ob;
8967 :
8968 : /* We could use ctx->object unconditionally, but using ctx->ctor when we
8969 : can is a minor optimization. */
8970 16116 : if (!lval && ctx->ctor && same_type_p (TREE_TYPE (ctx->ctor), type))
8971 300 : return ctx->ctor;
8972 :
8973 15816 : if (!ctx->object)
8974 : return NULL_TREE;
8975 :
8976 : /* Since an object cannot have a field of its own type, we can search outward
8977 : from ctx->object to find the unique containing object of TYPE. */
8978 : tree ob = ctx->object;
8979 15804 : while (ob)
8980 : {
8981 15804 : if (same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (ob), type))
8982 : break;
8983 219 : if (handled_component_p (ob))
8984 217 : ob = TREE_OPERAND (ob, 0);
8985 : else
8986 : ob = NULL_TREE;
8987 : }
8988 :
8989 : return ob;
8990 : }
8991 :
8992 : /* Complain about an attempt to evaluate inline assembly. If FUNDEF_P is
8993 : true, we're checking a constexpr function body. */
8994 :
8995 : static void
8996 34 : inline_asm_in_constexpr_error (location_t loc, bool fundef_p)
8997 : {
8998 34 : auto_diagnostic_group d;
8999 34 : if (constexpr_error (loc, fundef_p, "inline assembly is not a "
9000 : "constant expression"))
9001 34 : inform (loc, "only unevaluated inline assembly is allowed in a "
9002 : "%<constexpr%> function in C++20");
9003 34 : }
9004 :
9005 : /* We're getting the constant value of DECL in a manifestly constant-evaluated
9006 : context; maybe complain about that. */
9007 :
9008 : static void
9009 85013515 : maybe_warn_about_constant_value (location_t loc, tree decl)
9010 : {
9011 85013515 : static bool explained = false;
9012 85013515 : if (cxx_dialect >= cxx17
9013 84847993 : && warn_interference_size
9014 84847993 : && !OPTION_SET_P (param_destruct_interfere_size)
9015 84847993 : && DECL_CONTEXT (decl) == std_node
9016 17616554 : && DECL_NAME (decl)
9017 17616548 : && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
9018 16 : && (LOCATION_FILE (input_location) != main_input_filename
9019 10 : || module_exporting_p ())
9020 85013518 : && warning_at (loc, OPT_Winterference_size, "use of %qD", decl)
9021 85013524 : && !explained)
9022 : {
9023 6 : explained = true;
9024 6 : inform (loc, "its value can vary between compiler versions or "
9025 : "with different %<-mtune%> or %<-mcpu%> flags");
9026 6 : inform (loc, "if this use is part of a public ABI, change it to "
9027 : "instead use a constant variable you define");
9028 6 : inform (loc, "the default value for the current CPU tuning "
9029 : "is %d bytes", param_destruct_interfere_size);
9030 6 : inform (loc, "you can stabilize this value with %<--param "
9031 : "hardware_destructive_interference_size=%d%>, or disable "
9032 : "this warning with %<-Wno-interference-size%>",
9033 : param_destruct_interfere_size);
9034 : }
9035 85013515 : }
9036 :
9037 : /* For element type ELT_TYPE, return the appropriate type of the heap object
9038 : containing such element(s). COOKIE_SIZE is NULL or the size of cookie
9039 : in bytes. If COOKIE_SIZE is NULL, return array type
9040 : ELT_TYPE[FULL_SIZE / sizeof(ELT_TYPE)], otherwise return
9041 : struct { size_t[COOKIE_SIZE/sizeof(size_t)]; ELT_TYPE[N]; }
9042 : where N is computed such that the size of the struct fits into FULL_SIZE.
9043 : If ARG_SIZE is non-NULL, it is the first argument to the new operator.
9044 : It should be passed if ELT_TYPE is zero sized type in which case FULL_SIZE
9045 : will be also 0 and so it is not possible to determine the actual array
9046 : size. CTX, NON_CONSTANT_P and OVERFLOW_P are used during constant
9047 : expression evaluation of subexpressions of ARG_SIZE. */
9048 :
9049 : static tree
9050 63586 : build_new_constexpr_heap_type (const constexpr_ctx *ctx, tree elt_type,
9051 : tree cookie_size, tree full_size, tree arg_size,
9052 : bool *non_constant_p, bool *overflow_p,
9053 : tree *jump_target)
9054 : {
9055 63586 : gcc_assert (cookie_size == NULL_TREE || tree_fits_uhwi_p (cookie_size));
9056 63586 : gcc_assert (tree_fits_uhwi_p (full_size));
9057 63586 : unsigned HOST_WIDE_INT csz = cookie_size ? tree_to_uhwi (cookie_size) : 0;
9058 63586 : if (arg_size)
9059 : {
9060 9 : STRIP_NOPS (arg_size);
9061 9 : if (cookie_size)
9062 : {
9063 0 : if (TREE_CODE (arg_size) != PLUS_EXPR)
9064 : arg_size = NULL_TREE;
9065 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 0)) == INTEGER_CST
9066 0 : && tree_int_cst_equal (cookie_size,
9067 0 : TREE_OPERAND (arg_size, 0)))
9068 : {
9069 0 : arg_size = TREE_OPERAND (arg_size, 1);
9070 0 : STRIP_NOPS (arg_size);
9071 : }
9072 0 : else if (TREE_CODE (TREE_OPERAND (arg_size, 1)) == INTEGER_CST
9073 0 : && tree_int_cst_equal (cookie_size,
9074 0 : TREE_OPERAND (arg_size, 1)))
9075 : {
9076 0 : arg_size = TREE_OPERAND (arg_size, 0);
9077 0 : STRIP_NOPS (arg_size);
9078 : }
9079 : else
9080 : arg_size = NULL_TREE;
9081 : }
9082 9 : if (arg_size && TREE_CODE (arg_size) == MULT_EXPR)
9083 : {
9084 9 : tree op0 = TREE_OPERAND (arg_size, 0);
9085 9 : tree op1 = TREE_OPERAND (arg_size, 1);
9086 9 : if (integer_zerop (op0))
9087 9 : arg_size
9088 9 : = cxx_eval_constant_expression (ctx, op1, vc_prvalue,
9089 : non_constant_p, overflow_p,
9090 : jump_target);
9091 0 : else if (integer_zerop (op1))
9092 0 : arg_size
9093 0 : = cxx_eval_constant_expression (ctx, op0, vc_prvalue,
9094 : non_constant_p, overflow_p,
9095 : jump_target);
9096 : else
9097 : arg_size = NULL_TREE;
9098 9 : if (*jump_target)
9099 : return NULL_TREE;
9100 : }
9101 : else
9102 : arg_size = NULL_TREE;
9103 : }
9104 :
9105 9 : unsigned HOST_WIDE_INT fsz = tree_to_uhwi (arg_size ? arg_size : full_size);
9106 63586 : if (!arg_size)
9107 : {
9108 63577 : unsigned HOST_WIDE_INT esz = int_size_in_bytes (elt_type);
9109 63577 : gcc_assert (fsz >= csz);
9110 63577 : fsz -= csz;
9111 63577 : if (esz)
9112 63577 : fsz /= esz;
9113 : }
9114 63586 : tree itype2 = build_index_type (size_int (fsz - 1));
9115 63586 : if (!cookie_size)
9116 63577 : return build_cplus_array_type (elt_type, itype2);
9117 9 : return build_new_constexpr_heap_type (elt_type, cookie_size, itype2);
9118 : }
9119 :
9120 : /* Handle the case when a cleanup of some expression throws. JMP_TARGET
9121 : indicates whether the cleanup threw or not, *JUMP_TARGET indicates whether
9122 : the expression which needed the cleanup threw. If both threw, diagnose
9123 : it and return NULL, otherwise return R. If only the cleanup threw, set
9124 : *JUMP_TARGET to the exception object from the cleanup. */
9125 :
9126 : static tree
9127 798968 : merge_jump_target (location_t loc, const constexpr_ctx *ctx, tree r,
9128 : bool *non_constant_p, tree *jump_target, tree jmp_target)
9129 : {
9130 798969 : if (!throws (&jmp_target))
9131 : return r;
9132 7 : if (throws (jump_target))
9133 : {
9134 : /* [except.throw]/9 - If the exception handling mechanism
9135 : handling an uncaught exception directly invokes a function
9136 : that exits via an exception, the function std::terminate is
9137 : invoked. */
9138 6 : if (!ctx->quiet)
9139 : {
9140 2 : auto_diagnostic_group d;
9141 2 : diagnose_std_terminate (loc, ctx, *jump_target);
9142 2 : inform (loc, "destructor exited with an exception");
9143 2 : }
9144 6 : *non_constant_p = true;
9145 6 : *jump_target = NULL_TREE;
9146 6 : return NULL_TREE;
9147 : }
9148 1 : *jump_target = jmp_target;
9149 1 : return r;
9150 : }
9151 :
9152 : /* Attempt to reduce the expression T to a constant value.
9153 : On failure, issue diagnostic and return error_mark_node. */
9154 : /* FIXME unify with c_fully_fold */
9155 : /* FIXME overflow_p is too global */
9156 :
9157 : tree
9158 1957769157 : cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
9159 : value_cat lval,
9160 : bool *non_constant_p, bool *overflow_p,
9161 : tree *jump_target)
9162 : {
9163 1957769157 : if (*jump_target)
9164 : {
9165 : /* If we are jumping, ignore all statements/expressions except those
9166 : that could have LABEL_EXPR or CASE_LABEL_EXPR in their bodies. */
9167 1226265 : switch (TREE_CODE (t))
9168 : {
9169 : case BIND_EXPR:
9170 : case STATEMENT_LIST:
9171 : case LOOP_EXPR:
9172 : case COND_EXPR:
9173 : case IF_STMT:
9174 : case DO_STMT:
9175 : case WHILE_STMT:
9176 : case FOR_STMT:
9177 : break;
9178 306548 : case LABEL_EXPR:
9179 306548 : case CASE_LABEL_EXPR:
9180 306548 : if (label_matches (ctx, jump_target, t))
9181 : /* Found it. */
9182 30700 : *jump_target = NULL_TREE;
9183 306548 : return NULL_TREE;
9184 : default:
9185 : return NULL_TREE;
9186 : }
9187 : }
9188 1956753384 : if (error_operand_p (t))
9189 : {
9190 132 : *non_constant_p = true;
9191 132 : return t;
9192 : }
9193 :
9194 : /* Change the input location to the currently processed expression for
9195 : better error messages when a subexpression has no location. */
9196 1956753252 : location_t loc = cp_expr_loc_or_input_loc (t);
9197 3913501091 : iloc_sentinel sentinel (loc);
9198 :
9199 1956753252 : STRIP_ANY_LOCATION_WRAPPER (t);
9200 :
9201 1956753252 : if (CONSTANT_CLASS_P (t))
9202 : {
9203 369487622 : if (TREE_OVERFLOW (t))
9204 : {
9205 62 : if (!ctx->quiet)
9206 15 : permerror (input_location, "overflow in constant expression");
9207 62 : if (!flag_permissive || ctx->quiet)
9208 59 : *overflow_p = true;
9209 : }
9210 :
9211 369487622 : if (TREE_CODE (t) == INTEGER_CST
9212 355457474 : && TYPE_PTR_P (TREE_TYPE (t))
9213 : /* INTEGER_CST with pointer-to-method type is only used
9214 : for a virtual method in a pointer to member function.
9215 : Don't reject those. */
9216 1584148 : && TREE_CODE (TREE_TYPE (TREE_TYPE (t))) != METHOD_TYPE
9217 371071503 : && !integer_zerop (t))
9218 : {
9219 5 : if (!ctx->quiet)
9220 0 : error ("value %qE of type %qT is not a constant expression",
9221 0 : t, TREE_TYPE (t));
9222 5 : *non_constant_p = true;
9223 : }
9224 :
9225 369487622 : return t;
9226 : }
9227 :
9228 : /* Avoid excessively long constexpr evaluations. */
9229 1587265630 : if (++ctx->global->constexpr_ops_count >= constexpr_ops_limit)
9230 : {
9231 9 : if (!ctx->quiet)
9232 3 : error_at (loc,
9233 : "%<constexpr%> evaluation operation count exceeds limit of "
9234 : "%wd (use %<-fconstexpr-ops-limit=%> to increase the limit)",
9235 : constexpr_ops_limit);
9236 9 : ctx->global->constexpr_ops_count = INTTYPE_MINIMUM (HOST_WIDE_INT);
9237 9 : *non_constant_p = true;
9238 9 : return t;
9239 : }
9240 :
9241 1587265621 : constexpr_ctx new_ctx;
9242 1587265621 : tree r = t;
9243 :
9244 1587265621 : tree_code tcode = TREE_CODE (t);
9245 1587265621 : switch (tcode)
9246 : {
9247 28921982 : case RESULT_DECL:
9248 28921982 : if (lval)
9249 : return t;
9250 : /* We ask for an rvalue for the RESULT_DECL when indirecting
9251 : through an invisible reference, or in named return value
9252 : optimization. */
9253 31639 : if (tree v = ctx->global->get_value (t))
9254 : return v;
9255 : else
9256 : {
9257 3 : if (!ctx->quiet)
9258 0 : error ("%qE is not a constant expression", t);
9259 3 : *non_constant_p = true;
9260 : }
9261 3 : break;
9262 :
9263 218900281 : case VAR_DECL:
9264 218900281 : if (DECL_HAS_VALUE_EXPR_P (t))
9265 : {
9266 4382069 : if (is_normal_capture_proxy (t)
9267 4382069 : && current_function_decl == DECL_CONTEXT (t))
9268 : {
9269 : /* Function parms aren't constexpr within the function
9270 : definition, so don't try to look at the closure. But if the
9271 : captured variable is constant, try to evaluate it directly. */
9272 324484 : r = DECL_CAPTURED_VARIABLE (t);
9273 : }
9274 : else
9275 4057585 : r = DECL_VALUE_EXPR (t);
9276 :
9277 4382069 : tree type = TREE_TYPE (t);
9278 4382069 : if (TYPE_REF_P (type) != TYPE_REF_P (TREE_TYPE (r)))
9279 : {
9280 : /* Adjust r to match the reference-ness of t. */
9281 143091 : if (TYPE_REF_P (type))
9282 140790 : r = build_address (r);
9283 : else
9284 2301 : r = convert_from_reference (r);
9285 : }
9286 4382069 : return cxx_eval_constant_expression (ctx, r, lval, non_constant_p,
9287 4382069 : overflow_p, jump_target);
9288 : }
9289 : /* fall through */
9290 223174083 : case CONST_DECL:
9291 : /* We used to not check lval for CONST_DECL, but darwin.cc uses
9292 : CONST_DECL for aggregate constants. */
9293 223174083 : if (lval)
9294 : return t;
9295 168258149 : else if (t == ctx->object)
9296 940781 : return ctx->ctor;
9297 167317368 : if (VAR_P (t))
9298 : {
9299 158661497 : if (tree v = ctx->global->get_value (t))
9300 : {
9301 : r = v;
9302 : break;
9303 : }
9304 123526525 : if (ctx->global->is_outside_lifetime (t))
9305 : {
9306 103 : if (!ctx->quiet)
9307 30 : outside_lifetime_error (loc, t);
9308 103 : *non_constant_p = true;
9309 103 : break;
9310 : }
9311 : }
9312 132182293 : if (ctx->manifestly_const_eval == mce_true)
9313 85013515 : maybe_warn_about_constant_value (loc, t);
9314 132182293 : if (COMPLETE_TYPE_P (TREE_TYPE (t))
9315 132182293 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9316 : {
9317 : /* If the class is empty, we aren't actually loading anything. */
9318 136354 : r = build_constructor (TREE_TYPE (t), NULL);
9319 136354 : TREE_CONSTANT (r) = true;
9320 : }
9321 132045939 : else if (ctx->strict)
9322 131771081 : r = decl_really_constant_value (t, /*unshare_p=*/false);
9323 : else
9324 274858 : r = decl_constant_value (t, /*unshare_p=*/false);
9325 132179596 : if (TREE_CODE (r) == TARGET_EXPR
9326 132179596 : && TREE_CODE (TARGET_EXPR_INITIAL (r)) == CONSTRUCTOR)
9327 0 : r = TARGET_EXPR_INITIAL (r);
9328 132179596 : if (DECL_P (r)
9329 : /* P2280 allows references to unknown. */
9330 132436160 : && !(p2280_active_p (ctx) && VAR_P (t) && TYPE_REF_P (TREE_TYPE (t))))
9331 : {
9332 28425054 : if (!ctx->quiet)
9333 180 : non_const_var_error (loc, r, /*fundef_p*/false);
9334 28425054 : *non_constant_p = true;
9335 : }
9336 : break;
9337 :
9338 : case DEBUG_BEGIN_STMT:
9339 : /* ??? It might be nice to retain this information somehow, so
9340 : as to be able to step into a constexpr function call. */
9341 : /* Fall through. */
9342 :
9343 : case FUNCTION_DECL:
9344 : case TEMPLATE_DECL:
9345 : case LABEL_DECL:
9346 : case LABEL_EXPR:
9347 : case CASE_LABEL_EXPR:
9348 : case PREDICT_EXPR:
9349 : case OMP_DECLARE_MAPPER:
9350 : case REFLECT_EXPR:
9351 : return t;
9352 :
9353 174490238 : case PARM_DECL:
9354 174490238 : if (lval && !TYPE_REF_P (TREE_TYPE (t)))
9355 : {
9356 : /* glvalue use. */
9357 8952805 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9358 110914 : if (tree v = ctx->global->get_value (t))
9359 1260588857 : r = v;
9360 : }
9361 165537433 : else if (tree v = ctx->global->get_value (t))
9362 : {
9363 75901146 : r = v;
9364 75901146 : if (TREE_ADDRESSABLE (TREE_TYPE (t)))
9365 5147 : r = cxx_eval_constant_expression (ctx, r, vc_prvalue,
9366 : non_constant_p, overflow_p,
9367 : jump_target);
9368 75901146 : if (*jump_target)
9369 : return NULL_TREE;
9370 : }
9371 89636287 : else if (lval)
9372 : /* Defer in case this is only used for its type. */;
9373 89636287 : else if (ctx->global->is_outside_lifetime (t))
9374 : {
9375 18 : if (!ctx->quiet)
9376 6 : outside_lifetime_error (loc, t);
9377 18 : *non_constant_p = true;
9378 18 : break;
9379 : }
9380 89636269 : else if (COMPLETE_TYPE_P (TREE_TYPE (t))
9381 89636269 : && is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
9382 : {
9383 : /* If the class is empty, we aren't actually loading anything. */
9384 25023 : r = build_constructor (TREE_TYPE (t), NULL);
9385 25023 : TREE_CONSTANT (r) = true;
9386 : }
9387 89611246 : else if (p2280_active_p (ctx) && TYPE_REF_P (TREE_TYPE (t)))
9388 : /* P2280 allows references to unknown... */;
9389 89359140 : else if (p2280_active_p (ctx) && is_this_parameter (t))
9390 : /* ...as well as the this pointer. */;
9391 : else
9392 : {
9393 88954792 : if (!ctx->quiet)
9394 195 : error ("%qE is not a constant expression", t);
9395 88954792 : *non_constant_p = true;
9396 : }
9397 : break;
9398 :
9399 139021591 : case CALL_EXPR:
9400 139021591 : case AGGR_INIT_EXPR:
9401 139021591 : r = cxx_eval_call_expression (ctx, t, lval,
9402 : non_constant_p, overflow_p, jump_target);
9403 139021591 : break;
9404 :
9405 10993633 : case DECL_EXPR:
9406 10993633 : {
9407 10993633 : r = DECL_EXPR_DECL (t);
9408 10993633 : if (TREE_CODE (r) == USING_DECL)
9409 : {
9410 9034 : r = void_node;
9411 9034 : break;
9412 : }
9413 :
9414 10984599 : if (VAR_P (r)
9415 10984599 : && (TREE_STATIC (r)
9416 10862177 : || (CP_DECL_THREAD_LOCAL_P (r) && !DECL_REALLY_EXTERN (r)))
9417 : /* Allow __FUNCTION__ etc. */
9418 122422 : && !DECL_ARTIFICIAL (r)
9419 10984641 : && !decl_constant_var_p (r))
9420 : {
9421 7 : if (!ctx->quiet)
9422 : {
9423 2 : if (CP_DECL_THREAD_LOCAL_P (r))
9424 1 : error_at (loc, "control passes through definition of %qD "
9425 : "with thread storage duration", r);
9426 : else
9427 1 : error_at (loc, "control passes through definition of %qD "
9428 : "with static storage duration", r);
9429 : }
9430 7 : *non_constant_p = true;
9431 7 : break;
9432 : }
9433 :
9434 : /* make_rtl_for_nonlocal_decl could have deferred emission of
9435 : a local static var, but if it appears in a statement expression
9436 : which is constant expression evaluated to e.g. just the address
9437 : of the variable, its DECL_EXPR will never be seen during
9438 : gimple lowering's record_vars_into as the statement expression
9439 : will not be in the IL at all. */
9440 10984592 : if (VAR_P (r)
9441 10984592 : && TREE_STATIC (r)
9442 122415 : && !DECL_REALLY_EXTERN (r)
9443 122404 : && DECL_FUNCTION_SCOPE_P (r)
9444 122404 : && !var_in_maybe_constexpr_fn (r)
9445 10984595 : && decl_constant_var_p (r))
9446 : {
9447 3 : varpool_node *node = varpool_node::get (r);
9448 3 : if (node == NULL || !node->definition)
9449 3 : rest_of_decl_compilation (r, 0, at_eof);
9450 : }
9451 :
9452 21813645 : if (AGGREGATE_TYPE_P (TREE_TYPE (r))
9453 20774945 : || VECTOR_TYPE_P (TREE_TYPE (r)))
9454 : {
9455 1195345 : new_ctx = *ctx;
9456 1195345 : new_ctx.object = r;
9457 1195345 : new_ctx.ctor = build_constructor (TREE_TYPE (r), NULL);
9458 1195345 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9459 1195345 : ctx->global->put_value (r, new_ctx.ctor);
9460 1195345 : ctx = &new_ctx;
9461 : }
9462 :
9463 10984592 : if (tree init = DECL_INITIAL (r))
9464 : {
9465 6156821 : init = cxx_eval_constant_expression (ctx, init, vc_prvalue,
9466 : non_constant_p, overflow_p,
9467 : jump_target);
9468 6156821 : if (*jump_target)
9469 : return NULL_TREE;
9470 : /* Don't share a CONSTRUCTOR that might be changed. */
9471 6156821 : init = unshare_constructor (init);
9472 : /* Remember that a constant object's constructor has already
9473 : run. */
9474 12313642 : if (CLASS_TYPE_P (TREE_TYPE (r))
9475 6448569 : && CP_TYPE_CONST_P (TREE_TYPE (r)))
9476 35596 : TREE_READONLY (init) = true;
9477 6156821 : ctx->global->put_value (r, init);
9478 : }
9479 4827771 : else if (ctx == &new_ctx)
9480 : /* We gave it a CONSTRUCTOR above. */;
9481 : else
9482 3957257 : ctx->global->put_value (r, NULL_TREE);
9483 : }
9484 : break;
9485 :
9486 17246810 : case TARGET_EXPR:
9487 17246810 : {
9488 17246810 : tree type = TREE_TYPE (t);
9489 :
9490 17246810 : if (!literal_type_p (type))
9491 : {
9492 5138 : if (!ctx->quiet)
9493 : {
9494 0 : auto_diagnostic_group d;
9495 0 : error ("temporary of non-literal type %qT in a "
9496 : "constant expression", type);
9497 0 : explain_non_literal_class (type);
9498 0 : }
9499 5138 : *non_constant_p = true;
9500 8161183 : break;
9501 : }
9502 17241672 : gcc_checking_assert (!TARGET_EXPR_DIRECT_INIT_P (t));
9503 : /* Avoid evaluating a TARGET_EXPR more than once. */
9504 17241672 : tree slot = TARGET_EXPR_SLOT (t);
9505 17241672 : if (tree v = ctx->global->get_value (slot))
9506 : {
9507 1797 : if (lval)
9508 7349917 : return slot;
9509 : r = v;
9510 : break;
9511 : }
9512 17239875 : if ((AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9513 : {
9514 : /* We're being expanded without an explicit target, so start
9515 : initializing a new object; expansion with an explicit target
9516 : strips the TARGET_EXPR before we get here. */
9517 13675078 : new_ctx = *ctx;
9518 : /* Link CTX to NEW_CTX so that lookup_placeholder can resolve
9519 : any PLACEHOLDER_EXPR within the initializer that refers to the
9520 : former object under construction. */
9521 13675078 : new_ctx.parent = ctx;
9522 13675078 : new_ctx.ctor = build_constructor (type, NULL);
9523 13675078 : CONSTRUCTOR_NO_CLEARING (new_ctx.ctor) = true;
9524 13675078 : new_ctx.object = slot;
9525 13675078 : ctx->global->put_value (new_ctx.object, new_ctx.ctor);
9526 13675078 : ctx = &new_ctx;
9527 : }
9528 :
9529 : /* If the initializer is complex, evaluate it to initialize slot. */
9530 17239875 : bool is_complex = target_expr_needs_replace (t);
9531 17239875 : if (is_complex)
9532 : /* In case no initialization actually happens, clear out any
9533 : void_node from a previous evaluation. */
9534 267 : ctx->global->put_value (slot, NULL_TREE);
9535 :
9536 : /* Pass vc_prvalue because this indicates
9537 : initialization of a temporary. */
9538 17239875 : r = cxx_eval_constant_expression (ctx, TARGET_EXPR_INITIAL (t),
9539 : vc_prvalue, non_constant_p,
9540 : overflow_p, jump_target);
9541 17239875 : if (*non_constant_p)
9542 : break;
9543 9084813 : if (ctx->save_exprs)
9544 4584024 : ctx->save_exprs->safe_push (slot);
9545 9084813 : if (*jump_target)
9546 : {
9547 251 : if (!is_complex
9548 251 : && !(AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)))
9549 : /* If TARGET_EXPR_INITIAL throws exception and slot's value
9550 : has not been changed yet, CLEANUP_POINT_EXPR handling
9551 : could see there void_node from a previous evaluation
9552 : and complain. */
9553 3 : ctx->global->put_value (slot, NULL_TREE);
9554 251 : return NULL_TREE;
9555 : }
9556 9084562 : if (!is_complex)
9557 : {
9558 9084341 : r = unshare_constructor (r);
9559 : /* Adjust the type of the result to the type of the temporary. */
9560 9084341 : r = adjust_temp_type (type, r);
9561 9084341 : ctx->global->put_value (slot, r);
9562 : }
9563 9084562 : if (TARGET_EXPR_CLEANUP (t)
9564 9084562 : && (!CLEANUP_EH_ONLY (t) || cxx_dialect >= cxx26))
9565 : {
9566 712990 : ctx->global->cleanups->safe_push (TARGET_EXPR_CLEANUP (t));
9567 : /* Mark CLEANUP_EH_ONLY cleanups by pushing NULL_TREE after
9568 : them. */
9569 712990 : if (CLEANUP_EH_ONLY (t))
9570 955 : ctx->global->cleanups->safe_push (NULL_TREE);
9571 : }
9572 9084562 : if (lval)
9573 : return slot;
9574 1735710 : if (is_complex)
9575 0 : r = ctx->global->get_value (slot);
9576 : }
9577 1735710 : break;
9578 :
9579 58379331 : case INIT_EXPR:
9580 58379331 : case MODIFY_EXPR:
9581 58379331 : gcc_assert (jump_target == NULL || *jump_target == NULL_TREE);
9582 58379331 : r = cxx_eval_store_expression (ctx, t, lval,
9583 : non_constant_p, overflow_p, jump_target);
9584 58379331 : break;
9585 :
9586 0 : case SCOPE_REF:
9587 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1),
9588 : lval,
9589 : non_constant_p, overflow_p,
9590 : jump_target);
9591 0 : break;
9592 :
9593 35254166 : case RETURN_EXPR:
9594 35254166 : if (TREE_OPERAND (t, 0) != NULL_TREE)
9595 35081053 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9596 : lval,
9597 : non_constant_p, overflow_p,
9598 : jump_target);
9599 35254164 : if (!throws (jump_target))
9600 35254026 : *jump_target = t;
9601 : break;
9602 20592 : case BREAK_STMT:
9603 20592 : case CONTINUE_STMT:
9604 20592 : *jump_target = t;
9605 20592 : break;
9606 :
9607 598267 : case SAVE_EXPR:
9608 : /* Avoid evaluating a SAVE_EXPR more than once. */
9609 598267 : if (tree v = ctx->global->get_value (t))
9610 : r = v;
9611 : else
9612 : {
9613 586107 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9614 : vc_prvalue, non_constant_p,
9615 : overflow_p, jump_target);
9616 586107 : if (*non_constant_p || *jump_target)
9617 : break;
9618 8435 : ctx->global->put_value (t, r);
9619 8435 : if (ctx->save_exprs)
9620 6297 : ctx->save_exprs->safe_push (t);
9621 : }
9622 : break;
9623 :
9624 34953464 : case NON_LVALUE_EXPR:
9625 34953464 : case EXPR_STMT:
9626 34953464 : case EH_SPEC_BLOCK:
9627 34953464 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9628 : lval,
9629 : non_constant_p, overflow_p,
9630 : jump_target);
9631 34953464 : break;
9632 :
9633 2258 : case TRY_BLOCK:
9634 2258 : r = cxx_eval_constant_expression (ctx, TRY_STMTS (t), lval,
9635 : non_constant_p, overflow_p,
9636 : jump_target);
9637 2258 : if (!*non_constant_p && throws (jump_target))
9638 1733 : if (tree h = TRY_HANDLERS (t))
9639 : {
9640 1733 : tree type = strip_array_types (TREE_TYPE (*jump_target));
9641 1733 : if (TREE_CODE (h) == STATEMENT_LIST)
9642 : {
9643 468 : for (tree stmt : tsi_range (h))
9644 451 : if (TREE_CODE (stmt) == HANDLER
9645 451 : && handler_match_for_exception_type (stmt, type))
9646 : {
9647 : h = stmt;
9648 : break;
9649 : }
9650 230 : if (TREE_CODE (h) == STATEMENT_LIST)
9651 : h = NULL_TREE;
9652 : }
9653 1503 : else if (TREE_CODE (h) != HANDLER
9654 1503 : || !handler_match_for_exception_type (h, type))
9655 : h = NULL_TREE;
9656 : if (h)
9657 : {
9658 1716 : gcc_assert (VAR_P (*jump_target));
9659 1716 : ctx->global->caught_exceptions.safe_push (*jump_target);
9660 1716 : ctx->global->caught_exceptions.safe_push (HANDLER_TYPE (h));
9661 1716 : *jump_target = NULL_TREE;
9662 1716 : r = cxx_eval_constant_expression (ctx, HANDLER_BODY (h),
9663 : vc_discard, non_constant_p,
9664 : overflow_p, jump_target);
9665 : }
9666 : }
9667 : break;
9668 :
9669 51198854 : case CLEANUP_POINT_EXPR:
9670 51198854 : {
9671 51198854 : auto_vec<tree, 2> cleanups;
9672 51198854 : vec<tree> *prev_cleanups = ctx->global->cleanups;
9673 51198854 : ctx->global->cleanups = &cleanups;
9674 :
9675 51198854 : auto_vec<tree, 10> save_exprs;
9676 51198854 : constexpr_ctx new_ctx = *ctx;
9677 51198854 : new_ctx.save_exprs = &save_exprs;
9678 :
9679 51198854 : r = cxx_eval_constant_expression (&new_ctx, TREE_OPERAND (t, 0),
9680 : lval,
9681 : non_constant_p, overflow_p,
9682 : jump_target);
9683 :
9684 51198853 : ctx->global->cleanups = prev_cleanups;
9685 51198853 : unsigned int i;
9686 51198853 : tree cleanup, jmp_target = NULL_TREE;
9687 51198853 : bool eh = throws (jump_target);
9688 : /* Evaluate the cleanups. */
9689 103031144 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
9690 633438 : if (cleanup == NULL_TREE)
9691 : {
9692 : /* NULL_TREE cleanup is a marker that before it is
9693 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it
9694 : if the body didn't throw. */
9695 932 : if (!eh)
9696 931 : --i;
9697 : }
9698 : else
9699 632506 : cxx_eval_constant_expression (&new_ctx, cleanup, vc_discard,
9700 : non_constant_p, overflow_p,
9701 : &jmp_target);
9702 :
9703 : /* Forget SAVE_EXPRs and TARGET_EXPRs created by this
9704 : full-expression. */
9705 158186880 : for (tree save_expr : save_exprs)
9706 4590321 : destroy_value_checked (ctx, save_expr, non_constant_p);
9707 51198853 : if (throws (&jmp_target))
9708 0 : *jump_target = jmp_target;
9709 51198853 : }
9710 51198853 : break;
9711 :
9712 34127877 : case MUST_NOT_THROW_EXPR:
9713 34127877 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
9714 : lval,
9715 : non_constant_p, overflow_p,
9716 : jump_target);
9717 34127876 : if (throws (jump_target))
9718 : {
9719 : /* [except.handle]/7 - If the search for a handler exits the
9720 : function body of a function with a non-throwing exception
9721 : specification, the function std::terminate is invoked. */
9722 27 : if (!ctx->quiet)
9723 : {
9724 9 : auto_diagnostic_group d;
9725 9 : diagnose_std_terminate (loc, ctx, *jump_target);
9726 9 : if (MUST_NOT_THROW_NOEXCEPT_P (t)
9727 7 : && ctx->call
9728 16 : && ctx->call->fundef)
9729 7 : inform (loc, "uncaught exception exited from %<noexcept%> "
9730 : "function %qD",
9731 : ctx->call->fundef->decl);
9732 2 : else if (MUST_NOT_THROW_THROW_P (t))
9733 1 : inform (loc, "destructor exited with an exception after "
9734 : "initializing the exception object");
9735 1 : else if (MUST_NOT_THROW_CATCH_P (t))
9736 1 : inform (loc, "constructor exited with another exception while "
9737 : "entering handler");
9738 9 : }
9739 27 : *non_constant_p = true;
9740 27 : *jump_target = NULL_TREE;
9741 27 : r = NULL_TREE;
9742 : }
9743 : break;
9744 :
9745 0 : case TRY_CATCH_EXPR:
9746 0 : if (TREE_OPERAND (t, 0) == NULL_TREE)
9747 : {
9748 0 : r = void_node;
9749 0 : break;
9750 : }
9751 0 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9752 : non_constant_p, overflow_p,
9753 : jump_target);
9754 0 : if (!*non_constant_p && throws (jump_target))
9755 : {
9756 0 : tree jmp_target = NULL_TREE;
9757 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9758 : non_constant_p, overflow_p,
9759 : &jmp_target);
9760 0 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9761 : jmp_target);
9762 : }
9763 : break;
9764 :
9765 623 : case TRY_FINALLY_EXPR:
9766 623 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9767 : non_constant_p, overflow_p,
9768 : jump_target);
9769 623 : if (!*non_constant_p)
9770 : {
9771 608 : tree jmp_target = NULL_TREE;
9772 : /* Also evaluate the cleanup. */
9773 608 : if (TREE_CODE (TREE_OPERAND (t, 1)) == EH_ELSE_EXPR
9774 608 : && throws (jump_target))
9775 0 : cxx_eval_constant_expression (ctx,
9776 0 : TREE_OPERAND (TREE_OPERAND (t, 1),
9777 : 1), vc_discard,
9778 : non_constant_p, overflow_p,
9779 : &jmp_target);
9780 : else
9781 608 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 1), vc_discard,
9782 : non_constant_p, overflow_p,
9783 : &jmp_target);
9784 608 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9785 : jmp_target);
9786 : }
9787 : break;
9788 :
9789 0 : case EH_ELSE_EXPR:
9790 : /* Evaluate any cleanup that applies to non-EH exits. */
9791 0 : cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), vc_discard,
9792 : non_constant_p, overflow_p,
9793 : jump_target);
9794 :
9795 : /* The EH path is handled in TRY_FINALLY_EXPR handling above. */
9796 0 : break;
9797 :
9798 2181195 : case CLEANUP_STMT:
9799 2181195 : r = cxx_eval_constant_expression (ctx, CLEANUP_BODY (t), lval,
9800 : non_constant_p, overflow_p,
9801 : jump_target);
9802 2181194 : if ((!CLEANUP_EH_ONLY (t) || throws (jump_target)) && !*non_constant_p)
9803 : {
9804 798360 : iloc_sentinel ils (loc);
9805 798360 : tree jmp_target = NULL_TREE;
9806 : /* Also evaluate the cleanup. */
9807 798360 : cxx_eval_constant_expression (ctx, CLEANUP_EXPR (t), vc_discard,
9808 : non_constant_p, overflow_p,
9809 : &jmp_target);
9810 798360 : r = merge_jump_target (loc, ctx, r, non_constant_p, jump_target,
9811 : jmp_target);
9812 798360 : }
9813 : break;
9814 :
9815 : /* These differ from cxx_eval_unary_expression in that this doesn't
9816 : check for a constant operand or result; an address can be
9817 : constant without its operand being, and vice versa. */
9818 59084870 : case MEM_REF:
9819 59084870 : case INDIRECT_REF:
9820 59084870 : r = cxx_eval_indirect_ref (ctx, t, lval,
9821 : non_constant_p, overflow_p,
9822 : jump_target);
9823 59084870 : break;
9824 :
9825 65970934 : case ADDR_EXPR:
9826 65970934 : {
9827 65970934 : tree oldop = TREE_OPERAND (t, 0);
9828 65970934 : tree op = cxx_eval_constant_expression (ctx, oldop, vc_glvalue,
9829 : non_constant_p, overflow_p,
9830 : jump_target);
9831 65970934 : if (*jump_target)
9832 : return NULL_TREE;
9833 : /* Don't VERIFY_CONSTANT here. */
9834 65970930 : if (*non_constant_p)
9835 : return t;
9836 50872212 : gcc_checking_assert (TREE_CODE (op) != CONSTRUCTOR);
9837 : /* This function does more aggressive folding than fold itself. */
9838 50872212 : r = build_fold_addr_expr_with_type (op, TREE_TYPE (t));
9839 50872212 : if (TREE_CODE (r) == ADDR_EXPR && TREE_OPERAND (r, 0) == oldop)
9840 : {
9841 36513169 : ggc_free (r);
9842 36513169 : return t;
9843 : }
9844 : break;
9845 : }
9846 :
9847 472145 : case REALPART_EXPR:
9848 472145 : case IMAGPART_EXPR:
9849 472145 : if (lval)
9850 : {
9851 614 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
9852 : non_constant_p, overflow_p,
9853 : jump_target);
9854 614 : if (*jump_target)
9855 : return NULL_TREE;
9856 614 : if (r == error_mark_node)
9857 : ;
9858 614 : else if (r == TREE_OPERAND (t, 0) || lval == vc_discard)
9859 : r = t;
9860 : else
9861 568 : r = fold_build1 (TREE_CODE (t), TREE_TYPE (t), r);
9862 : break;
9863 : }
9864 : /* FALLTHRU */
9865 21089671 : case CONJ_EXPR:
9866 21089671 : case FIX_TRUNC_EXPR:
9867 21089671 : case FLOAT_EXPR:
9868 21089671 : case NEGATE_EXPR:
9869 21089671 : case ABS_EXPR:
9870 21089671 : case ABSU_EXPR:
9871 21089671 : case BIT_NOT_EXPR:
9872 21089671 : case TRUTH_NOT_EXPR:
9873 21089671 : case FIXED_CONVERT_EXPR:
9874 21089671 : case VEC_DUPLICATE_EXPR:
9875 21089671 : r = cxx_eval_unary_expression (ctx, t, lval,
9876 : non_constant_p, overflow_p,
9877 : jump_target);
9878 21089671 : break;
9879 :
9880 8554970 : case SIZEOF_EXPR:
9881 8554970 : r = fold_sizeof_expr (t);
9882 : /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
9883 : which could lead to an infinite recursion. */
9884 8554970 : if (TREE_CODE (r) != SIZEOF_EXPR)
9885 8554970 : r = cxx_eval_constant_expression (ctx, r, lval,
9886 : non_constant_p, overflow_p,
9887 : jump_target);
9888 : else
9889 : {
9890 0 : *non_constant_p = true;
9891 0 : gcc_assert (ctx->quiet);
9892 : }
9893 :
9894 : break;
9895 :
9896 6879638 : case COMPOUND_EXPR:
9897 6879638 : {
9898 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
9899 : COMPOUND_EXPR; don't get confused. Also handle EMPTY_CLASS_EXPR
9900 : introduced by build_call_a. */
9901 6879638 : tree op0 = TREE_OPERAND (t, 0);
9902 6879638 : tree op1 = TREE_OPERAND (t, 1);
9903 6879638 : STRIP_NOPS (op1);
9904 2157097 : if ((TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
9905 7946422 : || TREE_CODE (op1) == EMPTY_CLASS_EXPR)
9906 3903885 : r = cxx_eval_constant_expression (ctx, op0,
9907 : lval, non_constant_p, overflow_p,
9908 : jump_target);
9909 : else
9910 : {
9911 : /* Check that the LHS is constant and then discard it. */
9912 2975753 : cxx_eval_constant_expression (ctx, op0, vc_discard,
9913 : non_constant_p, overflow_p,
9914 : jump_target);
9915 2975753 : if (*jump_target)
9916 : return NULL_TREE;
9917 2972279 : if (*non_constant_p)
9918 : return t;
9919 2724377 : op1 = TREE_OPERAND (t, 1);
9920 2724377 : r = cxx_eval_constant_expression (ctx, op1,
9921 : lval, non_constant_p, overflow_p,
9922 : jump_target);
9923 : }
9924 : }
9925 : break;
9926 :
9927 76327947 : case POINTER_PLUS_EXPR:
9928 76327947 : case POINTER_DIFF_EXPR:
9929 76327947 : case PLUS_EXPR:
9930 76327947 : case MINUS_EXPR:
9931 76327947 : case MULT_EXPR:
9932 76327947 : case TRUNC_DIV_EXPR:
9933 76327947 : case CEIL_DIV_EXPR:
9934 76327947 : case FLOOR_DIV_EXPR:
9935 76327947 : case ROUND_DIV_EXPR:
9936 76327947 : case TRUNC_MOD_EXPR:
9937 76327947 : case CEIL_MOD_EXPR:
9938 76327947 : case ROUND_MOD_EXPR:
9939 76327947 : case RDIV_EXPR:
9940 76327947 : case EXACT_DIV_EXPR:
9941 76327947 : case MIN_EXPR:
9942 76327947 : case MAX_EXPR:
9943 76327947 : case LSHIFT_EXPR:
9944 76327947 : case RSHIFT_EXPR:
9945 76327947 : case LROTATE_EXPR:
9946 76327947 : case RROTATE_EXPR:
9947 76327947 : case BIT_IOR_EXPR:
9948 76327947 : case BIT_XOR_EXPR:
9949 76327947 : case BIT_AND_EXPR:
9950 76327947 : case TRUTH_XOR_EXPR:
9951 76327947 : case LT_EXPR:
9952 76327947 : case LE_EXPR:
9953 76327947 : case GT_EXPR:
9954 76327947 : case GE_EXPR:
9955 76327947 : case EQ_EXPR:
9956 76327947 : case NE_EXPR:
9957 76327947 : case SPACESHIP_EXPR:
9958 76327947 : case UNORDERED_EXPR:
9959 76327947 : case ORDERED_EXPR:
9960 76327947 : case UNLT_EXPR:
9961 76327947 : case UNLE_EXPR:
9962 76327947 : case UNGT_EXPR:
9963 76327947 : case UNGE_EXPR:
9964 76327947 : case UNEQ_EXPR:
9965 76327947 : case LTGT_EXPR:
9966 76327947 : case RANGE_EXPR:
9967 76327947 : case COMPLEX_EXPR:
9968 76327947 : r = cxx_eval_binary_expression (ctx, t, lval,
9969 : non_constant_p, overflow_p,
9970 : jump_target);
9971 76327947 : break;
9972 :
9973 : /* fold can introduce non-IF versions of these; still treat them as
9974 : short-circuiting. */
9975 8731820 : case TRUTH_AND_EXPR:
9976 8731820 : case TRUTH_ANDIF_EXPR:
9977 8731820 : r = cxx_eval_logical_expression (ctx, t, boolean_false_node,
9978 : boolean_true_node,
9979 : non_constant_p, overflow_p,
9980 : jump_target);
9981 8731820 : break;
9982 :
9983 1961339 : case TRUTH_OR_EXPR:
9984 1961339 : case TRUTH_ORIF_EXPR:
9985 1961339 : r = cxx_eval_logical_expression (ctx, t, boolean_true_node,
9986 : boolean_false_node,
9987 : non_constant_p, overflow_p,
9988 : jump_target);
9989 1961339 : break;
9990 :
9991 8263818 : case ARRAY_REF:
9992 8263818 : r = cxx_eval_array_reference (ctx, t, lval,
9993 : non_constant_p, overflow_p,
9994 : jump_target);
9995 8263818 : break;
9996 :
9997 65336760 : case COMPONENT_REF:
9998 65336760 : if (is_overloaded_fn (t))
9999 : {
10000 : /* We can only get here in checking mode via
10001 : build_non_dependent_expr, because any expression that
10002 : calls or takes the address of the function will have
10003 : pulled a FUNCTION_DECL out of the COMPONENT_REF. */
10004 6 : gcc_checking_assert (ctx->quiet || errorcount);
10005 6 : *non_constant_p = true;
10006 6 : return t;
10007 : }
10008 65336754 : r = cxx_eval_component_reference (ctx, t, lval,
10009 : non_constant_p, overflow_p,
10010 : jump_target);
10011 65336754 : break;
10012 :
10013 15766 : case BIT_FIELD_REF:
10014 15766 : r = cxx_eval_bit_field_ref (ctx, t, lval,
10015 : non_constant_p, overflow_p,
10016 : jump_target);
10017 15766 : break;
10018 :
10019 18474382 : case COND_EXPR:
10020 18474382 : case IF_STMT:
10021 18474382 : if (*jump_target)
10022 : {
10023 150852 : tree orig_jump = *jump_target;
10024 150852 : tree arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 1))
10025 301704 : ? TREE_OPERAND (t, 1) : void_node);
10026 : /* When jumping to a label, the label might be either in the
10027 : then or else blocks, so process then block first in skipping
10028 : mode first, and if we are still in the skipping mode at its end,
10029 : process the else block too. */
10030 150852 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10031 : overflow_p, jump_target);
10032 : /* It's possible that we found the label in the then block. But
10033 : it could have been followed by another jumping statement, e.g.
10034 : say we're looking for case 1:
10035 : if (cond)
10036 : {
10037 : // skipped statements
10038 : case 1:; // clears up *jump_target
10039 : return 1; // and sets it to a RETURN_EXPR
10040 : }
10041 : else { ... }
10042 : in which case we need not go looking to the else block.
10043 : (goto is not allowed in a constexpr function.) */
10044 150852 : if (*jump_target == orig_jump)
10045 : {
10046 150900 : arg = ((TREE_CODE (t) != IF_STMT || TREE_OPERAND (t, 2))
10047 150900 : ? TREE_OPERAND (t, 2) : void_node);
10048 150818 : r = cxx_eval_constant_expression (ctx, arg, lval, non_constant_p,
10049 : overflow_p, jump_target);
10050 : }
10051 : break;
10052 : }
10053 18323530 : r = cxx_eval_conditional_expression (ctx, t, lval,
10054 : non_constant_p, overflow_p,
10055 : jump_target);
10056 18323530 : break;
10057 4582 : case VEC_COND_EXPR:
10058 4582 : r = cxx_eval_vector_conditional_expression (ctx, t, non_constant_p,
10059 : overflow_p, jump_target);
10060 4582 : break;
10061 :
10062 17814821 : case CONSTRUCTOR:
10063 17814821 : if (TREE_CONSTANT (t) && reduced_constant_expression_p (t))
10064 : {
10065 : /* Don't re-process a constant CONSTRUCTOR. */
10066 16307369 : verify_constructor_flags (t);
10067 16307369 : if (TREE_CONSTANT (t))
10068 : return t;
10069 : }
10070 1507452 : if (TREE_CLOBBER_P (t))
10071 : {
10072 : /* Assignment from a clobber is handled in cxx_eval_store_expression;
10073 : a clobber by itself isn't a constant-expression. */
10074 0 : gcc_assert (ctx->quiet);
10075 0 : *non_constant_p = true;
10076 0 : break;
10077 : }
10078 1507452 : r = cxx_eval_bare_aggregate (ctx, t, lval,
10079 : non_constant_p, overflow_p, jump_target);
10080 1507452 : break;
10081 :
10082 538 : case VEC_INIT_EXPR:
10083 : /* We can get this in a defaulted constructor for a class with a
10084 : non-static data member of array type. Either the initializer will
10085 : be NULL, meaning default-initialization, or it will be an lvalue
10086 : or xvalue of the same type, meaning direct-initialization from the
10087 : corresponding member. */
10088 538 : r = cxx_eval_vec_init (ctx, t, lval,
10089 : non_constant_p, overflow_p, jump_target);
10090 538 : break;
10091 :
10092 14887 : case VEC_PERM_EXPR:
10093 14887 : r = cxx_eval_trinary_expression (ctx, t, lval,
10094 : non_constant_p, overflow_p,
10095 : jump_target);
10096 14887 : break;
10097 :
10098 4 : case PAREN_EXPR:
10099 4 : gcc_assert (!REF_PARENTHESIZED_P (t));
10100 : /* A PAREN_EXPR resulting from __builtin_assoc_barrier has no effect in
10101 : constant expressions since it's unaffected by -fassociative-math. */
10102 4 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10103 : non_constant_p, overflow_p,
10104 : jump_target);
10105 4 : break;
10106 :
10107 289061300 : case NOP_EXPR:
10108 289061300 : if (REINTERPRET_CAST_P (t))
10109 : {
10110 73 : if (!ctx->quiet)
10111 6 : error_at (loc,
10112 : "%<reinterpret_cast%> is not a constant expression");
10113 73 : *non_constant_p = true;
10114 73 : return t;
10115 : }
10116 : /* FALLTHROUGH. */
10117 349487768 : case CONVERT_EXPR:
10118 349487768 : case VIEW_CONVERT_EXPR:
10119 349487768 : case UNARY_PLUS_EXPR:
10120 349487768 : {
10121 349487768 : tree oldop = TREE_OPERAND (t, 0);
10122 :
10123 349487768 : tree op = cxx_eval_constant_expression (ctx, oldop,
10124 349487768 : VOID_TYPE_P (TREE_TYPE (t))
10125 : ? vc_discard
10126 : : tcode == VIEW_CONVERT_EXPR
10127 328706585 : ? lval : vc_prvalue,
10128 : non_constant_p, overflow_p,
10129 : jump_target);
10130 349485070 : if (*jump_target)
10131 : return NULL_TREE;
10132 349483035 : if (*non_constant_p)
10133 : return t;
10134 266815635 : tree type = TREE_TYPE (t);
10135 :
10136 266815635 : if (VOID_TYPE_P (type))
10137 19879716 : return void_node;
10138 :
10139 246935919 : if (TREE_CODE (t) == CONVERT_EXPR
10140 25912862 : && ARITHMETIC_TYPE_P (type)
10141 4127629 : && INDIRECT_TYPE_P (TREE_TYPE (op))
10142 246959377 : && ctx->strict)
10143 : {
10144 23366 : if (!ctx->quiet)
10145 54 : error_at (loc,
10146 : "conversion from pointer type %qT to arithmetic type "
10147 54 : "%qT in a constant expression", TREE_TYPE (op), type);
10148 23366 : *non_constant_p = true;
10149 23366 : return t;
10150 : }
10151 :
10152 : /* [expr.const]: a conversion from type cv void* to a pointer-to-object
10153 : type cannot be part of a core constant expression as a resolution to
10154 : DR 1312. */
10155 77138183 : if (TYPE_PTROB_P (type)
10156 75019811 : && TYPE_PTR_P (TREE_TYPE (op))
10157 53105424 : && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
10158 : /* Inside a call to std::construct_at,
10159 : std::allocator<T>::{,de}allocate, or
10160 : std::source_location::current, we permit casting from void*
10161 : because that is compiler-generated code. */
10162 748042 : && !is_std_construct_at (ctx->call)
10163 82158 : && !is_std_allocator_allocate (ctx->call)
10164 246954131 : && !is_std_source_location_current (ctx->call))
10165 : {
10166 : /* Likewise, don't error when casting from void* when OP is
10167 : &heap uninit and similar. */
10168 41419 : tree sop = tree_strip_nop_conversions (op);
10169 41419 : tree decl = NULL_TREE;
10170 41419 : if (TREE_CODE (sop) == ADDR_EXPR)
10171 32010 : decl = TREE_OPERAND (sop, 0);
10172 32010 : if (decl
10173 32010 : && VAR_P (decl)
10174 31655 : && DECL_ARTIFICIAL (decl)
10175 63626 : && (DECL_NAME (decl) == heap_identifier
10176 24318 : || DECL_NAME (decl) == heap_uninit_identifier
10177 2817 : || DECL_NAME (decl) == heap_vec_identifier
10178 1521 : || DECL_NAME (decl) == heap_vec_uninit_identifier))
10179 : /* OK */;
10180 : /* P2738 (C++26): a conversion from a prvalue P of type "pointer to
10181 : cv void" to a pointer-to-object type T unless P is a null
10182 : pointer value or points to an object whose type is similar to
10183 : T. */
10184 9819 : else if (cxx_dialect > cxx23)
10185 : {
10186 9670 : if (integer_zerop (sop))
10187 28 : return build_int_cst (type, 0);
10188 9642 : r = cxx_fold_indirect_ref (ctx, loc, TREE_TYPE (type), sop,
10189 : NULL, jump_target);
10190 9642 : if (*jump_target)
10191 : return NULL_TREE;
10192 9642 : if (r)
10193 : {
10194 9607 : r = build1 (ADDR_EXPR, type, r);
10195 9607 : break;
10196 : }
10197 35 : if (!ctx->quiet)
10198 : {
10199 3 : gcc_assert (TREE_CODE (sop) == ADDR_EXPR);
10200 3 : auto_diagnostic_group d;
10201 3 : error_at (loc, "cast from %qT is not allowed in a "
10202 : "constant expression because "
10203 : "pointed-to type %qT is not similar to %qT",
10204 3 : TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
10205 3 : TREE_TYPE (type));
10206 3 : tree obj = build_fold_indirect_ref (sop);
10207 3 : if (TREE_CODE (obj) == COMPONENT_REF)
10208 2 : obj = TREE_OPERAND (obj, 1);
10209 3 : if (DECL_P (obj))
10210 3 : inform (DECL_SOURCE_LOCATION (obj),
10211 : "pointed-to object declared here");
10212 3 : }
10213 35 : *non_constant_p = true;
10214 35 : return t;
10215 : }
10216 : else
10217 : {
10218 149 : if (!ctx->quiet)
10219 26 : error_at (loc, "cast from %qT is not allowed in a "
10220 : "constant expression before C++26",
10221 26 : TREE_TYPE (op));
10222 149 : *non_constant_p = true;
10223 149 : return t;
10224 : }
10225 : }
10226 :
10227 246902734 : if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
10228 : {
10229 981 : op = cplus_expand_constant (op);
10230 981 : if (TREE_CODE (op) == PTRMEM_CST)
10231 : {
10232 21 : if (!ctx->quiet)
10233 3 : error_at (loc, "%qE is not a constant expression when the "
10234 : "class %qT is still incomplete", op,
10235 3 : PTRMEM_CST_CLASS (op));
10236 21 : *non_constant_p = true;
10237 21 : return t;
10238 : }
10239 : }
10240 :
10241 246902713 : if (TREE_CODE (op) == PTRMEM_CST && tcode == NOP_EXPR)
10242 : {
10243 643 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (op))
10244 643 : && !can_convert_qual (type, op))
10245 6 : op = cplus_expand_constant (op);
10246 643 : return cp_fold_convert (type, op);
10247 : }
10248 :
10249 246902070 : if (INDIRECT_TYPE_P (type) && TREE_CODE (op) == INTEGER_CST)
10250 : {
10251 540962 : if (integer_zerop (op))
10252 : {
10253 486236 : if (TYPE_REF_P (type))
10254 : {
10255 35 : if (!ctx->quiet)
10256 4 : error_at (loc, "dereferencing a null pointer");
10257 35 : *non_constant_p = true;
10258 35 : return t;
10259 : }
10260 : }
10261 54726 : else if (TYPE_PTR_P (type)
10262 54726 : && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
10263 : /* INTEGER_CST with pointer-to-method type is only used
10264 : for a virtual method in a pointer to member function.
10265 : Don't reject those. */
10266 : ;
10267 : else
10268 : {
10269 : /* This detects for example:
10270 : reinterpret_cast<void*>(sizeof 0)
10271 : */
10272 54723 : if (!ctx->quiet)
10273 15 : error_at (loc, "%<reinterpret_cast<%T>(%E)%> is not "
10274 : "a constant expression",
10275 : type, op);
10276 54723 : *non_constant_p = true;
10277 54723 : return t;
10278 : }
10279 : }
10280 :
10281 246847312 : if (INDIRECT_TYPE_P (type)
10282 122275457 : && TREE_CODE (op) == NOP_EXPR
10283 61606248 : && TREE_TYPE (op) == ptr_type_node
10284 260893 : && TREE_CODE (TREE_OPERAND (op, 0)) == ADDR_EXPR
10285 258197 : && VAR_P (TREE_OPERAND (TREE_OPERAND (op, 0), 0))
10286 247053270 : && (DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10287 205958 : 0)) == heap_uninit_identifier
10288 143877 : || DECL_NAME (TREE_OPERAND (TREE_OPERAND (op, 0),
10289 143877 : 0)) == heap_vec_uninit_identifier))
10290 : {
10291 63586 : tree var = TREE_OPERAND (TREE_OPERAND (op, 0), 0);
10292 63586 : tree var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
10293 63586 : tree elt_type = TREE_TYPE (type);
10294 63586 : tree cookie_size = NULL_TREE;
10295 63586 : tree arg_size = NULL_TREE;
10296 63586 : if (TREE_CODE (elt_type) == RECORD_TYPE
10297 63586 : && TYPE_IDENTIFIER (elt_type) == heap_identifier)
10298 : {
10299 9 : tree fld1 = TYPE_FIELDS (elt_type);
10300 9 : tree fld2 = DECL_CHAIN (fld1);
10301 9 : elt_type = TREE_TYPE (TREE_TYPE (fld2));
10302 9 : cookie_size = TYPE_SIZE_UNIT (TREE_TYPE (fld1));
10303 : }
10304 63586 : DECL_NAME (var)
10305 63586 : = (DECL_NAME (var) == heap_uninit_identifier
10306 63586 : ? heap_identifier : heap_vec_identifier);
10307 : /* For zero sized elt_type, try to recover how many outer_nelts
10308 : it should have. */
10309 63586 : if ((cookie_size ? tree_int_cst_equal (var_size, cookie_size)
10310 63577 : : integer_zerop (var_size))
10311 89 : && !int_size_in_bytes (elt_type)
10312 9 : && TREE_CODE (oldop) == CALL_EXPR
10313 63604 : && call_expr_nargs (oldop) >= 1)
10314 9 : if (tree fun = get_function_named_in_call (oldop))
10315 9 : if (cxx_replaceable_global_alloc_fn (fun)
10316 9 : && IDENTIFIER_NEW_OP_P (DECL_NAME (fun)))
10317 9 : arg_size = CALL_EXPR_ARG (oldop, 0);
10318 63586 : tree new_type
10319 63586 : = build_new_constexpr_heap_type (ctx, elt_type, cookie_size,
10320 : var_size, arg_size,
10321 : non_constant_p, overflow_p,
10322 : jump_target);
10323 63586 : if (*jump_target)
10324 : return NULL_TREE;
10325 63586 : TREE_TYPE (var) = new_type;
10326 63586 : TREE_TYPE (TREE_OPERAND (op, 0))
10327 127172 : = build_pointer_type (TREE_TYPE (var));
10328 : }
10329 :
10330 : /* This can happen for std::meta::info(^^int) where the cast has no
10331 : meaning. */
10332 246847312 : if (REFLECTION_TYPE_P (type) && REFLECT_EXPR_P (op))
10333 : {
10334 : r = op;
10335 : break;
10336 : }
10337 :
10338 246799041 : if (op == oldop && tcode != UNARY_PLUS_EXPR)
10339 : /* We didn't fold at the top so we could check for ptr-int
10340 : conversion. */
10341 19511523 : return fold (t);
10342 :
10343 227287518 : tree sop;
10344 :
10345 : /* Handle an array's bounds having been deduced after we built
10346 : the wrapping expression. */
10347 227287518 : if (same_type_ignoring_tlq_and_bounds_p (type, TREE_TYPE (op)))
10348 : r = op;
10349 76080974 : else if (sop = tree_strip_nop_conversions (op),
10350 106154029 : sop != op && (same_type_ignoring_tlq_and_bounds_p
10351 30073055 : (type, TREE_TYPE (sop))))
10352 : r = sop;
10353 62210237 : else if (tcode == UNARY_PLUS_EXPR)
10354 0 : r = fold_convert (TREE_TYPE (t), op);
10355 : else
10356 62210237 : r = fold_build1 (tcode, type, op);
10357 :
10358 : /* Conversion of an out-of-range value has implementation-defined
10359 : behavior; the language considers it different from arithmetic
10360 : overflow, which is undefined. */
10361 227287518 : if (TREE_OVERFLOW_P (r) && !TREE_OVERFLOW_P (op))
10362 12065 : TREE_OVERFLOW (r) = false;
10363 : }
10364 : break;
10365 :
10366 6514 : case EXCESS_PRECISION_EXPR:
10367 6514 : {
10368 6514 : tree oldop = TREE_OPERAND (t, 0);
10369 :
10370 6514 : tree op = cxx_eval_constant_expression (ctx, oldop,
10371 : lval,
10372 : non_constant_p, overflow_p,
10373 : jump_target);
10374 6514 : if (*jump_target)
10375 : return NULL_TREE;
10376 6514 : if (*non_constant_p)
10377 : return t;
10378 6510 : r = fold_convert (TREE_TYPE (t), op);
10379 6510 : break;
10380 : }
10381 :
10382 38771 : case EMPTY_CLASS_EXPR:
10383 : /* Handle EMPTY_CLASS_EXPR produced by build_call_a by lowering
10384 : it to an appropriate CONSTRUCTOR. */
10385 38771 : return build_constructor (TREE_TYPE (t), NULL);
10386 :
10387 30801256 : case STATEMENT_LIST:
10388 30801256 : new_ctx = *ctx;
10389 30801256 : new_ctx.ctor = new_ctx.object = NULL_TREE;
10390 30801256 : return cxx_eval_statement_list (&new_ctx, t,
10391 30801254 : non_constant_p, overflow_p, jump_target);
10392 :
10393 18670933 : case BIND_EXPR:
10394 : /* Pre-emptively clear the vars declared by this BIND_EXPR from the value
10395 : map, so that when checking whether they're already destroyed later we
10396 : don't get confused by remnants of previous calls. */
10397 27242728 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10398 8571795 : ctx->global->clear_value (decl);
10399 18670933 : r = cxx_eval_constant_expression (ctx, BIND_EXPR_BODY (t),
10400 : lval,
10401 : non_constant_p, overflow_p,
10402 : jump_target);
10403 27242726 : for (tree decl = BIND_EXPR_VARS (t); decl; decl = DECL_CHAIN (decl))
10404 8571794 : destroy_value_checked (ctx, decl, non_constant_p);
10405 : break;
10406 :
10407 4961017 : case PREINCREMENT_EXPR:
10408 4961017 : case POSTINCREMENT_EXPR:
10409 4961017 : case PREDECREMENT_EXPR:
10410 4961017 : case POSTDECREMENT_EXPR:
10411 4961017 : return cxx_eval_increment_expression (ctx, t,
10412 : lval, non_constant_p, overflow_p,
10413 4961017 : jump_target);
10414 :
10415 1115 : case THROW_EXPR:
10416 1115 : if (cxx_dialect >= cxx26)
10417 788 : return cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval,
10418 : non_constant_p, overflow_p,
10419 788 : jump_target);
10420 : /* FALLTHROUGH */
10421 335 : case LAMBDA_EXPR:
10422 335 : case NEW_EXPR:
10423 335 : case VEC_NEW_EXPR:
10424 335 : case DELETE_EXPR:
10425 335 : case VEC_DELETE_EXPR:
10426 335 : case MODOP_EXPR:
10427 : /* GCC internal stuff. */
10428 335 : case VA_ARG_EXPR:
10429 335 : case BASELINK:
10430 335 : case OFFSET_REF:
10431 335 : if (!ctx->quiet)
10432 86 : error_at (loc, "expression %qE is not a constant expression", t);
10433 335 : *non_constant_p = true;
10434 335 : break;
10435 :
10436 207202 : case OBJ_TYPE_REF:
10437 : /* Virtual function lookup. We don't need to do anything fancy. */
10438 207202 : return cxx_eval_constant_expression (ctx, OBJ_TYPE_REF_EXPR (t),
10439 : lval, non_constant_p, overflow_p,
10440 207202 : jump_target);
10441 :
10442 16116 : case PLACEHOLDER_EXPR:
10443 : /* Use of the value or address of the current object. */
10444 16116 : if (tree ctor = lookup_placeholder (ctx, lval, TREE_TYPE (t)))
10445 : {
10446 15885 : if (TREE_CODE (ctor) == CONSTRUCTOR)
10447 : return ctor;
10448 : else
10449 15585 : return cxx_eval_constant_expression (ctx, ctor, lval,
10450 : non_constant_p, overflow_p,
10451 15585 : jump_target);
10452 : }
10453 : /* A placeholder without a referent. We can get here when
10454 : checking whether NSDMIs are noexcept, or in massage_init_elt;
10455 : just say it's non-constant for now. */
10456 231 : gcc_assert (ctx->quiet);
10457 231 : *non_constant_p = true;
10458 231 : break;
10459 :
10460 3319 : case EXIT_EXPR:
10461 3319 : {
10462 3319 : tree cond = TREE_OPERAND (t, 0);
10463 3319 : cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
10464 : non_constant_p, overflow_p,
10465 : jump_target);
10466 3319 : if (*jump_target)
10467 : return NULL_TREE;
10468 3319 : VERIFY_CONSTANT (cond);
10469 3319 : if (integer_nonzerop (cond))
10470 1600 : *jump_target = t;
10471 : }
10472 : break;
10473 :
10474 21 : case GOTO_EXPR:
10475 21 : if (breaks (&TREE_OPERAND (t, 0))
10476 21 : || continues (&TREE_OPERAND (t, 0)))
10477 9 : *jump_target = TREE_OPERAND (t, 0);
10478 : else
10479 : {
10480 12 : if (!ctx->quiet)
10481 1 : error_at (loc, "%<goto%> is not a constant expression");
10482 12 : *non_constant_p = true;
10483 : }
10484 : break;
10485 :
10486 3296727 : case LOOP_EXPR:
10487 3296727 : case DO_STMT:
10488 3296727 : case WHILE_STMT:
10489 3296727 : case FOR_STMT:
10490 3296727 : cxx_eval_loop_expr (ctx, t,
10491 : non_constant_p, overflow_p, jump_target);
10492 3296727 : break;
10493 :
10494 30977 : case SWITCH_EXPR:
10495 30977 : case SWITCH_STMT:
10496 30977 : cxx_eval_switch_expr (ctx, t,
10497 : non_constant_p, overflow_p, jump_target);
10498 30977 : break;
10499 :
10500 120 : case REQUIRES_EXPR:
10501 : /* It's possible to get a requires-expression in a constant
10502 : expression. For example:
10503 :
10504 : template<typename T> concept bool C() {
10505 : return requires (T t) { t; };
10506 : }
10507 :
10508 : template<typename T> requires !C<T>() void f(T);
10509 :
10510 : Normalization leaves f with the associated constraint
10511 : '!requires (T t) { ... }' which is not transformed into
10512 : a constraint. */
10513 120 : if (!processing_template_decl)
10514 120 : return evaluate_requires_expr (t);
10515 : else
10516 0 : *non_constant_p = true;
10517 0 : return t;
10518 :
10519 16266 : case ANNOTATE_EXPR:
10520 16266 : r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0),
10521 : lval,
10522 : non_constant_p, overflow_p,
10523 : jump_target);
10524 16266 : break;
10525 :
10526 11646 : case USING_STMT:
10527 11646 : r = void_node;
10528 11646 : break;
10529 :
10530 20 : case ASSERTION_STMT:
10531 20 : case PRECONDITION_STMT:
10532 20 : case POSTCONDITION_STMT:
10533 20 : {
10534 20 : r = void_node;
10535 : /* Only record the first fail, and do not go further is the semantic
10536 : is 'ignore'. */
10537 20 : if (*non_constant_p || ctx->global->contract_statement
10538 40 : || contract_ignored_p (t))
10539 : break;
10540 :
10541 20 : tree cond = CONTRACT_CONDITION (t);
10542 20 : if (!potential_rvalue_constant_expression (cond))
10543 : {
10544 4 : ctx->global->contract_statement = t;
10545 4 : ctx->global->contract_condition_non_const = true;
10546 4 : break;
10547 : }
10548 :
10549 : /* We need to evaluate and stash the result of this here, since whether
10550 : it needs to be reported (and how) depends on whether the containing
10551 : expression is otherwise const. */
10552 16 : bool ctrct_non_const_p = false;
10553 16 : bool ctrct_overflow_p = false;
10554 16 : tree jmp_target = NULL_TREE;
10555 16 : constexpr_ctx new_ctx = *ctx;
10556 16 : new_ctx.quiet = true;
10557 : /* Avoid modification of existing values. */
10558 16 : modifiable_tracker ms (new_ctx.global);
10559 16 : tree eval =
10560 16 : cxx_eval_constant_expression (&new_ctx, cond, vc_prvalue,
10561 : &ctrct_non_const_p,
10562 : &ctrct_overflow_p, &jmp_target);
10563 : /* Not a constant. */
10564 16 : if (ctrct_non_const_p)
10565 : {
10566 0 : ctx->global->contract_statement = t;
10567 0 : ctx->global->contract_condition_non_const = true;
10568 0 : break;
10569 : }
10570 : /* Constant, but check failed. */
10571 16 : if (integer_zerop (eval))
10572 16 : ctx->global->contract_statement = t;
10573 16 : }
10574 16 : break;
10575 :
10576 2414991 : case TEMPLATE_ID_EXPR:
10577 2414991 : {
10578 : /* We can evaluate template-id that refers to a concept only if
10579 : the template arguments are non-dependent. */
10580 2414991 : gcc_assert (concept_check_p (t));
10581 :
10582 2414991 : if (!value_dependent_expression_p (t)
10583 2414991 : && !uid_sensitive_constexpr_evaluation_p ())
10584 2414552 : r = evaluate_concept_check (t);
10585 : else
10586 439 : *non_constant_p = true;
10587 :
10588 : break;
10589 : }
10590 :
10591 51 : case ASM_EXPR:
10592 51 : if (!ctx->quiet)
10593 29 : inline_asm_in_constexpr_error (loc, /*constexpr_fundef_p*/false);
10594 51 : *non_constant_p = true;
10595 51 : return t;
10596 :
10597 1125 : case BIT_CAST_EXPR:
10598 1125 : if (lval)
10599 : {
10600 0 : if (!ctx->quiet)
10601 0 : error_at (EXPR_LOCATION (t),
10602 : "address of a call to %qs is not a constant expression",
10603 : "__builtin_bit_cast");
10604 0 : *non_constant_p = true;
10605 0 : return t;
10606 : }
10607 1125 : r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p, jump_target);
10608 1125 : break;
10609 :
10610 0 : case OMP_PARALLEL:
10611 0 : case OMP_TASK:
10612 0 : case OMP_FOR:
10613 0 : case OMP_SIMD:
10614 0 : case OMP_DISTRIBUTE:
10615 0 : case OMP_TASKLOOP:
10616 0 : case OMP_LOOP:
10617 0 : case OMP_TEAMS:
10618 0 : case OMP_TARGET_DATA:
10619 0 : case OMP_TARGET:
10620 0 : case OMP_SECTIONS:
10621 0 : case OMP_ORDERED:
10622 0 : case OMP_CRITICAL:
10623 0 : case OMP_SINGLE:
10624 0 : case OMP_SCAN:
10625 0 : case OMP_SCOPE:
10626 0 : case OMP_SECTION:
10627 0 : case OMP_STRUCTURED_BLOCK:
10628 0 : case OMP_MASTER:
10629 0 : case OMP_MASKED:
10630 0 : case OMP_TASKGROUP:
10631 0 : case OMP_TARGET_UPDATE:
10632 0 : case OMP_TARGET_ENTER_DATA:
10633 0 : case OMP_TARGET_EXIT_DATA:
10634 0 : case OMP_ATOMIC:
10635 0 : case OMP_ATOMIC_READ:
10636 0 : case OMP_ATOMIC_CAPTURE_OLD:
10637 0 : case OMP_ATOMIC_CAPTURE_NEW:
10638 0 : case OMP_DEPOBJ:
10639 0 : case OACC_PARALLEL:
10640 0 : case OACC_KERNELS:
10641 0 : case OACC_SERIAL:
10642 0 : case OACC_DATA:
10643 0 : case OACC_HOST_DATA:
10644 0 : case OACC_LOOP:
10645 0 : case OACC_CACHE:
10646 0 : case OACC_DECLARE:
10647 0 : case OACC_ENTER_DATA:
10648 0 : case OACC_EXIT_DATA:
10649 0 : case OACC_UPDATE:
10650 0 : if (!ctx->quiet)
10651 0 : error_at (EXPR_LOCATION (t),
10652 : "statement is not a constant expression");
10653 0 : *non_constant_p = true;
10654 0 : break;
10655 :
10656 0 : default:
10657 0 : if (STATEMENT_CODE_P (TREE_CODE (t)))
10658 : {
10659 : /* This function doesn't know how to deal with pre-genericize
10660 : statements; this can only happen with statement-expressions,
10661 : so for now just fail. */
10662 0 : if (!ctx->quiet)
10663 0 : error_at (EXPR_LOCATION (t),
10664 : "statement is not a constant expression");
10665 : }
10666 0 : else if (flag_checking)
10667 0 : internal_error ("unexpected expression %qE of kind %s", t,
10668 : get_tree_code_name (TREE_CODE (t)));
10669 0 : *non_constant_p = true;
10670 0 : break;
10671 : }
10672 :
10673 1260588857 : if (r == error_mark_node)
10674 12026260 : *non_constant_p = true;
10675 :
10676 72785324 : if (r == void_node && lval != vc_discard && !*jump_target
10677 1260601112 : && !VOID_TYPE_P (TREE_TYPE (t)))
10678 : {
10679 : /* For diagnostic quality we should have handled this sooner, where we
10680 : can be more specific about the out-of-lifetime object. But here we
10681 : can still be correct. */
10682 1 : gcc_checking_assert (false);
10683 : if (!ctx->quiet)
10684 : error_at (EXPR_LOCATION (t),
10685 : "%qE accesses an object outside its lifetime", t);
10686 : *non_constant_p = true;
10687 : }
10688 :
10689 1260588856 : if (*non_constant_p)
10690 327221132 : return t;
10691 : else
10692 : return r;
10693 : }
10694 :
10695 : /* P0859: A function is needed for constant evaluation if it is a constexpr
10696 : function that is named by an expression ([basic.def.odr]) that is
10697 : potentially constant evaluated.
10698 :
10699 : So we need to instantiate any constexpr functions mentioned by the
10700 : expression even if the definition isn't needed for evaluating the
10701 : expression. */
10702 :
10703 : static tree
10704 550801171 : instantiate_cx_fn_r (tree *tp, int *walk_subtrees, void */*data*/)
10705 : {
10706 550801171 : if (TREE_CODE (*tp) == FUNCTION_DECL
10707 11519981 : && DECL_DECLARED_CONSTEXPR_P (*tp)
10708 11395018 : && !DECL_INITIAL (*tp)
10709 2882641 : && !trivial_fn_p (*tp)
10710 2882621 : && (DECL_TEMPLOID_INSTANTIATION (*tp) || DECL_DEFAULTED_FN (*tp))
10711 550801171 : && !uid_sensitive_constexpr_evaluation_p ())
10712 : {
10713 2868452 : ++function_depth;
10714 2868452 : if (DECL_TEMPLOID_INSTANTIATION (*tp))
10715 2868444 : instantiate_decl (*tp, /*defer_ok*/false, /*expl_inst*/false);
10716 : else
10717 8 : synthesize_method (*tp);
10718 2868452 : --function_depth;
10719 : }
10720 547932719 : else if (TREE_CODE (*tp) == CALL_EXPR
10721 536886838 : || TREE_CODE (*tp) == AGGR_INIT_EXPR)
10722 : {
10723 11582212 : if (EXPR_HAS_LOCATION (*tp))
10724 11578894 : input_location = EXPR_LOCATION (*tp);
10725 : }
10726 :
10727 550801171 : if (!EXPR_P (*tp))
10728 311418125 : *walk_subtrees = 0;
10729 :
10730 550801171 : return NULL_TREE;
10731 : }
10732 :
10733 : static void
10734 264749579 : instantiate_constexpr_fns (tree t)
10735 : {
10736 264749579 : location_t loc = input_location;
10737 264749579 : cp_walk_tree_without_duplicates (&t, instantiate_cx_fn_r, NULL);
10738 264749579 : input_location = loc;
10739 264749579 : }
10740 :
10741 : /* Look for heap variables in the expression *TP. */
10742 :
10743 : static tree
10744 324037 : find_heap_var_refs (tree *tp, int *walk_subtrees, void */*data*/)
10745 : {
10746 324037 : if (VAR_P (*tp)
10747 324037 : && (DECL_NAME (*tp) == heap_uninit_identifier
10748 5691 : || DECL_NAME (*tp) == heap_identifier
10749 2423 : || DECL_NAME (*tp) == heap_vec_uninit_identifier
10750 1718 : || DECL_NAME (*tp) == heap_vec_identifier
10751 554 : || DECL_NAME (*tp) == heap_deleted_identifier))
10752 : return *tp;
10753 :
10754 223738 : if (TYPE_P (*tp))
10755 83 : *walk_subtrees = 0;
10756 : return NULL_TREE;
10757 : }
10758 :
10759 : /* Find immediate function decls in *TP if any. */
10760 :
10761 : static tree
10762 567343246 : find_immediate_fndecl (tree *tp, int *walk_subtrees, void */*data*/)
10763 : {
10764 571028205 : if (TREE_CODE (*tp) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (*tp))
10765 : return *tp;
10766 567338412 : if (TREE_CODE (*tp) == PTRMEM_CST
10767 28757 : && TREE_CODE (PTRMEM_CST_MEMBER (*tp)) == FUNCTION_DECL
10768 567366316 : && DECL_IMMEDIATE_FUNCTION_P (PTRMEM_CST_MEMBER (*tp)))
10769 : return PTRMEM_CST_MEMBER (*tp);
10770 567338307 : if (REFLECT_EXPR_P (*tp))
10771 49553 : *walk_subtrees = 0;
10772 : return NULL_TREE;
10773 : }
10774 :
10775 : /* T has TREE_CONSTANT set but has been deemed not a valid C++ constant
10776 : expression. Return a version of T that has TREE_CONSTANT cleared. */
10777 :
10778 : static tree
10779 142118 : mark_non_constant (tree t)
10780 : {
10781 142118 : gcc_checking_assert (TREE_CONSTANT (t));
10782 :
10783 : /* This isn't actually constant, so unset TREE_CONSTANT.
10784 : Don't clear TREE_CONSTANT on ADDR_EXPR, as the middle-end requires
10785 : it to be set if it is invariant address, even when it is not
10786 : a valid C++ constant expression. Wrap it with a NOP_EXPR
10787 : instead. */
10788 142118 : if (EXPR_P (t) && TREE_CODE (t) != ADDR_EXPR)
10789 140471 : t = copy_node (t);
10790 1647 : else if (TREE_CODE (t) == CONSTRUCTOR)
10791 233 : t = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (t), t);
10792 : else
10793 1414 : t = build_nop (TREE_TYPE (t), t);
10794 142118 : TREE_CONSTANT (t) = false;
10795 142118 : return t;
10796 : }
10797 :
10798 : /* If we have a successful constant evaluation, now check whether there is
10799 : a failed or non-constant contract that would invalidate this. */
10800 :
10801 : static bool
10802 500368811 : check_for_failed_contracts (constexpr_ctx *ctx)
10803 : {
10804 500368811 : constexpr_global_ctx *const global_ctx = ctx->global;
10805 500368811 : if (!flag_contracts || !global_ctx->contract_statement)
10806 : return false;
10807 :
10808 20 : location_t loc = EXPR_LOCATION (global_ctx->contract_statement);
10809 20 : enum diagnostics::kind kind;
10810 20 : bool error = false;
10811 : /* [intro.compliance.general]/2.3.4. */
10812 : /* [basic.contract.eval]/8. */
10813 20 : if (ctx->manifestly_const_eval != mce_true)
10814 : {
10815 : /* When !MCE, silently return not constant. */
10816 : return true;
10817 : }
10818 18 : else if (contract_terminating_p (global_ctx->contract_statement))
10819 : {
10820 : kind = diagnostics::kind::error;
10821 : error = true;
10822 : }
10823 : else
10824 4 : kind = diagnostics::kind::warning;
10825 :
10826 : /* [basic.contract.eval]/7.3 */
10827 18 : if (global_ctx->contract_condition_non_const)
10828 : {
10829 4 : emit_diagnostic (kind, loc, 0, "contract condition is not constant");
10830 4 : return error;
10831 : }
10832 :
10833 : /* Otherwise, the evaluation was const, but determined to be false. */
10834 14 : emit_diagnostic (kind, loc, 0,
10835 : "contract predicate is false in constant expression");
10836 14 : return error;
10837 : }
10838 :
10839 : /* ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
10840 : STRICT has the same sense as for constant_value_1: true if we only allow
10841 : conforming C++ constant expressions, or false if we want a constant value
10842 : even if it doesn't conform.
10843 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
10844 : per P0595 even when ALLOW_NON_CONSTANT is true.
10845 : CONSTEXPR_DTOR is true when evaluating the dtor of a constexpr variable.
10846 : OBJECT must be non-NULL in that case. */
10847 :
10848 : static tree
10849 509355384 : cxx_eval_outermost_constant_expr (tree t, bool allow_non_constant,
10850 : bool strict = true,
10851 : mce_value manifestly_const_eval = mce_unknown,
10852 : bool constexpr_dtor = false,
10853 : tree object = NULL_TREE)
10854 : {
10855 509355384 : auto_timevar time (TV_CONSTEXPR);
10856 :
10857 509355384 : bool non_constant_p = false;
10858 509355384 : bool overflow_p = false;
10859 :
10860 509355384 : if (BRACE_ENCLOSED_INITIALIZER_P (t))
10861 : {
10862 0 : gcc_checking_assert (allow_non_constant);
10863 : return t;
10864 : }
10865 :
10866 509355384 : constexpr_global_ctx global_ctx;
10867 509355384 : constexpr_ctx ctx = { &global_ctx, NULL, NULL, NULL, NULL, NULL, NULL,
10868 : allow_non_constant, strict,
10869 509355384 : !allow_non_constant ? mce_true : manifestly_const_eval };
10870 :
10871 : /* Turn off -frounding-math for manifestly constant evaluation. */
10872 509355384 : warning_sentinel rm (flag_rounding_math,
10873 509355384 : ctx.manifestly_const_eval == mce_true);
10874 509355384 : tree type = (object
10875 509355384 : ? cv_unqualified (TREE_TYPE (object))
10876 468774705 : : initialized_type (t));
10877 509355384 : tree r = t;
10878 509355384 : bool is_consteval = false;
10879 509355384 : if (VOID_TYPE_P (type))
10880 : {
10881 8980158 : if (!constexpr_dtor)
10882 : {
10883 8980158 : if (cxx_dialect < cxx20)
10884 : return t;
10885 : /* We could have a COMPOUND_EXPR here coming from
10886 : keep_unused_object_arg. */
10887 8969156 : tree x = extract_call_expr (t);
10888 8969156 : if (x == NULL_TREE || x == error_mark_node)
10889 : return t;
10890 : /* Calls to immediate functions returning void need to be
10891 : evaluated. */
10892 8969116 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10893 17938232 : if (fndecl == NULL_TREE || !DECL_IMMEDIATE_FUNCTION_P (fndecl))
10894 : return t;
10895 : else
10896 582 : is_consteval = true;
10897 582 : tree lam;
10898 582 : if (manifestly_const_eval == mce_true
10899 841 : && LAMBDA_FUNCTION_P (fndecl)
10900 253 : && (lam = CLASSTYPE_LAMBDA_EXPR (CP_DECL_CONTEXT (fndecl)))
10901 835 : && LAMBDA_EXPR_CONSTEVAL_BLOCK_P (lam))
10902 243 : global_ctx.consteval_block = fndecl;
10903 : }
10904 : }
10905 500375226 : else if (cxx_dialect >= cxx20
10906 492224350 : && (TREE_CODE (t) == CALL_EXPR
10907 426792222 : || TREE_CODE (t) == AGGR_INIT_EXPR
10908 422656636 : || TREE_CODE (t) == TARGET_EXPR))
10909 : {
10910 74287258 : tree x = t;
10911 74287258 : if (TREE_CODE (x) == TARGET_EXPR)
10912 4719544 : x = TARGET_EXPR_INITIAL (x);
10913 74287258 : tree fndecl = cp_get_callee_fndecl_nofold (x);
10914 146299356 : if (fndecl && DECL_IMMEDIATE_FUNCTION_P (fndecl))
10915 : is_consteval = true;
10916 : /* Don't try to evaluate a std::vector constructor taking an integer, it
10917 : will fail in the 'if (heap_var)' block below after doing all the work
10918 : (c++/113835). This will need adjustment if P3554 is accepted. Note
10919 : that evaluation of e.g. the vector default constructor can succeed, so
10920 : we don't shortcut all vector constructors. */
10921 144024196 : if (fndecl && DECL_CONSTRUCTOR_P (fndecl) && allow_non_constant
10922 10028158 : && is_std_class (type, "vector") && call_expr_nargs (x) > 1
10923 74308324 : && TREE_CODE (TREE_TYPE (get_nth_callarg (x, 1))) == INTEGER_TYPE)
10924 : return t;
10925 : }
10926 500374456 : if (AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type))
10927 : {
10928 : /* In C++14 an NSDMI can participate in aggregate initialization,
10929 : and can refer to the address of the object being initialized, so
10930 : we need to pass in the relevant VAR_DECL if we want to do the
10931 : evaluation in a single pass. The evaluation will dynamically
10932 : update ctx.values for the VAR_DECL. We use the same strategy
10933 : for C++11 constexpr constructors that refer to the object being
10934 : initialized. */
10935 38188033 : if (constexpr_dtor)
10936 : {
10937 161 : gcc_assert (object && VAR_P (object));
10938 161 : gcc_assert (DECL_DECLARED_CONSTEXPR_P (object));
10939 161 : gcc_assert (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object));
10940 161 : if (error_operand_p (DECL_INITIAL (object)))
10941 : return t;
10942 149 : ctx.ctor = unshare_expr (DECL_INITIAL (object));
10943 149 : TREE_READONLY (ctx.ctor) = false;
10944 : /* Temporarily force decl_really_constant_value to return false
10945 : for it, we want to use ctx.ctor for the current value instead. */
10946 149 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = false;
10947 : }
10948 : else
10949 : {
10950 38187872 : ctx.ctor = build_constructor (type, NULL);
10951 38187872 : CONSTRUCTOR_NO_CLEARING (ctx.ctor) = true;
10952 : }
10953 38188021 : if (!object)
10954 : {
10955 19562922 : if (TREE_CODE (t) == CALL_EXPR)
10956 : {
10957 : /* If T is calling a constructor to initialize an object, reframe
10958 : it as an AGGR_INIT_EXPR to avoid trying to modify an object
10959 : from outside the constant evaluation, which will fail even if
10960 : the value is actually constant (is_constant_evaluated3.C). */
10961 8877966 : tree fn = cp_get_callee_fndecl_nofold (t);
10962 17755836 : if (fn && DECL_CONSTRUCTOR_P (fn))
10963 : {
10964 3758653 : object = CALL_EXPR_ARG (t, 0);
10965 3758653 : object = build_fold_indirect_ref (object);
10966 3758653 : r = build_aggr_init_expr (type, r);
10967 : }
10968 : }
10969 10684956 : else if (TREE_CODE (t) == TARGET_EXPR)
10970 4631397 : object = TARGET_EXPR_SLOT (t);
10971 6053559 : else if (TREE_CODE (t) == AGGR_INIT_EXPR)
10972 673653 : object = AGGR_INIT_EXPR_SLOT (t);
10973 : }
10974 38188021 : ctx.object = object;
10975 38188021 : if (object)
10976 27688802 : gcc_assert (same_type_ignoring_top_level_qualifiers_p
10977 : (type, TREE_TYPE (object)));
10978 27688802 : if (object && DECL_P (object))
10979 23610305 : global_ctx.put_value (object, ctx.ctor);
10980 38188021 : if (TREE_CODE (r) == TARGET_EXPR)
10981 : /* Avoid creating another CONSTRUCTOR when we expand the
10982 : TARGET_EXPR. */
10983 4755686 : r = TARGET_EXPR_INITIAL (r);
10984 : }
10985 :
10986 : /* uid_sensitive_constexpr_evaluation_value restricts warning-dependent
10987 : constexpr evaluation to avoid unnecessary template instantiation, and is
10988 : always done with mce_unknown. But due to gaps in the restriction logic
10989 : we may still end up taking an evaluation path that in turn requires
10990 : manifestly constant evaluation, and such evaluation must not be
10991 : restricted since it likely has semantic consequences.
10992 : TODO: Remove/replace the mechanism in GCC 17. */
10993 500374444 : auto uids = make_temp_override (uid_sensitive_constexpr_evaluation_value);
10994 500374444 : if (ctx.manifestly_const_eval == mce_true)
10995 264749579 : uid_sensitive_constexpr_evaluation_value = false;
10996 :
10997 500374444 : auto_vec<tree, 16> cleanups;
10998 500374444 : global_ctx.cleanups = &cleanups;
10999 :
11000 500374444 : if (manifestly_const_eval == mce_true)
11001 264749579 : instantiate_constexpr_fns (r);
11002 500374444 : tree jmp_target = NULL_TREE;
11003 500374444 : r = cxx_eval_constant_expression (&ctx, r, vc_prvalue,
11004 : &non_constant_p, &overflow_p,
11005 : &jmp_target);
11006 500372480 : if (throws (&jmp_target) && !non_constant_p)
11007 : {
11008 734 : if (!ctx.quiet)
11009 216 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11010 734 : non_constant_p = true;
11011 734 : jmp_target = NULL_TREE;
11012 734 : r = t;
11013 : }
11014 500371012 : else if (!non_constant_p && jmp_target)
11015 : {
11016 24 : non_constant_p = true;
11017 24 : if (!ctx.quiet)
11018 : {
11019 3 : if (breaks (&jmp_target))
11020 3 : error ("%<break%> outside of a loop or %<switch%>");
11021 0 : else if (continues (&jmp_target))
11022 0 : error ("%<continue%> outside of a loop");
11023 0 : else if (returns (&jmp_target))
11024 0 : error ("%<return%> in a statement expression");
11025 : else
11026 0 : gcc_unreachable ();
11027 : }
11028 24 : r = t;
11029 : }
11030 :
11031 : /* If we got a non-simple TARGET_EXPR, the initializer was a sequence
11032 : of statements, and the result ought to be stored in ctx.ctor. */
11033 500371746 : if (r == void_node && !constexpr_dtor && ctx.ctor)
11034 0 : r = ctx.ctor;
11035 :
11036 500371746 : unsigned int i;
11037 500371746 : tree cleanup;
11038 500371746 : jmp_target = NULL_TREE;
11039 : /* Evaluate the cleanups. */
11040 1000823045 : FOR_EACH_VEC_ELT_REVERSE (cleanups, i, cleanup)
11041 79553 : if (cleanup == NULL_TREE)
11042 : /* NULL_TREE cleanup is a marker that before it is
11043 : CLEANUP_EH_ONLY cleanup. Skip the cleanup before it. */
11044 23 : --i;
11045 : else
11046 79530 : cxx_eval_constant_expression (&ctx, cleanup, vc_discard,
11047 : &non_constant_p, &overflow_p,
11048 : &jmp_target);
11049 500371746 : if (throws (&jmp_target) && !non_constant_p)
11050 : {
11051 0 : if (!ctx.quiet)
11052 0 : diagnose_uncaught_exception (input_location, &ctx, jmp_target);
11053 0 : non_constant_p = true;
11054 0 : r = t;
11055 : }
11056 :
11057 : /* Mutable logic is a bit tricky: we want to allow initialization of
11058 : constexpr variables with mutable members, but we can't copy those
11059 : members to another constexpr variable. */
11060 500371746 : if (!non_constant_p
11061 380980029 : && TREE_CODE (r) == CONSTRUCTOR
11062 515322433 : && CONSTRUCTOR_MUTABLE_POISON (r))
11063 : {
11064 89 : if (!allow_non_constant)
11065 6 : error ("%qE is not a constant expression because it refers to "
11066 : "mutable subobjects of %qT", t, type);
11067 89 : non_constant_p = true;
11068 : }
11069 :
11070 380979940 : if (!non_constant_p && cxx_dialect >= cxx20
11071 881351686 : && !global_ctx.heap_vars.is_empty ())
11072 : {
11073 105653 : tree heap_var = cp_walk_tree_without_duplicates (&r, find_heap_var_refs,
11074 : NULL);
11075 105653 : unsigned int i;
11076 105653 : if (heap_var)
11077 : {
11078 100299 : if (!allow_non_constant && !non_constant_p)
11079 : {
11080 12 : if (DECL_LANG_SPECIFIC (heap_var))
11081 2 : error ("%qE is not a constant expression because it refers to "
11082 : "exception object allocated with "
11083 : "%<__cxa_allocate_exception%>", t);
11084 : else
11085 10 : error ("%qE is not a constant expression because it refers to "
11086 : "a result of %<operator new%>", t);
11087 12 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11088 : }
11089 100299 : r = t;
11090 100299 : non_constant_p = true;
11091 : }
11092 247650 : FOR_EACH_VEC_ELT (global_ctx.heap_vars, i, heap_var)
11093 : {
11094 141997 : if (DECL_NAME (heap_var) != heap_deleted_identifier)
11095 : {
11096 100353 : if (!allow_non_constant && !non_constant_p)
11097 : {
11098 16 : error ("%qE is not a constant expression because allocated "
11099 : "storage has not been deallocated", t);
11100 16 : inform (DECL_SOURCE_LOCATION (heap_var), "allocated here");
11101 : }
11102 100353 : r = t;
11103 100353 : non_constant_p = true;
11104 : }
11105 : }
11106 : }
11107 :
11108 : /* Check that immediate invocation does not return an expression referencing
11109 : any immediate function decls. */
11110 500371746 : if (!non_constant_p && cxx_dialect >= cxx20)
11111 750442048 : if (tree immediate_fndecl
11112 375221024 : = cp_walk_tree_without_duplicates (&r, find_immediate_fndecl,
11113 : NULL))
11114 : {
11115 4939 : if (!allow_non_constant && !non_constant_p)
11116 : {
11117 73 : if (is_consteval)
11118 36 : error_at (cp_expr_loc_or_input_loc (t),
11119 : "immediate evaluation returns address of immediate "
11120 : "function %qD", immediate_fndecl);
11121 : else
11122 56 : error_at (cp_expr_loc_or_input_loc (t),
11123 : "constant evaluation returns address of immediate "
11124 : "function %qD", immediate_fndecl);
11125 : }
11126 4939 : r = t;
11127 4939 : non_constant_p = true;
11128 : }
11129 :
11130 : /* Detect consteval-only smuggling: turning a consteval-only object
11131 : into one that is not. For instance, in
11132 : struct B { };
11133 : struct D : B { info r; };
11134 : constexpr D d{^^::};
11135 : constexpr const B &b = d; // #1
11136 : #1 is wrong because D is a consteval-only type but B is not. */
11137 500371746 : if (flag_reflection
11138 5149563 : && !non_constant_p
11139 4164317 : && object
11140 473058 : && POINTER_TYPE_P (TREE_TYPE (object))
11141 858 : && !consteval_only_p (object)
11142 500372547 : && check_out_of_consteval_use (r, /*complain=*/false))
11143 : {
11144 15 : if (!allow_non_constant)
11145 : {
11146 5 : if (TYPE_REF_P (TREE_TYPE (object)))
11147 6 : error_at (cp_expr_loc_or_input_loc (t),
11148 : "reference into an object of consteval-only type is "
11149 : "not a constant expression unless it also has "
11150 : "consteval-only type");
11151 : else
11152 2 : error_at (cp_expr_loc_or_input_loc (t),
11153 : "pointer into an object of consteval-only type is "
11154 : "not a constant expression unless it also has "
11155 : "consteval-only type");
11156 : }
11157 15 : r = t;
11158 15 : non_constant_p = true;
11159 : }
11160 :
11161 500371746 : if (!non_constant_p && !constexpr_dtor)
11162 380874488 : verify_constant (r, allow_non_constant, &non_constant_p, &overflow_p);
11163 :
11164 : /* After verify_constant because reduced_constant_expression_p can unset
11165 : CONSTRUCTOR_NO_CLEARING. */
11166 500371746 : if (TREE_CODE (r) == CONSTRUCTOR && CONSTRUCTOR_NO_CLEARING (r))
11167 : {
11168 58960 : if (!allow_non_constant)
11169 43 : error ("%qE is not a constant expression because it refers to "
11170 : "an incompletely initialized variable", t);
11171 58960 : TREE_CONSTANT (r) = false;
11172 58960 : non_constant_p = true;
11173 : }
11174 :
11175 500371746 : if (non_constant_p)
11176 : /* If we saw something bad, go back to our argument. The wrapping below is
11177 : only for the cases of TREE_CONSTANT argument or overflow. */
11178 121470156 : r = t;
11179 :
11180 500371746 : if (!non_constant_p && overflow_p)
11181 215 : non_constant_p = true;
11182 :
11183 : /* Unshare the result. */
11184 500371746 : bool should_unshare = true;
11185 500371746 : if (r == t || (TREE_CODE (t) == TARGET_EXPR
11186 1350010 : && TARGET_EXPR_INITIAL (t) == r))
11187 : should_unshare = false;
11188 :
11189 500371746 : if (non_constant_p && !allow_non_constant)
11190 2935 : return error_mark_node;
11191 500368811 : else if (check_for_failed_contracts (&ctx))
11192 : {
11193 16 : if (manifestly_const_eval == mce_true)
11194 : /* If MCE, we gave a hard error and return error_mark_node. */
11195 14 : return error_mark_node;
11196 : else
11197 : {
11198 : /* Otherwise treat it as non-constant so the violation is still
11199 : detectable at run-time. */
11200 2 : gcc_checking_assert (allow_non_constant);
11201 : return t;
11202 : }
11203 : }
11204 500368795 : else if (non_constant_p && TREE_CONSTANT (r))
11205 137628 : r = mark_non_constant (r);
11206 500231167 : else if (non_constant_p)
11207 : return t;
11208 :
11209 379038987 : if (constexpr_dtor)
11210 : {
11211 131 : DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (object) = true;
11212 131 : return r;
11213 : }
11214 :
11215 : /* Check we are not trying to return the wrong type. */
11216 379038856 : if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (r)))
11217 : {
11218 : /* If so, this is not a constant expression. */
11219 151 : if (!allow_non_constant)
11220 2 : error ("%qE is not a constant expression because it initializes "
11221 2 : "a %qT rather than %qT", t, TREE_TYPE (t), type);
11222 151 : return t;
11223 : }
11224 :
11225 379038705 : if (should_unshare)
11226 218514018 : r = unshare_expr (r);
11227 :
11228 379038705 : if (TREE_CODE (r) == CONSTRUCTOR && CLASS_TYPE_P (TREE_TYPE (r)))
11229 : {
11230 14139414 : r = adjust_temp_type (type, r);
11231 14139414 : if (TREE_CODE (t) == TARGET_EXPR
11232 14139414 : && TARGET_EXPR_INITIAL (t) == r)
11233 : return t;
11234 13241513 : else if (TREE_CODE (t) == CONSTRUCTOR || TREE_CODE (t) == CALL_EXPR
11235 2266667 : || TREE_CODE (t) == AGGR_INIT_EXPR)
11236 : /* Don't add a TARGET_EXPR if our argument didn't have one. */;
11237 522856 : else if (TREE_CODE (t) == TARGET_EXPR && TARGET_EXPR_CLEANUP (t))
11238 490 : r = get_target_expr (r);
11239 : else
11240 : {
11241 522366 : r = get_target_expr (r, tf_warning_or_error | tf_no_cleanup);
11242 522366 : TREE_CONSTANT (r) = true;
11243 : }
11244 : }
11245 :
11246 378140804 : if (TREE_CODE (t) == TARGET_EXPR
11247 452109 : && TREE_CODE (r) == TARGET_EXPR)
11248 : {
11249 : /* Preserve this flag for potential_constant_expression, and the others
11250 : for good measure. */
11251 452008 : TARGET_EXPR_ELIDING_P (r) = TARGET_EXPR_ELIDING_P (t);
11252 452008 : TARGET_EXPR_IMPLICIT_P (r) = TARGET_EXPR_IMPLICIT_P (t);
11253 452008 : TARGET_EXPR_LIST_INIT_P (r) = TARGET_EXPR_LIST_INIT_P (t);
11254 452008 : TARGET_EXPR_DIRECT_INIT_P (r) = TARGET_EXPR_DIRECT_INIT_P (t);
11255 : }
11256 :
11257 : /* Remember the original location if that wouldn't need a wrapper. */
11258 378140804 : if (location_t loc = EXPR_LOCATION (t))
11259 176641051 : protected_set_expr_location (r, loc);
11260 :
11261 378140804 : return r;
11262 509352686 : }
11263 :
11264 : /* If T represents a constant expression returns its reduced value.
11265 : Otherwise return error_mark_node. */
11266 :
11267 : tree
11268 140352254 : cxx_constant_value (tree t, tree decl /* = NULL_TREE */,
11269 : tsubst_flags_t complain /* = tf_error */)
11270 : {
11271 140352254 : bool sfinae = !(complain & tf_error);
11272 140352254 : tree r = cxx_eval_outermost_constant_expr (t, sfinae, true, mce_true, false, decl);
11273 140352254 : if (sfinae && !TREE_CONSTANT (r))
11274 1649 : r = error_mark_node;
11275 140352254 : return r;
11276 : }
11277 :
11278 : /* Like cxx_constant_value, but used for evaluation of constexpr destructors
11279 : of constexpr variables. The actual initializer of DECL is not modified. */
11280 :
11281 : void
11282 161 : cxx_constant_dtor (tree t, tree decl)
11283 : {
11284 161 : cxx_eval_outermost_constant_expr (t, false, true, mce_true, true, decl);
11285 161 : }
11286 :
11287 : /* Helper routine for fold_simple function. Either return simplified
11288 : expression T, otherwise NULL_TREE.
11289 : In contrast to cp_fully_fold, and to maybe_constant_value, we try to fold
11290 : even if we are within template-declaration. So be careful on call, as in
11291 : such case types can be undefined. */
11292 :
11293 : static tree
11294 155602962 : fold_simple_1 (tree t)
11295 : {
11296 155602962 : tree op1;
11297 155602962 : enum tree_code code = TREE_CODE (t);
11298 :
11299 155602962 : switch (code)
11300 : {
11301 : case INTEGER_CST:
11302 : case REAL_CST:
11303 : case VECTOR_CST:
11304 : case FIXED_CST:
11305 : case COMPLEX_CST:
11306 : return t;
11307 :
11308 1350436 : case SIZEOF_EXPR:
11309 1350436 : return fold_sizeof_expr (t);
11310 :
11311 35416427 : case ABS_EXPR:
11312 35416427 : case ABSU_EXPR:
11313 35416427 : case CONJ_EXPR:
11314 35416427 : case REALPART_EXPR:
11315 35416427 : case IMAGPART_EXPR:
11316 35416427 : case NEGATE_EXPR:
11317 35416427 : case BIT_NOT_EXPR:
11318 35416427 : case TRUTH_NOT_EXPR:
11319 35416427 : case VIEW_CONVERT_EXPR:
11320 35416427 : CASE_CONVERT:
11321 35416427 : case FLOAT_EXPR:
11322 35416427 : case FIX_TRUNC_EXPR:
11323 35416427 : case FIXED_CONVERT_EXPR:
11324 35416427 : case ADDR_SPACE_CONVERT_EXPR:
11325 :
11326 35416427 : op1 = TREE_OPERAND (t, 0);
11327 :
11328 35416427 : t = const_unop (code, TREE_TYPE (t), op1);
11329 35416427 : if (!t)
11330 : return NULL_TREE;
11331 :
11332 3938964 : if (CONVERT_EXPR_CODE_P (code)
11333 3938964 : && TREE_OVERFLOW_P (t) && !TREE_OVERFLOW_P (op1))
11334 0 : TREE_OVERFLOW (t) = false;
11335 : return t;
11336 :
11337 : default:
11338 : return NULL_TREE;
11339 : }
11340 : }
11341 :
11342 : /* If T is a simple constant expression, returns its simplified value.
11343 : Otherwise returns T. In contrast to maybe_constant_value we
11344 : simplify only few operations on constant-expressions, and we don't
11345 : try to simplify constexpressions. */
11346 :
11347 : tree
11348 156271320 : fold_simple (tree t)
11349 : {
11350 156271320 : if (processing_template_decl)
11351 : return t;
11352 :
11353 155602962 : tree r = fold_simple_1 (t);
11354 155602962 : if (r)
11355 : return r;
11356 :
11357 : return t;
11358 : }
11359 :
11360 : /* Try folding the expression T to a simple constant.
11361 : Returns that constant, otherwise returns T. */
11362 :
11363 : tree
11364 1541772 : fold_to_constant (tree t)
11365 : {
11366 1541772 : if (processing_template_decl)
11367 : return t;
11368 :
11369 1449097 : tree r = fold (t);
11370 1449097 : if (CONSTANT_CLASS_P (r) && !TREE_OVERFLOW (r))
11371 : return r;
11372 : else
11373 : return t;
11374 : }
11375 :
11376 : /* If T is a constant expression, returns its reduced value.
11377 : Otherwise, if T does not have TREE_CONSTANT set, returns T.
11378 : Otherwise, returns a version of T without TREE_CONSTANT.
11379 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated
11380 : as per P0595. */
11381 :
11382 : static GTY((deletable)) hash_map<tree, tree> *cv_cache;
11383 :
11384 : tree
11385 804939356 : maybe_constant_value (tree t, tree decl /* = NULL_TREE */,
11386 : mce_value manifestly_const_eval /* = mce_unknown */)
11387 : {
11388 804939356 : tree orig_t = t;
11389 804939356 : tree r;
11390 :
11391 804939356 : if (EXPR_P (t) && manifestly_const_eval == mce_unknown)
11392 : {
11393 : /* Look up each operand in the cv_cache first to see if we've already
11394 : reduced it, and reuse that result to avoid quadratic behavior if
11395 : we're called when building up a large expression. */
11396 231858634 : int n = cp_tree_operand_length (t);
11397 231858634 : tree *ops = XALLOCAVEC (tree, n);
11398 231858634 : bool rebuild = false;
11399 651041761 : for (int i = 0; i < n; ++i)
11400 : {
11401 419183127 : ops[i] = TREE_OPERAND (t, i);
11402 1256966940 : if (tree *cached = hash_map_safe_get (cv_cache, ops[i]))
11403 28791230 : if (*cached != ops[i])
11404 : {
11405 8933140 : ops[i] = *cached;
11406 8933140 : rebuild = true;
11407 : }
11408 : }
11409 231858634 : if (rebuild)
11410 : {
11411 7527635 : t = copy_node (t);
11412 24459723 : for (int i = 0; i < n; ++i)
11413 16932088 : TREE_OPERAND (t, i) = ops[i];
11414 : }
11415 : }
11416 :
11417 804939356 : if (!is_nondependent_constant_expression (t))
11418 : {
11419 0 : if (TREE_OVERFLOW_P (t)
11420 120527023 : || (!processing_template_decl && TREE_CONSTANT (t)))
11421 4490 : t = mark_non_constant (t);
11422 120527023 : return t;
11423 : }
11424 684412333 : else if (CONSTANT_CLASS_P (t))
11425 : /* No caching or evaluation needed. */
11426 : return t;
11427 :
11428 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11429 : but at least try folding it to a simple constant. */
11430 313307567 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11431 488075 : return fold_to_constant (t);
11432 :
11433 297418589 : if (manifestly_const_eval != mce_unknown)
11434 : /* TODO: Extend the cache to be mce_value aware. And if we have a
11435 : previously cached mce_unknown result that's TREE_CONSTANT, it means
11436 : the reduced value is independent of mce_value and so we should
11437 : be able to reuse it in the mce_true/false case. */
11438 163633142 : return cxx_eval_outermost_constant_expr (t, true, true,
11439 163630444 : manifestly_const_eval, false, decl);
11440 :
11441 149186350 : if (cv_cache == NULL)
11442 139835 : cv_cache = hash_map<tree, tree>::create_ggc (101);
11443 149186350 : if (tree *cached = cv_cache->get (t))
11444 : {
11445 12665710 : r = *cached;
11446 12665710 : if (r != t)
11447 : {
11448 : /* Clear processing_template_decl for sake of break_out_target_exprs;
11449 : entries in the cv_cache are non-templated. */
11450 4282979 : processing_template_decl_sentinel ptds;
11451 :
11452 4282979 : r = break_out_target_exprs (r, /*clear_loc*/true);
11453 4282979 : protected_set_expr_location (r, EXPR_LOCATION (t));
11454 4282979 : }
11455 12665710 : return r;
11456 : }
11457 :
11458 136520640 : uid_sensitive_constexpr_evaluation_checker c;
11459 136520640 : r = cxx_eval_outermost_constant_expr (t, true, true,
11460 : manifestly_const_eval, false, decl);
11461 136520640 : gcc_checking_assert (r == t
11462 : || CONVERT_EXPR_P (t)
11463 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11464 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11465 : || !cp_tree_equal (r, t));
11466 136520640 : if (!c.evaluation_restricted_p ())
11467 136268983 : cv_cache->put (orig_t, r);
11468 : return r;
11469 : }
11470 :
11471 : /* Dispose of the whole CV_CACHE. */
11472 :
11473 : static void
11474 38349338 : clear_cv_cache (void)
11475 : {
11476 38349338 : if (cv_cache != NULL)
11477 38114501 : cv_cache->empty ();
11478 38349338 : }
11479 :
11480 : /* Dispose of the whole CV_CACHE and FOLD_CACHE. */
11481 :
11482 : void
11483 38349338 : clear_cv_and_fold_caches ()
11484 : {
11485 38349338 : clear_cv_cache ();
11486 38349338 : clear_fold_cache ();
11487 38349338 : }
11488 :
11489 : /* Internal function handling expressions in templates for
11490 : fold_non_dependent_expr and fold_non_dependent_init.
11491 :
11492 : If we're in a template, but T isn't value dependent, simplify
11493 : it. We're supposed to treat:
11494 :
11495 : template <typename T> void f(T[1 + 1]);
11496 : template <typename T> void f(T[2]);
11497 :
11498 : as two declarations of the same function, for example. */
11499 :
11500 : static tree
11501 29375251 : fold_non_dependent_expr_template (tree t, tsubst_flags_t complain,
11502 : bool manifestly_const_eval,
11503 : tree object)
11504 : {
11505 29375251 : gcc_assert (processing_template_decl);
11506 :
11507 29375251 : if (is_nondependent_constant_expression (t))
11508 : {
11509 16179885 : processing_template_decl_sentinel s;
11510 16179885 : t = instantiate_non_dependent_expr_internal (t, complain);
11511 :
11512 16179885 : if (type_unknown_p (t) || BRACE_ENCLOSED_INITIALIZER_P (t))
11513 : {
11514 0 : if (TREE_OVERFLOW_P (t))
11515 : {
11516 0 : t = build_nop (TREE_TYPE (t), t);
11517 0 : TREE_CONSTANT (t) = false;
11518 : }
11519 0 : return t;
11520 : }
11521 16179885 : else if (CONSTANT_CLASS_P (t))
11522 : /* No evaluation needed. */
11523 : return t;
11524 :
11525 : /* Don't constant evaluate an unevaluated non-manifestly-constant operand,
11526 : but at least try folding it to a simple constant. */
11527 4209113 : if (cp_unevaluated_operand && !manifestly_const_eval)
11528 89 : return fold_to_constant (t);
11529 :
11530 4209024 : tree r = cxx_eval_outermost_constant_expr (t, true, true,
11531 : mce_value (manifestly_const_eval),
11532 : false, object);
11533 : /* cp_tree_equal looks through NOPs, so allow them. */
11534 4209024 : gcc_checking_assert (r == t
11535 : || CONVERT_EXPR_P (t)
11536 : || TREE_CODE (t) == VIEW_CONVERT_EXPR
11537 : || (TREE_CONSTANT (t) && !TREE_CONSTANT (r))
11538 : || !cp_tree_equal (r, t));
11539 4209024 : return r;
11540 16179885 : }
11541 13195366 : else if (TREE_OVERFLOW_P (t))
11542 : {
11543 0 : t = build_nop (TREE_TYPE (t), t);
11544 0 : TREE_CONSTANT (t) = false;
11545 : }
11546 :
11547 : return t;
11548 : }
11549 :
11550 : /* Like maybe_constant_value but first fully instantiate the argument.
11551 :
11552 : Note: this is equivalent to instantiate_non_dependent_expr (t, complain)
11553 : followed by maybe_constant_value but is more efficient,
11554 : because it calls instantiation_dependent_expression_p and
11555 : potential_constant_expression at most once.
11556 : The manifestly_const_eval argument is passed to maybe_constant_value.
11557 :
11558 : Callers should generally pass their active complain, or if they are in a
11559 : non-template, diagnosing context, they can use the default of
11560 : tf_warning_or_error. Callers that might be within a template context, don't
11561 : have a complain parameter, and aren't going to remember the result for long
11562 : (e.g. null_ptr_cst_p), can pass tf_none and deal with error_mark_node
11563 : appropriately. */
11564 :
11565 : tree
11566 246524779 : fold_non_dependent_expr (tree t,
11567 : tsubst_flags_t complain /* = tf_warning_or_error */,
11568 : bool manifestly_const_eval /* = false */,
11569 : tree object /* = NULL_TREE */)
11570 : {
11571 246524779 : if (t == NULL_TREE)
11572 : return NULL_TREE;
11573 :
11574 245476906 : if (processing_template_decl)
11575 28343657 : return fold_non_dependent_expr_template (t, complain,
11576 28343657 : manifestly_const_eval, object);
11577 :
11578 217133249 : return maybe_constant_value (t, object, mce_value (manifestly_const_eval));
11579 : }
11580 :
11581 : /* Like fold_non_dependent_expr, but if EXPR couldn't be folded to a constant,
11582 : return the original expression. */
11583 :
11584 : tree
11585 2224040 : maybe_fold_non_dependent_expr (tree expr,
11586 : tsubst_flags_t complain/*=tf_warning_or_error*/)
11587 : {
11588 2224040 : tree t = fold_non_dependent_expr (expr, complain);
11589 2224040 : if (t && TREE_CONSTANT (t))
11590 1049216 : return t;
11591 :
11592 : return expr;
11593 : }
11594 :
11595 : /* Like maybe_constant_init but first fully instantiate the argument. */
11596 :
11597 : tree
11598 58877000 : fold_non_dependent_init (tree t,
11599 : tsubst_flags_t complain /*=tf_warning_or_error*/,
11600 : bool manifestly_const_eval /*=false*/,
11601 : tree object /* = NULL_TREE */)
11602 : {
11603 58877000 : if (t == NULL_TREE)
11604 : return NULL_TREE;
11605 :
11606 58877000 : if (processing_template_decl)
11607 : {
11608 1031594 : t = fold_non_dependent_expr_template (t, complain,
11609 : manifestly_const_eval, object);
11610 : /* maybe_constant_init does this stripping, so do it here too. */
11611 1031594 : if (TREE_CODE (t) == TARGET_EXPR)
11612 : {
11613 934 : tree init = TARGET_EXPR_INITIAL (t);
11614 934 : if (TREE_CODE (init) == CONSTRUCTOR)
11615 1031594 : t = init;
11616 : }
11617 1031594 : return t;
11618 : }
11619 :
11620 57845406 : return maybe_constant_init (t, object, manifestly_const_eval);
11621 : }
11622 :
11623 : /* Like maybe_constant_value, but returns a CONSTRUCTOR directly, rather
11624 : than wrapped in a TARGET_EXPR.
11625 : ALLOW_NON_CONSTANT is false if T is required to be a constant expression.
11626 : MANIFESTLY_CONST_EVAL is true if T is manifestly const-evaluated as
11627 : per P0595 even when ALLOW_NON_CONSTANT is true. */
11628 :
11629 : static tree
11630 113006715 : maybe_constant_init_1 (tree t, tree decl, bool allow_non_constant,
11631 : mce_value manifestly_const_eval)
11632 : {
11633 113006715 : if (!t)
11634 : return t;
11635 113006715 : if (TREE_CODE (t) == EXPR_STMT)
11636 24448 : t = TREE_OPERAND (t, 0);
11637 113006715 : if (TREE_CODE (t) == CONVERT_EXPR
11638 113006715 : && VOID_TYPE_P (TREE_TYPE (t)))
11639 26357 : t = TREE_OPERAND (t, 0);
11640 : /* If the types don't match, the INIT_EXPR is initializing a subobject of
11641 : DECL and losing that information would cause mischief later. */
11642 113006715 : if (TREE_CODE (t) == INIT_EXPR
11643 113006715 : && (!decl
11644 1925 : || same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (decl),
11645 1925 : TREE_TYPE (t))))
11646 25693 : t = TREE_OPERAND (t, 1);
11647 113006715 : if (TREE_CODE (t) == TARGET_EXPR)
11648 523985 : t = TARGET_EXPR_INITIAL (t);
11649 113006715 : if (!is_nondependent_static_init_expression (t))
11650 : /* Don't try to evaluate it. */;
11651 104887068 : else if (CONSTANT_CLASS_P (t) && TREE_CODE (t) != PTRMEM_CST)
11652 : /* No evaluation needed. PTRMEM_CST needs the immediate fn check. */;
11653 : else
11654 : {
11655 : /* [basic.start.static] allows constant-initialization of variables with
11656 : static or thread storage duration even if it isn't required, but we
11657 : shouldn't bend the rules the same way for automatic variables.
11658 :
11659 : But still enforce the requirements of constexpr/constinit.
11660 : [dcl.constinit] "If a variable declared with the constinit specifier
11661 : has dynamic initialization, the program is ill-formed, even if the
11662 : implementation would perform that initialization as a static
11663 : initialization." */
11664 18796936 : bool is_static = (decl && DECL_P (decl)
11665 61129181 : && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)));
11666 3229289 : bool strict = (!is_static
11667 : || (decl && DECL_P (decl)
11668 3229289 : && (DECL_DECLARED_CONSTEXPR_P (decl)
11669 938855 : || DECL_DECLARED_CONSTINIT_P (decl))));
11670 : if (is_static)
11671 : manifestly_const_eval = mce_true;
11672 :
11673 43026801 : if (cp_unevaluated_operand && manifestly_const_eval != mce_true)
11674 51162 : return fold_to_constant (t);
11675 :
11676 42975639 : t = cxx_eval_outermost_constant_expr (t, allow_non_constant, strict,
11677 : manifestly_const_eval,
11678 : false, decl);
11679 : }
11680 112955553 : if (TREE_CODE (t) == TARGET_EXPR)
11681 : {
11682 70378 : tree init = TARGET_EXPR_INITIAL (t);
11683 70378 : if (TREE_CODE (init) == CONSTRUCTOR)
11684 113006715 : t = init;
11685 : }
11686 : return t;
11687 : }
11688 :
11689 : /* Wrapper for maybe_constant_init_1 which permits non constants. */
11690 :
11691 : tree
11692 93256842 : maybe_constant_init (tree t, tree decl, bool manifestly_const_eval)
11693 : {
11694 93256842 : return maybe_constant_init_1 (t, decl, true, mce_value (manifestly_const_eval));
11695 : }
11696 :
11697 : tree
11698 19748318 : maybe_constant_init (tree t, tree decl, mce_value manifestly_const_eval)
11699 : {
11700 19748318 : return maybe_constant_init_1 (t, decl, true, manifestly_const_eval);
11701 : }
11702 :
11703 : /* Wrapper for maybe_constant_init_1 which does not permit non constants. */
11704 :
11705 : tree
11706 1555 : cxx_constant_init (tree t, tree decl)
11707 : {
11708 1555 : return maybe_constant_init_1 (t, decl, false, mce_true);
11709 : }
11710 :
11711 : /* Return true if CALL_EXPR T might throw during constant evaluation. */
11712 :
11713 : static bool
11714 196285598 : callee_might_throw (tree t)
11715 : {
11716 196285598 : if (cxx_dialect < cxx26 || !flag_exceptions)
11717 : return false;
11718 19196359 : tree callee = cp_get_callee (t);
11719 19196359 : if (callee == NULL_TREE)
11720 : return false;
11721 19161333 : tree callee_fn = cp_get_fndecl_from_callee (callee, false);
11722 19161333 : return (!flag_enforce_eh_specs
11723 19161333 : || type_dependent_expression_p (callee)
11724 17259410 : || !POINTER_TYPE_P (TREE_TYPE (callee))
11725 35144762 : || (!type_noexcept_p (TREE_TYPE (TREE_TYPE (callee)))
11726 6768118 : && (callee_fn == NULL_TREE || !TREE_NOTHROW (callee_fn))));
11727 : }
11728 :
11729 : #if 0
11730 : /* FIXME see ADDR_EXPR section in potential_constant_expression_1. */
11731 : /* Return true if the object referred to by REF has automatic or thread
11732 : local storage. */
11733 :
11734 : enum { ck_ok, ck_bad, ck_unknown };
11735 : static int
11736 : check_automatic_or_tls (tree ref)
11737 : {
11738 : machine_mode mode;
11739 : poly_int64 bitsize, bitpos;
11740 : tree offset;
11741 : int volatilep = 0, unsignedp = 0;
11742 : tree decl = get_inner_reference (ref, &bitsize, &bitpos, &offset,
11743 : &mode, &unsignedp, &volatilep, false);
11744 : duration_kind dk;
11745 :
11746 : /* If there isn't a decl in the middle, we don't know the linkage here,
11747 : and this isn't a constant expression anyway. */
11748 : if (!DECL_P (decl))
11749 : return ck_unknown;
11750 : dk = decl_storage_duration (decl);
11751 : return (dk == dk_auto || dk == dk_thread) ? ck_bad : ck_ok;
11752 : }
11753 : #endif
11754 :
11755 : /* Data structure for passing data from potential_constant_expression_1
11756 : to check_for_return_continue via cp_walk_tree. */
11757 : struct check_for_return_continue_data {
11758 : hash_set<tree> *pset;
11759 : tree continue_stmt;
11760 : tree break_stmt;
11761 : bool could_throw;
11762 : };
11763 :
11764 : /* Helper function for potential_constant_expression_1 SWITCH_STMT handling,
11765 : called through cp_walk_tree. Return the first RETURN_EXPR found, or note
11766 : the first CONTINUE_STMT and/or BREAK_STMT if RETURN_EXPR is not found.
11767 : For C++26 also note presence of possibly throwing calls. */
11768 : static tree
11769 45612243 : check_for_return_continue (tree *tp, int *walk_subtrees, void *data)
11770 : {
11771 45612243 : tree t = *tp, s, b;
11772 45612243 : check_for_return_continue_data *d = (check_for_return_continue_data *) data;
11773 45612243 : switch (TREE_CODE (t))
11774 : {
11775 : case RETURN_EXPR:
11776 : return t;
11777 :
11778 28 : case CONTINUE_STMT:
11779 28 : if (d->continue_stmt == NULL_TREE)
11780 28 : d->continue_stmt = t;
11781 : break;
11782 :
11783 3012 : case BREAK_STMT:
11784 3012 : if (d->break_stmt == NULL_TREE)
11785 1297 : d->break_stmt = t;
11786 : break;
11787 :
11788 : #define RECUR(x) \
11789 : if (tree r = cp_walk_tree (&x, check_for_return_continue, data, \
11790 : d->pset)) \
11791 : return r
11792 :
11793 : /* For loops, walk subtrees manually, so that continue stmts found
11794 : inside of the bodies of the loops are ignored. */
11795 39314 : case DO_STMT:
11796 39314 : *walk_subtrees = 0;
11797 39314 : RECUR (DO_COND (t));
11798 39314 : s = d->continue_stmt;
11799 39314 : b = d->break_stmt;
11800 39314 : RECUR (DO_BODY (t));
11801 39314 : d->continue_stmt = s;
11802 39314 : d->break_stmt = b;
11803 39314 : break;
11804 :
11805 117 : case WHILE_STMT:
11806 117 : *walk_subtrees = 0;
11807 117 : RECUR (WHILE_COND_PREP (t));
11808 117 : RECUR (WHILE_COND (t));
11809 117 : s = d->continue_stmt;
11810 117 : b = d->break_stmt;
11811 117 : RECUR (WHILE_BODY (t));
11812 105 : d->continue_stmt = s;
11813 105 : d->break_stmt = b;
11814 105 : break;
11815 :
11816 277 : case FOR_STMT:
11817 277 : *walk_subtrees = 0;
11818 277 : RECUR (FOR_INIT_STMT (t));
11819 277 : RECUR (FOR_COND_PREP (t));
11820 277 : RECUR (FOR_COND (t));
11821 277 : RECUR (FOR_EXPR (t));
11822 277 : s = d->continue_stmt;
11823 277 : b = d->break_stmt;
11824 277 : RECUR (FOR_BODY (t));
11825 247 : d->continue_stmt = s;
11826 247 : d->break_stmt = b;
11827 247 : break;
11828 :
11829 0 : case RANGE_FOR_STMT:
11830 0 : *walk_subtrees = 0;
11831 0 : RECUR (RANGE_FOR_EXPR (t));
11832 0 : s = d->continue_stmt;
11833 0 : b = d->break_stmt;
11834 0 : RECUR (RANGE_FOR_BODY (t));
11835 0 : d->continue_stmt = s;
11836 0 : d->break_stmt = b;
11837 0 : break;
11838 :
11839 207 : case SWITCH_STMT:
11840 207 : *walk_subtrees = 0;
11841 207 : RECUR (SWITCH_STMT_COND (t));
11842 207 : b = d->break_stmt;
11843 207 : RECUR (SWITCH_STMT_BODY (t));
11844 187 : d->break_stmt = b;
11845 187 : break;
11846 : #undef RECUR
11847 :
11848 : case STATEMENT_LIST:
11849 : case CONSTRUCTOR:
11850 : break;
11851 :
11852 2442514 : case AGGR_INIT_EXPR:
11853 2442514 : case CALL_EXPR:
11854 : /* In C++26 a function could throw. */
11855 2442514 : if (callee_might_throw (t))
11856 52935 : d->could_throw = true;
11857 : break;
11858 :
11859 41859358 : default:
11860 41859358 : if (!EXPR_P (t))
11861 13027754 : *walk_subtrees = 0;
11862 : break;
11863 : }
11864 :
11865 : return NULL_TREE;
11866 : }
11867 :
11868 : /* Return true if T denotes a potentially constant expression. Issue
11869 : diagnostic as appropriate under control of FLAGS. If WANT_RVAL is true,
11870 : an lvalue-rvalue conversion is implied. If NOW is true, we want to
11871 : consider the expression in the current context, independent of constexpr
11872 : substitution. If FUNDEF_P is true, we're checking a constexpr function body
11873 : and hard errors should not be reported by constexpr_error.
11874 :
11875 : C++0x [expr.const] used to say
11876 :
11877 : 6 An expression is a potential constant expression if it is
11878 : a constant expression where all occurrences of function
11879 : parameters are replaced by arbitrary constant expressions
11880 : of the appropriate type.
11881 :
11882 : 2 A conditional expression is a constant expression unless it
11883 : involves one of the following as a potentially evaluated
11884 : subexpression (3.2), but subexpressions of logical AND (5.14),
11885 : logical OR (5.15), and conditional (5.16) operations that are
11886 : not evaluated are not considered. */
11887 :
11888 : static bool
11889 4359067607 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
11890 : bool fundef_p, tsubst_flags_t flags,
11891 : tree *jump_target)
11892 : {
11893 : #define RECUR(T,RV) \
11894 : potential_constant_expression_1 ((T), (RV), strict, now, fundef_p, flags, \
11895 : jump_target)
11896 :
11897 4359067607 : enum { any = false, rval = true };
11898 4359067607 : int i;
11899 4359067607 : tree tmp;
11900 :
11901 4359067607 : if (t == error_mark_node)
11902 : return false;
11903 4359056476 : if (t == NULL_TREE)
11904 : return true;
11905 4351964130 : location_t loc = cp_expr_loc_or_input_loc (t);
11906 4351964130 : iloc_sentinel ils = loc;
11907 :
11908 4351964130 : if (*jump_target)
11909 : /* If we are jumping, ignore everything. This is simpler than the
11910 : cxx_eval_constant_expression handling because we only need to be
11911 : conservatively correct, and we don't necessarily have a constant value
11912 : available, so we don't bother with switch tracking. */
11913 : return true;
11914 :
11915 1601504 : if (TREE_THIS_VOLATILE (t) && want_rval
11916 1037366 : && !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (t))
11917 4344558200 : && !NULLPTR_TYPE_P (TREE_TYPE (t)))
11918 : {
11919 78028 : if (TREE_CLOBBER_P (t))
11920 : {
11921 : /* We should have caught any clobbers in INIT/MODIFY_EXPR. */
11922 0 : gcc_checking_assert (false);
11923 : return true;
11924 : }
11925 :
11926 78028 : if (flags & tf_error)
11927 22 : constexpr_error (loc, fundef_p, "lvalue-to-rvalue conversion of "
11928 : "a volatile lvalue %qE with type %qT", t,
11929 22 : TREE_TYPE (t));
11930 78028 : return false;
11931 : }
11932 4344402118 : if (CONSTANT_CLASS_P (t))
11933 : return true;
11934 3117927666 : if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_TYPED)
11935 3117927666 : && TREE_TYPE (t) == error_mark_node)
11936 : return false;
11937 :
11938 3117927629 : switch (TREE_CODE (t))
11939 : {
11940 : case FUNCTION_DECL:
11941 : case BASELINK:
11942 : case TEMPLATE_DECL:
11943 : case OVERLOAD:
11944 : case TEMPLATE_ID_EXPR:
11945 : case LABEL_DECL:
11946 : case CASE_LABEL_EXPR:
11947 : case PREDICT_EXPR:
11948 : case CONST_DECL:
11949 : case SIZEOF_EXPR:
11950 : case ALIGNOF_EXPR:
11951 : case OFFSETOF_EXPR:
11952 : case NOEXCEPT_EXPR:
11953 : case TEMPLATE_PARM_INDEX:
11954 : case TRAIT_EXPR:
11955 : case IDENTIFIER_NODE:
11956 : case USERDEF_LITERAL:
11957 : /* We can see a FIELD_DECL in a pointer-to-member expression. */
11958 : case FIELD_DECL:
11959 : case RESULT_DECL:
11960 : case USING_DECL:
11961 : case USING_STMT:
11962 : case PLACEHOLDER_EXPR:
11963 : case REQUIRES_EXPR:
11964 : case STATIC_ASSERT:
11965 : case DEBUG_BEGIN_STMT:
11966 : case REFLECT_EXPR:
11967 : return true;
11968 :
11969 23063108 : case RETURN_EXPR:
11970 23063108 : if (!RECUR (TREE_OPERAND (t, 0), any))
11971 : return false;
11972 : /* FALLTHROUGH */
11973 :
11974 22921891 : case BREAK_STMT:
11975 22921891 : case CONTINUE_STMT:
11976 22921891 : *jump_target = t;
11977 22921891 : return true;
11978 :
11979 324995073 : case PARM_DECL:
11980 324995073 : if (now && want_rval)
11981 : {
11982 83087846 : tree type = TREE_TYPE (t);
11983 83087846 : if (dependent_type_p (type)
11984 55664809 : || !COMPLETE_TYPE_P (processing_template_decl
11985 : ? type : complete_type (type))
11986 138752655 : || is_really_empty_class (type, /*ignore_vptr*/false))
11987 : /* An empty class has no data to read. */
11988 27423508 : return true;
11989 55664338 : if (flags & tf_error)
11990 17 : constexpr_error (input_location, fundef_p,
11991 : "%qE is not a constant expression", t);
11992 55664338 : return false;
11993 : }
11994 : return true;
11995 :
11996 237164268 : case AGGR_INIT_EXPR:
11997 237164268 : case CALL_EXPR:
11998 : /* -- an invocation of a function other than a constexpr function
11999 : or a constexpr constructor. */
12000 237164268 : {
12001 237164268 : tree fun = get_function_named_in_call (t);
12002 237164268 : const int nargs = call_expr_nargs (t);
12003 237164268 : i = 0;
12004 :
12005 237164268 : if (fun == NULL_TREE)
12006 : {
12007 : /* Reset to allow the function to continue past the end
12008 : of the block below. Otherwise return early. */
12009 395644 : bool bail = true;
12010 :
12011 395644 : if (TREE_CODE (t) == CALL_EXPR
12012 395644 : && CALL_EXPR_FN (t) == NULL_TREE)
12013 395644 : switch (CALL_EXPR_IFN (t))
12014 : {
12015 : /* These should be ignored, they are optimized away from
12016 : constexpr functions. */
12017 : case IFN_UBSAN_NULL:
12018 : case IFN_UBSAN_BOUNDS:
12019 : case IFN_UBSAN_VPTR:
12020 : case IFN_FALLTHROUGH:
12021 : case IFN_ASSUME:
12022 : return true;
12023 :
12024 : case IFN_ADD_OVERFLOW:
12025 : case IFN_SUB_OVERFLOW:
12026 : case IFN_MUL_OVERFLOW:
12027 : case IFN_LAUNDER:
12028 : case IFN_VEC_CONVERT:
12029 : bail = false;
12030 : break;
12031 :
12032 : default:
12033 : break;
12034 : }
12035 :
12036 : if (bail)
12037 : {
12038 : /* fold_call_expr can't do anything with IFN calls. */
12039 66 : if (flags & tf_error)
12040 0 : constexpr_error (loc, fundef_p,
12041 : "call to internal function %qE", t);
12042 66 : return false;
12043 : }
12044 : }
12045 :
12046 236768624 : if (fun && is_overloaded_fn (fun))
12047 : {
12048 219297166 : if (!RECUR (fun, true))
12049 : return false;
12050 218875323 : fun = get_fns (fun);
12051 :
12052 218875323 : if (TREE_CODE (fun) == FUNCTION_DECL)
12053 : {
12054 201974638 : if (builtin_valid_in_constant_expr_p (fun))
12055 : return true;
12056 200155865 : if (!maybe_constexpr_fn (fun)
12057 : /* Allow any built-in function; if the expansion
12058 : isn't constant, we'll deal with that then. */
12059 39241759 : && !fndecl_built_in_p (fun)
12060 : /* In C++20, replaceable global allocation functions
12061 : are constant expressions. */
12062 27187188 : && (!cxx_replaceable_global_alloc_fn (fun)
12063 689515 : || TREE_CODE (t) != CALL_EXPR
12064 689515 : || (!CALL_FROM_NEW_OR_DELETE_P (t)
12065 261999 : && (current_function_decl == NULL_TREE
12066 261999 : || !is_std_allocator_allocate
12067 261999 : (current_function_decl))))
12068 : /* Allow placement new in std::construct_at. */
12069 26501756 : && (!cxx_placement_new_fn (fun)
12070 381973 : || TREE_CODE (t) != CALL_EXPR
12071 381973 : || current_function_decl == NULL_TREE
12072 381961 : || !is_std_construct_at (current_function_decl))
12073 26238024 : && !cxx_dynamic_cast_fn_p (fun)
12074 226391086 : && !cxx_cxa_builtin_fn_p (fun))
12075 : {
12076 : /* In C++26 evaluation of the function arguments might
12077 : throw and in that case it is irrelevant whether
12078 : fun is constexpr or not. */
12079 26204769 : if (cxx_dialect >= cxx26)
12080 4833704 : for (; i < nargs; ++i)
12081 : {
12082 2873514 : tree x = get_nth_callarg (t, i);
12083 2873514 : bool rv = processing_template_decl ? any : rval;
12084 2873514 : bool sub_now = false;
12085 2873514 : if (!potential_constant_expression_1 (x, rv, strict,
12086 : sub_now,
12087 : fundef_p,
12088 : flags,
12089 : jump_target))
12090 : return false;
12091 2636265 : if (throws (jump_target))
12092 : return true;
12093 : }
12094 25927644 : if ((flags & tf_error)
12095 25927644 : && constexpr_error (loc, fundef_p,
12096 : "call to non-%<constexpr%> "
12097 : "function %qD", fun))
12098 250 : explain_invalid_constexpr_fn (fun);
12099 25927644 : return false;
12100 : }
12101 : }
12102 :
12103 190851781 : fun = OVL_FIRST (fun);
12104 : /* Skip initial arguments to base constructors. */
12105 190851781 : if (DECL_BASE_CONSTRUCTOR_P (fun))
12106 3859625 : i = num_artificial_parms_for (fun);
12107 : }
12108 17471458 : else if (fun)
12109 : {
12110 17471458 : if (TREE_TYPE (fun)
12111 17471458 : && FUNCTION_POINTER_TYPE_P (TREE_TYPE (fun)))
12112 : want_rval = rval;
12113 : else
12114 : want_rval = any;
12115 17471458 : if (RECUR (fun, want_rval))
12116 : /* Might end up being a constant function pointer. But it
12117 : could also be a function object with constexpr op(), so
12118 : we pass 'any' so that the underlying VAR_DECL is deemed
12119 : as potentially-constant even though it wasn't declared
12120 : constexpr. */;
12121 : else
12122 : return false;
12123 : }
12124 455244058 : for (; i < nargs; ++i)
12125 : {
12126 261365809 : tree x = get_nth_callarg (t, i);
12127 : /* In a template, reference arguments haven't been converted to
12128 : REFERENCE_TYPE and we might not even know if the parameter
12129 : is a reference, so accept lvalue constants too. */
12130 261365809 : bool rv = processing_template_decl ? any : rval;
12131 : /* Don't require an immediately constant value, as constexpr
12132 : substitution might not use the value of the argument. */
12133 261365809 : bool sub_now = false;
12134 261365809 : if (!potential_constant_expression_1 (x, rv, strict,
12135 : sub_now, fundef_p, flags,
12136 : jump_target))
12137 : return false;
12138 2846582096 : if (throws (jump_target))
12139 : return true;
12140 : }
12141 : /* In C++26 a function could throw. */
12142 193878249 : if (*jump_target == NULL_TREE && callee_might_throw (t))
12143 6001171 : *jump_target = void_node;
12144 : return true;
12145 : }
12146 :
12147 187395597 : case NON_LVALUE_EXPR:
12148 : /* -- an lvalue-to-rvalue conversion (4.1) unless it is applied to
12149 : -- an lvalue of integral type that refers to a non-volatile
12150 : const variable or static data member initialized with
12151 : constant expressions, or
12152 :
12153 : -- an lvalue of literal type that refers to non-volatile
12154 : object defined with constexpr, or that refers to a
12155 : sub-object of such an object; */
12156 187395597 : return RECUR (TREE_OPERAND (t, 0), rval);
12157 :
12158 27276 : case EXCESS_PRECISION_EXPR:
12159 27276 : return RECUR (TREE_OPERAND (t, 0), rval);
12160 :
12161 286574331 : case VAR_DECL:
12162 286574331 : if (DECL_HAS_VALUE_EXPR_P (t))
12163 : {
12164 3823344 : if (now && is_normal_capture_proxy (t))
12165 : {
12166 : /* -- in a lambda-expression, a reference to this or to a
12167 : variable with automatic storage duration defined outside that
12168 : lambda-expression, where the reference would be an
12169 : odr-use. */
12170 :
12171 354698 : if (want_rval)
12172 : /* Since we're doing an lvalue-rvalue conversion, this might
12173 : not be an odr-use, so evaluate the variable directly. */
12174 353550 : return RECUR (DECL_CAPTURED_VARIABLE (t), rval);
12175 :
12176 1148 : if (flags & tf_error)
12177 : {
12178 3 : tree cap = DECL_CAPTURED_VARIABLE (t);
12179 3 : auto_diagnostic_group d;
12180 3 : if (constexpr_error (input_location, fundef_p,
12181 : "lambda capture of %qE is not a "
12182 : "constant expression", cap)
12183 3 : && decl_constant_var_p (cap))
12184 3 : inform (input_location, "because it is used as a glvalue");
12185 3 : }
12186 1148 : return false;
12187 : }
12188 3468646 : tree ve = DECL_VALUE_EXPR (t);
12189 : /* Treat __PRETTY_FUNCTION__ inside a template function as
12190 : potentially-constant. */
12191 3468646 : if (DECL_PRETTY_FUNCTION_P (t) && ve == error_mark_node)
12192 : return true;
12193 3468641 : if (DECL_DECOMPOSITION_P (t) && TREE_CODE (ve) == TREE_VEC)
12194 2736 : return RECUR (TREE_VEC_ELT (ve, 0), rval);
12195 3465905 : return RECUR (ve, rval);
12196 : }
12197 282750987 : if (want_rval
12198 202534361 : && (now || !var_in_maybe_constexpr_fn (t))
12199 186572501 : && !type_dependent_expression_p (t)
12200 156235997 : && !decl_maybe_constant_var_p (t)
12201 49181559 : && !is_local_temp (t)
12202 48680246 : && (strict
12203 1231830 : || !CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (t))
12204 592577 : || (DECL_INITIAL (t)
12205 589736 : && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t)))
12206 48676761 : && COMPLETE_TYPE_P (TREE_TYPE (t))
12207 331427598 : && !is_really_empty_class (TREE_TYPE (t), /*ignore_vptr*/false))
12208 : {
12209 48671936 : if (flags & tf_error)
12210 163 : non_const_var_error (loc, t, fundef_p);
12211 48671936 : return false;
12212 : }
12213 : return true;
12214 :
12215 377867402 : case NOP_EXPR:
12216 377867402 : if (REINTERPRET_CAST_P (t))
12217 : {
12218 154468 : if (flags & tf_error)
12219 46 : constexpr_error (loc, fundef_p, "%<reinterpret_cast%> is not a "
12220 : "constant expression");
12221 154468 : return false;
12222 : }
12223 : /* FALLTHRU */
12224 783811529 : case CONVERT_EXPR:
12225 783811529 : case VIEW_CONVERT_EXPR:
12226 : /* -- a reinterpret_cast. FIXME not implemented, and this rule
12227 : may change to something more specific to type-punning (DR 1312). */
12228 783811529 : {
12229 783811529 : tree from = TREE_OPERAND (t, 0);
12230 783811529 : if (location_wrapper_p (t))
12231 336918613 : return (RECUR (from, want_rval));
12232 446892916 : if (INDIRECT_TYPE_P (TREE_TYPE (t)))
12233 : {
12234 247432743 : STRIP_ANY_LOCATION_WRAPPER (from);
12235 247432743 : if (TREE_CODE (from) == INTEGER_CST
12236 247432743 : && !integer_zerop (from))
12237 : {
12238 1536 : if (flags & tf_error)
12239 25 : constexpr_error (loc, fundef_p,
12240 : "%<reinterpret_cast%> from integer to "
12241 : "pointer");
12242 1536 : return false;
12243 : }
12244 : }
12245 446891380 : return (RECUR (from, TREE_CODE (t) != VIEW_CONVERT_EXPR));
12246 : }
12247 :
12248 11851 : case ADDRESSOF_EXPR:
12249 : /* This is like ADDR_EXPR, except it won't form pointer-to-member. */
12250 11851 : t = TREE_OPERAND (t, 0);
12251 11851 : goto handle_addr_expr;
12252 :
12253 81331933 : case ADDR_EXPR:
12254 : /* -- a unary operator & that is applied to an lvalue that
12255 : designates an object with thread or automatic storage
12256 : duration; */
12257 81331933 : t = TREE_OPERAND (t, 0);
12258 :
12259 81331933 : if (TREE_CODE (t) == OFFSET_REF && PTRMEM_OK_P (t))
12260 : /* A pointer-to-member constant. */
12261 : return true;
12262 :
12263 81343546 : handle_addr_expr:
12264 : #if 0
12265 : /* FIXME adjust when issue 1197 is fully resolved. For now don't do
12266 : any checking here, as we might dereference the pointer later. If
12267 : we remove this code, also remove check_automatic_or_tls. */
12268 : i = check_automatic_or_tls (t);
12269 : if (i == ck_ok)
12270 : return true;
12271 : if (i == ck_bad)
12272 : {
12273 : if (flags & tf_error)
12274 : error ("address-of an object %qE with thread local or "
12275 : "automatic storage is not a constant expression", t);
12276 : return false;
12277 : }
12278 : #endif
12279 81343546 : return RECUR (t, any);
12280 :
12281 90287659 : case COMPONENT_REF:
12282 90287659 : case ARROW_EXPR:
12283 90287659 : case OFFSET_REF:
12284 : /* -- a class member access unless its postfix-expression is
12285 : of literal type or of pointer to literal type. */
12286 : /* This test would be redundant, as it follows from the
12287 : postfix-expression being a potential constant expression. */
12288 90287659 : if (type_unknown_p (t))
12289 : return true;
12290 82592904 : if (is_overloaded_fn (t))
12291 : /* In a template, a COMPONENT_REF of a function expresses ob.fn(),
12292 : which uses ob as an lvalue. */
12293 84144235 : want_rval = false;
12294 84144235 : gcc_fallthrough ();
12295 :
12296 84144235 : case REALPART_EXPR:
12297 84144235 : case IMAGPART_EXPR:
12298 84144235 : case BIT_FIELD_REF:
12299 84144235 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12300 :
12301 252467 : case EXPR_PACK_EXPANSION:
12302 252467 : return RECUR (PACK_EXPANSION_PATTERN (t), want_rval);
12303 :
12304 : case PACK_INDEX_EXPR:
12305 : return true;
12306 :
12307 94763369 : case INDIRECT_REF:
12308 94763369 : return RECUR (TREE_OPERAND (t, 0), rval);
12309 :
12310 23515146 : case STATEMENT_LIST:
12311 4444009977 : for (tree stmt : tsi_range (t))
12312 69050746 : if (!RECUR (stmt, any))
12313 252911855 : return false;
12314 : return true;
12315 :
12316 4181793 : case MODIFY_EXPR:
12317 4181793 : if (cxx_dialect < cxx14)
12318 1973 : goto fail;
12319 4179820 : if (!RECUR (TREE_OPERAND (t, 0), any))
12320 : return false;
12321 : /* Just ignore clobbers. */
12322 3756690 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12323 : return true;
12324 3315592 : if (!RECUR (TREE_OPERAND (t, 1), rval))
12325 : return false;
12326 : return true;
12327 :
12328 873389 : case MODOP_EXPR:
12329 873389 : if (cxx_dialect < cxx14)
12330 98 : goto fail;
12331 873291 : if (!RECUR (TREE_OPERAND (t, 0), rval))
12332 : return false;
12333 863372 : if (!RECUR (TREE_OPERAND (t, 2), rval))
12334 : return false;
12335 : return true;
12336 :
12337 512223 : case DO_STMT:
12338 512223 : if (!RECUR (DO_COND (t), rval))
12339 : return false;
12340 512223 : if (!RECUR (DO_BODY (t), any))
12341 : return false;
12342 511934 : if (breaks (jump_target) || continues (jump_target))
12343 2 : *jump_target = NULL_TREE;
12344 : return true;
12345 :
12346 412679 : case FOR_STMT:
12347 412679 : if (!RECUR (FOR_INIT_STMT (t), any))
12348 : return false;
12349 412679 : if (!RECUR (FOR_COND_PREP (t), any))
12350 : return false;
12351 412679 : tmp = FOR_COND (t);
12352 412679 : if (!RECUR (tmp, rval))
12353 : return false;
12354 412611 : if (tmp)
12355 : {
12356 368119 : if (!processing_template_decl)
12357 364940 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12358 : /* If we couldn't evaluate the condition, it might not ever be
12359 : true. */
12360 368119 : if (!integer_onep (tmp))
12361 : {
12362 : /* Before returning true, check if the for body can contain
12363 : a return. */
12364 368119 : hash_set<tree> pset;
12365 368119 : check_for_return_continue_data data = { &pset, NULL_TREE,
12366 368119 : NULL_TREE, false };
12367 368119 : if (tree ret_expr
12368 368119 : = cp_walk_tree (&FOR_BODY (t), check_for_return_continue,
12369 : &data, &pset))
12370 129175 : *jump_target = ret_expr;
12371 368119 : if (data.could_throw)
12372 9452 : *jump_target = void_node;
12373 368119 : return true;
12374 368119 : }
12375 : }
12376 44492 : if (!RECUR (FOR_EXPR (t), any))
12377 : return false;
12378 44492 : if (!RECUR (FOR_BODY (t), any))
12379 : return false;
12380 44492 : if (!RECUR (FOR_COND_CLEANUP (t), any))
12381 : return false;
12382 44492 : if (breaks (jump_target) || continues (jump_target))
12383 12 : *jump_target = NULL_TREE;
12384 : return true;
12385 :
12386 463 : case RANGE_FOR_STMT:
12387 463 : if (!RECUR (RANGE_FOR_INIT_STMT (t), any))
12388 : return false;
12389 463 : if (!RECUR (RANGE_FOR_EXPR (t), any))
12390 : return false;
12391 463 : if (!RECUR (RANGE_FOR_BODY (t), any))
12392 : return false;
12393 461 : if (breaks (jump_target) || continues (jump_target))
12394 0 : *jump_target = NULL_TREE;
12395 : return true;
12396 :
12397 163768 : case WHILE_STMT:
12398 163768 : if (!RECUR (WHILE_COND_PREP (t), any))
12399 : return false;
12400 163768 : tmp = WHILE_COND (t);
12401 163768 : if (!RECUR (tmp, rval))
12402 : return false;
12403 163704 : if (!processing_template_decl)
12404 163680 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12405 : /* If we couldn't evaluate the condition, it might not ever be true. */
12406 163704 : if (!integer_onep (tmp))
12407 : {
12408 : /* Before returning true, check if the while body can contain
12409 : a return. */
12410 154000 : hash_set<tree> pset;
12411 154000 : check_for_return_continue_data data = { &pset, NULL_TREE,
12412 154000 : NULL_TREE, false };
12413 154000 : if (tree ret_expr
12414 154000 : = cp_walk_tree (&WHILE_BODY (t), check_for_return_continue,
12415 : &data, &pset))
12416 417 : *jump_target = ret_expr;
12417 154000 : if (data.could_throw)
12418 3124 : *jump_target = void_node;
12419 154000 : return true;
12420 154000 : }
12421 9704 : if (!RECUR (WHILE_BODY (t), any))
12422 : return false;
12423 9693 : if (!RECUR (WHILE_COND_CLEANUP (t), any))
12424 : return false;
12425 9693 : if (breaks (jump_target) || continues (jump_target))
12426 24 : *jump_target = NULL_TREE;
12427 : return true;
12428 :
12429 24437 : case SWITCH_STMT:
12430 24437 : if (!RECUR (SWITCH_STMT_COND (t), rval))
12431 : return false;
12432 : /* FIXME we don't check SWITCH_STMT_BODY currently, because even
12433 : unreachable labels would be checked and it is enough if there is
12434 : a single switch cond value for which it is a valid constant
12435 : expression. We need to check if there are any RETURN_EXPRs
12436 : or CONTINUE_STMTs inside of the body though, as in that case
12437 : we need to set *jump_target. */
12438 : else
12439 : {
12440 24431 : hash_set<tree> pset;
12441 24431 : check_for_return_continue_data data = { &pset, NULL_TREE,
12442 24431 : NULL_TREE, false };
12443 24431 : if (tree ret_expr
12444 24431 : = cp_walk_tree (&SWITCH_STMT_BODY (t), check_for_return_continue,
12445 : &data, &pset))
12446 : /* The switch might return. */
12447 24051 : *jump_target = ret_expr;
12448 380 : else if (data.continue_stmt)
12449 : /* The switch can't return, but might continue. */
12450 3 : *jump_target = data.continue_stmt;
12451 24431 : if (data.could_throw)
12452 25 : *jump_target = void_node;
12453 24431 : }
12454 24431 : return true;
12455 :
12456 50 : case STMT_EXPR:
12457 50 : return RECUR (STMT_EXPR_STMT (t), rval);
12458 :
12459 537429 : case LAMBDA_EXPR:
12460 537429 : if (cxx_dialect >= cxx17)
12461 : /* In C++17 lambdas can be constexpr, don't give up yet. */
12462 : return true;
12463 444 : else if (flags & tf_error)
12464 0 : constexpr_error (loc, fundef_p, "lambda-expression is not a "
12465 : "constant expression before C++17");
12466 : return false;
12467 :
12468 108924 : case NEW_EXPR:
12469 108924 : case VEC_NEW_EXPR:
12470 108924 : case DELETE_EXPR:
12471 108924 : case VEC_DELETE_EXPR:
12472 108924 : if (cxx_dialect >= cxx20)
12473 : /* In C++20, new-expressions are potentially constant. */
12474 : return true;
12475 1071 : else if (flags & tf_error)
12476 0 : constexpr_error (loc, fundef_p, "new-expression is not a "
12477 : "constant expression before C++20");
12478 : return false;
12479 :
12480 79564 : case DYNAMIC_CAST_EXPR:
12481 79564 : case PSEUDO_DTOR_EXPR:
12482 79564 : case OMP_PARALLEL:
12483 79564 : case OMP_TASK:
12484 79564 : case OMP_FOR:
12485 79564 : case OMP_SIMD:
12486 79564 : case OMP_DISTRIBUTE:
12487 79564 : case OMP_TASKLOOP:
12488 79564 : case OMP_LOOP:
12489 79564 : case OMP_TEAMS:
12490 79564 : case OMP_TARGET_DATA:
12491 79564 : case OMP_TARGET:
12492 79564 : case OMP_SECTIONS:
12493 79564 : case OMP_ORDERED:
12494 79564 : case OMP_CRITICAL:
12495 79564 : case OMP_SINGLE:
12496 79564 : case OMP_SCAN:
12497 79564 : case OMP_SCOPE:
12498 79564 : case OMP_SECTION:
12499 79564 : case OMP_MASTER:
12500 79564 : case OMP_MASKED:
12501 79564 : case OMP_TASKGROUP:
12502 79564 : case OMP_TARGET_UPDATE:
12503 79564 : case OMP_TARGET_ENTER_DATA:
12504 79564 : case OMP_TARGET_EXIT_DATA:
12505 79564 : case OMP_ATOMIC:
12506 79564 : case OMP_ATOMIC_READ:
12507 79564 : case OMP_ATOMIC_CAPTURE_OLD:
12508 79564 : case OMP_ATOMIC_CAPTURE_NEW:
12509 79564 : case OMP_DEPOBJ:
12510 79564 : case OACC_PARALLEL:
12511 79564 : case OACC_KERNELS:
12512 79564 : case OACC_SERIAL:
12513 79564 : case OACC_DATA:
12514 79564 : case OACC_HOST_DATA:
12515 79564 : case OACC_LOOP:
12516 79564 : case OACC_CACHE:
12517 79564 : case OACC_DECLARE:
12518 79564 : case OACC_ENTER_DATA:
12519 79564 : case OACC_EXIT_DATA:
12520 79564 : case OACC_UPDATE:
12521 79564 : case OMP_ARRAY_SECTION:
12522 : /* GCC internal stuff. */
12523 79564 : case VA_ARG_EXPR:
12524 79564 : case TRANSACTION_EXPR:
12525 79564 : case AT_ENCODE_EXPR:
12526 79564 : fail:
12527 79564 : if (flags & tf_error)
12528 23 : constexpr_error (loc, fundef_p, "expression %qE is not a constant "
12529 : "expression", t);
12530 : return false;
12531 :
12532 : case OMP_DECLARE_MAPPER:
12533 : /* This can be used to initialize VAR_DECLs: it's treated as a magic
12534 : constant. */
12535 : return true;
12536 :
12537 15753 : case THROW_EXPR:
12538 15753 : if (cxx_dialect < cxx26)
12539 278 : goto fail;
12540 15475 : return RECUR (TREE_OPERAND (t, 0), rval);
12541 :
12542 517 : case ASM_EXPR:
12543 517 : if (flags & tf_error)
12544 5 : inline_asm_in_constexpr_error (loc, fundef_p);
12545 : return false;
12546 :
12547 302237 : case OBJ_TYPE_REF:
12548 302237 : if (cxx_dialect >= cxx20)
12549 : /* In C++20 virtual calls can be constexpr, don't give up yet. */
12550 : return true;
12551 5556 : else if (flags & tf_error)
12552 0 : constexpr_error (loc, fundef_p, "virtual functions cannot be "
12553 : "%<constexpr%> before C++20");
12554 : return false;
12555 :
12556 4512 : case TYPEID_EXPR:
12557 : /* In C++20, a typeid expression whose operand is of polymorphic
12558 : class type can be constexpr. */
12559 4512 : {
12560 4512 : tree e = TREE_OPERAND (t, 0);
12561 4512 : if (cxx_dialect < cxx20
12562 26 : && strict
12563 26 : && !TYPE_P (e)
12564 19 : && !type_dependent_expression_p (e)
12565 7 : && CLASS_TYPE_P (TREE_TYPE (e))
12566 4517 : && TYPE_POLYMORPHIC_P (TREE_TYPE (e)))
12567 : {
12568 4 : if (flags & tf_error)
12569 1 : constexpr_error (loc, fundef_p, "%<typeid%> is not a "
12570 : "constant expression because %qE is "
12571 : "of polymorphic type", e);
12572 4 : return false;
12573 : }
12574 : return true;
12575 : }
12576 :
12577 28340683 : case POINTER_DIFF_EXPR:
12578 28340683 : case MINUS_EXPR:
12579 28340683 : want_rval = true;
12580 28340683 : goto binary;
12581 :
12582 74256638 : case LT_EXPR:
12583 74256638 : case LE_EXPR:
12584 74256638 : case GT_EXPR:
12585 74256638 : case GE_EXPR:
12586 74256638 : case EQ_EXPR:
12587 74256638 : case NE_EXPR:
12588 74256638 : case SPACESHIP_EXPR:
12589 74256638 : want_rval = true;
12590 74256638 : goto binary;
12591 :
12592 1771691 : case PREINCREMENT_EXPR:
12593 1771691 : case POSTINCREMENT_EXPR:
12594 1771691 : case PREDECREMENT_EXPR:
12595 1771691 : case POSTDECREMENT_EXPR:
12596 1771691 : if (cxx_dialect < cxx14)
12597 8602 : goto fail;
12598 1763089 : goto unary;
12599 :
12600 1020788 : case BIT_NOT_EXPR:
12601 : /* A destructor. */
12602 1020788 : if (TYPE_P (TREE_OPERAND (t, 0)))
12603 : return true;
12604 : /* fall through. */
12605 :
12606 42613951 : case CONJ_EXPR:
12607 42613951 : case SAVE_EXPR:
12608 42613951 : case FIX_TRUNC_EXPR:
12609 42613951 : case FLOAT_EXPR:
12610 42613951 : case NEGATE_EXPR:
12611 42613951 : case ABS_EXPR:
12612 42613951 : case ABSU_EXPR:
12613 42613951 : case TRUTH_NOT_EXPR:
12614 42613951 : case FIXED_CONVERT_EXPR:
12615 42613951 : case UNARY_PLUS_EXPR:
12616 42613951 : case UNARY_LEFT_FOLD_EXPR:
12617 42613951 : case UNARY_RIGHT_FOLD_EXPR:
12618 42613951 : case VEC_DUPLICATE_EXPR:
12619 1020788 : unary:
12620 42613951 : return RECUR (TREE_OPERAND (t, 0), rval);
12621 :
12622 30033383 : case CAST_EXPR:
12623 30033383 : case CONST_CAST_EXPR:
12624 30033383 : case STATIC_CAST_EXPR:
12625 30033383 : case REINTERPRET_CAST_EXPR:
12626 30033383 : case IMPLICIT_CONV_EXPR:
12627 30033383 : if (!cast_valid_in_integral_constant_expression_p (TREE_TYPE (t)))
12628 : /* In C++98, a conversion to non-integral type can't be part of a
12629 : constant expression. */
12630 : {
12631 319 : if (flags & tf_error)
12632 0 : constexpr_error (loc, fundef_p,
12633 : "cast to non-integral type %qT in a constant "
12634 0 : "expression", TREE_TYPE (t));
12635 319 : return false;
12636 : }
12637 : /* This might be a conversion from a class to a (potentially) literal
12638 : type. Let's consider it potentially constant since the conversion
12639 : might be a constexpr user-defined conversion. */
12640 30033064 : else if (cxx_dialect >= cxx11
12641 30012505 : && (dependent_type_p (TREE_TYPE (t))
12642 10503126 : || !COMPLETE_TYPE_P (TREE_TYPE (t))
12643 10490040 : || literal_type_p (TREE_TYPE (t)))
12644 29975804 : && TREE_OPERAND (t, 0)
12645 59778134 : && (TREE_CODE (t) != CAST_EXPR
12646 22505095 : || !TREE_CHAIN (TREE_OPERAND (t, 0))))
12647 : {
12648 29704696 : tree from = TREE_OPERAND (t, 0);
12649 29704696 : if (TREE_CODE (t) == CAST_EXPR)
12650 22464721 : from = TREE_VALUE (from);
12651 29704696 : tree type = TREE_TYPE (from);
12652 : /* If this is a dependent type, it could end up being a class
12653 : with conversions. */
12654 29704696 : if (type == NULL_TREE || WILDCARD_TYPE_P (type))
12655 : return true;
12656 : /* Or a non-dependent class which has conversions. */
12657 607167 : else if (CLASS_TYPE_P (type)
12658 607167 : && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type)))
12659 268141 : return true;
12660 : }
12661 :
12662 23848193 : return (RECUR (TREE_OPERAND (t, 0),
12663 23848193 : !TYPE_REF_P (TREE_TYPE (t))));
12664 :
12665 12837465 : case BIND_EXPR:
12666 12837465 : return RECUR (BIND_EXPR_BODY (t), want_rval);
12667 :
12668 65594691 : case CLEANUP_POINT_EXPR:
12669 65594691 : case MUST_NOT_THROW_EXPR:
12670 65594691 : case TRY_CATCH_EXPR:
12671 : /* Even for C++26 handle TRY_BLOCK conservatively, if we detect the
12672 : body could throw, even with catch (...) among handlers we'd need
12673 : to analyze them in detail if they couldn't rethrow it. More
12674 : importantly though, throws (jump_target) is just conservative,
12675 : and there could be e.g.
12676 : try
12677 : {
12678 : possibly_throwing_fn (args);
12679 : break;
12680 : }
12681 : catch (...)
12682 : {
12683 : }
12684 : or continue or return instead of break. So, clearing *jump_target
12685 : because we see catch (...) handler might mean we missed break
12686 : etc. */
12687 65594691 : case TRY_BLOCK:
12688 65594691 : case EH_SPEC_BLOCK:
12689 65594691 : case EXPR_STMT:
12690 65594691 : case PAREN_EXPR:
12691 : /* For convenience. */
12692 65594691 : case LOOP_EXPR:
12693 65594691 : case EXIT_EXPR:
12694 65594691 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12695 :
12696 11690144 : case DECL_EXPR:
12697 11690144 : tmp = DECL_EXPR_DECL (t);
12698 7497600 : if (VAR_P (tmp) && !DECL_ARTIFICIAL (tmp)
12699 17215208 : && (processing_template_decl
12700 5069641 : ? !decl_maybe_constant_var_p (tmp)
12701 4614218 : : !decl_constant_var_p (tmp)))
12702 : {
12703 4481870 : if (CP_DECL_THREAD_LOCAL_P (tmp) && !DECL_REALLY_EXTERN (tmp))
12704 : {
12705 8 : if (flags & tf_error)
12706 3 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12707 : "%qD defined %<thread_local%> in "
12708 : "%<constexpr%> context", tmp);
12709 8 : return false;
12710 : }
12711 4481862 : else if (TREE_STATIC (tmp))
12712 : {
12713 114 : if (flags & tf_error)
12714 25 : constexpr_error (DECL_SOURCE_LOCATION (tmp), fundef_p,
12715 : "%qD defined %<static%> in %<constexpr%> "
12716 : "context", tmp);
12717 114 : return false;
12718 : }
12719 4481748 : else if (!check_for_uninitialized_const_var
12720 4481748 : (tmp, /*constexpr_context_p=*/true, flags))
12721 : return false;
12722 : }
12723 11686287 : if (VAR_P (tmp))
12724 7493743 : return RECUR (DECL_INITIAL (tmp), want_rval);
12725 : return true;
12726 :
12727 27661 : case TRY_FINALLY_EXPR:
12728 27661 : return (RECUR (TREE_OPERAND (t, 0), want_rval)
12729 27661 : && RECUR (TREE_OPERAND (t, 1), any));
12730 :
12731 0 : case EH_ELSE_EXPR:
12732 : /* maybe_apply_function_contracts uses this to check postconditions only
12733 : on normal return. */
12734 0 : return (RECUR (TREE_OPERAND (t, 1), any)
12735 0 : || RECUR (TREE_OPERAND (t, 0), any));
12736 :
12737 23792068 : case SCOPE_REF:
12738 23792068 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12739 :
12740 40618268 : case TARGET_EXPR:
12741 40618268 : if (!TARGET_EXPR_DIRECT_INIT_P (t)
12742 40431692 : && !TARGET_EXPR_ELIDING_P (t)
12743 73119125 : && !literal_type_p (TREE_TYPE (t)))
12744 : {
12745 785448 : if (flags & tf_error)
12746 : {
12747 23 : auto_diagnostic_group d;
12748 23 : if (constexpr_error (loc, fundef_p,
12749 : "temporary of non-literal type %qT in a "
12750 23 : "constant expression", TREE_TYPE (t)))
12751 23 : explain_non_literal_class (TREE_TYPE (t));
12752 23 : }
12753 785448 : return false;
12754 : }
12755 : /* FALLTHRU */
12756 66338206 : case INIT_EXPR:
12757 66338206 : if (TREE_CLOBBER_P (TREE_OPERAND (t, 1)))
12758 : return true;
12759 66274034 : return RECUR (TREE_OPERAND (t, 1), rval);
12760 :
12761 33600137 : case CONSTRUCTOR:
12762 33600137 : {
12763 33600137 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (t);
12764 33600137 : constructor_elt *ce;
12765 4598891826 : for (i = 0; vec_safe_iterate (v, i, &ce); ++i)
12766 214313773 : if (!RECUR (ce->value, want_rval))
12767 : return false;
12768 : return true;
12769 : }
12770 :
12771 20236452 : case TREE_LIST:
12772 20236452 : {
12773 20236452 : gcc_assert (TREE_PURPOSE (t) == NULL_TREE
12774 : || DECL_P (TREE_PURPOSE (t)));
12775 20236452 : if (!RECUR (TREE_VALUE (t), want_rval))
12776 : return false;
12777 18128699 : if (TREE_CHAIN (t) == NULL_TREE)
12778 : return true;
12779 51500 : return RECUR (TREE_CHAIN (t), want_rval);
12780 : }
12781 :
12782 14703596 : case TRUNC_DIV_EXPR:
12783 14703596 : case CEIL_DIV_EXPR:
12784 14703596 : case FLOOR_DIV_EXPR:
12785 14703596 : case ROUND_DIV_EXPR:
12786 14703596 : case TRUNC_MOD_EXPR:
12787 14703596 : case CEIL_MOD_EXPR:
12788 14703596 : case ROUND_MOD_EXPR:
12789 14703596 : {
12790 14703596 : tree denom = TREE_OPERAND (t, 1);
12791 14703596 : if (!RECUR (denom, rval))
12792 : return false;
12793 : /* We can't call cxx_eval_outermost_constant_expr on an expression
12794 : that hasn't been through instantiate_non_dependent_expr yet. */
12795 14263486 : if (!processing_template_decl)
12796 7527837 : denom = cxx_eval_outermost_constant_expr (denom, true);
12797 14263486 : if (integer_zerop (denom))
12798 : {
12799 337 : if (flags & tf_error)
12800 35 : constexpr_error (input_location, fundef_p,
12801 : "division by zero is not a constant expression");
12802 337 : return false;
12803 : }
12804 : else
12805 : {
12806 14263149 : want_rval = true;
12807 14263149 : return RECUR (TREE_OPERAND (t, 0), want_rval);
12808 : }
12809 : }
12810 :
12811 6586298 : case COMPOUND_EXPR:
12812 6586298 : {
12813 : /* check_return_expr sometimes wraps a TARGET_EXPR in a
12814 : COMPOUND_EXPR; don't get confused. */
12815 6586298 : tree op0 = TREE_OPERAND (t, 0);
12816 6586298 : tree op1 = TREE_OPERAND (t, 1);
12817 6586298 : STRIP_NOPS (op1);
12818 6586298 : if (TREE_CODE (op0) == TARGET_EXPR && op1 == TARGET_EXPR_SLOT (op0))
12819 1254106 : return RECUR (op0, want_rval);
12820 : else
12821 5332192 : goto binary;
12822 : }
12823 :
12824 : /* If the first operand is the non-short-circuit constant, look at
12825 : the second operand; otherwise we only care about the first one for
12826 : potentiality. */
12827 17042983 : case TRUTH_AND_EXPR:
12828 17042983 : case TRUTH_ANDIF_EXPR:
12829 17042983 : tmp = boolean_true_node;
12830 17042983 : goto truth;
12831 6770590 : case TRUTH_OR_EXPR:
12832 6770590 : case TRUTH_ORIF_EXPR:
12833 6770590 : tmp = boolean_false_node;
12834 23813573 : truth:
12835 23813573 : {
12836 23813573 : tree op0 = TREE_OPERAND (t, 0);
12837 23813573 : tree op1 = TREE_OPERAND (t, 1);
12838 23813573 : if (!RECUR (op0, rval))
12839 : return false;
12840 16292160 : if (!(flags & tf_error) && RECUR (op1, rval))
12841 : /* When quiet, try to avoid expensive trial evaluation by first
12842 : checking potentiality of the second operand. */
12843 : return true;
12844 1715175 : if (!processing_template_decl)
12845 1324020 : op0 = cxx_eval_outermost_constant_expr (op0, true);
12846 1715175 : if (tree_int_cst_equal (op0, tmp))
12847 5143 : return (flags & tf_error) ? RECUR (op1, rval) : false;
12848 : else
12849 : return true;
12850 : }
12851 :
12852 : case PLUS_EXPR:
12853 : case MULT_EXPR:
12854 : case POINTER_PLUS_EXPR:
12855 : case RDIV_EXPR:
12856 : case EXACT_DIV_EXPR:
12857 : case MIN_EXPR:
12858 : case MAX_EXPR:
12859 : case LSHIFT_EXPR:
12860 : case RSHIFT_EXPR:
12861 : case LROTATE_EXPR:
12862 : case RROTATE_EXPR:
12863 : case BIT_IOR_EXPR:
12864 : case BIT_XOR_EXPR:
12865 : case BIT_AND_EXPR:
12866 : case TRUTH_XOR_EXPR:
12867 : case UNORDERED_EXPR:
12868 : case ORDERED_EXPR:
12869 : case UNLT_EXPR:
12870 : case UNLE_EXPR:
12871 : case UNGT_EXPR:
12872 : case UNGE_EXPR:
12873 : case UNEQ_EXPR:
12874 : case LTGT_EXPR:
12875 : case RANGE_EXPR:
12876 : case COMPLEX_EXPR:
12877 218791714 : want_rval = true;
12878 : /* Fall through. */
12879 218791714 : case ARRAY_REF:
12880 218791714 : case ARRAY_RANGE_REF:
12881 218791714 : case MEMBER_REF:
12882 218791714 : case DOTSTAR_EXPR:
12883 218791714 : case MEM_REF:
12884 218791714 : case BINARY_LEFT_FOLD_EXPR:
12885 218791714 : case BINARY_RIGHT_FOLD_EXPR:
12886 218791714 : binary:
12887 483285827 : for (i = 0; i < 2; ++i)
12888 357462608 : if (!RECUR (TREE_OPERAND (t, i), want_rval))
12889 : return false;
12890 : return true;
12891 :
12892 : case VEC_PERM_EXPR:
12893 106204 : for (i = 0; i < 3; ++i)
12894 105806 : if (!RECUR (TREE_OPERAND (t, i), true))
12895 : return false;
12896 : return true;
12897 :
12898 6154257 : case COND_EXPR:
12899 6154257 : if (COND_EXPR_IS_VEC_DELETE (t) && cxx_dialect < cxx20)
12900 : {
12901 8 : if (flags & tf_error)
12902 2 : constexpr_error (loc, fundef_p, "%<delete[]%> is not a "
12903 : "constant expression");
12904 8 : return false;
12905 : }
12906 : /* Fall through. */
12907 15695028 : case IF_STMT:
12908 15695028 : case VEC_COND_EXPR:
12909 : /* If the condition is a known constant, we know which of the legs we
12910 : care about; otherwise we only require that the condition and
12911 : either of the legs be potentially constant. */
12912 15695028 : tmp = TREE_OPERAND (t, 0);
12913 15695028 : if (!RECUR (tmp, rval))
12914 : return false;
12915 14187700 : if (!processing_template_decl)
12916 12284047 : tmp = cxx_eval_outermost_constant_expr (tmp, true);
12917 : /* potential_constant_expression* isn't told if it is called for
12918 : manifestly_const_eval or not, so for consteval if always
12919 : process both branches as if the condition is not a known
12920 : constant. */
12921 14187700 : if (TREE_CODE (t) != IF_STMT || !IF_STMT_CONSTEVAL_P (t))
12922 : {
12923 14154394 : if (integer_zerop (tmp))
12924 2991436 : return RECUR (TREE_OPERAND (t, 2), want_rval);
12925 11162958 : else if (TREE_CODE (tmp) == INTEGER_CST)
12926 3468120 : return RECUR (TREE_OPERAND (t, 1), want_rval);
12927 : }
12928 7728144 : tmp = *jump_target;
12929 8853338 : for (i = 1; i < 3; ++i)
12930 : {
12931 8781190 : tree this_jump_target = tmp;
12932 8781190 : if (potential_constant_expression_1 (TREE_OPERAND (t, i),
12933 : want_rval, strict, now, fundef_p,
12934 : tf_none, &this_jump_target))
12935 : {
12936 7820706 : if (returns (&this_jump_target) || throws (&this_jump_target))
12937 2203706 : *jump_target = this_jump_target;
12938 5452290 : else if (!returns (jump_target) && !throws (jump_target))
12939 : {
12940 5452290 : if (breaks (&this_jump_target)
12941 5452290 : || continues (&this_jump_target))
12942 42 : *jump_target = this_jump_target;
12943 5452290 : if (i == 1)
12944 : {
12945 : /* If the then branch is potentially constant, but
12946 : does not return, check if the else branch
12947 : couldn't return, break or continue. */
12948 4556672 : hash_set<tree> pset;
12949 4556672 : check_for_return_continue_data data = { &pset, NULL_TREE,
12950 : NULL_TREE,
12951 4556672 : false };
12952 9113344 : if (tree ret_expr
12953 4556672 : = cp_walk_tree (&TREE_OPERAND (t, 2),
12954 : check_for_return_continue, &data,
12955 : &pset))
12956 60400 : *jump_target = ret_expr;
12957 4496272 : else if (*jump_target == NULL_TREE)
12958 : {
12959 4496233 : if (data.continue_stmt)
12960 0 : *jump_target = data.continue_stmt;
12961 4496233 : else if (data.break_stmt)
12962 13 : *jump_target = data.break_stmt;
12963 : }
12964 4556672 : if (data.could_throw)
12965 22175 : *jump_target = void_node;
12966 4556672 : }
12967 : }
12968 7655996 : return true;
12969 : }
12970 : }
12971 72148 : if (flags & tf_error)
12972 : {
12973 3 : if (TREE_CODE (t) == IF_STMT)
12974 3 : constexpr_error (loc, fundef_p, "neither branch of %<if%> is a "
12975 : "constant expression");
12976 : else
12977 0 : constexpr_error (loc, fundef_p, "expression %qE is not a "
12978 : "constant expression", t);
12979 : }
12980 : return false;
12981 :
12982 1000 : case VEC_INIT_EXPR:
12983 1000 : if (VEC_INIT_EXPR_IS_CONSTEXPR (t))
12984 : return true;
12985 399 : if (flags & tf_error)
12986 : {
12987 3 : if (constexpr_error (loc, fundef_p, "non-constant array "
12988 : "initialization"))
12989 2 : diagnose_non_constexpr_vec_init (t);
12990 : }
12991 : return false;
12992 :
12993 : case TYPE_DECL:
12994 : case TAG_DEFN:
12995 : /* We can see these in statement-expressions. */
12996 : return true;
12997 :
12998 1331552 : case CLEANUP_STMT:
12999 1331552 : if (!RECUR (CLEANUP_BODY (t), any))
13000 : return false;
13001 1331029 : if (!CLEANUP_EH_ONLY (t) && !RECUR (CLEANUP_EXPR (t), any))
13002 : return false;
13003 : return true;
13004 :
13005 : case EMPTY_CLASS_EXPR:
13006 : return true;
13007 :
13008 27 : case GOTO_EXPR:
13009 27 : {
13010 27 : tree *target = &TREE_OPERAND (t, 0);
13011 : /* Gotos representing break, continue and cdtor return are OK. */
13012 27 : if (breaks (target) || continues (target) || returns (target))
13013 : {
13014 9 : *jump_target = *target;
13015 9 : return true;
13016 : }
13017 18 : if (TREE_CODE (*target) == LABEL_DECL && DECL_ARTIFICIAL (*target))
13018 : /* The user didn't write this goto, this isn't the problem. */
13019 : return true;
13020 16 : if (flags & tf_error)
13021 3 : constexpr_error (loc, fundef_p, "%<goto%> is not a constant "
13022 : "expression");
13023 : return false;
13024 : }
13025 :
13026 : case ASSERTION_STMT:
13027 : case PRECONDITION_STMT:
13028 : case POSTCONDITION_STMT:
13029 : /* Contracts are not supposed to alter this; we have to check that this
13030 : is not violated at a later time. */
13031 : return true;
13032 :
13033 223 : case LABEL_EXPR:
13034 223 : t = LABEL_EXPR_LABEL (t);
13035 223 : if (DECL_ARTIFICIAL (t) || cxx_dialect >= cxx23)
13036 : return true;
13037 25 : else if (flags & tf_error)
13038 5 : constexpr_error (loc, fundef_p, "label definition in %<constexpr%> "
13039 : "function only available with %<-std=c++23%> or "
13040 : "%<-std=gnu++23%>");
13041 : return false;
13042 :
13043 9799 : case ANNOTATE_EXPR:
13044 9799 : return RECUR (TREE_OPERAND (t, 0), rval);
13045 :
13046 129471 : case BIT_CAST_EXPR:
13047 129471 : return RECUR (TREE_OPERAND (t, 0), rval);
13048 :
13049 : /* Coroutine await, yield and return expressions are not. */
13050 718 : case CO_AWAIT_EXPR:
13051 718 : case CO_YIELD_EXPR:
13052 718 : case CO_RETURN_EXPR:
13053 718 : case TEMPLATE_FOR_STMT:
13054 718 : if (flags & tf_error)
13055 3 : constexpr_error (cp_expr_loc_or_loc (t, input_location), fundef_p,
13056 : "%qE is not a constant expression", t);
13057 : return false;
13058 :
13059 : /* Assume a TU-local entity is not constant, we'll error later when
13060 : instantiating. */
13061 : case TU_LOCAL_ENTITY:
13062 : return false;
13063 :
13064 : /* A splice expression is dependent, but will be constant after
13065 : substitution. */
13066 : case SPLICE_EXPR:
13067 : return true;
13068 :
13069 30 : case NONTYPE_ARGUMENT_PACK:
13070 30 : {
13071 30 : tree args = ARGUMENT_PACK_ARGS (t);
13072 30 : int len = TREE_VEC_LENGTH (args);
13073 90 : for (int i = 0; i < len; ++i)
13074 60 : if (!RECUR (TREE_VEC_ELT (args, i), any))
13075 : return false;
13076 : return true;
13077 : }
13078 :
13079 0 : default:
13080 0 : if (objc_non_constant_expr_p (t))
13081 : return false;
13082 :
13083 0 : sorry ("unexpected AST of kind %s", get_tree_code_name (TREE_CODE (t)));
13084 0 : gcc_unreachable ();
13085 : return false;
13086 : }
13087 : #undef RECUR
13088 4351964130 : }
13089 :
13090 : bool
13091 1590060483 : potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
13092 : bool fundef_p, tsubst_flags_t flags)
13093 : {
13094 1590060483 : if (flags & tf_error)
13095 : {
13096 : /* Check potentiality quietly first, as that could be performed more
13097 : efficiently in some cases (currently only for TRUTH_*_EXPR). If
13098 : that fails, replay the check noisily to give errors. */
13099 9979046 : flags &= ~tf_error;
13100 9979046 : if (potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13101 : flags))
13102 : return true;
13103 972 : flags |= tf_error;
13104 : }
13105 :
13106 1580082409 : tree target = NULL_TREE;
13107 1580082409 : return potential_constant_expression_1 (t, want_rval, strict, now, fundef_p,
13108 1580082409 : flags, &target);
13109 : }
13110 :
13111 : /* The main entry point to the above. */
13112 :
13113 : bool
13114 412011498 : potential_constant_expression (tree t)
13115 : {
13116 412011498 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13117 : /*now*/false, /*fundef_p*/false,
13118 412011498 : tf_none);
13119 : }
13120 :
13121 : /* As above, but require a constant rvalue. */
13122 :
13123 : bool
13124 32712634 : potential_rvalue_constant_expression (tree t)
13125 : {
13126 32712634 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13127 : /*now*/false, /*fundef_p*/false,
13128 32712634 : tf_none);
13129 : }
13130 :
13131 : /* Like above, but complain about non-constant expressions. */
13132 :
13133 : bool
13134 80 : require_potential_constant_expression (tree t)
13135 : {
13136 80 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13137 : /*now*/false, /*fundef_p*/false,
13138 80 : tf_warning_or_error);
13139 : }
13140 :
13141 : /* Cross product of the above. */
13142 :
13143 : bool
13144 126853 : require_potential_rvalue_constant_expression (tree t)
13145 : {
13146 126853 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13147 : /*now*/false, /*fundef_p*/false,
13148 126853 : tf_warning_or_error);
13149 : }
13150 :
13151 : /* Like require_potential_rvalue_constant_expression, but fundef_p is true. */
13152 :
13153 : bool
13154 252 : require_potential_rvalue_constant_expression_fncheck (tree t)
13155 : {
13156 252 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13157 : /*now*/false, /*fundef_p*/true,
13158 252 : tf_warning_or_error);
13159 : }
13160 :
13161 : /* Like above, but don't consider PARM_DECL a potential_constant_expression. */
13162 :
13163 : bool
13164 771 : require_rvalue_constant_expression (tree t)
13165 : {
13166 771 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13167 : /*now*/true, /*fundef_p*/false,
13168 771 : tf_warning_or_error);
13169 : }
13170 :
13171 : /* Like potential_constant_expression, but don't consider possible constexpr
13172 : substitution of the current function. That is, PARM_DECL qualifies under
13173 : potential_constant_expression, but not here.
13174 :
13175 : This is basically what you can check when any actual constant values might
13176 : be value-dependent. */
13177 :
13178 : bool
13179 864349917 : is_constant_expression (tree t)
13180 : {
13181 864349917 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13182 : /*now*/true, /*fundef_p*/false,
13183 864349917 : tf_none);
13184 : }
13185 :
13186 : /* As above, but expect an rvalue. */
13187 :
13188 : bool
13189 148021627 : is_rvalue_constant_expression (tree t)
13190 : {
13191 148021627 : return potential_constant_expression_1 (t, /*want_rval*/true, /*strict*/true,
13192 : /*now*/true, /*fundef_p*/false,
13193 148021627 : tf_none);
13194 : }
13195 :
13196 : /* Like above, but complain about non-constant expressions. */
13197 :
13198 : bool
13199 9851090 : require_constant_expression (tree t)
13200 : {
13201 9851090 : return potential_constant_expression_1 (t, /*want_rval*/false, /*strict*/true,
13202 : /*now*/true, /*fundef_p*/false,
13203 9851090 : tf_warning_or_error);
13204 : }
13205 :
13206 : /* Like is_constant_expression, but allow const variables that are not allowed
13207 : under constexpr rules. */
13208 :
13209 : bool
13210 113006715 : is_static_init_expression (tree t)
13211 : {
13212 113006715 : return potential_constant_expression_1 (t, /*want_rval*/false,
13213 : /*strict*/false, /*now*/true,
13214 113006715 : /*fundef_p*/false, tf_none);
13215 : }
13216 :
13217 : /* Returns true if T is a potential constant expression that is not
13218 : instantiation-dependent, and therefore a candidate for constant folding even
13219 : in a template. */
13220 :
13221 : bool
13222 834661663 : is_nondependent_constant_expression (tree t)
13223 : {
13224 834661663 : return (!type_unknown_p (t)
13225 834661614 : && is_constant_expression (t)
13226 1563698085 : && !instantiation_dependent_expression_p (t));
13227 : }
13228 :
13229 : /* Returns true if T is a potential static initializer expression that is not
13230 : instantiation-dependent. */
13231 :
13232 : bool
13233 113006715 : is_nondependent_static_init_expression (tree t)
13234 : {
13235 113006715 : return (!type_unknown_p (t)
13236 113006715 : && is_static_init_expression (t)
13237 217930922 : && !instantiation_dependent_expression_p (t));
13238 : }
13239 :
13240 : /* True iff FN is an implicitly constexpr function. */
13241 :
13242 : bool
13243 182554 : decl_implicit_constexpr_p (tree fn)
13244 : {
13245 182554 : if (!(flag_implicit_constexpr
13246 2 : && TREE_CODE (fn) == FUNCTION_DECL
13247 2 : && DECL_DECLARED_CONSTEXPR_P (fn)))
13248 : return false;
13249 :
13250 2 : if (DECL_CLONED_FUNCTION_P (fn))
13251 0 : fn = DECL_CLONED_FUNCTION (fn);
13252 :
13253 2 : return (DECL_LANG_SPECIFIC (fn)
13254 2 : && DECL_LANG_SPECIFIC (fn)->u.fn.implicit_constexpr);
13255 : }
13256 :
13257 : /* Finalize constexpr processing after parsing. */
13258 :
13259 : void
13260 96776 : fini_constexpr (void)
13261 : {
13262 : /* The contexpr call and fundef copies tables are no longer needed. */
13263 96776 : constexpr_call_table = NULL;
13264 96776 : fundef_copies_table = NULL;
13265 96776 : }
13266 :
13267 : #include "gt-cp-constexpr.h"
|